]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs4.patch
- require latest geninitrd with ext4 changes
[packages/kernel.git] / kernel-aufs4.patch
CommitLineData
2121bcd9 1SPDX-License-Identifier: GPL-2.0
ffa93bbd 2aufs4.x-rcN kbuild patch
7f207e10
AM
3
4diff --git a/fs/Kconfig b/fs/Kconfig
be118d29 5index bc821a8..7ae814c 100644
7f207e10
AM
6--- a/fs/Kconfig
7+++ b/fs/Kconfig
be118d29 8@@ -251,6 +251,7 @@ source "fs/pstore/Kconfig"
5527c038 9 source "fs/sysv/Kconfig"
7e9cd9fe 10 source "fs/ufs/Kconfig"
7f207e10
AM
11 source "fs/exofs/Kconfig"
12+source "fs/aufs/Kconfig"
13
14 endif # MISC_FILESYSTEMS
15
16diff --git a/fs/Makefile b/fs/Makefile
be118d29 17index add789e..26100d6 100644
7f207e10
AM
18--- a/fs/Makefile
19+++ b/fs/Makefile
be118d29 20@@ -128,3 +128,4 @@ obj-y += exofs/ # Multiple modules
7f207e10 21 obj-$(CONFIG_CEPH_FS) += ceph/
bf0370f2 22 obj-$(CONFIG_PSTORE) += pstore/
c06a8ce3 23 obj-$(CONFIG_EFIVAR_FS) += efivarfs/
86dc4139 24+obj-$(CONFIG_AUFS_FS) += aufs/
2121bcd9 25SPDX-License-Identifier: GPL-2.0
ffa93bbd 26aufs4.x-rcN base patch
7f207e10 27
c1595e42 28diff --git a/MAINTAINERS b/MAINTAINERS
be118d29 29index 4623caf..7617c0e 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
be118d29 32@@ -2484,6 +2484,19 @@ F: include/linux/audit.h
c1595e42
JR
33 F: include/uapi/linux/audit.h
34 F: kernel/audit*
35
36+AUFS (advanced multi layered unification filesystem) FILESYSTEM
37+M: "J. R. Okajima" <hooanon05g@gmail.com>
38+L: linux-unionfs@vger.kernel.org
39+L: aufs-users@lists.sourceforge.net (members only)
40+W: http://aufs.sourceforge.net
5527c038 41+T: git://github.com/sfjro/aufs4-linux.git
c1595e42
JR
42+S: Supported
43+F: Documentation/filesystems/aufs/
44+F: Documentation/ABI/testing/debugfs-aufs
45+F: Documentation/ABI/testing/sysfs-aufs
46+F: fs/aufs/
47+F: include/uapi/linux/aufs_type.h
48+
49 AUXILIARY DISPLAY DRIVERS
50 M: Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>
51 W: http://miguelojeda.es/auxdisplay.htm
392086de 52diff --git a/drivers/block/loop.c b/drivers/block/loop.c
be118d29 53index 87855b5..962b2d6 100644
392086de
AM
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
2121bcd9 56@@ -691,6 +691,24 @@ static inline int is_loop_device(struct file *file)
392086de
AM
57 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
58 }
59
60+/*
61+ * for AUFS
62+ * no get/put for file.
63+ */
64+struct file *loop_backing_file(struct super_block *sb)
65+{
66+ struct file *ret;
67+ struct loop_device *l;
68+
69+ ret = NULL;
70+ if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
71+ l = sb->s_bdev->bd_disk->private_data;
72+ ret = l->lo_backing_file;
73+ }
74+ return ret;
75+}
febd17d6 76+EXPORT_SYMBOL_GPL(loop_backing_file);
392086de
AM
77+
78 /* loop sysfs attributes */
79
80 static ssize_t loop_attr_show(struct device *dev, char *page,
c1595e42 81diff --git a/fs/dcache.c b/fs/dcache.c
be118d29 82index 7c38f39..9ce5e26 100644
c1595e42
JR
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
be118d29 85@@ -1200,7 +1200,7 @@ enum d_walk_ret {
c1595e42
JR
86 *
87 * The @enter() and @finish() callbacks are called with d_lock held.
88 */
89-static void d_walk(struct dentry *parent, void *data,
90+void d_walk(struct dentry *parent, void *data,
91 enum d_walk_ret (*enter)(void *, struct dentry *),
92 void (*finish)(void *))
93 {
febd17d6 94diff --git a/fs/fcntl.c b/fs/fcntl.c
be118d29 95index 1e97f1f..8cd01f7 100644
febd17d6
JR
96--- a/fs/fcntl.c
97+++ b/fs/fcntl.c
2121bcd9 98@@ -32,7 +32,7 @@
febd17d6
JR
99
100 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101
102-static int setfl(int fd, struct file * filp, unsigned long arg)
103+int setfl(int fd, struct file * filp, unsigned long arg)
104 {
105 struct inode * inode = file_inode(filp);
106 int error = 0;
2121bcd9 107@@ -63,6 +63,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
febd17d6
JR
108
109 if (filp->f_op->check_flags)
110 error = filp->f_op->check_flags(arg);
111+ if (!error && filp->f_op->setfl)
112+ error = filp->f_op->setfl(filp, arg);
113 if (error)
114 return error;
115
5afbbe0d 116diff --git a/fs/inode.c b/fs/inode.c
be118d29 117index ef36236..929a5a3 100644
5afbbe0d
AM
118--- a/fs/inode.c
119+++ b/fs/inode.c
be118d29 120@@ -1659,7 +1659,7 @@ EXPORT_SYMBOL(generic_update_time);
5afbbe0d
AM
121 * This does the actual work of updating an inodes time or version. Must have
122 * had called mnt_want_write() before calling this.
123 */
124-static int update_time(struct inode *inode, struct timespec *time, int flags)
125+int update_time(struct inode *inode, struct timespec *time, int flags)
126 {
127 int (*update_time)(struct inode *, struct timespec *, int);
128
8b6a4947 129diff --git a/fs/namespace.c b/fs/namespace.c
be118d29 130index 9d1374a..26ef600 100644
8b6a4947
AM
131--- a/fs/namespace.c
132+++ b/fs/namespace.c
133@@ -846,6 +846,12 @@ static inline int check_mnt(struct mount *mnt)
134 return mnt->mnt_ns == current->nsproxy->mnt_ns;
135 }
136
137+/* for aufs, CONFIG_AUFS_BR_FUSE */
138+int is_current_mnt_ns(struct vfsmount *mnt)
139+{
140+ return check_mnt(real_mount(mnt));
141+}
142+
143 /*
144 * vfsmount lock must be held for write
145 */
5527c038 146diff --git a/fs/read_write.c b/fs/read_write.c
2121bcd9 147index f8547b8..0a5c47b 100644
5527c038
JR
148--- a/fs/read_write.c
149+++ b/fs/read_write.c
2121bcd9 150@@ -484,6 +484,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
8b6a4947 151 return -EINVAL;
5527c038 152 }
5527c038
JR
153
154+vfs_readf_t vfs_readf(struct file *file)
155+{
156+ const struct file_operations *fop = file->f_op;
157+
158+ if (fop->read)
159+ return fop->read;
160+ if (fop->read_iter)
161+ return new_sync_read;
162+ return ERR_PTR(-ENOSYS);
163+}
164+
165+vfs_writef_t vfs_writef(struct file *file)
166+{
167+ const struct file_operations *fop = file->f_op;
168+
169+ if (fop->write)
170+ return fop->write;
171+ if (fop->write_iter)
172+ return new_sync_write;
173+ return ERR_PTR(-ENOSYS);
174+}
175+
8b6a4947 176 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
5527c038
JR
177 {
178 mm_segment_t old_fs;
7f207e10 179diff --git a/fs/splice.c b/fs/splice.c
2121bcd9 180index 39e2dc0..c5fb195 100644
7f207e10
AM
181--- a/fs/splice.c
182+++ b/fs/splice.c
8b6a4947 183@@ -837,8 +837,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
7f207e10
AM
184 /*
185 * Attempt to initiate a splice from pipe to file.
186 */
187-static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
188- loff_t *ppos, size_t len, unsigned int flags)
189+long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
190+ loff_t *ppos, size_t len, unsigned int flags)
191 {
192 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
193 loff_t *, size_t, unsigned int);
8b6a4947 194@@ -854,9 +854,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
7f207e10
AM
195 /*
196 * Attempt to initiate a splice from a file to a pipe.
197 */
198-static long do_splice_to(struct file *in, loff_t *ppos,
199- struct pipe_inode_info *pipe, size_t len,
200- unsigned int flags)
201+long do_splice_to(struct file *in, loff_t *ppos,
202+ struct pipe_inode_info *pipe, size_t len,
203+ unsigned int flags)
204 {
205 ssize_t (*splice_read)(struct file *, loff_t *,
206 struct pipe_inode_info *, size_t, unsigned int);
a2654f78 207diff --git a/fs/sync.c b/fs/sync.c
2121bcd9 208index 6e0a2cb..a6891ee 100644
a2654f78
AM
209--- a/fs/sync.c
210+++ b/fs/sync.c
2121bcd9 211@@ -28,7 +28,7 @@
a2654f78
AM
212 * wait == 1 case since in that case write_inode() functions do
213 * sync_dirty_buffer() and thus effectively write one block at a time.
214 */
215-static int __sync_filesystem(struct super_block *sb, int wait)
216+int __sync_filesystem(struct super_block *sb, int wait)
217 {
218 if (wait)
219 sync_inodes_sb(sb);
b912730e 220diff --git a/include/linux/file.h b/include/linux/file.h
2121bcd9 221index 279720d..76e38ea 100644
b912730e
AM
222--- a/include/linux/file.h
223+++ b/include/linux/file.h
2121bcd9 224@@ -20,6 +20,7 @@ struct dentry;
b912730e 225 struct path;
a2654f78 226 extern struct file *alloc_file(const struct path *, fmode_t mode,
b912730e
AM
227 const struct file_operations *fop);
228+extern struct file *get_empty_filp(void);
229
230 static inline void fput_light(struct file *file, int fput_needed)
231 {
5527c038 232diff --git a/include/linux/fs.h b/include/linux/fs.h
be118d29 233index 79c41398..383ab06 100644
5527c038
JR
234--- a/include/linux/fs.h
235+++ b/include/linux/fs.h
be118d29 236@@ -1270,6 +1270,7 @@ extern void fasync_free(struct fasync_struct *);
febd17d6
JR
237 /* can be called from interrupts */
238 extern void kill_fasync(struct fasync_struct **, int, int);
239
240+extern int setfl(int fd, struct file * filp, unsigned long arg);
241 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
3c1bdaff 242 extern int f_setown(struct file *filp, unsigned long arg, int force);
febd17d6 243 extern void f_delown(struct file *filp);
be118d29 244@@ -1721,6 +1722,7 @@ struct file_operations {
febd17d6
JR
245 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
246 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
247 int (*check_flags)(int);
248+ int (*setfl)(struct file *, unsigned long);
249 int (*flock) (struct file *, int, struct file_lock *);
250 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
251 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
be118d29 252@@ -1791,6 +1793,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
5527c038
JR
253 struct iovec *fast_pointer,
254 struct iovec **ret_pointer);
255
256+typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
257+typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
258+ loff_t *);
259+vfs_readf_t vfs_readf(struct file *file);
260+vfs_writef_t vfs_writef(struct file *file);
261+
262 extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
5527c038 263 extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
8b6a4947 264 extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
be118d29 265@@ -2195,6 +2203,7 @@ extern int current_umask(void);
5afbbe0d
AM
266 extern void ihold(struct inode * inode);
267 extern void iput(struct inode *);
268 extern int generic_update_time(struct inode *, struct timespec *, int);
269+extern int update_time(struct inode *, struct timespec *, int);
270
271 /* /sys/fs */
272 extern struct kobject *fs_kobj;
be118d29 273@@ -2475,6 +2484,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
a2654f78
AM
274 return false;
275 }
276 #endif
277+extern int __sync_filesystem(struct super_block *, int);
278 extern int sync_filesystem(struct super_block *);
279 extern const struct file_operations def_blk_fops;
280 extern const struct file_operations def_chr_fops;
8b6a4947 281diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
be118d29 282index 6fc77d4..27e76f0 100644
8b6a4947
AM
283--- a/include/linux/lockdep.h
284+++ b/include/linux/lockdep.h
be118d29 285@@ -313,6 +313,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
8b6a4947
AM
286 return lock->key == key;
287 }
288
289+struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
290+
291 /*
292 * Acquire a lock.
293 *
be118d29 294@@ -439,6 +441,7 @@ struct lockdep_map { };
8b6a4947
AM
295
296 #define lockdep_depth(tsk) (0)
297
298+#define lockdep_is_held(lock) (1)
299 #define lockdep_is_held_type(l, r) (1)
300
301 #define lockdep_assert_held(l) do { (void)(l); } while (0)
302diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
2121bcd9 303index 3594208..24f5fd1 100644
8b6a4947
AM
304--- a/include/linux/mnt_namespace.h
305+++ b/include/linux/mnt_namespace.h
2121bcd9 306@@ -6,11 +6,14 @@
8b6a4947
AM
307 struct mnt_namespace;
308 struct fs_struct;
309 struct user_namespace;
310+struct vfsmount;
311
312 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
313 struct user_namespace *, struct fs_struct *);
314 extern void put_mnt_ns(struct mnt_namespace *ns);
315
316+extern int is_current_mnt_ns(struct vfsmount *mnt);
317+
318 extern const struct file_operations proc_mounts_operations;
319 extern const struct file_operations proc_mountinfo_operations;
320 extern const struct file_operations proc_mountstats_operations;
1e00d052 321diff --git a/include/linux/splice.h b/include/linux/splice.h
2121bcd9 322index 74b4911..19789fb 100644
1e00d052
AM
323--- a/include/linux/splice.h
324+++ b/include/linux/splice.h
2121bcd9 325@@ -87,4 +87,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
4b3da204
AM
326
327 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
106341ce 328 extern const struct pipe_buf_operations default_pipe_buf_ops;
1e00d052
AM
329+
330+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
331+ loff_t *ppos, size_t len, unsigned int flags);
332+extern long do_splice_to(struct file *in, loff_t *ppos,
333+ struct pipe_inode_info *pipe, size_t len,
334+ unsigned int flags);
335 #endif
8b6a4947 336diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
be118d29 337index 89b5f83..0dca42f 100644
8b6a4947
AM
338--- a/kernel/locking/lockdep.c
339+++ b/kernel/locking/lockdep.c
be118d29 340@@ -140,7 +140,7 @@ static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
8b6a4947
AM
341 unsigned long nr_lock_classes;
342 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
343
344-static inline struct lock_class *hlock_class(struct held_lock *hlock)
345+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
346 {
347 if (!hlock->class_idx) {
348 /*
be118d29 349@@ -151,6 +151,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
8b6a4947
AM
350 }
351 return lock_classes + hlock->class_idx - 1;
352 }
353+#define hlock_class(hlock) lockdep_hlock_class(hlock)
354
355 #ifdef CONFIG_LOCK_STAT
356 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
2121bcd9 357SPDX-License-Identifier: GPL-2.0
ffa93bbd 358aufs4.x-rcN mmap patch
fb47a38f 359
c1595e42 360diff --git a/fs/proc/base.c b/fs/proc/base.c
be118d29 361index 9298324..da5bf4f9 100644
c1595e42
JR
362--- a/fs/proc/base.c
363+++ b/fs/proc/base.c
be118d29 364@@ -2014,7 +2014,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
c1595e42
JR
365 down_read(&mm->mmap_sem);
366 vma = find_exact_vma(mm, vm_start, vm_end);
367 if (vma && vma->vm_file) {
368- *path = vma->vm_file->f_path;
369+ *path = vma_pr_or_file(vma)->f_path;
370 path_get(path);
371 rc = 0;
372 }
fb47a38f 373diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
a2654f78 374index 7563437..7c0dc0f 100644
fb47a38f
JR
375--- a/fs/proc/nommu.c
376+++ b/fs/proc/nommu.c
076b876e 377@@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
fb47a38f
JR
378 file = region->vm_file;
379
380 if (file) {
381- struct inode *inode = file_inode(region->vm_file);
382+ struct inode *inode;
076b876e 383+
fb47a38f
JR
384+ file = vmr_pr_or_file(region);
385+ inode = file_inode(file);
386 dev = inode->i_sb->s_dev;
387 ino = inode->i_ino;
388 }
389diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
be118d29 390index ec6d298..34c7193 100644
fb47a38f
JR
391--- a/fs/proc/task_mmu.c
392+++ b/fs/proc/task_mmu.c
be118d29 393@@ -311,7 +311,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
fb47a38f
JR
394 const char *name = NULL;
395
396 if (file) {
397- struct inode *inode = file_inode(vma->vm_file);
398+ struct inode *inode;
076b876e 399+
fb47a38f
JR
400+ file = vma_pr_or_file(vma);
401+ inode = file_inode(file);
402 dev = inode->i_sb->s_dev;
403 ino = inode->i_ino;
404 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
be118d29 405@@ -1741,7 +1744,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
076b876e
AM
406 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
407 struct vm_area_struct *vma = v;
408 struct numa_maps *md = &numa_priv->md;
409- struct file *file = vma->vm_file;
410+ struct file *file = vma_pr_or_file(vma);
076b876e 411 struct mm_struct *mm = vma->vm_mm;
7e9cd9fe
AM
412 struct mm_walk walk = {
413 .hugetlb_entry = gather_hugetlb_stats,
fb47a38f 414diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
2121bcd9 415index 5b62f57..dfb4a3b 100644
fb47a38f
JR
416--- a/fs/proc/task_nommu.c
417+++ b/fs/proc/task_nommu.c
2121bcd9 418@@ -156,7 +156,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
fb47a38f
JR
419 file = vma->vm_file;
420
421 if (file) {
422- struct inode *inode = file_inode(vma->vm_file);
423+ struct inode *inode;
076b876e 424+
b912730e 425+ file = vma_pr_or_file(vma);
fb47a38f
JR
426+ inode = file_inode(file);
427 dev = inode->i_sb->s_dev;
428 ino = inode->i_ino;
429 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
430diff --git a/include/linux/mm.h b/include/linux/mm.h
be118d29 431index ad06d42..75e5d37 100644
fb47a38f
JR
432--- a/include/linux/mm.h
433+++ b/include/linux/mm.h
be118d29 434@@ -1368,6 +1368,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
a3232e84 435 unmap_mapping_range(mapping, holebegin, holelen, 0);
fb47a38f 436 }
fb47a38f 437
076b876e
AM
438+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
439+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
f2c43d5f 440+ int);
076b876e
AM
441+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
442+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
fb47a38f 443+
f2c43d5f
AM
444+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
445+ __LINE__)
446+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
447+ __LINE__)
448+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
449+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
b912730e
AM
450+
451+#ifndef CONFIG_MMU
076b876e
AM
452+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
453+extern void vmr_do_fput(struct vm_region *, const char[], int);
454+
f2c43d5f
AM
455+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
456+ __LINE__)
457+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
b912730e 458+#endif /* !CONFIG_MMU */
fb47a38f 459+
a3232e84
JR
460 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
461 void *buf, int len, unsigned int gup_flags);
fb47a38f 462 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
fb47a38f 463diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
be118d29 464index fd1af6b..89ec438 100644
fb47a38f
JR
465--- a/include/linux/mm_types.h
466+++ b/include/linux/mm_types.h
be118d29 467@@ -249,6 +249,7 @@ struct vm_region {
fb47a38f
JR
468 unsigned long vm_top; /* region allocated to here */
469 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
470 struct file *vm_file; /* the backing file or NULL */
471+ struct file *vm_prfile; /* the virtual backing file or NULL */
472
473 int vm_usage; /* region usage count (access under nommu_region_sem) */
474 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
be118d29 475@@ -323,6 +324,7 @@ struct vm_area_struct {
fb47a38f 476 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
5afbbe0d 477 units */
fb47a38f
JR
478 struct file * vm_file; /* File we map to (can be NULL). */
479+ struct file *vm_prfile; /* shadow of vm_file */
480 void * vm_private_data; /* was vm_pte (shared mem) */
481
8b6a4947 482 atomic_long_t swap_readahead_info;
fb47a38f 483diff --git a/kernel/fork.c b/kernel/fork.c
be118d29 484index e5d9d40..f6f6fbf 100644
fb47a38f
JR
485--- a/kernel/fork.c
486+++ b/kernel/fork.c
be118d29 487@@ -474,7 +474,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
fb47a38f
JR
488 struct inode *inode = file_inode(file);
489 struct address_space *mapping = file->f_mapping;
490
491- get_file(file);
492+ vma_get_file(tmp);
493 if (tmp->vm_flags & VM_DENYWRITE)
494 atomic_dec(&inode->i_writecount);
2000de60 495 i_mmap_lock_write(mapping);
076b876e 496diff --git a/mm/Makefile b/mm/Makefile
2121bcd9 497index e669f02..9c36567 100644
076b876e
AM
498--- a/mm/Makefile
499+++ b/mm/Makefile
521ced18 500@@ -39,7 +39,7 @@ obj-y := filemap.o mempool.o oom_kill.o \
076b876e 501 mm_init.o mmu_context.o percpu.o slab_common.o \
521ced18 502 compaction.o vmacache.o swap_slots.o \
076b876e 503 interval_tree.o list_lru.o workingset.o \
7e9cd9fe
AM
504- debug.o $(mmu-y)
505+ prfile.o debug.o $(mmu-y)
076b876e
AM
506
507 obj-y += init-mm.o
508
fb47a38f 509diff --git a/mm/filemap.c b/mm/filemap.c
be118d29 510index 693f622..ea46048 100644
fb47a38f
JR
511--- a/mm/filemap.c
512+++ b/mm/filemap.c
be118d29 513@@ -2703,7 +2703,7 @@ int filemap_page_mkwrite(struct vm_fault *vmf)
fb47a38f
JR
514 int ret = VM_FAULT_LOCKED;
515
516 sb_start_pagefault(inode->i_sb);
521ced18
JR
517- file_update_time(vmf->vma->vm_file);
518+ vma_file_update_time(vmf->vma);
fb47a38f
JR
519 lock_page(page);
520 if (page->mapping != inode->i_mapping) {
521 unlock_page(page);
fb47a38f 522diff --git a/mm/mmap.c b/mm/mmap.c
be118d29 523index 9efdc021..d77f01f 100644
fb47a38f
JR
524--- a/mm/mmap.c
525+++ b/mm/mmap.c
8b6a4947 526@@ -171,7 +171,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
fb47a38f
JR
527 if (vma->vm_ops && vma->vm_ops->close)
528 vma->vm_ops->close(vma);
529 if (vma->vm_file)
530- fput(vma->vm_file);
531+ vma_fput(vma);
532 mpol_put(vma_policy(vma));
533 kmem_cache_free(vm_area_cachep, vma);
534 return next;
8b6a4947 535@@ -896,7 +896,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
fb47a38f
JR
536 if (remove_next) {
537 if (file) {
538 uprobe_munmap(next, next->vm_start, next->vm_end);
539- fput(file);
540+ vma_fput(vma);
541 }
542 if (next->anon_vma)
543 anon_vma_merge(vma, next);
2121bcd9 544@@ -1761,8 +1761,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
35939ee7
JR
545 return addr;
546
fb47a38f 547 unmap_and_free_vma:
fb47a38f
JR
548+ vma_fput(vma);
549 vma->vm_file = NULL;
550- fput(file);
551
552 /* Undo any partial mapping done by a device driver. */
553 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
2121bcd9 554@@ -2586,7 +2586,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
555 goto out_free_mpol;
556
557 if (new->vm_file)
558- get_file(new->vm_file);
559+ vma_get_file(new);
560
561 if (new->vm_ops && new->vm_ops->open)
562 new->vm_ops->open(new);
2121bcd9 563@@ -2605,7 +2605,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
564 if (new->vm_ops && new->vm_ops->close)
565 new->vm_ops->close(new);
566 if (new->vm_file)
567- fput(new->vm_file);
568+ vma_fput(new);
569 unlink_anon_vmas(new);
570 out_free_mpol:
571 mpol_put(vma_policy(new));
2121bcd9 572@@ -2767,7 +2767,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
7e9cd9fe
AM
573 struct vm_area_struct *vma;
574 unsigned long populate = 0;
575 unsigned long ret = -EINVAL;
576- struct file *file;
5afbbe0d 577+ struct file *file, *prfile;
7e9cd9fe 578
5afbbe0d
AM
579 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
580 current->comm, current->pid);
2121bcd9 581@@ -2842,10 +2842,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
febd17d6 582 }
7e9cd9fe
AM
583 }
584
585- file = get_file(vma->vm_file);
586+ vma_get_file(vma);
5afbbe0d
AM
587+ file = vma->vm_file;
588+ prfile = vma->vm_prfile;
7e9cd9fe 589 ret = do_mmap_pgoff(vma->vm_file, start, size,
521ced18 590 prot, flags, pgoff, &populate, NULL);
5afbbe0d
AM
591+ if (!IS_ERR_VALUE(ret) && file && prfile) {
592+ struct vm_area_struct *new_vma;
593+
594+ new_vma = find_vma(mm, ret);
595+ if (!new_vma->vm_prfile)
596+ new_vma->vm_prfile = prfile;
597+ if (new_vma != vma)
598+ get_file(prfile);
599+ }
600+ /*
601+ * two fput()s instead of vma_fput(vma),
602+ * coz vma may not be available anymore.
603+ */
604 fput(file);
605+ if (prfile)
606+ fput(prfile);
7e9cd9fe
AM
607 out:
608 up_write(&mm->mmap_sem);
609 if (populate)
2121bcd9 610@@ -3153,7 +3170,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
79b8bda9
AM
611 if (anon_vma_clone(new_vma, vma))
612 goto out_free_mempol;
613 if (new_vma->vm_file)
614- get_file(new_vma->vm_file);
615+ vma_get_file(new_vma);
616 if (new_vma->vm_ops && new_vma->vm_ops->open)
617 new_vma->vm_ops->open(new_vma);
618 vma_link(mm, new_vma, prev, rb_link, rb_parent);
fb47a38f 619diff --git a/mm/nommu.c b/mm/nommu.c
be118d29 620index ebb6e61..8cf2428 100644
fb47a38f
JR
621--- a/mm/nommu.c
622+++ b/mm/nommu.c
ffa93bbd 623@@ -641,7 +641,7 @@ static void __put_nommu_region(struct vm_region *region)
fb47a38f
JR
624 up_write(&nommu_region_sem);
625
626 if (region->vm_file)
627- fput(region->vm_file);
628+ vmr_fput(region);
629
630 /* IO memory and memory shared directly out of the pagecache
631 * from ramfs/tmpfs mustn't be released here */
ffa93bbd 632@@ -799,7 +799,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
fb47a38f
JR
633 if (vma->vm_ops && vma->vm_ops->close)
634 vma->vm_ops->close(vma);
635 if (vma->vm_file)
636- fput(vma->vm_file);
637+ vma_fput(vma);
638 put_nommu_region(vma->vm_region);
639 kmem_cache_free(vm_area_cachep, vma);
640 }
8b6a4947 641@@ -1321,7 +1321,7 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
642 goto error_just_free;
643 }
644 }
645- fput(region->vm_file);
646+ vmr_fput(region);
647 kmem_cache_free(vm_region_jar, region);
648 region = pregion;
649 result = start;
8b6a4947 650@@ -1396,10 +1396,10 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
651 up_write(&nommu_region_sem);
652 error:
653 if (region->vm_file)
654- fput(region->vm_file);
655+ vmr_fput(region);
656 kmem_cache_free(vm_region_jar, region);
657 if (vma->vm_file)
658- fput(vma->vm_file);
659+ vma_fput(vma);
660 kmem_cache_free(vm_area_cachep, vma);
fb47a38f 661 return ret;
c2c0f25c 662
076b876e
AM
663diff --git a/mm/prfile.c b/mm/prfile.c
664new file mode 100644
2121bcd9 665index 0000000..3f56669
076b876e
AM
666--- /dev/null
667+++ b/mm/prfile.c
2121bcd9 668@@ -0,0 +1,86 @@
076b876e 669+/*
2121bcd9 670+ * SPDX-License-Identifier: GPL-2.0
1c60b727
AM
671+ * Mainly for aufs which mmap(2) different file and wants to print different
672+ * path in /proc/PID/maps.
076b876e
AM
673+ * Call these functions via macros defined in linux/mm.h.
674+ *
675+ * See Documentation/filesystems/aufs/design/06mmap.txt
676+ *
1c60b727 677+ * Copyright (c) 2014-2017 Junjro R. Okajima
076b876e
AM
678+ * Copyright (c) 2014 Ian Campbell
679+ */
680+
681+#include <linux/mm.h>
682+#include <linux/file.h>
683+#include <linux/fs.h>
684+
685+/* #define PRFILE_TRACE */
686+static inline void prfile_trace(struct file *f, struct file *pr,
687+ const char func[], int line, const char func2[])
688+{
689+#ifdef PRFILE_TRACE
690+ if (pr)
1c60b727 691+ pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
076b876e
AM
692+#endif
693+}
694+
076b876e
AM
695+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
696+ int line)
697+{
698+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
699+
700+ prfile_trace(f, pr, func, line, __func__);
701+ file_update_time(f);
702+ if (f && pr)
703+ file_update_time(pr);
704+}
705+
706+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
707+ int line)
708+{
709+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
710+
711+ prfile_trace(f, pr, func, line, __func__);
712+ return (f && pr) ? pr : f;
713+}
714+
715+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
716+{
717+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
718+
719+ prfile_trace(f, pr, func, line, __func__);
720+ get_file(f);
721+ if (f && pr)
722+ get_file(pr);
723+}
724+
725+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
726+{
727+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
728+
729+ prfile_trace(f, pr, func, line, __func__);
730+ fput(f);
731+ if (f && pr)
732+ fput(pr);
733+}
b912730e
AM
734+
735+#ifndef CONFIG_MMU
076b876e
AM
736+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
737+ int line)
738+{
739+ struct file *f = region->vm_file, *pr = region->vm_prfile;
740+
741+ prfile_trace(f, pr, func, line, __func__);
742+ return (f && pr) ? pr : f;
743+}
744+
745+void vmr_do_fput(struct vm_region *region, const char func[], int line)
746+{
747+ struct file *f = region->vm_file, *pr = region->vm_prfile;
748+
749+ prfile_trace(f, pr, func, line, __func__);
750+ fput(f);
751+ if (f && pr)
752+ fput(pr);
753+}
b912730e 754+#endif /* !CONFIG_MMU */
2121bcd9 755SPDX-License-Identifier: GPL-2.0
ffa93bbd 756aufs4.x-rcN standalone patch
7f207e10 757
c1595e42 758diff --git a/fs/dcache.c b/fs/dcache.c
be118d29 759index 9ce5e26..0f59369 100644
c1595e42
JR
760--- a/fs/dcache.c
761+++ b/fs/dcache.c
be118d29 762@@ -1308,6 +1308,7 @@ void d_walk(struct dentry *parent, void *data,
c1595e42
JR
763 seq = 1;
764 goto again;
765 }
febd17d6 766+EXPORT_SYMBOL_GPL(d_walk);
c1595e42 767
a2654f78
AM
768 struct check_mount {
769 struct vfsmount *mnt;
be118d29 770@@ -2920,6 +2921,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
f2c43d5f
AM
771
772 write_sequnlock(&rename_lock);
773 }
774+EXPORT_SYMBOL_GPL(d_exchange);
775
776 /**
777 * d_ancestor - search for an ancestor
79b8bda9 778diff --git a/fs/exec.c b/fs/exec.c
be118d29 779index 7eb8d21..56d7985 100644
79b8bda9
AM
780--- a/fs/exec.c
781+++ b/fs/exec.c
521ced18 782@@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
79b8bda9
AM
783 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
784 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
785 }
febd17d6 786+EXPORT_SYMBOL_GPL(path_noexec);
79b8bda9
AM
787
788 #ifdef CONFIG_USELIB
789 /*
febd17d6 790diff --git a/fs/fcntl.c b/fs/fcntl.c
be118d29 791index 8cd01f7..bdd1c6c 100644
febd17d6
JR
792--- a/fs/fcntl.c
793+++ b/fs/fcntl.c
2121bcd9 794@@ -85,6 +85,7 @@ int setfl(int fd, struct file * filp, unsigned long arg)
febd17d6
JR
795 out:
796 return error;
797 }
798+EXPORT_SYMBOL_GPL(setfl);
799
800 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
801 int force)
b912730e 802diff --git a/fs/file_table.c b/fs/file_table.c
be118d29 803index 7ec0b3e..819ee07 100644
b912730e
AM
804--- a/fs/file_table.c
805+++ b/fs/file_table.c
be118d29 806@@ -147,6 +147,7 @@ struct file *get_empty_filp(void)
b912730e
AM
807 }
808 return ERR_PTR(-ENFILE);
809 }
febd17d6 810+EXPORT_SYMBOL_GPL(get_empty_filp);
b912730e
AM
811
812 /**
813 * alloc_file - allocate and initialize a 'struct file'
be118d29 814@@ -257,6 +258,7 @@ void flush_delayed_fput(void)
8cdd5066
JR
815 {
816 delayed_fput(NULL);
817 }
febd17d6 818+EXPORT_SYMBOL_GPL(flush_delayed_fput);
8cdd5066
JR
819
820 static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
821
be118d29 822@@ -299,6 +301,7 @@ void __fput_sync(struct file *file)
8cdd5066
JR
823 }
824
825 EXPORT_SYMBOL(fput);
febd17d6 826+EXPORT_SYMBOL_GPL(__fput_sync);
8cdd5066
JR
827
828 void put_filp(struct file *file)
829 {
be118d29 830@@ -307,6 +310,7 @@ void put_filp(struct file *file)
b912730e
AM
831 file_free(file);
832 }
833 }
febd17d6 834+EXPORT_SYMBOL_GPL(put_filp);
b912730e 835
79b8bda9 836 void __init files_init(void)
8b6a4947 837 {
5afbbe0d 838diff --git a/fs/inode.c b/fs/inode.c
be118d29 839index 929a5a3..d93653e 100644
5afbbe0d
AM
840--- a/fs/inode.c
841+++ b/fs/inode.c
be118d29 842@@ -1668,6 +1668,7 @@ int update_time(struct inode *inode, struct timespec *time, int flags)
5afbbe0d
AM
843
844 return update_time(inode, time, flags);
845 }
846+EXPORT_SYMBOL_GPL(update_time);
847
848 /**
849 * touch_atime - update the access time
7f207e10 850diff --git a/fs/namespace.c b/fs/namespace.c
be118d29 851index 26ef600..a4b9707 100644
7f207e10
AM
852--- a/fs/namespace.c
853+++ b/fs/namespace.c
8b6a4947 854@@ -517,6 +517,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
c06a8ce3
AM
855 mnt_dec_writers(real_mount(mnt));
856 preempt_enable();
857 }
858+EXPORT_SYMBOL_GPL(__mnt_drop_write);
859
860 /**
861 * mnt_drop_write - give up write access to a mount
8b6a4947
AM
862@@ -851,6 +852,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
863 {
864 return check_mnt(real_mount(mnt));
865 }
866+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
867
868 /*
869 * vfsmount lock must be held for write
870@@ -1887,6 +1889,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
7f207e10
AM
871 }
872 return 0;
873 }
febd17d6 874+EXPORT_SYMBOL_GPL(iterate_mounts);
7f207e10 875
7eafdf33 876 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
7f207e10
AM
877 {
878diff --git a/fs/notify/group.c b/fs/notify/group.c
2121bcd9 879index b7a4b6a..5a69d60 100644
7f207e10
AM
880--- a/fs/notify/group.c
881+++ b/fs/notify/group.c
882@@ -22,6 +22,7 @@
883 #include <linux/srcu.h>
884 #include <linux/rculist.h>
885 #include <linux/wait.h>
886+#include <linux/module.h>
887
888 #include <linux/fsnotify_backend.h>
889 #include "fsnotify.h"
ffa93bbd 890@@ -109,6 +110,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
1716fcea 891 {
2121bcd9 892 refcount_inc(&group->refcnt);
1716fcea 893 }
febd17d6 894+EXPORT_SYMBOL_GPL(fsnotify_get_group);
1716fcea
AM
895
896 /*
897 * Drop a reference to a group. Free it if it's through.
ffa93bbd 898@@ -118,6 +120,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
2121bcd9 899 if (refcount_dec_and_test(&group->refcnt))
1716fcea 900 fsnotify_final_destroy_group(group);
7f207e10 901 }
febd17d6 902+EXPORT_SYMBOL_GPL(fsnotify_put_group);
7f207e10
AM
903
904 /*
905 * Create a new fsnotify_group and hold a reference for the group returned.
ffa93bbd 906@@ -147,6 +150,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
7f207e10
AM
907
908 return group;
909 }
febd17d6 910+EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
1716fcea
AM
911
912 int fsnotify_fasync(int fd, struct file *file, int on)
913 {
7f207e10 914diff --git a/fs/notify/mark.c b/fs/notify/mark.c
2121bcd9 915index e9191b4..1f8ccfa 100644
7f207e10
AM
916--- a/fs/notify/mark.c
917+++ b/fs/notify/mark.c
2121bcd9
AM
918@@ -108,6 +108,7 @@ void fsnotify_get_mark(struct fsnotify_mark *mark)
919 WARN_ON_ONCE(!refcount_read(&mark->refcnt));
920 refcount_inc(&mark->refcnt);
7f207e10 921 }
febd17d6 922+EXPORT_SYMBOL_GPL(fsnotify_put_mark);
7f207e10 923
ffa93bbd
AM
924 static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
925 {
2121bcd9 926@@ -392,6 +393,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
1716fcea 927 mutex_unlock(&group->mark_mutex);
79b8bda9 928 fsnotify_free_mark(mark);
7f207e10 929 }
febd17d6 930+EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
7f207e10 931
ffa93bbd
AM
932 /*
933 * Sorting function for lists of fsnotify marks.
2121bcd9 934@@ -606,6 +608,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
ffa93bbd 935 fsnotify_put_mark(mark);
7f207e10
AM
936 return ret;
937 }
febd17d6 938+EXPORT_SYMBOL_GPL(fsnotify_add_mark);
7f207e10 939
ffa93bbd
AM
940 int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode,
941 struct vfsmount *mnt, int allow_dups)
2121bcd9 942@@ -741,6 +744,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
ffa93bbd
AM
943 fsnotify_get_group(group);
944 mark->group = group;
7f207e10 945 }
febd17d6 946+EXPORT_SYMBOL_GPL(fsnotify_init_mark);
7f207e10 947
5afbbe0d
AM
948 /*
949 * Destroy all marks in destroy_list, waits for SRCU period to finish before
7f207e10 950diff --git a/fs/open.c b/fs/open.c
8b6a4947 951index 7ea1184..6e2e241 100644
7f207e10
AM
952--- a/fs/open.c
953+++ b/fs/open.c
c2c0f25c 954@@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
febd17d6 955 inode_unlock(dentry->d_inode);
7f207e10
AM
956 return ret;
957 }
febd17d6 958+EXPORT_SYMBOL_GPL(do_truncate);
7f207e10 959
5afbbe0d 960 long vfs_truncate(const struct path *path, loff_t length)
7f207e10 961 {
ffa93bbd 962@@ -691,6 +692,7 @@ int open_check_o_direct(struct file *f)
b912730e
AM
963 }
964 return 0;
965 }
febd17d6 966+EXPORT_SYMBOL_GPL(open_check_o_direct);
b912730e
AM
967
968 static int do_dentry_open(struct file *f,
c2c0f25c 969 struct inode *inode,
5527c038 970diff --git a/fs/read_write.c b/fs/read_write.c
2121bcd9 971index 0a5c47b..d423a5f 100644
5527c038
JR
972--- a/fs/read_write.c
973+++ b/fs/read_write.c
2121bcd9 974@@ -454,6 +454,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
8b6a4947
AM
975
976 return ret;
977 }
978+EXPORT_SYMBOL_GPL(vfs_read);
979
980 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
981 {
2121bcd9 982@@ -494,6 +495,7 @@ vfs_readf_t vfs_readf(struct file *file)
5527c038
JR
983 return new_sync_read;
984 return ERR_PTR(-ENOSYS);
985 }
febd17d6 986+EXPORT_SYMBOL_GPL(vfs_readf);
5527c038
JR
987
988 vfs_writef_t vfs_writef(struct file *file)
989 {
2121bcd9 990@@ -505,6 +507,7 @@ vfs_writef_t vfs_writef(struct file *file)
5527c038
JR
991 return new_sync_write;
992 return ERR_PTR(-ENOSYS);
993 }
febd17d6 994+EXPORT_SYMBOL_GPL(vfs_writef);
5527c038 995
8b6a4947
AM
996 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
997 {
2121bcd9 998@@ -574,6 +577,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
8b6a4947
AM
999
1000 return ret;
1001 }
1002+EXPORT_SYMBOL_GPL(vfs_write);
1003
1004 static inline loff_t file_pos_read(struct file *file)
5527c038 1005 {
7f207e10 1006diff --git a/fs/splice.c b/fs/splice.c
2121bcd9 1007index c5fb195..ce01a74 100644
7f207e10
AM
1008--- a/fs/splice.c
1009+++ b/fs/splice.c
8b6a4947 1010@@ -850,6 +850,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
392086de
AM
1011
1012 return splice_write(pipe, out, ppos, len, flags);
7f207e10 1013 }
febd17d6 1014+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
1015
1016 /*
1017 * Attempt to initiate a splice from a file to a pipe.
8b6a4947 1018@@ -879,6 +880,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
7f207e10
AM
1019
1020 return splice_read(in, ppos, pipe, len, flags);
1021 }
febd17d6 1022+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
1023
1024 /**
1025 * splice_direct_to_actor - splices data directly between two non-pipes
a2654f78 1026diff --git a/fs/sync.c b/fs/sync.c
2121bcd9 1027index a6891ee..47a78bd 100644
a2654f78
AM
1028--- a/fs/sync.c
1029+++ b/fs/sync.c
2121bcd9 1030@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
a2654f78
AM
1031 sb->s_op->sync_fs(sb, wait);
1032 return __sync_blockdev(sb->s_bdev, wait);
1033 }
1034+EXPORT_SYMBOL_GPL(__sync_filesystem);
1035
1036 /*
1037 * Write out and wait upon all dirty data associated with this
c1595e42 1038diff --git a/fs/xattr.c b/fs/xattr.c
8b6a4947 1039index 61cd28b..35570cd 100644
c1595e42
JR
1040--- a/fs/xattr.c
1041+++ b/fs/xattr.c
8b6a4947 1042@@ -297,6 +297,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
c1595e42
JR
1043 *xattr_value = value;
1044 return error;
1045 }
febd17d6 1046+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 1047
febd17d6 1048 ssize_t
f2c43d5f 1049 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947 1050diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
be118d29 1051index 0dca42f..7bb20c5 100644
8b6a4947
AM
1052--- a/kernel/locking/lockdep.c
1053+++ b/kernel/locking/lockdep.c
be118d29 1054@@ -151,6 +151,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
8b6a4947
AM
1055 }
1056 return lock_classes + hlock->class_idx - 1;
1057 }
1058+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
1059 #define hlock_class(hlock) lockdep_hlock_class(hlock)
1060
1061 #ifdef CONFIG_LOCK_STAT
8cdd5066 1062diff --git a/kernel/task_work.c b/kernel/task_work.c
2121bcd9 1063index 0fef395..83fb1ec 100644
8cdd5066
JR
1064--- a/kernel/task_work.c
1065+++ b/kernel/task_work.c
2121bcd9 1066@@ -116,3 +116,4 @@ void task_work_run(void)
8cdd5066
JR
1067 } while (work);
1068 }
1069 }
febd17d6 1070+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 1071diff --git a/security/commoncap.c b/security/commoncap.c
be118d29 1072index 48620c9..4981104 100644
7f207e10
AM
1073--- a/security/commoncap.c
1074+++ b/security/commoncap.c
be118d29 1075@@ -1330,12 +1330,14 @@ int cap_mmap_addr(unsigned long addr)
94337f0d 1076 }
7f207e10
AM
1077 return ret;
1078 }
febd17d6 1079+EXPORT_SYMBOL_GPL(cap_mmap_addr);
0c3ec466
AM
1080
1081 int cap_mmap_file(struct file *file, unsigned long reqprot,
1082 unsigned long prot, unsigned long flags)
1083 {
1084 return 0;
1085 }
febd17d6 1086+EXPORT_SYMBOL_GPL(cap_mmap_file);
c2c0f25c
AM
1087
1088 #ifdef CONFIG_SECURITY
1089
7f207e10 1090diff --git a/security/device_cgroup.c b/security/device_cgroup.c
2121bcd9 1091index c65b39b..e363d22 100644
7f207e10
AM
1092--- a/security/device_cgroup.c
1093+++ b/security/device_cgroup.c
2121bcd9 1094@@ -8,6 +8,7 @@
f6c5ef8b
AM
1095 #include <linux/device_cgroup.h>
1096 #include <linux/cgroup.h>
1097 #include <linux/ctype.h>
1098+#include <linux/export.h>
1099 #include <linux/list.h>
1100 #include <linux/uaccess.h>
1101 #include <linux/seq_file.h>
2121bcd9 1102@@ -824,3 +825,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
7f207e10 1103
2121bcd9
AM
1104 return 0;
1105 }
1106+EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
7f207e10 1107diff --git a/security/security.c b/security/security.c
2121bcd9 1108index 1cd8526..f2e4736 100644
7f207e10
AM
1109--- a/security/security.c
1110+++ b/security/security.c
2121bcd9 1111@@ -531,6 +531,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 1112 return 0;
c2c0f25c 1113 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 1114 }
febd17d6 1115+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 1116
5afbbe0d 1117 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 1118 {
2121bcd9 1119@@ -547,6 +548,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 1120 return 0;
c2c0f25c 1121 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 1122 }
febd17d6 1123+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 1124
5afbbe0d 1125 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1126 struct dentry *new_dentry)
2121bcd9 1127@@ -555,6 +557,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1128 return 0;
c2c0f25c 1129 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 1130 }
febd17d6 1131+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 1132
5afbbe0d
AM
1133 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1134 const struct path *new_dir, struct dentry *new_dentry,
2121bcd9 1135@@ -582,6 +585,7 @@ int security_path_truncate(const struct path *path)
7f207e10 1136 return 0;
c2c0f25c 1137 return call_int_hook(path_truncate, 0, path);
7f207e10 1138 }
febd17d6 1139+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 1140
5afbbe0d 1141 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 1142 {
2121bcd9 1143@@ -589,6 +593,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 1144 return 0;
c2c0f25c 1145 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 1146 }
febd17d6 1147+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 1148
5afbbe0d 1149 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1150 {
2121bcd9 1151@@ -596,6 +601,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1152 return 0;
c2c0f25c 1153 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 1154 }
febd17d6 1155+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 1156
5afbbe0d 1157 int security_path_chroot(const struct path *path)
7f207e10 1158 {
2121bcd9 1159@@ -681,6 +687,7 @@ int security_inode_readlink(struct dentry *dentry)
7f207e10 1160 return 0;
c2c0f25c 1161 return call_int_hook(inode_readlink, 0, dentry);
7f207e10 1162 }
febd17d6 1163+EXPORT_SYMBOL_GPL(security_inode_readlink);
7f207e10 1164
c2c0f25c
AM
1165 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1166 bool rcu)
2121bcd9 1167@@ -696,6 +703,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 1168 return 0;
c2c0f25c 1169 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 1170 }
febd17d6 1171+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 1172
1e00d052 1173 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
7f207e10 1174 {
2121bcd9 1175@@ -867,6 +875,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
1176
1177 return fsnotify_perm(file, mask);
1178 }
febd17d6 1179+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
1180
1181 int security_file_alloc(struct file *file)
1182 {
2121bcd9 1183@@ -926,6 +935,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
7f207e10
AM
1184 return ret;
1185 return ima_file_mmap(file, prot);
1186 }
febd17d6 1187+EXPORT_SYMBOL_GPL(security_mmap_file);
7f207e10 1188
0c3ec466
AM
1189 int security_mmap_addr(unsigned long addr)
1190 {
7f207e10
AM
1191diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1192--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
be118d29 1193+++ linux/Documentation/ABI/testing/debugfs-aufs 2018-04-06 07:48:44.201271351 +0200
86dc4139 1194@@ -0,0 +1,50 @@
7f207e10
AM
1195+What: /debug/aufs/si_<id>/
1196+Date: March 2009
f6b6e03d 1197+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1198+Description:
1199+ Under /debug/aufs, a directory named si_<id> is created
1200+ per aufs mount, where <id> is a unique id generated
1201+ internally.
1facf9fc 1202+
86dc4139
AM
1203+What: /debug/aufs/si_<id>/plink
1204+Date: Apr 2013
f6b6e03d 1205+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
1206+Description:
1207+ It has three lines and shows the information about the
1208+ pseudo-link. The first line is a single number
1209+ representing a number of buckets. The second line is a
1210+ number of pseudo-links per buckets (separated by a
1211+ blank). The last line is a single number representing a
1212+ total number of psedo-links.
1213+ When the aufs mount option 'noplink' is specified, it
1214+ will show "1\n0\n0\n".
1215+
7f207e10
AM
1216+What: /debug/aufs/si_<id>/xib
1217+Date: March 2009
f6b6e03d 1218+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1219+Description:
1220+ It shows the consumed blocks by xib (External Inode Number
1221+ Bitmap), its block size and file size.
1222+ When the aufs mount option 'noxino' is specified, it
1223+ will be empty. About XINO files, see the aufs manual.
1224+
1225+What: /debug/aufs/si_<id>/xino0, xino1 ... xinoN
1226+Date: March 2009
f6b6e03d 1227+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1228+Description:
1229+ It shows the consumed blocks by xino (External Inode Number
1230+ Translation Table), its link count, block size and file
1231+ size.
1232+ When the aufs mount option 'noxino' is specified, it
1233+ will be empty. About XINO files, see the aufs manual.
1234+
1235+What: /debug/aufs/si_<id>/xigen
1236+Date: March 2009
f6b6e03d 1237+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1238+Description:
1239+ It shows the consumed blocks by xigen (External Inode
1240+ Generation Table), its block size and file size.
1241+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1242+ be created.
1243+ When the aufs mount option 'noxino' is specified, it
1244+ will be empty. About XINO files, see the aufs manual.
1245diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1246--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
be118d29 1247+++ linux/Documentation/ABI/testing/sysfs-aufs 2018-04-06 07:48:44.201271351 +0200
392086de 1248@@ -0,0 +1,31 @@
7f207e10
AM
1249+What: /sys/fs/aufs/si_<id>/
1250+Date: March 2009
f6b6e03d 1251+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1252+Description:
1253+ Under /sys/fs/aufs, a directory named si_<id> is created
1254+ per aufs mount, where <id> is a unique id generated
1255+ internally.
1256+
1257+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1258+Date: March 2009
f6b6e03d 1259+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1260+Description:
1261+ It shows the abolute path of a member directory (which
1262+ is called branch) in aufs, and its permission.
1263+
392086de
AM
1264+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1265+Date: July 2013
f6b6e03d 1266+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1267+Description:
1268+ It shows the id of a member directory (which is called
1269+ branch) in aufs.
1270+
7f207e10
AM
1271+What: /sys/fs/aufs/si_<id>/xi_path
1272+Date: March 2009
f6b6e03d 1273+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1274+Description:
1275+ It shows the abolute path of XINO (External Inode Number
1276+ Bitmap, Translation Table and Generation Table) file
1277+ even if it is the default path.
1278+ When the aufs mount option 'noxino' is specified, it
1279+ will be empty. About XINO files, see the aufs manual.
53392da6
AM
1280diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1281--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 1282+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2018-04-06 07:48:44.201271351 +0200
1c60b727 1283@@ -0,0 +1,171 @@
53392da6 1284+
a2654f78 1285+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
1286+#
1287+# This program is free software; you can redistribute it and/or modify
1288+# it under the terms of the GNU General Public License as published by
1289+# the Free Software Foundation; either version 2 of the License, or
1290+# (at your option) any later version.
1291+#
1292+# This program is distributed in the hope that it will be useful,
1293+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1294+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1295+# GNU General Public License for more details.
1296+#
1297+# You should have received a copy of the GNU General Public License
523b37e3 1298+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1299+
1300+Introduction
1301+----------------------------------------
1302+
3c1bdaff 1303+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1304+1. abbrev. for "advanced multi-layered unification filesystem".
1305+2. abbrev. for "another unionfs".
1306+3. abbrev. for "auf das" in German which means "on the" in English.
1307+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1308+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1309+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1310+
1311+AUFS is a filesystem with features:
1312+- multi layered stackable unification filesystem, the member directory
1313+ is called as a branch.
1314+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1315+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1316+ combination.
1317+- internal "file copy-on-write".
1318+- logical deletion, whiteout.
1319+- dynamic branch manipulation, adding, deleting and changing permission.
1320+- allow bypassing aufs, user's direct branch access.
1321+- external inode number translation table and bitmap which maintains the
1322+ persistent aufs inode number.
1323+- seekable directory, including NFS readdir.
1324+- file mapping, mmap and sharing pages.
1325+- pseudo-link, hardlink over branches.
1326+- loopback mounted filesystem as a branch.
1327+- several policies to select one among multiple writable branches.
1328+- revert a single systemcall when an error occurs in aufs.
1329+- and more...
1330+
1331+
1332+Multi Layered Stackable Unification Filesystem
1333+----------------------------------------------------------------------
1334+Most people already knows what it is.
1335+It is a filesystem which unifies several directories and provides a
1336+merged single directory. When users access a file, the access will be
1337+passed/re-directed/converted (sorry, I am not sure which English word is
1338+correct) to the real file on the member filesystem. The member
1339+filesystem is called 'lower filesystem' or 'branch' and has a mode
1340+'readonly' and 'readwrite.' And the deletion for a file on the lower
1341+readonly branch is handled by creating 'whiteout' on the upper writable
1342+branch.
1343+
1344+On LKML, there have been discussions about UnionMount (Jan Blunck,
1345+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1346+different approaches to implement the merged-view.
1347+The former tries putting it into VFS, and the latter implements as a
1348+separate filesystem.
1349+(If I misunderstand about these implementations, please let me know and
1350+I shall correct it. Because it is a long time ago when I read their
1351+source files last time).
1352+
1353+UnionMount's approach will be able to small, but may be hard to share
1354+branches between several UnionMount since the whiteout in it is
1355+implemented in the inode on branch filesystem and always
1356+shared. According to Bharata's post, readdir does not seems to be
1357+finished yet.
1358+There are several missing features known in this implementations such as
1359+- for users, the inode number may change silently. eg. copy-up.
1360+- link(2) may break by copy-up.
1361+- read(2) may get an obsoleted filedata (fstat(2) too).
1362+- fcntl(F_SETLK) may be broken by copy-up.
1363+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1364+ open(O_RDWR).
1365+
7e9cd9fe
AM
1366+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1367+merged into mainline. This is another implementation of UnionMount as a
1368+separated filesystem. All the limitations and known problems which
1369+UnionMount are equally inherited to "overlay" filesystem.
1370+
1371+Unionfs has a longer history. When I started implementing a stackable
1372+filesystem (Aug 2005), it already existed. It has virtual super_block,
1373+inode, dentry and file objects and they have an array pointing lower
1374+same kind objects. After contributing many patches for Unionfs, I
1375+re-started my project AUFS (Jun 2006).
53392da6
AM
1376+
1377+In AUFS, the structure of filesystem resembles to Unionfs, but I
1378+implemented my own ideas, approaches and enhancements and it became
1379+totally different one.
1380+
1381+Comparing DM snapshot and fs based implementation
1382+- the number of bytes to be copied between devices is much smaller.
1383+- the type of filesystem must be one and only.
1384+- the fs must be writable, no readonly fs, even for the lower original
1385+ device. so the compression fs will not be usable. but if we use
1386+ loopback mount, we may address this issue.
1387+ for instance,
1388+ mount /cdrom/squashfs.img /sq
1389+ losetup /sq/ext2.img
1390+ losetup /somewhere/cow
1391+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1392+- it will be difficult (or needs more operations) to extract the
1393+ difference between the original device and COW.
1394+- DM snapshot-merge may help a lot when users try merging. in the
1395+ fs-layer union, users will use rsync(1).
1396+
7e9cd9fe
AM
1397+You may want to read my old paper "Filesystems in LiveCD"
1398+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1399+
7e9cd9fe
AM
1400+
1401+Several characters/aspects/persona of aufs
53392da6
AM
1402+----------------------------------------------------------------------
1403+
7e9cd9fe 1404+Aufs has several characters, aspects or persona.
53392da6
AM
1405+1. a filesystem, callee of VFS helper
1406+2. sub-VFS, caller of VFS helper for branches
1407+3. a virtual filesystem which maintains persistent inode number
1408+4. reader/writer of files on branches such like an application
1409+
1410+1. Callee of VFS Helper
1411+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1412+unlink(2) from an application reaches sys_unlink() kernel function and
1413+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1414+calls filesystem specific unlink operation. Actually aufs implements the
1415+unlink operation but it behaves like a redirector.
1416+
1417+2. Caller of VFS Helper for Branches
1418+aufs_unlink() passes the unlink request to the branch filesystem as if
1419+it were called from VFS. So the called unlink operation of the branch
1420+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1421+every necessary pre/post operation for the branch filesystem.
1422+- acquire the lock for the parent dir on a branch
1423+- lookup in a branch
1424+- revalidate dentry on a branch
1425+- mnt_want_write() for a branch
1426+- vfs_unlink() for a branch
1427+- mnt_drop_write() for a branch
1428+- release the lock on a branch
1429+
1430+3. Persistent Inode Number
1431+One of the most important issue for a filesystem is to maintain inode
1432+numbers. This is particularly important to support exporting a
1433+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1434+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1435+keep and maintain the inode numbers. It may be a large space and may not
1436+suit to keep in memory. Aufs rents some space from its first writable
1437+branch filesystem (by default) and creates file(s) on it. These files
1438+are created by aufs internally and removed soon (currently) keeping
1439+opened.
53392da6
AM
1440+Note: Because these files are removed, they are totally gone after
1441+ unmounting aufs. It means the inode numbers are not persistent
1442+ across unmount or reboot. I have a plan to make them really
1443+ persistent which will be important for aufs on NFS server.
1444+
1445+4. Read/Write Files Internally (copy-on-write)
1446+Because a branch can be readonly, when you write a file on it, aufs will
1447+"copy-up" it to the upper writable branch internally. And then write the
1448+originally requested thing to the file. Generally kernel doesn't
1449+open/read/write file actively. In aufs, even a single write may cause a
1450+internal "file copy". This behaviour is very similar to cp(1) command.
1451+
1452+Some people may think it is better to pass such work to user space
1453+helper, instead of doing in kernel space. Actually I am still thinking
1454+about it. But currently I have implemented it in kernel space.
1455diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1456--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 1457+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2018-04-06 07:48:44.201271351 +0200
7e9cd9fe 1458@@ -0,0 +1,258 @@
53392da6 1459+
a2654f78 1460+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
1461+#
1462+# This program is free software; you can redistribute it and/or modify
1463+# it under the terms of the GNU General Public License as published by
1464+# the Free Software Foundation; either version 2 of the License, or
1465+# (at your option) any later version.
1466+#
1467+# This program is distributed in the hope that it will be useful,
1468+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1469+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1470+# GNU General Public License for more details.
1471+#
1472+# You should have received a copy of the GNU General Public License
523b37e3 1473+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1474+
1475+Basic Aufs Internal Structure
1476+
1477+Superblock/Inode/Dentry/File Objects
1478+----------------------------------------------------------------------
1479+As like an ordinary filesystem, aufs has its own
1480+superblock/inode/dentry/file objects. All these objects have a
1481+dynamically allocated array and store the same kind of pointers to the
1482+lower filesystem, branch.
1483+For example, when you build a union with one readwrite branch and one
1484+readonly, mounted /au, /rw and /ro respectively.
1485+- /au = /rw + /ro
1486+- /ro/fileA exists but /rw/fileA
1487+
1488+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1489+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1490+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1491+- [1] = /ro/fileA
1492+
1493+This style of an array is essentially same to the aufs
1494+superblock/inode/dentry/file objects.
1495+
1496+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1497+branches dynamically, these objects has its own generation. When
1498+branches are changed, the generation in aufs superblock is
1499+incremented. And a generation in other object are compared when it is
1500+accessed. When a generation in other objects are obsoleted, aufs
1501+refreshes the internal array.
53392da6
AM
1502+
1503+
1504+Superblock
1505+----------------------------------------------------------------------
1506+Additionally aufs superblock has some data for policies to select one
1507+among multiple writable branches, XIB files, pseudo-links and kobject.
1508+See below in detail.
7e9cd9fe
AM
1509+About the policies which supports copy-down a directory, see
1510+wbr_policy.txt too.
53392da6
AM
1511+
1512+
1513+Branch and XINO(External Inode Number Translation Table)
1514+----------------------------------------------------------------------
1515+Every branch has its own xino (external inode number translation table)
1516+file. The xino file is created and unlinked by aufs internally. When two
1517+members of a union exist on the same filesystem, they share the single
1518+xino file.
1519+The struct of a xino file is simple, just a sequence of aufs inode
1520+numbers which is indexed by the lower inode number.
1521+In the above sample, assume the inode number of /ro/fileA is i111 and
1522+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1523+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1524+
1525+When the inode numbers are not contiguous, the xino file will be sparse
1526+which has a hole in it and doesn't consume as much disk space as it
1527+might appear. If your branch filesystem consumes disk space for such
1528+holes, then you should specify 'xino=' option at mounting aufs.
1529+
7e9cd9fe
AM
1530+Aufs has a mount option to free the disk blocks for such holes in XINO
1531+files on tmpfs or ramdisk. But it is not so effective actually. If you
1532+meet a problem of disk shortage due to XINO files, then you should try
1533+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1534+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1535+the holes in XINO files.
1536+
53392da6 1537+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1538+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1539+whiteout-ed doubly, so that users will never see their names in aufs
1540+hierarchy.
7e9cd9fe 1541+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1542+2. a directory to store a pseudo-link.
7e9cd9fe 1543+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1544+
1545+1. Whiteout Base
1546+ When you remove a file on a readonly branch, aufs handles it as a
1547+ logical deletion and creates a whiteout on the upper writable branch
1548+ as a hardlink of this file in order not to consume inode on the
1549+ writable branch.
1550+2. Pseudo-link Dir
1551+ See below, Pseudo-link.
1552+3. Step-Parent Dir
1553+ When "fileC" exists on the lower readonly branch only and it is
1554+ opened and removed with its parent dir, and then user writes
1555+ something into it, then aufs copies-up fileC to this
1556+ directory. Because there is no other dir to store fileC. After
1557+ creating a file under this dir, the file is unlinked.
1558+
1559+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1560+dynamically, a branch has its own id. When the branch order changes,
1561+aufs finds the new index by searching the branch id.
53392da6
AM
1562+
1563+
1564+Pseudo-link
1565+----------------------------------------------------------------------
1566+Assume "fileA" exists on the lower readonly branch only and it is
1567+hardlinked to "fileB" on the branch. When you write something to fileA,
1568+aufs copies-up it to the upper writable branch. Additionally aufs
1569+creates a hardlink under the Pseudo-link Directory of the writable
1570+branch. The inode of a pseudo-link is kept in aufs super_block as a
1571+simple list. If fileB is read after unlinking fileA, aufs returns
1572+filedata from the pseudo-link instead of the lower readonly
1573+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1574+inode number by xino (see above) is essentially necessary.
53392da6
AM
1575+
1576+All the hardlinks under the Pseudo-link Directory of the writable branch
1577+should be restored in a proper location later. Aufs provides a utility
1578+to do this. The userspace helpers executed at remounting and unmounting
1579+aufs by default.
1580+During this utility is running, it puts aufs into the pseudo-link
1581+maintenance mode. In this mode, only the process which began the
1582+maintenance mode (and its child processes) is allowed to operate in
1583+aufs. Some other processes which are not related to the pseudo-link will
1584+be allowed to run too, but the rest have to return an error or wait
1585+until the maintenance mode ends. If a process already acquires an inode
1586+mutex (in VFS), it has to return an error.
1587+
1588+
1589+XIB(external inode number bitmap)
1590+----------------------------------------------------------------------
1591+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1592+bitmap in a superblock object. It is also an internal file such like a
1593+xino file.
53392da6
AM
1594+It is a simple bitmap to mark whether the aufs inode number is in-use or
1595+not.
1596+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1597+
7e9cd9fe 1598+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1599+reduce the number of consumed disk blocks for these files.
1600+
1601+
1602+Virtual or Vertical Dir, and Readdir in Userspace
1603+----------------------------------------------------------------------
1604+In order to support multiple layers (branches), aufs readdir operation
1605+constructs a virtual dir block on memory. For readdir, aufs calls
1606+vfs_readdir() internally for each dir on branches, merges their entries
1607+with eliminating the whiteout-ed ones, and sets it to file (dir)
1608+object. So the file object has its entry list until it is closed. The
1609+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1610+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1611+
1612+The dynamically allocated memory block for the name of entries has a
1613+unit of 512 bytes (by default) and stores the names contiguously (no
1614+padding). Another block for each entry is handled by kmem_cache too.
1615+During building dir blocks, aufs creates hash list and judging whether
1616+the entry is whiteouted by its upper branch or already listed.
1617+The merged result is cached in the corresponding inode object and
1618+maintained by a customizable life-time option.
1619+
1620+Some people may call it can be a security hole or invite DoS attack
1621+since the opened and once readdir-ed dir (file object) holds its entry
1622+list and becomes a pressure for system memory. But I'd say it is similar
1623+to files under /proc or /sys. The virtual files in them also holds a
1624+memory page (generally) while they are opened. When an idea to reduce
1625+memory for them is introduced, it will be applied to aufs too.
1626+For those who really hate this situation, I've developed readdir(3)
1627+library which operates this merging in userspace. You just need to set
1628+LD_PRELOAD environment variable, and aufs will not consume no memory in
1629+kernel space for readdir(3).
1630+
1631+
1632+Workqueue
1633+----------------------------------------------------------------------
1634+Aufs sometimes requires privilege access to a branch. For instance,
1635+in copy-up/down operation. When a user process is going to make changes
1636+to a file which exists in the lower readonly branch only, and the mode
1637+of one of ancestor directories may not be writable by a user
1638+process. Here aufs copy-up the file with its ancestors and they may
1639+require privilege to set its owner/group/mode/etc.
1640+This is a typical case of a application character of aufs (see
1641+Introduction).
1642+
1643+Aufs uses workqueue synchronously for this case. It creates its own
1644+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1645+passes the request to call mkdir or write (for example), and wait for
1646+its completion. This approach solves a problem of a signal handler
1647+simply.
1648+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1649+process, then the process may receive the unexpected SIGXFSZ or other
1650+signals.
53392da6
AM
1651+
1652+Also aufs uses the system global workqueue ("events" kernel thread) too
1653+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1654+whiteout base and etc. This is unrelated to a privilege.
1655+Most of aufs operation tries acquiring a rw_semaphore for aufs
1656+superblock at the beginning, at the same time waits for the completion
1657+of all queued asynchronous tasks.
1658+
1659+
1660+Whiteout
1661+----------------------------------------------------------------------
1662+The whiteout in aufs is very similar to Unionfs's. That is represented
1663+by its filename. UnionMount takes an approach of a file mode, but I am
1664+afraid several utilities (find(1) or something) will have to support it.
1665+
1666+Basically the whiteout represents "logical deletion" which stops aufs to
1667+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1668+further lookup.
53392da6
AM
1669+
1670+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1671+In order to make several functions in a single systemcall to be
1672+revertible, aufs adopts an approach to rename a directory to a temporary
1673+unique whiteouted name.
1674+For example, in rename(2) dir where the target dir already existed, aufs
1675+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1676+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1677+update the attributes, etc). If an error happens in these actions, aufs
1678+simply renames the whiteouted name back and returns an error. If all are
1679+succeeded, aufs registers a function to remove the whiteouted unique
1680+temporary name completely and asynchronously to the system global
1681+workqueue.
1682+
1683+
1684+Copy-up
1685+----------------------------------------------------------------------
1686+It is a well-known feature or concept.
1687+When user modifies a file on a readonly branch, aufs operate "copy-up"
1688+internally and makes change to the new file on the upper writable branch.
1689+When the trigger systemcall does not update the timestamps of the parent
1690+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1691+
1692+
1693+Move-down (aufs3.9 and later)
1694+----------------------------------------------------------------------
1695+"Copy-up" is one of the essential feature in aufs. It copies a file from
1696+the lower readonly branch to the upper writable branch when a user
1697+changes something about the file.
1698+"Move-down" is an opposite action of copy-up. Basically this action is
1699+ran manually instead of automatically and internally.
076b876e
AM
1700+For desgin and implementation, aufs has to consider these issues.
1701+- whiteout for the file may exist on the lower branch.
1702+- ancestor directories may not exist on the lower branch.
1703+- diropq for the ancestor directories may exist on the upper branch.
1704+- free space on the lower branch will reduce.
1705+- another access to the file may happen during moving-down, including
7e9cd9fe 1706+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1707+- the file should not be hard-linked nor pseudo-linked. they should be
1708+ handled by auplink utility later.
c2b27bf2
AM
1709+
1710+Sometimes users want to move-down a file from the upper writable branch
1711+to the lower readonly or writable branch. For instance,
1712+- the free space of the upper writable branch is going to run out.
1713+- create a new intermediate branch between the upper and lower branch.
1714+- etc.
1715+
1716+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e
AM
1717diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1718--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 1719+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2018-04-06 07:48:44.201271351 +0200
b912730e
AM
1720@@ -0,0 +1,85 @@
1721+
a2654f78 1722+# Copyright (C) 2015-2017 Junjiro R. Okajima
b912730e
AM
1723+#
1724+# This program is free software; you can redistribute it and/or modify
1725+# it under the terms of the GNU General Public License as published by
1726+# the Free Software Foundation; either version 2 of the License, or
1727+# (at your option) any later version.
1728+#
1729+# This program is distributed in the hope that it will be useful,
1730+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1731+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1732+# GNU General Public License for more details.
1733+#
1734+# You should have received a copy of the GNU General Public License
1735+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1736+
1737+Support for a branch who has its ->atomic_open()
1738+----------------------------------------------------------------------
1739+The filesystems who implement its ->atomic_open() are not majority. For
1740+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1741+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1742+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1743+sure whether all filesystems who have ->atomic_open() behave like this,
1744+but NFSv4 surely returns the error.
1745+
1746+In order to support ->atomic_open() for aufs, there are a few
1747+approaches.
1748+
1749+A. Introduce aufs_atomic_open()
1750+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1751+ branch fs.
1752+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1753+ an aufs user Pip Cet's approach
1754+ - calls aufs_create(), VFS finish_open() and notify_change().
1755+ - pass fake-mode to finish_open(), and then correct the mode by
1756+ notify_change().
1757+C. Extend aufs_open() to call branch fs's ->atomic_open()
1758+ - no aufs_atomic_open().
1759+ - aufs_lookup() registers the TID to an aufs internal object.
1760+ - aufs_create() does nothing when the matching TID is registered, but
1761+ registers the mode.
1762+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1763+ TID is registered.
1764+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1765+ credential
1766+ - no aufs_atomic_open().
1767+ - aufs_create() registers the TID to an internal object. this info
1768+ represents "this process created this file just now."
1769+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1770+ registered TID and re-try open() with superuser's credential.
1771+
1772+Pros and cons for each approach.
1773+
1774+A.
1775+ - straightforward but highly depends upon VFS internal.
1776+ - the atomic behavaiour is kept.
1777+ - some of parameters such as nameidata are hard to reproduce for
1778+ branch fs.
1779+ - large overhead.
1780+B.
1781+ - easy to implement.
1782+ - the atomic behavaiour is lost.
1783+C.
1784+ - the atomic behavaiour is kept.
1785+ - dirty and tricky.
1786+ - VFS checks whether the file is created correctly after calling
1787+ ->create(), which means this approach doesn't work.
1788+D.
1789+ - easy to implement.
1790+ - the atomic behavaiour is lost.
1791+ - to open a file with superuser's credential and give it to a user
1792+ process is a bad idea, since the file object keeps the credential
1793+ in it. It may affect LSM or something. This approach doesn't work
1794+ either.
1795+
1796+The approach A is ideal, but it hard to implement. So here is a
1797+variation of A, which is to be implemented.
1798+
1799+A-1. Introduce aufs_atomic_open()
1800+ - calls branch fs ->atomic_open() if exists. otherwise calls
1801+ vfs_create() and finish_open().
1802+ - the demerit is that the several checks after branch fs
1803+ ->atomic_open() are lost. in the ordinary case, the checks are
1804+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1805+ be implemented in aufs, but not all I am afraid.
53392da6
AM
1806diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1807--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 1808+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2018-04-06 07:48:44.201271351 +0200
7e9cd9fe 1809@@ -0,0 +1,113 @@
53392da6 1810+
a2654f78 1811+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
1812+#
1813+# This program is free software; you can redistribute it and/or modify
1814+# it under the terms of the GNU General Public License as published by
1815+# the Free Software Foundation; either version 2 of the License, or
1816+# (at your option) any later version.
1817+#
1818+# This program is distributed in the hope that it will be useful,
1819+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1820+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1821+# GNU General Public License for more details.
1822+#
1823+# You should have received a copy of the GNU General Public License
523b37e3 1824+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1825+
1826+Lookup in a Branch
1827+----------------------------------------------------------------------
1828+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1829+lookup for branches as VFS does. It may be a heavy work. But almost all
1830+lookup operation in aufs is the simplest case, ie. lookup only an entry
1831+directly connected to its parent. Digging down the directory hierarchy
1832+is unnecessary. VFS has a function lookup_one_len() for that use, and
1833+aufs calls it.
1834+
1835+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1836+->d_revalidate(), also aufs forces the hardest revalidate tests for
1837+them.
1838+For d_revalidate, aufs implements three levels of revalidate tests. See
1839+"Revalidate Dentry and UDBA" in detail.
1840+
1841+
076b876e
AM
1842+Test Only the Highest One for the Directory Permission (dirperm1 option)
1843+----------------------------------------------------------------------
1844+Let's try case study.
1845+- aufs has two branches, upper readwrite and lower readonly.
1846+ /au = /rw + /ro
1847+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1848+- user invoked "chmod a+rx /au/dirA"
1849+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1850+ permission bits are set to world readable.
076b876e
AM
1851+- then "/au/dirA" becomes world readable?
1852+
1853+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1854+or it may be a natively readonly filesystem. If aufs respects the lower
1855+branch, it should not respond readdir request from other users. But user
1856+allowed it by chmod. Should really aufs rejects showing the entries
1857+under /ro/dirA?
1858+
7e9cd9fe
AM
1859+To be honest, I don't have a good solution for this case. So aufs
1860+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1861+users.
076b876e
AM
1862+When dirperm1 is specified, aufs checks only the highest one for the
1863+directory permission, and shows the entries. Otherwise, as usual, checks
1864+every dir existing on all branches and rejects the request.
1865+
1866+As a side effect, dirperm1 option improves the performance of aufs
1867+because the number of permission check is reduced when the number of
1868+branch is many.
1869+
1870+
53392da6
AM
1871+Revalidate Dentry and UDBA (User's Direct Branch Access)
1872+----------------------------------------------------------------------
1873+Generally VFS helpers re-validate a dentry as a part of lookup.
1874+0. digging down the directory hierarchy.
1875+1. lock the parent dir by its i_mutex.
1876+2. lookup the final (child) entry.
1877+3. revalidate it.
1878+4. call the actual operation (create, unlink, etc.)
1879+5. unlock the parent dir
1880+
1881+If the filesystem implements its ->d_revalidate() (step 3), then it is
1882+called. Actually aufs implements it and checks the dentry on a branch is
1883+still valid.
1884+But it is not enough. Because aufs has to release the lock for the
1885+parent dir on a branch at the end of ->lookup() (step 2) and
1886+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1887+held by VFS.
1888+If the file on a branch is changed directly, eg. bypassing aufs, after
1889+aufs released the lock, then the subsequent operation may cause
1890+something unpleasant result.
1891+
1892+This situation is a result of VFS architecture, ->lookup() and
1893+->d_revalidate() is separated. But I never say it is wrong. It is a good
1894+design from VFS's point of view. It is just not suitable for sub-VFS
1895+character in aufs.
1896+
1897+Aufs supports such case by three level of revalidation which is
1898+selectable by user.
1899+1. Simple Revalidate
1900+ Addition to the native flow in VFS's, confirm the child-parent
1901+ relationship on the branch just after locking the parent dir on the
1902+ branch in the "actual operation" (step 4). When this validation
1903+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1904+ checks the validation of the dentry on branches.
1905+2. Monitor Changes Internally by Inotify/Fsnotify
1906+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1907+ the dentry on the branch, and returns EBUSY if it finds different
1908+ dentry.
1909+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1910+ during it is in cache. When the event is notified, aufs registers a
1911+ function to kernel 'events' thread by schedule_work(). And the
1912+ function sets some special status to the cached aufs dentry and inode
1913+ private data. If they are not cached, then aufs has nothing to
1914+ do. When the same file is accessed through aufs (step 0-3) later,
1915+ aufs will detect the status and refresh all necessary data.
1916+ In this mode, aufs has to ignore the event which is fired by aufs
1917+ itself.
1918+3. No Extra Validation
1919+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1920+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1921+ aufs performance when system surely hide the aufs branches from user,
1922+ by over-mounting something (or another method).
1923diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1924--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 1925+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2018-04-06 07:48:44.201271351 +0200
7e9cd9fe 1926@@ -0,0 +1,74 @@
53392da6 1927+
a2654f78 1928+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
1929+#
1930+# This program is free software; you can redistribute it and/or modify
1931+# it under the terms of the GNU General Public License as published by
1932+# the Free Software Foundation; either version 2 of the License, or
1933+# (at your option) any later version.
1934+#
1935+# This program is distributed in the hope that it will be useful,
1936+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1937+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1938+# GNU General Public License for more details.
1939+#
1940+# You should have received a copy of the GNU General Public License
523b37e3 1941+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1942+
1943+Branch Manipulation
1944+
1945+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1946+and changing its permission/attribute, there are a lot of works to do.
1947+
1948+
1949+Add a Branch
1950+----------------------------------------------------------------------
1951+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1952+ mount, and its various attributes.
53392da6
AM
1953+o Initialize the xino file and whiteout bases if necessary.
1954+ See struct.txt.
1955+
1956+o Check the owner/group/mode of the directory
1957+ When the owner/group/mode of the adding directory differs from the
1958+ existing branch, aufs issues a warning because it may impose a
1959+ security risk.
1960+ For example, when a upper writable branch has a world writable empty
1961+ top directory, a malicious user can create any files on the writable
1962+ branch directly, like copy-up and modify manually. If something like
1963+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1964+ writable branch, and the writable branch is world-writable, then a
1965+ malicious guy may create /etc/passwd on the writable branch directly
1966+ and the infected file will be valid in aufs.
7e9cd9fe 1967+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1968+ producing a warning.
1969+
1970+
1971+Delete a Branch
1972+----------------------------------------------------------------------
1973+o Confirm the deleting branch is not busy
1974+ To be general, there is one merit to adopt "remount" interface to
1975+ manipulate branches. It is to discard caches. At deleting a branch,
1976+ aufs checks the still cached (and connected) dentries and inodes. If
1977+ there are any, then they are all in-use. An inode without its
1978+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1979+
1980+ For the cached one, aufs checks whether the same named entry exists on
1981+ other branches.
1982+ If the cached one is a directory, because aufs provides a merged view
1983+ to users, as long as one dir is left on any branch aufs can show the
1984+ dir to users. In this case, the branch can be removed from aufs.
1985+ Otherwise aufs rejects deleting the branch.
1986+
1987+ If any file on the deleting branch is opened by aufs, then aufs
1988+ rejects deleting.
1989+
1990+
1991+Modify the Permission of a Branch
1992+----------------------------------------------------------------------
1993+o Re-initialize or remove the xino file and whiteout bases if necessary.
1994+ See struct.txt.
1995+
1996+o rw --> ro: Confirm the modifying branch is not busy
1997+ Aufs rejects the request if any of these conditions are true.
1998+ - a file on the branch is mmap-ed.
1999+ - a regular file on the branch is opened for write and there is no
2000+ same named entry on the upper branch.
2001diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
2002--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 2003+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2018-04-06 07:48:44.201271351 +0200
523b37e3 2004@@ -0,0 +1,64 @@
53392da6 2005+
a2654f78 2006+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
2007+#
2008+# This program is free software; you can redistribute it and/or modify
2009+# it under the terms of the GNU General Public License as published by
2010+# the Free Software Foundation; either version 2 of the License, or
2011+# (at your option) any later version.
2012+#
2013+# This program is distributed in the hope that it will be useful,
2014+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2015+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2016+# GNU General Public License for more details.
2017+#
2018+# You should have received a copy of the GNU General Public License
523b37e3 2019+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2020+
2021+Policies to Select One among Multiple Writable Branches
2022+----------------------------------------------------------------------
2023+When the number of writable branch is more than one, aufs has to decide
2024+the target branch for file creation or copy-up. By default, the highest
2025+writable branch which has the parent (or ancestor) dir of the target
2026+file is chosen (top-down-parent policy).
2027+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
2028+writable branch, for file creation several policies, round-robin,
2029+most-free-space, and other policies. For copy-up, top-down-parent,
2030+bottom-up-parent, bottom-up and others.
53392da6
AM
2031+
2032+As expected, the round-robin policy selects the branch in circular. When
2033+you have two writable branches and creates 10 new files, 5 files will be
2034+created for each branch. mkdir(2) systemcall is an exception. When you
2035+create 10 new directories, all will be created on the same branch.
2036+And the most-free-space policy selects the one which has most free
2037+space among the writable branches. The amount of free space will be
2038+checked by aufs internally, and users can specify its time interval.
2039+
2040+The policies for copy-up is more simple,
2041+top-down-parent is equivalent to the same named on in create policy,
2042+bottom-up-parent selects the writable branch where the parent dir
2043+exists and the nearest upper one from the copyup-source,
2044+bottom-up selects the nearest upper writable branch from the
2045+copyup-source, regardless the existence of the parent dir.
2046+
2047+There are some rules or exceptions to apply these policies.
2048+- If there is a readonly branch above the policy-selected branch and
2049+ the parent dir is marked as opaque (a variation of whiteout), or the
2050+ target (creating) file is whiteout-ed on the upper readonly branch,
2051+ then the result of the policy is ignored and the target file will be
2052+ created on the nearest upper writable branch than the readonly branch.
2053+- If there is a writable branch above the policy-selected branch and
2054+ the parent dir is marked as opaque or the target file is whiteouted
2055+ on the branch, then the result of the policy is ignored and the target
2056+ file will be created on the highest one among the upper writable
2057+ branches who has diropq or whiteout. In case of whiteout, aufs removes
2058+ it as usual.
2059+- link(2) and rename(2) systemcalls are exceptions in every policy.
2060+ They try selecting the branch where the source exists as possible
2061+ since copyup a large file will take long time. If it can't be,
2062+ ie. the branch where the source exists is readonly, then they will
2063+ follow the copyup policy.
2064+- There is an exception for rename(2) when the target exists.
2065+ If the rename target exists, aufs compares the index of the branches
2066+ where the source and the target exists and selects the higher
2067+ one. If the selected branch is readonly, then aufs follows the
2068+ copyup policy.
8b6a4947
AM
2069diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
2070--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
be118d29 2071+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2018-04-06 07:48:44.201271351 +0200
8b6a4947
AM
2072@@ -0,0 +1,31 @@
2073+
2074+// to view this graph, run dot(1) command in GRAPHVIZ.
2075+
2076+digraph G {
2077+node [shape=box];
2078+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
2079+
2080+node [shape=oval];
2081+
2082+aufs_rename -> whinfo [label="store/remove"];
2083+
2084+node [shape=oval];
2085+inode_list [label="h_inum list in branch\ncache"];
2086+
2087+node [shape=box];
2088+whinode [label="h_inum list file"];
2089+
2090+node [shape=oval];
2091+brmgmt [label="br_add/del/mod/umount"];
2092+
2093+brmgmt -> inode_list [label="create/remove"];
2094+brmgmt -> whinode [label="load/store"];
2095+
2096+inode_list -> whinode [style=dashed,dir=both];
2097+
2098+aufs_rename -> inode_list [label="add/del"];
2099+
2100+aufs_lookup -> inode_list [label="search"];
2101+
2102+aufs_lookup -> whinfo [label="load/remove"];
2103+}
2104diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
2105--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 2106+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2018-04-06 07:48:44.201271351 +0200
8b6a4947
AM
2107@@ -0,0 +1,102 @@
2108+
2109+# Copyright (C) 2017 Junjiro R. Okajima
2110+#
2111+# This program is free software; you can redistribute it and/or modify
2112+# it under the terms of the GNU General Public License as published by
2113+# the Free Software Foundation; either version 2 of the License, or
2114+# (at your option) any later version.
2115+#
2116+# This program is distributed in the hope that it will be useful,
2117+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2118+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2119+# GNU General Public License for more details.
2120+#
2121+# You should have received a copy of the GNU General Public License
2122+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2123+
2124+Special handling for renaming a directory (DIRREN)
2125+----------------------------------------------------------------------
2126+First, let's assume we have a simple usecase.
2127+
2128+- /u = /rw + /ro
2129+- /rw/dirA exists
2130+- /ro/dirA and /ro/dirA/file exist too
2131+- there is no dirB on both branches
2132+- a user issues rename("dirA", "dirB")
2133+
2134+Now, what should aufs behave against this rename(2)?
2135+There are a few possible cases.
2136+
2137+A. returns EROFS.
2138+ since dirA exists on a readonly branch which cannot be renamed.
2139+B. returns EXDEV.
2140+ it is possible to copy-up dirA (only the dir itself), but the child
2141+ entries ("file" in this case) should not be. it must be a bad
2142+ approach to copy-up recursively.
2143+C. returns a success.
2144+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
2145+ is a violation of aufs' policy.
2146+D. construct an extra information which indicates that /ro/dirA should
2147+ be handled as the name of dirB.
2148+ overlayfs has a similar feature called REDIRECT.
2149+
2150+Until now, aufs implements the case B only which returns EXDEV, and
2151+expects the userspace application behaves like mv(1) which tries
2152+issueing rename(2) recursively.
2153+
2154+A new aufs feature called DIRREN is introduced which implements the case
2155+D. There are several "extra information" added.
2156+
2157+1. detailed info per renamed directory
2158+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2159+2. the inode-number list of directories on a branch
2160+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
2161+
2162+The filename of "detailed info per directory" represents the lower
2163+branch, and its format is
2164+- a type of the branch id
2165+ one of these.
2166+ + uuid (not implemented yet)
2167+ + fsid
2168+ + dev
2169+- the inode-number of the branch root dir
2170+
2171+And it contains these info in a single regular file.
2172+- magic number
2173+- branch's inode-number of the logically renamed dir
2174+- the name of the before-renamed dir
2175+
2176+The "detailed info per directory" file is created in aufs rename(2), and
2177+loaded in any lookup.
2178+The info is considered in lookup for the matching case only. Here
2179+"matching" means that the root of branch (in the info filename) is same
2180+to the current looking-up branch. After looking-up the before-renamed
2181+name, the inode-number is compared. And the matched dentry is used.
2182+
2183+The "inode-number list of directories" is a regular file which contains
2184+simply the inode-numbers on the branch. The file is created or updated
2185+in removing the branch, and loaded in adding the branch. Its lifetime is
2186+equal to the branch.
2187+The list is refered in lookup, and when the current target inode is
2188+found in the list, the aufs tries loading the "detailed info per
2189+directory" and get the changed and valid name of the dir.
2190+
2191+Theoretically these "extra informaiton" may be able to be put into XATTR
2192+in the dir inode. But aufs doesn't choose this way because
2193+1. XATTR may not be supported by the branch (or its configuration)
2194+2. XATTR may have its size limit.
2195+3. XATTR may be less easy to convert than a regular file, when the
2196+ format of the info is changed in the future.
2197+At the same time, I agree that the regular file approach is much slower
2198+than XATTR approach. So, in the future, aufs may take the XATTR or other
2199+better approach.
2200+
2201+This DIRREN feature is enabled by aufs configuration, and is activated
2202+by a new mount option.
2203+
2204+For the more complicated case, there is a work with UDBA option, which
2205+is to dected the direct access to the branches (by-passing aufs) and to
2206+maintain the cashes in aufs. Since a single cached aufs dentry may
2207+contains two names, before- and after-rename, the name comparision in
2208+UDBA handler may not work correctly. In this case, the behaviour will be
2209+equivalen to udba=reval case.
076b876e
AM
2210diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2211--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 2212+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2018-04-06 07:48:44.201271351 +0200
076b876e
AM
2213@@ -0,0 +1,120 @@
2214+
a2654f78 2215+# Copyright (C) 2011-2017 Junjiro R. Okajima
076b876e
AM
2216+#
2217+# This program is free software; you can redistribute it and/or modify
2218+# it under the terms of the GNU General Public License as published by
2219+# the Free Software Foundation; either version 2 of the License, or
2220+# (at your option) any later version.
2221+#
2222+# This program is distributed in the hope that it will be useful,
2223+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2224+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2225+# GNU General Public License for more details.
2226+#
2227+# You should have received a copy of the GNU General Public License
2228+# along with this program; if not, write to the Free Software
2229+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2230+
2231+
2232+File-based Hierarchical Storage Management (FHSM)
2233+----------------------------------------------------------------------
2234+Hierarchical Storage Management (or HSM) is a well-known feature in the
2235+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 2236+writable branches, based upon the principle of "Colder, the Lower".
076b876e 2237+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 2238+that the position in the order of the stacked branches vertically.
076b876e
AM
2239+These multiple writable branches are prioritized, ie. the topmost one
2240+should be the fastest drive and be used heavily.
2241+
2242+o Characters in aufs FHSM story
2243+- aufs itself and a new branch attribute.
2244+- a new ioctl interface to move-down and to establish a connection with
2245+ the daemon ("move-down" is a converse of "copy-up").
2246+- userspace tool and daemon.
2247+
2248+The userspace daemon establishes a connection with aufs and waits for
2249+the notification. The notified information is very similar to struct
2250+statfs containing the number of consumed blocks and inodes.
2251+When the consumed blocks/inodes of a branch exceeds the user-specified
2252+upper watermark, the daemon activates its move-down process until the
2253+consumed blocks/inodes reaches the user-specified lower watermark.
2254+
2255+The actual move-down is done by aufs based upon the request from
2256+user-space since we need to maintain the inode number and the internal
2257+pointer arrays in aufs.
2258+
2259+Currently aufs FHSM handles the regular files only. Additionally they
2260+must not be hard-linked nor pseudo-linked.
2261+
2262+
2263+o Cowork of aufs and the user-space daemon
2264+ During the userspace daemon established the connection, aufs sends a
2265+ small notification to it whenever aufs writes something into the
2266+ writable branch. But it may cost high since aufs issues statfs(2)
2267+ internally. So user can specify a new option to cache the
2268+ info. Actually the notification is controlled by these factors.
2269+ + the specified cache time.
2270+ + classified as "force" by aufs internally.
2271+ Until the specified time expires, aufs doesn't send the info
2272+ except the forced cases. When aufs decide forcing, the info is always
2273+ notified to userspace.
2274+ For example, the number of free inodes is generally large enough and
2275+ the shortage of it happens rarely. So aufs doesn't force the
2276+ notification when creating a new file, directory and others. This is
2277+ the typical case which aufs doesn't force.
2278+ When aufs writes the actual filedata and the files consumes any of new
2279+ blocks, the aufs forces notifying.
2280+
2281+
2282+o Interfaces in aufs
2283+- New branch attribute.
2284+ + fhsm
2285+ Specifies that the branch is managed by FHSM feature. In other word,
2286+ participant in the FHSM.
2287+ When nofhsm is set to the branch, it will not be the source/target
2288+ branch of the move-down operation. This attribute is set
2289+ independently from coo and moo attributes, and if you want full
2290+ FHSM, you should specify them as well.
2291+- New mount option.
2292+ + fhsm_sec
2293+ Specifies a second to suppress many less important info to be
2294+ notified.
2295+- New ioctl.
2296+ + AUFS_CTL_FHSM_FD
2297+ create a new file descriptor which userspace can read the notification
2298+ (a subset of struct statfs) from aufs.
2299+- Module parameter 'brs'
2300+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2301+ be set.
2302+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2303+ When there are two or more branches with fhsm attributes,
2304+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2305+ terminates it. As a result of remounting and branch-manipulation, the
2306+ number of branches with fhsm attribute can be one. In this case,
2307+ /sbin/mount.aufs will terminate the user-space daemon.
2308+
2309+
2310+Finally the operation is done as these steps in kernel-space.
2311+- make sure that,
2312+ + no one else is using the file.
2313+ + the file is not hard-linked.
2314+ + the file is not pseudo-linked.
2315+ + the file is a regular file.
2316+ + the parent dir is not opaqued.
2317+- find the target writable branch.
2318+- make sure the file is not whiteout-ed by the upper (than the target)
2319+ branch.
2320+- make the parent dir on the target branch.
2321+- mutex lock the inode on the branch.
2322+- unlink the whiteout on the target branch (if exists).
2323+- lookup and create the whiteout-ed temporary name on the target branch.
2324+- copy the file as the whiteout-ed temporary name on the target branch.
2325+- rename the whiteout-ed temporary name to the original name.
2326+- unlink the file on the source branch.
2327+- maintain the internal pointer array and the external inode number
2328+ table (XINO).
2329+- maintain the timestamps and other attributes of the parent dir and the
2330+ file.
2331+
2332+And of course, in every step, an error may happen. So the operation
2333+should restore the original file state after an error happens.
53392da6
AM
2334diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2335--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 2336+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2018-04-06 07:48:44.201271351 +0200
b912730e 2337@@ -0,0 +1,72 @@
53392da6 2338+
a2654f78 2339+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
2340+#
2341+# This program is free software; you can redistribute it and/or modify
2342+# it under the terms of the GNU General Public License as published by
2343+# the Free Software Foundation; either version 2 of the License, or
2344+# (at your option) any later version.
2345+#
2346+# This program is distributed in the hope that it will be useful,
2347+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2348+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2349+# GNU General Public License for more details.
2350+#
2351+# You should have received a copy of the GNU General Public License
523b37e3 2352+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2353+
2354+mmap(2) -- File Memory Mapping
2355+----------------------------------------------------------------------
2356+In aufs, the file-mapped pages are handled by a branch fs directly, no
2357+interaction with aufs. It means aufs_mmap() calls the branch fs's
2358+->mmap().
2359+This approach is simple and good, but there is one problem.
7e9cd9fe 2360+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2361+device and inode number), and the printed path will be the path on the
2362+branch fs's instead of virtual aufs's.
2363+This is not a problem in most cases, but some utilities lsof(1) (and its
2364+user) may expect the path on aufs.
2365+
2366+To address this issue, aufs adds a new member called vm_prfile in struct
2367+vm_area_struct (and struct vm_region). The original vm_file points to
2368+the file on the branch fs in order to handle everything correctly as
2369+usual. The new vm_prfile points to a virtual file in aufs, and the
2370+show-functions in procfs refers to vm_prfile if it is set.
2371+Also we need to maintain several other places where touching vm_file
2372+such like
2373+- fork()/clone() copies vma and the reference count of vm_file is
2374+ incremented.
2375+- merging vma maintains the ref count too.
2376+
7e9cd9fe 2377+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2378+leaves all behaviour around f_mapping unchanged. This is surely an
2379+advantage.
2380+Actually aufs had adopted another complicated approach which calls
2381+generic_file_mmap() and handles struct vm_operations_struct. In this
2382+approach, aufs met a hard problem and I could not solve it without
2383+switching the approach.
b912730e
AM
2384+
2385+There may be one more another approach which is
2386+- bind-mount the branch-root onto the aufs-root internally
2387+- grab the new vfsmount (ie. struct mount)
2388+- lazy-umount the branch-root internally
2389+- in open(2) the aufs-file, open the branch-file with the hidden
2390+ vfsmount (instead of the original branch's vfsmount)
2391+- ideally this "bind-mount and lazy-umount" should be done atomically,
2392+ but it may be possible from userspace by the mount helper.
2393+
2394+Adding the internal hidden vfsmount and using it in opening a file, the
2395+file path under /proc will be printed correctly. This approach looks
2396+smarter, but is not possible I am afraid.
2397+- aufs-root may be bind-mount later. when it happens, another hidden
2398+ vfsmount will be required.
2399+- it is hard to get the chance to bind-mount and lazy-umount
2400+ + in kernel-space, FS can have vfsmount in open(2) via
2401+ file->f_path, and aufs can know its vfsmount. But several locks are
2402+ already acquired, and if aufs tries to bind-mount and lazy-umount
2403+ here, then it may cause a deadlock.
2404+ + in user-space, bind-mount doesn't invoke the mount helper.
2405+- since /proc shows dev and ino, aufs has to give vma these info. it
2406+ means a new member vm_prinode will be necessary. this is essentially
2407+ equivalent to vm_prfile described above.
2408+
2409+I have to give up this "looks-smater" approach.
c1595e42
JR
2410diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2411--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 2412+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2018-04-06 07:48:44.201271351 +0200
c1595e42
JR
2413@@ -0,0 +1,96 @@
2414+
a2654f78 2415+# Copyright (C) 2014-2017 Junjiro R. Okajima
c1595e42
JR
2416+#
2417+# This program is free software; you can redistribute it and/or modify
2418+# it under the terms of the GNU General Public License as published by
2419+# the Free Software Foundation; either version 2 of the License, or
2420+# (at your option) any later version.
2421+#
2422+# This program is distributed in the hope that it will be useful,
2423+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2424+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2425+# GNU General Public License for more details.
2426+#
2427+# You should have received a copy of the GNU General Public License
2428+# along with this program; if not, write to the Free Software
2429+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2430+
2431+
2432+Listing XATTR/EA and getting the value
2433+----------------------------------------------------------------------
2434+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2435+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2436+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2437+information. But for the directories, aufs considers all the same named
2438+entries on the lower branches. Which means, if one of the lower entry
2439+rejects readdir call, then aufs returns an error even if the topmost
2440+entry allows it. This behaviour is necessary to respect the branch fs's
2441+security, but can make users confused since the user-visible standard
2442+attributes don't match the behaviour.
2443+To address this issue, aufs has a mount option called dirperm1 which
2444+checks the permission for the topmost entry only, and ignores the lower
2445+entry's permission.
2446+
2447+A similar issue can happen around XATTR.
2448+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2449+always set. Otherwise these very unpleasant situation would happen.
2450+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2451+- users may not be able to remove or reset the XATTR forever,
2452+
2453+
2454+XATTR/EA support in the internal (copy,move)-(up,down)
2455+----------------------------------------------------------------------
7e9cd9fe 2456+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2457+- "security" for LSM and capability.
2458+- "system" for posix ACL, 'acl' mount option is required for the branch
2459+ fs generally.
2460+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2461+- "user" for userspace, 'user_xattr' mount option is required for the
2462+ branch fs generally.
2463+
2464+Moreover there are some other categories. Aufs handles these rather
2465+unpopular categories as the ordinary ones, ie. there is no special
2466+condition nor exception.
2467+
2468+In copy-up, the support for XATTR on the dst branch may differ from the
2469+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2470+the original user operation which triggered the copy-up will fail. It
2471+can happen that even all copy-up will fail.
c1595e42
JR
2472+When both of src and dst branches support XATTR and if an error occurs
2473+during copying XATTR, then the copy-up should fail obviously. That is a
2474+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2475+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2476+For example, the src branch supports ACL but the dst branch doesn't
2477+because the dst branch may natively un-support it or temporary
2478+un-support it due to "noacl" mount option. Of course, the dst branch fs
2479+may NOT return an error even if the XATTR is not supported. It is
2480+totally up to the branch fs.
2481+
2482+Anyway when the aufs internal copy-up gets an error from the dst branch
2483+fs, then aufs tries removing the just copied entry and returns the error
2484+to the userspace. The worst case of this situation will be all copy-up
2485+will fail.
2486+
2487+For the copy-up operation, there two basic approaches.
2488+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2489+ error unconditionally if it happens.
c1595e42
JR
2490+- copy all XATTR, and ignore the error on the specified category only.
2491+
2492+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2493+chooses the latter approach and introduces some new branch attributes,
2494+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2495+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2496+convenient, "icex" is also provided which means all "icex*" attributes
2497+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2498+
2499+The meaning of these attributes is to ignore the error from setting
2500+XATTR on that branch.
2501+Note that aufs tries copying all XATTR unconditionally, and ignores the
2502+error from the dst branch according to the specified attributes.
2503+
2504+Some XATTR may have its default value. The default value may come from
2505+the parent dir or the environment. If the default value is set at the
2506+file creating-time, it will be overwritten by copy-up.
2507+Some contradiction may happen I am afraid.
2508+Do we need another attribute to stop copying XATTR? I am unsure. For
2509+now, aufs implements the branch attributes to ignore the error.
53392da6
AM
2510diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2511--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 2512+++ linux/Documentation/filesystems/aufs/design/07export.txt 2018-04-06 07:48:44.201271351 +0200
523b37e3 2513@@ -0,0 +1,58 @@
53392da6 2514+
a2654f78 2515+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
2516+#
2517+# This program is free software; you can redistribute it and/or modify
2518+# it under the terms of the GNU General Public License as published by
2519+# the Free Software Foundation; either version 2 of the License, or
2520+# (at your option) any later version.
2521+#
2522+# This program is distributed in the hope that it will be useful,
2523+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2524+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2525+# GNU General Public License for more details.
2526+#
2527+# You should have received a copy of the GNU General Public License
523b37e3 2528+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2529+
2530+Export Aufs via NFS
2531+----------------------------------------------------------------------
2532+Here is an approach.
2533+- like xino/xib, add a new file 'xigen' which stores aufs inode
2534+ generation.
2535+- iget_locked(): initialize aufs inode generation for a new inode, and
2536+ store it in xigen file.
2537+- destroy_inode(): increment aufs inode generation and store it in xigen
2538+ file. it is necessary even if it is not unlinked, because any data of
2539+ inode may be changed by UDBA.
2540+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2541+ build file handle by
2542+ + branch id (4 bytes)
2543+ + superblock generation (4 bytes)
2544+ + inode number (4 or 8 bytes)
2545+ + parent dir inode number (4 or 8 bytes)
2546+ + inode generation (4 bytes))
2547+ + return value of exportfs_encode_fh() for the parent on a branch (4
2548+ bytes)
2549+ + file handle for a branch (by exportfs_encode_fh())
2550+- fh_to_dentry():
2551+ + find the index of a branch from its id in handle, and check it is
2552+ still exist in aufs.
2553+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2554+ + 2nd level: if not found in cache, get the parent inode number from
2555+ the handle and search it in cache. and then open the found parent
2556+ dir, find the matching inode number by vfs_readdir() and get its
2557+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2558+ + 3rd level: if the parent dir is not cached, call
2559+ exportfs_decode_fh() for a branch and get the parent on a branch,
2560+ build a pathname of it, convert it a pathname in aufs, call
2561+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2562+ the 2nd level.
2563+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2564+ for every branch, but not itself. to get this, (currently) aufs
2565+ searches in current->nsproxy->mnt_ns list. it may not be a good
2566+ idea, but I didn't get other approach.
2567+ + test the generation of the gotten inode.
2568+- every inode operation: they may get EBUSY due to UDBA. in this case,
2569+ convert it into ESTALE for NFSD.
2570+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2571+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2572diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2573--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 2574+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2018-04-06 07:48:44.201271351 +0200
523b37e3 2575@@ -0,0 +1,52 @@
53392da6 2576+
a2654f78 2577+# Copyright (C) 2005-2017 Junjiro R. Okajima
53392da6
AM
2578+#
2579+# This program is free software; you can redistribute it and/or modify
2580+# it under the terms of the GNU General Public License as published by
2581+# the Free Software Foundation; either version 2 of the License, or
2582+# (at your option) any later version.
2583+#
2584+# This program is distributed in the hope that it will be useful,
2585+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2586+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2587+# GNU General Public License for more details.
2588+#
2589+# You should have received a copy of the GNU General Public License
523b37e3 2590+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2591+
2592+Show Whiteout Mode (shwh)
2593+----------------------------------------------------------------------
2594+Generally aufs hides the name of whiteouts. But in some cases, to show
2595+them is very useful for users. For instance, creating a new middle layer
2596+(branch) by merging existing layers.
2597+
2598+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2599+When you have three branches,
2600+- Bottom: 'system', squashfs (underlying base system), read-only
2601+- Middle: 'mods', squashfs, read-only
2602+- Top: 'overlay', ram (tmpfs), read-write
2603+
2604+The top layer is loaded at boot time and saved at shutdown, to preserve
2605+the changes made to the system during the session.
2606+When larger changes have been made, or smaller changes have accumulated,
2607+the size of the saved top layer data grows. At this point, it would be
2608+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2609+and rewrite the 'mods' squashfs, clearing the top layer and thus
2610+restoring save and load speed.
2611+
2612+This merging is simplified by the use of another aufs mount, of just the
2613+two overlay branches using the 'shwh' option.
2614+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2615+ aufs /livesys/merge_union
2616+
2617+A merged view of these two branches is then available at
2618+/livesys/merge_union, and the new feature is that the whiteouts are
2619+visible!
2620+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2621+writing to all branches. Also the default mode for all branches is 'ro'.
2622+It is now possible to save the combined contents of the two overlay
2623+branches to a new squashfs, e.g.:
2624+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2625+
2626+This new squashfs archive can be stored on the boot device and the
2627+initramfs will use it to replace the old one at the next boot.
2628diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2629--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
be118d29 2630+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2018-04-06 07:48:44.201271351 +0200
7e9cd9fe 2631@@ -0,0 +1,47 @@
53392da6 2632+
a2654f78 2633+# Copyright (C) 2010-2017 Junjiro R. Okajima
53392da6
AM
2634+#
2635+# This program is free software; you can redistribute it and/or modify
2636+# it under the terms of the GNU General Public License as published by
2637+# the Free Software Foundation; either version 2 of the License, or
2638+# (at your option) any later version.
2639+#
2640+# This program is distributed in the hope that it will be useful,
2641+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2642+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2643+# GNU General Public License for more details.
2644+#
2645+# You should have received a copy of the GNU General Public License
523b37e3 2646+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2647+
2648+Dynamically customizable FS operations
2649+----------------------------------------------------------------------
2650+Generally FS operations (struct inode_operations, struct
2651+address_space_operations, struct file_operations, etc.) are defined as
2652+"static const", but it never means that FS have only one set of
2653+operation. Some FS have multiple sets of them. For instance, ext2 has
2654+three sets, one for XIP, for NOBH, and for normal.
2655+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2656+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2657+VFS acts differently if a function (member in the struct) is set or
2658+not. It means aufs should have several sets of operations and select one
2659+among them according to the branch FS definition.
2660+
7e9cd9fe 2661+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2662+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2663+dummy direct_IO function for struct address_space_operations, but it may
2664+not be set to the address_space_operations actually. When the branch FS
2665+doesn't have it, aufs doesn't set it to its address_space_operations
2666+while the function definition itself is still alive. So the behaviour
2667+itself will not change, and it will return an error when direct_IO is
2668+not set.
53392da6
AM
2669+
2670+The lifetime of these dynamically generated operation object is
2671+maintained by aufs branch object. When the branch is removed from aufs,
2672+the reference counter of the object is decremented. When it reaches
2673+zero, the dynamically generated operation object will be freed.
2674+
7e9cd9fe
AM
2675+This approach is designed to support AIO (io_submit), Direct I/O and
2676+XIP (DAX) mainly.
2677+Currently this approach is applied to address_space_operations for
2678+regular files only.
53392da6
AM
2679diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2680--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
be118d29 2681+++ linux/Documentation/filesystems/aufs/README 2018-04-06 07:48:44.201271351 +0200
f2c43d5f 2682@@ -0,0 +1,393 @@
53392da6 2683+
5527c038 2684+Aufs4 -- advanced multi layered unification filesystem version 4.x
53392da6
AM
2685+http://aufs.sf.net
2686+Junjiro R. Okajima
2687+
2688+
2689+0. Introduction
2690+----------------------------------------
2691+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2692+Unionfs Version 1.x series. Adding many original ideas, approaches,
53392da6
AM
2693+improvements and implementations, it becomes totally different from
2694+Unionfs while keeping the basic features.
2695+Recently, Unionfs Version 2.x series begin taking some of the same
2696+approaches to aufs1's.
2697+Unionfs is being developed by Professor Erez Zadok at Stony Brook
2698+University and his team.
2699+
5527c038 2700+Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
53392da6
AM
2701+If you want older kernel version support, try aufs2-2.6.git or
2702+aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2703+
2704+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2705+ According to Christoph Hellwig, linux rejects all union-type
2706+ filesystems but UnionMount.
53392da6
AM
2707+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2708+
38d290e6
JR
2709+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2710+ UnionMount, and he pointed out an issue around a directory mutex
2711+ lock and aufs addressed it. But it is still unsure whether aufs will
2712+ be merged (or any other union solution).
076b876e 2713+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2714+
53392da6
AM
2715+
2716+1. Features
2717+----------------------------------------
2718+- unite several directories into a single virtual filesystem. The member
2719+ directory is called as a branch.
2720+- you can specify the permission flags to the branch, which are 'readonly',
2721+ 'readwrite' and 'whiteout-able.'
2722+- by upper writable branch, internal copyup and whiteout, files/dirs on
2723+ readonly branch are modifiable logically.
2724+- dynamic branch manipulation, add, del.
2725+- etc...
2726+
7e9cd9fe
AM
2727+Also there are many enhancements in aufs, such as:
2728+- test only the highest one for the directory permission (dirperm1)
2729+- copyup on open (coo=)
2730+- 'move' policy for copy-up between two writable branches, after
2731+ checking free space.
2732+- xattr, acl
53392da6
AM
2733+- readdir(3) in userspace.
2734+- keep inode number by external inode number table
2735+- keep the timestamps of file/dir in internal copyup operation
2736+- seekable directory, supporting NFS readdir.
2737+- whiteout is hardlinked in order to reduce the consumption of inodes
2738+ on branch
2739+- do not copyup, nor create a whiteout when it is unnecessary
2740+- revert a single systemcall when an error occurs in aufs
2741+- remount interface instead of ioctl
2742+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2743+- loopback mounted filesystem as a branch
2744+- kernel thread for removing the dir who has a plenty of whiteouts
2745+- support copyup sparse file (a file which has a 'hole' in it)
2746+- default permission flags for branches
2747+- selectable permission flags for ro branch, whether whiteout can
2748+ exist or not
2749+- export via NFS.
2750+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2751+- support multiple writable branches, some policies to select one
2752+ among multiple writable branches.
2753+- a new semantics for link(2) and rename(2) to support multiple
2754+ writable branches.
2755+- no glibc changes are required.
2756+- pseudo hardlink (hardlink over branches)
2757+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2758+ including NFS or remote filesystem branch.
2759+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2760+- and more...
2761+
5527c038 2762+Currently these features are dropped temporary from aufs4.
53392da6 2763+See design/08plan.txt in detail.
53392da6
AM
2764+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2765+ (robr)
2766+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2767+
2768+Features or just an idea in the future (see also design/*.txt),
2769+- reorder the branch index without del/re-add.
2770+- permanent xino files for NFSD
2771+- an option for refreshing the opened files after add/del branches
53392da6
AM
2772+- light version, without branch manipulation. (unnecessary?)
2773+- copyup in userspace
2774+- inotify in userspace
2775+- readv/writev
53392da6
AM
2776+
2777+
2778+2. Download
2779+----------------------------------------
5527c038
JR
2780+There are three GIT trees for aufs4, aufs4-linux.git,
2781+aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
1e00d052 2782+"aufs-util.git."
5527c038
JR
2783+While the aufs-util is always necessary, you need either of aufs4-linux
2784+or aufs4-standalone.
1e00d052 2785+
5527c038 2786+The aufs4-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2787+git://git.kernel.org/.../torvalds/linux.git.
2788+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
5527c038 2789+build aufs4 as an external kernel module.
2000de60 2790+Several extra patches are not included in this tree. Only
be52b249 2791+aufs4-standalone tree contains them. They are described in the later
2000de60 2792+section "Configuration and Compilation."
1e00d052 2793+
5527c038 2794+On the other hand, the aufs4-standalone tree has only aufs source files
53392da6 2795+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2796+But you need to apply all aufs patches manually.
53392da6 2797+
5527c038
JR
2798+You will find GIT branches whose name is in form of "aufs4.x" where "x"
2799+represents the linux kernel version, "linux-4.x". For instance,
2800+"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2801+"aufs4.x-rcN" branch.
1e00d052 2802+
5527c038 2803+o aufs4-linux tree
1e00d052 2804+$ git clone --reference /your/linux/git/tree \
5527c038 2805+ git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
1e00d052 2806+- if you don't have linux GIT tree, then remove "--reference ..."
5527c038
JR
2807+$ cd aufs4-linux.git
2808+$ git checkout origin/aufs4.0
53392da6 2809+
2000de60
JR
2810+Or You may want to directly git-pull aufs into your linux GIT tree, and
2811+leave the patch-work to GIT.
2812+$ cd /your/linux/git/tree
5527c038
JR
2813+$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2814+$ git fetch aufs4
2815+$ git checkout -b my4.0 v4.0
2816+$ (add your local change...)
2817+$ git pull aufs4 aufs4.0
2818+- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2000de60 2819+- you may need to solve some conflicts between your_changes and
5527c038
JR
2820+ aufs4.0. in this case, git-rerere is recommended so that you can
2821+ solve the similar conflicts automatically when you upgrade to 4.1 or
2000de60
JR
2822+ later in the future.
2823+
5527c038
JR
2824+o aufs4-standalone tree
2825+$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2826+$ cd aufs4-standalone.git
2827+$ git checkout origin/aufs4.0
53392da6
AM
2828+
2829+o aufs-util tree
5527c038
JR
2830+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2831+- note that the public aufs-util.git is on SourceForge instead of
2832+ GitHUB.
53392da6 2833+$ cd aufs-util.git
5527c038 2834+$ git checkout origin/aufs4.0
53392da6 2835+
5527c038
JR
2836+Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2837+The minor version number, 'x' in '4.x', of aufs may not always
9dbd164d
AM
2838+follow the minor version number of the kernel.
2839+Because changes in the kernel that cause the use of a new
2840+minor version number do not always require changes to aufs-util.
2841+
2842+Since aufs-util has its own minor version number, you may not be
2843+able to find a GIT branch in aufs-util for your kernel's
2844+exact minor version number.
2845+In this case, you should git-checkout the branch for the
53392da6 2846+nearest lower number.
9dbd164d
AM
2847+
2848+For (an unreleased) example:
5527c038
JR
2849+If you are using "linux-4.10" and the "aufs4.10" branch
2850+does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
9dbd164d
AM
2851+or something numerically smaller is the branch for your kernel.
2852+
53392da6
AM
2853+Also you can view all branches by
2854+ $ git branch -a
2855+
2856+
2857+3. Configuration and Compilation
2858+----------------------------------------
2859+Make sure you have git-checkout'ed the correct branch.
2860+
5527c038 2861+For aufs4-linux tree,
c06a8ce3 2862+- enable CONFIG_AUFS_FS.
1e00d052
AM
2863+- set other aufs configurations if necessary.
2864+
5527c038 2865+For aufs4-standalone tree,
53392da6
AM
2866+There are several ways to build.
2867+
2868+1.
5527c038
JR
2869+- apply ./aufs4-kbuild.patch to your kernel source files.
2870+- apply ./aufs4-base.patch too.
2871+- apply ./aufs4-mmap.patch too.
2872+- apply ./aufs4-standalone.patch too, if you have a plan to set
2873+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
537831f9
AM
2874+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2875+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2876+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2877+ =m or =y.
2878+- and build your kernel as usual.
2879+- install the built kernel.
c06a8ce3
AM
2880+ Note: Since linux-3.9, every filesystem module requires an alias
2881+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2882+ modules.aliases file if you set CONFIG_AUFS_FS=m.
7eafdf33
AM
2883+- install the header files too by "make headers_install" to the
2884+ directory where you specify. By default, it is $PWD/usr.
b4510431 2885+ "make help" shows a brief note for headers_install.
53392da6
AM
2886+- and reboot your system.
2887+
2888+2.
2889+- module only (CONFIG_AUFS_FS=m).
5527c038
JR
2890+- apply ./aufs4-base.patch to your kernel source files.
2891+- apply ./aufs4-mmap.patch too.
2892+- apply ./aufs4-standalone.patch too.
53392da6
AM
2893+- build your kernel, don't forget "make headers_install", and reboot.
2894+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2895+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2896+ every aufs configurations.
2897+- build the module by simple "make".
c06a8ce3
AM
2898+ Note: Since linux-3.9, every filesystem module requires an alias
2899+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2900+ modules.aliases file.
53392da6
AM
2901+- you can specify ${KDIR} make variable which points to your kernel
2902+ source tree.
2903+- install the files
2904+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2905+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2906+ + run "make install_headers" (instead of headers_install) to install
2907+ the modified aufs header file (you can specify DESTDIR which is
2908+ available in aufs standalone version's Makefile only), or copy
2909+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2910+ you like manually. By default, the target directory is $PWD/usr.
5527c038 2911+- no need to apply aufs4-kbuild.patch, nor copying source files to your
53392da6
AM
2912+ kernel source tree.
2913+
b4510431 2914+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2915+ as well as "make headers_install" in the kernel source tree.
2916+ headers_install is subject to be forgotten, but it is essentially
2917+ necessary, not only for building aufs-util.
2918+ You may not meet problems without headers_install in some older
2919+ version though.
2920+
2921+And then,
2922+- read README in aufs-util, build and install it
9dbd164d
AM
2923+- note that your distribution may contain an obsoleted version of
2924+ aufs_type.h in /usr/include/linux or something. When you build aufs
2925+ utilities, make sure that your compiler refers the correct aufs header
2926+ file which is built by "make headers_install."
53392da6
AM
2927+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2928+ then run "make install_ulib" too. And refer to the aufs manual in
2929+ detail.
2930+
5527c038 2931+There several other patches in aufs4-standalone.git. They are all
38d290e6 2932+optional. When you meet some problems, they will help you.
5527c038 2933+- aufs4-loopback.patch
38d290e6
JR
2934+ Supports a nested loopback mount in a branch-fs. This patch is
2935+ unnecessary until aufs produces a message like "you may want to try
2936+ another patch for loopback file".
2937+- vfs-ino.patch
2938+ Modifies a system global kernel internal function get_next_ino() in
2939+ order to stop assigning 0 for an inode-number. Not directly related to
2940+ aufs, but recommended generally.
2941+- tmpfs-idr.patch
2942+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2943+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2944+ duplication of inode number, which is important for backup tools and
2945+ other utilities. When you find aufs XINO files for tmpfs branch
2946+ growing too much, try this patch.
be52b249
AM
2947+- lockdep-debug.patch
2948+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2949+ also a caller of VFS functions for branch filesystems, subclassing of
2950+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2951+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2952+ need to apply this debug patch to expand several constant values.
2953+ If don't know what LOCKDEP, then you don't have apply this patch.
38d290e6 2954+
53392da6
AM
2955+
2956+4. Usage
2957+----------------------------------------
2958+At first, make sure aufs-util are installed, and please read the aufs
2959+manual, aufs.5 in aufs-util.git tree.
2960+$ man -l aufs.5
2961+
2962+And then,
2963+$ mkdir /tmp/rw /tmp/aufs
2964+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2965+
2966+Here is another example. The result is equivalent.
2967+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2968+ Or
2969+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2970+# mount -o remount,append:${HOME} /tmp/aufs
2971+
2972+Then, you can see whole tree of your home dir through /tmp/aufs. If
2973+you modify a file under /tmp/aufs, the one on your home directory is
2974+not affected, instead the same named file will be newly created under
2975+/tmp/rw. And all of your modification to a file will be applied to
2976+the one under /tmp/rw. This is called the file based Copy on Write
2977+(COW) method.
2978+Aufs mount options are described in aufs.5.
2979+If you run chroot or something and make your aufs as a root directory,
2980+then you need to customize the shutdown script. See the aufs manual in
2981+detail.
2982+
2983+Additionally, there are some sample usages of aufs which are a
2984+diskless system with network booting, and LiveCD over NFS.
2985+See sample dir in CVS tree on SourceForge.
2986+
2987+
2988+5. Contact
2989+----------------------------------------
2990+When you have any problems or strange behaviour in aufs, please let me
2991+know with:
2992+- /proc/mounts (instead of the output of mount(8))
2993+- /sys/module/aufs/*
2994+- /sys/fs/aufs/* (if you have them)
2995+- /debug/aufs/* (if you have them)
2996+- linux kernel version
2997+ if your kernel is not plain, for example modified by distributor,
2998+ the url where i can download its source is necessary too.
2999+- aufs version which was printed at loading the module or booting the
3000+ system, instead of the date you downloaded.
3001+- configuration (define/undefine CONFIG_AUFS_xxx)
3002+- kernel configuration or /proc/config.gz (if you have it)
3003+- behaviour which you think to be incorrect
3004+- actual operation, reproducible one is better
3005+- mailto: aufs-users at lists.sourceforge.net
3006+
3007+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
3008+and Feature Requests) on SourceForge. Please join and write to
3009+aufs-users ML.
3010+
3011+
3012+6. Acknowledgements
3013+----------------------------------------
3014+Thanks to everyone who have tried and are using aufs, whoever
3015+have reported a bug or any feedback.
3016+
3017+Especially donators:
3018+Tomas Matejicek(slax.org) made a donation (much more than once).
3019+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
3020+ scripts) is making "doubling" donations.
3021+ Unfortunately I cannot list all of the donators, but I really
b4510431 3022+ appreciate.
53392da6
AM
3023+ It ends Aug 2010, but the ordinary donation URL is still available.
3024+ <http://sourceforge.net/donate/index.php?group_id=167503>
3025+Dai Itasaka made a donation (2007/8).
3026+Chuck Smith made a donation (2008/4, 10 and 12).
3027+Henk Schoneveld made a donation (2008/9).
3028+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
3029+Francois Dupoux made a donation (2008/11).
3030+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
3031+ aufs2 GIT tree (2009/2).
3032+William Grant made a donation (2009/3).
3033+Patrick Lane made a donation (2009/4).
3034+The Mail Archive (mail-archive.com) made donations (2009/5).
3035+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
3036+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
3037+Pavel Pronskiy made a donation (2011/2).
3038+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
3039+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
3040+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
3041+11).
1e00d052 3042+Sam Liddicott made a donation (2011/9).
86dc4139
AM
3043+Era Scarecrow made a donation (2013/4).
3044+Bor Ratajc made a donation (2013/4).
3045+Alessandro Gorreta made a donation (2013/4).
3046+POIRETTE Marc made a donation (2013/4).
3047+Alessandro Gorreta made a donation (2013/4).
3048+lauri kasvandik made a donation (2013/5).
392086de 3049+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
3050+The Parted Magic Project made a donation (2013/9 and 11).
3051+Pavel Barta made a donation (2013/10).
38d290e6 3052+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 3053+James B made a donation (2014/7 and 2015/7).
076b876e 3054+Stefano Di Biase made a donation (2014/8).
2000de60 3055+Daniel Epellei made a donation (2015/1).
8cdd5066 3056+OmegaPhil made a donation (2016/1).
5afbbe0d 3057+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 3058+James Burry made a donation (2016/12).
53392da6
AM
3059+
3060+Thank you very much.
3061+Donations are always, including future donations, very important and
3062+helpful for me to keep on developing aufs.
3063+
3064+
3065+7.
3066+----------------------------------------
3067+If you are an experienced user, no explanation is needed. Aufs is
3068+just a linux filesystem.
3069+
3070+
3071+Enjoy!
3072+
3073+# Local variables: ;
3074+# mode: text;
3075+# End: ;
7f207e10
AM
3076diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
3077--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
be118d29 3078+++ linux/fs/aufs/aufs.h 2018-04-06 07:48:44.201271351 +0200
8b6a4947 3079@@ -0,0 +1,60 @@
7f207e10 3080+/*
a2654f78 3081+ * Copyright (C) 2005-2017 Junjiro R. Okajima
7f207e10
AM
3082+ *
3083+ * This program, aufs is free software; you can redistribute it and/or modify
3084+ * it under the terms of the GNU General Public License as published by
3085+ * the Free Software Foundation; either version 2 of the License, or
3086+ * (at your option) any later version.
3087+ *
3088+ * This program is distributed in the hope that it will be useful,
3089+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3090+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3091+ * GNU General Public License for more details.
3092+ *
3093+ * You should have received a copy of the GNU General Public License
523b37e3 3094+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3095+ */
3096+
3097+/*
3098+ * all header files
3099+ */
3100+
3101+#ifndef __AUFS_H__
3102+#define __AUFS_H__
3103+
3104+#ifdef __KERNEL__
3105+
3106+#define AuStub(type, name, body, ...) \
3107+ static inline type name(__VA_ARGS__) { body; }
3108+
3109+#define AuStubVoid(name, ...) \
3110+ AuStub(void, name, , __VA_ARGS__)
3111+#define AuStubInt0(name, ...) \
3112+ AuStub(int, name, return 0, __VA_ARGS__)
3113+
3114+#include "debug.h"
3115+
3116+#include "branch.h"
3117+#include "cpup.h"
3118+#include "dcsub.h"
3119+#include "dbgaufs.h"
3120+#include "dentry.h"
3121+#include "dir.h"
8b6a4947 3122+#include "dirren.h"
7f207e10
AM
3123+#include "dynop.h"
3124+#include "file.h"
3125+#include "fstype.h"
8b6a4947 3126+#include "hbl.h"
7f207e10
AM
3127+#include "inode.h"
3128+#include "loop.h"
3129+#include "module.h"
7f207e10
AM
3130+#include "opts.h"
3131+#include "rwsem.h"
7f207e10
AM
3132+#include "super.h"
3133+#include "sysaufs.h"
3134+#include "vfsub.h"
3135+#include "whout.h"
3136+#include "wkq.h"
3137+
3138+#endif /* __KERNEL__ */
3139+#endif /* __AUFS_H__ */
3140diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3141--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
be118d29 3142+++ linux/fs/aufs/branch.c 2018-04-06 07:48:44.204604724 +0200
8b6a4947 3143@@ -0,0 +1,1432 @@
7f207e10 3144+/*
a2654f78 3145+ * Copyright (C) 2005-2017 Junjiro R. Okajima
7f207e10
AM
3146+ *
3147+ * This program, aufs is free software; you can redistribute it and/or modify
3148+ * it under the terms of the GNU General Public License as published by
3149+ * the Free Software Foundation; either version 2 of the License, or
3150+ * (at your option) any later version.
3151+ *
3152+ * This program is distributed in the hope that it will be useful,
3153+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3154+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3155+ * GNU General Public License for more details.
3156+ *
3157+ * You should have received a copy of the GNU General Public License
523b37e3 3158+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3159+ */
3160+
3161+/*
3162+ * branch management
3163+ */
3164+
027c5e7a 3165+#include <linux/compat.h>
7f207e10
AM
3166+#include <linux/statfs.h>
3167+#include "aufs.h"
3168+
3169+/*
3170+ * free a single branch
1facf9fc 3171+ */
3172+static void au_br_do_free(struct au_branch *br)
3173+{
3174+ int i;
3175+ struct au_wbr *wbr;
4a4d8108 3176+ struct au_dykey **key;
1facf9fc 3177+
027c5e7a 3178+ au_hnotify_fin_br(br);
8b6a4947
AM
3179+ /* always, regardless the mount option */
3180+ au_dr_hino_free(&br->br_dirren);
027c5e7a 3181+
1facf9fc 3182+ if (br->br_xino.xi_file)
3183+ fput(br->br_xino.xi_file);
521ced18
JR
3184+ for (i = br->br_xino.xi_nondir.total - 1; i >= 0; i--)
3185+ AuDebugOn(br->br_xino.xi_nondir.array[i]);
1c60b727 3186+ kfree(br->br_xino.xi_nondir.array);
1facf9fc 3187+
5afbbe0d
AM
3188+ AuDebugOn(au_br_count(br));
3189+ au_br_count_fin(br);
1facf9fc 3190+
3191+ wbr = br->br_wbr;
3192+ if (wbr) {
3193+ for (i = 0; i < AuBrWh_Last; i++)
3194+ dput(wbr->wbr_wh[i]);
3195+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 3196+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 3197+ }
3198+
076b876e
AM
3199+ if (br->br_fhsm) {
3200+ au_br_fhsm_fin(br->br_fhsm);
1c60b727 3201+ kfree(br->br_fhsm);
076b876e
AM
3202+ }
3203+
4a4d8108
AM
3204+ key = br->br_dykey;
3205+ for (i = 0; i < AuBrDynOp; i++, key++)
3206+ if (*key)
3207+ au_dy_put(*key);
3208+ else
3209+ break;
3210+
537831f9
AM
3211+ /* recursive lock, s_umount of branch's */
3212+ lockdep_off();
86dc4139 3213+ path_put(&br->br_path);
537831f9 3214+ lockdep_on();
1c60b727
AM
3215+ kfree(wbr);
3216+ kfree(br);
1facf9fc 3217+}
3218+
3219+/*
3220+ * frees all branches
3221+ */
3222+void au_br_free(struct au_sbinfo *sbinfo)
3223+{
3224+ aufs_bindex_t bmax;
3225+ struct au_branch **br;
3226+
dece6358
AM
3227+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3228+
5afbbe0d 3229+ bmax = sbinfo->si_bbot + 1;
1facf9fc 3230+ br = sbinfo->si_branch;
3231+ while (bmax--)
3232+ au_br_do_free(*br++);
3233+}
3234+
3235+/*
3236+ * find the index of a branch which is specified by @br_id.
3237+ */
3238+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3239+{
5afbbe0d 3240+ aufs_bindex_t bindex, bbot;
1facf9fc 3241+
5afbbe0d
AM
3242+ bbot = au_sbbot(sb);
3243+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3244+ if (au_sbr_id(sb, bindex) == br_id)
3245+ return bindex;
3246+ return -1;
3247+}
3248+
3249+/* ---------------------------------------------------------------------- */
3250+
3251+/*
3252+ * add a branch
3253+ */
3254+
b752ccd1
AM
3255+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3256+ struct dentry *h_root)
1facf9fc 3257+{
b752ccd1
AM
3258+ if (unlikely(h_adding == h_root
3259+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3260+ return 1;
b752ccd1
AM
3261+ if (h_adding->d_sb != h_root->d_sb)
3262+ return 0;
3263+ return au_test_subdir(h_adding, h_root)
3264+ || au_test_subdir(h_root, h_adding);
1facf9fc 3265+}
3266+
3267+/*
3268+ * returns a newly allocated branch. @new_nbranch is a number of branches
3269+ * after adding a branch.
3270+ */
3271+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3272+ int perm)
3273+{
3274+ struct au_branch *add_branch;
3275+ struct dentry *root;
5527c038 3276+ struct inode *inode;
4a4d8108 3277+ int err;
1facf9fc 3278+
4a4d8108 3279+ err = -ENOMEM;
be52b249 3280+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3281+ if (unlikely(!add_branch))
3282+ goto out;
521ced18
JR
3283+ add_branch->br_xino.xi_nondir.total = 8; /* initial size */
3284+ add_branch->br_xino.xi_nondir.array
8b6a4947 3285+ = kcalloc(add_branch->br_xino.xi_nondir.total, sizeof(ino_t),
521ced18
JR
3286+ GFP_NOFS);
3287+ if (unlikely(!add_branch->br_xino.xi_nondir.array))
3288+ goto out_br;
1facf9fc 3289+
027c5e7a
AM
3290+ err = au_hnotify_init_br(add_branch, perm);
3291+ if (unlikely(err))
521ced18 3292+ goto out_xinondir;
027c5e7a 3293+
1facf9fc 3294+ if (au_br_writable(perm)) {
3295+ /* may be freed separately at changing the branch permission */
be52b249 3296+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3297+ GFP_NOFS);
3298+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3299+ goto out_hnotify;
1facf9fc 3300+ }
3301+
076b876e
AM
3302+ if (au_br_fhsm(perm)) {
3303+ err = au_fhsm_br_alloc(add_branch);
3304+ if (unlikely(err))
3305+ goto out_wbr;
3306+ }
3307+
521ced18 3308+ root = sb->s_root;
e2f27e51 3309+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3310+ if (!err)
e2f27e51 3311+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3312+ if (!err) {
3313+ inode = d_inode(root);
1c60b727
AM
3314+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3315+ /*may_shrink*/0);
5527c038 3316+ }
4a4d8108
AM
3317+ if (!err)
3318+ return add_branch; /* success */
1facf9fc 3319+
076b876e 3320+out_wbr:
1c60b727 3321+ kfree(add_branch->br_wbr);
027c5e7a
AM
3322+out_hnotify:
3323+ au_hnotify_fin_br(add_branch);
521ced18 3324+out_xinondir:
1c60b727 3325+ kfree(add_branch->br_xino.xi_nondir.array);
4f0767ce 3326+out_br:
1c60b727 3327+ kfree(add_branch);
4f0767ce 3328+out:
4a4d8108 3329+ return ERR_PTR(err);
1facf9fc 3330+}
3331+
3332+/*
3333+ * test if the branch permission is legal or not.
3334+ */
3335+static int test_br(struct inode *inode, int brperm, char *path)
3336+{
3337+ int err;
3338+
4a4d8108
AM
3339+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3340+ if (!err)
3341+ goto out;
1facf9fc 3342+
4a4d8108
AM
3343+ err = -EINVAL;
3344+ pr_err("write permission for readonly mount or inode, %s\n", path);
3345+
4f0767ce 3346+out:
1facf9fc 3347+ return err;
3348+}
3349+
3350+/*
3351+ * returns:
3352+ * 0: success, the caller will add it
3353+ * plus: success, it is already unified, the caller should ignore it
3354+ * minus: error
3355+ */
3356+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3357+{
3358+ int err;
5afbbe0d 3359+ aufs_bindex_t bbot, bindex;
5527c038 3360+ struct dentry *root, *h_dentry;
1facf9fc 3361+ struct inode *inode, *h_inode;
3362+
3363+ root = sb->s_root;
5afbbe0d
AM
3364+ bbot = au_sbbot(sb);
3365+ if (unlikely(bbot >= 0
1facf9fc 3366+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3367+ err = 1;
3368+ if (!remount) {
3369+ err = -EINVAL;
4a4d8108 3370+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3371+ }
3372+ goto out;
3373+ }
3374+
3375+ err = -ENOSPC; /* -E2BIG; */
3376+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3377+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3378+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3379+ goto out;
3380+ }
3381+
3382+ err = -EDOM;
5afbbe0d 3383+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3384+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3385+ goto out;
3386+ }
3387+
5527c038 3388+ inode = d_inode(add->path.dentry);
1facf9fc 3389+ err = -ENOENT;
3390+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3391+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3392+ goto out;
3393+ }
3394+
3395+ err = -EINVAL;
3396+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3397+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3398+ goto out;
3399+ }
3400+
3401+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3402+ pr_err("unsupported filesystem, %s (%s)\n",
3403+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3404+ goto out;
3405+ }
3406+
c1595e42
JR
3407+ if (unlikely(inode->i_sb->s_stack_depth)) {
3408+ pr_err("already stacked, %s (%s)\n",
3409+ add->pathname, au_sbtype(inode->i_sb));
3410+ goto out;
3411+ }
3412+
5527c038 3413+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3414+ if (unlikely(err))
3415+ goto out;
3416+
5afbbe0d 3417+ if (bbot < 0)
1facf9fc 3418+ return 0; /* success */
3419+
3420+ err = -EINVAL;
5afbbe0d 3421+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3422+ if (unlikely(test_overlap(sb, add->path.dentry,
3423+ au_h_dptr(root, bindex)))) {
4a4d8108 3424+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3425+ goto out;
3426+ }
3427+
3428+ err = 0;
3429+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3430+ h_dentry = au_h_dptr(root, 0);
3431+ h_inode = d_inode(h_dentry);
1facf9fc 3432+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3433+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3434+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3435+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3436+ add->pathname,
3437+ i_uid_read(inode), i_gid_read(inode),
3438+ (inode->i_mode & S_IALLUGO),
3439+ i_uid_read(h_inode), i_gid_read(h_inode),
3440+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3441+ }
3442+
4f0767ce 3443+out:
1facf9fc 3444+ return err;
3445+}
3446+
3447+/*
3448+ * initialize or clean the whiteouts for an adding branch
3449+ */
3450+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3451+ int new_perm)
1facf9fc 3452+{
3453+ int err, old_perm;
3454+ aufs_bindex_t bindex;
febd17d6 3455+ struct inode *h_inode;
1facf9fc 3456+ struct au_wbr *wbr;
3457+ struct au_hinode *hdir;
5527c038 3458+ struct dentry *h_dentry;
1facf9fc 3459+
86dc4139
AM
3460+ err = vfsub_mnt_want_write(au_br_mnt(br));
3461+ if (unlikely(err))
3462+ goto out;
3463+
1facf9fc 3464+ wbr = br->br_wbr;
3465+ old_perm = br->br_perm;
3466+ br->br_perm = new_perm;
3467+ hdir = NULL;
febd17d6 3468+ h_inode = NULL;
1facf9fc 3469+ bindex = au_br_index(sb, br->br_id);
3470+ if (0 <= bindex) {
5527c038 3471+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3472+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3473+ } else {
5527c038 3474+ h_dentry = au_br_dentry(br);
febd17d6
JR
3475+ h_inode = d_inode(h_dentry);
3476+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3477+ }
3478+ if (!wbr)
86dc4139 3479+ err = au_wh_init(br, sb);
1facf9fc 3480+ else {
3481+ wbr_wh_write_lock(wbr);
86dc4139 3482+ err = au_wh_init(br, sb);
1facf9fc 3483+ wbr_wh_write_unlock(wbr);
3484+ }
3485+ if (hdir)
5afbbe0d 3486+ au_hn_inode_unlock(hdir);
1facf9fc 3487+ else
febd17d6 3488+ inode_unlock(h_inode);
86dc4139 3489+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3490+ br->br_perm = old_perm;
3491+
3492+ if (!err && wbr && !au_br_writable(new_perm)) {
1c60b727 3493+ kfree(wbr);
1facf9fc 3494+ br->br_wbr = NULL;
3495+ }
3496+
86dc4139 3497+out:
1facf9fc 3498+ return err;
3499+}
3500+
3501+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3502+ int perm)
1facf9fc 3503+{
3504+ int err;
4a4d8108 3505+ struct kstatfs kst;
1facf9fc 3506+ struct au_wbr *wbr;
3507+
3508+ wbr = br->br_wbr;
dece6358 3509+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3510+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3511+
4a4d8108
AM
3512+ /*
3513+ * a limit for rmdir/rename a dir
523b37e3 3514+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3515+ */
86dc4139 3516+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3517+ if (unlikely(err))
3518+ goto out;
3519+ err = -EINVAL;
3520+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3521+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3522+ else
523b37e3
AM
3523+ pr_err("%pd(%s), unsupported namelen %ld\n",
3524+ au_br_dentry(br),
86dc4139 3525+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3526+
4f0767ce 3527+out:
1facf9fc 3528+ return err;
3529+}
3530+
c1595e42 3531+/* initialize a new branch */
1facf9fc 3532+static int au_br_init(struct au_branch *br, struct super_block *sb,
3533+ struct au_opt_add *add)
3534+{
3535+ int err;
5527c038 3536+ struct inode *h_inode;
1facf9fc 3537+
3538+ err = 0;
521ced18
JR
3539+ spin_lock_init(&br->br_xino.xi_nondir.spin);
3540+ init_waitqueue_head(&br->br_xino.xi_nondir.wqh);
1facf9fc 3541+ br->br_perm = add->perm;
86dc4139 3542+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3543+ spin_lock_init(&br->br_dykey_lock);
5afbbe0d 3544+ au_br_count_init(br);
1facf9fc 3545+ atomic_set(&br->br_xino_running, 0);
3546+ br->br_id = au_new_br_id(sb);
7f207e10 3547+ AuDebugOn(br->br_id < 0);
1facf9fc 3548+
8b6a4947
AM
3549+ /* always, regardless the given option */
3550+ err = au_dr_br_init(sb, br, &add->path);
3551+ if (unlikely(err))
3552+ goto out_err;
3553+
1facf9fc 3554+ if (au_br_writable(add->perm)) {
86dc4139 3555+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3556+ if (unlikely(err))
b752ccd1 3557+ goto out_err;
1facf9fc 3558+ }
3559+
3560+ if (au_opt_test(au_mntflags(sb), XINO)) {
5527c038
JR
3561+ h_inode = d_inode(add->path.dentry);
3562+ err = au_xino_br(sb, br, h_inode->i_ino,
1facf9fc 3563+ au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3564+ if (unlikely(err)) {
3565+ AuDebugOn(br->br_xino.xi_file);
b752ccd1 3566+ goto out_err;
1facf9fc 3567+ }
3568+ }
3569+
3570+ sysaufs_br_init(br);
86dc4139 3571+ path_get(&br->br_path);
b752ccd1 3572+ goto out; /* success */
1facf9fc 3573+
4f0767ce 3574+out_err:
86dc4139 3575+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3576+out:
1facf9fc 3577+ return err;
3578+}
3579+
3580+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3581+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3582+ aufs_bindex_t amount)
3583+{
3584+ struct au_branch **brp;
3585+
dece6358
AM
3586+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3587+
1facf9fc 3588+ brp = sbinfo->si_branch + bindex;
3589+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3590+ *brp = br;
5afbbe0d
AM
3591+ sbinfo->si_bbot++;
3592+ if (unlikely(bbot < 0))
3593+ sbinfo->si_bbot = 0;
1facf9fc 3594+}
3595+
3596+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3597+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3598+{
3599+ struct au_hdentry *hdp;
3600+
1308ab2a 3601+ AuRwMustWriteLock(&dinfo->di_rwsem);
3602+
5afbbe0d 3603+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3604+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3605+ au_h_dentry_init(hdp);
5afbbe0d
AM
3606+ dinfo->di_bbot++;
3607+ if (unlikely(bbot < 0))
3608+ dinfo->di_btop = 0;
1facf9fc 3609+}
3610+
3611+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3612+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3613+{
3614+ struct au_hinode *hip;
3615+
1308ab2a 3616+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3617+
5afbbe0d 3618+ hip = au_hinode(iinfo, bindex);
1facf9fc 3619+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3620+ au_hinode_init(hip);
3621+ iinfo->ii_bbot++;
3622+ if (unlikely(bbot < 0))
3623+ iinfo->ii_btop = 0;
1facf9fc 3624+}
3625+
86dc4139
AM
3626+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3627+ aufs_bindex_t bindex)
1facf9fc 3628+{
86dc4139 3629+ struct dentry *root, *h_dentry;
5527c038 3630+ struct inode *root_inode, *h_inode;
5afbbe0d 3631+ aufs_bindex_t bbot, amount;
1facf9fc 3632+
3633+ root = sb->s_root;
5527c038 3634+ root_inode = d_inode(root);
5afbbe0d
AM
3635+ bbot = au_sbbot(sb);
3636+ amount = bbot + 1 - bindex;
86dc4139 3637+ h_dentry = au_br_dentry(br);
53392da6 3638+ au_sbilist_lock();
5afbbe0d
AM
3639+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3640+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3641+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3642+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3643+ h_inode = d_inode(h_dentry);
3644+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3645+ au_sbilist_unlock();
1facf9fc 3646+}
3647+
3648+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3649+{
3650+ int err;
5afbbe0d 3651+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3652+ struct dentry *root, *h_dentry;
3653+ struct inode *root_inode;
3654+ struct au_branch *add_branch;
3655+
3656+ root = sb->s_root;
5527c038 3657+ root_inode = d_inode(root);
1facf9fc 3658+ IMustLock(root_inode);
5afbbe0d 3659+ IiMustWriteLock(root_inode);
1facf9fc 3660+ err = test_add(sb, add, remount);
3661+ if (unlikely(err < 0))
3662+ goto out;
3663+ if (err) {
3664+ err = 0;
3665+ goto out; /* success */
3666+ }
3667+
5afbbe0d
AM
3668+ bbot = au_sbbot(sb);
3669+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3670+ err = PTR_ERR(add_branch);
3671+ if (IS_ERR(add_branch))
3672+ goto out;
3673+
3674+ err = au_br_init(add_branch, sb, add);
3675+ if (unlikely(err)) {
3676+ au_br_do_free(add_branch);
3677+ goto out;
3678+ }
3679+
3680+ add_bindex = add->bindex;
1facf9fc 3681+ if (!remount)
86dc4139 3682+ au_br_do_add(sb, add_branch, add_bindex);
1facf9fc 3683+ else {
3684+ sysaufs_brs_del(sb, add_bindex);
86dc4139 3685+ au_br_do_add(sb, add_branch, add_bindex);
1facf9fc 3686+ sysaufs_brs_add(sb, add_bindex);
3687+ }
3688+
86dc4139 3689+ h_dentry = add->path.dentry;
1308ab2a 3690+ if (!add_bindex) {
1facf9fc 3691+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3692+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3693+ } else
5527c038 3694+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3695+
3696+ /*
4a4d8108 3697+ * this test/set prevents aufs from handling unnecesary notify events
027c5e7a 3698+ * of xino files, in case of re-adding a writable branch which was
1facf9fc 3699+ * once detached from aufs.
3700+ */
3701+ if (au_xino_brid(sb) < 0
3702+ && au_br_writable(add_branch->br_perm)
3703+ && !au_test_fs_bad_xino(h_dentry->d_sb)
3704+ && add_branch->br_xino.xi_file
2000de60 3705+ && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
1facf9fc 3706+ au_xino_brid_set(sb, add_branch->br_id);
3707+
4f0767ce 3708+out:
1facf9fc 3709+ return err;
3710+}
3711+
3712+/* ---------------------------------------------------------------------- */
3713+
79b8bda9 3714+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3715+ unsigned long long max __maybe_unused,
3716+ void *arg)
3717+{
3718+ unsigned long long n;
3719+ struct file **p, *f;
8b6a4947
AM
3720+ struct hlist_bl_head *files;
3721+ struct hlist_bl_node *pos;
076b876e 3722+ struct au_finfo *finfo;
076b876e
AM
3723+
3724+ n = 0;
3725+ p = a;
3726+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3727+ hlist_bl_lock(files);
3728+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3729+ f = finfo->fi_file;
3730+ if (file_count(f)
3731+ && !special_file(file_inode(f)->i_mode)) {
3732+ get_file(f);
3733+ *p++ = f;
3734+ n++;
3735+ AuDebugOn(n > max);
3736+ }
3737+ }
8b6a4947 3738+ hlist_bl_unlock(files);
076b876e
AM
3739+
3740+ return n;
3741+}
3742+
3743+static struct file **au_farray_alloc(struct super_block *sb,
3744+ unsigned long long *max)
3745+{
5afbbe0d 3746+ *max = au_nfiles(sb);
79b8bda9 3747+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3748+}
3749+
3750+static void au_farray_free(struct file **a, unsigned long long max)
3751+{
3752+ unsigned long long ull;
3753+
3754+ for (ull = 0; ull < max; ull++)
3755+ if (a[ull])
3756+ fput(a[ull]);
be52b249 3757+ kvfree(a);
076b876e
AM
3758+}
3759+
3760+/* ---------------------------------------------------------------------- */
3761+
1facf9fc 3762+/*
3763+ * delete a branch
3764+ */
3765+
3766+/* to show the line number, do not make it inlined function */
4a4d8108 3767+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3768+ if (do_info) \
4a4d8108 3769+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3770+} while (0)
3771+
5afbbe0d
AM
3772+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3773+ aufs_bindex_t bbot)
027c5e7a 3774+{
5afbbe0d 3775+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3776+}
3777+
5afbbe0d
AM
3778+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3779+ aufs_bindex_t bbot)
027c5e7a 3780+{
5afbbe0d 3781+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3782+}
3783+
1facf9fc 3784+/*
3785+ * test if the branch is deletable or not.
3786+ */
3787+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3788+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3789+{
3790+ int err, i, j, ndentry;
5afbbe0d 3791+ aufs_bindex_t btop, bbot;
1facf9fc 3792+ struct au_dcsub_pages dpages;
3793+ struct au_dpage *dpage;
3794+ struct dentry *d;
1facf9fc 3795+
3796+ err = au_dpages_init(&dpages, GFP_NOFS);
3797+ if (unlikely(err))
3798+ goto out;
3799+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3800+ if (unlikely(err))
3801+ goto out_dpages;
3802+
1facf9fc 3803+ for (i = 0; !err && i < dpages.ndpage; i++) {
3804+ dpage = dpages.dpages + i;
3805+ ndentry = dpage->ndentry;
3806+ for (j = 0; !err && j < ndentry; j++) {
3807+ d = dpage->dentries[j];
c1595e42 3808+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3809+ if (!au_digen_test(d, sigen)) {
1facf9fc 3810+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3811+ if (unlikely(au_dbrange_test(d))) {
3812+ di_read_unlock(d, AuLock_IR);
3813+ continue;
3814+ }
3815+ } else {
1facf9fc 3816+ di_write_lock_child(d);
027c5e7a
AM
3817+ if (unlikely(au_dbrange_test(d))) {
3818+ di_write_unlock(d);
3819+ continue;
3820+ }
1facf9fc 3821+ err = au_reval_dpath(d, sigen);
3822+ if (!err)
3823+ di_downgrade_lock(d, AuLock_IR);
3824+ else {
3825+ di_write_unlock(d);
3826+ break;
3827+ }
3828+ }
3829+
027c5e7a 3830+ /* AuDbgDentry(d); */
5afbbe0d
AM
3831+ btop = au_dbtop(d);
3832+ bbot = au_dbbot(d);
3833+ if (btop <= bindex
3834+ && bindex <= bbot
1facf9fc 3835+ && au_h_dptr(d, bindex)
5afbbe0d 3836+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3837+ err = -EBUSY;
523b37e3 3838+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3839+ AuDbgDentry(d);
1facf9fc 3840+ }
3841+ di_read_unlock(d, AuLock_IR);
3842+ }
3843+ }
3844+
4f0767ce 3845+out_dpages:
1facf9fc 3846+ au_dpages_free(&dpages);
4f0767ce 3847+out:
1facf9fc 3848+ return err;
3849+}
3850+
3851+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3852+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3853+{
3854+ int err;
7f207e10
AM
3855+ unsigned long long max, ull;
3856+ struct inode *i, **array;
5afbbe0d 3857+ aufs_bindex_t btop, bbot;
1facf9fc 3858+
7f207e10
AM
3859+ array = au_iarray_alloc(sb, &max);
3860+ err = PTR_ERR(array);
3861+ if (IS_ERR(array))
3862+ goto out;
3863+
1facf9fc 3864+ err = 0;
7f207e10
AM
3865+ AuDbg("b%d\n", bindex);
3866+ for (ull = 0; !err && ull < max; ull++) {
3867+ i = array[ull];
076b876e
AM
3868+ if (unlikely(!i))
3869+ break;
7f207e10 3870+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3871+ continue;
3872+
7f207e10 3873+ /* AuDbgInode(i); */
537831f9 3874+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3875+ ii_read_lock_child(i);
3876+ else {
3877+ ii_write_lock_child(i);
027c5e7a
AM
3878+ err = au_refresh_hinode_self(i);
3879+ au_iigen_dec(i);
1facf9fc 3880+ if (!err)
3881+ ii_downgrade_lock(i);
3882+ else {
3883+ ii_write_unlock(i);
3884+ break;
3885+ }
3886+ }
3887+
5afbbe0d
AM
3888+ btop = au_ibtop(i);
3889+ bbot = au_ibbot(i);
3890+ if (btop <= bindex
3891+ && bindex <= bbot
1facf9fc 3892+ && au_h_iptr(i, bindex)
5afbbe0d 3893+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3894+ err = -EBUSY;
3895+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3896+ AuDbgInode(i);
1facf9fc 3897+ }
3898+ ii_read_unlock(i);
3899+ }
7f207e10 3900+ au_iarray_free(array, max);
1facf9fc 3901+
7f207e10 3902+out:
1facf9fc 3903+ return err;
3904+}
3905+
b752ccd1
AM
3906+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3907+ const unsigned int verbose)
1facf9fc 3908+{
3909+ int err;
3910+ unsigned int sigen;
3911+
3912+ sigen = au_sigen(root->d_sb);
3913+ DiMustNoWaiters(root);
5527c038 3914+ IiMustNoWaiters(d_inode(root));
1facf9fc 3915+ di_write_unlock(root);
b752ccd1 3916+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3917+ if (!err)
b752ccd1 3918+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3919+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3920+
3921+ return err;
3922+}
3923+
076b876e
AM
3924+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3925+ struct file **to_free, int *idx)
3926+{
3927+ int err;
c1595e42 3928+ unsigned char matched, root;
5afbbe0d 3929+ aufs_bindex_t bindex, bbot;
076b876e
AM
3930+ struct au_fidir *fidir;
3931+ struct au_hfile *hfile;
3932+
3933+ err = 0;
2000de60 3934+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3935+ if (root) {
3936+ get_file(file);
3937+ to_free[*idx] = file;
3938+ (*idx)++;
3939+ goto out;
3940+ }
3941+
076b876e 3942+ matched = 0;
076b876e
AM
3943+ fidir = au_fi(file)->fi_hdir;
3944+ AuDebugOn(!fidir);
5afbbe0d
AM
3945+ bbot = au_fbbot_dir(file);
3946+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3947+ hfile = fidir->fd_hfile + bindex;
3948+ if (!hfile->hf_file)
3949+ continue;
3950+
c1595e42 3951+ if (hfile->hf_br->br_id == br_id) {
076b876e 3952+ matched = 1;
076b876e 3953+ break;
c1595e42 3954+ }
076b876e 3955+ }
c1595e42 3956+ if (matched)
076b876e
AM
3957+ err = -EBUSY;
3958+
3959+out:
3960+ return err;
3961+}
3962+
3963+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3964+ struct file **to_free, int opened)
3965+{
3966+ int err, idx;
3967+ unsigned long long ull, max;
5afbbe0d 3968+ aufs_bindex_t btop;
076b876e 3969+ struct file *file, **array;
076b876e
AM
3970+ struct dentry *root;
3971+ struct au_hfile *hfile;
3972+
3973+ array = au_farray_alloc(sb, &max);
3974+ err = PTR_ERR(array);
3975+ if (IS_ERR(array))
3976+ goto out;
3977+
3978+ err = 0;
3979+ idx = 0;
3980+ root = sb->s_root;
3981+ di_write_unlock(root);
3982+ for (ull = 0; ull < max; ull++) {
3983+ file = array[ull];
3984+ if (unlikely(!file))
3985+ break;
3986+
3987+ /* AuDbg("%pD\n", file); */
3988+ fi_read_lock(file);
5afbbe0d 3989+ btop = au_fbtop(file);
2000de60 3990+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3991+ hfile = &au_fi(file)->fi_htop;
3992+ if (hfile->hf_br->br_id == br_id)
3993+ err = -EBUSY;
3994+ } else
3995+ err = test_dir_busy(file, br_id, to_free, &idx);
3996+ fi_read_unlock(file);
3997+ if (unlikely(err))
3998+ break;
3999+ }
4000+ di_write_lock_child(root);
4001+ au_farray_free(array, max);
4002+ AuDebugOn(idx > opened);
4003+
4004+out:
4005+ return err;
4006+}
4007+
4008+static void br_del_file(struct file **to_free, unsigned long long opened,
4009+ aufs_bindex_t br_id)
4010+{
4011+ unsigned long long ull;
5afbbe0d 4012+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
4013+ struct file *file;
4014+ struct au_fidir *fidir;
4015+ struct au_hfile *hfile;
4016+
4017+ for (ull = 0; ull < opened; ull++) {
4018+ file = to_free[ull];
4019+ if (unlikely(!file))
4020+ break;
4021+
4022+ /* AuDbg("%pD\n", file); */
2000de60 4023+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
4024+ bfound = -1;
4025+ fidir = au_fi(file)->fi_hdir;
4026+ AuDebugOn(!fidir);
4027+ fi_write_lock(file);
5afbbe0d
AM
4028+ btop = au_fbtop(file);
4029+ bbot = au_fbbot_dir(file);
4030+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
4031+ hfile = fidir->fd_hfile + bindex;
4032+ if (!hfile->hf_file)
4033+ continue;
4034+
4035+ if (hfile->hf_br->br_id == br_id) {
4036+ bfound = bindex;
4037+ break;
4038+ }
4039+ }
4040+ AuDebugOn(bfound < 0);
4041+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
4042+ if (bfound == btop) {
4043+ for (btop++; btop <= bbot; btop++)
4044+ if (au_hf_dir(file, btop)) {
4045+ au_set_fbtop(file, btop);
076b876e
AM
4046+ break;
4047+ }
4048+ }
4049+ fi_write_unlock(file);
4050+ }
4051+}
4052+
1facf9fc 4053+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
4054+ const aufs_bindex_t bindex,
5afbbe0d 4055+ const aufs_bindex_t bbot)
1facf9fc 4056+{
4057+ struct au_branch **brp, **p;
4058+
dece6358
AM
4059+ AuRwMustWriteLock(&sbinfo->si_rwsem);
4060+
1facf9fc 4061+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
4062+ if (bindex < bbot)
4063+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
4064+ sbinfo->si_branch[0 + bbot] = NULL;
4065+ sbinfo->si_bbot--;
1facf9fc 4066+
e2f27e51
AM
4067+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
4068+ /*may_shrink*/1);
1facf9fc 4069+ if (p)
4070+ sbinfo->si_branch = p;
4a4d8108 4071+ /* harmless error */
1facf9fc 4072+}
4073+
4074+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 4075+ const aufs_bindex_t bbot)
1facf9fc 4076+{
4077+ struct au_hdentry *hdp, *p;
4078+
1308ab2a 4079+ AuRwMustWriteLock(&dinfo->di_rwsem);
4080+
5afbbe0d
AM
4081+ hdp = au_hdentry(dinfo, bindex);
4082+ if (bindex < bbot)
4083+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
4084+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
4085+ dinfo->di_bbot--;
1facf9fc 4086+
e2f27e51
AM
4087+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
4088+ /*may_shrink*/1);
1facf9fc 4089+ if (p)
4090+ dinfo->di_hdentry = p;
4a4d8108 4091+ /* harmless error */
1facf9fc 4092+}
4093+
4094+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 4095+ const aufs_bindex_t bbot)
1facf9fc 4096+{
4097+ struct au_hinode *hip, *p;
4098+
1308ab2a 4099+ AuRwMustWriteLock(&iinfo->ii_rwsem);
4100+
5afbbe0d
AM
4101+ hip = au_hinode(iinfo, bindex);
4102+ if (bindex < bbot)
4103+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
4104+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
4105+ iinfo->ii_bbot--;
1facf9fc 4106+
e2f27e51
AM
4107+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
4108+ /*may_shrink*/1);
1facf9fc 4109+ if (p)
4110+ iinfo->ii_hinode = p;
4a4d8108 4111+ /* harmless error */
1facf9fc 4112+}
4113+
4114+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
4115+ struct au_branch *br)
4116+{
5afbbe0d 4117+ aufs_bindex_t bbot;
1facf9fc 4118+ struct au_sbinfo *sbinfo;
53392da6
AM
4119+ struct dentry *root, *h_root;
4120+ struct inode *inode, *h_inode;
4121+ struct au_hinode *hinode;
1facf9fc 4122+
dece6358
AM
4123+ SiMustWriteLock(sb);
4124+
1facf9fc 4125+ root = sb->s_root;
5527c038 4126+ inode = d_inode(root);
1facf9fc 4127+ sbinfo = au_sbi(sb);
5afbbe0d 4128+ bbot = sbinfo->si_bbot;
1facf9fc 4129+
53392da6
AM
4130+ h_root = au_h_dptr(root, bindex);
4131+ hinode = au_hi(inode, bindex);
4132+ h_inode = au_igrab(hinode->hi_inode);
4133+ au_hiput(hinode);
1facf9fc 4134+
53392da6 4135+ au_sbilist_lock();
5afbbe0d
AM
4136+ au_br_do_del_brp(sbinfo, bindex, bbot);
4137+ au_br_do_del_hdp(au_di(root), bindex, bbot);
4138+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
4139+ au_sbilist_unlock();
4140+
8b6a4947
AM
4141+ /* ignore an error */
4142+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
4143+
53392da6
AM
4144+ dput(h_root);
4145+ iput(h_inode);
4146+ au_br_do_free(br);
1facf9fc 4147+}
4148+
79b8bda9
AM
4149+static unsigned long long empty_cb(struct super_block *sb, void *array,
4150+ unsigned long long max, void *arg)
076b876e
AM
4151+{
4152+ return max;
4153+}
4154+
1facf9fc 4155+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4156+{
4157+ int err, rerr, i;
076b876e 4158+ unsigned long long opened;
1facf9fc 4159+ unsigned int mnt_flags;
5afbbe0d 4160+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 4161+ unsigned char do_wh, verbose;
4162+ struct au_branch *br;
4163+ struct au_wbr *wbr;
076b876e
AM
4164+ struct dentry *root;
4165+ struct file **to_free;
1facf9fc 4166+
4167+ err = 0;
076b876e
AM
4168+ opened = 0;
4169+ to_free = NULL;
4170+ root = sb->s_root;
4171+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 4172+ if (bindex < 0) {
4173+ if (remount)
4174+ goto out; /* success */
4175+ err = -ENOENT;
4a4d8108 4176+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 4177+ goto out;
4178+ }
4179+ AuDbg("bindex b%d\n", bindex);
4180+
4181+ err = -EBUSY;
4182+ mnt_flags = au_mntflags(sb);
4183+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
4184+ bbot = au_sbbot(sb);
4185+ if (unlikely(!bbot)) {
1facf9fc 4186+ AuVerbose(verbose, "no more branches left\n");
4187+ goto out;
4188+ }
4189+ br = au_sbr(sb, bindex);
86dc4139 4190+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
076b876e
AM
4191+
4192+ br_id = br->br_id;
5afbbe0d 4193+ opened = au_br_count(br);
076b876e 4194+ if (unlikely(opened)) {
79b8bda9 4195+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
4196+ err = PTR_ERR(to_free);
4197+ if (IS_ERR(to_free))
4198+ goto out;
4199+
4200+ err = test_file_busy(sb, br_id, to_free, opened);
4201+ if (unlikely(err)) {
4202+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
4203+ goto out;
4204+ }
1facf9fc 4205+ }
4206+
4207+ wbr = br->br_wbr;
4208+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4209+ if (do_wh) {
1308ab2a 4210+ /* instead of WbrWhMustWriteLock(wbr) */
4211+ SiMustWriteLock(sb);
1facf9fc 4212+ for (i = 0; i < AuBrWh_Last; i++) {
4213+ dput(wbr->wbr_wh[i]);
4214+ wbr->wbr_wh[i] = NULL;
4215+ }
4216+ }
4217+
076b876e 4218+ err = test_children_busy(root, bindex, verbose);
1facf9fc 4219+ if (unlikely(err)) {
4220+ if (do_wh)
4221+ goto out_wh;
4222+ goto out;
4223+ }
4224+
4225+ err = 0;
076b876e
AM
4226+ if (to_free) {
4227+ /*
4228+ * now we confirmed the branch is deletable.
4229+ * let's free the remaining opened dirs on the branch.
4230+ */
4231+ di_write_unlock(root);
4232+ br_del_file(to_free, opened, br_id);
4233+ di_write_lock_child(root);
4234+ }
4235+
1facf9fc 4236+ if (!remount)
4237+ au_br_do_del(sb, bindex, br);
4238+ else {
4239+ sysaufs_brs_del(sb, bindex);
4240+ au_br_do_del(sb, bindex, br);
4241+ sysaufs_brs_add(sb, bindex);
4242+ }
4243+
1308ab2a 4244+ if (!bindex) {
5527c038 4245+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4246+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4247+ } else
5527c038 4248+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4249+ if (au_opt_test(mnt_flags, PLINK))
4250+ au_plink_half_refresh(sb, br_id);
4251+
b752ccd1 4252+ if (au_xino_brid(sb) == br_id)
1facf9fc 4253+ au_xino_brid_set(sb, -1);
4254+ goto out; /* success */
4255+
4f0767ce 4256+out_wh:
1facf9fc 4257+ /* revert */
86dc4139 4258+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4259+ if (rerr)
0c3ec466
AM
4260+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4261+ del->pathname, rerr);
4f0767ce 4262+out:
076b876e
AM
4263+ if (to_free)
4264+ au_farray_free(to_free, opened);
1facf9fc 4265+ return err;
4266+}
4267+
4268+/* ---------------------------------------------------------------------- */
4269+
027c5e7a
AM
4270+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4271+{
4272+ int err;
5afbbe0d 4273+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4274+ struct aufs_ibusy ibusy;
4275+ struct inode *inode, *h_inode;
4276+
4277+ err = -EPERM;
4278+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4279+ goto out;
4280+
4281+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4282+ if (!err)
4283+ err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4284+ if (unlikely(err)) {
4285+ err = -EFAULT;
4286+ AuTraceErr(err);
4287+ goto out;
4288+ }
4289+
4290+ err = -EINVAL;
4291+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4292+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4293+ goto out_unlock;
4294+
4295+ err = 0;
4296+ ibusy.h_ino = 0; /* invalid */
4297+ inode = ilookup(sb, ibusy.ino);
4298+ if (!inode
4299+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4300+ || au_is_bad_inode(inode))
027c5e7a
AM
4301+ goto out_unlock;
4302+
4303+ ii_read_lock_child(inode);
5afbbe0d
AM
4304+ btop = au_ibtop(inode);
4305+ bbot = au_ibbot(inode);
4306+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4307+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4308+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4309+ ibusy.h_ino = h_inode->i_ino;
4310+ }
4311+ ii_read_unlock(inode);
4312+ iput(inode);
4313+
4314+out_unlock:
4315+ si_read_unlock(sb);
4316+ if (!err) {
4317+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4318+ if (unlikely(err)) {
4319+ err = -EFAULT;
4320+ AuTraceErr(err);
4321+ }
4322+ }
4323+out:
4324+ return err;
4325+}
4326+
4327+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4328+{
2000de60 4329+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4330+}
4331+
4332+#ifdef CONFIG_COMPAT
4333+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4334+{
2000de60 4335+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4336+}
4337+#endif
4338+
4339+/* ---------------------------------------------------------------------- */
4340+
1facf9fc 4341+/*
4342+ * change a branch permission
4343+ */
4344+
dece6358
AM
4345+static void au_warn_ima(void)
4346+{
4347+#ifdef CONFIG_IMA
1308ab2a 4348+ /* since it doesn't support mark_files_ro() */
027c5e7a 4349+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4350+#endif
4351+}
4352+
1facf9fc 4353+static int do_need_sigen_inc(int a, int b)
4354+{
4355+ return au_br_whable(a) && !au_br_whable(b);
4356+}
4357+
4358+static int need_sigen_inc(int old, int new)
4359+{
4360+ return do_need_sigen_inc(old, new)
4361+ || do_need_sigen_inc(new, old);
4362+}
4363+
4364+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4365+{
7f207e10 4366+ int err, do_warn;
027c5e7a 4367+ unsigned int mnt_flags;
7f207e10 4368+ unsigned long long ull, max;
e49829fe 4369+ aufs_bindex_t br_id;
38d290e6 4370+ unsigned char verbose, writer;
7f207e10 4371+ struct file *file, *hf, **array;
e49829fe 4372+ struct au_hfile *hfile;
1facf9fc 4373+
027c5e7a
AM
4374+ mnt_flags = au_mntflags(sb);
4375+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4376+
7f207e10
AM
4377+ array = au_farray_alloc(sb, &max);
4378+ err = PTR_ERR(array);
4379+ if (IS_ERR(array))
1facf9fc 4380+ goto out;
4381+
7f207e10 4382+ do_warn = 0;
e49829fe 4383+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4384+ for (ull = 0; ull < max; ull++) {
4385+ file = array[ull];
076b876e
AM
4386+ if (unlikely(!file))
4387+ break;
1facf9fc 4388+
523b37e3 4389+ /* AuDbg("%pD\n", file); */
1facf9fc 4390+ fi_read_lock(file);
4391+ if (unlikely(au_test_mmapped(file))) {
4392+ err = -EBUSY;
523b37e3 4393+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4394+ AuDbgFile(file);
1facf9fc 4395+ FiMustNoWaiters(file);
4396+ fi_read_unlock(file);
7f207e10 4397+ goto out_array;
1facf9fc 4398+ }
4399+
e49829fe
JR
4400+ hfile = &au_fi(file)->fi_htop;
4401+ hf = hfile->hf_file;
7e9cd9fe 4402+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4403+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4404+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4405+ || !(hf->f_mode & FMODE_WRITE))
4406+ array[ull] = NULL;
4407+ else {
4408+ do_warn = 1;
4409+ get_file(file);
1facf9fc 4410+ }
4411+
1facf9fc 4412+ FiMustNoWaiters(file);
4413+ fi_read_unlock(file);
7f207e10
AM
4414+ fput(file);
4415+ }
1facf9fc 4416+
4417+ err = 0;
7f207e10 4418+ if (do_warn)
dece6358 4419+ au_warn_ima();
7f207e10
AM
4420+
4421+ for (ull = 0; ull < max; ull++) {
4422+ file = array[ull];
4423+ if (!file)
4424+ continue;
4425+
1facf9fc 4426+ /* todo: already flushed? */
523b37e3
AM
4427+ /*
4428+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4429+ * approach which resets f_mode and calls mnt_drop_write() and
4430+ * file_release_write() for each file, because the branch
4431+ * attribute in aufs world is totally different from the native
4432+ * fs rw/ro mode.
4433+ */
7f207e10
AM
4434+ /* fi_read_lock(file); */
4435+ hfile = &au_fi(file)->fi_htop;
4436+ hf = hfile->hf_file;
4437+ /* fi_read_unlock(file); */
027c5e7a 4438+ spin_lock(&hf->f_lock);
38d290e6
JR
4439+ writer = !!(hf->f_mode & FMODE_WRITER);
4440+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4441+ spin_unlock(&hf->f_lock);
38d290e6
JR
4442+ if (writer) {
4443+ put_write_access(file_inode(hf));
c06a8ce3 4444+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4445+ }
4446+ }
4447+
7f207e10
AM
4448+out_array:
4449+ au_farray_free(array, max);
4f0767ce 4450+out:
7f207e10 4451+ AuTraceErr(err);
1facf9fc 4452+ return err;
4453+}
4454+
4455+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4456+ int *do_refresh)
1facf9fc 4457+{
4458+ int err, rerr;
4459+ aufs_bindex_t bindex;
4460+ struct dentry *root;
4461+ struct au_branch *br;
076b876e 4462+ struct au_br_fhsm *bf;
1facf9fc 4463+
4464+ root = sb->s_root;
1facf9fc 4465+ bindex = au_find_dbindex(root, mod->h_root);
4466+ if (bindex < 0) {
4467+ if (remount)
4468+ return 0; /* success */
4469+ err = -ENOENT;
4a4d8108 4470+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4471+ goto out;
4472+ }
4473+ AuDbg("bindex b%d\n", bindex);
4474+
5527c038 4475+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4476+ if (unlikely(err))
4477+ goto out;
4478+
4479+ br = au_sbr(sb, bindex);
86dc4139 4480+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4481+ if (br->br_perm == mod->perm)
4482+ return 0; /* success */
4483+
076b876e
AM
4484+ /* pre-allocate for non-fhsm --> fhsm */
4485+ bf = NULL;
4486+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4487+ err = au_fhsm_br_alloc(br);
4488+ if (unlikely(err))
4489+ goto out;
4490+ bf = br->br_fhsm;
4491+ br->br_fhsm = NULL;
4492+ }
4493+
1facf9fc 4494+ if (au_br_writable(br->br_perm)) {
4495+ /* remove whiteout base */
86dc4139 4496+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4497+ if (unlikely(err))
076b876e 4498+ goto out_bf;
1facf9fc 4499+
4500+ if (!au_br_writable(mod->perm)) {
4501+ /* rw --> ro, file might be mmapped */
4502+ DiMustNoWaiters(root);
5527c038 4503+ IiMustNoWaiters(d_inode(root));
1facf9fc 4504+ di_write_unlock(root);
4505+ err = au_br_mod_files_ro(sb, bindex);
4506+ /* aufs_write_lock() calls ..._child() */
4507+ di_write_lock_child(root);
4508+
4509+ if (unlikely(err)) {
4510+ rerr = -ENOMEM;
be52b249 4511+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4512+ GFP_NOFS);
86dc4139
AM
4513+ if (br->br_wbr)
4514+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4515+ if (unlikely(rerr)) {
4516+ AuIOErr("nested error %d (%d)\n",
4517+ rerr, err);
4518+ br->br_perm = mod->perm;
4519+ }
4520+ }
4521+ }
4522+ } else if (au_br_writable(mod->perm)) {
4523+ /* ro --> rw */
4524+ err = -ENOMEM;
be52b249 4525+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4526+ if (br->br_wbr) {
86dc4139 4527+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4528+ if (unlikely(err)) {
1c60b727 4529+ kfree(br->br_wbr);
1facf9fc 4530+ br->br_wbr = NULL;
4531+ }
4532+ }
4533+ }
076b876e
AM
4534+ if (unlikely(err))
4535+ goto out_bf;
4536+
4537+ if (au_br_fhsm(br->br_perm)) {
4538+ if (!au_br_fhsm(mod->perm)) {
4539+ /* fhsm --> non-fhsm */
4540+ au_br_fhsm_fin(br->br_fhsm);
1c60b727 4541+ kfree(br->br_fhsm);
076b876e
AM
4542+ br->br_fhsm = NULL;
4543+ }
4544+ } else if (au_br_fhsm(mod->perm))
4545+ /* non-fhsm --> fhsm */
4546+ br->br_fhsm = bf;
4547+
076b876e
AM
4548+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4549+ br->br_perm = mod->perm;
4550+ goto out; /* success */
1facf9fc 4551+
076b876e 4552+out_bf:
8b6a4947 4553+ kfree(bf);
076b876e
AM
4554+out:
4555+ AuTraceErr(err);
4556+ return err;
4557+}
4558+
4559+/* ---------------------------------------------------------------------- */
4560+
4561+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4562+{
4563+ int err;
4564+ struct kstatfs kstfs;
4565+
4566+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4567+ if (!err) {
076b876e
AM
4568+ stfs->f_blocks = kstfs.f_blocks;
4569+ stfs->f_bavail = kstfs.f_bavail;
4570+ stfs->f_files = kstfs.f_files;
4571+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4572+ }
4573+
1facf9fc 4574+ return err;
4575+}
7f207e10
AM
4576diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4577--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
be118d29 4578+++ linux/fs/aufs/branch.h 2018-04-06 07:48:44.204604724 +0200
8b6a4947 4579@@ -0,0 +1,333 @@
1facf9fc 4580+/*
a2654f78 4581+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 4582+ *
4583+ * This program, aufs is free software; you can redistribute it and/or modify
4584+ * it under the terms of the GNU General Public License as published by
4585+ * the Free Software Foundation; either version 2 of the License, or
4586+ * (at your option) any later version.
dece6358
AM
4587+ *
4588+ * This program is distributed in the hope that it will be useful,
4589+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4590+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4591+ * GNU General Public License for more details.
4592+ *
4593+ * You should have received a copy of the GNU General Public License
523b37e3 4594+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4595+ */
4596+
4597+/*
4598+ * branch filesystems and xino for them
4599+ */
4600+
4601+#ifndef __AUFS_BRANCH_H__
4602+#define __AUFS_BRANCH_H__
4603+
4604+#ifdef __KERNEL__
4605+
1facf9fc 4606+#include <linux/mount.h>
8b6a4947 4607+#include "dirren.h"
4a4d8108 4608+#include "dynop.h"
1facf9fc 4609+#include "rwsem.h"
4610+#include "super.h"
4611+
4612+/* ---------------------------------------------------------------------- */
4613+
4614+/* a xino file */
4615+struct au_xino_file {
4616+ struct file *xi_file;
521ced18
JR
4617+ struct {
4618+ spinlock_t spin;
4619+ ino_t *array;
4620+ int total;
4621+ /* reserved for future use */
4622+ /* unsigned long *bitmap; */
4623+ wait_queue_head_t wqh;
4624+ } xi_nondir;
1facf9fc 4625+
4626+ /* todo: make xino files an array to support huge inode number */
4627+
4628+#ifdef CONFIG_DEBUG_FS
4629+ struct dentry *xi_dbgaufs;
4630+#endif
4631+};
4632+
076b876e
AM
4633+/* File-based Hierarchical Storage Management */
4634+struct au_br_fhsm {
4635+#ifdef CONFIG_AUFS_FHSM
4636+ struct mutex bf_lock;
4637+ unsigned long bf_jiffy;
4638+ struct aufs_stfs bf_stfs;
4639+ int bf_readable;
4640+#endif
4641+};
4642+
1facf9fc 4643+/* members for writable branch only */
4644+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4645+struct au_wbr {
dece6358 4646+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4647+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4648+ atomic_t wbr_wh_running;
1facf9fc 4649+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4650+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4651+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4652+
4653+ /* mfs mode */
4654+ unsigned long long wbr_bytes;
4655+};
4656+
4a4d8108
AM
4657+/* ext2 has 3 types of operations at least, ext3 has 4 */
4658+#define AuBrDynOp (AuDyLast * 4)
4659+
1716fcea
AM
4660+#ifdef CONFIG_AUFS_HFSNOTIFY
4661+/* support for asynchronous destruction */
4662+struct au_br_hfsnotify {
4663+ struct fsnotify_group *hfsn_group;
4664+};
4665+#endif
4666+
392086de
AM
4667+/* sysfs entries */
4668+struct au_brsysfs {
4669+ char name[16];
4670+ struct attribute attr;
4671+};
4672+
4673+enum {
4674+ AuBrSysfs_BR,
4675+ AuBrSysfs_BRID,
4676+ AuBrSysfs_Last
4677+};
4678+
1facf9fc 4679+/* protected by superblock rwsem */
4680+struct au_branch {
4681+ struct au_xino_file br_xino;
4682+
4683+ aufs_bindex_t br_id;
4684+
4685+ int br_perm;
86dc4139 4686+ struct path br_path;
4a4d8108
AM
4687+ spinlock_t br_dykey_lock;
4688+ struct au_dykey *br_dykey[AuBrDynOp];
5afbbe0d 4689+ struct percpu_counter br_count;
1facf9fc 4690+
4691+ struct au_wbr *br_wbr;
076b876e 4692+ struct au_br_fhsm *br_fhsm;
1facf9fc 4693+
4694+ /* xino truncation */
1facf9fc 4695+ atomic_t br_xino_running;
4696+
027c5e7a 4697+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4698+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4699+#endif
4700+
1facf9fc 4701+#ifdef CONFIG_SYSFS
392086de
AM
4702+ /* entries under sysfs per mount-point */
4703+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4704+#endif
8b6a4947
AM
4705+
4706+ struct au_dr_br br_dirren;
1facf9fc 4707+};
4708+
4709+/* ---------------------------------------------------------------------- */
4710+
86dc4139
AM
4711+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4712+{
4713+ return br->br_path.mnt;
4714+}
4715+
4716+static inline struct dentry *au_br_dentry(struct au_branch *br)
4717+{
4718+ return br->br_path.dentry;
4719+}
4720+
4721+static inline struct super_block *au_br_sb(struct au_branch *br)
4722+{
4723+ return au_br_mnt(br)->mnt_sb;
4724+}
4725+
5afbbe0d
AM
4726+static inline void au_br_get(struct au_branch *br)
4727+{
4728+ percpu_counter_inc(&br->br_count);
4729+}
4730+
4731+static inline void au_br_put(struct au_branch *br)
4732+{
4733+ percpu_counter_dec(&br->br_count);
4734+}
4735+
4736+static inline s64 au_br_count(struct au_branch *br)
4737+{
4738+ return percpu_counter_sum(&br->br_count);
4739+}
4740+
4741+static inline void au_br_count_init(struct au_branch *br)
4742+{
4743+ percpu_counter_init(&br->br_count, 0, GFP_NOFS);
4744+}
4745+
4746+static inline void au_br_count_fin(struct au_branch *br)
4747+{
4748+ percpu_counter_destroy(&br->br_count);
4749+}
4750+
1facf9fc 4751+static inline int au_br_rdonly(struct au_branch *br)
4752+{
8b6a4947 4753+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4754+ || !au_br_writable(br->br_perm))
4755+ ? -EROFS : 0;
4756+}
4757+
4a4d8108 4758+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4759+{
4a4d8108 4760+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4761+ return !(brperm & AuBrPerm_RR);
1facf9fc 4762+#else
4763+ return 0;
4764+#endif
4765+}
4766+
b912730e
AM
4767+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4768+{
4769+ int err, exec_flag;
4770+
4771+ err = 0;
4772+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4773+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4774+ err = -EACCES;
4775+
4776+ return err;
4777+}
4778+
1facf9fc 4779+/* ---------------------------------------------------------------------- */
4780+
4781+/* branch.c */
4782+struct au_sbinfo;
4783+void au_br_free(struct au_sbinfo *sinfo);
4784+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4785+struct au_opt_add;
4786+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4787+struct au_opt_del;
4788+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4789+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4790+#ifdef CONFIG_COMPAT
4791+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4792+#endif
1facf9fc 4793+struct au_opt_mod;
4794+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4795+ int *do_refresh);
076b876e
AM
4796+struct aufs_stfs;
4797+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4798+
4799+/* xino.c */
4800+static const loff_t au_loff_max = LLONG_MAX;
4801+
4802+int au_xib_trunc(struct super_block *sb);
5527c038 4803+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4804+ loff_t *pos);
5527c038
JR
4805+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4806+ size_t size, loff_t *pos);
1facf9fc 4807+struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4808+struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4809+ino_t au_xino_new_ino(struct super_block *sb);
b752ccd1 4810+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4811+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4812+ ino_t ino);
4813+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4814+ ino_t *ino);
4815+int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4816+ struct file *base_file, int do_test);
4817+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4818+
4819+struct au_opt_xino;
4820+int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4821+void au_xino_clr(struct super_block *sb);
4822+struct file *au_xino_def(struct super_block *sb);
4823+int au_xino_path(struct seq_file *seq, struct file *file);
4824+
521ced18
JR
4825+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4826+ ino_t h_ino, int idx);
4827+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4828+ int *idx);
4829+
1facf9fc 4830+/* ---------------------------------------------------------------------- */
4831+
4832+/* Superblock to branch */
4833+static inline
4834+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4835+{
4836+ return au_sbr(sb, bindex)->br_id;
4837+}
4838+
4839+static inline
4840+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4841+{
86dc4139 4842+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4843+}
4844+
4845+static inline
4846+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4847+{
86dc4139 4848+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4849+}
4850+
5afbbe0d
AM
4851+static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
4852+{
4853+ au_br_get(au_sbr(sb, bindex));
4854+}
4855+
1facf9fc 4856+static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4857+{
5afbbe0d 4858+ au_br_put(au_sbr(sb, bindex));
1facf9fc 4859+}
4860+
4861+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4862+{
4863+ return au_sbr(sb, bindex)->br_perm;
4864+}
4865+
4866+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4867+{
4868+ return au_br_whable(au_sbr_perm(sb, bindex));
4869+}
4870+
4871+/* ---------------------------------------------------------------------- */
4872+
8b6a4947
AM
4873+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4874+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4875+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4876+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4877+/*
8b6a4947
AM
4878+#define wbr_wh_read_trylock_nested(wbr) \
4879+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4880+#define wbr_wh_write_trylock_nested(wbr) \
4881+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4882+*/
1facf9fc 4883+
8b6a4947
AM
4884+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4885+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4886+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4887+
4888+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4889+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4890+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4891+
076b876e
AM
4892+/* ---------------------------------------------------------------------- */
4893+
4894+#ifdef CONFIG_AUFS_FHSM
4895+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4896+{
4897+ mutex_init(&brfhsm->bf_lock);
4898+ brfhsm->bf_jiffy = 0;
4899+ brfhsm->bf_readable = 0;
4900+}
4901+
4902+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4903+{
4904+ mutex_destroy(&brfhsm->bf_lock);
4905+}
4906+#else
4907+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4908+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4909+#endif
4910+
1facf9fc 4911+#endif /* __KERNEL__ */
4912+#endif /* __AUFS_BRANCH_H__ */
7f207e10
AM
4913diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4914--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
be118d29 4915+++ linux/fs/aufs/conf.mk 2018-04-06 07:48:44.204604724 +0200
2121bcd9
AM
4916@@ -0,0 +1,40 @@
4917+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4918+
4919+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4920+
4921+define AuConf
4922+ifdef ${1}
4923+AuConfStr += ${1}=${${1}}
4924+endif
4925+endef
4926+
b752ccd1 4927+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4928+ SBILIST \
7f207e10 4929+ HNOTIFY HFSNOTIFY \
4a4d8108 4930+ EXPORT INO_T_64 \
c1595e42 4931+ XATTR \
076b876e 4932+ FHSM \
4a4d8108 4933+ RDU \
8b6a4947 4934+ DIRREN \
4a4d8108
AM
4935+ SHWH \
4936+ BR_RAMFS \
4937+ BR_FUSE POLL \
4938+ BR_HFSPLUS \
4939+ BDEV_LOOP \
b752ccd1
AM
4940+ DEBUG MAGIC_SYSRQ
4941+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4942+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4943+
4944+AuConfName = ${obj}/conf.str
4945+${AuConfName}.tmp: FORCE
4946+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4947+${AuConfName}: ${AuConfName}.tmp
4948+ @diff -q $< $@ > /dev/null 2>&1 || { \
4949+ echo ' GEN ' $@; \
4950+ cp -p $< $@; \
4951+ }
4952+FORCE:
4953+clean-files += ${AuConfName} ${AuConfName}.tmp
4954+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4955+
4956+-include ${srctree}/${src}/conf_priv.mk
7f207e10
AM
4957diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4958--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
be118d29
JR
4959+++ linux/fs/aufs/cpup.c 2018-04-06 07:48:44.204604724 +0200
4960@@ -0,0 +1,1441 @@
1facf9fc 4961+/*
a2654f78 4962+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 4963+ *
4964+ * This program, aufs is free software; you can redistribute it and/or modify
4965+ * it under the terms of the GNU General Public License as published by
4966+ * the Free Software Foundation; either version 2 of the License, or
4967+ * (at your option) any later version.
dece6358
AM
4968+ *
4969+ * This program is distributed in the hope that it will be useful,
4970+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4971+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4972+ * GNU General Public License for more details.
4973+ *
4974+ * You should have received a copy of the GNU General Public License
523b37e3 4975+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4976+ */
4977+
4978+/*
4979+ * copy-up functions, see wbr_policy.c for copy-down
4980+ */
4981+
4982+#include <linux/fs_stack.h>
dece6358 4983+#include <linux/mm.h>
8cdd5066 4984+#include <linux/task_work.h>
1facf9fc 4985+#include "aufs.h"
4986+
86dc4139 4987+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4988+{
4989+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4990+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4991+
86dc4139
AM
4992+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4993+
4994+ dst->i_flags |= iflags & ~mask;
1facf9fc 4995+ if (au_test_fs_notime(dst->i_sb))
4996+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4997+}
4998+
4999+void au_cpup_attr_timesizes(struct inode *inode)
5000+{
5001+ struct inode *h_inode;
5002+
5afbbe0d 5003+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5004+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 5005+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 5006+}
5007+
5008+void au_cpup_attr_nlink(struct inode *inode, int force)
5009+{
5010+ struct inode *h_inode;
5011+ struct super_block *sb;
5afbbe0d 5012+ aufs_bindex_t bindex, bbot;
1facf9fc 5013+
5014+ sb = inode->i_sb;
5afbbe0d 5015+ bindex = au_ibtop(inode);
1facf9fc 5016+ h_inode = au_h_iptr(inode, bindex);
5017+ if (!force
5018+ && !S_ISDIR(h_inode->i_mode)
5019+ && au_opt_test(au_mntflags(sb), PLINK)
5020+ && au_plink_test(inode))
5021+ return;
5022+
7eafdf33
AM
5023+ /*
5024+ * 0 can happen in revalidating.
38d290e6
JR
5025+ * h_inode->i_mutex may not be held here, but it is harmless since once
5026+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
5027+ * case.
5028+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
5029+ * the incorrect link count.
7eafdf33 5030+ */
92d182d2 5031+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 5032+
5033+ /*
5034+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
5035+ * it may includes whplink directory.
5036+ */
5037+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
5038+ bbot = au_ibbot(inode);
5039+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 5040+ h_inode = au_h_iptr(inode, bindex);
5041+ if (h_inode)
5042+ au_add_nlink(inode, h_inode);
5043+ }
5044+ }
5045+}
5046+
5047+void au_cpup_attr_changeable(struct inode *inode)
5048+{
5049+ struct inode *h_inode;
5050+
5afbbe0d 5051+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5052+ inode->i_mode = h_inode->i_mode;
5053+ inode->i_uid = h_inode->i_uid;
5054+ inode->i_gid = h_inode->i_gid;
5055+ au_cpup_attr_timesizes(inode);
86dc4139 5056+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 5057+}
5058+
5059+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
5060+{
5061+ struct au_iinfo *iinfo = au_ii(inode);
5062+
1308ab2a 5063+ IiMustWriteLock(inode);
5064+
1facf9fc 5065+ iinfo->ii_higen = h_inode->i_generation;
5066+ iinfo->ii_hsb1 = h_inode->i_sb;
5067+}
5068+
5069+void au_cpup_attr_all(struct inode *inode, int force)
5070+{
5071+ struct inode *h_inode;
5072+
5afbbe0d 5073+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5074+ au_cpup_attr_changeable(inode);
5075+ if (inode->i_nlink > 0)
5076+ au_cpup_attr_nlink(inode, force);
5077+ inode->i_rdev = h_inode->i_rdev;
5078+ inode->i_blkbits = h_inode->i_blkbits;
5079+ au_cpup_igen(inode, h_inode);
5080+}
5081+
5082+/* ---------------------------------------------------------------------- */
5083+
5084+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
5085+
5086+/* keep the timestamps of the parent dir when cpup */
5087+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5088+ struct path *h_path)
5089+{
5090+ struct inode *h_inode;
5091+
5092+ dt->dt_dentry = dentry;
5093+ dt->dt_h_path = *h_path;
5527c038 5094+ h_inode = d_inode(h_path->dentry);
1facf9fc 5095+ dt->dt_atime = h_inode->i_atime;
5096+ dt->dt_mtime = h_inode->i_mtime;
5097+ /* smp_mb(); */
5098+}
5099+
5100+void au_dtime_revert(struct au_dtime *dt)
5101+{
5102+ struct iattr attr;
5103+ int err;
5104+
5105+ attr.ia_atime = dt->dt_atime;
5106+ attr.ia_mtime = dt->dt_mtime;
5107+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5108+ | ATTR_ATIME | ATTR_ATIME_SET;
5109+
523b37e3
AM
5110+ /* no delegation since this is a directory */
5111+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 5112+ if (unlikely(err))
0c3ec466 5113+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 5114+}
5115+
5116+/* ---------------------------------------------------------------------- */
5117+
86dc4139
AM
5118+/* internal use only */
5119+struct au_cpup_reg_attr {
5120+ int valid;
5121+ struct kstat st;
5122+ unsigned int iflags; /* inode->i_flags */
5123+};
5124+
1facf9fc 5125+static noinline_for_stack
86dc4139
AM
5126+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5127+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5128+{
c1595e42 5129+ int err, sbits, icex;
7e9cd9fe
AM
5130+ unsigned int mnt_flags;
5131+ unsigned char verbose;
1facf9fc 5132+ struct iattr ia;
5133+ struct path h_path;
1308ab2a 5134+ struct inode *h_isrc, *h_idst;
86dc4139 5135+ struct kstat *h_st;
c1595e42 5136+ struct au_branch *br;
1facf9fc 5137+
5138+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 5139+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
5140+ br = au_sbr(dst->d_sb, bindex);
5141+ h_path.mnt = au_br_mnt(br);
5527c038 5142+ h_isrc = d_inode(h_src);
1308ab2a 5143+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 5144+ | ATTR_ATIME | ATTR_MTIME
5145+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
5146+ if (h_src_attr && h_src_attr->valid) {
5147+ h_st = &h_src_attr->st;
5148+ ia.ia_uid = h_st->uid;
5149+ ia.ia_gid = h_st->gid;
5150+ ia.ia_atime = h_st->atime;
5151+ ia.ia_mtime = h_st->mtime;
5152+ if (h_idst->i_mode != h_st->mode
5153+ && !S_ISLNK(h_idst->i_mode)) {
5154+ ia.ia_valid |= ATTR_MODE;
5155+ ia.ia_mode = h_st->mode;
5156+ }
5157+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5158+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5159+ } else {
5160+ ia.ia_uid = h_isrc->i_uid;
5161+ ia.ia_gid = h_isrc->i_gid;
5162+ ia.ia_atime = h_isrc->i_atime;
5163+ ia.ia_mtime = h_isrc->i_mtime;
5164+ if (h_idst->i_mode != h_isrc->i_mode
5165+ && !S_ISLNK(h_idst->i_mode)) {
5166+ ia.ia_valid |= ATTR_MODE;
5167+ ia.ia_mode = h_isrc->i_mode;
5168+ }
5169+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5170+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 5171+ }
523b37e3
AM
5172+ /* no delegation since it is just created */
5173+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5174+
5175+ /* is this nfs only? */
5176+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5177+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5178+ ia.ia_mode = h_isrc->i_mode;
523b37e3 5179+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5180+ }
5181+
c1595e42 5182+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
5183+ if (!err) {
5184+ mnt_flags = au_mntflags(dst->d_sb);
5185+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5186+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5187+ }
c1595e42 5188+
1facf9fc 5189+ return err;
5190+}
5191+
5192+/* ---------------------------------------------------------------------- */
5193+
5194+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5195+ char *buf, unsigned long blksize)
5196+{
5197+ int err;
5198+ size_t sz, rbytes, wbytes;
5199+ unsigned char all_zero;
5200+ char *p, *zp;
febd17d6 5201+ struct inode *h_inode;
1facf9fc 5202+ /* reduce stack usage */
5203+ struct iattr *ia;
5204+
5205+ zp = page_address(ZERO_PAGE(0));
5206+ if (unlikely(!zp))
5207+ return -ENOMEM; /* possible? */
5208+
5209+ err = 0;
5210+ all_zero = 0;
5211+ while (len) {
5212+ AuDbg("len %lld\n", len);
5213+ sz = blksize;
5214+ if (len < blksize)
5215+ sz = len;
5216+
5217+ rbytes = 0;
5218+ /* todo: signal_pending? */
5219+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5220+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5221+ err = rbytes;
5222+ }
5223+ if (unlikely(err < 0))
5224+ break;
5225+
5226+ all_zero = 0;
5227+ if (len >= rbytes && rbytes == blksize)
5228+ all_zero = !memcmp(buf, zp, rbytes);
5229+ if (!all_zero) {
5230+ wbytes = rbytes;
5231+ p = buf;
5232+ while (wbytes) {
5233+ size_t b;
5234+
5235+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5236+ err = b;
5237+ /* todo: signal_pending? */
5238+ if (unlikely(err == -EAGAIN || err == -EINTR))
5239+ continue;
5240+ if (unlikely(err < 0))
5241+ break;
5242+ wbytes -= b;
5243+ p += b;
5244+ }
392086de
AM
5245+ if (unlikely(err < 0))
5246+ break;
1facf9fc 5247+ } else {
5248+ loff_t res;
5249+
5250+ AuLabel(hole);
5251+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5252+ err = res;
5253+ if (unlikely(res < 0))
5254+ break;
5255+ }
5256+ len -= rbytes;
5257+ err = 0;
5258+ }
5259+
5260+ /* the last block may be a hole */
5261+ if (!err && all_zero) {
5262+ AuLabel(last hole);
5263+
5264+ err = 1;
2000de60 5265+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5266+ /* nfs requires this step to make last hole */
5267+ /* is this only nfs? */
5268+ do {
5269+ /* todo: signal_pending? */
5270+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5271+ } while (err == -EAGAIN || err == -EINTR);
5272+ if (err == 1)
5273+ dst->f_pos--;
5274+ }
5275+
5276+ if (err == 1) {
5277+ ia = (void *)buf;
5278+ ia->ia_size = dst->f_pos;
5279+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5280+ ia->ia_file = dst;
febd17d6
JR
5281+ h_inode = file_inode(dst);
5282+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5283+ /* no delegation since it is just created */
5284+ err = vfsub_notify_change(&dst->f_path, ia,
5285+ /*delegated*/NULL);
febd17d6 5286+ inode_unlock(h_inode);
1facf9fc 5287+ }
5288+ }
5289+
5290+ return err;
5291+}
5292+
5293+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5294+{
5295+ int err;
5296+ unsigned long blksize;
5297+ unsigned char do_kfree;
5298+ char *buf;
5299+
5300+ err = -ENOMEM;
2000de60 5301+ blksize = dst->f_path.dentry->d_sb->s_blocksize;
1facf9fc 5302+ if (!blksize || PAGE_SIZE < blksize)
5303+ blksize = PAGE_SIZE;
5304+ AuDbg("blksize %lu\n", blksize);
5305+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5306+ if (do_kfree)
5307+ buf = kmalloc(blksize, GFP_NOFS);
5308+ else
5309+ buf = (void *)__get_free_page(GFP_NOFS);
5310+ if (unlikely(!buf))
5311+ goto out;
5312+
5313+ if (len > (1 << 22))
5314+ AuDbg("copying a large file %lld\n", (long long)len);
5315+
5316+ src->f_pos = 0;
5317+ dst->f_pos = 0;
5318+ err = au_do_copy_file(dst, src, len, buf, blksize);
5319+ if (do_kfree)
1c60b727 5320+ kfree(buf);
1facf9fc 5321+ else
1c60b727 5322+ free_page((unsigned long)buf);
1facf9fc 5323+
4f0767ce 5324+out:
1facf9fc 5325+ return err;
5326+}
5327+
1c60b727
AM
5328+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5329+{
5330+ int err;
5331+ struct super_block *h_src_sb;
5332+ struct inode *h_src_inode;
5333+
5334+ h_src_inode = file_inode(src);
5335+ h_src_sb = h_src_inode->i_sb;
5336+
5337+ /* XFS acquires inode_lock */
5338+ if (!au_test_xfs(h_src_sb))
5339+ err = au_copy_file(dst, src, len);
5340+ else {
3c1bdaff 5341+ inode_unlock_shared(h_src_inode);
1c60b727 5342+ err = au_copy_file(dst, src, len);
be118d29 5343+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5344+ }
5345+
5346+ return err;
5347+}
5348+
5349+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5350+{
5351+ int err;
5352+ struct super_block *h_src_sb;
5353+ struct inode *h_src_inode;
5354+
5355+ h_src_inode = file_inode(src);
5356+ h_src_sb = h_src_inode->i_sb;
5357+ if (h_src_sb != file_inode(dst)->i_sb
5358+ || !dst->f_op->clone_file_range) {
5359+ err = au_do_copy(dst, src, len);
5360+ goto out;
5361+ }
5362+
5363+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5364+ inode_unlock_shared(h_src_inode);
1c60b727 5365+ err = vfsub_clone_file_range(src, dst, len);
be118d29 5366+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5367+ } else
5368+ err = vfsub_clone_file_range(src, dst, len);
5369+ /* older XFS has a condition in cloning */
5370+ if (unlikely(err != -EOPNOTSUPP))
5371+ goto out;
5372+
5373+ /* the backend fs on NFS may not support cloning */
5374+ err = au_do_copy(dst, src, len);
5375+
5376+out:
5377+ AuTraceErr(err);
5378+ return err;
5379+}
5380+
1facf9fc 5381+/*
5382+ * to support a sparse file which is opened with O_APPEND,
5383+ * we need to close the file.
5384+ */
c2b27bf2 5385+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5386+{
5387+ int err, i;
5388+ enum { SRC, DST };
5389+ struct {
5390+ aufs_bindex_t bindex;
5391+ unsigned int flags;
5392+ struct dentry *dentry;
392086de 5393+ int force_wr;
1facf9fc 5394+ struct file *file;
523b37e3 5395+ void *label;
1facf9fc 5396+ } *f, file[] = {
5397+ {
c2b27bf2 5398+ .bindex = cpg->bsrc,
1facf9fc 5399+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
523b37e3 5400+ .label = &&out
1facf9fc 5401+ },
5402+ {
c2b27bf2 5403+ .bindex = cpg->bdst,
1facf9fc 5404+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5405+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
523b37e3 5406+ .label = &&out_src
1facf9fc 5407+ }
5408+ };
521ced18 5409+ struct super_block *sb, *h_src_sb;
e2f27e51 5410+ struct inode *h_src_inode;
8cdd5066 5411+ struct task_struct *tsk = current;
1facf9fc 5412+
5413+ /* bsrc branch can be ro/rw. */
c2b27bf2 5414+ sb = cpg->dentry->d_sb;
1facf9fc 5415+ f = file;
5416+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5417+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5418+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5419+ /*file*/NULL, f->force_wr);
1facf9fc 5420+ err = PTR_ERR(f->file);
5421+ if (IS_ERR(f->file))
5422+ goto *f->label;
1facf9fc 5423+ }
5424+
5425+ /* try stopping to update while we copyup */
e2f27e51 5426+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5427+ h_src_sb = h_src_inode->i_sb;
5428+ if (!au_test_nfs(h_src_sb))
e2f27e51 5429+ IMustLock(h_src_inode);
1c60b727 5430+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5431+
8cdd5066
JR
5432+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5433+ if (tsk->flags & PF_KTHREAD)
5434+ __fput_sync(file[DST].file);
5435+ else {
2121bcd9 5436+ /* it happend actually */
8cdd5066
JR
5437+ fput(file[DST].file);
5438+ /*
5439+ * too bad.
5440+ * we have to call both since we don't know which place the file
5441+ * was added to.
5442+ */
5443+ task_work_run();
5444+ flush_delayed_fput();
5445+ }
1facf9fc 5446+ au_sbr_put(sb, file[DST].bindex);
523b37e3 5447+
4f0767ce 5448+out_src:
1facf9fc 5449+ fput(file[SRC].file);
5450+ au_sbr_put(sb, file[SRC].bindex);
4f0767ce 5451+out:
1facf9fc 5452+ return err;
5453+}
5454+
c2b27bf2 5455+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5456+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5457+{
5458+ int err, rerr;
5459+ loff_t l;
86dc4139 5460+ struct path h_path;
38d290e6 5461+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5462+
5463+ err = 0;
5527c038 5464+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5465+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5466+ if (cpg->len == -1 || l < cpg->len)
5467+ cpg->len = l;
5468+ if (cpg->len) {
86dc4139 5469+ /* try stopping to update while we are referencing */
be118d29 5470+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5471+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5472+
c2b27bf2
AM
5473+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5474+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5475+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5476+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5477+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5478+ else {
3c1bdaff 5479+ inode_unlock_shared(h_src_inode);
521ced18 5480+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5481+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5482+ }
86dc4139 5483+ if (unlikely(err)) {
3c1bdaff 5484+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5485+ goto out;
5486+ }
5487+ h_src_attr->valid = 1;
e2f27e51
AM
5488+ if (!au_test_nfs(h_src_inode->i_sb)) {
5489+ err = au_cp_regular(cpg);
3c1bdaff 5490+ inode_unlock_shared(h_src_inode);
e2f27e51 5491+ } else {
3c1bdaff 5492+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5493+ err = au_cp_regular(cpg);
5494+ }
c2b27bf2 5495+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5496+ if (!err && rerr)
5497+ err = rerr;
1facf9fc 5498+ }
38d290e6
JR
5499+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5500+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5501+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5502+ spin_lock(&h_dst_inode->i_lock);
5503+ h_dst_inode->i_state |= I_LINKABLE;
5504+ spin_unlock(&h_dst_inode->i_lock);
5505+ }
1facf9fc 5506+
4f0767ce 5507+out:
1facf9fc 5508+ return err;
5509+}
5510+
5511+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5512+ struct inode *h_dir)
5513+{
5514+ int err, symlen;
5515+ mm_segment_t old_fs;
b752ccd1
AM
5516+ union {
5517+ char *k;
5518+ char __user *u;
5519+ } sym;
1facf9fc 5520+
5521+ err = -ENOMEM;
537831f9 5522+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5523+ if (unlikely(!sym.k))
1facf9fc 5524+ goto out;
5525+
9dbd164d 5526+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5527+ old_fs = get_fs();
5528+ set_fs(KERNEL_DS);
a2654f78 5529+ symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5530+ err = symlen;
5531+ set_fs(old_fs);
5532+
5533+ if (symlen > 0) {
b752ccd1
AM
5534+ sym.k[symlen] = 0;
5535+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5536+ }
1c60b727 5537+ free_page((unsigned long)sym.k);
1facf9fc 5538+
4f0767ce 5539+out:
1facf9fc 5540+ return err;
5541+}
5542+
8cdd5066
JR
5543+/*
5544+ * regardless 'acl' option, reset all ACL.
5545+ * All ACL will be copied up later from the original entry on the lower branch.
5546+ */
5547+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5548+{
5549+ int err;
5550+ struct dentry *h_dentry;
5551+ struct inode *h_inode;
5552+
5553+ h_dentry = h_path->dentry;
5554+ h_inode = d_inode(h_dentry);
5555+ /* forget_all_cached_acls(h_inode)); */
5556+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5557+ AuTraceErr(err);
5558+ if (err == -EOPNOTSUPP)
5559+ err = 0;
5560+ if (!err)
5561+ err = vfsub_acl_chmod(h_inode, mode);
5562+
5563+ AuTraceErr(err);
5564+ return err;
5565+}
5566+
5567+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5568+ struct inode *h_dir, struct path *h_path)
5569+{
5570+ int err;
5571+ struct inode *dir, *inode;
5572+
5573+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5574+ AuTraceErr(err);
5575+ if (err == -EOPNOTSUPP)
5576+ err = 0;
5577+ if (unlikely(err))
5578+ goto out;
5579+
5580+ /*
5581+ * strange behaviour from the users view,
5582+ * particularry setattr case
5583+ */
5584+ dir = d_inode(dst_parent);
5afbbe0d 5585+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5586+ au_cpup_attr_nlink(dir, /*force*/1);
5587+ inode = d_inode(cpg->dentry);
5588+ au_cpup_attr_nlink(inode, /*force*/1);
5589+
5590+out:
5591+ return err;
5592+}
5593+
1facf9fc 5594+static noinline_for_stack
c2b27bf2 5595+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5596+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5597+{
5598+ int err;
5599+ umode_t mode;
5600+ unsigned int mnt_flags;
076b876e 5601+ unsigned char isdir, isreg, force;
c2b27bf2 5602+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5603+ struct au_dtime dt;
5604+ struct path h_path;
5605+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5606+ struct inode *h_inode, *h_dir;
1facf9fc 5607+ struct super_block *sb;
5608+
5609+ /* bsrc branch can be ro/rw. */
c2b27bf2 5610+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5611+ h_inode = d_inode(h_src);
5612+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5613+
5614+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5615+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5616+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5617+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5618+ AUFS_WH_PFX_LEN));
1facf9fc 5619+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5620+ h_dir = d_inode(h_parent);
1facf9fc 5621+ IMustLock(h_dir);
5622+ AuDebugOn(h_parent != h_dst->d_parent);
5623+
c2b27bf2
AM
5624+ sb = cpg->dentry->d_sb;
5625+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5626+ if (do_dt) {
5627+ h_path.dentry = h_parent;
5628+ au_dtime_store(&dt, dst_parent, &h_path);
5629+ }
5630+ h_path.dentry = h_dst;
5631+
076b876e 5632+ isreg = 0;
1facf9fc 5633+ isdir = 0;
5634+ mode = h_inode->i_mode;
5635+ switch (mode & S_IFMT) {
5636+ case S_IFREG:
076b876e 5637+ isreg = 1;
8cdd5066 5638+ err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
b4510431 5639+ /*want_excl*/true);
1facf9fc 5640+ if (!err)
c2b27bf2 5641+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5642+ break;
5643+ case S_IFDIR:
5644+ isdir = 1;
5645+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5646+ if (!err)
5647+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5648+ break;
5649+ case S_IFLNK:
5650+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5651+ break;
5652+ case S_IFCHR:
5653+ case S_IFBLK:
5654+ AuDebugOn(!capable(CAP_MKNOD));
5655+ /*FALLTHROUGH*/
5656+ case S_IFIFO:
5657+ case S_IFSOCK:
5658+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5659+ break;
5660+ default:
5661+ AuIOErr("Unknown inode type 0%o\n", mode);
5662+ err = -EIO;
5663+ }
8cdd5066
JR
5664+ if (!err)
5665+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5666+
5667+ mnt_flags = au_mntflags(sb);
5668+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5669+ && !isdir
5670+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5671+ && (h_inode->i_nlink == 1
5672+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5673+ /* todo: unnecessary? */
5527c038 5674+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5675+ && cpg->bdst < cpg->bsrc
5676+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5677+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5678+ /* ignore this error */
5679+
076b876e
AM
5680+ if (!err) {
5681+ force = 0;
5682+ if (isreg) {
5683+ force = !!cpg->len;
5684+ if (cpg->len == -1)
5685+ force = !!i_size_read(h_inode);
5686+ }
5687+ au_fhsm_wrote(sb, cpg->bdst, force);
5688+ }
5689+
1facf9fc 5690+ if (do_dt)
5691+ au_dtime_revert(&dt);
5692+ return err;
5693+}
5694+
392086de 5695+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5696+{
5697+ int err;
392086de 5698+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5699+ struct inode *h_dir;
392086de 5700+ aufs_bindex_t bdst;
86dc4139 5701+
392086de
AM
5702+ dentry = cpg->dentry;
5703+ bdst = cpg->bdst;
5704+ h_dentry = au_h_dptr(dentry, bdst);
5705+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5706+ dget(h_dentry);
5707+ au_set_h_dptr(dentry, bdst, NULL);
5708+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5709+ if (!err)
5710+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5711+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5712+ } else {
5713+ err = 0;
5714+ parent = dget_parent(dentry);
5715+ h_parent = au_h_dptr(parent, bdst);
5716+ dput(parent);
5717+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5718+ if (IS_ERR(h_path->dentry))
5719+ err = PTR_ERR(h_path->dentry);
86dc4139 5720+ }
392086de
AM
5721+ if (unlikely(err))
5722+ goto out;
86dc4139 5723+
86dc4139 5724+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5725+ h_dir = d_inode(h_parent);
86dc4139 5726+ IMustLock(h_dir);
523b37e3
AM
5727+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5728+ /* no delegation since it is just created */
f2c43d5f
AM
5729+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5730+ /*flags*/0);
86dc4139
AM
5731+ dput(h_path->dentry);
5732+
5733+out:
5734+ return err;
5735+}
5736+
1facf9fc 5737+/*
5738+ * copyup the @dentry from @bsrc to @bdst.
5739+ * the caller must set the both of lower dentries.
5740+ * @len is for truncating when it is -1 copyup the entire file.
5741+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5742+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f
AM
5743+ * aufs doesn't touch the credential so
5744+ * security_inode_copy_up{,_xattr}() are unnecrssary.
1facf9fc 5745+ */
c2b27bf2 5746+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5747+{
5748+ int err, rerr;
5afbbe0d 5749+ aufs_bindex_t old_ibtop;
1facf9fc 5750+ unsigned char isdir, plink;
1facf9fc 5751+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5752+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5753+ struct super_block *sb;
86dc4139 5754+ struct au_branch *br;
c2b27bf2
AM
5755+ /* to reuduce stack size */
5756+ struct {
5757+ struct au_dtime dt;
5758+ struct path h_path;
5759+ struct au_cpup_reg_attr h_src_attr;
5760+ } *a;
1facf9fc 5761+
c2b27bf2
AM
5762+ err = -ENOMEM;
5763+ a = kmalloc(sizeof(*a), GFP_NOFS);
5764+ if (unlikely(!a))
5765+ goto out;
5766+ a->h_src_attr.valid = 0;
1facf9fc 5767+
c2b27bf2
AM
5768+ sb = cpg->dentry->d_sb;
5769+ br = au_sbr(sb, cpg->bdst);
5770+ a->h_path.mnt = au_br_mnt(br);
5771+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5772+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5773+ h_dir = d_inode(h_parent);
1facf9fc 5774+ IMustLock(h_dir);
5775+
c2b27bf2 5776+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5777+ inode = d_inode(cpg->dentry);
1facf9fc 5778+
5779+ if (!dst_parent)
c2b27bf2 5780+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5781+ else
5782+ dget(dst_parent);
5783+
5784+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5785+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5786+ if (dst_inode) {
5787+ if (unlikely(!plink)) {
5788+ err = -EIO;
027c5e7a
AM
5789+ AuIOErr("hi%lu(i%lu) exists on b%d "
5790+ "but plink is disabled\n",
c2b27bf2
AM
5791+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5792+ goto out_parent;
1facf9fc 5793+ }
5794+
5795+ if (dst_inode->i_nlink) {
c2b27bf2 5796+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5797+
c2b27bf2 5798+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5799+ err = PTR_ERR(h_src);
5800+ if (IS_ERR(h_src))
c2b27bf2 5801+ goto out_parent;
5527c038 5802+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5803+ err = -EIO;
79b8bda9 5804+ AuIOErr("i%lu exists on b%d "
027c5e7a 5805+ "but not pseudo-linked\n",
79b8bda9 5806+ inode->i_ino, cpg->bdst);
1facf9fc 5807+ dput(h_src);
c2b27bf2 5808+ goto out_parent;
1facf9fc 5809+ }
5810+
5811+ if (do_dt) {
c2b27bf2
AM
5812+ a->h_path.dentry = h_parent;
5813+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5814+ }
86dc4139 5815+
c2b27bf2 5816+ a->h_path.dentry = h_dst;
523b37e3
AM
5817+ delegated = NULL;
5818+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5819+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5820+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5821+ if (do_dt)
c2b27bf2 5822+ au_dtime_revert(&a->dt);
523b37e3
AM
5823+ if (unlikely(err == -EWOULDBLOCK)) {
5824+ pr_warn("cannot retry for NFSv4 delegation"
5825+ " for an internal link\n");
5826+ iput(delegated);
5827+ }
1facf9fc 5828+ dput(h_src);
c2b27bf2 5829+ goto out_parent;
1facf9fc 5830+ } else
5831+ /* todo: cpup_wh_file? */
5832+ /* udba work */
4a4d8108 5833+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5834+ }
5835+
86dc4139 5836+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5837+ old_ibtop = au_ibtop(inode);
c2b27bf2 5838+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5839+ if (unlikely(err))
86dc4139 5840+ goto out_rev;
5527c038 5841+ dst_inode = d_inode(h_dst);
febd17d6 5842+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5843+ /* todo: necessary? */
c2b27bf2 5844+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5845+
c2b27bf2 5846+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5847+ if (unlikely(err)) {
5848+ /* todo: necessary? */
c2b27bf2 5849+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5850+ inode_unlock(dst_inode);
86dc4139
AM
5851+ goto out_rev;
5852+ }
5853+
5afbbe0d 5854+ if (cpg->bdst < old_ibtop) {
86dc4139 5855+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5856+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5857+ if (unlikely(err)) {
c2b27bf2
AM
5858+ /* ignore an error */
5859+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5860+ inode_unlock(dst_inode);
86dc4139 5861+ goto out_rev;
4a4d8108 5862+ }
4a4d8108 5863+ }
5afbbe0d 5864+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5865+ } else
5afbbe0d 5866+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5867+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5868+ au_hi_flags(inode, isdir));
5869+
5870+ /* todo: necessary? */
c2b27bf2 5871+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5872+ inode_unlock(dst_inode);
86dc4139
AM
5873+ if (unlikely(err))
5874+ goto out_rev;
5875+
5527c038 5876+ src_inode = d_inode(h_src);
86dc4139 5877+ if (!isdir
5527c038
JR
5878+ && (src_inode->i_nlink > 1
5879+ || src_inode->i_state & I_LINKABLE)
86dc4139 5880+ && plink)
c2b27bf2 5881+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5882+
c2b27bf2
AM
5883+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5884+ a->h_path.dentry = h_dst;
392086de 5885+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5886+ }
5887+ if (!err)
c2b27bf2 5888+ goto out_parent; /* success */
1facf9fc 5889+
5890+ /* revert */
4a4d8108 5891+out_rev:
c2b27bf2
AM
5892+ a->h_path.dentry = h_parent;
5893+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5894+ a->h_path.dentry = h_dst;
86dc4139 5895+ rerr = 0;
5527c038 5896+ if (d_is_positive(h_dst)) {
523b37e3
AM
5897+ if (!isdir) {
5898+ /* no delegation since it is just created */
5899+ rerr = vfsub_unlink(h_dir, &a->h_path,
5900+ /*delegated*/NULL, /*force*/0);
5901+ } else
c2b27bf2 5902+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5903+ }
c2b27bf2 5904+ au_dtime_revert(&a->dt);
1facf9fc 5905+ if (rerr) {
5906+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5907+ err = -EIO;
5908+ }
c2b27bf2 5909+out_parent:
1facf9fc 5910+ dput(dst_parent);
1c60b727 5911+ kfree(a);
c2b27bf2 5912+out:
1facf9fc 5913+ return err;
5914+}
5915+
7e9cd9fe 5916+#if 0 /* reserved */
1facf9fc 5917+struct au_cpup_single_args {
5918+ int *errp;
c2b27bf2 5919+ struct au_cp_generic *cpg;
1facf9fc 5920+ struct dentry *dst_parent;
5921+};
5922+
5923+static void au_call_cpup_single(void *args)
5924+{
5925+ struct au_cpup_single_args *a = args;
86dc4139 5926+
c2b27bf2
AM
5927+ au_pin_hdir_acquire_nest(a->cpg->pin);
5928+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5929+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5930+}
c2b27bf2 5931+#endif
1facf9fc 5932+
53392da6
AM
5933+/*
5934+ * prevent SIGXFSZ in copy-up.
5935+ * testing CAP_MKNOD is for generic fs,
5936+ * but CAP_FSETID is for xfs only, currently.
5937+ */
86dc4139 5938+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5939+{
5940+ int do_sio;
86dc4139
AM
5941+ struct super_block *sb;
5942+ struct inode *h_dir;
53392da6
AM
5943+
5944+ do_sio = 0;
86dc4139 5945+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5946+ if (!au_wkq_test()
5947+ && (!au_sbi(sb)->si_plink_maint_pid
5948+ || au_plink_maint(sb, AuLock_NOPLM))) {
5949+ switch (mode & S_IFMT) {
5950+ case S_IFREG:
5951+ /* no condition about RLIMIT_FSIZE and the file size */
5952+ do_sio = 1;
5953+ break;
5954+ case S_IFCHR:
5955+ case S_IFBLK:
5956+ do_sio = !capable(CAP_MKNOD);
5957+ break;
5958+ }
5959+ if (!do_sio)
5960+ do_sio = ((mode & (S_ISUID | S_ISGID))
5961+ && !capable(CAP_FSETID));
86dc4139
AM
5962+ /* this workaround may be removed in the future */
5963+ if (!do_sio) {
5964+ h_dir = au_pinned_h_dir(pin);
5965+ do_sio = h_dir->i_mode & S_ISVTX;
5966+ }
53392da6
AM
5967+ }
5968+
5969+ return do_sio;
5970+}
5971+
7e9cd9fe 5972+#if 0 /* reserved */
c2b27bf2 5973+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5974+{
5975+ int err, wkq_err;
1facf9fc 5976+ struct dentry *h_dentry;
5977+
c2b27bf2 5978+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5979+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5980+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5981+ else {
5982+ struct au_cpup_single_args args = {
5983+ .errp = &err,
c2b27bf2
AM
5984+ .cpg = cpg,
5985+ .dst_parent = dst_parent
1facf9fc 5986+ };
5987+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5988+ if (unlikely(wkq_err))
5989+ err = wkq_err;
5990+ }
5991+
5992+ return err;
5993+}
c2b27bf2 5994+#endif
1facf9fc 5995+
5996+/*
5997+ * copyup the @dentry from the first active lower branch to @bdst,
5998+ * using au_cpup_single().
5999+ */
c2b27bf2 6000+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6001+{
6002+ int err;
c2b27bf2
AM
6003+ unsigned int flags_orig;
6004+ struct dentry *dentry;
6005+
6006+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 6007+
c2b27bf2 6008+ dentry = cpg->dentry;
86dc4139 6009+ DiMustWriteLock(dentry);
1facf9fc 6010+
c2b27bf2 6011+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 6012+ if (!err) {
c2b27bf2
AM
6013+ flags_orig = cpg->flags;
6014+ au_fset_cpup(cpg->flags, RENAME);
6015+ err = au_cpup_single(cpg, NULL);
6016+ cpg->flags = flags_orig;
1facf9fc 6017+ if (!err)
6018+ return 0; /* success */
6019+
6020+ /* revert */
c2b27bf2 6021+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 6022+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 6023+ }
6024+
6025+ return err;
6026+}
6027+
6028+struct au_cpup_simple_args {
6029+ int *errp;
c2b27bf2 6030+ struct au_cp_generic *cpg;
1facf9fc 6031+};
6032+
6033+static void au_call_cpup_simple(void *args)
6034+{
6035+ struct au_cpup_simple_args *a = args;
86dc4139 6036+
c2b27bf2
AM
6037+ au_pin_hdir_acquire_nest(a->cpg->pin);
6038+ *a->errp = au_cpup_simple(a->cpg);
6039+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6040+}
6041+
c2b27bf2 6042+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6043+{
6044+ int err, wkq_err;
c2b27bf2
AM
6045+ struct dentry *dentry, *parent;
6046+ struct file *h_file;
1facf9fc 6047+ struct inode *h_dir;
6048+
c2b27bf2
AM
6049+ dentry = cpg->dentry;
6050+ h_file = NULL;
6051+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
6052+ AuDebugOn(cpg->bsrc < 0);
392086de 6053+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
6054+ err = PTR_ERR(h_file);
6055+ if (IS_ERR(h_file))
6056+ goto out;
6057+ }
6058+
1facf9fc 6059+ parent = dget_parent(dentry);
5527c038 6060+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 6061+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 6062+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6063+ err = au_cpup_simple(cpg);
1facf9fc 6064+ else {
6065+ struct au_cpup_simple_args args = {
6066+ .errp = &err,
c2b27bf2 6067+ .cpg = cpg
1facf9fc 6068+ };
6069+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6070+ if (unlikely(wkq_err))
6071+ err = wkq_err;
6072+ }
6073+
6074+ dput(parent);
c2b27bf2
AM
6075+ if (h_file)
6076+ au_h_open_post(dentry, cpg->bsrc, h_file);
6077+
6078+out:
1facf9fc 6079+ return err;
6080+}
6081+
c2b27bf2 6082+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 6083+{
5afbbe0d 6084+ aufs_bindex_t bsrc, bbot;
c2b27bf2 6085+ struct dentry *dentry, *h_dentry;
367653fa 6086+
c2b27bf2
AM
6087+ if (cpg->bsrc < 0) {
6088+ dentry = cpg->dentry;
5afbbe0d
AM
6089+ bbot = au_dbbot(dentry);
6090+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
6091+ h_dentry = au_h_dptr(dentry, bsrc);
6092+ if (h_dentry) {
5527c038 6093+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
6094+ break;
6095+ }
6096+ }
5afbbe0d 6097+ AuDebugOn(bsrc > bbot);
c2b27bf2 6098+ cpg->bsrc = bsrc;
367653fa 6099+ }
c2b27bf2
AM
6100+ AuDebugOn(cpg->bsrc <= cpg->bdst);
6101+ return au_do_sio_cpup_simple(cpg);
6102+}
367653fa 6103+
c2b27bf2
AM
6104+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6105+{
6106+ AuDebugOn(cpg->bdst <= cpg->bsrc);
6107+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
6108+}
6109+
1facf9fc 6110+/* ---------------------------------------------------------------------- */
6111+
6112+/*
6113+ * copyup the deleted file for writing.
6114+ */
c2b27bf2
AM
6115+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6116+ struct file *file)
1facf9fc 6117+{
6118+ int err;
c2b27bf2
AM
6119+ unsigned int flags_orig;
6120+ aufs_bindex_t bsrc_orig;
c2b27bf2 6121+ struct au_dinfo *dinfo;
5afbbe0d
AM
6122+ struct {
6123+ struct au_hdentry *hd;
6124+ struct dentry *h_dentry;
6125+ } hdst, hsrc;
1facf9fc 6126+
c2b27bf2 6127+ dinfo = au_di(cpg->dentry);
1308ab2a 6128+ AuRwMustWriteLock(&dinfo->di_rwsem);
6129+
c2b27bf2 6130+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
6131+ cpg->bsrc = dinfo->di_btop;
6132+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
6133+ hdst.h_dentry = hdst.hd->hd_dentry;
6134+ hdst.hd->hd_dentry = wh_dentry;
6135+ dinfo->di_btop = cpg->bdst;
6136+
6137+ hsrc.h_dentry = NULL;
027c5e7a 6138+ if (file) {
5afbbe0d
AM
6139+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6140+ hsrc.h_dentry = hsrc.hd->hd_dentry;
6141+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 6142+ }
c2b27bf2
AM
6143+ flags_orig = cpg->flags;
6144+ cpg->flags = !AuCpup_DTIME;
6145+ err = au_cpup_single(cpg, /*h_parent*/NULL);
6146+ cpg->flags = flags_orig;
027c5e7a
AM
6147+ if (file) {
6148+ if (!err)
6149+ err = au_reopen_nondir(file);
5afbbe0d 6150+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 6151+ }
5afbbe0d
AM
6152+ hdst.hd->hd_dentry = hdst.h_dentry;
6153+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 6154+ cpg->bsrc = bsrc_orig;
1facf9fc 6155+
6156+ return err;
6157+}
6158+
c2b27bf2 6159+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6160+{
6161+ int err;
c2b27bf2 6162+ aufs_bindex_t bdst;
1facf9fc 6163+ struct au_dtime dt;
c2b27bf2 6164+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6165+ struct au_branch *br;
6166+ struct path h_path;
6167+
c2b27bf2
AM
6168+ dentry = cpg->dentry;
6169+ bdst = cpg->bdst;
1facf9fc 6170+ br = au_sbr(dentry->d_sb, bdst);
6171+ parent = dget_parent(dentry);
6172+ h_parent = au_h_dptr(parent, bdst);
6173+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6174+ err = PTR_ERR(wh_dentry);
6175+ if (IS_ERR(wh_dentry))
6176+ goto out;
6177+
6178+ h_path.dentry = h_parent;
86dc4139 6179+ h_path.mnt = au_br_mnt(br);
1facf9fc 6180+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6181+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6182+ if (unlikely(err))
6183+ goto out_wh;
6184+
6185+ dget(wh_dentry);
6186+ h_path.dentry = wh_dentry;
2000de60 6187+ if (!d_is_dir(wh_dentry)) {
523b37e3 6188+ /* no delegation since it is just created */
5527c038 6189+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6190+ /*delegated*/NULL, /*force*/0);
6191+ } else
5527c038 6192+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6193+ if (unlikely(err)) {
523b37e3
AM
6194+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6195+ wh_dentry, err);
1facf9fc 6196+ err = -EIO;
6197+ }
6198+ au_dtime_revert(&dt);
5527c038 6199+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6200+
4f0767ce 6201+out_wh:
1facf9fc 6202+ dput(wh_dentry);
4f0767ce 6203+out:
1facf9fc 6204+ dput(parent);
6205+ return err;
6206+}
6207+
6208+struct au_cpup_wh_args {
6209+ int *errp;
c2b27bf2 6210+ struct au_cp_generic *cpg;
1facf9fc 6211+ struct file *file;
6212+};
6213+
6214+static void au_call_cpup_wh(void *args)
6215+{
6216+ struct au_cpup_wh_args *a = args;
86dc4139 6217+
c2b27bf2
AM
6218+ au_pin_hdir_acquire_nest(a->cpg->pin);
6219+ *a->errp = au_cpup_wh(a->cpg, a->file);
6220+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6221+}
6222+
c2b27bf2 6223+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6224+{
6225+ int err, wkq_err;
c2b27bf2 6226+ aufs_bindex_t bdst;
c1595e42 6227+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6228+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6229+ struct au_wbr *wbr;
c2b27bf2 6230+ struct au_pin wh_pin, *pin_orig;
1facf9fc 6231+
c2b27bf2
AM
6232+ dentry = cpg->dentry;
6233+ bdst = cpg->bdst;
1facf9fc 6234+ parent = dget_parent(dentry);
5527c038 6235+ dir = d_inode(parent);
1facf9fc 6236+ h_orph = NULL;
6237+ h_parent = NULL;
6238+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6239+ h_tmpdir = h_dir;
c2b27bf2 6240+ pin_orig = NULL;
1facf9fc 6241+ if (!h_dir->i_nlink) {
6242+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6243+ h_orph = wbr->wbr_orph;
6244+
6245+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6246+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6247+ h_tmpdir = d_inode(h_orph);
1facf9fc 6248+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6249+
febd17d6 6250+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6251+ /* todo: au_h_open_pre()? */
86dc4139 6252+
c2b27bf2 6253+ pin_orig = cpg->pin;
86dc4139 6254+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6255+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6256+ cpg->pin = &wh_pin;
1facf9fc 6257+ }
6258+
53392da6 6259+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6260+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6261+ err = au_cpup_wh(cpg, file);
1facf9fc 6262+ else {
6263+ struct au_cpup_wh_args args = {
6264+ .errp = &err,
c2b27bf2
AM
6265+ .cpg = cpg,
6266+ .file = file
1facf9fc 6267+ };
6268+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6269+ if (unlikely(wkq_err))
6270+ err = wkq_err;
6271+ }
6272+
6273+ if (h_orph) {
febd17d6 6274+ inode_unlock(h_tmpdir);
4a4d8108 6275+ /* todo: au_h_open_post()? */
1facf9fc 6276+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6277+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6278+ AuDebugOn(!pin_orig);
6279+ cpg->pin = pin_orig;
1facf9fc 6280+ }
6281+ iput(h_dir);
6282+ dput(parent);
6283+
6284+ return err;
6285+}
6286+
6287+/* ---------------------------------------------------------------------- */
6288+
6289+/*
6290+ * generic routine for both of copy-up and copy-down.
6291+ */
6292+/* cf. revalidate function in file.c */
6293+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6294+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6295+ struct au_pin *pin,
1facf9fc 6296+ struct dentry *h_parent, void *arg),
6297+ void *arg)
6298+{
6299+ int err;
6300+ struct au_pin pin;
5527c038 6301+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6302+
6303+ err = 0;
6304+ parent = dget_parent(dentry);
6305+ if (IS_ROOT(parent))
6306+ goto out;
6307+
6308+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6309+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6310+
6311+ /* do not use au_dpage */
6312+ real_parent = parent;
6313+ while (1) {
6314+ dput(parent);
6315+ parent = dget_parent(dentry);
6316+ h_parent = au_h_dptr(parent, bdst);
6317+ if (h_parent)
6318+ goto out; /* success */
6319+
6320+ /* find top dir which is necessary to cpup */
6321+ do {
6322+ d = parent;
6323+ dput(parent);
6324+ parent = dget_parent(d);
6325+ di_read_lock_parent3(parent, !AuLock_IR);
6326+ h_parent = au_h_dptr(parent, bdst);
6327+ di_read_unlock(parent, !AuLock_IR);
6328+ } while (!h_parent);
6329+
6330+ if (d != real_parent)
6331+ di_write_lock_child3(d);
6332+
6333+ /* somebody else might create while we were sleeping */
5527c038
JR
6334+ h_dentry = au_h_dptr(d, bdst);
6335+ if (!h_dentry || d_is_negative(h_dentry)) {
6336+ if (h_dentry)
5afbbe0d 6337+ au_update_dbtop(d);
1facf9fc 6338+
6339+ au_pin_set_dentry(&pin, d);
6340+ err = au_do_pin(&pin);
6341+ if (!err) {
86dc4139 6342+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6343+ au_unpin(&pin);
6344+ }
6345+ }
6346+
6347+ if (d != real_parent)
6348+ di_write_unlock(d);
6349+ if (unlikely(err))
6350+ break;
6351+ }
6352+
4f0767ce 6353+out:
1facf9fc 6354+ dput(parent);
6355+ return err;
6356+}
6357+
6358+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6359+ struct au_pin *pin,
2000de60 6360+ struct dentry *h_parent __maybe_unused,
1facf9fc 6361+ void *arg __maybe_unused)
6362+{
c2b27bf2
AM
6363+ struct au_cp_generic cpg = {
6364+ .dentry = dentry,
6365+ .bdst = bdst,
6366+ .bsrc = -1,
6367+ .len = 0,
6368+ .pin = pin,
6369+ .flags = AuCpup_DTIME
6370+ };
6371+ return au_sio_cpup_simple(&cpg);
1facf9fc 6372+}
6373+
6374+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6375+{
6376+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6377+}
6378+
6379+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6380+{
6381+ int err;
6382+ struct dentry *parent;
6383+ struct inode *dir;
6384+
6385+ parent = dget_parent(dentry);
5527c038 6386+ dir = d_inode(parent);
1facf9fc 6387+ err = 0;
6388+ if (au_h_iptr(dir, bdst))
6389+ goto out;
6390+
6391+ di_read_unlock(parent, AuLock_IR);
6392+ di_write_lock_parent(parent);
6393+ /* someone else might change our inode while we were sleeping */
6394+ if (!au_h_iptr(dir, bdst))
6395+ err = au_cpup_dirs(dentry, bdst);
6396+ di_downgrade_lock(parent, AuLock_IR);
6397+
4f0767ce 6398+out:
1facf9fc 6399+ dput(parent);
6400+ return err;
6401+}
7f207e10
AM
6402diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6403--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
be118d29 6404+++ linux/fs/aufs/cpup.h 2018-04-06 07:48:44.204604724 +0200
8b6a4947 6405@@ -0,0 +1,99 @@
1facf9fc 6406+/*
a2654f78 6407+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 6408+ *
6409+ * This program, aufs is free software; you can redistribute it and/or modify
6410+ * it under the terms of the GNU General Public License as published by
6411+ * the Free Software Foundation; either version 2 of the License, or
6412+ * (at your option) any later version.
dece6358
AM
6413+ *
6414+ * This program is distributed in the hope that it will be useful,
6415+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6416+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6417+ * GNU General Public License for more details.
6418+ *
6419+ * You should have received a copy of the GNU General Public License
523b37e3 6420+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6421+ */
6422+
6423+/*
6424+ * copy-up/down functions
6425+ */
6426+
6427+#ifndef __AUFS_CPUP_H__
6428+#define __AUFS_CPUP_H__
6429+
6430+#ifdef __KERNEL__
6431+
dece6358 6432+#include <linux/path.h>
1facf9fc 6433+
dece6358
AM
6434+struct inode;
6435+struct file;
86dc4139 6436+struct au_pin;
dece6358 6437+
86dc4139 6438+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6439+void au_cpup_attr_timesizes(struct inode *inode);
6440+void au_cpup_attr_nlink(struct inode *inode, int force);
6441+void au_cpup_attr_changeable(struct inode *inode);
6442+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6443+void au_cpup_attr_all(struct inode *inode, int force);
6444+
6445+/* ---------------------------------------------------------------------- */
6446+
c2b27bf2
AM
6447+struct au_cp_generic {
6448+ struct dentry *dentry;
6449+ aufs_bindex_t bdst, bsrc;
6450+ loff_t len;
6451+ struct au_pin *pin;
6452+ unsigned int flags;
6453+};
6454+
1facf9fc 6455+/* cpup flags */
392086de
AM
6456+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6457+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6458+ for link(2) */
6459+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6460+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6461+ cpup */
6462+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6463+ existing entry */
6464+#define AuCpup_RWDST (1 << 5) /* force write target even if
6465+ the branch is marked as RO */
c2b27bf2 6466+
8b6a4947
AM
6467+#ifndef CONFIG_AUFS_BR_HFSPLUS
6468+#undef AuCpup_HOPEN
6469+#define AuCpup_HOPEN 0
6470+#endif
6471+
1facf9fc 6472+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6473+#define au_fset_cpup(flags, name) \
6474+ do { (flags) |= AuCpup_##name; } while (0)
6475+#define au_fclr_cpup(flags, name) \
6476+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6477+
6478+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6479+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6480+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6481+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6482+
6483+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6484+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6485+ struct au_pin *pin,
1facf9fc 6486+ struct dentry *h_parent, void *arg),
6487+ void *arg);
6488+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6489+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6490+
6491+/* ---------------------------------------------------------------------- */
6492+
6493+/* keep timestamps when copyup */
6494+struct au_dtime {
6495+ struct dentry *dt_dentry;
6496+ struct path dt_h_path;
6497+ struct timespec dt_atime, dt_mtime;
6498+};
6499+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6500+ struct path *h_path);
6501+void au_dtime_revert(struct au_dtime *dt);
6502+
6503+#endif /* __KERNEL__ */
6504+#endif /* __AUFS_CPUP_H__ */
7f207e10
AM
6505diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6506--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
be118d29 6507+++ linux/fs/aufs/dbgaufs.c 2018-04-06 07:48:44.204604724 +0200
8b6a4947 6508@@ -0,0 +1,437 @@
1facf9fc 6509+/*
a2654f78 6510+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 6511+ *
6512+ * This program, aufs is free software; you can redistribute it and/or modify
6513+ * it under the terms of the GNU General Public License as published by
6514+ * the Free Software Foundation; either version 2 of the License, or
6515+ * (at your option) any later version.
dece6358
AM
6516+ *
6517+ * This program is distributed in the hope that it will be useful,
6518+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6519+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6520+ * GNU General Public License for more details.
6521+ *
6522+ * You should have received a copy of the GNU General Public License
523b37e3 6523+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6524+ */
6525+
6526+/*
6527+ * debugfs interface
6528+ */
6529+
6530+#include <linux/debugfs.h>
6531+#include "aufs.h"
6532+
6533+#ifndef CONFIG_SYSFS
6534+#error DEBUG_FS depends upon SYSFS
6535+#endif
6536+
6537+static struct dentry *dbgaufs;
6538+static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
6539+
6540+/* 20 is max digits length of ulong 64 */
6541+struct dbgaufs_arg {
6542+ int n;
6543+ char a[20 * 4];
6544+};
6545+
6546+/*
6547+ * common function for all XINO files
6548+ */
6549+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6550+ struct file *file)
6551+{
1c60b727 6552+ kfree(file->private_data);
1facf9fc 6553+ return 0;
6554+}
6555+
6556+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
6557+{
6558+ int err;
6559+ struct kstat st;
6560+ struct dbgaufs_arg *p;
6561+
6562+ err = -ENOMEM;
6563+ p = kmalloc(sizeof(*p), GFP_NOFS);
6564+ if (unlikely(!p))
6565+ goto out;
6566+
6567+ err = 0;
6568+ p->n = 0;
6569+ file->private_data = p;
6570+ if (!xf)
6571+ goto out;
6572+
521ced18 6573+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6574+ if (!err) {
6575+ if (do_fcnt)
6576+ p->n = snprintf
521ced18 6577+ (p->a, sizeof(p->a), "%ld, %llux%u %lld\n",
1facf9fc 6578+ (long)file_count(xf), st.blocks, st.blksize,
6579+ (long long)st.size);
6580+ else
521ced18 6581+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6582+ st.blocks, st.blksize,
6583+ (long long)st.size);
6584+ AuDebugOn(p->n >= sizeof(p->a));
6585+ } else {
6586+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6587+ err = 0;
6588+ }
6589+
4f0767ce 6590+out:
1facf9fc 6591+ return err;
6592+
6593+}
6594+
6595+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6596+ size_t count, loff_t *ppos)
6597+{
6598+ struct dbgaufs_arg *p;
6599+
6600+ p = file->private_data;
6601+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6602+}
6603+
6604+/* ---------------------------------------------------------------------- */
6605+
86dc4139
AM
6606+struct dbgaufs_plink_arg {
6607+ int n;
6608+ char a[];
6609+};
6610+
6611+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6612+ struct file *file)
6613+{
1c60b727 6614+ free_page((unsigned long)file->private_data);
86dc4139
AM
6615+ return 0;
6616+}
6617+
6618+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6619+{
6620+ int err, i, limit;
6621+ unsigned long n, sum;
6622+ struct dbgaufs_plink_arg *p;
6623+ struct au_sbinfo *sbinfo;
6624+ struct super_block *sb;
8b6a4947 6625+ struct hlist_bl_head *hbl;
86dc4139
AM
6626+
6627+ err = -ENOMEM;
6628+ p = (void *)get_zeroed_page(GFP_NOFS);
6629+ if (unlikely(!p))
6630+ goto out;
6631+
6632+ err = -EFBIG;
6633+ sbinfo = inode->i_private;
6634+ sb = sbinfo->si_sb;
6635+ si_noflush_read_lock(sb);
6636+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6637+ limit = PAGE_SIZE - sizeof(p->n);
6638+
6639+ /* the number of buckets */
6640+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6641+ p->n += n;
6642+ limit -= n;
6643+
6644+ sum = 0;
8b6a4947
AM
6645+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6646+ i++, hbl++) {
6647+ n = au_hbl_count(hbl);
86dc4139
AM
6648+ sum += n;
6649+
6650+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6651+ p->n += n;
6652+ limit -= n;
6653+ if (unlikely(limit <= 0))
6654+ goto out_free;
6655+ }
6656+ p->a[p->n - 1] = '\n';
6657+
6658+ /* the sum of plinks */
6659+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6660+ p->n += n;
6661+ limit -= n;
6662+ if (unlikely(limit <= 0))
6663+ goto out_free;
6664+ } else {
6665+#define str "1\n0\n0\n"
6666+ p->n = sizeof(str) - 1;
6667+ strcpy(p->a, str);
6668+#undef str
6669+ }
6670+ si_read_unlock(sb);
6671+
6672+ err = 0;
6673+ file->private_data = p;
6674+ goto out; /* success */
6675+
6676+out_free:
1c60b727 6677+ free_page((unsigned long)p);
86dc4139
AM
6678+out:
6679+ return err;
6680+}
6681+
6682+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6683+ size_t count, loff_t *ppos)
6684+{
6685+ struct dbgaufs_plink_arg *p;
6686+
6687+ p = file->private_data;
6688+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6689+}
6690+
6691+static const struct file_operations dbgaufs_plink_fop = {
6692+ .owner = THIS_MODULE,
6693+ .open = dbgaufs_plink_open,
6694+ .release = dbgaufs_plink_release,
6695+ .read = dbgaufs_plink_read
6696+};
6697+
6698+/* ---------------------------------------------------------------------- */
6699+
1facf9fc 6700+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6701+{
6702+ int err;
6703+ struct au_sbinfo *sbinfo;
6704+ struct super_block *sb;
6705+
6706+ sbinfo = inode->i_private;
6707+ sb = sbinfo->si_sb;
6708+ si_noflush_read_lock(sb);
6709+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
6710+ si_read_unlock(sb);
6711+ return err;
6712+}
6713+
6714+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6715+ .owner = THIS_MODULE,
1facf9fc 6716+ .open = dbgaufs_xib_open,
6717+ .release = dbgaufs_xi_release,
6718+ .read = dbgaufs_xi_read
6719+};
6720+
6721+/* ---------------------------------------------------------------------- */
6722+
6723+#define DbgaufsXi_PREFIX "xi"
6724+
6725+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6726+{
6727+ int err;
6728+ long l;
6729+ struct au_sbinfo *sbinfo;
6730+ struct super_block *sb;
6731+ struct file *xf;
6732+ struct qstr *name;
6733+
6734+ err = -ENOENT;
6735+ xf = NULL;
2000de60 6736+ name = &file->f_path.dentry->d_name;
1facf9fc 6737+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6738+ || memcmp(name->name, DbgaufsXi_PREFIX,
6739+ sizeof(DbgaufsXi_PREFIX) - 1)))
6740+ goto out;
9dbd164d 6741+ err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6742+ if (unlikely(err))
6743+ goto out;
6744+
6745+ sbinfo = inode->i_private;
6746+ sb = sbinfo->si_sb;
6747+ si_noflush_read_lock(sb);
5afbbe0d 6748+ if (l <= au_sbbot(sb)) {
1facf9fc 6749+ xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
6750+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
6751+ } else
6752+ err = -ENOENT;
6753+ si_read_unlock(sb);
6754+
4f0767ce 6755+out:
1facf9fc 6756+ return err;
6757+}
6758+
6759+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6760+ .owner = THIS_MODULE,
1facf9fc 6761+ .open = dbgaufs_xino_open,
6762+ .release = dbgaufs_xi_release,
6763+ .read = dbgaufs_xi_read
6764+};
6765+
6766+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6767+{
5afbbe0d 6768+ aufs_bindex_t bbot;
1facf9fc 6769+ struct au_branch *br;
6770+ struct au_xino_file *xi;
6771+
6772+ if (!au_sbi(sb)->si_dbgaufs)
6773+ return;
6774+
5afbbe0d
AM
6775+ bbot = au_sbbot(sb);
6776+ for (; bindex <= bbot; bindex++) {
1facf9fc 6777+ br = au_sbr(sb, bindex);
6778+ xi = &br->br_xino;
e2f27e51
AM
6779+ /* debugfs acquires the parent i_mutex */
6780+ lockdep_off();
c06a8ce3 6781+ debugfs_remove(xi->xi_dbgaufs);
e2f27e51 6782+ lockdep_on();
c06a8ce3 6783+ xi->xi_dbgaufs = NULL;
1facf9fc 6784+ }
6785+}
6786+
6787+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6788+{
6789+ struct au_sbinfo *sbinfo;
6790+ struct dentry *parent;
6791+ struct au_branch *br;
6792+ struct au_xino_file *xi;
5afbbe0d 6793+ aufs_bindex_t bbot;
1facf9fc 6794+ char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6795+
6796+ sbinfo = au_sbi(sb);
6797+ parent = sbinfo->si_dbgaufs;
6798+ if (!parent)
6799+ return;
6800+
5afbbe0d
AM
6801+ bbot = au_sbbot(sb);
6802+ for (; bindex <= bbot; bindex++) {
1facf9fc 6803+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6804+ br = au_sbr(sb, bindex);
6805+ xi = &br->br_xino;
6806+ AuDebugOn(xi->xi_dbgaufs);
f0c0a007
AM
6807+ /* debugfs acquires the parent i_mutex */
6808+ lockdep_off();
1facf9fc 6809+ xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6810+ sbinfo, &dbgaufs_xino_fop);
f0c0a007 6811+ lockdep_on();
1facf9fc 6812+ /* ignore an error */
6813+ if (unlikely(!xi->xi_dbgaufs))
6814+ AuWarn1("failed %s under debugfs\n", name);
6815+ }
6816+}
6817+
6818+/* ---------------------------------------------------------------------- */
6819+
6820+#ifdef CONFIG_AUFS_EXPORT
6821+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6822+{
6823+ int err;
6824+ struct au_sbinfo *sbinfo;
6825+ struct super_block *sb;
6826+
6827+ sbinfo = inode->i_private;
6828+ sb = sbinfo->si_sb;
6829+ si_noflush_read_lock(sb);
6830+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6831+ si_read_unlock(sb);
6832+ return err;
6833+}
6834+
6835+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6836+ .owner = THIS_MODULE,
1facf9fc 6837+ .open = dbgaufs_xigen_open,
6838+ .release = dbgaufs_xi_release,
6839+ .read = dbgaufs_xi_read
6840+};
6841+
6842+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6843+{
6844+ int err;
6845+
dece6358 6846+ /*
c1595e42 6847+ * This function is a dynamic '__init' function actually,
dece6358
AM
6848+ * so the tiny check for si_rwsem is unnecessary.
6849+ */
6850+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6851+
1facf9fc 6852+ err = -EIO;
6853+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6854+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6855+ &dbgaufs_xigen_fop);
6856+ if (sbinfo->si_dbgaufs_xigen)
6857+ err = 0;
6858+
6859+ return err;
6860+}
6861+#else
6862+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6863+{
6864+ return 0;
6865+}
6866+#endif /* CONFIG_AUFS_EXPORT */
6867+
6868+/* ---------------------------------------------------------------------- */
6869+
6870+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6871+{
dece6358 6872+ /*
7e9cd9fe 6873+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6874+ * so the tiny check for si_rwsem is unnecessary.
6875+ */
6876+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6877+
1facf9fc 6878+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6879+ sbinfo->si_dbgaufs = NULL;
6880+ kobject_put(&sbinfo->si_kobj);
6881+}
6882+
6883+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6884+{
6885+ int err;
6886+ char name[SysaufsSiNameLen];
6887+
dece6358 6888+ /*
c1595e42 6889+ * This function is a dynamic '__init' function actually,
dece6358
AM
6890+ * so the tiny check for si_rwsem is unnecessary.
6891+ */
6892+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6893+
1facf9fc 6894+ err = -ENOENT;
6895+ if (!dbgaufs) {
6896+ AuErr1("/debug/aufs is uninitialized\n");
6897+ goto out;
6898+ }
6899+
6900+ err = -EIO;
6901+ sysaufs_name(sbinfo, name);
6902+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6903+ if (unlikely(!sbinfo->si_dbgaufs))
6904+ goto out;
6905+ kobject_get(&sbinfo->si_kobj);
6906+
6907+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6908+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6909+ &dbgaufs_xib_fop);
6910+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6911+ goto out_dir;
6912+
86dc4139
AM
6913+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6914+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6915+ &dbgaufs_plink_fop);
6916+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6917+ goto out_dir;
6918+
1facf9fc 6919+ err = dbgaufs_xigen_init(sbinfo);
6920+ if (!err)
6921+ goto out; /* success */
6922+
4f0767ce 6923+out_dir:
1facf9fc 6924+ dbgaufs_si_fin(sbinfo);
4f0767ce 6925+out:
1facf9fc 6926+ return err;
6927+}
6928+
6929+/* ---------------------------------------------------------------------- */
6930+
6931+void dbgaufs_fin(void)
6932+{
6933+ debugfs_remove(dbgaufs);
6934+}
6935+
6936+int __init dbgaufs_init(void)
6937+{
6938+ int err;
6939+
6940+ err = -EIO;
6941+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6942+ if (dbgaufs)
6943+ err = 0;
6944+ return err;
6945+}
7f207e10
AM
6946diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6947--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
be118d29 6948+++ linux/fs/aufs/dbgaufs.h 2018-04-06 07:48:44.204604724 +0200
523b37e3 6949@@ -0,0 +1,48 @@
1facf9fc 6950+/*
a2654f78 6951+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 6952+ *
6953+ * This program, aufs is free software; you can redistribute it and/or modify
6954+ * it under the terms of the GNU General Public License as published by
6955+ * the Free Software Foundation; either version 2 of the License, or
6956+ * (at your option) any later version.
dece6358
AM
6957+ *
6958+ * This program is distributed in the hope that it will be useful,
6959+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6960+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6961+ * GNU General Public License for more details.
6962+ *
6963+ * You should have received a copy of the GNU General Public License
523b37e3 6964+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6965+ */
6966+
6967+/*
6968+ * debugfs interface
6969+ */
6970+
6971+#ifndef __DBGAUFS_H__
6972+#define __DBGAUFS_H__
6973+
6974+#ifdef __KERNEL__
6975+
dece6358 6976+struct super_block;
1facf9fc 6977+struct au_sbinfo;
dece6358 6978+
1facf9fc 6979+#ifdef CONFIG_DEBUG_FS
6980+/* dbgaufs.c */
6981+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6982+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6983+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6984+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6985+void dbgaufs_fin(void);
6986+int __init dbgaufs_init(void);
1facf9fc 6987+#else
4a4d8108
AM
6988+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6989+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6990+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6991+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6992+AuStubVoid(dbgaufs_fin, void)
6993+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 6994+#endif /* CONFIG_DEBUG_FS */
6995+
6996+#endif /* __KERNEL__ */
6997+#endif /* __DBGAUFS_H__ */
7f207e10
AM
6998diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6999--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
be118d29 7000+++ linux/fs/aufs/dcsub.c 2018-04-06 07:48:44.204604724 +0200
e2f27e51 7001@@ -0,0 +1,225 @@
1facf9fc 7002+/*
a2654f78 7003+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 7004+ *
7005+ * This program, aufs is free software; you can redistribute it and/or modify
7006+ * it under the terms of the GNU General Public License as published by
7007+ * the Free Software Foundation; either version 2 of the License, or
7008+ * (at your option) any later version.
dece6358
AM
7009+ *
7010+ * This program is distributed in the hope that it will be useful,
7011+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7012+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7013+ * GNU General Public License for more details.
7014+ *
7015+ * You should have received a copy of the GNU General Public License
523b37e3 7016+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7017+ */
7018+
7019+/*
7020+ * sub-routines for dentry cache
7021+ */
7022+
7023+#include "aufs.h"
7024+
7025+static void au_dpage_free(struct au_dpage *dpage)
7026+{
7027+ int i;
7028+ struct dentry **p;
7029+
7030+ p = dpage->dentries;
7031+ for (i = 0; i < dpage->ndentry; i++)
7032+ dput(*p++);
1c60b727 7033+ free_page((unsigned long)dpage->dentries);
1facf9fc 7034+}
7035+
7036+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7037+{
7038+ int err;
7039+ void *p;
7040+
7041+ err = -ENOMEM;
7042+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7043+ if (unlikely(!dpages->dpages))
7044+ goto out;
7045+
7046+ p = (void *)__get_free_page(gfp);
7047+ if (unlikely(!p))
7048+ goto out_dpages;
7049+
7050+ dpages->dpages[0].ndentry = 0;
7051+ dpages->dpages[0].dentries = p;
7052+ dpages->ndpage = 1;
7053+ return 0; /* success */
7054+
4f0767ce 7055+out_dpages:
1c60b727 7056+ kfree(dpages->dpages);
4f0767ce 7057+out:
1facf9fc 7058+ return err;
7059+}
7060+
7061+void au_dpages_free(struct au_dcsub_pages *dpages)
7062+{
7063+ int i;
7064+ struct au_dpage *p;
7065+
7066+ p = dpages->dpages;
7067+ for (i = 0; i < dpages->ndpage; i++)
7068+ au_dpage_free(p++);
1c60b727 7069+ kfree(dpages->dpages);
1facf9fc 7070+}
7071+
7072+static int au_dpages_append(struct au_dcsub_pages *dpages,
7073+ struct dentry *dentry, gfp_t gfp)
7074+{
7075+ int err, sz;
7076+ struct au_dpage *dpage;
7077+ void *p;
7078+
7079+ dpage = dpages->dpages + dpages->ndpage - 1;
7080+ sz = PAGE_SIZE / sizeof(dentry);
7081+ if (unlikely(dpage->ndentry >= sz)) {
7082+ AuLabel(new dpage);
7083+ err = -ENOMEM;
7084+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7085+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7086+ sz + sizeof(*dpages->dpages), gfp,
7087+ /*may_shrink*/0);
1facf9fc 7088+ if (unlikely(!p))
7089+ goto out;
7090+
7091+ dpages->dpages = p;
7092+ dpage = dpages->dpages + dpages->ndpage;
7093+ p = (void *)__get_free_page(gfp);
7094+ if (unlikely(!p))
7095+ goto out;
7096+
7097+ dpage->ndentry = 0;
7098+ dpage->dentries = p;
7099+ dpages->ndpage++;
7100+ }
7101+
c1595e42 7102+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7103+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7104+ return 0; /* success */
7105+
4f0767ce 7106+out:
1facf9fc 7107+ return err;
7108+}
7109+
c1595e42
JR
7110+/* todo: BAD approach */
7111+/* copied from linux/fs/dcache.c */
7112+enum d_walk_ret {
7113+ D_WALK_CONTINUE,
7114+ D_WALK_QUIT,
7115+ D_WALK_NORETRY,
7116+ D_WALK_SKIP,
7117+};
7118+
7119+extern void d_walk(struct dentry *parent, void *data,
7120+ enum d_walk_ret (*enter)(void *, struct dentry *),
7121+ void (*finish)(void *));
7122+
7123+struct ac_dpages_arg {
1facf9fc 7124+ int err;
c1595e42
JR
7125+ struct au_dcsub_pages *dpages;
7126+ struct super_block *sb;
7127+ au_dpages_test test;
7128+ void *arg;
7129+};
1facf9fc 7130+
c1595e42
JR
7131+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7132+{
7133+ enum d_walk_ret ret;
7134+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7135+
c1595e42
JR
7136+ ret = D_WALK_CONTINUE;
7137+ if (dentry->d_sb == arg->sb
7138+ && !IS_ROOT(dentry)
7139+ && au_dcount(dentry) > 0
7140+ && au_di(dentry)
7141+ && (!arg->test || arg->test(dentry, arg->arg))) {
7142+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7143+ if (unlikely(arg->err))
7144+ ret = D_WALK_QUIT;
1facf9fc 7145+ }
7146+
c1595e42
JR
7147+ return ret;
7148+}
027c5e7a 7149+
c1595e42
JR
7150+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7151+ au_dpages_test test, void *arg)
7152+{
7153+ struct ac_dpages_arg args = {
7154+ .err = 0,
7155+ .dpages = dpages,
7156+ .sb = root->d_sb,
7157+ .test = test,
7158+ .arg = arg
7159+ };
027c5e7a 7160+
c1595e42
JR
7161+ d_walk(root, &args, au_call_dpages_append, NULL);
7162+
7163+ return args.err;
1facf9fc 7164+}
7165+
7166+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7167+ int do_include, au_dpages_test test, void *arg)
7168+{
7169+ int err;
7170+
7171+ err = 0;
027c5e7a
AM
7172+ write_seqlock(&rename_lock);
7173+ spin_lock(&dentry->d_lock);
7174+ if (do_include
c1595e42 7175+ && au_dcount(dentry) > 0
027c5e7a 7176+ && (!test || test(dentry, arg)))
1facf9fc 7177+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7178+ spin_unlock(&dentry->d_lock);
7179+ if (unlikely(err))
7180+ goto out;
7181+
7182+ /*
523b37e3 7183+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7184+ * mount
7185+ */
1facf9fc 7186+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7187+ dentry = dentry->d_parent; /* rename_lock is locked */
7188+ spin_lock(&dentry->d_lock);
c1595e42 7189+ if (au_dcount(dentry) > 0
027c5e7a 7190+ && (!test || test(dentry, arg)))
1facf9fc 7191+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7192+ spin_unlock(&dentry->d_lock);
7193+ if (unlikely(err))
7194+ break;
1facf9fc 7195+ }
7196+
4f0767ce 7197+out:
027c5e7a 7198+ write_sequnlock(&rename_lock);
1facf9fc 7199+ return err;
7200+}
7201+
027c5e7a
AM
7202+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7203+{
7204+ return au_di(dentry) && dentry->d_sb == arg;
7205+}
7206+
7207+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7208+ struct dentry *dentry, int do_include)
7209+{
7210+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7211+ au_dcsub_dpages_aufs, dentry->d_sb);
7212+}
7213+
4a4d8108 7214+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7215+{
4a4d8108
AM
7216+ struct path path[2] = {
7217+ {
7218+ .dentry = d1
7219+ },
7220+ {
7221+ .dentry = d2
7222+ }
7223+ };
1facf9fc 7224+
4a4d8108 7225+ return path_is_under(path + 0, path + 1);
1facf9fc 7226+}
7f207e10
AM
7227diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7228--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
be118d29 7229+++ linux/fs/aufs/dcsub.h 2018-04-06 07:48:44.204604724 +0200
5527c038 7230@@ -0,0 +1,136 @@
1facf9fc 7231+/*
a2654f78 7232+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 7233+ *
7234+ * This program, aufs is free software; you can redistribute it and/or modify
7235+ * it under the terms of the GNU General Public License as published by
7236+ * the Free Software Foundation; either version 2 of the License, or
7237+ * (at your option) any later version.
dece6358
AM
7238+ *
7239+ * This program is distributed in the hope that it will be useful,
7240+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7241+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7242+ * GNU General Public License for more details.
7243+ *
7244+ * You should have received a copy of the GNU General Public License
523b37e3 7245+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7246+ */
7247+
7248+/*
7249+ * sub-routines for dentry cache
7250+ */
7251+
7252+#ifndef __AUFS_DCSUB_H__
7253+#define __AUFS_DCSUB_H__
7254+
7255+#ifdef __KERNEL__
7256+
7f207e10 7257+#include <linux/dcache.h>
027c5e7a 7258+#include <linux/fs.h>
dece6358 7259+
1facf9fc 7260+struct au_dpage {
7261+ int ndentry;
7262+ struct dentry **dentries;
7263+};
7264+
7265+struct au_dcsub_pages {
7266+ int ndpage;
7267+ struct au_dpage *dpages;
7268+};
7269+
7270+/* ---------------------------------------------------------------------- */
7271+
7f207e10 7272+/* dcsub.c */
1facf9fc 7273+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7274+void au_dpages_free(struct au_dcsub_pages *dpages);
7275+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7276+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7277+ au_dpages_test test, void *arg);
7278+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7279+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7280+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7281+ struct dentry *dentry, int do_include);
4a4d8108 7282+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7283+
7f207e10
AM
7284+/* ---------------------------------------------------------------------- */
7285+
523b37e3
AM
7286+/*
7287+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7288+ * include/linux/dcache.h. Try them (in the future).
7289+ */
7290+
027c5e7a
AM
7291+static inline int au_d_hashed_positive(struct dentry *d)
7292+{
7293+ int err;
5527c038 7294+ struct inode *inode = d_inode(d);
076b876e 7295+
027c5e7a 7296+ err = 0;
5527c038
JR
7297+ if (unlikely(d_unhashed(d)
7298+ || d_is_negative(d)
7299+ || !inode->i_nlink))
027c5e7a
AM
7300+ err = -ENOENT;
7301+ return err;
7302+}
7303+
38d290e6
JR
7304+static inline int au_d_linkable(struct dentry *d)
7305+{
7306+ int err;
5527c038 7307+ struct inode *inode = d_inode(d);
076b876e 7308+
38d290e6
JR
7309+ err = au_d_hashed_positive(d);
7310+ if (err
5527c038 7311+ && d_is_positive(d)
38d290e6
JR
7312+ && (inode->i_state & I_LINKABLE))
7313+ err = 0;
7314+ return err;
7315+}
7316+
027c5e7a
AM
7317+static inline int au_d_alive(struct dentry *d)
7318+{
7319+ int err;
7320+ struct inode *inode;
076b876e 7321+
027c5e7a
AM
7322+ err = 0;
7323+ if (!IS_ROOT(d))
7324+ err = au_d_hashed_positive(d);
7325+ else {
5527c038
JR
7326+ inode = d_inode(d);
7327+ if (unlikely(d_unlinked(d)
7328+ || d_is_negative(d)
7329+ || !inode->i_nlink))
027c5e7a
AM
7330+ err = -ENOENT;
7331+ }
7332+ return err;
7333+}
7334+
7335+static inline int au_alive_dir(struct dentry *d)
7f207e10 7336+{
027c5e7a 7337+ int err;
076b876e 7338+
027c5e7a 7339+ err = au_d_alive(d);
5527c038 7340+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7341+ err = -ENOENT;
7342+ return err;
7f207e10
AM
7343+}
7344+
38d290e6
JR
7345+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7346+{
7347+ return a->len == b->len
7348+ && !memcmp(a->name, b->name, a->len);
7349+}
7350+
7e9cd9fe
AM
7351+/*
7352+ * by the commit
7353+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7354+ * taking d_lock
7355+ * the type of d_lockref.count became int, but the inlined function d_count()
7356+ * still returns unsigned int.
7357+ * I don't know why. Maybe it is for every d_count() users?
7358+ * Anyway au_dcount() lives on.
7359+ */
c1595e42
JR
7360+static inline int au_dcount(struct dentry *d)
7361+{
7362+ return (int)d_count(d);
7363+}
7364+
1facf9fc 7365+#endif /* __KERNEL__ */
7366+#endif /* __AUFS_DCSUB_H__ */
7f207e10
AM
7367diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7368--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
be118d29 7369+++ linux/fs/aufs/debug.c 2018-04-06 07:48:44.204604724 +0200
f0c0a007 7370@@ -0,0 +1,440 @@
1facf9fc 7371+/*
a2654f78 7372+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 7373+ *
7374+ * This program, aufs is free software; you can redistribute it and/or modify
7375+ * it under the terms of the GNU General Public License as published by
7376+ * the Free Software Foundation; either version 2 of the License, or
7377+ * (at your option) any later version.
dece6358
AM
7378+ *
7379+ * This program is distributed in the hope that it will be useful,
7380+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7381+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7382+ * GNU General Public License for more details.
7383+ *
7384+ * You should have received a copy of the GNU General Public License
523b37e3 7385+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7386+ */
7387+
7388+/*
7389+ * debug print functions
7390+ */
7391+
7392+#include "aufs.h"
7393+
392086de
AM
7394+/* Returns 0, or -errno. arg is in kp->arg. */
7395+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7396+{
7397+ int err, n;
7398+
7399+ err = kstrtoint(val, 0, &n);
7400+ if (!err) {
7401+ if (n > 0)
7402+ au_debug_on();
7403+ else
7404+ au_debug_off();
7405+ }
7406+ return err;
7407+}
7408+
7409+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7410+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7411+{
7412+ atomic_t *a;
7413+
7414+ a = kp->arg;
7415+ return sprintf(buffer, "%d", atomic_read(a));
7416+}
7417+
7418+static struct kernel_param_ops param_ops_atomic_t = {
7419+ .set = param_atomic_t_set,
7420+ .get = param_atomic_t_get
7421+ /* void (*free)(void *arg) */
7422+};
7423+
7424+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7425+MODULE_PARM_DESC(debug, "debug print");
392086de 7426+module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
1facf9fc 7427+
c1595e42 7428+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7429+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7430+#define dpri(fmt, ...) do { \
7431+ if ((au_plevel \
7432+ && strcmp(au_plevel, KERN_DEBUG)) \
7433+ || au_debug_test()) \
7434+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7435+} while (0)
7436+
7437+/* ---------------------------------------------------------------------- */
7438+
7439+void au_dpri_whlist(struct au_nhash *whlist)
7440+{
7441+ unsigned long ul, n;
7442+ struct hlist_head *head;
c06a8ce3 7443+ struct au_vdir_wh *pos;
1facf9fc 7444+
7445+ n = whlist->nh_num;
7446+ head = whlist->nh_head;
7447+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7448+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7449+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7450+ pos->wh_bindex,
7451+ pos->wh_str.len, pos->wh_str.name,
7452+ pos->wh_str.len);
1facf9fc 7453+ head++;
7454+ }
7455+}
7456+
7457+void au_dpri_vdir(struct au_vdir *vdir)
7458+{
7459+ unsigned long ul;
7460+ union au_vdir_deblk_p p;
7461+ unsigned char *o;
7462+
7463+ if (!vdir || IS_ERR(vdir)) {
7464+ dpri("err %ld\n", PTR_ERR(vdir));
7465+ return;
7466+ }
7467+
be118d29 7468+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7469+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7470+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7471+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7472+ p.deblk = vdir->vd_deblk[ul];
7473+ o = p.deblk;
7474+ dpri("[%lu]: %p\n", ul, o);
7475+ }
7476+}
7477+
53392da6 7478+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7479+ struct dentry *wh)
7480+{
7481+ char *n = NULL;
7482+ int l = 0;
7483+
7484+ if (!inode || IS_ERR(inode)) {
7485+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7486+ return -1;
7487+ }
7488+
c2b27bf2 7489+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7490+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7491+ && sizeof(inode->i_blocks) != sizeof(u64));
7492+ if (wh) {
7493+ n = (void *)wh->d_name.name;
7494+ l = wh->d_name.len;
7495+ }
7496+
53392da6
AM
7497+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7498+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7499+ bindex, inode,
1facf9fc 7500+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7501+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7502+ i_size_read(inode), (unsigned long long)inode->i_blocks,
53392da6 7503+ hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7504+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7505+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7506+ inode->i_generation,
1facf9fc 7507+ l ? ", wh " : "", l, n);
7508+ return 0;
7509+}
7510+
7511+void au_dpri_inode(struct inode *inode)
7512+{
7513+ struct au_iinfo *iinfo;
5afbbe0d 7514+ struct au_hinode *hi;
1facf9fc 7515+ aufs_bindex_t bindex;
53392da6 7516+ int err, hn;
1facf9fc 7517+
53392da6 7518+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7519+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7520+ return;
7521+
7522+ iinfo = au_ii(inode);
5afbbe0d
AM
7523+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7524+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7525+ if (iinfo->ii_btop < 0)
1facf9fc 7526+ return;
53392da6 7527+ hn = 0;
5afbbe0d
AM
7528+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7529+ hi = au_hinode(iinfo, bindex);
7530+ hn = !!au_hn(hi);
7531+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7532+ }
1facf9fc 7533+}
7534+
2cbb1c4b
JR
7535+void au_dpri_dalias(struct inode *inode)
7536+{
7537+ struct dentry *d;
7538+
7539+ spin_lock(&inode->i_lock);
c1595e42 7540+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7541+ au_dpri_dentry(d);
7542+ spin_unlock(&inode->i_lock);
7543+}
7544+
1facf9fc 7545+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7546+{
7547+ struct dentry *wh = NULL;
53392da6 7548+ int hn;
5afbbe0d 7549+ struct inode *inode;
076b876e 7550+ struct au_iinfo *iinfo;
5afbbe0d 7551+ struct au_hinode *hi;
1facf9fc 7552+
7553+ if (!dentry || IS_ERR(dentry)) {
7554+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7555+ return -1;
7556+ }
7557+ /* do not call dget_parent() here */
027c5e7a 7558+ /* note: access d_xxx without d_lock */
523b37e3
AM
7559+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7560+ bindex, dentry, dentry,
1facf9fc 7561+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7562+ au_dcount(dentry), dentry->d_flags,
523b37e3 7563+ d_unhashed(dentry) ? "un" : "");
53392da6 7564+ hn = -1;
5afbbe0d
AM
7565+ inode = NULL;
7566+ if (d_is_positive(dentry))
7567+ inode = d_inode(dentry);
7568+ if (inode
7569+ && au_test_aufs(dentry->d_sb)
7570+ && bindex >= 0
7571+ && !au_is_bad_inode(inode)) {
7572+ iinfo = au_ii(inode);
7573+ hi = au_hinode(iinfo, bindex);
7574+ hn = !!au_hn(hi);
7575+ wh = hi->hi_whdentry;
7576+ }
7577+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7578+ return 0;
7579+}
7580+
7581+void au_dpri_dentry(struct dentry *dentry)
7582+{
7583+ struct au_dinfo *dinfo;
7584+ aufs_bindex_t bindex;
7585+ int err;
7586+
7587+ err = do_pri_dentry(-1, dentry);
7588+ if (err || !au_test_aufs(dentry->d_sb))
7589+ return;
7590+
7591+ dinfo = au_di(dentry);
7592+ if (!dinfo)
7593+ return;
5afbbe0d
AM
7594+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7595+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7596+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7597+ dinfo->di_tmpfile);
5afbbe0d 7598+ if (dinfo->di_btop < 0)
1facf9fc 7599+ return;
5afbbe0d
AM
7600+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7601+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7602+}
7603+
7604+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7605+{
7606+ char a[32];
7607+
7608+ if (!file || IS_ERR(file)) {
7609+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7610+ return -1;
7611+ }
7612+ a[0] = 0;
7613+ if (bindex < 0
b912730e 7614+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7615+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7616+ && au_fi(file))
e49829fe 7617+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7618+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7619+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7620+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7621+ file->f_version, file->f_pos, a);
b912730e 7622+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7623+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7624+ return 0;
7625+}
7626+
7627+void au_dpri_file(struct file *file)
7628+{
7629+ struct au_finfo *finfo;
4a4d8108
AM
7630+ struct au_fidir *fidir;
7631+ struct au_hfile *hfile;
1facf9fc 7632+ aufs_bindex_t bindex;
7633+ int err;
7634+
7635+ err = do_pri_file(-1, file);
2000de60 7636+ if (err
b912730e 7637+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7638+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7639+ return;
7640+
7641+ finfo = au_fi(file);
7642+ if (!finfo)
7643+ return;
4a4d8108 7644+ if (finfo->fi_btop < 0)
1facf9fc 7645+ return;
4a4d8108
AM
7646+ fidir = finfo->fi_hdir;
7647+ if (!fidir)
7648+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7649+ else
e49829fe
JR
7650+ for (bindex = finfo->fi_btop;
7651+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7652+ bindex++) {
7653+ hfile = fidir->fd_hfile + bindex;
7654+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7655+ }
1facf9fc 7656+}
7657+
7658+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7659+{
7660+ struct vfsmount *mnt;
7661+ struct super_block *sb;
7662+
7663+ if (!br || IS_ERR(br))
7664+ goto out;
86dc4139 7665+ mnt = au_br_mnt(br);
1facf9fc 7666+ if (!mnt || IS_ERR(mnt))
7667+ goto out;
7668+ sb = mnt->mnt_sb;
7669+ if (!sb || IS_ERR(sb))
7670+ goto out;
7671+
5afbbe0d 7672+ dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
b752ccd1 7673+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7674+ "xino %d\n",
5afbbe0d 7675+ bindex, br->br_perm, br->br_id, au_br_count(br),
1e00d052 7676+ br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7677+ sb->s_flags, sb->s_count,
1facf9fc 7678+ atomic_read(&sb->s_active), !!br->br_xino.xi_file);
7679+ return 0;
7680+
4f0767ce 7681+out:
1facf9fc 7682+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7683+ return -1;
7684+}
7685+
7686+void au_dpri_sb(struct super_block *sb)
7687+{
7688+ struct au_sbinfo *sbinfo;
7689+ aufs_bindex_t bindex;
7690+ int err;
7691+ /* to reuduce stack size */
7692+ struct {
7693+ struct vfsmount mnt;
7694+ struct au_branch fake;
7695+ } *a;
7696+
7697+ /* this function can be called from magic sysrq */
7698+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7699+ if (unlikely(!a)) {
7700+ dpri("no memory\n");
7701+ return;
7702+ }
7703+
7704+ a->mnt.mnt_sb = sb;
86dc4139 7705+ a->fake.br_path.mnt = &a->mnt;
5afbbe0d 7706+ au_br_count_init(&a->fake);
1facf9fc 7707+ err = do_pri_br(-1, &a->fake);
5afbbe0d 7708+ au_br_count_fin(&a->fake);
1c60b727 7709+ kfree(a);
1facf9fc 7710+ dpri("dev 0x%x\n", sb->s_dev);
7711+ if (err || !au_test_aufs(sb))
7712+ return;
7713+
7714+ sbinfo = au_sbi(sb);
7715+ if (!sbinfo)
7716+ return;
f0c0a007
AM
7717+ dpri("nw %d, gen %u, kobj %d\n",
7718+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7719+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7720+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7721+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7722+}
7723+
7724+/* ---------------------------------------------------------------------- */
7725+
027c5e7a
AM
7726+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7727+{
5527c038 7728+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7729+ struct dentry *h_dentry;
5afbbe0d 7730+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7731+
7732+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7733+ return;
7734+
5afbbe0d
AM
7735+ bbot = au_dbbot(dentry);
7736+ bi = au_ibbot(inode);
7737+ if (bi < bbot)
7738+ bbot = bi;
7739+ bindex = au_dbtop(dentry);
7740+ bi = au_ibtop(inode);
027c5e7a
AM
7741+ if (bi > bindex)
7742+ bindex = bi;
7743+
5afbbe0d 7744+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7745+ h_dentry = au_h_dptr(dentry, bindex);
7746+ if (!h_dentry)
7747+ continue;
7748+ h_inode = au_h_iptr(inode, bindex);
5527c038 7749+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7750+ au_debug_on();
027c5e7a
AM
7751+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7752+ AuDbgDentry(dentry);
7753+ AuDbgInode(inode);
392086de 7754+ au_debug_off();
027c5e7a
AM
7755+ BUG();
7756+ }
7757+ }
7758+}
7759+
1facf9fc 7760+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7761+{
7762+ int err, i, j;
7763+ struct au_dcsub_pages dpages;
7764+ struct au_dpage *dpage;
7765+ struct dentry **dentries;
7766+
7767+ err = au_dpages_init(&dpages, GFP_NOFS);
7768+ AuDebugOn(err);
027c5e7a 7769+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7770+ AuDebugOn(err);
7771+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7772+ dpage = dpages.dpages + i;
7773+ dentries = dpage->dentries;
7774+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7775+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7776+ }
7777+ au_dpages_free(&dpages);
7778+}
7779+
1facf9fc 7780+void au_dbg_verify_kthread(void)
7781+{
53392da6 7782+ if (au_wkq_test()) {
1facf9fc 7783+ au_dbg_blocked();
1e00d052
AM
7784+ /*
7785+ * It may be recursive, but udba=notify between two aufs mounts,
7786+ * where a single ro branch is shared, is not a problem.
7787+ */
7788+ /* WARN_ON(1); */
1facf9fc 7789+ }
7790+}
7791+
7792+/* ---------------------------------------------------------------------- */
7793+
1facf9fc 7794+int __init au_debug_init(void)
7795+{
7796+ aufs_bindex_t bindex;
7797+ struct au_vdir_destr destr;
7798+
7799+ bindex = -1;
7800+ AuDebugOn(bindex >= 0);
7801+
7802+ destr.len = -1;
7803+ AuDebugOn(destr.len < NAME_MAX);
7804+
7805+#ifdef CONFIG_4KSTACKS
0c3ec466 7806+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7807+#endif
7808+
1facf9fc 7809+ return 0;
7810+}
7f207e10
AM
7811diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7812--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
be118d29 7813+++ linux/fs/aufs/debug.h 2018-04-06 07:48:44.204604724 +0200
5527c038 7814@@ -0,0 +1,225 @@
1facf9fc 7815+/*
a2654f78 7816+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 7817+ *
7818+ * This program, aufs is free software; you can redistribute it and/or modify
7819+ * it under the terms of the GNU General Public License as published by
7820+ * the Free Software Foundation; either version 2 of the License, or
7821+ * (at your option) any later version.
dece6358
AM
7822+ *
7823+ * This program is distributed in the hope that it will be useful,
7824+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7825+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7826+ * GNU General Public License for more details.
7827+ *
7828+ * You should have received a copy of the GNU General Public License
523b37e3 7829+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7830+ */
7831+
7832+/*
7833+ * debug print functions
7834+ */
7835+
7836+#ifndef __AUFS_DEBUG_H__
7837+#define __AUFS_DEBUG_H__
7838+
7839+#ifdef __KERNEL__
7840+
392086de 7841+#include <linux/atomic.h>
4a4d8108
AM
7842+#include <linux/module.h>
7843+#include <linux/kallsyms.h>
1facf9fc 7844+#include <linux/sysrq.h>
4a4d8108 7845+
1facf9fc 7846+#ifdef CONFIG_AUFS_DEBUG
7847+#define AuDebugOn(a) BUG_ON(a)
7848+
7849+/* module parameter */
392086de
AM
7850+extern atomic_t aufs_debug;
7851+static inline void au_debug_on(void)
1facf9fc 7852+{
392086de
AM
7853+ atomic_inc(&aufs_debug);
7854+}
7855+static inline void au_debug_off(void)
7856+{
7857+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7858+}
7859+
7860+static inline int au_debug_test(void)
7861+{
392086de 7862+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7863+}
7864+#else
7865+#define AuDebugOn(a) do {} while (0)
392086de
AM
7866+AuStubVoid(au_debug_on, void)
7867+AuStubVoid(au_debug_off, void)
4a4d8108 7868+AuStubInt0(au_debug_test, void)
1facf9fc 7869+#endif /* CONFIG_AUFS_DEBUG */
7870+
392086de
AM
7871+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7872+
1facf9fc 7873+/* ---------------------------------------------------------------------- */
7874+
7875+/* debug print */
7876+
4a4d8108 7877+#define AuDbg(fmt, ...) do { \
1facf9fc 7878+ if (au_debug_test()) \
4a4d8108 7879+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7880+} while (0)
4a4d8108
AM
7881+#define AuLabel(l) AuDbg(#l "\n")
7882+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7883+#define AuWarn1(fmt, ...) do { \
1facf9fc 7884+ static unsigned char _c; \
7885+ if (!_c++) \
0c3ec466 7886+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7887+} while (0)
7888+
4a4d8108 7889+#define AuErr1(fmt, ...) do { \
1facf9fc 7890+ static unsigned char _c; \
7891+ if (!_c++) \
4a4d8108 7892+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7893+} while (0)
7894+
4a4d8108 7895+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7896+ static unsigned char _c; \
7897+ if (!_c++) \
4a4d8108 7898+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7899+} while (0)
7900+
7901+#define AuUnsupportMsg "This operation is not supported." \
7902+ " Please report this application to aufs-users ML."
4a4d8108
AM
7903+#define AuUnsupport(fmt, ...) do { \
7904+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7905+ dump_stack(); \
7906+} while (0)
7907+
7908+#define AuTraceErr(e) do { \
7909+ if (unlikely((e) < 0)) \
7910+ AuDbg("err %d\n", (int)(e)); \
7911+} while (0)
7912+
7913+#define AuTraceErrPtr(p) do { \
7914+ if (IS_ERR(p)) \
7915+ AuDbg("err %ld\n", PTR_ERR(p)); \
7916+} while (0)
7917+
7918+/* dirty macros for debug print, use with "%.*s" and caution */
7919+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 7920+
7921+/* ---------------------------------------------------------------------- */
7922+
dece6358 7923+struct dentry;
1facf9fc 7924+#ifdef CONFIG_AUFS_DEBUG
c1595e42 7925+extern struct mutex au_dbg_mtx;
1facf9fc 7926+extern char *au_plevel;
7927+struct au_nhash;
7928+void au_dpri_whlist(struct au_nhash *whlist);
7929+struct au_vdir;
7930+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 7931+struct inode;
1facf9fc 7932+void au_dpri_inode(struct inode *inode);
2cbb1c4b 7933+void au_dpri_dalias(struct inode *inode);
1facf9fc 7934+void au_dpri_dentry(struct dentry *dentry);
dece6358 7935+struct file;
1facf9fc 7936+void au_dpri_file(struct file *filp);
dece6358 7937+struct super_block;
1facf9fc 7938+void au_dpri_sb(struct super_block *sb);
7939+
027c5e7a
AM
7940+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7941+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 7942+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 7943+void au_dbg_verify_kthread(void);
7944+
7945+int __init au_debug_init(void);
7e9cd9fe 7946+
1facf9fc 7947+#define AuDbgWhlist(w) do { \
c1595e42 7948+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7949+ AuDbg(#w "\n"); \
7950+ au_dpri_whlist(w); \
c1595e42 7951+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7952+} while (0)
7953+
7954+#define AuDbgVdir(v) do { \
c1595e42 7955+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7956+ AuDbg(#v "\n"); \
7957+ au_dpri_vdir(v); \
c1595e42 7958+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7959+} while (0)
7960+
7961+#define AuDbgInode(i) do { \
c1595e42 7962+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7963+ AuDbg(#i "\n"); \
7964+ au_dpri_inode(i); \
c1595e42 7965+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7966+} while (0)
7967+
2cbb1c4b 7968+#define AuDbgDAlias(i) do { \
c1595e42 7969+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
7970+ AuDbg(#i "\n"); \
7971+ au_dpri_dalias(i); \
c1595e42 7972+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
7973+} while (0)
7974+
1facf9fc 7975+#define AuDbgDentry(d) do { \
c1595e42 7976+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7977+ AuDbg(#d "\n"); \
7978+ au_dpri_dentry(d); \
c1595e42 7979+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7980+} while (0)
7981+
7982+#define AuDbgFile(f) do { \
c1595e42 7983+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7984+ AuDbg(#f "\n"); \
7985+ au_dpri_file(f); \
c1595e42 7986+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7987+} while (0)
7988+
7989+#define AuDbgSb(sb) do { \
c1595e42 7990+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7991+ AuDbg(#sb "\n"); \
7992+ au_dpri_sb(sb); \
c1595e42 7993+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7994+} while (0)
7995+
4a4d8108
AM
7996+#define AuDbgSym(addr) do { \
7997+ char sym[KSYM_SYMBOL_LEN]; \
7998+ sprint_symbol(sym, (unsigned long)addr); \
7999+ AuDbg("%s\n", sym); \
8000+} while (0)
1facf9fc 8001+#else
027c5e7a 8002+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
8003+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8004+AuStubVoid(au_dbg_verify_kthread, void)
8005+AuStubInt0(__init au_debug_init, void)
1facf9fc 8006+
1facf9fc 8007+#define AuDbgWhlist(w) do {} while (0)
8008+#define AuDbgVdir(v) do {} while (0)
8009+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 8010+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 8011+#define AuDbgDentry(d) do {} while (0)
8012+#define AuDbgFile(f) do {} while (0)
8013+#define AuDbgSb(sb) do {} while (0)
4a4d8108 8014+#define AuDbgSym(addr) do {} while (0)
1facf9fc 8015+#endif /* CONFIG_AUFS_DEBUG */
8016+
8017+/* ---------------------------------------------------------------------- */
8018+
8019+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8020+int __init au_sysrq_init(void);
8021+void au_sysrq_fin(void);
8022+
8023+#ifdef CONFIG_HW_CONSOLE
8024+#define au_dbg_blocked() do { \
8025+ WARN_ON(1); \
0c5527e5 8026+ handle_sysrq('w'); \
1facf9fc 8027+} while (0)
8028+#else
4a4d8108 8029+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8030+#endif
8031+
8032+#else
4a4d8108
AM
8033+AuStubInt0(__init au_sysrq_init, void)
8034+AuStubVoid(au_sysrq_fin, void)
8035+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8036+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8037+
8038+#endif /* __KERNEL__ */
8039+#endif /* __AUFS_DEBUG_H__ */
7f207e10
AM
8040diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8041--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
be118d29 8042+++ linux/fs/aufs/dentry.c 2018-04-06 07:48:44.204604724 +0200
8b6a4947 8043@@ -0,0 +1,1152 @@
1facf9fc 8044+/*
a2654f78 8045+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 8046+ *
8047+ * This program, aufs is free software; you can redistribute it and/or modify
8048+ * it under the terms of the GNU General Public License as published by
8049+ * the Free Software Foundation; either version 2 of the License, or
8050+ * (at your option) any later version.
dece6358
AM
8051+ *
8052+ * This program is distributed in the hope that it will be useful,
8053+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8054+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8055+ * GNU General Public License for more details.
8056+ *
8057+ * You should have received a copy of the GNU General Public License
523b37e3 8058+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8059+ */
8060+
8061+/*
8062+ * lookup and dentry operations
8063+ */
8064+
dece6358 8065+#include <linux/namei.h>
1facf9fc 8066+#include "aufs.h"
8067+
1facf9fc 8068+/*
8069+ * returns positive/negative dentry, NULL or an error.
8070+ * NULL means whiteout-ed or not-found.
8071+ */
8072+static struct dentry*
8073+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8074+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8075+{
8076+ struct dentry *h_dentry;
2000de60 8077+ struct inode *h_inode;
1facf9fc 8078+ struct au_branch *br;
8079+ int wh_found, opq;
8080+ unsigned char wh_able;
8081+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8082+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8083+ IGNORE_PERM);
1facf9fc 8084+
1facf9fc 8085+ wh_found = 0;
8086+ br = au_sbr(dentry->d_sb, bindex);
8087+ wh_able = !!au_br_whable(br->br_perm);
8088+ if (wh_able)
8b6a4947 8089+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
1facf9fc 8090+ h_dentry = ERR_PTR(wh_found);
8091+ if (!wh_found)
8092+ goto real_lookup;
8093+ if (unlikely(wh_found < 0))
8094+ goto out;
8095+
8096+ /* We found a whiteout */
5afbbe0d 8097+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8098+ au_set_dbwh(dentry, bindex);
8099+ if (!allow_neg)
8100+ return NULL; /* success */
8101+
4f0767ce 8102+real_lookup:
076b876e 8103+ if (!ignore_perm)
8b6a4947 8104+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8105+ else
8b6a4947 8106+ h_dentry = au_sio_lkup_one(args->name, h_parent);
2000de60
JR
8107+ if (IS_ERR(h_dentry)) {
8108+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8109+ && !allow_neg)
8110+ h_dentry = NULL;
1facf9fc 8111+ goto out;
2000de60 8112+ }
1facf9fc 8113+
5527c038
JR
8114+ h_inode = d_inode(h_dentry);
8115+ if (d_is_negative(h_dentry)) {
1facf9fc 8116+ if (!allow_neg)
8117+ goto out_neg;
8118+ } else if (wh_found
8119+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8120+ goto out_neg;
8b6a4947
AM
8121+ else if (au_ftest_lkup(args->flags, DIRREN)
8122+ /* && h_inode */
8123+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8124+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8125+ (unsigned long long)h_inode->i_ino);
8126+ goto out_neg;
8127+ }
1facf9fc 8128+
5afbbe0d
AM
8129+ if (au_dbbot(dentry) <= bindex)
8130+ au_set_dbbot(dentry, bindex);
8131+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8132+ au_set_dbtop(dentry, bindex);
1facf9fc 8133+ au_set_h_dptr(dentry, bindex, h_dentry);
8134+
2000de60
JR
8135+ if (!d_is_dir(h_dentry)
8136+ || !wh_able
5527c038 8137+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8138+ goto out; /* success */
8139+
be118d29 8140+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
076b876e 8141+ opq = au_diropq_test(h_dentry);
3c1bdaff 8142+ inode_unlock_shared(h_inode);
1facf9fc 8143+ if (opq > 0)
8144+ au_set_dbdiropq(dentry, bindex);
8145+ else if (unlikely(opq < 0)) {
8146+ au_set_h_dptr(dentry, bindex, NULL);
8147+ h_dentry = ERR_PTR(opq);
8148+ }
8149+ goto out;
8150+
4f0767ce 8151+out_neg:
1facf9fc 8152+ dput(h_dentry);
8153+ h_dentry = NULL;
4f0767ce 8154+out:
1facf9fc 8155+ return h_dentry;
8156+}
8157+
dece6358
AM
8158+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8159+{
8160+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8161+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8162+ return -EPERM;
8163+ return 0;
8164+}
8165+
1facf9fc 8166+/*
8167+ * returns the number of lower positive dentries,
8168+ * otherwise an error.
8169+ * can be called at unlinking with @type is zero.
8170+ */
5afbbe0d
AM
8171+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8172+ unsigned int flags)
1facf9fc 8173+{
8174+ int npositive, err;
8175+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8176+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8177+ struct au_do_lookup_args args = {
8b6a4947
AM
8178+ .flags = flags,
8179+ .name = &dentry->d_name
1facf9fc 8180+ };
1facf9fc 8181+ struct dentry *parent;
076b876e 8182+ struct super_block *sb;
1facf9fc 8183+
076b876e 8184+ sb = dentry->d_sb;
8b6a4947 8185+ err = au_test_shwh(sb, args.name);
dece6358 8186+ if (unlikely(err))
1facf9fc 8187+ goto out;
8188+
8b6a4947 8189+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8190+ if (unlikely(err))
8191+ goto out;
8192+
2000de60 8193+ isdir = !!d_is_dir(dentry);
076b876e 8194+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8195+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8196+ if (dirren)
8197+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8198+
8199+ npositive = 0;
4a4d8108 8200+ parent = dget_parent(dentry);
1facf9fc 8201+ btail = au_dbtaildir(parent);
5afbbe0d 8202+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8203+ struct dentry *h_parent, *h_dentry;
8204+ struct inode *h_inode, *h_dir;
8b6a4947 8205+ struct au_branch *br;
1facf9fc 8206+
8207+ h_dentry = au_h_dptr(dentry, bindex);
8208+ if (h_dentry) {
5527c038 8209+ if (d_is_positive(h_dentry))
1facf9fc 8210+ npositive++;
5afbbe0d 8211+ break;
1facf9fc 8212+ }
8213+ h_parent = au_h_dptr(parent, bindex);
2000de60 8214+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8215+ continue;
8216+
8b6a4947
AM
8217+ if (dirren) {
8218+ /* if the inum matches, then use the prepared name */
8219+ err = au_dr_lkup_name(&args, bindex);
8220+ if (unlikely(err))
8221+ goto out_parent;
8222+ }
8223+
5527c038 8224+ h_dir = d_inode(h_parent);
be118d29 8225+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8226+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8227+ inode_unlock_shared(h_dir);
1facf9fc 8228+ err = PTR_ERR(h_dentry);
8229+ if (IS_ERR(h_dentry))
4a4d8108 8230+ goto out_parent;
2000de60
JR
8231+ if (h_dentry)
8232+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8233+ if (dirperm1)
8234+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8235+
79b8bda9 8236+ if (au_dbwh(dentry) == bindex)
1facf9fc 8237+ break;
8238+ if (!h_dentry)
8239+ continue;
5527c038 8240+ if (d_is_negative(h_dentry))
1facf9fc 8241+ continue;
5527c038 8242+ h_inode = d_inode(h_dentry);
1facf9fc 8243+ npositive++;
8244+ if (!args.type)
8245+ args.type = h_inode->i_mode & S_IFMT;
8246+ if (args.type != S_IFDIR)
8247+ break;
8248+ else if (isdir) {
8249+ /* the type of lower may be different */
8250+ bdiropq = au_dbdiropq(dentry);
8251+ if (bdiropq >= 0 && bdiropq <= bindex)
8252+ break;
8253+ }
8b6a4947
AM
8254+ br = au_sbr(sb, bindex);
8255+ if (dirren
8256+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8257+ /*add_ent*/NULL)) {
8258+ /* prepare next name to lookup */
8259+ err = au_dr_lkup(&args, dentry, bindex);
8260+ if (unlikely(err))
8261+ goto out_parent;
8262+ }
1facf9fc 8263+ }
8264+
8265+ if (npositive) {
8266+ AuLabel(positive);
5afbbe0d 8267+ au_update_dbtop(dentry);
1facf9fc 8268+ }
8269+ err = npositive;
076b876e 8270+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8271+ && au_dbtop(dentry) < 0)) {
1facf9fc 8272+ err = -EIO;
523b37e3
AM
8273+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8274+ dentry, err);
027c5e7a 8275+ }
1facf9fc 8276+
4f0767ce 8277+out_parent:
4a4d8108 8278+ dput(parent);
8b6a4947
AM
8279+ kfree(args.whname.name);
8280+ if (dirren)
8281+ au_dr_lkup_fin(&args);
4f0767ce 8282+out:
1facf9fc 8283+ return err;
8284+}
8285+
076b876e 8286+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 8287+{
8288+ struct dentry *dentry;
8289+ int wkq_err;
8290+
5527c038 8291+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 8292+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8293+ else {
b4510431
AM
8294+ struct vfsub_lkup_one_args args = {
8295+ .errp = &dentry,
8296+ .name = name,
8297+ .parent = parent
1facf9fc 8298+ };
8299+
b4510431 8300+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8301+ if (unlikely(wkq_err))
8302+ dentry = ERR_PTR(wkq_err);
8303+ }
8304+
8305+ return dentry;
8306+}
8307+
8308+/*
8309+ * lookup @dentry on @bindex which should be negative.
8310+ */
86dc4139 8311+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8312+{
8313+ int err;
8314+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8315+ struct au_branch *br;
1facf9fc 8316+
1facf9fc 8317+ parent = dget_parent(dentry);
8318+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
8319+ br = au_sbr(dentry->d_sb, bindex);
8320+ if (wh)
8321+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8322+ else
076b876e 8323+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 8324+ err = PTR_ERR(h_dentry);
8325+ if (IS_ERR(h_dentry))
8326+ goto out;
5527c038 8327+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8328+ err = -EIO;
523b37e3 8329+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8330+ dput(h_dentry);
8331+ goto out;
8332+ }
8333+
4a4d8108 8334+ err = 0;
5afbbe0d
AM
8335+ if (bindex < au_dbtop(dentry))
8336+ au_set_dbtop(dentry, bindex);
8337+ if (au_dbbot(dentry) < bindex)
8338+ au_set_dbbot(dentry, bindex);
1facf9fc 8339+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8340+
4f0767ce 8341+out:
1facf9fc 8342+ dput(parent);
8343+ return err;
8344+}
8345+
8346+/* ---------------------------------------------------------------------- */
8347+
8348+/* subset of struct inode */
8349+struct au_iattr {
8350+ unsigned long i_ino;
8351+ /* unsigned int i_nlink; */
0c3ec466
AM
8352+ kuid_t i_uid;
8353+ kgid_t i_gid;
1facf9fc 8354+ u64 i_version;
8355+/*
8356+ loff_t i_size;
8357+ blkcnt_t i_blocks;
8358+*/
8359+ umode_t i_mode;
8360+};
8361+
8362+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8363+{
8364+ ia->i_ino = h_inode->i_ino;
8365+ /* ia->i_nlink = h_inode->i_nlink; */
8366+ ia->i_uid = h_inode->i_uid;
8367+ ia->i_gid = h_inode->i_gid;
be118d29 8368+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8369+/*
8370+ ia->i_size = h_inode->i_size;
8371+ ia->i_blocks = h_inode->i_blocks;
8372+*/
8373+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8374+}
8375+
8376+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8377+{
8378+ return ia->i_ino != h_inode->i_ino
8379+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8380+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8381+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8382+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8383+/*
8384+ || ia->i_size != h_inode->i_size
8385+ || ia->i_blocks != h_inode->i_blocks
8386+*/
8387+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8388+}
8389+
8390+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8391+ struct au_branch *br)
8392+{
8393+ int err;
8394+ struct au_iattr ia;
8395+ struct inode *h_inode;
8396+ struct dentry *h_d;
8397+ struct super_block *h_sb;
8398+
8399+ err = 0;
8400+ memset(&ia, -1, sizeof(ia));
8401+ h_sb = h_dentry->d_sb;
5527c038
JR
8402+ h_inode = NULL;
8403+ if (d_is_positive(h_dentry)) {
8404+ h_inode = d_inode(h_dentry);
1facf9fc 8405+ au_iattr_save(&ia, h_inode);
5527c038 8406+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8407+ /* nfs d_revalidate may return 0 for negative dentry */
8408+ /* fuse d_revalidate always return 0 for negative dentry */
8409+ goto out;
8410+
8411+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8412+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8413+ err = PTR_ERR(h_d);
8414+ if (IS_ERR(h_d))
8415+ goto out;
8416+
8417+ err = 0;
8418+ if (unlikely(h_d != h_dentry
5527c038 8419+ || d_inode(h_d) != h_inode
1facf9fc 8420+ || (h_inode && au_iattr_test(&ia, h_inode))))
8421+ err = au_busy_or_stale();
8422+ dput(h_d);
8423+
4f0767ce 8424+out:
1facf9fc 8425+ AuTraceErr(err);
8426+ return err;
8427+}
8428+
8429+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8430+ struct dentry *h_parent, struct au_branch *br)
8431+{
8432+ int err;
8433+
8434+ err = 0;
027c5e7a
AM
8435+ if (udba == AuOpt_UDBA_REVAL
8436+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8437+ IMustLock(h_dir);
5527c038 8438+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8439+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8440+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8441+
8442+ return err;
8443+}
8444+
8445+/* ---------------------------------------------------------------------- */
8446+
027c5e7a 8447+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8448+{
027c5e7a 8449+ int err;
5afbbe0d 8450+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8451+ struct au_hdentry tmp, *p, *q;
8452+ struct au_dinfo *dinfo;
8453+ struct super_block *sb;
1facf9fc 8454+
027c5e7a 8455+ DiMustWriteLock(dentry);
1308ab2a 8456+
027c5e7a
AM
8457+ sb = dentry->d_sb;
8458+ dinfo = au_di(dentry);
5afbbe0d 8459+ bbot = dinfo->di_bbot;
1facf9fc 8460+ bwh = dinfo->di_bwh;
8461+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8462+ bindex = dinfo->di_btop;
8463+ p = au_hdentry(dinfo, bindex);
8464+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8465+ if (!p->hd_dentry)
1facf9fc 8466+ continue;
8467+
027c5e7a
AM
8468+ new_bindex = au_br_index(sb, p->hd_id);
8469+ if (new_bindex == bindex)
1facf9fc 8470+ continue;
1facf9fc 8471+
1facf9fc 8472+ if (dinfo->di_bwh == bindex)
8473+ bwh = new_bindex;
8474+ if (dinfo->di_bdiropq == bindex)
8475+ bdiropq = new_bindex;
8476+ if (new_bindex < 0) {
8477+ au_hdput(p);
8478+ p->hd_dentry = NULL;
8479+ continue;
8480+ }
8481+
8482+ /* swap two lower dentries, and loop again */
5afbbe0d 8483+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8484+ tmp = *q;
8485+ *q = *p;
8486+ *p = tmp;
8487+ if (tmp.hd_dentry) {
8488+ bindex--;
8489+ p--;
8490+ }
8491+ }
8492+
1facf9fc 8493+ dinfo->di_bwh = -1;
5afbbe0d 8494+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8495+ dinfo->di_bwh = bwh;
8496+
8497+ dinfo->di_bdiropq = -1;
8498+ if (bdiropq >= 0
5afbbe0d 8499+ && bdiropq <= au_sbbot(sb)
1facf9fc 8500+ && au_sbr_whable(sb, bdiropq))
8501+ dinfo->di_bdiropq = bdiropq;
8502+
027c5e7a 8503+ err = -EIO;
5afbbe0d
AM
8504+ dinfo->di_btop = -1;
8505+ dinfo->di_bbot = -1;
8506+ bbot = au_dbbot(parent);
8507+ bindex = 0;
8508+ p = au_hdentry(dinfo, bindex);
8509+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8510+ if (p->hd_dentry) {
5afbbe0d 8511+ dinfo->di_btop = bindex;
1facf9fc 8512+ break;
8513+ }
8514+
5afbbe0d
AM
8515+ if (dinfo->di_btop >= 0) {
8516+ bindex = bbot;
8517+ p = au_hdentry(dinfo, bindex);
8518+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8519+ if (p->hd_dentry) {
5afbbe0d 8520+ dinfo->di_bbot = bindex;
027c5e7a
AM
8521+ err = 0;
8522+ break;
8523+ }
8524+ }
8525+
8526+ return err;
1facf9fc 8527+}
8528+
027c5e7a 8529+static void au_do_hide(struct dentry *dentry)
1facf9fc 8530+{
027c5e7a 8531+ struct inode *inode;
1facf9fc 8532+
5527c038
JR
8533+ if (d_really_is_positive(dentry)) {
8534+ inode = d_inode(dentry);
8535+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8536+ if (inode->i_nlink && !d_unhashed(dentry))
8537+ drop_nlink(inode);
8538+ } else {
8539+ clear_nlink(inode);
8540+ /* stop next lookup */
8541+ inode->i_flags |= S_DEAD;
8542+ }
8543+ smp_mb(); /* necessary? */
8544+ }
8545+ d_drop(dentry);
8546+}
1308ab2a 8547+
027c5e7a
AM
8548+static int au_hide_children(struct dentry *parent)
8549+{
8550+ int err, i, j, ndentry;
8551+ struct au_dcsub_pages dpages;
8552+ struct au_dpage *dpage;
8553+ struct dentry *dentry;
1facf9fc 8554+
027c5e7a 8555+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8556+ if (unlikely(err))
8557+ goto out;
027c5e7a
AM
8558+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8559+ if (unlikely(err))
8560+ goto out_dpages;
1facf9fc 8561+
027c5e7a
AM
8562+ /* in reverse order */
8563+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8564+ dpage = dpages.dpages + i;
8565+ ndentry = dpage->ndentry;
8566+ for (j = ndentry - 1; j >= 0; j--) {
8567+ dentry = dpage->dentries[j];
8568+ if (dentry != parent)
8569+ au_do_hide(dentry);
8570+ }
8571+ }
1facf9fc 8572+
027c5e7a
AM
8573+out_dpages:
8574+ au_dpages_free(&dpages);
4f0767ce 8575+out:
027c5e7a 8576+ return err;
1facf9fc 8577+}
8578+
027c5e7a 8579+static void au_hide(struct dentry *dentry)
1facf9fc 8580+{
027c5e7a 8581+ int err;
1facf9fc 8582+
027c5e7a 8583+ AuDbgDentry(dentry);
2000de60 8584+ if (d_is_dir(dentry)) {
027c5e7a
AM
8585+ /* shrink_dcache_parent(dentry); */
8586+ err = au_hide_children(dentry);
8587+ if (unlikely(err))
523b37e3
AM
8588+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8589+ dentry, err);
027c5e7a
AM
8590+ }
8591+ au_do_hide(dentry);
8592+}
1facf9fc 8593+
027c5e7a
AM
8594+/*
8595+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8596+ *
8597+ * a dirty branch is added
8598+ * - on the top of layers
8599+ * - in the middle of layers
8600+ * - to the bottom of layers
8601+ *
8602+ * on the added branch there exists
8603+ * - a whiteout
8604+ * - a diropq
8605+ * - a same named entry
8606+ * + exist
8607+ * * negative --> positive
8608+ * * positive --> positive
8609+ * - type is unchanged
8610+ * - type is changed
8611+ * + doesn't exist
8612+ * * negative --> negative
8613+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8614+ * - none
8615+ */
8616+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8617+ struct au_dinfo *tmp)
8618+{
8619+ int err;
5afbbe0d 8620+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8621+ struct {
8622+ struct dentry *dentry;
8623+ struct inode *inode;
8624+ mode_t mode;
be52b249
AM
8625+ } orig_h, tmp_h = {
8626+ .dentry = NULL
8627+ };
027c5e7a
AM
8628+ struct au_hdentry *hd;
8629+ struct inode *inode, *h_inode;
8630+ struct dentry *h_dentry;
8631+
8632+ err = 0;
5afbbe0d 8633+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8634+ orig_h.mode = 0;
5afbbe0d 8635+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8636+ orig_h.inode = NULL;
8637+ if (d_is_positive(orig_h.dentry)) {
8638+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8639+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8640+ }
5afbbe0d
AM
8641+ if (tmp->di_btop >= 0) {
8642+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8643+ if (d_is_positive(tmp_h.dentry)) {
8644+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8645+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8646+ }
027c5e7a
AM
8647+ }
8648+
5527c038
JR
8649+ inode = NULL;
8650+ if (d_really_is_positive(dentry))
8651+ inode = d_inode(dentry);
027c5e7a
AM
8652+ if (!orig_h.inode) {
8653+ AuDbg("nagative originally\n");
8654+ if (inode) {
8655+ au_hide(dentry);
8656+ goto out;
8657+ }
8658+ AuDebugOn(inode);
5afbbe0d 8659+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8660+ AuDebugOn(dinfo->di_bdiropq != -1);
8661+
8662+ if (!tmp_h.inode) {
8663+ AuDbg("negative --> negative\n");
8664+ /* should have only one negative lower */
5afbbe0d
AM
8665+ if (tmp->di_btop >= 0
8666+ && tmp->di_btop < dinfo->di_btop) {
8667+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8668+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8669+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8670+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8671+ hd = au_hdentry(tmp, tmp->di_btop);
8672+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8673+ dget(hd->hd_dentry));
8674+ }
8675+ au_dbg_verify_dinode(dentry);
8676+ } else {
8677+ AuDbg("negative --> positive\n");
8678+ /*
8679+ * similar to the behaviour of creating with bypassing
8680+ * aufs.
8681+ * unhash it in order to force an error in the
8682+ * succeeding create operation.
8683+ * we should not set S_DEAD here.
8684+ */
8685+ d_drop(dentry);
8686+ /* au_di_swap(tmp, dinfo); */
8687+ au_dbg_verify_dinode(dentry);
8688+ }
8689+ } else {
8690+ AuDbg("positive originally\n");
8691+ /* inode may be NULL */
8692+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8693+ if (!tmp_h.inode) {
8694+ AuDbg("positive --> negative\n");
8695+ /* or bypassing aufs */
8696+ au_hide(dentry);
5afbbe0d 8697+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8698+ dinfo->di_bwh = tmp->di_bwh;
8699+ if (inode)
8700+ err = au_refresh_hinode_self(inode);
8701+ au_dbg_verify_dinode(dentry);
8702+ } else if (orig_h.mode == tmp_h.mode) {
8703+ AuDbg("positive --> positive, same type\n");
8704+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8705+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8706+ /*
8707+ * similar to the behaviour of removing and
8708+ * creating.
8709+ */
8710+ au_hide(dentry);
8711+ if (inode)
8712+ err = au_refresh_hinode_self(inode);
8713+ au_dbg_verify_dinode(dentry);
8714+ } else {
8715+ /* fill empty slots */
5afbbe0d
AM
8716+ if (dinfo->di_btop > tmp->di_btop)
8717+ dinfo->di_btop = tmp->di_btop;
8718+ if (dinfo->di_bbot < tmp->di_bbot)
8719+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8720+ dinfo->di_bwh = tmp->di_bwh;
8721+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8722+ bbot = dinfo->di_bbot;
8723+ bindex = tmp->di_btop;
8724+ hd = au_hdentry(tmp, bindex);
8725+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8726+ if (au_h_dptr(dentry, bindex))
8727+ continue;
5afbbe0d 8728+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8729+ if (!h_dentry)
8730+ continue;
5527c038
JR
8731+ AuDebugOn(d_is_negative(h_dentry));
8732+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8733+ AuDebugOn(orig_h.mode
8734+ != (h_inode->i_mode
8735+ & S_IFMT));
8736+ au_set_h_dptr(dentry, bindex,
8737+ dget(h_dentry));
8738+ }
5afbbe0d
AM
8739+ if (inode)
8740+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8741+ au_dbg_verify_dinode(dentry);
8742+ }
8743+ } else {
8744+ AuDbg("positive --> positive, different type\n");
8745+ /* similar to the behaviour of removing and creating */
8746+ au_hide(dentry);
8747+ if (inode)
8748+ err = au_refresh_hinode_self(inode);
8749+ au_dbg_verify_dinode(dentry);
8750+ }
8751+ }
8752+
8753+out:
8754+ return err;
8755+}
8756+
79b8bda9
AM
8757+void au_refresh_dop(struct dentry *dentry, int force_reval)
8758+{
8759+ const struct dentry_operations *dop
8760+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8761+ static const unsigned int mask
8762+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8763+
8764+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8765+
8766+ if (dentry->d_op == dop)
8767+ return;
8768+
8769+ AuDbg("%pd\n", dentry);
8770+ spin_lock(&dentry->d_lock);
8771+ if (dop == &aufs_dop)
8772+ dentry->d_flags |= mask;
8773+ else
8774+ dentry->d_flags &= ~mask;
8775+ dentry->d_op = dop;
8776+ spin_unlock(&dentry->d_lock);
8777+}
8778+
027c5e7a
AM
8779+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8780+{
e2f27e51 8781+ int err, ebrange, nbr;
027c5e7a
AM
8782+ unsigned int sigen;
8783+ struct au_dinfo *dinfo, *tmp;
8784+ struct super_block *sb;
8785+ struct inode *inode;
8786+
8787+ DiMustWriteLock(dentry);
8788+ AuDebugOn(IS_ROOT(dentry));
5527c038 8789+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8790+
8791+ sb = dentry->d_sb;
027c5e7a
AM
8792+ sigen = au_sigen(sb);
8793+ err = au_digen_test(parent, sigen);
8794+ if (unlikely(err))
8795+ goto out;
8796+
e2f27e51 8797+ nbr = au_sbbot(sb) + 1;
027c5e7a 8798+ dinfo = au_di(dentry);
e2f27e51 8799+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8800+ if (unlikely(err))
8801+ goto out;
8802+ ebrange = au_dbrange_test(dentry);
8803+ if (!ebrange)
8804+ ebrange = au_do_refresh_hdentry(dentry, parent);
8805+
38d290e6 8806+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8807+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8808+ if (d_really_is_positive(dentry)) {
8809+ inode = d_inode(dentry);
027c5e7a 8810+ err = au_refresh_hinode_self(inode);
5527c038 8811+ }
027c5e7a
AM
8812+ au_dbg_verify_dinode(dentry);
8813+ if (!err)
8814+ goto out_dgen; /* success */
8815+ goto out;
8816+ }
8817+
8818+ /* temporary dinfo */
8819+ AuDbgDentry(dentry);
8820+ err = -ENOMEM;
8821+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8822+ if (unlikely(!tmp))
8823+ goto out;
8824+ au_di_swap(tmp, dinfo);
8825+ /* returns the number of positive dentries */
8826+ /*
8827+ * if current working dir is removed, it returns an error.
8828+ * but the dentry is legal.
8829+ */
5afbbe0d 8830+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8831+ AuDbgDentry(dentry);
8832+ au_di_swap(tmp, dinfo);
8833+ if (err == -ENOENT)
8834+ err = 0;
8835+ if (err >= 0) {
8836+ /* compare/refresh by dinfo */
8837+ AuDbgDentry(dentry);
8838+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8839+ au_dbg_verify_dinode(dentry);
8840+ AuTraceErr(err);
8841+ }
e2f27e51 8842+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8843+ au_rw_write_unlock(&tmp->di_rwsem);
8844+ au_di_free(tmp);
8845+ if (unlikely(err))
8846+ goto out;
8847+
8848+out_dgen:
8849+ au_update_digen(dentry);
8850+out:
8851+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8852+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8853+ AuDbgDentry(dentry);
8854+ }
8855+ AuTraceErr(err);
8856+ return err;
8857+}
8858+
b4510431
AM
8859+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8860+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8861+{
8862+ int err, valid;
027c5e7a
AM
8863+
8864+ err = 0;
8865+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8866+ goto out;
027c5e7a
AM
8867+
8868+ AuDbg("b%d\n", bindex);
b4510431
AM
8869+ /*
8870+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8871+ * due to whiteout and branch permission.
8872+ */
8873+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8874+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8875+ /* it may return tri-state */
8876+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8877+
8878+ if (unlikely(valid < 0))
8879+ err = valid;
8880+ else if (!valid)
8881+ err = -EINVAL;
8882+
4f0767ce 8883+out:
1facf9fc 8884+ AuTraceErr(err);
8885+ return err;
8886+}
8887+
8888+/* todo: remove this */
8889+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8890+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8891+{
8892+ int err;
8893+ umode_t mode, h_mode;
5afbbe0d 8894+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8895+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8896+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8897+ struct dentry *h_dentry;
8898+ struct qstr *name, *h_name;
8899+
8900+ err = 0;
8901+ plus = 0;
8902+ mode = 0;
1facf9fc 8903+ ibs = -1;
8904+ ibe = -1;
8905+ unhashed = !!d_unhashed(dentry);
8906+ is_root = !!IS_ROOT(dentry);
8907+ name = &dentry->d_name;
38d290e6 8908+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8909+
8910+ /*
7f207e10
AM
8911+ * Theoretically, REVAL test should be unnecessary in case of
8912+ * {FS,I}NOTIFY.
8913+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8914+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8915+ * Let's do REVAL test too.
8916+ */
8917+ if (do_udba && inode) {
8918+ mode = (inode->i_mode & S_IFMT);
8919+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
8920+ ibs = au_ibtop(inode);
8921+ ibe = au_ibbot(inode);
1facf9fc 8922+ }
8923+
5afbbe0d
AM
8924+ btop = au_dbtop(dentry);
8925+ btail = btop;
1facf9fc 8926+ if (inode && S_ISDIR(inode->i_mode))
8927+ btail = au_dbtaildir(dentry);
5afbbe0d 8928+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8929+ h_dentry = au_h_dptr(dentry, bindex);
8930+ if (!h_dentry)
8931+ continue;
8932+
523b37e3
AM
8933+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8934+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 8935+ spin_lock(&h_dentry->d_lock);
1facf9fc 8936+ h_name = &h_dentry->d_name;
8937+ if (unlikely(do_udba
8938+ && !is_root
523b37e3
AM
8939+ && ((!h_nfs
8940+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 8941+ || (!tmpfile && !dirren
38d290e6
JR
8942+ && !au_qstreq(name, h_name))
8943+ ))
523b37e3
AM
8944+ || (h_nfs
8945+ && !(flags & LOOKUP_OPEN)
8946+ && (h_dentry->d_flags
8947+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 8948+ )) {
38d290e6
JR
8949+ int h_unhashed;
8950+
8951+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 8952+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
8953+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8954+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 8955+ goto err;
8956+ }
027c5e7a 8957+ spin_unlock(&h_dentry->d_lock);
1facf9fc 8958+
b4510431 8959+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 8960+ if (unlikely(err))
8961+ /* do not goto err, to keep the errno */
8962+ break;
8963+
8964+ /* todo: plink too? */
8965+ if (!do_udba)
8966+ continue;
8967+
8968+ /* UDBA tests */
5527c038 8969+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 8970+ goto err;
8971+
5527c038
JR
8972+ h_inode = NULL;
8973+ if (d_is_positive(h_dentry))
8974+ h_inode = d_inode(h_dentry);
1facf9fc 8975+ h_plus = plus;
8976+ h_mode = mode;
8977+ h_cached_inode = h_inode;
8978+ if (h_inode) {
8979+ h_mode = (h_inode->i_mode & S_IFMT);
8980+ h_plus = (h_inode->i_nlink > 0);
8981+ }
8982+ if (inode && ibs <= bindex && bindex <= ibe)
8983+ h_cached_inode = au_h_iptr(inode, bindex);
8984+
523b37e3 8985+ if (!h_nfs) {
38d290e6 8986+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
8987+ goto err;
8988+ } else {
8989+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8990+ && !is_root
8991+ && !IS_ROOT(h_dentry)
8992+ && unhashed != d_unhashed(h_dentry)))
8993+ goto err;
8994+ }
8995+ if (unlikely(mode != h_mode
1facf9fc 8996+ || h_cached_inode != h_inode))
8997+ goto err;
8998+ continue;
8999+
f6b6e03d 9000+err:
1facf9fc 9001+ err = -EINVAL;
9002+ break;
9003+ }
9004+
523b37e3 9005+ AuTraceErr(err);
1facf9fc 9006+ return err;
9007+}
9008+
027c5e7a 9009+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 9010+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9011+{
9012+ int err;
9013+ struct dentry *parent;
1facf9fc 9014+
027c5e7a 9015+ if (!au_digen_test(dentry, sigen))
1facf9fc 9016+ return 0;
9017+
9018+ parent = dget_parent(dentry);
9019+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9020+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 9021+ au_dbg_verify_gen(parent, sigen);
027c5e7a 9022+ err = au_refresh_dentry(dentry, parent);
1facf9fc 9023+ di_read_unlock(parent, AuLock_IR);
9024+ dput(parent);
027c5e7a 9025+ AuTraceErr(err);
1facf9fc 9026+ return err;
9027+}
9028+
9029+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9030+{
9031+ int err;
9032+ struct dentry *d, *parent;
1facf9fc 9033+
027c5e7a 9034+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 9035+ return simple_reval_dpath(dentry, sigen);
9036+
9037+ /* slow loop, keep it simple and stupid */
9038+ /* cf: au_cpup_dirs() */
9039+ err = 0;
9040+ parent = NULL;
027c5e7a 9041+ while (au_digen_test(dentry, sigen)) {
1facf9fc 9042+ d = dentry;
9043+ while (1) {
9044+ dput(parent);
9045+ parent = dget_parent(d);
027c5e7a 9046+ if (!au_digen_test(parent, sigen))
1facf9fc 9047+ break;
9048+ d = parent;
9049+ }
9050+
1facf9fc 9051+ if (d != dentry)
027c5e7a 9052+ di_write_lock_child2(d);
1facf9fc 9053+
9054+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9055+ if (au_digen_test(d, sigen)) {
9056+ /*
9057+ * todo: consolidate with simple_reval_dpath(),
9058+ * do_refresh() and au_reval_for_attr().
9059+ */
1facf9fc 9060+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9061+ err = au_refresh_dentry(d, parent);
1facf9fc 9062+ di_read_unlock(parent, AuLock_IR);
9063+ }
9064+
9065+ if (d != dentry)
9066+ di_write_unlock(d);
9067+ dput(parent);
9068+ if (unlikely(err))
9069+ break;
9070+ }
9071+
9072+ return err;
9073+}
9074+
9075+/*
9076+ * if valid returns 1, otherwise 0.
9077+ */
b4510431 9078+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9079+{
9080+ int valid, err;
9081+ unsigned int sigen;
8b6a4947 9082+ unsigned char do_udba, dirren;
1facf9fc 9083+ struct super_block *sb;
9084+ struct inode *inode;
9085+
027c5e7a 9086+ /* todo: support rcu-walk? */
b4510431 9087+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9088+ return -ECHILD;
9089+
9090+ valid = 0;
9091+ if (unlikely(!au_di(dentry)))
9092+ goto out;
9093+
e49829fe 9094+ valid = 1;
1facf9fc 9095+ sb = dentry->d_sb;
e49829fe
JR
9096+ /*
9097+ * todo: very ugly
9098+ * i_mutex of parent dir may be held,
9099+ * but we should not return 'invalid' due to busy.
9100+ */
9101+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9102+ if (unlikely(err)) {
9103+ valid = err;
027c5e7a 9104+ AuTraceErr(err);
e49829fe
JR
9105+ goto out;
9106+ }
5527c038
JR
9107+ inode = NULL;
9108+ if (d_really_is_positive(dentry))
9109+ inode = d_inode(dentry);
5afbbe0d 9110+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9111+ err = -EINVAL;
9112+ AuTraceErr(err);
9113+ goto out_dgrade;
9114+ }
027c5e7a
AM
9115+ if (unlikely(au_dbrange_test(dentry))) {
9116+ err = -EINVAL;
9117+ AuTraceErr(err);
9118+ goto out_dgrade;
1facf9fc 9119+ }
027c5e7a
AM
9120+
9121+ sigen = au_sigen(sb);
9122+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9123+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9124+ err = au_reval_dpath(dentry, sigen);
9125+ if (unlikely(err)) {
9126+ AuTraceErr(err);
1facf9fc 9127+ goto out_dgrade;
027c5e7a 9128+ }
1facf9fc 9129+ }
9130+ di_downgrade_lock(dentry, AuLock_IR);
9131+
1facf9fc 9132+ err = -EINVAL;
c1595e42 9133+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9134+ && inode
38d290e6 9135+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9136+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9137+ AuTraceErr(err);
027c5e7a 9138+ goto out_inval;
79b8bda9 9139+ }
027c5e7a 9140+
1facf9fc 9141+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9142+ if (do_udba && inode) {
5afbbe0d 9143+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9144+ struct inode *h_inode;
1facf9fc 9145+
5afbbe0d
AM
9146+ if (btop >= 0) {
9147+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9148+ if (h_inode && au_test_higen(inode, h_inode)) {
9149+ AuTraceErr(err);
027c5e7a 9150+ goto out_inval;
79b8bda9 9151+ }
027c5e7a 9152+ }
1facf9fc 9153+ }
9154+
8b6a4947
AM
9155+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9156+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9157+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9158+ err = -EIO;
523b37e3
AM
9159+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9160+ dentry, err);
027c5e7a 9161+ }
e49829fe 9162+ goto out_inval;
1facf9fc 9163+
4f0767ce 9164+out_dgrade:
1facf9fc 9165+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9166+out_inval:
1facf9fc 9167+ aufs_read_unlock(dentry, AuLock_IR);
9168+ AuTraceErr(err);
9169+ valid = !err;
e49829fe 9170+out:
027c5e7a 9171+ if (!valid) {
523b37e3 9172+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9173+ d_drop(dentry);
9174+ }
1facf9fc 9175+ return valid;
9176+}
9177+
9178+static void aufs_d_release(struct dentry *dentry)
9179+{
027c5e7a 9180+ if (au_di(dentry)) {
4a4d8108
AM
9181+ au_di_fin(dentry);
9182+ au_hn_di_reinit(dentry);
1facf9fc 9183+ }
1facf9fc 9184+}
9185+
4a4d8108 9186+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9187+ .d_revalidate = aufs_d_revalidate,
9188+ .d_weak_revalidate = aufs_d_revalidate,
9189+ .d_release = aufs_d_release
1facf9fc 9190+};
79b8bda9
AM
9191+
9192+/* aufs_dop without d_revalidate */
9193+const struct dentry_operations aufs_dop_noreval = {
9194+ .d_release = aufs_d_release
9195+};
7f207e10
AM
9196diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9197--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
be118d29 9198+++ linux/fs/aufs/dentry.h 2018-04-06 07:48:44.204604724 +0200
8b6a4947 9199@@ -0,0 +1,266 @@
1facf9fc 9200+/*
a2654f78 9201+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 9202+ *
9203+ * This program, aufs is free software; you can redistribute it and/or modify
9204+ * it under the terms of the GNU General Public License as published by
9205+ * the Free Software Foundation; either version 2 of the License, or
9206+ * (at your option) any later version.
dece6358
AM
9207+ *
9208+ * This program is distributed in the hope that it will be useful,
9209+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9210+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9211+ * GNU General Public License for more details.
9212+ *
9213+ * You should have received a copy of the GNU General Public License
523b37e3 9214+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9215+ */
9216+
9217+/*
9218+ * lookup and dentry operations
9219+ */
9220+
9221+#ifndef __AUFS_DENTRY_H__
9222+#define __AUFS_DENTRY_H__
9223+
9224+#ifdef __KERNEL__
9225+
dece6358 9226+#include <linux/dcache.h>
8b6a4947 9227+#include "dirren.h"
1facf9fc 9228+#include "rwsem.h"
9229+
1facf9fc 9230+struct au_hdentry {
9231+ struct dentry *hd_dentry;
027c5e7a 9232+ aufs_bindex_t hd_id;
1facf9fc 9233+};
9234+
9235+struct au_dinfo {
9236+ atomic_t di_generation;
9237+
dece6358 9238+ struct au_rwsem di_rwsem;
5afbbe0d 9239+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9240+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9241+ struct au_hdentry *di_hdentry;
4a4d8108 9242+} ____cacheline_aligned_in_smp;
1facf9fc 9243+
9244+/* ---------------------------------------------------------------------- */
9245+
5afbbe0d
AM
9246+/* flags for au_lkup_dentry() */
9247+#define AuLkup_ALLOW_NEG 1
9248+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9249+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9250+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9251+#define au_fset_lkup(flags, name) \
9252+ do { (flags) |= AuLkup_##name; } while (0)
9253+#define au_fclr_lkup(flags, name) \
9254+ do { (flags) &= ~AuLkup_##name; } while (0)
9255+
8b6a4947
AM
9256+#ifndef CONFIG_AUFS_DIRREN
9257+#undef AuLkup_DIRREN
9258+#define AuLkup_DIRREN 0
9259+#endif
9260+
9261+struct au_do_lookup_args {
9262+ unsigned int flags;
9263+ mode_t type;
9264+ struct qstr whname, *name;
9265+ struct au_dr_lookup dirren;
9266+};
9267+
5afbbe0d
AM
9268+/* ---------------------------------------------------------------------- */
9269+
1facf9fc 9270+/* dentry.c */
79b8bda9 9271+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9272+struct au_branch;
076b876e 9273+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 9274+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9275+ struct dentry *h_parent, struct au_branch *br);
9276+
5afbbe0d
AM
9277+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9278+ unsigned int flags);
86dc4139 9279+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9280+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9281+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9282+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9283+
9284+/* dinfo.c */
4a4d8108 9285+void au_di_init_once(void *_di);
027c5e7a
AM
9286+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9287+void au_di_free(struct au_dinfo *dinfo);
9288+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9289+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9290+int au_di_init(struct dentry *dentry);
9291+void au_di_fin(struct dentry *dentry);
e2f27e51 9292+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9293+
9294+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9295+void di_read_unlock(struct dentry *d, int flags);
9296+void di_downgrade_lock(struct dentry *d, int flags);
9297+void di_write_lock(struct dentry *d, unsigned int lsc);
9298+void di_write_unlock(struct dentry *d);
9299+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9300+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9301+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9302+
9303+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9304+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9305+aufs_bindex_t au_dbtail(struct dentry *dentry);
9306+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9307+
9308+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9309+ struct dentry *h_dentry);
027c5e7a
AM
9310+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9311+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9312+void au_update_digen(struct dentry *dentry);
9313+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9314+void au_update_dbtop(struct dentry *dentry);
9315+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9316+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9317+
9318+/* ---------------------------------------------------------------------- */
9319+
9320+static inline struct au_dinfo *au_di(struct dentry *dentry)
9321+{
9322+ return dentry->d_fsdata;
9323+}
9324+
9325+/* ---------------------------------------------------------------------- */
9326+
9327+/* lock subclass for dinfo */
9328+enum {
9329+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9330+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9331+ AuLsc_DI_CHILD3, /* copyup dirs */
9332+ AuLsc_DI_PARENT,
9333+ AuLsc_DI_PARENT2,
027c5e7a
AM
9334+ AuLsc_DI_PARENT3,
9335+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9336+};
9337+
9338+/*
9339+ * di_read_lock_child, di_write_lock_child,
9340+ * di_read_lock_child2, di_write_lock_child2,
9341+ * di_read_lock_child3, di_write_lock_child3,
9342+ * di_read_lock_parent, di_write_lock_parent,
9343+ * di_read_lock_parent2, di_write_lock_parent2,
9344+ * di_read_lock_parent3, di_write_lock_parent3,
9345+ */
9346+#define AuReadLockFunc(name, lsc) \
9347+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9348+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9349+
9350+#define AuWriteLockFunc(name, lsc) \
9351+static inline void di_write_lock_##name(struct dentry *d) \
9352+{ di_write_lock(d, AuLsc_DI_##lsc); }
9353+
9354+#define AuRWLockFuncs(name, lsc) \
9355+ AuReadLockFunc(name, lsc) \
9356+ AuWriteLockFunc(name, lsc)
9357+
9358+AuRWLockFuncs(child, CHILD);
9359+AuRWLockFuncs(child2, CHILD2);
9360+AuRWLockFuncs(child3, CHILD3);
9361+AuRWLockFuncs(parent, PARENT);
9362+AuRWLockFuncs(parent2, PARENT2);
9363+AuRWLockFuncs(parent3, PARENT3);
9364+
9365+#undef AuReadLockFunc
9366+#undef AuWriteLockFunc
9367+#undef AuRWLockFuncs
9368+
9369+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9370+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9371+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9372+
9373+/* ---------------------------------------------------------------------- */
9374+
9375+/* todo: memory barrier? */
9376+static inline unsigned int au_digen(struct dentry *d)
9377+{
9378+ return atomic_read(&au_di(d)->di_generation);
9379+}
9380+
9381+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9382+{
9383+ hdentry->hd_dentry = NULL;
9384+}
9385+
5afbbe0d
AM
9386+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9387+ aufs_bindex_t bindex)
9388+{
9389+ return di->di_hdentry + bindex;
9390+}
9391+
1facf9fc 9392+static inline void au_hdput(struct au_hdentry *hd)
9393+{
4a4d8108
AM
9394+ if (hd)
9395+ dput(hd->hd_dentry);
1facf9fc 9396+}
9397+
5afbbe0d 9398+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9399+{
1308ab2a 9400+ DiMustAnyLock(dentry);
5afbbe0d 9401+ return au_di(dentry)->di_btop;
1facf9fc 9402+}
9403+
5afbbe0d 9404+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9405+{
1308ab2a 9406+ DiMustAnyLock(dentry);
5afbbe0d 9407+ return au_di(dentry)->di_bbot;
1facf9fc 9408+}
9409+
9410+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9411+{
1308ab2a 9412+ DiMustAnyLock(dentry);
1facf9fc 9413+ return au_di(dentry)->di_bwh;
9414+}
9415+
9416+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9417+{
1308ab2a 9418+ DiMustAnyLock(dentry);
1facf9fc 9419+ return au_di(dentry)->di_bdiropq;
9420+}
9421+
9422+/* todo: hard/soft set? */
5afbbe0d 9423+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9424+{
1308ab2a 9425+ DiMustWriteLock(dentry);
5afbbe0d 9426+ au_di(dentry)->di_btop = bindex;
1facf9fc 9427+}
9428+
5afbbe0d 9429+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9430+{
1308ab2a 9431+ DiMustWriteLock(dentry);
5afbbe0d 9432+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9433+}
9434+
9435+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9436+{
1308ab2a 9437+ DiMustWriteLock(dentry);
5afbbe0d 9438+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9439+ au_di(dentry)->di_bwh = bindex;
9440+}
9441+
9442+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9443+{
1308ab2a 9444+ DiMustWriteLock(dentry);
1facf9fc 9445+ au_di(dentry)->di_bdiropq = bindex;
9446+}
9447+
9448+/* ---------------------------------------------------------------------- */
9449+
4a4d8108 9450+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9451+static inline void au_digen_dec(struct dentry *d)
9452+{
e49829fe 9453+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9454+}
9455+
4a4d8108 9456+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9457+{
9458+ dentry->d_fsdata = NULL;
9459+}
9460+#else
4a4d8108
AM
9461+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9462+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9463+
9464+#endif /* __KERNEL__ */
9465+#endif /* __AUFS_DENTRY_H__ */
7f207e10
AM
9466diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9467--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
be118d29 9468+++ linux/fs/aufs/dinfo.c 2018-04-06 07:48:44.204604724 +0200
e2f27e51 9469@@ -0,0 +1,553 @@
1facf9fc 9470+/*
a2654f78 9471+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 9472+ *
9473+ * This program, aufs is free software; you can redistribute it and/or modify
9474+ * it under the terms of the GNU General Public License as published by
9475+ * the Free Software Foundation; either version 2 of the License, or
9476+ * (at your option) any later version.
dece6358
AM
9477+ *
9478+ * This program is distributed in the hope that it will be useful,
9479+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9480+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9481+ * GNU General Public License for more details.
9482+ *
9483+ * You should have received a copy of the GNU General Public License
523b37e3 9484+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9485+ */
9486+
9487+/*
9488+ * dentry private data
9489+ */
9490+
9491+#include "aufs.h"
9492+
e49829fe 9493+void au_di_init_once(void *_dinfo)
4a4d8108 9494+{
e49829fe 9495+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9496+
e49829fe 9497+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9498+}
9499+
027c5e7a 9500+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9501+{
9502+ struct au_dinfo *dinfo;
027c5e7a 9503+ int nbr, i;
1facf9fc 9504+
9505+ dinfo = au_cache_alloc_dinfo();
9506+ if (unlikely(!dinfo))
9507+ goto out;
9508+
5afbbe0d 9509+ nbr = au_sbbot(sb) + 1;
1facf9fc 9510+ if (nbr <= 0)
9511+ nbr = 1;
9512+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9513+ if (dinfo->di_hdentry) {
9514+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9515+ dinfo->di_btop = -1;
9516+ dinfo->di_bbot = -1;
027c5e7a
AM
9517+ dinfo->di_bwh = -1;
9518+ dinfo->di_bdiropq = -1;
38d290e6 9519+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9520+ for (i = 0; i < nbr; i++)
9521+ dinfo->di_hdentry[i].hd_id = -1;
9522+ goto out;
9523+ }
1facf9fc 9524+
1c60b727 9525+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9526+ dinfo = NULL;
9527+
4f0767ce 9528+out:
027c5e7a 9529+ return dinfo;
1facf9fc 9530+}
9531+
027c5e7a 9532+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9533+{
4a4d8108 9534+ struct au_hdentry *p;
5afbbe0d 9535+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9536+
9537+ /* dentry may not be revalidated */
5afbbe0d 9538+ bindex = dinfo->di_btop;
4a4d8108 9539+ if (bindex >= 0) {
5afbbe0d
AM
9540+ bbot = dinfo->di_bbot;
9541+ p = au_hdentry(dinfo, bindex);
9542+ while (bindex++ <= bbot)
4a4d8108
AM
9543+ au_hdput(p++);
9544+ }
1c60b727
AM
9545+ kfree(dinfo->di_hdentry);
9546+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9547+}
9548+
9549+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9550+{
9551+ struct au_hdentry *p;
9552+ aufs_bindex_t bi;
9553+
9554+ AuRwMustWriteLock(&a->di_rwsem);
9555+ AuRwMustWriteLock(&b->di_rwsem);
9556+
9557+#define DiSwap(v, name) \
9558+ do { \
9559+ v = a->di_##name; \
9560+ a->di_##name = b->di_##name; \
9561+ b->di_##name = v; \
9562+ } while (0)
9563+
9564+ DiSwap(p, hdentry);
5afbbe0d
AM
9565+ DiSwap(bi, btop);
9566+ DiSwap(bi, bbot);
027c5e7a
AM
9567+ DiSwap(bi, bwh);
9568+ DiSwap(bi, bdiropq);
9569+ /* smp_mb(); */
9570+
9571+#undef DiSwap
9572+}
9573+
9574+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9575+{
9576+ AuRwMustWriteLock(&dst->di_rwsem);
9577+ AuRwMustWriteLock(&src->di_rwsem);
9578+
5afbbe0d
AM
9579+ dst->di_btop = src->di_btop;
9580+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9581+ dst->di_bwh = src->di_bwh;
9582+ dst->di_bdiropq = src->di_bdiropq;
9583+ /* smp_mb(); */
9584+}
9585+
9586+int au_di_init(struct dentry *dentry)
9587+{
9588+ int err;
9589+ struct super_block *sb;
9590+ struct au_dinfo *dinfo;
9591+
9592+ err = 0;
9593+ sb = dentry->d_sb;
9594+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9595+ if (dinfo) {
9596+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9597+ /* smp_mb(); */ /* atomic_set */
9598+ dentry->d_fsdata = dinfo;
9599+ } else
9600+ err = -ENOMEM;
9601+
9602+ return err;
9603+}
9604+
9605+void au_di_fin(struct dentry *dentry)
9606+{
9607+ struct au_dinfo *dinfo;
9608+
9609+ dinfo = au_di(dentry);
9610+ AuRwDestroy(&dinfo->di_rwsem);
9611+ au_di_free(dinfo);
4a4d8108
AM
9612+}
9613+
e2f27e51 9614+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9615+{
9616+ int err, sz;
9617+ struct au_hdentry *hdp;
9618+
1308ab2a 9619+ AuRwMustWriteLock(&dinfo->di_rwsem);
9620+
1facf9fc 9621+ err = -ENOMEM;
5afbbe0d 9622+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9623+ if (!sz)
9624+ sz = sizeof(*hdp);
e2f27e51
AM
9625+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9626+ may_shrink);
1facf9fc 9627+ if (hdp) {
9628+ dinfo->di_hdentry = hdp;
9629+ err = 0;
9630+ }
9631+
9632+ return err;
9633+}
9634+
9635+/* ---------------------------------------------------------------------- */
9636+
9637+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9638+{
9639+ switch (lsc) {
9640+ case AuLsc_DI_CHILD:
9641+ ii_write_lock_child(inode);
9642+ break;
9643+ case AuLsc_DI_CHILD2:
9644+ ii_write_lock_child2(inode);
9645+ break;
9646+ case AuLsc_DI_CHILD3:
9647+ ii_write_lock_child3(inode);
9648+ break;
9649+ case AuLsc_DI_PARENT:
9650+ ii_write_lock_parent(inode);
9651+ break;
9652+ case AuLsc_DI_PARENT2:
9653+ ii_write_lock_parent2(inode);
9654+ break;
9655+ case AuLsc_DI_PARENT3:
9656+ ii_write_lock_parent3(inode);
9657+ break;
9658+ default:
9659+ BUG();
9660+ }
9661+}
9662+
9663+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9664+{
9665+ switch (lsc) {
9666+ case AuLsc_DI_CHILD:
9667+ ii_read_lock_child(inode);
9668+ break;
9669+ case AuLsc_DI_CHILD2:
9670+ ii_read_lock_child2(inode);
9671+ break;
9672+ case AuLsc_DI_CHILD3:
9673+ ii_read_lock_child3(inode);
9674+ break;
9675+ case AuLsc_DI_PARENT:
9676+ ii_read_lock_parent(inode);
9677+ break;
9678+ case AuLsc_DI_PARENT2:
9679+ ii_read_lock_parent2(inode);
9680+ break;
9681+ case AuLsc_DI_PARENT3:
9682+ ii_read_lock_parent3(inode);
9683+ break;
9684+ default:
9685+ BUG();
9686+ }
9687+}
9688+
9689+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9690+{
5527c038
JR
9691+ struct inode *inode;
9692+
dece6358 9693+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9694+ if (d_really_is_positive(d)) {
9695+ inode = d_inode(d);
1facf9fc 9696+ if (au_ftest_lock(flags, IW))
5527c038 9697+ do_ii_write_lock(inode, lsc);
1facf9fc 9698+ else if (au_ftest_lock(flags, IR))
5527c038 9699+ do_ii_read_lock(inode, lsc);
1facf9fc 9700+ }
9701+}
9702+
9703+void di_read_unlock(struct dentry *d, int flags)
9704+{
5527c038
JR
9705+ struct inode *inode;
9706+
9707+ if (d_really_is_positive(d)) {
9708+ inode = d_inode(d);
027c5e7a
AM
9709+ if (au_ftest_lock(flags, IW)) {
9710+ au_dbg_verify_dinode(d);
5527c038 9711+ ii_write_unlock(inode);
027c5e7a
AM
9712+ } else if (au_ftest_lock(flags, IR)) {
9713+ au_dbg_verify_dinode(d);
5527c038 9714+ ii_read_unlock(inode);
027c5e7a 9715+ }
1facf9fc 9716+ }
dece6358 9717+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9718+}
9719+
9720+void di_downgrade_lock(struct dentry *d, int flags)
9721+{
5527c038
JR
9722+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9723+ ii_downgrade_lock(d_inode(d));
dece6358 9724+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9725+}
9726+
9727+void di_write_lock(struct dentry *d, unsigned int lsc)
9728+{
dece6358 9729+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9730+ if (d_really_is_positive(d))
9731+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9732+}
9733+
9734+void di_write_unlock(struct dentry *d)
9735+{
027c5e7a 9736+ au_dbg_verify_dinode(d);
5527c038
JR
9737+ if (d_really_is_positive(d))
9738+ ii_write_unlock(d_inode(d));
dece6358 9739+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9740+}
9741+
9742+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9743+{
9744+ AuDebugOn(d1 == d2
5527c038 9745+ || d_inode(d1) == d_inode(d2)
1facf9fc 9746+ || d1->d_sb != d2->d_sb);
9747+
521ced18
JR
9748+ if ((isdir && au_test_subdir(d1, d2))
9749+ || d1 < d2) {
1facf9fc 9750+ di_write_lock_child(d1);
9751+ di_write_lock_child2(d2);
9752+ } else {
1facf9fc 9753+ di_write_lock_child(d2);
9754+ di_write_lock_child2(d1);
9755+ }
9756+}
9757+
9758+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9759+{
9760+ AuDebugOn(d1 == d2
5527c038 9761+ || d_inode(d1) == d_inode(d2)
1facf9fc 9762+ || d1->d_sb != d2->d_sb);
9763+
521ced18
JR
9764+ if ((isdir && au_test_subdir(d1, d2))
9765+ || d1 < d2) {
1facf9fc 9766+ di_write_lock_parent(d1);
9767+ di_write_lock_parent2(d2);
9768+ } else {
1facf9fc 9769+ di_write_lock_parent(d2);
9770+ di_write_lock_parent2(d1);
9771+ }
9772+}
9773+
9774+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9775+{
9776+ di_write_unlock(d1);
5527c038 9777+ if (d_inode(d1) == d_inode(d2))
dece6358 9778+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9779+ else
9780+ di_write_unlock(d2);
9781+}
9782+
9783+/* ---------------------------------------------------------------------- */
9784+
9785+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9786+{
9787+ struct dentry *d;
9788+
1308ab2a 9789+ DiMustAnyLock(dentry);
9790+
5afbbe0d 9791+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9792+ return NULL;
9793+ AuDebugOn(bindex < 0);
5afbbe0d 9794+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9795+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9796+ return d;
9797+}
9798+
2cbb1c4b
JR
9799+/*
9800+ * extended version of au_h_dptr().
38d290e6
JR
9801+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9802+ * error.
2cbb1c4b
JR
9803+ */
9804+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9805+{
9806+ struct dentry *h_dentry;
9807+ struct inode *inode, *h_inode;
9808+
5527c038 9809+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9810+
9811+ h_dentry = NULL;
5afbbe0d
AM
9812+ if (au_dbtop(dentry) <= bindex
9813+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9814+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9815+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9816+ dget(h_dentry);
9817+ goto out; /* success */
9818+ }
9819+
5527c038 9820+ inode = d_inode(dentry);
5afbbe0d
AM
9821+ AuDebugOn(bindex < au_ibtop(inode));
9822+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9823+ h_inode = au_h_iptr(inode, bindex);
9824+ h_dentry = d_find_alias(h_inode);
9825+ if (h_dentry) {
9826+ if (!IS_ERR(h_dentry)) {
38d290e6 9827+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9828+ goto out; /* success */
9829+ dput(h_dentry);
9830+ } else
9831+ goto out;
9832+ }
9833+
9834+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9835+ h_dentry = au_plink_lkup(inode, bindex);
9836+ AuDebugOn(!h_dentry);
9837+ if (!IS_ERR(h_dentry)) {
9838+ if (!au_d_hashed_positive(h_dentry))
9839+ goto out; /* success */
9840+ dput(h_dentry);
9841+ h_dentry = NULL;
9842+ }
9843+ }
9844+
9845+out:
9846+ AuDbgDentry(h_dentry);
9847+ return h_dentry;
9848+}
9849+
1facf9fc 9850+aufs_bindex_t au_dbtail(struct dentry *dentry)
9851+{
5afbbe0d 9852+ aufs_bindex_t bbot, bwh;
1facf9fc 9853+
5afbbe0d
AM
9854+ bbot = au_dbbot(dentry);
9855+ if (0 <= bbot) {
1facf9fc 9856+ bwh = au_dbwh(dentry);
9857+ if (!bwh)
9858+ return bwh;
5afbbe0d 9859+ if (0 < bwh && bwh < bbot)
1facf9fc 9860+ return bwh - 1;
9861+ }
5afbbe0d 9862+ return bbot;
1facf9fc 9863+}
9864+
9865+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9866+{
5afbbe0d 9867+ aufs_bindex_t bbot, bopq;
1facf9fc 9868+
5afbbe0d
AM
9869+ bbot = au_dbtail(dentry);
9870+ if (0 <= bbot) {
1facf9fc 9871+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9872+ if (0 <= bopq && bopq < bbot)
9873+ bbot = bopq;
1facf9fc 9874+ }
5afbbe0d 9875+ return bbot;
1facf9fc 9876+}
9877+
9878+/* ---------------------------------------------------------------------- */
9879+
9880+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9881+ struct dentry *h_dentry)
9882+{
5afbbe0d
AM
9883+ struct au_dinfo *dinfo;
9884+ struct au_hdentry *hd;
027c5e7a 9885+ struct au_branch *br;
1facf9fc 9886+
1308ab2a 9887+ DiMustWriteLock(dentry);
9888+
5afbbe0d
AM
9889+ dinfo = au_di(dentry);
9890+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9891+ au_hdput(hd);
1facf9fc 9892+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9893+ if (h_dentry) {
9894+ br = au_sbr(dentry->d_sb, bindex);
9895+ hd->hd_id = br->br_id;
9896+ }
9897+}
9898+
9899+int au_dbrange_test(struct dentry *dentry)
9900+{
9901+ int err;
5afbbe0d 9902+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9903+
9904+ err = 0;
5afbbe0d
AM
9905+ btop = au_dbtop(dentry);
9906+ bbot = au_dbbot(dentry);
9907+ if (btop >= 0)
9908+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9909+ else {
9910+ err = -EIO;
5afbbe0d 9911+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9912+ }
9913+
9914+ return err;
9915+}
9916+
9917+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9918+{
9919+ int err;
9920+
9921+ err = 0;
9922+ if (unlikely(au_digen(dentry) != sigen
5527c038 9923+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
9924+ err = -EIO;
9925+
9926+ return err;
1facf9fc 9927+}
9928+
9929+void au_update_digen(struct dentry *dentry)
9930+{
9931+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9932+ /* smp_mb(); */ /* atomic_set */
9933+}
9934+
9935+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9936+{
9937+ struct au_dinfo *dinfo;
9938+ struct dentry *h_d;
4a4d8108 9939+ struct au_hdentry *hdp;
5afbbe0d 9940+ aufs_bindex_t bindex, bbot;
1facf9fc 9941+
1308ab2a 9942+ DiMustWriteLock(dentry);
9943+
1facf9fc 9944+ dinfo = au_di(dentry);
5afbbe0d 9945+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 9946+ return;
9947+
9948+ if (do_put_zero) {
5afbbe0d
AM
9949+ bbot = dinfo->di_bbot;
9950+ bindex = dinfo->di_btop;
9951+ hdp = au_hdentry(dinfo, bindex);
9952+ for (; bindex <= bbot; bindex++, hdp++) {
9953+ h_d = hdp->hd_dentry;
5527c038 9954+ if (h_d && d_is_negative(h_d))
1facf9fc 9955+ au_set_h_dptr(dentry, bindex, NULL);
9956+ }
9957+ }
9958+
5afbbe0d
AM
9959+ dinfo->di_btop = 0;
9960+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9961+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9962+ if (hdp->hd_dentry)
1facf9fc 9963+ break;
5afbbe0d
AM
9964+ if (dinfo->di_btop > dinfo->di_bbot) {
9965+ dinfo->di_btop = -1;
9966+ dinfo->di_bbot = -1;
1facf9fc 9967+ return;
9968+ }
9969+
5afbbe0d
AM
9970+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9971+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9972+ if (hdp->hd_dentry)
1facf9fc 9973+ break;
5afbbe0d 9974+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 9975+}
9976+
5afbbe0d 9977+void au_update_dbtop(struct dentry *dentry)
1facf9fc 9978+{
5afbbe0d 9979+ aufs_bindex_t bindex, bbot;
1facf9fc 9980+ struct dentry *h_dentry;
9981+
5afbbe0d
AM
9982+ bbot = au_dbbot(dentry);
9983+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 9984+ h_dentry = au_h_dptr(dentry, bindex);
9985+ if (!h_dentry)
9986+ continue;
5527c038 9987+ if (d_is_positive(h_dentry)) {
5afbbe0d 9988+ au_set_dbtop(dentry, bindex);
1facf9fc 9989+ return;
9990+ }
9991+ au_set_h_dptr(dentry, bindex, NULL);
9992+ }
9993+}
9994+
5afbbe0d 9995+void au_update_dbbot(struct dentry *dentry)
1facf9fc 9996+{
5afbbe0d 9997+ aufs_bindex_t bindex, btop;
1facf9fc 9998+ struct dentry *h_dentry;
9999+
5afbbe0d
AM
10000+ btop = au_dbtop(dentry);
10001+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 10002+ h_dentry = au_h_dptr(dentry, bindex);
10003+ if (!h_dentry)
10004+ continue;
5527c038 10005+ if (d_is_positive(h_dentry)) {
5afbbe0d 10006+ au_set_dbbot(dentry, bindex);
1facf9fc 10007+ return;
10008+ }
10009+ au_set_h_dptr(dentry, bindex, NULL);
10010+ }
10011+}
10012+
10013+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10014+{
5afbbe0d 10015+ aufs_bindex_t bindex, bbot;
1facf9fc 10016+
5afbbe0d
AM
10017+ bbot = au_dbbot(dentry);
10018+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 10019+ if (au_h_dptr(dentry, bindex) == h_dentry)
10020+ return bindex;
10021+ return -1;
10022+}
7f207e10
AM
10023diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10024--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
be118d29 10025+++ linux/fs/aufs/dir.c 2018-04-06 07:48:44.204604724 +0200
1c60b727 10026@@ -0,0 +1,759 @@
1facf9fc 10027+/*
a2654f78 10028+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 10029+ *
10030+ * This program, aufs is free software; you can redistribute it and/or modify
10031+ * it under the terms of the GNU General Public License as published by
10032+ * the Free Software Foundation; either version 2 of the License, or
10033+ * (at your option) any later version.
dece6358
AM
10034+ *
10035+ * This program is distributed in the hope that it will be useful,
10036+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10037+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10038+ * GNU General Public License for more details.
10039+ *
10040+ * You should have received a copy of the GNU General Public License
523b37e3 10041+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10042+ */
10043+
10044+/*
10045+ * directory operations
10046+ */
10047+
10048+#include <linux/fs_stack.h>
10049+#include "aufs.h"
10050+
10051+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10052+{
9dbd164d
AM
10053+ unsigned int nlink;
10054+
1facf9fc 10055+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10056+
9dbd164d
AM
10057+ nlink = dir->i_nlink;
10058+ nlink += h_dir->i_nlink - 2;
1facf9fc 10059+ if (h_dir->i_nlink < 2)
9dbd164d 10060+ nlink += 2;
f6b6e03d 10061+ smp_mb(); /* for i_nlink */
7eafdf33 10062+ /* 0 can happen in revaliding */
92d182d2 10063+ set_nlink(dir, nlink);
1facf9fc 10064+}
10065+
10066+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10067+{
9dbd164d
AM
10068+ unsigned int nlink;
10069+
1facf9fc 10070+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10071+
9dbd164d
AM
10072+ nlink = dir->i_nlink;
10073+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10074+ if (h_dir->i_nlink < 2)
9dbd164d 10075+ nlink -= 2;
f6b6e03d 10076+ smp_mb(); /* for i_nlink */
92d182d2 10077+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10078+ set_nlink(dir, nlink);
1facf9fc 10079+}
10080+
1308ab2a 10081+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10082+{
10083+ loff_t sz;
5afbbe0d 10084+ aufs_bindex_t bindex, bbot;
1308ab2a 10085+ struct file *h_file;
10086+ struct dentry *h_dentry;
10087+
10088+ sz = 0;
10089+ if (file) {
2000de60 10090+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10091+
5afbbe0d
AM
10092+ bbot = au_fbbot_dir(file);
10093+ for (bindex = au_fbtop(file);
10094+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10095+ bindex++) {
4a4d8108 10096+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10097+ if (h_file && file_inode(h_file))
10098+ sz += vfsub_f_size_read(h_file);
1308ab2a 10099+ }
10100+ } else {
10101+ AuDebugOn(!dentry);
2000de60 10102+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10103+
5afbbe0d
AM
10104+ bbot = au_dbtaildir(dentry);
10105+ for (bindex = au_dbtop(dentry);
10106+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10107+ bindex++) {
10108+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10109+ if (h_dentry && d_is_positive(h_dentry))
10110+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10111+ }
10112+ }
10113+ if (sz < KMALLOC_MAX_SIZE)
10114+ sz = roundup_pow_of_two(sz);
10115+ if (sz > KMALLOC_MAX_SIZE)
10116+ sz = KMALLOC_MAX_SIZE;
10117+ else if (sz < NAME_MAX) {
10118+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10119+ sz = AUFS_RDBLK_DEF;
10120+ }
10121+ return sz;
10122+}
10123+
b912730e
AM
10124+struct au_dir_ts_arg {
10125+ struct dentry *dentry;
10126+ aufs_bindex_t brid;
10127+};
10128+
10129+static void au_do_dir_ts(void *arg)
10130+{
10131+ struct au_dir_ts_arg *a = arg;
10132+ struct au_dtime dt;
10133+ struct path h_path;
10134+ struct inode *dir, *h_dir;
10135+ struct super_block *sb;
10136+ struct au_branch *br;
10137+ struct au_hinode *hdir;
10138+ int err;
5afbbe0d 10139+ aufs_bindex_t btop, bindex;
b912730e
AM
10140+
10141+ sb = a->dentry->d_sb;
5527c038 10142+ if (d_really_is_negative(a->dentry))
b912730e 10143+ goto out;
5527c038 10144+ /* no dir->i_mutex lock */
b95c5147
AM
10145+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10146+
5527c038 10147+ dir = d_inode(a->dentry);
5afbbe0d 10148+ btop = au_ibtop(dir);
b912730e 10149+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10150+ if (bindex < btop)
b912730e
AM
10151+ goto out_unlock;
10152+
10153+ br = au_sbr(sb, bindex);
10154+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10155+ if (!h_path.dentry)
10156+ goto out_unlock;
10157+ h_path.mnt = au_br_mnt(br);
10158+ au_dtime_store(&dt, a->dentry, &h_path);
10159+
5afbbe0d 10160+ br = au_sbr(sb, btop);
b912730e
AM
10161+ if (!au_br_writable(br->br_perm))
10162+ goto out_unlock;
5afbbe0d 10163+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10164+ h_path.mnt = au_br_mnt(br);
10165+ err = vfsub_mnt_want_write(h_path.mnt);
10166+ if (err)
10167+ goto out_unlock;
5afbbe0d
AM
10168+ hdir = au_hi(dir, btop);
10169+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10170+ h_dir = au_h_iptr(dir, btop);
b912730e
AM
10171+ if (h_dir->i_nlink
10172+ && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10173+ dt.dt_h_path = h_path;
10174+ au_dtime_revert(&dt);
10175+ }
5afbbe0d 10176+ au_hn_inode_unlock(hdir);
b912730e
AM
10177+ vfsub_mnt_drop_write(h_path.mnt);
10178+ au_cpup_attr_timesizes(dir);
10179+
10180+out_unlock:
10181+ aufs_read_unlock(a->dentry, AuLock_DW);
10182+out:
10183+ dput(a->dentry);
10184+ au_nwt_done(&au_sbi(sb)->si_nowait);
1c60b727 10185+ kfree(arg);
b912730e
AM
10186+}
10187+
10188+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10189+{
10190+ int perm, wkq_err;
5afbbe0d 10191+ aufs_bindex_t btop;
b912730e
AM
10192+ struct au_dir_ts_arg *arg;
10193+ struct dentry *dentry;
10194+ struct super_block *sb;
10195+
10196+ IMustLock(dir);
10197+
10198+ dentry = d_find_any_alias(dir);
10199+ AuDebugOn(!dentry);
10200+ sb = dentry->d_sb;
5afbbe0d
AM
10201+ btop = au_ibtop(dir);
10202+ if (btop == bindex) {
b912730e
AM
10203+ au_cpup_attr_timesizes(dir);
10204+ goto out;
10205+ }
10206+
5afbbe0d 10207+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10208+ if (!au_br_writable(perm))
10209+ goto out;
10210+
10211+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10212+ if (!arg)
10213+ goto out;
10214+
10215+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10216+ arg->brid = au_sbr_id(sb, bindex);
10217+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10218+ if (unlikely(wkq_err)) {
10219+ pr_err("wkq %d\n", wkq_err);
10220+ dput(dentry);
1c60b727 10221+ kfree(arg);
b912730e
AM
10222+ }
10223+
10224+out:
10225+ dput(dentry);
10226+}
10227+
1facf9fc 10228+/* ---------------------------------------------------------------------- */
10229+
10230+static int reopen_dir(struct file *file)
10231+{
10232+ int err;
10233+ unsigned int flags;
5afbbe0d 10234+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10235+ struct dentry *dentry, *h_dentry;
10236+ struct file *h_file;
10237+
10238+ /* open all lower dirs */
2000de60 10239+ dentry = file->f_path.dentry;
5afbbe0d
AM
10240+ btop = au_dbtop(dentry);
10241+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10242+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10243+ au_set_fbtop(file, btop);
1facf9fc 10244+
10245+ btail = au_dbtaildir(dentry);
5afbbe0d 10246+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10247+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10248+ au_set_fbbot_dir(file, btail);
1facf9fc 10249+
4a4d8108 10250+ flags = vfsub_file_flags(file);
5afbbe0d 10251+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10252+ h_dentry = au_h_dptr(dentry, bindex);
10253+ if (!h_dentry)
10254+ continue;
4a4d8108 10255+ h_file = au_hf_dir(file, bindex);
1facf9fc 10256+ if (h_file)
10257+ continue;
10258+
392086de 10259+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10260+ err = PTR_ERR(h_file);
10261+ if (IS_ERR(h_file))
10262+ goto out; /* close all? */
10263+ au_set_h_fptr(file, bindex, h_file);
10264+ }
10265+ au_update_figen(file);
10266+ /* todo: necessary? */
10267+ /* file->f_ra = h_file->f_ra; */
10268+ err = 0;
10269+
4f0767ce 10270+out:
1facf9fc 10271+ return err;
10272+}
10273+
b912730e 10274+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10275+{
10276+ int err;
10277+ aufs_bindex_t bindex, btail;
10278+ struct dentry *dentry, *h_dentry;
8cdd5066 10279+ struct vfsmount *mnt;
1facf9fc 10280+
1308ab2a 10281+ FiMustWriteLock(file);
b912730e 10282+ AuDebugOn(h_file);
1308ab2a 10283+
523b37e3 10284+ err = 0;
8cdd5066 10285+ mnt = file->f_path.mnt;
2000de60 10286+ dentry = file->f_path.dentry;
be118d29 10287+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10288+ bindex = au_dbtop(dentry);
10289+ au_set_fbtop(file, bindex);
1facf9fc 10290+ btail = au_dbtaildir(dentry);
5afbbe0d 10291+ au_set_fbbot_dir(file, btail);
1facf9fc 10292+ for (; !err && bindex <= btail; bindex++) {
10293+ h_dentry = au_h_dptr(dentry, bindex);
10294+ if (!h_dentry)
10295+ continue;
10296+
8cdd5066
JR
10297+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10298+ if (unlikely(err))
10299+ break;
392086de 10300+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10301+ if (IS_ERR(h_file)) {
10302+ err = PTR_ERR(h_file);
10303+ break;
10304+ }
10305+ au_set_h_fptr(file, bindex, h_file);
10306+ }
10307+ au_update_figen(file);
10308+ /* todo: necessary? */
10309+ /* file->f_ra = h_file->f_ra; */
10310+ if (!err)
10311+ return 0; /* success */
10312+
10313+ /* close all */
5afbbe0d 10314+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10315+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10316+ au_set_fbtop(file, -1);
10317+ au_set_fbbot_dir(file, -1);
4a4d8108 10318+
1facf9fc 10319+ return err;
10320+}
10321+
10322+static int aufs_open_dir(struct inode *inode __maybe_unused,
10323+ struct file *file)
10324+{
4a4d8108
AM
10325+ int err;
10326+ struct super_block *sb;
10327+ struct au_fidir *fidir;
10328+
10329+ err = -ENOMEM;
2000de60 10330+ sb = file->f_path.dentry->d_sb;
4a4d8108 10331+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10332+ fidir = au_fidir_alloc(sb);
4a4d8108 10333+ if (fidir) {
b912730e
AM
10334+ struct au_do_open_args args = {
10335+ .open = do_open_dir,
10336+ .fidir = fidir
10337+ };
10338+ err = au_do_open(file, &args);
4a4d8108 10339+ if (unlikely(err))
1c60b727 10340+ kfree(fidir);
4a4d8108
AM
10341+ }
10342+ si_read_unlock(sb);
10343+ return err;
1facf9fc 10344+}
10345+
10346+static int aufs_release_dir(struct inode *inode __maybe_unused,
10347+ struct file *file)
10348+{
10349+ struct au_vdir *vdir_cache;
4a4d8108
AM
10350+ struct au_finfo *finfo;
10351+ struct au_fidir *fidir;
f0c0a007 10352+ struct au_hfile *hf;
5afbbe0d 10353+ aufs_bindex_t bindex, bbot;
1facf9fc 10354+
4a4d8108
AM
10355+ finfo = au_fi(file);
10356+ fidir = finfo->fi_hdir;
10357+ if (fidir) {
8b6a4947
AM
10358+ au_hbl_del(&finfo->fi_hlist,
10359+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10360+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10361+ if (vdir_cache)
1c60b727 10362+ au_vdir_free(vdir_cache);
4a4d8108
AM
10363+
10364+ bindex = finfo->fi_btop;
10365+ if (bindex >= 0) {
f0c0a007 10366+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10367+ /*
10368+ * calls fput() instead of filp_close(),
10369+ * since no dnotify or lock for the lower file.
10370+ */
5afbbe0d 10371+ bbot = fidir->fd_bbot;
f0c0a007
AM
10372+ for (; bindex <= bbot; bindex++, hf++)
10373+ if (hf->hf_file)
1c60b727 10374+ au_hfput(hf, /*execed*/0);
4a4d8108 10375+ }
1c60b727 10376+ kfree(fidir);
4a4d8108 10377+ finfo->fi_hdir = NULL;
1facf9fc 10378+ }
1c60b727 10379+ au_finfo_fin(file);
1facf9fc 10380+ return 0;
10381+}
10382+
10383+/* ---------------------------------------------------------------------- */
10384+
4a4d8108
AM
10385+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10386+{
10387+ int err;
5afbbe0d 10388+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10389+ struct file *h_file;
10390+
10391+ err = 0;
5afbbe0d
AM
10392+ bbot = au_fbbot_dir(file);
10393+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10394+ h_file = au_hf_dir(file, bindex);
10395+ if (h_file)
10396+ err = vfsub_flush(h_file, id);
10397+ }
10398+ return err;
10399+}
10400+
10401+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10402+{
10403+ return au_do_flush(file, id, au_do_flush_dir);
10404+}
10405+
10406+/* ---------------------------------------------------------------------- */
10407+
1facf9fc 10408+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10409+{
10410+ int err;
5afbbe0d 10411+ aufs_bindex_t bbot, bindex;
1facf9fc 10412+ struct inode *inode;
10413+ struct super_block *sb;
10414+
10415+ err = 0;
10416+ sb = dentry->d_sb;
5527c038 10417+ inode = d_inode(dentry);
1facf9fc 10418+ IMustLock(inode);
5afbbe0d
AM
10419+ bbot = au_dbbot(dentry);
10420+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10421+ struct path h_path;
1facf9fc 10422+
10423+ if (au_test_ro(sb, bindex, inode))
10424+ continue;
10425+ h_path.dentry = au_h_dptr(dentry, bindex);
10426+ if (!h_path.dentry)
10427+ continue;
1facf9fc 10428+
1facf9fc 10429+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10430+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10431+ }
10432+
10433+ return err;
10434+}
10435+
10436+static int au_do_fsync_dir(struct file *file, int datasync)
10437+{
10438+ int err;
5afbbe0d 10439+ aufs_bindex_t bbot, bindex;
1facf9fc 10440+ struct file *h_file;
10441+ struct super_block *sb;
10442+ struct inode *inode;
1facf9fc 10443+
521ced18 10444+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10445+ if (unlikely(err))
10446+ goto out;
10447+
c06a8ce3 10448+ inode = file_inode(file);
b912730e 10449+ sb = inode->i_sb;
5afbbe0d
AM
10450+ bbot = au_fbbot_dir(file);
10451+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10452+ h_file = au_hf_dir(file, bindex);
1facf9fc 10453+ if (!h_file || au_test_ro(sb, bindex, inode))
10454+ continue;
10455+
53392da6 10456+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10457+ }
10458+
4f0767ce 10459+out:
1facf9fc 10460+ return err;
10461+}
10462+
10463+/*
10464+ * @file may be NULL
10465+ */
1e00d052
AM
10466+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10467+ int datasync)
1facf9fc 10468+{
10469+ int err;
b752ccd1 10470+ struct dentry *dentry;
5527c038 10471+ struct inode *inode;
1facf9fc 10472+ struct super_block *sb;
1facf9fc 10473+
10474+ err = 0;
2000de60 10475+ dentry = file->f_path.dentry;
5527c038 10476+ inode = d_inode(dentry);
febd17d6 10477+ inode_lock(inode);
1facf9fc 10478+ sb = dentry->d_sb;
10479+ si_noflush_read_lock(sb);
10480+ if (file)
10481+ err = au_do_fsync_dir(file, datasync);
10482+ else {
10483+ di_write_lock_child(dentry);
10484+ err = au_do_fsync_dir_no_file(dentry, datasync);
10485+ }
5527c038 10486+ au_cpup_attr_timesizes(inode);
1facf9fc 10487+ di_write_unlock(dentry);
10488+ if (file)
10489+ fi_write_unlock(file);
10490+
10491+ si_read_unlock(sb);
febd17d6 10492+ inode_unlock(inode);
1facf9fc 10493+ return err;
10494+}
10495+
10496+/* ---------------------------------------------------------------------- */
10497+
5afbbe0d 10498+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10499+{
10500+ int err;
10501+ struct dentry *dentry;
9dbd164d 10502+ struct inode *inode, *h_inode;
1facf9fc 10503+ struct super_block *sb;
10504+
523b37e3 10505+ AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10506+
2000de60 10507+ dentry = file->f_path.dentry;
5527c038 10508+ inode = d_inode(dentry);
1facf9fc 10509+ IMustLock(inode);
10510+
10511+ sb = dentry->d_sb;
10512+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10513+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10514+ if (unlikely(err))
10515+ goto out;
027c5e7a
AM
10516+ err = au_alive_dir(dentry);
10517+ if (!err)
10518+ err = au_vdir_init(file);
1facf9fc 10519+ di_downgrade_lock(dentry, AuLock_IR);
10520+ if (unlikely(err))
10521+ goto out_unlock;
10522+
5afbbe0d 10523+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10524+ if (!au_test_nfsd()) {
392086de 10525+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10526+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10527+ } else {
10528+ /*
10529+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10530+ * encode_fh() and others.
10531+ */
9dbd164d 10532+ atomic_inc(&h_inode->i_count);
1facf9fc 10533+ di_read_unlock(dentry, AuLock_IR);
10534+ si_read_unlock(sb);
392086de 10535+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10536+ fsstack_copy_attr_atime(inode, h_inode);
10537+ fi_write_unlock(file);
9dbd164d 10538+ iput(h_inode);
1facf9fc 10539+
10540+ AuTraceErr(err);
10541+ return err;
10542+ }
10543+
4f0767ce 10544+out_unlock:
1facf9fc 10545+ di_read_unlock(dentry, AuLock_IR);
10546+ fi_write_unlock(file);
4f0767ce 10547+out:
1facf9fc 10548+ si_read_unlock(sb);
10549+ return err;
10550+}
10551+
10552+/* ---------------------------------------------------------------------- */
10553+
10554+#define AuTestEmpty_WHONLY 1
dece6358
AM
10555+#define AuTestEmpty_CALLED (1 << 1)
10556+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10557+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10558+#define au_fset_testempty(flags, name) \
10559+ do { (flags) |= AuTestEmpty_##name; } while (0)
10560+#define au_fclr_testempty(flags, name) \
10561+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10562+
dece6358
AM
10563+#ifndef CONFIG_AUFS_SHWH
10564+#undef AuTestEmpty_SHWH
10565+#define AuTestEmpty_SHWH 0
10566+#endif
10567+
1facf9fc 10568+struct test_empty_arg {
392086de 10569+ struct dir_context ctx;
1308ab2a 10570+ struct au_nhash *whlist;
1facf9fc 10571+ unsigned int flags;
10572+ int err;
10573+ aufs_bindex_t bindex;
10574+};
10575+
392086de
AM
10576+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10577+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10578+ unsigned int d_type)
1facf9fc 10579+{
392086de
AM
10580+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10581+ ctx);
1facf9fc 10582+ char *name = (void *)__name;
10583+
10584+ arg->err = 0;
10585+ au_fset_testempty(arg->flags, CALLED);
10586+ /* smp_mb(); */
10587+ if (name[0] == '.'
10588+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10589+ goto out; /* success */
10590+
10591+ if (namelen <= AUFS_WH_PFX_LEN
10592+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10593+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10594+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10595+ arg->err = -ENOTEMPTY;
10596+ goto out;
10597+ }
10598+
10599+ name += AUFS_WH_PFX_LEN;
10600+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10601+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10602+ arg->err = au_nhash_append_wh
1308ab2a 10603+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10604+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10605+
4f0767ce 10606+out:
1facf9fc 10607+ /* smp_mb(); */
10608+ AuTraceErr(arg->err);
10609+ return arg->err;
10610+}
10611+
10612+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10613+{
10614+ int err;
10615+ struct file *h_file;
10616+
10617+ h_file = au_h_open(dentry, arg->bindex,
10618+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10619+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10620+ err = PTR_ERR(h_file);
10621+ if (IS_ERR(h_file))
10622+ goto out;
10623+
10624+ err = 0;
10625+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10626+ && !file_inode(h_file)->i_nlink)
1facf9fc 10627+ goto out_put;
10628+
10629+ do {
10630+ arg->err = 0;
10631+ au_fclr_testempty(arg->flags, CALLED);
10632+ /* smp_mb(); */
392086de 10633+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10634+ if (err >= 0)
10635+ err = arg->err;
10636+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10637+
4f0767ce 10638+out_put:
1facf9fc 10639+ fput(h_file);
10640+ au_sbr_put(dentry->d_sb, arg->bindex);
4f0767ce 10641+out:
1facf9fc 10642+ return err;
10643+}
10644+
10645+struct do_test_empty_args {
10646+ int *errp;
10647+ struct dentry *dentry;
10648+ struct test_empty_arg *arg;
10649+};
10650+
10651+static void call_do_test_empty(void *args)
10652+{
10653+ struct do_test_empty_args *a = args;
10654+ *a->errp = do_test_empty(a->dentry, a->arg);
10655+}
10656+
10657+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10658+{
10659+ int err, wkq_err;
10660+ struct dentry *h_dentry;
10661+ struct inode *h_inode;
10662+
10663+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10664+ h_inode = d_inode(h_dentry);
53392da6 10665+ /* todo: i_mode changes anytime? */
be118d29 10666+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10667+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10668+ inode_unlock_shared(h_inode);
1facf9fc 10669+ if (!err)
10670+ err = do_test_empty(dentry, arg);
10671+ else {
10672+ struct do_test_empty_args args = {
10673+ .errp = &err,
10674+ .dentry = dentry,
10675+ .arg = arg
10676+ };
10677+ unsigned int flags = arg->flags;
10678+
10679+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10680+ if (unlikely(wkq_err))
10681+ err = wkq_err;
10682+ arg->flags = flags;
10683+ }
10684+
10685+ return err;
10686+}
10687+
10688+int au_test_empty_lower(struct dentry *dentry)
10689+{
10690+ int err;
1308ab2a 10691+ unsigned int rdhash;
5afbbe0d 10692+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10693+ struct au_nhash whlist;
392086de
AM
10694+ struct test_empty_arg arg = {
10695+ .ctx = {
2000de60 10696+ .actor = test_empty_cb
392086de
AM
10697+ }
10698+ };
076b876e 10699+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10700+
dece6358
AM
10701+ SiMustAnyLock(dentry->d_sb);
10702+
1308ab2a 10703+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10704+ if (!rdhash)
10705+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10706+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10707+ if (unlikely(err))
1facf9fc 10708+ goto out;
10709+
1facf9fc 10710+ arg.flags = 0;
1308ab2a 10711+ arg.whlist = &whlist;
5afbbe0d 10712+ btop = au_dbtop(dentry);
dece6358
AM
10713+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10714+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10715+ test_empty = do_test_empty;
10716+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10717+ test_empty = sio_test_empty;
5afbbe0d 10718+ arg.bindex = btop;
076b876e 10719+ err = test_empty(dentry, &arg);
1facf9fc 10720+ if (unlikely(err))
10721+ goto out_whlist;
10722+
10723+ au_fset_testempty(arg.flags, WHONLY);
10724+ btail = au_dbtaildir(dentry);
5afbbe0d 10725+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10726+ struct dentry *h_dentry;
10727+
10728+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10729+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10730+ arg.bindex = bindex;
076b876e 10731+ err = test_empty(dentry, &arg);
1facf9fc 10732+ }
10733+ }
10734+
4f0767ce 10735+out_whlist:
1308ab2a 10736+ au_nhash_wh_free(&whlist);
4f0767ce 10737+out:
1facf9fc 10738+ return err;
10739+}
10740+
10741+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10742+{
10743+ int err;
392086de
AM
10744+ struct test_empty_arg arg = {
10745+ .ctx = {
2000de60 10746+ .actor = test_empty_cb
392086de
AM
10747+ }
10748+ };
1facf9fc 10749+ aufs_bindex_t bindex, btail;
10750+
10751+ err = 0;
1308ab2a 10752+ arg.whlist = whlist;
1facf9fc 10753+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10754+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10755+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10756+ btail = au_dbtaildir(dentry);
5afbbe0d 10757+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10758+ struct dentry *h_dentry;
10759+
10760+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10761+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10762+ arg.bindex = bindex;
10763+ err = sio_test_empty(dentry, &arg);
10764+ }
10765+ }
10766+
10767+ return err;
10768+}
10769+
10770+/* ---------------------------------------------------------------------- */
10771+
10772+const struct file_operations aufs_dir_fop = {
4a4d8108 10773+ .owner = THIS_MODULE,
027c5e7a 10774+ .llseek = default_llseek,
1facf9fc 10775+ .read = generic_read_dir,
5afbbe0d 10776+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10777+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10778+#ifdef CONFIG_COMPAT
10779+ .compat_ioctl = aufs_compat_ioctl_dir,
10780+#endif
1facf9fc 10781+ .open = aufs_open_dir,
10782+ .release = aufs_release_dir,
4a4d8108 10783+ .flush = aufs_flush_dir,
1facf9fc 10784+ .fsync = aufs_fsync_dir
10785+};
7f207e10
AM
10786diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10787--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
be118d29 10788+++ linux/fs/aufs/dir.h 2018-04-06 07:48:44.204604724 +0200
1c60b727 10789@@ -0,0 +1,131 @@
1facf9fc 10790+/*
a2654f78 10791+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 10792+ *
10793+ * This program, aufs is free software; you can redistribute it and/or modify
10794+ * it under the terms of the GNU General Public License as published by
10795+ * the Free Software Foundation; either version 2 of the License, or
10796+ * (at your option) any later version.
dece6358
AM
10797+ *
10798+ * This program is distributed in the hope that it will be useful,
10799+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10800+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10801+ * GNU General Public License for more details.
10802+ *
10803+ * You should have received a copy of the GNU General Public License
523b37e3 10804+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10805+ */
10806+
10807+/*
10808+ * directory operations
10809+ */
10810+
10811+#ifndef __AUFS_DIR_H__
10812+#define __AUFS_DIR_H__
10813+
10814+#ifdef __KERNEL__
10815+
10816+#include <linux/fs.h>
1facf9fc 10817+
10818+/* ---------------------------------------------------------------------- */
10819+
10820+/* need to be faster and smaller */
10821+
10822+struct au_nhash {
dece6358
AM
10823+ unsigned int nh_num;
10824+ struct hlist_head *nh_head;
1facf9fc 10825+};
10826+
10827+struct au_vdir_destr {
10828+ unsigned char len;
10829+ unsigned char name[0];
10830+} __packed;
10831+
10832+struct au_vdir_dehstr {
10833+ struct hlist_node hash;
1c60b727 10834+ struct au_vdir_destr *str;
4a4d8108 10835+} ____cacheline_aligned_in_smp;
1facf9fc 10836+
10837+struct au_vdir_de {
10838+ ino_t de_ino;
10839+ unsigned char de_type;
10840+ /* caution: packed */
10841+ struct au_vdir_destr de_str;
10842+} __packed;
10843+
10844+struct au_vdir_wh {
10845+ struct hlist_node wh_hash;
dece6358
AM
10846+#ifdef CONFIG_AUFS_SHWH
10847+ ino_t wh_ino;
1facf9fc 10848+ aufs_bindex_t wh_bindex;
dece6358
AM
10849+ unsigned char wh_type;
10850+#else
10851+ aufs_bindex_t wh_bindex;
10852+#endif
10853+ /* caution: packed */
1facf9fc 10854+ struct au_vdir_destr wh_str;
10855+} __packed;
10856+
10857+union au_vdir_deblk_p {
10858+ unsigned char *deblk;
10859+ struct au_vdir_de *de;
10860+};
10861+
10862+struct au_vdir {
10863+ unsigned char **vd_deblk;
10864+ unsigned long vd_nblk;
1facf9fc 10865+ struct {
10866+ unsigned long ul;
10867+ union au_vdir_deblk_p p;
10868+ } vd_last;
10869+
be118d29 10870+ u64 vd_version;
dece6358 10871+ unsigned int vd_deblk_sz;
1c60b727 10872+ unsigned long vd_jiffy;
4a4d8108 10873+} ____cacheline_aligned_in_smp;
1facf9fc 10874+
10875+/* ---------------------------------------------------------------------- */
10876+
10877+/* dir.c */
10878+extern const struct file_operations aufs_dir_fop;
10879+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10880+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10881+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10882+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10883+int au_test_empty_lower(struct dentry *dentry);
10884+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10885+
10886+/* vdir.c */
1308ab2a 10887+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10888+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10889+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10890+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10891+ int limit);
dece6358
AM
10892+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10893+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10894+ unsigned int d_type, aufs_bindex_t bindex,
10895+ unsigned char shwh);
1c60b727 10896+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 10897+int au_vdir_init(struct file *file);
392086de 10898+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10899+
10900+/* ioctl.c */
10901+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10902+
1308ab2a 10903+#ifdef CONFIG_AUFS_RDU
10904+/* rdu.c */
10905+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10906+#ifdef CONFIG_COMPAT
10907+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10908+ unsigned long arg);
10909+#endif
1308ab2a 10910+#else
c1595e42
JR
10911+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10912+ unsigned int cmd, unsigned long arg)
b752ccd1 10913+#ifdef CONFIG_COMPAT
c1595e42
JR
10914+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10915+ unsigned int cmd, unsigned long arg)
b752ccd1 10916+#endif
1308ab2a 10917+#endif
10918+
1facf9fc 10919+#endif /* __KERNEL__ */
10920+#endif /* __AUFS_DIR_H__ */
8b6a4947
AM
10921diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10922--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
be118d29 10923+++ linux/fs/aufs/dirren.c 2018-04-06 07:48:44.204604724 +0200
8b6a4947
AM
10924@@ -0,0 +1,1315 @@
10925+/*
10926+ * Copyright (C) 2017 Junjiro R. Okajima
10927+ *
10928+ * This program, aufs is free software; you can redistribute it and/or modify
10929+ * it under the terms of the GNU General Public License as published by
10930+ * the Free Software Foundation; either version 2 of the License, or
10931+ * (at your option) any later version.
10932+ *
10933+ * This program is distributed in the hope that it will be useful,
10934+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10935+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10936+ * GNU General Public License for more details.
10937+ *
10938+ * You should have received a copy of the GNU General Public License
10939+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10940+ */
10941+
10942+/*
10943+ * special handling in renaming a directoy
10944+ * in order to support looking-up the before-renamed name on the lower readonly
10945+ * branches
10946+ */
10947+
10948+#include <linux/byteorder/generic.h>
10949+#include "aufs.h"
10950+
10951+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10952+{
10953+ int idx;
10954+
10955+ idx = au_dr_ihash(ent->dr_h_ino);
10956+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10957+}
10958+
10959+static int au_dr_hino_test_empty(struct au_dr_br *dr)
10960+{
10961+ int ret, i;
10962+ struct hlist_bl_head *hbl;
10963+
10964+ ret = 1;
10965+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
10966+ hbl = dr->dr_h_ino + i;
10967+ hlist_bl_lock(hbl);
10968+ ret &= hlist_bl_empty(hbl);
10969+ hlist_bl_unlock(hbl);
10970+ }
10971+
10972+ return ret;
10973+}
10974+
10975+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10976+{
10977+ struct au_dr_hino *found, *ent;
10978+ struct hlist_bl_head *hbl;
10979+ struct hlist_bl_node *pos;
10980+ int idx;
10981+
10982+ found = NULL;
10983+ idx = au_dr_ihash(ino);
10984+ hbl = dr->dr_h_ino + idx;
10985+ hlist_bl_lock(hbl);
10986+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10987+ if (ent->dr_h_ino == ino) {
10988+ found = ent;
10989+ break;
10990+ }
10991+ hlist_bl_unlock(hbl);
10992+
10993+ return found;
10994+}
10995+
10996+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10997+ struct au_dr_hino *add_ent)
10998+{
10999+ int found, idx;
11000+ struct hlist_bl_head *hbl;
11001+ struct hlist_bl_node *pos;
11002+ struct au_dr_hino *ent;
11003+
11004+ found = 0;
11005+ idx = au_dr_ihash(ino);
11006+ hbl = dr->dr_h_ino + idx;
11007+#if 0
11008+ {
11009+ struct hlist_bl_node *tmp;
11010+
11011+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11012+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11013+ }
11014+#endif
11015+ hlist_bl_lock(hbl);
11016+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11017+ if (ent->dr_h_ino == ino) {
11018+ found = 1;
11019+ break;
11020+ }
11021+ if (!found && add_ent)
11022+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11023+ hlist_bl_unlock(hbl);
11024+
11025+ if (!found && add_ent)
11026+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11027+
11028+ return found;
11029+}
11030+
11031+void au_dr_hino_free(struct au_dr_br *dr)
11032+{
11033+ int i;
11034+ struct hlist_bl_head *hbl;
11035+ struct hlist_bl_node *pos, *tmp;
11036+ struct au_dr_hino *ent;
11037+
11038+ /* SiMustWriteLock(sb); */
11039+
11040+ for (i = 0; i < AuDirren_NHASH; i++) {
11041+ hbl = dr->dr_h_ino + i;
11042+ /* no spinlock since sbinfo must be write-locked */
11043+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11044+ kfree(ent);
11045+ INIT_HLIST_BL_HEAD(hbl);
11046+ }
11047+}
11048+
11049+/* returns the number of inodes or an error */
11050+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11051+ struct file *hinofile)
11052+{
11053+ int err, i;
11054+ ssize_t ssz;
11055+ loff_t pos, oldsize;
11056+ __be64 u64;
11057+ struct inode *hinoinode;
11058+ struct hlist_bl_head *hbl;
11059+ struct hlist_bl_node *n1, *n2;
11060+ struct au_dr_hino *ent;
11061+
11062+ SiMustWriteLock(sb);
11063+ AuDebugOn(!au_br_writable(br->br_perm));
11064+
11065+ hinoinode = file_inode(hinofile);
11066+ oldsize = i_size_read(hinoinode);
11067+
11068+ err = 0;
11069+ pos = 0;
11070+ hbl = br->br_dirren.dr_h_ino;
11071+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11072+ /* no bit-lock since sbinfo must be write-locked */
11073+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11074+ AuDbg("hi%llu, %pD2\n",
11075+ (unsigned long long)ent->dr_h_ino, hinofile);
11076+ u64 = cpu_to_be64(ent->dr_h_ino);
11077+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11078+ if (ssz == sizeof(u64))
11079+ continue;
11080+
11081+ /* write error */
11082+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11083+ err = -ENOSPC;
11084+ if (ssz < 0)
11085+ err = ssz;
11086+ break;
11087+ }
11088+ }
11089+ /* regardless the error */
11090+ if (pos < oldsize) {
11091+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11092+ AuTraceErr(err);
11093+ }
11094+
11095+ AuTraceErr(err);
11096+ return err;
11097+}
11098+
11099+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11100+{
11101+ int err, hidx;
11102+ ssize_t ssz;
11103+ size_t sz, n;
11104+ loff_t pos;
11105+ uint64_t u64;
11106+ struct au_dr_hino *ent;
11107+ struct inode *hinoinode;
11108+ struct hlist_bl_head *hbl;
11109+
11110+ err = 0;
11111+ pos = 0;
11112+ hbl = dr->dr_h_ino;
11113+ hinoinode = file_inode(hinofile);
11114+ sz = i_size_read(hinoinode);
11115+ AuDebugOn(sz % sizeof(u64));
11116+ n = sz / sizeof(u64);
11117+ while (n--) {
11118+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11119+ if (unlikely(ssz != sizeof(u64))) {
11120+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11121+ err = -EINVAL;
11122+ if (ssz < 0)
11123+ err = ssz;
11124+ goto out_free;
11125+ }
11126+
11127+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11128+ if (!ent) {
11129+ err = -ENOMEM;
11130+ AuTraceErr(err);
11131+ goto out_free;
11132+ }
11133+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11134+ AuDbg("hi%llu, %pD2\n",
11135+ (unsigned long long)ent->dr_h_ino, hinofile);
11136+ hidx = au_dr_ihash(ent->dr_h_ino);
11137+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11138+ }
11139+ goto out; /* success */
11140+
11141+out_free:
11142+ au_dr_hino_free(dr);
11143+out:
11144+ AuTraceErr(err);
11145+ return err;
11146+}
11147+
11148+/*
11149+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11150+ * @path is a switch to distinguish load and store.
11151+ */
11152+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11153+ struct au_branch *br, const struct path *path)
11154+{
11155+ int err, flags;
11156+ unsigned char load, suspend;
11157+ struct file *hinofile;
11158+ struct au_hinode *hdir;
11159+ struct inode *dir, *delegated;
11160+ struct path hinopath;
11161+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11162+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11163+
11164+ AuDebugOn(bindex < 0 && !br);
11165+ AuDebugOn(bindex >= 0 && br);
11166+
11167+ err = -EINVAL;
11168+ suspend = !br;
11169+ if (suspend)
11170+ br = au_sbr(sb, bindex);
11171+ load = !!path;
11172+ if (!load) {
11173+ path = &br->br_path;
11174+ AuDebugOn(!au_br_writable(br->br_perm));
11175+ if (unlikely(!au_br_writable(br->br_perm)))
11176+ goto out;
11177+ }
11178+
11179+ hdir = NULL;
11180+ if (suspend) {
11181+ dir = d_inode(sb->s_root);
11182+ hdir = au_hinode(au_ii(dir), bindex);
11183+ dir = hdir->hi_inode;
11184+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11185+ } else {
11186+ dir = d_inode(path->dentry);
11187+ inode_lock_nested(dir, AuLsc_I_CHILD);
11188+ }
11189+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11190+ err = PTR_ERR(hinopath.dentry);
11191+ if (IS_ERR(hinopath.dentry))
11192+ goto out_unlock;
11193+
11194+ err = 0;
11195+ flags = O_RDONLY;
11196+ if (load) {
11197+ if (d_is_negative(hinopath.dentry))
11198+ goto out_dput; /* success */
11199+ } else {
11200+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11201+ if (d_is_positive(hinopath.dentry)) {
11202+ delegated = NULL;
11203+ err = vfsub_unlink(dir, &hinopath, &delegated,
11204+ /*force*/0);
11205+ AuTraceErr(err);
11206+ if (unlikely(err))
11207+ pr_err("ignored err %d, %pd2\n",
11208+ err, hinopath.dentry);
11209+ if (unlikely(err == -EWOULDBLOCK))
11210+ iput(delegated);
11211+ err = 0;
11212+ }
11213+ goto out_dput;
11214+ } else if (!d_is_positive(hinopath.dentry)) {
11215+ err = vfsub_create(dir, &hinopath, 0600,
11216+ /*want_excl*/false);
11217+ AuTraceErr(err);
11218+ if (unlikely(err))
11219+ goto out_dput;
11220+ }
11221+ flags = O_WRONLY;
11222+ }
11223+ hinopath.mnt = path->mnt;
11224+ hinofile = vfsub_dentry_open(&hinopath, flags);
11225+ if (suspend)
11226+ au_hn_inode_unlock(hdir);
11227+ else
11228+ inode_unlock(dir);
11229+ dput(hinopath.dentry);
11230+ AuTraceErrPtr(hinofile);
11231+ if (IS_ERR(hinofile)) {
11232+ err = PTR_ERR(hinofile);
11233+ goto out;
11234+ }
11235+
11236+ if (load)
11237+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11238+ else
11239+ err = au_dr_hino_store(sb, br, hinofile);
11240+ fput(hinofile);
11241+ goto out;
11242+
11243+out_dput:
11244+ dput(hinopath.dentry);
11245+out_unlock:
11246+ if (suspend)
11247+ au_hn_inode_unlock(hdir);
11248+ else
11249+ inode_unlock(dir);
11250+out:
11251+ AuTraceErr(err);
11252+ return err;
11253+}
11254+
11255+/* ---------------------------------------------------------------------- */
11256+
11257+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11258+{
11259+ int err;
11260+ struct kstatfs kstfs;
11261+ dev_t dev;
11262+ struct dentry *dentry;
11263+ struct super_block *sb;
11264+
11265+ err = vfs_statfs((void *)path, &kstfs);
11266+ AuTraceErr(err);
11267+ if (unlikely(err))
11268+ goto out;
11269+
11270+ /* todo: support for UUID */
11271+
11272+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11273+ brid->type = AuBrid_FSID;
11274+ brid->fsid = kstfs.f_fsid;
11275+ } else {
11276+ dentry = path->dentry;
11277+ sb = dentry->d_sb;
11278+ dev = sb->s_dev;
11279+ if (dev) {
11280+ brid->type = AuBrid_DEV;
11281+ brid->dev = dev;
11282+ }
11283+ }
11284+
11285+out:
11286+ return err;
11287+}
11288+
11289+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11290+ const struct path *path)
11291+{
11292+ int err, i;
11293+ struct au_dr_br *dr;
11294+ struct hlist_bl_head *hbl;
11295+
11296+ dr = &br->br_dirren;
11297+ hbl = dr->dr_h_ino;
11298+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11299+ INIT_HLIST_BL_HEAD(hbl);
11300+
11301+ err = au_dr_brid_init(&dr->dr_brid, path);
11302+ if (unlikely(err))
11303+ goto out;
11304+
11305+ if (au_opt_test(au_mntflags(sb), DIRREN))
11306+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11307+
11308+out:
11309+ AuTraceErr(err);
11310+ return err;
11311+}
11312+
11313+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11314+{
11315+ int err;
11316+
11317+ err = 0;
11318+ if (au_br_writable(br->br_perm))
11319+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11320+ if (!err)
11321+ au_dr_hino_free(&br->br_dirren);
11322+
11323+ return err;
11324+}
11325+
11326+/* ---------------------------------------------------------------------- */
11327+
11328+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11329+ char *buf, size_t sz)
11330+{
11331+ int err;
11332+ unsigned int major, minor;
11333+ char *p;
11334+
11335+ p = buf;
11336+ err = snprintf(p, sz, "%d_", brid->type);
11337+ AuDebugOn(err > sz);
11338+ p += err;
11339+ sz -= err;
11340+ switch (brid->type) {
11341+ case AuBrid_Unset:
11342+ return -EINVAL;
11343+ case AuBrid_UUID:
11344+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11345+ break;
11346+ case AuBrid_FSID:
11347+ err = snprintf(p, sz, "%08x-%08x",
11348+ brid->fsid.val[0], brid->fsid.val[1]);
11349+ break;
11350+ case AuBrid_DEV:
11351+ major = MAJOR(brid->dev);
11352+ minor = MINOR(brid->dev);
11353+ if (major <= 0xff && minor <= 0xff)
11354+ err = snprintf(p, sz, "%02x%02x", major, minor);
11355+ else
11356+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11357+ break;
11358+ }
11359+ AuDebugOn(err > sz);
11360+ p += err;
11361+ sz -= err;
11362+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11363+ AuDebugOn(err > sz);
11364+ p += err;
11365+ sz -= err;
11366+
11367+ return p - buf;
11368+}
11369+
11370+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11371+{
11372+ int rlen;
11373+ struct dentry *br_dentry;
11374+ struct inode *br_inode;
11375+
11376+ br_dentry = au_br_dentry(br);
11377+ br_inode = d_inode(br_dentry);
11378+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11379+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11380+ AuDebugOn(rlen > len);
11381+
11382+ return rlen;
11383+}
11384+
11385+/* ---------------------------------------------------------------------- */
11386+
11387+/*
11388+ * from the given @h_dentry, construct drinfo at @*fdata.
11389+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11390+ * @allocated.
11391+ */
11392+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11393+ struct dentry *h_dentry,
11394+ unsigned char *allocated)
11395+{
11396+ int err, v;
11397+ struct au_drinfo_fdata *f, *p;
11398+ struct au_drinfo *drinfo;
11399+ struct inode *h_inode;
11400+ struct qstr *qname;
11401+
11402+ err = 0;
11403+ f = *fdata;
11404+ h_inode = d_inode(h_dentry);
11405+ qname = &h_dentry->d_name;
11406+ drinfo = &f->drinfo;
11407+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11408+ drinfo->oldnamelen = qname->len;
11409+ if (*allocated < sizeof(*f) + qname->len) {
11410+ v = roundup_pow_of_two(*allocated + qname->len);
11411+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11412+ if (unlikely(!p)) {
11413+ err = -ENOMEM;
11414+ AuTraceErr(err);
11415+ goto out;
11416+ }
11417+ f = p;
11418+ *fdata = f;
11419+ *allocated = v;
11420+ drinfo = &f->drinfo;
11421+ }
11422+ memcpy(drinfo->oldname, qname->name, qname->len);
11423+ AuDbg("i%llu, %.*s\n",
11424+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11425+ drinfo->oldname);
11426+
11427+out:
11428+ AuTraceErr(err);
11429+ return err;
11430+}
11431+
11432+/* callers have to free the return value */
11433+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11434+{
11435+ struct au_drinfo *ret, *drinfo;
11436+ struct au_drinfo_fdata fdata;
11437+ int len;
11438+ loff_t pos;
11439+ ssize_t ssz;
11440+
11441+ ret = ERR_PTR(-EIO);
11442+ pos = 0;
11443+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11444+ if (unlikely(ssz != sizeof(fdata))) {
11445+ AuIOErr("ssz %zd, %u, %pD2\n",
11446+ ssz, (unsigned int)sizeof(fdata), file);
11447+ goto out;
11448+ }
11449+
11450+ fdata.magic = ntohl((__force __be32)fdata.magic);
11451+ switch (fdata.magic) {
11452+ case AUFS_DRINFO_MAGIC_V1:
11453+ break;
11454+ default:
11455+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11456+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11457+ goto out;
11458+ }
11459+
11460+ drinfo = &fdata.drinfo;
11461+ len = drinfo->oldnamelen;
11462+ if (!len) {
11463+ AuIOErr("broken drinfo %pD2\n", file);
11464+ goto out;
11465+ }
11466+
11467+ ret = NULL;
11468+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11469+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11470+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11471+ (unsigned long long)drinfo->ino,
11472+ (unsigned long long)h_ino, file);
11473+ goto out; /* success */
11474+ }
11475+
11476+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11477+ if (unlikely(!ret)) {
11478+ ret = ERR_PTR(-ENOMEM);
11479+ AuTraceErrPtr(ret);
11480+ goto out;
11481+ }
11482+
11483+ *ret = *drinfo;
11484+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11485+ if (unlikely(ssz != len)) {
11486+ kfree(ret);
11487+ ret = ERR_PTR(-EIO);
11488+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11489+ goto out;
11490+ }
11491+
11492+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11493+
11494+out:
11495+ return ret;
11496+}
11497+
11498+/* ---------------------------------------------------------------------- */
11499+
11500+/* in order to be revertible */
11501+struct au_drinfo_rev_elm {
11502+ int created;
11503+ struct dentry *info_dentry;
11504+ struct au_drinfo *info_last;
11505+};
11506+
11507+struct au_drinfo_rev {
11508+ unsigned char already;
11509+ aufs_bindex_t nelm;
11510+ struct au_drinfo_rev_elm elm[0];
11511+};
11512+
11513+/* todo: isn't it too large? */
11514+struct au_drinfo_store {
11515+ struct path h_ppath;
11516+ struct dentry *h_dentry;
11517+ struct au_drinfo_fdata *fdata;
11518+ char *infoname; /* inside of whname, just after PFX */
11519+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11520+ aufs_bindex_t btgt, btail;
11521+ unsigned char no_sio,
11522+ allocated, /* current size of *fdata */
11523+ infonamelen, /* room size for p */
11524+ whnamelen, /* length of the genarated name */
11525+ renameback; /* renamed back */
11526+};
11527+
11528+/* on rename(2) error, the caller should revert it using @elm */
11529+static int au_drinfo_do_store(struct au_drinfo_store *w,
11530+ struct au_drinfo_rev_elm *elm)
11531+{
11532+ int err, len;
11533+ ssize_t ssz;
11534+ loff_t pos;
11535+ struct path infopath = {
11536+ .mnt = w->h_ppath.mnt
11537+ };
11538+ struct inode *h_dir, *h_inode, *delegated;
11539+ struct file *infofile;
11540+ struct qstr *qname;
11541+
11542+ AuDebugOn(elm
11543+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11544+
11545+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11546+ w->whnamelen);
11547+ AuTraceErrPtr(infopath.dentry);
11548+ if (IS_ERR(infopath.dentry)) {
11549+ err = PTR_ERR(infopath.dentry);
11550+ goto out;
11551+ }
11552+
11553+ err = 0;
11554+ h_dir = d_inode(w->h_ppath.dentry);
11555+ if (elm && d_is_negative(infopath.dentry)) {
11556+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11557+ AuTraceErr(err);
11558+ if (unlikely(err))
11559+ goto out_dput;
11560+ elm->created = 1;
11561+ elm->info_dentry = dget(infopath.dentry);
11562+ }
11563+
11564+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11565+ AuTraceErrPtr(infofile);
11566+ if (IS_ERR(infofile)) {
11567+ err = PTR_ERR(infofile);
11568+ goto out_dput;
11569+ }
11570+
11571+ h_inode = d_inode(infopath.dentry);
11572+ if (elm && i_size_read(h_inode)) {
11573+ h_inode = d_inode(w->h_dentry);
11574+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11575+ AuTraceErrPtr(elm->info_last);
11576+ if (IS_ERR(elm->info_last)) {
11577+ err = PTR_ERR(elm->info_last);
11578+ elm->info_last = NULL;
11579+ AuDebugOn(elm->info_dentry);
11580+ goto out_fput;
11581+ }
11582+ }
11583+
11584+ if (elm && w->renameback) {
11585+ delegated = NULL;
11586+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11587+ AuTraceErr(err);
11588+ if (unlikely(err == -EWOULDBLOCK))
11589+ iput(delegated);
11590+ goto out_fput;
11591+ }
11592+
11593+ pos = 0;
11594+ qname = &w->h_dentry->d_name;
11595+ len = sizeof(*w->fdata) + qname->len;
11596+ if (!elm)
11597+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11598+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11599+ if (ssz == len) {
11600+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11601+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11602+ goto out_fput; /* success */
11603+ } else {
11604+ err = -EIO;
11605+ if (ssz < 0)
11606+ err = ssz;
11607+ /* the caller should revert it using @elm */
11608+ }
11609+
11610+out_fput:
11611+ fput(infofile);
11612+out_dput:
11613+ dput(infopath.dentry);
11614+out:
11615+ AuTraceErr(err);
11616+ return err;
11617+}
11618+
11619+struct au_call_drinfo_do_store_args {
11620+ int *errp;
11621+ struct au_drinfo_store *w;
11622+ struct au_drinfo_rev_elm *elm;
11623+};
11624+
11625+static void au_call_drinfo_do_store(void *args)
11626+{
11627+ struct au_call_drinfo_do_store_args *a = args;
11628+
11629+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11630+}
11631+
11632+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11633+ struct au_drinfo_rev_elm *elm)
11634+{
11635+ int err, wkq_err;
11636+
11637+ if (w->no_sio)
11638+ err = au_drinfo_do_store(w, elm);
11639+ else {
11640+ struct au_call_drinfo_do_store_args a = {
11641+ .errp = &err,
11642+ .w = w,
11643+ .elm = elm
11644+ };
11645+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11646+ if (unlikely(wkq_err))
11647+ err = wkq_err;
11648+ }
11649+ AuTraceErr(err);
11650+
11651+ return err;
11652+}
11653+
11654+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11655+ aufs_bindex_t btgt)
11656+{
11657+ int err;
11658+
11659+ memset(w, 0, sizeof(*w));
11660+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11661+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11662+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11663+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11664+ w->btgt = btgt;
11665+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11666+
11667+ err = -ENOMEM;
11668+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11669+ if (unlikely(!w->fdata)) {
11670+ AuTraceErr(err);
11671+ goto out;
11672+ }
11673+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11674+ err = 0;
11675+
11676+out:
11677+ return err;
11678+}
11679+
11680+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11681+{
11682+ kfree(w->fdata);
11683+}
11684+
11685+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11686+ struct au_drinfo_store *w)
11687+{
11688+ struct au_drinfo_rev_elm *elm;
11689+ struct inode *h_dir, *delegated;
11690+ int err, nelm;
11691+ struct path infopath = {
11692+ .mnt = w->h_ppath.mnt
11693+ };
11694+
11695+ h_dir = d_inode(w->h_ppath.dentry);
11696+ IMustLock(h_dir);
11697+
11698+ err = 0;
11699+ elm = rev->elm;
11700+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11701+ AuDebugOn(elm->created && elm->info_last);
11702+ if (elm->created) {
11703+ AuDbg("here\n");
11704+ delegated = NULL;
11705+ infopath.dentry = elm->info_dentry;
11706+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11707+ !w->no_sio);
11708+ AuTraceErr(err);
11709+ if (unlikely(err == -EWOULDBLOCK))
11710+ iput(delegated);
11711+ dput(elm->info_dentry);
11712+ } else if (elm->info_last) {
11713+ AuDbg("here\n");
11714+ w->fdata->drinfo = *elm->info_last;
11715+ memcpy(w->fdata->drinfo.oldname,
11716+ elm->info_last->oldname,
11717+ elm->info_last->oldnamelen);
11718+ err = au_drinfo_store_sio(w, /*elm*/NULL);
11719+ kfree(elm->info_last);
11720+ }
11721+ if (unlikely(err))
11722+ AuIOErr("%d, %s\n", err, w->whname);
11723+ /* go on even if err */
11724+ }
11725+}
11726+
11727+/* caller has to call au_dr_rename_fin() later */
11728+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11729+ struct qstr *dst_name, void *_rev)
11730+{
11731+ int err, sz, nelm;
11732+ aufs_bindex_t bindex, btail;
11733+ struct au_drinfo_store work;
11734+ struct au_drinfo_rev *rev, **p;
11735+ struct au_drinfo_rev_elm *elm;
11736+ struct super_block *sb;
11737+ struct au_branch *br;
11738+ struct au_hinode *hdir;
11739+
11740+ err = au_drinfo_store_work_init(&work, btgt);
11741+ AuTraceErr(err);
11742+ if (unlikely(err))
11743+ goto out;
11744+
11745+ err = -ENOMEM;
11746+ btail = au_dbtaildir(dentry);
11747+ nelm = btail - btgt;
11748+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11749+ rev = kcalloc(1, sz, GFP_NOFS);
11750+ if (unlikely(!rev)) {
11751+ AuTraceErr(err);
11752+ goto out_args;
11753+ }
11754+ rev->nelm = nelm;
11755+ elm = rev->elm;
11756+ p = _rev;
11757+ *p = rev;
11758+
11759+ err = 0;
11760+ sb = dentry->d_sb;
11761+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11762+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11763+ hdir = au_hi(d_inode(dentry), btgt);
11764+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11765+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11766+ work.h_dentry = au_h_dptr(dentry, bindex);
11767+ if (!work.h_dentry)
11768+ continue;
11769+
11770+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11771+ &work.allocated);
11772+ AuTraceErr(err);
11773+ if (unlikely(err))
11774+ break;
11775+
11776+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11777+ br = au_sbr(sb, bindex);
11778+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11779+ work.whnamelen += au_drinfo_name(br, work.infoname,
11780+ work.infonamelen);
11781+ AuDbg("whname %.*s, i%llu, %.*s\n",
11782+ work.whnamelen, work.whname,
11783+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11784+ work.fdata->drinfo.oldnamelen,
11785+ work.fdata->drinfo.oldname);
11786+
11787+ err = au_drinfo_store_sio(&work, elm);
11788+ AuTraceErr(err);
11789+ if (unlikely(err))
11790+ break;
11791+ }
11792+ if (unlikely(err)) {
11793+ /* revert all drinfo */
11794+ au_drinfo_store_rev(rev, &work);
11795+ kfree(rev);
11796+ *p = NULL;
11797+ }
11798+ au_hn_inode_unlock(hdir);
11799+
11800+out_args:
11801+ au_drinfo_store_work_fin(&work);
11802+out:
11803+ return err;
11804+}
11805+
11806+/* ---------------------------------------------------------------------- */
11807+
11808+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11809+ struct qstr *dst_name, void *_rev)
11810+{
11811+ int err, already;
11812+ ino_t ino;
11813+ struct super_block *sb;
11814+ struct au_branch *br;
11815+ struct au_dr_br *dr;
11816+ struct dentry *h_dentry;
11817+ struct inode *h_inode;
11818+ struct au_dr_hino *ent;
11819+ struct au_drinfo_rev *rev, **p;
11820+
11821+ AuDbg("bindex %d\n", bindex);
11822+
11823+ err = -ENOMEM;
11824+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11825+ if (unlikely(!ent))
11826+ goto out;
11827+
11828+ sb = src->d_sb;
11829+ br = au_sbr(sb, bindex);
11830+ dr = &br->br_dirren;
11831+ h_dentry = au_h_dptr(src, bindex);
11832+ h_inode = d_inode(h_dentry);
11833+ ino = h_inode->i_ino;
11834+ ent->dr_h_ino = ino;
11835+ already = au_dr_hino_test_add(dr, ino, ent);
11836+ AuDbg("b%d, hi%llu, already %d\n",
11837+ bindex, (unsigned long long)ino, already);
11838+
11839+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11840+ AuTraceErr(err);
11841+ if (!err) {
11842+ p = _rev;
11843+ rev = *p;
11844+ rev->already = already;
11845+ goto out; /* success */
11846+ }
11847+
11848+ /* revert */
11849+ if (!already)
11850+ au_dr_hino_del(dr, ent);
11851+ kfree(ent);
11852+
11853+out:
11854+ AuTraceErr(err);
11855+ return err;
11856+}
11857+
11858+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11859+{
11860+ struct au_drinfo_rev *rev;
11861+ struct au_drinfo_rev_elm *elm;
11862+ int nelm;
11863+
11864+ rev = _rev;
11865+ elm = rev->elm;
11866+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11867+ dput(elm->info_dentry);
11868+ kfree(elm->info_last);
11869+ }
11870+ kfree(rev);
11871+}
11872+
11873+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11874+{
11875+ int err;
11876+ struct au_drinfo_store work;
11877+ struct au_drinfo_rev *rev = _rev;
11878+ struct super_block *sb;
11879+ struct au_branch *br;
11880+ struct inode *h_inode;
11881+ struct au_dr_br *dr;
11882+ struct au_dr_hino *ent;
11883+
11884+ err = au_drinfo_store_work_init(&work, btgt);
11885+ if (unlikely(err))
11886+ goto out;
11887+
11888+ sb = src->d_sb;
11889+ br = au_sbr(sb, btgt);
11890+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11891+ work.h_ppath.mnt = au_br_mnt(br);
11892+ au_drinfo_store_rev(rev, &work);
11893+ au_drinfo_store_work_fin(&work);
11894+ if (rev->already)
11895+ goto out;
11896+
11897+ dr = &br->br_dirren;
11898+ h_inode = d_inode(work.h_ppath.dentry);
11899+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11900+ BUG_ON(!ent);
11901+ au_dr_hino_del(dr, ent);
11902+ kfree(ent);
11903+
11904+out:
11905+ kfree(rev);
11906+ if (unlikely(err))
11907+ pr_err("failed to remove dirren info\n");
11908+}
11909+
11910+/* ---------------------------------------------------------------------- */
11911+
11912+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11913+ char *whname, int whnamelen,
11914+ struct dentry **info_dentry)
11915+{
11916+ struct au_drinfo *drinfo;
11917+ struct file *f;
11918+ struct inode *h_dir;
11919+ struct path infopath;
11920+ int unlocked;
11921+
11922+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11923+
11924+ *info_dentry = NULL;
11925+ drinfo = NULL;
11926+ unlocked = 0;
11927+ h_dir = d_inode(h_ppath->dentry);
be118d29 11928+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947
AM
11929+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11930+ whnamelen);
11931+ if (IS_ERR(infopath.dentry)) {
11932+ drinfo = (void *)infopath.dentry;
11933+ goto out;
11934+ }
11935+
11936+ if (d_is_negative(infopath.dentry))
11937+ goto out_dput; /* success */
11938+
11939+ infopath.mnt = h_ppath->mnt;
11940+ f = vfsub_dentry_open(&infopath, O_RDONLY);
11941+ inode_unlock_shared(h_dir);
11942+ unlocked = 1;
11943+ if (IS_ERR(f)) {
11944+ drinfo = (void *)f;
11945+ goto out_dput;
11946+ }
11947+
11948+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11949+ if (IS_ERR_OR_NULL(drinfo))
11950+ goto out_fput;
11951+
11952+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11953+ *info_dentry = dget(infopath.dentry); /* keep it alive */
11954+
11955+out_fput:
11956+ fput(f);
11957+out_dput:
11958+ dput(infopath.dentry);
11959+out:
11960+ if (!unlocked)
11961+ inode_unlock_shared(h_dir);
11962+ AuTraceErrPtr(drinfo);
11963+ return drinfo;
11964+}
11965+
11966+struct au_drinfo_do_load_args {
11967+ struct au_drinfo **drinfop;
11968+ struct path *h_ppath;
11969+ char *whname;
11970+ int whnamelen;
11971+ struct dentry **info_dentry;
11972+};
11973+
11974+static void au_call_drinfo_do_load(void *args)
11975+{
11976+ struct au_drinfo_do_load_args *a = args;
11977+
11978+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11979+ a->info_dentry);
11980+}
11981+
11982+struct au_drinfo_load {
11983+ struct path h_ppath;
11984+ struct qstr *qname;
11985+ unsigned char no_sio;
11986+
11987+ aufs_bindex_t ninfo;
11988+ struct au_drinfo **drinfo;
11989+};
11990+
11991+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11992+ struct au_branch *br)
11993+{
11994+ int err, wkq_err, whnamelen, e;
11995+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11996+ = AUFS_WH_DR_INFO_PFX;
11997+ struct au_drinfo *drinfo;
11998+ struct qstr oldname;
11999+ struct inode *h_dir, *delegated;
12000+ struct dentry *info_dentry;
12001+ struct path infopath;
12002+
12003+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12004+ whnamelen += au_drinfo_name(br, whname + whnamelen,
12005+ sizeof(whname) - whnamelen);
12006+ if (w->no_sio)
12007+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12008+ &info_dentry);
12009+ else {
12010+ struct au_drinfo_do_load_args args = {
12011+ .drinfop = &drinfo,
12012+ .h_ppath = &w->h_ppath,
12013+ .whname = whname,
12014+ .whnamelen = whnamelen,
12015+ .info_dentry = &info_dentry
12016+ };
12017+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12018+ if (unlikely(wkq_err))
12019+ drinfo = ERR_PTR(wkq_err);
12020+ }
12021+ err = PTR_ERR(drinfo);
12022+ if (IS_ERR_OR_NULL(drinfo))
12023+ goto out;
12024+
12025+ err = 0;
12026+ oldname.len = drinfo->oldnamelen;
12027+ oldname.name = drinfo->oldname;
12028+ if (au_qstreq(w->qname, &oldname)) {
12029+ /* the name is renamed back */
12030+ kfree(drinfo);
12031+ drinfo = NULL;
12032+
12033+ infopath.dentry = info_dentry;
12034+ infopath.mnt = w->h_ppath.mnt;
12035+ h_dir = d_inode(w->h_ppath.dentry);
12036+ delegated = NULL;
12037+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12038+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12039+ inode_unlock(h_dir);
12040+ if (unlikely(e))
12041+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12042+ if (unlikely(e == -EWOULDBLOCK))
12043+ iput(delegated);
12044+ }
12045+ kfree(w->drinfo[bindex]);
12046+ w->drinfo[bindex] = drinfo;
12047+ dput(info_dentry);
12048+
12049+out:
12050+ AuTraceErr(err);
12051+ return err;
12052+}
12053+
12054+/* ---------------------------------------------------------------------- */
12055+
12056+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12057+{
12058+ struct au_drinfo **p = drinfo;
12059+
12060+ while (n-- > 0)
12061+ kfree(*drinfo++);
12062+ kfree(p);
12063+}
12064+
12065+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12066+ aufs_bindex_t btgt)
12067+{
12068+ int err, ninfo;
12069+ struct au_drinfo_load w;
12070+ aufs_bindex_t bindex, bbot;
12071+ struct au_branch *br;
12072+ struct inode *h_dir;
12073+ struct au_dr_hino *ent;
12074+ struct super_block *sb;
12075+
12076+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12077+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12078+ AuLNPair(&lkup->whname), btgt);
12079+
12080+ sb = dentry->d_sb;
12081+ bbot = au_sbbot(sb);
12082+ w.ninfo = bbot + 1;
12083+ if (!lkup->dirren.drinfo) {
12084+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12085+ sizeof(*lkup->dirren.drinfo),
12086+ GFP_NOFS);
12087+ if (unlikely(!lkup->dirren.drinfo)) {
12088+ err = -ENOMEM;
12089+ goto out;
12090+ }
12091+ lkup->dirren.ninfo = w.ninfo;
12092+ }
12093+ w.drinfo = lkup->dirren.drinfo;
12094+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12095+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12096+ AuDebugOn(!w.h_ppath.dentry);
12097+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12098+ w.qname = &dentry->d_name;
12099+
12100+ ninfo = 0;
12101+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12102+ br = au_sbr(sb, bindex);
12103+ err = au_drinfo_load(&w, bindex, br);
12104+ if (unlikely(err))
12105+ goto out_free;
12106+ if (w.drinfo[bindex])
12107+ ninfo++;
12108+ }
12109+ if (!ninfo) {
12110+ br = au_sbr(sb, btgt);
12111+ h_dir = d_inode(w.h_ppath.dentry);
12112+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12113+ AuDebugOn(!ent);
12114+ au_dr_hino_del(&br->br_dirren, ent);
12115+ kfree(ent);
12116+ }
12117+ goto out; /* success */
12118+
12119+out_free:
12120+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12121+ lkup->dirren.ninfo = 0;
12122+ lkup->dirren.drinfo = NULL;
12123+out:
12124+ AuTraceErr(err);
12125+ return err;
12126+}
12127+
12128+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12129+{
12130+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12131+}
12132+
12133+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12134+{
12135+ int err;
12136+ struct au_drinfo *drinfo;
12137+
12138+ err = 0;
12139+ if (!lkup->dirren.drinfo)
12140+ goto out;
12141+ AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12142+ drinfo = lkup->dirren.drinfo[btgt + 1];
12143+ if (!drinfo)
12144+ goto out;
12145+
12146+ kfree(lkup->whname.name);
12147+ lkup->whname.name = NULL;
12148+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12149+ lkup->dirren.dr_name.name = drinfo->oldname;
12150+ lkup->name = &lkup->dirren.dr_name;
12151+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12152+ if (!err)
12153+ AuDbg("name %.*s, whname %.*s, b%d\n",
12154+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12155+ btgt);
12156+
12157+out:
12158+ AuTraceErr(err);
12159+ return err;
12160+}
12161+
12162+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12163+ ino_t h_ino)
12164+{
12165+ int match;
12166+ struct au_drinfo *drinfo;
12167+
12168+ match = 1;
12169+ if (!lkup->dirren.drinfo)
12170+ goto out;
12171+ AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12172+ drinfo = lkup->dirren.drinfo[bindex + 1];
12173+ if (!drinfo)
12174+ goto out;
12175+
12176+ match = (drinfo->ino == h_ino);
12177+ AuDbg("match %d\n", match);
12178+
12179+out:
12180+ return match;
12181+}
12182+
12183+/* ---------------------------------------------------------------------- */
12184+
12185+int au_dr_opt_set(struct super_block *sb)
12186+{
12187+ int err;
12188+ aufs_bindex_t bindex, bbot;
12189+ struct au_branch *br;
12190+
12191+ err = 0;
12192+ bbot = au_sbbot(sb);
12193+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12194+ br = au_sbr(sb, bindex);
12195+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12196+ }
12197+
12198+ return err;
12199+}
12200+
12201+int au_dr_opt_flush(struct super_block *sb)
12202+{
12203+ int err;
12204+ aufs_bindex_t bindex, bbot;
12205+ struct au_branch *br;
12206+
12207+ err = 0;
12208+ bbot = au_sbbot(sb);
12209+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12210+ br = au_sbr(sb, bindex);
12211+ if (au_br_writable(br->br_perm))
12212+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12213+ }
12214+
12215+ return err;
12216+}
12217+
12218+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12219+{
12220+ int err;
12221+ aufs_bindex_t bindex, bbot;
12222+ struct au_branch *br;
12223+
12224+ err = 0;
12225+ if (!no_flush) {
12226+ err = au_dr_opt_flush(sb);
12227+ if (unlikely(err))
12228+ goto out;
12229+ }
12230+
12231+ bbot = au_sbbot(sb);
12232+ for (bindex = 0; bindex <= bbot; bindex++) {
12233+ br = au_sbr(sb, bindex);
12234+ au_dr_hino_free(&br->br_dirren);
12235+ }
12236+
12237+out:
12238+ return err;
12239+}
12240diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12241--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
be118d29 12242+++ linux/fs/aufs/dirren.h 2018-04-06 07:48:44.204604724 +0200
8b6a4947
AM
12243@@ -0,0 +1,139 @@
12244+/*
12245+ * Copyright (C) 2017 Junjiro R. Okajima
12246+ *
12247+ * This program, aufs is free software; you can redistribute it and/or modify
12248+ * it under the terms of the GNU General Public License as published by
12249+ * the Free Software Foundation; either version 2 of the License, or
12250+ * (at your option) any later version.
12251+ *
12252+ * This program is distributed in the hope that it will be useful,
12253+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12254+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12255+ * GNU General Public License for more details.
12256+ *
12257+ * You should have received a copy of the GNU General Public License
12258+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12259+ */
12260+
12261+/*
12262+ * renamed dir info
12263+ */
12264+
12265+#ifndef __AUFS_DIRREN_H__
12266+#define __AUFS_DIRREN_H__
12267+
12268+#ifdef __KERNEL__
12269+
12270+#include <linux/dcache.h>
12271+#include <linux/statfs.h>
12272+#include <linux/uuid.h>
12273+#include "hbl.h"
12274+
12275+#define AuDirren_NHASH 100
12276+
12277+#ifdef CONFIG_AUFS_DIRREN
12278+enum au_brid_type {
12279+ AuBrid_Unset,
12280+ AuBrid_UUID,
12281+ AuBrid_FSID,
12282+ AuBrid_DEV
12283+};
12284+
12285+struct au_dr_brid {
12286+ enum au_brid_type type;
12287+ union {
12288+ uuid_t uuid; /* unimplemented yet */
12289+ fsid_t fsid;
12290+ dev_t dev;
12291+ };
12292+};
12293+
12294+/* 20 is the max digits length of ulong 64 */
12295+/* brid-type "_" uuid "_" inum */
12296+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12297+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12298+
12299+struct au_dr_hino {
12300+ struct hlist_bl_node dr_hnode;
12301+ ino_t dr_h_ino;
12302+};
12303+
12304+struct au_dr_br {
12305+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12306+ struct au_dr_brid dr_brid;
12307+};
12308+
12309+struct au_dr_lookup {
12310+ /* dr_name is pointed by struct au_do_lookup_args.name */
12311+ struct qstr dr_name; /* subset of dr_info */
12312+ aufs_bindex_t ninfo;
12313+ struct au_drinfo **drinfo;
12314+};
12315+#else
12316+struct au_dr_hino;
12317+/* empty */
12318+struct au_dr_br { };
12319+struct au_dr_lookup { };
12320+#endif
12321+
12322+/* ---------------------------------------------------------------------- */
12323+
12324+struct au_branch;
12325+struct au_do_lookup_args;
12326+struct au_hinode;
12327+#ifdef CONFIG_AUFS_DIRREN
12328+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12329+ struct au_dr_hino *add_ent);
12330+void au_dr_hino_free(struct au_dr_br *dr);
12331+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12332+ const struct path *path);
12333+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12334+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12335+ struct qstr *dst_name, void *_rev);
12336+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12337+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12338+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12339+ aufs_bindex_t bindex);
12340+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12341+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12342+ ino_t h_ino);
12343+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12344+int au_dr_opt_set(struct super_block *sb);
12345+int au_dr_opt_flush(struct super_block *sb);
12346+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12347+#else
12348+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12349+ struct au_dr_hino *add_ent);
12350+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12351+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12352+ const struct path *path);
12353+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12354+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12355+ struct qstr *dst_name, void *_rev);
12356+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12357+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12358+ void *rev);
12359+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12360+ aufs_bindex_t bindex);
12361+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12362+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12363+ aufs_bindex_t bindex, ino_t h_ino);
12364+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12365+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12366+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12367+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12368+#endif
12369+
12370+/* ---------------------------------------------------------------------- */
12371+
12372+#ifdef CONFIG_AUFS_DIRREN
12373+static inline int au_dr_ihash(ino_t h_ino)
12374+{
12375+ return h_ino % AuDirren_NHASH;
12376+}
12377+#else
12378+AuStubInt0(au_dr_ihash, ino_t h_ino);
12379+#endif
12380+
12381+#endif /* __KERNEL__ */
12382+#endif /* __AUFS_DIRREN_H__ */
7f207e10
AM
12383diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12384--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
be118d29 12385+++ linux/fs/aufs/dynop.c 2018-04-06 07:48:44.204604724 +0200
8b6a4947 12386@@ -0,0 +1,369 @@
1facf9fc 12387+/*
a2654f78 12388+ * Copyright (C) 2010-2017 Junjiro R. Okajima
1facf9fc 12389+ *
12390+ * This program, aufs is free software; you can redistribute it and/or modify
12391+ * it under the terms of the GNU General Public License as published by
12392+ * the Free Software Foundation; either version 2 of the License, or
12393+ * (at your option) any later version.
dece6358
AM
12394+ *
12395+ * This program is distributed in the hope that it will be useful,
12396+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12397+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12398+ * GNU General Public License for more details.
12399+ *
12400+ * You should have received a copy of the GNU General Public License
523b37e3 12401+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12402+ */
12403+
12404+/*
4a4d8108 12405+ * dynamically customizable operations for regular files
1facf9fc 12406+ */
12407+
1facf9fc 12408+#include "aufs.h"
12409+
4a4d8108 12410+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12411+
4a4d8108
AM
12412+/*
12413+ * How large will these lists be?
12414+ * Usually just a few elements, 20-30 at most for each, I guess.
12415+ */
8b6a4947 12416+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12417+
8b6a4947
AM
12418+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12419+ const void *h_op)
1facf9fc 12420+{
4a4d8108 12421+ struct au_dykey *key, *tmp;
8b6a4947 12422+ struct hlist_bl_node *pos;
1facf9fc 12423+
4a4d8108 12424+ key = NULL;
8b6a4947
AM
12425+ hlist_bl_lock(hbl);
12426+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12427+ if (tmp->dk_op.dy_hop == h_op) {
12428+ key = tmp;
12429+ kref_get(&key->dk_kref);
12430+ break;
12431+ }
8b6a4947 12432+ hlist_bl_unlock(hbl);
4a4d8108
AM
12433+
12434+ return key;
1facf9fc 12435+}
12436+
4a4d8108 12437+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12438+{
4a4d8108
AM
12439+ struct au_dykey **k, *found;
12440+ const void *h_op = key->dk_op.dy_hop;
12441+ int i;
1facf9fc 12442+
4a4d8108
AM
12443+ found = NULL;
12444+ k = br->br_dykey;
12445+ for (i = 0; i < AuBrDynOp; i++)
12446+ if (k[i]) {
12447+ if (k[i]->dk_op.dy_hop == h_op) {
12448+ found = k[i];
12449+ break;
12450+ }
12451+ } else
12452+ break;
12453+ if (!found) {
12454+ spin_lock(&br->br_dykey_lock);
12455+ for (; i < AuBrDynOp; i++)
12456+ if (k[i]) {
12457+ if (k[i]->dk_op.dy_hop == h_op) {
12458+ found = k[i];
12459+ break;
12460+ }
12461+ } else {
12462+ k[i] = key;
12463+ break;
12464+ }
12465+ spin_unlock(&br->br_dykey_lock);
12466+ BUG_ON(i == AuBrDynOp); /* expand the array */
12467+ }
12468+
12469+ return found;
1facf9fc 12470+}
12471+
4a4d8108 12472+/* kref_get() if @key is already added */
8b6a4947 12473+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12474+{
12475+ struct au_dykey *tmp, *found;
8b6a4947 12476+ struct hlist_bl_node *pos;
4a4d8108 12477+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12478+
4a4d8108 12479+ found = NULL;
8b6a4947
AM
12480+ hlist_bl_lock(hbl);
12481+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12482+ if (tmp->dk_op.dy_hop == h_op) {
12483+ kref_get(&tmp->dk_kref);
12484+ found = tmp;
12485+ break;
12486+ }
12487+ if (!found)
8b6a4947
AM
12488+ hlist_bl_add_head(&key->dk_hnode, hbl);
12489+ hlist_bl_unlock(hbl);
1facf9fc 12490+
4a4d8108
AM
12491+ if (!found)
12492+ DyPrSym(key);
12493+ return found;
12494+}
12495+
12496+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12497+{
4a4d8108
AM
12498+ struct au_dykey *key;
12499+
12500+ key = container_of(rcu, struct au_dykey, dk_rcu);
12501+ DyPrSym(key);
1c60b727 12502+ kfree(key);
1facf9fc 12503+}
12504+
4a4d8108
AM
12505+static void dy_free(struct kref *kref)
12506+{
12507+ struct au_dykey *key;
8b6a4947 12508+ struct hlist_bl_head *hbl;
1facf9fc 12509+
4a4d8108 12510+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12511+ hbl = dynop + key->dk_op.dy_type;
12512+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12513+ call_rcu(&key->dk_rcu, dy_free_rcu);
12514+}
12515+
12516+void au_dy_put(struct au_dykey *key)
1facf9fc 12517+{
4a4d8108
AM
12518+ kref_put(&key->dk_kref, dy_free);
12519+}
1facf9fc 12520+
4a4d8108
AM
12521+/* ---------------------------------------------------------------------- */
12522+
12523+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12524+
12525+#ifdef CONFIG_AUFS_DEBUG
12526+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12527+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12528+#else
12529+#define DyDbgDeclare(cnt) do {} while (0)
12530+#define DyDbgInc(cnt) do {} while (0)
12531+#endif
12532+
12533+#define DySet(func, dst, src, h_op, h_sb) do { \
12534+ DyDbgInc(cnt); \
12535+ if (h_op->func) { \
12536+ if (src.func) \
12537+ dst.func = src.func; \
12538+ else \
12539+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12540+ } \
12541+} while (0)
12542+
12543+#define DySetForce(func, dst, src) do { \
12544+ AuDebugOn(!src.func); \
12545+ DyDbgInc(cnt); \
12546+ dst.func = src.func; \
12547+} while (0)
12548+
12549+#define DySetAop(func) \
12550+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12551+#define DySetAopForce(func) \
12552+ DySetForce(func, dyaop->da_op, aufs_aop)
12553+
12554+static void dy_aop(struct au_dykey *key, const void *h_op,
12555+ struct super_block *h_sb __maybe_unused)
12556+{
12557+ struct au_dyaop *dyaop = (void *)key;
12558+ const struct address_space_operations *h_aop = h_op;
12559+ DyDbgDeclare(cnt);
12560+
12561+ AuDbg("%s\n", au_sbtype(h_sb));
12562+
12563+ DySetAop(writepage);
12564+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12565+ DySetAop(writepages);
12566+ DySetAop(set_page_dirty);
12567+ DySetAop(readpages);
12568+ DySetAop(write_begin);
12569+ DySetAop(write_end);
12570+ DySetAop(bmap);
12571+ DySetAop(invalidatepage);
12572+ DySetAop(releasepage);
027c5e7a 12573+ DySetAop(freepage);
7e9cd9fe 12574+ /* this one will be changed according to an aufs mount option */
4a4d8108 12575+ DySetAop(direct_IO);
4a4d8108 12576+ DySetAop(migratepage);
e2f27e51
AM
12577+ DySetAop(isolate_page);
12578+ DySetAop(putback_page);
4a4d8108
AM
12579+ DySetAop(launder_page);
12580+ DySetAop(is_partially_uptodate);
392086de 12581+ DySetAop(is_dirty_writeback);
4a4d8108 12582+ DySetAop(error_remove_page);
b4510431
AM
12583+ DySetAop(swap_activate);
12584+ DySetAop(swap_deactivate);
4a4d8108
AM
12585+
12586+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12587+}
12588+
4a4d8108
AM
12589+/* ---------------------------------------------------------------------- */
12590+
12591+static void dy_bug(struct kref *kref)
12592+{
12593+ BUG();
12594+}
12595+
12596+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12597+{
12598+ struct au_dykey *key, *old;
8b6a4947 12599+ struct hlist_bl_head *hbl;
b752ccd1 12600+ struct op {
4a4d8108 12601+ unsigned int sz;
b752ccd1
AM
12602+ void (*set)(struct au_dykey *key, const void *h_op,
12603+ struct super_block *h_sb __maybe_unused);
12604+ };
12605+ static const struct op a[] = {
4a4d8108
AM
12606+ [AuDy_AOP] = {
12607+ .sz = sizeof(struct au_dyaop),
b752ccd1 12608+ .set = dy_aop
4a4d8108 12609+ }
b752ccd1
AM
12610+ };
12611+ const struct op *p;
4a4d8108 12612+
8b6a4947
AM
12613+ hbl = dynop + op->dy_type;
12614+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12615+ if (key)
12616+ goto out_add; /* success */
12617+
12618+ p = a + op->dy_type;
12619+ key = kzalloc(p->sz, GFP_NOFS);
12620+ if (unlikely(!key)) {
12621+ key = ERR_PTR(-ENOMEM);
12622+ goto out;
12623+ }
12624+
12625+ key->dk_op.dy_hop = op->dy_hop;
12626+ kref_init(&key->dk_kref);
86dc4139 12627+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12628+ old = dy_gadd(hbl, key);
4a4d8108 12629+ if (old) {
1c60b727 12630+ kfree(key);
4a4d8108
AM
12631+ key = old;
12632+ }
12633+
12634+out_add:
12635+ old = dy_bradd(br, key);
12636+ if (old)
12637+ /* its ref-count should never be zero here */
12638+ kref_put(&key->dk_kref, dy_bug);
12639+out:
12640+ return key;
12641+}
12642+
12643+/* ---------------------------------------------------------------------- */
12644+/*
12645+ * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
c1595e42 12646+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12647+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12648+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12649+ * See the aufs manual in detail.
4a4d8108
AM
12650+ */
12651+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12652+{
7e9cd9fe 12653+ if (!do_dx)
4a4d8108 12654+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12655+ else
4a4d8108 12656+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12657+}
12658+
12659+static struct au_dyaop *dy_aget(struct au_branch *br,
12660+ const struct address_space_operations *h_aop,
12661+ int do_dx)
12662+{
12663+ struct au_dyaop *dyaop;
12664+ struct au_dynop op;
12665+
12666+ op.dy_type = AuDy_AOP;
12667+ op.dy_haop = h_aop;
12668+ dyaop = (void *)dy_get(&op, br);
12669+ if (IS_ERR(dyaop))
12670+ goto out;
12671+ dy_adx(dyaop, do_dx);
12672+
12673+out:
12674+ return dyaop;
12675+}
12676+
12677+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12678+ struct inode *h_inode)
12679+{
12680+ int err, do_dx;
12681+ struct super_block *sb;
12682+ struct au_branch *br;
12683+ struct au_dyaop *dyaop;
12684+
12685+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12686+ IiMustWriteLock(inode);
12687+
12688+ sb = inode->i_sb;
12689+ br = au_sbr(sb, bindex);
12690+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12691+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12692+ err = PTR_ERR(dyaop);
12693+ if (IS_ERR(dyaop))
12694+ /* unnecessary to call dy_fput() */
12695+ goto out;
12696+
12697+ err = 0;
12698+ inode->i_mapping->a_ops = &dyaop->da_op;
12699+
12700+out:
12701+ return err;
12702+}
12703+
b752ccd1
AM
12704+/*
12705+ * Is it safe to replace a_ops during the inode/file is in operation?
12706+ * Yes, I hope so.
12707+ */
12708+int au_dy_irefresh(struct inode *inode)
12709+{
12710+ int err;
5afbbe0d 12711+ aufs_bindex_t btop;
b752ccd1
AM
12712+ struct inode *h_inode;
12713+
12714+ err = 0;
12715+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12716+ btop = au_ibtop(inode);
12717+ h_inode = au_h_iptr(inode, btop);
12718+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12719+ }
12720+ return err;
12721+}
12722+
4a4d8108
AM
12723+void au_dy_arefresh(int do_dx)
12724+{
8b6a4947
AM
12725+ struct hlist_bl_head *hbl;
12726+ struct hlist_bl_node *pos;
4a4d8108
AM
12727+ struct au_dykey *key;
12728+
8b6a4947
AM
12729+ hbl = dynop + AuDy_AOP;
12730+ hlist_bl_lock(hbl);
12731+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12732+ dy_adx((void *)key, do_dx);
8b6a4947 12733+ hlist_bl_unlock(hbl);
4a4d8108
AM
12734+}
12735+
4a4d8108
AM
12736+/* ---------------------------------------------------------------------- */
12737+
12738+void __init au_dy_init(void)
12739+{
12740+ int i;
12741+
12742+ /* make sure that 'struct au_dykey *' can be any type */
12743+ BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
4a4d8108
AM
12744+
12745+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12746+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12747+}
12748+
12749+void au_dy_fin(void)
12750+{
12751+ int i;
12752+
12753+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12754+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12755+}
7f207e10
AM
12756diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12757--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
be118d29 12758+++ linux/fs/aufs/dynop.h 2018-04-06 07:48:44.204604724 +0200
7e9cd9fe 12759@@ -0,0 +1,74 @@
4a4d8108 12760+/*
a2654f78 12761+ * Copyright (C) 2010-2017 Junjiro R. Okajima
4a4d8108
AM
12762+ *
12763+ * This program, aufs is free software; you can redistribute it and/or modify
12764+ * it under the terms of the GNU General Public License as published by
12765+ * the Free Software Foundation; either version 2 of the License, or
12766+ * (at your option) any later version.
12767+ *
12768+ * This program is distributed in the hope that it will be useful,
12769+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12770+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12771+ * GNU General Public License for more details.
12772+ *
12773+ * You should have received a copy of the GNU General Public License
523b37e3 12774+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12775+ */
12776+
12777+/*
12778+ * dynamically customizable operations (for regular files only)
12779+ */
12780+
12781+#ifndef __AUFS_DYNOP_H__
12782+#define __AUFS_DYNOP_H__
12783+
12784+#ifdef __KERNEL__
12785+
7e9cd9fe
AM
12786+#include <linux/fs.h>
12787+#include <linux/kref.h>
4a4d8108 12788+
2cbb1c4b 12789+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12790+
12791+struct au_dynop {
12792+ int dy_type;
12793+ union {
12794+ const void *dy_hop;
12795+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12796+ };
12797+};
12798+
12799+struct au_dykey {
12800+ union {
8b6a4947 12801+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12802+ struct rcu_head dk_rcu;
12803+ };
12804+ struct au_dynop dk_op;
12805+
12806+ /*
12807+ * during I am in the branch local array, kref is gotten. when the
12808+ * branch is removed, kref is put.
12809+ */
12810+ struct kref dk_kref;
12811+};
12812+
12813+/* stop unioning since their sizes are very different from each other */
12814+struct au_dyaop {
12815+ struct au_dykey da_key;
12816+ struct address_space_operations da_op; /* not const */
4a4d8108
AM
12817+};
12818+
4a4d8108
AM
12819+/* ---------------------------------------------------------------------- */
12820+
12821+/* dynop.c */
12822+struct au_branch;
12823+void au_dy_put(struct au_dykey *key);
12824+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12825+ struct inode *h_inode);
b752ccd1 12826+int au_dy_irefresh(struct inode *inode);
4a4d8108 12827+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12828+
12829+void __init au_dy_init(void);
12830+void au_dy_fin(void);
12831+
4a4d8108
AM
12832+#endif /* __KERNEL__ */
12833+#endif /* __AUFS_DYNOP_H__ */
7f207e10
AM
12834diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12835--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
be118d29 12836+++ linux/fs/aufs/export.c 2018-04-06 07:48:44.204604724 +0200
f2c43d5f 12837@@ -0,0 +1,836 @@
4a4d8108 12838+/*
a2654f78 12839+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
12840+ *
12841+ * This program, aufs is free software; you can redistribute it and/or modify
12842+ * it under the terms of the GNU General Public License as published by
12843+ * the Free Software Foundation; either version 2 of the License, or
12844+ * (at your option) any later version.
12845+ *
12846+ * This program is distributed in the hope that it will be useful,
12847+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12848+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12849+ * GNU General Public License for more details.
12850+ *
12851+ * You should have received a copy of the GNU General Public License
523b37e3 12852+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12853+ */
12854+
12855+/*
12856+ * export via nfs
12857+ */
12858+
12859+#include <linux/exportfs.h>
7eafdf33 12860+#include <linux/fs_struct.h>
4a4d8108
AM
12861+#include <linux/namei.h>
12862+#include <linux/nsproxy.h>
12863+#include <linux/random.h>
12864+#include <linux/writeback.h>
12865+#include "aufs.h"
12866+
12867+union conv {
12868+#ifdef CONFIG_AUFS_INO_T_64
12869+ __u32 a[2];
12870+#else
12871+ __u32 a[1];
12872+#endif
12873+ ino_t ino;
12874+};
12875+
12876+static ino_t decode_ino(__u32 *a)
12877+{
12878+ union conv u;
12879+
12880+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12881+ u.a[0] = a[0];
12882+#ifdef CONFIG_AUFS_INO_T_64
12883+ u.a[1] = a[1];
12884+#endif
12885+ return u.ino;
12886+}
12887+
12888+static void encode_ino(__u32 *a, ino_t ino)
12889+{
12890+ union conv u;
12891+
12892+ u.ino = ino;
12893+ a[0] = u.a[0];
12894+#ifdef CONFIG_AUFS_INO_T_64
12895+ a[1] = u.a[1];
12896+#endif
12897+}
12898+
12899+/* NFS file handle */
12900+enum {
12901+ Fh_br_id,
12902+ Fh_sigen,
12903+#ifdef CONFIG_AUFS_INO_T_64
12904+ /* support 64bit inode number */
12905+ Fh_ino1,
12906+ Fh_ino2,
12907+ Fh_dir_ino1,
12908+ Fh_dir_ino2,
12909+#else
12910+ Fh_ino1,
12911+ Fh_dir_ino1,
12912+#endif
12913+ Fh_igen,
12914+ Fh_h_type,
12915+ Fh_tail,
12916+
12917+ Fh_ino = Fh_ino1,
12918+ Fh_dir_ino = Fh_dir_ino1
12919+};
12920+
12921+static int au_test_anon(struct dentry *dentry)
12922+{
027c5e7a 12923+ /* note: read d_flags without d_lock */
4a4d8108
AM
12924+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12925+}
12926+
a2a7ad62
AM
12927+int au_test_nfsd(void)
12928+{
12929+ int ret;
12930+ struct task_struct *tsk = current;
12931+ char comm[sizeof(tsk->comm)];
12932+
12933+ ret = 0;
12934+ if (tsk->flags & PF_KTHREAD) {
12935+ get_task_comm(comm, tsk);
12936+ ret = !strcmp(comm, "nfsd");
12937+ }
12938+
12939+ return ret;
12940+}
12941+
4a4d8108
AM
12942+/* ---------------------------------------------------------------------- */
12943+/* inode generation external table */
12944+
b752ccd1 12945+void au_xigen_inc(struct inode *inode)
4a4d8108 12946+{
4a4d8108
AM
12947+ loff_t pos;
12948+ ssize_t sz;
12949+ __u32 igen;
12950+ struct super_block *sb;
12951+ struct au_sbinfo *sbinfo;
12952+
4a4d8108 12953+ sb = inode->i_sb;
b752ccd1 12954+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 12955+
b752ccd1 12956+ sbinfo = au_sbi(sb);
1facf9fc 12957+ pos = inode->i_ino;
12958+ pos *= sizeof(igen);
12959+ igen = inode->i_generation + 1;
1facf9fc 12960+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
12961+ sizeof(igen), &pos);
12962+ if (sz == sizeof(igen))
b752ccd1 12963+ return; /* success */
1facf9fc 12964+
b752ccd1 12965+ if (unlikely(sz >= 0))
1facf9fc 12966+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 12967+}
12968+
12969+int au_xigen_new(struct inode *inode)
12970+{
12971+ int err;
12972+ loff_t pos;
12973+ ssize_t sz;
12974+ struct super_block *sb;
12975+ struct au_sbinfo *sbinfo;
12976+ struct file *file;
12977+
12978+ err = 0;
12979+ /* todo: dirty, at mount time */
12980+ if (inode->i_ino == AUFS_ROOT_INO)
12981+ goto out;
12982+ sb = inode->i_sb;
dece6358 12983+ SiMustAnyLock(sb);
1facf9fc 12984+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12985+ goto out;
12986+
12987+ err = -EFBIG;
12988+ pos = inode->i_ino;
12989+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12990+ AuIOErr1("too large i%lld\n", pos);
12991+ goto out;
12992+ }
12993+ pos *= sizeof(inode->i_generation);
12994+
12995+ err = 0;
12996+ sbinfo = au_sbi(sb);
12997+ file = sbinfo->si_xigen;
12998+ BUG_ON(!file);
12999+
c06a8ce3 13000+ if (vfsub_f_size_read(file)
1facf9fc 13001+ < pos + sizeof(inode->i_generation)) {
13002+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13003+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13004+ sizeof(inode->i_generation), &pos);
13005+ } else
13006+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13007+ sizeof(inode->i_generation), &pos);
13008+ if (sz == sizeof(inode->i_generation))
13009+ goto out; /* success */
13010+
13011+ err = sz;
13012+ if (unlikely(sz >= 0)) {
13013+ err = -EIO;
13014+ AuIOErr("xigen error (%zd)\n", sz);
13015+ }
13016+
4f0767ce 13017+out:
1facf9fc 13018+ return err;
13019+}
13020+
13021+int au_xigen_set(struct super_block *sb, struct file *base)
13022+{
13023+ int err;
13024+ struct au_sbinfo *sbinfo;
13025+ struct file *file;
13026+
dece6358
AM
13027+ SiMustWriteLock(sb);
13028+
1facf9fc 13029+ sbinfo = au_sbi(sb);
13030+ file = au_xino_create2(base, sbinfo->si_xigen);
13031+ err = PTR_ERR(file);
13032+ if (IS_ERR(file))
13033+ goto out;
13034+ err = 0;
13035+ if (sbinfo->si_xigen)
13036+ fput(sbinfo->si_xigen);
13037+ sbinfo->si_xigen = file;
13038+
4f0767ce 13039+out:
1facf9fc 13040+ return err;
13041+}
13042+
13043+void au_xigen_clr(struct super_block *sb)
13044+{
13045+ struct au_sbinfo *sbinfo;
13046+
dece6358
AM
13047+ SiMustWriteLock(sb);
13048+
1facf9fc 13049+ sbinfo = au_sbi(sb);
13050+ if (sbinfo->si_xigen) {
13051+ fput(sbinfo->si_xigen);
13052+ sbinfo->si_xigen = NULL;
13053+ }
13054+}
13055+
13056+/* ---------------------------------------------------------------------- */
13057+
13058+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13059+ ino_t dir_ino)
13060+{
13061+ struct dentry *dentry, *d;
13062+ struct inode *inode;
13063+ unsigned int sigen;
13064+
13065+ dentry = NULL;
13066+ inode = ilookup(sb, ino);
13067+ if (!inode)
13068+ goto out;
13069+
13070+ dentry = ERR_PTR(-ESTALE);
13071+ sigen = au_sigen(sb);
5afbbe0d 13072+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13073+ || IS_DEADDIR(inode)
537831f9 13074+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13075+ goto out_iput;
13076+
13077+ dentry = NULL;
13078+ if (!dir_ino || S_ISDIR(inode->i_mode))
13079+ dentry = d_find_alias(inode);
13080+ else {
027c5e7a 13081+ spin_lock(&inode->i_lock);
c1595e42 13082+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13083+ spin_lock(&d->d_lock);
1facf9fc 13084+ if (!au_test_anon(d)
5527c038 13085+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13086+ dentry = dget_dlock(d);
13087+ spin_unlock(&d->d_lock);
1facf9fc 13088+ break;
13089+ }
027c5e7a
AM
13090+ spin_unlock(&d->d_lock);
13091+ }
13092+ spin_unlock(&inode->i_lock);
1facf9fc 13093+ }
027c5e7a 13094+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13095+ /* need to refresh */
1facf9fc 13096+ dput(dentry);
2cbb1c4b 13097+ dentry = NULL;
1facf9fc 13098+ }
13099+
4f0767ce 13100+out_iput:
1facf9fc 13101+ iput(inode);
4f0767ce 13102+out:
2cbb1c4b 13103+ AuTraceErrPtr(dentry);
1facf9fc 13104+ return dentry;
13105+}
13106+
13107+/* ---------------------------------------------------------------------- */
13108+
13109+/* todo: dirty? */
13110+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13111+
13112+struct au_compare_mnt_args {
13113+ /* input */
13114+ struct super_block *sb;
13115+
13116+ /* output */
13117+ struct vfsmount *mnt;
13118+};
13119+
13120+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13121+{
13122+ struct au_compare_mnt_args *a = arg;
13123+
13124+ if (mnt->mnt_sb != a->sb)
13125+ return 0;
13126+ a->mnt = mntget(mnt);
13127+ return 1;
13128+}
13129+
1facf9fc 13130+static struct vfsmount *au_mnt_get(struct super_block *sb)
13131+{
4a4d8108 13132+ int err;
7eafdf33 13133+ struct path root;
4a4d8108
AM
13134+ struct au_compare_mnt_args args = {
13135+ .sb = sb
13136+ };
1facf9fc 13137+
7eafdf33 13138+ get_fs_root(current->fs, &root);
523b37e3 13139+ rcu_read_lock();
7eafdf33 13140+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13141+ rcu_read_unlock();
7eafdf33 13142+ path_put(&root);
4a4d8108
AM
13143+ AuDebugOn(!err);
13144+ AuDebugOn(!args.mnt);
13145+ return args.mnt;
1facf9fc 13146+}
13147+
13148+struct au_nfsd_si_lock {
4a4d8108 13149+ unsigned int sigen;
027c5e7a 13150+ aufs_bindex_t bindex, br_id;
1facf9fc 13151+ unsigned char force_lock;
13152+};
13153+
027c5e7a
AM
13154+static int si_nfsd_read_lock(struct super_block *sb,
13155+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13156+{
027c5e7a 13157+ int err;
1facf9fc 13158+ aufs_bindex_t bindex;
13159+
13160+ si_read_lock(sb, AuLock_FLUSH);
13161+
13162+ /* branch id may be wrapped around */
027c5e7a 13163+ err = 0;
1facf9fc 13164+ bindex = au_br_index(sb, nsi_lock->br_id);
13165+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13166+ goto out; /* success */
13167+
027c5e7a
AM
13168+ err = -ESTALE;
13169+ bindex = -1;
1facf9fc 13170+ if (!nsi_lock->force_lock)
13171+ si_read_unlock(sb);
1facf9fc 13172+
4f0767ce 13173+out:
027c5e7a
AM
13174+ nsi_lock->bindex = bindex;
13175+ return err;
1facf9fc 13176+}
13177+
13178+struct find_name_by_ino {
392086de 13179+ struct dir_context ctx;
1facf9fc 13180+ int called, found;
13181+ ino_t ino;
13182+ char *name;
13183+ int namelen;
13184+};
13185+
13186+static int
392086de
AM
13187+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13188+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13189+{
392086de
AM
13190+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13191+ ctx);
1facf9fc 13192+
13193+ a->called++;
13194+ if (a->ino != ino)
13195+ return 0;
13196+
13197+ memcpy(a->name, name, namelen);
13198+ a->namelen = namelen;
13199+ a->found = 1;
13200+ return 1;
13201+}
13202+
13203+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13204+ struct au_nfsd_si_lock *nsi_lock)
13205+{
13206+ struct dentry *dentry, *parent;
13207+ struct file *file;
13208+ struct inode *dir;
392086de
AM
13209+ struct find_name_by_ino arg = {
13210+ .ctx = {
2000de60 13211+ .actor = find_name_by_ino
392086de
AM
13212+ }
13213+ };
1facf9fc 13214+ int err;
13215+
13216+ parent = path->dentry;
13217+ if (nsi_lock)
13218+ si_read_unlock(parent->d_sb);
4a4d8108 13219+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13220+ dentry = (void *)file;
13221+ if (IS_ERR(file))
13222+ goto out;
13223+
13224+ dentry = ERR_PTR(-ENOMEM);
537831f9 13225+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13226+ if (unlikely(!arg.name))
13227+ goto out_file;
13228+ arg.ino = ino;
13229+ arg.found = 0;
13230+ do {
13231+ arg.called = 0;
13232+ /* smp_mb(); */
392086de 13233+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13234+ } while (!err && !arg.found && arg.called);
13235+ dentry = ERR_PTR(err);
13236+ if (unlikely(err))
13237+ goto out_name;
1716fcea
AM
13238+ /* instead of ENOENT */
13239+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13240+ if (!arg.found)
13241+ goto out_name;
13242+
b4510431 13243+ /* do not call vfsub_lkup_one() */
5527c038 13244+ dir = d_inode(parent);
febd17d6 13245+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13246+ AuTraceErrPtr(dentry);
13247+ if (IS_ERR(dentry))
13248+ goto out_name;
13249+ AuDebugOn(au_test_anon(dentry));
5527c038 13250+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13251+ dput(dentry);
13252+ dentry = ERR_PTR(-ENOENT);
13253+ }
13254+
4f0767ce 13255+out_name:
1c60b727 13256+ free_page((unsigned long)arg.name);
4f0767ce 13257+out_file:
1facf9fc 13258+ fput(file);
4f0767ce 13259+out:
1facf9fc 13260+ if (unlikely(nsi_lock
13261+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13262+ if (!IS_ERR(dentry)) {
13263+ dput(dentry);
13264+ dentry = ERR_PTR(-ESTALE);
13265+ }
13266+ AuTraceErrPtr(dentry);
13267+ return dentry;
13268+}
13269+
13270+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13271+ ino_t dir_ino,
13272+ struct au_nfsd_si_lock *nsi_lock)
13273+{
13274+ struct dentry *dentry;
13275+ struct path path;
13276+
13277+ if (dir_ino != AUFS_ROOT_INO) {
13278+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13279+ dentry = path.dentry;
13280+ if (!path.dentry || IS_ERR(path.dentry))
13281+ goto out;
13282+ AuDebugOn(au_test_anon(path.dentry));
13283+ } else
13284+ path.dentry = dget(sb->s_root);
13285+
13286+ path.mnt = au_mnt_get(sb);
13287+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13288+ path_put(&path);
13289+
4f0767ce 13290+out:
1facf9fc 13291+ AuTraceErrPtr(dentry);
13292+ return dentry;
13293+}
13294+
13295+/* ---------------------------------------------------------------------- */
13296+
13297+static int h_acceptable(void *expv, struct dentry *dentry)
13298+{
13299+ return 1;
13300+}
13301+
13302+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13303+ char *buf, int len, struct super_block *sb)
13304+{
13305+ char *p;
13306+ int n;
13307+ struct path path;
13308+
13309+ p = d_path(h_rootpath, buf, len);
13310+ if (IS_ERR(p))
13311+ goto out;
13312+ n = strlen(p);
13313+
13314+ path.mnt = h_rootpath->mnt;
13315+ path.dentry = h_parent;
13316+ p = d_path(&path, buf, len);
13317+ if (IS_ERR(p))
13318+ goto out;
13319+ if (n != 1)
13320+ p += n;
13321+
13322+ path.mnt = au_mnt_get(sb);
13323+ path.dentry = sb->s_root;
13324+ p = d_path(&path, buf, len - strlen(p));
13325+ mntput(path.mnt);
13326+ if (IS_ERR(p))
13327+ goto out;
13328+ if (n != 1)
13329+ p[strlen(p)] = '/';
13330+
4f0767ce 13331+out:
1facf9fc 13332+ AuTraceErrPtr(p);
13333+ return p;
13334+}
13335+
13336+static
027c5e7a
AM
13337+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13338+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13339+{
13340+ struct dentry *dentry, *h_parent, *root;
13341+ struct super_block *h_sb;
13342+ char *pathname, *p;
13343+ struct vfsmount *h_mnt;
13344+ struct au_branch *br;
13345+ int err;
13346+ struct path path;
13347+
027c5e7a 13348+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13349+ h_mnt = au_br_mnt(br);
1facf9fc 13350+ h_sb = h_mnt->mnt_sb;
13351+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13352+ lockdep_off();
1facf9fc 13353+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13354+ fh_len - Fh_tail, fh[Fh_h_type],
13355+ h_acceptable, /*context*/NULL);
5afbbe0d 13356+ lockdep_on();
1facf9fc 13357+ dentry = h_parent;
13358+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13359+ AuWarn1("%s decode_fh failed, %ld\n",
13360+ au_sbtype(h_sb), PTR_ERR(h_parent));
13361+ goto out;
13362+ }
13363+ dentry = NULL;
13364+ if (unlikely(au_test_anon(h_parent))) {
13365+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13366+ au_sbtype(h_sb));
13367+ goto out_h_parent;
13368+ }
13369+
13370+ dentry = ERR_PTR(-ENOMEM);
13371+ pathname = (void *)__get_free_page(GFP_NOFS);
13372+ if (unlikely(!pathname))
13373+ goto out_h_parent;
13374+
13375+ root = sb->s_root;
13376+ path.mnt = h_mnt;
13377+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13378+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13379+ di_read_unlock(root, !AuLock_IR);
13380+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13381+ dentry = (void *)p;
13382+ if (IS_ERR(p))
13383+ goto out_pathname;
13384+
13385+ si_read_unlock(sb);
13386+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13387+ dentry = ERR_PTR(err);
13388+ if (unlikely(err))
13389+ goto out_relock;
13390+
13391+ dentry = ERR_PTR(-ENOENT);
13392+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13393+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13394+ goto out_path;
13395+
5527c038 13396+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13397+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13398+ else
13399+ dentry = dget(path.dentry);
13400+
4f0767ce 13401+out_path:
1facf9fc 13402+ path_put(&path);
4f0767ce 13403+out_relock:
1facf9fc 13404+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13405+ if (!IS_ERR(dentry)) {
13406+ dput(dentry);
13407+ dentry = ERR_PTR(-ESTALE);
13408+ }
4f0767ce 13409+out_pathname:
1c60b727 13410+ free_page((unsigned long)pathname);
4f0767ce 13411+out_h_parent:
1facf9fc 13412+ dput(h_parent);
4f0767ce 13413+out:
1facf9fc 13414+ AuTraceErrPtr(dentry);
13415+ return dentry;
13416+}
13417+
13418+/* ---------------------------------------------------------------------- */
13419+
13420+static struct dentry *
13421+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13422+ int fh_type)
13423+{
13424+ struct dentry *dentry;
13425+ __u32 *fh = fid->raw;
027c5e7a 13426+ struct au_branch *br;
1facf9fc 13427+ ino_t ino, dir_ino;
1facf9fc 13428+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13429+ .force_lock = 0
13430+ };
13431+
1facf9fc 13432+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13433+ /* it should never happen, but the file handle is unreliable */
13434+ if (unlikely(fh_len < Fh_tail))
13435+ goto out;
13436+ nsi_lock.sigen = fh[Fh_sigen];
13437+ nsi_lock.br_id = fh[Fh_br_id];
13438+
1facf9fc 13439+ /* branch id may be wrapped around */
027c5e7a
AM
13440+ br = NULL;
13441+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13442+ goto out;
13443+ nsi_lock.force_lock = 1;
13444+
13445+ /* is this inode still cached? */
13446+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13447+ /* it should never happen */
13448+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13449+ goto out_unlock;
4a4d8108 13450+
1facf9fc 13451+ dir_ino = decode_ino(fh + Fh_dir_ino);
13452+ dentry = decode_by_ino(sb, ino, dir_ino);
13453+ if (IS_ERR(dentry))
13454+ goto out_unlock;
13455+ if (dentry)
13456+ goto accept;
13457+
13458+ /* is the parent dir cached? */
027c5e7a 13459+ br = au_sbr(sb, nsi_lock.bindex);
5afbbe0d 13460+ au_br_get(br);
1facf9fc 13461+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13462+ if (IS_ERR(dentry))
13463+ goto out_unlock;
13464+ if (dentry)
13465+ goto accept;
13466+
13467+ /* lookup path */
027c5e7a 13468+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13469+ if (IS_ERR(dentry))
13470+ goto out_unlock;
13471+ if (unlikely(!dentry))
13472+ /* todo?: make it ESTALE */
13473+ goto out_unlock;
13474+
4f0767ce 13475+accept:
027c5e7a 13476+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13477+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13478+ goto out_unlock; /* success */
13479+
13480+ dput(dentry);
13481+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13482+out_unlock:
027c5e7a 13483+ if (br)
5afbbe0d 13484+ au_br_put(br);
1facf9fc 13485+ si_read_unlock(sb);
4f0767ce 13486+out:
1facf9fc 13487+ AuTraceErrPtr(dentry);
13488+ return dentry;
13489+}
13490+
13491+#if 0 /* reserved for future use */
13492+/* support subtreecheck option */
13493+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13494+ int fh_len, int fh_type)
13495+{
13496+ struct dentry *parent;
13497+ __u32 *fh = fid->raw;
13498+ ino_t dir_ino;
13499+
13500+ dir_ino = decode_ino(fh + Fh_dir_ino);
13501+ parent = decode_by_ino(sb, dir_ino, 0);
13502+ if (IS_ERR(parent))
13503+ goto out;
13504+ if (!parent)
13505+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13506+ dir_ino, fh, fh_len);
13507+
4f0767ce 13508+out:
1facf9fc 13509+ AuTraceErrPtr(parent);
13510+ return parent;
13511+}
13512+#endif
13513+
13514+/* ---------------------------------------------------------------------- */
13515+
0c3ec466
AM
13516+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13517+ struct inode *dir)
1facf9fc 13518+{
13519+ int err;
0c3ec466 13520+ aufs_bindex_t bindex;
1facf9fc 13521+ struct super_block *sb, *h_sb;
0c3ec466
AM
13522+ struct dentry *dentry, *parent, *h_parent;
13523+ struct inode *h_dir;
1facf9fc 13524+ struct au_branch *br;
13525+
1facf9fc 13526+ err = -ENOSPC;
13527+ if (unlikely(*max_len <= Fh_tail)) {
13528+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13529+ goto out;
13530+ }
13531+
13532+ err = FILEID_ROOT;
0c3ec466
AM
13533+ if (inode->i_ino == AUFS_ROOT_INO) {
13534+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13535+ goto out;
13536+ }
13537+
1facf9fc 13538+ h_parent = NULL;
0c3ec466
AM
13539+ sb = inode->i_sb;
13540+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13541+ if (unlikely(err))
13542+ goto out;
13543+
1facf9fc 13544+#ifdef CONFIG_AUFS_DEBUG
13545+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13546+ AuWarn1("NFS-exporting requires xino\n");
13547+#endif
027c5e7a 13548+ err = -EIO;
0c3ec466
AM
13549+ parent = NULL;
13550+ ii_read_lock_child(inode);
5afbbe0d 13551+ bindex = au_ibtop(inode);
0c3ec466 13552+ if (!dir) {
c1595e42 13553+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13554+ if (unlikely(!dentry))
13555+ goto out_unlock;
13556+ AuDebugOn(au_test_anon(dentry));
13557+ parent = dget_parent(dentry);
13558+ dput(dentry);
13559+ if (unlikely(!parent))
13560+ goto out_unlock;
5527c038
JR
13561+ if (d_really_is_positive(parent))
13562+ dir = d_inode(parent);
1facf9fc 13563+ }
0c3ec466
AM
13564+
13565+ ii_read_lock_parent(dir);
13566+ h_dir = au_h_iptr(dir, bindex);
13567+ ii_read_unlock(dir);
13568+ if (unlikely(!h_dir))
13569+ goto out_parent;
c1595e42 13570+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13571+ if (unlikely(!h_parent))
0c3ec466 13572+ goto out_hparent;
1facf9fc 13573+
13574+ err = -EPERM;
13575+ br = au_sbr(sb, bindex);
86dc4139 13576+ h_sb = au_br_sb(br);
1facf9fc 13577+ if (unlikely(!h_sb->s_export_op)) {
13578+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13579+ goto out_hparent;
1facf9fc 13580+ }
13581+
13582+ fh[Fh_br_id] = br->br_id;
13583+ fh[Fh_sigen] = au_sigen(sb);
13584+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13585+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13586+ fh[Fh_igen] = inode->i_generation;
13587+
13588+ *max_len -= Fh_tail;
13589+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13590+ max_len,
13591+ /*connectable or subtreecheck*/0);
13592+ err = fh[Fh_h_type];
13593+ *max_len += Fh_tail;
13594+ /* todo: macros? */
1716fcea 13595+ if (err != FILEID_INVALID)
1facf9fc 13596+ err = 99;
13597+ else
13598+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13599+
0c3ec466 13600+out_hparent:
1facf9fc 13601+ dput(h_parent);
0c3ec466 13602+out_parent:
1facf9fc 13603+ dput(parent);
0c3ec466
AM
13604+out_unlock:
13605+ ii_read_unlock(inode);
13606+ si_read_unlock(sb);
4f0767ce 13607+out:
1facf9fc 13608+ if (unlikely(err < 0))
1716fcea 13609+ err = FILEID_INVALID;
1facf9fc 13610+ return err;
13611+}
13612+
13613+/* ---------------------------------------------------------------------- */
13614+
4a4d8108
AM
13615+static int aufs_commit_metadata(struct inode *inode)
13616+{
13617+ int err;
13618+ aufs_bindex_t bindex;
13619+ struct super_block *sb;
13620+ struct inode *h_inode;
13621+ int (*f)(struct inode *inode);
13622+
13623+ sb = inode->i_sb;
e49829fe 13624+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13625+ ii_write_lock_child(inode);
5afbbe0d 13626+ bindex = au_ibtop(inode);
4a4d8108
AM
13627+ AuDebugOn(bindex < 0);
13628+ h_inode = au_h_iptr(inode, bindex);
13629+
13630+ f = h_inode->i_sb->s_export_op->commit_metadata;
13631+ if (f)
13632+ err = f(h_inode);
13633+ else {
13634+ struct writeback_control wbc = {
13635+ .sync_mode = WB_SYNC_ALL,
13636+ .nr_to_write = 0 /* metadata only */
13637+ };
13638+
13639+ err = sync_inode(h_inode, &wbc);
13640+ }
13641+
13642+ au_cpup_attr_timesizes(inode);
13643+ ii_write_unlock(inode);
13644+ si_read_unlock(sb);
13645+ return err;
13646+}
13647+
13648+/* ---------------------------------------------------------------------- */
13649+
1facf9fc 13650+static struct export_operations aufs_export_op = {
4a4d8108 13651+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13652+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13653+ .encode_fh = aufs_encode_fh,
13654+ .commit_metadata = aufs_commit_metadata
1facf9fc 13655+};
13656+
13657+void au_export_init(struct super_block *sb)
13658+{
13659+ struct au_sbinfo *sbinfo;
13660+ __u32 u;
13661+
5afbbe0d
AM
13662+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13663+ && IS_MODULE(CONFIG_EXPORTFS),
13664+ AUFS_NAME ": unsupported configuration "
13665+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13666+
1facf9fc 13667+ sb->s_export_op = &aufs_export_op;
13668+ sbinfo = au_sbi(sb);
13669+ sbinfo->si_xigen = NULL;
13670+ get_random_bytes(&u, sizeof(u));
13671+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13672+ atomic_set(&sbinfo->si_xigen_next, u);
13673+}
076b876e
AM
13674diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13675--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
be118d29
JR
13676+++ linux/fs/aufs/fhsm.c 2018-04-06 07:48:44.204604724 +0200
13677@@ -0,0 +1,425 @@
076b876e 13678+/*
a2654f78 13679+ * Copyright (C) 2011-2017 Junjiro R. Okajima
076b876e
AM
13680+ *
13681+ * This program, aufs is free software; you can redistribute it and/or modify
13682+ * it under the terms of the GNU General Public License as published by
13683+ * the Free Software Foundation; either version 2 of the License, or
13684+ * (at your option) any later version.
13685+ *
13686+ * This program is distributed in the hope that it will be useful,
13687+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13688+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13689+ * GNU General Public License for more details.
13690+ *
13691+ * You should have received a copy of the GNU General Public License
13692+ * along with this program; if not, write to the Free Software
13693+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13694+ */
13695+
13696+/*
13697+ * File-based Hierarchy Storage Management
13698+ */
13699+
13700+#include <linux/anon_inodes.h>
13701+#include <linux/poll.h>
13702+#include <linux/seq_file.h>
13703+#include <linux/statfs.h>
13704+#include "aufs.h"
13705+
c1595e42
JR
13706+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13707+{
13708+ struct au_sbinfo *sbinfo;
13709+ struct au_fhsm *fhsm;
13710+
13711+ SiMustAnyLock(sb);
13712+
13713+ sbinfo = au_sbi(sb);
13714+ fhsm = &sbinfo->si_fhsm;
13715+ AuDebugOn(!fhsm);
13716+ return fhsm->fhsm_bottom;
13717+}
13718+
13719+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13720+{
13721+ struct au_sbinfo *sbinfo;
13722+ struct au_fhsm *fhsm;
13723+
13724+ SiMustWriteLock(sb);
13725+
13726+ sbinfo = au_sbi(sb);
13727+ fhsm = &sbinfo->si_fhsm;
13728+ AuDebugOn(!fhsm);
13729+ fhsm->fhsm_bottom = bindex;
13730+}
13731+
13732+/* ---------------------------------------------------------------------- */
13733+
076b876e
AM
13734+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13735+{
13736+ struct au_br_fhsm *bf;
13737+
13738+ bf = br->br_fhsm;
13739+ MtxMustLock(&bf->bf_lock);
13740+
13741+ return !bf->bf_readable
13742+ || time_after(jiffies,
13743+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13744+}
13745+
13746+/* ---------------------------------------------------------------------- */
13747+
13748+static void au_fhsm_notify(struct super_block *sb, int val)
13749+{
13750+ struct au_sbinfo *sbinfo;
13751+ struct au_fhsm *fhsm;
13752+
13753+ SiMustAnyLock(sb);
13754+
13755+ sbinfo = au_sbi(sb);
13756+ fhsm = &sbinfo->si_fhsm;
13757+ if (au_fhsm_pid(fhsm)
13758+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13759+ atomic_set(&fhsm->fhsm_readable, val);
13760+ if (val)
13761+ wake_up(&fhsm->fhsm_wqh);
13762+ }
13763+}
13764+
13765+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13766+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13767+{
13768+ int err;
13769+ struct au_branch *br;
13770+ struct au_br_fhsm *bf;
13771+
13772+ br = au_sbr(sb, bindex);
13773+ AuDebugOn(au_br_rdonly(br));
13774+ bf = br->br_fhsm;
13775+ AuDebugOn(!bf);
13776+
13777+ if (do_lock)
13778+ mutex_lock(&bf->bf_lock);
13779+ else
13780+ MtxMustLock(&bf->bf_lock);
13781+
13782+ /* sb->s_root for NFS is unreliable */
13783+ err = au_br_stfs(br, &bf->bf_stfs);
13784+ if (unlikely(err)) {
13785+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13786+ goto out;
13787+ }
13788+
13789+ bf->bf_jiffy = jiffies;
13790+ bf->bf_readable = 1;
13791+ if (do_notify)
13792+ au_fhsm_notify(sb, /*val*/1);
13793+ if (rstfs)
13794+ *rstfs = bf->bf_stfs;
13795+
13796+out:
13797+ if (do_lock)
13798+ mutex_unlock(&bf->bf_lock);
13799+ au_fhsm_notify(sb, /*val*/1);
13800+
13801+ return err;
13802+}
13803+
13804+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13805+{
13806+ int err;
076b876e
AM
13807+ struct au_sbinfo *sbinfo;
13808+ struct au_fhsm *fhsm;
13809+ struct au_branch *br;
13810+ struct au_br_fhsm *bf;
13811+
13812+ AuDbg("b%d, force %d\n", bindex, force);
13813+ SiMustAnyLock(sb);
13814+
13815+ sbinfo = au_sbi(sb);
13816+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13817+ if (!au_ftest_si(sbinfo, FHSM)
13818+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13819+ return;
13820+
13821+ br = au_sbr(sb, bindex);
13822+ bf = br->br_fhsm;
13823+ AuDebugOn(!bf);
13824+ mutex_lock(&bf->bf_lock);
13825+ if (force
13826+ || au_fhsm_pid(fhsm)
13827+ || au_fhsm_test_jiffy(sbinfo, br))
13828+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13829+ /*do_notify*/1);
13830+ mutex_unlock(&bf->bf_lock);
13831+}
13832+
13833+void au_fhsm_wrote_all(struct super_block *sb, int force)
13834+{
5afbbe0d 13835+ aufs_bindex_t bindex, bbot;
076b876e
AM
13836+ struct au_branch *br;
13837+
13838+ /* exclude the bottom */
5afbbe0d
AM
13839+ bbot = au_fhsm_bottom(sb);
13840+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13841+ br = au_sbr(sb, bindex);
13842+ if (au_br_fhsm(br->br_perm))
13843+ au_fhsm_wrote(sb, bindex, force);
13844+ }
13845+}
13846+
13847+/* ---------------------------------------------------------------------- */
13848+
be118d29 13849+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13850+{
be118d29 13851+ __poll_t mask;
076b876e
AM
13852+ struct au_sbinfo *sbinfo;
13853+ struct au_fhsm *fhsm;
13854+
13855+ mask = 0;
13856+ sbinfo = file->private_data;
13857+ fhsm = &sbinfo->si_fhsm;
13858+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13859+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13860+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13861+
be118d29 13862+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13863+ return mask;
13864+}
13865+
13866+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13867+ struct aufs_stfs *stfs, __s16 brid)
13868+{
13869+ int err;
13870+
13871+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13872+ if (!err)
13873+ err = __put_user(brid, &stbr->brid);
13874+ if (unlikely(err))
13875+ err = -EFAULT;
13876+
13877+ return err;
13878+}
13879+
13880+static ssize_t au_fhsm_do_read(struct super_block *sb,
13881+ struct aufs_stbr __user *stbr, size_t count)
13882+{
13883+ ssize_t err;
13884+ int nstbr;
5afbbe0d 13885+ aufs_bindex_t bindex, bbot;
076b876e
AM
13886+ struct au_branch *br;
13887+ struct au_br_fhsm *bf;
13888+
13889+ /* except the bottom branch */
13890+ err = 0;
13891+ nstbr = 0;
5afbbe0d
AM
13892+ bbot = au_fhsm_bottom(sb);
13893+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
13894+ br = au_sbr(sb, bindex);
13895+ if (!au_br_fhsm(br->br_perm))
13896+ continue;
13897+
13898+ bf = br->br_fhsm;
13899+ mutex_lock(&bf->bf_lock);
13900+ if (bf->bf_readable) {
13901+ err = -EFAULT;
13902+ if (count >= sizeof(*stbr))
13903+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13904+ br->br_id);
13905+ if (!err) {
13906+ bf->bf_readable = 0;
13907+ count -= sizeof(*stbr);
13908+ nstbr++;
13909+ }
13910+ }
13911+ mutex_unlock(&bf->bf_lock);
13912+ }
13913+ if (!err)
13914+ err = sizeof(*stbr) * nstbr;
13915+
13916+ return err;
13917+}
13918+
13919+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13920+ loff_t *pos)
13921+{
13922+ ssize_t err;
13923+ int readable;
5afbbe0d 13924+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
13925+ struct au_sbinfo *sbinfo;
13926+ struct au_fhsm *fhsm;
13927+ struct au_branch *br;
13928+ struct super_block *sb;
13929+
13930+ err = 0;
13931+ sbinfo = file->private_data;
13932+ fhsm = &sbinfo->si_fhsm;
13933+need_data:
13934+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
13935+ if (!atomic_read(&fhsm->fhsm_readable)) {
13936+ if (vfsub_file_flags(file) & O_NONBLOCK)
13937+ err = -EAGAIN;
13938+ else
13939+ err = wait_event_interruptible_locked_irq
13940+ (fhsm->fhsm_wqh,
13941+ atomic_read(&fhsm->fhsm_readable));
13942+ }
13943+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13944+ if (unlikely(err))
13945+ goto out;
13946+
13947+ /* sb may already be dead */
13948+ au_rw_read_lock(&sbinfo->si_rwsem);
13949+ readable = atomic_read(&fhsm->fhsm_readable);
13950+ if (readable > 0) {
13951+ sb = sbinfo->si_sb;
13952+ AuDebugOn(!sb);
13953+ /* exclude the bottom branch */
13954+ nfhsm = 0;
5afbbe0d
AM
13955+ bbot = au_fhsm_bottom(sb);
13956+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13957+ br = au_sbr(sb, bindex);
13958+ if (au_br_fhsm(br->br_perm))
13959+ nfhsm++;
13960+ }
13961+ err = -EMSGSIZE;
13962+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13963+ atomic_set(&fhsm->fhsm_readable, 0);
13964+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13965+ count);
13966+ }
13967+ }
13968+ au_rw_read_unlock(&sbinfo->si_rwsem);
13969+ if (!readable)
13970+ goto need_data;
13971+
13972+out:
13973+ return err;
13974+}
13975+
13976+static int au_fhsm_release(struct inode *inode, struct file *file)
13977+{
13978+ struct au_sbinfo *sbinfo;
13979+ struct au_fhsm *fhsm;
13980+
13981+ /* sb may already be dead */
13982+ sbinfo = file->private_data;
13983+ fhsm = &sbinfo->si_fhsm;
13984+ spin_lock(&fhsm->fhsm_spin);
13985+ fhsm->fhsm_pid = 0;
13986+ spin_unlock(&fhsm->fhsm_spin);
13987+ kobject_put(&sbinfo->si_kobj);
13988+
13989+ return 0;
13990+}
13991+
13992+static const struct file_operations au_fhsm_fops = {
13993+ .owner = THIS_MODULE,
13994+ .llseek = noop_llseek,
13995+ .read = au_fhsm_read,
13996+ .poll = au_fhsm_poll,
13997+ .release = au_fhsm_release
13998+};
13999+
14000+int au_fhsm_fd(struct super_block *sb, int oflags)
14001+{
14002+ int err, fd;
14003+ struct au_sbinfo *sbinfo;
14004+ struct au_fhsm *fhsm;
14005+
14006+ err = -EPERM;
14007+ if (unlikely(!capable(CAP_SYS_ADMIN)))
14008+ goto out;
14009+
14010+ err = -EINVAL;
14011+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14012+ goto out;
14013+
14014+ err = 0;
14015+ sbinfo = au_sbi(sb);
14016+ fhsm = &sbinfo->si_fhsm;
14017+ spin_lock(&fhsm->fhsm_spin);
14018+ if (!fhsm->fhsm_pid)
14019+ fhsm->fhsm_pid = current->pid;
14020+ else
14021+ err = -EBUSY;
14022+ spin_unlock(&fhsm->fhsm_spin);
14023+ if (unlikely(err))
14024+ goto out;
14025+
14026+ oflags |= O_RDONLY;
14027+ /* oflags |= FMODE_NONOTIFY; */
14028+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14029+ err = fd;
14030+ if (unlikely(fd < 0))
14031+ goto out_pid;
14032+
14033+ /* succeed reglardless 'fhsm' status */
14034+ kobject_get(&sbinfo->si_kobj);
14035+ si_noflush_read_lock(sb);
14036+ if (au_ftest_si(sbinfo, FHSM))
14037+ au_fhsm_wrote_all(sb, /*force*/0);
14038+ si_read_unlock(sb);
14039+ goto out; /* success */
14040+
14041+out_pid:
14042+ spin_lock(&fhsm->fhsm_spin);
14043+ fhsm->fhsm_pid = 0;
14044+ spin_unlock(&fhsm->fhsm_spin);
14045+out:
14046+ AuTraceErr(err);
14047+ return err;
14048+}
14049+
14050+/* ---------------------------------------------------------------------- */
14051+
14052+int au_fhsm_br_alloc(struct au_branch *br)
14053+{
14054+ int err;
14055+
14056+ err = 0;
14057+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14058+ if (br->br_fhsm)
14059+ au_br_fhsm_init(br->br_fhsm);
14060+ else
14061+ err = -ENOMEM;
14062+
14063+ return err;
14064+}
14065+
14066+/* ---------------------------------------------------------------------- */
14067+
14068+void au_fhsm_fin(struct super_block *sb)
14069+{
14070+ au_fhsm_notify(sb, /*val*/-1);
14071+}
14072+
14073+void au_fhsm_init(struct au_sbinfo *sbinfo)
14074+{
14075+ struct au_fhsm *fhsm;
14076+
14077+ fhsm = &sbinfo->si_fhsm;
14078+ spin_lock_init(&fhsm->fhsm_spin);
14079+ init_waitqueue_head(&fhsm->fhsm_wqh);
14080+ atomic_set(&fhsm->fhsm_readable, 0);
14081+ fhsm->fhsm_expire
14082+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14083+ fhsm->fhsm_bottom = -1;
076b876e
AM
14084+}
14085+
14086+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14087+{
14088+ sbinfo->si_fhsm.fhsm_expire
14089+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14090+}
14091+
14092+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14093+{
14094+ unsigned int u;
14095+
14096+ if (!au_ftest_si(sbinfo, FHSM))
14097+ return;
14098+
14099+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14100+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14101+ seq_printf(seq, ",fhsm_sec=%u", u);
14102+}
7f207e10
AM
14103diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14104--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
be118d29 14105+++ linux/fs/aufs/file.c 2018-04-06 07:48:44.204604724 +0200
8b6a4947 14106@@ -0,0 +1,856 @@
1facf9fc 14107+/*
a2654f78 14108+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 14109+ *
14110+ * This program, aufs is free software; you can redistribute it and/or modify
14111+ * it under the terms of the GNU General Public License as published by
14112+ * the Free Software Foundation; either version 2 of the License, or
14113+ * (at your option) any later version.
dece6358
AM
14114+ *
14115+ * This program is distributed in the hope that it will be useful,
14116+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14117+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14118+ * GNU General Public License for more details.
14119+ *
14120+ * You should have received a copy of the GNU General Public License
523b37e3 14121+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14122+ */
14123+
14124+/*
4a4d8108 14125+ * handling file/dir, and address_space operation
1facf9fc 14126+ */
14127+
7eafdf33
AM
14128+#ifdef CONFIG_AUFS_DEBUG
14129+#include <linux/migrate.h>
14130+#endif
4a4d8108 14131+#include <linux/pagemap.h>
1facf9fc 14132+#include "aufs.h"
14133+
4a4d8108
AM
14134+/* drop flags for writing */
14135+unsigned int au_file_roflags(unsigned int flags)
14136+{
14137+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14138+ flags |= O_RDONLY | O_NOATIME;
14139+ return flags;
14140+}
14141+
14142+/* common functions to regular file and dir */
14143+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14144+ struct file *file, int force_wr)
1facf9fc 14145+{
1308ab2a 14146+ struct file *h_file;
4a4d8108
AM
14147+ struct dentry *h_dentry;
14148+ struct inode *h_inode;
14149+ struct super_block *sb;
14150+ struct au_branch *br;
14151+ struct path h_path;
b912730e 14152+ int err;
1facf9fc 14153+
4a4d8108
AM
14154+ /* a race condition can happen between open and unlink/rmdir */
14155+ h_file = ERR_PTR(-ENOENT);
14156+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14157+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14158+ goto out;
5527c038 14159+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14160+ spin_lock(&h_dentry->d_lock);
14161+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14162+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14163+ ;
14164+ spin_unlock(&h_dentry->d_lock);
14165+ if (unlikely(err))
4a4d8108 14166+ goto out;
1facf9fc 14167+
4a4d8108
AM
14168+ sb = dentry->d_sb;
14169+ br = au_sbr(sb, bindex);
b912730e
AM
14170+ err = au_br_test_oflag(flags, br);
14171+ h_file = ERR_PTR(err);
14172+ if (unlikely(err))
027c5e7a 14173+ goto out;
1facf9fc 14174+
4a4d8108 14175+ /* drop flags for writing */
5527c038 14176+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14177+ if (force_wr && !(flags & O_WRONLY))
14178+ force_wr = 0;
4a4d8108 14179+ flags = au_file_roflags(flags);
392086de
AM
14180+ if (force_wr) {
14181+ h_file = ERR_PTR(-EROFS);
14182+ flags = au_file_roflags(flags);
14183+ if (unlikely(vfsub_native_ro(h_inode)
14184+ || IS_APPEND(h_inode)))
14185+ goto out;
14186+ flags &= ~O_ACCMODE;
14187+ flags |= O_WRONLY;
14188+ }
14189+ }
4a4d8108 14190+ flags &= ~O_CREAT;
5afbbe0d 14191+ au_br_get(br);
4a4d8108 14192+ h_path.dentry = h_dentry;
86dc4139 14193+ h_path.mnt = au_br_mnt(br);
38d290e6 14194+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14195+ if (IS_ERR(h_file))
14196+ goto out_br;
dece6358 14197+
b912730e 14198+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14199+ err = deny_write_access(h_file);
14200+ if (unlikely(err)) {
14201+ fput(h_file);
14202+ h_file = ERR_PTR(err);
14203+ goto out_br;
14204+ }
14205+ }
953406b4 14206+ fsnotify_open(h_file);
4a4d8108 14207+ goto out; /* success */
1facf9fc 14208+
4f0767ce 14209+out_br:
5afbbe0d 14210+ au_br_put(br);
4f0767ce 14211+out:
4a4d8108
AM
14212+ return h_file;
14213+}
1308ab2a 14214+
076b876e
AM
14215+static int au_cmoo(struct dentry *dentry)
14216+{
8b6a4947 14217+ int err, cmoo, matched;
076b876e
AM
14218+ unsigned int udba;
14219+ struct path h_path;
14220+ struct au_pin pin;
14221+ struct au_cp_generic cpg = {
14222+ .dentry = dentry,
14223+ .bdst = -1,
14224+ .bsrc = -1,
14225+ .len = -1,
14226+ .pin = &pin,
14227+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14228+ };
7e9cd9fe 14229+ struct inode *delegated;
076b876e
AM
14230+ struct super_block *sb;
14231+ struct au_sbinfo *sbinfo;
14232+ struct au_fhsm *fhsm;
14233+ pid_t pid;
14234+ struct au_branch *br;
14235+ struct dentry *parent;
14236+ struct au_hinode *hdir;
14237+
14238+ DiMustWriteLock(dentry);
5527c038 14239+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14240+
14241+ err = 0;
14242+ if (IS_ROOT(dentry))
14243+ goto out;
5afbbe0d 14244+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14245+ if (!cpg.bsrc)
14246+ goto out;
14247+
14248+ sb = dentry->d_sb;
14249+ sbinfo = au_sbi(sb);
14250+ fhsm = &sbinfo->si_fhsm;
14251+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14252+ rcu_read_lock();
14253+ matched = (pid
14254+ && (current->pid == pid
14255+ || rcu_dereference(current->real_parent)->pid == pid));
14256+ rcu_read_unlock();
14257+ if (matched)
076b876e
AM
14258+ goto out;
14259+
14260+ br = au_sbr(sb, cpg.bsrc);
14261+ cmoo = au_br_cmoo(br->br_perm);
14262+ if (!cmoo)
14263+ goto out;
7e9cd9fe 14264+ if (!d_is_reg(dentry))
076b876e
AM
14265+ cmoo &= AuBrAttr_COO_ALL;
14266+ if (!cmoo)
14267+ goto out;
14268+
14269+ parent = dget_parent(dentry);
14270+ di_write_lock_parent(parent);
14271+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14272+ cpg.bdst = err;
14273+ if (unlikely(err < 0)) {
14274+ err = 0; /* there is no upper writable branch */
14275+ goto out_dgrade;
14276+ }
14277+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14278+
14279+ /* do not respect the coo attrib for the target branch */
14280+ err = au_cpup_dirs(dentry, cpg.bdst);
14281+ if (unlikely(err))
14282+ goto out_dgrade;
14283+
14284+ di_downgrade_lock(parent, AuLock_IR);
14285+ udba = au_opt_udba(sb);
14286+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14287+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14288+ if (unlikely(err))
14289+ goto out_parent;
14290+
14291+ err = au_sio_cpup_simple(&cpg);
14292+ au_unpin(&pin);
14293+ if (unlikely(err))
14294+ goto out_parent;
14295+ if (!(cmoo & AuBrWAttr_MOO))
14296+ goto out_parent; /* success */
14297+
14298+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14299+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14300+ if (unlikely(err))
14301+ goto out_parent;
14302+
14303+ h_path.mnt = au_br_mnt(br);
14304+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14305+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14306+ delegated = NULL;
14307+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14308+ au_unpin(&pin);
14309+ /* todo: keep h_dentry or not? */
14310+ if (unlikely(err == -EWOULDBLOCK)) {
14311+ pr_warn("cannot retry for NFSv4 delegation"
14312+ " for an internal unlink\n");
14313+ iput(delegated);
14314+ }
14315+ if (unlikely(err)) {
14316+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14317+ dentry, err);
14318+ err = 0;
14319+ }
14320+ goto out_parent; /* success */
14321+
14322+out_dgrade:
14323+ di_downgrade_lock(parent, AuLock_IR);
14324+out_parent:
14325+ di_read_unlock(parent, AuLock_IR);
14326+ dput(parent);
14327+out:
14328+ AuTraceErr(err);
14329+ return err;
14330+}
14331+
b912730e 14332+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14333+{
8b6a4947 14334+ int err, aopen = args->aopen;
1facf9fc 14335+ struct dentry *dentry;
076b876e 14336+ struct au_finfo *finfo;
1308ab2a 14337+
8b6a4947 14338+ if (!aopen)
b912730e
AM
14339+ err = au_finfo_init(file, args->fidir);
14340+ else {
14341+ lockdep_off();
14342+ err = au_finfo_init(file, args->fidir);
14343+ lockdep_on();
14344+ }
4a4d8108
AM
14345+ if (unlikely(err))
14346+ goto out;
1facf9fc 14347+
2000de60 14348+ dentry = file->f_path.dentry;
b912730e 14349+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14350+ di_write_lock_child(dentry);
14351+ err = au_cmoo(dentry);
14352+ di_downgrade_lock(dentry, AuLock_IR);
14353+ if (!err) {
14354+ if (!aopen)
b912730e 14355+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14356+ else {
14357+ lockdep_off();
14358+ err = args->open(file, vfsub_file_flags(file), NULL);
14359+ lockdep_on();
14360+ }
b912730e 14361+ }
8b6a4947 14362+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14363+
076b876e
AM
14364+ finfo = au_fi(file);
14365+ if (!err) {
14366+ finfo->fi_file = file;
8b6a4947
AM
14367+ au_hbl_add(&finfo->fi_hlist,
14368+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14369+ }
8b6a4947 14370+ if (!aopen)
b912730e
AM
14371+ fi_write_unlock(file);
14372+ else {
14373+ lockdep_off();
14374+ fi_write_unlock(file);
14375+ lockdep_on();
14376+ }
4a4d8108 14377+ if (unlikely(err)) {
076b876e 14378+ finfo->fi_hdir = NULL;
1c60b727 14379+ au_finfo_fin(file);
1308ab2a 14380+ }
4a4d8108 14381+
4f0767ce 14382+out:
8b6a4947 14383+ AuTraceErr(err);
1308ab2a 14384+ return err;
14385+}
dece6358 14386+
4a4d8108 14387+int au_reopen_nondir(struct file *file)
1308ab2a 14388+{
4a4d8108 14389+ int err;
5afbbe0d 14390+ aufs_bindex_t btop;
4a4d8108
AM
14391+ struct dentry *dentry;
14392+ struct file *h_file, *h_file_tmp;
1308ab2a 14393+
2000de60 14394+ dentry = file->f_path.dentry;
5afbbe0d 14395+ btop = au_dbtop(dentry);
4a4d8108 14396+ h_file_tmp = NULL;
5afbbe0d 14397+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14398+ h_file = au_hf_top(file);
14399+ if (file->f_mode == h_file->f_mode)
14400+ return 0; /* success */
14401+ h_file_tmp = h_file;
14402+ get_file(h_file_tmp);
5afbbe0d 14403+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14404+ }
14405+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14406+ /*
14407+ * it can happen
14408+ * file exists on both of rw and ro
5afbbe0d 14409+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14410+ * prepend a branch as rw, "rw" become ro
14411+ * remove rw/file
14412+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14413+ * --> dbtop is 1, fbtop is still 0
14414+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14415+ */
5afbbe0d 14416+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14417+
5afbbe0d 14418+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14419+ file, /*force_wr*/0);
4a4d8108 14420+ err = PTR_ERR(h_file);
86dc4139
AM
14421+ if (IS_ERR(h_file)) {
14422+ if (h_file_tmp) {
5afbbe0d
AM
14423+ au_sbr_get(dentry->d_sb, btop);
14424+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14425+ h_file_tmp = NULL;
14426+ }
4a4d8108 14427+ goto out; /* todo: close all? */
86dc4139 14428+ }
4a4d8108
AM
14429+
14430+ err = 0;
5afbbe0d
AM
14431+ au_set_fbtop(file, btop);
14432+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14433+ au_update_figen(file);
14434+ /* todo: necessary? */
14435+ /* file->f_ra = h_file->f_ra; */
14436+
4f0767ce 14437+out:
4a4d8108
AM
14438+ if (h_file_tmp)
14439+ fput(h_file_tmp);
14440+ return err;
1facf9fc 14441+}
14442+
1308ab2a 14443+/* ---------------------------------------------------------------------- */
14444+
4a4d8108
AM
14445+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14446+ struct dentry *hi_wh)
1facf9fc 14447+{
4a4d8108 14448+ int err;
5afbbe0d 14449+ aufs_bindex_t btop;
4a4d8108
AM
14450+ struct au_dinfo *dinfo;
14451+ struct dentry *h_dentry;
14452+ struct au_hdentry *hdp;
1facf9fc 14453+
2000de60 14454+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14455+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14456+
5afbbe0d
AM
14457+ btop = dinfo->di_btop;
14458+ dinfo->di_btop = btgt;
14459+ hdp = au_hdentry(dinfo, btgt);
14460+ h_dentry = hdp->hd_dentry;
14461+ hdp->hd_dentry = hi_wh;
4a4d8108 14462+ err = au_reopen_nondir(file);
5afbbe0d
AM
14463+ hdp->hd_dentry = h_dentry;
14464+ dinfo->di_btop = btop;
1facf9fc 14465+
1facf9fc 14466+ return err;
14467+}
14468+
4a4d8108 14469+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14470+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14471+{
4a4d8108 14472+ int err;
027c5e7a 14473+ struct inode *inode, *h_inode;
c2b27bf2
AM
14474+ struct dentry *h_dentry, *hi_wh;
14475+ struct au_cp_generic cpg = {
2000de60 14476+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14477+ .bdst = bcpup,
14478+ .bsrc = -1,
14479+ .len = len,
14480+ .pin = pin
14481+ };
1facf9fc 14482+
5afbbe0d 14483+ au_update_dbtop(cpg.dentry);
5527c038 14484+ inode = d_inode(cpg.dentry);
027c5e7a 14485+ h_inode = NULL;
5afbbe0d
AM
14486+ if (au_dbtop(cpg.dentry) <= bcpup
14487+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14488+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14489+ if (h_dentry && d_is_positive(h_dentry))
14490+ h_inode = d_inode(h_dentry);
027c5e7a 14491+ }
4a4d8108 14492+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14493+ if (!hi_wh && !h_inode)
c2b27bf2 14494+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14495+ else
14496+ /* already copied-up after unlink */
14497+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14498+
4a4d8108 14499+ if (!err
38d290e6
JR
14500+ && (inode->i_nlink > 1
14501+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14502+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14503+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14504+
dece6358 14505+ return err;
1facf9fc 14506+}
14507+
4a4d8108
AM
14508+/*
14509+ * prepare the @file for writing.
14510+ */
14511+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14512+{
4a4d8108 14513+ int err;
5afbbe0d 14514+ aufs_bindex_t dbtop;
c1595e42 14515+ struct dentry *parent;
86dc4139 14516+ struct inode *inode;
1facf9fc 14517+ struct super_block *sb;
4a4d8108 14518+ struct file *h_file;
c2b27bf2 14519+ struct au_cp_generic cpg = {
2000de60 14520+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14521+ .bdst = -1,
14522+ .bsrc = -1,
14523+ .len = len,
14524+ .pin = pin,
14525+ .flags = AuCpup_DTIME
14526+ };
1facf9fc 14527+
c2b27bf2 14528+ sb = cpg.dentry->d_sb;
5527c038 14529+ inode = d_inode(cpg.dentry);
5afbbe0d 14530+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14531+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14532+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14533+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14534+ /*flags*/0);
1facf9fc 14535+ goto out;
4a4d8108 14536+ }
1facf9fc 14537+
027c5e7a 14538+ /* need to cpup or reopen */
c2b27bf2 14539+ parent = dget_parent(cpg.dentry);
4a4d8108 14540+ di_write_lock_parent(parent);
c2b27bf2
AM
14541+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14542+ cpg.bdst = err;
4a4d8108
AM
14543+ if (unlikely(err < 0))
14544+ goto out_dgrade;
14545+ err = 0;
14546+
c2b27bf2
AM
14547+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14548+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14549+ if (unlikely(err))
4a4d8108
AM
14550+ goto out_dgrade;
14551+ }
14552+
c2b27bf2 14553+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14554+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14555+ if (unlikely(err))
14556+ goto out_dgrade;
14557+
5afbbe0d
AM
14558+ dbtop = au_dbtop(cpg.dentry);
14559+ if (dbtop <= cpg.bdst)
c2b27bf2 14560+ cpg.bsrc = cpg.bdst;
027c5e7a 14561+
5afbbe0d 14562+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14563+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14564+ ) {
392086de 14565+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14566+ if (IS_ERR(h_file))
027c5e7a 14567+ err = PTR_ERR(h_file);
86dc4139 14568+ else {
027c5e7a 14569+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14570+ if (dbtop > cpg.bdst)
c2b27bf2 14571+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14572+ if (!err)
14573+ err = au_reopen_nondir(file);
c2b27bf2 14574+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14575+ }
027c5e7a
AM
14576+ } else { /* copyup as wh and reopen */
14577+ /*
14578+ * since writable hfsplus branch is not supported,
14579+ * h_open_pre/post() are unnecessary.
14580+ */
c2b27bf2 14581+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14582+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14583+ }
4a4d8108
AM
14584+
14585+ if (!err) {
14586+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14587+ goto out_dput; /* success */
14588+ }
14589+ au_unpin(pin);
14590+ goto out_unlock;
1facf9fc 14591+
4f0767ce 14592+out_dgrade:
4a4d8108 14593+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14594+out_unlock:
4a4d8108 14595+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14596+out_dput:
4a4d8108 14597+ dput(parent);
4f0767ce 14598+out:
1facf9fc 14599+ return err;
14600+}
14601+
4a4d8108
AM
14602+/* ---------------------------------------------------------------------- */
14603+
14604+int au_do_flush(struct file *file, fl_owner_t id,
14605+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14606+{
4a4d8108 14607+ int err;
1facf9fc 14608+ struct super_block *sb;
4a4d8108 14609+ struct inode *inode;
1facf9fc 14610+
c06a8ce3
AM
14611+ inode = file_inode(file);
14612+ sb = inode->i_sb;
4a4d8108
AM
14613+ si_noflush_read_lock(sb);
14614+ fi_read_lock(file);
b752ccd1 14615+ ii_read_lock_child(inode);
1facf9fc 14616+
4a4d8108
AM
14617+ err = flush(file, id);
14618+ au_cpup_attr_timesizes(inode);
1facf9fc 14619+
b752ccd1 14620+ ii_read_unlock(inode);
4a4d8108 14621+ fi_read_unlock(file);
1308ab2a 14622+ si_read_unlock(sb);
dece6358 14623+ return err;
1facf9fc 14624+}
14625+
4a4d8108
AM
14626+/* ---------------------------------------------------------------------- */
14627+
14628+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14629+{
4a4d8108 14630+ int err;
4a4d8108
AM
14631+ struct au_pin pin;
14632+ struct au_finfo *finfo;
c2b27bf2 14633+ struct dentry *parent, *hi_wh;
4a4d8108 14634+ struct inode *inode;
1facf9fc 14635+ struct super_block *sb;
c2b27bf2 14636+ struct au_cp_generic cpg = {
2000de60 14637+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14638+ .bdst = -1,
14639+ .bsrc = -1,
14640+ .len = -1,
14641+ .pin = &pin,
14642+ .flags = AuCpup_DTIME
14643+ };
1facf9fc 14644+
4a4d8108
AM
14645+ FiMustWriteLock(file);
14646+
14647+ err = 0;
14648+ finfo = au_fi(file);
c2b27bf2 14649+ sb = cpg.dentry->d_sb;
5527c038 14650+ inode = d_inode(cpg.dentry);
5afbbe0d 14651+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14652+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14653+ goto out;
dece6358 14654+
c2b27bf2
AM
14655+ parent = dget_parent(cpg.dentry);
14656+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14657+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14658+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14659+ cpg.bdst = err;
4a4d8108
AM
14660+ di_read_unlock(parent, !AuLock_IR);
14661+ if (unlikely(err < 0))
14662+ goto out_parent;
14663+ err = 0;
1facf9fc 14664+ }
1facf9fc 14665+
4a4d8108 14666+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14667+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14668+ if (!S_ISDIR(inode->i_mode)
14669+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14670+ && au_plink_test(inode)
c2b27bf2 14671+ && !d_unhashed(cpg.dentry)
5afbbe0d 14672+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14673+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14674+ if (unlikely(err))
14675+ goto out_unlock;
14676+
14677+ /* always superio. */
c2b27bf2 14678+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14679+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14680+ if (!err) {
c2b27bf2 14681+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14682+ au_unpin(&pin);
14683+ }
4a4d8108
AM
14684+ } else if (hi_wh) {
14685+ /* already copied-up after unlink */
c2b27bf2 14686+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14687+ *need_reopen = 0;
14688+ }
1facf9fc 14689+
4f0767ce 14690+out_unlock:
4a4d8108 14691+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14692+out_parent:
4a4d8108 14693+ dput(parent);
4f0767ce 14694+out:
1308ab2a 14695+ return err;
dece6358 14696+}
1facf9fc 14697+
4a4d8108 14698+static void au_do_refresh_dir(struct file *file)
dece6358 14699+{
5afbbe0d 14700+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14701+ struct au_hfile *p, tmp, *q;
14702+ struct au_finfo *finfo;
1308ab2a 14703+ struct super_block *sb;
4a4d8108 14704+ struct au_fidir *fidir;
1facf9fc 14705+
4a4d8108 14706+ FiMustWriteLock(file);
1facf9fc 14707+
2000de60 14708+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14709+ finfo = au_fi(file);
14710+ fidir = finfo->fi_hdir;
14711+ AuDebugOn(!fidir);
14712+ p = fidir->fd_hfile + finfo->fi_btop;
14713+ brid = p->hf_br->br_id;
5afbbe0d
AM
14714+ bbot = fidir->fd_bbot;
14715+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14716+ if (!p->hf_file)
14717+ continue;
1308ab2a 14718+
4a4d8108
AM
14719+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14720+ if (new_bindex == bindex)
14721+ continue;
14722+ if (new_bindex < 0) {
14723+ au_set_h_fptr(file, bindex, NULL);
14724+ continue;
14725+ }
1308ab2a 14726+
4a4d8108
AM
14727+ /* swap two lower inode, and loop again */
14728+ q = fidir->fd_hfile + new_bindex;
14729+ tmp = *q;
14730+ *q = *p;
14731+ *p = tmp;
14732+ if (tmp.hf_file) {
14733+ bindex--;
14734+ p--;
14735+ }
14736+ }
1308ab2a 14737+
4a4d8108 14738+ p = fidir->fd_hfile;
2000de60 14739+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14740+ bbot = au_sbbot(sb);
14741+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14742+ finfo->fi_btop++, p++)
14743+ if (p->hf_file) {
c06a8ce3 14744+ if (file_inode(p->hf_file))
4a4d8108 14745+ break;
1c60b727 14746+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14747+ }
14748+ } else {
5afbbe0d
AM
14749+ bbot = au_br_index(sb, brid);
14750+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14751+ finfo->fi_btop++, p++)
14752+ if (p->hf_file)
1c60b727 14753+ au_hfput(p, /*execed*/0);
5afbbe0d 14754+ bbot = au_sbbot(sb);
4a4d8108 14755+ }
1308ab2a 14756+
5afbbe0d
AM
14757+ p = fidir->fd_hfile + bbot;
14758+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14759+ fidir->fd_bbot--, p--)
14760+ if (p->hf_file) {
c06a8ce3 14761+ if (file_inode(p->hf_file))
4a4d8108 14762+ break;
1c60b727 14763+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14764+ }
14765+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14766+}
14767+
4a4d8108
AM
14768+/*
14769+ * after branch manipulating, refresh the file.
14770+ */
14771+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14772+{
e2f27e51 14773+ int err, need_reopen, nbr;
5afbbe0d 14774+ aufs_bindex_t bbot, bindex;
4a4d8108 14775+ struct dentry *dentry;
e2f27e51 14776+ struct super_block *sb;
1308ab2a 14777+ struct au_finfo *finfo;
4a4d8108 14778+ struct au_hfile *hfile;
1facf9fc 14779+
2000de60 14780+ dentry = file->f_path.dentry;
e2f27e51
AM
14781+ sb = dentry->d_sb;
14782+ nbr = au_sbbot(sb) + 1;
1308ab2a 14783+ finfo = au_fi(file);
4a4d8108
AM
14784+ if (!finfo->fi_hdir) {
14785+ hfile = &finfo->fi_htop;
14786+ AuDebugOn(!hfile->hf_file);
e2f27e51 14787+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14788+ AuDebugOn(bindex < 0);
14789+ if (bindex != finfo->fi_btop)
5afbbe0d 14790+ au_set_fbtop(file, bindex);
4a4d8108 14791+ } else {
e2f27e51 14792+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14793+ if (unlikely(err))
14794+ goto out;
14795+ au_do_refresh_dir(file);
14796+ }
1facf9fc 14797+
4a4d8108
AM
14798+ err = 0;
14799+ need_reopen = 1;
14800+ if (!au_test_mmapped(file))
14801+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14802+ if (finfo->fi_hdir)
14803+ /* harmless if err */
14804+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14805+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14806+ err = reopen(file);
14807+ if (!err) {
14808+ au_update_figen(file);
14809+ goto out; /* success */
14810+ }
14811+
14812+ /* error, close all lower files */
14813+ if (finfo->fi_hdir) {
5afbbe0d
AM
14814+ bbot = au_fbbot_dir(file);
14815+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14816+ au_set_h_fptr(file, bindex, NULL);
14817+ }
1facf9fc 14818+
4f0767ce 14819+out:
1facf9fc 14820+ return err;
14821+}
14822+
4a4d8108
AM
14823+/* common function to regular file and dir */
14824+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14825+ int wlock, unsigned int fi_lsc)
dece6358 14826+{
1308ab2a 14827+ int err;
4a4d8108 14828+ unsigned int sigen, figen;
5afbbe0d 14829+ aufs_bindex_t btop;
4a4d8108
AM
14830+ unsigned char pseudo_link;
14831+ struct dentry *dentry;
14832+ struct inode *inode;
1facf9fc 14833+
4a4d8108 14834+ err = 0;
2000de60 14835+ dentry = file->f_path.dentry;
5527c038 14836+ inode = d_inode(dentry);
4a4d8108 14837+ sigen = au_sigen(dentry->d_sb);
521ced18 14838+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14839+ figen = au_figen(file);
521ced18
JR
14840+ if (!fi_lsc)
14841+ di_write_lock_child(dentry);
14842+ else
14843+ di_write_lock_child2(dentry);
5afbbe0d
AM
14844+ btop = au_dbtop(dentry);
14845+ pseudo_link = (btop != au_ibtop(inode));
14846+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14847+ if (!wlock) {
14848+ di_downgrade_lock(dentry, AuLock_IR);
14849+ fi_downgrade_lock(file);
14850+ }
14851+ goto out; /* success */
14852+ }
dece6358 14853+
4a4d8108 14854+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14855+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14856+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14857+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14858+ }
dece6358 14859+
027c5e7a
AM
14860+ if (!err)
14861+ err = refresh_file(file, reopen);
4a4d8108
AM
14862+ if (!err) {
14863+ if (!wlock) {
14864+ di_downgrade_lock(dentry, AuLock_IR);
14865+ fi_downgrade_lock(file);
14866+ }
14867+ } else {
14868+ di_write_unlock(dentry);
14869+ fi_write_unlock(file);
14870+ }
1facf9fc 14871+
4f0767ce 14872+out:
1308ab2a 14873+ return err;
14874+}
1facf9fc 14875+
4a4d8108
AM
14876+/* ---------------------------------------------------------------------- */
14877+
14878+/* cf. aufs_nopage() */
14879+/* for madvise(2) */
14880+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 14881+{
4a4d8108
AM
14882+ unlock_page(page);
14883+ return 0;
14884+}
1facf9fc 14885+
4a4d8108 14886+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 14887+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 14888+{ BUG(); return 0; }
1facf9fc 14889+
4a4d8108
AM
14890+/* they will never be called. */
14891+#ifdef CONFIG_AUFS_DEBUG
14892+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14893+ loff_t pos, unsigned len, unsigned flags,
14894+ struct page **pagep, void **fsdata)
14895+{ AuUnsupport(); return 0; }
14896+static int aufs_write_end(struct file *file, struct address_space *mapping,
14897+ loff_t pos, unsigned len, unsigned copied,
14898+ struct page *page, void *fsdata)
14899+{ AuUnsupport(); return 0; }
14900+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14901+{ AuUnsupport(); return 0; }
1308ab2a 14902+
4a4d8108
AM
14903+static int aufs_set_page_dirty(struct page *page)
14904+{ AuUnsupport(); return 0; }
392086de
AM
14905+static void aufs_invalidatepage(struct page *page, unsigned int offset,
14906+ unsigned int length)
4a4d8108
AM
14907+{ AuUnsupport(); }
14908+static int aufs_releasepage(struct page *page, gfp_t gfp)
14909+{ AuUnsupport(); return 0; }
79b8bda9 14910+#if 0 /* called by memory compaction regardless file */
4a4d8108 14911+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 14912+ struct page *page, enum migrate_mode mode)
4a4d8108 14913+{ AuUnsupport(); return 0; }
79b8bda9 14914+#endif
e2f27e51
AM
14915+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14916+{ AuUnsupport(); return true; }
14917+static void aufs_putback_page(struct page *page)
14918+{ AuUnsupport(); }
4a4d8108
AM
14919+static int aufs_launder_page(struct page *page)
14920+{ AuUnsupport(); return 0; }
14921+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
14922+ unsigned long from,
14923+ unsigned long count)
4a4d8108 14924+{ AuUnsupport(); return 0; }
392086de
AM
14925+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14926+ bool *writeback)
14927+{ AuUnsupport(); }
4a4d8108
AM
14928+static int aufs_error_remove_page(struct address_space *mapping,
14929+ struct page *page)
14930+{ AuUnsupport(); return 0; }
b4510431
AM
14931+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14932+ sector_t *span)
14933+{ AuUnsupport(); return 0; }
14934+static void aufs_swap_deactivate(struct file *file)
14935+{ AuUnsupport(); }
4a4d8108
AM
14936+#endif /* CONFIG_AUFS_DEBUG */
14937+
14938+const struct address_space_operations aufs_aop = {
14939+ .readpage = aufs_readpage,
14940+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
14941+#ifdef CONFIG_AUFS_DEBUG
14942+ .writepage = aufs_writepage,
4a4d8108
AM
14943+ /* no writepages, because of writepage */
14944+ .set_page_dirty = aufs_set_page_dirty,
14945+ /* no readpages, because of readpage */
14946+ .write_begin = aufs_write_begin,
14947+ .write_end = aufs_write_end,
14948+ /* no bmap, no block device */
14949+ .invalidatepage = aufs_invalidatepage,
14950+ .releasepage = aufs_releasepage,
79b8bda9
AM
14951+ /* is fallback_migrate_page ok? */
14952+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
14953+ .isolate_page = aufs_isolate_page,
14954+ .putback_page = aufs_putback_page,
4a4d8108
AM
14955+ .launder_page = aufs_launder_page,
14956+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 14957+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
14958+ .error_remove_page = aufs_error_remove_page,
14959+ .swap_activate = aufs_swap_activate,
14960+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 14961+#endif /* CONFIG_AUFS_DEBUG */
dece6358 14962+};
7f207e10
AM
14963diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14964--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
be118d29 14965+++ linux/fs/aufs/file.h 2018-04-06 07:48:44.204604724 +0200
8b6a4947 14966@@ -0,0 +1,340 @@
4a4d8108 14967+/*
a2654f78 14968+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
14969+ *
14970+ * This program, aufs is free software; you can redistribute it and/or modify
14971+ * it under the terms of the GNU General Public License as published by
14972+ * the Free Software Foundation; either version 2 of the License, or
14973+ * (at your option) any later version.
14974+ *
14975+ * This program is distributed in the hope that it will be useful,
14976+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14977+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14978+ * GNU General Public License for more details.
14979+ *
14980+ * You should have received a copy of the GNU General Public License
523b37e3 14981+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 14982+ */
1facf9fc 14983+
4a4d8108
AM
14984+/*
14985+ * file operations
14986+ */
1facf9fc 14987+
4a4d8108
AM
14988+#ifndef __AUFS_FILE_H__
14989+#define __AUFS_FILE_H__
1facf9fc 14990+
4a4d8108 14991+#ifdef __KERNEL__
1facf9fc 14992+
2cbb1c4b 14993+#include <linux/file.h>
4a4d8108 14994+#include <linux/fs.h>
3c1bdaff 14995+#include <linux/mm_types.h>
4a4d8108 14996+#include <linux/poll.h>
4a4d8108 14997+#include "rwsem.h"
1facf9fc 14998+
4a4d8108
AM
14999+struct au_branch;
15000+struct au_hfile {
15001+ struct file *hf_file;
15002+ struct au_branch *hf_br;
15003+};
1facf9fc 15004+
4a4d8108
AM
15005+struct au_vdir;
15006+struct au_fidir {
15007+ aufs_bindex_t fd_bbot;
15008+ aufs_bindex_t fd_nent;
15009+ struct au_vdir *fd_vdir_cache;
15010+ struct au_hfile fd_hfile[];
15011+};
1facf9fc 15012+
4a4d8108 15013+static inline int au_fidir_sz(int nent)
dece6358 15014+{
4f0767ce
JR
15015+ AuDebugOn(nent < 0);
15016+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 15017+}
1facf9fc 15018+
4a4d8108
AM
15019+struct au_finfo {
15020+ atomic_t fi_generation;
dece6358 15021+
4a4d8108
AM
15022+ struct au_rwsem fi_rwsem;
15023+ aufs_bindex_t fi_btop;
15024+
15025+ /* do not union them */
15026+ struct { /* for non-dir */
15027+ struct au_hfile fi_htop;
2cbb1c4b 15028+ atomic_t fi_mmapped;
4a4d8108
AM
15029+ };
15030+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15031+
8b6a4947 15032+ struct hlist_bl_node fi_hlist;
1c60b727 15033+ struct file *fi_file; /* very ugly */
4a4d8108 15034+} ____cacheline_aligned_in_smp;
1facf9fc 15035+
4a4d8108 15036+/* ---------------------------------------------------------------------- */
1facf9fc 15037+
4a4d8108
AM
15038+/* file.c */
15039+extern const struct address_space_operations aufs_aop;
15040+unsigned int au_file_roflags(unsigned int flags);
15041+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15042+ struct file *file, int force_wr);
b912730e 15043+struct au_do_open_args {
8b6a4947 15044+ int aopen;
b912730e
AM
15045+ int (*open)(struct file *file, int flags,
15046+ struct file *h_file);
15047+ struct au_fidir *fidir;
15048+ struct file *h_file;
15049+};
15050+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15051+int au_reopen_nondir(struct file *file);
15052+struct au_pin;
15053+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15054+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15055+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15056+int au_do_flush(struct file *file, fl_owner_t id,
15057+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15058+
4a4d8108
AM
15059+/* poll.c */
15060+#ifdef CONFIG_AUFS_POLL
be118d29 15061+__poll_t aufs_poll(struct file *file, poll_table *wait);
4a4d8108 15062+#endif
1facf9fc 15063+
4a4d8108
AM
15064+#ifdef CONFIG_AUFS_BR_HFSPLUS
15065+/* hfsplus.c */
392086de
AM
15066+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15067+ int force_wr);
4a4d8108
AM
15068+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15069+ struct file *h_file);
15070+#else
c1595e42
JR
15071+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15072+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15073+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15074+ struct file *h_file);
15075+#endif
1facf9fc 15076+
4a4d8108
AM
15077+/* f_op.c */
15078+extern const struct file_operations aufs_file_fop;
b912730e 15079+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15080+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15081+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15082+
4a4d8108 15083+/* finfo.c */
f0c0a007 15084+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15085+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15086+ struct file *h_file);
1facf9fc 15087+
4a4d8108 15088+void au_update_figen(struct file *file);
4a4d8108 15089+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15090+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15091+
4a4d8108 15092+void au_fi_init_once(void *_fi);
1c60b727 15093+void au_finfo_fin(struct file *file);
4a4d8108 15094+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15095+
4a4d8108
AM
15096+/* ioctl.c */
15097+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15098+#ifdef CONFIG_COMPAT
15099+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15100+ unsigned long arg);
c2b27bf2
AM
15101+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15102+ unsigned long arg);
b752ccd1 15103+#endif
1facf9fc 15104+
4a4d8108 15105+/* ---------------------------------------------------------------------- */
1facf9fc 15106+
4a4d8108
AM
15107+static inline struct au_finfo *au_fi(struct file *file)
15108+{
38d290e6 15109+ return file->private_data;
4a4d8108 15110+}
1facf9fc 15111+
4a4d8108 15112+/* ---------------------------------------------------------------------- */
1facf9fc 15113+
8b6a4947
AM
15114+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15115+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15116+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15117+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15118+/*
8b6a4947
AM
15119+#define fi_read_trylock_nested(f) \
15120+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15121+#define fi_write_trylock_nested(f) \
15122+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15123+*/
15124+
15125+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15126+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15127+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15128+
521ced18
JR
15129+/* lock subclass for finfo */
15130+enum {
15131+ AuLsc_FI_1,
15132+ AuLsc_FI_2
15133+};
15134+
15135+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15136+{
15137+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15138+}
15139+
15140+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15141+{
15142+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15143+}
15144+
15145+/*
15146+ * fi_read_lock_1, fi_write_lock_1,
15147+ * fi_read_lock_2, fi_write_lock_2
15148+ */
15149+#define AuReadLockFunc(name) \
15150+static inline void fi_read_lock_##name(struct file *f) \
15151+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15152+
15153+#define AuWriteLockFunc(name) \
15154+static inline void fi_write_lock_##name(struct file *f) \
15155+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15156+
15157+#define AuRWLockFuncs(name) \
15158+ AuReadLockFunc(name) \
15159+ AuWriteLockFunc(name)
15160+
15161+AuRWLockFuncs(1);
15162+AuRWLockFuncs(2);
15163+
15164+#undef AuReadLockFunc
15165+#undef AuWriteLockFunc
15166+#undef AuRWLockFuncs
15167+
4a4d8108
AM
15168+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15169+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15170+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15171+
1308ab2a 15172+/* ---------------------------------------------------------------------- */
15173+
4a4d8108 15174+/* todo: hard/soft set? */
5afbbe0d 15175+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15176+{
4a4d8108
AM
15177+ FiMustAnyLock(file);
15178+ return au_fi(file)->fi_btop;
15179+}
dece6358 15180+
5afbbe0d 15181+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15182+{
15183+ FiMustAnyLock(file);
15184+ AuDebugOn(!au_fi(file)->fi_hdir);
15185+ return au_fi(file)->fi_hdir->fd_bbot;
15186+}
1facf9fc 15187+
4a4d8108
AM
15188+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15189+{
15190+ FiMustAnyLock(file);
15191+ AuDebugOn(!au_fi(file)->fi_hdir);
15192+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15193+}
1facf9fc 15194+
5afbbe0d 15195+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15196+{
15197+ FiMustWriteLock(file);
15198+ au_fi(file)->fi_btop = bindex;
15199+}
1facf9fc 15200+
5afbbe0d 15201+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15202+{
15203+ FiMustWriteLock(file);
15204+ AuDebugOn(!au_fi(file)->fi_hdir);
15205+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15206+}
1308ab2a 15207+
4a4d8108
AM
15208+static inline void au_set_fvdir_cache(struct file *file,
15209+ struct au_vdir *vdir_cache)
15210+{
15211+ FiMustWriteLock(file);
15212+ AuDebugOn(!au_fi(file)->fi_hdir);
15213+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15214+}
dece6358 15215+
4a4d8108
AM
15216+static inline struct file *au_hf_top(struct file *file)
15217+{
15218+ FiMustAnyLock(file);
15219+ AuDebugOn(au_fi(file)->fi_hdir);
15220+ return au_fi(file)->fi_htop.hf_file;
15221+}
1facf9fc 15222+
4a4d8108
AM
15223+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15224+{
15225+ FiMustAnyLock(file);
15226+ AuDebugOn(!au_fi(file)->fi_hdir);
15227+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15228+}
15229+
4a4d8108
AM
15230+/* todo: memory barrier? */
15231+static inline unsigned int au_figen(struct file *f)
dece6358 15232+{
4a4d8108
AM
15233+ return atomic_read(&au_fi(f)->fi_generation);
15234+}
dece6358 15235+
2cbb1c4b
JR
15236+static inline void au_set_mmapped(struct file *f)
15237+{
15238+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15239+ return;
0c3ec466 15240+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15241+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15242+ ;
15243+}
15244+
15245+static inline void au_unset_mmapped(struct file *f)
15246+{
15247+ atomic_dec(&au_fi(f)->fi_mmapped);
15248+}
15249+
4a4d8108
AM
15250+static inline int au_test_mmapped(struct file *f)
15251+{
2cbb1c4b
JR
15252+ return atomic_read(&au_fi(f)->fi_mmapped);
15253+}
15254+
15255+/* customize vma->vm_file */
15256+
15257+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15258+ struct file *file)
15259+{
53392da6
AM
15260+ struct file *f;
15261+
15262+ f = vma->vm_file;
2cbb1c4b
JR
15263+ get_file(file);
15264+ vma->vm_file = file;
53392da6 15265+ fput(f);
2cbb1c4b
JR
15266+}
15267+
15268+#ifdef CONFIG_MMU
15269+#define AuDbgVmRegion(file, vma) do {} while (0)
15270+
15271+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15272+ struct file *file)
15273+{
15274+ au_do_vm_file_reset(vma, file);
15275+}
15276+#else
15277+#define AuDbgVmRegion(file, vma) \
15278+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15279+
15280+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15281+ struct file *file)
15282+{
53392da6
AM
15283+ struct file *f;
15284+
2cbb1c4b 15285+ au_do_vm_file_reset(vma, file);
53392da6 15286+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15287+ get_file(file);
15288+ vma->vm_region->vm_file = file;
53392da6 15289+ fput(f);
2cbb1c4b
JR
15290+}
15291+#endif /* CONFIG_MMU */
15292+
15293+/* handle vma->vm_prfile */
fb47a38f 15294+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15295+ struct file *file)
15296+{
2cbb1c4b
JR
15297+ get_file(file);
15298+ vma->vm_prfile = file;
15299+#ifndef CONFIG_MMU
15300+ get_file(file);
15301+ vma->vm_region->vm_prfile = file;
15302+#endif
fb47a38f 15303+}
1308ab2a 15304+
4a4d8108
AM
15305+#endif /* __KERNEL__ */
15306+#endif /* __AUFS_FILE_H__ */
7f207e10
AM
15307diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15308--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
be118d29 15309+++ linux/fs/aufs/finfo.c 2018-04-06 07:48:44.204604724 +0200
1c60b727 15310@@ -0,0 +1,148 @@
4a4d8108 15311+/*
a2654f78 15312+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
15313+ *
15314+ * This program, aufs is free software; you can redistribute it and/or modify
15315+ * it under the terms of the GNU General Public License as published by
15316+ * the Free Software Foundation; either version 2 of the License, or
15317+ * (at your option) any later version.
15318+ *
15319+ * This program is distributed in the hope that it will be useful,
15320+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15321+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15322+ * GNU General Public License for more details.
15323+ *
15324+ * You should have received a copy of the GNU General Public License
523b37e3 15325+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15326+ */
1308ab2a 15327+
4a4d8108
AM
15328+/*
15329+ * file private data
15330+ */
1facf9fc 15331+
4a4d8108 15332+#include "aufs.h"
1facf9fc 15333+
f0c0a007 15334+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15335+{
f0c0a007 15336+ if (execed)
4a4d8108
AM
15337+ allow_write_access(hf->hf_file);
15338+ fput(hf->hf_file);
15339+ hf->hf_file = NULL;
5afbbe0d 15340+ au_br_put(hf->hf_br);
4a4d8108
AM
15341+ hf->hf_br = NULL;
15342+}
1facf9fc 15343+
4a4d8108
AM
15344+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15345+{
15346+ struct au_finfo *finfo = au_fi(file);
15347+ struct au_hfile *hf;
15348+ struct au_fidir *fidir;
15349+
15350+ fidir = finfo->fi_hdir;
15351+ if (!fidir) {
15352+ AuDebugOn(finfo->fi_btop != bindex);
15353+ hf = &finfo->fi_htop;
15354+ } else
15355+ hf = fidir->fd_hfile + bindex;
15356+
15357+ if (hf && hf->hf_file)
f0c0a007 15358+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15359+ if (val) {
15360+ FiMustWriteLock(file);
b912730e 15361+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15362+ hf->hf_file = val;
2000de60 15363+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15364+ }
4a4d8108 15365+}
1facf9fc 15366+
4a4d8108
AM
15367+void au_update_figen(struct file *file)
15368+{
2000de60 15369+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15370+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15371+}
15372+
4a4d8108
AM
15373+/* ---------------------------------------------------------------------- */
15374+
4a4d8108
AM
15375+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15376+{
15377+ struct au_fidir *fidir;
15378+ int nbr;
15379+
5afbbe0d 15380+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15381+ if (nbr < 2)
15382+ nbr = 2; /* initial allocate for 2 branches */
15383+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15384+ if (fidir) {
15385+ fidir->fd_bbot = -1;
15386+ fidir->fd_nent = nbr;
4a4d8108
AM
15387+ }
15388+
15389+ return fidir;
15390+}
15391+
e2f27e51 15392+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15393+{
15394+ int err;
15395+ struct au_fidir *fidir, *p;
15396+
15397+ AuRwMustWriteLock(&finfo->fi_rwsem);
15398+ fidir = finfo->fi_hdir;
15399+ AuDebugOn(!fidir);
15400+
15401+ err = -ENOMEM;
15402+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15403+ GFP_NOFS, may_shrink);
4a4d8108
AM
15404+ if (p) {
15405+ p->fd_nent = nbr;
15406+ finfo->fi_hdir = p;
15407+ err = 0;
15408+ }
1facf9fc 15409+
dece6358 15410+ return err;
1facf9fc 15411+}
1308ab2a 15412+
15413+/* ---------------------------------------------------------------------- */
15414+
1c60b727 15415+void au_finfo_fin(struct file *file)
1308ab2a 15416+{
4a4d8108
AM
15417+ struct au_finfo *finfo;
15418+
2000de60 15419+ au_nfiles_dec(file->f_path.dentry->d_sb);
7f207e10 15420+
4a4d8108
AM
15421+ finfo = au_fi(file);
15422+ AuDebugOn(finfo->fi_hdir);
15423+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15424+ au_cache_free_finfo(finfo);
1308ab2a 15425+}
1308ab2a 15426+
e49829fe 15427+void au_fi_init_once(void *_finfo)
4a4d8108 15428+{
e49829fe 15429+ struct au_finfo *finfo = _finfo;
1308ab2a 15430+
e49829fe 15431+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15432+}
1308ab2a 15433+
4a4d8108
AM
15434+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15435+{
1716fcea 15436+ int err;
4a4d8108
AM
15437+ struct au_finfo *finfo;
15438+ struct dentry *dentry;
15439+
15440+ err = -ENOMEM;
2000de60 15441+ dentry = file->f_path.dentry;
4a4d8108
AM
15442+ finfo = au_cache_alloc_finfo();
15443+ if (unlikely(!finfo))
15444+ goto out;
15445+
15446+ err = 0;
7f207e10 15447+ au_nfiles_inc(dentry->d_sb);
4a4d8108
AM
15448+ au_rw_write_lock(&finfo->fi_rwsem);
15449+ finfo->fi_btop = -1;
15450+ finfo->fi_hdir = fidir;
15451+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15452+ /* smp_mb(); */ /* atomic_set */
15453+
15454+ file->private_data = finfo;
15455+
15456+out:
15457+ return err;
15458+}
7f207e10
AM
15459diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15460--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
be118d29 15461+++ linux/fs/aufs/f_op.c 2018-04-06 07:48:44.204604724 +0200
1c60b727 15462@@ -0,0 +1,817 @@
dece6358 15463+/*
a2654f78 15464+ * Copyright (C) 2005-2017 Junjiro R. Okajima
dece6358
AM
15465+ *
15466+ * This program, aufs is free software; you can redistribute it and/or modify
15467+ * it under the terms of the GNU General Public License as published by
15468+ * the Free Software Foundation; either version 2 of the License, or
15469+ * (at your option) any later version.
15470+ *
15471+ * This program is distributed in the hope that it will be useful,
15472+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15473+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15474+ * GNU General Public License for more details.
15475+ *
15476+ * You should have received a copy of the GNU General Public License
523b37e3 15477+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15478+ */
1facf9fc 15479+
15480+/*
4a4d8108 15481+ * file and vm operations
1facf9fc 15482+ */
dece6358 15483+
86dc4139 15484+#include <linux/aio.h>
4a4d8108
AM
15485+#include <linux/fs_stack.h>
15486+#include <linux/mman.h>
4a4d8108 15487+#include <linux/security.h>
dece6358
AM
15488+#include "aufs.h"
15489+
b912730e 15490+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15491+{
4a4d8108
AM
15492+ int err;
15493+ aufs_bindex_t bindex;
8cdd5066 15494+ struct dentry *dentry, *h_dentry;
4a4d8108 15495+ struct au_finfo *finfo;
38d290e6 15496+ struct inode *h_inode;
4a4d8108
AM
15497+
15498+ FiMustWriteLock(file);
15499+
523b37e3 15500+ err = 0;
2000de60 15501+ dentry = file->f_path.dentry;
b912730e 15502+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15503+ finfo = au_fi(file);
15504+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15505+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15506+ bindex = au_dbtop(dentry);
8cdd5066
JR
15507+ if (!h_file) {
15508+ h_dentry = au_h_dptr(dentry, bindex);
15509+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15510+ if (unlikely(err))
15511+ goto out;
b912730e 15512+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
8cdd5066
JR
15513+ } else {
15514+ h_dentry = h_file->f_path.dentry;
15515+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15516+ if (unlikely(err))
15517+ goto out;
b912730e 15518+ get_file(h_file);
8cdd5066 15519+ }
4a4d8108
AM
15520+ if (IS_ERR(h_file))
15521+ err = PTR_ERR(h_file);
15522+ else {
38d290e6
JR
15523+ if ((flags & __O_TMPFILE)
15524+ && !(flags & O_EXCL)) {
15525+ h_inode = file_inode(h_file);
15526+ spin_lock(&h_inode->i_lock);
15527+ h_inode->i_state |= I_LINKABLE;
15528+ spin_unlock(&h_inode->i_lock);
15529+ }
5afbbe0d 15530+ au_set_fbtop(file, bindex);
4a4d8108
AM
15531+ au_set_h_fptr(file, bindex, h_file);
15532+ au_update_figen(file);
15533+ /* todo: necessary? */
15534+ /* file->f_ra = h_file->f_ra; */
15535+ }
027c5e7a 15536+
8cdd5066 15537+out:
4a4d8108 15538+ return err;
1facf9fc 15539+}
15540+
4a4d8108
AM
15541+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15542+ struct file *file)
1facf9fc 15543+{
4a4d8108 15544+ int err;
1308ab2a 15545+ struct super_block *sb;
b912730e
AM
15546+ struct au_do_open_args args = {
15547+ .open = au_do_open_nondir
15548+ };
1facf9fc 15549+
523b37e3
AM
15550+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15551+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15552+
2000de60 15553+ sb = file->f_path.dentry->d_sb;
4a4d8108 15554+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15555+ err = au_do_open(file, &args);
4a4d8108
AM
15556+ si_read_unlock(sb);
15557+ return err;
15558+}
1facf9fc 15559+
4a4d8108
AM
15560+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15561+{
15562+ struct au_finfo *finfo;
15563+ aufs_bindex_t bindex;
1facf9fc 15564+
4a4d8108 15565+ finfo = au_fi(file);
8b6a4947
AM
15566+ au_hbl_del(&finfo->fi_hlist,
15567+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15568+ bindex = finfo->fi_btop;
b4510431 15569+ if (bindex >= 0)
4a4d8108 15570+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15571+
1c60b727 15572+ au_finfo_fin(file);
4a4d8108 15573+ return 0;
1facf9fc 15574+}
15575+
4a4d8108
AM
15576+/* ---------------------------------------------------------------------- */
15577+
15578+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15579+{
1308ab2a 15580+ int err;
4a4d8108
AM
15581+ struct file *h_file;
15582+
15583+ err = 0;
15584+ h_file = au_hf_top(file);
15585+ if (h_file)
15586+ err = vfsub_flush(h_file, id);
15587+ return err;
15588+}
15589+
15590+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15591+{
15592+ return au_do_flush(file, id, au_do_flush_nondir);
15593+}
15594+
15595+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15596+/*
15597+ * read and write functions acquire [fdi]_rwsem once, but release before
15598+ * mmap_sem. This is because to stop a race condition between mmap(2).
15599+ * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
15600+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15601+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15602+ */
4a4d8108 15603+
b912730e 15604+/* Callers should call au_read_post() or fput() in the end */
521ced18 15605+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15606+{
4a4d8108 15607+ struct file *h_file;
b912730e 15608+ int err;
1facf9fc 15609+
521ced18 15610+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15611+ if (!err) {
15612+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15613+ h_file = au_hf_top(file);
15614+ get_file(h_file);
15615+ if (!keep_fi)
15616+ fi_read_unlock(file);
15617+ } else
15618+ h_file = ERR_PTR(err);
15619+
15620+ return h_file;
15621+}
15622+
15623+static void au_read_post(struct inode *inode, struct file *h_file)
15624+{
15625+ /* update without lock, I don't think it a problem */
15626+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15627+ fput(h_file);
15628+}
15629+
15630+struct au_write_pre {
521ced18
JR
15631+ /* input */
15632+ unsigned int lsc;
15633+
15634+ /* output */
b912730e 15635+ blkcnt_t blks;
5afbbe0d 15636+ aufs_bindex_t btop;
b912730e
AM
15637+};
15638+
15639+/*
15640+ * return with iinfo is write-locked
15641+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15642+ * end
15643+ */
15644+static struct file *au_write_pre(struct file *file, int do_ready,
15645+ struct au_write_pre *wpre)
15646+{
15647+ struct file *h_file;
15648+ struct dentry *dentry;
15649+ int err;
521ced18 15650+ unsigned int lsc;
b912730e
AM
15651+ struct au_pin pin;
15652+
521ced18
JR
15653+ lsc = 0;
15654+ if (wpre)
15655+ lsc = wpre->lsc;
15656+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15657+ h_file = ERR_PTR(err);
dece6358
AM
15658+ if (unlikely(err))
15659+ goto out;
1facf9fc 15660+
b912730e
AM
15661+ dentry = file->f_path.dentry;
15662+ if (do_ready) {
15663+ err = au_ready_to_write(file, -1, &pin);
15664+ if (unlikely(err)) {
15665+ h_file = ERR_PTR(err);
15666+ di_write_unlock(dentry);
15667+ goto out_fi;
15668+ }
15669+ }
15670+
15671+ di_downgrade_lock(dentry, /*flags*/0);
15672+ if (wpre)
5afbbe0d 15673+ wpre->btop = au_fbtop(file);
4a4d8108 15674+ h_file = au_hf_top(file);
9dbd164d 15675+ get_file(h_file);
b912730e
AM
15676+ if (wpre)
15677+ wpre->blks = file_inode(h_file)->i_blocks;
15678+ if (do_ready)
15679+ au_unpin(&pin);
15680+ di_read_unlock(dentry, /*flags*/0);
15681+
15682+out_fi:
15683+ fi_write_unlock(file);
15684+out:
15685+ return h_file;
15686+}
15687+
15688+static void au_write_post(struct inode *inode, struct file *h_file,
15689+ struct au_write_pre *wpre, ssize_t written)
15690+{
15691+ struct inode *h_inode;
15692+
15693+ au_cpup_attr_timesizes(inode);
5afbbe0d 15694+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15695+ h_inode = file_inode(h_file);
15696+ inode->i_mode = h_inode->i_mode;
15697+ ii_write_unlock(inode);
b912730e
AM
15698+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15699+ if (written > 0)
5afbbe0d 15700+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15701+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15702+ fput(h_file);
b912730e
AM
15703+}
15704+
15705+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15706+ loff_t *ppos)
15707+{
15708+ ssize_t err;
15709+ struct inode *inode;
15710+ struct file *h_file;
15711+ struct super_block *sb;
15712+
15713+ inode = file_inode(file);
15714+ sb = inode->i_sb;
15715+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15716+
521ced18 15717+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15718+ err = PTR_ERR(h_file);
15719+ if (IS_ERR(h_file))
15720+ goto out;
9dbd164d
AM
15721+
15722+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
15723+ err = vfsub_read_u(h_file, buf, count, ppos);
15724+ /* todo: necessary? */
15725+ /* file->f_ra = h_file->f_ra; */
b912730e 15726+ au_read_post(inode, h_file);
1308ab2a 15727+
4f0767ce 15728+out:
dece6358
AM
15729+ si_read_unlock(sb);
15730+ return err;
15731+}
1facf9fc 15732+
e49829fe
JR
15733+/*
15734+ * todo: very ugly
15735+ * it locks both of i_mutex and si_rwsem for read in safe.
15736+ * if the plink maintenance mode continues forever (that is the problem),
15737+ * may loop forever.
15738+ */
15739+static void au_mtx_and_read_lock(struct inode *inode)
15740+{
15741+ int err;
15742+ struct super_block *sb = inode->i_sb;
15743+
15744+ while (1) {
febd17d6 15745+ inode_lock(inode);
e49829fe
JR
15746+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15747+ if (!err)
15748+ break;
febd17d6 15749+ inode_unlock(inode);
e49829fe
JR
15750+ si_read_lock(sb, AuLock_NOPLMW);
15751+ si_read_unlock(sb);
15752+ }
15753+}
15754+
4a4d8108
AM
15755+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15756+ size_t count, loff_t *ppos)
dece6358 15757+{
4a4d8108 15758+ ssize_t err;
b912730e
AM
15759+ struct au_write_pre wpre;
15760+ struct inode *inode;
4a4d8108
AM
15761+ struct file *h_file;
15762+ char __user *buf = (char __user *)ubuf;
1facf9fc 15763+
b912730e 15764+ inode = file_inode(file);
e49829fe 15765+ au_mtx_and_read_lock(inode);
1facf9fc 15766+
521ced18 15767+ wpre.lsc = 0;
b912730e
AM
15768+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15769+ err = PTR_ERR(h_file);
15770+ if (IS_ERR(h_file))
9dbd164d 15771+ goto out;
9dbd164d 15772+
4a4d8108 15773+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 15774+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15775+
4f0767ce 15776+out:
b912730e 15777+ si_read_unlock(inode->i_sb);
febd17d6 15778+ inode_unlock(inode);
dece6358
AM
15779+ return err;
15780+}
1facf9fc 15781+
076b876e
AM
15782+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15783+ struct iov_iter *iov_iter)
dece6358 15784+{
4a4d8108
AM
15785+ ssize_t err;
15786+ struct file *file;
076b876e 15787+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15788+
4a4d8108
AM
15789+ err = security_file_permission(h_file, rw);
15790+ if (unlikely(err))
15791+ goto out;
1facf9fc 15792+
4a4d8108 15793+ err = -ENOSYS;
076b876e 15794+ iter = NULL;
5527c038 15795+ if (rw == MAY_READ)
076b876e 15796+ iter = h_file->f_op->read_iter;
5527c038 15797+ else if (rw == MAY_WRITE)
076b876e 15798+ iter = h_file->f_op->write_iter;
076b876e
AM
15799+
15800+ file = kio->ki_filp;
15801+ kio->ki_filp = h_file;
15802+ if (iter) {
2cbb1c4b 15803+ lockdep_off();
076b876e
AM
15804+ err = iter(kio, iov_iter);
15805+ lockdep_on();
4a4d8108
AM
15806+ } else
15807+ /* currently there is no such fs */
15808+ WARN_ON_ONCE(1);
076b876e 15809+ kio->ki_filp = file;
1facf9fc 15810+
4f0767ce 15811+out:
dece6358
AM
15812+ return err;
15813+}
1facf9fc 15814+
076b876e 15815+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15816+{
4a4d8108
AM
15817+ ssize_t err;
15818+ struct file *file, *h_file;
b912730e 15819+ struct inode *inode;
dece6358 15820+ struct super_block *sb;
1facf9fc 15821+
4a4d8108 15822+ file = kio->ki_filp;
b912730e
AM
15823+ inode = file_inode(file);
15824+ sb = inode->i_sb;
e49829fe 15825+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15826+
521ced18 15827+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15828+ err = PTR_ERR(h_file);
15829+ if (IS_ERR(h_file))
15830+ goto out;
9dbd164d 15831+
5afbbe0d
AM
15832+ if (au_test_loopback_kthread()) {
15833+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15834+ if (file->f_mapping != h_file->f_mapping) {
15835+ file->f_mapping = h_file->f_mapping;
15836+ smp_mb(); /* unnecessary? */
15837+ }
15838+ }
15839+ fi_read_unlock(file);
15840+
076b876e 15841+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15842+ /* todo: necessary? */
15843+ /* file->f_ra = h_file->f_ra; */
b912730e 15844+ au_read_post(inode, h_file);
1facf9fc 15845+
4f0767ce 15846+out:
4a4d8108 15847+ si_read_unlock(sb);
1308ab2a 15848+ return err;
15849+}
1facf9fc 15850+
076b876e 15851+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15852+{
4a4d8108 15853+ ssize_t err;
b912730e
AM
15854+ struct au_write_pre wpre;
15855+ struct inode *inode;
4a4d8108 15856+ struct file *file, *h_file;
1308ab2a 15857+
4a4d8108 15858+ file = kio->ki_filp;
b912730e 15859+ inode = file_inode(file);
e49829fe
JR
15860+ au_mtx_and_read_lock(inode);
15861+
521ced18 15862+ wpre.lsc = 0;
b912730e
AM
15863+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15864+ err = PTR_ERR(h_file);
15865+ if (IS_ERR(h_file))
9dbd164d 15866+ goto out;
9dbd164d 15867+
076b876e 15868+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 15869+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15870+
4f0767ce 15871+out:
b912730e 15872+ si_read_unlock(inode->i_sb);
febd17d6 15873+ inode_unlock(inode);
dece6358 15874+ return err;
1facf9fc 15875+}
15876+
4a4d8108
AM
15877+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15878+ struct pipe_inode_info *pipe, size_t len,
15879+ unsigned int flags)
1facf9fc 15880+{
4a4d8108
AM
15881+ ssize_t err;
15882+ struct file *h_file;
b912730e 15883+ struct inode *inode;
dece6358 15884+ struct super_block *sb;
1facf9fc 15885+
b912730e
AM
15886+ inode = file_inode(file);
15887+ sb = inode->i_sb;
e49829fe 15888+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 15889+
521ced18 15890+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15891+ err = PTR_ERR(h_file);
15892+ if (IS_ERR(h_file))
dece6358 15893+ goto out;
1facf9fc 15894+
4a4d8108
AM
15895+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15896+ /* todo: necessasry? */
15897+ /* file->f_ra = h_file->f_ra; */
b912730e 15898+ au_read_post(inode, h_file);
1facf9fc 15899+
4f0767ce 15900+out:
4a4d8108 15901+ si_read_unlock(sb);
dece6358 15902+ return err;
1facf9fc 15903+}
15904+
4a4d8108
AM
15905+static ssize_t
15906+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15907+ size_t len, unsigned int flags)
1facf9fc 15908+{
4a4d8108 15909+ ssize_t err;
b912730e
AM
15910+ struct au_write_pre wpre;
15911+ struct inode *inode;
076b876e 15912+ struct file *h_file;
1facf9fc 15913+
b912730e 15914+ inode = file_inode(file);
e49829fe 15915+ au_mtx_and_read_lock(inode);
9dbd164d 15916+
521ced18 15917+ wpre.lsc = 0;
b912730e
AM
15918+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15919+ err = PTR_ERR(h_file);
15920+ if (IS_ERR(h_file))
9dbd164d 15921+ goto out;
9dbd164d 15922+
4a4d8108 15923+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 15924+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15925+
4f0767ce 15926+out:
b912730e 15927+ si_read_unlock(inode->i_sb);
febd17d6 15928+ inode_unlock(inode);
4a4d8108
AM
15929+ return err;
15930+}
1facf9fc 15931+
38d290e6
JR
15932+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15933+ loff_t len)
15934+{
15935+ long err;
b912730e 15936+ struct au_write_pre wpre;
38d290e6
JR
15937+ struct inode *inode;
15938+ struct file *h_file;
15939+
b912730e 15940+ inode = file_inode(file);
38d290e6
JR
15941+ au_mtx_and_read_lock(inode);
15942+
521ced18 15943+ wpre.lsc = 0;
b912730e
AM
15944+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15945+ err = PTR_ERR(h_file);
15946+ if (IS_ERR(h_file))
38d290e6 15947+ goto out;
38d290e6
JR
15948+
15949+ lockdep_off();
03673fb0 15950+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 15951+ lockdep_on();
b912730e 15952+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
15953+
15954+out:
b912730e 15955+ si_read_unlock(inode->i_sb);
febd17d6 15956+ inode_unlock(inode);
38d290e6
JR
15957+ return err;
15958+}
15959+
521ced18
JR
15960+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15961+ struct file *dst, loff_t dst_pos,
15962+ size_t len, unsigned int flags)
15963+{
15964+ ssize_t err;
15965+ struct au_write_pre wpre;
15966+ enum { SRC, DST };
15967+ struct {
15968+ struct inode *inode;
15969+ struct file *h_file;
15970+ struct super_block *h_sb;
15971+ } a[2];
15972+#define a_src a[SRC]
15973+#define a_dst a[DST]
15974+
15975+ err = -EINVAL;
15976+ a_src.inode = file_inode(src);
15977+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15978+ goto out;
15979+ a_dst.inode = file_inode(dst);
15980+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15981+ goto out;
15982+
15983+ au_mtx_and_read_lock(a_dst.inode);
15984+ /*
15985+ * in order to match the order in di_write_lock2_{child,parent}(),
15986+ * use f_path.dentry for this comparision.
15987+ */
15988+ if (src->f_path.dentry < dst->f_path.dentry) {
15989+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15990+ err = PTR_ERR(a_src.h_file);
15991+ if (IS_ERR(a_src.h_file))
15992+ goto out_si;
15993+
15994+ wpre.lsc = AuLsc_FI_2;
15995+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15996+ err = PTR_ERR(a_dst.h_file);
15997+ if (IS_ERR(a_dst.h_file)) {
15998+ au_read_post(a_src.inode, a_src.h_file);
15999+ goto out_si;
16000+ }
16001+ } else {
16002+ wpre.lsc = AuLsc_FI_1;
16003+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16004+ err = PTR_ERR(a_dst.h_file);
16005+ if (IS_ERR(a_dst.h_file))
16006+ goto out_si;
16007+
16008+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16009+ err = PTR_ERR(a_src.h_file);
16010+ if (IS_ERR(a_src.h_file)) {
16011+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16012+ /*written*/0);
16013+ goto out_si;
16014+ }
16015+ }
16016+
16017+ err = -EXDEV;
16018+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16019+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16020+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16021+ AuDbgFile(src);
16022+ AuDbgFile(dst);
16023+ goto out_file;
16024+ }
16025+
16026+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16027+ dst_pos, len, flags);
16028+
16029+out_file:
16030+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16031+ fi_read_unlock(src);
16032+ au_read_post(a_src.inode, a_src.h_file);
16033+out_si:
16034+ si_read_unlock(a_dst.inode->i_sb);
16035+ inode_unlock(a_dst.inode);
16036+out:
16037+ return err;
16038+#undef a_src
16039+#undef a_dst
16040+}
16041+
4a4d8108
AM
16042+/* ---------------------------------------------------------------------- */
16043+
9dbd164d
AM
16044+/*
16045+ * The locking order around current->mmap_sem.
16046+ * - in most and regular cases
16047+ * file I/O syscall -- aufs_read() or something
16048+ * -- si_rwsem for read -- mmap_sem
16049+ * (Note that [fdi]i_rwsem are released before mmap_sem).
16050+ * - in mmap case
16051+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16052+ * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
16053+ * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
16054+ * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16055+ * It means that when aufs acquires si_rwsem for write, the process should never
16056+ * acquire mmap_sem.
16057+ *
392086de 16058+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
16059+ * problem either since any directory is not able to be mmap-ed.
16060+ * The similar scenario is applied to aufs_readlink() too.
16061+ */
16062+
38d290e6 16063+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16064+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16065+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16066+
16067+static unsigned long au_arch_prot_conv(unsigned long flags)
16068+{
16069+ /* currently ppc64 only */
16070+#ifdef CONFIG_PPC64
16071+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16072+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16073+ return AuConv_VM_PROT(flags, SAO);
16074+#else
16075+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16076+ return 0;
16077+#endif
16078+}
16079+
16080+static unsigned long au_prot_conv(unsigned long flags)
16081+{
16082+ return AuConv_VM_PROT(flags, READ)
16083+ | AuConv_VM_PROT(flags, WRITE)
16084+ | AuConv_VM_PROT(flags, EXEC)
16085+ | au_arch_prot_conv(flags);
16086+}
16087+
16088+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16089+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16090+
16091+static unsigned long au_flag_conv(unsigned long flags)
16092+{
16093+ return AuConv_VM_MAP(flags, GROWSDOWN)
16094+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16095+ | AuConv_VM_MAP(flags, LOCKED);
16096+}
38d290e6 16097+#endif
2dfbb274 16098+
9dbd164d 16099+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16100+{
4a4d8108 16101+ int err;
4a4d8108 16102+ const unsigned char wlock
9dbd164d 16103+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16104+ struct super_block *sb;
9dbd164d 16105+ struct file *h_file;
b912730e 16106+ struct inode *inode;
9dbd164d
AM
16107+
16108+ AuDbgVmRegion(file, vma);
1308ab2a 16109+
b912730e
AM
16110+ inode = file_inode(file);
16111+ sb = inode->i_sb;
9dbd164d 16112+ lockdep_off();
e49829fe 16113+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16114+
b912730e 16115+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16116+ lockdep_on();
b912730e
AM
16117+ err = PTR_ERR(h_file);
16118+ if (IS_ERR(h_file))
16119+ goto out;
1308ab2a 16120+
b912730e
AM
16121+ err = 0;
16122+ au_set_mmapped(file);
9dbd164d 16123+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16124+ /*
16125+ * we cannot call security_mmap_file() here since it may acquire
16126+ * mmap_sem or i_mutex.
16127+ *
16128+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16129+ * au_flag_conv(vma->vm_flags));
16130+ */
9dbd164d 16131+ if (!err)
521ced18 16132+ err = call_mmap(h_file, vma);
b912730e
AM
16133+ if (!err) {
16134+ au_vm_prfile_set(vma, file);
16135+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16136+ goto out_fput; /* success */
16137+ }
2cbb1c4b
JR
16138+ au_unset_mmapped(file);
16139+ au_vm_file_reset(vma, file);
b912730e 16140+
2cbb1c4b 16141+out_fput:
9dbd164d 16142+ lockdep_off();
b912730e
AM
16143+ ii_write_unlock(inode);
16144+ lockdep_on();
16145+ fput(h_file);
4f0767ce 16146+out:
b912730e 16147+ lockdep_off();
9dbd164d
AM
16148+ si_read_unlock(sb);
16149+ lockdep_on();
16150+ AuTraceErr(err);
4a4d8108
AM
16151+ return err;
16152+}
16153+
16154+/* ---------------------------------------------------------------------- */
16155+
1e00d052
AM
16156+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16157+ int datasync)
4a4d8108
AM
16158+{
16159+ int err;
b912730e 16160+ struct au_write_pre wpre;
4a4d8108
AM
16161+ struct inode *inode;
16162+ struct file *h_file;
4a4d8108
AM
16163+
16164+ err = 0; /* -EBADF; */ /* posix? */
16165+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16166+ goto out;
4a4d8108 16167+
b912730e
AM
16168+ inode = file_inode(file);
16169+ au_mtx_and_read_lock(inode);
16170+
521ced18 16171+ wpre.lsc = 0;
b912730e
AM
16172+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16173+ err = PTR_ERR(h_file);
16174+ if (IS_ERR(h_file))
4a4d8108 16175+ goto out_unlock;
4a4d8108 16176+
53392da6 16177+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16178+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16179+
4f0767ce 16180+out_unlock:
b912730e 16181+ si_read_unlock(inode->i_sb);
febd17d6 16182+ inode_unlock(inode);
b912730e 16183+out:
4a4d8108 16184+ return err;
dece6358
AM
16185+}
16186+
4a4d8108 16187+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16188+{
4a4d8108
AM
16189+ int err;
16190+ struct file *h_file;
4a4d8108 16191+ struct super_block *sb;
1308ab2a 16192+
b912730e 16193+ sb = file->f_path.dentry->d_sb;
e49829fe 16194+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16195+
521ced18 16196+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16197+ err = PTR_ERR(h_file);
16198+ if (IS_ERR(h_file))
4a4d8108
AM
16199+ goto out;
16200+
523b37e3 16201+ if (h_file->f_op->fasync)
4a4d8108 16202+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16203+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16204+
4f0767ce 16205+out:
4a4d8108 16206+ si_read_unlock(sb);
1308ab2a 16207+ return err;
dece6358 16208+}
4a4d8108 16209+
febd17d6
JR
16210+static int aufs_setfl(struct file *file, unsigned long arg)
16211+{
16212+ int err;
16213+ struct file *h_file;
16214+ struct super_block *sb;
16215+
16216+ sb = file->f_path.dentry->d_sb;
16217+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16218+
521ced18 16219+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16220+ err = PTR_ERR(h_file);
16221+ if (IS_ERR(h_file))
16222+ goto out;
16223+
1c60b727
AM
16224+ /* stop calling h_file->fasync */
16225+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16226+ err = setfl(/*unused fd*/-1, h_file, arg);
16227+ fput(h_file); /* instead of au_read_post() */
16228+
16229+out:
16230+ si_read_unlock(sb);
16231+ return err;
16232+}
16233+
4a4d8108
AM
16234+/* ---------------------------------------------------------------------- */
16235+
16236+/* no one supports this operation, currently */
16237+#if 0
16238+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16239+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16240+{
16241+}
16242+#endif
16243+
16244+/* ---------------------------------------------------------------------- */
16245+
16246+const struct file_operations aufs_file_fop = {
16247+ .owner = THIS_MODULE,
2cbb1c4b 16248+
027c5e7a 16249+ .llseek = default_llseek,
4a4d8108
AM
16250+
16251+ .read = aufs_read,
16252+ .write = aufs_write,
076b876e
AM
16253+ .read_iter = aufs_read_iter,
16254+ .write_iter = aufs_write_iter,
16255+
4a4d8108
AM
16256+#ifdef CONFIG_AUFS_POLL
16257+ .poll = aufs_poll,
16258+#endif
16259+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16260+#ifdef CONFIG_COMPAT
c2b27bf2 16261+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16262+#endif
4a4d8108
AM
16263+ .mmap = aufs_mmap,
16264+ .open = aufs_open_nondir,
16265+ .flush = aufs_flush_nondir,
16266+ .release = aufs_release_nondir,
16267+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16268+ .fasync = aufs_fasync,
16269+ /* .sendpage = aufs_sendpage, */
febd17d6 16270+ .setfl = aufs_setfl,
4a4d8108
AM
16271+ .splice_write = aufs_splice_write,
16272+ .splice_read = aufs_splice_read,
16273+#if 0
16274+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16275+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16276+#endif
521ced18
JR
16277+ .fallocate = aufs_fallocate,
16278+ .copy_file_range = aufs_copy_file_range
4a4d8108 16279+};
7f207e10
AM
16280diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16281--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
be118d29 16282+++ linux/fs/aufs/fstype.h 2018-04-06 07:48:44.204604724 +0200
b912730e 16283@@ -0,0 +1,400 @@
4a4d8108 16284+/*
a2654f78 16285+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
16286+ *
16287+ * This program, aufs is free software; you can redistribute it and/or modify
16288+ * it under the terms of the GNU General Public License as published by
16289+ * the Free Software Foundation; either version 2 of the License, or
16290+ * (at your option) any later version.
16291+ *
16292+ * This program is distributed in the hope that it will be useful,
16293+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16294+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16295+ * GNU General Public License for more details.
16296+ *
16297+ * You should have received a copy of the GNU General Public License
523b37e3 16298+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16299+ */
16300+
16301+/*
16302+ * judging filesystem type
16303+ */
16304+
16305+#ifndef __AUFS_FSTYPE_H__
16306+#define __AUFS_FSTYPE_H__
16307+
16308+#ifdef __KERNEL__
16309+
16310+#include <linux/fs.h>
16311+#include <linux/magic.h>
b912730e 16312+#include <linux/nfs_fs.h>
b95c5147 16313+#include <linux/romfs_fs.h>
4a4d8108
AM
16314+
16315+static inline int au_test_aufs(struct super_block *sb)
16316+{
16317+ return sb->s_magic == AUFS_SUPER_MAGIC;
16318+}
16319+
16320+static inline const char *au_sbtype(struct super_block *sb)
16321+{
16322+ return sb->s_type->name;
16323+}
1308ab2a 16324+
16325+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16326+{
f0c0a007 16327+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16328+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16329+#else
16330+ return 0;
16331+#endif
16332+}
16333+
1308ab2a 16334+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16335+{
f0c0a007 16336+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16337+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16338+#else
16339+ return 0;
16340+#endif
16341+}
16342+
1308ab2a 16343+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16344+{
f0c0a007 16345+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16346+ return sb->s_magic == CRAMFS_MAGIC;
16347+#endif
16348+ return 0;
16349+}
16350+
16351+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16352+{
f0c0a007 16353+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16354+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16355+#else
16356+ return 0;
16357+#endif
16358+}
16359+
1308ab2a 16360+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16361+{
f0c0a007 16362+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16363+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16364+#else
16365+ return 0;
16366+#endif
16367+}
16368+
1308ab2a 16369+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16370+{
f0c0a007 16371+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16372+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16373+#else
16374+ return 0;
16375+#endif
16376+}
16377+
1308ab2a 16378+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16379+{
1308ab2a 16380+#ifdef CONFIG_TMPFS
16381+ return sb->s_magic == TMPFS_MAGIC;
16382+#else
16383+ return 0;
dece6358 16384+#endif
dece6358
AM
16385+}
16386+
1308ab2a 16387+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16388+{
f0c0a007 16389+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16390+ return !strcmp(au_sbtype(sb), "ecryptfs");
16391+#else
16392+ return 0;
16393+#endif
1facf9fc 16394+}
16395+
1308ab2a 16396+static inline int au_test_ramfs(struct super_block *sb)
16397+{
16398+ return sb->s_magic == RAMFS_MAGIC;
16399+}
16400+
16401+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16402+{
f0c0a007 16403+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16404+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16405+#else
16406+ return 0;
16407+#endif
16408+}
16409+
16410+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16411+{
16412+#ifdef CONFIG_PROC_FS
16413+ return sb->s_magic == PROC_SUPER_MAGIC;
16414+#else
16415+ return 0;
16416+#endif
16417+}
16418+
16419+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16420+{
16421+#ifdef CONFIG_SYSFS
16422+ return sb->s_magic == SYSFS_MAGIC;
16423+#else
16424+ return 0;
16425+#endif
16426+}
16427+
16428+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16429+{
f0c0a007 16430+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16431+ return sb->s_magic == CONFIGFS_MAGIC;
16432+#else
16433+ return 0;
16434+#endif
16435+}
16436+
16437+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16438+{
f0c0a007 16439+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16440+ return sb->s_magic == MINIX3_SUPER_MAGIC
16441+ || sb->s_magic == MINIX2_SUPER_MAGIC
16442+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16443+ || sb->s_magic == MINIX_SUPER_MAGIC
16444+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16445+#else
16446+ return 0;
16447+#endif
16448+}
16449+
1308ab2a 16450+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16451+{
f0c0a007 16452+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16453+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16454+#else
16455+ return 0;
16456+#endif
16457+}
16458+
16459+static inline int au_test_msdos(struct super_block *sb)
16460+{
16461+ return au_test_fat(sb);
16462+}
16463+
16464+static inline int au_test_vfat(struct super_block *sb)
16465+{
16466+ return au_test_fat(sb);
16467+}
16468+
16469+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16470+{
16471+#ifdef CONFIG_SECURITYFS
16472+ return sb->s_magic == SECURITYFS_MAGIC;
16473+#else
16474+ return 0;
16475+#endif
16476+}
16477+
16478+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16479+{
f0c0a007 16480+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16481+ return sb->s_magic == SQUASHFS_MAGIC;
16482+#else
16483+ return 0;
16484+#endif
16485+}
16486+
16487+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16488+{
f0c0a007 16489+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16490+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16491+#else
16492+ return 0;
16493+#endif
16494+}
16495+
16496+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16497+{
f0c0a007 16498+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16499+ return sb->s_magic == XENFS_SUPER_MAGIC;
16500+#else
16501+ return 0;
16502+#endif
16503+}
16504+
16505+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16506+{
16507+#ifdef CONFIG_DEBUG_FS
16508+ return sb->s_magic == DEBUGFS_MAGIC;
16509+#else
16510+ return 0;
16511+#endif
16512+}
16513+
16514+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16515+{
f0c0a007 16516+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16517+ return sb->s_magic == NILFS_SUPER_MAGIC;
16518+#else
16519+ return 0;
16520+#endif
16521+}
16522+
4a4d8108
AM
16523+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16524+{
f0c0a007 16525+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16526+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16527+#else
16528+ return 0;
16529+#endif
16530+}
16531+
1308ab2a 16532+/* ---------------------------------------------------------------------- */
16533+/*
16534+ * they can't be an aufs branch.
16535+ */
16536+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16537+{
16538+ return
16539+#ifndef CONFIG_AUFS_BR_RAMFS
16540+ au_test_ramfs(sb) ||
16541+#endif
16542+ au_test_procfs(sb)
16543+ || au_test_sysfs(sb)
16544+ || au_test_configfs(sb)
16545+ || au_test_debugfs(sb)
16546+ || au_test_securityfs(sb)
16547+ || au_test_xenfs(sb)
16548+ || au_test_ecryptfs(sb)
16549+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16550+ || au_test_aufs(sb); /* will be supported in next version */
16551+}
16552+
1308ab2a 16553+static inline int au_test_fs_remote(struct super_block *sb)
16554+{
16555+ return !au_test_tmpfs(sb)
16556+#ifdef CONFIG_AUFS_BR_RAMFS
16557+ && !au_test_ramfs(sb)
16558+#endif
16559+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16560+}
16561+
16562+/* ---------------------------------------------------------------------- */
16563+
16564+/*
16565+ * Note: these functions (below) are created after reading ->getattr() in all
16566+ * filesystems under linux/fs. it means we have to do so in every update...
16567+ */
16568+
16569+/*
16570+ * some filesystems require getattr to refresh the inode attributes before
16571+ * referencing.
16572+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16573+ * and leave the work for d_revalidate()
16574+ */
16575+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16576+{
16577+ return au_test_nfs(sb)
16578+ || au_test_fuse(sb)
1308ab2a 16579+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16580+ ;
16581+}
16582+
16583+/*
16584+ * filesystems which don't maintain i_size or i_blocks.
16585+ */
16586+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16587+{
16588+ return au_test_xfs(sb)
4a4d8108
AM
16589+ || au_test_btrfs(sb)
16590+ || au_test_ubifs(sb)
16591+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16592+ /* || au_test_minix(sb) */ /* untested */
16593+ ;
16594+}
16595+
16596+/*
16597+ * filesystems which don't store the correct value in some of their inode
16598+ * attributes.
16599+ */
16600+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16601+{
16602+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16603+ || au_test_fat(sb)
16604+ || au_test_msdos(sb)
16605+ || au_test_vfat(sb);
1facf9fc 16606+}
16607+
16608+/* they don't check i_nlink in link(2) */
16609+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16610+{
16611+ return au_test_tmpfs(sb)
16612+#ifdef CONFIG_AUFS_BR_RAMFS
16613+ || au_test_ramfs(sb)
16614+#endif
4a4d8108 16615+ || au_test_ubifs(sb)
4a4d8108 16616+ || au_test_hfsplus(sb);
1facf9fc 16617+}
16618+
16619+/*
16620+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16621+ */
16622+static inline int au_test_fs_notime(struct super_block *sb)
16623+{
16624+ return au_test_nfs(sb)
16625+ || au_test_fuse(sb)
dece6358 16626+ || au_test_ubifs(sb)
1facf9fc 16627+ ;
16628+}
16629+
1facf9fc 16630+/* temporary support for i#1 in cramfs */
16631+static inline int au_test_fs_unique_ino(struct inode *inode)
16632+{
16633+ if (au_test_cramfs(inode->i_sb))
16634+ return inode->i_ino != 1;
16635+ return 1;
16636+}
16637+
16638+/* ---------------------------------------------------------------------- */
16639+
16640+/*
16641+ * the filesystem where the xino files placed must support i/o after unlink and
16642+ * maintain i_size and i_blocks.
16643+ */
16644+static inline int au_test_fs_bad_xino(struct super_block *sb)
16645+{
16646+ return au_test_fs_remote(sb)
16647+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16648+ /* don't want unnecessary work for xino */
16649+ || au_test_aufs(sb)
1308ab2a 16650+ || au_test_ecryptfs(sb)
16651+ || au_test_nilfs(sb);
1facf9fc 16652+}
16653+
16654+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16655+{
16656+ return au_test_tmpfs(sb)
16657+ || au_test_ramfs(sb);
16658+}
16659+
16660+/*
16661+ * test if the @sb is real-readonly.
16662+ */
16663+static inline int au_test_fs_rr(struct super_block *sb)
16664+{
16665+ return au_test_squashfs(sb)
16666+ || au_test_iso9660(sb)
16667+ || au_test_cramfs(sb)
16668+ || au_test_romfs(sb);
16669+}
16670+
b912730e
AM
16671+/*
16672+ * test if the @inode is nfs with 'noacl' option
2121bcd9 16673+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
16674+ */
16675+static inline int au_test_nfs_noacl(struct inode *inode)
16676+{
16677+ return au_test_nfs(inode->i_sb)
16678+ /* && IS_POSIXACL(inode) */
16679+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16680+}
16681+
1facf9fc 16682+#endif /* __KERNEL__ */
16683+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947
AM
16684diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16685--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
be118d29 16686+++ linux/fs/aufs/hbl.h 2018-04-06 07:48:44.204604724 +0200
8b6a4947
AM
16687@@ -0,0 +1,64 @@
16688+/*
16689+ * Copyright (C) 2017 Junjiro R. Okajima
16690+ *
16691+ * This program, aufs is free software; you can redistribute it and/or modify
16692+ * it under the terms of the GNU General Public License as published by
16693+ * the Free Software Foundation; either version 2 of the License, or
16694+ * (at your option) any later version.
16695+ *
16696+ * This program is distributed in the hope that it will be useful,
16697+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16698+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16699+ * GNU General Public License for more details.
16700+ *
16701+ * You should have received a copy of the GNU General Public License
16702+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16703+ */
16704+
16705+/*
16706+ * helpers for hlist_bl.h
16707+ */
16708+
16709+#ifndef __AUFS_HBL_H__
16710+#define __AUFS_HBL_H__
16711+
16712+#ifdef __KERNEL__
16713+
16714+#include <linux/list_bl.h>
16715+
16716+static inline void au_hbl_add(struct hlist_bl_node *node,
16717+ struct hlist_bl_head *hbl)
16718+{
16719+ hlist_bl_lock(hbl);
16720+ hlist_bl_add_head(node, hbl);
16721+ hlist_bl_unlock(hbl);
16722+}
16723+
16724+static inline void au_hbl_del(struct hlist_bl_node *node,
16725+ struct hlist_bl_head *hbl)
16726+{
16727+ hlist_bl_lock(hbl);
16728+ hlist_bl_del(node);
16729+ hlist_bl_unlock(hbl);
16730+}
16731+
16732+#define au_hbl_for_each(pos, head) \
16733+ for (pos = hlist_bl_first(head); \
16734+ pos; \
16735+ pos = pos->next)
16736+
16737+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16738+{
16739+ unsigned long cnt;
16740+ struct hlist_bl_node *pos;
16741+
16742+ cnt = 0;
16743+ hlist_bl_lock(hbl);
16744+ au_hbl_for_each(pos, hbl)
16745+ cnt++;
16746+ hlist_bl_unlock(hbl);
16747+ return cnt;
16748+}
16749+
16750+#endif /* __KERNEL__ */
16751+#endif /* __AUFS_HBL_H__ */
7f207e10
AM
16752diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16753--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
be118d29 16754+++ linux/fs/aufs/hfsnotify.c 2018-04-06 07:48:44.204604724 +0200
ffa93bbd 16755@@ -0,0 +1,289 @@
1facf9fc 16756+/*
a2654f78 16757+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 16758+ *
16759+ * This program, aufs is free software; you can redistribute it and/or modify
16760+ * it under the terms of the GNU General Public License as published by
16761+ * the Free Software Foundation; either version 2 of the License, or
16762+ * (at your option) any later version.
dece6358
AM
16763+ *
16764+ * This program is distributed in the hope that it will be useful,
16765+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16766+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16767+ * GNU General Public License for more details.
16768+ *
16769+ * You should have received a copy of the GNU General Public License
523b37e3 16770+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16771+ */
16772+
16773+/*
4a4d8108 16774+ * fsnotify for the lower directories
1facf9fc 16775+ */
16776+
16777+#include "aufs.h"
16778+
4a4d8108
AM
16779+/* FS_IN_IGNORED is unnecessary */
16780+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16781+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16782+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16783+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16784+
0c5527e5 16785+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16786+{
0c5527e5
AM
16787+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16788+ hn_mark);
5afbbe0d 16789+ /* AuDbg("here\n"); */
1c60b727 16790+ au_cache_free_hnotify(hn);
8b6a4947 16791+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16792+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16793+ wake_up(&au_hfsn_wq);
4a4d8108 16794+}
1facf9fc 16795+
027c5e7a 16796+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16797+{
1716fcea 16798+ int err;
027c5e7a
AM
16799+ struct au_hnotify *hn;
16800+ struct super_block *sb;
16801+ struct au_branch *br;
0c5527e5 16802+ struct fsnotify_mark *mark;
027c5e7a 16803+ aufs_bindex_t bindex;
1facf9fc 16804+
027c5e7a
AM
16805+ hn = hinode->hi_notify;
16806+ sb = hn->hn_aufs_inode->i_sb;
16807+ bindex = au_br_index(sb, hinode->hi_id);
16808+ br = au_sbr(sb, bindex);
1716fcea
AM
16809+ AuDebugOn(!br->br_hfsn);
16810+
0c5527e5 16811+ mark = &hn->hn_mark;
ffa93bbd 16812+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16813+ mark->mask = AuHfsnMask;
7f207e10
AM
16814+ /*
16815+ * by udba rename or rmdir, aufs assign a new inode to the known
16816+ * h_inode, so specify 1 to allow dups.
16817+ */
c1595e42 16818+ lockdep_off();
ffa93bbd
AM
16819+ err = fsnotify_add_mark(mark, hinode->hi_inode, /*mnt*/NULL,
16820+ /*allow_dups*/1);
c1595e42 16821+ lockdep_on();
1716fcea
AM
16822+
16823+ return err;
1facf9fc 16824+}
16825+
7eafdf33 16826+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16827+{
0c5527e5 16828+ struct fsnotify_mark *mark;
7eafdf33 16829+ unsigned long long ull;
1716fcea 16830+ struct fsnotify_group *group;
7eafdf33
AM
16831+
16832+ ull = atomic64_inc_return(&au_hfsn_ifree);
16833+ BUG_ON(!ull);
953406b4 16834+
0c5527e5 16835+ mark = &hn->hn_mark;
1716fcea
AM
16836+ spin_lock(&mark->lock);
16837+ group = mark->group;
16838+ fsnotify_get_group(group);
16839+ spin_unlock(&mark->lock);
c1595e42 16840+ lockdep_off();
1716fcea 16841+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16842+ fsnotify_put_mark(mark);
1716fcea 16843+ fsnotify_put_group(group);
c1595e42 16844+ lockdep_on();
7f207e10 16845+
7eafdf33
AM
16846+ /* free hn by myself */
16847+ return 0;
1facf9fc 16848+}
16849+
16850+/* ---------------------------------------------------------------------- */
16851+
4a4d8108 16852+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16853+{
0c5527e5 16854+ struct fsnotify_mark *mark;
1facf9fc 16855+
0c5527e5
AM
16856+ mark = &hinode->hi_notify->hn_mark;
16857+ spin_lock(&mark->lock);
1facf9fc 16858+ if (do_set) {
0c5527e5
AM
16859+ AuDebugOn(mark->mask & AuHfsnMask);
16860+ mark->mask |= AuHfsnMask;
1facf9fc 16861+ } else {
0c5527e5
AM
16862+ AuDebugOn(!(mark->mask & AuHfsnMask));
16863+ mark->mask &= ~AuHfsnMask;
1facf9fc 16864+ }
0c5527e5 16865+ spin_unlock(&mark->lock);
4a4d8108 16866+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 16867+}
16868+
4a4d8108 16869+/* ---------------------------------------------------------------------- */
1facf9fc 16870+
4a4d8108
AM
16871+/* #define AuDbgHnotify */
16872+#ifdef AuDbgHnotify
16873+static char *au_hfsn_name(u32 mask)
16874+{
16875+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
16876+#define test_ret(flag) \
16877+ do { \
16878+ if (mask & flag) \
16879+ return #flag; \
16880+ } while (0)
4a4d8108
AM
16881+ test_ret(FS_ACCESS);
16882+ test_ret(FS_MODIFY);
16883+ test_ret(FS_ATTRIB);
16884+ test_ret(FS_CLOSE_WRITE);
16885+ test_ret(FS_CLOSE_NOWRITE);
16886+ test_ret(FS_OPEN);
16887+ test_ret(FS_MOVED_FROM);
16888+ test_ret(FS_MOVED_TO);
16889+ test_ret(FS_CREATE);
16890+ test_ret(FS_DELETE);
16891+ test_ret(FS_DELETE_SELF);
16892+ test_ret(FS_MOVE_SELF);
16893+ test_ret(FS_UNMOUNT);
16894+ test_ret(FS_Q_OVERFLOW);
16895+ test_ret(FS_IN_IGNORED);
b912730e 16896+ test_ret(FS_ISDIR);
4a4d8108
AM
16897+ test_ret(FS_IN_ONESHOT);
16898+ test_ret(FS_EVENT_ON_CHILD);
16899+ return "";
16900+#undef test_ret
16901+#else
16902+ return "??";
16903+#endif
1facf9fc 16904+}
4a4d8108 16905+#endif
1facf9fc 16906+
16907+/* ---------------------------------------------------------------------- */
16908+
1716fcea
AM
16909+static void au_hfsn_free_group(struct fsnotify_group *group)
16910+{
16911+ struct au_br_hfsnotify *hfsn = group->private;
16912+
5afbbe0d 16913+ /* AuDbg("here\n"); */
1c60b727 16914+ kfree(hfsn);
1716fcea
AM
16915+}
16916+
4a4d8108 16917+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 16918+ struct inode *inode,
0c5527e5
AM
16919+ struct fsnotify_mark *inode_mark,
16920+ struct fsnotify_mark *vfsmount_mark,
a2654f78 16921+ u32 mask, const void *data, int data_type,
ffa93bbd
AM
16922+ const unsigned char *file_name, u32 cookie,
16923+ struct fsnotify_iter_info *iter_info)
1facf9fc 16924+{
16925+ int err;
4a4d8108
AM
16926+ struct au_hnotify *hnotify;
16927+ struct inode *h_dir, *h_inode;
fb47a38f 16928+ struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
4a4d8108 16929+
fb47a38f 16930+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 16931+
16932+ err = 0;
0c5527e5 16933+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 16934+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 16935+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 16936+ goto out;
1facf9fc 16937+
fb47a38f
JR
16938+ h_dir = inode;
16939+ h_inode = NULL;
4a4d8108 16940+#ifdef AuDbgHnotify
392086de 16941+ au_debug_on();
4a4d8108
AM
16942+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16943+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16944+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16945+ h_dir->i_ino, mask, au_hfsn_name(mask),
16946+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16947+ /* WARN_ON(1); */
1facf9fc 16948+ }
392086de 16949+ au_debug_off();
1facf9fc 16950+#endif
4a4d8108 16951+
0c5527e5
AM
16952+ AuDebugOn(!inode_mark);
16953+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16954+ err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
1facf9fc 16955+
4a4d8108
AM
16956+out:
16957+ return err;
16958+}
1facf9fc 16959+
4a4d8108 16960+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 16961+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
16962+ .free_group_priv = au_hfsn_free_group,
16963+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
16964+};
16965+
16966+/* ---------------------------------------------------------------------- */
16967+
027c5e7a
AM
16968+static void au_hfsn_fin_br(struct au_branch *br)
16969+{
1716fcea 16970+ struct au_br_hfsnotify *hfsn;
027c5e7a 16971+
1716fcea 16972+ hfsn = br->br_hfsn;
c1595e42
JR
16973+ if (hfsn) {
16974+ lockdep_off();
1716fcea 16975+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
16976+ lockdep_on();
16977+ }
027c5e7a
AM
16978+}
16979+
1716fcea 16980+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
16981+{
16982+ int err;
1716fcea
AM
16983+ struct fsnotify_group *group;
16984+ struct au_br_hfsnotify *hfsn;
1facf9fc 16985+
4a4d8108 16986+ err = 0;
1716fcea
AM
16987+ br->br_hfsn = NULL;
16988+ if (!au_br_hnotifyable(perm))
027c5e7a 16989+ goto out;
027c5e7a 16990+
1716fcea
AM
16991+ err = -ENOMEM;
16992+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16993+ if (unlikely(!hfsn))
027c5e7a
AM
16994+ goto out;
16995+
1716fcea
AM
16996+ err = 0;
16997+ group = fsnotify_alloc_group(&au_hfsn_ops);
16998+ if (IS_ERR(group)) {
16999+ err = PTR_ERR(group);
0c5527e5 17000+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 17001+ goto out_hfsn;
4a4d8108 17002+ }
1facf9fc 17003+
1716fcea
AM
17004+ group->private = hfsn;
17005+ hfsn->hfsn_group = group;
17006+ br->br_hfsn = hfsn;
17007+ goto out; /* success */
17008+
17009+out_hfsn:
1c60b727 17010+ kfree(hfsn);
027c5e7a 17011+out:
1716fcea
AM
17012+ return err;
17013+}
17014+
17015+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17016+{
17017+ int err;
17018+
17019+ err = 0;
17020+ if (!br->br_hfsn)
17021+ err = au_hfsn_init_br(br, perm);
17022+
1facf9fc 17023+ return err;
17024+}
17025+
7eafdf33
AM
17026+/* ---------------------------------------------------------------------- */
17027+
17028+static void au_hfsn_fin(void)
17029+{
17030+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17031+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17032+}
17033+
4a4d8108
AM
17034+const struct au_hnotify_op au_hnotify_op = {
17035+ .ctl = au_hfsn_ctl,
17036+ .alloc = au_hfsn_alloc,
17037+ .free = au_hfsn_free,
1facf9fc 17038+
7eafdf33
AM
17039+ .fin = au_hfsn_fin,
17040+
027c5e7a
AM
17041+ .reset_br = au_hfsn_reset_br,
17042+ .fin_br = au_hfsn_fin_br,
17043+ .init_br = au_hfsn_init_br
4a4d8108 17044+};
7f207e10
AM
17045diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17046--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
be118d29 17047+++ linux/fs/aufs/hfsplus.c 2018-04-06 07:48:44.204604724 +0200
523b37e3 17048@@ -0,0 +1,56 @@
4a4d8108 17049+/*
a2654f78 17050+ * Copyright (C) 2010-2017 Junjiro R. Okajima
4a4d8108
AM
17051+ *
17052+ * This program, aufs is free software; you can redistribute it and/or modify
17053+ * it under the terms of the GNU General Public License as published by
17054+ * the Free Software Foundation; either version 2 of the License, or
17055+ * (at your option) any later version.
17056+ *
17057+ * This program is distributed in the hope that it will be useful,
17058+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17059+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17060+ * GNU General Public License for more details.
17061+ *
17062+ * You should have received a copy of the GNU General Public License
523b37e3 17063+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17064+ */
1facf9fc 17065+
4a4d8108
AM
17066+/*
17067+ * special support for filesystems which aqucires an inode mutex
17068+ * at final closing a file, eg, hfsplus.
17069+ *
17070+ * This trick is very simple and stupid, just to open the file before really
17071+ * neceeary open to tell hfsplus that this is not the final closing.
17072+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17073+ * and au_h_open_post() after releasing it.
17074+ */
1facf9fc 17075+
4a4d8108 17076+#include "aufs.h"
1facf9fc 17077+
392086de
AM
17078+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17079+ int force_wr)
4a4d8108
AM
17080+{
17081+ struct file *h_file;
17082+ struct dentry *h_dentry;
1facf9fc 17083+
4a4d8108
AM
17084+ h_dentry = au_h_dptr(dentry, bindex);
17085+ AuDebugOn(!h_dentry);
5527c038 17086+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17087+
17088+ h_file = NULL;
17089+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17090+ && d_is_reg(h_dentry))
4a4d8108
AM
17091+ h_file = au_h_open(dentry, bindex,
17092+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17093+ /*file*/NULL, force_wr);
4a4d8108 17094+ return h_file;
1facf9fc 17095+}
17096+
4a4d8108
AM
17097+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17098+ struct file *h_file)
17099+{
17100+ if (h_file) {
17101+ fput(h_file);
17102+ au_sbr_put(dentry->d_sb, bindex);
17103+ }
17104+}
7f207e10
AM
17105diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17106--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
be118d29 17107+++ linux/fs/aufs/hnotify.c 2018-04-06 07:48:44.204604724 +0200
8b6a4947 17108@@ -0,0 +1,719 @@
e49829fe 17109+/*
a2654f78 17110+ * Copyright (C) 2005-2017 Junjiro R. Okajima
e49829fe
JR
17111+ *
17112+ * This program, aufs is free software; you can redistribute it and/or modify
17113+ * it under the terms of the GNU General Public License as published by
17114+ * the Free Software Foundation; either version 2 of the License, or
17115+ * (at your option) any later version.
17116+ *
17117+ * This program is distributed in the hope that it will be useful,
17118+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17119+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17120+ * GNU General Public License for more details.
17121+ *
17122+ * You should have received a copy of the GNU General Public License
523b37e3 17123+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17124+ */
17125+
17126+/*
7f207e10 17127+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17128+ */
17129+
17130+#include "aufs.h"
17131+
027c5e7a 17132+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17133+{
17134+ int err;
7f207e10 17135+ struct au_hnotify *hn;
1facf9fc 17136+
4a4d8108
AM
17137+ err = -ENOMEM;
17138+ hn = au_cache_alloc_hnotify();
17139+ if (hn) {
17140+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17141+ hinode->hi_notify = hn;
17142+ err = au_hnotify_op.alloc(hinode);
17143+ AuTraceErr(err);
17144+ if (unlikely(err)) {
17145+ hinode->hi_notify = NULL;
1c60b727 17146+ au_cache_free_hnotify(hn);
4a4d8108
AM
17147+ /*
17148+ * The upper dir was removed by udba, but the same named
17149+ * dir left. In this case, aufs assignes a new inode
17150+ * number and set the monitor again.
17151+ * For the lower dir, the old monitnor is still left.
17152+ */
17153+ if (err == -EEXIST)
17154+ err = 0;
17155+ }
1308ab2a 17156+ }
1308ab2a 17157+
027c5e7a 17158+ AuTraceErr(err);
1308ab2a 17159+ return err;
dece6358 17160+}
1facf9fc 17161+
4a4d8108 17162+void au_hn_free(struct au_hinode *hinode)
dece6358 17163+{
4a4d8108 17164+ struct au_hnotify *hn;
1facf9fc 17165+
4a4d8108
AM
17166+ hn = hinode->hi_notify;
17167+ if (hn) {
4a4d8108 17168+ hinode->hi_notify = NULL;
7eafdf33 17169+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17170+ au_cache_free_hnotify(hn);
4a4d8108
AM
17171+ }
17172+}
dece6358 17173+
4a4d8108 17174+/* ---------------------------------------------------------------------- */
dece6358 17175+
4a4d8108
AM
17176+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17177+{
17178+ if (hinode->hi_notify)
17179+ au_hnotify_op.ctl(hinode, do_set);
17180+}
17181+
17182+void au_hn_reset(struct inode *inode, unsigned int flags)
17183+{
5afbbe0d 17184+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17185+ struct inode *hi;
17186+ struct dentry *iwhdentry;
1facf9fc 17187+
5afbbe0d
AM
17188+ bbot = au_ibbot(inode);
17189+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17190+ hi = au_h_iptr(inode, bindex);
17191+ if (!hi)
17192+ continue;
1308ab2a 17193+
febd17d6 17194+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17195+ iwhdentry = au_hi_wh(inode, bindex);
17196+ if (iwhdentry)
17197+ dget(iwhdentry);
17198+ au_igrab(hi);
17199+ au_set_h_iptr(inode, bindex, NULL, 0);
17200+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17201+ flags & ~AuHi_XINO);
17202+ iput(hi);
17203+ dput(iwhdentry);
febd17d6 17204+ /* inode_unlock(hi); */
1facf9fc 17205+ }
1facf9fc 17206+}
17207+
1308ab2a 17208+/* ---------------------------------------------------------------------- */
1facf9fc 17209+
4a4d8108 17210+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17211+{
4a4d8108 17212+ int err;
5afbbe0d 17213+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17214+ struct inode *h_i;
1facf9fc 17215+
4a4d8108
AM
17216+ err = 0;
17217+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17218+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17219+ goto out;
17220+ }
1facf9fc 17221+
4a4d8108 17222+ bfound = -1;
5afbbe0d
AM
17223+ bbot = au_ibbot(inode);
17224+ btop = au_ibtop(inode);
4a4d8108 17225+#if 0 /* reserved for future use */
5afbbe0d 17226+ if (bindex == bbot) {
4a4d8108
AM
17227+ /* keep this ino in rename case */
17228+ goto out;
17229+ }
17230+#endif
5afbbe0d 17231+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17232+ if (au_h_iptr(inode, bindex) == h_inode) {
17233+ bfound = bindex;
17234+ break;
17235+ }
17236+ if (bfound < 0)
1308ab2a 17237+ goto out;
1facf9fc 17238+
5afbbe0d 17239+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17240+ h_i = au_h_iptr(inode, bindex);
17241+ if (!h_i)
17242+ continue;
1facf9fc 17243+
4a4d8108
AM
17244+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17245+ /* ignore this error */
17246+ /* bad action? */
1facf9fc 17247+ }
1facf9fc 17248+
4a4d8108 17249+ /* children inode number will be broken */
1facf9fc 17250+
4f0767ce 17251+out:
4a4d8108
AM
17252+ AuTraceErr(err);
17253+ return err;
1facf9fc 17254+}
17255+
4a4d8108 17256+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17257+{
4a4d8108
AM
17258+ int err, i, j, ndentry;
17259+ struct au_dcsub_pages dpages;
17260+ struct au_dpage *dpage;
17261+ struct dentry **dentries;
1facf9fc 17262+
4a4d8108
AM
17263+ err = au_dpages_init(&dpages, GFP_NOFS);
17264+ if (unlikely(err))
17265+ goto out;
17266+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17267+ if (unlikely(err))
17268+ goto out_dpages;
1facf9fc 17269+
4a4d8108
AM
17270+ for (i = 0; i < dpages.ndpage; i++) {
17271+ dpage = dpages.dpages + i;
17272+ dentries = dpage->dentries;
17273+ ndentry = dpage->ndentry;
17274+ for (j = 0; j < ndentry; j++) {
17275+ struct dentry *d;
17276+
17277+ d = dentries[j];
17278+ if (IS_ROOT(d))
17279+ continue;
17280+
4a4d8108 17281+ au_digen_dec(d);
5527c038 17282+ if (d_really_is_positive(d))
4a4d8108
AM
17283+ /* todo: reset children xino?
17284+ cached children only? */
5527c038 17285+ au_iigen_dec(d_inode(d));
1308ab2a 17286+ }
dece6358 17287+ }
1facf9fc 17288+
4f0767ce 17289+out_dpages:
4a4d8108 17290+ au_dpages_free(&dpages);
dece6358 17291+
027c5e7a 17292+#if 0
4a4d8108
AM
17293+ /* discard children */
17294+ dentry_unhash(dentry);
17295+ dput(dentry);
027c5e7a 17296+#endif
4f0767ce 17297+out:
dece6358
AM
17298+ return err;
17299+}
17300+
1308ab2a 17301+/*
4a4d8108 17302+ * return 0 if processed.
1308ab2a 17303+ */
4a4d8108
AM
17304+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17305+ const unsigned int isdir)
dece6358 17306+{
1308ab2a 17307+ int err;
4a4d8108
AM
17308+ struct dentry *d;
17309+ struct qstr *dname;
1facf9fc 17310+
4a4d8108
AM
17311+ err = 1;
17312+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17313+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17314+ err = 0;
17315+ goto out;
17316+ }
dece6358 17317+
4a4d8108
AM
17318+ if (!isdir) {
17319+ AuDebugOn(!name);
17320+ au_iigen_dec(inode);
027c5e7a 17321+ spin_lock(&inode->i_lock);
c1595e42 17322+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17323+ spin_lock(&d->d_lock);
4a4d8108
AM
17324+ dname = &d->d_name;
17325+ if (dname->len != nlen
027c5e7a
AM
17326+ && memcmp(dname->name, name, nlen)) {
17327+ spin_unlock(&d->d_lock);
4a4d8108 17328+ continue;
027c5e7a 17329+ }
4a4d8108 17330+ err = 0;
4a4d8108
AM
17331+ au_digen_dec(d);
17332+ spin_unlock(&d->d_lock);
17333+ break;
1facf9fc 17334+ }
027c5e7a 17335+ spin_unlock(&inode->i_lock);
1308ab2a 17336+ } else {
027c5e7a 17337+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17338+ d = d_find_any_alias(inode);
4a4d8108
AM
17339+ if (!d) {
17340+ au_iigen_dec(inode);
17341+ goto out;
17342+ }
1facf9fc 17343+
027c5e7a 17344+ spin_lock(&d->d_lock);
4a4d8108 17345+ dname = &d->d_name;
027c5e7a
AM
17346+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17347+ spin_unlock(&d->d_lock);
4a4d8108 17348+ err = hn_gen_tree(d);
027c5e7a
AM
17349+ spin_lock(&d->d_lock);
17350+ }
17351+ spin_unlock(&d->d_lock);
4a4d8108
AM
17352+ dput(d);
17353+ }
1facf9fc 17354+
4f0767ce 17355+out:
4a4d8108 17356+ AuTraceErr(err);
1308ab2a 17357+ return err;
17358+}
dece6358 17359+
4a4d8108 17360+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17361+{
4a4d8108 17362+ int err;
1facf9fc 17363+
5527c038 17364+ if (IS_ROOT(dentry)) {
0c3ec466 17365+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17366+ return 0;
17367+ }
1308ab2a 17368+
4a4d8108
AM
17369+ err = 0;
17370+ if (!isdir) {
4a4d8108 17371+ au_digen_dec(dentry);
5527c038
JR
17372+ if (d_really_is_positive(dentry))
17373+ au_iigen_dec(d_inode(dentry));
4a4d8108 17374+ } else {
027c5e7a 17375+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17376+ if (d_really_is_positive(dentry))
4a4d8108
AM
17377+ err = hn_gen_tree(dentry);
17378+ }
17379+
17380+ AuTraceErr(err);
17381+ return err;
1facf9fc 17382+}
17383+
4a4d8108 17384+/* ---------------------------------------------------------------------- */
1facf9fc 17385+
4a4d8108
AM
17386+/* hnotify job flags */
17387+#define AuHnJob_XINO0 1
17388+#define AuHnJob_GEN (1 << 1)
17389+#define AuHnJob_DIRENT (1 << 2)
17390+#define AuHnJob_ISDIR (1 << 3)
17391+#define AuHnJob_TRYXINO0 (1 << 4)
17392+#define AuHnJob_MNTPNT (1 << 5)
17393+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17394+#define au_fset_hnjob(flags, name) \
17395+ do { (flags) |= AuHnJob_##name; } while (0)
17396+#define au_fclr_hnjob(flags, name) \
17397+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17398+
4a4d8108
AM
17399+enum {
17400+ AuHn_CHILD,
17401+ AuHn_PARENT,
17402+ AuHnLast
17403+};
1facf9fc 17404+
4a4d8108
AM
17405+struct au_hnotify_args {
17406+ struct inode *h_dir, *dir, *h_child_inode;
17407+ u32 mask;
17408+ unsigned int flags[AuHnLast];
17409+ unsigned int h_child_nlen;
17410+ char h_child_name[];
17411+};
1facf9fc 17412+
4a4d8108
AM
17413+struct hn_job_args {
17414+ unsigned int flags;
17415+ struct inode *inode, *h_inode, *dir, *h_dir;
17416+ struct dentry *dentry;
17417+ char *h_name;
17418+ int h_nlen;
17419+};
1308ab2a 17420+
4a4d8108
AM
17421+static int hn_job(struct hn_job_args *a)
17422+{
17423+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17424+ int e;
1308ab2a 17425+
4a4d8108
AM
17426+ /* reset xino */
17427+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17428+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17429+
4a4d8108
AM
17430+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17431+ && a->inode
17432+ && a->h_inode) {
be118d29 17433+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17434+ if (!a->h_inode->i_nlink
17435+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17436+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17437+ inode_unlock_shared(a->h_inode);
1308ab2a 17438+ }
1facf9fc 17439+
4a4d8108
AM
17440+ /* make the generation obsolete */
17441+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17442+ e = -1;
4a4d8108 17443+ if (a->inode)
076b876e 17444+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17445+ isdir);
076b876e 17446+ if (e && a->dentry)
4a4d8108
AM
17447+ hn_gen_by_name(a->dentry, isdir);
17448+ /* ignore this error */
1facf9fc 17449+ }
1facf9fc 17450+
4a4d8108
AM
17451+ /* make dir entries obsolete */
17452+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17453+ struct au_vdir *vdir;
1facf9fc 17454+
4a4d8108
AM
17455+ vdir = au_ivdir(a->inode);
17456+ if (vdir)
17457+ vdir->vd_jiffy = 0;
17458+ /* IMustLock(a->inode); */
be118d29 17459+ /* inode_inc_iversion(a->inode); */
4a4d8108 17460+ }
1facf9fc 17461+
4a4d8108
AM
17462+ /* can do nothing but warn */
17463+ if (au_ftest_hnjob(a->flags, MNTPNT)
17464+ && a->dentry
17465+ && d_mountpoint(a->dentry))
523b37e3 17466+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17467+
4a4d8108 17468+ return 0;
1308ab2a 17469+}
1facf9fc 17470+
1308ab2a 17471+/* ---------------------------------------------------------------------- */
1facf9fc 17472+
4a4d8108
AM
17473+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17474+ struct inode *dir)
1308ab2a 17475+{
4a4d8108
AM
17476+ struct dentry *dentry, *d, *parent;
17477+ struct qstr *dname;
1308ab2a 17478+
c1595e42 17479+ parent = d_find_any_alias(dir);
4a4d8108
AM
17480+ if (!parent)
17481+ return NULL;
1308ab2a 17482+
4a4d8108 17483+ dentry = NULL;
027c5e7a 17484+ spin_lock(&parent->d_lock);
c1595e42 17485+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17486+ /* AuDbg("%pd\n", d); */
027c5e7a 17487+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17488+ dname = &d->d_name;
17489+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17490+ goto cont_unlock;
17491+ if (au_di(d))
17492+ au_digen_dec(d);
17493+ else
17494+ goto cont_unlock;
c1595e42 17495+ if (au_dcount(d) > 0) {
027c5e7a 17496+ dentry = dget_dlock(d);
4a4d8108 17497+ spin_unlock(&d->d_lock);
027c5e7a 17498+ break;
dece6358 17499+ }
1facf9fc 17500+
f6b6e03d 17501+cont_unlock:
027c5e7a 17502+ spin_unlock(&d->d_lock);
1308ab2a 17503+ }
027c5e7a 17504+ spin_unlock(&parent->d_lock);
4a4d8108 17505+ dput(parent);
1facf9fc 17506+
4a4d8108
AM
17507+ if (dentry)
17508+ di_write_lock_child(dentry);
1308ab2a 17509+
4a4d8108
AM
17510+ return dentry;
17511+}
dece6358 17512+
4a4d8108
AM
17513+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17514+ aufs_bindex_t bindex, ino_t h_ino)
17515+{
17516+ struct inode *inode;
17517+ ino_t ino;
17518+ int err;
17519+
17520+ inode = NULL;
17521+ err = au_xino_read(sb, bindex, h_ino, &ino);
17522+ if (!err && ino)
17523+ inode = ilookup(sb, ino);
17524+ if (!inode)
17525+ goto out;
17526+
17527+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17528+ pr_warn("wrong root branch\n");
4a4d8108
AM
17529+ iput(inode);
17530+ inode = NULL;
17531+ goto out;
1308ab2a 17532+ }
17533+
4a4d8108 17534+ ii_write_lock_child(inode);
1308ab2a 17535+
4f0767ce 17536+out:
4a4d8108 17537+ return inode;
dece6358
AM
17538+}
17539+
4a4d8108 17540+static void au_hn_bh(void *_args)
1facf9fc 17541+{
4a4d8108
AM
17542+ struct au_hnotify_args *a = _args;
17543+ struct super_block *sb;
5afbbe0d 17544+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17545+ unsigned char xino, try_iput;
1facf9fc 17546+ int err;
1308ab2a 17547+ struct inode *inode;
4a4d8108
AM
17548+ ino_t h_ino;
17549+ struct hn_job_args args;
17550+ struct dentry *dentry;
17551+ struct au_sbinfo *sbinfo;
1facf9fc 17552+
4a4d8108
AM
17553+ AuDebugOn(!_args);
17554+ AuDebugOn(!a->h_dir);
17555+ AuDebugOn(!a->dir);
17556+ AuDebugOn(!a->mask);
17557+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17558+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17559+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17560+
4a4d8108
AM
17561+ inode = NULL;
17562+ dentry = NULL;
17563+ /*
17564+ * do not lock a->dir->i_mutex here
17565+ * because of d_revalidate() may cause a deadlock.
17566+ */
17567+ sb = a->dir->i_sb;
17568+ AuDebugOn(!sb);
17569+ sbinfo = au_sbi(sb);
17570+ AuDebugOn(!sbinfo);
7f207e10 17571+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17572+
8b6a4947
AM
17573+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17574+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17575+ case FS_MOVED_FROM:
17576+ case FS_MOVED_TO:
17577+ AuWarn1("DIRREN with UDBA may not work correctly "
17578+ "for the direct rename(2)\n");
17579+ }
17580+
4a4d8108
AM
17581+ ii_read_lock_parent(a->dir);
17582+ bfound = -1;
5afbbe0d
AM
17583+ bbot = au_ibbot(a->dir);
17584+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17585+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17586+ bfound = bindex;
17587+ break;
17588+ }
17589+ ii_read_unlock(a->dir);
17590+ if (unlikely(bfound < 0))
17591+ goto out;
1facf9fc 17592+
4a4d8108
AM
17593+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17594+ h_ino = 0;
17595+ if (a->h_child_inode)
17596+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17597+
4a4d8108
AM
17598+ if (a->h_child_nlen
17599+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17600+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17601+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17602+ a->dir);
17603+ try_iput = 0;
5527c038
JR
17604+ if (dentry && d_really_is_positive(dentry))
17605+ inode = d_inode(dentry);
4a4d8108
AM
17606+ if (xino && !inode && h_ino
17607+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17608+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17609+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17610+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17611+ try_iput = 1;
f0c0a007 17612+ }
1facf9fc 17613+
4a4d8108
AM
17614+ args.flags = a->flags[AuHn_CHILD];
17615+ args.dentry = dentry;
17616+ args.inode = inode;
17617+ args.h_inode = a->h_child_inode;
17618+ args.dir = a->dir;
17619+ args.h_dir = a->h_dir;
17620+ args.h_name = a->h_child_name;
17621+ args.h_nlen = a->h_child_nlen;
17622+ err = hn_job(&args);
17623+ if (dentry) {
027c5e7a 17624+ if (au_di(dentry))
4a4d8108
AM
17625+ di_write_unlock(dentry);
17626+ dput(dentry);
17627+ }
17628+ if (inode && try_iput) {
17629+ ii_write_unlock(inode);
17630+ iput(inode);
17631+ }
1facf9fc 17632+
4a4d8108
AM
17633+ ii_write_lock_parent(a->dir);
17634+ args.flags = a->flags[AuHn_PARENT];
17635+ args.dentry = NULL;
17636+ args.inode = a->dir;
17637+ args.h_inode = a->h_dir;
17638+ args.dir = NULL;
17639+ args.h_dir = NULL;
17640+ args.h_name = NULL;
17641+ args.h_nlen = 0;
17642+ err = hn_job(&args);
17643+ ii_write_unlock(a->dir);
1facf9fc 17644+
4f0767ce 17645+out:
4a4d8108
AM
17646+ iput(a->h_child_inode);
17647+ iput(a->h_dir);
17648+ iput(a->dir);
027c5e7a
AM
17649+ si_write_unlock(sb);
17650+ au_nwt_done(&sbinfo->si_nowait);
1c60b727 17651+ kfree(a);
dece6358 17652+}
1facf9fc 17653+
4a4d8108
AM
17654+/* ---------------------------------------------------------------------- */
17655+
17656+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17657+ struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17658+{
4a4d8108 17659+ int err, len;
53392da6 17660+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17661+ unsigned char isdir, isroot, wh;
17662+ struct inode *dir;
17663+ struct au_hnotify_args *args;
17664+ char *p, *h_child_name;
dece6358 17665+
1308ab2a 17666+ err = 0;
4a4d8108
AM
17667+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17668+ dir = igrab(hnotify->hn_aufs_inode);
17669+ if (!dir)
17670+ goto out;
1facf9fc 17671+
4a4d8108
AM
17672+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17673+ wh = 0;
17674+ h_child_name = (void *)h_child_qstr->name;
17675+ len = h_child_qstr->len;
17676+ if (h_child_name) {
17677+ if (len > AUFS_WH_PFX_LEN
17678+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17679+ h_child_name += AUFS_WH_PFX_LEN;
17680+ len -= AUFS_WH_PFX_LEN;
17681+ wh = 1;
17682+ }
1facf9fc 17683+ }
dece6358 17684+
4a4d8108
AM
17685+ isdir = 0;
17686+ if (h_child_inode)
17687+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17688+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17689+ flags[AuHn_CHILD] = 0;
17690+ if (isdir)
17691+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17692+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17693+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17694+ switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17695+ case FS_MOVED_FROM:
17696+ case FS_MOVED_TO:
17697+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17698+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17699+ /*FALLTHROUGH*/
17700+ case FS_CREATE:
fb47a38f 17701+ AuDebugOn(!h_child_name);
4a4d8108 17702+ break;
1facf9fc 17703+
4a4d8108
AM
17704+ case FS_DELETE:
17705+ /*
17706+ * aufs never be able to get this child inode.
17707+ * revalidation should be in d_revalidate()
17708+ * by checking i_nlink, i_generation or d_unhashed().
17709+ */
17710+ AuDebugOn(!h_child_name);
17711+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17712+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17713+ break;
dece6358 17714+
4a4d8108
AM
17715+ default:
17716+ AuDebugOn(1);
17717+ }
1308ab2a 17718+
4a4d8108
AM
17719+ if (wh)
17720+ h_child_inode = NULL;
1308ab2a 17721+
4a4d8108
AM
17722+ err = -ENOMEM;
17723+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17724+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17725+ if (unlikely(!args)) {
17726+ AuErr1("no memory\n");
17727+ iput(dir);
17728+ goto out;
17729+ }
17730+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17731+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17732+ args->mask = mask;
17733+ args->dir = dir;
17734+ args->h_dir = igrab(h_dir);
17735+ if (h_child_inode)
17736+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17737+ args->h_child_inode = h_child_inode;
17738+ args->h_child_nlen = len;
17739+ if (len) {
17740+ p = (void *)args;
17741+ p += sizeof(*args);
17742+ memcpy(p, h_child_name, len);
17743+ p[len] = 0;
1308ab2a 17744+ }
1308ab2a 17745+
38d290e6 17746+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17747+ f = 0;
38d290e6
JR
17748+ if (!dir->i_nlink
17749+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17750+ f = AuWkq_NEST;
17751+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17752+ if (unlikely(err)) {
17753+ pr_err("wkq %d\n", err);
17754+ iput(args->h_child_inode);
17755+ iput(args->h_dir);
17756+ iput(args->dir);
1c60b727 17757+ kfree(args);
1facf9fc 17758+ }
1facf9fc 17759+
4a4d8108 17760+out:
1facf9fc 17761+ return err;
17762+}
17763+
027c5e7a
AM
17764+/* ---------------------------------------------------------------------- */
17765+
17766+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17767+{
17768+ int err;
17769+
17770+ AuDebugOn(!(udba & AuOptMask_UDBA));
17771+
17772+ err = 0;
17773+ if (au_hnotify_op.reset_br)
17774+ err = au_hnotify_op.reset_br(udba, br, perm);
17775+
17776+ return err;
17777+}
17778+
17779+int au_hnotify_init_br(struct au_branch *br, int perm)
17780+{
17781+ int err;
17782+
17783+ err = 0;
17784+ if (au_hnotify_op.init_br)
17785+ err = au_hnotify_op.init_br(br, perm);
17786+
17787+ return err;
17788+}
17789+
17790+void au_hnotify_fin_br(struct au_branch *br)
17791+{
17792+ if (au_hnotify_op.fin_br)
17793+ au_hnotify_op.fin_br(br);
17794+}
17795+
4a4d8108
AM
17796+static void au_hn_destroy_cache(void)
17797+{
1c60b727
AM
17798+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17799+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17800+}
1308ab2a 17801+
4a4d8108 17802+int __init au_hnotify_init(void)
1facf9fc 17803+{
1308ab2a 17804+ int err;
1308ab2a 17805+
4a4d8108 17806+ err = -ENOMEM;
1c60b727
AM
17807+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17808+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17809+ err = 0;
17810+ if (au_hnotify_op.init)
17811+ err = au_hnotify_op.init();
4a4d8108
AM
17812+ if (unlikely(err))
17813+ au_hn_destroy_cache();
1308ab2a 17814+ }
1308ab2a 17815+ AuTraceErr(err);
4a4d8108 17816+ return err;
1308ab2a 17817+}
17818+
4a4d8108 17819+void au_hnotify_fin(void)
1308ab2a 17820+{
027c5e7a
AM
17821+ if (au_hnotify_op.fin)
17822+ au_hnotify_op.fin();
f0c0a007 17823+
4a4d8108 17824+ /* cf. au_cache_fin() */
1c60b727 17825+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17826+ au_hn_destroy_cache();
dece6358 17827+}
7f207e10
AM
17828diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17829--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
be118d29 17830+++ linux/fs/aufs/iinfo.c 2018-04-06 07:48:44.207938097 +0200
e2f27e51 17831@@ -0,0 +1,285 @@
dece6358 17832+/*
a2654f78 17833+ * Copyright (C) 2005-2017 Junjiro R. Okajima
dece6358
AM
17834+ *
17835+ * This program, aufs is free software; you can redistribute it and/or modify
17836+ * it under the terms of the GNU General Public License as published by
17837+ * the Free Software Foundation; either version 2 of the License, or
17838+ * (at your option) any later version.
17839+ *
17840+ * This program is distributed in the hope that it will be useful,
17841+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17842+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17843+ * GNU General Public License for more details.
17844+ *
17845+ * You should have received a copy of the GNU General Public License
523b37e3 17846+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17847+ */
1facf9fc 17848+
dece6358 17849+/*
4a4d8108 17850+ * inode private data
dece6358 17851+ */
1facf9fc 17852+
1308ab2a 17853+#include "aufs.h"
1facf9fc 17854+
4a4d8108 17855+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17856+{
4a4d8108 17857+ struct inode *h_inode;
5afbbe0d 17858+ struct au_hinode *hinode;
1facf9fc 17859+
4a4d8108 17860+ IiMustAnyLock(inode);
1facf9fc 17861+
5afbbe0d
AM
17862+ hinode = au_hinode(au_ii(inode), bindex);
17863+ h_inode = hinode->hi_inode;
4a4d8108
AM
17864+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17865+ return h_inode;
17866+}
1facf9fc 17867+
4a4d8108
AM
17868+/* todo: hard/soft set? */
17869+void au_hiput(struct au_hinode *hinode)
17870+{
17871+ au_hn_free(hinode);
17872+ dput(hinode->hi_whdentry);
17873+ iput(hinode->hi_inode);
17874+}
1facf9fc 17875+
4a4d8108
AM
17876+unsigned int au_hi_flags(struct inode *inode, int isdir)
17877+{
17878+ unsigned int flags;
17879+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 17880+
4a4d8108
AM
17881+ flags = 0;
17882+ if (au_opt_test(mnt_flags, XINO))
17883+ au_fset_hi(flags, XINO);
17884+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17885+ au_fset_hi(flags, HNOTIFY);
17886+ return flags;
1facf9fc 17887+}
17888+
4a4d8108
AM
17889+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17890+ struct inode *h_inode, unsigned int flags)
1308ab2a 17891+{
4a4d8108
AM
17892+ struct au_hinode *hinode;
17893+ struct inode *hi;
17894+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 17895+
4a4d8108 17896+ IiMustWriteLock(inode);
dece6358 17897+
5afbbe0d 17898+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
17899+ hi = hinode->hi_inode;
17900+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17901+
17902+ if (hi)
17903+ au_hiput(hinode);
17904+ hinode->hi_inode = h_inode;
17905+ if (h_inode) {
17906+ int err;
17907+ struct super_block *sb = inode->i_sb;
17908+ struct au_branch *br;
17909+
027c5e7a
AM
17910+ AuDebugOn(inode->i_mode
17911+ && (h_inode->i_mode & S_IFMT)
17912+ != (inode->i_mode & S_IFMT));
5afbbe0d 17913+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
17914+ au_cpup_igen(inode, h_inode);
17915+ br = au_sbr(sb, bindex);
17916+ hinode->hi_id = br->br_id;
17917+ if (au_ftest_hi(flags, XINO)) {
17918+ err = au_xino_write(sb, bindex, h_inode->i_ino,
17919+ inode->i_ino);
17920+ if (unlikely(err))
17921+ AuIOErr1("failed au_xino_write() %d\n", err);
17922+ }
17923+
17924+ if (au_ftest_hi(flags, HNOTIFY)
17925+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 17926+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
17927+ if (unlikely(err))
17928+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 17929+ }
17930+ }
4a4d8108 17931+}
dece6358 17932+
4a4d8108
AM
17933+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17934+ struct dentry *h_wh)
17935+{
17936+ struct au_hinode *hinode;
dece6358 17937+
4a4d8108
AM
17938+ IiMustWriteLock(inode);
17939+
5afbbe0d 17940+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
17941+ AuDebugOn(hinode->hi_whdentry);
17942+ hinode->hi_whdentry = h_wh;
1facf9fc 17943+}
17944+
537831f9 17945+void au_update_iigen(struct inode *inode, int half)
1308ab2a 17946+{
537831f9
AM
17947+ struct au_iinfo *iinfo;
17948+ struct au_iigen *iigen;
17949+ unsigned int sigen;
17950+
17951+ sigen = au_sigen(inode->i_sb);
17952+ iinfo = au_ii(inode);
17953+ iigen = &iinfo->ii_generation;
be52b249 17954+ spin_lock(&iigen->ig_spin);
537831f9
AM
17955+ iigen->ig_generation = sigen;
17956+ if (half)
17957+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17958+ else
17959+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 17960+ spin_unlock(&iigen->ig_spin);
4a4d8108 17961+}
1facf9fc 17962+
4a4d8108
AM
17963+/* it may be called at remount time, too */
17964+void au_update_ibrange(struct inode *inode, int do_put_zero)
17965+{
17966+ struct au_iinfo *iinfo;
5afbbe0d 17967+ aufs_bindex_t bindex, bbot;
1facf9fc 17968+
5afbbe0d 17969+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 17970+ IiMustWriteLock(inode);
1facf9fc 17971+
5afbbe0d
AM
17972+ iinfo = au_ii(inode);
17973+ if (do_put_zero && iinfo->ii_btop >= 0) {
17974+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
17975+ bindex++) {
17976+ struct inode *h_i;
1facf9fc 17977+
5afbbe0d 17978+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
17979+ if (h_i
17980+ && !h_i->i_nlink
17981+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
17982+ au_set_h_iptr(inode, bindex, NULL, 0);
17983+ }
4a4d8108
AM
17984+ }
17985+
5afbbe0d
AM
17986+ iinfo->ii_btop = -1;
17987+ iinfo->ii_bbot = -1;
17988+ bbot = au_sbbot(inode->i_sb);
17989+ for (bindex = 0; bindex <= bbot; bindex++)
17990+ if (au_hinode(iinfo, bindex)->hi_inode) {
17991+ iinfo->ii_btop = bindex;
4a4d8108 17992+ break;
027c5e7a 17993+ }
5afbbe0d
AM
17994+ if (iinfo->ii_btop >= 0)
17995+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17996+ if (au_hinode(iinfo, bindex)->hi_inode) {
17997+ iinfo->ii_bbot = bindex;
027c5e7a
AM
17998+ break;
17999+ }
5afbbe0d 18000+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 18001+}
1facf9fc 18002+
dece6358 18003+/* ---------------------------------------------------------------------- */
1facf9fc 18004+
4a4d8108 18005+void au_icntnr_init_once(void *_c)
dece6358 18006+{
4a4d8108
AM
18007+ struct au_icntnr *c = _c;
18008+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 18009+
be52b249 18010+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
18011+ au_rw_init(&iinfo->ii_rwsem);
18012+ inode_init_once(&c->vfs_inode);
18013+}
1facf9fc 18014+
5afbbe0d
AM
18015+void au_hinode_init(struct au_hinode *hinode)
18016+{
18017+ hinode->hi_inode = NULL;
18018+ hinode->hi_id = -1;
18019+ au_hn_init(hinode);
18020+ hinode->hi_whdentry = NULL;
18021+}
18022+
4a4d8108
AM
18023+int au_iinfo_init(struct inode *inode)
18024+{
18025+ struct au_iinfo *iinfo;
18026+ struct super_block *sb;
5afbbe0d 18027+ struct au_hinode *hi;
4a4d8108 18028+ int nbr, i;
1facf9fc 18029+
4a4d8108
AM
18030+ sb = inode->i_sb;
18031+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 18032+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
18033+ if (unlikely(nbr <= 0))
18034+ nbr = 1;
5afbbe0d
AM
18035+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18036+ if (hi) {
7f207e10 18037+ au_ninodes_inc(sb);
5afbbe0d
AM
18038+
18039+ iinfo->ii_hinode = hi;
18040+ for (i = 0; i < nbr; i++, hi++)
18041+ au_hinode_init(hi);
1facf9fc 18042+
537831f9 18043+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
18044+ iinfo->ii_btop = -1;
18045+ iinfo->ii_bbot = -1;
4a4d8108
AM
18046+ iinfo->ii_vdir = NULL;
18047+ return 0;
1308ab2a 18048+ }
4a4d8108
AM
18049+ return -ENOMEM;
18050+}
1facf9fc 18051+
e2f27e51 18052+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 18053+{
5afbbe0d 18054+ int err, i;
4a4d8108 18055+ struct au_hinode *hip;
1facf9fc 18056+
4a4d8108
AM
18057+ AuRwMustWriteLock(&iinfo->ii_rwsem);
18058+
18059+ err = -ENOMEM;
e2f27e51
AM
18060+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18061+ may_shrink);
4a4d8108
AM
18062+ if (hip) {
18063+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18064+ i = iinfo->ii_bbot + 1;
18065+ hip += i;
18066+ for (; i < nbr; i++, hip++)
18067+ au_hinode_init(hip);
4a4d8108 18068+ err = 0;
1308ab2a 18069+ }
4a4d8108 18070+
1308ab2a 18071+ return err;
1facf9fc 18072+}
18073+
4a4d8108 18074+void au_iinfo_fin(struct inode *inode)
1facf9fc 18075+{
4a4d8108
AM
18076+ struct au_iinfo *iinfo;
18077+ struct au_hinode *hi;
18078+ struct super_block *sb;
5afbbe0d 18079+ aufs_bindex_t bindex, bbot;
b752ccd1 18080+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18081+
5afbbe0d 18082+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18083+
b752ccd1 18084+ sb = inode->i_sb;
7f207e10 18085+ au_ninodes_dec(sb);
b752ccd1
AM
18086+ if (si_pid_test(sb))
18087+ au_xino_delete_inode(inode, unlinked);
18088+ else {
18089+ /*
18090+ * it is safe to hide the dependency between sbinfo and
18091+ * sb->s_umount.
18092+ */
18093+ lockdep_off();
18094+ si_noflush_read_lock(sb);
18095+ au_xino_delete_inode(inode, unlinked);
18096+ si_read_unlock(sb);
18097+ lockdep_on();
18098+ }
18099+
5afbbe0d 18100+ iinfo = au_ii(inode);
4a4d8108 18101+ if (iinfo->ii_vdir)
1c60b727 18102+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18103+
5afbbe0d 18104+ bindex = iinfo->ii_btop;
b752ccd1 18105+ if (bindex >= 0) {
5afbbe0d
AM
18106+ hi = au_hinode(iinfo, bindex);
18107+ bbot = iinfo->ii_bbot;
18108+ while (bindex++ <= bbot) {
b752ccd1 18109+ if (hi->hi_inode)
4a4d8108 18110+ au_hiput(hi);
4a4d8108
AM
18111+ hi++;
18112+ }
18113+ }
1c60b727 18114+ kfree(iinfo->ii_hinode);
4a4d8108 18115+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18116+}
7f207e10
AM
18117diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18118--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
be118d29 18119+++ linux/fs/aufs/inode.c 2018-04-06 07:48:44.207938097 +0200
521ced18 18120@@ -0,0 +1,527 @@
4a4d8108 18121+/*
a2654f78 18122+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
18123+ *
18124+ * This program, aufs is free software; you can redistribute it and/or modify
18125+ * it under the terms of the GNU General Public License as published by
18126+ * the Free Software Foundation; either version 2 of the License, or
18127+ * (at your option) any later version.
18128+ *
18129+ * This program is distributed in the hope that it will be useful,
18130+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18131+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18132+ * GNU General Public License for more details.
18133+ *
18134+ * You should have received a copy of the GNU General Public License
523b37e3 18135+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18136+ */
1facf9fc 18137+
4a4d8108
AM
18138+/*
18139+ * inode functions
18140+ */
1facf9fc 18141+
4a4d8108 18142+#include "aufs.h"
1308ab2a 18143+
4a4d8108
AM
18144+struct inode *au_igrab(struct inode *inode)
18145+{
18146+ if (inode) {
18147+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18148+ ihold(inode);
1facf9fc 18149+ }
4a4d8108
AM
18150+ return inode;
18151+}
1facf9fc 18152+
4a4d8108
AM
18153+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18154+{
18155+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18156+ au_update_iigen(inode, /*half*/1);
4a4d8108 18157+ if (do_version)
be118d29 18158+ inode_inc_iversion(inode);
dece6358 18159+}
1facf9fc 18160+
027c5e7a 18161+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18162+{
e2f27e51 18163+ int err, e, nbr;
027c5e7a 18164+ umode_t type;
4a4d8108 18165+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18166+ struct super_block *sb;
4a4d8108 18167+ struct au_iinfo *iinfo;
027c5e7a 18168+ struct au_hinode *p, *q, tmp;
1facf9fc 18169+
5afbbe0d 18170+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18171+ IiMustWriteLock(inode);
1facf9fc 18172+
027c5e7a 18173+ *update = 0;
4a4d8108 18174+ sb = inode->i_sb;
e2f27e51 18175+ nbr = au_sbbot(sb) + 1;
027c5e7a 18176+ type = inode->i_mode & S_IFMT;
4a4d8108 18177+ iinfo = au_ii(inode);
e2f27e51 18178+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18179+ if (unlikely(err))
1308ab2a 18180+ goto out;
1facf9fc 18181+
5afbbe0d
AM
18182+ AuDebugOn(iinfo->ii_btop < 0);
18183+ p = au_hinode(iinfo, iinfo->ii_btop);
18184+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18185+ bindex++, p++) {
18186+ if (!p->hi_inode)
18187+ continue;
1facf9fc 18188+
027c5e7a 18189+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18190+ new_bindex = au_br_index(sb, p->hi_id);
18191+ if (new_bindex == bindex)
18192+ continue;
1facf9fc 18193+
4a4d8108 18194+ if (new_bindex < 0) {
027c5e7a 18195+ *update = 1;
4a4d8108
AM
18196+ au_hiput(p);
18197+ p->hi_inode = NULL;
18198+ continue;
1308ab2a 18199+ }
4a4d8108 18200+
5afbbe0d
AM
18201+ if (new_bindex < iinfo->ii_btop)
18202+ iinfo->ii_btop = new_bindex;
18203+ if (iinfo->ii_bbot < new_bindex)
18204+ iinfo->ii_bbot = new_bindex;
4a4d8108 18205+ /* swap two lower inode, and loop again */
5afbbe0d 18206+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18207+ tmp = *q;
18208+ *q = *p;
18209+ *p = tmp;
18210+ if (tmp.hi_inode) {
18211+ bindex--;
18212+ p--;
1308ab2a 18213+ }
18214+ }
4a4d8108 18215+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18216+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18217+ e = au_dy_irefresh(inode);
18218+ if (unlikely(e && !err))
18219+ err = e;
1facf9fc 18220+
4f0767ce 18221+out:
027c5e7a
AM
18222+ AuTraceErr(err);
18223+ return err;
18224+}
18225+
b95c5147
AM
18226+void au_refresh_iop(struct inode *inode, int force_getattr)
18227+{
18228+ int type;
18229+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18230+ const struct inode_operations *iop
18231+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18232+
18233+ if (inode->i_op == iop)
18234+ return;
18235+
18236+ switch (inode->i_mode & S_IFMT) {
18237+ case S_IFDIR:
18238+ type = AuIop_DIR;
18239+ break;
18240+ case S_IFLNK:
18241+ type = AuIop_SYMLINK;
18242+ break;
18243+ default:
18244+ type = AuIop_OTHER;
18245+ break;
18246+ }
18247+
18248+ inode->i_op = iop + type;
18249+ /* unnecessary smp_wmb() */
18250+}
18251+
027c5e7a
AM
18252+int au_refresh_hinode_self(struct inode *inode)
18253+{
18254+ int err, update;
18255+
18256+ err = au_ii_refresh(inode, &update);
18257+ if (!err)
18258+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18259+
18260+ AuTraceErr(err);
4a4d8108
AM
18261+ return err;
18262+}
1facf9fc 18263+
4a4d8108
AM
18264+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18265+{
027c5e7a 18266+ int err, e, update;
4a4d8108 18267+ unsigned int flags;
027c5e7a 18268+ umode_t mode;
5afbbe0d 18269+ aufs_bindex_t bindex, bbot;
027c5e7a 18270+ unsigned char isdir;
4a4d8108
AM
18271+ struct au_hinode *p;
18272+ struct au_iinfo *iinfo;
1facf9fc 18273+
027c5e7a 18274+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18275+ if (unlikely(err))
18276+ goto out;
18277+
18278+ update = 0;
18279+ iinfo = au_ii(inode);
5afbbe0d 18280+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18281+ mode = (inode->i_mode & S_IFMT);
18282+ isdir = S_ISDIR(mode);
4a4d8108 18283+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18284+ bbot = au_dbbot(dentry);
18285+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18286+ struct inode *h_i, *h_inode;
4a4d8108
AM
18287+ struct dentry *h_d;
18288+
18289+ h_d = au_h_dptr(dentry, bindex);
5527c038 18290+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18291+ continue;
18292+
5527c038
JR
18293+ h_inode = d_inode(h_d);
18294+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18295+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18296+ h_i = au_h_iptr(inode, bindex);
18297+ if (h_i) {
5527c038 18298+ if (h_i == h_inode)
4a4d8108
AM
18299+ continue;
18300+ err = -EIO;
18301+ break;
18302+ }
18303+ }
5afbbe0d
AM
18304+ if (bindex < iinfo->ii_btop)
18305+ iinfo->ii_btop = bindex;
18306+ if (iinfo->ii_bbot < bindex)
18307+ iinfo->ii_bbot = bindex;
5527c038 18308+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18309+ update = 1;
1308ab2a 18310+ }
4a4d8108
AM
18311+ au_update_ibrange(inode, /*do_put_zero*/0);
18312+ e = au_dy_irefresh(inode);
18313+ if (unlikely(e && !err))
18314+ err = e;
027c5e7a
AM
18315+ if (!err)
18316+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18317+
4f0767ce 18318+out:
4a4d8108 18319+ AuTraceErr(err);
1308ab2a 18320+ return err;
dece6358
AM
18321+}
18322+
4a4d8108 18323+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18324+{
4a4d8108
AM
18325+ int err;
18326+ unsigned int flags;
18327+ umode_t mode;
5afbbe0d 18328+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18329+ unsigned char isdir;
18330+ struct dentry *h_dentry;
18331+ struct inode *h_inode;
18332+ struct au_iinfo *iinfo;
b95c5147 18333+ struct inode_operations *iop;
dece6358 18334+
4a4d8108 18335+ IiMustWriteLock(inode);
dece6358 18336+
4a4d8108
AM
18337+ err = 0;
18338+ isdir = 0;
b95c5147 18339+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18340+ btop = au_dbtop(dentry);
18341+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18342+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18343+ mode = h_inode->i_mode;
18344+ switch (mode & S_IFMT) {
18345+ case S_IFREG:
18346+ btail = au_dbtail(dentry);
b95c5147 18347+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18348+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18349+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18350+ if (unlikely(err))
18351+ goto out;
18352+ break;
18353+ case S_IFDIR:
18354+ isdir = 1;
18355+ btail = au_dbtaildir(dentry);
b95c5147 18356+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18357+ inode->i_fop = &aufs_dir_fop;
18358+ break;
18359+ case S_IFLNK:
18360+ btail = au_dbtail(dentry);
b95c5147 18361+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18362+ break;
18363+ case S_IFBLK:
18364+ case S_IFCHR:
18365+ case S_IFIFO:
18366+ case S_IFSOCK:
18367+ btail = au_dbtail(dentry);
b95c5147 18368+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18369+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18370+ break;
18371+ default:
18372+ AuIOErr("Unknown file type 0%o\n", mode);
18373+ err = -EIO;
1308ab2a 18374+ goto out;
4a4d8108 18375+ }
dece6358 18376+
4a4d8108
AM
18377+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18378+ flags = au_hi_flags(inode, isdir);
18379+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18380+ && au_ftest_hi(flags, HNOTIFY)
18381+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18382+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18383+ au_fclr_hi(flags, HNOTIFY);
18384+ iinfo = au_ii(inode);
5afbbe0d
AM
18385+ iinfo->ii_btop = btop;
18386+ iinfo->ii_bbot = btail;
18387+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18388+ h_dentry = au_h_dptr(dentry, bindex);
18389+ if (h_dentry)
18390+ au_set_h_iptr(inode, bindex,
5527c038 18391+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18392+ }
18393+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18394+ /*
18395+ * to force calling aufs_get_acl() every time,
18396+ * do not call cache_no_acl() for aufs inode.
18397+ */
dece6358 18398+
4f0767ce 18399+out:
4a4d8108
AM
18400+ return err;
18401+}
dece6358 18402+
027c5e7a
AM
18403+/*
18404+ * successful returns with iinfo write_locked
18405+ * minus: errno
18406+ * zero: success, matched
18407+ * plus: no error, but unmatched
18408+ */
18409+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18410+{
18411+ int err;
cfc41e69 18412+ unsigned int gen, igflags;
5afbbe0d 18413+ aufs_bindex_t bindex, bbot;
4a4d8108 18414+ struct inode *h_inode, *h_dinode;
5527c038 18415+ struct dentry *h_dentry;
dece6358 18416+
4a4d8108
AM
18417+ /*
18418+ * before this function, if aufs got any iinfo lock, it must be only
18419+ * one, the parent dir.
18420+ * it can happen by UDBA and the obsoleted inode number.
18421+ */
18422+ err = -EIO;
18423+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18424+ goto out;
18425+
027c5e7a 18426+ err = 1;
4a4d8108 18427+ ii_write_lock_new_child(inode);
5afbbe0d 18428+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18429+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18430+ bbot = au_ibbot(inode);
18431+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18432+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18433+ if (!h_inode || h_inode != h_dinode)
18434+ continue;
18435+
18436+ err = 0;
cfc41e69 18437+ gen = au_iigen(inode, &igflags);
537831f9 18438+ if (gen == au_digen(dentry)
cfc41e69 18439+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18440+ break;
537831f9
AM
18441+
18442+ /* fully refresh inode using dentry */
18443+ err = au_refresh_hinode(inode, dentry);
18444+ if (!err)
18445+ au_update_iigen(inode, /*half*/0);
18446+ break;
1facf9fc 18447+ }
dece6358 18448+
4a4d8108
AM
18449+ if (unlikely(err))
18450+ ii_write_unlock(inode);
4f0767ce 18451+out:
1facf9fc 18452+ return err;
18453+}
1facf9fc 18454+
4a4d8108
AM
18455+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18456+ unsigned int d_type, ino_t *ino)
1facf9fc 18457+{
521ced18
JR
18458+ int err, idx;
18459+ const int isnondir = d_type != DT_DIR;
1facf9fc 18460+
b752ccd1 18461+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18462+ if (isnondir) {
18463+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18464+ if (unlikely(err))
18465+ goto out;
4a4d8108 18466+ }
521ced18 18467+
4a4d8108
AM
18468+ err = au_xino_read(sb, bindex, h_ino, ino);
18469+ if (unlikely(err))
521ced18 18470+ goto out_xinondir;
1308ab2a 18471+
4a4d8108
AM
18472+ if (!*ino) {
18473+ err = -EIO;
18474+ *ino = au_xino_new_ino(sb);
18475+ if (unlikely(!*ino))
521ced18 18476+ goto out_xinondir;
4a4d8108
AM
18477+ err = au_xino_write(sb, bindex, h_ino, *ino);
18478+ if (unlikely(err))
521ced18 18479+ goto out_xinondir;
1308ab2a 18480+ }
1facf9fc 18481+
521ced18
JR
18482+out_xinondir:
18483+ if (isnondir && idx >= 0)
18484+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18485+out:
1facf9fc 18486+ return err;
18487+}
18488+
4a4d8108
AM
18489+/* successful returns with iinfo write_locked */
18490+/* todo: return with unlocked? */
18491+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18492+{
5527c038 18493+ struct inode *inode, *h_inode;
4a4d8108
AM
18494+ struct dentry *h_dentry;
18495+ struct super_block *sb;
18496+ ino_t h_ino, ino;
521ced18 18497+ int err, idx, hlinked;
5afbbe0d 18498+ aufs_bindex_t btop;
1facf9fc 18499+
4a4d8108 18500+ sb = dentry->d_sb;
5afbbe0d
AM
18501+ btop = au_dbtop(dentry);
18502+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18503+ h_inode = d_inode(h_dentry);
18504+ h_ino = h_inode->i_ino;
521ced18 18505+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18506+
521ced18 18507+new_ino:
b752ccd1
AM
18508+ /*
18509+ * stop 'race'-ing between hardlinks under different
18510+ * parents.
18511+ */
521ced18
JR
18512+ if (hlinked) {
18513+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18514+ inode = ERR_PTR(err);
18515+ if (unlikely(err))
18516+ goto out;
18517+ }
b752ccd1 18518+
5afbbe0d 18519+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18520+ inode = ERR_PTR(err);
18521+ if (unlikely(err))
521ced18 18522+ goto out_xinondir;
b752ccd1 18523+
4a4d8108
AM
18524+ if (!ino) {
18525+ ino = au_xino_new_ino(sb);
18526+ if (unlikely(!ino)) {
18527+ inode = ERR_PTR(-EIO);
521ced18 18528+ goto out_xinondir;
dece6358
AM
18529+ }
18530+ }
1facf9fc 18531+
4a4d8108
AM
18532+ AuDbg("i%lu\n", (unsigned long)ino);
18533+ inode = au_iget_locked(sb, ino);
18534+ err = PTR_ERR(inode);
18535+ if (IS_ERR(inode))
521ced18 18536+ goto out_xinondir;
1facf9fc 18537+
4a4d8108
AM
18538+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18539+ if (inode->i_state & I_NEW) {
18540+ ii_write_lock_new_child(inode);
18541+ err = set_inode(inode, dentry);
18542+ if (!err) {
18543+ unlock_new_inode(inode);
521ced18 18544+ goto out_xinondir; /* success */
4a4d8108 18545+ }
1308ab2a 18546+
027c5e7a
AM
18547+ /*
18548+ * iget_failed() calls iput(), but we need to call
18549+ * ii_write_unlock() after iget_failed(). so dirty hack for
18550+ * i_count.
18551+ */
18552+ atomic_inc(&inode->i_count);
4a4d8108 18553+ iget_failed(inode);
027c5e7a 18554+ ii_write_unlock(inode);
5afbbe0d 18555+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18556+ /* ignore this error */
18557+ goto out_iput;
18558+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18559+ /*
18560+ * horrible race condition between lookup, readdir and copyup
18561+ * (or something).
18562+ */
521ced18
JR
18563+ if (hlinked && idx >= 0)
18564+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18565+ err = reval_inode(inode, dentry);
18566+ if (unlikely(err < 0)) {
521ced18 18567+ hlinked = 0;
027c5e7a
AM
18568+ goto out_iput;
18569+ }
521ced18 18570+ if (!err)
4a4d8108 18571+ goto out; /* success */
521ced18
JR
18572+ else if (hlinked && idx >= 0) {
18573+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18574+ if (unlikely(err)) {
18575+ iput(inode);
18576+ inode = ERR_PTR(err);
18577+ goto out;
18578+ }
18579+ }
4a4d8108
AM
18580+ }
18581+
5527c038 18582+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18583+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18584+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18585+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18586+ (unsigned long)h_ino, (unsigned long)ino);
18587+ ino = 0;
5afbbe0d 18588+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18589+ if (!err) {
18590+ iput(inode);
521ced18
JR
18591+ if (hlinked && idx >= 0)
18592+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18593+ goto new_ino;
18594+ }
1308ab2a 18595+
4f0767ce 18596+out_iput:
4a4d8108 18597+ iput(inode);
4a4d8108 18598+ inode = ERR_PTR(err);
521ced18
JR
18599+out_xinondir:
18600+ if (hlinked && idx >= 0)
18601+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18602+out:
4a4d8108 18603+ return inode;
1facf9fc 18604+}
18605+
4a4d8108 18606+/* ---------------------------------------------------------------------- */
1facf9fc 18607+
4a4d8108
AM
18608+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18609+ struct inode *inode)
18610+{
18611+ int err;
076b876e 18612+ struct inode *hi;
1facf9fc 18613+
4a4d8108 18614+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18615+
4a4d8108
AM
18616+ /* pseudo-link after flushed may happen out of bounds */
18617+ if (!err
18618+ && inode
5afbbe0d
AM
18619+ && au_ibtop(inode) <= bindex
18620+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18621+ /*
18622+ * permission check is unnecessary since vfsub routine
18623+ * will be called later
18624+ */
076b876e 18625+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18626+ if (hi)
18627+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18628+ }
18629+
4a4d8108
AM
18630+ return err;
18631+}
dece6358 18632+
4a4d8108
AM
18633+int au_test_h_perm(struct inode *h_inode, int mask)
18634+{
2dfbb274 18635+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
18636+ return 0;
18637+ return inode_permission(h_inode, mask);
18638+}
1facf9fc 18639+
4a4d8108
AM
18640+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18641+{
18642+ if (au_test_nfs(h_inode->i_sb)
18643+ && (mask & MAY_WRITE)
18644+ && S_ISDIR(h_inode->i_mode))
18645+ mask |= MAY_READ; /* force permission check */
18646+ return au_test_h_perm(h_inode, mask);
1facf9fc 18647+}
7f207e10
AM
18648diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18649--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
be118d29 18650+++ linux/fs/aufs/inode.h 2018-04-06 07:48:44.207938097 +0200
8b6a4947 18651@@ -0,0 +1,695 @@
4a4d8108 18652+/*
a2654f78 18653+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
18654+ *
18655+ * This program, aufs is free software; you can redistribute it and/or modify
18656+ * it under the terms of the GNU General Public License as published by
18657+ * the Free Software Foundation; either version 2 of the License, or
18658+ * (at your option) any later version.
18659+ *
18660+ * This program is distributed in the hope that it will be useful,
18661+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18662+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18663+ * GNU General Public License for more details.
18664+ *
18665+ * You should have received a copy of the GNU General Public License
523b37e3 18666+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18667+ */
1facf9fc 18668+
1308ab2a 18669+/*
4a4d8108 18670+ * inode operations
1308ab2a 18671+ */
dece6358 18672+
4a4d8108
AM
18673+#ifndef __AUFS_INODE_H__
18674+#define __AUFS_INODE_H__
dece6358 18675+
4a4d8108 18676+#ifdef __KERNEL__
1308ab2a 18677+
4a4d8108 18678+#include <linux/fsnotify.h>
4a4d8108 18679+#include "rwsem.h"
1308ab2a 18680+
4a4d8108 18681+struct vfsmount;
1facf9fc 18682+
4a4d8108
AM
18683+struct au_hnotify {
18684+#ifdef CONFIG_AUFS_HNOTIFY
18685+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18686+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18687+ struct fsnotify_mark hn_mark;
4a4d8108 18688+#endif
1c60b727 18689+ struct inode *hn_aufs_inode; /* no get/put */
4a4d8108
AM
18690+#endif
18691+} ____cacheline_aligned_in_smp;
1facf9fc 18692+
4a4d8108
AM
18693+struct au_hinode {
18694+ struct inode *hi_inode;
18695+ aufs_bindex_t hi_id;
18696+#ifdef CONFIG_AUFS_HNOTIFY
18697+ struct au_hnotify *hi_notify;
18698+#endif
dece6358 18699+
4a4d8108
AM
18700+ /* reference to the copied-up whiteout with get/put */
18701+ struct dentry *hi_whdentry;
18702+};
dece6358 18703+
537831f9
AM
18704+/* ig_flags */
18705+#define AuIG_HALF_REFRESHED 1
18706+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18707+#define au_ig_fset(flags, name) \
18708+ do { (flags) |= AuIG_##name; } while (0)
18709+#define au_ig_fclr(flags, name) \
18710+ do { (flags) &= ~AuIG_##name; } while (0)
18711+
18712+struct au_iigen {
be52b249 18713+ spinlock_t ig_spin;
537831f9
AM
18714+ __u32 ig_generation, ig_flags;
18715+};
18716+
4a4d8108
AM
18717+struct au_vdir;
18718+struct au_iinfo {
7a9e40b8 18719+ struct au_iigen ii_generation;
4a4d8108 18720+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18721+
4a4d8108 18722+ struct au_rwsem ii_rwsem;
5afbbe0d 18723+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18724+ __u32 ii_higen;
18725+ struct au_hinode *ii_hinode;
18726+ struct au_vdir *ii_vdir;
18727+};
1facf9fc 18728+
4a4d8108
AM
18729+struct au_icntnr {
18730+ struct au_iinfo iinfo;
18731+ struct inode vfs_inode;
8b6a4947 18732+ struct hlist_bl_node plink;
4a4d8108 18733+} ____cacheline_aligned_in_smp;
1308ab2a 18734+
4a4d8108
AM
18735+/* au_pin flags */
18736+#define AuPin_DI_LOCKED 1
18737+#define AuPin_MNT_WRITE (1 << 1)
18738+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18739+#define au_fset_pin(flags, name) \
18740+ do { (flags) |= AuPin_##name; } while (0)
18741+#define au_fclr_pin(flags, name) \
18742+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18743+
18744+struct au_pin {
18745+ /* input */
18746+ struct dentry *dentry;
18747+ unsigned int udba;
18748+ unsigned char lsc_di, lsc_hi, flags;
18749+ aufs_bindex_t bindex;
18750+
18751+ /* output */
18752+ struct dentry *parent;
18753+ struct au_hinode *hdir;
18754+ struct vfsmount *h_mnt;
86dc4139
AM
18755+
18756+ /* temporary unlock/relock for copyup */
18757+ struct dentry *h_dentry, *h_parent;
18758+ struct au_branch *br;
18759+ struct task_struct *task;
4a4d8108 18760+};
1facf9fc 18761+
86dc4139 18762+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18763+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18764+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18765+void au_pin_hdir_acquire_nest(struct au_pin *p);
18766+void au_pin_hdir_release(struct au_pin *p);
18767+
1308ab2a 18768+/* ---------------------------------------------------------------------- */
18769+
4a4d8108 18770+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18771+{
5afbbe0d
AM
18772+ BUG_ON(is_bad_inode(inode));
18773+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18774+}
1facf9fc 18775+
4a4d8108 18776+/* ---------------------------------------------------------------------- */
1facf9fc 18777+
4a4d8108
AM
18778+/* inode.c */
18779+struct inode *au_igrab(struct inode *inode);
b95c5147 18780+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18781+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18782+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18783+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18784+ unsigned int d_type, ino_t *ino);
18785+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18786+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18787+ struct inode *inode);
18788+int au_test_h_perm(struct inode *h_inode, int mask);
18789+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 18790+
4a4d8108
AM
18791+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18792+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18793+{
18794+#ifdef CONFIG_AUFS_SHWH
18795+ return au_ino(sb, bindex, h_ino, d_type, ino);
18796+#else
18797+ return 0;
18798+#endif
18799+}
1facf9fc 18800+
4a4d8108 18801+/* i_op.c */
b95c5147
AM
18802+enum {
18803+ AuIop_SYMLINK,
18804+ AuIop_DIR,
18805+ AuIop_OTHER,
18806+ AuIop_Last
18807+};
18808+extern struct inode_operations aufs_iop[AuIop_Last],
18809+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18810+
4a4d8108
AM
18811+/* au_wr_dir flags */
18812+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18813+#define AuWrDir_ISDIR (1 << 1)
18814+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18815+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18816+#define au_fset_wrdir(flags, name) \
18817+ do { (flags) |= AuWrDir_##name; } while (0)
18818+#define au_fclr_wrdir(flags, name) \
18819+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18820+
4a4d8108
AM
18821+struct au_wr_dir_args {
18822+ aufs_bindex_t force_btgt;
18823+ unsigned char flags;
18824+};
18825+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18826+ struct au_wr_dir_args *args);
dece6358 18827+
4a4d8108
AM
18828+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18829+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18830+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18831+ unsigned int udba, unsigned char flags);
18832+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18833+ unsigned int udba, unsigned char flags) __must_check;
18834+int au_do_pin(struct au_pin *pin) __must_check;
18835+void au_unpin(struct au_pin *pin);
c1595e42
JR
18836+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18837+
18838+#define AuIcpup_DID_CPUP 1
18839+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18840+#define au_fset_icpup(flags, name) \
18841+ do { (flags) |= AuIcpup_##name; } while (0)
18842+#define au_fclr_icpup(flags, name) \
18843+ do { (flags) &= ~AuIcpup_##name; } while (0)
18844+
18845+struct au_icpup_args {
18846+ unsigned char flags;
18847+ unsigned char pin_flags;
18848+ aufs_bindex_t btgt;
18849+ unsigned int udba;
18850+ struct au_pin pin;
18851+ struct path h_path;
18852+ struct inode *h_inode;
18853+};
18854+
18855+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18856+ struct au_icpup_args *a);
18857+
a2654f78
AM
18858+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
18859+ int locked);
1facf9fc 18860+
4a4d8108
AM
18861+/* i_op_add.c */
18862+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18863+ struct dentry *h_parent, int isdir);
7eafdf33
AM
18864+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18865+ dev_t dev);
4a4d8108 18866+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 18867+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 18868+ bool want_excl);
b912730e
AM
18869+struct vfsub_aopen_args;
18870+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18871+ struct vfsub_aopen_args *args);
38d290e6 18872+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
18873+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18874+ struct dentry *dentry);
7eafdf33 18875+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 18876+
4a4d8108
AM
18877+/* i_op_del.c */
18878+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18879+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18880+ struct dentry *h_parent, int isdir);
18881+int aufs_unlink(struct inode *dir, struct dentry *dentry);
18882+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 18883+
4a4d8108
AM
18884+/* i_op_ren.c */
18885+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18886+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
f2c43d5f
AM
18887+ struct inode *dir, struct dentry *dentry,
18888+ unsigned int flags);
1facf9fc 18889+
4a4d8108
AM
18890+/* iinfo.c */
18891+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18892+void au_hiput(struct au_hinode *hinode);
18893+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18894+ struct dentry *h_wh);
18895+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 18896+
4a4d8108
AM
18897+/* hinode flags */
18898+#define AuHi_XINO 1
18899+#define AuHi_HNOTIFY (1 << 1)
18900+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
18901+#define au_fset_hi(flags, name) \
18902+ do { (flags) |= AuHi_##name; } while (0)
18903+#define au_fclr_hi(flags, name) \
18904+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 18905+
4a4d8108
AM
18906+#ifndef CONFIG_AUFS_HNOTIFY
18907+#undef AuHi_HNOTIFY
18908+#define AuHi_HNOTIFY 0
18909+#endif
1facf9fc 18910+
4a4d8108
AM
18911+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18912+ struct inode *h_inode, unsigned int flags);
1facf9fc 18913+
537831f9 18914+void au_update_iigen(struct inode *inode, int half);
4a4d8108 18915+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 18916+
4a4d8108 18917+void au_icntnr_init_once(void *_c);
5afbbe0d 18918+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
18919+int au_iinfo_init(struct inode *inode);
18920+void au_iinfo_fin(struct inode *inode);
e2f27e51 18921+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 18922+
e49829fe 18923+#ifdef CONFIG_PROC_FS
4a4d8108 18924+/* plink.c */
e49829fe 18925+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 18926+struct au_sbinfo;
e49829fe
JR
18927+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18928+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
18929+#ifdef CONFIG_AUFS_DEBUG
18930+void au_plink_list(struct super_block *sb);
18931+#else
18932+AuStubVoid(au_plink_list, struct super_block *sb)
18933+#endif
18934+int au_plink_test(struct inode *inode);
18935+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18936+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18937+ struct dentry *h_dentry);
e49829fe
JR
18938+void au_plink_put(struct super_block *sb, int verbose);
18939+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 18940+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
18941+#else
18942+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18943+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18944+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18945+AuStubVoid(au_plink_list, struct super_block *sb);
18946+AuStubInt0(au_plink_test, struct inode *inode);
18947+AuStub(struct dentry *, au_plink_lkup, return NULL,
18948+ struct inode *inode, aufs_bindex_t bindex);
18949+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18950+ struct dentry *h_dentry);
18951+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18952+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18953+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18954+#endif /* CONFIG_PROC_FS */
1facf9fc 18955+
c1595e42
JR
18956+#ifdef CONFIG_AUFS_XATTR
18957+/* xattr.c */
7e9cd9fe
AM
18958+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
18959+ unsigned int verbose);
c1595e42 18960+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 18961+void au_xattr_init(struct super_block *sb);
c1595e42
JR
18962+#else
18963+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 18964+ int ignore_flags, unsigned int verbose);
f2c43d5f 18965+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
18966+#endif
18967+
18968+#ifdef CONFIG_FS_POSIX_ACL
18969+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
18970+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
18971+#endif
18972+
18973+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18974+enum {
18975+ AU_XATTR_SET,
c1595e42
JR
18976+ AU_ACL_SET
18977+};
18978+
f2c43d5f 18979+struct au_sxattr {
c1595e42
JR
18980+ int type;
18981+ union {
18982+ struct {
18983+ const char *name;
18984+ const void *value;
18985+ size_t size;
18986+ int flags;
18987+ } set;
18988+ struct {
c1595e42
JR
18989+ struct posix_acl *acl;
18990+ int type;
18991+ } acl_set;
18992+ } u;
18993+};
f2c43d5f
AM
18994+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18995+ struct au_sxattr *arg);
c1595e42
JR
18996+#endif
18997+
4a4d8108 18998+/* ---------------------------------------------------------------------- */
1308ab2a 18999+
4a4d8108
AM
19000+/* lock subclass for iinfo */
19001+enum {
19002+ AuLsc_II_CHILD, /* child first */
19003+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
19004+ AuLsc_II_CHILD3, /* copyup dirs */
19005+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
19006+ AuLsc_II_PARENT2,
19007+ AuLsc_II_PARENT3, /* copyup dirs */
19008+ AuLsc_II_NEW_CHILD
19009+};
1308ab2a 19010+
1facf9fc 19011+/*
4a4d8108
AM
19012+ * ii_read_lock_child, ii_write_lock_child,
19013+ * ii_read_lock_child2, ii_write_lock_child2,
19014+ * ii_read_lock_child3, ii_write_lock_child3,
19015+ * ii_read_lock_parent, ii_write_lock_parent,
19016+ * ii_read_lock_parent2, ii_write_lock_parent2,
19017+ * ii_read_lock_parent3, ii_write_lock_parent3,
19018+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 19019+ */
4a4d8108
AM
19020+#define AuReadLockFunc(name, lsc) \
19021+static inline void ii_read_lock_##name(struct inode *i) \
19022+{ \
19023+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19024+}
19025+
19026+#define AuWriteLockFunc(name, lsc) \
19027+static inline void ii_write_lock_##name(struct inode *i) \
19028+{ \
19029+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19030+}
19031+
19032+#define AuRWLockFuncs(name, lsc) \
19033+ AuReadLockFunc(name, lsc) \
19034+ AuWriteLockFunc(name, lsc)
19035+
19036+AuRWLockFuncs(child, CHILD);
19037+AuRWLockFuncs(child2, CHILD2);
19038+AuRWLockFuncs(child3, CHILD3);
19039+AuRWLockFuncs(parent, PARENT);
19040+AuRWLockFuncs(parent2, PARENT2);
19041+AuRWLockFuncs(parent3, PARENT3);
19042+AuRWLockFuncs(new_child, NEW_CHILD);
19043+
19044+#undef AuReadLockFunc
19045+#undef AuWriteLockFunc
19046+#undef AuRWLockFuncs
1facf9fc 19047+
8b6a4947
AM
19048+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19049+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19050+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19051+
4a4d8108
AM
19052+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19053+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19054+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19055+
4a4d8108 19056+/* ---------------------------------------------------------------------- */
1308ab2a 19057+
027c5e7a
AM
19058+static inline void au_icntnr_init(struct au_icntnr *c)
19059+{
19060+#ifdef CONFIG_AUFS_DEBUG
19061+ c->vfs_inode.i_mode = 0;
19062+#endif
19063+}
19064+
cfc41e69 19065+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19066+{
537831f9
AM
19067+ unsigned int gen;
19068+ struct au_iinfo *iinfo;
be52b249 19069+ struct au_iigen *iigen;
537831f9
AM
19070+
19071+ iinfo = au_ii(inode);
be52b249
AM
19072+ iigen = &iinfo->ii_generation;
19073+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19074+ if (igflags)
19075+ *igflags = iigen->ig_flags;
be52b249
AM
19076+ gen = iigen->ig_generation;
19077+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19078+
19079+ return gen;
4a4d8108 19080+}
1308ab2a 19081+
4a4d8108
AM
19082+/* tiny test for inode number */
19083+/* tmpfs generation is too rough */
19084+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19085+{
19086+ struct au_iinfo *iinfo;
1308ab2a 19087+
4a4d8108
AM
19088+ iinfo = au_ii(inode);
19089+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19090+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19091+ && iinfo->ii_higen == h_inode->i_generation);
19092+}
1308ab2a 19093+
4a4d8108
AM
19094+static inline void au_iigen_dec(struct inode *inode)
19095+{
537831f9 19096+ struct au_iinfo *iinfo;
be52b249 19097+ struct au_iigen *iigen;
537831f9
AM
19098+
19099+ iinfo = au_ii(inode);
be52b249
AM
19100+ iigen = &iinfo->ii_generation;
19101+ spin_lock(&iigen->ig_spin);
19102+ iigen->ig_generation--;
19103+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19104+}
19105+
19106+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19107+{
19108+ int err;
19109+
19110+ err = 0;
537831f9 19111+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19112+ err = -EIO;
19113+
19114+ return err;
4a4d8108 19115+}
1308ab2a 19116+
4a4d8108 19117+/* ---------------------------------------------------------------------- */
1308ab2a 19118+
5afbbe0d
AM
19119+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19120+ aufs_bindex_t bindex)
19121+{
19122+ return iinfo->ii_hinode + bindex;
19123+}
19124+
19125+static inline int au_is_bad_inode(struct inode *inode)
19126+{
19127+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19128+}
19129+
4a4d8108
AM
19130+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19131+ aufs_bindex_t bindex)
19132+{
19133+ IiMustAnyLock(inode);
5afbbe0d 19134+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19135+}
1308ab2a 19136+
5afbbe0d 19137+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19138+{
19139+ IiMustAnyLock(inode);
5afbbe0d 19140+ return au_ii(inode)->ii_btop;
4a4d8108 19141+}
1308ab2a 19142+
5afbbe0d 19143+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19144+{
19145+ IiMustAnyLock(inode);
5afbbe0d 19146+ return au_ii(inode)->ii_bbot;
4a4d8108 19147+}
1308ab2a 19148+
4a4d8108
AM
19149+static inline struct au_vdir *au_ivdir(struct inode *inode)
19150+{
19151+ IiMustAnyLock(inode);
19152+ return au_ii(inode)->ii_vdir;
19153+}
1308ab2a 19154+
4a4d8108
AM
19155+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19156+{
19157+ IiMustAnyLock(inode);
5afbbe0d 19158+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19159+}
1308ab2a 19160+
5afbbe0d 19161+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19162+{
4a4d8108 19163+ IiMustWriteLock(inode);
5afbbe0d 19164+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19165+}
1308ab2a 19166+
5afbbe0d 19167+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19168+{
19169+ IiMustWriteLock(inode);
5afbbe0d 19170+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19171+}
19172+
4a4d8108
AM
19173+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19174+{
19175+ IiMustWriteLock(inode);
19176+ au_ii(inode)->ii_vdir = vdir;
19177+}
1facf9fc 19178+
4a4d8108 19179+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19180+{
4a4d8108 19181+ IiMustAnyLock(inode);
5afbbe0d 19182+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19183+}
dece6358 19184+
4a4d8108 19185+/* ---------------------------------------------------------------------- */
1facf9fc 19186+
4a4d8108
AM
19187+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19188+{
19189+ if (pin)
19190+ return pin->parent;
19191+ return NULL;
1facf9fc 19192+}
19193+
4a4d8108 19194+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19195+{
4a4d8108
AM
19196+ if (pin && pin->hdir)
19197+ return pin->hdir->hi_inode;
19198+ return NULL;
1308ab2a 19199+}
1facf9fc 19200+
4a4d8108
AM
19201+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19202+{
19203+ if (pin)
19204+ return pin->hdir;
19205+ return NULL;
19206+}
1facf9fc 19207+
4a4d8108 19208+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19209+{
4a4d8108
AM
19210+ if (pin)
19211+ pin->dentry = dentry;
19212+}
1308ab2a 19213+
4a4d8108
AM
19214+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19215+ unsigned char lflag)
19216+{
19217+ if (pin) {
7f207e10 19218+ if (lflag)
4a4d8108 19219+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19220+ else
4a4d8108 19221+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19222+ }
4a4d8108
AM
19223+}
19224+
7e9cd9fe 19225+#if 0 /* reserved */
4a4d8108
AM
19226+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19227+{
19228+ if (pin) {
19229+ dput(pin->parent);
19230+ pin->parent = dget(parent);
1facf9fc 19231+ }
4a4d8108 19232+}
7e9cd9fe 19233+#endif
1facf9fc 19234+
4a4d8108
AM
19235+/* ---------------------------------------------------------------------- */
19236+
027c5e7a 19237+struct au_branch;
4a4d8108
AM
19238+#ifdef CONFIG_AUFS_HNOTIFY
19239+struct au_hnotify_op {
19240+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19241+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19242+
19243+ /*
19244+ * if it returns true, the the caller should free hinode->hi_notify,
19245+ * otherwise ->free() frees it.
19246+ */
19247+ int (*free)(struct au_hinode *hinode,
19248+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19249+
19250+ void (*fin)(void);
19251+ int (*init)(void);
027c5e7a
AM
19252+
19253+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19254+ void (*fin_br)(struct au_branch *br);
19255+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19256+};
19257+
19258+/* hnotify.c */
027c5e7a 19259+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19260+void au_hn_free(struct au_hinode *hinode);
19261+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19262+void au_hn_reset(struct inode *inode, unsigned int flags);
19263+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19264+ struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19265+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19266+int au_hnotify_init_br(struct au_branch *br, int perm);
19267+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19268+int __init au_hnotify_init(void);
19269+void au_hnotify_fin(void);
19270+
7f207e10 19271+/* hfsnotify.c */
4a4d8108
AM
19272+extern const struct au_hnotify_op au_hnotify_op;
19273+
19274+static inline
19275+void au_hn_init(struct au_hinode *hinode)
19276+{
19277+ hinode->hi_notify = NULL;
1308ab2a 19278+}
19279+
53392da6
AM
19280+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19281+{
19282+ return hinode->hi_notify;
19283+}
19284+
4a4d8108 19285+#else
c1595e42
JR
19286+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19287+ struct au_hinode *hinode __maybe_unused,
19288+ struct inode *inode __maybe_unused)
19289+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19290+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19291+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19292+ int do_set __maybe_unused)
19293+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19294+ unsigned int flags __maybe_unused)
027c5e7a
AM
19295+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19296+ struct au_branch *br __maybe_unused,
19297+ int perm __maybe_unused)
19298+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19299+ int perm __maybe_unused)
19300+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19301+AuStubInt0(__init au_hnotify_init, void)
19302+AuStubVoid(au_hnotify_fin, void)
19303+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19304+#endif /* CONFIG_AUFS_HNOTIFY */
19305+
19306+static inline void au_hn_suspend(struct au_hinode *hdir)
19307+{
19308+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19309+}
19310+
4a4d8108 19311+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19312+{
4a4d8108
AM
19313+ au_hn_ctl(hdir, /*do_set*/1);
19314+}
1308ab2a 19315+
5afbbe0d 19316+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19317+{
febd17d6 19318+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19319+ au_hn_suspend(hdir);
19320+}
dece6358 19321+
5afbbe0d 19322+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19323+ unsigned int sc __maybe_unused)
19324+{
febd17d6 19325+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19326+ au_hn_suspend(hdir);
1facf9fc 19327+}
1facf9fc 19328+
8b6a4947
AM
19329+#if 0 /* unused */
19330+#include "vfsub.h"
3c1bdaff
AM
19331+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19332+ unsigned int sc)
19333+{
be118d29 19334+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
19335+ au_hn_suspend(hdir);
19336+}
8b6a4947 19337+#endif
3c1bdaff 19338+
5afbbe0d 19339+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19340+{
19341+ au_hn_resume(hdir);
febd17d6 19342+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19343+}
19344+
19345+#endif /* __KERNEL__ */
19346+#endif /* __AUFS_INODE_H__ */
7f207e10
AM
19347diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19348--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
be118d29 19349+++ linux/fs/aufs/ioctl.c 2018-04-06 07:48:44.207938097 +0200
c1595e42 19350@@ -0,0 +1,219 @@
4a4d8108 19351+/*
a2654f78 19352+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
19353+ *
19354+ * This program, aufs is free software; you can redistribute it and/or modify
19355+ * it under the terms of the GNU General Public License as published by
19356+ * the Free Software Foundation; either version 2 of the License, or
19357+ * (at your option) any later version.
19358+ *
19359+ * This program is distributed in the hope that it will be useful,
19360+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19361+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19362+ * GNU General Public License for more details.
19363+ *
19364+ * You should have received a copy of the GNU General Public License
523b37e3 19365+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19366+ */
19367+
19368+/*
19369+ * ioctl
19370+ * plink-management and readdir in userspace.
19371+ * assist the pathconf(3) wrapper library.
c2b27bf2 19372+ * move-down
076b876e 19373+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19374+ */
19375+
c2b27bf2
AM
19376+#include <linux/compat.h>
19377+#include <linux/file.h>
4a4d8108
AM
19378+#include "aufs.h"
19379+
1e00d052 19380+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19381+{
19382+ int err, fd;
5afbbe0d 19383+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19384+ struct file *h_file;
19385+ struct super_block *sb;
19386+ struct dentry *root;
1e00d052
AM
19387+ struct au_branch *br;
19388+ struct aufs_wbr_fd wbrfd = {
19389+ .oflags = au_dir_roflags,
19390+ .brid = -1
19391+ };
19392+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19393+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19394+
1e00d052
AM
19395+ AuDebugOn(wbrfd.oflags & ~valid);
19396+
19397+ if (arg) {
19398+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19399+ if (unlikely(err)) {
19400+ err = -EFAULT;
19401+ goto out;
19402+ }
19403+
19404+ err = -EINVAL;
19405+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19406+ wbrfd.oflags |= au_dir_roflags;
19407+ AuDbg("0%o\n", wbrfd.oflags);
19408+ if (unlikely(wbrfd.oflags & ~valid))
19409+ goto out;
19410+ }
19411+
2000de60 19412+ fd = get_unused_fd_flags(0);
1e00d052
AM
19413+ err = fd;
19414+ if (unlikely(fd < 0))
4a4d8108 19415+ goto out;
4a4d8108 19416+
1e00d052 19417+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19418+ wbi = 0;
1e00d052 19419+ br = NULL;
4a4d8108
AM
19420+ sb = path->dentry->d_sb;
19421+ root = sb->s_root;
19422+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19423+ bbot = au_sbbot(sb);
1e00d052
AM
19424+ if (wbrfd.brid >= 0) {
19425+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19426+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19427+ goto out_unlock;
19428+ }
19429+
19430+ h_file = ERR_PTR(-ENOENT);
19431+ br = au_sbr(sb, wbi);
19432+ if (!au_br_writable(br->br_perm)) {
19433+ if (arg)
19434+ goto out_unlock;
19435+
19436+ bindex = wbi + 1;
19437+ wbi = -1;
5afbbe0d 19438+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19439+ br = au_sbr(sb, bindex);
19440+ if (au_br_writable(br->br_perm)) {
4a4d8108 19441+ wbi = bindex;
1e00d052 19442+ br = au_sbr(sb, wbi);
4a4d8108
AM
19443+ break;
19444+ }
19445+ }
4a4d8108
AM
19446+ }
19447+ AuDbg("wbi %d\n", wbi);
1e00d052 19448+ if (wbi >= 0)
392086de
AM
19449+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19450+ /*force_wr*/0);
1e00d052
AM
19451+
19452+out_unlock:
4a4d8108
AM
19453+ aufs_read_unlock(root, AuLock_IR);
19454+ err = PTR_ERR(h_file);
19455+ if (IS_ERR(h_file))
19456+ goto out_fd;
19457+
5afbbe0d 19458+ au_br_put(br); /* cf. au_h_open() */
4a4d8108
AM
19459+ fd_install(fd, h_file);
19460+ err = fd;
19461+ goto out; /* success */
19462+
4f0767ce 19463+out_fd:
4a4d8108 19464+ put_unused_fd(fd);
4f0767ce 19465+out:
1e00d052 19466+ AuTraceErr(err);
4a4d8108
AM
19467+ return err;
19468+}
19469+
19470+/* ---------------------------------------------------------------------- */
19471+
19472+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19473+{
19474+ long err;
c1595e42 19475+ struct dentry *dentry;
4a4d8108
AM
19476+
19477+ switch (cmd) {
4a4d8108
AM
19478+ case AUFS_CTL_RDU:
19479+ case AUFS_CTL_RDU_INO:
19480+ err = au_rdu_ioctl(file, cmd, arg);
19481+ break;
19482+
19483+ case AUFS_CTL_WBR_FD:
1e00d052 19484+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19485+ break;
19486+
027c5e7a
AM
19487+ case AUFS_CTL_IBUSY:
19488+ err = au_ibusy_ioctl(file, arg);
19489+ break;
19490+
076b876e
AM
19491+ case AUFS_CTL_BRINFO:
19492+ err = au_brinfo_ioctl(file, arg);
19493+ break;
19494+
19495+ case AUFS_CTL_FHSM_FD:
2000de60 19496+ dentry = file->f_path.dentry;
c1595e42
JR
19497+ if (IS_ROOT(dentry))
19498+ err = au_fhsm_fd(dentry->d_sb, arg);
19499+ else
19500+ err = -ENOTTY;
076b876e
AM
19501+ break;
19502+
4a4d8108
AM
19503+ default:
19504+ /* do not call the lower */
19505+ AuDbg("0x%x\n", cmd);
19506+ err = -ENOTTY;
19507+ }
19508+
19509+ AuTraceErr(err);
19510+ return err;
19511+}
19512+
19513+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19514+{
19515+ long err;
19516+
19517+ switch (cmd) {
c2b27bf2 19518+ case AUFS_CTL_MVDOWN:
2000de60 19519+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19520+ break;
19521+
4a4d8108 19522+ case AUFS_CTL_WBR_FD:
1e00d052 19523+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19524+ break;
19525+
19526+ default:
19527+ /* do not call the lower */
19528+ AuDbg("0x%x\n", cmd);
19529+ err = -ENOTTY;
19530+ }
19531+
19532+ AuTraceErr(err);
19533+ return err;
19534+}
b752ccd1
AM
19535+
19536+#ifdef CONFIG_COMPAT
19537+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19538+ unsigned long arg)
19539+{
19540+ long err;
19541+
19542+ switch (cmd) {
19543+ case AUFS_CTL_RDU:
19544+ case AUFS_CTL_RDU_INO:
19545+ err = au_rdu_compat_ioctl(file, cmd, arg);
19546+ break;
19547+
027c5e7a
AM
19548+ case AUFS_CTL_IBUSY:
19549+ err = au_ibusy_compat_ioctl(file, arg);
19550+ break;
19551+
076b876e
AM
19552+ case AUFS_CTL_BRINFO:
19553+ err = au_brinfo_compat_ioctl(file, arg);
19554+ break;
19555+
b752ccd1
AM
19556+ default:
19557+ err = aufs_ioctl_dir(file, cmd, arg);
19558+ }
19559+
19560+ AuTraceErr(err);
19561+ return err;
19562+}
19563+
b752ccd1
AM
19564+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19565+ unsigned long arg)
19566+{
19567+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19568+}
19569+#endif
7f207e10
AM
19570diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19571--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
be118d29 19572+++ linux/fs/aufs/i_op_add.c 2018-04-06 07:48:44.204604724 +0200
521ced18 19573@@ -0,0 +1,920 @@
4a4d8108 19574+/*
a2654f78 19575+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
19576+ *
19577+ * This program, aufs is free software; you can redistribute it and/or modify
19578+ * it under the terms of the GNU General Public License as published by
19579+ * the Free Software Foundation; either version 2 of the License, or
19580+ * (at your option) any later version.
19581+ *
19582+ * This program is distributed in the hope that it will be useful,
19583+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19584+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19585+ * GNU General Public License for more details.
19586+ *
19587+ * You should have received a copy of the GNU General Public License
523b37e3 19588+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19589+ */
19590+
19591+/*
19592+ * inode operations (add entry)
19593+ */
19594+
19595+#include "aufs.h"
19596+
19597+/*
19598+ * final procedure of adding a new entry, except link(2).
19599+ * remove whiteout, instantiate, copyup the parent dir's times and size
19600+ * and update version.
19601+ * if it failed, re-create the removed whiteout.
19602+ */
19603+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19604+ struct dentry *wh_dentry, struct dentry *dentry)
19605+{
19606+ int err, rerr;
19607+ aufs_bindex_t bwh;
19608+ struct path h_path;
076b876e 19609+ struct super_block *sb;
4a4d8108
AM
19610+ struct inode *inode, *h_dir;
19611+ struct dentry *wh;
19612+
19613+ bwh = -1;
076b876e 19614+ sb = dir->i_sb;
4a4d8108 19615+ if (wh_dentry) {
5527c038 19616+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19617+ IMustLock(h_dir);
19618+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19619+ bwh = au_dbwh(dentry);
19620+ h_path.dentry = wh_dentry;
076b876e 19621+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19622+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19623+ dentry);
19624+ if (unlikely(err))
19625+ goto out;
19626+ }
19627+
19628+ inode = au_new_inode(dentry, /*must_new*/1);
19629+ if (!IS_ERR(inode)) {
19630+ d_instantiate(dentry, inode);
5527c038 19631+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19632+ IMustLock(dir);
b912730e 19633+ au_dir_ts(dir, bindex);
be118d29 19634+ inode_inc_iversion(dir);
076b876e 19635+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19636+ return 0; /* success */
19637+ }
19638+
19639+ err = PTR_ERR(inode);
19640+ if (!wh_dentry)
19641+ goto out;
19642+
19643+ /* revert */
19644+ /* dir inode is locked */
19645+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19646+ rerr = PTR_ERR(wh);
19647+ if (IS_ERR(wh)) {
523b37e3
AM
19648+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19649+ dentry, err, rerr);
4a4d8108
AM
19650+ err = -EIO;
19651+ } else
19652+ dput(wh);
19653+
4f0767ce 19654+out:
4a4d8108
AM
19655+ return err;
19656+}
19657+
027c5e7a
AM
19658+static int au_d_may_add(struct dentry *dentry)
19659+{
19660+ int err;
19661+
19662+ err = 0;
19663+ if (unlikely(d_unhashed(dentry)))
19664+ err = -ENOENT;
5527c038 19665+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19666+ err = -EEXIST;
19667+ return err;
19668+}
19669+
4a4d8108
AM
19670+/*
19671+ * simple tests for the adding inode operations.
19672+ * following the checks in vfs, plus the parent-child relationship.
19673+ */
19674+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19675+ struct dentry *h_parent, int isdir)
19676+{
19677+ int err;
19678+ umode_t h_mode;
19679+ struct dentry *h_dentry;
19680+ struct inode *h_inode;
19681+
19682+ err = -ENAMETOOLONG;
19683+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19684+ goto out;
19685+
19686+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19687+ if (d_really_is_negative(dentry)) {
4a4d8108 19688+ err = -EEXIST;
5527c038 19689+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19690+ goto out;
19691+ } else {
19692+ /* rename(2) case */
19693+ err = -EIO;
5527c038
JR
19694+ if (unlikely(d_is_negative(h_dentry)))
19695+ goto out;
19696+ h_inode = d_inode(h_dentry);
19697+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19698+ goto out;
19699+
19700+ h_mode = h_inode->i_mode;
19701+ if (!isdir) {
19702+ err = -EISDIR;
19703+ if (unlikely(S_ISDIR(h_mode)))
19704+ goto out;
19705+ } else if (unlikely(!S_ISDIR(h_mode))) {
19706+ err = -ENOTDIR;
19707+ goto out;
19708+ }
19709+ }
19710+
19711+ err = 0;
19712+ /* expected parent dir is locked */
19713+ if (unlikely(h_parent != h_dentry->d_parent))
19714+ err = -EIO;
19715+
4f0767ce 19716+out:
4a4d8108
AM
19717+ AuTraceErr(err);
19718+ return err;
19719+}
19720+
19721+/*
19722+ * initial procedure of adding a new entry.
19723+ * prepare writable branch and the parent dir, lock it,
19724+ * and lookup whiteout for the new entry.
19725+ */
19726+static struct dentry*
19727+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19728+ struct dentry *src_dentry, struct au_pin *pin,
19729+ struct au_wr_dir_args *wr_dir_args)
19730+{
19731+ struct dentry *wh_dentry, *h_parent;
19732+ struct super_block *sb;
19733+ struct au_branch *br;
19734+ int err;
19735+ unsigned int udba;
19736+ aufs_bindex_t bcpup;
19737+
523b37e3 19738+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19739+
19740+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19741+ bcpup = err;
19742+ wh_dentry = ERR_PTR(err);
19743+ if (unlikely(err < 0))
19744+ goto out;
19745+
19746+ sb = dentry->d_sb;
19747+ udba = au_opt_udba(sb);
19748+ err = au_pin(pin, dentry, bcpup, udba,
19749+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19750+ wh_dentry = ERR_PTR(err);
19751+ if (unlikely(err))
19752+ goto out;
19753+
19754+ h_parent = au_pinned_h_parent(pin);
19755+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19756+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19757+ err = au_may_add(dentry, bcpup, h_parent,
19758+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19759+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19760+ err = -ENAMETOOLONG;
19761+ wh_dentry = ERR_PTR(err);
19762+ if (unlikely(err))
19763+ goto out_unpin;
19764+
19765+ br = au_sbr(sb, bcpup);
19766+ if (dt) {
19767+ struct path tmp = {
19768+ .dentry = h_parent,
86dc4139 19769+ .mnt = au_br_mnt(br)
4a4d8108
AM
19770+ };
19771+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19772+ }
19773+
19774+ wh_dentry = NULL;
19775+ if (bcpup != au_dbwh(dentry))
19776+ goto out; /* success */
19777+
2000de60
JR
19778+ /*
19779+ * ENAMETOOLONG here means that if we allowed create such name, then it
19780+ * would not be able to removed in the future. So we don't allow such
19781+ * name here and we don't handle ENAMETOOLONG differently here.
19782+ */
4a4d8108
AM
19783+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19784+
4f0767ce 19785+out_unpin:
4a4d8108
AM
19786+ if (IS_ERR(wh_dentry))
19787+ au_unpin(pin);
4f0767ce 19788+out:
4a4d8108
AM
19789+ return wh_dentry;
19790+}
19791+
19792+/* ---------------------------------------------------------------------- */
19793+
19794+enum { Mknod, Symlink, Creat };
19795+struct simple_arg {
19796+ int type;
19797+ union {
19798+ struct {
b912730e
AM
19799+ umode_t mode;
19800+ bool want_excl;
19801+ bool try_aopen;
19802+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19803+ } c;
19804+ struct {
19805+ const char *symname;
19806+ } s;
19807+ struct {
7eafdf33 19808+ umode_t mode;
4a4d8108
AM
19809+ dev_t dev;
19810+ } m;
19811+ } u;
19812+};
19813+
19814+static int add_simple(struct inode *dir, struct dentry *dentry,
19815+ struct simple_arg *arg)
19816+{
076b876e 19817+ int err, rerr;
5afbbe0d 19818+ aufs_bindex_t btop;
4a4d8108 19819+ unsigned char created;
b912730e
AM
19820+ const unsigned char try_aopen
19821+ = (arg->type == Creat && arg->u.c.try_aopen);
4a4d8108
AM
19822+ struct dentry *wh_dentry, *parent;
19823+ struct inode *h_dir;
b912730e
AM
19824+ struct super_block *sb;
19825+ struct au_branch *br;
c2b27bf2
AM
19826+ /* to reuduce stack size */
19827+ struct {
19828+ struct au_dtime dt;
19829+ struct au_pin pin;
19830+ struct path h_path;
19831+ struct au_wr_dir_args wr_dir_args;
19832+ } *a;
4a4d8108 19833+
523b37e3 19834+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19835+ IMustLock(dir);
19836+
c2b27bf2
AM
19837+ err = -ENOMEM;
19838+ a = kmalloc(sizeof(*a), GFP_NOFS);
19839+ if (unlikely(!a))
19840+ goto out;
19841+ a->wr_dir_args.force_btgt = -1;
19842+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19843+
4a4d8108 19844+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
19845+ if (!try_aopen) {
19846+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19847+ if (unlikely(err))
19848+ goto out_free;
19849+ }
027c5e7a
AM
19850+ err = au_d_may_add(dentry);
19851+ if (unlikely(err))
19852+ goto out_unlock;
b912730e
AM
19853+ if (!try_aopen)
19854+ di_write_lock_parent(parent);
c2b27bf2
AM
19855+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19856+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
19857+ err = PTR_ERR(wh_dentry);
19858+ if (IS_ERR(wh_dentry))
027c5e7a 19859+ goto out_parent;
4a4d8108 19860+
5afbbe0d 19861+ btop = au_dbtop(dentry);
b912730e 19862+ sb = dentry->d_sb;
5afbbe0d
AM
19863+ br = au_sbr(sb, btop);
19864+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 19865+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 19866+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
19867+ switch (arg->type) {
19868+ case Creat:
b912730e
AM
19869+ err = 0;
19870+ if (!try_aopen || !h_dir->i_op->atomic_open)
19871+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19872+ arg->u.c.want_excl);
19873+ else
19874+ err = vfsub_atomic_open(h_dir, a->h_path.dentry,
19875+ arg->u.c.aopen, br);
4a4d8108
AM
19876+ break;
19877+ case Symlink:
c2b27bf2 19878+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
4a4d8108
AM
19879+ break;
19880+ case Mknod:
c2b27bf2
AM
19881+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19882+ arg->u.m.dev);
4a4d8108
AM
19883+ break;
19884+ default:
19885+ BUG();
19886+ }
19887+ created = !err;
19888+ if (!err)
5afbbe0d 19889+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108
AM
19890+
19891+ /* revert */
5527c038 19892+ if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
523b37e3
AM
19893+ /* no delegation since it is just created */
19894+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19895+ /*force*/0);
4a4d8108 19896+ if (rerr) {
523b37e3
AM
19897+ AuIOErr("%pd revert failure(%d, %d)\n",
19898+ dentry, err, rerr);
4a4d8108
AM
19899+ err = -EIO;
19900+ }
c2b27bf2 19901+ au_dtime_revert(&a->dt);
4a4d8108
AM
19902+ }
19903+
b912730e
AM
19904+ if (!err && try_aopen && !h_dir->i_op->atomic_open)
19905+ *arg->u.c.aopen->opened |= FILE_CREATED;
19906+
c2b27bf2 19907+ au_unpin(&a->pin);
4a4d8108
AM
19908+ dput(wh_dentry);
19909+
027c5e7a 19910+out_parent:
b912730e
AM
19911+ if (!try_aopen)
19912+ di_write_unlock(parent);
027c5e7a 19913+out_unlock:
4a4d8108 19914+ if (unlikely(err)) {
5afbbe0d 19915+ au_update_dbtop(dentry);
4a4d8108
AM
19916+ d_drop(dentry);
19917+ }
b912730e
AM
19918+ if (!try_aopen)
19919+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 19920+out_free:
1c60b727 19921+ kfree(a);
027c5e7a 19922+out:
4a4d8108
AM
19923+ return err;
19924+}
19925+
7eafdf33
AM
19926+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19927+ dev_t dev)
4a4d8108
AM
19928+{
19929+ struct simple_arg arg = {
19930+ .type = Mknod,
19931+ .u.m = {
19932+ .mode = mode,
19933+ .dev = dev
19934+ }
19935+ };
19936+ return add_simple(dir, dentry, &arg);
19937+}
19938+
19939+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
19940+{
19941+ struct simple_arg arg = {
19942+ .type = Symlink,
19943+ .u.s.symname = symname
19944+ };
19945+ return add_simple(dir, dentry, &arg);
19946+}
19947+
7eafdf33 19948+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 19949+ bool want_excl)
4a4d8108
AM
19950+{
19951+ struct simple_arg arg = {
19952+ .type = Creat,
19953+ .u.c = {
b4510431
AM
19954+ .mode = mode,
19955+ .want_excl = want_excl
4a4d8108
AM
19956+ }
19957+ };
19958+ return add_simple(dir, dentry, &arg);
19959+}
19960+
b912730e
AM
19961+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19962+ struct vfsub_aopen_args *aopen_args)
19963+{
19964+ struct simple_arg arg = {
19965+ .type = Creat,
19966+ .u.c = {
19967+ .mode = aopen_args->create_mode,
19968+ .want_excl = aopen_args->open_flag & O_EXCL,
19969+ .try_aopen = true,
19970+ .aopen = aopen_args
19971+ }
19972+ };
19973+ return add_simple(dir, dentry, &arg);
19974+}
19975+
38d290e6
JR
19976+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
19977+{
19978+ int err;
19979+ aufs_bindex_t bindex;
19980+ struct super_block *sb;
19981+ struct dentry *parent, *h_parent, *h_dentry;
19982+ struct inode *h_dir, *inode;
19983+ struct vfsmount *h_mnt;
19984+ struct au_wr_dir_args wr_dir_args = {
19985+ .force_btgt = -1,
19986+ .flags = AuWrDir_TMPFILE
19987+ };
19988+
19989+ /* copy-up may happen */
febd17d6 19990+ inode_lock(dir);
38d290e6
JR
19991+
19992+ sb = dir->i_sb;
19993+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19994+ if (unlikely(err))
19995+ goto out;
19996+
19997+ err = au_di_init(dentry);
19998+ if (unlikely(err))
19999+ goto out_si;
20000+
20001+ err = -EBUSY;
20002+ parent = d_find_any_alias(dir);
20003+ AuDebugOn(!parent);
20004+ di_write_lock_parent(parent);
5527c038 20005+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
20006+ goto out_parent;
20007+
20008+ err = au_digen_test(parent, au_sigen(sb));
20009+ if (unlikely(err))
20010+ goto out_parent;
20011+
5afbbe0d
AM
20012+ bindex = au_dbtop(parent);
20013+ au_set_dbtop(dentry, bindex);
20014+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20015+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20016+ bindex = err;
20017+ if (unlikely(err < 0))
20018+ goto out_parent;
20019+
20020+ err = -EOPNOTSUPP;
20021+ h_dir = au_h_iptr(dir, bindex);
20022+ if (unlikely(!h_dir->i_op->tmpfile))
20023+ goto out_parent;
20024+
20025+ h_mnt = au_sbr_mnt(sb, bindex);
20026+ err = vfsub_mnt_want_write(h_mnt);
20027+ if (unlikely(err))
20028+ goto out_parent;
20029+
20030+ h_parent = au_h_dptr(parent, bindex);
521ced18
JR
20031+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20032+ if (IS_ERR(h_dentry)) {
20033+ err = PTR_ERR(h_dentry);
38d290e6 20034+ goto out_mnt;
521ced18 20035+ }
38d290e6 20036+
5afbbe0d
AM
20037+ au_set_dbtop(dentry, bindex);
20038+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20039+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20040+ inode = au_new_inode(dentry, /*must_new*/1);
20041+ if (IS_ERR(inode)) {
20042+ err = PTR_ERR(inode);
20043+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20044+ au_set_dbtop(dentry, -1);
20045+ au_set_dbbot(dentry, -1);
38d290e6
JR
20046+ } else {
20047+ if (!inode->i_nlink)
20048+ set_nlink(inode, 1);
20049+ d_tmpfile(dentry, inode);
20050+ au_di(dentry)->di_tmpfile = 1;
20051+
20052+ /* update without i_mutex */
5afbbe0d 20053+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20054+ au_cpup_attr_timesizes(dir);
20055+ }
38d290e6 20056+ dput(h_dentry);
521ced18 20057+
38d290e6
JR
20058+out_mnt:
20059+ vfsub_mnt_drop_write(h_mnt);
20060+out_parent:
20061+ di_write_unlock(parent);
20062+ dput(parent);
20063+ di_write_unlock(dentry);
5afbbe0d 20064+ if (unlikely(err)) {
38d290e6
JR
20065+ au_di_fin(dentry);
20066+ dentry->d_fsdata = NULL;
20067+ }
20068+out_si:
20069+ si_read_unlock(sb);
20070+out:
febd17d6 20071+ inode_unlock(dir);
38d290e6
JR
20072+ return err;
20073+}
20074+
4a4d8108
AM
20075+/* ---------------------------------------------------------------------- */
20076+
20077+struct au_link_args {
20078+ aufs_bindex_t bdst, bsrc;
20079+ struct au_pin pin;
20080+ struct path h_path;
20081+ struct dentry *src_parent, *parent;
20082+};
20083+
20084+static int au_cpup_before_link(struct dentry *src_dentry,
20085+ struct au_link_args *a)
20086+{
20087+ int err;
20088+ struct dentry *h_src_dentry;
c2b27bf2
AM
20089+ struct au_cp_generic cpg = {
20090+ .dentry = src_dentry,
20091+ .bdst = a->bdst,
20092+ .bsrc = a->bsrc,
20093+ .len = -1,
20094+ .pin = &a->pin,
20095+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20096+ };
4a4d8108
AM
20097+
20098+ di_read_lock_parent(a->src_parent, AuLock_IR);
20099+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20100+ if (unlikely(err))
20101+ goto out;
20102+
20103+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20104+ err = au_pin(&a->pin, src_dentry, a->bdst,
20105+ au_opt_udba(src_dentry->d_sb),
20106+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20107+ if (unlikely(err))
20108+ goto out;
367653fa 20109+
c2b27bf2 20110+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20111+ au_unpin(&a->pin);
20112+
4f0767ce 20113+out:
4a4d8108
AM
20114+ di_read_unlock(a->src_parent, AuLock_IR);
20115+ return err;
20116+}
20117+
86dc4139
AM
20118+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20119+ struct au_link_args *a)
4a4d8108
AM
20120+{
20121+ int err;
20122+ unsigned char plink;
5afbbe0d 20123+ aufs_bindex_t bbot;
4a4d8108 20124+ struct dentry *h_src_dentry;
523b37e3 20125+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20126+ struct super_block *sb;
20127+ struct file *h_file;
20128+
20129+ plink = 0;
20130+ h_inode = NULL;
20131+ sb = src_dentry->d_sb;
5527c038 20132+ inode = d_inode(src_dentry);
5afbbe0d 20133+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20134+ h_inode = au_h_iptr(inode, a->bdst);
20135+ if (!h_inode || !h_inode->i_nlink) {
20136+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20137+ bbot = au_dbbot(dentry);
20138+ if (bbot < a->bsrc)
20139+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20140+ au_set_h_dptr(dentry, a->bsrc,
20141+ dget(au_h_dptr(src_dentry, a->bsrc)));
20142+ dget(a->h_path.dentry);
20143+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20144+ AuDbg("temporary d_inode...\n");
20145+ spin_lock(&dentry->d_lock);
5527c038 20146+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20147+ spin_unlock(&dentry->d_lock);
392086de 20148+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20149+ if (IS_ERR(h_file))
4a4d8108 20150+ err = PTR_ERR(h_file);
86dc4139 20151+ else {
c2b27bf2
AM
20152+ struct au_cp_generic cpg = {
20153+ .dentry = dentry,
20154+ .bdst = a->bdst,
20155+ .bsrc = -1,
20156+ .len = -1,
20157+ .pin = &a->pin,
20158+ .flags = AuCpup_KEEPLINO
20159+ };
20160+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20161+ au_h_open_post(dentry, a->bsrc, h_file);
20162+ if (!err) {
20163+ dput(a->h_path.dentry);
20164+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20165+ } else
20166+ au_set_h_dptr(dentry, a->bdst,
20167+ a->h_path.dentry);
20168+ }
c1595e42 20169+ spin_lock(&dentry->d_lock);
86dc4139 20170+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20171+ spin_unlock(&dentry->d_lock);
20172+ AuDbg("temporary d_inode...done\n");
86dc4139 20173+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20174+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20175+ } else {
20176+ /* the inode of src_dentry already exists on a.bdst branch */
20177+ h_src_dentry = d_find_alias(h_inode);
20178+ if (!h_src_dentry && au_plink_test(inode)) {
20179+ plink = 1;
20180+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20181+ err = PTR_ERR(h_src_dentry);
20182+ if (IS_ERR(h_src_dentry))
20183+ goto out;
20184+
5527c038 20185+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20186+ dput(h_src_dentry);
20187+ h_src_dentry = NULL;
20188+ }
20189+
20190+ }
20191+ if (h_src_dentry) {
523b37e3 20192+ delegated = NULL;
4a4d8108 20193+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20194+ &a->h_path, &delegated);
20195+ if (unlikely(err == -EWOULDBLOCK)) {
20196+ pr_warn("cannot retry for NFSv4 delegation"
20197+ " for an internal link\n");
20198+ iput(delegated);
20199+ }
4a4d8108
AM
20200+ dput(h_src_dentry);
20201+ } else {
20202+ AuIOErr("no dentry found for hi%lu on b%d\n",
20203+ h_inode->i_ino, a->bdst);
20204+ err = -EIO;
20205+ }
20206+ }
20207+
20208+ if (!err && !plink)
20209+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20210+
20211+out:
2cbb1c4b 20212+ AuTraceErr(err);
4a4d8108
AM
20213+ return err;
20214+}
20215+
20216+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20217+ struct dentry *dentry)
20218+{
20219+ int err, rerr;
20220+ struct au_dtime dt;
20221+ struct au_link_args *a;
20222+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20223+ struct inode *inode, *delegated;
4a4d8108
AM
20224+ struct super_block *sb;
20225+ struct au_wr_dir_args wr_dir_args = {
20226+ /* .force_btgt = -1, */
20227+ .flags = AuWrDir_ADD_ENTRY
20228+ };
20229+
20230+ IMustLock(dir);
5527c038 20231+ inode = d_inode(src_dentry);
4a4d8108
AM
20232+ IMustLock(inode);
20233+
4a4d8108
AM
20234+ err = -ENOMEM;
20235+ a = kzalloc(sizeof(*a), GFP_NOFS);
20236+ if (unlikely(!a))
20237+ goto out;
20238+
20239+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20240+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20241+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20242+ if (unlikely(err))
20243+ goto out_kfree;
38d290e6 20244+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20245+ if (unlikely(err))
20246+ goto out_unlock;
20247+ err = au_d_may_add(dentry);
20248+ if (unlikely(err))
20249+ goto out_unlock;
e49829fe 20250+
4a4d8108 20251+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20252+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20253+
20254+ di_write_lock_parent(a->parent);
20255+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20256+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20257+ &wr_dir_args);
20258+ err = PTR_ERR(wh_dentry);
20259+ if (IS_ERR(wh_dentry))
027c5e7a 20260+ goto out_parent;
4a4d8108
AM
20261+
20262+ err = 0;
20263+ sb = dentry->d_sb;
5afbbe0d 20264+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20265+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20266+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20267+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20268+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20269+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20270+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20271+ if (!h_src_dentry) {
5afbbe0d 20272+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20273+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20274+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20275+ } else if (IS_ERR(h_src_dentry)) {
20276+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20277+ goto out_parent;
38d290e6 20278+ }
2cbb1c4b 20279+
f2c43d5f
AM
20280+ /*
20281+ * aufs doesn't touch the credential so
20282+ * security_dentry_create_files_as() is unnecrssary.
20283+ */
4a4d8108
AM
20284+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20285+ if (a->bdst < a->bsrc
20286+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20287+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20288+ else {
20289+ delegated = NULL;
4a4d8108 20290+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20291+ &a->h_path, &delegated);
20292+ if (unlikely(err == -EWOULDBLOCK)) {
20293+ pr_warn("cannot retry for NFSv4 delegation"
20294+ " for an internal link\n");
20295+ iput(delegated);
20296+ }
20297+ }
2cbb1c4b 20298+ dput(h_src_dentry);
4a4d8108
AM
20299+ } else {
20300+ /*
20301+ * copyup src_dentry to the branch we process,
20302+ * and then link(2) to it.
20303+ */
2cbb1c4b 20304+ dput(h_src_dentry);
4a4d8108
AM
20305+ if (a->bdst < a->bsrc
20306+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20307+ au_unpin(&a->pin);
20308+ di_write_unlock(a->parent);
20309+ err = au_cpup_before_link(src_dentry, a);
20310+ di_write_lock_parent(a->parent);
20311+ if (!err)
20312+ err = au_pin(&a->pin, dentry, a->bdst,
20313+ au_opt_udba(sb),
20314+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20315+ if (unlikely(err))
20316+ goto out_wh;
20317+ }
20318+ if (!err) {
20319+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20320+ err = -ENOENT;
5527c038 20321+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20322+ delegated = NULL;
4a4d8108
AM
20323+ err = vfsub_link(h_src_dentry,
20324+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20325+ &a->h_path, &delegated);
20326+ if (unlikely(err == -EWOULDBLOCK)) {
20327+ pr_warn("cannot retry"
20328+ " for NFSv4 delegation"
20329+ " for an internal link\n");
20330+ iput(delegated);
20331+ }
20332+ }
4a4d8108
AM
20333+ }
20334+ }
20335+ if (unlikely(err))
20336+ goto out_unpin;
20337+
20338+ if (wh_dentry) {
20339+ a->h_path.dentry = wh_dentry;
20340+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20341+ dentry);
20342+ if (unlikely(err))
20343+ goto out_revert;
20344+ }
20345+
b912730e 20346+ au_dir_ts(dir, a->bdst);
be118d29 20347+ inode_inc_iversion(dir);
4a4d8108
AM
20348+ inc_nlink(inode);
20349+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20350+ d_instantiate(dentry, au_igrab(inode));
20351+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20352+ /* some filesystem calls d_drop() */
20353+ d_drop(dentry);
076b876e
AM
20354+ /* some filesystems consume an inode even hardlink */
20355+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20356+ goto out_unpin; /* success */
20357+
4f0767ce 20358+out_revert:
523b37e3
AM
20359+ /* no delegation since it is just created */
20360+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20361+ /*delegated*/NULL, /*force*/0);
027c5e7a 20362+ if (unlikely(rerr)) {
523b37e3 20363+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20364+ err = -EIO;
20365+ }
4a4d8108 20366+ au_dtime_revert(&dt);
4f0767ce 20367+out_unpin:
4a4d8108 20368+ au_unpin(&a->pin);
4f0767ce 20369+out_wh:
4a4d8108 20370+ dput(wh_dentry);
027c5e7a
AM
20371+out_parent:
20372+ di_write_unlock(a->parent);
20373+ dput(a->src_parent);
4f0767ce 20374+out_unlock:
4a4d8108 20375+ if (unlikely(err)) {
5afbbe0d 20376+ au_update_dbtop(dentry);
4a4d8108
AM
20377+ d_drop(dentry);
20378+ }
4a4d8108 20379+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20380+out_kfree:
1c60b727 20381+ kfree(a);
4f0767ce 20382+out:
86dc4139 20383+ AuTraceErr(err);
4a4d8108
AM
20384+ return err;
20385+}
20386+
7eafdf33 20387+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
20388+{
20389+ int err, rerr;
20390+ aufs_bindex_t bindex;
20391+ unsigned char diropq;
20392+ struct path h_path;
20393+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20394+ struct inode *h_inode;
4a4d8108
AM
20395+ struct super_block *sb;
20396+ struct {
20397+ struct au_pin pin;
20398+ struct au_dtime dt;
20399+ } *a; /* reduce the stack usage */
20400+ struct au_wr_dir_args wr_dir_args = {
20401+ .force_btgt = -1,
20402+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20403+ };
20404+
20405+ IMustLock(dir);
20406+
20407+ err = -ENOMEM;
20408+ a = kmalloc(sizeof(*a), GFP_NOFS);
20409+ if (unlikely(!a))
20410+ goto out;
20411+
027c5e7a
AM
20412+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20413+ if (unlikely(err))
20414+ goto out_free;
20415+ err = au_d_may_add(dentry);
20416+ if (unlikely(err))
20417+ goto out_unlock;
20418+
4a4d8108
AM
20419+ parent = dentry->d_parent; /* dir inode is locked */
20420+ di_write_lock_parent(parent);
20421+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20422+ &a->pin, &wr_dir_args);
20423+ err = PTR_ERR(wh_dentry);
20424+ if (IS_ERR(wh_dentry))
027c5e7a 20425+ goto out_parent;
4a4d8108
AM
20426+
20427+ sb = dentry->d_sb;
5afbbe0d 20428+ bindex = au_dbtop(dentry);
4a4d8108
AM
20429+ h_path.dentry = au_h_dptr(dentry, bindex);
20430+ h_path.mnt = au_sbr_mnt(sb, bindex);
20431+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20432+ if (unlikely(err))
027c5e7a 20433+ goto out_unpin;
4a4d8108
AM
20434+
20435+ /* make the dir opaque */
20436+ diropq = 0;
febd17d6 20437+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20438+ if (wh_dentry
20439+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20440+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20441+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20442+ inode_unlock(h_inode);
4a4d8108
AM
20443+ err = PTR_ERR(opq_dentry);
20444+ if (IS_ERR(opq_dentry))
20445+ goto out_dir;
20446+ dput(opq_dentry);
20447+ diropq = 1;
20448+ }
20449+
20450+ err = epilog(dir, bindex, wh_dentry, dentry);
20451+ if (!err) {
20452+ inc_nlink(dir);
027c5e7a 20453+ goto out_unpin; /* success */
4a4d8108
AM
20454+ }
20455+
20456+ /* revert */
20457+ if (diropq) {
20458+ AuLabel(revert opq);
febd17d6 20459+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20460+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20461+ inode_unlock(h_inode);
4a4d8108 20462+ if (rerr) {
523b37e3
AM
20463+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20464+ dentry, err, rerr);
4a4d8108
AM
20465+ err = -EIO;
20466+ }
20467+ }
20468+
4f0767ce 20469+out_dir:
4a4d8108
AM
20470+ AuLabel(revert dir);
20471+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20472+ if (rerr) {
523b37e3
AM
20473+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20474+ dentry, err, rerr);
4a4d8108
AM
20475+ err = -EIO;
20476+ }
4a4d8108 20477+ au_dtime_revert(&a->dt);
027c5e7a 20478+out_unpin:
4a4d8108
AM
20479+ au_unpin(&a->pin);
20480+ dput(wh_dentry);
027c5e7a
AM
20481+out_parent:
20482+ di_write_unlock(parent);
20483+out_unlock:
4a4d8108 20484+ if (unlikely(err)) {
5afbbe0d 20485+ au_update_dbtop(dentry);
4a4d8108
AM
20486+ d_drop(dentry);
20487+ }
4a4d8108 20488+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20489+out_free:
1c60b727 20490+ kfree(a);
4f0767ce 20491+out:
4a4d8108
AM
20492+ return err;
20493+}
7f207e10
AM
20494diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20495--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
be118d29 20496+++ linux/fs/aufs/i_op.c 2018-04-06 07:48:44.204604724 +0200
8b6a4947 20497@@ -0,0 +1,1459 @@
4a4d8108 20498+/*
a2654f78 20499+ * Copyright (C) 2005-2017 Junjiro R. Okajima
4a4d8108
AM
20500+ *
20501+ * This program, aufs is free software; you can redistribute it and/or modify
20502+ * it under the terms of the GNU General Public License as published by
20503+ * the Free Software Foundation; either version 2 of the License, or
20504+ * (at your option) any later version.
20505+ *
20506+ * This program is distributed in the hope that it will be useful,
20507+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20508+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20509+ * GNU General Public License for more details.
20510+ *
20511+ * You should have received a copy of the GNU General Public License
523b37e3 20512+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20513+ */
1facf9fc 20514+
1308ab2a 20515+/*
4a4d8108 20516+ * inode operations (except add/del/rename)
1308ab2a 20517+ */
4a4d8108
AM
20518+
20519+#include <linux/device_cgroup.h>
20520+#include <linux/fs_stack.h>
4a4d8108
AM
20521+#include <linux/namei.h>
20522+#include <linux/security.h>
4a4d8108
AM
20523+#include "aufs.h"
20524+
1e00d052 20525+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20526+ struct path *h_path, int brperm)
1facf9fc 20527+{
1308ab2a 20528+ int err;
4a4d8108 20529+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 20530+
e2f27e51
AM
20531+ err = -EPERM;
20532+ if (write_mask && IS_IMMUTABLE(h_inode))
20533+ goto out;
20534+
4a4d8108 20535+ err = -EACCES;
e2f27e51
AM
20536+ if (((mask & MAY_EXEC)
20537+ && S_ISREG(h_inode->i_mode)
20538+ && (path_noexec(h_path)
20539+ || !(h_inode->i_mode & S_IXUGO))))
4a4d8108
AM
20540+ goto out;
20541+
20542+ /*
20543+ * - skip the lower fs test in the case of write to ro branch.
20544+ * - nfs dir permission write check is optimized, but a policy for
20545+ * link/rename requires a real check.
2121bcd9 20546+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 20547+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
20548+ */
20549+ if ((write_mask && !au_br_writable(brperm))
20550+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20551+ && write_mask && !(mask & MAY_READ))
20552+ || !h_inode->i_op->permission) {
20553+ /* AuLabel(generic_permission); */
b912730e 20554+ /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
1e00d052 20555+ err = generic_permission(h_inode, mask);
b912730e
AM
20556+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20557+ err = h_inode->i_op->permission(h_inode, mask);
20558+ AuTraceErr(err);
1308ab2a 20559+ } else {
4a4d8108 20560+ /* AuLabel(h_inode->permission); */
1e00d052 20561+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
20562+ AuTraceErr(err);
20563+ }
1facf9fc 20564+
4a4d8108
AM
20565+ if (!err)
20566+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20567+ if (!err)
4a4d8108 20568+ err = security_inode_permission(h_inode, mask);
4a4d8108
AM
20569+
20570+#if 0
20571+ if (!err) {
20572+ /* todo: do we need to call ima_path_check()? */
20573+ struct path h_path = {
20574+ .dentry =
20575+ .mnt = h_mnt
20576+ };
20577+ err = ima_path_check(&h_path,
20578+ mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20579+ IMA_COUNT_LEAVE);
1308ab2a 20580+ }
4a4d8108 20581+#endif
dece6358 20582+
4f0767ce 20583+out:
1308ab2a 20584+ return err;
20585+}
dece6358 20586+
1e00d052 20587+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 20588+{
20589+ int err;
5afbbe0d 20590+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20591+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20592+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20593+ struct inode *h_inode;
20594+ struct super_block *sb;
20595+ struct au_branch *br;
1facf9fc 20596+
027c5e7a 20597+ /* todo: support rcu-walk? */
1e00d052 20598+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20599+ return -ECHILD;
20600+
4a4d8108
AM
20601+ sb = inode->i_sb;
20602+ si_read_lock(sb, AuLock_FLUSH);
20603+ ii_read_lock_child(inode);
027c5e7a
AM
20604+#if 0
20605+ err = au_iigen_test(inode, au_sigen(sb));
20606+ if (unlikely(err))
20607+ goto out;
20608+#endif
dece6358 20609+
076b876e
AM
20610+ if (!isdir
20611+ || write_mask
20612+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20613+ err = au_busy_or_stale();
5afbbe0d 20614+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20615+ if (unlikely(!h_inode
20616+ || (h_inode->i_mode & S_IFMT)
20617+ != (inode->i_mode & S_IFMT)))
20618+ goto out;
1facf9fc 20619+
4a4d8108 20620+ err = 0;
5afbbe0d 20621+ bindex = au_ibtop(inode);
4a4d8108 20622+ br = au_sbr(sb, bindex);
79b8bda9 20623+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20624+ if (write_mask
20625+ && !err
20626+ && !special_file(h_inode->i_mode)) {
20627+ /* test whether the upper writable branch exists */
20628+ err = -EROFS;
20629+ for (; bindex >= 0; bindex--)
20630+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20631+ err = 0;
20632+ break;
20633+ }
20634+ }
20635+ goto out;
20636+ }
dece6358 20637+
4a4d8108 20638+ /* non-write to dir */
1308ab2a 20639+ err = 0;
5afbbe0d
AM
20640+ bbot = au_ibbot(inode);
20641+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20642+ h_inode = au_h_iptr(inode, bindex);
20643+ if (h_inode) {
20644+ err = au_busy_or_stale();
20645+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20646+ break;
20647+
20648+ br = au_sbr(sb, bindex);
79b8bda9 20649+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20650+ br->br_perm);
20651+ }
20652+ }
1308ab2a 20653+
4f0767ce 20654+out:
4a4d8108
AM
20655+ ii_read_unlock(inode);
20656+ si_read_unlock(sb);
1308ab2a 20657+ return err;
20658+}
20659+
4a4d8108 20660+/* ---------------------------------------------------------------------- */
1facf9fc 20661+
4a4d8108 20662+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20663+ unsigned int flags)
4a4d8108
AM
20664+{
20665+ struct dentry *ret, *parent;
b752ccd1 20666+ struct inode *inode;
4a4d8108 20667+ struct super_block *sb;
1716fcea 20668+ int err, npositive;
dece6358 20669+
4a4d8108 20670+ IMustLock(dir);
1308ab2a 20671+
537831f9
AM
20672+ /* todo: support rcu-walk? */
20673+ ret = ERR_PTR(-ECHILD);
20674+ if (flags & LOOKUP_RCU)
20675+ goto out;
20676+
20677+ ret = ERR_PTR(-ENAMETOOLONG);
20678+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20679+ goto out;
20680+
4a4d8108 20681+ sb = dir->i_sb;
7f207e10
AM
20682+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20683+ ret = ERR_PTR(err);
20684+ if (unlikely(err))
20685+ goto out;
20686+
4a4d8108
AM
20687+ err = au_di_init(dentry);
20688+ ret = ERR_PTR(err);
20689+ if (unlikely(err))
7f207e10 20690+ goto out_si;
1308ab2a 20691+
9dbd164d 20692+ inode = NULL;
027c5e7a 20693+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20694+ parent = dentry->d_parent; /* dir inode is locked */
20695+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20696+ err = au_alive_dir(parent);
20697+ if (!err)
20698+ err = au_digen_test(parent, au_sigen(sb));
20699+ if (!err) {
5afbbe0d
AM
20700+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20701+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20702+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20703+ err = npositive;
20704+ }
4a4d8108 20705+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20706+ ret = ERR_PTR(err);
20707+ if (unlikely(err < 0))
20708+ goto out_unlock;
1308ab2a 20709+
4a4d8108 20710+ if (npositive) {
b752ccd1 20711+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20712+ if (IS_ERR(inode)) {
20713+ ret = (void *)inode;
20714+ inode = NULL;
20715+ goto out_unlock;
20716+ }
9dbd164d 20717+ }
4a4d8108 20718+
c1595e42
JR
20719+ if (inode)
20720+ atomic_inc(&inode->i_count);
4a4d8108 20721+ ret = d_splice_alias(inode, dentry);
537831f9
AM
20722+#if 0
20723+ if (unlikely(d_need_lookup(dentry))) {
20724+ spin_lock(&dentry->d_lock);
20725+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20726+ spin_unlock(&dentry->d_lock);
20727+ } else
20728+#endif
c1595e42 20729+ if (inode) {
2000de60 20730+ if (!IS_ERR(ret)) {
c1595e42 20731+ iput(inode);
2000de60
JR
20732+ if (ret && ret != dentry)
20733+ ii_write_unlock(inode);
20734+ } else {
c1595e42
JR
20735+ ii_write_unlock(inode);
20736+ iput(inode);
20737+ inode = NULL;
20738+ }
7f207e10 20739+ }
1facf9fc 20740+
4f0767ce 20741+out_unlock:
4a4d8108 20742+ di_write_unlock(dentry);
7f207e10 20743+out_si:
4a4d8108 20744+ si_read_unlock(sb);
7f207e10 20745+out:
4a4d8108
AM
20746+ return ret;
20747+}
1facf9fc 20748+
4a4d8108 20749+/* ---------------------------------------------------------------------- */
1facf9fc 20750+
b912730e 20751+struct aopen_node {
8b6a4947 20752+ struct hlist_bl_node hblist;
b912730e
AM
20753+ struct file *file, *h_file;
20754+};
20755+
20756+static int au_do_aopen(struct inode *inode, struct file *file)
20757+{
8b6a4947
AM
20758+ struct hlist_bl_head *aopen;
20759+ struct hlist_bl_node *pos;
b912730e
AM
20760+ struct aopen_node *node;
20761+ struct au_do_open_args args = {
8b6a4947
AM
20762+ .aopen = 1,
20763+ .open = au_do_open_nondir
b912730e
AM
20764+ };
20765+
20766+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20767+ hlist_bl_lock(aopen);
20768+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20769+ if (node->file == file) {
20770+ args.h_file = node->h_file;
20771+ break;
20772+ }
8b6a4947 20773+ hlist_bl_unlock(aopen);
b912730e
AM
20774+ /* AuDebugOn(!args.h_file); */
20775+
20776+ return au_do_open(file, &args);
20777+}
20778+
20779+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20780+ struct file *file, unsigned int open_flag,
20781+ umode_t create_mode, int *opened)
20782+{
8b6a4947 20783+ int err, unlocked, h_opened = *opened;
5afbbe0d 20784+ unsigned int lkup_flags;
f0c0a007 20785+ struct dentry *parent, *d;
8b6a4947 20786+ struct hlist_bl_head *aopen;
b912730e
AM
20787+ struct vfsub_aopen_args args = {
20788+ .open_flag = open_flag,
20789+ .create_mode = create_mode,
20790+ .opened = &h_opened
20791+ };
20792+ struct aopen_node aopen_node = {
20793+ .file = file
20794+ };
20795+
20796+ IMustLock(dir);
5afbbe0d 20797+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
20798+ AuDbgDentry(dentry);
20799+
20800+ err = 0;
20801+ if (!au_di(dentry)) {
5afbbe0d
AM
20802+ lkup_flags = LOOKUP_OPEN;
20803+ if (open_flag & O_CREAT)
20804+ lkup_flags |= LOOKUP_CREATE;
20805+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
20806+ if (IS_ERR(d)) {
20807+ err = PTR_ERR(d);
5afbbe0d 20808+ AuTraceErr(err);
b912730e
AM
20809+ goto out;
20810+ } else if (d) {
20811+ /*
20812+ * obsoleted dentry found.
20813+ * another error will be returned later.
20814+ */
20815+ d_drop(d);
b912730e 20816+ AuDbgDentry(d);
5afbbe0d 20817+ dput(d);
b912730e
AM
20818+ }
20819+ AuDbgDentry(dentry);
20820+ }
20821+
20822+ if (d_is_positive(dentry)
20823+ || d_unhashed(dentry)
20824+ || d_unlinked(dentry)
20825+ || !(open_flag & O_CREAT))
20826+ goto out_no_open;
20827+
8b6a4947 20828+ unlocked = 0;
b912730e
AM
20829+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20830+ if (unlikely(err))
20831+ goto out;
20832+
20833+ parent = dentry->d_parent; /* dir is locked */
20834+ di_write_lock_parent(parent);
5afbbe0d 20835+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
b912730e
AM
20836+ if (unlikely(err))
20837+ goto out_unlock;
20838+
20839+ AuDbgDentry(dentry);
20840+ if (d_is_positive(dentry))
20841+ goto out_unlock;
20842+
20843+ args.file = get_empty_filp();
20844+ err = PTR_ERR(args.file);
20845+ if (IS_ERR(args.file))
20846+ goto out_unlock;
20847+
20848+ args.file->f_flags = file->f_flags;
20849+ err = au_aopen_or_create(dir, dentry, &args);
20850+ AuTraceErr(err);
20851+ AuDbgFile(args.file);
20852+ if (unlikely(err < 0)) {
20853+ if (h_opened & FILE_OPENED)
20854+ fput(args.file);
20855+ else
20856+ put_filp(args.file);
20857+ goto out_unlock;
20858+ }
8b6a4947
AM
20859+ di_write_unlock(parent);
20860+ di_write_unlock(dentry);
20861+ unlocked = 1;
b912730e
AM
20862+
20863+ /* some filesystems don't set FILE_CREATED while succeeded? */
20864+ *opened |= FILE_CREATED;
20865+ if (h_opened & FILE_OPENED)
20866+ aopen_node.h_file = args.file;
20867+ else {
20868+ put_filp(args.file);
20869+ args.file = NULL;
20870+ }
20871+ aopen = &au_sbi(dir->i_sb)->si_aopen;
8b6a4947 20872+ au_hbl_add(&aopen_node.hblist, aopen);
b912730e 20873+ err = finish_open(file, dentry, au_do_aopen, opened);
8b6a4947 20874+ au_hbl_del(&aopen_node.hblist, aopen);
b912730e
AM
20875+ AuTraceErr(err);
20876+ AuDbgFile(file);
20877+ if (aopen_node.h_file)
20878+ fput(aopen_node.h_file);
20879+
20880+out_unlock:
8b6a4947
AM
20881+ if (unlocked)
20882+ si_read_unlock(dentry->d_sb);
20883+ else {
20884+ di_write_unlock(parent);
20885+ aufs_read_unlock(dentry, AuLock_DW);
20886+ }
b912730e 20887+ AuDbgDentry(dentry);
f0c0a007 20888+ if (unlikely(err < 0))
b912730e
AM
20889+ goto out;
20890+out_no_open:
f0c0a007 20891+ if (err >= 0 && !(*opened & FILE_CREATED)) {
b912730e
AM
20892+ AuLabel(out_no_open);
20893+ dget(dentry);
20894+ err = finish_no_open(file, dentry);
20895+ }
20896+out:
20897+ AuDbg("%pd%s%s\n", dentry,
20898+ (*opened & FILE_CREATED) ? " created" : "",
20899+ (*opened & FILE_OPENED) ? " opened" : "");
20900+ AuTraceErr(err);
20901+ return err;
20902+}
20903+
20904+
20905+/* ---------------------------------------------------------------------- */
20906+
4a4d8108
AM
20907+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20908+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 20909+ aufs_bindex_t btop)
4a4d8108
AM
20910+{
20911+ int err;
20912+ struct dentry *h_parent;
20913+ struct inode *h_dir;
1facf9fc 20914+
027c5e7a 20915+ if (add_entry)
5527c038 20916+ IMustLock(d_inode(parent));
027c5e7a 20917+ else
4a4d8108
AM
20918+ di_write_lock_parent(parent);
20919+
20920+ err = 0;
20921+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 20922+ if (btop > bcpup)
c2b27bf2 20923+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 20924+ else if (btop < bcpup)
4a4d8108
AM
20925+ err = au_cpdown_dirs(dentry, bcpup);
20926+ else
c2b27bf2 20927+ BUG();
4a4d8108 20928+ }
38d290e6 20929+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 20930+ h_parent = au_h_dptr(parent, bcpup);
5527c038 20931+ h_dir = d_inode(h_parent);
be118d29 20932+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 20933+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 20934+ /* todo: no unlock here */
3c1bdaff 20935+ inode_unlock_shared(h_dir);
027c5e7a
AM
20936+
20937+ AuDbg("bcpup %d\n", bcpup);
20938+ if (!err) {
5527c038 20939+ if (d_really_is_negative(dentry))
5afbbe0d 20940+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
20941+ au_update_dbrange(dentry, /*do_put_zero*/0);
20942+ }
1308ab2a 20943+ }
1facf9fc 20944+
4a4d8108
AM
20945+ if (!add_entry)
20946+ di_write_unlock(parent);
20947+ if (!err)
20948+ err = bcpup; /* success */
1308ab2a 20949+
027c5e7a 20950+ AuTraceErr(err);
4a4d8108
AM
20951+ return err;
20952+}
1facf9fc 20953+
4a4d8108
AM
20954+/*
20955+ * decide the branch and the parent dir where we will create a new entry.
20956+ * returns new bindex or an error.
20957+ * copyup the parent dir if needed.
20958+ */
20959+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20960+ struct au_wr_dir_args *args)
20961+{
20962+ int err;
392086de 20963+ unsigned int flags;
5afbbe0d 20964+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
20965+ const unsigned char add_entry
20966+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 20967+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
20968+ struct super_block *sb;
20969+ struct dentry *parent;
20970+ struct au_sbinfo *sbinfo;
1facf9fc 20971+
4a4d8108
AM
20972+ sb = dentry->d_sb;
20973+ sbinfo = au_sbi(sb);
20974+ parent = dget_parent(dentry);
5afbbe0d
AM
20975+ btop = au_dbtop(dentry);
20976+ bcpup = btop;
4a4d8108
AM
20977+ if (args->force_btgt < 0) {
20978+ if (src_dentry) {
5afbbe0d
AM
20979+ src_btop = au_dbtop(src_dentry);
20980+ if (src_btop < btop)
20981+ bcpup = src_btop;
4a4d8108 20982+ } else if (add_entry) {
392086de
AM
20983+ flags = 0;
20984+ if (au_ftest_wrdir(args->flags, ISDIR))
20985+ au_fset_wbr(flags, DIR);
20986+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
20987+ bcpup = err;
20988+ }
1facf9fc 20989+
5527c038 20990+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
20991+ if (add_entry)
20992+ err = AuWbrCopyup(sbinfo, dentry);
20993+ else {
20994+ if (!IS_ROOT(dentry)) {
20995+ di_read_lock_parent(parent, !AuLock_IR);
20996+ err = AuWbrCopyup(sbinfo, dentry);
20997+ di_read_unlock(parent, !AuLock_IR);
20998+ } else
20999+ err = AuWbrCopyup(sbinfo, dentry);
21000+ }
21001+ bcpup = err;
21002+ if (unlikely(err < 0))
21003+ goto out;
21004+ }
21005+ } else {
21006+ bcpup = args->force_btgt;
5527c038 21007+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21008+ }
027c5e7a 21009+
5afbbe0d 21010+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21011+ err = bcpup;
5afbbe0d 21012+ if (bcpup == btop)
4a4d8108 21013+ goto out; /* success */
4a4d8108
AM
21014+
21015+ /* copyup the new parent into the branch we process */
5afbbe0d 21016+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21017+ if (err >= 0) {
5527c038 21018+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21019+ au_set_h_dptr(dentry, btop, NULL);
21020+ au_set_dbtop(dentry, bcpup);
21021+ au_set_dbbot(dentry, bcpup);
027c5e7a 21022+ }
38d290e6
JR
21023+ AuDebugOn(add_entry
21024+ && !au_ftest_wrdir(args->flags, TMPFILE)
21025+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21026+ }
86dc4139
AM
21027+
21028+out:
21029+ dput(parent);
21030+ return err;
21031+}
21032+
21033+/* ---------------------------------------------------------------------- */
21034+
21035+void au_pin_hdir_unlock(struct au_pin *p)
21036+{
21037+ if (p->hdir)
5afbbe0d 21038+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21039+}
21040+
c1595e42 21041+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21042+{
21043+ int err;
21044+
21045+ err = 0;
21046+ if (!p->hdir)
21047+ goto out;
21048+
21049+ /* even if an error happens later, keep this lock */
5afbbe0d 21050+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21051+
21052+ err = -EBUSY;
5527c038 21053+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21054+ goto out;
21055+
21056+ err = 0;
21057+ if (p->h_dentry)
21058+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21059+ p->h_parent, p->br);
21060+
21061+out:
21062+ return err;
21063+}
21064+
21065+int au_pin_hdir_relock(struct au_pin *p)
21066+{
21067+ int err, i;
21068+ struct inode *h_i;
21069+ struct dentry *h_d[] = {
21070+ p->h_dentry,
21071+ p->h_parent
21072+ };
21073+
21074+ err = au_pin_hdir_lock(p);
21075+ if (unlikely(err))
21076+ goto out;
21077+
21078+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21079+ if (!h_d[i])
21080+ continue;
5527c038
JR
21081+ if (d_is_positive(h_d[i])) {
21082+ h_i = d_inode(h_d[i]);
86dc4139 21083+ err = !h_i->i_nlink;
5527c038 21084+ }
86dc4139
AM
21085+ }
21086+
21087+out:
21088+ return err;
21089+}
21090+
5afbbe0d 21091+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21092+{
5afbbe0d
AM
21093+#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
21094+ p->hdir->hi_inode->i_rwsem.owner = task;
86dc4139
AM
21095+#endif
21096+}
21097+
21098+void au_pin_hdir_acquire_nest(struct au_pin *p)
21099+{
21100+ if (p->hdir) {
5afbbe0d 21101+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21102+ p->lsc_hi, 0, NULL, _RET_IP_);
21103+ au_pin_hdir_set_owner(p, current);
21104+ }
dece6358 21105+}
1facf9fc 21106+
86dc4139
AM
21107+void au_pin_hdir_release(struct au_pin *p)
21108+{
21109+ if (p->hdir) {
21110+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 21111+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
21112+ }
21113+}
1308ab2a 21114+
4a4d8108 21115+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21116+{
4a4d8108
AM
21117+ if (pin && pin->parent)
21118+ return au_h_dptr(pin->parent, pin->bindex);
21119+ return NULL;
dece6358 21120+}
1facf9fc 21121+
4a4d8108 21122+void au_unpin(struct au_pin *p)
dece6358 21123+{
86dc4139
AM
21124+ if (p->hdir)
21125+ au_pin_hdir_unlock(p);
e49829fe 21126+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21127+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21128+ if (!p->hdir)
21129+ return;
1facf9fc 21130+
4a4d8108
AM
21131+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21132+ di_read_unlock(p->parent, AuLock_IR);
21133+ iput(p->hdir->hi_inode);
21134+ dput(p->parent);
21135+ p->parent = NULL;
21136+ p->hdir = NULL;
21137+ p->h_mnt = NULL;
86dc4139 21138+ /* do not clear p->task */
4a4d8108 21139+}
1308ab2a 21140+
4a4d8108
AM
21141+int au_do_pin(struct au_pin *p)
21142+{
21143+ int err;
21144+ struct super_block *sb;
4a4d8108
AM
21145+ struct inode *h_dir;
21146+
21147+ err = 0;
21148+ sb = p->dentry->d_sb;
86dc4139 21149+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21150+ if (IS_ROOT(p->dentry)) {
21151+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21152+ p->h_mnt = au_br_mnt(p->br);
b4510431 21153+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21154+ if (unlikely(err)) {
21155+ au_fclr_pin(p->flags, MNT_WRITE);
21156+ goto out_err;
21157+ }
21158+ }
dece6358 21159+ goto out;
1facf9fc 21160+ }
21161+
86dc4139 21162+ p->h_dentry = NULL;
5afbbe0d 21163+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21164+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21165+
4a4d8108
AM
21166+ p->parent = dget_parent(p->dentry);
21167+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21168+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21169+
4a4d8108 21170+ h_dir = NULL;
86dc4139 21171+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21172+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21173+ if (p->hdir)
21174+ h_dir = p->hdir->hi_inode;
dece6358 21175+
b752ccd1
AM
21176+ /*
21177+ * udba case, or
21178+ * if DI_LOCKED is not set, then p->parent may be different
21179+ * and h_parent can be NULL.
21180+ */
86dc4139 21181+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21182+ err = -EBUSY;
4a4d8108
AM
21183+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21184+ di_read_unlock(p->parent, AuLock_IR);
21185+ dput(p->parent);
21186+ p->parent = NULL;
21187+ goto out_err;
21188+ }
1308ab2a 21189+
4a4d8108 21190+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21191+ p->h_mnt = au_br_mnt(p->br);
b4510431 21192+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21193+ if (unlikely(err)) {
4a4d8108 21194+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
21195+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21196+ di_read_unlock(p->parent, AuLock_IR);
21197+ dput(p->parent);
21198+ p->parent = NULL;
21199+ goto out_err;
dece6358
AM
21200+ }
21201+ }
4a4d8108 21202+
86dc4139
AM
21203+ au_igrab(h_dir);
21204+ err = au_pin_hdir_lock(p);
21205+ if (!err)
21206+ goto out; /* success */
21207+
076b876e
AM
21208+ au_unpin(p);
21209+
4f0767ce 21210+out_err:
4a4d8108
AM
21211+ pr_err("err %d\n", err);
21212+ err = au_busy_or_stale();
4f0767ce 21213+out:
1facf9fc 21214+ return err;
21215+}
21216+
4a4d8108
AM
21217+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21218+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21219+ unsigned int udba, unsigned char flags)
21220+{
21221+ p->dentry = dentry;
21222+ p->udba = udba;
21223+ p->lsc_di = lsc_di;
21224+ p->lsc_hi = lsc_hi;
21225+ p->flags = flags;
21226+ p->bindex = bindex;
21227+
21228+ p->parent = NULL;
21229+ p->hdir = NULL;
21230+ p->h_mnt = NULL;
86dc4139
AM
21231+
21232+ p->h_dentry = NULL;
21233+ p->h_parent = NULL;
21234+ p->br = NULL;
21235+ p->task = current;
4a4d8108
AM
21236+}
21237+
21238+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21239+ unsigned int udba, unsigned char flags)
21240+{
21241+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21242+ udba, flags);
21243+ return au_do_pin(pin);
21244+}
21245+
dece6358
AM
21246+/* ---------------------------------------------------------------------- */
21247+
1308ab2a 21248+/*
4a4d8108
AM
21249+ * ->setattr() and ->getattr() are called in various cases.
21250+ * chmod, stat: dentry is revalidated.
21251+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21252+ * unhashed.
21253+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21254+ */
027c5e7a 21255+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21256+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21257+{
4a4d8108 21258+ int err;
4a4d8108 21259+ struct dentry *parent;
1facf9fc 21260+
1308ab2a 21261+ err = 0;
027c5e7a 21262+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21263+ parent = dget_parent(dentry);
21264+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21265+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21266+ di_read_unlock(parent, AuLock_IR);
21267+ dput(parent);
dece6358 21268+ }
1facf9fc 21269+
4a4d8108 21270+ AuTraceErr(err);
1308ab2a 21271+ return err;
21272+}
dece6358 21273+
c1595e42
JR
21274+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21275+ struct au_icpup_args *a)
1308ab2a 21276+{
21277+ int err;
4a4d8108 21278+ loff_t sz;
5afbbe0d 21279+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21280+ struct dentry *hi_wh, *parent;
21281+ struct inode *inode;
4a4d8108
AM
21282+ struct au_wr_dir_args wr_dir_args = {
21283+ .force_btgt = -1,
21284+ .flags = 0
21285+ };
21286+
2000de60 21287+ if (d_is_dir(dentry))
4a4d8108
AM
21288+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21289+ /* plink or hi_wh() case */
5afbbe0d 21290+ btop = au_dbtop(dentry);
5527c038 21291+ inode = d_inode(dentry);
5afbbe0d
AM
21292+ ibtop = au_ibtop(inode);
21293+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21294+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21295+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21296+ if (unlikely(err < 0))
21297+ goto out;
21298+ a->btgt = err;
5afbbe0d 21299+ if (err != btop)
4a4d8108
AM
21300+ au_fset_icpup(a->flags, DID_CPUP);
21301+
21302+ err = 0;
21303+ a->pin_flags = AuPin_MNT_WRITE;
21304+ parent = NULL;
21305+ if (!IS_ROOT(dentry)) {
21306+ au_fset_pin(a->pin_flags, DI_LOCKED);
21307+ parent = dget_parent(dentry);
21308+ di_write_lock_parent(parent);
21309+ }
21310+
21311+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21312+ if (unlikely(err))
21313+ goto out_parent;
21314+
4a4d8108 21315+ sz = -1;
5afbbe0d 21316+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21317+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21318+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 21319+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21320+ if (ia->ia_size < i_size_read(a->h_inode))
21321+ sz = ia->ia_size;
3c1bdaff 21322+ inode_unlock_shared(a->h_inode);
c1595e42 21323+ }
4a4d8108 21324+
4a4d8108 21325+ hi_wh = NULL;
027c5e7a 21326+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21327+ hi_wh = au_hi_wh(inode, a->btgt);
21328+ if (!hi_wh) {
c2b27bf2
AM
21329+ struct au_cp_generic cpg = {
21330+ .dentry = dentry,
21331+ .bdst = a->btgt,
21332+ .bsrc = -1,
21333+ .len = sz,
21334+ .pin = &a->pin
21335+ };
21336+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21337+ if (unlikely(err))
21338+ goto out_unlock;
21339+ hi_wh = au_hi_wh(inode, a->btgt);
21340+ /* todo: revalidate hi_wh? */
21341+ }
21342+ }
21343+
21344+ if (parent) {
21345+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21346+ di_downgrade_lock(parent, AuLock_IR);
21347+ dput(parent);
21348+ parent = NULL;
21349+ }
21350+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21351+ goto out; /* success */
21352+
21353+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21354+ struct au_cp_generic cpg = {
21355+ .dentry = dentry,
21356+ .bdst = a->btgt,
5afbbe0d 21357+ .bsrc = btop,
c2b27bf2
AM
21358+ .len = sz,
21359+ .pin = &a->pin,
21360+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21361+ };
21362+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21363+ if (!err)
21364+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21365+ } else if (!hi_wh)
21366+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21367+ else
21368+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21369+
4f0767ce 21370+out_unlock:
5527c038 21371+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21372+ if (!err)
dece6358 21373+ goto out; /* success */
4a4d8108 21374+ au_unpin(&a->pin);
4f0767ce 21375+out_parent:
4a4d8108
AM
21376+ if (parent) {
21377+ di_write_unlock(parent);
21378+ dput(parent);
21379+ }
4f0767ce 21380+out:
86dc4139 21381+ if (!err)
febd17d6 21382+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21383+ return err;
21384+}
21385+
4a4d8108 21386+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 21387+{
4a4d8108 21388+ int err;
523b37e3 21389+ struct inode *inode, *delegated;
4a4d8108
AM
21390+ struct super_block *sb;
21391+ struct file *file;
21392+ struct au_icpup_args *a;
1facf9fc 21393+
5527c038 21394+ inode = d_inode(dentry);
4a4d8108 21395+ IMustLock(inode);
dece6358 21396+
f2c43d5f
AM
21397+ err = setattr_prepare(dentry, ia);
21398+ if (unlikely(err))
21399+ goto out;
21400+
4a4d8108
AM
21401+ err = -ENOMEM;
21402+ a = kzalloc(sizeof(*a), GFP_NOFS);
21403+ if (unlikely(!a))
21404+ goto out;
1facf9fc 21405+
4a4d8108
AM
21406+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21407+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21408+
4a4d8108
AM
21409+ file = NULL;
21410+ sb = dentry->d_sb;
e49829fe
JR
21411+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21412+ if (unlikely(err))
21413+ goto out_kfree;
21414+
4a4d8108
AM
21415+ if (ia->ia_valid & ATTR_FILE) {
21416+ /* currently ftruncate(2) only */
7e9cd9fe 21417+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21418+ file = ia->ia_file;
521ced18
JR
21419+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21420+ /*fi_lsc*/0);
4a4d8108
AM
21421+ if (unlikely(err))
21422+ goto out_si;
21423+ ia->ia_file = au_hf_top(file);
21424+ a->udba = AuOpt_UDBA_NONE;
21425+ } else {
21426+ /* fchmod() doesn't pass ia_file */
21427+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21428+ di_write_lock_child(dentry);
21429+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21430+ if (d_unhashed(dentry))
21431+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21432+ if (a->udba != AuOpt_UDBA_NONE) {
21433+ AuDebugOn(IS_ROOT(dentry));
21434+ err = au_reval_for_attr(dentry, au_sigen(sb));
21435+ if (unlikely(err))
21436+ goto out_dentry;
21437+ }
dece6358 21438+ }
dece6358 21439+
4a4d8108
AM
21440+ err = au_pin_and_icpup(dentry, ia, a);
21441+ if (unlikely(err < 0))
21442+ goto out_dentry;
21443+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21444+ ia->ia_file = NULL;
21445+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21446+ }
dece6358 21447+
4a4d8108
AM
21448+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21449+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21450+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21451+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21452+ if (unlikely(err))
21453+ goto out_unlock;
21454+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21455+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21456+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21457+ if (unlikely(err))
21458+ goto out_unlock;
21459+ }
dece6358 21460+
4a4d8108
AM
21461+ if (ia->ia_valid & ATTR_SIZE) {
21462+ struct file *f;
1308ab2a 21463+
953406b4 21464+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21465+ /* unmap only */
953406b4 21466+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21467+
4a4d8108
AM
21468+ f = NULL;
21469+ if (ia->ia_valid & ATTR_FILE)
21470+ f = ia->ia_file;
febd17d6 21471+ inode_unlock(a->h_inode);
4a4d8108 21472+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21473+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21474+ } else {
21475+ delegated = NULL;
21476+ while (1) {
21477+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21478+ if (delegated) {
21479+ err = break_deleg_wait(&delegated);
21480+ if (!err)
21481+ continue;
21482+ }
21483+ break;
21484+ }
21485+ }
8cdd5066
JR
21486+ /*
21487+ * regardless aufs 'acl' option setting.
21488+ * why don't all acl-aware fs call this func from their ->setattr()?
21489+ */
21490+ if (!err && (ia->ia_valid & ATTR_MODE))
21491+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
21492+ if (!err)
21493+ au_cpup_attr_changeable(inode);
1308ab2a 21494+
4f0767ce 21495+out_unlock:
febd17d6 21496+ inode_unlock(a->h_inode);
4a4d8108 21497+ au_unpin(&a->pin);
027c5e7a 21498+ if (unlikely(err))
5afbbe0d 21499+ au_update_dbtop(dentry);
4f0767ce 21500+out_dentry:
4a4d8108
AM
21501+ di_write_unlock(dentry);
21502+ if (file) {
21503+ fi_write_unlock(file);
21504+ ia->ia_file = file;
21505+ ia->ia_valid |= ATTR_FILE;
21506+ }
4f0767ce 21507+out_si:
4a4d8108 21508+ si_read_unlock(sb);
e49829fe 21509+out_kfree:
1c60b727 21510+ kfree(a);
4f0767ce 21511+out:
4a4d8108
AM
21512+ AuTraceErr(err);
21513+ return err;
1facf9fc 21514+}
21515+
c1595e42
JR
21516+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21517+static int au_h_path_to_set_attr(struct dentry *dentry,
21518+ struct au_icpup_args *a, struct path *h_path)
21519+{
21520+ int err;
21521+ struct super_block *sb;
21522+
21523+ sb = dentry->d_sb;
21524+ a->udba = au_opt_udba(sb);
21525+ /* no d_unlinked(), to set UDBA_NONE for root */
21526+ if (d_unhashed(dentry))
21527+ a->udba = AuOpt_UDBA_NONE;
21528+ if (a->udba != AuOpt_UDBA_NONE) {
21529+ AuDebugOn(IS_ROOT(dentry));
21530+ err = au_reval_for_attr(dentry, au_sigen(sb));
21531+ if (unlikely(err))
21532+ goto out;
21533+ }
21534+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21535+ if (unlikely(err < 0))
21536+ goto out;
21537+
21538+ h_path->dentry = a->h_path.dentry;
21539+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21540+
21541+out:
21542+ return err;
21543+}
21544+
f2c43d5f
AM
21545+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21546+ struct au_sxattr *arg)
c1595e42
JR
21547+{
21548+ int err;
21549+ struct path h_path;
21550+ struct super_block *sb;
21551+ struct au_icpup_args *a;
5afbbe0d 21552+ struct inode *h_inode;
c1595e42 21553+
c1595e42
JR
21554+ IMustLock(inode);
21555+
21556+ err = -ENOMEM;
21557+ a = kzalloc(sizeof(*a), GFP_NOFS);
21558+ if (unlikely(!a))
21559+ goto out;
21560+
21561+ sb = dentry->d_sb;
21562+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21563+ if (unlikely(err))
21564+ goto out_kfree;
21565+
21566+ h_path.dentry = NULL; /* silence gcc */
21567+ di_write_lock_child(dentry);
21568+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21569+ if (unlikely(err))
21570+ goto out_di;
21571+
febd17d6 21572+ inode_unlock(a->h_inode);
c1595e42
JR
21573+ switch (arg->type) {
21574+ case AU_XATTR_SET:
5afbbe0d 21575+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
21576+ err = vfsub_setxattr(h_path.dentry,
21577+ arg->u.set.name, arg->u.set.value,
21578+ arg->u.set.size, arg->u.set.flags);
21579+ break;
c1595e42
JR
21580+ case AU_ACL_SET:
21581+ err = -EOPNOTSUPP;
5527c038 21582+ h_inode = d_inode(h_path.dentry);
c1595e42 21583+ if (h_inode->i_op->set_acl)
f2c43d5f 21584+ /* this will call posix_acl_update_mode */
c1595e42
JR
21585+ err = h_inode->i_op->set_acl(h_inode,
21586+ arg->u.acl_set.acl,
21587+ arg->u.acl_set.type);
21588+ break;
21589+ }
21590+ if (!err)
21591+ au_cpup_attr_timesizes(inode);
21592+
21593+ au_unpin(&a->pin);
21594+ if (unlikely(err))
5afbbe0d 21595+ au_update_dbtop(dentry);
c1595e42
JR
21596+
21597+out_di:
21598+ di_write_unlock(dentry);
21599+ si_read_unlock(sb);
21600+out_kfree:
1c60b727 21601+ kfree(a);
c1595e42
JR
21602+out:
21603+ AuTraceErr(err);
21604+ return err;
21605+}
21606+#endif
21607+
4a4d8108
AM
21608+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21609+ unsigned int nlink)
1facf9fc 21610+{
9dbd164d
AM
21611+ unsigned int n;
21612+
4a4d8108 21613+ inode->i_mode = st->mode;
86dc4139
AM
21614+ /* don't i_[ug]id_write() here */
21615+ inode->i_uid = st->uid;
21616+ inode->i_gid = st->gid;
4a4d8108
AM
21617+ inode->i_atime = st->atime;
21618+ inode->i_mtime = st->mtime;
21619+ inode->i_ctime = st->ctime;
1facf9fc 21620+
4a4d8108
AM
21621+ au_cpup_attr_nlink(inode, /*force*/0);
21622+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21623+ n = inode->i_nlink;
21624+ n -= nlink;
21625+ n += st->nlink;
f6b6e03d 21626+ smp_mb(); /* for i_nlink */
7eafdf33 21627+ /* 0 can happen */
92d182d2 21628+ set_nlink(inode, n);
4a4d8108 21629+ }
1facf9fc 21630+
4a4d8108
AM
21631+ spin_lock(&inode->i_lock);
21632+ inode->i_blocks = st->blocks;
21633+ i_size_write(inode, st->size);
21634+ spin_unlock(&inode->i_lock);
1facf9fc 21635+}
21636+
c1595e42 21637+/*
f2c43d5f 21638+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21639+ * returns zero or negative (an error).
21640+ * @dentry will be read-locked in success.
21641+ */
a2654f78
AM
21642+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21643+ int locked)
1facf9fc 21644+{
4a4d8108 21645+ int err;
076b876e 21646+ unsigned int mnt_flags, sigen;
c1595e42 21647+ unsigned char udba_none;
4a4d8108 21648+ aufs_bindex_t bindex;
4a4d8108
AM
21649+ struct super_block *sb, *h_sb;
21650+ struct inode *inode;
1facf9fc 21651+
c1595e42
JR
21652+ h_path->mnt = NULL;
21653+ h_path->dentry = NULL;
21654+
21655+ err = 0;
4a4d8108 21656+ sb = dentry->d_sb;
4a4d8108
AM
21657+ mnt_flags = au_mntflags(sb);
21658+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21659+
a2654f78
AM
21660+ if (unlikely(locked))
21661+ goto body; /* skip locking dinfo */
21662+
4a4d8108 21663+ /* support fstat(2) */
027c5e7a 21664+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21665+ sigen = au_sigen(sb);
027c5e7a
AM
21666+ err = au_digen_test(dentry, sigen);
21667+ if (!err) {
4a4d8108 21668+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21669+ err = au_dbrange_test(dentry);
c1595e42
JR
21670+ if (unlikely(err)) {
21671+ di_read_unlock(dentry, AuLock_IR);
21672+ goto out;
21673+ }
027c5e7a 21674+ } else {
4a4d8108
AM
21675+ AuDebugOn(IS_ROOT(dentry));
21676+ di_write_lock_child(dentry);
027c5e7a
AM
21677+ err = au_dbrange_test(dentry);
21678+ if (!err)
21679+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21680+ if (!err)
21681+ di_downgrade_lock(dentry, AuLock_IR);
21682+ else {
21683+ di_write_unlock(dentry);
21684+ goto out;
21685+ }
4a4d8108
AM
21686+ }
21687+ } else
21688+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21689+
a2654f78 21690+body:
5527c038 21691+ inode = d_inode(dentry);
5afbbe0d 21692+ bindex = au_ibtop(inode);
c1595e42
JR
21693+ h_path->mnt = au_sbr_mnt(sb, bindex);
21694+ h_sb = h_path->mnt->mnt_sb;
21695+ if (!force
21696+ && !au_test_fs_bad_iattr(h_sb)
21697+ && udba_none)
21698+ goto out; /* success */
1facf9fc 21699+
5afbbe0d 21700+ if (au_dbtop(dentry) == bindex)
c1595e42 21701+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21702+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21703+ h_path->dentry = au_plink_lkup(inode, bindex);
21704+ if (IS_ERR(h_path->dentry))
21705+ /* pretending success */
21706+ h_path->dentry = NULL;
21707+ else
21708+ dput(h_path->dentry);
4a4d8108 21709+ }
c1595e42
JR
21710+
21711+out:
21712+ return err;
21713+}
21714+
521ced18
JR
21715+static int aufs_getattr(const struct path *path, struct kstat *st,
21716+ u32 request, unsigned int query)
c1595e42
JR
21717+{
21718+ int err;
21719+ unsigned char positive;
21720+ struct path h_path;
521ced18 21721+ struct dentry *dentry;
c1595e42
JR
21722+ struct inode *inode;
21723+ struct super_block *sb;
21724+
521ced18 21725+ dentry = path->dentry;
5527c038 21726+ inode = d_inode(dentry);
c1595e42
JR
21727+ sb = dentry->d_sb;
21728+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21729+ if (unlikely(err))
21730+ goto out;
a2654f78 21731+ err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
c1595e42
JR
21732+ if (unlikely(err))
21733+ goto out_si;
c06a8ce3 21734+ if (unlikely(!h_path.dentry))
c1595e42 21735+ /* illegally overlapped or something */
4a4d8108
AM
21736+ goto out_fill; /* pretending success */
21737+
5527c038 21738+ positive = d_is_positive(h_path.dentry);
4a4d8108 21739+ if (positive)
521ced18
JR
21740+ /* no vfsub version */
21741+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21742+ if (!err) {
21743+ if (positive)
c06a8ce3 21744+ au_refresh_iattr(inode, st,
5527c038 21745+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21746+ goto out_fill; /* success */
1facf9fc 21747+ }
7f207e10 21748+ AuTraceErr(err);
c1595e42 21749+ goto out_di;
4a4d8108 21750+
4f0767ce 21751+out_fill:
4a4d8108 21752+ generic_fillattr(inode, st);
c1595e42 21753+out_di:
4a4d8108 21754+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21755+out_si:
4a4d8108 21756+ si_read_unlock(sb);
7f207e10
AM
21757+out:
21758+ AuTraceErr(err);
4a4d8108 21759+ return err;
1facf9fc 21760+}
21761+
21762+/* ---------------------------------------------------------------------- */
21763+
febd17d6
JR
21764+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21765+ struct delayed_call *done)
4a4d8108 21766+{
c2c0f25c 21767+ const char *ret;
c2c0f25c 21768+ struct dentry *h_dentry;
febd17d6 21769+ struct inode *h_inode;
4a4d8108 21770+ int err;
c2c0f25c 21771+ aufs_bindex_t bindex;
1facf9fc 21772+
79b8bda9 21773+ ret = NULL; /* suppress a warning */
febd17d6
JR
21774+ err = -ECHILD;
21775+ if (!dentry)
21776+ goto out;
21777+
027c5e7a
AM
21778+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21779+ if (unlikely(err))
c2c0f25c 21780+ goto out;
027c5e7a
AM
21781+
21782+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
21783+ if (unlikely(err))
21784+ goto out_unlock;
21785+
21786+ err = -EINVAL;
21787+ inode = d_inode(dentry);
5afbbe0d 21788+ bindex = au_ibtop(inode);
c2c0f25c 21789+ h_inode = au_h_iptr(inode, bindex);
febd17d6 21790+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
21791+ goto out_unlock;
21792+
21793+ err = -EBUSY;
21794+ h_dentry = NULL;
5afbbe0d 21795+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
21796+ h_dentry = au_h_dptr(dentry, bindex);
21797+ if (h_dentry)
21798+ dget(h_dentry);
027c5e7a 21799+ }
c2c0f25c
AM
21800+ if (!h_dentry) {
21801+ h_dentry = d_find_any_alias(h_inode);
21802+ if (IS_ERR(h_dentry)) {
21803+ err = PTR_ERR(h_dentry);
febd17d6 21804+ goto out_unlock;
c2c0f25c
AM
21805+ }
21806+ }
21807+ if (unlikely(!h_dentry))
febd17d6 21808+ goto out_unlock;
1facf9fc 21809+
c2c0f25c 21810+ err = 0;
febd17d6 21811+ AuDbg("%pf\n", h_inode->i_op->get_link);
c2c0f25c 21812+ AuDbgDentry(h_dentry);
f2c43d5f 21813+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 21814+ dput(h_dentry);
febd17d6
JR
21815+ if (IS_ERR(ret))
21816+ err = PTR_ERR(ret);
c2c0f25c 21817+
c2c0f25c
AM
21818+out_unlock:
21819+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 21820+out:
c2c0f25c
AM
21821+ if (unlikely(err))
21822+ ret = ERR_PTR(err);
21823+ AuTraceErrPtr(ret);
21824+ return ret;
4a4d8108 21825+}
1facf9fc 21826+
4a4d8108 21827+/* ---------------------------------------------------------------------- */
1facf9fc 21828+
e2f27e51
AM
21829+static int au_is_special(struct inode *inode)
21830+{
21831+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21832+}
21833+
0c3ec466 21834+static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
4a4d8108 21835+{
0c3ec466 21836+ int err;
e2f27e51 21837+ aufs_bindex_t bindex;
0c3ec466
AM
21838+ struct super_block *sb;
21839+ struct inode *h_inode;
e2f27e51 21840+ struct vfsmount *h_mnt;
0c3ec466
AM
21841+
21842+ sb = inode->i_sb;
e2f27e51
AM
21843+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21844+ "unexpected s_flags 0x%lx", sb->s_flags);
21845+
0c3ec466
AM
21846+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
21847+ lockdep_off();
21848+ si_read_lock(sb, AuLock_FLUSH);
21849+ ii_write_lock_child(inode);
e2f27e51
AM
21850+
21851+ err = 0;
21852+ bindex = au_ibtop(inode);
21853+ h_inode = au_h_iptr(inode, bindex);
21854+ if (!au_test_ro(sb, bindex, inode)) {
21855+ h_mnt = au_sbr_mnt(sb, bindex);
21856+ err = vfsub_mnt_want_write(h_mnt);
21857+ if (!err) {
21858+ err = vfsub_update_time(h_inode, ts, flags);
21859+ vfsub_mnt_drop_write(h_mnt);
21860+ }
21861+ } else if (au_is_special(h_inode)) {
21862+ /*
21863+ * Never copy-up here.
21864+ * These special files may already be opened and used for
21865+ * communicating. If we copied it up, then the communication
21866+ * would be corrupted.
21867+ */
21868+ AuWarn1("timestamps for i%lu are ignored "
21869+ "since it is on readonly branch (hi%lu).\n",
21870+ inode->i_ino, h_inode->i_ino);
21871+ } else if (flags & ~S_ATIME) {
21872+ err = -EIO;
21873+ AuIOErr1("unexpected flags 0x%x\n", flags);
21874+ AuDebugOn(1);
21875+ }
21876+
38d290e6
JR
21877+ if (!err)
21878+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
21879+ ii_write_unlock(inode);
21880+ si_read_unlock(sb);
21881+ lockdep_on();
38d290e6
JR
21882+
21883+ if (!err && (flags & S_VERSION))
21884+ inode_inc_iversion(inode);
21885+
0c3ec466 21886+ return err;
4a4d8108 21887+}
1facf9fc 21888+
4a4d8108 21889+/* ---------------------------------------------------------------------- */
1308ab2a 21890+
b95c5147
AM
21891+/* no getattr version will be set by module.c:aufs_init() */
21892+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21893+ aufs_iop[] = {
21894+ [AuIop_SYMLINK] = {
21895+ .permission = aufs_permission,
c1595e42 21896+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21897+ .get_acl = aufs_get_acl,
21898+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
21899+#endif
21900+
b95c5147
AM
21901+ .setattr = aufs_setattr,
21902+ .getattr = aufs_getattr,
0c3ec466 21903+
c1595e42 21904+#ifdef CONFIG_AUFS_XATTR
b95c5147 21905+ .listxattr = aufs_listxattr,
c1595e42
JR
21906+#endif
21907+
febd17d6 21908+ .get_link = aufs_get_link,
0c3ec466 21909+
b95c5147
AM
21910+ /* .update_time = aufs_update_time */
21911+ },
21912+ [AuIop_DIR] = {
21913+ .create = aufs_create,
21914+ .lookup = aufs_lookup,
21915+ .link = aufs_link,
21916+ .unlink = aufs_unlink,
21917+ .symlink = aufs_symlink,
21918+ .mkdir = aufs_mkdir,
21919+ .rmdir = aufs_rmdir,
21920+ .mknod = aufs_mknod,
21921+ .rename = aufs_rename,
21922+
21923+ .permission = aufs_permission,
c1595e42 21924+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21925+ .get_acl = aufs_get_acl,
21926+ .set_acl = aufs_set_acl,
c1595e42
JR
21927+#endif
21928+
b95c5147
AM
21929+ .setattr = aufs_setattr,
21930+ .getattr = aufs_getattr,
0c3ec466 21931+
c1595e42 21932+#ifdef CONFIG_AUFS_XATTR
b95c5147 21933+ .listxattr = aufs_listxattr,
c1595e42
JR
21934+#endif
21935+
b95c5147
AM
21936+ .update_time = aufs_update_time,
21937+ .atomic_open = aufs_atomic_open,
21938+ .tmpfile = aufs_tmpfile
21939+ },
21940+ [AuIop_OTHER] = {
21941+ .permission = aufs_permission,
c1595e42 21942+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21943+ .get_acl = aufs_get_acl,
21944+ .set_acl = aufs_set_acl,
c1595e42
JR
21945+#endif
21946+
b95c5147
AM
21947+ .setattr = aufs_setattr,
21948+ .getattr = aufs_getattr,
0c3ec466 21949+
c1595e42 21950+#ifdef CONFIG_AUFS_XATTR
b95c5147 21951+ .listxattr = aufs_listxattr,
c1595e42
JR
21952+#endif
21953+
b95c5147
AM
21954+ .update_time = aufs_update_time
21955+ }
4a4d8108 21956+};
7f207e10
AM
21957diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
21958--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
be118d29 21959+++ linux/fs/aufs/i_op_del.c 2018-04-06 07:48:44.204604724 +0200
5afbbe0d 21960@@ -0,0 +1,511 @@
1facf9fc 21961+/*
a2654f78 21962+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 21963+ *
21964+ * This program, aufs is free software; you can redistribute it and/or modify
21965+ * it under the terms of the GNU General Public License as published by
21966+ * the Free Software Foundation; either version 2 of the License, or
21967+ * (at your option) any later version.
dece6358
AM
21968+ *
21969+ * This program is distributed in the hope that it will be useful,
21970+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21971+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21972+ * GNU General Public License for more details.
21973+ *
21974+ * You should have received a copy of the GNU General Public License
523b37e3 21975+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 21976+ */
21977+
21978+/*
4a4d8108 21979+ * inode operations (del entry)
1308ab2a 21980+ */
dece6358 21981+
1308ab2a 21982+#include "aufs.h"
dece6358 21983+
4a4d8108
AM
21984+/*
21985+ * decide if a new whiteout for @dentry is necessary or not.
21986+ * when it is necessary, prepare the parent dir for the upper branch whose
21987+ * branch index is @bcpup for creation. the actual creation of the whiteout will
21988+ * be done by caller.
21989+ * return value:
21990+ * 0: wh is unnecessary
21991+ * plus: wh is necessary
21992+ * minus: error
21993+ */
21994+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 21995+{
4a4d8108 21996+ int need_wh, err;
5afbbe0d 21997+ aufs_bindex_t btop;
4a4d8108 21998+ struct super_block *sb;
dece6358 21999+
4a4d8108 22000+ sb = dentry->d_sb;
5afbbe0d 22001+ btop = au_dbtop(dentry);
4a4d8108 22002+ if (*bcpup < 0) {
5afbbe0d
AM
22003+ *bcpup = btop;
22004+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
22005+ err = AuWbrCopyup(au_sbi(sb), dentry);
22006+ *bcpup = err;
22007+ if (unlikely(err < 0))
22008+ goto out;
22009+ }
22010+ } else
5afbbe0d 22011+ AuDebugOn(btop < *bcpup
5527c038 22012+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22013+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22014+
5afbbe0d 22015+ if (*bcpup != btop) {
4a4d8108
AM
22016+ err = au_cpup_dirs(dentry, *bcpup);
22017+ if (unlikely(err))
22018+ goto out;
22019+ need_wh = 1;
22020+ } else {
027c5e7a 22021+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22022+
027c5e7a
AM
22023+ need_wh = -ENOMEM;
22024+ dinfo = au_di(dentry);
22025+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22026+ if (tmp) {
22027+ au_di_cp(tmp, dinfo);
22028+ au_di_swap(tmp, dinfo);
22029+ /* returns the number of positive dentries */
5afbbe0d
AM
22030+ need_wh = au_lkup_dentry(dentry, btop + 1,
22031+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22032+ au_di_swap(tmp, dinfo);
22033+ au_rw_write_unlock(&tmp->di_rwsem);
22034+ au_di_free(tmp);
4a4d8108
AM
22035+ }
22036+ }
22037+ AuDbg("need_wh %d\n", need_wh);
22038+ err = need_wh;
22039+
4f0767ce 22040+out:
4a4d8108 22041+ return err;
1facf9fc 22042+}
22043+
4a4d8108
AM
22044+/*
22045+ * simple tests for the del-entry operations.
22046+ * following the checks in vfs, plus the parent-child relationship.
22047+ */
22048+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22049+ struct dentry *h_parent, int isdir)
1facf9fc 22050+{
4a4d8108
AM
22051+ int err;
22052+ umode_t h_mode;
22053+ struct dentry *h_dentry, *h_latest;
1308ab2a 22054+ struct inode *h_inode;
1facf9fc 22055+
4a4d8108 22056+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22057+ if (d_really_is_positive(dentry)) {
4a4d8108 22058+ err = -ENOENT;
5527c038
JR
22059+ if (unlikely(d_is_negative(h_dentry)))
22060+ goto out;
22061+ h_inode = d_inode(h_dentry);
22062+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22063+ goto out;
1facf9fc 22064+
4a4d8108
AM
22065+ h_mode = h_inode->i_mode;
22066+ if (!isdir) {
22067+ err = -EISDIR;
22068+ if (unlikely(S_ISDIR(h_mode)))
22069+ goto out;
22070+ } else if (unlikely(!S_ISDIR(h_mode))) {
22071+ err = -ENOTDIR;
22072+ goto out;
22073+ }
22074+ } else {
22075+ /* rename(2) case */
22076+ err = -EIO;
5527c038 22077+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22078+ goto out;
22079+ }
1facf9fc 22080+
4a4d8108
AM
22081+ err = -ENOENT;
22082+ /* expected parent dir is locked */
22083+ if (unlikely(h_parent != h_dentry->d_parent))
22084+ goto out;
22085+ err = 0;
22086+
22087+ /*
22088+ * rmdir a dir may break the consistency on some filesystem.
22089+ * let's try heavy test.
22090+ */
22091+ err = -EACCES;
076b876e 22092+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 22093+ && au_test_h_perm(d_inode(h_parent),
076b876e 22094+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22095+ goto out;
22096+
076b876e 22097+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
22098+ err = -EIO;
22099+ if (IS_ERR(h_latest))
22100+ goto out;
22101+ if (h_latest == h_dentry)
22102+ err = 0;
22103+ dput(h_latest);
22104+
4f0767ce 22105+out:
4a4d8108 22106+ return err;
1308ab2a 22107+}
1facf9fc 22108+
4a4d8108
AM
22109+/*
22110+ * decide the branch where we operate for @dentry. the branch index will be set
22111+ * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
22112+ * dir for reverting.
22113+ * when a new whiteout is necessary, create it.
22114+ */
22115+static struct dentry*
22116+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22117+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22118+{
4a4d8108
AM
22119+ struct dentry *wh_dentry;
22120+ struct super_block *sb;
22121+ struct path h_path;
22122+ int err, need_wh;
22123+ unsigned int udba;
22124+ aufs_bindex_t bcpup;
dece6358 22125+
4a4d8108
AM
22126+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22127+ wh_dentry = ERR_PTR(need_wh);
22128+ if (unlikely(need_wh < 0))
22129+ goto out;
22130+
22131+ sb = dentry->d_sb;
22132+ udba = au_opt_udba(sb);
22133+ bcpup = *rbcpup;
22134+ err = au_pin(pin, dentry, bcpup, udba,
22135+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22136+ wh_dentry = ERR_PTR(err);
22137+ if (unlikely(err))
22138+ goto out;
22139+
22140+ h_path.dentry = au_pinned_h_parent(pin);
22141+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22142+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22143+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22144+ wh_dentry = ERR_PTR(err);
22145+ if (unlikely(err))
22146+ goto out_unpin;
22147+ }
22148+
22149+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22150+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22151+ wh_dentry = NULL;
22152+ if (!need_wh)
22153+ goto out; /* success, no need to create whiteout */
22154+
22155+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22156+ if (IS_ERR(wh_dentry))
22157+ goto out_unpin;
22158+
22159+ /* returns with the parent is locked and wh_dentry is dget-ed */
22160+ goto out; /* success */
22161+
4f0767ce 22162+out_unpin:
4a4d8108 22163+ au_unpin(pin);
4f0767ce 22164+out:
4a4d8108 22165+ return wh_dentry;
1facf9fc 22166+}
22167+
4a4d8108
AM
22168+/*
22169+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22170+ * in order to be revertible and save time for removing many child whiteouts
22171+ * under the dir.
22172+ * returns 1 when there are too many child whiteout and caller should remove
22173+ * them asynchronously. returns 0 when the number of children is enough small to
22174+ * remove now or the branch fs is a remote fs.
22175+ * otherwise return an error.
22176+ */
22177+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22178+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22179+{
4a4d8108
AM
22180+ int rmdir_later, err, dirwh;
22181+ struct dentry *h_dentry;
22182+ struct super_block *sb;
5527c038 22183+ struct inode *inode;
4a4d8108
AM
22184+
22185+ sb = dentry->d_sb;
22186+ SiMustAnyLock(sb);
22187+ h_dentry = au_h_dptr(dentry, bindex);
22188+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22189+ if (unlikely(err))
22190+ goto out;
22191+
22192+ /* stop monitoring */
5527c038
JR
22193+ inode = d_inode(dentry);
22194+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22195+
22196+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22197+ dirwh = au_sbi(sb)->si_dirwh;
22198+ rmdir_later = (dirwh <= 1);
22199+ if (!rmdir_later)
22200+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22201+ dirwh);
22202+ if (rmdir_later)
22203+ return rmdir_later;
22204+ }
1facf9fc 22205+
4a4d8108
AM
22206+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22207+ if (unlikely(err)) {
523b37e3
AM
22208+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22209+ h_dentry, bindex, err);
4a4d8108
AM
22210+ err = 0;
22211+ }
dece6358 22212+
4f0767ce 22213+out:
4a4d8108
AM
22214+ AuTraceErr(err);
22215+ return err;
22216+}
1308ab2a 22217+
4a4d8108
AM
22218+/*
22219+ * final procedure for deleting a entry.
22220+ * maintain dentry and iattr.
22221+ */
22222+static void epilog(struct inode *dir, struct dentry *dentry,
22223+ aufs_bindex_t bindex)
22224+{
22225+ struct inode *inode;
1308ab2a 22226+
5527c038 22227+ inode = d_inode(dentry);
4a4d8108
AM
22228+ d_drop(dentry);
22229+ inode->i_ctime = dir->i_ctime;
1308ab2a 22230+
b912730e 22231+ au_dir_ts(dir, bindex);
be118d29 22232+ inode_inc_iversion(dir);
1facf9fc 22233+}
22234+
4a4d8108
AM
22235+/*
22236+ * when an error happened, remove the created whiteout and revert everything.
22237+ */
7f207e10
AM
22238+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22239+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22240+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22241+{
4a4d8108
AM
22242+ int rerr;
22243+ struct path h_path = {
22244+ .dentry = wh_dentry,
7f207e10 22245+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22246+ };
dece6358 22247+
7f207e10 22248+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22249+ if (!rerr) {
22250+ au_set_dbwh(dentry, bwh);
22251+ au_dtime_revert(dt);
22252+ return 0;
22253+ }
dece6358 22254+
523b37e3 22255+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22256+ return -EIO;
1facf9fc 22257+}
22258+
4a4d8108 22259+/* ---------------------------------------------------------------------- */
1facf9fc 22260+
4a4d8108 22261+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22262+{
4a4d8108 22263+ int err;
5afbbe0d 22264+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22265+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22266+ struct dentry *parent, *wh_dentry;
c2b27bf2
AM
22267+ /* to reuduce stack size */
22268+ struct {
22269+ struct au_dtime dt;
22270+ struct au_pin pin;
22271+ struct path h_path;
22272+ } *a;
1facf9fc 22273+
4a4d8108 22274+ IMustLock(dir);
027c5e7a 22275+
c2b27bf2
AM
22276+ err = -ENOMEM;
22277+ a = kmalloc(sizeof(*a), GFP_NOFS);
22278+ if (unlikely(!a))
22279+ goto out;
22280+
027c5e7a
AM
22281+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22282+ if (unlikely(err))
c2b27bf2 22283+ goto out_free;
027c5e7a
AM
22284+ err = au_d_hashed_positive(dentry);
22285+ if (unlikely(err))
22286+ goto out_unlock;
5527c038 22287+ inode = d_inode(dentry);
4a4d8108 22288+ IMustLock(inode);
027c5e7a 22289+ err = -EISDIR;
2000de60 22290+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22291+ goto out_unlock; /* possible? */
1facf9fc 22292+
5afbbe0d 22293+ btop = au_dbtop(dentry);
4a4d8108
AM
22294+ bwh = au_dbwh(dentry);
22295+ bindex = -1;
027c5e7a
AM
22296+ parent = dentry->d_parent; /* dir inode is locked */
22297+ di_write_lock_parent(parent);
c2b27bf2
AM
22298+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22299+ &a->pin);
4a4d8108
AM
22300+ err = PTR_ERR(wh_dentry);
22301+ if (IS_ERR(wh_dentry))
027c5e7a 22302+ goto out_parent;
1facf9fc 22303+
5afbbe0d
AM
22304+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22305+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22306+ dget(a->h_path.dentry);
5afbbe0d 22307+ if (bindex == btop) {
c2b27bf2 22308+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22309+ delegated = NULL;
22310+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22311+ if (unlikely(err == -EWOULDBLOCK)) {
22312+ pr_warn("cannot retry for NFSv4 delegation"
22313+ " for an internal unlink\n");
22314+ iput(delegated);
22315+ }
4a4d8108
AM
22316+ } else {
22317+ /* dir inode is locked */
5527c038 22318+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22319+ IMustLock(h_dir);
22320+ err = 0;
22321+ }
dece6358 22322+
4a4d8108 22323+ if (!err) {
7f207e10 22324+ vfsub_drop_nlink(inode);
4a4d8108
AM
22325+ epilog(dir, dentry, bindex);
22326+
22327+ /* update target timestamps */
5afbbe0d 22328+ if (bindex == btop) {
c2b27bf2
AM
22329+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22330+ /*ignore*/
5527c038 22331+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22332+ } else
22333+ /* todo: this timestamp may be reverted later */
22334+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22335+ goto out_unpin; /* success */
1facf9fc 22336+ }
22337+
4a4d8108
AM
22338+ /* revert */
22339+ if (wh_dentry) {
22340+ int rerr;
22341+
c2b27bf2
AM
22342+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22343+ &a->dt);
4a4d8108
AM
22344+ if (rerr)
22345+ err = rerr;
dece6358 22346+ }
1facf9fc 22347+
027c5e7a 22348+out_unpin:
c2b27bf2 22349+ au_unpin(&a->pin);
4a4d8108 22350+ dput(wh_dentry);
c2b27bf2 22351+ dput(a->h_path.dentry);
027c5e7a 22352+out_parent:
4a4d8108 22353+ di_write_unlock(parent);
027c5e7a 22354+out_unlock:
4a4d8108 22355+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22356+out_free:
1c60b727 22357+ kfree(a);
027c5e7a 22358+out:
4a4d8108 22359+ return err;
dece6358
AM
22360+}
22361+
4a4d8108 22362+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22363+{
4a4d8108 22364+ int err, rmdir_later;
5afbbe0d 22365+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22366+ struct inode *inode;
22367+ struct dentry *parent, *wh_dentry, *h_dentry;
22368+ struct au_whtmp_rmdir *args;
c2b27bf2
AM
22369+ /* to reuduce stack size */
22370+ struct {
22371+ struct au_dtime dt;
22372+ struct au_pin pin;
22373+ } *a;
1facf9fc 22374+
4a4d8108 22375+ IMustLock(dir);
027c5e7a 22376+
c2b27bf2
AM
22377+ err = -ENOMEM;
22378+ a = kmalloc(sizeof(*a), GFP_NOFS);
22379+ if (unlikely(!a))
22380+ goto out;
22381+
027c5e7a
AM
22382+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22383+ if (unlikely(err))
c2b27bf2 22384+ goto out_free;
53392da6
AM
22385+ err = au_alive_dir(dentry);
22386+ if (unlikely(err))
027c5e7a 22387+ goto out_unlock;
5527c038 22388+ inode = d_inode(dentry);
4a4d8108 22389+ IMustLock(inode);
027c5e7a 22390+ err = -ENOTDIR;
2000de60 22391+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22392+ goto out_unlock; /* possible? */
dece6358 22393+
4a4d8108
AM
22394+ err = -ENOMEM;
22395+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22396+ if (unlikely(!args))
22397+ goto out_unlock;
dece6358 22398+
4a4d8108
AM
22399+ parent = dentry->d_parent; /* dir inode is locked */
22400+ di_write_lock_parent(parent);
22401+ err = au_test_empty(dentry, &args->whlist);
22402+ if (unlikely(err))
027c5e7a 22403+ goto out_parent;
1facf9fc 22404+
5afbbe0d 22405+ btop = au_dbtop(dentry);
4a4d8108
AM
22406+ bwh = au_dbwh(dentry);
22407+ bindex = -1;
c2b27bf2
AM
22408+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22409+ &a->pin);
4a4d8108
AM
22410+ err = PTR_ERR(wh_dentry);
22411+ if (IS_ERR(wh_dentry))
027c5e7a 22412+ goto out_parent;
1facf9fc 22413+
5afbbe0d 22414+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22415+ dget(h_dentry);
22416+ rmdir_later = 0;
5afbbe0d
AM
22417+ if (bindex == btop) {
22418+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22419+ if (err > 0) {
22420+ rmdir_later = err;
22421+ err = 0;
22422+ }
22423+ } else {
22424+ /* stop monitoring */
5afbbe0d 22425+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22426+
22427+ /* dir inode is locked */
5527c038 22428+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22429+ err = 0;
22430+ }
22431+
4a4d8108 22432+ if (!err) {
027c5e7a 22433+ vfsub_dead_dir(inode);
4a4d8108
AM
22434+ au_set_dbdiropq(dentry, -1);
22435+ epilog(dir, dentry, bindex);
1308ab2a 22436+
4a4d8108 22437+ if (rmdir_later) {
5afbbe0d 22438+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22439+ args = NULL;
22440+ }
1308ab2a 22441+
4a4d8108 22442+ goto out_unpin; /* success */
1facf9fc 22443+ }
22444+
4a4d8108
AM
22445+ /* revert */
22446+ AuLabel(revert);
22447+ if (wh_dentry) {
22448+ int rerr;
1308ab2a 22449+
c2b27bf2
AM
22450+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22451+ &a->dt);
4a4d8108
AM
22452+ if (rerr)
22453+ err = rerr;
1facf9fc 22454+ }
22455+
4f0767ce 22456+out_unpin:
c2b27bf2 22457+ au_unpin(&a->pin);
4a4d8108
AM
22458+ dput(wh_dentry);
22459+ dput(h_dentry);
027c5e7a 22460+out_parent:
4a4d8108
AM
22461+ di_write_unlock(parent);
22462+ if (args)
22463+ au_whtmp_rmdir_free(args);
4f0767ce 22464+out_unlock:
4a4d8108 22465+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22466+out_free:
1c60b727 22467+ kfree(a);
4f0767ce 22468+out:
4a4d8108
AM
22469+ AuTraceErr(err);
22470+ return err;
dece6358 22471+}
7f207e10
AM
22472diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22473--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
be118d29 22474+++ linux/fs/aufs/i_op_ren.c 2018-04-06 07:48:44.207938097 +0200
8b6a4947 22475@@ -0,0 +1,1246 @@
1facf9fc 22476+/*
a2654f78 22477+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 22478+ *
22479+ * This program, aufs is free software; you can redistribute it and/or modify
22480+ * it under the terms of the GNU General Public License as published by
22481+ * the Free Software Foundation; either version 2 of the License, or
22482+ * (at your option) any later version.
dece6358
AM
22483+ *
22484+ * This program is distributed in the hope that it will be useful,
22485+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22486+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22487+ * GNU General Public License for more details.
22488+ *
22489+ * You should have received a copy of the GNU General Public License
523b37e3 22490+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22491+ */
22492+
22493+/*
4a4d8108
AM
22494+ * inode operation (rename entry)
22495+ * todo: this is crazy monster
1facf9fc 22496+ */
22497+
22498+#include "aufs.h"
22499+
4a4d8108
AM
22500+enum { AuSRC, AuDST, AuSrcDst };
22501+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22502+
f2c43d5f
AM
22503+#define AuRen_ISDIR_SRC 1
22504+#define AuRen_ISDIR_DST (1 << 1)
22505+#define AuRen_ISSAMEDIR (1 << 2)
22506+#define AuRen_WHSRC (1 << 3)
22507+#define AuRen_WHDST (1 << 4)
22508+#define AuRen_MNT_WRITE (1 << 5)
22509+#define AuRen_DT_DSTDIR (1 << 6)
22510+#define AuRen_DIROPQ_SRC (1 << 7)
22511+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22512+#define AuRen_DIRREN (1 << 9)
22513+#define AuRen_DROPPED_SRC (1 << 10)
22514+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22515+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22516+#define au_fset_ren(flags, name) \
22517+ do { (flags) |= AuRen_##name; } while (0)
22518+#define au_fclr_ren(flags, name) \
22519+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22520+
8b6a4947
AM
22521+#ifndef CONFIG_AUFS_DIRREN
22522+#undef AuRen_DIRREN
22523+#define AuRen_DIRREN 0
22524+#endif
22525+
4a4d8108
AM
22526+struct au_ren_args {
22527+ struct {
22528+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22529+ *wh_dentry;
22530+ struct inode *dir, *inode;
f2c43d5f 22531+ struct au_hinode *hdir, *hinode;
4a4d8108 22532+ struct au_dtime dt[AuParentChild];
f2c43d5f 22533+ aufs_bindex_t btop, bdiropq;
4a4d8108 22534+ } sd[AuSrcDst];
1facf9fc 22535+
4a4d8108
AM
22536+#define src_dentry sd[AuSRC].dentry
22537+#define src_dir sd[AuSRC].dir
22538+#define src_inode sd[AuSRC].inode
22539+#define src_h_dentry sd[AuSRC].h_dentry
22540+#define src_parent sd[AuSRC].parent
22541+#define src_h_parent sd[AuSRC].h_parent
22542+#define src_wh_dentry sd[AuSRC].wh_dentry
22543+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22544+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22545+#define src_h_dir sd[AuSRC].hdir->hi_inode
22546+#define src_dt sd[AuSRC].dt
5afbbe0d 22547+#define src_btop sd[AuSRC].btop
f2c43d5f 22548+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22549+
4a4d8108
AM
22550+#define dst_dentry sd[AuDST].dentry
22551+#define dst_dir sd[AuDST].dir
22552+#define dst_inode sd[AuDST].inode
22553+#define dst_h_dentry sd[AuDST].h_dentry
22554+#define dst_parent sd[AuDST].parent
22555+#define dst_h_parent sd[AuDST].h_parent
22556+#define dst_wh_dentry sd[AuDST].wh_dentry
22557+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22558+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22559+#define dst_h_dir sd[AuDST].hdir->hi_inode
22560+#define dst_dt sd[AuDST].dt
5afbbe0d 22561+#define dst_btop sd[AuDST].btop
f2c43d5f 22562+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22563+
22564+ struct dentry *h_trap;
22565+ struct au_branch *br;
4a4d8108
AM
22566+ struct path h_path;
22567+ struct au_nhash whlist;
f2c43d5f 22568+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22569+
f2c43d5f
AM
22570+ struct {
22571+ unsigned short auren_flags;
22572+ unsigned char flags; /* syscall parameter */
22573+ unsigned char exchange;
22574+ } __packed;
1facf9fc 22575+
4a4d8108
AM
22576+ struct au_whtmp_rmdir *thargs;
22577+ struct dentry *h_dst;
8b6a4947 22578+ struct au_hinode *h_root;
4a4d8108 22579+};
1308ab2a 22580+
4a4d8108 22581+/* ---------------------------------------------------------------------- */
1308ab2a 22582+
4a4d8108
AM
22583+/*
22584+ * functions for reverting.
22585+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22586+ * everything as if nothing happened.
4a4d8108
AM
22587+ * we don't need to revert the copied-up/down the parent dir since they are
22588+ * harmless.
22589+ */
1facf9fc 22590+
4a4d8108
AM
22591+#define RevertFailure(fmt, ...) do { \
22592+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22593+ ##__VA_ARGS__, err, rerr); \
22594+ err = -EIO; \
22595+} while (0)
1facf9fc 22596+
f2c43d5f 22597+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22598+{
4a4d8108 22599+ int rerr;
f2c43d5f
AM
22600+ struct dentry *d;
22601+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22602+
f2c43d5f
AM
22603+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22604+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22605+ rerr = au_diropq_remove(d, a->btgt);
22606+ au_hn_inode_unlock(src_or_dst(hinode));
22607+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22608+ if (rerr)
f2c43d5f
AM
22609+ RevertFailure("remove diropq %pd", d);
22610+
22611+#undef src_or_dst_
22612+}
22613+
22614+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22615+{
22616+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22617+ au_ren_do_rev_diropq(err, a, AuSRC);
22618+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22619+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22620+}
1facf9fc 22621+
4a4d8108
AM
22622+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22623+{
22624+ int rerr;
523b37e3 22625+ struct inode *delegated;
1facf9fc 22626+
b4510431
AM
22627+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22628+ a->src_h_parent);
4a4d8108
AM
22629+ rerr = PTR_ERR(a->h_path.dentry);
22630+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22631+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22632+ return;
1facf9fc 22633+ }
22634+
523b37e3 22635+ delegated = NULL;
4a4d8108
AM
22636+ rerr = vfsub_rename(a->dst_h_dir,
22637+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22638+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22639+ if (unlikely(rerr == -EWOULDBLOCK)) {
22640+ pr_warn("cannot retry for NFSv4 delegation"
22641+ " for an internal rename\n");
22642+ iput(delegated);
22643+ }
4a4d8108
AM
22644+ d_drop(a->h_path.dentry);
22645+ dput(a->h_path.dentry);
22646+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22647+ if (rerr)
523b37e3 22648+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22649+}
22650+
4a4d8108 22651+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22652+{
4a4d8108 22653+ int rerr;
523b37e3 22654+ struct inode *delegated;
dece6358 22655+
b4510431
AM
22656+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22657+ a->dst_h_parent);
4a4d8108
AM
22658+ rerr = PTR_ERR(a->h_path.dentry);
22659+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22660+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22661+ return;
22662+ }
5527c038 22663+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22664+ d_drop(a->h_path.dentry);
22665+ dput(a->h_path.dentry);
22666+ return;
dece6358
AM
22667+ }
22668+
523b37e3
AM
22669+ delegated = NULL;
22670+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22671+ &delegated, a->flags);
523b37e3
AM
22672+ if (unlikely(rerr == -EWOULDBLOCK)) {
22673+ pr_warn("cannot retry for NFSv4 delegation"
22674+ " for an internal rename\n");
22675+ iput(delegated);
22676+ }
4a4d8108
AM
22677+ d_drop(a->h_path.dentry);
22678+ dput(a->h_path.dentry);
22679+ if (!rerr)
22680+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22681+ else
523b37e3 22682+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22683+}
1308ab2a 22684+
4a4d8108
AM
22685+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22686+{
22687+ int rerr;
1308ab2a 22688+
4a4d8108
AM
22689+ a->h_path.dentry = a->src_wh_dentry;
22690+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22691+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22692+ if (rerr)
523b37e3 22693+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22694+}
4a4d8108 22695+#undef RevertFailure
1facf9fc 22696+
1308ab2a 22697+/* ---------------------------------------------------------------------- */
22698+
4a4d8108
AM
22699+/*
22700+ * when we have to copyup the renaming entry, do it with the rename-target name
22701+ * in order to minimize the cost (the later actual rename is unnecessary).
22702+ * otherwise rename it on the target branch.
22703+ */
22704+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22705+{
dece6358 22706+ int err;
4a4d8108 22707+ struct dentry *d;
523b37e3 22708+ struct inode *delegated;
1facf9fc 22709+
4a4d8108 22710+ d = a->src_dentry;
5afbbe0d 22711+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22712+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22713+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22714+ delegated = NULL;
4a4d8108 22715+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22716+ a->dst_h_dir, &a->h_path, &delegated,
22717+ a->flags);
523b37e3
AM
22718+ if (unlikely(err == -EWOULDBLOCK)) {
22719+ pr_warn("cannot retry for NFSv4 delegation"
22720+ " for an internal rename\n");
22721+ iput(delegated);
22722+ }
c2b27bf2 22723+ } else
86dc4139 22724+ BUG();
1308ab2a 22725+
027c5e7a
AM
22726+ if (!err && a->h_dst)
22727+ /* it will be set to dinfo later */
22728+ dget(a->h_dst);
1facf9fc 22729+
dece6358
AM
22730+ return err;
22731+}
1facf9fc 22732+
4a4d8108
AM
22733+/* cf. aufs_rmdir() */
22734+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22735+{
4a4d8108
AM
22736+ int err;
22737+ struct inode *dir;
1facf9fc 22738+
4a4d8108
AM
22739+ dir = a->dst_dir;
22740+ SiMustAnyLock(dir->i_sb);
22741+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22742+ au_sbi(dir->i_sb)->si_dirwh)
22743+ || au_test_fs_remote(a->h_dst->d_sb)) {
22744+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22745+ if (unlikely(err))
523b37e3
AM
22746+ pr_warn("failed removing whtmp dir %pd (%d), "
22747+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22748+ } else {
22749+ au_nhash_wh_free(&a->thargs->whlist);
22750+ a->thargs->whlist = a->whlist;
22751+ a->whlist.nh_num = 0;
22752+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22753+ dput(a->h_dst);
22754+ a->thargs = NULL;
22755+ }
22756+
22757+ return 0;
1308ab2a 22758+}
1facf9fc 22759+
4a4d8108 22760+/* make it 'opaque' dir. */
f2c43d5f 22761+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22762+{
22763+ int err;
f2c43d5f
AM
22764+ struct dentry *d, *diropq;
22765+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22766+
4a4d8108 22767+ err = 0;
f2c43d5f
AM
22768+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22769+ src_or_dst(bdiropq) = au_dbdiropq(d);
22770+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22771+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22772+ diropq = au_diropq_create(d, a->btgt);
22773+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22774+ if (IS_ERR(diropq))
22775+ err = PTR_ERR(diropq);
076b876e
AM
22776+ else
22777+ dput(diropq);
1facf9fc 22778+
f2c43d5f 22779+#undef src_or_dst_
4a4d8108
AM
22780+ return err;
22781+}
1facf9fc 22782+
f2c43d5f 22783+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
22784+{
22785+ int err;
f2c43d5f
AM
22786+ unsigned char always;
22787+ struct dentry *d;
1facf9fc 22788+
f2c43d5f
AM
22789+ err = 0;
22790+ d = a->dst_dentry; /* already renamed on the branch */
22791+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22792+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 22793+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
22794+ && a->btgt != au_dbdiropq(a->src_dentry)
22795+ && (a->dst_wh_dentry
22796+ || a->btgt <= au_dbdiropq(d)
22797+ /* hide the lower to keep xino */
22798+ /* the lowers may not be a dir, but we hide them anyway */
22799+ || a->btgt < au_dbbot(d)
22800+ || always)) {
22801+ AuDbg("here\n");
22802+ err = au_ren_do_diropq(a, AuSRC);
22803+ if (unlikely(err))
4a4d8108 22804+ goto out;
f2c43d5f 22805+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 22806+ }
f2c43d5f
AM
22807+ if (!a->exchange)
22808+ goto out; /* success */
1facf9fc 22809+
f2c43d5f
AM
22810+ d = a->src_dentry; /* already renamed on the branch */
22811+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22812+ && a->btgt != au_dbdiropq(a->dst_dentry)
22813+ && (a->btgt < au_dbdiropq(d)
22814+ || a->btgt < au_dbbot(d)
22815+ || always)) {
22816+ AuDbgDentry(a->src_dentry);
22817+ AuDbgDentry(a->dst_dentry);
22818+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 22819+ if (unlikely(err))
f2c43d5f
AM
22820+ goto out_rev_src;
22821+ au_fset_ren(a->auren_flags, DIROPQ_DST);
22822+ }
22823+ goto out; /* success */
dece6358 22824+
f2c43d5f
AM
22825+out_rev_src:
22826+ AuDbg("err %d, reverting src\n", err);
22827+ au_ren_rev_diropq(err, a);
22828+out:
22829+ return err;
22830+}
22831+
22832+static int do_rename(struct au_ren_args *a)
22833+{
22834+ int err;
22835+ struct dentry *d, *h_d;
22836+
22837+ if (!a->exchange) {
22838+ /* prepare workqueue args for asynchronous rmdir */
22839+ h_d = a->dst_h_dentry;
22840+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 22841+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
22842+ && d_is_positive(h_d)) {
22843+ err = -ENOMEM;
22844+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22845+ GFP_NOFS);
22846+ if (unlikely(!a->thargs))
22847+ goto out;
22848+ a->h_dst = dget(h_d);
22849+ }
22850+
22851+ /* create whiteout for src_dentry */
22852+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
22853+ a->src_bwh = au_dbwh(a->src_dentry);
22854+ AuDebugOn(a->src_bwh >= 0);
22855+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22856+ a->src_h_parent);
22857+ err = PTR_ERR(a->src_wh_dentry);
22858+ if (IS_ERR(a->src_wh_dentry))
22859+ goto out_thargs;
22860+ }
22861+
22862+ /* lookup whiteout for dentry */
22863+ if (au_ftest_ren(a->auren_flags, WHDST)) {
22864+ h_d = au_wh_lkup(a->dst_h_parent,
22865+ &a->dst_dentry->d_name, a->br);
22866+ err = PTR_ERR(h_d);
22867+ if (IS_ERR(h_d))
22868+ goto out_whsrc;
22869+ if (d_is_negative(h_d))
22870+ dput(h_d);
22871+ else
22872+ a->dst_wh_dentry = h_d;
22873+ }
22874+
22875+ /* rename dentry to tmpwh */
22876+ if (a->thargs) {
22877+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
22878+ if (unlikely(err))
22879+ goto out_whdst;
22880+
22881+ d = a->dst_dentry;
22882+ au_set_h_dptr(d, a->btgt, NULL);
22883+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
22884+ if (unlikely(err))
22885+ goto out_whtmp;
22886+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
22887+ }
4a4d8108 22888+ }
1facf9fc 22889+
5afbbe0d 22890+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
8b6a4947
AM
22891+#if 0
22892+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22893+ && d_is_positive(a->dst_h_dentry)
22894+ && a->src_btop != a->btgt);
22895+#endif
1facf9fc 22896+
4a4d8108 22897+ /* rename by vfs_rename or cpup */
4a4d8108
AM
22898+ err = au_ren_or_cpup(a);
22899+ if (unlikely(err))
22900+ /* leave the copied-up one */
22901+ goto out_whtmp;
1308ab2a 22902+
4a4d8108 22903+ /* make dir opaque */
f2c43d5f
AM
22904+ err = au_ren_diropq(a);
22905+ if (unlikely(err))
22906+ goto out_rename;
1308ab2a 22907+
4a4d8108 22908+ /* update target timestamps */
f2c43d5f
AM
22909+ if (a->exchange) {
22910+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22911+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22912+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22913+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22914+ }
5afbbe0d 22915+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
22916+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22917+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 22918+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 22919+
f2c43d5f
AM
22920+ if (!a->exchange) {
22921+ /* remove whiteout for dentry */
22922+ if (a->dst_wh_dentry) {
22923+ a->h_path.dentry = a->dst_wh_dentry;
22924+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22925+ a->dst_dentry);
22926+ if (unlikely(err))
22927+ goto out_diropq;
22928+ }
1facf9fc 22929+
f2c43d5f
AM
22930+ /* remove whtmp */
22931+ if (a->thargs)
22932+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 22933+
f2c43d5f
AM
22934+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22935+ }
4a4d8108
AM
22936+ err = 0;
22937+ goto out_success;
22938+
4f0767ce 22939+out_diropq:
f2c43d5f 22940+ au_ren_rev_diropq(err, a);
4f0767ce 22941+out_rename:
7e9cd9fe 22942+ au_ren_rev_rename(err, a);
027c5e7a 22943+ dput(a->h_dst);
4f0767ce 22944+out_whtmp:
4a4d8108
AM
22945+ if (a->thargs)
22946+ au_ren_rev_whtmp(err, a);
4f0767ce 22947+out_whdst:
4a4d8108
AM
22948+ dput(a->dst_wh_dentry);
22949+ a->dst_wh_dentry = NULL;
4f0767ce 22950+out_whsrc:
4a4d8108
AM
22951+ if (a->src_wh_dentry)
22952+ au_ren_rev_whsrc(err, a);
4f0767ce 22953+out_success:
4a4d8108
AM
22954+ dput(a->src_wh_dentry);
22955+ dput(a->dst_wh_dentry);
4f0767ce 22956+out_thargs:
4a4d8108
AM
22957+ if (a->thargs) {
22958+ dput(a->h_dst);
22959+ au_whtmp_rmdir_free(a->thargs);
22960+ a->thargs = NULL;
22961+ }
4f0767ce 22962+out:
4a4d8108 22963+ return err;
dece6358 22964+}
1facf9fc 22965+
1308ab2a 22966+/* ---------------------------------------------------------------------- */
1facf9fc 22967+
4a4d8108
AM
22968+/*
22969+ * test if @dentry dir can be rename destination or not.
22970+ * success means, it is a logically empty dir.
22971+ */
22972+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 22973+{
4a4d8108 22974+ return au_test_empty(dentry, whlist);
1308ab2a 22975+}
1facf9fc 22976+
4a4d8108 22977+/*
8b6a4947
AM
22978+ * test if @a->src_dentry dir can be rename source or not.
22979+ * if it can, return 0.
4a4d8108
AM
22980+ * success means,
22981+ * - it is a logically empty dir.
22982+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 22983+ * on the lower branch unless DIRREN is on.
4a4d8108 22984+ */
8b6a4947 22985+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
22986+{
22987+ int err;
22988+ unsigned int rdhash;
8b6a4947
AM
22989+ aufs_bindex_t btop, btgt;
22990+ struct dentry *dentry;
22991+ struct super_block *sb;
22992+ struct au_sbinfo *sbinfo;
1facf9fc 22993+
8b6a4947
AM
22994+ dentry = a->src_dentry;
22995+ sb = dentry->d_sb;
22996+ sbinfo = au_sbi(sb);
22997+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
22998+ au_fset_ren(a->auren_flags, DIRREN);
22999+
23000+ btgt = a->btgt;
5afbbe0d
AM
23001+ btop = au_dbtop(dentry);
23002+ if (btop != btgt) {
4a4d8108 23003+ struct au_nhash whlist;
dece6358 23004+
8b6a4947
AM
23005+ SiMustAnyLock(sb);
23006+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23007+ if (!rdhash)
23008+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23009+ dentry));
23010+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23011+ if (unlikely(err))
23012+ goto out;
23013+ err = au_test_empty(dentry, &whlist);
23014+ au_nhash_wh_free(&whlist);
23015+ goto out;
23016+ }
dece6358 23017+
5afbbe0d 23018+ if (btop == au_dbtaildir(dentry))
4a4d8108 23019+ return 0; /* success */
dece6358 23020+
4a4d8108 23021+ err = au_test_empty_lower(dentry);
1facf9fc 23022+
4f0767ce 23023+out:
4a4d8108 23024+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23025+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23026+ err = 0;
23027+ } else {
23028+ AuWarn1("renaming dir who has child(ren) on multiple "
23029+ "branches, is not supported\n");
23030+ err = -EXDEV;
23031+ }
4a4d8108
AM
23032+ }
23033+ return err;
23034+}
1308ab2a 23035+
4a4d8108
AM
23036+/* side effect: sets whlist and h_dentry */
23037+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23038+{
4a4d8108
AM
23039+ int err;
23040+ unsigned int rdhash;
23041+ struct dentry *d;
1facf9fc 23042+
4a4d8108
AM
23043+ d = a->dst_dentry;
23044+ SiMustAnyLock(d->d_sb);
1facf9fc 23045+
4a4d8108 23046+ err = 0;
f2c43d5f 23047+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23048+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23049+ if (!rdhash)
23050+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23051+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23052+ if (unlikely(err))
23053+ goto out;
1308ab2a 23054+
f2c43d5f
AM
23055+ if (!a->exchange) {
23056+ au_set_dbtop(d, a->dst_btop);
23057+ err = may_rename_dstdir(d, &a->whlist);
23058+ au_set_dbtop(d, a->btgt);
23059+ } else
8b6a4947 23060+ err = may_rename_srcdir(a);
4a4d8108 23061+ }
5afbbe0d 23062+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23063+ if (unlikely(err))
23064+ goto out;
23065+
23066+ d = a->src_dentry;
5afbbe0d 23067+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23068+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23069+ err = may_rename_srcdir(a);
4a4d8108
AM
23070+ if (unlikely(err)) {
23071+ au_nhash_wh_free(&a->whlist);
23072+ a->whlist.nh_num = 0;
23073+ }
23074+ }
4f0767ce 23075+out:
4a4d8108 23076+ return err;
1facf9fc 23077+}
23078+
4a4d8108 23079+/* ---------------------------------------------------------------------- */
1facf9fc 23080+
4a4d8108
AM
23081+/*
23082+ * simple tests for rename.
23083+ * following the checks in vfs, plus the parent-child relationship.
23084+ */
23085+static int au_may_ren(struct au_ren_args *a)
23086+{
23087+ int err, isdir;
23088+ struct inode *h_inode;
1facf9fc 23089+
5afbbe0d 23090+ if (a->src_btop == a->btgt) {
4a4d8108 23091+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23092+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23093+ if (unlikely(err))
23094+ goto out;
23095+ err = -EINVAL;
23096+ if (unlikely(a->src_h_dentry == a->h_trap))
23097+ goto out;
23098+ }
1facf9fc 23099+
4a4d8108 23100+ err = 0;
5afbbe0d 23101+ if (a->dst_btop != a->btgt)
4a4d8108 23102+ goto out;
1facf9fc 23103+
027c5e7a
AM
23104+ err = -ENOTEMPTY;
23105+ if (unlikely(a->dst_h_dentry == a->h_trap))
23106+ goto out;
23107+
4a4d8108 23108+ err = -EIO;
f2c43d5f 23109+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23110+ if (d_really_is_negative(a->dst_dentry)) {
23111+ if (d_is_negative(a->dst_h_dentry))
23112+ err = au_may_add(a->dst_dentry, a->btgt,
23113+ a->dst_h_parent, isdir);
4a4d8108 23114+ } else {
5527c038 23115+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23116+ goto out;
5527c038
JR
23117+ h_inode = d_inode(a->dst_h_dentry);
23118+ if (h_inode->i_nlink)
23119+ err = au_may_del(a->dst_dentry, a->btgt,
23120+ a->dst_h_parent, isdir);
4a4d8108 23121+ }
1facf9fc 23122+
4f0767ce 23123+out:
4a4d8108
AM
23124+ if (unlikely(err == -ENOENT || err == -EEXIST))
23125+ err = -EIO;
23126+ AuTraceErr(err);
23127+ return err;
23128+}
1facf9fc 23129+
1308ab2a 23130+/* ---------------------------------------------------------------------- */
1facf9fc 23131+
4a4d8108
AM
23132+/*
23133+ * locking order
23134+ * (VFS)
23135+ * - src_dir and dir by lock_rename()
23136+ * - inode if exitsts
23137+ * (aufs)
23138+ * - lock all
23139+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23140+ * + si_read_lock
23141+ * + di_write_lock2_child()
23142+ * + di_write_lock_child()
23143+ * + ii_write_lock_child()
23144+ * + di_write_lock_child2()
23145+ * + ii_write_lock_child2()
23146+ * + src_parent and parent
23147+ * + di_write_lock_parent()
23148+ * + ii_write_lock_parent()
23149+ * + di_write_lock_parent2()
23150+ * + ii_write_lock_parent2()
23151+ * + lower src_dir and dir by vfsub_lock_rename()
23152+ * + verify the every relationships between child and parent. if any
23153+ * of them failed, unlock all and return -EBUSY.
23154+ */
23155+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23156+{
4a4d8108
AM
23157+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23158+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23159+ if (au_ftest_ren(a->auren_flags, DIRREN)
23160+ && a->h_root)
23161+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23162+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23163+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23164+}
23165+
4a4d8108 23166+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23167+{
4a4d8108
AM
23168+ int err;
23169+ unsigned int udba;
1308ab2a 23170+
4a4d8108
AM
23171+ err = 0;
23172+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23173+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23174+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23175+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23176+
23177+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23178+ if (unlikely(err))
23179+ goto out;
f2c43d5f 23180+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23181+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23182+ struct dentry *root;
23183+ struct inode *dir;
23184+
23185+ /*
23186+ * sbinfo is already locked, so this ii_read_lock is
23187+ * unnecessary. but our debugging feature checks it.
23188+ */
23189+ root = a->src_inode->i_sb->s_root;
23190+ if (root != a->src_parent && root != a->dst_parent) {
23191+ dir = d_inode(root);
23192+ ii_read_lock_parent3(dir);
23193+ a->h_root = au_hi(dir, a->btgt);
23194+ ii_read_unlock(dir);
23195+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23196+ }
23197+ }
4a4d8108
AM
23198+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23199+ a->dst_h_parent, a->dst_hdir);
23200+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23201+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23202+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23203+ err = au_busy_or_stale();
5afbbe0d 23204+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23205+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23206+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23207+ a->br);
5afbbe0d 23208+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23209+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23210+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23211+ a->br);
86dc4139 23212+ if (!err)
4a4d8108 23213+ goto out; /* success */
4a4d8108
AM
23214+
23215+ err = au_busy_or_stale();
4a4d8108 23216+ au_ren_unlock(a);
86dc4139 23217+
4f0767ce 23218+out:
4a4d8108 23219+ return err;
1facf9fc 23220+}
23221+
23222+/* ---------------------------------------------------------------------- */
23223+
4a4d8108 23224+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23225+{
4a4d8108 23226+ struct inode *dir;
dece6358 23227+
4a4d8108 23228+ dir = a->dst_dir;
be118d29 23229+ inode_inc_iversion(dir);
f2c43d5f 23230+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23231+ /* is this updating defined in POSIX? */
23232+ au_cpup_attr_timesizes(a->src_inode);
23233+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23234+ }
b912730e 23235+ au_dir_ts(dir, a->btgt);
dece6358 23236+
f2c43d5f
AM
23237+ if (a->exchange) {
23238+ dir = a->src_dir;
be118d29 23239+ inode_inc_iversion(dir);
f2c43d5f
AM
23240+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23241+ /* is this updating defined in POSIX? */
23242+ au_cpup_attr_timesizes(a->dst_inode);
23243+ au_cpup_attr_nlink(dir, /*force*/1);
23244+ }
23245+ au_dir_ts(dir, a->btgt);
23246+ }
23247+
23248+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23249+ return;
dece6358 23250+
4a4d8108 23251+ dir = a->src_dir;
be118d29 23252+ inode_inc_iversion(dir);
f2c43d5f 23253+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23254+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23255+ au_dir_ts(dir, a->btgt);
1facf9fc 23256+}
23257+
4a4d8108 23258+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23259+{
5afbbe0d 23260+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23261+ struct dentry *d, *h_d;
23262+ struct inode *i, *h_i;
23263+ struct super_block *sb;
dece6358 23264+
027c5e7a
AM
23265+ d = a->dst_dentry;
23266+ d_drop(d);
23267+ if (a->h_dst)
23268+ /* already dget-ed by au_ren_or_cpup() */
23269+ au_set_h_dptr(d, a->btgt, a->h_dst);
23270+
23271+ i = a->dst_inode;
23272+ if (i) {
f2c43d5f
AM
23273+ if (!a->exchange) {
23274+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23275+ vfsub_drop_nlink(i);
23276+ else {
23277+ vfsub_dead_dir(i);
23278+ au_cpup_attr_timesizes(i);
23279+ }
23280+ au_update_dbrange(d, /*do_put_zero*/1);
23281+ } else
23282+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23283+ } else {
5afbbe0d
AM
23284+ bbot = a->btgt;
23285+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23286+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23287+ bbot = au_dbbot(d);
23288+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23289+ au_set_h_dptr(d, bindex, NULL);
23290+ au_update_dbrange(d, /*do_put_zero*/0);
23291+ }
23292+
8b6a4947
AM
23293+ if (a->exchange
23294+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23295+ d_drop(a->src_dentry);
23296+ if (au_ftest_ren(a->auren_flags, DIRREN))
23297+ au_set_dbwh(a->src_dentry, -1);
23298+ return;
23299+ }
23300+
4a4d8108 23301+ d = a->src_dentry;
8b6a4947
AM
23302+ au_set_dbwh(d, -1);
23303+ bbot = au_dbbot(d);
23304+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23305+ h_d = au_h_dptr(d, bindex);
23306+ if (h_d)
23307+ au_set_h_dptr(d, bindex, NULL);
23308+ }
23309+ au_set_dbbot(d, a->btgt);
4a4d8108 23310+
8b6a4947
AM
23311+ sb = d->d_sb;
23312+ i = a->src_inode;
23313+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23314+ return; /* success */
4a4d8108 23315+
8b6a4947
AM
23316+ bbot = au_ibbot(i);
23317+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23318+ h_i = au_h_iptr(i, bindex);
23319+ if (h_i) {
23320+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23321+ /* ignore this error */
23322+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23323+ }
23324+ }
8b6a4947 23325+ au_set_ibbot(i, a->btgt);
1308ab2a 23326+}
dece6358 23327+
4a4d8108
AM
23328+/* ---------------------------------------------------------------------- */
23329+
23330+/* mainly for link(2) and rename(2) */
23331+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23332+{
4a4d8108
AM
23333+ aufs_bindex_t bdiropq, bwh;
23334+ struct dentry *parent;
23335+ struct au_branch *br;
23336+
23337+ parent = dentry->d_parent;
5527c038 23338+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23339+
23340+ bdiropq = au_dbdiropq(parent);
23341+ bwh = au_dbwh(dentry);
23342+ br = au_sbr(dentry->d_sb, btgt);
23343+ if (au_br_rdonly(br)
23344+ || (0 <= bdiropq && bdiropq < btgt)
23345+ || (0 <= bwh && bwh < btgt))
23346+ btgt = -1;
23347+
23348+ AuDbg("btgt %d\n", btgt);
23349+ return btgt;
1facf9fc 23350+}
23351+
5afbbe0d 23352+/* sets src_btop, dst_btop and btgt */
4a4d8108 23353+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23354+{
4a4d8108
AM
23355+ int err;
23356+ struct au_wr_dir_args wr_dir_args = {
23357+ /* .force_btgt = -1, */
23358+ .flags = AuWrDir_ADD_ENTRY
23359+ };
dece6358 23360+
5afbbe0d
AM
23361+ a->src_btop = au_dbtop(a->src_dentry);
23362+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23363+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23364+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23365+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23366+ wr_dir_args.force_btgt = a->src_btop;
23367+ if (a->dst_inode && a->dst_btop < a->src_btop)
23368+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23369+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23370+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23371+ a->btgt = err;
f2c43d5f
AM
23372+ if (a->exchange)
23373+ au_update_dbtop(a->dst_dentry);
dece6358 23374+
4a4d8108 23375+ return err;
1facf9fc 23376+}
23377+
4a4d8108 23378+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23379+{
4a4d8108
AM
23380+ a->h_path.dentry = a->src_h_parent;
23381+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23382+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23383+ a->h_path.dentry = a->dst_h_parent;
23384+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23385+ }
1facf9fc 23386+
f2c43d5f
AM
23387+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23388+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23389+ && !a->exchange)
4a4d8108 23390+ return;
dece6358 23391+
4a4d8108
AM
23392+ a->h_path.dentry = a->src_h_dentry;
23393+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23394+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23395+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23396+ a->h_path.dentry = a->dst_h_dentry;
23397+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23398+ }
1308ab2a 23399+}
dece6358 23400+
4a4d8108 23401+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23402+{
4a4d8108 23403+ struct dentry *h_d;
febd17d6 23404+ struct inode *h_inode;
4a4d8108
AM
23405+
23406+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23407+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23408+ au_dtime_revert(a->dst_dt + AuPARENT);
23409+
f2c43d5f 23410+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23411+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23412+ h_inode = d_inode(h_d);
23413+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23414+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23415+ inode_unlock(h_inode);
4a4d8108 23416+
f2c43d5f 23417+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23418+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23419+ h_inode = d_inode(h_d);
23420+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23421+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 23422+ inode_unlock(h_inode);
1facf9fc 23423+ }
23424+ }
23425+}
23426+
4a4d8108
AM
23427+/* ---------------------------------------------------------------------- */
23428+
23429+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23430+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23431+ unsigned int _flags)
1facf9fc 23432+{
f2c43d5f 23433+ int err, lock_flags;
8b6a4947 23434+ void *rev;
4a4d8108
AM
23435+ /* reduce stack space */
23436+ struct au_ren_args *a;
f2c43d5f 23437+ struct au_pin pin;
4a4d8108 23438+
f2c43d5f 23439+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23440+ IMustLock(_src_dir);
23441+ IMustLock(_dst_dir);
23442+
f2c43d5f
AM
23443+ err = -EINVAL;
23444+ if (unlikely(_flags & RENAME_WHITEOUT))
23445+ goto out;
23446+
4a4d8108
AM
23447+ err = -ENOMEM;
23448+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23449+ a = kzalloc(sizeof(*a), GFP_NOFS);
23450+ if (unlikely(!a))
23451+ goto out;
23452+
f2c43d5f
AM
23453+ a->flags = _flags;
23454+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23455+ a->src_dir = _src_dir;
23456+ a->src_dentry = _src_dentry;
5527c038
JR
23457+ a->src_inode = NULL;
23458+ if (d_really_is_positive(a->src_dentry))
23459+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23460+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23461+ a->dst_dir = _dst_dir;
23462+ a->dst_dentry = _dst_dentry;
5527c038
JR
23463+ a->dst_inode = NULL;
23464+ if (d_really_is_positive(a->dst_dentry))
23465+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23466+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23467+ if (a->dst_inode) {
f2c43d5f
AM
23468+ /*
23469+ * if EXCHANGE && src is non-dir && dst is dir,
23470+ * dst is not locked.
23471+ */
23472+ /* IMustLock(a->dst_inode); */
4a4d8108 23473+ au_igrab(a->dst_inode);
1facf9fc 23474+ }
1facf9fc 23475+
4a4d8108 23476+ err = -ENOTDIR;
f2c43d5f 23477+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23478+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23479+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23480+ if (unlikely(!a->exchange
23481+ && d_really_is_positive(a->dst_dentry)
2000de60 23482+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23483+ goto out_free;
f2c43d5f
AM
23484+ lock_flags |= AuLock_DIRS;
23485+ }
23486+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23487+ au_fset_ren(a->auren_flags, ISDIR_DST);
23488+ if (unlikely(!a->exchange
23489+ && d_really_is_positive(a->src_dentry)
23490+ && !d_is_dir(a->src_dentry)))
23491+ goto out_free;
23492+ lock_flags |= AuLock_DIRS;
b95c5147 23493+ }
8b6a4947
AM
23494+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23495+ lock_flags);
e49829fe
JR
23496+ if (unlikely(err))
23497+ goto out_free;
1facf9fc 23498+
027c5e7a
AM
23499+ err = au_d_hashed_positive(a->src_dentry);
23500+ if (unlikely(err))
23501+ goto out_unlock;
23502+ err = -ENOENT;
23503+ if (a->dst_inode) {
23504+ /*
f2c43d5f 23505+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23506+ * function. It means we cannot rely upon d_unhashed().
23507+ */
23508+ if (unlikely(!a->dst_inode->i_nlink))
23509+ goto out_unlock;
f2c43d5f 23510+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23511+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23512+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23513+ goto out_unlock;
23514+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23515+ goto out_unlock;
23516+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23517+ goto out_unlock;
23518+
7eafdf33
AM
23519+ /*
23520+ * is it possible?
79b8bda9 23521+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23522+ * there may exist a problem somewhere else.
23523+ */
23524+ err = -EINVAL;
5527c038 23525+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23526+ goto out_unlock;
23527+
f2c43d5f 23528+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23529+ di_write_lock_parent(a->dst_parent);
1facf9fc 23530+
4a4d8108
AM
23531+ /* which branch we process */
23532+ err = au_ren_wbr(a);
23533+ if (unlikely(err < 0))
027c5e7a 23534+ goto out_parent;
4a4d8108 23535+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23536+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23537+
4a4d8108
AM
23538+ /* are they available to be renamed */
23539+ err = au_ren_may_dir(a);
23540+ if (unlikely(err))
23541+ goto out_children;
1facf9fc 23542+
4a4d8108 23543+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23544+ if (a->dst_btop == a->btgt) {
f2c43d5f 23545+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23546+ } else {
23547+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23548+ if (unlikely(err))
23549+ goto out_children;
23550+ }
1facf9fc 23551+
f2c43d5f
AM
23552+ err = 0;
23553+ if (!a->exchange) {
23554+ if (a->src_dir != a->dst_dir) {
23555+ /*
23556+ * this temporary unlock is safe,
23557+ * because both dir->i_mutex are locked.
23558+ */
23559+ di_write_unlock(a->dst_parent);
23560+ di_write_lock_parent(a->src_parent);
23561+ err = au_wr_dir_need_wh(a->src_dentry,
23562+ au_ftest_ren(a->auren_flags,
23563+ ISDIR_SRC),
23564+ &a->btgt);
23565+ di_write_unlock(a->src_parent);
23566+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23567+ /*isdir*/1);
23568+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23569+ } else
23570+ err = au_wr_dir_need_wh(a->src_dentry,
23571+ au_ftest_ren(a->auren_flags,
23572+ ISDIR_SRC),
23573+ &a->btgt);
23574+ }
4a4d8108
AM
23575+ if (unlikely(err < 0))
23576+ goto out_children;
23577+ if (err)
f2c43d5f 23578+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23579+
86dc4139 23580+ /* cpup src */
5afbbe0d 23581+ if (a->src_btop != a->btgt) {
86dc4139
AM
23582+ err = au_pin(&pin, a->src_dentry, a->btgt,
23583+ au_opt_udba(a->src_dentry->d_sb),
23584+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23585+ if (!err) {
c2b27bf2
AM
23586+ struct au_cp_generic cpg = {
23587+ .dentry = a->src_dentry,
23588+ .bdst = a->btgt,
5afbbe0d 23589+ .bsrc = a->src_btop,
c2b27bf2
AM
23590+ .len = -1,
23591+ .pin = &pin,
23592+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23593+ };
5afbbe0d 23594+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23595+ err = au_sio_cpup_simple(&cpg);
367653fa 23596+ au_unpin(&pin);
86dc4139 23597+ }
86dc4139
AM
23598+ if (unlikely(err))
23599+ goto out_children;
5afbbe0d 23600+ a->src_btop = a->btgt;
86dc4139 23601+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23602+ if (!a->exchange)
23603+ au_fset_ren(a->auren_flags, WHSRC);
23604+ }
23605+
23606+ /* cpup dst */
23607+ if (a->exchange && a->dst_inode
23608+ && a->dst_btop != a->btgt) {
23609+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23610+ au_opt_udba(a->dst_dentry->d_sb),
23611+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23612+ if (!err) {
23613+ struct au_cp_generic cpg = {
23614+ .dentry = a->dst_dentry,
23615+ .bdst = a->btgt,
23616+ .bsrc = a->dst_btop,
23617+ .len = -1,
23618+ .pin = &pin,
23619+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23620+ };
23621+ err = au_sio_cpup_simple(&cpg);
23622+ au_unpin(&pin);
23623+ }
23624+ if (unlikely(err))
23625+ goto out_children;
23626+ a->dst_btop = a->btgt;
23627+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23628+ }
23629+
4a4d8108
AM
23630+ /* lock them all */
23631+ err = au_ren_lock(a);
23632+ if (unlikely(err))
86dc4139 23633+ /* leave the copied-up one */
4a4d8108 23634+ goto out_children;
1facf9fc 23635+
f2c43d5f
AM
23636+ if (!a->exchange) {
23637+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23638+ err = au_may_ren(a);
23639+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23640+ err = -ENAMETOOLONG;
23641+ if (unlikely(err))
23642+ goto out_hdir;
23643+ }
1facf9fc 23644+
4a4d8108
AM
23645+ /* store timestamps to be revertible */
23646+ au_ren_dt(a);
1facf9fc 23647+
8b6a4947
AM
23648+ /* store dirren info */
23649+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23650+ err = au_dr_rename(a->src_dentry, a->btgt,
23651+ &a->dst_dentry->d_name, &rev);
23652+ AuTraceErr(err);
23653+ if (unlikely(err))
23654+ goto out_dt;
23655+ }
23656+
4a4d8108
AM
23657+ /* here we go */
23658+ err = do_rename(a);
23659+ if (unlikely(err))
8b6a4947
AM
23660+ goto out_dirren;
23661+
23662+ if (au_ftest_ren(a->auren_flags, DIRREN))
23663+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23664+
23665+ /* update dir attributes */
23666+ au_ren_refresh_dir(a);
23667+
23668+ /* dput/iput all lower dentries */
23669+ au_ren_refresh(a);
23670+
23671+ goto out_hdir; /* success */
23672+
8b6a4947
AM
23673+out_dirren:
23674+ if (au_ftest_ren(a->auren_flags, DIRREN))
23675+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23676+out_dt:
4a4d8108 23677+ au_ren_rev_dt(err, a);
4f0767ce 23678+out_hdir:
4a4d8108 23679+ au_ren_unlock(a);
4f0767ce 23680+out_children:
4a4d8108 23681+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23682+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23683+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23684+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23685+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23686+ }
027c5e7a 23687+out_parent:
f2c43d5f 23688+ if (!err) {
8b6a4947
AM
23689+ if (d_unhashed(a->src_dentry))
23690+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23691+ if (d_unhashed(a->dst_dentry))
23692+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23693+ if (!a->exchange)
23694+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23695+ else {
f2c43d5f 23696+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23697+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23698+ d_drop(a->dst_dentry);
23699+ }
23700+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23701+ d_drop(a->src_dentry);
f2c43d5f 23702+ } else {
5afbbe0d 23703+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23704+ if (!a->dst_inode)
23705+ d_drop(a->dst_dentry);
23706+ }
f2c43d5f 23707+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23708+ di_write_unlock(a->dst_parent);
23709+ else
23710+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23711+out_unlock:
4a4d8108 23712+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23713+out_free:
4a4d8108
AM
23714+ iput(a->dst_inode);
23715+ if (a->thargs)
23716+ au_whtmp_rmdir_free(a->thargs);
1c60b727 23717+ kfree(a);
4f0767ce 23718+out:
4a4d8108
AM
23719+ AuTraceErr(err);
23720+ return err;
1308ab2a 23721+}
7f207e10
AM
23722diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23723--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
be118d29 23724+++ linux/fs/aufs/Kconfig 2018-04-06 07:48:44.201271351 +0200
2121bcd9
AM
23725@@ -0,0 +1,199 @@
23726+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
23727+config AUFS_FS
23728+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23729+ help
23730+ Aufs is a stackable unification filesystem such as Unionfs,
23731+ which unifies several directories and provides a merged single
23732+ directory.
23733+ In the early days, aufs was entirely re-designed and
23734+ re-implemented Unionfs Version 1.x series. Introducing many
23735+ original ideas, approaches and improvements, it becomes totally
23736+ different from Unionfs while keeping the basic features.
1facf9fc 23737+
4a4d8108
AM
23738+if AUFS_FS
23739+choice
23740+ prompt "Maximum number of branches"
23741+ default AUFS_BRANCH_MAX_127
23742+ help
23743+ Specifies the maximum number of branches (or member directories)
23744+ in a single aufs. The larger value consumes more system
23745+ resources and has a minor impact to performance.
23746+config AUFS_BRANCH_MAX_127
23747+ bool "127"
23748+ help
23749+ Specifies the maximum number of branches (or member directories)
23750+ in a single aufs. The larger value consumes more system
23751+ resources and has a minor impact to performance.
23752+config AUFS_BRANCH_MAX_511
23753+ bool "511"
23754+ help
23755+ Specifies the maximum number of branches (or member directories)
23756+ in a single aufs. The larger value consumes more system
23757+ resources and has a minor impact to performance.
23758+config AUFS_BRANCH_MAX_1023
23759+ bool "1023"
23760+ help
23761+ Specifies the maximum number of branches (or member directories)
23762+ in a single aufs. The larger value consumes more system
23763+ resources and has a minor impact to performance.
23764+config AUFS_BRANCH_MAX_32767
23765+ bool "32767"
23766+ help
23767+ Specifies the maximum number of branches (or member directories)
23768+ in a single aufs. The larger value consumes more system
23769+ resources and has a minor impact to performance.
23770+endchoice
1facf9fc 23771+
e49829fe
JR
23772+config AUFS_SBILIST
23773+ bool
23774+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23775+ default y
23776+ help
23777+ Automatic configuration for internal use.
23778+ When aufs supports Magic SysRq or /proc, enabled automatically.
23779+
4a4d8108
AM
23780+config AUFS_HNOTIFY
23781+ bool "Detect direct branch access (bypassing aufs)"
23782+ help
23783+ If you want to modify files on branches directly, eg. bypassing aufs,
23784+ and want aufs to detect the changes of them fully, then enable this
23785+ option and use 'udba=notify' mount option.
7f207e10 23786+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
23787+ It will have a negative impact to the performance.
23788+ See detail in aufs.5.
dece6358 23789+
4a4d8108
AM
23790+choice
23791+ prompt "method" if AUFS_HNOTIFY
23792+ default AUFS_HFSNOTIFY
23793+config AUFS_HFSNOTIFY
23794+ bool "fsnotify"
23795+ select FSNOTIFY
4a4d8108 23796+endchoice
1facf9fc 23797+
4a4d8108
AM
23798+config AUFS_EXPORT
23799+ bool "NFS-exportable aufs"
2cbb1c4b 23800+ depends on EXPORTFS
4a4d8108
AM
23801+ help
23802+ If you want to export your mounted aufs via NFS, then enable this
23803+ option. There are several requirements for this configuration.
23804+ See detail in aufs.5.
1facf9fc 23805+
4a4d8108
AM
23806+config AUFS_INO_T_64
23807+ bool
23808+ depends on AUFS_EXPORT
23809+ depends on 64BIT && !(ALPHA || S390)
23810+ default y
23811+ help
23812+ Automatic configuration for internal use.
23813+ /* typedef unsigned long/int __kernel_ino_t */
23814+ /* alpha and s390x are int */
1facf9fc 23815+
c1595e42
JR
23816+config AUFS_XATTR
23817+ bool "support for XATTR/EA (including Security Labels)"
23818+ help
23819+ If your branch fs supports XATTR/EA and you want to make them
23820+ available in aufs too, then enable this opsion and specify the
23821+ branch attributes for EA.
23822+ See detail in aufs.5.
23823+
076b876e
AM
23824+config AUFS_FHSM
23825+ bool "File-based Hierarchical Storage Management"
23826+ help
23827+ Hierarchical Storage Management (or HSM) is a well-known feature
23828+ in the storage world. Aufs provides this feature as file-based.
23829+ with multiple branches.
23830+ These multiple branches are prioritized, ie. the topmost one
23831+ should be the fastest drive and be used heavily.
23832+
4a4d8108
AM
23833+config AUFS_RDU
23834+ bool "Readdir in userspace"
23835+ help
23836+ Aufs has two methods to provide a merged view for a directory,
23837+ by a user-space library and by kernel-space natively. The latter
23838+ is always enabled but sometimes large and slow.
23839+ If you enable this option, install the library in aufs2-util
23840+ package, and set some environment variables for your readdir(3),
23841+ then the work will be handled in user-space which generally
23842+ shows better performance in most cases.
23843+ See detail in aufs.5.
1facf9fc 23844+
8b6a4947
AM
23845+config AUFS_DIRREN
23846+ bool "Workaround for rename(2)-ing a directory"
23847+ help
23848+ By default, aufs returns EXDEV error in renameing a dir who has
23849+ his child on the lower branch, since it is a bad idea to issue
23850+ rename(2) internally for every lower branch. But user may not
23851+ accept this behaviour. So here is a workaround to allow such
23852+ rename(2) and store some extra infromation on the writable
23853+ branch. Obviously this costs high (and I don't like it).
23854+ To use this feature, you need to enable this configuration AND
23855+ to specify the mount option `dirren.'
23856+ See details in aufs.5 and the design documents.
23857+
4a4d8108
AM
23858+config AUFS_SHWH
23859+ bool "Show whiteouts"
23860+ help
23861+ If you want to make the whiteouts in aufs visible, then enable
23862+ this option and specify 'shwh' mount option. Although it may
23863+ sounds like philosophy or something, but in technically it
23864+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 23865+
4a4d8108
AM
23866+config AUFS_BR_RAMFS
23867+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
23868+ help
23869+ If you want to use ramfs as an aufs branch fs, then enable this
23870+ option. Generally tmpfs is recommended.
23871+ Aufs prohibited them to be a branch fs by default, because
23872+ initramfs becomes unusable after switch_root or something
23873+ generally. If you sets initramfs as an aufs branch and boot your
23874+ system by switch_root, you will meet a problem easily since the
23875+ files in initramfs may be inaccessible.
23876+ Unless you are going to use ramfs as an aufs branch fs without
23877+ switch_root or something, leave it N.
1facf9fc 23878+
4a4d8108
AM
23879+config AUFS_BR_FUSE
23880+ bool "Fuse fs as an aufs branch"
23881+ depends on FUSE_FS
23882+ select AUFS_POLL
23883+ help
23884+ If you want to use fuse-based userspace filesystem as an aufs
23885+ branch fs, then enable this option.
23886+ It implements the internal poll(2) operation which is
23887+ implemented by fuse only (curretnly).
1facf9fc 23888+
4a4d8108
AM
23889+config AUFS_POLL
23890+ bool
23891+ help
23892+ Automatic configuration for internal use.
1facf9fc 23893+
4a4d8108
AM
23894+config AUFS_BR_HFSPLUS
23895+ bool "Hfsplus as an aufs branch"
23896+ depends on HFSPLUS_FS
23897+ default y
23898+ help
23899+ If you want to use hfsplus fs as an aufs branch fs, then enable
23900+ this option. This option introduces a small overhead at
23901+ copying-up a file on hfsplus.
1facf9fc 23902+
4a4d8108
AM
23903+config AUFS_BDEV_LOOP
23904+ bool
23905+ depends on BLK_DEV_LOOP
23906+ default y
23907+ help
23908+ Automatic configuration for internal use.
23909+ Convert =[ym] into =y.
1308ab2a 23910+
4a4d8108
AM
23911+config AUFS_DEBUG
23912+ bool "Debug aufs"
23913+ help
23914+ Enable this to compile aufs internal debug code.
23915+ It will have a negative impact to the performance.
23916+
23917+config AUFS_MAGIC_SYSRQ
23918+ bool
23919+ depends on AUFS_DEBUG && MAGIC_SYSRQ
23920+ default y
23921+ help
23922+ Automatic configuration for internal use.
23923+ When aufs supports Magic SysRq, enabled automatically.
23924+endif
7f207e10
AM
23925diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
23926--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
be118d29 23927+++ linux/fs/aufs/loop.c 2018-04-06 07:48:44.207938097 +0200
e2f27e51 23928@@ -0,0 +1,147 @@
1facf9fc 23929+/*
a2654f78 23930+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 23931+ *
23932+ * This program, aufs is free software; you can redistribute it and/or modify
23933+ * it under the terms of the GNU General Public License as published by
23934+ * the Free Software Foundation; either version 2 of the License, or
23935+ * (at your option) any later version.
dece6358
AM
23936+ *
23937+ * This program is distributed in the hope that it will be useful,
23938+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23939+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23940+ * GNU General Public License for more details.
23941+ *
23942+ * You should have received a copy of the GNU General Public License
523b37e3 23943+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 23944+ */
23945+
23946+/*
23947+ * support for loopback block device as a branch
23948+ */
23949+
1facf9fc 23950+#include "aufs.h"
23951+
392086de
AM
23952+/* added into drivers/block/loop.c */
23953+static struct file *(*backing_file_func)(struct super_block *sb);
23954+
1facf9fc 23955+/*
23956+ * test if two lower dentries have overlapping branches.
23957+ */
b752ccd1 23958+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 23959+{
b752ccd1 23960+ struct super_block *h_sb;
392086de
AM
23961+ struct file *backing_file;
23962+
23963+ if (unlikely(!backing_file_func)) {
23964+ /* don't load "loop" module here */
23965+ backing_file_func = symbol_get(loop_backing_file);
23966+ if (unlikely(!backing_file_func))
23967+ /* "loop" module is not loaded */
23968+ return 0;
23969+ }
1facf9fc 23970+
b752ccd1 23971+ h_sb = h_adding->d_sb;
392086de
AM
23972+ backing_file = backing_file_func(h_sb);
23973+ if (!backing_file)
1facf9fc 23974+ return 0;
23975+
2000de60 23976+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
23977+ /*
23978+ * h_adding can be local NFS.
23979+ * in this case aufs cannot detect the loop.
23980+ */
23981+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 23982+ return 1;
b752ccd1 23983+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 23984+}
23985+
23986+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
23987+int au_test_loopback_kthread(void)
23988+{
b752ccd1
AM
23989+ int ret;
23990+ struct task_struct *tsk = current;
a2a7ad62 23991+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
23992+
23993+ ret = 0;
23994+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
23995+ get_task_comm(comm, tsk);
23996+ c = comm[4];
b752ccd1 23997+ ret = ('0' <= c && c <= '9'
a2a7ad62 23998+ && !strncmp(comm, "loop", 4));
b752ccd1 23999+ }
1facf9fc 24000+
b752ccd1 24001+ return ret;
1facf9fc 24002+}
87a755f4
AM
24003+
24004+/* ---------------------------------------------------------------------- */
24005+
24006+#define au_warn_loopback_step 16
24007+static int au_warn_loopback_nelem = au_warn_loopback_step;
24008+static unsigned long *au_warn_loopback_array;
24009+
24010+void au_warn_loopback(struct super_block *h_sb)
24011+{
24012+ int i, new_nelem;
24013+ unsigned long *a, magic;
24014+ static DEFINE_SPINLOCK(spin);
24015+
24016+ magic = h_sb->s_magic;
24017+ spin_lock(&spin);
24018+ a = au_warn_loopback_array;
24019+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24020+ if (a[i] == magic) {
24021+ spin_unlock(&spin);
24022+ return;
24023+ }
24024+
24025+ /* h_sb is new to us, print it */
24026+ if (i < au_warn_loopback_nelem) {
24027+ a[i] = magic;
24028+ goto pr;
24029+ }
24030+
24031+ /* expand the array */
24032+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24033+ a = au_kzrealloc(au_warn_loopback_array,
24034+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24035+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24036+ /*may_shrink*/0);
87a755f4
AM
24037+ if (a) {
24038+ au_warn_loopback_nelem = new_nelem;
24039+ au_warn_loopback_array = a;
24040+ a[i] = magic;
24041+ goto pr;
24042+ }
24043+
24044+ spin_unlock(&spin);
24045+ AuWarn1("realloc failed, ignored\n");
24046+ return;
24047+
24048+pr:
24049+ spin_unlock(&spin);
0c3ec466
AM
24050+ pr_warn("you may want to try another patch for loopback file "
24051+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24052+}
24053+
24054+int au_loopback_init(void)
24055+{
24056+ int err;
24057+ struct super_block *sb __maybe_unused;
24058+
79b8bda9 24059+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
87a755f4
AM
24060+
24061+ err = 0;
24062+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24063+ sizeof(unsigned long), GFP_NOFS);
24064+ if (unlikely(!au_warn_loopback_array))
24065+ err = -ENOMEM;
24066+
24067+ return err;
24068+}
24069+
24070+void au_loopback_fin(void)
24071+{
79b8bda9
AM
24072+ if (backing_file_func)
24073+ symbol_put(loop_backing_file);
1c60b727 24074+ kfree(au_warn_loopback_array);
87a755f4 24075+}
7f207e10
AM
24076diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24077--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
be118d29 24078+++ linux/fs/aufs/loop.h 2018-04-06 07:48:44.207938097 +0200
523b37e3 24079@@ -0,0 +1,52 @@
1facf9fc 24080+/*
a2654f78 24081+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 24082+ *
24083+ * This program, aufs is free software; you can redistribute it and/or modify
24084+ * it under the terms of the GNU General Public License as published by
24085+ * the Free Software Foundation; either version 2 of the License, or
24086+ * (at your option) any later version.
dece6358
AM
24087+ *
24088+ * This program is distributed in the hope that it will be useful,
24089+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24090+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24091+ * GNU General Public License for more details.
24092+ *
24093+ * You should have received a copy of the GNU General Public License
523b37e3 24094+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24095+ */
24096+
24097+/*
24098+ * support for loopback mount as a branch
24099+ */
24100+
24101+#ifndef __AUFS_LOOP_H__
24102+#define __AUFS_LOOP_H__
24103+
24104+#ifdef __KERNEL__
24105+
dece6358
AM
24106+struct dentry;
24107+struct super_block;
1facf9fc 24108+
24109+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24110+/* drivers/block/loop.c */
24111+struct file *loop_backing_file(struct super_block *sb);
24112+
1facf9fc 24113+/* loop.c */
b752ccd1 24114+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24115+int au_test_loopback_kthread(void);
87a755f4
AM
24116+void au_warn_loopback(struct super_block *h_sb);
24117+
24118+int au_loopback_init(void);
24119+void au_loopback_fin(void);
1facf9fc 24120+#else
4a4d8108 24121+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24122+ struct dentry *h_adding)
4a4d8108 24123+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24124+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24125+
24126+AuStubInt0(au_loopback_init, void)
24127+AuStubVoid(au_loopback_fin, void)
1facf9fc 24128+#endif /* BLK_DEV_LOOP */
24129+
24130+#endif /* __KERNEL__ */
24131+#endif /* __AUFS_LOOP_H__ */
7f207e10
AM
24132diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24133--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
be118d29 24134+++ linux/fs/aufs/magic.mk 2018-04-06 07:48:44.207938097 +0200
2121bcd9
AM
24135@@ -0,0 +1,31 @@
24136+# SPDX-License-Identifier: GPL-2.0
1facf9fc 24137+
24138+# defined in ${srctree}/fs/fuse/inode.c
24139+# tristate
24140+ifdef CONFIG_FUSE_FS
24141+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24142+endif
24143+
1facf9fc 24144+# defined in ${srctree}/fs/xfs/xfs_sb.h
24145+# tristate
24146+ifdef CONFIG_XFS_FS
24147+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24148+endif
24149+
24150+# defined in ${srctree}/fs/configfs/mount.c
24151+# tristate
24152+ifdef CONFIG_CONFIGFS_FS
24153+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24154+endif
24155+
1facf9fc 24156+# defined in ${srctree}/fs/ubifs/ubifs.h
24157+# tristate
24158+ifdef CONFIG_UBIFS_FS
24159+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24160+endif
4a4d8108
AM
24161+
24162+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24163+# tristate
24164+ifdef CONFIG_HFSPLUS_FS
24165+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24166+endif
7f207e10
AM
24167diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24168--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
be118d29 24169+++ linux/fs/aufs/Makefile 2018-04-06 07:48:44.201271351 +0200
2121bcd9
AM
24170@@ -0,0 +1,46 @@
24171+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
24172+
24173+include ${src}/magic.mk
24174+ifeq (${CONFIG_AUFS_FS},m)
24175+include ${src}/conf.mk
24176+endif
24177+-include ${src}/priv_def.mk
24178+
24179+# cf. include/linux/kernel.h
24180+# enable pr_debug
24181+ccflags-y += -DDEBUG
f6c5ef8b
AM
24182+# sparse requires the full pathname
24183+ifdef M
523b37e3 24184+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24185+else
523b37e3 24186+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24187+endif
4a4d8108
AM
24188+
24189+obj-$(CONFIG_AUFS_FS) += aufs.o
24190+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24191+ wkq.o vfsub.o dcsub.o \
e49829fe 24192+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24193+ dinfo.o dentry.o \
24194+ dynop.o \
24195+ finfo.o file.o f_op.o \
24196+ dir.o vdir.o \
24197+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24198+ mvdown.o ioctl.o
4a4d8108
AM
24199+
24200+# all are boolean
e49829fe 24201+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24202+aufs-$(CONFIG_SYSFS) += sysfs.o
24203+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24204+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24205+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24206+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24207+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24208+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24209+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24210+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24211+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24212+aufs-$(CONFIG_AUFS_POLL) += poll.o
24213+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24214+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24215+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24216+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10
AM
24217diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24218--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
be118d29 24219+++ linux/fs/aufs/module.c 2018-04-06 07:48:44.207938097 +0200
1c60b727 24220@@ -0,0 +1,266 @@
1facf9fc 24221+/*
a2654f78 24222+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 24223+ *
24224+ * This program, aufs is free software; you can redistribute it and/or modify
24225+ * it under the terms of the GNU General Public License as published by
24226+ * the Free Software Foundation; either version 2 of the License, or
24227+ * (at your option) any later version.
dece6358
AM
24228+ *
24229+ * This program is distributed in the hope that it will be useful,
24230+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24231+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24232+ * GNU General Public License for more details.
24233+ *
24234+ * You should have received a copy of the GNU General Public License
523b37e3 24235+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24236+ */
24237+
24238+/*
24239+ * module global variables and operations
24240+ */
24241+
24242+#include <linux/module.h>
24243+#include <linux/seq_file.h>
24244+#include "aufs.h"
24245+
e2f27e51
AM
24246+/* shrinkable realloc */
24247+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24248+{
e2f27e51
AM
24249+ size_t sz;
24250+ int diff;
1facf9fc 24251+
e2f27e51
AM
24252+ sz = 0;
24253+ diff = -1;
24254+ if (p) {
24255+#if 0 /* unused */
24256+ if (!new_sz) {
1c60b727 24257+ kfree(p);
e2f27e51
AM
24258+ p = NULL;
24259+ goto out;
24260+ }
24261+#else
24262+ AuDebugOn(!new_sz);
24263+#endif
24264+ sz = ksize(p);
24265+ diff = au_kmidx_sub(sz, new_sz);
24266+ }
24267+ if (sz && !diff)
24268+ goto out;
24269+
24270+ if (sz < new_sz)
24271+ /* expand or SLOB */
24272+ p = krealloc(p, new_sz, gfp);
24273+ else if (new_sz < sz && may_shrink) {
24274+ /* shrink */
24275+ void *q;
24276+
24277+ q = kmalloc(new_sz, gfp);
24278+ if (q) {
24279+ if (p) {
24280+ memcpy(q, p, new_sz);
1c60b727 24281+ kfree(p);
e2f27e51
AM
24282+ }
24283+ p = q;
24284+ } else
24285+ p = NULL;
24286+ }
24287+
24288+out:
24289+ return p;
24290+}
24291+
24292+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24293+ int may_shrink)
24294+{
24295+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24296+ if (p && new_sz > nused)
1facf9fc 24297+ memset(p + nused, 0, new_sz - nused);
24298+ return p;
24299+}
24300+
24301+/* ---------------------------------------------------------------------- */
1facf9fc 24302+/*
24303+ * aufs caches
24304+ */
1c60b727 24305+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24306+
24307+static void au_cache_fin(void)
24308+{
24309+ int i;
24310+
24311+ /*
24312+ * Make sure all delayed rcu free inodes are flushed before we
24313+ * destroy cache.
24314+ */
24315+ rcu_barrier();
24316+
24317+ /* excluding AuCache_HNOTIFY */
24318+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24319+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24320+ kmem_cache_destroy(au_cache[i]);
24321+ au_cache[i] = NULL;
5afbbe0d
AM
24322+ }
24323+}
24324+
1facf9fc 24325+static int __init au_cache_init(void)
24326+{
1c60b727
AM
24327+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24328+ if (au_cache[AuCache_DINFO])
027c5e7a 24329+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24330+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24331+ au_icntnr_init_once);
1c60b727
AM
24332+ if (au_cache[AuCache_ICNTNR])
24333+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24334+ au_fi_init_once);
1c60b727
AM
24335+ if (au_cache[AuCache_FINFO])
24336+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24337+ if (au_cache[AuCache_VDIR])
24338+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24339+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24340+ return 0;
24341+
5afbbe0d 24342+ au_cache_fin();
1facf9fc 24343+ return -ENOMEM;
24344+}
24345+
1facf9fc 24346+/* ---------------------------------------------------------------------- */
24347+
24348+int au_dir_roflags;
24349+
e49829fe 24350+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24351+/*
24352+ * iterate_supers_type() doesn't protect us from
24353+ * remounting (branch management)
24354+ */
8b6a4947 24355+struct hlist_bl_head au_sbilist;
e49829fe
JR
24356+#endif
24357+
1facf9fc 24358+/*
24359+ * functions for module interface.
24360+ */
24361+MODULE_LICENSE("GPL");
24362+/* MODULE_LICENSE("GPL v2"); */
dece6358 24363+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24364+MODULE_DESCRIPTION(AUFS_NAME
24365+ " -- Advanced multi layered unification filesystem");
24366+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24367+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24368+
1facf9fc 24369+/* this module parameter has no meaning when SYSFS is disabled */
24370+int sysaufs_brs = 1;
24371+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24372+module_param_named(brs, sysaufs_brs, int, S_IRUGO);
24373+
076b876e 24374+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24375+bool au_userns;
076b876e
AM
24376+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24377+module_param_named(allow_userns, au_userns, bool, S_IRUGO);
24378+
1facf9fc 24379+/* ---------------------------------------------------------------------- */
24380+
24381+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24382+
24383+int au_seq_path(struct seq_file *seq, struct path *path)
24384+{
79b8bda9
AM
24385+ int err;
24386+
24387+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24388+ if (err >= 0)
79b8bda9 24389+ err = 0;
1c60b727 24390+ else
79b8bda9
AM
24391+ err = -ENOMEM;
24392+
24393+ return err;
1facf9fc 24394+}
24395+
24396+/* ---------------------------------------------------------------------- */
24397+
24398+static int __init aufs_init(void)
24399+{
24400+ int err, i;
24401+ char *p;
24402+
24403+ p = au_esc_chars;
24404+ for (i = 1; i <= ' '; i++)
24405+ *p++ = i;
24406+ *p++ = '\\';
24407+ *p++ = '\x7f';
24408+ *p = 0;
24409+
24410+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24411+
b95c5147
AM
24412+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24413+ for (i = 0; i < AuIop_Last; i++)
24414+ aufs_iop_nogetattr[i].getattr = NULL;
24415+
1c60b727 24416+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24417+
e49829fe 24418+ au_sbilist_init();
1facf9fc 24419+ sysaufs_brs_init();
24420+ au_debug_init();
4a4d8108 24421+ au_dy_init();
1facf9fc 24422+ err = sysaufs_init();
24423+ if (unlikely(err))
24424+ goto out;
e49829fe 24425+ err = au_procfs_init();
4f0767ce 24426+ if (unlikely(err))
953406b4 24427+ goto out_sysaufs;
e49829fe
JR
24428+ err = au_wkq_init();
24429+ if (unlikely(err))
24430+ goto out_procfs;
87a755f4 24431+ err = au_loopback_init();
1facf9fc 24432+ if (unlikely(err))
24433+ goto out_wkq;
87a755f4
AM
24434+ err = au_hnotify_init();
24435+ if (unlikely(err))
24436+ goto out_loopback;
1facf9fc 24437+ err = au_sysrq_init();
24438+ if (unlikely(err))
24439+ goto out_hin;
24440+ err = au_cache_init();
24441+ if (unlikely(err))
24442+ goto out_sysrq;
076b876e
AM
24443+
24444+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24445+ err = register_filesystem(&aufs_fs_type);
24446+ if (unlikely(err))
24447+ goto out_cache;
076b876e 24448+
4a4d8108
AM
24449+ /* since we define pr_fmt, call printk directly */
24450+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24451+ goto out; /* success */
24452+
4f0767ce 24453+out_cache:
1facf9fc 24454+ au_cache_fin();
4f0767ce 24455+out_sysrq:
1facf9fc 24456+ au_sysrq_fin();
4f0767ce 24457+out_hin:
4a4d8108 24458+ au_hnotify_fin();
87a755f4
AM
24459+out_loopback:
24460+ au_loopback_fin();
4f0767ce 24461+out_wkq:
1facf9fc 24462+ au_wkq_fin();
e49829fe
JR
24463+out_procfs:
24464+ au_procfs_fin();
4f0767ce 24465+out_sysaufs:
1facf9fc 24466+ sysaufs_fin();
4a4d8108 24467+ au_dy_fin();
4f0767ce 24468+out:
1facf9fc 24469+ return err;
24470+}
24471+
24472+static void __exit aufs_exit(void)
24473+{
24474+ unregister_filesystem(&aufs_fs_type);
24475+ au_cache_fin();
24476+ au_sysrq_fin();
4a4d8108 24477+ au_hnotify_fin();
87a755f4 24478+ au_loopback_fin();
1facf9fc 24479+ au_wkq_fin();
e49829fe 24480+ au_procfs_fin();
1facf9fc 24481+ sysaufs_fin();
4a4d8108 24482+ au_dy_fin();
1facf9fc 24483+}
24484+
24485+module_init(aufs_init);
24486+module_exit(aufs_exit);
7f207e10
AM
24487diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24488--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
be118d29 24489+++ linux/fs/aufs/module.h 2018-04-06 07:48:44.207938097 +0200
1c60b727 24490@@ -0,0 +1,101 @@
1facf9fc 24491+/*
a2654f78 24492+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 24493+ *
24494+ * This program, aufs is free software; you can redistribute it and/or modify
24495+ * it under the terms of the GNU General Public License as published by
24496+ * the Free Software Foundation; either version 2 of the License, or
24497+ * (at your option) any later version.
dece6358
AM
24498+ *
24499+ * This program is distributed in the hope that it will be useful,
24500+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24501+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24502+ * GNU General Public License for more details.
24503+ *
24504+ * You should have received a copy of the GNU General Public License
523b37e3 24505+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24506+ */
24507+
24508+/*
24509+ * module initialization and module-global
24510+ */
24511+
24512+#ifndef __AUFS_MODULE_H__
24513+#define __AUFS_MODULE_H__
24514+
24515+#ifdef __KERNEL__
24516+
24517+#include <linux/slab.h>
24518+
dece6358
AM
24519+struct path;
24520+struct seq_file;
24521+
1facf9fc 24522+/* module parameters */
1facf9fc 24523+extern int sysaufs_brs;
8cdd5066 24524+extern bool au_userns;
1facf9fc 24525+
24526+/* ---------------------------------------------------------------------- */
24527+
24528+extern int au_dir_roflags;
24529+
e2f27e51
AM
24530+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24531+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24532+ int may_shrink);
24533+
24534+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24535+{
24536+#ifndef CONFIG_SLOB
24537+ return kmalloc_index(sz) - kmalloc_index(new_sz);
24538+#else
24539+ return -1; /* SLOB is untested */
24540+#endif
24541+}
24542+
1facf9fc 24543+int au_seq_path(struct seq_file *seq, struct path *path);
24544+
e49829fe
JR
24545+#ifdef CONFIG_PROC_FS
24546+/* procfs.c */
24547+int __init au_procfs_init(void);
24548+void au_procfs_fin(void);
24549+#else
24550+AuStubInt0(au_procfs_init, void);
24551+AuStubVoid(au_procfs_fin, void);
24552+#endif
24553+
4f0767ce
JR
24554+/* ---------------------------------------------------------------------- */
24555+
1c60b727 24556+/* kmem cache */
1facf9fc 24557+enum {
24558+ AuCache_DINFO,
24559+ AuCache_ICNTNR,
24560+ AuCache_FINFO,
24561+ AuCache_VDIR,
24562+ AuCache_DEHSTR,
7eafdf33 24563+ AuCache_HNOTIFY, /* must be last */
1facf9fc 24564+ AuCache_Last
24565+};
24566+
1c60b727 24567+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 24568+
4a4d8108
AM
24569+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24570+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
24571+#define AuCacheCtor(type, ctor) \
24572+ kmem_cache_create(#type, sizeof(struct type), \
24573+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 24574+
1facf9fc 24575+#define AuCacheFuncs(name, index) \
4a4d8108 24576+static inline struct au_##name *au_cache_alloc_##name(void) \
1c60b727 24577+{ return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
4a4d8108 24578+static inline void au_cache_free_##name(struct au_##name *p) \
1c60b727 24579+{ kmem_cache_free(au_cache[AuCache_##index], p); }
1facf9fc 24580+
24581+AuCacheFuncs(dinfo, DINFO);
24582+AuCacheFuncs(icntnr, ICNTNR);
24583+AuCacheFuncs(finfo, FINFO);
24584+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
24585+AuCacheFuncs(vdir_dehstr, DEHSTR);
24586+#ifdef CONFIG_AUFS_HNOTIFY
24587+AuCacheFuncs(hnotify, HNOTIFY);
24588+#endif
1facf9fc 24589+
4a4d8108
AM
24590+#endif /* __KERNEL__ */
24591+#endif /* __AUFS_MODULE_H__ */
c2b27bf2
AM
24592diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24593--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
be118d29 24594+++ linux/fs/aufs/mvdown.c 2018-04-06 07:48:44.207938097 +0200
5afbbe0d 24595@@ -0,0 +1,704 @@
c2b27bf2 24596+/*
a2654f78 24597+ * Copyright (C) 2011-2017 Junjiro R. Okajima
c2b27bf2
AM
24598+ *
24599+ * This program, aufs is free software; you can redistribute it and/or modify
24600+ * it under the terms of the GNU General Public License as published by
24601+ * the Free Software Foundation; either version 2 of the License, or
24602+ * (at your option) any later version.
24603+ *
24604+ * This program is distributed in the hope that it will be useful,
24605+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24606+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24607+ * GNU General Public License for more details.
24608+ *
24609+ * You should have received a copy of the GNU General Public License
523b37e3
AM
24610+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24611+ */
24612+
24613+/*
24614+ * move-down, opposite of copy-up
c2b27bf2
AM
24615+ */
24616+
24617+#include "aufs.h"
24618+
c2b27bf2
AM
24619+struct au_mvd_args {
24620+ struct {
c2b27bf2
AM
24621+ struct super_block *h_sb;
24622+ struct dentry *h_parent;
24623+ struct au_hinode *hdir;
392086de 24624+ struct inode *h_dir, *h_inode;
c1595e42 24625+ struct au_pin pin;
c2b27bf2
AM
24626+ } info[AUFS_MVDOWN_NARRAY];
24627+
24628+ struct aufs_mvdown mvdown;
24629+ struct dentry *dentry, *parent;
24630+ struct inode *inode, *dir;
24631+ struct super_block *sb;
24632+ aufs_bindex_t bopq, bwh, bfound;
24633+ unsigned char rename_lock;
c2b27bf2
AM
24634+};
24635+
392086de 24636+#define mvd_errno mvdown.au_errno
076b876e
AM
24637+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24638+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24639+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24640+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 24641+
392086de
AM
24642+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
24643+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
24644+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
24645+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
24646+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 24647+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
24648+
24649+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
24650+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
24651+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
24652+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
24653+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 24654+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
24655+
24656+#define AU_MVD_PR(flag, ...) do { \
24657+ if (flag) \
24658+ pr_err(__VA_ARGS__); \
24659+ } while (0)
24660+
076b876e
AM
24661+static int find_lower_writable(struct au_mvd_args *a)
24662+{
24663+ struct super_block *sb;
5afbbe0d 24664+ aufs_bindex_t bindex, bbot;
076b876e
AM
24665+ struct au_branch *br;
24666+
24667+ sb = a->sb;
24668+ bindex = a->mvd_bsrc;
5afbbe0d 24669+ bbot = au_sbbot(sb);
076b876e 24670+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 24671+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24672+ br = au_sbr(sb, bindex);
24673+ if (au_br_fhsm(br->br_perm)
8b6a4947 24674+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
24675+ return bindex;
24676+ }
24677+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 24678+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24679+ br = au_sbr(sb, bindex);
24680+ if (!au_br_rdonly(br))
24681+ return bindex;
24682+ }
24683+ else
5afbbe0d 24684+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 24685+ br = au_sbr(sb, bindex);
8b6a4947 24686+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
24687+ if (au_br_rdonly(br))
24688+ a->mvdown.flags
24689+ |= AUFS_MVDOWN_ROLOWER_R;
24690+ return bindex;
24691+ }
24692+ }
24693+
24694+ return -1;
24695+}
24696+
c2b27bf2 24697+/* make the parent dir on bdst */
392086de 24698+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24699+{
24700+ int err;
24701+
24702+ err = 0;
24703+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24704+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24705+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24706+ a->mvd_h_dst_parent = NULL;
5afbbe0d 24707+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
24708+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24709+ if (!a->mvd_h_dst_parent) {
24710+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
24711+ if (unlikely(err)) {
392086de 24712+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
24713+ goto out;
24714+ }
24715+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24716+ }
24717+
24718+out:
24719+ AuTraceErr(err);
24720+ return err;
24721+}
24722+
24723+/* lock them all */
392086de 24724+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24725+{
24726+ int err;
24727+ struct dentry *h_trap;
24728+
24729+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
24730+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
24731+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
24732+ au_opt_udba(a->sb),
24733+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24734+ AuTraceErr(err);
24735+ if (unlikely(err)) {
24736+ AU_MVD_PR(dmsg, "pin_dst failed\n");
24737+ goto out;
24738+ }
24739+
c2b27bf2
AM
24740+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
24741+ a->rename_lock = 0;
c1595e42
JR
24742+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24743+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
24744+ au_opt_udba(a->sb),
24745+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24746+ err = au_do_pin(&a->mvd_pin_src);
24747+ AuTraceErr(err);
5527c038 24748+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
24749+ if (unlikely(err)) {
24750+ AU_MVD_PR(dmsg, "pin_src failed\n");
24751+ goto out_dst;
24752+ }
24753+ goto out; /* success */
c2b27bf2
AM
24754+ }
24755+
c2b27bf2 24756+ a->rename_lock = 1;
c1595e42
JR
24757+ au_pin_hdir_unlock(&a->mvd_pin_dst);
24758+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24759+ au_opt_udba(a->sb),
24760+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24761+ AuTraceErr(err);
5527c038 24762+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
24763+ if (unlikely(err)) {
24764+ AU_MVD_PR(dmsg, "pin_src failed\n");
24765+ au_pin_hdir_lock(&a->mvd_pin_dst);
24766+ goto out_dst;
24767+ }
24768+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
24769+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24770+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
24771+ if (h_trap) {
24772+ err = (h_trap != a->mvd_h_src_parent);
24773+ if (err)
24774+ err = (h_trap != a->mvd_h_dst_parent);
24775+ }
24776+ BUG_ON(err); /* it should never happen */
c1595e42
JR
24777+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
24778+ err = -EBUSY;
24779+ AuTraceErr(err);
24780+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24781+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
24782+ au_pin_hdir_lock(&a->mvd_pin_src);
24783+ au_unpin(&a->mvd_pin_src);
24784+ au_pin_hdir_lock(&a->mvd_pin_dst);
24785+ goto out_dst;
24786+ }
24787+ goto out; /* success */
c2b27bf2 24788+
c1595e42
JR
24789+out_dst:
24790+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
24791+out:
24792+ AuTraceErr(err);
24793+ return err;
24794+}
24795+
392086de 24796+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 24797+{
c1595e42
JR
24798+ if (!a->rename_lock)
24799+ au_unpin(&a->mvd_pin_src);
24800+ else {
c2b27bf2
AM
24801+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24802+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
24803+ au_pin_hdir_lock(&a->mvd_pin_src);
24804+ au_unpin(&a->mvd_pin_src);
24805+ au_pin_hdir_lock(&a->mvd_pin_dst);
24806+ }
24807+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
24808+}
24809+
24810+/* copy-down the file */
392086de 24811+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24812+{
24813+ int err;
24814+ struct au_cp_generic cpg = {
24815+ .dentry = a->dentry,
24816+ .bdst = a->mvd_bdst,
24817+ .bsrc = a->mvd_bsrc,
24818+ .len = -1,
c1595e42 24819+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
24820+ .flags = AuCpup_DTIME | AuCpup_HOPEN
24821+ };
24822+
24823+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
24824+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
24825+ au_fset_cpup(cpg.flags, OVERWRITE);
24826+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
24827+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
24828+ err = au_sio_cpdown_simple(&cpg);
24829+ if (unlikely(err))
392086de 24830+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
24831+
24832+ AuTraceErr(err);
24833+ return err;
24834+}
24835+
24836+/*
24837+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
24838+ * were sleeping
24839+ */
392086de 24840+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24841+{
24842+ int err;
24843+ struct path h_path;
24844+ struct au_branch *br;
523b37e3 24845+ struct inode *delegated;
c2b27bf2
AM
24846+
24847+ br = au_sbr(a->sb, a->mvd_bdst);
24848+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
24849+ err = PTR_ERR(h_path.dentry);
24850+ if (IS_ERR(h_path.dentry)) {
392086de 24851+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
24852+ goto out;
24853+ }
24854+
24855+ err = 0;
5527c038 24856+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 24857+ h_path.mnt = au_br_mnt(br);
523b37e3 24858+ delegated = NULL;
5527c038 24859+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
24860+ &delegated, /*force*/0);
24861+ if (unlikely(err == -EWOULDBLOCK)) {
24862+ pr_warn("cannot retry for NFSv4 delegation"
24863+ " for an internal unlink\n");
24864+ iput(delegated);
24865+ }
c2b27bf2 24866+ if (unlikely(err))
392086de 24867+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
24868+ }
24869+ dput(h_path.dentry);
24870+
24871+out:
24872+ AuTraceErr(err);
24873+ return err;
24874+}
24875+
24876+/*
24877+ * unlink the topmost h_dentry
c2b27bf2 24878+ */
392086de 24879+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24880+{
24881+ int err;
24882+ struct path h_path;
523b37e3 24883+ struct inode *delegated;
c2b27bf2
AM
24884+
24885+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
24886+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
24887+ delegated = NULL;
24888+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
24889+ if (unlikely(err == -EWOULDBLOCK)) {
24890+ pr_warn("cannot retry for NFSv4 delegation"
24891+ " for an internal unlink\n");
24892+ iput(delegated);
24893+ }
c2b27bf2 24894+ if (unlikely(err))
392086de 24895+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
24896+
24897+ AuTraceErr(err);
24898+ return err;
24899+}
24900+
076b876e
AM
24901+/* Since mvdown succeeded, we ignore an error of this function */
24902+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
24903+{
24904+ int err;
24905+ struct au_branch *br;
24906+
24907+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
24908+ br = au_sbr(a->sb, a->mvd_bsrc);
24909+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
24910+ if (!err) {
24911+ br = au_sbr(a->sb, a->mvd_bdst);
24912+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
24913+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
24914+ }
24915+ if (!err)
24916+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
24917+ else
24918+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
24919+}
24920+
c2b27bf2
AM
24921+/*
24922+ * copy-down the file and unlink the bsrc file.
24923+ * - unlink the bdst whout if exist
24924+ * - copy-down the file (with whtmp name and rename)
24925+ * - unlink the bsrc file
24926+ */
392086de 24927+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24928+{
24929+ int err;
24930+
392086de 24931+ err = au_do_mkdir(dmsg, a);
c2b27bf2 24932+ if (!err)
392086de 24933+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
24934+ if (unlikely(err))
24935+ goto out;
24936+
24937+ /*
24938+ * do not revert the activities we made on bdst since they should be
24939+ * harmless in aufs.
24940+ */
24941+
392086de 24942+ err = au_do_cpdown(dmsg, a);
c2b27bf2 24943+ if (!err)
392086de
AM
24944+ err = au_do_unlink_wh(dmsg, a);
24945+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
24946+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
24947+ if (unlikely(err))
24948+ goto out_unlock;
24949+
c1595e42
JR
24950+ AuDbg("%pd2, 0x%x, %d --> %d\n",
24951+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
24952+ if (find_lower_writable(a) < 0)
24953+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
24954+
24955+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
24956+ au_do_stfs(dmsg, a);
24957+
c2b27bf2 24958+ /* maintain internal array */
392086de
AM
24959+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
24960+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 24961+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 24962+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 24963+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
24964+ } else {
24965+ /* hide the lower */
24966+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 24967+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 24968+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 24969+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 24970+ }
5afbbe0d
AM
24971+ if (au_dbbot(a->dentry) < a->mvd_bdst)
24972+ au_set_dbbot(a->dentry, a->mvd_bdst);
24973+ if (au_ibbot(a->inode) < a->mvd_bdst)
24974+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
24975+
24976+out_unlock:
392086de 24977+ au_do_unlock(dmsg, a);
c2b27bf2
AM
24978+out:
24979+ AuTraceErr(err);
24980+ return err;
24981+}
24982+
24983+/* ---------------------------------------------------------------------- */
24984+
c2b27bf2 24985+/* make sure the file is idle */
392086de 24986+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24987+{
24988+ int err, plinked;
c2b27bf2
AM
24989+
24990+ err = 0;
c2b27bf2 24991+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 24992+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 24993+ && au_dcount(a->dentry) == 1
c2b27bf2 24994+ && atomic_read(&a->inode->i_count) == 1
392086de 24995+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
24996+ && (!plinked || !au_plink_test(a->inode))
24997+ && a->inode->i_nlink == 1)
24998+ goto out;
24999+
25000+ err = -EBUSY;
392086de 25001+ AU_MVD_PR(dmsg,
c1595e42 25002+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 25003+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 25004+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 25005+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
25006+ plinked, plinked ? au_plink_test(a->inode) : 0);
25007+
25008+out:
25009+ AuTraceErr(err);
25010+ return err;
25011+}
25012+
25013+/* make sure the parent dir is fine */
392086de 25014+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25015+ struct au_mvd_args *a)
25016+{
25017+ int err;
25018+ aufs_bindex_t bindex;
25019+
25020+ err = 0;
25021+ if (unlikely(au_alive_dir(a->parent))) {
25022+ err = -ENOENT;
392086de 25023+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25024+ goto out;
25025+ }
25026+
25027+ a->bopq = au_dbdiropq(a->parent);
25028+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25029+ AuDbg("b%d\n", bindex);
25030+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25031+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25032+ err = -EINVAL;
392086de
AM
25033+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25034+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25035+ a->bopq, a->mvd_bdst);
25036+ }
25037+
25038+out:
25039+ AuTraceErr(err);
25040+ return err;
25041+}
25042+
392086de 25043+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25044+ struct au_mvd_args *a)
25045+{
25046+ int err;
25047+ struct au_dinfo *dinfo, *tmp;
25048+
25049+ /* lookup the next lower positive entry */
25050+ err = -ENOMEM;
25051+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25052+ if (unlikely(!tmp))
25053+ goto out;
25054+
25055+ a->bfound = -1;
25056+ a->bwh = -1;
25057+ dinfo = au_di(a->dentry);
25058+ au_di_cp(tmp, dinfo);
25059+ au_di_swap(tmp, dinfo);
25060+
25061+ /* returns the number of positive dentries */
5afbbe0d
AM
25062+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25063+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25064+ if (!err)
25065+ a->bwh = au_dbwh(a->dentry);
25066+ else if (err > 0)
5afbbe0d 25067+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25068+
25069+ au_di_swap(tmp, dinfo);
25070+ au_rw_write_unlock(&tmp->di_rwsem);
25071+ au_di_free(tmp);
25072+ if (unlikely(err < 0))
392086de 25073+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25074+
25075+ /*
25076+ * here, we have these cases.
25077+ * bfound == -1
25078+ * no positive dentry under bsrc. there are more sub-cases.
25079+ * bwh < 0
25080+ * there no whiteout, we can safely move-down.
25081+ * bwh <= bsrc
25082+ * impossible
25083+ * bsrc < bwh && bwh < bdst
25084+ * there is a whiteout on RO branch. cannot proceed.
25085+ * bwh == bdst
25086+ * there is a whiteout on the RW target branch. it should
25087+ * be removed.
25088+ * bdst < bwh
25089+ * there is a whiteout somewhere unrelated branch.
25090+ * -1 < bfound && bfound <= bsrc
25091+ * impossible.
25092+ * bfound < bdst
25093+ * found, but it is on RO branch between bsrc and bdst. cannot
25094+ * proceed.
25095+ * bfound == bdst
25096+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25097+ * error.
25098+ * bdst < bfound
25099+ * found, after we create the file on bdst, it will be hidden.
25100+ */
25101+
25102+ AuDebugOn(a->bfound == -1
25103+ && a->bwh != -1
25104+ && a->bwh <= a->mvd_bsrc);
25105+ AuDebugOn(-1 < a->bfound
25106+ && a->bfound <= a->mvd_bsrc);
25107+
25108+ err = -EINVAL;
25109+ if (a->bfound == -1
25110+ && a->mvd_bsrc < a->bwh
25111+ && a->bwh != -1
25112+ && a->bwh < a->mvd_bdst) {
392086de
AM
25113+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25114+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25115+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25116+ goto out;
25117+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25118+ a->mvd_errno = EAU_MVDOWN_UPPER;
25119+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25120+ a->mvd_bdst, a->bfound);
25121+ goto out;
25122+ }
25123+
25124+ err = 0; /* success */
25125+
25126+out:
25127+ AuTraceErr(err);
25128+ return err;
25129+}
25130+
392086de 25131+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25132+{
25133+ int err;
25134+
392086de
AM
25135+ err = 0;
25136+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25137+ && a->bfound == a->mvd_bdst)
25138+ err = -EEXIST;
c2b27bf2
AM
25139+ AuTraceErr(err);
25140+ return err;
25141+}
25142+
392086de 25143+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25144+{
25145+ int err;
25146+ struct au_branch *br;
25147+
25148+ err = -EISDIR;
25149+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25150+ goto out;
25151+
25152+ err = -EINVAL;
392086de 25153+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25154+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25155+ else {
25156+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25157+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25158+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25159+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25160+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25161+ || (a->mvd_bsrc < au_ibtop(a->inode)
25162+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25163+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25164+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25165+ AU_MVD_PR(dmsg, "no upper\n");
25166+ goto out;
25167+ }
25168+ }
5afbbe0d 25169+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25170+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25171+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25172+ goto out;
25173+ }
392086de 25174+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25175+ br = au_sbr(a->sb, a->mvd_bsrc);
25176+ err = au_br_rdonly(br);
392086de
AM
25177+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25178+ if (unlikely(err))
25179+ goto out;
25180+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25181+ || IS_APPEND(a->mvd_h_src_inode))) {
25182+ if (err)
25183+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25184+ /* go on */
25185+ } else
c2b27bf2
AM
25186+ goto out;
25187+
25188+ err = -EINVAL;
392086de
AM
25189+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25190+ a->mvd_bdst = find_lower_writable(a);
25191+ if (unlikely(a->mvd_bdst < 0)) {
25192+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25193+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25194+ goto out;
25195+ }
25196+ } else {
25197+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25198+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25199+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25200+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25201+ AU_MVD_PR(dmsg, "no lower brid\n");
25202+ goto out;
25203+ }
c2b27bf2
AM
25204+ }
25205+
392086de 25206+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25207+ if (!err)
392086de 25208+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25209+ if (!err)
392086de 25210+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25211+ if (!err)
392086de 25212+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25213+ if (!err)
25214+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25215+
25216+out:
25217+ AuTraceErr(err);
25218+ return err;
25219+}
25220+
25221+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25222+{
392086de
AM
25223+ int err, e;
25224+ unsigned char dmsg;
25225+ struct au_mvd_args *args;
79b8bda9 25226+ struct inode *inode;
c2b27bf2 25227+
79b8bda9 25228+ inode = d_inode(dentry);
c2b27bf2
AM
25229+ err = -EPERM;
25230+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25231+ goto out;
25232+
392086de
AM
25233+ err = -ENOMEM;
25234+ args = kmalloc(sizeof(*args), GFP_NOFS);
25235+ if (unlikely(!args))
25236+ goto out;
25237+
25238+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25239+ if (!err)
25240+ err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
c2b27bf2
AM
25241+ if (unlikely(err)) {
25242+ err = -EFAULT;
392086de
AM
25243+ AuTraceErr(err);
25244+ goto out_free;
c2b27bf2 25245+ }
392086de
AM
25246+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25247+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25248+ args->mvdown.au_errno = 0;
25249+ args->dentry = dentry;
79b8bda9 25250+ args->inode = inode;
392086de 25251+ args->sb = dentry->d_sb;
c2b27bf2 25252+
392086de
AM
25253+ err = -ENOENT;
25254+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25255+ args->parent = dget_parent(dentry);
5527c038 25256+ args->dir = d_inode(args->parent);
febd17d6 25257+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25258+ dput(args->parent);
25259+ if (unlikely(args->parent != dentry->d_parent)) {
25260+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25261+ goto out_dir;
25262+ }
25263+
febd17d6 25264+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25265+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25266+ if (unlikely(err))
25267+ goto out_inode;
25268+
392086de
AM
25269+ di_write_lock_parent(args->parent);
25270+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25271+ if (unlikely(err))
25272+ goto out_parent;
25273+
392086de 25274+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25275+ if (unlikely(err))
25276+ goto out_parent;
c2b27bf2 25277+
392086de 25278+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25279+ au_cpup_attr_timesizes(inode);
25280+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25281+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25282+ /* au_digen_dec(dentry); */
25283+
25284+out_parent:
392086de 25285+ di_write_unlock(args->parent);
c2b27bf2
AM
25286+ aufs_read_unlock(dentry, AuLock_DW);
25287+out_inode:
febd17d6 25288+ inode_unlock(inode);
c2b27bf2 25289+out_dir:
febd17d6 25290+ inode_unlock(args->dir);
392086de
AM
25291+out_free:
25292+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25293+ if (unlikely(e))
25294+ err = -EFAULT;
1c60b727 25295+ kfree(args);
c2b27bf2
AM
25296+out:
25297+ AuTraceErr(err);
25298+ return err;
25299+}
25300diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25301--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
be118d29 25302+++ linux/fs/aufs/opts.c 2018-04-06 07:48:44.207938097 +0200
8b6a4947 25303@@ -0,0 +1,1891 @@
1facf9fc 25304+/*
a2654f78 25305+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 25306+ *
25307+ * This program, aufs is free software; you can redistribute it and/or modify
25308+ * it under the terms of the GNU General Public License as published by
25309+ * the Free Software Foundation; either version 2 of the License, or
25310+ * (at your option) any later version.
dece6358
AM
25311+ *
25312+ * This program is distributed in the hope that it will be useful,
25313+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25314+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25315+ * GNU General Public License for more details.
25316+ *
25317+ * You should have received a copy of the GNU General Public License
523b37e3 25318+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25319+ */
25320+
25321+/*
25322+ * mount options/flags
25323+ */
25324+
dece6358 25325+#include <linux/namei.h>
1facf9fc 25326+#include <linux/types.h> /* a distribution requires */
25327+#include <linux/parser.h>
25328+#include "aufs.h"
25329+
25330+/* ---------------------------------------------------------------------- */
25331+
25332+enum {
25333+ Opt_br,
7e9cd9fe
AM
25334+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25335+ Opt_idel, Opt_imod,
25336+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25337+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25338+ Opt_xino, Opt_noxino,
1facf9fc 25339+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25340+ Opt_trunc_xino_path, Opt_itrunc_xino,
25341+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25342+ Opt_shwh, Opt_noshwh,
1facf9fc 25343+ Opt_plink, Opt_noplink, Opt_list_plink,
25344+ Opt_udba,
4a4d8108 25345+ Opt_dio, Opt_nodio,
1facf9fc 25346+ Opt_diropq_a, Opt_diropq_w,
25347+ Opt_warn_perm, Opt_nowarn_perm,
25348+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25349+ Opt_fhsm_sec,
1facf9fc 25350+ Opt_verbose, Opt_noverbose,
25351+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25352+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25353+ Opt_dirren, Opt_nodirren,
c1595e42 25354+ Opt_acl, Opt_noacl,
1facf9fc 25355+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25356+};
25357+
25358+static match_table_t options = {
25359+ {Opt_br, "br=%s"},
25360+ {Opt_br, "br:%s"},
25361+
25362+ {Opt_add, "add=%d:%s"},
25363+ {Opt_add, "add:%d:%s"},
25364+ {Opt_add, "ins=%d:%s"},
25365+ {Opt_add, "ins:%d:%s"},
25366+ {Opt_append, "append=%s"},
25367+ {Opt_append, "append:%s"},
25368+ {Opt_prepend, "prepend=%s"},
25369+ {Opt_prepend, "prepend:%s"},
25370+
25371+ {Opt_del, "del=%s"},
25372+ {Opt_del, "del:%s"},
25373+ /* {Opt_idel, "idel:%d"}, */
25374+ {Opt_mod, "mod=%s"},
25375+ {Opt_mod, "mod:%s"},
25376+ /* {Opt_imod, "imod:%d:%s"}, */
25377+
25378+ {Opt_dirwh, "dirwh=%d"},
25379+
25380+ {Opt_xino, "xino=%s"},
25381+ {Opt_noxino, "noxino"},
25382+ {Opt_trunc_xino, "trunc_xino"},
25383+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25384+ {Opt_notrunc_xino, "notrunc_xino"},
25385+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25386+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25387+ /* {Opt_zxino, "zxino=%s"}, */
25388+ {Opt_trunc_xib, "trunc_xib"},
25389+ {Opt_notrunc_xib, "notrunc_xib"},
25390+
e49829fe 25391+#ifdef CONFIG_PROC_FS
1facf9fc 25392+ {Opt_plink, "plink"},
e49829fe
JR
25393+#else
25394+ {Opt_ignore_silent, "plink"},
25395+#endif
25396+
1facf9fc 25397+ {Opt_noplink, "noplink"},
e49829fe 25398+
1facf9fc 25399+#ifdef CONFIG_AUFS_DEBUG
25400+ {Opt_list_plink, "list_plink"},
25401+#endif
25402+
25403+ {Opt_udba, "udba=%s"},
25404+
4a4d8108
AM
25405+ {Opt_dio, "dio"},
25406+ {Opt_nodio, "nodio"},
25407+
8b6a4947
AM
25408+#ifdef CONFIG_AUFS_DIRREN
25409+ {Opt_dirren, "dirren"},
25410+ {Opt_nodirren, "nodirren"},
25411+#else
25412+ {Opt_ignore, "dirren"},
25413+ {Opt_ignore_silent, "nodirren"},
25414+#endif
25415+
076b876e
AM
25416+#ifdef CONFIG_AUFS_FHSM
25417+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25418+#else
8b6a4947 25419+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25420+#endif
25421+
1facf9fc 25422+ {Opt_diropq_a, "diropq=always"},
25423+ {Opt_diropq_a, "diropq=a"},
25424+ {Opt_diropq_w, "diropq=whiteouted"},
25425+ {Opt_diropq_w, "diropq=w"},
25426+
25427+ {Opt_warn_perm, "warn_perm"},
25428+ {Opt_nowarn_perm, "nowarn_perm"},
25429+
25430+ /* keep them temporary */
1facf9fc 25431+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25432+ {Opt_ignore, "clean_plink"},
1facf9fc 25433+
dece6358
AM
25434+#ifdef CONFIG_AUFS_SHWH
25435+ {Opt_shwh, "shwh"},
25436+#endif
25437+ {Opt_noshwh, "noshwh"},
25438+
076b876e
AM
25439+ {Opt_dirperm1, "dirperm1"},
25440+ {Opt_nodirperm1, "nodirperm1"},
25441+
1facf9fc 25442+ {Opt_verbose, "verbose"},
25443+ {Opt_verbose, "v"},
25444+ {Opt_noverbose, "noverbose"},
25445+ {Opt_noverbose, "quiet"},
25446+ {Opt_noverbose, "q"},
25447+ {Opt_noverbose, "silent"},
25448+
25449+ {Opt_sum, "sum"},
25450+ {Opt_nosum, "nosum"},
25451+ {Opt_wsum, "wsum"},
25452+
25453+ {Opt_rdcache, "rdcache=%d"},
25454+ {Opt_rdblk, "rdblk=%d"},
dece6358 25455+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25456+ {Opt_rdhash, "rdhash=%d"},
dece6358 25457+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25458+
25459+ {Opt_wbr_create, "create=%s"},
25460+ {Opt_wbr_create, "create_policy=%s"},
25461+ {Opt_wbr_copyup, "cpup=%s"},
25462+ {Opt_wbr_copyup, "copyup=%s"},
25463+ {Opt_wbr_copyup, "copyup_policy=%s"},
25464+
c1595e42
JR
25465+ /* generic VFS flag */
25466+#ifdef CONFIG_FS_POSIX_ACL
25467+ {Opt_acl, "acl"},
25468+ {Opt_noacl, "noacl"},
25469+#else
8b6a4947 25470+ {Opt_ignore, "acl"},
c1595e42
JR
25471+ {Opt_ignore_silent, "noacl"},
25472+#endif
25473+
1facf9fc 25474+ /* internal use for the scripts */
25475+ {Opt_ignore_silent, "si=%s"},
25476+
25477+ {Opt_br, "dirs=%s"},
25478+ {Opt_ignore, "debug=%d"},
25479+ {Opt_ignore, "delete=whiteout"},
25480+ {Opt_ignore, "delete=all"},
25481+ {Opt_ignore, "imap=%s"},
25482+
1308ab2a 25483+ /* temporary workaround, due to old mount(8)? */
25484+ {Opt_ignore_silent, "relatime"},
25485+
1facf9fc 25486+ {Opt_err, NULL}
25487+};
25488+
25489+/* ---------------------------------------------------------------------- */
25490+
076b876e 25491+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25492+{
076b876e
AM
25493+ struct match_token *p;
25494+
25495+ p = tbl;
25496+ while (p->pattern) {
25497+ if (p->token == val)
25498+ return p->pattern;
25499+ p++;
1facf9fc 25500+ }
25501+ BUG();
25502+ return "??";
25503+}
25504+
076b876e
AM
25505+static const char *au_optstr(int *val, match_table_t tbl)
25506+{
25507+ struct match_token *p;
25508+ int v;
25509+
25510+ v = *val;
2000de60
JR
25511+ if (!v)
25512+ goto out;
076b876e 25513+ p = tbl;
2000de60
JR
25514+ while (p->pattern) {
25515+ if (p->token
25516+ && (v & p->token) == p->token) {
076b876e
AM
25517+ *val &= ~p->token;
25518+ return p->pattern;
25519+ }
25520+ p++;
25521+ }
2000de60
JR
25522+
25523+out:
076b876e
AM
25524+ return NULL;
25525+}
25526+
1facf9fc 25527+/* ---------------------------------------------------------------------- */
25528+
1e00d052 25529+static match_table_t brperm = {
1facf9fc 25530+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25531+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25532+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
25533+ {0, NULL}
25534+};
1facf9fc 25535+
86dc4139 25536+static match_table_t brattr = {
076b876e
AM
25537+ /* general */
25538+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25539+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 25540+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 25541+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 25542+#ifdef CONFIG_AUFS_FHSM
076b876e 25543+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
25544+#endif
25545+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
25546+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25547+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25548+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25549+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25550+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25551+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 25552+#endif
076b876e
AM
25553+
25554+ /* ro/rr branch */
1e00d052 25555+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
25556+
25557+ /* rw branch */
25558+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 25559+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 25560+
1e00d052 25561+ {0, NULL}
1facf9fc 25562+};
25563+
1e00d052
AM
25564+static int br_attr_val(char *str, match_table_t table, substring_t args[])
25565+{
25566+ int attr, v;
25567+ char *p;
25568+
25569+ attr = 0;
25570+ do {
25571+ p = strchr(str, '+');
25572+ if (p)
25573+ *p = 0;
25574+ v = match_token(str, table, args);
076b876e
AM
25575+ if (v) {
25576+ if (v & AuBrAttr_CMOO_Mask)
25577+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 25578+ attr |= v;
076b876e 25579+ } else {
1e00d052
AM
25580+ if (p)
25581+ *p = '+';
0c3ec466 25582+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
25583+ break;
25584+ }
25585+ if (p)
25586+ str = p + 1;
25587+ } while (p);
25588+
25589+ return attr;
25590+}
25591+
076b876e
AM
25592+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25593+{
25594+ int sz;
25595+ const char *p;
25596+ char *q;
25597+
076b876e
AM
25598+ q = str->a;
25599+ *q = 0;
25600+ p = au_optstr(&perm, brattr);
25601+ if (p) {
25602+ sz = strlen(p);
25603+ memcpy(q, p, sz + 1);
25604+ q += sz;
25605+ } else
25606+ goto out;
25607+
25608+ do {
25609+ p = au_optstr(&perm, brattr);
25610+ if (p) {
25611+ *q++ = '+';
25612+ sz = strlen(p);
25613+ memcpy(q, p, sz + 1);
25614+ q += sz;
25615+ }
25616+ } while (p);
25617+
25618+out:
c1595e42 25619+ return q - str->a;
076b876e
AM
25620+}
25621+
4a4d8108 25622+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 25623+{
076b876e
AM
25624+ int val, bad, sz;
25625+ char *p;
1facf9fc 25626+ substring_t args[MAX_OPT_ARGS];
076b876e 25627+ au_br_perm_str_t attr;
1facf9fc 25628+
1e00d052
AM
25629+ p = strchr(perm, '+');
25630+ if (p)
25631+ *p = 0;
25632+ val = match_token(perm, brperm, args);
25633+ if (!val) {
25634+ if (p)
25635+ *p = '+';
0c3ec466 25636+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
25637+ val = AuBrPerm_RO;
25638+ goto out;
25639+ }
25640+ if (!p)
25641+ goto out;
25642+
076b876e
AM
25643+ val |= br_attr_val(p + 1, brattr, args);
25644+
25645+ bad = 0;
86dc4139 25646+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
25647+ case AuBrPerm_RO:
25648+ case AuBrPerm_RR:
076b876e
AM
25649+ bad = val & AuBrWAttr_Mask;
25650+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
25651+ break;
25652+ case AuBrPerm_RW:
076b876e
AM
25653+ bad = val & AuBrRAttr_Mask;
25654+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
25655+ break;
25656+ }
c1595e42
JR
25657+
25658+ /*
25659+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25660+ * does not treat it as an error, just warning.
25661+ * this is a tiny guard for the user operation.
25662+ */
25663+ if (val & AuBrAttr_UNPIN) {
25664+ bad |= AuBrAttr_UNPIN;
25665+ val &= ~AuBrAttr_UNPIN;
25666+ }
25667+
076b876e
AM
25668+ if (unlikely(bad)) {
25669+ sz = au_do_optstr_br_attr(&attr, bad);
25670+ AuDebugOn(!sz);
25671+ pr_warn("ignored branch attribute %s\n", attr.a);
25672+ }
1e00d052
AM
25673+
25674+out:
1facf9fc 25675+ return val;
25676+}
25677+
076b876e 25678+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 25679+{
076b876e
AM
25680+ au_br_perm_str_t attr;
25681+ const char *p;
25682+ char *q;
1e00d052
AM
25683+ int sz;
25684+
076b876e
AM
25685+ q = str->a;
25686+ p = au_optstr(&perm, brperm);
25687+ AuDebugOn(!p || !*p);
25688+ sz = strlen(p);
25689+ memcpy(q, p, sz + 1);
25690+ q += sz;
1e00d052 25691+
076b876e
AM
25692+ sz = au_do_optstr_br_attr(&attr, perm);
25693+ if (sz) {
25694+ *q++ = '+';
25695+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
25696+ }
25697+
076b876e 25698+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 25699+}
25700+
25701+/* ---------------------------------------------------------------------- */
25702+
25703+static match_table_t udbalevel = {
25704+ {AuOpt_UDBA_REVAL, "reval"},
25705+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
25706+#ifdef CONFIG_AUFS_HNOTIFY
25707+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
25708+#ifdef CONFIG_AUFS_HFSNOTIFY
25709+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 25710+#endif
1facf9fc 25711+#endif
25712+ {-1, NULL}
25713+};
25714+
4a4d8108 25715+static int noinline_for_stack udba_val(char *str)
1facf9fc 25716+{
25717+ substring_t args[MAX_OPT_ARGS];
25718+
7f207e10 25719+ return match_token(str, udbalevel, args);
1facf9fc 25720+}
25721+
25722+const char *au_optstr_udba(int udba)
25723+{
076b876e 25724+ return au_parser_pattern(udba, udbalevel);
1facf9fc 25725+}
25726+
25727+/* ---------------------------------------------------------------------- */
25728+
25729+static match_table_t au_wbr_create_policy = {
25730+ {AuWbrCreate_TDP, "tdp"},
25731+ {AuWbrCreate_TDP, "top-down-parent"},
25732+ {AuWbrCreate_RR, "rr"},
25733+ {AuWbrCreate_RR, "round-robin"},
25734+ {AuWbrCreate_MFS, "mfs"},
25735+ {AuWbrCreate_MFS, "most-free-space"},
25736+ {AuWbrCreate_MFSV, "mfs:%d"},
25737+ {AuWbrCreate_MFSV, "most-free-space:%d"},
25738+
f2c43d5f
AM
25739+ /* top-down regardless the parent, and then mfs */
25740+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
25741+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
25742+
1facf9fc 25743+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
25744+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
25745+ {AuWbrCreate_PMFS, "pmfs"},
25746+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
25747+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
25748+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 25749+
25750+ {-1, NULL}
25751+};
25752+
1facf9fc 25753+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
25754+ struct au_opt_wbr_create *create)
25755+{
25756+ int err;
25757+ unsigned long long ull;
25758+
25759+ err = 0;
a2654f78 25760+ if (!match_u64(arg, &ull))
1facf9fc 25761+ create->mfsrr_watermark = ull;
25762+ else {
4a4d8108 25763+ pr_err("bad integer in %s\n", str);
1facf9fc 25764+ err = -EINVAL;
25765+ }
25766+
25767+ return err;
25768+}
25769+
25770+static int au_wbr_mfs_sec(substring_t *arg, char *str,
25771+ struct au_opt_wbr_create *create)
25772+{
25773+ int n, err;
25774+
25775+ err = 0;
027c5e7a 25776+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 25777+ create->mfs_second = n;
25778+ else {
4a4d8108 25779+ pr_err("bad integer in %s\n", str);
1facf9fc 25780+ err = -EINVAL;
25781+ }
25782+
25783+ return err;
25784+}
25785+
4a4d8108
AM
25786+static int noinline_for_stack
25787+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 25788+{
25789+ int err, e;
25790+ substring_t args[MAX_OPT_ARGS];
25791+
25792+ err = match_token(str, au_wbr_create_policy, args);
25793+ create->wbr_create = err;
25794+ switch (err) {
25795+ case AuWbrCreate_MFSRRV:
f2c43d5f 25796+ case AuWbrCreate_TDMFSV:
392086de 25797+ case AuWbrCreate_PMFSRRV:
1facf9fc 25798+ e = au_wbr_mfs_wmark(&args[0], str, create);
25799+ if (!e)
25800+ e = au_wbr_mfs_sec(&args[1], str, create);
25801+ if (unlikely(e))
25802+ err = e;
25803+ break;
25804+ case AuWbrCreate_MFSRR:
f2c43d5f 25805+ case AuWbrCreate_TDMFS:
392086de 25806+ case AuWbrCreate_PMFSRR:
1facf9fc 25807+ e = au_wbr_mfs_wmark(&args[0], str, create);
25808+ if (unlikely(e)) {
25809+ err = e;
25810+ break;
25811+ }
25812+ /*FALLTHROUGH*/
25813+ case AuWbrCreate_MFS:
25814+ case AuWbrCreate_PMFS:
027c5e7a 25815+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 25816+ break;
25817+ case AuWbrCreate_MFSV:
25818+ case AuWbrCreate_PMFSV:
25819+ e = au_wbr_mfs_sec(&args[0], str, create);
25820+ if (unlikely(e))
25821+ err = e;
25822+ break;
25823+ }
25824+
25825+ return err;
25826+}
25827+
25828+const char *au_optstr_wbr_create(int wbr_create)
25829+{
076b876e 25830+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 25831+}
25832+
25833+static match_table_t au_wbr_copyup_policy = {
25834+ {AuWbrCopyup_TDP, "tdp"},
25835+ {AuWbrCopyup_TDP, "top-down-parent"},
25836+ {AuWbrCopyup_BUP, "bup"},
25837+ {AuWbrCopyup_BUP, "bottom-up-parent"},
25838+ {AuWbrCopyup_BU, "bu"},
25839+ {AuWbrCopyup_BU, "bottom-up"},
25840+ {-1, NULL}
25841+};
25842+
4a4d8108 25843+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 25844+{
25845+ substring_t args[MAX_OPT_ARGS];
25846+
25847+ return match_token(str, au_wbr_copyup_policy, args);
25848+}
25849+
25850+const char *au_optstr_wbr_copyup(int wbr_copyup)
25851+{
076b876e 25852+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 25853+}
25854+
25855+/* ---------------------------------------------------------------------- */
25856+
25857+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
25858+
25859+static void dump_opts(struct au_opts *opts)
25860+{
25861+#ifdef CONFIG_AUFS_DEBUG
25862+ /* reduce stack space */
25863+ union {
25864+ struct au_opt_add *add;
25865+ struct au_opt_del *del;
25866+ struct au_opt_mod *mod;
25867+ struct au_opt_xino *xino;
25868+ struct au_opt_xino_itrunc *xino_itrunc;
25869+ struct au_opt_wbr_create *create;
25870+ } u;
25871+ struct au_opt *opt;
25872+
25873+ opt = opts->opt;
25874+ while (opt->type != Opt_tail) {
25875+ switch (opt->type) {
25876+ case Opt_add:
25877+ u.add = &opt->add;
25878+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
25879+ u.add->bindex, u.add->pathname, u.add->perm,
25880+ u.add->path.dentry);
25881+ break;
25882+ case Opt_del:
25883+ case Opt_idel:
25884+ u.del = &opt->del;
25885+ AuDbg("del {%s, %p}\n",
25886+ u.del->pathname, u.del->h_path.dentry);
25887+ break;
25888+ case Opt_mod:
25889+ case Opt_imod:
25890+ u.mod = &opt->mod;
25891+ AuDbg("mod {%s, 0x%x, %p}\n",
25892+ u.mod->path, u.mod->perm, u.mod->h_root);
25893+ break;
25894+ case Opt_append:
25895+ u.add = &opt->add;
25896+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
25897+ u.add->bindex, u.add->pathname, u.add->perm,
25898+ u.add->path.dentry);
25899+ break;
25900+ case Opt_prepend:
25901+ u.add = &opt->add;
25902+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
25903+ u.add->bindex, u.add->pathname, u.add->perm,
25904+ u.add->path.dentry);
25905+ break;
25906+ case Opt_dirwh:
25907+ AuDbg("dirwh %d\n", opt->dirwh);
25908+ break;
25909+ case Opt_rdcache:
25910+ AuDbg("rdcache %d\n", opt->rdcache);
25911+ break;
25912+ case Opt_rdblk:
25913+ AuDbg("rdblk %u\n", opt->rdblk);
25914+ break;
dece6358
AM
25915+ case Opt_rdblk_def:
25916+ AuDbg("rdblk_def\n");
25917+ break;
1facf9fc 25918+ case Opt_rdhash:
25919+ AuDbg("rdhash %u\n", opt->rdhash);
25920+ break;
dece6358
AM
25921+ case Opt_rdhash_def:
25922+ AuDbg("rdhash_def\n");
25923+ break;
1facf9fc 25924+ case Opt_xino:
25925+ u.xino = &opt->xino;
523b37e3 25926+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 25927+ break;
25928+ case Opt_trunc_xino:
25929+ AuLabel(trunc_xino);
25930+ break;
25931+ case Opt_notrunc_xino:
25932+ AuLabel(notrunc_xino);
25933+ break;
25934+ case Opt_trunc_xino_path:
25935+ case Opt_itrunc_xino:
25936+ u.xino_itrunc = &opt->xino_itrunc;
25937+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
25938+ break;
1facf9fc 25939+ case Opt_noxino:
25940+ AuLabel(noxino);
25941+ break;
25942+ case Opt_trunc_xib:
25943+ AuLabel(trunc_xib);
25944+ break;
25945+ case Opt_notrunc_xib:
25946+ AuLabel(notrunc_xib);
25947+ break;
dece6358
AM
25948+ case Opt_shwh:
25949+ AuLabel(shwh);
25950+ break;
25951+ case Opt_noshwh:
25952+ AuLabel(noshwh);
25953+ break;
076b876e
AM
25954+ case Opt_dirperm1:
25955+ AuLabel(dirperm1);
25956+ break;
25957+ case Opt_nodirperm1:
25958+ AuLabel(nodirperm1);
25959+ break;
1facf9fc 25960+ case Opt_plink:
25961+ AuLabel(plink);
25962+ break;
25963+ case Opt_noplink:
25964+ AuLabel(noplink);
25965+ break;
25966+ case Opt_list_plink:
25967+ AuLabel(list_plink);
25968+ break;
25969+ case Opt_udba:
25970+ AuDbg("udba %d, %s\n",
25971+ opt->udba, au_optstr_udba(opt->udba));
25972+ break;
4a4d8108
AM
25973+ case Opt_dio:
25974+ AuLabel(dio);
25975+ break;
25976+ case Opt_nodio:
25977+ AuLabel(nodio);
25978+ break;
1facf9fc 25979+ case Opt_diropq_a:
25980+ AuLabel(diropq_a);
25981+ break;
25982+ case Opt_diropq_w:
25983+ AuLabel(diropq_w);
25984+ break;
25985+ case Opt_warn_perm:
25986+ AuLabel(warn_perm);
25987+ break;
25988+ case Opt_nowarn_perm:
25989+ AuLabel(nowarn_perm);
25990+ break;
1facf9fc 25991+ case Opt_verbose:
25992+ AuLabel(verbose);
25993+ break;
25994+ case Opt_noverbose:
25995+ AuLabel(noverbose);
25996+ break;
25997+ case Opt_sum:
25998+ AuLabel(sum);
25999+ break;
26000+ case Opt_nosum:
26001+ AuLabel(nosum);
26002+ break;
26003+ case Opt_wsum:
26004+ AuLabel(wsum);
26005+ break;
26006+ case Opt_wbr_create:
26007+ u.create = &opt->wbr_create;
26008+ AuDbg("create %d, %s\n", u.create->wbr_create,
26009+ au_optstr_wbr_create(u.create->wbr_create));
26010+ switch (u.create->wbr_create) {
26011+ case AuWbrCreate_MFSV:
26012+ case AuWbrCreate_PMFSV:
26013+ AuDbg("%d sec\n", u.create->mfs_second);
26014+ break;
26015+ case AuWbrCreate_MFSRR:
f2c43d5f 26016+ case AuWbrCreate_TDMFS:
1facf9fc 26017+ AuDbg("%llu watermark\n",
26018+ u.create->mfsrr_watermark);
26019+ break;
26020+ case AuWbrCreate_MFSRRV:
f2c43d5f 26021+ case AuWbrCreate_TDMFSV:
392086de 26022+ case AuWbrCreate_PMFSRRV:
1facf9fc 26023+ AuDbg("%llu watermark, %d sec\n",
26024+ u.create->mfsrr_watermark,
26025+ u.create->mfs_second);
26026+ break;
26027+ }
26028+ break;
26029+ case Opt_wbr_copyup:
26030+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26031+ au_optstr_wbr_copyup(opt->wbr_copyup));
26032+ break;
076b876e
AM
26033+ case Opt_fhsm_sec:
26034+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26035+ break;
8b6a4947
AM
26036+ case Opt_dirren:
26037+ AuLabel(dirren);
26038+ break;
26039+ case Opt_nodirren:
26040+ AuLabel(nodirren);
26041+ break;
c1595e42
JR
26042+ case Opt_acl:
26043+ AuLabel(acl);
26044+ break;
26045+ case Opt_noacl:
26046+ AuLabel(noacl);
26047+ break;
1facf9fc 26048+ default:
26049+ BUG();
26050+ }
26051+ opt++;
26052+ }
26053+#endif
26054+}
26055+
26056+void au_opts_free(struct au_opts *opts)
26057+{
26058+ struct au_opt *opt;
26059+
26060+ opt = opts->opt;
26061+ while (opt->type != Opt_tail) {
26062+ switch (opt->type) {
26063+ case Opt_add:
26064+ case Opt_append:
26065+ case Opt_prepend:
26066+ path_put(&opt->add.path);
26067+ break;
26068+ case Opt_del:
26069+ case Opt_idel:
26070+ path_put(&opt->del.h_path);
26071+ break;
26072+ case Opt_mod:
26073+ case Opt_imod:
26074+ dput(opt->mod.h_root);
26075+ break;
26076+ case Opt_xino:
26077+ fput(opt->xino.file);
26078+ break;
26079+ }
26080+ opt++;
26081+ }
26082+}
26083+
26084+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26085+ aufs_bindex_t bindex)
26086+{
26087+ int err;
26088+ struct au_opt_add *add = &opt->add;
26089+ char *p;
26090+
26091+ add->bindex = bindex;
1e00d052 26092+ add->perm = AuBrPerm_RO;
1facf9fc 26093+ add->pathname = opt_str;
26094+ p = strchr(opt_str, '=');
26095+ if (p) {
26096+ *p++ = 0;
26097+ if (*p)
26098+ add->perm = br_perm_val(p);
26099+ }
26100+
26101+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26102+ if (!err) {
26103+ if (!p) {
26104+ add->perm = AuBrPerm_RO;
26105+ if (au_test_fs_rr(add->path.dentry->d_sb))
26106+ add->perm = AuBrPerm_RR;
2121bcd9 26107+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 26108+ add->perm = AuBrPerm_RW;
26109+ }
26110+ opt->type = Opt_add;
26111+ goto out;
26112+ }
4a4d8108 26113+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26114+ err = -EINVAL;
26115+
4f0767ce 26116+out:
1facf9fc 26117+ return err;
26118+}
26119+
26120+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26121+{
26122+ int err;
26123+
26124+ del->pathname = args[0].from;
26125+ AuDbg("del path %s\n", del->pathname);
26126+
26127+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26128+ if (unlikely(err))
4a4d8108 26129+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26130+
26131+ return err;
26132+}
26133+
26134+#if 0 /* reserved for future use */
26135+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26136+ struct au_opt_del *del, substring_t args[])
26137+{
26138+ int err;
26139+ struct dentry *root;
26140+
26141+ err = -EINVAL;
26142+ root = sb->s_root;
26143+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26144+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26145+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26146+ goto out;
26147+ }
26148+
26149+ err = 0;
26150+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26151+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26152+
4f0767ce 26153+out:
1facf9fc 26154+ aufs_read_unlock(root, !AuLock_IR);
26155+ return err;
26156+}
26157+#endif
26158+
4a4d8108
AM
26159+static int noinline_for_stack
26160+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26161+{
26162+ int err;
26163+ struct path path;
26164+ char *p;
26165+
26166+ err = -EINVAL;
26167+ mod->path = args[0].from;
26168+ p = strchr(mod->path, '=');
26169+ if (unlikely(!p)) {
4a4d8108 26170+ pr_err("no permssion %s\n", args[0].from);
1facf9fc 26171+ goto out;
26172+ }
26173+
26174+ *p++ = 0;
26175+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26176+ if (unlikely(err)) {
4a4d8108 26177+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26178+ goto out;
26179+ }
26180+
26181+ mod->perm = br_perm_val(p);
26182+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26183+ mod->h_root = dget(path.dentry);
26184+ path_put(&path);
26185+
4f0767ce 26186+out:
1facf9fc 26187+ return err;
26188+}
26189+
26190+#if 0 /* reserved for future use */
26191+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26192+ struct au_opt_mod *mod, substring_t args[])
26193+{
26194+ int err;
26195+ struct dentry *root;
26196+
26197+ err = -EINVAL;
26198+ root = sb->s_root;
26199+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26200+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26201+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26202+ goto out;
26203+ }
26204+
26205+ err = 0;
26206+ mod->perm = br_perm_val(args[1].from);
26207+ AuDbg("mod path %s, perm 0x%x, %s\n",
26208+ mod->path, mod->perm, args[1].from);
26209+ mod->h_root = dget(au_h_dptr(root, bindex));
26210+
4f0767ce 26211+out:
1facf9fc 26212+ aufs_read_unlock(root, !AuLock_IR);
26213+ return err;
26214+}
26215+#endif
26216+
26217+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26218+ substring_t args[])
26219+{
26220+ int err;
26221+ struct file *file;
26222+
26223+ file = au_xino_create(sb, args[0].from, /*silent*/0);
26224+ err = PTR_ERR(file);
26225+ if (IS_ERR(file))
26226+ goto out;
26227+
26228+ err = -EINVAL;
2000de60 26229+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26230+ fput(file);
4a4d8108 26231+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26232+ goto out;
26233+ }
26234+
26235+ err = 0;
26236+ xino->file = file;
26237+ xino->path = args[0].from;
26238+
4f0767ce 26239+out:
1facf9fc 26240+ return err;
26241+}
26242+
4a4d8108
AM
26243+static int noinline_for_stack
26244+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26245+ struct au_opt_xino_itrunc *xino_itrunc,
26246+ substring_t args[])
1facf9fc 26247+{
26248+ int err;
5afbbe0d 26249+ aufs_bindex_t bbot, bindex;
1facf9fc 26250+ struct path path;
26251+ struct dentry *root;
26252+
26253+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26254+ if (unlikely(err)) {
4a4d8108 26255+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26256+ goto out;
26257+ }
26258+
26259+ xino_itrunc->bindex = -1;
26260+ root = sb->s_root;
26261+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26262+ bbot = au_sbbot(sb);
26263+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26264+ if (au_h_dptr(root, bindex) == path.dentry) {
26265+ xino_itrunc->bindex = bindex;
26266+ break;
26267+ }
26268+ }
26269+ aufs_read_unlock(root, !AuLock_IR);
26270+ path_put(&path);
26271+
26272+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26273+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26274+ err = -EINVAL;
26275+ }
26276+
4f0767ce 26277+out:
1facf9fc 26278+ return err;
26279+}
26280+
26281+/* called without aufs lock */
26282+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26283+{
26284+ int err, n, token;
26285+ aufs_bindex_t bindex;
26286+ unsigned char skipped;
26287+ struct dentry *root;
26288+ struct au_opt *opt, *opt_tail;
26289+ char *opt_str;
26290+ /* reduce the stack space */
26291+ union {
26292+ struct au_opt_xino_itrunc *xino_itrunc;
26293+ struct au_opt_wbr_create *create;
26294+ } u;
26295+ struct {
26296+ substring_t args[MAX_OPT_ARGS];
26297+ } *a;
26298+
26299+ err = -ENOMEM;
26300+ a = kmalloc(sizeof(*a), GFP_NOFS);
26301+ if (unlikely(!a))
26302+ goto out;
26303+
26304+ root = sb->s_root;
26305+ err = 0;
26306+ bindex = 0;
26307+ opt = opts->opt;
26308+ opt_tail = opt + opts->max_opt - 1;
26309+ opt->type = Opt_tail;
26310+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26311+ err = -EINVAL;
26312+ skipped = 0;
26313+ token = match_token(opt_str, options, a->args);
26314+ switch (token) {
26315+ case Opt_br:
26316+ err = 0;
26317+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26318+ && *opt_str) {
26319+ err = opt_add(opt, opt_str, opts->sb_flags,
26320+ bindex++);
26321+ if (unlikely(!err && ++opt > opt_tail)) {
26322+ err = -E2BIG;
26323+ break;
26324+ }
26325+ opt->type = Opt_tail;
26326+ skipped = 1;
26327+ }
26328+ break;
26329+ case Opt_add:
26330+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26331+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26332+ break;
26333+ }
26334+ bindex = n;
26335+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26336+ bindex);
26337+ if (!err)
26338+ opt->type = token;
26339+ break;
26340+ case Opt_append:
26341+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26342+ /*dummy bindex*/1);
26343+ if (!err)
26344+ opt->type = token;
26345+ break;
26346+ case Opt_prepend:
26347+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26348+ /*bindex*/0);
26349+ if (!err)
26350+ opt->type = token;
26351+ break;
26352+ case Opt_del:
26353+ err = au_opts_parse_del(&opt->del, a->args);
26354+ if (!err)
26355+ opt->type = token;
26356+ break;
26357+#if 0 /* reserved for future use */
26358+ case Opt_idel:
26359+ del->pathname = "(indexed)";
26360+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26361+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26362+ break;
26363+ }
26364+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26365+ if (!err)
26366+ opt->type = token;
26367+ break;
26368+#endif
26369+ case Opt_mod:
26370+ err = au_opts_parse_mod(&opt->mod, a->args);
26371+ if (!err)
26372+ opt->type = token;
26373+ break;
26374+#ifdef IMOD /* reserved for future use */
26375+ case Opt_imod:
26376+ u.mod->path = "(indexed)";
26377+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26378+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26379+ break;
26380+ }
26381+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26382+ if (!err)
26383+ opt->type = token;
26384+ break;
26385+#endif
26386+ case Opt_xino:
26387+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26388+ if (!err)
26389+ opt->type = token;
26390+ break;
26391+
26392+ case Opt_trunc_xino_path:
26393+ err = au_opts_parse_xino_itrunc_path
26394+ (sb, &opt->xino_itrunc, a->args);
26395+ if (!err)
26396+ opt->type = token;
26397+ break;
26398+
26399+ case Opt_itrunc_xino:
26400+ u.xino_itrunc = &opt->xino_itrunc;
26401+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26402+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26403+ break;
26404+ }
26405+ u.xino_itrunc->bindex = n;
26406+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26407+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26408+ pr_err("out of bounds, %d\n", n);
1facf9fc 26409+ aufs_read_unlock(root, !AuLock_IR);
26410+ break;
26411+ }
26412+ aufs_read_unlock(root, !AuLock_IR);
26413+ err = 0;
26414+ opt->type = token;
26415+ break;
26416+
26417+ case Opt_dirwh:
26418+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26419+ break;
26420+ err = 0;
26421+ opt->type = token;
26422+ break;
26423+
26424+ case Opt_rdcache:
027c5e7a
AM
26425+ if (unlikely(match_int(&a->args[0], &n))) {
26426+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26427+ break;
027c5e7a
AM
26428+ }
26429+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26430+ pr_err("rdcache must be smaller than %d\n",
26431+ AUFS_RDCACHE_MAX);
26432+ break;
26433+ }
26434+ opt->rdcache = n;
1facf9fc 26435+ err = 0;
26436+ opt->type = token;
26437+ break;
26438+ case Opt_rdblk:
26439+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26440+ || n < 0
1facf9fc 26441+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26442+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26443+ break;
26444+ }
1308ab2a 26445+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26446+ pr_err("rdblk must be larger than %d\n",
26447+ NAME_MAX);
1facf9fc 26448+ break;
26449+ }
26450+ opt->rdblk = n;
26451+ err = 0;
26452+ opt->type = token;
26453+ break;
26454+ case Opt_rdhash:
26455+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26456+ || n < 0
1facf9fc 26457+ || n * sizeof(struct hlist_head)
26458+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26459+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26460+ break;
26461+ }
26462+ opt->rdhash = n;
26463+ err = 0;
26464+ opt->type = token;
26465+ break;
26466+
26467+ case Opt_trunc_xino:
26468+ case Opt_notrunc_xino:
26469+ case Opt_noxino:
26470+ case Opt_trunc_xib:
26471+ case Opt_notrunc_xib:
dece6358
AM
26472+ case Opt_shwh:
26473+ case Opt_noshwh:
076b876e
AM
26474+ case Opt_dirperm1:
26475+ case Opt_nodirperm1:
1facf9fc 26476+ case Opt_plink:
26477+ case Opt_noplink:
26478+ case Opt_list_plink:
4a4d8108
AM
26479+ case Opt_dio:
26480+ case Opt_nodio:
1facf9fc 26481+ case Opt_diropq_a:
26482+ case Opt_diropq_w:
26483+ case Opt_warn_perm:
26484+ case Opt_nowarn_perm:
1facf9fc 26485+ case Opt_verbose:
26486+ case Opt_noverbose:
26487+ case Opt_sum:
26488+ case Opt_nosum:
26489+ case Opt_wsum:
dece6358
AM
26490+ case Opt_rdblk_def:
26491+ case Opt_rdhash_def:
8b6a4947
AM
26492+ case Opt_dirren:
26493+ case Opt_nodirren:
c1595e42
JR
26494+ case Opt_acl:
26495+ case Opt_noacl:
1facf9fc 26496+ err = 0;
26497+ opt->type = token;
26498+ break;
26499+
26500+ case Opt_udba:
26501+ opt->udba = udba_val(a->args[0].from);
26502+ if (opt->udba >= 0) {
26503+ err = 0;
26504+ opt->type = token;
26505+ } else
4a4d8108 26506+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26507+ break;
26508+
26509+ case Opt_wbr_create:
26510+ u.create = &opt->wbr_create;
26511+ u.create->wbr_create
26512+ = au_wbr_create_val(a->args[0].from, u.create);
26513+ if (u.create->wbr_create >= 0) {
26514+ err = 0;
26515+ opt->type = token;
26516+ } else
4a4d8108 26517+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26518+ break;
26519+ case Opt_wbr_copyup:
26520+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26521+ if (opt->wbr_copyup >= 0) {
26522+ err = 0;
26523+ opt->type = token;
26524+ } else
4a4d8108 26525+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26526+ break;
26527+
076b876e
AM
26528+ case Opt_fhsm_sec:
26529+ if (unlikely(match_int(&a->args[0], &n)
26530+ || n < 0)) {
26531+ pr_err("bad integer in %s\n", opt_str);
26532+ break;
26533+ }
26534+ if (sysaufs_brs) {
26535+ opt->fhsm_second = n;
26536+ opt->type = token;
26537+ } else
26538+ pr_warn("ignored %s\n", opt_str);
26539+ err = 0;
26540+ break;
26541+
1facf9fc 26542+ case Opt_ignore:
0c3ec466 26543+ pr_warn("ignored %s\n", opt_str);
1facf9fc 26544+ /*FALLTHROUGH*/
26545+ case Opt_ignore_silent:
26546+ skipped = 1;
26547+ err = 0;
26548+ break;
26549+ case Opt_err:
4a4d8108 26550+ pr_err("unknown option %s\n", opt_str);
1facf9fc 26551+ break;
26552+ }
26553+
26554+ if (!err && !skipped) {
26555+ if (unlikely(++opt > opt_tail)) {
26556+ err = -E2BIG;
26557+ opt--;
26558+ opt->type = Opt_tail;
26559+ break;
26560+ }
26561+ opt->type = Opt_tail;
26562+ }
26563+ }
26564+
1c60b727 26565+ kfree(a);
1facf9fc 26566+ dump_opts(opts);
26567+ if (unlikely(err))
26568+ au_opts_free(opts);
26569+
4f0767ce 26570+out:
1facf9fc 26571+ return err;
26572+}
26573+
26574+static int au_opt_wbr_create(struct super_block *sb,
26575+ struct au_opt_wbr_create *create)
26576+{
26577+ int err;
26578+ struct au_sbinfo *sbinfo;
26579+
dece6358
AM
26580+ SiMustWriteLock(sb);
26581+
1facf9fc 26582+ err = 1; /* handled */
26583+ sbinfo = au_sbi(sb);
26584+ if (sbinfo->si_wbr_create_ops->fin) {
26585+ err = sbinfo->si_wbr_create_ops->fin(sb);
26586+ if (!err)
26587+ err = 1;
26588+ }
26589+
26590+ sbinfo->si_wbr_create = create->wbr_create;
26591+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26592+ switch (create->wbr_create) {
26593+ case AuWbrCreate_MFSRRV:
26594+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
26595+ case AuWbrCreate_TDMFS:
26596+ case AuWbrCreate_TDMFSV:
392086de
AM
26597+ case AuWbrCreate_PMFSRR:
26598+ case AuWbrCreate_PMFSRRV:
1facf9fc 26599+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26600+ /*FALLTHROUGH*/
26601+ case AuWbrCreate_MFS:
26602+ case AuWbrCreate_MFSV:
26603+ case AuWbrCreate_PMFS:
26604+ case AuWbrCreate_PMFSV:
e49829fe
JR
26605+ sbinfo->si_wbr_mfs.mfs_expire
26606+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 26607+ break;
26608+ }
26609+
26610+ if (sbinfo->si_wbr_create_ops->init)
26611+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26612+
26613+ return err;
26614+}
26615+
26616+/*
26617+ * returns,
26618+ * plus: processed without an error
26619+ * zero: unprocessed
26620+ */
26621+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26622+ struct au_opts *opts)
26623+{
26624+ int err;
26625+ struct au_sbinfo *sbinfo;
26626+
dece6358
AM
26627+ SiMustWriteLock(sb);
26628+
1facf9fc 26629+ err = 1; /* handled */
26630+ sbinfo = au_sbi(sb);
26631+ switch (opt->type) {
26632+ case Opt_udba:
26633+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26634+ sbinfo->si_mntflags |= opt->udba;
26635+ opts->given_udba |= opt->udba;
26636+ break;
26637+
26638+ case Opt_plink:
26639+ au_opt_set(sbinfo->si_mntflags, PLINK);
26640+ break;
26641+ case Opt_noplink:
26642+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 26643+ au_plink_put(sb, /*verbose*/1);
1facf9fc 26644+ au_opt_clr(sbinfo->si_mntflags, PLINK);
26645+ break;
26646+ case Opt_list_plink:
26647+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26648+ au_plink_list(sb);
26649+ break;
26650+
4a4d8108
AM
26651+ case Opt_dio:
26652+ au_opt_set(sbinfo->si_mntflags, DIO);
26653+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26654+ break;
26655+ case Opt_nodio:
26656+ au_opt_clr(sbinfo->si_mntflags, DIO);
26657+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26658+ break;
26659+
076b876e
AM
26660+ case Opt_fhsm_sec:
26661+ au_fhsm_set(sbinfo, opt->fhsm_second);
26662+ break;
26663+
1facf9fc 26664+ case Opt_diropq_a:
26665+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26666+ break;
26667+ case Opt_diropq_w:
26668+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26669+ break;
26670+
26671+ case Opt_warn_perm:
26672+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26673+ break;
26674+ case Opt_nowarn_perm:
26675+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26676+ break;
26677+
1facf9fc 26678+ case Opt_verbose:
26679+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
26680+ break;
26681+ case Opt_noverbose:
26682+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26683+ break;
26684+
26685+ case Opt_sum:
26686+ au_opt_set(sbinfo->si_mntflags, SUM);
26687+ break;
26688+ case Opt_wsum:
26689+ au_opt_clr(sbinfo->si_mntflags, SUM);
26690+ au_opt_set(sbinfo->si_mntflags, SUM_W);
26691+ case Opt_nosum:
26692+ au_opt_clr(sbinfo->si_mntflags, SUM);
26693+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
26694+ break;
26695+
26696+ case Opt_wbr_create:
26697+ err = au_opt_wbr_create(sb, &opt->wbr_create);
26698+ break;
26699+ case Opt_wbr_copyup:
26700+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
26701+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26702+ break;
26703+
26704+ case Opt_dirwh:
26705+ sbinfo->si_dirwh = opt->dirwh;
26706+ break;
26707+
26708+ case Opt_rdcache:
e49829fe
JR
26709+ sbinfo->si_rdcache
26710+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 26711+ break;
26712+ case Opt_rdblk:
26713+ sbinfo->si_rdblk = opt->rdblk;
26714+ break;
dece6358
AM
26715+ case Opt_rdblk_def:
26716+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26717+ break;
1facf9fc 26718+ case Opt_rdhash:
26719+ sbinfo->si_rdhash = opt->rdhash;
26720+ break;
dece6358
AM
26721+ case Opt_rdhash_def:
26722+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26723+ break;
26724+
26725+ case Opt_shwh:
26726+ au_opt_set(sbinfo->si_mntflags, SHWH);
26727+ break;
26728+ case Opt_noshwh:
26729+ au_opt_clr(sbinfo->si_mntflags, SHWH);
26730+ break;
1facf9fc 26731+
076b876e
AM
26732+ case Opt_dirperm1:
26733+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
26734+ break;
26735+ case Opt_nodirperm1:
26736+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
26737+ break;
26738+
1facf9fc 26739+ case Opt_trunc_xino:
26740+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
26741+ break;
26742+ case Opt_notrunc_xino:
26743+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
26744+ break;
26745+
26746+ case Opt_trunc_xino_path:
26747+ case Opt_itrunc_xino:
26748+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
26749+ if (!err)
26750+ err = 1;
26751+ break;
26752+
26753+ case Opt_trunc_xib:
26754+ au_fset_opts(opts->flags, TRUNC_XIB);
26755+ break;
26756+ case Opt_notrunc_xib:
26757+ au_fclr_opts(opts->flags, TRUNC_XIB);
26758+ break;
26759+
8b6a4947
AM
26760+ case Opt_dirren:
26761+ err = 1;
26762+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26763+ err = au_dr_opt_set(sb);
26764+ if (!err)
26765+ err = 1;
26766+ }
26767+ if (err == 1)
26768+ au_opt_set(sbinfo->si_mntflags, DIRREN);
26769+ break;
26770+ case Opt_nodirren:
26771+ err = 1;
26772+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26773+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
26774+ DR_FLUSHED));
26775+ if (!err)
26776+ err = 1;
26777+ }
26778+ if (err == 1)
26779+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
26780+ break;
26781+
c1595e42 26782+ case Opt_acl:
2121bcd9 26783+ sb->s_flags |= SB_POSIXACL;
c1595e42
JR
26784+ break;
26785+ case Opt_noacl:
2121bcd9 26786+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
26787+ break;
26788+
1facf9fc 26789+ default:
26790+ err = 0;
26791+ break;
26792+ }
26793+
26794+ return err;
26795+}
26796+
26797+/*
26798+ * returns tri-state.
26799+ * plus: processed without an error
26800+ * zero: unprocessed
26801+ * minus: error
26802+ */
26803+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
26804+ struct au_opts *opts)
26805+{
26806+ int err, do_refresh;
26807+
26808+ err = 0;
26809+ switch (opt->type) {
26810+ case Opt_append:
5afbbe0d 26811+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 26812+ if (opt->add.bindex < 0)
26813+ opt->add.bindex = 0;
26814+ goto add;
26815+ case Opt_prepend:
26816+ opt->add.bindex = 0;
f6b6e03d 26817+ add: /* indented label */
1facf9fc 26818+ case Opt_add:
26819+ err = au_br_add(sb, &opt->add,
26820+ au_ftest_opts(opts->flags, REMOUNT));
26821+ if (!err) {
26822+ err = 1;
027c5e7a 26823+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26824+ }
26825+ break;
26826+
26827+ case Opt_del:
26828+ case Opt_idel:
26829+ err = au_br_del(sb, &opt->del,
26830+ au_ftest_opts(opts->flags, REMOUNT));
26831+ if (!err) {
26832+ err = 1;
26833+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 26834+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26835+ }
26836+ break;
26837+
26838+ case Opt_mod:
26839+ case Opt_imod:
26840+ err = au_br_mod(sb, &opt->mod,
26841+ au_ftest_opts(opts->flags, REMOUNT),
26842+ &do_refresh);
26843+ if (!err) {
26844+ err = 1;
027c5e7a
AM
26845+ if (do_refresh)
26846+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26847+ }
26848+ break;
26849+ }
1facf9fc 26850+ return err;
26851+}
26852+
26853+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
26854+ struct au_opt_xino **opt_xino,
26855+ struct au_opts *opts)
26856+{
26857+ int err;
5afbbe0d 26858+ aufs_bindex_t bbot, bindex;
1facf9fc 26859+ struct dentry *root, *parent, *h_root;
26860+
26861+ err = 0;
26862+ switch (opt->type) {
26863+ case Opt_xino:
26864+ err = au_xino_set(sb, &opt->xino,
26865+ !!au_ftest_opts(opts->flags, REMOUNT));
26866+ if (unlikely(err))
26867+ break;
26868+
26869+ *opt_xino = &opt->xino;
26870+ au_xino_brid_set(sb, -1);
26871+
26872+ /* safe d_parent access */
2000de60 26873+ parent = opt->xino.file->f_path.dentry->d_parent;
1facf9fc 26874+ root = sb->s_root;
5afbbe0d
AM
26875+ bbot = au_sbbot(sb);
26876+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26877+ h_root = au_h_dptr(root, bindex);
26878+ if (h_root == parent) {
26879+ au_xino_brid_set(sb, au_sbr_id(sb, bindex));
26880+ break;
26881+ }
26882+ }
26883+ break;
26884+
26885+ case Opt_noxino:
26886+ au_xino_clr(sb);
26887+ au_xino_brid_set(sb, -1);
26888+ *opt_xino = (void *)-1;
26889+ break;
26890+ }
26891+
26892+ return err;
26893+}
26894+
26895+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
26896+ unsigned int pending)
26897+{
076b876e 26898+ int err, fhsm;
5afbbe0d 26899+ aufs_bindex_t bindex, bbot;
79b8bda9 26900+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 26901+ struct au_branch *br;
26902+ struct au_wbr *wbr;
79b8bda9 26903+ struct dentry *root, *dentry;
1facf9fc 26904+ struct inode *dir, *h_dir;
26905+ struct au_sbinfo *sbinfo;
26906+ struct au_hinode *hdir;
26907+
dece6358
AM
26908+ SiMustAnyLock(sb);
26909+
1facf9fc 26910+ sbinfo = au_sbi(sb);
26911+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
26912+
2121bcd9 26913+ if (!(sb_flags & SB_RDONLY)) {
dece6358 26914+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 26915+ pr_warn("first branch should be rw\n");
dece6358 26916+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 26917+ pr_warn_once("shwh should be used with ro\n");
dece6358 26918+ }
1facf9fc 26919+
4a4d8108 26920+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 26921+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 26922+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 26923+
076b876e 26924+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
26925+ pr_warn_once("dirperm1 breaks the protection"
26926+ " by the permission bits on the lower branch\n");
076b876e 26927+
1facf9fc 26928+ err = 0;
076b876e 26929+ fhsm = 0;
1facf9fc 26930+ root = sb->s_root;
5527c038 26931+ dir = d_inode(root);
1facf9fc 26932+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
26933+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
26934+ UDBA_NONE);
5afbbe0d
AM
26935+ bbot = au_sbbot(sb);
26936+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 26937+ skip = 0;
26938+ h_dir = au_h_iptr(dir, bindex);
26939+ br = au_sbr(sb, bindex);
1facf9fc 26940+
c1595e42
JR
26941+ if ((br->br_perm & AuBrAttr_ICEX)
26942+ && !h_dir->i_op->listxattr)
26943+ br->br_perm &= ~AuBrAttr_ICEX;
26944+#if 0
26945+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 26946+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
26947+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
26948+#endif
26949+
26950+ do_free = 0;
1facf9fc 26951+ wbr = br->br_wbr;
26952+ if (wbr)
26953+ wbr_wh_read_lock(wbr);
26954+
1e00d052 26955+ if (!au_br_writable(br->br_perm)) {
1facf9fc 26956+ do_free = !!wbr;
26957+ skip = (!wbr
26958+ || (!wbr->wbr_whbase
26959+ && !wbr->wbr_plink
26960+ && !wbr->wbr_orph));
1e00d052 26961+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 26962+ /* skip = (!br->br_whbase && !br->br_orph); */
26963+ skip = (!wbr || !wbr->wbr_whbase);
26964+ if (skip && wbr) {
26965+ if (do_plink)
26966+ skip = !!wbr->wbr_plink;
26967+ else
26968+ skip = !wbr->wbr_plink;
26969+ }
1e00d052 26970+ } else {
1facf9fc 26971+ /* skip = (br->br_whbase && br->br_ohph); */
26972+ skip = (wbr && wbr->wbr_whbase);
26973+ if (skip) {
26974+ if (do_plink)
26975+ skip = !!wbr->wbr_plink;
26976+ else
26977+ skip = !wbr->wbr_plink;
26978+ }
1facf9fc 26979+ }
26980+ if (wbr)
26981+ wbr_wh_read_unlock(wbr);
26982+
79b8bda9
AM
26983+ if (can_no_dreval) {
26984+ dentry = br->br_path.dentry;
26985+ spin_lock(&dentry->d_lock);
26986+ if (dentry->d_flags &
26987+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
26988+ can_no_dreval = 0;
26989+ spin_unlock(&dentry->d_lock);
26990+ }
26991+
076b876e
AM
26992+ if (au_br_fhsm(br->br_perm)) {
26993+ fhsm++;
26994+ AuDebugOn(!br->br_fhsm);
26995+ }
26996+
1facf9fc 26997+ if (skip)
26998+ continue;
26999+
27000+ hdir = au_hi(dir, bindex);
5afbbe0d 27001+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27002+ if (wbr)
27003+ wbr_wh_write_lock(wbr);
86dc4139 27004+ err = au_wh_init(br, sb);
1facf9fc 27005+ if (wbr)
27006+ wbr_wh_write_unlock(wbr);
5afbbe0d 27007+ au_hn_inode_unlock(hdir);
1facf9fc 27008+
27009+ if (!err && do_free) {
1c60b727 27010+ kfree(wbr);
1facf9fc 27011+ br->br_wbr = NULL;
27012+ }
27013+ }
27014+
79b8bda9
AM
27015+ if (can_no_dreval)
27016+ au_fset_si(sbinfo, NO_DREVAL);
27017+ else
27018+ au_fclr_si(sbinfo, NO_DREVAL);
27019+
c1595e42 27020+ if (fhsm >= 2) {
076b876e 27021+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27022+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27023+ br = au_sbr(sb, bindex);
27024+ if (au_br_fhsm(br->br_perm)) {
27025+ au_fhsm_set_bottom(sb, bindex);
27026+ break;
27027+ }
27028+ }
27029+ } else {
076b876e 27030+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27031+ au_fhsm_set_bottom(sb, -1);
27032+ }
076b876e 27033+
1facf9fc 27034+ return err;
27035+}
27036+
27037+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27038+{
27039+ int err;
27040+ unsigned int tmp;
5afbbe0d 27041+ aufs_bindex_t bindex, bbot;
1facf9fc 27042+ struct au_opt *opt;
27043+ struct au_opt_xino *opt_xino, xino;
27044+ struct au_sbinfo *sbinfo;
027c5e7a 27045+ struct au_branch *br;
076b876e 27046+ struct inode *dir;
1facf9fc 27047+
dece6358
AM
27048+ SiMustWriteLock(sb);
27049+
1facf9fc 27050+ err = 0;
27051+ opt_xino = NULL;
27052+ opt = opts->opt;
27053+ while (err >= 0 && opt->type != Opt_tail)
27054+ err = au_opt_simple(sb, opt++, opts);
27055+ if (err > 0)
27056+ err = 0;
27057+ else if (unlikely(err < 0))
27058+ goto out;
27059+
27060+ /* disable xino and udba temporary */
27061+ sbinfo = au_sbi(sb);
27062+ tmp = sbinfo->si_mntflags;
27063+ au_opt_clr(sbinfo->si_mntflags, XINO);
27064+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27065+
27066+ opt = opts->opt;
27067+ while (err >= 0 && opt->type != Opt_tail)
27068+ err = au_opt_br(sb, opt++, opts);
27069+ if (err > 0)
27070+ err = 0;
27071+ else if (unlikely(err < 0))
27072+ goto out;
27073+
5afbbe0d
AM
27074+ bbot = au_sbbot(sb);
27075+ if (unlikely(bbot < 0)) {
1facf9fc 27076+ err = -EINVAL;
4a4d8108 27077+ pr_err("no branches\n");
1facf9fc 27078+ goto out;
27079+ }
27080+
27081+ if (au_opt_test(tmp, XINO))
27082+ au_opt_set(sbinfo->si_mntflags, XINO);
27083+ opt = opts->opt;
27084+ while (!err && opt->type != Opt_tail)
27085+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27086+ if (unlikely(err))
27087+ goto out;
27088+
27089+ err = au_opts_verify(sb, sb->s_flags, tmp);
27090+ if (unlikely(err))
27091+ goto out;
27092+
27093+ /* restore xino */
27094+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27095+ xino.file = au_xino_def(sb);
27096+ err = PTR_ERR(xino.file);
27097+ if (IS_ERR(xino.file))
27098+ goto out;
27099+
27100+ err = au_xino_set(sb, &xino, /*remount*/0);
27101+ fput(xino.file);
27102+ if (unlikely(err))
27103+ goto out;
27104+ }
27105+
27106+ /* restore udba */
027c5e7a 27107+ tmp &= AuOptMask_UDBA;
1facf9fc 27108+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27109+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27110+ bbot = au_sbbot(sb);
27111+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27112+ br = au_sbr(sb, bindex);
27113+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27114+ if (unlikely(err))
27115+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27116+ bindex, err);
27117+ /* go on even if err */
27118+ }
4a4d8108 27119+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27120+ dir = d_inode(sb->s_root);
4a4d8108 27121+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27122+ }
27123+
4f0767ce 27124+out:
1facf9fc 27125+ return err;
27126+}
27127+
27128+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27129+{
27130+ int err, rerr;
79b8bda9 27131+ unsigned char no_dreval;
1facf9fc 27132+ struct inode *dir;
27133+ struct au_opt_xino *opt_xino;
27134+ struct au_opt *opt;
27135+ struct au_sbinfo *sbinfo;
27136+
dece6358
AM
27137+ SiMustWriteLock(sb);
27138+
8b6a4947
AM
27139+ err = au_dr_opt_flush(sb);
27140+ if (unlikely(err))
27141+ goto out;
27142+ au_fset_opts(opts->flags, DR_FLUSHED);
27143+
5527c038 27144+ dir = d_inode(sb->s_root);
1facf9fc 27145+ sbinfo = au_sbi(sb);
1facf9fc 27146+ opt_xino = NULL;
27147+ opt = opts->opt;
27148+ while (err >= 0 && opt->type != Opt_tail) {
27149+ err = au_opt_simple(sb, opt, opts);
27150+ if (!err)
27151+ err = au_opt_br(sb, opt, opts);
27152+ if (!err)
27153+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27154+ opt++;
27155+ }
27156+ if (err > 0)
27157+ err = 0;
27158+ AuTraceErr(err);
27159+ /* go on even err */
27160+
79b8bda9 27161+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27162+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27163+ if (unlikely(rerr && !err))
27164+ err = rerr;
27165+
79b8bda9 27166+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27167+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27168+
1facf9fc 27169+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27170+ rerr = au_xib_trunc(sb);
27171+ if (unlikely(rerr && !err))
27172+ err = rerr;
27173+ }
27174+
27175+ /* will be handled by the caller */
027c5e7a 27176+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27177+ && (opts->given_udba
27178+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27179+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27180+ ))
027c5e7a 27181+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27182+
27183+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27184+
27185+out:
1facf9fc 27186+ return err;
27187+}
27188+
27189+/* ---------------------------------------------------------------------- */
27190+
27191+unsigned int au_opt_udba(struct super_block *sb)
27192+{
27193+ return au_mntflags(sb) & AuOptMask_UDBA;
27194+}
7f207e10
AM
27195diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27196--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
be118d29 27197+++ linux/fs/aufs/opts.h 2018-04-06 07:48:44.207938097 +0200
8b6a4947 27198@@ -0,0 +1,224 @@
1facf9fc 27199+/*
a2654f78 27200+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 27201+ *
27202+ * This program, aufs is free software; you can redistribute it and/or modify
27203+ * it under the terms of the GNU General Public License as published by
27204+ * the Free Software Foundation; either version 2 of the License, or
27205+ * (at your option) any later version.
dece6358
AM
27206+ *
27207+ * This program is distributed in the hope that it will be useful,
27208+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27209+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27210+ * GNU General Public License for more details.
27211+ *
27212+ * You should have received a copy of the GNU General Public License
523b37e3 27213+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27214+ */
27215+
27216+/*
27217+ * mount options/flags
27218+ */
27219+
27220+#ifndef __AUFS_OPTS_H__
27221+#define __AUFS_OPTS_H__
27222+
27223+#ifdef __KERNEL__
27224+
dece6358 27225+#include <linux/path.h>
1facf9fc 27226+
dece6358 27227+struct file;
dece6358 27228+
1facf9fc 27229+/* ---------------------------------------------------------------------- */
27230+
27231+/* mount flags */
27232+#define AuOpt_XINO 1 /* external inode number bitmap
27233+ and translation table */
27234+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27235+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27236+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27237+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27238+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27239+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27240+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27241+ bits */
dece6358
AM
27242+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27243+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27244+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27245+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27246+#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
4a4d8108 27247+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27248+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27249+
4a4d8108
AM
27250+#ifndef CONFIG_AUFS_HNOTIFY
27251+#undef AuOpt_UDBA_HNOTIFY
27252+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27253+#endif
8b6a4947
AM
27254+#ifndef CONFIG_AUFS_DIRREN
27255+#undef AuOpt_DIRREN
27256+#define AuOpt_DIRREN 0
27257+#endif
dece6358
AM
27258+#ifndef CONFIG_AUFS_SHWH
27259+#undef AuOpt_SHWH
27260+#define AuOpt_SHWH 0
27261+#endif
1facf9fc 27262+
27263+#define AuOpt_Def (AuOpt_XINO \
27264+ | AuOpt_UDBA_REVAL \
27265+ | AuOpt_PLINK \
27266+ /* | AuOpt_DIRPERM1 */ \
27267+ | AuOpt_WARN_PERM)
27268+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27269+ | AuOpt_UDBA_REVAL \
4a4d8108 27270+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27271+
27272+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27273+#define au_opt_set(flags, name) do { \
27274+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27275+ ((flags) |= AuOpt_##name); \
27276+} while (0)
27277+#define au_opt_set_udba(flags, name) do { \
27278+ (flags) &= ~AuOptMask_UDBA; \
27279+ ((flags) |= AuOpt_##name); \
27280+} while (0)
7f207e10
AM
27281+#define au_opt_clr(flags, name) do { \
27282+ ((flags) &= ~AuOpt_##name); \
27283+} while (0)
1facf9fc 27284+
e49829fe
JR
27285+static inline unsigned int au_opts_plink(unsigned int mntflags)
27286+{
27287+#ifdef CONFIG_PROC_FS
27288+ return mntflags;
27289+#else
27290+ return mntflags & ~AuOpt_PLINK;
27291+#endif
27292+}
27293+
1facf9fc 27294+/* ---------------------------------------------------------------------- */
27295+
27296+/* policies to select one among multiple writable branches */
27297+enum {
27298+ AuWbrCreate_TDP, /* top down parent */
27299+ AuWbrCreate_RR, /* round robin */
27300+ AuWbrCreate_MFS, /* most free space */
27301+ AuWbrCreate_MFSV, /* mfs with seconds */
27302+ AuWbrCreate_MFSRR, /* mfs then rr */
27303+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27304+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27305+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27306+ AuWbrCreate_PMFS, /* parent and mfs */
27307+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27308+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27309+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27310+
27311+ AuWbrCreate_Def = AuWbrCreate_TDP
27312+};
27313+
27314+enum {
27315+ AuWbrCopyup_TDP, /* top down parent */
27316+ AuWbrCopyup_BUP, /* bottom up parent */
27317+ AuWbrCopyup_BU, /* bottom up */
27318+
27319+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27320+};
27321+
27322+/* ---------------------------------------------------------------------- */
27323+
27324+struct au_opt_add {
27325+ aufs_bindex_t bindex;
27326+ char *pathname;
27327+ int perm;
27328+ struct path path;
27329+};
27330+
27331+struct au_opt_del {
27332+ char *pathname;
27333+ struct path h_path;
27334+};
27335+
27336+struct au_opt_mod {
27337+ char *path;
27338+ int perm;
27339+ struct dentry *h_root;
27340+};
27341+
27342+struct au_opt_xino {
27343+ char *path;
27344+ struct file *file;
27345+};
27346+
27347+struct au_opt_xino_itrunc {
27348+ aufs_bindex_t bindex;
27349+};
27350+
27351+struct au_opt_wbr_create {
27352+ int wbr_create;
27353+ int mfs_second;
27354+ unsigned long long mfsrr_watermark;
27355+};
27356+
27357+struct au_opt {
27358+ int type;
27359+ union {
27360+ struct au_opt_xino xino;
27361+ struct au_opt_xino_itrunc xino_itrunc;
27362+ struct au_opt_add add;
27363+ struct au_opt_del del;
27364+ struct au_opt_mod mod;
27365+ int dirwh;
27366+ int rdcache;
27367+ unsigned int rdblk;
27368+ unsigned int rdhash;
27369+ int udba;
27370+ struct au_opt_wbr_create wbr_create;
27371+ int wbr_copyup;
076b876e 27372+ unsigned int fhsm_second;
1facf9fc 27373+ };
27374+};
27375+
27376+/* opts flags */
27377+#define AuOpts_REMOUNT 1
027c5e7a
AM
27378+#define AuOpts_REFRESH (1 << 1)
27379+#define AuOpts_TRUNC_XIB (1 << 2)
27380+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27381+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27382+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27383+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27384+#define au_fset_opts(flags, name) \
27385+ do { (flags) |= AuOpts_##name; } while (0)
27386+#define au_fclr_opts(flags, name) \
27387+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27388+
8b6a4947
AM
27389+#ifndef CONFIG_AUFS_DIRREN
27390+#undef AuOpts_DR_FLUSHED
27391+#define AuOpts_DR_FLUSHED 0
27392+#endif
27393+
1facf9fc 27394+struct au_opts {
27395+ struct au_opt *opt;
27396+ int max_opt;
27397+
27398+ unsigned int given_udba;
27399+ unsigned int flags;
27400+ unsigned long sb_flags;
27401+};
27402+
27403+/* ---------------------------------------------------------------------- */
27404+
7e9cd9fe 27405+/* opts.c */
076b876e 27406+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27407+const char *au_optstr_udba(int udba);
27408+const char *au_optstr_wbr_copyup(int wbr_copyup);
27409+const char *au_optstr_wbr_create(int wbr_create);
27410+
27411+void au_opts_free(struct au_opts *opts);
3c1bdaff 27412+struct super_block;
1facf9fc 27413+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27414+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27415+ unsigned int pending);
27416+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27417+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27418+
27419+unsigned int au_opt_udba(struct super_block *sb);
27420+
1facf9fc 27421+#endif /* __KERNEL__ */
27422+#endif /* __AUFS_OPTS_H__ */
7f207e10
AM
27423diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27424--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
be118d29 27425+++ linux/fs/aufs/plink.c 2018-04-06 07:48:44.207938097 +0200
8b6a4947 27426@@ -0,0 +1,515 @@
1facf9fc 27427+/*
a2654f78 27428+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 27429+ *
27430+ * This program, aufs is free software; you can redistribute it and/or modify
27431+ * it under the terms of the GNU General Public License as published by
27432+ * the Free Software Foundation; either version 2 of the License, or
27433+ * (at your option) any later version.
dece6358
AM
27434+ *
27435+ * This program is distributed in the hope that it will be useful,
27436+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27437+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27438+ * GNU General Public License for more details.
27439+ *
27440+ * You should have received a copy of the GNU General Public License
523b37e3 27441+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27442+ */
27443+
27444+/*
27445+ * pseudo-link
27446+ */
27447+
27448+#include "aufs.h"
27449+
27450+/*
e49829fe 27451+ * the pseudo-link maintenance mode.
1facf9fc 27452+ * during a user process maintains the pseudo-links,
27453+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27454+ *
27455+ * Flags
27456+ * NOPLM:
27457+ * For entry functions which will handle plink, and i_mutex is already held
27458+ * in VFS.
27459+ * They cannot wait and should return an error at once.
27460+ * Callers has to check the error.
27461+ * NOPLMW:
27462+ * For entry functions which will handle plink, but i_mutex is not held
27463+ * in VFS.
27464+ * They can wait the plink maintenance mode to finish.
27465+ *
27466+ * They behave like F_SETLK and F_SETLKW.
27467+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27468+ */
e49829fe
JR
27469+
27470+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27471+{
e49829fe
JR
27472+ int err;
27473+ pid_t pid, ppid;
f0c0a007 27474+ struct task_struct *parent, *prev;
e49829fe 27475+ struct au_sbinfo *sbi;
dece6358
AM
27476+
27477+ SiMustAnyLock(sb);
27478+
e49829fe
JR
27479+ err = 0;
27480+ if (!au_opt_test(au_mntflags(sb), PLINK))
27481+ goto out;
27482+
27483+ sbi = au_sbi(sb);
27484+ pid = sbi->si_plink_maint_pid;
27485+ if (!pid || pid == current->pid)
27486+ goto out;
27487+
27488+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27489+ prev = NULL;
27490+ parent = current;
27491+ ppid = 0;
e49829fe 27492+ rcu_read_lock();
f0c0a007
AM
27493+ while (1) {
27494+ parent = rcu_dereference(parent->real_parent);
27495+ if (parent == prev)
27496+ break;
27497+ ppid = task_pid_vnr(parent);
27498+ if (pid == ppid) {
27499+ rcu_read_unlock();
27500+ goto out;
27501+ }
27502+ prev = parent;
27503+ }
e49829fe 27504+ rcu_read_unlock();
e49829fe
JR
27505+
27506+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27507+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27508+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27509+ while (sbi->si_plink_maint_pid) {
27510+ si_read_unlock(sb);
27511+ /* gave up wake_up_bit() */
27512+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27513+
27514+ if (au_ftest_lock(flags, FLUSH))
27515+ au_nwt_flush(&sbi->si_nowait);
27516+ si_noflush_read_lock(sb);
27517+ }
27518+ } else if (au_ftest_lock(flags, NOPLM)) {
27519+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27520+ err = -EAGAIN;
27521+ }
27522+
27523+out:
27524+ return err;
4a4d8108
AM
27525+}
27526+
e49829fe 27527+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 27528+{
4a4d8108 27529+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 27530+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 27531+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 27532+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
27533+}
27534+
e49829fe 27535+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
27536+{
27537+ int err;
4a4d8108
AM
27538+ struct au_sbinfo *sbinfo;
27539+
27540+ err = 0;
4a4d8108
AM
27541+ sbinfo = au_sbi(sb);
27542+ /* make sure i am the only one in this fs */
e49829fe
JR
27543+ si_write_lock(sb, AuLock_FLUSH);
27544+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27545+ spin_lock(&sbinfo->si_plink_maint_lock);
27546+ if (!sbinfo->si_plink_maint_pid)
27547+ sbinfo->si_plink_maint_pid = current->pid;
27548+ else
27549+ err = -EBUSY;
27550+ spin_unlock(&sbinfo->si_plink_maint_lock);
27551+ }
4a4d8108
AM
27552+ si_write_unlock(sb);
27553+
27554+ return err;
1facf9fc 27555+}
27556+
27557+/* ---------------------------------------------------------------------- */
27558+
1facf9fc 27559+#ifdef CONFIG_AUFS_DEBUG
27560+void au_plink_list(struct super_block *sb)
27561+{
86dc4139 27562+ int i;
1facf9fc 27563+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27564+ struct hlist_bl_head *hbl;
27565+ struct hlist_bl_node *pos;
5afbbe0d 27566+ struct au_icntnr *icntnr;
1facf9fc 27567+
dece6358
AM
27568+ SiMustAnyLock(sb);
27569+
1facf9fc 27570+ sbinfo = au_sbi(sb);
27571+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27572+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27573+
86dc4139 27574+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27575+ hbl = sbinfo->si_plink + i;
27576+ hlist_bl_lock(hbl);
27577+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27578+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 27579+ hlist_bl_unlock(hbl);
86dc4139 27580+ }
1facf9fc 27581+}
27582+#endif
27583+
27584+/* is the inode pseudo-linked? */
27585+int au_plink_test(struct inode *inode)
27586+{
86dc4139 27587+ int found, i;
1facf9fc 27588+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27589+ struct hlist_bl_head *hbl;
27590+ struct hlist_bl_node *pos;
5afbbe0d 27591+ struct au_icntnr *icntnr;
1facf9fc 27592+
27593+ sbinfo = au_sbi(inode->i_sb);
dece6358 27594+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 27595+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 27596+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 27597+
27598+ found = 0;
86dc4139 27599+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
27600+ hbl = sbinfo->si_plink + i;
27601+ hlist_bl_lock(hbl);
27602+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27603+ if (&icntnr->vfs_inode == inode) {
1facf9fc 27604+ found = 1;
27605+ break;
27606+ }
8b6a4947 27607+ hlist_bl_unlock(hbl);
1facf9fc 27608+ return found;
27609+}
27610+
27611+/* ---------------------------------------------------------------------- */
27612+
27613+/*
27614+ * generate a name for plink.
27615+ * the file will be stored under AUFS_WH_PLINKDIR.
27616+ */
27617+/* 20 is max digits length of ulong 64 */
27618+#define PLINK_NAME_LEN ((20 + 1) * 2)
27619+
27620+static int plink_name(char *name, int len, struct inode *inode,
27621+ aufs_bindex_t bindex)
27622+{
27623+ int rlen;
27624+ struct inode *h_inode;
27625+
27626+ h_inode = au_h_iptr(inode, bindex);
27627+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27628+ return rlen;
27629+}
27630+
7f207e10
AM
27631+struct au_do_plink_lkup_args {
27632+ struct dentry **errp;
27633+ struct qstr *tgtname;
27634+ struct dentry *h_parent;
27635+ struct au_branch *br;
27636+};
27637+
27638+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27639+ struct dentry *h_parent,
27640+ struct au_branch *br)
27641+{
27642+ struct dentry *h_dentry;
febd17d6 27643+ struct inode *h_inode;
7f207e10 27644+
febd17d6 27645+ h_inode = d_inode(h_parent);
be118d29 27646+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 27647+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 27648+ inode_unlock_shared(h_inode);
7f207e10
AM
27649+ return h_dentry;
27650+}
27651+
27652+static void au_call_do_plink_lkup(void *args)
27653+{
27654+ struct au_do_plink_lkup_args *a = args;
27655+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27656+}
27657+
1facf9fc 27658+/* lookup the plink-ed @inode under the branch at @bindex */
27659+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27660+{
27661+ struct dentry *h_dentry, *h_parent;
27662+ struct au_branch *br;
7f207e10 27663+ int wkq_err;
1facf9fc 27664+ char a[PLINK_NAME_LEN];
0c3ec466 27665+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 27666+
e49829fe
JR
27667+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27668+
1facf9fc 27669+ br = au_sbr(inode->i_sb, bindex);
27670+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 27671+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27672+
2dfbb274 27673+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
27674+ struct au_do_plink_lkup_args args = {
27675+ .errp = &h_dentry,
27676+ .tgtname = &tgtname,
27677+ .h_parent = h_parent,
27678+ .br = br
27679+ };
27680+
27681+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27682+ if (unlikely(wkq_err))
27683+ h_dentry = ERR_PTR(wkq_err);
27684+ } else
27685+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27686+
1facf9fc 27687+ return h_dentry;
27688+}
27689+
27690+/* create a pseudo-link */
27691+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27692+ struct dentry *h_dentry, struct au_branch *br)
27693+{
27694+ int err;
27695+ struct path h_path = {
86dc4139 27696+ .mnt = au_br_mnt(br)
1facf9fc 27697+ };
523b37e3 27698+ struct inode *h_dir, *delegated;
1facf9fc 27699+
5527c038 27700+ h_dir = d_inode(h_parent);
febd17d6 27701+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 27702+again:
b4510431 27703+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 27704+ err = PTR_ERR(h_path.dentry);
27705+ if (IS_ERR(h_path.dentry))
27706+ goto out;
27707+
27708+ err = 0;
27709+ /* wh.plink dir is not monitored */
7f207e10 27710+ /* todo: is it really safe? */
5527c038
JR
27711+ if (d_is_positive(h_path.dentry)
27712+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
27713+ delegated = NULL;
27714+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27715+ if (unlikely(err == -EWOULDBLOCK)) {
27716+ pr_warn("cannot retry for NFSv4 delegation"
27717+ " for an internal unlink\n");
27718+ iput(delegated);
27719+ }
1facf9fc 27720+ dput(h_path.dentry);
27721+ h_path.dentry = NULL;
27722+ if (!err)
27723+ goto again;
27724+ }
5527c038 27725+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
27726+ delegated = NULL;
27727+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
27728+ if (unlikely(err == -EWOULDBLOCK)) {
27729+ pr_warn("cannot retry for NFSv4 delegation"
27730+ " for an internal link\n");
27731+ iput(delegated);
27732+ }
27733+ }
1facf9fc 27734+ dput(h_path.dentry);
27735+
4f0767ce 27736+out:
febd17d6 27737+ inode_unlock(h_dir);
1facf9fc 27738+ return err;
27739+}
27740+
27741+struct do_whplink_args {
27742+ int *errp;
27743+ struct qstr *tgt;
27744+ struct dentry *h_parent;
27745+ struct dentry *h_dentry;
27746+ struct au_branch *br;
27747+};
27748+
27749+static void call_do_whplink(void *args)
27750+{
27751+ struct do_whplink_args *a = args;
27752+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
27753+}
27754+
27755+static int whplink(struct dentry *h_dentry, struct inode *inode,
27756+ aufs_bindex_t bindex, struct au_branch *br)
27757+{
27758+ int err, wkq_err;
27759+ struct au_wbr *wbr;
27760+ struct dentry *h_parent;
1facf9fc 27761+ char a[PLINK_NAME_LEN];
0c3ec466 27762+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 27763+
27764+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
27765+ h_parent = wbr->wbr_plink;
1facf9fc 27766+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27767+
27768+ /* always superio. */
2dfbb274 27769+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 27770+ struct do_whplink_args args = {
27771+ .errp = &err,
27772+ .tgt = &tgtname,
27773+ .h_parent = h_parent,
27774+ .h_dentry = h_dentry,
27775+ .br = br
27776+ };
27777+ wkq_err = au_wkq_wait(call_do_whplink, &args);
27778+ if (unlikely(wkq_err))
27779+ err = wkq_err;
27780+ } else
27781+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 27782+
27783+ return err;
27784+}
27785+
1facf9fc 27786+/*
27787+ * create a new pseudo-link for @h_dentry on @bindex.
27788+ * the linked inode is held in aufs @inode.
27789+ */
27790+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
27791+ struct dentry *h_dentry)
27792+{
27793+ struct super_block *sb;
27794+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27795+ struct hlist_bl_head *hbl;
27796+ struct hlist_bl_node *pos;
5afbbe0d 27797+ struct au_icntnr *icntnr;
86dc4139 27798+ int found, err, cnt, i;
1facf9fc 27799+
27800+ sb = inode->i_sb;
27801+ sbinfo = au_sbi(sb);
27802+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27803+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27804+
86dc4139 27805+ found = au_plink_test(inode);
4a4d8108 27806+ if (found)
1facf9fc 27807+ return;
4a4d8108 27808+
86dc4139 27809+ i = au_plink_hash(inode->i_ino);
8b6a4947 27810+ hbl = sbinfo->si_plink + i;
5afbbe0d 27811+ au_igrab(inode);
1facf9fc 27812+
8b6a4947
AM
27813+ hlist_bl_lock(hbl);
27814+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 27815+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
27816+ found = 1;
27817+ break;
27818+ }
1facf9fc 27819+ }
5afbbe0d
AM
27820+ if (!found) {
27821+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 27822+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 27823+ }
8b6a4947 27824+ hlist_bl_unlock(hbl);
4a4d8108 27825+ if (!found) {
8b6a4947 27826+ cnt = au_hbl_count(hbl);
86dc4139
AM
27827+#define msg "unexpectedly unblanced or too many pseudo-links"
27828+ if (cnt > AUFS_PLINK_WARN)
27829+ AuWarn1(msg ", %d\n", cnt);
27830+#undef msg
1facf9fc 27831+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
27832+ if (unlikely(err)) {
27833+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 27834+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 27835+ iput(&icntnr->vfs_inode);
4a4d8108 27836+ }
5afbbe0d
AM
27837+ } else
27838+ iput(&icntnr->vfs_inode);
1facf9fc 27839+}
27840+
27841+/* free all plinks */
e49829fe 27842+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 27843+{
86dc4139 27844+ int i, warned;
1facf9fc 27845+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27846+ struct hlist_bl_head *hbl;
27847+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 27848+ struct au_icntnr *icntnr;
1facf9fc 27849+
dece6358
AM
27850+ SiMustWriteLock(sb);
27851+
1facf9fc 27852+ sbinfo = au_sbi(sb);
27853+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27854+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27855+
1facf9fc 27856+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
27857+ warned = 0;
27858+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27859+ hbl = sbinfo->si_plink + i;
27860+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
27861+ pr_warn("pseudo-link is not flushed");
27862+ warned = 1;
27863+ }
8b6a4947 27864+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 27865+ iput(&icntnr->vfs_inode);
8b6a4947 27866+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 27867+ }
1facf9fc 27868+}
27869+
e49829fe
JR
27870+void au_plink_clean(struct super_block *sb, int verbose)
27871+{
27872+ struct dentry *root;
27873+
27874+ root = sb->s_root;
27875+ aufs_write_lock(root);
27876+ if (au_opt_test(au_mntflags(sb), PLINK))
27877+ au_plink_put(sb, verbose);
27878+ aufs_write_unlock(root);
27879+}
27880+
86dc4139
AM
27881+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
27882+{
27883+ int do_put;
5afbbe0d 27884+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
27885+
27886+ do_put = 0;
5afbbe0d
AM
27887+ btop = au_ibtop(inode);
27888+ bbot = au_ibbot(inode);
27889+ if (btop >= 0) {
27890+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
27891+ if (!au_h_iptr(inode, bindex)
27892+ || au_ii_br_id(inode, bindex) != br_id)
27893+ continue;
27894+ au_set_h_iptr(inode, bindex, NULL, 0);
27895+ do_put = 1;
27896+ break;
27897+ }
27898+ if (do_put)
5afbbe0d 27899+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
27900+ if (au_h_iptr(inode, bindex)) {
27901+ do_put = 0;
27902+ break;
27903+ }
27904+ } else
27905+ do_put = 1;
27906+
27907+ return do_put;
27908+}
27909+
1facf9fc 27910+/* free the plinks on a branch specified by @br_id */
27911+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
27912+{
27913+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27914+ struct hlist_bl_head *hbl;
27915+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 27916+ struct au_icntnr *icntnr;
1facf9fc 27917+ struct inode *inode;
86dc4139 27918+ int i, do_put;
1facf9fc 27919+
dece6358
AM
27920+ SiMustWriteLock(sb);
27921+
1facf9fc 27922+ sbinfo = au_sbi(sb);
27923+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27924+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27925+
8b6a4947 27926+ /* no bit_lock since sbinfo is write-locked */
86dc4139 27927+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27928+ hbl = sbinfo->si_plink + i;
27929+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 27930+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
27931+ ii_write_lock_child(inode);
27932+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 27933+ if (do_put) {
8b6a4947 27934+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
27935+ iput(inode);
27936+ }
86dc4139
AM
27937+ ii_write_unlock(inode);
27938+ iput(inode);
dece6358 27939+ }
dece6358
AM
27940+ }
27941+}
7f207e10
AM
27942diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
27943--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
be118d29 27944+++ linux/fs/aufs/poll.c 2018-04-06 07:48:44.207938097 +0200
b912730e 27945@@ -0,0 +1,52 @@
dece6358 27946+/*
a2654f78 27947+ * Copyright (C) 2005-2017 Junjiro R. Okajima
dece6358
AM
27948+ *
27949+ * This program, aufs is free software; you can redistribute it and/or modify
27950+ * it under the terms of the GNU General Public License as published by
27951+ * the Free Software Foundation; either version 2 of the License, or
27952+ * (at your option) any later version.
27953+ *
27954+ * This program is distributed in the hope that it will be useful,
27955+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27956+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27957+ * GNU General Public License for more details.
27958+ *
27959+ * You should have received a copy of the GNU General Public License
523b37e3 27960+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
27961+ */
27962+
1308ab2a 27963+/*
27964+ * poll operation
27965+ * There is only one filesystem which implements ->poll operation, currently.
27966+ */
27967+
27968+#include "aufs.h"
27969+
be118d29 27970+__poll_t aufs_poll(struct file *file, poll_table *wait)
1308ab2a 27971+{
be118d29 27972+ __poll_t mask;
1308ab2a 27973+ int err;
27974+ struct file *h_file;
1308ab2a 27975+ struct super_block *sb;
27976+
27977+ /* We should pretend an error happened. */
be118d29 27978+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 27979+ sb = file->f_path.dentry->d_sb;
e49829fe 27980+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 27981+
521ced18 27982+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
27983+ err = PTR_ERR(h_file);
27984+ if (IS_ERR(h_file))
1308ab2a 27985+ goto out;
27986+
27987+ /* it is not an error if h_file has no operation */
27988+ mask = DEFAULT_POLLMASK;
523b37e3 27989+ if (h_file->f_op->poll)
1308ab2a 27990+ mask = h_file->f_op->poll(h_file, wait);
b912730e 27991+ fput(h_file); /* instead of au_read_post() */
1308ab2a 27992+
4f0767ce 27993+out:
1308ab2a 27994+ si_read_unlock(sb);
be118d29 27995+ AuDbg("mask 0x%x\n", mask);
1308ab2a 27996+ return mask;
27997+}
c1595e42
JR
27998diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
27999--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
be118d29 28000+++ linux/fs/aufs/posix_acl.c 2018-04-06 07:48:44.207938097 +0200
a2654f78 28001@@ -0,0 +1,102 @@
c1595e42 28002+/*
a2654f78 28003+ * Copyright (C) 2014-2017 Junjiro R. Okajima
c1595e42
JR
28004+ *
28005+ * This program, aufs is free software; you can redistribute it and/or modify
28006+ * it under the terms of the GNU General Public License as published by
28007+ * the Free Software Foundation; either version 2 of the License, or
28008+ * (at your option) any later version.
28009+ *
28010+ * This program is distributed in the hope that it will be useful,
28011+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28012+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28013+ * GNU General Public License for more details.
28014+ *
28015+ * You should have received a copy of the GNU General Public License
28016+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28017+ */
28018+
28019+/*
28020+ * posix acl operations
28021+ */
28022+
28023+#include <linux/fs.h>
c1595e42
JR
28024+#include "aufs.h"
28025+
28026+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28027+{
28028+ struct posix_acl *acl;
28029+ int err;
28030+ aufs_bindex_t bindex;
28031+ struct inode *h_inode;
28032+ struct super_block *sb;
28033+
28034+ acl = NULL;
28035+ sb = inode->i_sb;
28036+ si_read_lock(sb, AuLock_FLUSH);
28037+ ii_read_lock_child(inode);
2121bcd9 28038+ if (!(sb->s_flags & SB_POSIXACL))
c1595e42
JR
28039+ goto out;
28040+
5afbbe0d 28041+ bindex = au_ibtop(inode);
c1595e42
JR
28042+ h_inode = au_h_iptr(inode, bindex);
28043+ if (unlikely(!h_inode
28044+ || ((h_inode->i_mode & S_IFMT)
28045+ != (inode->i_mode & S_IFMT)))) {
28046+ err = au_busy_or_stale();
28047+ acl = ERR_PTR(err);
28048+ goto out;
28049+ }
28050+
28051+ /* always topmost only */
28052+ acl = get_acl(h_inode, type);
a2654f78
AM
28053+ if (!IS_ERR_OR_NULL(acl))
28054+ set_cached_acl(inode, type, acl);
c1595e42
JR
28055+
28056+out:
28057+ ii_read_unlock(inode);
28058+ si_read_unlock(sb);
28059+
28060+ AuTraceErrPtr(acl);
28061+ return acl;
28062+}
28063+
28064+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28065+{
28066+ int err;
28067+ ssize_t ssz;
28068+ struct dentry *dentry;
f2c43d5f 28069+ struct au_sxattr arg = {
c1595e42
JR
28070+ .type = AU_ACL_SET,
28071+ .u.acl_set = {
28072+ .acl = acl,
28073+ .type = type
28074+ },
28075+ };
28076+
5afbbe0d
AM
28077+ IMustLock(inode);
28078+
c1595e42
JR
28079+ if (inode->i_ino == AUFS_ROOT_INO)
28080+ dentry = dget(inode->i_sb->s_root);
28081+ else {
28082+ dentry = d_find_alias(inode);
28083+ if (!dentry)
28084+ dentry = d_find_any_alias(inode);
28085+ if (!dentry) {
28086+ pr_warn("cannot handle this inode, "
28087+ "please report to aufs-users ML\n");
28088+ err = -ENOENT;
28089+ goto out;
28090+ }
28091+ }
28092+
f2c43d5f 28093+ ssz = au_sxattr(dentry, inode, &arg);
c1595e42
JR
28094+ dput(dentry);
28095+ err = ssz;
a2654f78 28096+ if (ssz >= 0) {
c1595e42 28097+ err = 0;
a2654f78
AM
28098+ set_cached_acl(inode, type, acl);
28099+ }
c1595e42
JR
28100+
28101+out:
c1595e42
JR
28102+ return err;
28103+}
7f207e10
AM
28104diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28105--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
be118d29 28106+++ linux/fs/aufs/procfs.c 2018-04-06 07:48:44.207938097 +0200
8b6a4947 28107@@ -0,0 +1,170 @@
e49829fe 28108+/*
a2654f78 28109+ * Copyright (C) 2010-2017 Junjiro R. Okajima
e49829fe
JR
28110+ *
28111+ * This program, aufs is free software; you can redistribute it and/or modify
28112+ * it under the terms of the GNU General Public License as published by
28113+ * the Free Software Foundation; either version 2 of the License, or
28114+ * (at your option) any later version.
28115+ *
28116+ * This program is distributed in the hope that it will be useful,
28117+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28118+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28119+ * GNU General Public License for more details.
28120+ *
28121+ * You should have received a copy of the GNU General Public License
523b37e3 28122+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28123+ */
28124+
28125+/*
28126+ * procfs interfaces
28127+ */
28128+
28129+#include <linux/proc_fs.h>
28130+#include "aufs.h"
28131+
28132+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28133+{
28134+ struct au_sbinfo *sbinfo;
28135+
28136+ sbinfo = file->private_data;
28137+ if (sbinfo) {
28138+ au_plink_maint_leave(sbinfo);
28139+ kobject_put(&sbinfo->si_kobj);
28140+ }
28141+
28142+ return 0;
28143+}
28144+
28145+static void au_procfs_plm_write_clean(struct file *file)
28146+{
28147+ struct au_sbinfo *sbinfo;
28148+
28149+ sbinfo = file->private_data;
28150+ if (sbinfo)
28151+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28152+}
28153+
28154+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28155+{
28156+ int err;
28157+ struct super_block *sb;
28158+ struct au_sbinfo *sbinfo;
8b6a4947 28159+ struct hlist_bl_node *pos;
e49829fe
JR
28160+
28161+ err = -EBUSY;
28162+ if (unlikely(file->private_data))
28163+ goto out;
28164+
28165+ sb = NULL;
53392da6 28166+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28167+ hlist_bl_lock(&au_sbilist);
28168+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe
JR
28169+ if (id == sysaufs_si_id(sbinfo)) {
28170+ kobject_get(&sbinfo->si_kobj);
28171+ sb = sbinfo->si_sb;
28172+ break;
28173+ }
8b6a4947 28174+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28175+
28176+ err = -EINVAL;
28177+ if (unlikely(!sb))
28178+ goto out;
28179+
28180+ err = au_plink_maint_enter(sb);
28181+ if (!err)
28182+ /* keep kobject_get() */
28183+ file->private_data = sbinfo;
28184+ else
28185+ kobject_put(&sbinfo->si_kobj);
28186+out:
28187+ return err;
28188+}
28189+
28190+/*
28191+ * Accept a valid "si=xxxx" only.
28192+ * Once it is accepted successfully, accept "clean" too.
28193+ */
28194+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28195+ size_t count, loff_t *ppos)
28196+{
28197+ ssize_t err;
28198+ unsigned long id;
28199+ /* last newline is allowed */
28200+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28201+
28202+ err = -EACCES;
28203+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28204+ goto out;
28205+
28206+ err = -EINVAL;
28207+ if (unlikely(count > sizeof(buf)))
28208+ goto out;
28209+
28210+ err = copy_from_user(buf, ubuf, count);
28211+ if (unlikely(err)) {
28212+ err = -EFAULT;
28213+ goto out;
28214+ }
28215+ buf[count] = 0;
28216+
28217+ err = -EINVAL;
28218+ if (!strcmp("clean", buf)) {
28219+ au_procfs_plm_write_clean(file);
28220+ goto out_success;
28221+ } else if (unlikely(strncmp("si=", buf, 3)))
28222+ goto out;
28223+
9dbd164d 28224+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28225+ if (unlikely(err))
28226+ goto out;
28227+
28228+ err = au_procfs_plm_write_si(file, id);
28229+ if (unlikely(err))
28230+ goto out;
28231+
28232+out_success:
28233+ err = count; /* success */
28234+out:
28235+ return err;
28236+}
28237+
28238+static const struct file_operations au_procfs_plm_fop = {
28239+ .write = au_procfs_plm_write,
28240+ .release = au_procfs_plm_release,
28241+ .owner = THIS_MODULE
28242+};
28243+
28244+/* ---------------------------------------------------------------------- */
28245+
28246+static struct proc_dir_entry *au_procfs_dir;
28247+
28248+void au_procfs_fin(void)
28249+{
28250+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28251+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28252+}
28253+
28254+int __init au_procfs_init(void)
28255+{
28256+ int err;
28257+ struct proc_dir_entry *entry;
28258+
28259+ err = -ENOMEM;
28260+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28261+ if (unlikely(!au_procfs_dir))
28262+ goto out;
28263+
28264+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
28265+ au_procfs_dir, &au_procfs_plm_fop);
28266+ if (unlikely(!entry))
28267+ goto out_dir;
28268+
28269+ err = 0;
28270+ goto out; /* success */
28271+
28272+
28273+out_dir:
28274+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28275+out:
28276+ return err;
28277+}
7f207e10
AM
28278diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28279--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
be118d29 28280+++ linux/fs/aufs/rdu.c 2018-04-06 07:48:44.207938097 +0200
5afbbe0d 28281@@ -0,0 +1,381 @@
1308ab2a 28282+/*
a2654f78 28283+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1308ab2a 28284+ *
28285+ * This program, aufs is free software; you can redistribute it and/or modify
28286+ * it under the terms of the GNU General Public License as published by
28287+ * the Free Software Foundation; either version 2 of the License, or
28288+ * (at your option) any later version.
28289+ *
28290+ * This program is distributed in the hope that it will be useful,
28291+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28292+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28293+ * GNU General Public License for more details.
28294+ *
28295+ * You should have received a copy of the GNU General Public License
523b37e3 28296+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28297+ */
28298+
28299+/*
28300+ * readdir in userspace.
28301+ */
28302+
b752ccd1 28303+#include <linux/compat.h>
4a4d8108 28304+#include <linux/fs_stack.h>
1308ab2a 28305+#include <linux/security.h>
1308ab2a 28306+#include "aufs.h"
28307+
28308+/* bits for struct aufs_rdu.flags */
28309+#define AuRdu_CALLED 1
28310+#define AuRdu_CONT (1 << 1)
28311+#define AuRdu_FULL (1 << 2)
28312+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28313+#define au_fset_rdu(flags, name) \
28314+ do { (flags) |= AuRdu_##name; } while (0)
28315+#define au_fclr_rdu(flags, name) \
28316+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28317+
28318+struct au_rdu_arg {
392086de 28319+ struct dir_context ctx;
1308ab2a 28320+ struct aufs_rdu *rdu;
28321+ union au_rdu_ent_ul ent;
28322+ unsigned long end;
28323+
28324+ struct super_block *sb;
28325+ int err;
28326+};
28327+
392086de 28328+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28329+ loff_t offset, u64 h_ino, unsigned int d_type)
28330+{
28331+ int err, len;
392086de 28332+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28333+ struct aufs_rdu *rdu = arg->rdu;
28334+ struct au_rdu_ent ent;
28335+
28336+ err = 0;
28337+ arg->err = 0;
28338+ au_fset_rdu(rdu->cookie.flags, CALLED);
28339+ len = au_rdu_len(nlen);
28340+ if (arg->ent.ul + len < arg->end) {
28341+ ent.ino = h_ino;
28342+ ent.bindex = rdu->cookie.bindex;
28343+ ent.type = d_type;
28344+ ent.nlen = nlen;
4a4d8108
AM
28345+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28346+ ent.type = DT_UNKNOWN;
1308ab2a 28347+
9dbd164d 28348+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28349+ err = -EFAULT;
28350+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28351+ goto out;
28352+ if (copy_to_user(arg->ent.e->name, name, nlen))
28353+ goto out;
28354+ /* the terminating NULL */
28355+ if (__put_user(0, arg->ent.e->name + nlen))
28356+ goto out;
28357+ err = 0;
28358+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28359+ arg->ent.ul += len;
28360+ rdu->rent++;
28361+ } else {
28362+ err = -EFAULT;
28363+ au_fset_rdu(rdu->cookie.flags, FULL);
28364+ rdu->full = 1;
28365+ rdu->tail = arg->ent;
28366+ }
28367+
4f0767ce 28368+out:
1308ab2a 28369+ /* AuTraceErr(err); */
28370+ return err;
28371+}
28372+
28373+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28374+{
28375+ int err;
28376+ loff_t offset;
28377+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28378+
92d182d2 28379+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28380+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28381+ err = offset;
28382+ if (unlikely(offset != cookie->h_pos))
28383+ goto out;
28384+
28385+ err = 0;
28386+ do {
28387+ arg->err = 0;
28388+ au_fclr_rdu(cookie->flags, CALLED);
28389+ /* smp_mb(); */
392086de 28390+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28391+ if (err >= 0)
28392+ err = arg->err;
28393+ } while (!err
28394+ && au_ftest_rdu(cookie->flags, CALLED)
28395+ && !au_ftest_rdu(cookie->flags, FULL));
28396+ cookie->h_pos = h_file->f_pos;
28397+
4f0767ce 28398+out:
1308ab2a 28399+ AuTraceErr(err);
28400+ return err;
28401+}
28402+
28403+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28404+{
28405+ int err;
5afbbe0d 28406+ aufs_bindex_t bbot;
392086de
AM
28407+ struct au_rdu_arg arg = {
28408+ .ctx = {
2000de60 28409+ .actor = au_rdu_fill
392086de
AM
28410+ }
28411+ };
1308ab2a 28412+ struct dentry *dentry;
28413+ struct inode *inode;
28414+ struct file *h_file;
28415+ struct au_rdu_cookie *cookie = &rdu->cookie;
28416+
28417+ err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
28418+ if (unlikely(err)) {
28419+ err = -EFAULT;
28420+ AuTraceErr(err);
28421+ goto out;
28422+ }
28423+ rdu->rent = 0;
28424+ rdu->tail = rdu->ent;
28425+ rdu->full = 0;
28426+ arg.rdu = rdu;
28427+ arg.ent = rdu->ent;
28428+ arg.end = arg.ent.ul;
28429+ arg.end += rdu->sz;
28430+
28431+ err = -ENOTDIR;
5afbbe0d 28432+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28433+ goto out;
28434+
28435+ err = security_file_permission(file, MAY_READ);
28436+ AuTraceErr(err);
28437+ if (unlikely(err))
28438+ goto out;
28439+
2000de60 28440+ dentry = file->f_path.dentry;
5527c038 28441+ inode = d_inode(dentry);
5afbbe0d 28442+ inode_lock_shared(inode);
1308ab2a 28443+
28444+ arg.sb = inode->i_sb;
e49829fe
JR
28445+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28446+ if (unlikely(err))
28447+ goto out_mtx;
027c5e7a
AM
28448+ err = au_alive_dir(dentry);
28449+ if (unlikely(err))
28450+ goto out_si;
e49829fe 28451+ /* todo: reval? */
1308ab2a 28452+ fi_read_lock(file);
28453+
28454+ err = -EAGAIN;
28455+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28456+ && cookie->generation != au_figen(file)))
28457+ goto out_unlock;
28458+
28459+ err = 0;
28460+ if (!rdu->blk) {
28461+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28462+ if (!rdu->blk)
28463+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28464+ }
5afbbe0d
AM
28465+ bbot = au_fbtop(file);
28466+ if (cookie->bindex < bbot)
28467+ cookie->bindex = bbot;
28468+ bbot = au_fbbot_dir(file);
28469+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28470+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28471+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28472+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28473+ if (!h_file)
28474+ continue;
28475+
28476+ au_fclr_rdu(cookie->flags, FULL);
28477+ err = au_rdu_do(h_file, &arg);
28478+ AuTraceErr(err);
28479+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28480+ break;
28481+ }
28482+ AuDbg("rent %llu\n", rdu->rent);
28483+
28484+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28485+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28486+ au_fset_rdu(cookie->flags, CONT);
28487+ cookie->generation = au_figen(file);
28488+ }
28489+
28490+ ii_read_lock_child(inode);
5afbbe0d 28491+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28492+ ii_read_unlock(inode);
28493+
4f0767ce 28494+out_unlock:
1308ab2a 28495+ fi_read_unlock(file);
027c5e7a 28496+out_si:
1308ab2a 28497+ si_read_unlock(arg.sb);
4f0767ce 28498+out_mtx:
5afbbe0d 28499+ inode_unlock_shared(inode);
4f0767ce 28500+out:
1308ab2a 28501+ AuTraceErr(err);
28502+ return err;
28503+}
28504+
28505+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28506+{
28507+ int err;
28508+ ino_t ino;
28509+ unsigned long long nent;
28510+ union au_rdu_ent_ul *u;
28511+ struct au_rdu_ent ent;
28512+ struct super_block *sb;
28513+
28514+ err = 0;
28515+ nent = rdu->nent;
28516+ u = &rdu->ent;
2000de60 28517+ sb = file->f_path.dentry->d_sb;
1308ab2a 28518+ si_read_lock(sb, AuLock_FLUSH);
28519+ while (nent-- > 0) {
9dbd164d 28520+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28521+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108
AM
28522+ if (!err)
28523+ err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
1308ab2a 28524+ if (unlikely(err)) {
28525+ err = -EFAULT;
28526+ AuTraceErr(err);
28527+ break;
28528+ }
28529+
28530+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28531+ if (!ent.wh)
28532+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28533+ else
28534+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28535+ &ino);
28536+ if (unlikely(err)) {
28537+ AuTraceErr(err);
28538+ break;
28539+ }
28540+
28541+ err = __put_user(ino, &u->e->ino);
28542+ if (unlikely(err)) {
28543+ err = -EFAULT;
28544+ AuTraceErr(err);
28545+ break;
28546+ }
28547+ u->ul += au_rdu_len(ent.nlen);
28548+ }
28549+ si_read_unlock(sb);
28550+
28551+ return err;
28552+}
28553+
28554+/* ---------------------------------------------------------------------- */
28555+
28556+static int au_rdu_verify(struct aufs_rdu *rdu)
28557+{
b752ccd1 28558+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 28559+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 28560+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 28561+ rdu->blk,
28562+ rdu->rent, rdu->shwh, rdu->full,
28563+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28564+ rdu->cookie.generation);
dece6358 28565+
b752ccd1 28566+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 28567+ return 0;
dece6358 28568+
b752ccd1
AM
28569+ AuDbg("%u:%u\n",
28570+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 28571+ return -EINVAL;
28572+}
28573+
28574+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 28575+{
1308ab2a 28576+ long err, e;
28577+ struct aufs_rdu rdu;
28578+ void __user *p = (void __user *)arg;
dece6358 28579+
1308ab2a 28580+ err = copy_from_user(&rdu, p, sizeof(rdu));
28581+ if (unlikely(err)) {
28582+ err = -EFAULT;
28583+ AuTraceErr(err);
28584+ goto out;
28585+ }
28586+ err = au_rdu_verify(&rdu);
dece6358
AM
28587+ if (unlikely(err))
28588+ goto out;
28589+
1308ab2a 28590+ switch (cmd) {
28591+ case AUFS_CTL_RDU:
28592+ err = au_rdu(file, &rdu);
28593+ if (unlikely(err))
28594+ break;
dece6358 28595+
1308ab2a 28596+ e = copy_to_user(p, &rdu, sizeof(rdu));
28597+ if (unlikely(e)) {
28598+ err = -EFAULT;
28599+ AuTraceErr(err);
28600+ }
28601+ break;
28602+ case AUFS_CTL_RDU_INO:
28603+ err = au_rdu_ino(file, &rdu);
28604+ break;
28605+
28606+ default:
4a4d8108 28607+ /* err = -ENOTTY; */
1308ab2a 28608+ err = -EINVAL;
28609+ }
dece6358 28610+
4f0767ce 28611+out:
1308ab2a 28612+ AuTraceErr(err);
28613+ return err;
1facf9fc 28614+}
b752ccd1
AM
28615+
28616+#ifdef CONFIG_COMPAT
28617+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28618+{
28619+ long err, e;
28620+ struct aufs_rdu rdu;
28621+ void __user *p = compat_ptr(arg);
28622+
28623+ /* todo: get_user()? */
28624+ err = copy_from_user(&rdu, p, sizeof(rdu));
28625+ if (unlikely(err)) {
28626+ err = -EFAULT;
28627+ AuTraceErr(err);
28628+ goto out;
28629+ }
28630+ rdu.ent.e = compat_ptr(rdu.ent.ul);
28631+ err = au_rdu_verify(&rdu);
28632+ if (unlikely(err))
28633+ goto out;
28634+
28635+ switch (cmd) {
28636+ case AUFS_CTL_RDU:
28637+ err = au_rdu(file, &rdu);
28638+ if (unlikely(err))
28639+ break;
28640+
28641+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28642+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28643+ e = copy_to_user(p, &rdu, sizeof(rdu));
28644+ if (unlikely(e)) {
28645+ err = -EFAULT;
28646+ AuTraceErr(err);
28647+ }
28648+ break;
28649+ case AUFS_CTL_RDU_INO:
28650+ err = au_rdu_ino(file, &rdu);
28651+ break;
28652+
28653+ default:
28654+ /* err = -ENOTTY; */
28655+ err = -EINVAL;
28656+ }
28657+
4f0767ce 28658+out:
b752ccd1
AM
28659+ AuTraceErr(err);
28660+ return err;
28661+}
28662+#endif
7f207e10
AM
28663diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28664--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
be118d29 28665+++ linux/fs/aufs/rwsem.h 2018-04-06 07:48:44.207938097 +0200
8b6a4947 28666@@ -0,0 +1,72 @@
1facf9fc 28667+/*
a2654f78 28668+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 28669+ *
28670+ * This program, aufs is free software; you can redistribute it and/or modify
28671+ * it under the terms of the GNU General Public License as published by
28672+ * the Free Software Foundation; either version 2 of the License, or
28673+ * (at your option) any later version.
dece6358
AM
28674+ *
28675+ * This program is distributed in the hope that it will be useful,
28676+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28677+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28678+ * GNU General Public License for more details.
28679+ *
28680+ * You should have received a copy of the GNU General Public License
523b37e3 28681+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28682+ */
28683+
28684+/*
28685+ * simple read-write semaphore wrappers
28686+ */
28687+
28688+#ifndef __AUFS_RWSEM_H__
28689+#define __AUFS_RWSEM_H__
28690+
28691+#ifdef __KERNEL__
28692+
4a4d8108 28693+#include "debug.h"
dece6358 28694+
8b6a4947
AM
28695+/* in the futre, the name 'au_rwsem' will be totally gone */
28696+#define au_rwsem rw_semaphore
dece6358
AM
28697+
28698+/* to debug easier, do not make them inlined functions */
8b6a4947 28699+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
dece6358 28700+/* rwsem_is_locked() is unusable */
8b6a4947
AM
28701+#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
28702+ && debug_locks \
28703+ && !lockdep_is_held_type(rw, 1))
28704+#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
28705+ && debug_locks \
28706+ && !lockdep_is_held_type(rw, 0))
28707+#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
28708+ && debug_locks \
28709+ && !lockdep_is_held(rw))
28710+#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
28711+ && debug_locks \
28712+ && lockdep_is_held(rw))
28713+
28714+#define au_rw_init(rw) init_rwsem(rw)
dece6358 28715+
5afbbe0d
AM
28716+#define au_rw_init_wlock(rw) do { \
28717+ au_rw_init(rw); \
8b6a4947 28718+ down_write(rw); \
5afbbe0d 28719+ } while (0)
dece6358 28720+
8b6a4947
AM
28721+#define au_rw_init_wlock_nested(rw, lsc) do { \
28722+ au_rw_init(rw); \
28723+ down_write_nested(rw, lsc); \
5afbbe0d 28724+ } while (0)
dece6358 28725+
8b6a4947
AM
28726+#define au_rw_read_lock(rw) down_read(rw)
28727+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
28728+#define au_rw_read_unlock(rw) up_read(rw)
28729+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
28730+#define au_rw_write_lock(rw) down_write(rw)
28731+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
28732+#define au_rw_write_unlock(rw) up_write(rw)
28733+/* why is not _nested version defined? */
28734+#define au_rw_read_trylock(rw) down_read_trylock(rw)
28735+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 28736+
28737+#endif /* __KERNEL__ */
28738+#endif /* __AUFS_RWSEM_H__ */
7f207e10
AM
28739diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
28740--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
be118d29 28741+++ linux/fs/aufs/sbinfo.c 2018-04-06 07:48:44.207938097 +0200
a2654f78 28742@@ -0,0 +1,304 @@
1facf9fc 28743+/*
a2654f78 28744+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 28745+ *
28746+ * This program, aufs is free software; you can redistribute it and/or modify
28747+ * it under the terms of the GNU General Public License as published by
28748+ * the Free Software Foundation; either version 2 of the License, or
28749+ * (at your option) any later version.
dece6358
AM
28750+ *
28751+ * This program is distributed in the hope that it will be useful,
28752+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28753+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28754+ * GNU General Public License for more details.
28755+ *
28756+ * You should have received a copy of the GNU General Public License
523b37e3 28757+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28758+ */
28759+
28760+/*
28761+ * superblock private data
28762+ */
28763+
28764+#include "aufs.h"
28765+
28766+/*
28767+ * they are necessary regardless sysfs is disabled.
28768+ */
28769+void au_si_free(struct kobject *kobj)
28770+{
86dc4139 28771+ int i;
1facf9fc 28772+ struct au_sbinfo *sbinfo;
b752ccd1 28773+ char *locked __maybe_unused; /* debug only */
1facf9fc 28774+
28775+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 28776+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 28777+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 28778+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d
AM
28779+
28780+ AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
28781+ percpu_counter_destroy(&sbinfo->si_ninodes);
28782+ AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
28783+ percpu_counter_destroy(&sbinfo->si_nfiles);
1facf9fc 28784+
e49829fe 28785+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 28786+ au_br_free(sbinfo);
e49829fe 28787+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 28788+
1c60b727 28789+ kfree(sbinfo->si_branch);
1facf9fc 28790+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 28791+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 28792+
1c60b727 28793+ kfree(sbinfo);
1facf9fc 28794+}
28795+
28796+int au_si_alloc(struct super_block *sb)
28797+{
86dc4139 28798+ int err, i;
1facf9fc 28799+ struct au_sbinfo *sbinfo;
28800+
28801+ err = -ENOMEM;
4a4d8108 28802+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 28803+ if (unlikely(!sbinfo))
28804+ goto out;
28805+
28806+ /* will be reallocated separately */
28807+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
28808+ if (unlikely(!sbinfo->si_branch))
febd17d6 28809+ goto out_sbinfo;
1facf9fc 28810+
1facf9fc 28811+ err = sysaufs_si_init(sbinfo);
28812+ if (unlikely(err))
28813+ goto out_br;
28814+
28815+ au_nwt_init(&sbinfo->si_nowait);
dece6358 28816+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 28817+
5afbbe0d
AM
28818+ percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
28819+ percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
7f207e10 28820+
5afbbe0d 28821+ sbinfo->si_bbot = -1;
392086de 28822+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 28823+
28824+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
28825+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
28826+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
28827+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 28828+
076b876e
AM
28829+ au_fhsm_init(sbinfo);
28830+
e49829fe 28831+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 28832+
392086de
AM
28833+ sbinfo->si_xino_jiffy = jiffies;
28834+ sbinfo->si_xino_expire
28835+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 28836+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 28837+ sbinfo->si_xino_brid = -1;
28838+ /* leave si_xib_last_pindex and si_xib_next_bit */
28839+
8b6a4947 28840+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 28841+
e49829fe 28842+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 28843+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
28844+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
28845+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
28846+
86dc4139 28847+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 28848+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 28849+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 28850+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 28851+
8b6a4947 28852+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 28853+
b95c5147
AM
28854+ /* with getattr by default */
28855+ sbinfo->si_iop_array = aufs_iop;
28856+
1facf9fc 28857+ /* leave other members for sysaufs and si_mnt. */
28858+ sbinfo->si_sb = sb;
28859+ sb->s_fs_info = sbinfo;
b752ccd1 28860+ si_pid_set(sb);
1facf9fc 28861+ return 0; /* success */
28862+
4f0767ce 28863+out_br:
1c60b727 28864+ kfree(sbinfo->si_branch);
4f0767ce 28865+out_sbinfo:
1c60b727 28866+ kfree(sbinfo);
4f0767ce 28867+out:
1facf9fc 28868+ return err;
28869+}
28870+
e2f27e51 28871+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 28872+{
28873+ int err, sz;
28874+ struct au_branch **brp;
28875+
dece6358
AM
28876+ AuRwMustWriteLock(&sbinfo->si_rwsem);
28877+
1facf9fc 28878+ err = -ENOMEM;
5afbbe0d 28879+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 28880+ if (unlikely(!sz))
28881+ sz = sizeof(*brp);
e2f27e51
AM
28882+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
28883+ may_shrink);
1facf9fc 28884+ if (brp) {
28885+ sbinfo->si_branch = brp;
28886+ err = 0;
28887+ }
28888+
28889+ return err;
28890+}
28891+
28892+/* ---------------------------------------------------------------------- */
28893+
28894+unsigned int au_sigen_inc(struct super_block *sb)
28895+{
28896+ unsigned int gen;
5527c038 28897+ struct inode *inode;
1facf9fc 28898+
dece6358
AM
28899+ SiMustWriteLock(sb);
28900+
1facf9fc 28901+ gen = ++au_sbi(sb)->si_generation;
28902+ au_update_digen(sb->s_root);
5527c038
JR
28903+ inode = d_inode(sb->s_root);
28904+ au_update_iigen(inode, /*half*/0);
be118d29 28905+ inode_inc_iversion(inode);
1facf9fc 28906+ return gen;
28907+}
28908+
28909+aufs_bindex_t au_new_br_id(struct super_block *sb)
28910+{
28911+ aufs_bindex_t br_id;
28912+ int i;
28913+ struct au_sbinfo *sbinfo;
28914+
dece6358
AM
28915+ SiMustWriteLock(sb);
28916+
1facf9fc 28917+ sbinfo = au_sbi(sb);
28918+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
28919+ br_id = ++sbinfo->si_last_br_id;
7f207e10 28920+ AuDebugOn(br_id < 0);
1facf9fc 28921+ if (br_id && au_br_index(sb, br_id) < 0)
28922+ return br_id;
28923+ }
28924+
28925+ return -1;
28926+}
28927+
28928+/* ---------------------------------------------------------------------- */
28929+
e49829fe
JR
28930+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
28931+int si_read_lock(struct super_block *sb, int flags)
28932+{
28933+ int err;
28934+
28935+ err = 0;
28936+ if (au_ftest_lock(flags, FLUSH))
28937+ au_nwt_flush(&au_sbi(sb)->si_nowait);
28938+
28939+ si_noflush_read_lock(sb);
28940+ err = au_plink_maint(sb, flags);
28941+ if (unlikely(err))
28942+ si_read_unlock(sb);
28943+
28944+ return err;
28945+}
28946+
28947+int si_write_lock(struct super_block *sb, int flags)
28948+{
28949+ int err;
28950+
28951+ if (au_ftest_lock(flags, FLUSH))
28952+ au_nwt_flush(&au_sbi(sb)->si_nowait);
28953+
28954+ si_noflush_write_lock(sb);
28955+ err = au_plink_maint(sb, flags);
28956+ if (unlikely(err))
28957+ si_write_unlock(sb);
28958+
28959+ return err;
28960+}
28961+
1facf9fc 28962+/* dentry and super_block lock. call at entry point */
e49829fe 28963+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 28964+{
e49829fe 28965+ int err;
027c5e7a 28966+ struct super_block *sb;
e49829fe 28967+
027c5e7a
AM
28968+ sb = dentry->d_sb;
28969+ err = si_read_lock(sb, flags);
28970+ if (unlikely(err))
28971+ goto out;
28972+
28973+ if (au_ftest_lock(flags, DW))
28974+ di_write_lock_child(dentry);
28975+ else
28976+ di_read_lock_child(dentry, flags);
28977+
28978+ if (au_ftest_lock(flags, GEN)) {
28979+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
28980+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
28981+ AuDebugOn(!err && au_dbrange_test(dentry));
28982+ else if (!err)
28983+ err = au_dbrange_test(dentry);
027c5e7a
AM
28984+ if (unlikely(err))
28985+ aufs_read_unlock(dentry, flags);
e49829fe
JR
28986+ }
28987+
027c5e7a 28988+out:
e49829fe 28989+ return err;
1facf9fc 28990+}
28991+
28992+void aufs_read_unlock(struct dentry *dentry, int flags)
28993+{
28994+ if (au_ftest_lock(flags, DW))
28995+ di_write_unlock(dentry);
28996+ else
28997+ di_read_unlock(dentry, flags);
28998+ si_read_unlock(dentry->d_sb);
28999+}
29000+
29001+void aufs_write_lock(struct dentry *dentry)
29002+{
e49829fe 29003+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29004+ di_write_lock_child(dentry);
29005+}
29006+
29007+void aufs_write_unlock(struct dentry *dentry)
29008+{
29009+ di_write_unlock(dentry);
29010+ si_write_unlock(dentry->d_sb);
29011+}
29012+
e49829fe 29013+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29014+{
e49829fe 29015+ int err;
027c5e7a
AM
29016+ unsigned int sigen;
29017+ struct super_block *sb;
e49829fe 29018+
027c5e7a
AM
29019+ sb = d1->d_sb;
29020+ err = si_read_lock(sb, flags);
29021+ if (unlikely(err))
29022+ goto out;
29023+
b95c5147 29024+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29025+
29026+ if (au_ftest_lock(flags, GEN)) {
29027+ sigen = au_sigen(sb);
29028+ err = au_digen_test(d1, sigen);
29029+ AuDebugOn(!err && au_dbrange_test(d1));
29030+ if (!err) {
29031+ err = au_digen_test(d2, sigen);
29032+ AuDebugOn(!err && au_dbrange_test(d2));
29033+ }
29034+ if (unlikely(err))
29035+ aufs_read_and_write_unlock2(d1, d2);
29036+ }
29037+
29038+out:
e49829fe 29039+ return err;
1facf9fc 29040+}
29041+
29042+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29043+{
29044+ di_write_unlock2(d1, d2);
29045+ si_read_unlock(d1->d_sb);
29046+}
7f207e10
AM
29047diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29048--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
be118d29
JR
29049+++ linux/fs/aufs/super.c 2018-04-06 07:48:44.207938097 +0200
29050@@ -0,0 +1,1051 @@
1facf9fc 29051+/*
a2654f78 29052+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 29053+ *
29054+ * This program, aufs is free software; you can redistribute it and/or modify
29055+ * it under the terms of the GNU General Public License as published by
29056+ * the Free Software Foundation; either version 2 of the License, or
29057+ * (at your option) any later version.
dece6358
AM
29058+ *
29059+ * This program is distributed in the hope that it will be useful,
29060+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29061+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29062+ * GNU General Public License for more details.
29063+ *
29064+ * You should have received a copy of the GNU General Public License
523b37e3 29065+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29066+ */
29067+
29068+/*
29069+ * mount and super_block operations
29070+ */
29071+
f6c5ef8b 29072+#include <linux/mm.h>
1facf9fc 29073+#include <linux/seq_file.h>
29074+#include <linux/statfs.h>
7f207e10 29075+#include <linux/vmalloc.h>
1facf9fc 29076+#include "aufs.h"
29077+
29078+/*
29079+ * super_operations
29080+ */
29081+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29082+{
29083+ struct au_icntnr *c;
29084+
29085+ c = au_cache_alloc_icntnr();
29086+ if (c) {
027c5e7a 29087+ au_icntnr_init(c);
be118d29 29088+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29089+ c->iinfo.ii_hinode = NULL;
29090+ return &c->vfs_inode;
29091+ }
29092+ return NULL;
29093+}
29094+
027c5e7a
AM
29095+static void aufs_destroy_inode_cb(struct rcu_head *head)
29096+{
29097+ struct inode *inode = container_of(head, struct inode, i_rcu);
29098+
1c60b727 29099+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
027c5e7a
AM
29100+}
29101+
1facf9fc 29102+static void aufs_destroy_inode(struct inode *inode)
29103+{
5afbbe0d
AM
29104+ if (!au_is_bad_inode(inode))
29105+ au_iinfo_fin(inode);
027c5e7a 29106+ call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
1facf9fc 29107+}
29108+
29109+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29110+{
29111+ struct inode *inode;
29112+ int err;
29113+
29114+ inode = iget_locked(sb, ino);
29115+ if (unlikely(!inode)) {
29116+ inode = ERR_PTR(-ENOMEM);
29117+ goto out;
29118+ }
29119+ if (!(inode->i_state & I_NEW))
29120+ goto out;
29121+
29122+ err = au_xigen_new(inode);
29123+ if (!err)
29124+ err = au_iinfo_init(inode);
29125+ if (!err)
be118d29 29126+ inode_inc_iversion(inode);
1facf9fc 29127+ else {
29128+ iget_failed(inode);
29129+ inode = ERR_PTR(err);
29130+ }
29131+
4f0767ce 29132+out:
1facf9fc 29133+ /* never return NULL */
29134+ AuDebugOn(!inode);
29135+ AuTraceErrPtr(inode);
29136+ return inode;
29137+}
29138+
29139+/* lock free root dinfo */
29140+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29141+{
29142+ int err;
5afbbe0d 29143+ aufs_bindex_t bindex, bbot;
1facf9fc 29144+ struct path path;
4a4d8108 29145+ struct au_hdentry *hdp;
1facf9fc 29146+ struct au_branch *br;
076b876e 29147+ au_br_perm_str_t perm;
1facf9fc 29148+
29149+ err = 0;
5afbbe0d
AM
29150+ bbot = au_sbbot(sb);
29151+ bindex = 0;
29152+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29153+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29154+ br = au_sbr(sb, bindex);
86dc4139 29155+ path.mnt = au_br_mnt(br);
5afbbe0d 29156+ path.dentry = hdp->hd_dentry;
1facf9fc 29157+ err = au_seq_path(seq, &path);
79b8bda9 29158+ if (!err) {
076b876e 29159+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29160+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29161+ if (bindex != bbot)
79b8bda9 29162+ seq_putc(seq, ':');
1e00d052 29163+ }
1facf9fc 29164+ }
79b8bda9
AM
29165+ if (unlikely(err || seq_has_overflowed(seq)))
29166+ err = -E2BIG;
1facf9fc 29167+
29168+ return err;
29169+}
29170+
f2c43d5f
AM
29171+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29172+ const char *append)
29173+{
29174+ char *p;
29175+
29176+ p = fmt;
29177+ while (*pat != ':')
29178+ *p++ = *pat++;
29179+ *p++ = *pat++;
29180+ strcpy(p, append);
29181+ AuDebugOn(strlen(fmt) >= len);
29182+}
29183+
1facf9fc 29184+static void au_show_wbr_create(struct seq_file *m, int v,
29185+ struct au_sbinfo *sbinfo)
29186+{
29187+ const char *pat;
f2c43d5f
AM
29188+ char fmt[32];
29189+ struct au_wbr_mfs *mfs;
1facf9fc 29190+
dece6358
AM
29191+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29192+
c2b27bf2 29193+ seq_puts(m, ",create=");
1facf9fc 29194+ pat = au_optstr_wbr_create(v);
f2c43d5f 29195+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29196+ switch (v) {
29197+ case AuWbrCreate_TDP:
29198+ case AuWbrCreate_RR:
29199+ case AuWbrCreate_MFS:
29200+ case AuWbrCreate_PMFS:
c2b27bf2 29201+ seq_puts(m, pat);
1facf9fc 29202+ break;
f2c43d5f
AM
29203+ case AuWbrCreate_MFSRR:
29204+ case AuWbrCreate_TDMFS:
29205+ case AuWbrCreate_PMFSRR:
29206+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29207+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29208+ break;
f2c43d5f 29209+ case AuWbrCreate_MFSV:
1facf9fc 29210+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29211+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29212+ seq_printf(m, fmt,
29213+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29214+ / MSEC_PER_SEC);
1facf9fc 29215+ break;
1facf9fc 29216+ case AuWbrCreate_MFSRRV:
f2c43d5f 29217+ case AuWbrCreate_TDMFSV:
392086de 29218+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29219+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29220+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29221+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29222+ break;
f2c43d5f
AM
29223+ default:
29224+ BUG();
1facf9fc 29225+ }
29226+}
29227+
7eafdf33 29228+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29229+{
29230+#ifdef CONFIG_SYSFS
29231+ return 0;
29232+#else
29233+ int err;
29234+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29235+ aufs_bindex_t bindex, brid;
1facf9fc 29236+ struct qstr *name;
29237+ struct file *f;
29238+ struct dentry *d, *h_root;
29239+
dece6358
AM
29240+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29241+
1facf9fc 29242+ err = 0;
1facf9fc 29243+ f = au_sbi(sb)->si_xib;
29244+ if (!f)
29245+ goto out;
29246+
29247+ /* stop printing the default xino path on the first writable branch */
29248+ h_root = NULL;
29249+ brid = au_xino_brid(sb);
29250+ if (brid >= 0) {
29251+ bindex = au_br_index(sb, brid);
5afbbe0d 29252+ h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
1facf9fc 29253+ }
2000de60 29254+ d = f->f_path.dentry;
1facf9fc 29255+ name = &d->d_name;
29256+ /* safe ->d_parent because the file is unlinked */
29257+ if (d->d_parent == h_root
29258+ && name->len == len
29259+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29260+ goto out;
29261+
29262+ seq_puts(seq, ",xino=");
29263+ err = au_xino_path(seq, f);
29264+
4f0767ce 29265+out:
1facf9fc 29266+ return err;
29267+#endif
29268+}
29269+
29270+/* seq_file will re-call me in case of too long string */
7eafdf33 29271+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29272+{
027c5e7a 29273+ int err;
1facf9fc 29274+ unsigned int mnt_flags, v;
29275+ struct super_block *sb;
29276+ struct au_sbinfo *sbinfo;
29277+
29278+#define AuBool(name, str) do { \
29279+ v = au_opt_test(mnt_flags, name); \
29280+ if (v != au_opt_test(AuOpt_Def, name)) \
29281+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29282+} while (0)
29283+
29284+#define AuStr(name, str) do { \
29285+ v = mnt_flags & AuOptMask_##name; \
29286+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29287+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29288+} while (0)
29289+
29290+#define AuUInt(name, str, val) do { \
29291+ if (val != AUFS_##name##_DEF) \
29292+ seq_printf(m, "," #str "=%u", val); \
29293+} while (0)
29294+
7eafdf33 29295+ sb = dentry->d_sb;
2121bcd9 29296+ if (sb->s_flags & SB_POSIXACL)
c1595e42 29297+ seq_puts(m, ",acl");
be118d29
JR
29298+#if 0
29299+ if (sb->s_flags & SB_I_VERSION)
29300+ seq_puts(m, ",i_version");
29301+#endif
c1595e42
JR
29302+
29303+ /* lock free root dinfo */
1facf9fc 29304+ si_noflush_read_lock(sb);
29305+ sbinfo = au_sbi(sb);
29306+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29307+
29308+ mnt_flags = au_mntflags(sb);
29309+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29310+ err = au_show_xino(m, sb);
1facf9fc 29311+ if (unlikely(err))
29312+ goto out;
29313+ } else
29314+ seq_puts(m, ",noxino");
29315+
29316+ AuBool(TRUNC_XINO, trunc_xino);
29317+ AuStr(UDBA, udba);
dece6358 29318+ AuBool(SHWH, shwh);
1facf9fc 29319+ AuBool(PLINK, plink);
4a4d8108 29320+ AuBool(DIO, dio);
076b876e 29321+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29322+
29323+ v = sbinfo->si_wbr_create;
29324+ if (v != AuWbrCreate_Def)
29325+ au_show_wbr_create(m, v, sbinfo);
29326+
29327+ v = sbinfo->si_wbr_copyup;
29328+ if (v != AuWbrCopyup_Def)
29329+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29330+
29331+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29332+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29333+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29334+
29335+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29336+
027c5e7a
AM
29337+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29338+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29339+
29340+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29341+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29342+
076b876e
AM
29343+ au_fhsm_show(m, sbinfo);
29344+
8b6a4947 29345+ AuBool(DIRREN, dirren);
1facf9fc 29346+ AuBool(SUM, sum);
29347+ /* AuBool(SUM_W, wsum); */
29348+ AuBool(WARN_PERM, warn_perm);
29349+ AuBool(VERBOSE, verbose);
29350+
4f0767ce 29351+out:
1facf9fc 29352+ /* be sure to print "br:" last */
29353+ if (!sysaufs_brs) {
29354+ seq_puts(m, ",br:");
29355+ au_show_brs(m, sb);
29356+ }
29357+ si_read_unlock(sb);
29358+ return 0;
29359+
1facf9fc 29360+#undef AuBool
29361+#undef AuStr
4a4d8108 29362+#undef AuUInt
1facf9fc 29363+}
29364+
29365+/* ---------------------------------------------------------------------- */
29366+
29367+/* sum mode which returns the summation for statfs(2) */
29368+
29369+static u64 au_add_till_max(u64 a, u64 b)
29370+{
29371+ u64 old;
29372+
29373+ old = a;
29374+ a += b;
92d182d2
AM
29375+ if (old <= a)
29376+ return a;
29377+ return ULLONG_MAX;
29378+}
29379+
29380+static u64 au_mul_till_max(u64 a, long mul)
29381+{
29382+ u64 old;
29383+
29384+ old = a;
29385+ a *= mul;
29386+ if (old <= a)
1facf9fc 29387+ return a;
29388+ return ULLONG_MAX;
29389+}
29390+
29391+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29392+{
29393+ int err;
92d182d2 29394+ long bsize, factor;
1facf9fc 29395+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29396+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29397+ unsigned char shared;
7f207e10 29398+ struct path h_path;
1facf9fc 29399+ struct super_block *h_sb;
29400+
92d182d2
AM
29401+ err = 0;
29402+ bsize = LONG_MAX;
29403+ files = 0;
29404+ ffree = 0;
1facf9fc 29405+ blocks = 0;
29406+ bfree = 0;
29407+ bavail = 0;
5afbbe0d
AM
29408+ bbot = au_sbbot(sb);
29409+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29410+ h_path.mnt = au_sbr_mnt(sb, bindex);
29411+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29412+ shared = 0;
92d182d2 29413+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29414+ shared = (au_sbr_sb(sb, i) == h_sb);
29415+ if (shared)
29416+ continue;
29417+
29418+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29419+ h_path.dentry = h_path.mnt->mnt_root;
29420+ err = vfs_statfs(&h_path, buf);
1facf9fc 29421+ if (unlikely(err))
29422+ goto out;
29423+
92d182d2
AM
29424+ if (bsize > buf->f_bsize) {
29425+ /*
29426+ * we will reduce bsize, so we have to expand blocks
29427+ * etc. to match them again
29428+ */
29429+ factor = (bsize / buf->f_bsize);
29430+ blocks = au_mul_till_max(blocks, factor);
29431+ bfree = au_mul_till_max(bfree, factor);
29432+ bavail = au_mul_till_max(bavail, factor);
29433+ bsize = buf->f_bsize;
29434+ }
29435+
29436+ factor = (buf->f_bsize / bsize);
29437+ blocks = au_add_till_max(blocks,
29438+ au_mul_till_max(buf->f_blocks, factor));
29439+ bfree = au_add_till_max(bfree,
29440+ au_mul_till_max(buf->f_bfree, factor));
29441+ bavail = au_add_till_max(bavail,
29442+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29443+ files = au_add_till_max(files, buf->f_files);
29444+ ffree = au_add_till_max(ffree, buf->f_ffree);
29445+ }
29446+
92d182d2 29447+ buf->f_bsize = bsize;
1facf9fc 29448+ buf->f_blocks = blocks;
29449+ buf->f_bfree = bfree;
29450+ buf->f_bavail = bavail;
29451+ buf->f_files = files;
29452+ buf->f_ffree = ffree;
92d182d2 29453+ buf->f_frsize = 0;
1facf9fc 29454+
4f0767ce 29455+out:
1facf9fc 29456+ return err;
29457+}
29458+
29459+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29460+{
29461+ int err;
7f207e10 29462+ struct path h_path;
1facf9fc 29463+ struct super_block *sb;
29464+
29465+ /* lock free root dinfo */
29466+ sb = dentry->d_sb;
29467+ si_noflush_read_lock(sb);
7f207e10 29468+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29469+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29470+ h_path.mnt = au_sbr_mnt(sb, 0);
29471+ h_path.dentry = h_path.mnt->mnt_root;
29472+ err = vfs_statfs(&h_path, buf);
29473+ } else
1facf9fc 29474+ err = au_statfs_sum(sb, buf);
29475+ si_read_unlock(sb);
29476+
29477+ if (!err) {
29478+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29479+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29480+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29481+ }
29482+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29483+
29484+ return err;
29485+}
29486+
29487+/* ---------------------------------------------------------------------- */
29488+
537831f9
AM
29489+static int aufs_sync_fs(struct super_block *sb, int wait)
29490+{
29491+ int err, e;
5afbbe0d 29492+ aufs_bindex_t bbot, bindex;
537831f9
AM
29493+ struct au_branch *br;
29494+ struct super_block *h_sb;
29495+
29496+ err = 0;
29497+ si_noflush_read_lock(sb);
5afbbe0d
AM
29498+ bbot = au_sbbot(sb);
29499+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29500+ br = au_sbr(sb, bindex);
29501+ if (!au_br_writable(br->br_perm))
29502+ continue;
29503+
29504+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29505+ e = vfsub_sync_filesystem(h_sb, wait);
29506+ if (unlikely(e && !err))
29507+ err = e;
29508+ /* go on even if an error happens */
537831f9
AM
29509+ }
29510+ si_read_unlock(sb);
29511+
29512+ return err;
29513+}
29514+
29515+/* ---------------------------------------------------------------------- */
29516+
1facf9fc 29517+/* final actions when unmounting a file system */
29518+static void aufs_put_super(struct super_block *sb)
29519+{
29520+ struct au_sbinfo *sbinfo;
29521+
29522+ sbinfo = au_sbi(sb);
29523+ if (!sbinfo)
29524+ return;
29525+
1facf9fc 29526+ dbgaufs_si_fin(sbinfo);
29527+ kobject_put(&sbinfo->si_kobj);
29528+}
29529+
29530+/* ---------------------------------------------------------------------- */
29531+
79b8bda9
AM
29532+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29533+ struct super_block *sb, void *arg)
7f207e10
AM
29534+{
29535+ void *array;
076b876e 29536+ unsigned long long n, sz;
7f207e10
AM
29537+
29538+ array = NULL;
29539+ n = 0;
29540+ if (!*hint)
29541+ goto out;
29542+
29543+ if (*hint > ULLONG_MAX / sizeof(array)) {
29544+ array = ERR_PTR(-EMFILE);
29545+ pr_err("hint %llu\n", *hint);
29546+ goto out;
29547+ }
29548+
076b876e
AM
29549+ sz = sizeof(array) * *hint;
29550+ array = kzalloc(sz, GFP_NOFS);
7f207e10 29551+ if (unlikely(!array))
076b876e 29552+ array = vzalloc(sz);
7f207e10
AM
29553+ if (unlikely(!array)) {
29554+ array = ERR_PTR(-ENOMEM);
29555+ goto out;
29556+ }
29557+
79b8bda9 29558+ n = cb(sb, array, *hint, arg);
7f207e10
AM
29559+ AuDebugOn(n > *hint);
29560+
29561+out:
29562+ *hint = n;
29563+ return array;
29564+}
29565+
79b8bda9 29566+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
29567+ unsigned long long max __maybe_unused,
29568+ void *arg)
29569+{
29570+ unsigned long long n;
29571+ struct inode **p, *inode;
29572+ struct list_head *head;
29573+
29574+ n = 0;
29575+ p = a;
29576+ head = arg;
79b8bda9 29577+ spin_lock(&sb->s_inode_list_lock);
7f207e10 29578+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
29579+ if (!au_is_bad_inode(inode)
29580+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
29581+ spin_lock(&inode->i_lock);
29582+ if (atomic_read(&inode->i_count)) {
29583+ au_igrab(inode);
29584+ *p++ = inode;
29585+ n++;
29586+ AuDebugOn(n > max);
29587+ }
29588+ spin_unlock(&inode->i_lock);
7f207e10
AM
29589+ }
29590+ }
79b8bda9 29591+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
29592+
29593+ return n;
29594+}
29595+
29596+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29597+{
5afbbe0d 29598+ *max = au_ninodes(sb);
79b8bda9 29599+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
29600+}
29601+
29602+void au_iarray_free(struct inode **a, unsigned long long max)
29603+{
29604+ unsigned long long ull;
29605+
29606+ for (ull = 0; ull < max; ull++)
29607+ iput(a[ull]);
be52b249 29608+ kvfree(a);
7f207e10
AM
29609+}
29610+
29611+/* ---------------------------------------------------------------------- */
29612+
1facf9fc 29613+/*
29614+ * refresh dentry and inode at remount time.
29615+ */
027c5e7a
AM
29616+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29617+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29618+ struct dentry *parent)
1facf9fc 29619+{
29620+ int err;
1facf9fc 29621+
29622+ di_write_lock_child(dentry);
1facf9fc 29623+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
29624+ err = au_refresh_dentry(dentry, parent);
29625+ if (!err && dir_flags)
5527c038 29626+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 29627+ di_read_unlock(parent, AuLock_IR);
1facf9fc 29628+ di_write_unlock(dentry);
29629+
29630+ return err;
29631+}
29632+
027c5e7a
AM
29633+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29634+ struct au_sbinfo *sbinfo,
b95c5147 29635+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 29636+{
027c5e7a
AM
29637+ int err;
29638+ struct dentry *parent;
027c5e7a
AM
29639+
29640+ err = 0;
29641+ parent = dget_parent(dentry);
29642+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
29643+ if (d_really_is_positive(dentry)) {
29644+ if (!d_is_dir(dentry))
027c5e7a
AM
29645+ err = au_do_refresh(dentry, /*dir_flags*/0,
29646+ parent);
29647+ else {
29648+ err = au_do_refresh(dentry, dir_flags, parent);
29649+ if (unlikely(err))
29650+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29651+ }
29652+ } else
29653+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29654+ AuDbgDentry(dentry);
29655+ }
29656+ dput(parent);
29657+
79b8bda9 29658+ if (!err) {
b95c5147 29659+ if (do_idop)
79b8bda9
AM
29660+ au_refresh_dop(dentry, /*force_reval*/0);
29661+ } else
29662+ au_refresh_dop(dentry, /*force_reval*/1);
29663+
027c5e7a
AM
29664+ AuTraceErr(err);
29665+ return err;
1facf9fc 29666+}
29667+
b95c5147 29668+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 29669+{
29670+ int err, i, j, ndentry, e;
027c5e7a 29671+ unsigned int sigen;
1facf9fc 29672+ struct au_dcsub_pages dpages;
29673+ struct au_dpage *dpage;
027c5e7a
AM
29674+ struct dentry **dentries, *d;
29675+ struct au_sbinfo *sbinfo;
29676+ struct dentry *root = sb->s_root;
5527c038 29677+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 29678+
b95c5147 29679+ if (do_idop)
79b8bda9
AM
29680+ au_refresh_dop(root, /*force_reval*/0);
29681+
027c5e7a
AM
29682+ err = au_dpages_init(&dpages, GFP_NOFS);
29683+ if (unlikely(err))
1facf9fc 29684+ goto out;
027c5e7a
AM
29685+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
29686+ if (unlikely(err))
1facf9fc 29687+ goto out_dpages;
1facf9fc 29688+
027c5e7a
AM
29689+ sigen = au_sigen(sb);
29690+ sbinfo = au_sbi(sb);
29691+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 29692+ dpage = dpages.dpages + i;
29693+ dentries = dpage->dentries;
29694+ ndentry = dpage->ndentry;
027c5e7a 29695+ for (j = 0; j < ndentry; j++) {
1facf9fc 29696+ d = dentries[j];
79b8bda9 29697+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 29698+ do_idop);
027c5e7a
AM
29699+ if (unlikely(e && !err))
29700+ err = e;
29701+ /* go on even err */
1facf9fc 29702+ }
29703+ }
29704+
4f0767ce 29705+out_dpages:
1facf9fc 29706+ au_dpages_free(&dpages);
4f0767ce 29707+out:
1facf9fc 29708+ return err;
29709+}
29710+
b95c5147 29711+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 29712+{
027c5e7a
AM
29713+ int err, e;
29714+ unsigned int sigen;
29715+ unsigned long long max, ull;
29716+ struct inode *inode, **array;
1facf9fc 29717+
027c5e7a
AM
29718+ array = au_iarray_alloc(sb, &max);
29719+ err = PTR_ERR(array);
29720+ if (IS_ERR(array))
29721+ goto out;
1facf9fc 29722+
29723+ err = 0;
027c5e7a
AM
29724+ sigen = au_sigen(sb);
29725+ for (ull = 0; ull < max; ull++) {
29726+ inode = array[ull];
076b876e
AM
29727+ if (unlikely(!inode))
29728+ break;
b95c5147
AM
29729+
29730+ e = 0;
29731+ ii_write_lock_child(inode);
537831f9 29732+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 29733+ e = au_refresh_hinode_self(inode);
1facf9fc 29734+ if (unlikely(e)) {
b95c5147 29735+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 29736+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 29737+ if (!err)
29738+ err = e;
29739+ /* go on even if err */
29740+ }
29741+ }
b95c5147
AM
29742+ if (!e && do_idop)
29743+ au_refresh_iop(inode, /*force_getattr*/0);
29744+ ii_write_unlock(inode);
1facf9fc 29745+ }
29746+
027c5e7a 29747+ au_iarray_free(array, max);
1facf9fc 29748+
4f0767ce 29749+out:
1facf9fc 29750+ return err;
29751+}
29752+
b95c5147 29753+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 29754+{
027c5e7a
AM
29755+ int err, e;
29756+ unsigned int udba;
5afbbe0d 29757+ aufs_bindex_t bindex, bbot;
1facf9fc 29758+ struct dentry *root;
29759+ struct inode *inode;
027c5e7a 29760+ struct au_branch *br;
79b8bda9 29761+ struct au_sbinfo *sbi;
1facf9fc 29762+
29763+ au_sigen_inc(sb);
79b8bda9
AM
29764+ sbi = au_sbi(sb);
29765+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 29766+
29767+ root = sb->s_root;
29768+ DiMustNoWaiters(root);
5527c038 29769+ inode = d_inode(root);
1facf9fc 29770+ IiMustNoWaiters(inode);
1facf9fc 29771+
027c5e7a 29772+ udba = au_opt_udba(sb);
5afbbe0d
AM
29773+ bbot = au_sbbot(sb);
29774+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
29775+ br = au_sbr(sb, bindex);
29776+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 29777+ if (unlikely(err))
027c5e7a
AM
29778+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
29779+ bindex, err);
29780+ /* go on even if err */
1facf9fc 29781+ }
027c5e7a 29782+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 29783+
b95c5147 29784+ if (do_idop) {
79b8bda9
AM
29785+ if (au_ftest_si(sbi, NO_DREVAL)) {
29786+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
29787+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
29788+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
29789+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
29790+ } else {
29791+ AuDebugOn(sb->s_d_op == &aufs_dop);
29792+ sb->s_d_op = &aufs_dop;
b95c5147
AM
29793+ AuDebugOn(sbi->si_iop_array == aufs_iop);
29794+ sbi->si_iop_array = aufs_iop;
79b8bda9 29795+ }
b95c5147
AM
29796+ pr_info("reset to %pf and %pf\n",
29797+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
29798+ }
29799+
027c5e7a 29800+ di_write_unlock(root);
b95c5147
AM
29801+ err = au_refresh_d(sb, do_idop);
29802+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
29803+ if (unlikely(e && !err))
29804+ err = e;
1facf9fc 29805+ /* aufs_write_lock() calls ..._child() */
29806+ di_write_lock_child(root);
027c5e7a
AM
29807+
29808+ au_cpup_attr_all(inode, /*force*/1);
29809+
29810+ if (unlikely(err))
29811+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 29812+}
29813+
29814+/* stop extra interpretation of errno in mount(8), and strange error messages */
29815+static int cvt_err(int err)
29816+{
29817+ AuTraceErr(err);
29818+
29819+ switch (err) {
29820+ case -ENOENT:
29821+ case -ENOTDIR:
29822+ case -EEXIST:
29823+ case -EIO:
29824+ err = -EINVAL;
29825+ }
29826+ return err;
29827+}
29828+
29829+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
29830+{
4a4d8108
AM
29831+ int err, do_dx;
29832+ unsigned int mntflags;
be52b249
AM
29833+ struct au_opts opts = {
29834+ .opt = NULL
29835+ };
1facf9fc 29836+ struct dentry *root;
29837+ struct inode *inode;
29838+ struct au_sbinfo *sbinfo;
29839+
29840+ err = 0;
29841+ root = sb->s_root;
29842+ if (!data || !*data) {
e49829fe
JR
29843+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
29844+ if (!err) {
29845+ di_write_lock_child(root);
29846+ err = au_opts_verify(sb, *flags, /*pending*/0);
29847+ aufs_write_unlock(root);
29848+ }
1facf9fc 29849+ goto out;
29850+ }
29851+
29852+ err = -ENOMEM;
1facf9fc 29853+ opts.opt = (void *)__get_free_page(GFP_NOFS);
29854+ if (unlikely(!opts.opt))
29855+ goto out;
29856+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
29857+ opts.flags = AuOpts_REMOUNT;
29858+ opts.sb_flags = *flags;
29859+
29860+ /* parse it before aufs lock */
29861+ err = au_opts_parse(sb, data, &opts);
29862+ if (unlikely(err))
29863+ goto out_opts;
29864+
29865+ sbinfo = au_sbi(sb);
5527c038 29866+ inode = d_inode(root);
febd17d6 29867+ inode_lock(inode);
e49829fe
JR
29868+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
29869+ if (unlikely(err))
29870+ goto out_mtx;
29871+ di_write_lock_child(root);
1facf9fc 29872+
29873+ /* au_opts_remount() may return an error */
29874+ err = au_opts_remount(sb, &opts);
29875+ au_opts_free(&opts);
29876+
027c5e7a 29877+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 29878+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 29879+
4a4d8108
AM
29880+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
29881+ mntflags = au_mntflags(sb);
29882+ do_dx = !!au_opt_test(mntflags, DIO);
29883+ au_dy_arefresh(do_dx);
29884+ }
29885+
076b876e 29886+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 29887+ aufs_write_unlock(root);
953406b4 29888+
e49829fe 29889+out_mtx:
febd17d6 29890+ inode_unlock(inode);
4f0767ce 29891+out_opts:
1c60b727 29892+ free_page((unsigned long)opts.opt);
4f0767ce 29893+out:
1facf9fc 29894+ err = cvt_err(err);
29895+ AuTraceErr(err);
29896+ return err;
29897+}
29898+
4a4d8108 29899+static const struct super_operations aufs_sop = {
1facf9fc 29900+ .alloc_inode = aufs_alloc_inode,
29901+ .destroy_inode = aufs_destroy_inode,
b752ccd1 29902+ /* always deleting, no clearing */
1facf9fc 29903+ .drop_inode = generic_delete_inode,
29904+ .show_options = aufs_show_options,
29905+ .statfs = aufs_statfs,
29906+ .put_super = aufs_put_super,
537831f9 29907+ .sync_fs = aufs_sync_fs,
1facf9fc 29908+ .remount_fs = aufs_remount_fs
29909+};
29910+
29911+/* ---------------------------------------------------------------------- */
29912+
29913+static int alloc_root(struct super_block *sb)
29914+{
29915+ int err;
29916+ struct inode *inode;
29917+ struct dentry *root;
29918+
29919+ err = -ENOMEM;
29920+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
29921+ err = PTR_ERR(inode);
29922+ if (IS_ERR(inode))
29923+ goto out;
29924+
b95c5147 29925+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 29926+ inode->i_fop = &aufs_dir_fop;
29927+ inode->i_mode = S_IFDIR;
9dbd164d 29928+ set_nlink(inode, 2);
1facf9fc 29929+ unlock_new_inode(inode);
29930+
92d182d2 29931+ root = d_make_root(inode);
1facf9fc 29932+ if (unlikely(!root))
92d182d2 29933+ goto out;
1facf9fc 29934+ err = PTR_ERR(root);
29935+ if (IS_ERR(root))
92d182d2 29936+ goto out;
1facf9fc 29937+
4a4d8108 29938+ err = au_di_init(root);
1facf9fc 29939+ if (!err) {
29940+ sb->s_root = root;
29941+ return 0; /* success */
29942+ }
29943+ dput(root);
1facf9fc 29944+
4f0767ce 29945+out:
1facf9fc 29946+ return err;
1facf9fc 29947+}
29948+
29949+static int aufs_fill_super(struct super_block *sb, void *raw_data,
29950+ int silent __maybe_unused)
29951+{
29952+ int err;
be52b249
AM
29953+ struct au_opts opts = {
29954+ .opt = NULL
29955+ };
79b8bda9 29956+ struct au_sbinfo *sbinfo;
1facf9fc 29957+ struct dentry *root;
29958+ struct inode *inode;
29959+ char *arg = raw_data;
29960+
29961+ if (unlikely(!arg || !*arg)) {
29962+ err = -EINVAL;
4a4d8108 29963+ pr_err("no arg\n");
1facf9fc 29964+ goto out;
29965+ }
29966+
29967+ err = -ENOMEM;
1facf9fc 29968+ opts.opt = (void *)__get_free_page(GFP_NOFS);
29969+ if (unlikely(!opts.opt))
29970+ goto out;
29971+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
29972+ opts.sb_flags = sb->s_flags;
29973+
29974+ err = au_si_alloc(sb);
29975+ if (unlikely(err))
29976+ goto out_opts;
79b8bda9 29977+ sbinfo = au_sbi(sb);
1facf9fc 29978+
29979+ /* all timestamps always follow the ones on the branch */
2121bcd9 29980+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
be118d29 29981+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
1facf9fc 29982+ sb->s_op = &aufs_sop;
027c5e7a 29983+ sb->s_d_op = &aufs_dop;
1facf9fc 29984+ sb->s_magic = AUFS_SUPER_MAGIC;
29985+ sb->s_maxbytes = 0;
c1595e42 29986+ sb->s_stack_depth = 1;
1facf9fc 29987+ au_export_init(sb);
f2c43d5f 29988+ au_xattr_init(sb);
1facf9fc 29989+
29990+ err = alloc_root(sb);
29991+ if (unlikely(err)) {
29992+ si_write_unlock(sb);
29993+ goto out_info;
29994+ }
29995+ root = sb->s_root;
5527c038 29996+ inode = d_inode(root);
1facf9fc 29997+
29998+ /*
29999+ * actually we can parse options regardless aufs lock here.
30000+ * but at remount time, parsing must be done before aufs lock.
30001+ * so we follow the same rule.
30002+ */
30003+ ii_write_lock_parent(inode);
30004+ aufs_write_unlock(root);
30005+ err = au_opts_parse(sb, arg, &opts);
30006+ if (unlikely(err))
30007+ goto out_root;
30008+
30009+ /* lock vfs_inode first, then aufs. */
febd17d6 30010+ inode_lock(inode);
1facf9fc 30011+ aufs_write_lock(root);
30012+ err = au_opts_mount(sb, &opts);
30013+ au_opts_free(&opts);
79b8bda9
AM
30014+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30015+ sb->s_d_op = &aufs_dop_noreval;
30016+ pr_info("%pf\n", sb->s_d_op);
30017+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30018+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30019+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30020+ }
1facf9fc 30021+ aufs_write_unlock(root);
febd17d6 30022+ inode_unlock(inode);
4a4d8108
AM
30023+ if (!err)
30024+ goto out_opts; /* success */
1facf9fc 30025+
4f0767ce 30026+out_root:
1facf9fc 30027+ dput(root);
30028+ sb->s_root = NULL;
4f0767ce 30029+out_info:
79b8bda9
AM
30030+ dbgaufs_si_fin(sbinfo);
30031+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30032+ sb->s_fs_info = NULL;
4f0767ce 30033+out_opts:
1c60b727 30034+ free_page((unsigned long)opts.opt);
4f0767ce 30035+out:
1facf9fc 30036+ AuTraceErr(err);
30037+ err = cvt_err(err);
30038+ AuTraceErr(err);
30039+ return err;
30040+}
30041+
30042+/* ---------------------------------------------------------------------- */
30043+
027c5e7a
AM
30044+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30045+ const char *dev_name __maybe_unused,
30046+ void *raw_data)
1facf9fc 30047+{
027c5e7a 30048+ struct dentry *root;
1facf9fc 30049+ struct super_block *sb;
30050+
30051+ /* all timestamps always follow the ones on the branch */
30052+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30053+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30054+ if (IS_ERR(root))
30055+ goto out;
30056+
30057+ sb = root->d_sb;
30058+ si_write_lock(sb, !AuLock_FLUSH);
30059+ sysaufs_brs_add(sb, 0);
30060+ si_write_unlock(sb);
30061+ au_sbilist_add(sb);
30062+
30063+out:
30064+ return root;
1facf9fc 30065+}
30066+
e49829fe
JR
30067+static void aufs_kill_sb(struct super_block *sb)
30068+{
30069+ struct au_sbinfo *sbinfo;
30070+
30071+ sbinfo = au_sbi(sb);
30072+ if (sbinfo) {
30073+ au_sbilist_del(sb);
30074+ aufs_write_lock(sb->s_root);
076b876e 30075+ au_fhsm_fin(sb);
e49829fe
JR
30076+ if (sbinfo->si_wbr_create_ops->fin)
30077+ sbinfo->si_wbr_create_ops->fin(sb);
30078+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30079+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30080+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30081+ }
30082+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30083+ au_plink_put(sb, /*verbose*/1);
30084+ au_xino_clr(sb);
8b6a4947 30085+ au_dr_opt_flush(sb);
1e00d052 30086+ sbinfo->si_sb = NULL;
e49829fe 30087+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30088+ au_nwt_flush(&sbinfo->si_nowait);
30089+ }
98d9a5b1 30090+ kill_anon_super(sb);
e49829fe
JR
30091+}
30092+
1facf9fc 30093+struct file_system_type aufs_fs_type = {
30094+ .name = AUFS_FSTYPE,
c06a8ce3
AM
30095+ /* a race between rename and others */
30096+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 30097+ .mount = aufs_mount,
e49829fe 30098+ .kill_sb = aufs_kill_sb,
1facf9fc 30099+ /* no need to __module_get() and module_put(). */
30100+ .owner = THIS_MODULE,
30101+};
7f207e10
AM
30102diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30103--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
be118d29 30104+++ linux/fs/aufs/super.h 2018-04-06 07:48:44.207938097 +0200
8b6a4947 30105@@ -0,0 +1,626 @@
1facf9fc 30106+/*
a2654f78 30107+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 30108+ *
30109+ * This program, aufs is free software; you can redistribute it and/or modify
30110+ * it under the terms of the GNU General Public License as published by
30111+ * the Free Software Foundation; either version 2 of the License, or
30112+ * (at your option) any later version.
dece6358
AM
30113+ *
30114+ * This program is distributed in the hope that it will be useful,
30115+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30116+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30117+ * GNU General Public License for more details.
30118+ *
30119+ * You should have received a copy of the GNU General Public License
523b37e3 30120+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30121+ */
30122+
30123+/*
30124+ * super_block operations
30125+ */
30126+
30127+#ifndef __AUFS_SUPER_H__
30128+#define __AUFS_SUPER_H__
30129+
30130+#ifdef __KERNEL__
30131+
30132+#include <linux/fs.h>
5527c038 30133+#include <linux/kobject.h>
8b6a4947 30134+#include "hbl.h"
1facf9fc 30135+#include "rwsem.h"
1facf9fc 30136+#include "wkq.h"
30137+
1facf9fc 30138+/* policies to select one among multiple writable branches */
30139+struct au_wbr_copyup_operations {
30140+ int (*copyup)(struct dentry *dentry);
30141+};
30142+
392086de
AM
30143+#define AuWbr_DIR 1 /* target is a dir */
30144+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30145+
30146+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30147+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30148+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30149+
1facf9fc 30150+struct au_wbr_create_operations {
392086de 30151+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30152+ int (*init)(struct super_block *sb);
30153+ int (*fin)(struct super_block *sb);
30154+};
30155+
30156+struct au_wbr_mfs {
30157+ struct mutex mfs_lock; /* protect this structure */
30158+ unsigned long mfs_jiffy;
30159+ unsigned long mfs_expire;
30160+ aufs_bindex_t mfs_bindex;
30161+
30162+ unsigned long long mfsrr_bytes;
30163+ unsigned long long mfsrr_watermark;
30164+};
30165+
86dc4139
AM
30166+#define AuPlink_NHASH 100
30167+static inline int au_plink_hash(ino_t ino)
30168+{
30169+ return ino % AuPlink_NHASH;
30170+}
30171+
076b876e
AM
30172+/* File-based Hierarchical Storage Management */
30173+struct au_fhsm {
30174+#ifdef CONFIG_AUFS_FHSM
30175+ /* allow only one process who can receive the notification */
30176+ spinlock_t fhsm_spin;
30177+ pid_t fhsm_pid;
30178+ wait_queue_head_t fhsm_wqh;
30179+ atomic_t fhsm_readable;
30180+
c1595e42 30181+ /* these are protected by si_rwsem */
076b876e 30182+ unsigned long fhsm_expire;
c1595e42 30183+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30184+#endif
30185+};
30186+
1facf9fc 30187+struct au_branch;
30188+struct au_sbinfo {
30189+ /* nowait tasks in the system-wide workqueue */
30190+ struct au_nowait_tasks si_nowait;
30191+
b752ccd1
AM
30192+ /*
30193+ * tried sb->s_umount, but failed due to the dependecy between i_mutex.
30194+ * rwsem for au_sbinfo is necessary.
30195+ */
dece6358 30196+ struct au_rwsem si_rwsem;
1facf9fc 30197+
7f207e10 30198+ /*
523b37e3
AM
30199+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30200+ * remount.
7f207e10 30201+ */
5afbbe0d 30202+ struct percpu_counter si_ninodes, si_nfiles;
7f207e10 30203+
1facf9fc 30204+ /* branch management */
30205+ unsigned int si_generation;
30206+
2000de60 30207+ /* see AuSi_ flags */
1facf9fc 30208+ unsigned char au_si_status;
30209+
5afbbe0d 30210+ aufs_bindex_t si_bbot;
7f207e10
AM
30211+
30212+ /* dirty trick to keep br_id plus */
30213+ unsigned int si_last_br_id :
30214+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30215+ struct au_branch **si_branch;
30216+
30217+ /* policy to select a writable branch */
30218+ unsigned char si_wbr_copyup;
30219+ unsigned char si_wbr_create;
30220+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30221+ struct au_wbr_create_operations *si_wbr_create_ops;
30222+
30223+ /* round robin */
30224+ atomic_t si_wbr_rr_next;
30225+
30226+ /* most free space */
30227+ struct au_wbr_mfs si_wbr_mfs;
30228+
076b876e
AM
30229+ /* File-based Hierarchical Storage Management */
30230+ struct au_fhsm si_fhsm;
30231+
1facf9fc 30232+ /* mount flags */
30233+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30234+ unsigned int si_mntflags;
30235+
30236+ /* external inode number (bitmap and translation table) */
5527c038
JR
30237+ vfs_readf_t si_xread;
30238+ vfs_writef_t si_xwrite;
1facf9fc 30239+ struct file *si_xib;
30240+ struct mutex si_xib_mtx; /* protect xib members */
30241+ unsigned long *si_xib_buf;
30242+ unsigned long si_xib_last_pindex;
30243+ int si_xib_next_bit;
30244+ aufs_bindex_t si_xino_brid;
392086de
AM
30245+ unsigned long si_xino_jiffy;
30246+ unsigned long si_xino_expire;
1facf9fc 30247+ /* reserved for future use */
30248+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30249+
30250+#ifdef CONFIG_AUFS_EXPORT
30251+ /* i_generation */
30252+ struct file *si_xigen;
30253+ atomic_t si_xigen_next;
30254+#endif
30255+
b912730e 30256+ /* dirty trick to suppoer atomic_open */
8b6a4947 30257+ struct hlist_bl_head si_aopen;
b912730e 30258+
1facf9fc 30259+ /* vdir parameters */
e49829fe 30260+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30261+ unsigned int si_rdblk; /* deblk size */
30262+ unsigned int si_rdhash; /* hash size */
30263+
30264+ /*
30265+ * If the number of whiteouts are larger than si_dirwh, leave all of
30266+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30267+ * future fsck.aufs or kernel thread will remove them later.
30268+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30269+ */
30270+ unsigned int si_dirwh;
30271+
1facf9fc 30272+ /* pseudo_link list */
8b6a4947 30273+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30274+ wait_queue_head_t si_plink_wq;
4a4d8108 30275+ spinlock_t si_plink_maint_lock;
e49829fe 30276+ pid_t si_plink_maint_pid;
1facf9fc 30277+
523b37e3 30278+ /* file list */
8b6a4947 30279+ struct hlist_bl_head si_files;
523b37e3 30280+
b95c5147
AM
30281+ /* with/without getattr, brother of sb->s_d_op */
30282+ struct inode_operations *si_iop_array;
30283+
1facf9fc 30284+ /*
30285+ * sysfs and lifetime management.
30286+ * this is not a small structure and it may be a waste of memory in case
30287+ * of sysfs is disabled, particulary when many aufs-es are mounted.
30288+ * but using sysfs is majority.
30289+ */
30290+ struct kobject si_kobj;
30291+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30292+ struct dentry *si_dbgaufs;
30293+ struct dentry *si_dbgaufs_plink;
30294+ struct dentry *si_dbgaufs_xib;
1facf9fc 30295+#ifdef CONFIG_AUFS_EXPORT
30296+ struct dentry *si_dbgaufs_xigen;
30297+#endif
30298+#endif
30299+
e49829fe 30300+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30301+ struct hlist_bl_node si_list;
e49829fe
JR
30302+#endif
30303+
1facf9fc 30304+ /* dirty, necessary for unmounting, sysfs and sysrq */
30305+ struct super_block *si_sb;
30306+};
30307+
dece6358
AM
30308+/* sbinfo status flags */
30309+/*
30310+ * set true when refresh_dirs() failed at remount time.
30311+ * then try refreshing dirs at access time again.
30312+ * if it is false, refreshing dirs at access time is unnecesary
30313+ */
027c5e7a 30314+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30315+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30316+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30317+
30318+#ifndef CONFIG_AUFS_FHSM
30319+#undef AuSi_FHSM
30320+#define AuSi_FHSM 0
30321+#endif
30322+
dece6358
AM
30323+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30324+ unsigned int flag)
30325+{
30326+ AuRwMustAnyLock(&sbi->si_rwsem);
30327+ return sbi->au_si_status & flag;
30328+}
30329+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30330+#define au_fset_si(sbinfo, name) do { \
30331+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30332+ (sbinfo)->au_si_status |= AuSi_##name; \
30333+} while (0)
30334+#define au_fclr_si(sbinfo, name) do { \
30335+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30336+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30337+} while (0)
30338+
1facf9fc 30339+/* ---------------------------------------------------------------------- */
30340+
30341+/* policy to select one among writable branches */
4a4d8108
AM
30342+#define AuWbrCopyup(sbinfo, ...) \
30343+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30344+#define AuWbrCreate(sbinfo, ...) \
30345+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30346+
30347+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30348+#define AuLock_DW 1 /* write-lock dentry */
30349+#define AuLock_IR (1 << 1) /* read-lock inode */
30350+#define AuLock_IW (1 << 2) /* write-lock inode */
30351+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30352+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30353+ /* except RENAME_EXCHANGE */
e49829fe
JR
30354+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30355+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30356+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30357+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30358+#define au_fset_lock(flags, name) \
30359+ do { (flags) |= AuLock_##name; } while (0)
30360+#define au_fclr_lock(flags, name) \
30361+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30362+
30363+/* ---------------------------------------------------------------------- */
30364+
30365+/* super.c */
30366+extern struct file_system_type aufs_fs_type;
30367+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30368+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30369+ unsigned long long max, void *arg);
79b8bda9
AM
30370+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30371+ struct super_block *sb, void *arg);
7f207e10
AM
30372+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30373+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30374+
30375+/* sbinfo.c */
30376+void au_si_free(struct kobject *kobj);
30377+int au_si_alloc(struct super_block *sb);
e2f27e51 30378+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30379+
30380+unsigned int au_sigen_inc(struct super_block *sb);
30381+aufs_bindex_t au_new_br_id(struct super_block *sb);
30382+
e49829fe
JR
30383+int si_read_lock(struct super_block *sb, int flags);
30384+int si_write_lock(struct super_block *sb, int flags);
30385+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30386+void aufs_read_unlock(struct dentry *dentry, int flags);
30387+void aufs_write_lock(struct dentry *dentry);
30388+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30389+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30390+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30391+
30392+/* wbr_policy.c */
30393+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30394+extern struct au_wbr_create_operations au_wbr_create_ops[];
30395+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30396+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30397+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30398+
30399+/* mvdown.c */
30400+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30401+
076b876e
AM
30402+#ifdef CONFIG_AUFS_FHSM
30403+/* fhsm.c */
30404+
30405+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30406+{
30407+ pid_t pid;
30408+
30409+ spin_lock(&fhsm->fhsm_spin);
30410+ pid = fhsm->fhsm_pid;
30411+ spin_unlock(&fhsm->fhsm_spin);
30412+
30413+ return pid;
30414+}
30415+
30416+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30417+void au_fhsm_wrote_all(struct super_block *sb, int force);
30418+int au_fhsm_fd(struct super_block *sb, int oflags);
30419+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30420+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30421+void au_fhsm_fin(struct super_block *sb);
30422+void au_fhsm_init(struct au_sbinfo *sbinfo);
30423+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30424+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30425+#else
30426+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30427+ int force)
30428+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30429+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30430+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30431+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30432+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30433+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30434+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30435+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30436+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30437+#endif
30438+
1facf9fc 30439+/* ---------------------------------------------------------------------- */
30440+
30441+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30442+{
30443+ return sb->s_fs_info;
30444+}
30445+
30446+/* ---------------------------------------------------------------------- */
30447+
30448+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30449+int au_test_nfsd(void);
1facf9fc 30450+void au_export_init(struct super_block *sb);
b752ccd1 30451+void au_xigen_inc(struct inode *inode);
1facf9fc 30452+int au_xigen_new(struct inode *inode);
30453+int au_xigen_set(struct super_block *sb, struct file *base);
30454+void au_xigen_clr(struct super_block *sb);
30455+
30456+static inline int au_busy_or_stale(void)
30457+{
b752ccd1 30458+ if (!au_test_nfsd())
1facf9fc 30459+ return -EBUSY;
30460+ return -ESTALE;
30461+}
30462+#else
b752ccd1 30463+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30464+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30465+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108
AM
30466+AuStubInt0(au_xigen_new, struct inode *inode)
30467+AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
30468+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30469+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30470+#endif /* CONFIG_AUFS_EXPORT */
30471+
30472+/* ---------------------------------------------------------------------- */
30473+
e49829fe
JR
30474+#ifdef CONFIG_AUFS_SBILIST
30475+/* module.c */
8b6a4947 30476+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30477+
30478+static inline void au_sbilist_init(void)
30479+{
8b6a4947 30480+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30481+}
30482+
30483+static inline void au_sbilist_add(struct super_block *sb)
30484+{
8b6a4947 30485+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30486+}
30487+
30488+static inline void au_sbilist_del(struct super_block *sb)
30489+{
8b6a4947 30490+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30491+}
53392da6
AM
30492+
30493+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30494+static inline void au_sbilist_lock(void)
30495+{
8b6a4947 30496+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30497+}
30498+
30499+static inline void au_sbilist_unlock(void)
30500+{
8b6a4947 30501+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30502+}
30503+#define AuGFP_SBILIST GFP_ATOMIC
30504+#else
30505+AuStubVoid(au_sbilist_lock, void)
30506+AuStubVoid(au_sbilist_unlock, void)
30507+#define AuGFP_SBILIST GFP_NOFS
30508+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
30509+#else
30510+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
30511+AuStubVoid(au_sbilist_add, struct super_block *sb)
30512+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
30513+AuStubVoid(au_sbilist_lock, void)
30514+AuStubVoid(au_sbilist_unlock, void)
30515+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
30516+#endif
30517+
30518+/* ---------------------------------------------------------------------- */
30519+
1facf9fc 30520+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30521+{
dece6358 30522+ /*
c1595e42 30523+ * This function is a dynamic '__init' function actually,
dece6358
AM
30524+ * so the tiny check for si_rwsem is unnecessary.
30525+ */
30526+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 30527+#ifdef CONFIG_DEBUG_FS
30528+ sbinfo->si_dbgaufs = NULL;
86dc4139 30529+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 30530+ sbinfo->si_dbgaufs_xib = NULL;
30531+#ifdef CONFIG_AUFS_EXPORT
30532+ sbinfo->si_dbgaufs_xigen = NULL;
30533+#endif
30534+#endif
30535+}
30536+
30537+/* ---------------------------------------------------------------------- */
30538+
a2654f78
AM
30539+/* current->atomic_flags */
30540+/* this value should never corrupt the ones defined in linux/sched.h */
30541+#define PFA_AUFS 7
30542+
30543+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30544+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
30545+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
30546+
30547+static inline int si_pid_test(struct super_block *sb)
30548+{
a2654f78 30549+ return !!task_test_aufs(current);
b752ccd1
AM
30550+}
30551+
30552+static inline void si_pid_clr(struct super_block *sb)
30553+{
a2654f78
AM
30554+ AuDebugOn(!task_test_aufs(current));
30555+ task_clear_aufs(current);
b752ccd1
AM
30556+}
30557+
a2654f78
AM
30558+static inline void si_pid_set(struct super_block *sb)
30559+{
30560+ AuDebugOn(task_test_aufs(current));
30561+ task_set_aufs(current);
30562+}
febd17d6 30563+
b752ccd1
AM
30564+/* ---------------------------------------------------------------------- */
30565+
1facf9fc 30566+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
30567+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30568+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30569+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30570+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30571+/*
30572+#define __si_read_trylock_nested(sb) \
30573+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30574+#define __si_write_trylock_nested(sb) \
30575+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30576+*/
30577+
30578+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30579+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30580+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 30581+
dece6358
AM
30582+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30583+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30584+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30585+
b752ccd1
AM
30586+static inline void si_noflush_read_lock(struct super_block *sb)
30587+{
30588+ __si_read_lock(sb);
30589+ si_pid_set(sb);
30590+}
30591+
30592+static inline int si_noflush_read_trylock(struct super_block *sb)
30593+{
076b876e
AM
30594+ int locked;
30595+
30596+ locked = __si_read_trylock(sb);
b752ccd1
AM
30597+ if (locked)
30598+ si_pid_set(sb);
30599+ return locked;
30600+}
30601+
30602+static inline void si_noflush_write_lock(struct super_block *sb)
30603+{
30604+ __si_write_lock(sb);
30605+ si_pid_set(sb);
30606+}
30607+
30608+static inline int si_noflush_write_trylock(struct super_block *sb)
30609+{
076b876e
AM
30610+ int locked;
30611+
30612+ locked = __si_write_trylock(sb);
b752ccd1
AM
30613+ if (locked)
30614+ si_pid_set(sb);
30615+ return locked;
30616+}
30617+
7e9cd9fe 30618+#if 0 /* reserved */
1facf9fc 30619+static inline int si_read_trylock(struct super_block *sb, int flags)
30620+{
30621+ if (au_ftest_lock(flags, FLUSH))
30622+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30623+ return si_noflush_read_trylock(sb);
30624+}
e49829fe 30625+#endif
1facf9fc 30626+
b752ccd1
AM
30627+static inline void si_read_unlock(struct super_block *sb)
30628+{
30629+ si_pid_clr(sb);
30630+ __si_read_unlock(sb);
30631+}
30632+
7e9cd9fe 30633+#if 0 /* reserved */
1facf9fc 30634+static inline int si_write_trylock(struct super_block *sb, int flags)
30635+{
30636+ if (au_ftest_lock(flags, FLUSH))
30637+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30638+ return si_noflush_write_trylock(sb);
30639+}
b752ccd1
AM
30640+#endif
30641+
30642+static inline void si_write_unlock(struct super_block *sb)
30643+{
30644+ si_pid_clr(sb);
30645+ __si_write_unlock(sb);
30646+}
30647+
7e9cd9fe 30648+#if 0 /* reserved */
b752ccd1
AM
30649+static inline void si_downgrade_lock(struct super_block *sb)
30650+{
30651+ __si_downgrade_lock(sb);
30652+}
30653+#endif
1facf9fc 30654+
30655+/* ---------------------------------------------------------------------- */
30656+
5afbbe0d 30657+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 30658+{
dece6358 30659+ SiMustAnyLock(sb);
5afbbe0d 30660+ return au_sbi(sb)->si_bbot;
1facf9fc 30661+}
30662+
30663+static inline unsigned int au_mntflags(struct super_block *sb)
30664+{
dece6358 30665+ SiMustAnyLock(sb);
1facf9fc 30666+ return au_sbi(sb)->si_mntflags;
30667+}
30668+
30669+static inline unsigned int au_sigen(struct super_block *sb)
30670+{
dece6358 30671+ SiMustAnyLock(sb);
1facf9fc 30672+ return au_sbi(sb)->si_generation;
30673+}
30674+
5afbbe0d
AM
30675+static inline unsigned long long au_ninodes(struct super_block *sb)
30676+{
30677+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
30678+
30679+ BUG_ON(n < 0);
30680+ return n;
30681+}
30682+
7f207e10
AM
30683+static inline void au_ninodes_inc(struct super_block *sb)
30684+{
5afbbe0d 30685+ percpu_counter_inc(&au_sbi(sb)->si_ninodes);
7f207e10
AM
30686+}
30687+
30688+static inline void au_ninodes_dec(struct super_block *sb)
30689+{
5afbbe0d
AM
30690+ percpu_counter_dec(&au_sbi(sb)->si_ninodes);
30691+}
30692+
30693+static inline unsigned long long au_nfiles(struct super_block *sb)
30694+{
30695+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
30696+
30697+ BUG_ON(n < 0);
30698+ return n;
7f207e10
AM
30699+}
30700+
30701+static inline void au_nfiles_inc(struct super_block *sb)
30702+{
5afbbe0d 30703+ percpu_counter_inc(&au_sbi(sb)->si_nfiles);
7f207e10
AM
30704+}
30705+
30706+static inline void au_nfiles_dec(struct super_block *sb)
30707+{
5afbbe0d 30708+ percpu_counter_dec(&au_sbi(sb)->si_nfiles);
7f207e10
AM
30709+}
30710+
1facf9fc 30711+static inline struct au_branch *au_sbr(struct super_block *sb,
30712+ aufs_bindex_t bindex)
30713+{
dece6358 30714+ SiMustAnyLock(sb);
1facf9fc 30715+ return au_sbi(sb)->si_branch[0 + bindex];
30716+}
30717+
30718+static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
30719+{
dece6358 30720+ SiMustWriteLock(sb);
1facf9fc 30721+ au_sbi(sb)->si_xino_brid = brid;
30722+}
30723+
30724+static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
30725+{
dece6358 30726+ SiMustAnyLock(sb);
1facf9fc 30727+ return au_sbi(sb)->si_xino_brid;
30728+}
30729+
30730+#endif /* __KERNEL__ */
30731+#endif /* __AUFS_SUPER_H__ */
7f207e10
AM
30732diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
30733--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
be118d29 30734+++ linux/fs/aufs/sysaufs.c 2018-04-06 07:48:44.207938097 +0200
523b37e3 30735@@ -0,0 +1,104 @@
1facf9fc 30736+/*
a2654f78 30737+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 30738+ *
30739+ * This program, aufs is free software; you can redistribute it and/or modify
30740+ * it under the terms of the GNU General Public License as published by
30741+ * the Free Software Foundation; either version 2 of the License, or
30742+ * (at your option) any later version.
dece6358
AM
30743+ *
30744+ * This program is distributed in the hope that it will be useful,
30745+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30746+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30747+ * GNU General Public License for more details.
30748+ *
30749+ * You should have received a copy of the GNU General Public License
523b37e3 30750+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30751+ */
30752+
30753+/*
30754+ * sysfs interface and lifetime management
30755+ * they are necessary regardless sysfs is disabled.
30756+ */
30757+
1facf9fc 30758+#include <linux/random.h>
1facf9fc 30759+#include "aufs.h"
30760+
30761+unsigned long sysaufs_si_mask;
e49829fe 30762+struct kset *sysaufs_kset;
1facf9fc 30763+
30764+#define AuSiAttr(_name) { \
30765+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
30766+ .show = sysaufs_si_##_name, \
30767+}
30768+
30769+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
30770+struct attribute *sysaufs_si_attrs[] = {
30771+ &sysaufs_si_attr_xi_path.attr,
30772+ NULL,
30773+};
30774+
4a4d8108 30775+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 30776+ .show = sysaufs_si_show
30777+};
30778+
30779+static struct kobj_type au_sbi_ktype = {
30780+ .release = au_si_free,
30781+ .sysfs_ops = &au_sbi_ops,
30782+ .default_attrs = sysaufs_si_attrs
30783+};
30784+
30785+/* ---------------------------------------------------------------------- */
30786+
30787+int sysaufs_si_init(struct au_sbinfo *sbinfo)
30788+{
30789+ int err;
30790+
e49829fe 30791+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 30792+ /* cf. sysaufs_name() */
30793+ err = kobject_init_and_add
e49829fe 30794+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 30795+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
30796+
30797+ dbgaufs_si_null(sbinfo);
30798+ if (!err) {
30799+ err = dbgaufs_si_init(sbinfo);
30800+ if (unlikely(err))
30801+ kobject_put(&sbinfo->si_kobj);
30802+ }
30803+ return err;
30804+}
30805+
30806+void sysaufs_fin(void)
30807+{
30808+ dbgaufs_fin();
e49829fe
JR
30809+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
30810+ kset_unregister(sysaufs_kset);
1facf9fc 30811+}
30812+
30813+int __init sysaufs_init(void)
30814+{
30815+ int err;
30816+
30817+ do {
30818+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
30819+ } while (!sysaufs_si_mask);
30820+
4a4d8108 30821+ err = -EINVAL;
e49829fe
JR
30822+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
30823+ if (unlikely(!sysaufs_kset))
4a4d8108 30824+ goto out;
e49829fe
JR
30825+ err = PTR_ERR(sysaufs_kset);
30826+ if (IS_ERR(sysaufs_kset))
1facf9fc 30827+ goto out;
e49829fe 30828+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
1facf9fc 30829+ if (unlikely(err)) {
e49829fe 30830+ kset_unregister(sysaufs_kset);
1facf9fc 30831+ goto out;
30832+ }
30833+
30834+ err = dbgaufs_init();
30835+ if (unlikely(err))
30836+ sysaufs_fin();
4f0767ce 30837+out:
1facf9fc 30838+ return err;
30839+}
7f207e10
AM
30840diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
30841--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
be118d29 30842+++ linux/fs/aufs/sysaufs.h 2018-04-06 07:48:44.207938097 +0200
c1595e42 30843@@ -0,0 +1,101 @@
1facf9fc 30844+/*
a2654f78 30845+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 30846+ *
30847+ * This program, aufs is free software; you can redistribute it and/or modify
30848+ * it under the terms of the GNU General Public License as published by
30849+ * the Free Software Foundation; either version 2 of the License, or
30850+ * (at your option) any later version.
dece6358
AM
30851+ *
30852+ * This program is distributed in the hope that it will be useful,
30853+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30854+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30855+ * GNU General Public License for more details.
30856+ *
30857+ * You should have received a copy of the GNU General Public License
523b37e3 30858+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30859+ */
30860+
30861+/*
30862+ * sysfs interface and mount lifetime management
30863+ */
30864+
30865+#ifndef __SYSAUFS_H__
30866+#define __SYSAUFS_H__
30867+
30868+#ifdef __KERNEL__
30869+
1facf9fc 30870+#include <linux/sysfs.h>
1facf9fc 30871+#include "module.h"
30872+
dece6358
AM
30873+struct super_block;
30874+struct au_sbinfo;
30875+
1facf9fc 30876+struct sysaufs_si_attr {
30877+ struct attribute attr;
30878+ int (*show)(struct seq_file *seq, struct super_block *sb);
30879+};
30880+
30881+/* ---------------------------------------------------------------------- */
30882+
30883+/* sysaufs.c */
30884+extern unsigned long sysaufs_si_mask;
e49829fe 30885+extern struct kset *sysaufs_kset;
1facf9fc 30886+extern struct attribute *sysaufs_si_attrs[];
30887+int sysaufs_si_init(struct au_sbinfo *sbinfo);
30888+int __init sysaufs_init(void);
30889+void sysaufs_fin(void);
30890+
30891+/* ---------------------------------------------------------------------- */
30892+
30893+/* some people doesn't like to show a pointer in kernel */
30894+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
30895+{
30896+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
30897+}
30898+
30899+#define SysaufsSiNamePrefix "si_"
30900+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
30901+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
30902+{
30903+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
30904+ sysaufs_si_id(sbinfo));
30905+}
30906+
30907+struct au_branch;
30908+#ifdef CONFIG_SYSFS
30909+/* sysfs.c */
30910+extern struct attribute_group *sysaufs_attr_group;
30911+
30912+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
30913+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
30914+ char *buf);
076b876e
AM
30915+long au_brinfo_ioctl(struct file *file, unsigned long arg);
30916+#ifdef CONFIG_COMPAT
30917+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
30918+#endif
1facf9fc 30919+
30920+void sysaufs_br_init(struct au_branch *br);
30921+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
30922+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
30923+
30924+#define sysaufs_brs_init() do {} while (0)
30925+
30926+#else
30927+#define sysaufs_attr_group NULL
30928+
4a4d8108 30929+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
30930+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
30931+ struct attribute *attr, char *buf)
4a4d8108
AM
30932+AuStubVoid(sysaufs_br_init, struct au_branch *br)
30933+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
30934+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 30935+
30936+static inline void sysaufs_brs_init(void)
30937+{
30938+ sysaufs_brs = 0;
30939+}
30940+
30941+#endif /* CONFIG_SYSFS */
30942+
30943+#endif /* __KERNEL__ */
30944+#endif /* __SYSAUFS_H__ */
7f207e10
AM
30945diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
30946--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
be118d29 30947+++ linux/fs/aufs/sysfs.c 2018-04-06 07:48:44.207938097 +0200
79b8bda9 30948@@ -0,0 +1,376 @@
1facf9fc 30949+/*
a2654f78 30950+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 30951+ *
30952+ * This program, aufs is free software; you can redistribute it and/or modify
30953+ * it under the terms of the GNU General Public License as published by
30954+ * the Free Software Foundation; either version 2 of the License, or
30955+ * (at your option) any later version.
dece6358
AM
30956+ *
30957+ * This program is distributed in the hope that it will be useful,
30958+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30959+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30960+ * GNU General Public License for more details.
30961+ *
30962+ * You should have received a copy of the GNU General Public License
523b37e3 30963+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30964+ */
30965+
30966+/*
30967+ * sysfs interface
30968+ */
30969+
076b876e 30970+#include <linux/compat.h>
1facf9fc 30971+#include <linux/seq_file.h>
1facf9fc 30972+#include "aufs.h"
30973+
4a4d8108
AM
30974+#ifdef CONFIG_AUFS_FS_MODULE
30975+/* this entry violates the "one line per file" policy of sysfs */
30976+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
30977+ char *buf)
30978+{
30979+ ssize_t err;
30980+ static char *conf =
30981+/* this file is generated at compiling */
30982+#include "conf.str"
30983+ ;
30984+
30985+ err = snprintf(buf, PAGE_SIZE, conf);
30986+ if (unlikely(err >= PAGE_SIZE))
30987+ err = -EFBIG;
30988+ return err;
30989+}
30990+
30991+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
30992+#endif
30993+
1facf9fc 30994+static struct attribute *au_attr[] = {
4a4d8108
AM
30995+#ifdef CONFIG_AUFS_FS_MODULE
30996+ &au_config_attr.attr,
30997+#endif
1facf9fc 30998+ NULL, /* need to NULL terminate the list of attributes */
30999+};
31000+
31001+static struct attribute_group sysaufs_attr_group_body = {
31002+ .attrs = au_attr
31003+};
31004+
31005+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31006+
31007+/* ---------------------------------------------------------------------- */
31008+
31009+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31010+{
31011+ int err;
31012+
dece6358
AM
31013+ SiMustAnyLock(sb);
31014+
1facf9fc 31015+ err = 0;
31016+ if (au_opt_test(au_mntflags(sb), XINO)) {
31017+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31018+ seq_putc(seq, '\n');
31019+ }
31020+ return err;
31021+}
31022+
31023+/*
31024+ * the lifetime of branch is independent from the entry under sysfs.
31025+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31026+ * unlinked.
31027+ */
31028+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31029+ aufs_bindex_t bindex, int idx)
1facf9fc 31030+{
1e00d052 31031+ int err;
1facf9fc 31032+ struct path path;
31033+ struct dentry *root;
31034+ struct au_branch *br;
076b876e 31035+ au_br_perm_str_t perm;
1facf9fc 31036+
31037+ AuDbg("b%d\n", bindex);
31038+
1e00d052 31039+ err = 0;
1facf9fc 31040+ root = sb->s_root;
31041+ di_read_lock_parent(root, !AuLock_IR);
31042+ br = au_sbr(sb, bindex);
392086de
AM
31043+
31044+ switch (idx) {
31045+ case AuBrSysfs_BR:
31046+ path.mnt = au_br_mnt(br);
31047+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31048+ err = au_seq_path(seq, &path);
31049+ if (!err) {
31050+ au_optstr_br_perm(&perm, br->br_perm);
31051+ seq_printf(seq, "=%s\n", perm.a);
31052+ }
392086de
AM
31053+ break;
31054+ case AuBrSysfs_BRID:
79b8bda9 31055+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31056+ break;
31057+ }
076b876e 31058+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31059+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31060+ err = -E2BIG;
392086de 31061+
1e00d052 31062+ return err;
1facf9fc 31063+}
31064+
31065+/* ---------------------------------------------------------------------- */
31066+
31067+static struct seq_file *au_seq(char *p, ssize_t len)
31068+{
31069+ struct seq_file *seq;
31070+
31071+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31072+ if (seq) {
31073+ /* mutex_init(&seq.lock); */
31074+ seq->buf = p;
31075+ seq->size = len;
31076+ return seq; /* success */
31077+ }
31078+
31079+ seq = ERR_PTR(-ENOMEM);
31080+ return seq;
31081+}
31082+
392086de
AM
31083+#define SysaufsBr_PREFIX "br"
31084+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31085+
31086+/* todo: file size may exceed PAGE_SIZE */
31087+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31088+ char *buf)
1facf9fc 31089+{
31090+ ssize_t err;
392086de 31091+ int idx;
1facf9fc 31092+ long l;
5afbbe0d 31093+ aufs_bindex_t bbot;
1facf9fc 31094+ struct au_sbinfo *sbinfo;
31095+ struct super_block *sb;
31096+ struct seq_file *seq;
31097+ char *name;
31098+ struct attribute **cattr;
31099+
31100+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31101+ sb = sbinfo->si_sb;
1308ab2a 31102+
31103+ /*
31104+ * prevent a race condition between sysfs and aufs.
31105+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31106+ * prohibits maintaining the sysfs entries.
31107+ * hew we acquire read lock after sysfs_get_active_two().
31108+ * on the other hand, the remount process may maintain the sysfs/aufs
31109+ * entries after acquiring write lock.
31110+ * it can cause a deadlock.
31111+ * simply we gave up processing read here.
31112+ */
31113+ err = -EBUSY;
31114+ if (unlikely(!si_noflush_read_trylock(sb)))
31115+ goto out;
1facf9fc 31116+
31117+ seq = au_seq(buf, PAGE_SIZE);
31118+ err = PTR_ERR(seq);
31119+ if (IS_ERR(seq))
1308ab2a 31120+ goto out_unlock;
1facf9fc 31121+
31122+ name = (void *)attr->name;
31123+ cattr = sysaufs_si_attrs;
31124+ while (*cattr) {
31125+ if (!strcmp(name, (*cattr)->name)) {
31126+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31127+ ->show(seq, sb);
31128+ goto out_seq;
31129+ }
31130+ cattr++;
31131+ }
31132+
392086de
AM
31133+ if (!strncmp(name, SysaufsBrid_PREFIX,
31134+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31135+ idx = AuBrSysfs_BRID;
31136+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31137+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31138+ sizeof(SysaufsBr_PREFIX) - 1)) {
31139+ idx = AuBrSysfs_BR;
1facf9fc 31140+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31141+ } else
31142+ BUG();
31143+
31144+ err = kstrtol(name, 10, &l);
31145+ if (!err) {
5afbbe0d
AM
31146+ bbot = au_sbbot(sb);
31147+ if (l <= bbot)
392086de
AM
31148+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31149+ else
31150+ err = -ENOENT;
1facf9fc 31151+ }
1facf9fc 31152+
4f0767ce 31153+out_seq:
1facf9fc 31154+ if (!err) {
31155+ err = seq->count;
31156+ /* sysfs limit */
31157+ if (unlikely(err == PAGE_SIZE))
31158+ err = -EFBIG;
31159+ }
1c60b727 31160+ kfree(seq);
4f0767ce 31161+out_unlock:
1facf9fc 31162+ si_read_unlock(sb);
4f0767ce 31163+out:
1facf9fc 31164+ return err;
31165+}
31166+
31167+/* ---------------------------------------------------------------------- */
31168+
076b876e
AM
31169+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31170+{
31171+ int err;
31172+ int16_t brid;
5afbbe0d 31173+ aufs_bindex_t bindex, bbot;
076b876e
AM
31174+ size_t sz;
31175+ char *buf;
31176+ struct seq_file *seq;
31177+ struct au_branch *br;
31178+
31179+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31180+ bbot = au_sbbot(sb);
31181+ err = bbot + 1;
076b876e
AM
31182+ if (!arg)
31183+ goto out;
31184+
31185+ err = -ENOMEM;
31186+ buf = (void *)__get_free_page(GFP_NOFS);
31187+ if (unlikely(!buf))
31188+ goto out;
31189+
31190+ seq = au_seq(buf, PAGE_SIZE);
31191+ err = PTR_ERR(seq);
31192+ if (IS_ERR(seq))
31193+ goto out_buf;
31194+
31195+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31196+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
076b876e
AM
31197+ err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
31198+ if (unlikely(err))
31199+ break;
31200+
31201+ br = au_sbr(sb, bindex);
31202+ brid = br->br_id;
31203+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31204+ err = __put_user(brid, &arg->id);
31205+ if (unlikely(err))
31206+ break;
31207+
31208+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31209+ err = __put_user(br->br_perm, &arg->perm);
31210+ if (unlikely(err))
31211+ break;
31212+
79b8bda9
AM
31213+ err = au_seq_path(seq, &br->br_path);
31214+ if (unlikely(err))
31215+ break;
31216+ seq_putc(seq, '\0');
31217+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31218+ err = copy_to_user(arg->path, seq->buf, seq->count);
31219+ seq->count = 0;
31220+ if (unlikely(err))
31221+ break;
31222+ } else {
31223+ err = -E2BIG;
31224+ goto out_seq;
31225+ }
31226+ }
31227+ if (unlikely(err))
31228+ err = -EFAULT;
31229+
31230+out_seq:
1c60b727 31231+ kfree(seq);
076b876e 31232+out_buf:
1c60b727 31233+ free_page((unsigned long)buf);
076b876e
AM
31234+out:
31235+ si_read_unlock(sb);
31236+ return err;
31237+}
31238+
31239+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31240+{
2000de60 31241+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31242+}
31243+
31244+#ifdef CONFIG_COMPAT
31245+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31246+{
2000de60 31247+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31248+}
31249+#endif
31250+
31251+/* ---------------------------------------------------------------------- */
31252+
1facf9fc 31253+void sysaufs_br_init(struct au_branch *br)
31254+{
392086de
AM
31255+ int i;
31256+ struct au_brsysfs *br_sysfs;
31257+ struct attribute *attr;
4a4d8108 31258+
392086de
AM
31259+ br_sysfs = br->br_sysfs;
31260+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31261+ attr = &br_sysfs->attr;
31262+ sysfs_attr_init(attr);
31263+ attr->name = br_sysfs->name;
31264+ attr->mode = S_IRUGO;
31265+ br_sysfs++;
31266+ }
1facf9fc 31267+}
31268+
31269+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31270+{
31271+ struct au_branch *br;
31272+ struct kobject *kobj;
392086de
AM
31273+ struct au_brsysfs *br_sysfs;
31274+ int i;
5afbbe0d 31275+ aufs_bindex_t bbot;
1facf9fc 31276+
31277+ dbgaufs_brs_del(sb, bindex);
31278+
31279+ if (!sysaufs_brs)
31280+ return;
31281+
31282+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31283+ bbot = au_sbbot(sb);
31284+ for (; bindex <= bbot; bindex++) {
1facf9fc 31285+ br = au_sbr(sb, bindex);
392086de
AM
31286+ br_sysfs = br->br_sysfs;
31287+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31288+ sysfs_remove_file(kobj, &br_sysfs->attr);
31289+ br_sysfs++;
31290+ }
1facf9fc 31291+ }
31292+}
31293+
31294+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31295+{
392086de 31296+ int err, i;
5afbbe0d 31297+ aufs_bindex_t bbot;
1facf9fc 31298+ struct kobject *kobj;
31299+ struct au_branch *br;
392086de 31300+ struct au_brsysfs *br_sysfs;
1facf9fc 31301+
31302+ dbgaufs_brs_add(sb, bindex);
31303+
31304+ if (!sysaufs_brs)
31305+ return;
31306+
31307+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31308+ bbot = au_sbbot(sb);
31309+ for (; bindex <= bbot; bindex++) {
1facf9fc 31310+ br = au_sbr(sb, bindex);
392086de
AM
31311+ br_sysfs = br->br_sysfs;
31312+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31313+ SysaufsBr_PREFIX "%d", bindex);
31314+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31315+ SysaufsBrid_PREFIX "%d", bindex);
31316+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31317+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31318+ if (unlikely(err))
31319+ pr_warn("failed %s under sysfs(%d)\n",
31320+ br_sysfs->name, err);
31321+ br_sysfs++;
31322+ }
1facf9fc 31323+ }
31324+}
7f207e10
AM
31325diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31326--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
be118d29 31327+++ linux/fs/aufs/sysrq.c 2018-04-06 07:48:44.207938097 +0200
8b6a4947 31328@@ -0,0 +1,159 @@
1facf9fc 31329+/*
a2654f78 31330+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 31331+ *
31332+ * This program, aufs is free software; you can redistribute it and/or modify
31333+ * it under the terms of the GNU General Public License as published by
31334+ * the Free Software Foundation; either version 2 of the License, or
31335+ * (at your option) any later version.
dece6358
AM
31336+ *
31337+ * This program is distributed in the hope that it will be useful,
31338+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31339+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31340+ * GNU General Public License for more details.
31341+ *
31342+ * You should have received a copy of the GNU General Public License
523b37e3 31343+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31344+ */
31345+
31346+/*
31347+ * magic sysrq hanlder
31348+ */
31349+
1facf9fc 31350+/* #include <linux/sysrq.h> */
027c5e7a 31351+#include <linux/writeback.h>
1facf9fc 31352+#include "aufs.h"
31353+
31354+/* ---------------------------------------------------------------------- */
31355+
31356+static void sysrq_sb(struct super_block *sb)
31357+{
31358+ char *plevel;
31359+ struct au_sbinfo *sbinfo;
31360+ struct file *file;
8b6a4947
AM
31361+ struct hlist_bl_head *files;
31362+ struct hlist_bl_node *pos;
523b37e3 31363+ struct au_finfo *finfo;
1facf9fc 31364+
31365+ plevel = au_plevel;
31366+ au_plevel = KERN_WARNING;
1facf9fc 31367+
4a4d8108 31368+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31369+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31370+
31371+ sbinfo = au_sbi(sb);
4a4d8108 31372+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31373+ pr("superblock\n");
1facf9fc 31374+ au_dpri_sb(sb);
027c5e7a
AM
31375+
31376+#if 0
c06a8ce3 31377+ pr("root dentry\n");
1facf9fc 31378+ au_dpri_dentry(sb->s_root);
c06a8ce3 31379+ pr("root inode\n");
5527c038 31380+ au_dpri_inode(d_inode(sb->s_root));
027c5e7a
AM
31381+#endif
31382+
1facf9fc 31383+#if 0
027c5e7a
AM
31384+ do {
31385+ int err, i, j, ndentry;
31386+ struct au_dcsub_pages dpages;
31387+ struct au_dpage *dpage;
31388+
31389+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31390+ if (unlikely(err))
31391+ break;
31392+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31393+ if (!err)
31394+ for (i = 0; i < dpages.ndpage; i++) {
31395+ dpage = dpages.dpages + i;
31396+ ndentry = dpage->ndentry;
31397+ for (j = 0; j < ndentry; j++)
31398+ au_dpri_dentry(dpage->dentries[j]);
31399+ }
31400+ au_dpages_free(&dpages);
31401+ } while (0);
31402+#endif
31403+
31404+#if 1
31405+ {
31406+ struct inode *i;
076b876e 31407+
c06a8ce3 31408+ pr("isolated inode\n");
79b8bda9 31409+ spin_lock(&sb->s_inode_list_lock);
2cbb1c4b
JR
31410+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31411+ spin_lock(&i->i_lock);
b4510431 31412+ if (1 || hlist_empty(&i->i_dentry))
027c5e7a 31413+ au_dpri_inode(i);
2cbb1c4b
JR
31414+ spin_unlock(&i->i_lock);
31415+ }
79b8bda9 31416+ spin_unlock(&sb->s_inode_list_lock);
027c5e7a 31417+ }
1facf9fc 31418+#endif
c06a8ce3 31419+ pr("files\n");
523b37e3 31420+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31421+ hlist_bl_lock(files);
31422+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31423+ umode_t mode;
076b876e 31424+
523b37e3 31425+ file = finfo->fi_file;
c06a8ce3 31426+ mode = file_inode(file)->i_mode;
38d290e6 31427+ if (!special_file(mode))
1facf9fc 31428+ au_dpri_file(file);
523b37e3 31429+ }
8b6a4947 31430+ hlist_bl_unlock(files);
c06a8ce3 31431+ pr("done\n");
1facf9fc 31432+
c06a8ce3 31433+#undef pr
1facf9fc 31434+ au_plevel = plevel;
1facf9fc 31435+}
31436+
31437+/* ---------------------------------------------------------------------- */
31438+
31439+/* module parameter */
31440+static char *aufs_sysrq_key = "a";
31441+module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
31442+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31443+
0c5527e5 31444+static void au_sysrq(int key __maybe_unused)
1facf9fc 31445+{
1facf9fc 31446+ struct au_sbinfo *sbinfo;
8b6a4947 31447+ struct hlist_bl_node *pos;
1facf9fc 31448+
027c5e7a 31449+ lockdep_off();
53392da6 31450+ au_sbilist_lock();
8b6a4947 31451+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31452+ sysrq_sb(sbinfo->si_sb);
53392da6 31453+ au_sbilist_unlock();
027c5e7a 31454+ lockdep_on();
1facf9fc 31455+}
31456+
31457+static struct sysrq_key_op au_sysrq_op = {
31458+ .handler = au_sysrq,
31459+ .help_msg = "Aufs",
31460+ .action_msg = "Aufs",
31461+ .enable_mask = SYSRQ_ENABLE_DUMP
31462+};
31463+
31464+/* ---------------------------------------------------------------------- */
31465+
31466+int __init au_sysrq_init(void)
31467+{
31468+ int err;
31469+ char key;
31470+
31471+ err = -1;
31472+ key = *aufs_sysrq_key;
31473+ if ('a' <= key && key <= 'z')
31474+ err = register_sysrq_key(key, &au_sysrq_op);
31475+ if (unlikely(err))
4a4d8108 31476+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31477+ return err;
31478+}
31479+
31480+void au_sysrq_fin(void)
31481+{
31482+ int err;
076b876e 31483+
1facf9fc 31484+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31485+ if (unlikely(err))
4a4d8108 31486+ pr_err("err %d (ignored)\n", err);
1facf9fc 31487+}
7f207e10
AM
31488diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31489--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
be118d29 31490+++ linux/fs/aufs/vdir.c 2018-04-06 07:48:44.207938097 +0200
1c60b727 31491@@ -0,0 +1,892 @@
1facf9fc 31492+/*
a2654f78 31493+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 31494+ *
31495+ * This program, aufs is free software; you can redistribute it and/or modify
31496+ * it under the terms of the GNU General Public License as published by
31497+ * the Free Software Foundation; either version 2 of the License, or
31498+ * (at your option) any later version.
dece6358
AM
31499+ *
31500+ * This program is distributed in the hope that it will be useful,
31501+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31502+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31503+ * GNU General Public License for more details.
31504+ *
31505+ * You should have received a copy of the GNU General Public License
523b37e3 31506+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31507+ */
31508+
31509+/*
31510+ * virtual or vertical directory
31511+ */
31512+
31513+#include "aufs.h"
31514+
dece6358 31515+static unsigned int calc_size(int nlen)
1facf9fc 31516+{
dece6358 31517+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31518+}
31519+
31520+static int set_deblk_end(union au_vdir_deblk_p *p,
31521+ union au_vdir_deblk_p *deblk_end)
31522+{
31523+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31524+ p->de->de_str.len = 0;
31525+ /* smp_mb(); */
31526+ return 0;
31527+ }
31528+ return -1; /* error */
31529+}
31530+
31531+/* returns true or false */
31532+static int is_deblk_end(union au_vdir_deblk_p *p,
31533+ union au_vdir_deblk_p *deblk_end)
31534+{
31535+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31536+ return !p->de->de_str.len;
31537+ return 1;
31538+}
31539+
31540+static unsigned char *last_deblk(struct au_vdir *vdir)
31541+{
31542+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31543+}
31544+
31545+/* ---------------------------------------------------------------------- */
31546+
79b8bda9 31547+/* estimate the appropriate size for name hash table */
1308ab2a 31548+unsigned int au_rdhash_est(loff_t sz)
31549+{
31550+ unsigned int n;
31551+
31552+ n = UINT_MAX;
31553+ sz >>= 10;
31554+ if (sz < n)
31555+ n = sz;
31556+ if (sz < AUFS_RDHASH_DEF)
31557+ n = AUFS_RDHASH_DEF;
4a4d8108 31558+ /* pr_info("n %u\n", n); */
1308ab2a 31559+ return n;
31560+}
31561+
1facf9fc 31562+/*
31563+ * the allocated memory has to be freed by
dece6358 31564+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 31565+ */
dece6358 31566+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 31567+{
1facf9fc 31568+ struct hlist_head *head;
dece6358 31569+ unsigned int u;
076b876e 31570+ size_t sz;
1facf9fc 31571+
076b876e
AM
31572+ sz = sizeof(*nhash->nh_head) * num_hash;
31573+ head = kmalloc(sz, gfp);
dece6358
AM
31574+ if (head) {
31575+ nhash->nh_num = num_hash;
31576+ nhash->nh_head = head;
31577+ for (u = 0; u < num_hash; u++)
1facf9fc 31578+ INIT_HLIST_HEAD(head++);
dece6358 31579+ return 0; /* success */
1facf9fc 31580+ }
1facf9fc 31581+
dece6358 31582+ return -ENOMEM;
1facf9fc 31583+}
31584+
dece6358
AM
31585+static void nhash_count(struct hlist_head *head)
31586+{
31587+#if 0
31588+ unsigned long n;
31589+ struct hlist_node *pos;
31590+
31591+ n = 0;
31592+ hlist_for_each(pos, head)
31593+ n++;
4a4d8108 31594+ pr_info("%lu\n", n);
dece6358
AM
31595+#endif
31596+}
31597+
31598+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 31599+{
c06a8ce3
AM
31600+ struct au_vdir_wh *pos;
31601+ struct hlist_node *node;
1facf9fc 31602+
c06a8ce3 31603+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
1c60b727 31604+ kfree(pos);
1facf9fc 31605+}
31606+
dece6358 31607+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 31608+{
c06a8ce3
AM
31609+ struct au_vdir_dehstr *pos;
31610+ struct hlist_node *node;
1facf9fc 31611+
c06a8ce3 31612+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 31613+ au_cache_free_vdir_dehstr(pos);
1facf9fc 31614+}
31615+
dece6358
AM
31616+static void au_nhash_do_free(struct au_nhash *nhash,
31617+ void (*free)(struct hlist_head *head))
1facf9fc 31618+{
1308ab2a 31619+ unsigned int n;
1facf9fc 31620+ struct hlist_head *head;
1facf9fc 31621+
dece6358 31622+ n = nhash->nh_num;
1308ab2a 31623+ if (!n)
31624+ return;
31625+
dece6358 31626+ head = nhash->nh_head;
1308ab2a 31627+ while (n-- > 0) {
dece6358
AM
31628+ nhash_count(head);
31629+ free(head++);
1facf9fc 31630+ }
1c60b727 31631+ kfree(nhash->nh_head);
1facf9fc 31632+}
31633+
dece6358 31634+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 31635+{
dece6358
AM
31636+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
31637+}
1facf9fc 31638+
dece6358
AM
31639+static void au_nhash_de_free(struct au_nhash *delist)
31640+{
31641+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 31642+}
31643+
31644+/* ---------------------------------------------------------------------- */
31645+
31646+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31647+ int limit)
31648+{
31649+ int num;
31650+ unsigned int u, n;
31651+ struct hlist_head *head;
c06a8ce3 31652+ struct au_vdir_wh *pos;
1facf9fc 31653+
31654+ num = 0;
31655+ n = whlist->nh_num;
31656+ head = whlist->nh_head;
1308ab2a 31657+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
31658+ hlist_for_each_entry(pos, head, wh_hash)
31659+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 31660+ return 1;
1facf9fc 31661+ return 0;
31662+}
31663+
31664+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 31665+ unsigned char *name,
1facf9fc 31666+ unsigned int len)
31667+{
dece6358
AM
31668+ unsigned int v;
31669+ /* const unsigned int magic_bit = 12; */
31670+
1308ab2a 31671+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31672+
dece6358 31673+ v = 0;
f0c0a007
AM
31674+ if (len > 8)
31675+ len = 8;
dece6358
AM
31676+ while (len--)
31677+ v += *name++;
31678+ /* v = hash_long(v, magic_bit); */
31679+ v %= nhash->nh_num;
31680+ return nhash->nh_head + v;
31681+}
31682+
31683+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31684+ int nlen)
31685+{
31686+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 31687+}
31688+
31689+/* returns found or not */
dece6358 31690+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 31691+{
31692+ struct hlist_head *head;
c06a8ce3 31693+ struct au_vdir_wh *pos;
1facf9fc 31694+ struct au_vdir_destr *str;
31695+
dece6358 31696+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
31697+ hlist_for_each_entry(pos, head, wh_hash) {
31698+ str = &pos->wh_str;
1facf9fc 31699+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
31700+ if (au_nhash_test_name(str, name, nlen))
31701+ return 1;
31702+ }
31703+ return 0;
31704+}
31705+
31706+/* returns found(true) or not */
31707+static int test_known(struct au_nhash *delist, char *name, int nlen)
31708+{
31709+ struct hlist_head *head;
c06a8ce3 31710+ struct au_vdir_dehstr *pos;
dece6358
AM
31711+ struct au_vdir_destr *str;
31712+
31713+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
31714+ hlist_for_each_entry(pos, head, hash) {
31715+ str = pos->str;
dece6358
AM
31716+ AuDbg("%.*s\n", str->len, str->name);
31717+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 31718+ return 1;
31719+ }
31720+ return 0;
31721+}
31722+
dece6358
AM
31723+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31724+ unsigned char d_type)
31725+{
31726+#ifdef CONFIG_AUFS_SHWH
31727+ wh->wh_ino = ino;
31728+ wh->wh_type = d_type;
31729+#endif
31730+}
31731+
31732+/* ---------------------------------------------------------------------- */
31733+
31734+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31735+ unsigned int d_type, aufs_bindex_t bindex,
31736+ unsigned char shwh)
1facf9fc 31737+{
31738+ int err;
31739+ struct au_vdir_destr *str;
31740+ struct au_vdir_wh *wh;
31741+
dece6358 31742+ AuDbg("%.*s\n", nlen, name);
1308ab2a 31743+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31744+
1facf9fc 31745+ err = -ENOMEM;
dece6358 31746+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 31747+ if (unlikely(!wh))
31748+ goto out;
31749+
31750+ err = 0;
31751+ wh->wh_bindex = bindex;
dece6358
AM
31752+ if (shwh)
31753+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 31754+ str = &wh->wh_str;
dece6358
AM
31755+ str->len = nlen;
31756+ memcpy(str->name, name, nlen);
31757+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 31758+ /* smp_mb(); */
31759+
4f0767ce 31760+out:
1facf9fc 31761+ return err;
31762+}
31763+
1facf9fc 31764+static int append_deblk(struct au_vdir *vdir)
31765+{
31766+ int err;
dece6358 31767+ unsigned long ul;
1facf9fc 31768+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31769+ union au_vdir_deblk_p p, deblk_end;
31770+ unsigned char **o;
31771+
31772+ err = -ENOMEM;
e2f27e51
AM
31773+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
31774+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 31775+ if (unlikely(!o))
31776+ goto out;
31777+
31778+ vdir->vd_deblk = o;
31779+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
31780+ if (p.deblk) {
31781+ ul = vdir->vd_nblk++;
31782+ vdir->vd_deblk[ul] = p.deblk;
31783+ vdir->vd_last.ul = ul;
31784+ vdir->vd_last.p.deblk = p.deblk;
31785+ deblk_end.deblk = p.deblk + deblk_sz;
31786+ err = set_deblk_end(&p, &deblk_end);
31787+ }
31788+
4f0767ce 31789+out:
1facf9fc 31790+ return err;
31791+}
31792+
dece6358
AM
31793+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
31794+ unsigned int d_type, struct au_nhash *delist)
31795+{
31796+ int err;
31797+ unsigned int sz;
31798+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31799+ union au_vdir_deblk_p p, *room, deblk_end;
31800+ struct au_vdir_dehstr *dehstr;
31801+
31802+ p.deblk = last_deblk(vdir);
31803+ deblk_end.deblk = p.deblk + deblk_sz;
31804+ room = &vdir->vd_last.p;
31805+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
31806+ || !is_deblk_end(room, &deblk_end));
31807+
31808+ sz = calc_size(nlen);
31809+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
31810+ err = append_deblk(vdir);
31811+ if (unlikely(err))
31812+ goto out;
31813+
31814+ p.deblk = last_deblk(vdir);
31815+ deblk_end.deblk = p.deblk + deblk_sz;
31816+ /* smp_mb(); */
31817+ AuDebugOn(room->deblk != p.deblk);
31818+ }
31819+
31820+ err = -ENOMEM;
4a4d8108 31821+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
31822+ if (unlikely(!dehstr))
31823+ goto out;
31824+
31825+ dehstr->str = &room->de->de_str;
31826+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
31827+ room->de->de_ino = ino;
31828+ room->de->de_type = d_type;
31829+ room->de->de_str.len = nlen;
31830+ memcpy(room->de->de_str.name, name, nlen);
31831+
31832+ err = 0;
31833+ room->deblk += sz;
31834+ if (unlikely(set_deblk_end(room, &deblk_end)))
31835+ err = append_deblk(vdir);
31836+ /* smp_mb(); */
31837+
4f0767ce 31838+out:
dece6358
AM
31839+ return err;
31840+}
31841+
31842+/* ---------------------------------------------------------------------- */
31843+
1c60b727 31844+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
31845+{
31846+ unsigned char **deblk;
31847+
31848+ deblk = vdir->vd_deblk;
1c60b727
AM
31849+ while (vdir->vd_nblk--)
31850+ kfree(*deblk++);
31851+ kfree(vdir->vd_deblk);
31852+ au_cache_free_vdir(vdir);
dece6358
AM
31853+}
31854+
1308ab2a 31855+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 31856+{
31857+ struct au_vdir *vdir;
1308ab2a 31858+ struct super_block *sb;
1facf9fc 31859+ int err;
31860+
2000de60 31861+ sb = file->f_path.dentry->d_sb;
dece6358
AM
31862+ SiMustAnyLock(sb);
31863+
1facf9fc 31864+ err = -ENOMEM;
31865+ vdir = au_cache_alloc_vdir();
31866+ if (unlikely(!vdir))
31867+ goto out;
31868+
31869+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
31870+ if (unlikely(!vdir->vd_deblk))
31871+ goto out_free;
31872+
31873+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 31874+ if (!vdir->vd_deblk_sz) {
79b8bda9 31875+ /* estimate the appropriate size for deblk */
1308ab2a 31876+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 31877+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 31878+ }
1facf9fc 31879+ vdir->vd_nblk = 0;
31880+ vdir->vd_version = 0;
31881+ vdir->vd_jiffy = 0;
31882+ err = append_deblk(vdir);
31883+ if (!err)
31884+ return vdir; /* success */
31885+
1c60b727 31886+ kfree(vdir->vd_deblk);
1facf9fc 31887+
4f0767ce 31888+out_free:
1c60b727 31889+ au_cache_free_vdir(vdir);
4f0767ce 31890+out:
1facf9fc 31891+ vdir = ERR_PTR(err);
31892+ return vdir;
31893+}
31894+
31895+static int reinit_vdir(struct au_vdir *vdir)
31896+{
31897+ int err;
31898+ union au_vdir_deblk_p p, deblk_end;
31899+
31900+ while (vdir->vd_nblk > 1) {
1c60b727 31901+ kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 31902+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
31903+ vdir->vd_nblk--;
31904+ }
31905+ p.deblk = vdir->vd_deblk[0];
31906+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
31907+ err = set_deblk_end(&p, &deblk_end);
31908+ /* keep vd_dblk_sz */
31909+ vdir->vd_last.ul = 0;
31910+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
31911+ vdir->vd_version = 0;
31912+ vdir->vd_jiffy = 0;
31913+ /* smp_mb(); */
31914+ return err;
31915+}
31916+
31917+/* ---------------------------------------------------------------------- */
31918+
1facf9fc 31919+#define AuFillVdir_CALLED 1
31920+#define AuFillVdir_WHABLE (1 << 1)
dece6358 31921+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 31922+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
31923+#define au_fset_fillvdir(flags, name) \
31924+ do { (flags) |= AuFillVdir_##name; } while (0)
31925+#define au_fclr_fillvdir(flags, name) \
31926+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 31927+
dece6358
AM
31928+#ifndef CONFIG_AUFS_SHWH
31929+#undef AuFillVdir_SHWH
31930+#define AuFillVdir_SHWH 0
31931+#endif
31932+
1facf9fc 31933+struct fillvdir_arg {
392086de 31934+ struct dir_context ctx;
1facf9fc 31935+ struct file *file;
31936+ struct au_vdir *vdir;
dece6358
AM
31937+ struct au_nhash delist;
31938+ struct au_nhash whlist;
1facf9fc 31939+ aufs_bindex_t bindex;
31940+ unsigned int flags;
31941+ int err;
31942+};
31943+
392086de 31944+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 31945+ loff_t offset __maybe_unused, u64 h_ino,
31946+ unsigned int d_type)
31947+{
392086de 31948+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 31949+ char *name = (void *)__name;
31950+ struct super_block *sb;
1facf9fc 31951+ ino_t ino;
dece6358 31952+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 31953+
1facf9fc 31954+ arg->err = 0;
2000de60 31955+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 31956+ au_fset_fillvdir(arg->flags, CALLED);
31957+ /* smp_mb(); */
dece6358 31958+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 31959+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
31960+ if (test_known(&arg->delist, name, nlen)
31961+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
31962+ goto out; /* already exists or whiteouted */
1facf9fc 31963+
dece6358 31964+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
31965+ if (!arg->err) {
31966+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
31967+ d_type = DT_UNKNOWN;
dece6358
AM
31968+ arg->err = append_de(arg->vdir, name, nlen, ino,
31969+ d_type, &arg->delist);
4a4d8108 31970+ }
1facf9fc 31971+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
31972+ name += AUFS_WH_PFX_LEN;
dece6358
AM
31973+ nlen -= AUFS_WH_PFX_LEN;
31974+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
31975+ goto out; /* already whiteouted */
1facf9fc 31976+
dece6358
AM
31977+ if (shwh)
31978+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
31979+ &ino);
4a4d8108
AM
31980+ if (!arg->err) {
31981+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
31982+ d_type = DT_UNKNOWN;
1facf9fc 31983+ arg->err = au_nhash_append_wh
dece6358
AM
31984+ (&arg->whlist, name, nlen, ino, d_type,
31985+ arg->bindex, shwh);
4a4d8108 31986+ }
1facf9fc 31987+ }
31988+
4f0767ce 31989+out:
1facf9fc 31990+ if (!arg->err)
31991+ arg->vdir->vd_jiffy = jiffies;
31992+ /* smp_mb(); */
31993+ AuTraceErr(arg->err);
31994+ return arg->err;
31995+}
31996+
dece6358
AM
31997+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
31998+ struct au_nhash *whlist, struct au_nhash *delist)
31999+{
32000+#ifdef CONFIG_AUFS_SHWH
32001+ int err;
32002+ unsigned int nh, u;
32003+ struct hlist_head *head;
c06a8ce3
AM
32004+ struct au_vdir_wh *pos;
32005+ struct hlist_node *n;
dece6358
AM
32006+ char *p, *o;
32007+ struct au_vdir_destr *destr;
32008+
32009+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32010+
32011+ err = -ENOMEM;
537831f9 32012+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32013+ if (unlikely(!p))
32014+ goto out;
32015+
32016+ err = 0;
32017+ nh = whlist->nh_num;
32018+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32019+ p += AUFS_WH_PFX_LEN;
32020+ for (u = 0; u < nh; u++) {
32021+ head = whlist->nh_head + u;
c06a8ce3
AM
32022+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32023+ destr = &pos->wh_str;
dece6358
AM
32024+ memcpy(p, destr->name, destr->len);
32025+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32026+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32027+ if (unlikely(err))
32028+ break;
32029+ }
32030+ }
32031+
1c60b727 32032+ free_page((unsigned long)o);
dece6358 32033+
4f0767ce 32034+out:
dece6358
AM
32035+ AuTraceErr(err);
32036+ return err;
32037+#else
32038+ return 0;
32039+#endif
32040+}
32041+
1facf9fc 32042+static int au_do_read_vdir(struct fillvdir_arg *arg)
32043+{
32044+ int err;
dece6358 32045+ unsigned int rdhash;
1facf9fc 32046+ loff_t offset;
5afbbe0d 32047+ aufs_bindex_t bbot, bindex, btop;
dece6358 32048+ unsigned char shwh;
1facf9fc 32049+ struct file *hf, *file;
32050+ struct super_block *sb;
32051+
1facf9fc 32052+ file = arg->file;
2000de60 32053+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32054+ SiMustAnyLock(sb);
32055+
32056+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32057+ if (!rdhash)
32058+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32059+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32060+ if (unlikely(err))
1facf9fc 32061+ goto out;
dece6358
AM
32062+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32063+ if (unlikely(err))
1facf9fc 32064+ goto out_delist;
32065+
32066+ err = 0;
32067+ arg->flags = 0;
dece6358
AM
32068+ shwh = 0;
32069+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32070+ shwh = 1;
32071+ au_fset_fillvdir(arg->flags, SHWH);
32072+ }
5afbbe0d
AM
32073+ btop = au_fbtop(file);
32074+ bbot = au_fbbot_dir(file);
32075+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32076+ hf = au_hf_dir(file, bindex);
1facf9fc 32077+ if (!hf)
32078+ continue;
32079+
32080+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32081+ err = offset;
32082+ if (unlikely(offset))
32083+ break;
32084+
32085+ arg->bindex = bindex;
32086+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32087+ if (shwh
5afbbe0d 32088+ || (bindex != bbot
dece6358 32089+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32090+ au_fset_fillvdir(arg->flags, WHABLE);
32091+ do {
32092+ arg->err = 0;
32093+ au_fclr_fillvdir(arg->flags, CALLED);
32094+ /* smp_mb(); */
392086de 32095+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32096+ if (err >= 0)
32097+ err = arg->err;
32098+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32099+
32100+ /*
32101+ * dir_relax() may be good for concurrency, but aufs should not
32102+ * use it since it will cause a lockdep problem.
32103+ */
1facf9fc 32104+ }
dece6358
AM
32105+
32106+ if (!err && shwh)
32107+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32108+
32109+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32110+
4f0767ce 32111+out_delist:
dece6358 32112+ au_nhash_de_free(&arg->delist);
4f0767ce 32113+out:
1facf9fc 32114+ return err;
32115+}
32116+
32117+static int read_vdir(struct file *file, int may_read)
32118+{
32119+ int err;
32120+ unsigned long expire;
32121+ unsigned char do_read;
392086de
AM
32122+ struct fillvdir_arg arg = {
32123+ .ctx = {
2000de60 32124+ .actor = fillvdir
392086de
AM
32125+ }
32126+ };
1facf9fc 32127+ struct inode *inode;
32128+ struct au_vdir *vdir, *allocated;
32129+
32130+ err = 0;
c06a8ce3 32131+ inode = file_inode(file);
1facf9fc 32132+ IMustLock(inode);
5afbbe0d 32133+ IiMustWriteLock(inode);
dece6358
AM
32134+ SiMustAnyLock(inode->i_sb);
32135+
1facf9fc 32136+ allocated = NULL;
32137+ do_read = 0;
32138+ expire = au_sbi(inode->i_sb)->si_rdcache;
32139+ vdir = au_ivdir(inode);
32140+ if (!vdir) {
32141+ do_read = 1;
1308ab2a 32142+ vdir = alloc_vdir(file);
1facf9fc 32143+ err = PTR_ERR(vdir);
32144+ if (IS_ERR(vdir))
32145+ goto out;
32146+ err = 0;
32147+ allocated = vdir;
32148+ } else if (may_read
be118d29 32149+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32150+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32151+ do_read = 1;
32152+ err = reinit_vdir(vdir);
32153+ if (unlikely(err))
32154+ goto out;
32155+ }
32156+
32157+ if (!do_read)
32158+ return 0; /* success */
32159+
32160+ arg.file = file;
32161+ arg.vdir = vdir;
32162+ err = au_do_read_vdir(&arg);
32163+ if (!err) {
392086de 32164+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32165+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32166+ vdir->vd_last.ul = 0;
32167+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32168+ if (allocated)
32169+ au_set_ivdir(inode, allocated);
32170+ } else if (allocated)
1c60b727 32171+ au_vdir_free(allocated);
1facf9fc 32172+
4f0767ce 32173+out:
1facf9fc 32174+ return err;
32175+}
32176+
32177+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32178+{
32179+ int err, rerr;
32180+ unsigned long ul, n;
32181+ const unsigned int deblk_sz = src->vd_deblk_sz;
32182+
32183+ AuDebugOn(tgt->vd_nblk != 1);
32184+
32185+ err = -ENOMEM;
32186+ if (tgt->vd_nblk < src->vd_nblk) {
32187+ unsigned char **p;
32188+
e2f27e51
AM
32189+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32190+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32191+ if (unlikely(!p))
32192+ goto out;
32193+ tgt->vd_deblk = p;
32194+ }
32195+
1308ab2a 32196+ if (tgt->vd_deblk_sz != deblk_sz) {
32197+ unsigned char *p;
32198+
32199+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32200+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32201+ /*may_shrink*/1);
1308ab2a 32202+ if (unlikely(!p))
32203+ goto out;
32204+ tgt->vd_deblk[0] = p;
32205+ }
1facf9fc 32206+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32207+ tgt->vd_version = src->vd_version;
32208+ tgt->vd_jiffy = src->vd_jiffy;
32209+
32210+ n = src->vd_nblk;
32211+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32212+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32213+ GFP_NOFS);
32214+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32215+ goto out;
1308ab2a 32216+ tgt->vd_nblk++;
1facf9fc 32217+ }
1308ab2a 32218+ tgt->vd_nblk = n;
32219+ tgt->vd_last.ul = tgt->vd_last.ul;
32220+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32221+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32222+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32223+ /* smp_mb(); */
32224+ return 0; /* success */
32225+
4f0767ce 32226+out:
1facf9fc 32227+ rerr = reinit_vdir(tgt);
32228+ BUG_ON(rerr);
32229+ return err;
32230+}
32231+
32232+int au_vdir_init(struct file *file)
32233+{
32234+ int err;
32235+ struct inode *inode;
32236+ struct au_vdir *vdir_cache, *allocated;
32237+
392086de 32238+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32239+ err = read_vdir(file, !file->f_pos);
32240+ if (unlikely(err))
32241+ goto out;
32242+
32243+ allocated = NULL;
32244+ vdir_cache = au_fvdir_cache(file);
32245+ if (!vdir_cache) {
1308ab2a 32246+ vdir_cache = alloc_vdir(file);
1facf9fc 32247+ err = PTR_ERR(vdir_cache);
32248+ if (IS_ERR(vdir_cache))
32249+ goto out;
32250+ allocated = vdir_cache;
32251+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32252+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32253+ err = reinit_vdir(vdir_cache);
32254+ if (unlikely(err))
32255+ goto out;
32256+ } else
32257+ return 0; /* success */
32258+
c06a8ce3 32259+ inode = file_inode(file);
1facf9fc 32260+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32261+ if (!err) {
be118d29 32262+ file->f_version = inode_query_iversion(inode);
1facf9fc 32263+ if (allocated)
32264+ au_set_fvdir_cache(file, allocated);
32265+ } else if (allocated)
1c60b727 32266+ au_vdir_free(allocated);
1facf9fc 32267+
4f0767ce 32268+out:
1facf9fc 32269+ return err;
32270+}
32271+
32272+static loff_t calc_offset(struct au_vdir *vdir)
32273+{
32274+ loff_t offset;
32275+ union au_vdir_deblk_p p;
32276+
32277+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32278+ offset = vdir->vd_last.p.deblk - p.deblk;
32279+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32280+ return offset;
32281+}
32282+
32283+/* returns true or false */
392086de 32284+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32285+{
32286+ int valid;
32287+ unsigned int deblk_sz;
32288+ unsigned long ul, n;
32289+ loff_t offset;
32290+ union au_vdir_deblk_p p, deblk_end;
32291+ struct au_vdir *vdir_cache;
32292+
32293+ valid = 1;
32294+ vdir_cache = au_fvdir_cache(file);
32295+ offset = calc_offset(vdir_cache);
32296+ AuDbg("offset %lld\n", offset);
392086de 32297+ if (ctx->pos == offset)
1facf9fc 32298+ goto out;
32299+
32300+ vdir_cache->vd_last.ul = 0;
32301+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32302+ if (!ctx->pos)
1facf9fc 32303+ goto out;
32304+
32305+ valid = 0;
32306+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32307+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32308+ AuDbg("ul %lu\n", ul);
32309+ if (ul >= vdir_cache->vd_nblk)
32310+ goto out;
32311+
32312+ n = vdir_cache->vd_nblk;
32313+ for (; ul < n; ul++) {
32314+ p.deblk = vdir_cache->vd_deblk[ul];
32315+ deblk_end.deblk = p.deblk + deblk_sz;
32316+ offset = ul;
32317+ offset *= deblk_sz;
392086de 32318+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32319+ unsigned int l;
32320+
32321+ l = calc_size(p.de->de_str.len);
32322+ offset += l;
32323+ p.deblk += l;
32324+ }
32325+ if (!is_deblk_end(&p, &deblk_end)) {
32326+ valid = 1;
32327+ vdir_cache->vd_last.ul = ul;
32328+ vdir_cache->vd_last.p = p;
32329+ break;
32330+ }
32331+ }
32332+
4f0767ce 32333+out:
1facf9fc 32334+ /* smp_mb(); */
be118d29 32335+ AuDbg("valid %d\n", !valid);
1facf9fc 32336+ return valid;
32337+}
32338+
392086de 32339+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32340+{
1facf9fc 32341+ unsigned int l, deblk_sz;
32342+ union au_vdir_deblk_p deblk_end;
32343+ struct au_vdir *vdir_cache;
32344+ struct au_vdir_de *de;
32345+
32346+ vdir_cache = au_fvdir_cache(file);
392086de 32347+ if (!seek_vdir(file, ctx))
1facf9fc 32348+ return 0;
32349+
32350+ deblk_sz = vdir_cache->vd_deblk_sz;
32351+ while (1) {
32352+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32353+ deblk_end.deblk += deblk_sz;
32354+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32355+ de = vdir_cache->vd_last.p.de;
32356+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32357+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32358+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32359+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32360+ de->de_str.len, de->de_ino,
32361+ de->de_type))) {
1facf9fc 32362+ /* todo: ignore the error caused by udba? */
32363+ /* return err; */
32364+ return 0;
32365+ }
32366+
32367+ l = calc_size(de->de_str.len);
32368+ vdir_cache->vd_last.p.deblk += l;
392086de 32369+ ctx->pos += l;
1facf9fc 32370+ }
32371+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32372+ vdir_cache->vd_last.ul++;
32373+ vdir_cache->vd_last.p.deblk
32374+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32375+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32376+ continue;
32377+ }
32378+ break;
32379+ }
32380+
32381+ /* smp_mb(); */
32382+ return 0;
32383+}
7f207e10
AM
32384diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32385--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
be118d29 32386+++ linux/fs/aufs/vfsub.c 2018-04-06 07:48:44.207938097 +0200
8b6a4947 32387@@ -0,0 +1,894 @@
1facf9fc 32388+/*
a2654f78 32389+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 32390+ *
32391+ * This program, aufs is free software; you can redistribute it and/or modify
32392+ * it under the terms of the GNU General Public License as published by
32393+ * the Free Software Foundation; either version 2 of the License, or
32394+ * (at your option) any later version.
dece6358
AM
32395+ *
32396+ * This program is distributed in the hope that it will be useful,
32397+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32398+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32399+ * GNU General Public License for more details.
32400+ *
32401+ * You should have received a copy of the GNU General Public License
523b37e3 32402+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32403+ */
32404+
32405+/*
32406+ * sub-routines for VFS
32407+ */
32408+
8b6a4947 32409+#include <linux/mnt_namespace.h>
dece6358 32410+#include <linux/namei.h>
8cdd5066 32411+#include <linux/nsproxy.h>
dece6358
AM
32412+#include <linux/security.h>
32413+#include <linux/splice.h>
1facf9fc 32414+#include "aufs.h"
32415+
8cdd5066
JR
32416+#ifdef CONFIG_AUFS_BR_FUSE
32417+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32418+{
8cdd5066
JR
32419+ if (!au_test_fuse(h_sb) || !au_userns)
32420+ return 0;
32421+
8b6a4947 32422+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32423+}
32424+#endif
32425+
a2654f78
AM
32426+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32427+{
32428+ int err;
32429+
32430+ lockdep_off();
32431+ down_read(&h_sb->s_umount);
32432+ err = __sync_filesystem(h_sb, wait);
32433+ up_read(&h_sb->s_umount);
32434+ lockdep_on();
32435+
32436+ return err;
32437+}
32438+
8cdd5066
JR
32439+/* ---------------------------------------------------------------------- */
32440+
1facf9fc 32441+int vfsub_update_h_iattr(struct path *h_path, int *did)
32442+{
32443+ int err;
32444+ struct kstat st;
32445+ struct super_block *h_sb;
32446+
32447+ /* for remote fs, leave work for its getattr or d_revalidate */
32448+ /* for bad i_attr fs, handle them in aufs_getattr() */
32449+ /* still some fs may acquire i_mutex. we need to skip them */
32450+ err = 0;
32451+ if (!did)
32452+ did = &err;
32453+ h_sb = h_path->dentry->d_sb;
32454+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32455+ if (*did)
521ced18 32456+ err = vfsub_getattr(h_path, &st);
1facf9fc 32457+
32458+ return err;
32459+}
32460+
32461+/* ---------------------------------------------------------------------- */
32462+
4a4d8108 32463+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32464+{
32465+ struct file *file;
32466+
b4510431 32467+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32468+ current_cred());
2cbb1c4b
JR
32469+ if (!IS_ERR_OR_NULL(file)
32470+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 32471+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 32472+
1308ab2a 32473+ return file;
32474+}
32475+
1facf9fc 32476+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32477+{
32478+ struct file *file;
32479+
2cbb1c4b 32480+ lockdep_off();
7f207e10 32481+ file = filp_open(path,
2cbb1c4b 32482+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32483+ mode);
2cbb1c4b 32484+ lockdep_on();
1facf9fc 32485+ if (IS_ERR(file))
32486+ goto out;
32487+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32488+
4f0767ce 32489+out:
1facf9fc 32490+ return file;
32491+}
32492+
b912730e
AM
32493+/*
32494+ * Ideally this function should call VFS:do_last() in order to keep all its
32495+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32496+ * structure such as nameidata. This is a second (or third) best approach.
32497+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32498+ */
32499+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32500+ struct vfsub_aopen_args *args, struct au_branch *br)
32501+{
32502+ int err;
32503+ struct file *file = args->file;
32504+ /* copied from linux/fs/namei.c:atomic_open() */
32505+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32506+
32507+ IMustLock(dir);
32508+ AuDebugOn(!dir->i_op->atomic_open);
32509+
32510+ err = au_br_test_oflag(args->open_flag, br);
32511+ if (unlikely(err))
32512+ goto out;
32513+
32514+ args->file->f_path.dentry = DENTRY_NOT_SET;
32515+ args->file->f_path.mnt = au_br_mnt(br);
32516+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32517+ args->create_mode, args->opened);
32518+ if (err >= 0) {
32519+ /* some filesystems don't set FILE_CREATED while succeeded? */
32520+ if (*args->opened & FILE_CREATED)
32521+ fsnotify_create(dir, dentry);
32522+ } else
32523+ goto out;
32524+
32525+
32526+ if (!err) {
32527+ /* todo: call VFS:may_open() here */
32528+ err = open_check_o_direct(file);
32529+ /* todo: ima_file_check() too? */
32530+ if (!err && (args->open_flag & __FMODE_EXEC))
32531+ err = deny_write_access(file);
32532+ if (unlikely(err))
32533+ /* note that the file is created and still opened */
32534+ goto out;
32535+ }
32536+
5afbbe0d 32537+ au_br_get(br);
b912730e
AM
32538+ fsnotify_open(file);
32539+
32540+out:
32541+ return err;
32542+}
32543+
1facf9fc 32544+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32545+{
32546+ int err;
32547+
1facf9fc 32548+ err = kern_path(name, flags, path);
5527c038 32549+ if (!err && d_is_positive(path->dentry))
1facf9fc 32550+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32551+ return err;
32552+}
32553+
febd17d6
JR
32554+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32555+ struct dentry *parent, int len)
32556+{
32557+ struct path path = {
32558+ .mnt = NULL
32559+ };
32560+
32561+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32562+ if (IS_ERR(path.dentry))
32563+ goto out;
32564+ if (d_is_positive(path.dentry))
32565+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32566+
32567+out:
32568+ AuTraceErrPtr(path.dentry);
32569+ return path.dentry;
32570+}
32571+
1facf9fc 32572+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32573+ int len)
32574+{
32575+ struct path path = {
32576+ .mnt = NULL
32577+ };
32578+
1308ab2a 32579+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 32580+ IMustLock(d_inode(parent));
1facf9fc 32581+
32582+ path.dentry = lookup_one_len(name, parent, len);
32583+ if (IS_ERR(path.dentry))
32584+ goto out;
5527c038 32585+ if (d_is_positive(path.dentry))
1facf9fc 32586+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32587+
4f0767ce 32588+out:
4a4d8108 32589+ AuTraceErrPtr(path.dentry);
1facf9fc 32590+ return path.dentry;
32591+}
32592+
b4510431 32593+void vfsub_call_lkup_one(void *args)
2cbb1c4b 32594+{
b4510431
AM
32595+ struct vfsub_lkup_one_args *a = args;
32596+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
32597+}
32598+
1facf9fc 32599+/* ---------------------------------------------------------------------- */
32600+
32601+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32602+ struct dentry *d2, struct au_hinode *hdir2)
32603+{
32604+ struct dentry *d;
32605+
2cbb1c4b 32606+ lockdep_off();
1facf9fc 32607+ d = lock_rename(d1, d2);
2cbb1c4b 32608+ lockdep_on();
4a4d8108 32609+ au_hn_suspend(hdir1);
1facf9fc 32610+ if (hdir1 != hdir2)
4a4d8108 32611+ au_hn_suspend(hdir2);
1facf9fc 32612+
32613+ return d;
32614+}
32615+
32616+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32617+ struct dentry *d2, struct au_hinode *hdir2)
32618+{
4a4d8108 32619+ au_hn_resume(hdir1);
1facf9fc 32620+ if (hdir1 != hdir2)
4a4d8108 32621+ au_hn_resume(hdir2);
2cbb1c4b 32622+ lockdep_off();
1facf9fc 32623+ unlock_rename(d1, d2);
2cbb1c4b 32624+ lockdep_on();
1facf9fc 32625+}
32626+
32627+/* ---------------------------------------------------------------------- */
32628+
b4510431 32629+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 32630+{
32631+ int err;
32632+ struct dentry *d;
32633+
32634+ IMustLock(dir);
32635+
32636+ d = path->dentry;
32637+ path->dentry = d->d_parent;
b752ccd1 32638+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 32639+ path->dentry = d;
32640+ if (unlikely(err))
32641+ goto out;
32642+
c1595e42 32643+ lockdep_off();
b4510431 32644+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 32645+ lockdep_on();
1facf9fc 32646+ if (!err) {
32647+ struct path tmp = *path;
32648+ int did;
32649+
32650+ vfsub_update_h_iattr(&tmp, &did);
32651+ if (did) {
32652+ tmp.dentry = path->dentry->d_parent;
32653+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32654+ }
32655+ /*ignore*/
32656+ }
32657+
4f0767ce 32658+out:
1facf9fc 32659+ return err;
32660+}
32661+
32662+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32663+{
32664+ int err;
32665+ struct dentry *d;
32666+
32667+ IMustLock(dir);
32668+
32669+ d = path->dentry;
32670+ path->dentry = d->d_parent;
b752ccd1 32671+ err = security_path_symlink(path, d, symname);
1facf9fc 32672+ path->dentry = d;
32673+ if (unlikely(err))
32674+ goto out;
32675+
c1595e42 32676+ lockdep_off();
1facf9fc 32677+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 32678+ lockdep_on();
1facf9fc 32679+ if (!err) {
32680+ struct path tmp = *path;
32681+ int did;
32682+
32683+ vfsub_update_h_iattr(&tmp, &did);
32684+ if (did) {
32685+ tmp.dentry = path->dentry->d_parent;
32686+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32687+ }
32688+ /*ignore*/
32689+ }
32690+
4f0767ce 32691+out:
1facf9fc 32692+ return err;
32693+}
32694+
32695+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32696+{
32697+ int err;
32698+ struct dentry *d;
32699+
32700+ IMustLock(dir);
32701+
32702+ d = path->dentry;
32703+ path->dentry = d->d_parent;
027c5e7a 32704+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 32705+ path->dentry = d;
32706+ if (unlikely(err))
32707+ goto out;
32708+
c1595e42 32709+ lockdep_off();
1facf9fc 32710+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 32711+ lockdep_on();
1facf9fc 32712+ if (!err) {
32713+ struct path tmp = *path;
32714+ int did;
32715+
32716+ vfsub_update_h_iattr(&tmp, &did);
32717+ if (did) {
32718+ tmp.dentry = path->dentry->d_parent;
32719+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32720+ }
32721+ /*ignore*/
32722+ }
32723+
4f0767ce 32724+out:
1facf9fc 32725+ return err;
32726+}
32727+
32728+static int au_test_nlink(struct inode *inode)
32729+{
32730+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32731+
32732+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
32733+ || inode->i_nlink < link_max)
32734+ return 0;
32735+ return -EMLINK;
32736+}
32737+
523b37e3
AM
32738+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32739+ struct inode **delegated_inode)
1facf9fc 32740+{
32741+ int err;
32742+ struct dentry *d;
32743+
32744+ IMustLock(dir);
32745+
5527c038 32746+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 32747+ if (unlikely(err))
32748+ return err;
32749+
b4510431 32750+ /* we don't call may_linkat() */
1facf9fc 32751+ d = path->dentry;
32752+ path->dentry = d->d_parent;
b752ccd1 32753+ err = security_path_link(src_dentry, path, d);
1facf9fc 32754+ path->dentry = d;
32755+ if (unlikely(err))
32756+ goto out;
32757+
2cbb1c4b 32758+ lockdep_off();
523b37e3 32759+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 32760+ lockdep_on();
1facf9fc 32761+ if (!err) {
32762+ struct path tmp = *path;
32763+ int did;
32764+
32765+ /* fuse has different memory inode for the same inumber */
32766+ vfsub_update_h_iattr(&tmp, &did);
32767+ if (did) {
32768+ tmp.dentry = path->dentry->d_parent;
32769+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32770+ tmp.dentry = src_dentry;
32771+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32772+ }
32773+ /*ignore*/
32774+ }
32775+
4f0767ce 32776+out:
1facf9fc 32777+ return err;
32778+}
32779+
32780+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 32781+ struct inode *dir, struct path *path,
f2c43d5f 32782+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 32783+{
32784+ int err;
32785+ struct path tmp = {
32786+ .mnt = path->mnt
32787+ };
32788+ struct dentry *d;
32789+
32790+ IMustLock(dir);
32791+ IMustLock(src_dir);
32792+
32793+ d = path->dentry;
32794+ path->dentry = d->d_parent;
32795+ tmp.dentry = src_dentry->d_parent;
38d290e6 32796+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 32797+ path->dentry = d;
32798+ if (unlikely(err))
32799+ goto out;
32800+
2cbb1c4b 32801+ lockdep_off();
523b37e3 32802+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
f2c43d5f 32803+ delegated_inode, flags);
2cbb1c4b 32804+ lockdep_on();
1facf9fc 32805+ if (!err) {
32806+ int did;
32807+
32808+ tmp.dentry = d->d_parent;
32809+ vfsub_update_h_iattr(&tmp, &did);
32810+ if (did) {
32811+ tmp.dentry = src_dentry;
32812+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32813+ tmp.dentry = src_dentry->d_parent;
32814+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32815+ }
32816+ /*ignore*/
32817+ }
32818+
4f0767ce 32819+out:
1facf9fc 32820+ return err;
32821+}
32822+
32823+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
32824+{
32825+ int err;
32826+ struct dentry *d;
32827+
32828+ IMustLock(dir);
32829+
32830+ d = path->dentry;
32831+ path->dentry = d->d_parent;
b752ccd1 32832+ err = security_path_mkdir(path, d, mode);
1facf9fc 32833+ path->dentry = d;
32834+ if (unlikely(err))
32835+ goto out;
32836+
c1595e42 32837+ lockdep_off();
1facf9fc 32838+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 32839+ lockdep_on();
1facf9fc 32840+ if (!err) {
32841+ struct path tmp = *path;
32842+ int did;
32843+
32844+ vfsub_update_h_iattr(&tmp, &did);
32845+ if (did) {
32846+ tmp.dentry = path->dentry->d_parent;
32847+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32848+ }
32849+ /*ignore*/
32850+ }
32851+
4f0767ce 32852+out:
1facf9fc 32853+ return err;
32854+}
32855+
32856+int vfsub_rmdir(struct inode *dir, struct path *path)
32857+{
32858+ int err;
32859+ struct dentry *d;
32860+
32861+ IMustLock(dir);
32862+
32863+ d = path->dentry;
32864+ path->dentry = d->d_parent;
b752ccd1 32865+ err = security_path_rmdir(path, d);
1facf9fc 32866+ path->dentry = d;
32867+ if (unlikely(err))
32868+ goto out;
32869+
2cbb1c4b 32870+ lockdep_off();
1facf9fc 32871+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 32872+ lockdep_on();
1facf9fc 32873+ if (!err) {
32874+ struct path tmp = {
32875+ .dentry = path->dentry->d_parent,
32876+ .mnt = path->mnt
32877+ };
32878+
32879+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
32880+ }
32881+
4f0767ce 32882+out:
1facf9fc 32883+ return err;
32884+}
32885+
32886+/* ---------------------------------------------------------------------- */
32887+
9dbd164d 32888+/* todo: support mmap_sem? */
1facf9fc 32889+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
32890+ loff_t *ppos)
32891+{
32892+ ssize_t err;
32893+
2cbb1c4b 32894+ lockdep_off();
1facf9fc 32895+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 32896+ lockdep_on();
1facf9fc 32897+ if (err >= 0)
32898+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32899+ return err;
32900+}
32901+
32902+/* todo: kernel_read()? */
32903+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
32904+ loff_t *ppos)
32905+{
32906+ ssize_t err;
32907+ mm_segment_t oldfs;
b752ccd1
AM
32908+ union {
32909+ void *k;
32910+ char __user *u;
32911+ } buf;
1facf9fc 32912+
b752ccd1 32913+ buf.k = kbuf;
1facf9fc 32914+ oldfs = get_fs();
32915+ set_fs(KERNEL_DS);
b752ccd1 32916+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 32917+ set_fs(oldfs);
32918+ return err;
32919+}
32920+
32921+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
32922+ loff_t *ppos)
32923+{
32924+ ssize_t err;
32925+
2cbb1c4b 32926+ lockdep_off();
1facf9fc 32927+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 32928+ lockdep_on();
1facf9fc 32929+ if (err >= 0)
32930+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32931+ return err;
32932+}
32933+
32934+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
32935+{
32936+ ssize_t err;
32937+ mm_segment_t oldfs;
b752ccd1
AM
32938+ union {
32939+ void *k;
32940+ const char __user *u;
32941+ } buf;
1facf9fc 32942+
b752ccd1 32943+ buf.k = kbuf;
1facf9fc 32944+ oldfs = get_fs();
32945+ set_fs(KERNEL_DS);
b752ccd1 32946+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 32947+ set_fs(oldfs);
32948+ return err;
32949+}
32950+
4a4d8108
AM
32951+int vfsub_flush(struct file *file, fl_owner_t id)
32952+{
32953+ int err;
32954+
32955+ err = 0;
523b37e3 32956+ if (file->f_op->flush) {
2000de60 32957+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
32958+ err = file->f_op->flush(file, id);
32959+ else {
32960+ lockdep_off();
32961+ err = file->f_op->flush(file, id);
32962+ lockdep_on();
32963+ }
4a4d8108
AM
32964+ if (!err)
32965+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
32966+ /*ignore*/
32967+ }
32968+ return err;
32969+}
32970+
392086de 32971+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 32972+{
32973+ int err;
32974+
523b37e3 32975+ AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
392086de 32976+
2cbb1c4b 32977+ lockdep_off();
392086de 32978+ err = iterate_dir(file, ctx);
2cbb1c4b 32979+ lockdep_on();
1facf9fc 32980+ if (err >= 0)
32981+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 32982+
1facf9fc 32983+ return err;
32984+}
32985+
32986+long vfsub_splice_to(struct file *in, loff_t *ppos,
32987+ struct pipe_inode_info *pipe, size_t len,
32988+ unsigned int flags)
32989+{
32990+ long err;
32991+
2cbb1c4b 32992+ lockdep_off();
0fc653ad 32993+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 32994+ lockdep_on();
4a4d8108 32995+ file_accessed(in);
1facf9fc 32996+ if (err >= 0)
32997+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
32998+ return err;
32999+}
33000+
33001+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33002+ loff_t *ppos, size_t len, unsigned int flags)
33003+{
33004+ long err;
33005+
2cbb1c4b 33006+ lockdep_off();
0fc653ad 33007+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33008+ lockdep_on();
1facf9fc 33009+ if (err >= 0)
33010+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33011+ return err;
33012+}
33013+
53392da6
AM
33014+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33015+{
33016+ int err;
33017+
33018+ /* file can be NULL */
33019+ lockdep_off();
33020+ err = vfs_fsync(file, datasync);
33021+ lockdep_on();
33022+ if (!err) {
33023+ if (!path) {
33024+ AuDebugOn(!file);
33025+ path = &file->f_path;
33026+ }
33027+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33028+ }
33029+ return err;
33030+}
33031+
1facf9fc 33032+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33033+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33034+ struct file *h_file)
33035+{
33036+ int err;
33037+ struct inode *h_inode;
c06a8ce3 33038+ struct super_block *h_sb;
1facf9fc 33039+
1facf9fc 33040+ if (!h_file) {
c06a8ce3
AM
33041+ err = vfsub_truncate(h_path, length);
33042+ goto out;
1facf9fc 33043+ }
33044+
5527c038 33045+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33046+ h_sb = h_inode->i_sb;
33047+ lockdep_off();
33048+ sb_start_write(h_sb);
33049+ lockdep_on();
1facf9fc 33050+ err = locks_verify_truncate(h_inode, h_file, length);
33051+ if (!err)
953406b4 33052+ err = security_path_truncate(h_path);
2cbb1c4b
JR
33053+ if (!err) {
33054+ lockdep_off();
1facf9fc 33055+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
33056+ lockdep_on();
33057+ }
c06a8ce3
AM
33058+ lockdep_off();
33059+ sb_end_write(h_sb);
33060+ lockdep_on();
1facf9fc 33061+
4f0767ce 33062+out:
1facf9fc 33063+ return err;
33064+}
33065+
33066+/* ---------------------------------------------------------------------- */
33067+
33068+struct au_vfsub_mkdir_args {
33069+ int *errp;
33070+ struct inode *dir;
33071+ struct path *path;
33072+ int mode;
33073+};
33074+
33075+static void au_call_vfsub_mkdir(void *args)
33076+{
33077+ struct au_vfsub_mkdir_args *a = args;
33078+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33079+}
33080+
33081+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33082+{
33083+ int err, do_sio, wkq_err;
33084+
33085+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33086+ if (!do_sio) {
33087+ lockdep_off();
1facf9fc 33088+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33089+ lockdep_on();
33090+ } else {
1facf9fc 33091+ struct au_vfsub_mkdir_args args = {
33092+ .errp = &err,
33093+ .dir = dir,
33094+ .path = path,
33095+ .mode = mode
33096+ };
33097+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33098+ if (unlikely(wkq_err))
33099+ err = wkq_err;
33100+ }
33101+
33102+ return err;
33103+}
33104+
33105+struct au_vfsub_rmdir_args {
33106+ int *errp;
33107+ struct inode *dir;
33108+ struct path *path;
33109+};
33110+
33111+static void au_call_vfsub_rmdir(void *args)
33112+{
33113+ struct au_vfsub_rmdir_args *a = args;
33114+ *a->errp = vfsub_rmdir(a->dir, a->path);
33115+}
33116+
33117+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33118+{
33119+ int err, do_sio, wkq_err;
33120+
33121+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33122+ if (!do_sio) {
33123+ lockdep_off();
1facf9fc 33124+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33125+ lockdep_on();
33126+ } else {
1facf9fc 33127+ struct au_vfsub_rmdir_args args = {
33128+ .errp = &err,
33129+ .dir = dir,
33130+ .path = path
33131+ };
33132+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33133+ if (unlikely(wkq_err))
33134+ err = wkq_err;
33135+ }
33136+
33137+ return err;
33138+}
33139+
33140+/* ---------------------------------------------------------------------- */
33141+
33142+struct notify_change_args {
33143+ int *errp;
33144+ struct path *path;
33145+ struct iattr *ia;
523b37e3 33146+ struct inode **delegated_inode;
1facf9fc 33147+};
33148+
33149+static void call_notify_change(void *args)
33150+{
33151+ struct notify_change_args *a = args;
33152+ struct inode *h_inode;
33153+
5527c038 33154+ h_inode = d_inode(a->path->dentry);
1facf9fc 33155+ IMustLock(h_inode);
33156+
33157+ *a->errp = -EPERM;
33158+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 33159+ lockdep_off();
523b37e3
AM
33160+ *a->errp = notify_change(a->path->dentry, a->ia,
33161+ a->delegated_inode);
c1595e42 33162+ lockdep_on();
1facf9fc 33163+ if (!*a->errp)
33164+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33165+ }
33166+ AuTraceErr(*a->errp);
33167+}
33168+
523b37e3
AM
33169+int vfsub_notify_change(struct path *path, struct iattr *ia,
33170+ struct inode **delegated_inode)
1facf9fc 33171+{
33172+ int err;
33173+ struct notify_change_args args = {
523b37e3
AM
33174+ .errp = &err,
33175+ .path = path,
33176+ .ia = ia,
33177+ .delegated_inode = delegated_inode
1facf9fc 33178+ };
33179+
33180+ call_notify_change(&args);
33181+
33182+ return err;
33183+}
33184+
523b37e3
AM
33185+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33186+ struct inode **delegated_inode)
1facf9fc 33187+{
33188+ int err, wkq_err;
33189+ struct notify_change_args args = {
523b37e3
AM
33190+ .errp = &err,
33191+ .path = path,
33192+ .ia = ia,
33193+ .delegated_inode = delegated_inode
1facf9fc 33194+ };
33195+
33196+ wkq_err = au_wkq_wait(call_notify_change, &args);
33197+ if (unlikely(wkq_err))
33198+ err = wkq_err;
33199+
33200+ return err;
33201+}
33202+
33203+/* ---------------------------------------------------------------------- */
33204+
33205+struct unlink_args {
33206+ int *errp;
33207+ struct inode *dir;
33208+ struct path *path;
523b37e3 33209+ struct inode **delegated_inode;
1facf9fc 33210+};
33211+
33212+static void call_unlink(void *args)
33213+{
33214+ struct unlink_args *a = args;
33215+ struct dentry *d = a->path->dentry;
33216+ struct inode *h_inode;
33217+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33218+ && au_dcount(d) == 1);
1facf9fc 33219+
33220+ IMustLock(a->dir);
33221+
33222+ a->path->dentry = d->d_parent;
33223+ *a->errp = security_path_unlink(a->path, d);
33224+ a->path->dentry = d;
33225+ if (unlikely(*a->errp))
33226+ return;
33227+
33228+ if (!stop_sillyrename)
33229+ dget(d);
5527c038
JR
33230+ h_inode = NULL;
33231+ if (d_is_positive(d)) {
33232+ h_inode = d_inode(d);
027c5e7a 33233+ ihold(h_inode);
5527c038 33234+ }
1facf9fc 33235+
2cbb1c4b 33236+ lockdep_off();
523b37e3 33237+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 33238+ lockdep_on();
1facf9fc 33239+ if (!*a->errp) {
33240+ struct path tmp = {
33241+ .dentry = d->d_parent,
33242+ .mnt = a->path->mnt
33243+ };
33244+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33245+ }
33246+
33247+ if (!stop_sillyrename)
33248+ dput(d);
33249+ if (h_inode)
33250+ iput(h_inode);
33251+
33252+ AuTraceErr(*a->errp);
33253+}
33254+
33255+/*
33256+ * @dir: must be locked.
33257+ * @dentry: target dentry.
33258+ */
523b37e3
AM
33259+int vfsub_unlink(struct inode *dir, struct path *path,
33260+ struct inode **delegated_inode, int force)
1facf9fc 33261+{
33262+ int err;
33263+ struct unlink_args args = {
523b37e3
AM
33264+ .errp = &err,
33265+ .dir = dir,
33266+ .path = path,
33267+ .delegated_inode = delegated_inode
1facf9fc 33268+ };
33269+
33270+ if (!force)
33271+ call_unlink(&args);
33272+ else {
33273+ int wkq_err;
33274+
33275+ wkq_err = au_wkq_wait(call_unlink, &args);
33276+ if (unlikely(wkq_err))
33277+ err = wkq_err;
33278+ }
33279+
33280+ return err;
33281+}
7f207e10
AM
33282diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33283--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
be118d29
JR
33284+++ linux/fs/aufs/vfsub.h 2018-04-06 07:48:44.207938097 +0200
33285@@ -0,0 +1,354 @@
1facf9fc 33286+/*
a2654f78 33287+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 33288+ *
33289+ * This program, aufs is free software; you can redistribute it and/or modify
33290+ * it under the terms of the GNU General Public License as published by
33291+ * the Free Software Foundation; either version 2 of the License, or
33292+ * (at your option) any later version.
dece6358
AM
33293+ *
33294+ * This program is distributed in the hope that it will be useful,
33295+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33296+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33297+ * GNU General Public License for more details.
33298+ *
33299+ * You should have received a copy of the GNU General Public License
523b37e3 33300+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33301+ */
33302+
33303+/*
33304+ * sub-routines for VFS
33305+ */
33306+
33307+#ifndef __AUFS_VFSUB_H__
33308+#define __AUFS_VFSUB_H__
33309+
33310+#ifdef __KERNEL__
33311+
33312+#include <linux/fs.h>
be118d29 33313+#include <linux/iversion.h>
b4510431 33314+#include <linux/mount.h>
8cdd5066 33315+#include <linux/posix_acl.h>
c1595e42 33316+#include <linux/xattr.h>
7f207e10 33317+#include "debug.h"
1facf9fc 33318+
7f207e10 33319+/* copied from linux/fs/internal.h */
2cbb1c4b 33320+/* todo: BAD approach!! */
c06a8ce3 33321+extern void __mnt_drop_write(struct vfsmount *);
b912730e 33322+extern int open_check_o_direct(struct file *f);
7f207e10
AM
33323+
33324+/* ---------------------------------------------------------------------- */
1facf9fc 33325+
33326+/* lock subclass for lower inode */
33327+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33328+/* reduce? gave up. */
33329+enum {
c1595e42 33330+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33331+ AuLsc_I_PARENT, /* lower inode, parent first */
33332+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33333+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33334+ AuLsc_I_CHILD,
33335+ AuLsc_I_CHILD2,
33336+ AuLsc_I_End
33337+};
33338+
33339+/* to debug easier, do not make them inlined functions */
33340+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33341+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33342+
33343+/* ---------------------------------------------------------------------- */
33344+
7f207e10
AM
33345+static inline void vfsub_drop_nlink(struct inode *inode)
33346+{
33347+ AuDebugOn(!inode->i_nlink);
33348+ drop_nlink(inode);
33349+}
33350+
027c5e7a
AM
33351+static inline void vfsub_dead_dir(struct inode *inode)
33352+{
33353+ AuDebugOn(!S_ISDIR(inode->i_mode));
33354+ inode->i_flags |= S_DEAD;
33355+ clear_nlink(inode);
33356+}
33357+
392086de
AM
33358+static inline int vfsub_native_ro(struct inode *inode)
33359+{
8b6a4947 33360+ return sb_rdonly(inode->i_sb)
392086de
AM
33361+ || IS_RDONLY(inode)
33362+ /* || IS_APPEND(inode) */
33363+ || IS_IMMUTABLE(inode);
33364+}
33365+
8cdd5066
JR
33366+#ifdef CONFIG_AUFS_BR_FUSE
33367+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33368+#else
33369+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33370+#endif
33371+
a2654f78
AM
33372+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33373+
7f207e10
AM
33374+/* ---------------------------------------------------------------------- */
33375+
33376+int vfsub_update_h_iattr(struct path *h_path, int *did);
33377+struct file *vfsub_dentry_open(struct path *path, int flags);
33378+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
b912730e
AM
33379+struct vfsub_aopen_args {
33380+ struct file *file;
33381+ unsigned int open_flag;
33382+ umode_t create_mode;
33383+ int *opened;
33384+};
33385+struct au_branch;
33386+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33387+ struct vfsub_aopen_args *args, struct au_branch *br);
1facf9fc 33388+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33389+
febd17d6
JR
33390+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33391+ struct dentry *parent, int len);
1facf9fc 33392+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33393+ int len);
b4510431
AM
33394+
33395+struct vfsub_lkup_one_args {
33396+ struct dentry **errp;
33397+ struct qstr *name;
33398+ struct dentry *parent;
33399+};
33400+
33401+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33402+ struct dentry *parent)
33403+{
33404+ return vfsub_lookup_one_len(name->name, parent, name->len);
33405+}
33406+
33407+void vfsub_call_lkup_one(void *args);
33408+
33409+/* ---------------------------------------------------------------------- */
33410+
33411+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33412+{
33413+ int err;
076b876e 33414+
b4510431
AM
33415+ lockdep_off();
33416+ err = mnt_want_write(mnt);
33417+ lockdep_on();
33418+ return err;
33419+}
33420+
33421+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33422+{
33423+ lockdep_off();
33424+ mnt_drop_write(mnt);
33425+ lockdep_on();
33426+}
1facf9fc 33427+
7e9cd9fe 33428+#if 0 /* reserved */
c06a8ce3
AM
33429+static inline void vfsub_mnt_drop_write_file(struct file *file)
33430+{
33431+ lockdep_off();
33432+ mnt_drop_write_file(file);
33433+ lockdep_on();
33434+}
7e9cd9fe 33435+#endif
c06a8ce3 33436+
1facf9fc 33437+/* ---------------------------------------------------------------------- */
33438+
33439+struct au_hinode;
33440+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33441+ struct dentry *d2, struct au_hinode *hdir2);
33442+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33443+ struct dentry *d2, struct au_hinode *hdir2);
33444+
537831f9
AM
33445+int vfsub_create(struct inode *dir, struct path *path, int mode,
33446+ bool want_excl);
1facf9fc 33447+int vfsub_symlink(struct inode *dir, struct path *path,
33448+ const char *symname);
33449+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33450+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33451+ struct path *path, struct inode **delegated_inode);
1facf9fc 33452+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33453+ struct inode *hdir, struct path *path,
f2c43d5f 33454+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33455+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33456+int vfsub_rmdir(struct inode *dir, struct path *path);
33457+
33458+/* ---------------------------------------------------------------------- */
33459+
33460+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33461+ loff_t *ppos);
33462+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33463+ loff_t *ppos);
33464+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33465+ loff_t *ppos);
33466+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33467+ loff_t *ppos);
4a4d8108 33468+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33469+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33470+
c06a8ce3
AM
33471+static inline loff_t vfsub_f_size_read(struct file *file)
33472+{
33473+ return i_size_read(file_inode(file));
33474+}
33475+
4a4d8108
AM
33476+static inline unsigned int vfsub_file_flags(struct file *file)
33477+{
33478+ unsigned int flags;
33479+
33480+ spin_lock(&file->f_lock);
33481+ flags = file->f_flags;
33482+ spin_unlock(&file->f_lock);
33483+
33484+ return flags;
33485+}
1308ab2a 33486+
f0c0a007
AM
33487+static inline int vfsub_file_execed(struct file *file)
33488+{
33489+ /* todo: direct access f_flags */
33490+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33491+}
33492+
7e9cd9fe 33493+#if 0 /* reserved */
1facf9fc 33494+static inline void vfsub_file_accessed(struct file *h_file)
33495+{
33496+ file_accessed(h_file);
33497+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33498+}
7e9cd9fe 33499+#endif
1facf9fc 33500+
79b8bda9 33501+#if 0 /* reserved */
1facf9fc 33502+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33503+ struct dentry *h_dentry)
33504+{
33505+ struct path h_path = {
33506+ .dentry = h_dentry,
33507+ .mnt = h_mnt
33508+ };
92d182d2 33509+ touch_atime(&h_path);
1facf9fc 33510+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33511+}
79b8bda9 33512+#endif
1facf9fc 33513+
0c3ec466
AM
33514+static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
33515+ int flags)
33516+{
5afbbe0d 33517+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33518+ /* no vfsub_update_h_iattr() since we don't have struct path */
33519+}
33520+
8cdd5066
JR
33521+#ifdef CONFIG_FS_POSIX_ACL
33522+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33523+{
33524+ int err;
33525+
33526+ err = posix_acl_chmod(h_inode, h_mode);
33527+ if (err == -EOPNOTSUPP)
33528+ err = 0;
33529+ return err;
33530+}
33531+#else
33532+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33533+#endif
33534+
4a4d8108
AM
33535+long vfsub_splice_to(struct file *in, loff_t *ppos,
33536+ struct pipe_inode_info *pipe, size_t len,
33537+ unsigned int flags);
33538+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33539+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33540+
33541+static inline long vfsub_truncate(struct path *path, loff_t length)
33542+{
33543+ long err;
076b876e 33544+
c06a8ce3
AM
33545+ lockdep_off();
33546+ err = vfs_truncate(path, length);
33547+ lockdep_on();
33548+ return err;
33549+}
33550+
4a4d8108
AM
33551+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33552+ struct file *h_file);
53392da6 33553+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 33554+
521ced18
JR
33555+/*
33556+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33557+ * ioctl.
33558+ */
33559+static inline int vfsub_clone_file_range(struct file *src, struct file *dst,
33560+ u64 len)
33561+{
33562+ int err;
33563+
33564+ lockdep_off();
33565+ err = vfs_clone_file_range(src, 0, dst, 0, len);
33566+ lockdep_on();
33567+
33568+ return err;
33569+}
33570+
33571+/* copy_file_range(2) is a systemcall */
33572+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33573+ struct file *dst, loff_t dst_pos,
33574+ size_t len, unsigned int flags)
33575+{
33576+ ssize_t ssz;
33577+
33578+ lockdep_off();
33579+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33580+ lockdep_on();
33581+
33582+ return ssz;
33583+}
33584+
1facf9fc 33585+/* ---------------------------------------------------------------------- */
33586+
33587+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33588+{
33589+ loff_t err;
33590+
2cbb1c4b 33591+ lockdep_off();
1facf9fc 33592+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 33593+ lockdep_on();
1facf9fc 33594+ return err;
33595+}
33596+
33597+/* ---------------------------------------------------------------------- */
33598+
4a4d8108
AM
33599+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33600+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
33601+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33602+ struct inode **delegated_inode);
33603+int vfsub_notify_change(struct path *path, struct iattr *ia,
33604+ struct inode **delegated_inode);
33605+int vfsub_unlink(struct inode *dir, struct path *path,
33606+ struct inode **delegated_inode, int force);
4a4d8108 33607+
521ced18
JR
33608+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33609+{
33610+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33611+}
33612+
c1595e42
JR
33613+/* ---------------------------------------------------------------------- */
33614+
33615+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33616+ const void *value, size_t size, int flags)
33617+{
33618+ int err;
33619+
33620+ lockdep_off();
33621+ err = vfs_setxattr(dentry, name, value, size, flags);
33622+ lockdep_on();
33623+
33624+ return err;
33625+}
33626+
33627+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33628+{
33629+ int err;
33630+
33631+ lockdep_off();
33632+ err = vfs_removexattr(dentry, name);
33633+ lockdep_on();
33634+
33635+ return err;
33636+}
33637+
1facf9fc 33638+#endif /* __KERNEL__ */
33639+#endif /* __AUFS_VFSUB_H__ */
7f207e10
AM
33640diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33641--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
be118d29 33642+++ linux/fs/aufs/wbr_policy.c 2018-04-06 07:48:44.207938097 +0200
f2c43d5f 33643@@ -0,0 +1,830 @@
1facf9fc 33644+/*
a2654f78 33645+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 33646+ *
33647+ * This program, aufs is free software; you can redistribute it and/or modify
33648+ * it under the terms of the GNU General Public License as published by
33649+ * the Free Software Foundation; either version 2 of the License, or
33650+ * (at your option) any later version.
dece6358
AM
33651+ *
33652+ * This program is distributed in the hope that it will be useful,
33653+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33654+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33655+ * GNU General Public License for more details.
33656+ *
33657+ * You should have received a copy of the GNU General Public License
523b37e3 33658+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33659+ */
33660+
33661+/*
33662+ * policies for selecting one among multiple writable branches
33663+ */
33664+
33665+#include <linux/statfs.h>
33666+#include "aufs.h"
33667+
33668+/* subset of cpup_attr() */
33669+static noinline_for_stack
33670+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33671+{
33672+ int err, sbits;
33673+ struct iattr ia;
33674+ struct inode *h_isrc;
33675+
5527c038 33676+ h_isrc = d_inode(h_src);
1facf9fc 33677+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33678+ ia.ia_mode = h_isrc->i_mode;
33679+ ia.ia_uid = h_isrc->i_uid;
33680+ ia.ia_gid = h_isrc->i_gid;
33681+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 33682+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
33683+ /* no delegation since it is just created */
33684+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33685+
33686+ /* is this nfs only? */
33687+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33688+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33689+ ia.ia_mode = h_isrc->i_mode;
523b37e3 33690+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33691+ }
33692+
33693+ return err;
33694+}
33695+
33696+#define AuCpdown_PARENT_OPQ 1
33697+#define AuCpdown_WHED (1 << 1)
33698+#define AuCpdown_MADE_DIR (1 << 2)
33699+#define AuCpdown_DIROPQ (1 << 3)
33700+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
33701+#define au_fset_cpdown(flags, name) \
33702+ do { (flags) |= AuCpdown_##name; } while (0)
33703+#define au_fclr_cpdown(flags, name) \
33704+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 33705+
1facf9fc 33706+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 33707+ unsigned int *flags)
1facf9fc 33708+{
33709+ int err;
33710+ struct dentry *opq_dentry;
33711+
33712+ opq_dentry = au_diropq_create(dentry, bdst);
33713+ err = PTR_ERR(opq_dentry);
33714+ if (IS_ERR(opq_dentry))
33715+ goto out;
33716+ dput(opq_dentry);
c2b27bf2 33717+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 33718+
4f0767ce 33719+out:
1facf9fc 33720+ return err;
33721+}
33722+
33723+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33724+ struct inode *dir, aufs_bindex_t bdst)
33725+{
33726+ int err;
33727+ struct path h_path;
33728+ struct au_branch *br;
33729+
33730+ br = au_sbr(dentry->d_sb, bdst);
33731+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
33732+ err = PTR_ERR(h_path.dentry);
33733+ if (IS_ERR(h_path.dentry))
33734+ goto out;
33735+
33736+ err = 0;
5527c038 33737+ if (d_is_positive(h_path.dentry)) {
86dc4139 33738+ h_path.mnt = au_br_mnt(br);
1facf9fc 33739+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
33740+ dentry);
33741+ }
33742+ dput(h_path.dentry);
33743+
4f0767ce 33744+out:
1facf9fc 33745+ return err;
33746+}
33747+
33748+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 33749+ struct au_pin *pin,
1facf9fc 33750+ struct dentry *h_parent, void *arg)
33751+{
33752+ int err, rerr;
5afbbe0d 33753+ aufs_bindex_t bopq, btop;
1facf9fc 33754+ struct path h_path;
33755+ struct dentry *parent;
33756+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 33757+ unsigned int *flags = arg;
1facf9fc 33758+
5afbbe0d 33759+ btop = au_dbtop(dentry);
1facf9fc 33760+ /* dentry is di-locked */
33761+ parent = dget_parent(dentry);
5527c038
JR
33762+ dir = d_inode(parent);
33763+ h_dir = d_inode(h_parent);
1facf9fc 33764+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
33765+ IMustLock(h_dir);
33766+
86dc4139 33767+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 33768+ if (unlikely(err < 0))
33769+ goto out;
33770+ h_path.dentry = au_h_dptr(dentry, bdst);
33771+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
33772+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
33773+ S_IRWXU | S_IRUGO | S_IXUGO);
33774+ if (unlikely(err))
33775+ goto out_put;
c2b27bf2 33776+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 33777+
1facf9fc 33778+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
33779+ au_fclr_cpdown(*flags, WHED);
33780+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 33781+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
33782+ au_fset_cpdown(*flags, WHED);
33783+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
33784+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 33785+ h_inode = d_inode(h_path.dentry);
febd17d6 33786+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
33787+ if (au_ftest_cpdown(*flags, WHED)) {
33788+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 33789+ if (unlikely(err)) {
febd17d6 33790+ inode_unlock(h_inode);
1facf9fc 33791+ goto out_dir;
33792+ }
33793+ }
33794+
5afbbe0d 33795+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 33796+ inode_unlock(h_inode);
1facf9fc 33797+ if (unlikely(err))
33798+ goto out_opq;
33799+
c2b27bf2 33800+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 33801+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
33802+ if (unlikely(err))
33803+ goto out_opq;
33804+ }
33805+
5527c038 33806+ inode = d_inode(dentry);
5afbbe0d
AM
33807+ if (au_ibbot(inode) < bdst)
33808+ au_set_ibbot(inode, bdst);
1facf9fc 33809+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
33810+ au_hi_flags(inode, /*isdir*/1));
076b876e 33811+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 33812+ goto out; /* success */
33813+
33814+ /* revert */
4f0767ce 33815+out_opq:
c2b27bf2 33816+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 33817+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 33818+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 33819+ inode_unlock(h_inode);
1facf9fc 33820+ if (unlikely(rerr)) {
523b37e3
AM
33821+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
33822+ dentry, bdst, rerr);
1facf9fc 33823+ err = -EIO;
33824+ goto out;
33825+ }
33826+ }
4f0767ce 33827+out_dir:
c2b27bf2 33828+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 33829+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
33830+ if (unlikely(rerr)) {
523b37e3
AM
33831+ AuIOErr("failed removing %pd b%d (%d)\n",
33832+ dentry, bdst, rerr);
1facf9fc 33833+ err = -EIO;
33834+ }
33835+ }
4f0767ce 33836+out_put:
1facf9fc 33837+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
33838+ if (au_dbbot(dentry) == bdst)
33839+ au_update_dbbot(dentry);
4f0767ce 33840+out:
1facf9fc 33841+ dput(parent);
33842+ return err;
33843+}
33844+
33845+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
33846+{
33847+ int err;
c2b27bf2 33848+ unsigned int flags;
1facf9fc 33849+
c2b27bf2
AM
33850+ flags = 0;
33851+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 33852+
33853+ return err;
33854+}
33855+
33856+/* ---------------------------------------------------------------------- */
33857+
33858+/* policies for create */
33859+
c2b27bf2 33860+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
33861+{
33862+ int err, i, j, ndentry;
33863+ aufs_bindex_t bopq;
33864+ struct au_dcsub_pages dpages;
33865+ struct au_dpage *dpage;
33866+ struct dentry **dentries, *parent, *d;
33867+
33868+ err = au_dpages_init(&dpages, GFP_NOFS);
33869+ if (unlikely(err))
33870+ goto out;
33871+ parent = dget_parent(dentry);
027c5e7a 33872+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
33873+ if (unlikely(err))
33874+ goto out_free;
33875+
33876+ err = bindex;
33877+ for (i = 0; i < dpages.ndpage; i++) {
33878+ dpage = dpages.dpages + i;
33879+ dentries = dpage->dentries;
33880+ ndentry = dpage->ndentry;
33881+ for (j = 0; j < ndentry; j++) {
33882+ d = dentries[j];
33883+ di_read_lock_parent2(d, !AuLock_IR);
33884+ bopq = au_dbdiropq(d);
33885+ di_read_unlock(d, !AuLock_IR);
33886+ if (bopq >= 0 && bopq < err)
33887+ err = bopq;
33888+ }
33889+ }
33890+
33891+out_free:
33892+ dput(parent);
33893+ au_dpages_free(&dpages);
33894+out:
33895+ return err;
33896+}
33897+
1facf9fc 33898+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
33899+{
33900+ for (; bindex >= 0; bindex--)
33901+ if (!au_br_rdonly(au_sbr(sb, bindex)))
33902+ return bindex;
33903+ return -EROFS;
33904+}
33905+
33906+/* top down parent */
392086de
AM
33907+static int au_wbr_create_tdp(struct dentry *dentry,
33908+ unsigned int flags __maybe_unused)
1facf9fc 33909+{
33910+ int err;
5afbbe0d 33911+ aufs_bindex_t btop, bindex;
1facf9fc 33912+ struct super_block *sb;
33913+ struct dentry *parent, *h_parent;
33914+
33915+ sb = dentry->d_sb;
5afbbe0d
AM
33916+ btop = au_dbtop(dentry);
33917+ err = btop;
33918+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 33919+ goto out;
33920+
33921+ err = -EROFS;
33922+ parent = dget_parent(dentry);
5afbbe0d 33923+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 33924+ h_parent = au_h_dptr(parent, bindex);
5527c038 33925+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 33926+ continue;
33927+
33928+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
33929+ err = bindex;
33930+ break;
33931+ }
33932+ }
33933+ dput(parent);
33934+
33935+ /* bottom up here */
4a4d8108 33936+ if (unlikely(err < 0)) {
5afbbe0d 33937+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
33938+ if (err >= 0)
33939+ err = au_wbr_nonopq(dentry, err);
33940+ }
1facf9fc 33941+
4f0767ce 33942+out:
1facf9fc 33943+ AuDbg("b%d\n", err);
33944+ return err;
33945+}
33946+
33947+/* ---------------------------------------------------------------------- */
33948+
33949+/* an exception for the policy other than tdp */
33950+static int au_wbr_create_exp(struct dentry *dentry)
33951+{
33952+ int err;
33953+ aufs_bindex_t bwh, bdiropq;
33954+ struct dentry *parent;
33955+
33956+ err = -1;
33957+ bwh = au_dbwh(dentry);
33958+ parent = dget_parent(dentry);
33959+ bdiropq = au_dbdiropq(parent);
33960+ if (bwh >= 0) {
33961+ if (bdiropq >= 0)
33962+ err = min(bdiropq, bwh);
33963+ else
33964+ err = bwh;
33965+ AuDbg("%d\n", err);
33966+ } else if (bdiropq >= 0) {
33967+ err = bdiropq;
33968+ AuDbg("%d\n", err);
33969+ }
33970+ dput(parent);
33971+
4a4d8108
AM
33972+ if (err >= 0)
33973+ err = au_wbr_nonopq(dentry, err);
33974+
1facf9fc 33975+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
33976+ err = -1;
33977+
33978+ AuDbg("%d\n", err);
33979+ return err;
33980+}
33981+
33982+/* ---------------------------------------------------------------------- */
33983+
33984+/* round robin */
33985+static int au_wbr_create_init_rr(struct super_block *sb)
33986+{
33987+ int err;
33988+
5afbbe0d 33989+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 33990+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 33991+ /* smp_mb(); */
1facf9fc 33992+
33993+ AuDbg("b%d\n", err);
33994+ return err;
33995+}
33996+
392086de 33997+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 33998+{
33999+ int err, nbr;
34000+ unsigned int u;
5afbbe0d 34001+ aufs_bindex_t bindex, bbot;
1facf9fc 34002+ struct super_block *sb;
34003+ atomic_t *next;
34004+
34005+ err = au_wbr_create_exp(dentry);
34006+ if (err >= 0)
34007+ goto out;
34008+
34009+ sb = dentry->d_sb;
34010+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34011+ bbot = au_sbbot(sb);
34012+ nbr = bbot + 1;
34013+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34014+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34015+ err = atomic_dec_return(next) + 1;
34016+ /* modulo for 0 is meaningless */
34017+ if (unlikely(!err))
34018+ err = atomic_dec_return(next) + 1;
34019+ } else
34020+ err = atomic_read(next);
34021+ AuDbg("%d\n", err);
34022+ u = err;
34023+ err = u % nbr;
34024+ AuDbg("%d\n", err);
34025+ if (!au_br_rdonly(au_sbr(sb, err)))
34026+ break;
34027+ err = -EROFS;
34028+ }
34029+
4a4d8108
AM
34030+ if (err >= 0)
34031+ err = au_wbr_nonopq(dentry, err);
34032+
4f0767ce 34033+out:
1facf9fc 34034+ AuDbg("%d\n", err);
34035+ return err;
34036+}
34037+
34038+/* ---------------------------------------------------------------------- */
34039+
34040+/* most free space */
392086de 34041+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34042+{
34043+ struct super_block *sb;
34044+ struct au_branch *br;
34045+ struct au_wbr_mfs *mfs;
392086de 34046+ struct dentry *h_parent;
5afbbe0d 34047+ aufs_bindex_t bindex, bbot;
1facf9fc 34048+ int err;
34049+ unsigned long long b, bavail;
7f207e10 34050+ struct path h_path;
1facf9fc 34051+ /* reduce the stack usage */
34052+ struct kstatfs *st;
34053+
34054+ st = kmalloc(sizeof(*st), GFP_NOFS);
34055+ if (unlikely(!st)) {
34056+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34057+ return;
34058+ }
34059+
34060+ bavail = 0;
34061+ sb = dentry->d_sb;
34062+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34063+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34064+ mfs->mfs_bindex = -EROFS;
34065+ mfs->mfsrr_bytes = 0;
392086de
AM
34066+ if (!parent) {
34067+ bindex = 0;
5afbbe0d 34068+ bbot = au_sbbot(sb);
392086de 34069+ } else {
5afbbe0d
AM
34070+ bindex = au_dbtop(parent);
34071+ bbot = au_dbtaildir(parent);
392086de
AM
34072+ }
34073+
5afbbe0d 34074+ for (; bindex <= bbot; bindex++) {
392086de
AM
34075+ if (parent) {
34076+ h_parent = au_h_dptr(parent, bindex);
5527c038 34077+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34078+ continue;
34079+ }
1facf9fc 34080+ br = au_sbr(sb, bindex);
34081+ if (au_br_rdonly(br))
34082+ continue;
34083+
34084+ /* sb->s_root for NFS is unreliable */
86dc4139 34085+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34086+ h_path.dentry = h_path.mnt->mnt_root;
34087+ err = vfs_statfs(&h_path, st);
1facf9fc 34088+ if (unlikely(err)) {
34089+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34090+ continue;
34091+ }
34092+
34093+ /* when the available size is equal, select the lower one */
34094+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34095+ || sizeof(b) < sizeof(st->f_bsize));
34096+ b = st->f_bavail * st->f_bsize;
34097+ br->br_wbr->wbr_bytes = b;
34098+ if (b >= bavail) {
34099+ bavail = b;
34100+ mfs->mfs_bindex = bindex;
34101+ mfs->mfs_jiffy = jiffies;
34102+ }
34103+ }
34104+
34105+ mfs->mfsrr_bytes = bavail;
34106+ AuDbg("b%d\n", mfs->mfs_bindex);
1c60b727 34107+ kfree(st);
1facf9fc 34108+}
34109+
392086de 34110+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34111+{
34112+ int err;
392086de 34113+ struct dentry *parent;
1facf9fc 34114+ struct super_block *sb;
34115+ struct au_wbr_mfs *mfs;
34116+
34117+ err = au_wbr_create_exp(dentry);
34118+ if (err >= 0)
34119+ goto out;
34120+
34121+ sb = dentry->d_sb;
392086de
AM
34122+ parent = NULL;
34123+ if (au_ftest_wbr(flags, PARENT))
34124+ parent = dget_parent(dentry);
1facf9fc 34125+ mfs = &au_sbi(sb)->si_wbr_mfs;
34126+ mutex_lock(&mfs->mfs_lock);
34127+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34128+ || mfs->mfs_bindex < 0
34129+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34130+ au_mfs(dentry, parent);
1facf9fc 34131+ mutex_unlock(&mfs->mfs_lock);
34132+ err = mfs->mfs_bindex;
392086de 34133+ dput(parent);
1facf9fc 34134+
4a4d8108
AM
34135+ if (err >= 0)
34136+ err = au_wbr_nonopq(dentry, err);
34137+
4f0767ce 34138+out:
1facf9fc 34139+ AuDbg("b%d\n", err);
34140+ return err;
34141+}
34142+
34143+static int au_wbr_create_init_mfs(struct super_block *sb)
34144+{
34145+ struct au_wbr_mfs *mfs;
34146+
34147+ mfs = &au_sbi(sb)->si_wbr_mfs;
34148+ mutex_init(&mfs->mfs_lock);
34149+ mfs->mfs_jiffy = 0;
34150+ mfs->mfs_bindex = -EROFS;
34151+
34152+ return 0;
34153+}
34154+
34155+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34156+{
34157+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34158+ return 0;
34159+}
34160+
34161+/* ---------------------------------------------------------------------- */
34162+
f2c43d5f
AM
34163+/* top down regardless parent, and then mfs */
34164+static int au_wbr_create_tdmfs(struct dentry *dentry,
34165+ unsigned int flags __maybe_unused)
34166+{
34167+ int err;
34168+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34169+ unsigned long long watermark;
34170+ struct super_block *sb;
34171+ struct au_wbr_mfs *mfs;
34172+ struct au_branch *br;
34173+ struct dentry *parent;
34174+
34175+ sb = dentry->d_sb;
34176+ mfs = &au_sbi(sb)->si_wbr_mfs;
34177+ mutex_lock(&mfs->mfs_lock);
34178+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34179+ || mfs->mfs_bindex < 0)
34180+ au_mfs(dentry, /*parent*/NULL);
34181+ watermark = mfs->mfsrr_watermark;
34182+ bmfs = mfs->mfs_bindex;
34183+ mutex_unlock(&mfs->mfs_lock);
34184+
34185+ /* another style of au_wbr_create_exp() */
34186+ bwh = au_dbwh(dentry);
34187+ parent = dget_parent(dentry);
34188+ btail = au_dbtaildir(parent);
34189+ if (bwh >= 0 && bwh < btail)
34190+ btail = bwh;
34191+
34192+ err = au_wbr_nonopq(dentry, btail);
34193+ if (unlikely(err < 0))
34194+ goto out;
34195+ btail = err;
34196+ bfound = -1;
34197+ for (bindex = 0; bindex <= btail; bindex++) {
34198+ br = au_sbr(sb, bindex);
34199+ if (au_br_rdonly(br))
34200+ continue;
34201+ if (br->br_wbr->wbr_bytes > watermark) {
34202+ bfound = bindex;
34203+ break;
34204+ }
34205+ }
34206+ err = bfound;
34207+ if (err < 0)
34208+ err = bmfs;
34209+
34210+out:
34211+ dput(parent);
34212+ AuDbg("b%d\n", err);
34213+ return err;
34214+}
34215+
34216+/* ---------------------------------------------------------------------- */
34217+
1facf9fc 34218+/* most free space and then round robin */
392086de 34219+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34220+{
34221+ int err;
34222+ struct au_wbr_mfs *mfs;
34223+
392086de 34224+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34225+ if (err >= 0) {
34226+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34227+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34228+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34229+ err = au_wbr_create_rr(dentry, flags);
dece6358 34230+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34231+ }
34232+
34233+ AuDbg("b%d\n", err);
34234+ return err;
34235+}
34236+
34237+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34238+{
34239+ int err;
34240+
34241+ au_wbr_create_init_mfs(sb); /* ignore */
34242+ err = au_wbr_create_init_rr(sb);
34243+
34244+ return err;
34245+}
34246+
34247+/* ---------------------------------------------------------------------- */
34248+
34249+/* top down parent and most free space */
392086de 34250+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34251+{
34252+ int err, e2;
34253+ unsigned long long b;
5afbbe0d 34254+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34255+ struct super_block *sb;
34256+ struct dentry *parent, *h_parent;
34257+ struct au_branch *br;
34258+
392086de 34259+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34260+ if (unlikely(err < 0))
34261+ goto out;
34262+ parent = dget_parent(dentry);
5afbbe0d
AM
34263+ btop = au_dbtop(parent);
34264+ bbot = au_dbtaildir(parent);
34265+ if (btop == bbot)
1facf9fc 34266+ goto out_parent; /* success */
34267+
392086de 34268+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34269+ if (e2 < 0)
34270+ goto out_parent; /* success */
34271+
34272+ /* when the available size is equal, select upper one */
34273+ sb = dentry->d_sb;
34274+ br = au_sbr(sb, err);
34275+ b = br->br_wbr->wbr_bytes;
34276+ AuDbg("b%d, %llu\n", err, b);
34277+
5afbbe0d 34278+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34279+ h_parent = au_h_dptr(parent, bindex);
5527c038 34280+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34281+ continue;
34282+
34283+ br = au_sbr(sb, bindex);
34284+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34285+ b = br->br_wbr->wbr_bytes;
34286+ err = bindex;
34287+ AuDbg("b%d, %llu\n", err, b);
34288+ }
34289+ }
34290+
4a4d8108
AM
34291+ if (err >= 0)
34292+ err = au_wbr_nonopq(dentry, err);
34293+
4f0767ce 34294+out_parent:
1facf9fc 34295+ dput(parent);
4f0767ce 34296+out:
1facf9fc 34297+ AuDbg("b%d\n", err);
34298+ return err;
34299+}
34300+
34301+/* ---------------------------------------------------------------------- */
34302+
392086de
AM
34303+/*
34304+ * - top down parent
34305+ * - most free space with parent
34306+ * - most free space round-robin regardless parent
34307+ */
34308+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34309+{
34310+ int err;
34311+ unsigned long long watermark;
34312+ struct super_block *sb;
34313+ struct au_branch *br;
34314+ struct au_wbr_mfs *mfs;
34315+
34316+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34317+ if (unlikely(err < 0))
34318+ goto out;
34319+
34320+ sb = dentry->d_sb;
34321+ br = au_sbr(sb, err);
34322+ mfs = &au_sbi(sb)->si_wbr_mfs;
34323+ mutex_lock(&mfs->mfs_lock);
34324+ watermark = mfs->mfsrr_watermark;
34325+ mutex_unlock(&mfs->mfs_lock);
34326+ if (br->br_wbr->wbr_bytes < watermark)
34327+ /* regardless the parent dir */
34328+ err = au_wbr_create_mfsrr(dentry, flags);
34329+
34330+out:
34331+ AuDbg("b%d\n", err);
34332+ return err;
34333+}
34334+
34335+/* ---------------------------------------------------------------------- */
34336+
1facf9fc 34337+/* policies for copyup */
34338+
34339+/* top down parent */
34340+static int au_wbr_copyup_tdp(struct dentry *dentry)
34341+{
392086de 34342+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34343+}
34344+
34345+/* bottom up parent */
34346+static int au_wbr_copyup_bup(struct dentry *dentry)
34347+{
34348+ int err;
5afbbe0d 34349+ aufs_bindex_t bindex, btop;
1facf9fc 34350+ struct dentry *parent, *h_parent;
34351+ struct super_block *sb;
34352+
34353+ err = -EROFS;
34354+ sb = dentry->d_sb;
34355+ parent = dget_parent(dentry);
5afbbe0d
AM
34356+ btop = au_dbtop(parent);
34357+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34358+ h_parent = au_h_dptr(parent, bindex);
5527c038 34359+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34360+ continue;
34361+
34362+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34363+ err = bindex;
34364+ break;
34365+ }
34366+ }
34367+ dput(parent);
34368+
34369+ /* bottom up here */
34370+ if (unlikely(err < 0))
5afbbe0d 34371+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34372+
34373+ AuDbg("b%d\n", err);
34374+ return err;
34375+}
34376+
34377+/* bottom up */
5afbbe0d 34378+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34379+{
34380+ int err;
34381+
5afbbe0d 34382+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34383+ AuDbg("b%d\n", err);
5afbbe0d 34384+ if (err > btop)
4a4d8108 34385+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34386+
34387+ AuDbg("b%d\n", err);
34388+ return err;
34389+}
34390+
076b876e
AM
34391+static int au_wbr_copyup_bu(struct dentry *dentry)
34392+{
34393+ int err;
5afbbe0d 34394+ aufs_bindex_t btop;
076b876e 34395+
5afbbe0d
AM
34396+ btop = au_dbtop(dentry);
34397+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34398+ return err;
34399+}
34400+
1facf9fc 34401+/* ---------------------------------------------------------------------- */
34402+
34403+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34404+ [AuWbrCopyup_TDP] = {
34405+ .copyup = au_wbr_copyup_tdp
34406+ },
34407+ [AuWbrCopyup_BUP] = {
34408+ .copyup = au_wbr_copyup_bup
34409+ },
34410+ [AuWbrCopyup_BU] = {
34411+ .copyup = au_wbr_copyup_bu
34412+ }
34413+};
34414+
34415+struct au_wbr_create_operations au_wbr_create_ops[] = {
34416+ [AuWbrCreate_TDP] = {
34417+ .create = au_wbr_create_tdp
34418+ },
34419+ [AuWbrCreate_RR] = {
34420+ .create = au_wbr_create_rr,
34421+ .init = au_wbr_create_init_rr
34422+ },
34423+ [AuWbrCreate_MFS] = {
34424+ .create = au_wbr_create_mfs,
34425+ .init = au_wbr_create_init_mfs,
34426+ .fin = au_wbr_create_fin_mfs
34427+ },
34428+ [AuWbrCreate_MFSV] = {
34429+ .create = au_wbr_create_mfs,
34430+ .init = au_wbr_create_init_mfs,
34431+ .fin = au_wbr_create_fin_mfs
34432+ },
34433+ [AuWbrCreate_MFSRR] = {
34434+ .create = au_wbr_create_mfsrr,
34435+ .init = au_wbr_create_init_mfsrr,
34436+ .fin = au_wbr_create_fin_mfs
34437+ },
34438+ [AuWbrCreate_MFSRRV] = {
34439+ .create = au_wbr_create_mfsrr,
34440+ .init = au_wbr_create_init_mfsrr,
34441+ .fin = au_wbr_create_fin_mfs
34442+ },
f2c43d5f
AM
34443+ [AuWbrCreate_TDMFS] = {
34444+ .create = au_wbr_create_tdmfs,
34445+ .init = au_wbr_create_init_mfs,
34446+ .fin = au_wbr_create_fin_mfs
34447+ },
34448+ [AuWbrCreate_TDMFSV] = {
34449+ .create = au_wbr_create_tdmfs,
34450+ .init = au_wbr_create_init_mfs,
34451+ .fin = au_wbr_create_fin_mfs
34452+ },
1facf9fc 34453+ [AuWbrCreate_PMFS] = {
34454+ .create = au_wbr_create_pmfs,
34455+ .init = au_wbr_create_init_mfs,
34456+ .fin = au_wbr_create_fin_mfs
34457+ },
34458+ [AuWbrCreate_PMFSV] = {
34459+ .create = au_wbr_create_pmfs,
34460+ .init = au_wbr_create_init_mfs,
34461+ .fin = au_wbr_create_fin_mfs
392086de
AM
34462+ },
34463+ [AuWbrCreate_PMFSRR] = {
34464+ .create = au_wbr_create_pmfsrr,
34465+ .init = au_wbr_create_init_mfsrr,
34466+ .fin = au_wbr_create_fin_mfs
34467+ },
34468+ [AuWbrCreate_PMFSRRV] = {
34469+ .create = au_wbr_create_pmfsrr,
34470+ .init = au_wbr_create_init_mfsrr,
34471+ .fin = au_wbr_create_fin_mfs
1facf9fc 34472+ }
34473+};
7f207e10
AM
34474diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34475--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
be118d29 34476+++ linux/fs/aufs/whout.c 2018-04-06 07:48:44.207938097 +0200
f2c43d5f 34477@@ -0,0 +1,1061 @@
1facf9fc 34478+/*
a2654f78 34479+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 34480+ *
34481+ * This program, aufs is free software; you can redistribute it and/or modify
34482+ * it under the terms of the GNU General Public License as published by
34483+ * the Free Software Foundation; either version 2 of the License, or
34484+ * (at your option) any later version.
dece6358
AM
34485+ *
34486+ * This program is distributed in the hope that it will be useful,
34487+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34488+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34489+ * GNU General Public License for more details.
34490+ *
34491+ * You should have received a copy of the GNU General Public License
523b37e3 34492+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34493+ */
34494+
34495+/*
34496+ * whiteout for logical deletion and opaque directory
34497+ */
34498+
1facf9fc 34499+#include "aufs.h"
34500+
34501+#define WH_MASK S_IRUGO
34502+
34503+/*
34504+ * If a directory contains this file, then it is opaque. We start with the
34505+ * .wh. flag so that it is blocked by lookup.
34506+ */
0c3ec466
AM
34507+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34508+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34509+
34510+/*
34511+ * generate whiteout name, which is NOT terminated by NULL.
34512+ * @name: original d_name.name
34513+ * @len: original d_name.len
34514+ * @wh: whiteout qstr
34515+ * returns zero when succeeds, otherwise error.
34516+ * succeeded value as wh->name should be freed by kfree().
34517+ */
34518+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34519+{
34520+ char *p;
34521+
34522+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34523+ return -ENAMETOOLONG;
34524+
34525+ wh->len = name->len + AUFS_WH_PFX_LEN;
34526+ p = kmalloc(wh->len, GFP_NOFS);
34527+ wh->name = p;
34528+ if (p) {
34529+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34530+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34531+ /* smp_mb(); */
34532+ return 0;
34533+ }
34534+ return -ENOMEM;
34535+}
34536+
34537+/* ---------------------------------------------------------------------- */
34538+
34539+/*
34540+ * test if the @wh_name exists under @h_parent.
34541+ * @try_sio specifies the necessary of super-io.
34542+ */
076b876e 34543+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 34544+{
34545+ int err;
34546+ struct dentry *wh_dentry;
1facf9fc 34547+
1facf9fc 34548+ if (!try_sio)
b4510431 34549+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 34550+ else
076b876e 34551+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 34552+ err = PTR_ERR(wh_dentry);
2000de60
JR
34553+ if (IS_ERR(wh_dentry)) {
34554+ if (err == -ENAMETOOLONG)
34555+ err = 0;
1facf9fc 34556+ goto out;
2000de60 34557+ }
1facf9fc 34558+
34559+ err = 0;
5527c038 34560+ if (d_is_negative(wh_dentry))
1facf9fc 34561+ goto out_wh; /* success */
34562+
34563+ err = 1;
7e9cd9fe 34564+ if (d_is_reg(wh_dentry))
1facf9fc 34565+ goto out_wh; /* success */
34566+
34567+ err = -EIO;
523b37e3 34568+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 34569+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 34570+
4f0767ce 34571+out_wh:
1facf9fc 34572+ dput(wh_dentry);
4f0767ce 34573+out:
1facf9fc 34574+ return err;
34575+}
34576+
34577+/*
34578+ * test if the @h_dentry sets opaque or not.
34579+ */
076b876e 34580+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 34581+{
34582+ int err;
34583+ struct inode *h_dir;
34584+
5527c038 34585+ h_dir = d_inode(h_dentry);
076b876e 34586+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 34587+ au_test_h_perm_sio(h_dir, MAY_EXEC));
34588+ return err;
34589+}
34590+
34591+/*
34592+ * returns a negative dentry whose name is unique and temporary.
34593+ */
34594+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34595+ struct qstr *prefix)
34596+{
1facf9fc 34597+ struct dentry *dentry;
34598+ int i;
027c5e7a 34599+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 34600+ *name, *p;
027c5e7a 34601+ /* strict atomic_t is unnecessary here */
1facf9fc 34602+ static unsigned short cnt;
34603+ struct qstr qs;
34604+
4a4d8108
AM
34605+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34606+
1facf9fc 34607+ name = defname;
027c5e7a
AM
34608+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34609+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 34610+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 34611+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 34612+ goto out;
34613+ dentry = ERR_PTR(-ENOMEM);
34614+ name = kmalloc(qs.len + 1, GFP_NOFS);
34615+ if (unlikely(!name))
34616+ goto out;
34617+ }
34618+
34619+ /* doubly whiteout-ed */
34620+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34621+ p = name + AUFS_WH_PFX_LEN * 2;
34622+ memcpy(p, prefix->name, prefix->len);
34623+ p += prefix->len;
34624+ *p++ = '.';
4a4d8108 34625+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 34626+
34627+ qs.name = name;
34628+ for (i = 0; i < 3; i++) {
b752ccd1 34629+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 34630+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 34631+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 34632+ goto out_name;
34633+ dput(dentry);
34634+ }
0c3ec466 34635+ /* pr_warn("could not get random name\n"); */
1facf9fc 34636+ dentry = ERR_PTR(-EEXIST);
34637+ AuDbg("%.*s\n", AuLNPair(&qs));
34638+ BUG();
34639+
4f0767ce 34640+out_name:
1facf9fc 34641+ if (name != defname)
1c60b727 34642+ kfree(name);
4f0767ce 34643+out:
4a4d8108 34644+ AuTraceErrPtr(dentry);
1facf9fc 34645+ return dentry;
1facf9fc 34646+}
34647+
34648+/*
34649+ * rename the @h_dentry on @br to the whiteouted temporary name.
34650+ */
34651+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34652+{
34653+ int err;
34654+ struct path h_path = {
86dc4139 34655+ .mnt = au_br_mnt(br)
1facf9fc 34656+ };
523b37e3 34657+ struct inode *h_dir, *delegated;
1facf9fc 34658+ struct dentry *h_parent;
34659+
34660+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 34661+ h_dir = d_inode(h_parent);
1facf9fc 34662+ IMustLock(h_dir);
34663+
34664+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34665+ err = PTR_ERR(h_path.dentry);
34666+ if (IS_ERR(h_path.dentry))
34667+ goto out;
34668+
34669+ /* under the same dir, no need to lock_rename() */
523b37e3 34670+ delegated = NULL;
f2c43d5f
AM
34671+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34672+ /*flags*/0);
1facf9fc 34673+ AuTraceErr(err);
523b37e3
AM
34674+ if (unlikely(err == -EWOULDBLOCK)) {
34675+ pr_warn("cannot retry for NFSv4 delegation"
34676+ " for an internal rename\n");
34677+ iput(delegated);
34678+ }
1facf9fc 34679+ dput(h_path.dentry);
34680+
4f0767ce 34681+out:
4a4d8108 34682+ AuTraceErr(err);
1facf9fc 34683+ return err;
34684+}
34685+
34686+/* ---------------------------------------------------------------------- */
34687+/*
34688+ * functions for removing a whiteout
34689+ */
34690+
34691+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34692+{
523b37e3
AM
34693+ int err, force;
34694+ struct inode *delegated;
1facf9fc 34695+
34696+ /*
34697+ * forces superio when the dir has a sticky bit.
34698+ * this may be a violation of unix fs semantics.
34699+ */
34700+ force = (h_dir->i_mode & S_ISVTX)
5527c038 34701+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
34702+ delegated = NULL;
34703+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
34704+ if (unlikely(err == -EWOULDBLOCK)) {
34705+ pr_warn("cannot retry for NFSv4 delegation"
34706+ " for an internal unlink\n");
34707+ iput(delegated);
34708+ }
34709+ return err;
1facf9fc 34710+}
34711+
34712+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34713+ struct dentry *dentry)
34714+{
34715+ int err;
34716+
34717+ err = do_unlink_wh(h_dir, h_path);
34718+ if (!err && dentry)
34719+ au_set_dbwh(dentry, -1);
34720+
34721+ return err;
34722+}
34723+
34724+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
34725+ struct au_branch *br)
34726+{
34727+ int err;
34728+ struct path h_path = {
86dc4139 34729+ .mnt = au_br_mnt(br)
1facf9fc 34730+ };
34731+
34732+ err = 0;
b4510431 34733+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 34734+ if (IS_ERR(h_path.dentry))
34735+ err = PTR_ERR(h_path.dentry);
34736+ else {
5527c038
JR
34737+ if (d_is_reg(h_path.dentry))
34738+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 34739+ dput(h_path.dentry);
34740+ }
34741+
34742+ return err;
34743+}
34744+
34745+/* ---------------------------------------------------------------------- */
34746+/*
34747+ * initialize/clean whiteout for a branch
34748+ */
34749+
34750+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
34751+ const int isdir)
34752+{
34753+ int err;
523b37e3 34754+ struct inode *delegated;
1facf9fc 34755+
5527c038 34756+ if (d_is_negative(whpath->dentry))
1facf9fc 34757+ return;
34758+
86dc4139
AM
34759+ if (isdir)
34760+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
34761+ else {
34762+ delegated = NULL;
34763+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
34764+ if (unlikely(err == -EWOULDBLOCK)) {
34765+ pr_warn("cannot retry for NFSv4 delegation"
34766+ " for an internal unlink\n");
34767+ iput(delegated);
34768+ }
34769+ }
1facf9fc 34770+ if (unlikely(err))
523b37e3
AM
34771+ pr_warn("failed removing %pd (%d), ignored.\n",
34772+ whpath->dentry, err);
1facf9fc 34773+}
34774+
34775+static int test_linkable(struct dentry *h_root)
34776+{
5527c038 34777+ struct inode *h_dir = d_inode(h_root);
1facf9fc 34778+
34779+ if (h_dir->i_op->link)
34780+ return 0;
34781+
523b37e3
AM
34782+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
34783+ h_root, au_sbtype(h_root->d_sb));
1facf9fc 34784+ return -ENOSYS;
34785+}
34786+
34787+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
34788+static int au_whdir(struct inode *h_dir, struct path *path)
34789+{
34790+ int err;
34791+
34792+ err = -EEXIST;
5527c038 34793+ if (d_is_negative(path->dentry)) {
1facf9fc 34794+ int mode = S_IRWXU;
34795+
34796+ if (au_test_nfs(path->dentry->d_sb))
34797+ mode |= S_IXUGO;
86dc4139 34798+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 34799+ } else if (d_is_dir(path->dentry))
1facf9fc 34800+ err = 0;
34801+ else
523b37e3 34802+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 34803+
34804+ return err;
34805+}
34806+
34807+struct au_wh_base {
34808+ const struct qstr *name;
34809+ struct dentry *dentry;
34810+};
34811+
34812+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
34813+ struct path *h_path)
34814+{
34815+ h_path->dentry = base[AuBrWh_BASE].dentry;
34816+ au_wh_clean(h_dir, h_path, /*isdir*/0);
34817+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34818+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34819+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34820+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34821+}
34822+
34823+/*
34824+ * returns tri-state,
c1595e42 34825+ * minus: error, caller should print the message
1facf9fc 34826+ * zero: succuess
c1595e42 34827+ * plus: error, caller should NOT print the message
1facf9fc 34828+ */
34829+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
34830+ int do_plink, struct au_wh_base base[],
34831+ struct path *h_path)
34832+{
34833+ int err;
34834+ struct inode *h_dir;
34835+
5527c038 34836+ h_dir = d_inode(h_root);
1facf9fc 34837+ h_path->dentry = base[AuBrWh_BASE].dentry;
34838+ au_wh_clean(h_dir, h_path, /*isdir*/0);
34839+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34840+ if (do_plink) {
34841+ err = test_linkable(h_root);
34842+ if (unlikely(err)) {
34843+ err = 1;
34844+ goto out;
34845+ }
34846+
34847+ err = au_whdir(h_dir, h_path);
34848+ if (unlikely(err))
34849+ goto out;
34850+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
34851+ } else
34852+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34853+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34854+ err = au_whdir(h_dir, h_path);
34855+ if (unlikely(err))
34856+ goto out;
34857+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
34858+
4f0767ce 34859+out:
1facf9fc 34860+ return err;
34861+}
34862+
34863+/*
34864+ * for the moment, aufs supports the branch filesystem which does not support
34865+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
34866+ * copyup failed. finally, such filesystem will not be used as the writable
34867+ * branch.
34868+ *
34869+ * returns tri-state, see above.
34870+ */
34871+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
34872+ int do_plink, struct au_wh_base base[],
34873+ struct path *h_path)
34874+{
34875+ int err;
34876+ struct inode *h_dir;
34877+
1308ab2a 34878+ WbrWhMustWriteLock(wbr);
34879+
1facf9fc 34880+ err = test_linkable(h_root);
34881+ if (unlikely(err)) {
34882+ err = 1;
34883+ goto out;
34884+ }
34885+
34886+ /*
34887+ * todo: should this create be done in /sbin/mount.aufs helper?
34888+ */
34889+ err = -EEXIST;
5527c038
JR
34890+ h_dir = d_inode(h_root);
34891+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
34892+ h_path->dentry = base[AuBrWh_BASE].dentry;
34893+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 34894+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 34895+ err = 0;
34896+ else
523b37e3 34897+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 34898+ if (unlikely(err))
34899+ goto out;
34900+
34901+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34902+ if (do_plink) {
34903+ err = au_whdir(h_dir, h_path);
34904+ if (unlikely(err))
34905+ goto out;
34906+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
34907+ } else
34908+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34909+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
34910+
34911+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34912+ err = au_whdir(h_dir, h_path);
34913+ if (unlikely(err))
34914+ goto out;
34915+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
34916+
4f0767ce 34917+out:
1facf9fc 34918+ return err;
34919+}
34920+
34921+/*
34922+ * initialize the whiteout base file/dir for @br.
34923+ */
86dc4139 34924+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 34925+{
34926+ int err, i;
34927+ const unsigned char do_plink
34928+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 34929+ struct inode *h_dir;
86dc4139
AM
34930+ struct path path = br->br_path;
34931+ struct dentry *h_root = path.dentry;
1facf9fc 34932+ struct au_wbr *wbr = br->br_wbr;
34933+ static const struct qstr base_name[] = {
0c3ec466
AM
34934+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
34935+ sizeof(AUFS_BASE_NAME) - 1),
34936+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
34937+ sizeof(AUFS_PLINKDIR_NAME) - 1),
34938+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
34939+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 34940+ };
34941+ struct au_wh_base base[] = {
34942+ [AuBrWh_BASE] = {
34943+ .name = base_name + AuBrWh_BASE,
34944+ .dentry = NULL
34945+ },
34946+ [AuBrWh_PLINK] = {
34947+ .name = base_name + AuBrWh_PLINK,
34948+ .dentry = NULL
34949+ },
34950+ [AuBrWh_ORPH] = {
34951+ .name = base_name + AuBrWh_ORPH,
34952+ .dentry = NULL
34953+ }
34954+ };
34955+
1308ab2a 34956+ if (wbr)
34957+ WbrWhMustWriteLock(wbr);
1facf9fc 34958+
1facf9fc 34959+ for (i = 0; i < AuBrWh_Last; i++) {
34960+ /* doubly whiteouted */
34961+ struct dentry *d;
34962+
34963+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
34964+ err = PTR_ERR(d);
34965+ if (IS_ERR(d))
34966+ goto out;
34967+
34968+ base[i].dentry = d;
34969+ AuDebugOn(wbr
34970+ && wbr->wbr_wh[i]
34971+ && wbr->wbr_wh[i] != base[i].dentry);
34972+ }
34973+
34974+ if (wbr)
34975+ for (i = 0; i < AuBrWh_Last; i++) {
34976+ dput(wbr->wbr_wh[i]);
34977+ wbr->wbr_wh[i] = NULL;
34978+ }
34979+
34980+ err = 0;
1e00d052 34981+ if (!au_br_writable(br->br_perm)) {
5527c038 34982+ h_dir = d_inode(h_root);
1facf9fc 34983+ au_wh_init_ro(h_dir, base, &path);
1e00d052 34984+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 34985+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
34986+ if (err > 0)
34987+ goto out;
34988+ else if (err)
34989+ goto out_err;
1e00d052 34990+ } else {
1facf9fc 34991+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
34992+ if (err > 0)
34993+ goto out;
34994+ else if (err)
34995+ goto out_err;
1facf9fc 34996+ }
34997+ goto out; /* success */
34998+
4f0767ce 34999+out_err:
523b37e3
AM
35000+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35001+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35002+out:
1facf9fc 35003+ for (i = 0; i < AuBrWh_Last; i++)
35004+ dput(base[i].dentry);
35005+ return err;
35006+}
35007+
35008+/* ---------------------------------------------------------------------- */
35009+/*
35010+ * whiteouts are all hard-linked usually.
35011+ * when its link count reaches a ceiling, we create a new whiteout base
35012+ * asynchronously.
35013+ */
35014+
35015+struct reinit_br_wh {
35016+ struct super_block *sb;
35017+ struct au_branch *br;
35018+};
35019+
35020+static void reinit_br_wh(void *arg)
35021+{
35022+ int err;
35023+ aufs_bindex_t bindex;
35024+ struct path h_path;
35025+ struct reinit_br_wh *a = arg;
35026+ struct au_wbr *wbr;
523b37e3 35027+ struct inode *dir, *delegated;
1facf9fc 35028+ struct dentry *h_root;
35029+ struct au_hinode *hdir;
35030+
35031+ err = 0;
35032+ wbr = a->br->br_wbr;
35033+ /* big aufs lock */
35034+ si_noflush_write_lock(a->sb);
35035+ if (!au_br_writable(a->br->br_perm))
35036+ goto out;
35037+ bindex = au_br_index(a->sb, a->br->br_id);
35038+ if (unlikely(bindex < 0))
35039+ goto out;
35040+
1308ab2a 35041+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35042+ dir = d_inode(a->sb->s_root);
1facf9fc 35043+ hdir = au_hi(dir, bindex);
35044+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35045+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35046+
5afbbe0d 35047+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35048+ wbr_wh_write_lock(wbr);
35049+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35050+ h_root, a->br);
35051+ if (!err) {
86dc4139
AM
35052+ h_path.dentry = wbr->wbr_whbase;
35053+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35054+ delegated = NULL;
35055+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35056+ /*force*/0);
35057+ if (unlikely(err == -EWOULDBLOCK)) {
35058+ pr_warn("cannot retry for NFSv4 delegation"
35059+ " for an internal unlink\n");
35060+ iput(delegated);
35061+ }
1facf9fc 35062+ } else {
523b37e3 35063+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35064+ err = 0;
35065+ }
35066+ dput(wbr->wbr_whbase);
35067+ wbr->wbr_whbase = NULL;
35068+ if (!err)
86dc4139 35069+ err = au_wh_init(a->br, a->sb);
1facf9fc 35070+ wbr_wh_write_unlock(wbr);
5afbbe0d 35071+ au_hn_inode_unlock(hdir);
1308ab2a 35072+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35073+ if (!err)
35074+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35075+
4f0767ce 35076+out:
1facf9fc 35077+ if (wbr)
35078+ atomic_dec(&wbr->wbr_wh_running);
5afbbe0d 35079+ au_br_put(a->br);
1facf9fc 35080+ si_write_unlock(a->sb);
027c5e7a 35081+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
1c60b727 35082+ kfree(arg);
1facf9fc 35083+ if (unlikely(err))
35084+ AuIOErr("err %d\n", err);
35085+}
35086+
35087+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35088+{
35089+ int do_dec, wkq_err;
35090+ struct reinit_br_wh *arg;
35091+
35092+ do_dec = 1;
35093+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35094+ goto out;
35095+
35096+ /* ignore ENOMEM */
35097+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35098+ if (arg) {
35099+ /*
35100+ * dec(wh_running), kfree(arg) and dec(br_count)
35101+ * in reinit function
35102+ */
35103+ arg->sb = sb;
35104+ arg->br = br;
5afbbe0d 35105+ au_br_get(br);
53392da6 35106+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35107+ if (unlikely(wkq_err)) {
35108+ atomic_dec(&br->br_wbr->wbr_wh_running);
5afbbe0d 35109+ au_br_put(br);
1c60b727 35110+ kfree(arg);
1facf9fc 35111+ }
35112+ do_dec = 0;
35113+ }
35114+
4f0767ce 35115+out:
1facf9fc 35116+ if (do_dec)
35117+ atomic_dec(&br->br_wbr->wbr_wh_running);
35118+}
35119+
35120+/* ---------------------------------------------------------------------- */
35121+
35122+/*
35123+ * create the whiteout @wh.
35124+ */
35125+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35126+ struct dentry *wh)
35127+{
35128+ int err;
35129+ struct path h_path = {
35130+ .dentry = wh
35131+ };
35132+ struct au_branch *br;
35133+ struct au_wbr *wbr;
35134+ struct dentry *h_parent;
523b37e3 35135+ struct inode *h_dir, *delegated;
1facf9fc 35136+
35137+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35138+ h_dir = d_inode(h_parent);
1facf9fc 35139+ IMustLock(h_dir);
35140+
35141+ br = au_sbr(sb, bindex);
86dc4139 35142+ h_path.mnt = au_br_mnt(br);
1facf9fc 35143+ wbr = br->br_wbr;
35144+ wbr_wh_read_lock(wbr);
35145+ if (wbr->wbr_whbase) {
523b37e3
AM
35146+ delegated = NULL;
35147+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35148+ if (unlikely(err == -EWOULDBLOCK)) {
35149+ pr_warn("cannot retry for NFSv4 delegation"
35150+ " for an internal link\n");
35151+ iput(delegated);
35152+ }
1facf9fc 35153+ if (!err || err != -EMLINK)
35154+ goto out;
35155+
35156+ /* link count full. re-initialize br_whbase. */
35157+ kick_reinit_br_wh(sb, br);
35158+ }
35159+
35160+ /* return this error in this context */
b4510431 35161+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35162+ if (!err)
35163+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35164+
4f0767ce 35165+out:
1facf9fc 35166+ wbr_wh_read_unlock(wbr);
35167+ return err;
35168+}
35169+
35170+/* ---------------------------------------------------------------------- */
35171+
35172+/*
35173+ * create or remove the diropq.
35174+ */
35175+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35176+ unsigned int flags)
35177+{
35178+ struct dentry *opq_dentry, *h_dentry;
35179+ struct super_block *sb;
35180+ struct au_branch *br;
35181+ int err;
35182+
35183+ sb = dentry->d_sb;
35184+ br = au_sbr(sb, bindex);
35185+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35186+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35187+ if (IS_ERR(opq_dentry))
35188+ goto out;
35189+
35190+ if (au_ftest_diropq(flags, CREATE)) {
35191+ err = link_or_create_wh(sb, bindex, opq_dentry);
35192+ if (!err) {
35193+ au_set_dbdiropq(dentry, bindex);
35194+ goto out; /* success */
35195+ }
35196+ } else {
35197+ struct path tmp = {
35198+ .dentry = opq_dentry,
86dc4139 35199+ .mnt = au_br_mnt(br)
1facf9fc 35200+ };
5527c038 35201+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35202+ if (!err)
35203+ au_set_dbdiropq(dentry, -1);
35204+ }
35205+ dput(opq_dentry);
35206+ opq_dentry = ERR_PTR(err);
35207+
4f0767ce 35208+out:
1facf9fc 35209+ return opq_dentry;
35210+}
35211+
35212+struct do_diropq_args {
35213+ struct dentry **errp;
35214+ struct dentry *dentry;
35215+ aufs_bindex_t bindex;
35216+ unsigned int flags;
35217+};
35218+
35219+static void call_do_diropq(void *args)
35220+{
35221+ struct do_diropq_args *a = args;
35222+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35223+}
35224+
35225+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35226+ unsigned int flags)
35227+{
35228+ struct dentry *diropq, *h_dentry;
35229+
35230+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 35231+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 35232+ diropq = do_diropq(dentry, bindex, flags);
35233+ else {
35234+ int wkq_err;
35235+ struct do_diropq_args args = {
35236+ .errp = &diropq,
35237+ .dentry = dentry,
35238+ .bindex = bindex,
35239+ .flags = flags
35240+ };
35241+
35242+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35243+ if (unlikely(wkq_err))
35244+ diropq = ERR_PTR(wkq_err);
35245+ }
35246+
35247+ return diropq;
35248+}
35249+
35250+/* ---------------------------------------------------------------------- */
35251+
35252+/*
35253+ * lookup whiteout dentry.
35254+ * @h_parent: lower parent dentry which must exist and be locked
35255+ * @base_name: name of dentry which will be whiteouted
35256+ * returns dentry for whiteout.
35257+ */
35258+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35259+ struct au_branch *br)
35260+{
35261+ int err;
35262+ struct qstr wh_name;
35263+ struct dentry *wh_dentry;
35264+
35265+ err = au_wh_name_alloc(&wh_name, base_name);
35266+ wh_dentry = ERR_PTR(err);
35267+ if (!err) {
b4510431 35268+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
1c60b727 35269+ kfree(wh_name.name);
1facf9fc 35270+ }
35271+ return wh_dentry;
35272+}
35273+
35274+/*
35275+ * link/create a whiteout for @dentry on @bindex.
35276+ */
35277+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35278+ struct dentry *h_parent)
35279+{
35280+ struct dentry *wh_dentry;
35281+ struct super_block *sb;
35282+ int err;
35283+
35284+ sb = dentry->d_sb;
35285+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35286+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35287+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35288+ if (!err) {
1facf9fc 35289+ au_set_dbwh(dentry, bindex);
076b876e
AM
35290+ au_fhsm_wrote(sb, bindex, /*force*/0);
35291+ } else {
1facf9fc 35292+ dput(wh_dentry);
35293+ wh_dentry = ERR_PTR(err);
35294+ }
35295+ }
35296+
35297+ return wh_dentry;
35298+}
35299+
35300+/* ---------------------------------------------------------------------- */
35301+
35302+/* Delete all whiteouts in this directory on branch bindex. */
35303+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35304+ aufs_bindex_t bindex, struct au_branch *br)
35305+{
35306+ int err;
35307+ unsigned long ul, n;
35308+ struct qstr wh_name;
35309+ char *p;
35310+ struct hlist_head *head;
c06a8ce3 35311+ struct au_vdir_wh *pos;
1facf9fc 35312+ struct au_vdir_destr *str;
35313+
35314+ err = -ENOMEM;
537831f9 35315+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35316+ wh_name.name = p;
35317+ if (unlikely(!wh_name.name))
35318+ goto out;
35319+
35320+ err = 0;
35321+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35322+ p += AUFS_WH_PFX_LEN;
35323+ n = whlist->nh_num;
35324+ head = whlist->nh_head;
35325+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35326+ hlist_for_each_entry(pos, head, wh_hash) {
35327+ if (pos->wh_bindex != bindex)
1facf9fc 35328+ continue;
35329+
c06a8ce3 35330+ str = &pos->wh_str;
1facf9fc 35331+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35332+ memcpy(p, str->name, str->len);
35333+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35334+ err = unlink_wh_name(h_dentry, &wh_name, br);
35335+ if (!err)
35336+ continue;
35337+ break;
35338+ }
35339+ AuIOErr("whiteout name too long %.*s\n",
35340+ str->len, str->name);
35341+ err = -EIO;
35342+ break;
35343+ }
35344+ }
1c60b727 35345+ free_page((unsigned long)wh_name.name);
1facf9fc 35346+
4f0767ce 35347+out:
1facf9fc 35348+ return err;
35349+}
35350+
35351+struct del_wh_children_args {
35352+ int *errp;
35353+ struct dentry *h_dentry;
1308ab2a 35354+ struct au_nhash *whlist;
1facf9fc 35355+ aufs_bindex_t bindex;
35356+ struct au_branch *br;
35357+};
35358+
35359+static void call_del_wh_children(void *args)
35360+{
35361+ struct del_wh_children_args *a = args;
1308ab2a 35362+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35363+}
35364+
35365+/* ---------------------------------------------------------------------- */
35366+
35367+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35368+{
35369+ struct au_whtmp_rmdir *whtmp;
dece6358 35370+ int err;
1308ab2a 35371+ unsigned int rdhash;
dece6358
AM
35372+
35373+ SiMustAnyLock(sb);
1facf9fc 35374+
be52b249 35375+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35376+ if (unlikely(!whtmp)) {
35377+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35378+ goto out;
dece6358 35379+ }
1facf9fc 35380+
1308ab2a 35381+ /* no estimation for dir size */
35382+ rdhash = au_sbi(sb)->si_rdhash;
35383+ if (!rdhash)
35384+ rdhash = AUFS_RDHASH_DEF;
35385+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35386+ if (unlikely(err)) {
1c60b727 35387+ kfree(whtmp);
1308ab2a 35388+ whtmp = ERR_PTR(err);
35389+ }
dece6358 35390+
4f0767ce 35391+out:
dece6358 35392+ return whtmp;
1facf9fc 35393+}
35394+
35395+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35396+{
027c5e7a 35397+ if (whtmp->br)
5afbbe0d 35398+ au_br_put(whtmp->br);
1facf9fc 35399+ dput(whtmp->wh_dentry);
35400+ iput(whtmp->dir);
dece6358 35401+ au_nhash_wh_free(&whtmp->whlist);
1c60b727 35402+ kfree(whtmp);
1facf9fc 35403+}
35404+
35405+/*
35406+ * rmdir the whiteouted temporary named dir @h_dentry.
35407+ * @whlist: whiteouted children.
35408+ */
35409+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35410+ struct dentry *wh_dentry, struct au_nhash *whlist)
35411+{
35412+ int err;
2000de60 35413+ unsigned int h_nlink;
1facf9fc 35414+ struct path h_tmp;
35415+ struct inode *wh_inode, *h_dir;
35416+ struct au_branch *br;
35417+
5527c038 35418+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35419+ IMustLock(h_dir);
35420+
35421+ br = au_sbr(dir->i_sb, bindex);
5527c038 35422+ wh_inode = d_inode(wh_dentry);
febd17d6 35423+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35424+
35425+ /*
35426+ * someone else might change some whiteouts while we were sleeping.
35427+ * it means this whlist may have an obsoleted entry.
35428+ */
35429+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35430+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35431+ else {
35432+ int wkq_err;
35433+ struct del_wh_children_args args = {
35434+ .errp = &err,
35435+ .h_dentry = wh_dentry,
1308ab2a 35436+ .whlist = whlist,
1facf9fc 35437+ .bindex = bindex,
35438+ .br = br
35439+ };
35440+
35441+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35442+ if (unlikely(wkq_err))
35443+ err = wkq_err;
35444+ }
febd17d6 35445+ inode_unlock(wh_inode);
1facf9fc 35446+
35447+ if (!err) {
35448+ h_tmp.dentry = wh_dentry;
86dc4139 35449+ h_tmp.mnt = au_br_mnt(br);
2000de60 35450+ h_nlink = h_dir->i_nlink;
1facf9fc 35451+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35452+ /* some fs doesn't change the parent nlink in some cases */
35453+ h_nlink -= h_dir->i_nlink;
1facf9fc 35454+ }
35455+
35456+ if (!err) {
5afbbe0d 35457+ if (au_ibtop(dir) == bindex) {
7f207e10 35458+ /* todo: dir->i_mutex is necessary */
1facf9fc 35459+ au_cpup_attr_timesizes(dir);
2000de60
JR
35460+ if (h_nlink)
35461+ vfsub_drop_nlink(dir);
1facf9fc 35462+ }
35463+ return 0; /* success */
35464+ }
35465+
523b37e3 35466+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35467+ return err;
35468+}
35469+
35470+static void call_rmdir_whtmp(void *args)
35471+{
35472+ int err;
e49829fe 35473+ aufs_bindex_t bindex;
1facf9fc 35474+ struct au_whtmp_rmdir *a = args;
35475+ struct super_block *sb;
35476+ struct dentry *h_parent;
35477+ struct inode *h_dir;
1facf9fc 35478+ struct au_hinode *hdir;
35479+
35480+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35481+ /* inode_lock(a->dir); */
e49829fe 35482+ err = -EROFS;
1facf9fc 35483+ sb = a->dir->i_sb;
e49829fe
JR
35484+ si_read_lock(sb, !AuLock_FLUSH);
35485+ if (!au_br_writable(a->br->br_perm))
35486+ goto out;
35487+ bindex = au_br_index(sb, a->br->br_id);
35488+ if (unlikely(bindex < 0))
1facf9fc 35489+ goto out;
35490+
35491+ err = -EIO;
1facf9fc 35492+ ii_write_lock_parent(a->dir);
35493+ h_parent = dget_parent(a->wh_dentry);
5527c038 35494+ h_dir = d_inode(h_parent);
e49829fe 35495+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35496+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35497+ if (unlikely(err))
35498+ goto out_mnt;
5afbbe0d 35499+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35500+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35501+ a->br);
86dc4139
AM
35502+ if (!err)
35503+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35504+ au_hn_inode_unlock(hdir);
86dc4139
AM
35505+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35506+
35507+out_mnt:
1facf9fc 35508+ dput(h_parent);
35509+ ii_write_unlock(a->dir);
4f0767ce 35510+out:
febd17d6 35511+ /* inode_unlock(a->dir); */
1facf9fc 35512+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35513+ si_read_unlock(sb);
35514+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35515+ if (unlikely(err))
35516+ AuIOErr("err %d\n", err);
35517+}
35518+
35519+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35520+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35521+{
35522+ int wkq_err;
e49829fe 35523+ struct super_block *sb;
1facf9fc 35524+
35525+ IMustLock(dir);
35526+
35527+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35528+ sb = dir->i_sb;
1facf9fc 35529+ args->dir = au_igrab(dir);
e49829fe 35530+ args->br = au_sbr(sb, bindex);
5afbbe0d 35531+ au_br_get(args->br);
1facf9fc 35532+ args->wh_dentry = dget(wh_dentry);
53392da6 35533+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35534+ if (unlikely(wkq_err)) {
523b37e3 35535+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35536+ au_whtmp_rmdir_free(args);
35537+ }
35538+}
7f207e10
AM
35539diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35540--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
be118d29 35541+++ linux/fs/aufs/whout.h 2018-04-06 07:48:44.207938097 +0200
3c1bdaff 35542@@ -0,0 +1,85 @@
1facf9fc 35543+/*
a2654f78 35544+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 35545+ *
35546+ * This program, aufs is free software; you can redistribute it and/or modify
35547+ * it under the terms of the GNU General Public License as published by
35548+ * the Free Software Foundation; either version 2 of the License, or
35549+ * (at your option) any later version.
dece6358
AM
35550+ *
35551+ * This program is distributed in the hope that it will be useful,
35552+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35553+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35554+ * GNU General Public License for more details.
35555+ *
35556+ * You should have received a copy of the GNU General Public License
523b37e3 35557+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35558+ */
35559+
35560+/*
35561+ * whiteout for logical deletion and opaque directory
35562+ */
35563+
35564+#ifndef __AUFS_WHOUT_H__
35565+#define __AUFS_WHOUT_H__
35566+
35567+#ifdef __KERNEL__
35568+
1facf9fc 35569+#include "dir.h"
35570+
35571+/* whout.c */
35572+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
35573+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35574+int au_diropq_test(struct dentry *h_dentry);
3c1bdaff 35575+struct au_branch;
1facf9fc 35576+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35577+ struct qstr *prefix);
35578+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35579+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35580+ struct dentry *dentry);
86dc4139 35581+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 35582+
35583+/* diropq flags */
35584+#define AuDiropq_CREATE 1
35585+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
35586+#define au_fset_diropq(flags, name) \
35587+ do { (flags) |= AuDiropq_##name; } while (0)
35588+#define au_fclr_diropq(flags, name) \
35589+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 35590+
35591+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35592+ unsigned int flags);
35593+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35594+ struct au_branch *br);
35595+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35596+ struct dentry *h_parent);
35597+
35598+/* real rmdir for the whiteout-ed dir */
35599+struct au_whtmp_rmdir {
35600+ struct inode *dir;
e49829fe 35601+ struct au_branch *br;
1facf9fc 35602+ struct dentry *wh_dentry;
dece6358 35603+ struct au_nhash whlist;
1facf9fc 35604+};
35605+
35606+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35607+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35608+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35609+ struct dentry *wh_dentry, struct au_nhash *whlist);
35610+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35611+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35612+
35613+/* ---------------------------------------------------------------------- */
35614+
35615+static inline struct dentry *au_diropq_create(struct dentry *dentry,
35616+ aufs_bindex_t bindex)
35617+{
35618+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35619+}
35620+
35621+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35622+{
35623+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35624+}
35625+
35626+#endif /* __KERNEL__ */
35627+#endif /* __AUFS_WHOUT_H__ */
7f207e10
AM
35628diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35629--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
be118d29 35630+++ linux/fs/aufs/wkq.c 2018-04-06 07:48:44.207938097 +0200
8b6a4947 35631@@ -0,0 +1,390 @@
1facf9fc 35632+/*
a2654f78 35633+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 35634+ *
35635+ * This program, aufs is free software; you can redistribute it and/or modify
35636+ * it under the terms of the GNU General Public License as published by
35637+ * the Free Software Foundation; either version 2 of the License, or
35638+ * (at your option) any later version.
dece6358
AM
35639+ *
35640+ * This program is distributed in the hope that it will be useful,
35641+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35642+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35643+ * GNU General Public License for more details.
35644+ *
35645+ * You should have received a copy of the GNU General Public License
523b37e3 35646+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35647+ */
35648+
35649+/*
35650+ * workqueue for asynchronous/super-io operations
35651+ * todo: try new dredential scheme
35652+ */
35653+
dece6358 35654+#include <linux/module.h>
1facf9fc 35655+#include "aufs.h"
35656+
9dbd164d 35657+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 35658+
9dbd164d 35659+static struct workqueue_struct *au_wkq;
1facf9fc 35660+
35661+struct au_wkinfo {
35662+ struct work_struct wk;
7f207e10 35663+ struct kobject *kobj;
1facf9fc 35664+
35665+ unsigned int flags; /* see wkq.h */
35666+
35667+ au_wkq_func_t func;
35668+ void *args;
35669+
8b6a4947
AM
35670+#ifdef CONFIG_LOCKDEP
35671+ int dont_check;
35672+ struct held_lock **hlock;
35673+#endif
35674+
1facf9fc 35675+ struct completion *comp;
35676+};
35677+
35678+/* ---------------------------------------------------------------------- */
8b6a4947
AM
35679+/*
35680+ * Aufs passes some operations to the workqueue such as the internal copyup.
35681+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35682+ * job run by workqueue depends upon the locks acquired in the other task.
35683+ * Delegating a small operation to the workqueue, aufs passes its lockdep
35684+ * information too. And the job in the workqueue restores the info in order to
35685+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
35686+ * correctly and expectedly.
35687+ */
35688+
35689+#ifndef CONFIG_LOCKDEP
35690+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35691+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35692+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35693+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35694+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35695+#else
35696+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35697+{
35698+ wkinfo->hlock = NULL;
35699+ wkinfo->dont_check = 0;
35700+}
35701+
35702+/*
35703+ * 1: matched
35704+ * 0: unmatched
35705+ */
35706+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35707+{
35708+ static DEFINE_SPINLOCK(spin);
35709+ static struct {
35710+ char *name;
35711+ struct lock_class_key *key;
35712+ } a[] = {
35713+ { .name = "&sbinfo->si_rwsem" },
35714+ { .name = "&finfo->fi_rwsem" },
35715+ { .name = "&dinfo->di_rwsem" },
35716+ { .name = "&iinfo->ii_rwsem" }
35717+ };
35718+ static int set;
35719+ int i;
35720+
35721+ /* lockless read from 'set.' see below */
35722+ if (set == ARRAY_SIZE(a)) {
35723+ for (i = 0; i < ARRAY_SIZE(a); i++)
35724+ if (a[i].key == key)
35725+ goto match;
35726+ goto unmatch;
35727+ }
35728+
35729+ spin_lock(&spin);
35730+ if (set)
35731+ for (i = 0; i < ARRAY_SIZE(a); i++)
35732+ if (a[i].key == key) {
35733+ spin_unlock(&spin);
35734+ goto match;
35735+ }
35736+ for (i = 0; i < ARRAY_SIZE(a); i++) {
35737+ if (a[i].key) {
35738+ if (unlikely(a[i].key == key)) { /* rare but possible */
35739+ spin_unlock(&spin);
35740+ goto match;
35741+ } else
35742+ continue;
35743+ }
35744+ if (strstr(a[i].name, name)) {
35745+ /*
35746+ * the order of these three lines is important for the
35747+ * lockless read above.
35748+ */
35749+ a[i].key = key;
35750+ spin_unlock(&spin);
35751+ set++;
35752+ /* AuDbg("%d, %s\n", set, name); */
35753+ goto match;
35754+ }
35755+ }
35756+ spin_unlock(&spin);
35757+ goto unmatch;
35758+
35759+match:
35760+ return 1;
35761+unmatch:
35762+ return 0;
35763+}
35764+
35765+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
35766+{
35767+ int err, n;
35768+ struct task_struct *curr;
35769+ struct held_lock **hl, *held_locks, *p;
35770+
35771+ err = 0;
35772+ curr = current;
35773+ wkinfo->dont_check = lockdep_recursing(curr);
35774+ if (wkinfo->dont_check)
35775+ goto out;
35776+ n = curr->lockdep_depth;
35777+ if (!n)
35778+ goto out;
35779+
35780+ err = -ENOMEM;
35781+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
35782+ if (unlikely(!wkinfo->hlock))
35783+ goto out;
35784+
35785+ err = 0;
35786+#if 0
35787+ if (0 && au_debug_test()) /* left for debugging */
35788+ lockdep_print_held_locks(curr);
35789+#endif
35790+ held_locks = curr->held_locks;
35791+ hl = wkinfo->hlock;
35792+ while (n--) {
35793+ p = held_locks++;
35794+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
35795+ *hl++ = p;
35796+ }
35797+ *hl = NULL;
35798+
35799+out:
35800+ return err;
35801+}
35802+
35803+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
35804+{
35805+ kfree(wkinfo->hlock);
35806+}
35807+
35808+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
35809+{
35810+ struct held_lock *p, **hl = wkinfo->hlock;
35811+ int subclass;
35812+
35813+ if (wkinfo->dont_check)
35814+ lockdep_off();
35815+ if (!hl)
35816+ return;
35817+ while ((p = *hl++)) { /* assignment */
35818+ subclass = lockdep_hlock_class(p)->subclass;
35819+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
35820+ if (p->read)
35821+ rwsem_acquire_read(p->instance, subclass, 0,
35822+ /*p->acquire_ip*/_RET_IP_);
35823+ else
35824+ rwsem_acquire(p->instance, subclass, 0,
35825+ /*p->acquire_ip*/_RET_IP_);
35826+ }
35827+}
35828+
35829+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
35830+{
35831+ struct held_lock *p, **hl = wkinfo->hlock;
35832+
35833+ if (wkinfo->dont_check)
35834+ lockdep_on();
35835+ if (!hl)
35836+ return;
35837+ while ((p = *hl++)) /* assignment */
35838+ rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
35839+}
35840+#endif
1facf9fc 35841+
1facf9fc 35842+static void wkq_func(struct work_struct *wk)
35843+{
35844+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
35845+
2dfbb274 35846+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
35847+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
35848+
8b6a4947 35849+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 35850+ wkinfo->func(wkinfo->args);
8b6a4947 35851+ au_wkq_lockdep_post(wkinfo);
1facf9fc 35852+ if (au_ftest_wkq(wkinfo->flags, WAIT))
35853+ complete(wkinfo->comp);
35854+ else {
7f207e10 35855+ kobject_put(wkinfo->kobj);
9dbd164d 35856+ module_put(THIS_MODULE); /* todo: ?? */
1c60b727 35857+ kfree(wkinfo);
1facf9fc 35858+ }
35859+}
35860+
35861+/*
35862+ * Since struct completion is large, try allocating it dynamically.
35863+ */
c2b27bf2 35864+#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
1facf9fc 35865+#define AuWkqCompDeclare(name) struct completion *comp = NULL
35866+
35867+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
35868+{
35869+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
35870+ if (*comp) {
35871+ init_completion(*comp);
35872+ wkinfo->comp = *comp;
35873+ return 0;
35874+ }
35875+ return -ENOMEM;
35876+}
35877+
35878+static void au_wkq_comp_free(struct completion *comp)
35879+{
1c60b727 35880+ kfree(comp);
1facf9fc 35881+}
35882+
35883+#else
35884+
35885+/* no braces */
35886+#define AuWkqCompDeclare(name) \
35887+ DECLARE_COMPLETION_ONSTACK(_ ## name); \
35888+ struct completion *comp = &_ ## name
35889+
35890+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
35891+{
35892+ wkinfo->comp = *comp;
35893+ return 0;
35894+}
35895+
35896+static void au_wkq_comp_free(struct completion *comp __maybe_unused)
35897+{
35898+ /* empty */
35899+}
35900+#endif /* 4KSTACKS */
35901+
53392da6 35902+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 35903+{
53392da6
AM
35904+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
35905+ if (au_wkq_test()) {
38d290e6
JR
35906+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
35907+ " due to a dead dir by UDBA?\n");
53392da6
AM
35908+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
35909+ }
35910+ } else
35911+ au_dbg_verify_kthread();
35912+
35913+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 35914+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 35915+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
35916+ } else {
35917+ INIT_WORK(&wkinfo->wk, wkq_func);
35918+ schedule_work(&wkinfo->wk);
35919+ }
1facf9fc 35920+}
35921+
7f207e10
AM
35922+/*
35923+ * Be careful. It is easy to make deadlock happen.
35924+ * processA: lock, wkq and wait
35925+ * processB: wkq and wait, lock in wkq
35926+ * --> deadlock
35927+ */
b752ccd1 35928+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 35929+{
35930+ int err;
35931+ AuWkqCompDeclare(comp);
35932+ struct au_wkinfo wkinfo = {
b752ccd1 35933+ .flags = flags,
1facf9fc 35934+ .func = func,
35935+ .args = args
35936+ };
35937+
35938+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
35939+ if (unlikely(err))
35940+ goto out;
35941+ err = au_wkq_lockdep_alloc(&wkinfo);
35942+ if (unlikely(err))
35943+ goto out_comp;
1facf9fc 35944+ if (!err) {
53392da6 35945+ au_wkq_run(&wkinfo);
1facf9fc 35946+ /* no timeout, no interrupt */
35947+ wait_for_completion(wkinfo.comp);
1facf9fc 35948+ }
8b6a4947 35949+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 35950+
8b6a4947
AM
35951+out_comp:
35952+ au_wkq_comp_free(comp);
35953+out:
35954+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 35955+ return err;
1facf9fc 35956+}
35957+
027c5e7a
AM
35958+/*
35959+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
35960+ * problem in a concurrent umounting.
35961+ */
53392da6
AM
35962+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
35963+ unsigned int flags)
1facf9fc 35964+{
35965+ int err;
35966+ struct au_wkinfo *wkinfo;
35967+
f0c0a007 35968+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 35969+
35970+ /*
35971+ * wkq_func() must free this wkinfo.
35972+ * it highly depends upon the implementation of workqueue.
35973+ */
35974+ err = 0;
35975+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
35976+ if (wkinfo) {
7f207e10 35977+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 35978+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 35979+ wkinfo->func = func;
35980+ wkinfo->args = args;
35981+ wkinfo->comp = NULL;
8b6a4947 35982+ au_wkq_lockdep_init(wkinfo);
7f207e10 35983+ kobject_get(wkinfo->kobj);
9dbd164d 35984+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 35985+
53392da6 35986+ au_wkq_run(wkinfo);
1facf9fc 35987+ } else {
35988+ err = -ENOMEM;
e49829fe 35989+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35990+ }
35991+
35992+ return err;
35993+}
35994+
35995+/* ---------------------------------------------------------------------- */
35996+
35997+void au_nwt_init(struct au_nowait_tasks *nwt)
35998+{
f0c0a007
AM
35999+ atomic_set(&nwt->nw_len, 0);
36000+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36001+ init_waitqueue_head(&nwt->nw_wq);
36002+}
36003+
36004+void au_wkq_fin(void)
36005+{
9dbd164d 36006+ destroy_workqueue(au_wkq);
1facf9fc 36007+}
36008+
36009+int __init au_wkq_init(void)
36010+{
9dbd164d 36011+ int err;
b752ccd1
AM
36012+
36013+ err = 0;
86dc4139 36014+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36015+ if (IS_ERR(au_wkq))
36016+ err = PTR_ERR(au_wkq);
36017+ else if (!au_wkq)
36018+ err = -ENOMEM;
b752ccd1
AM
36019+
36020+ return err;
1facf9fc 36021+}
7f207e10
AM
36022diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36023--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
be118d29 36024+++ linux/fs/aufs/wkq.h 2018-04-06 07:48:44.207938097 +0200
f0c0a007 36025@@ -0,0 +1,93 @@
1facf9fc 36026+/*
a2654f78 36027+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 36028+ *
36029+ * This program, aufs is free software; you can redistribute it and/or modify
36030+ * it under the terms of the GNU General Public License as published by
36031+ * the Free Software Foundation; either version 2 of the License, or
36032+ * (at your option) any later version.
dece6358
AM
36033+ *
36034+ * This program is distributed in the hope that it will be useful,
36035+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36036+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36037+ * GNU General Public License for more details.
36038+ *
36039+ * You should have received a copy of the GNU General Public License
523b37e3 36040+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36041+ */
36042+
36043+/*
36044+ * workqueue for asynchronous/super-io operations
36045+ * todo: try new credentials management scheme
36046+ */
36047+
36048+#ifndef __AUFS_WKQ_H__
36049+#define __AUFS_WKQ_H__
36050+
36051+#ifdef __KERNEL__
36052+
8b6a4947 36053+#include <linux/wait.h>
5afbbe0d 36054+
dece6358
AM
36055+struct super_block;
36056+
1facf9fc 36057+/* ---------------------------------------------------------------------- */
36058+
36059+/*
36060+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36061+ */
36062+struct au_nowait_tasks {
f0c0a007 36063+ atomic_t nw_len;
1facf9fc 36064+ wait_queue_head_t nw_wq;
36065+};
36066+
36067+/* ---------------------------------------------------------------------- */
36068+
36069+typedef void (*au_wkq_func_t)(void *args);
36070+
36071+/* wkq flags */
36072+#define AuWkq_WAIT 1
9dbd164d 36073+#define AuWkq_NEST (1 << 1)
1facf9fc 36074+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36075+#define au_fset_wkq(flags, name) \
36076+ do { (flags) |= AuWkq_##name; } while (0)
36077+#define au_fclr_wkq(flags, name) \
36078+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36079+
9dbd164d
AM
36080+#ifndef CONFIG_AUFS_HNOTIFY
36081+#undef AuWkq_NEST
36082+#define AuWkq_NEST 0
36083+#endif
36084+
1facf9fc 36085+/* wkq.c */
b752ccd1 36086+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36087+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36088+ unsigned int flags);
1facf9fc 36089+void au_nwt_init(struct au_nowait_tasks *nwt);
36090+int __init au_wkq_init(void);
36091+void au_wkq_fin(void);
36092+
36093+/* ---------------------------------------------------------------------- */
36094+
53392da6
AM
36095+static inline int au_wkq_test(void)
36096+{
36097+ return current->flags & PF_WQ_WORKER;
36098+}
36099+
b752ccd1 36100+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36101+{
b752ccd1 36102+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36103+}
36104+
36105+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36106+{
f0c0a007 36107+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36108+ wake_up_all(&nwt->nw_wq);
36109+}
36110+
36111+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36112+{
f0c0a007 36113+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36114+ return 0;
36115+}
36116+
36117+#endif /* __KERNEL__ */
36118+#endif /* __AUFS_WKQ_H__ */
c1595e42
JR
36119diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36120--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
be118d29 36121+++ linux/fs/aufs/xattr.c 2018-04-06 07:48:44.207938097 +0200
8b6a4947 36122@@ -0,0 +1,355 @@
c1595e42 36123+/*
a2654f78 36124+ * Copyright (C) 2014-2017 Junjiro R. Okajima
c1595e42
JR
36125+ *
36126+ * This program, aufs is free software; you can redistribute it and/or modify
36127+ * it under the terms of the GNU General Public License as published by
36128+ * the Free Software Foundation; either version 2 of the License, or
36129+ * (at your option) any later version.
36130+ *
36131+ * This program is distributed in the hope that it will be useful,
36132+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36133+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36134+ * GNU General Public License for more details.
36135+ *
36136+ * You should have received a copy of the GNU General Public License
36137+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36138+ */
36139+
36140+/*
36141+ * handling xattr functions
36142+ */
36143+
a2654f78
AM
36144+#include <linux/fs.h>
36145+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36146+#include <linux/xattr.h>
36147+#include "aufs.h"
36148+
36149+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36150+{
36151+ if (!ignore_flags)
36152+ goto out;
36153+ switch (err) {
36154+ case -ENOMEM:
36155+ case -EDQUOT:
36156+ goto out;
36157+ }
36158+
36159+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36160+ err = 0;
36161+ goto out;
36162+ }
36163+
36164+#define cmp(brattr, prefix) do { \
36165+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36166+ XATTR_##prefix##_PREFIX_LEN)) { \
36167+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36168+ err = 0; \
36169+ goto out; \
36170+ } \
36171+ } while (0)
36172+
36173+ cmp(SEC, SECURITY);
36174+ cmp(SYS, SYSTEM);
36175+ cmp(TR, TRUSTED);
36176+ cmp(USR, USER);
36177+#undef cmp
36178+
36179+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36180+ err = 0;
36181+
36182+out:
36183+ return err;
36184+}
36185+
36186+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36187+
36188+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
36189+ char *name, char **buf, unsigned int ignore_flags,
36190+ unsigned int verbose)
c1595e42
JR
36191+{
36192+ int err;
36193+ ssize_t ssz;
36194+ struct inode *h_idst;
36195+
36196+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36197+ err = ssz;
36198+ if (unlikely(err <= 0)) {
c1595e42
JR
36199+ if (err == -ENODATA
36200+ || (err == -EOPNOTSUPP
b912730e 36201+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 36202+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
36203+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36204+ || !strcmp(name,
36205+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36206+ ))
c1595e42 36207+ err = 0;
b912730e
AM
36208+ if (err && (verbose || au_debug_test()))
36209+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36210+ goto out;
36211+ }
36212+
36213+ /* unlock it temporary */
5527c038 36214+ h_idst = d_inode(h_dst);
febd17d6 36215+ inode_unlock(h_idst);
c1595e42 36216+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 36217+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36218+ if (unlikely(err)) {
7e9cd9fe
AM
36219+ if (verbose || au_debug_test())
36220+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36221+ err = au_xattr_ignore(err, name, ignore_flags);
36222+ }
36223+
36224+out:
36225+ return err;
36226+}
36227+
7e9cd9fe
AM
36228+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36229+ unsigned int verbose)
c1595e42
JR
36230+{
36231+ int err, unlocked, acl_access, acl_default;
36232+ ssize_t ssz;
36233+ struct inode *h_isrc, *h_idst;
36234+ char *value, *p, *o, *e;
36235+
36236+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36237+ /* there should not be the parent-child relationship between them */
5527c038
JR
36238+ h_isrc = d_inode(h_src);
36239+ h_idst = d_inode(h_dst);
febd17d6 36240+ inode_unlock(h_idst);
be118d29 36241+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36242+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36243+ unlocked = 0;
36244+
36245+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36246+ ssz = vfs_listxattr(h_src, NULL, 0);
36247+ err = ssz;
36248+ if (unlikely(err < 0)) {
36249+ AuTraceErr(err);
36250+ if (err == -ENODATA
36251+ || err == -EOPNOTSUPP)
36252+ err = 0; /* ignore */
36253+ goto out;
36254+ }
36255+
36256+ err = 0;
36257+ p = NULL;
36258+ o = NULL;
36259+ if (ssz) {
36260+ err = -ENOMEM;
36261+ p = kmalloc(ssz, GFP_NOFS);
36262+ o = p;
36263+ if (unlikely(!p))
36264+ goto out;
36265+ err = vfs_listxattr(h_src, p, ssz);
36266+ }
3c1bdaff 36267+ inode_unlock_shared(h_isrc);
c1595e42
JR
36268+ unlocked = 1;
36269+ AuDbg("err %d, ssz %zd\n", err, ssz);
36270+ if (unlikely(err < 0))
36271+ goto out_free;
36272+
36273+ err = 0;
36274+ e = p + ssz;
36275+ value = NULL;
36276+ acl_access = 0;
36277+ acl_default = 0;
36278+ while (!err && p < e) {
36279+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36280+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36281+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36282+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36283+ - 1);
7e9cd9fe
AM
36284+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36285+ verbose);
c1595e42
JR
36286+ p += strlen(p) + 1;
36287+ }
36288+ AuTraceErr(err);
36289+ ignore_flags |= au_xattr_out_of_list;
36290+ if (!err && !acl_access) {
36291+ err = au_do_cpup_xattr(h_dst, h_src,
36292+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36293+ ignore_flags, verbose);
c1595e42
JR
36294+ AuTraceErr(err);
36295+ }
36296+ if (!err && !acl_default) {
36297+ err = au_do_cpup_xattr(h_dst, h_src,
36298+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36299+ ignore_flags, verbose);
c1595e42
JR
36300+ AuTraceErr(err);
36301+ }
36302+
8b6a4947 36303+ kfree(value);
c1595e42
JR
36304+
36305+out_free:
8b6a4947 36306+ kfree(o);
c1595e42
JR
36307+out:
36308+ if (!unlocked)
3c1bdaff 36309+ inode_unlock_shared(h_isrc);
c1595e42
JR
36310+ AuTraceErr(err);
36311+ return err;
36312+}
36313+
36314+/* ---------------------------------------------------------------------- */
36315+
a2654f78
AM
36316+static int au_smack_reentering(struct super_block *sb)
36317+{
36318+#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36319+ /*
36320+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36321+ * i_op->getxattr(). ouch.
36322+ */
36323+ return si_pid_test(sb);
36324+#else
36325+ return 0;
36326+#endif
36327+}
36328+
c1595e42
JR
36329+enum {
36330+ AU_XATTR_LIST,
36331+ AU_XATTR_GET
36332+};
36333+
36334+struct au_lgxattr {
36335+ int type;
36336+ union {
36337+ struct {
36338+ char *list;
36339+ size_t size;
36340+ } list;
36341+ struct {
36342+ const char *name;
36343+ void *value;
36344+ size_t size;
36345+ } get;
36346+ } u;
36347+};
36348+
36349+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36350+{
36351+ ssize_t err;
a2654f78 36352+ int reenter;
c1595e42
JR
36353+ struct path h_path;
36354+ struct super_block *sb;
36355+
36356+ sb = dentry->d_sb;
a2654f78
AM
36357+ reenter = au_smack_reentering(sb);
36358+ if (!reenter) {
36359+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36360+ if (unlikely(err))
36361+ goto out;
36362+ }
36363+ err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
c1595e42
JR
36364+ if (unlikely(err))
36365+ goto out_si;
36366+ if (unlikely(!h_path.dentry))
36367+ /* illegally overlapped or something */
36368+ goto out_di; /* pretending success */
36369+
36370+ /* always topmost entry only */
36371+ switch (arg->type) {
36372+ case AU_XATTR_LIST:
36373+ err = vfs_listxattr(h_path.dentry,
36374+ arg->u.list.list, arg->u.list.size);
36375+ break;
36376+ case AU_XATTR_GET:
5afbbe0d 36377+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
36378+ err = vfs_getxattr(h_path.dentry,
36379+ arg->u.get.name, arg->u.get.value,
36380+ arg->u.get.size);
36381+ break;
36382+ }
36383+
36384+out_di:
a2654f78
AM
36385+ if (!reenter)
36386+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36387+out_si:
a2654f78
AM
36388+ if (!reenter)
36389+ si_read_unlock(sb);
c1595e42
JR
36390+out:
36391+ AuTraceErr(err);
36392+ return err;
36393+}
36394+
36395+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36396+{
36397+ struct au_lgxattr arg = {
36398+ .type = AU_XATTR_LIST,
36399+ .u.list = {
36400+ .list = list,
36401+ .size = size
36402+ },
36403+ };
36404+
36405+ return au_lgxattr(dentry, &arg);
36406+}
36407+
f2c43d5f
AM
36408+static ssize_t au_getxattr(struct dentry *dentry,
36409+ struct inode *inode __maybe_unused,
36410+ const char *name, void *value, size_t size)
c1595e42
JR
36411+{
36412+ struct au_lgxattr arg = {
36413+ .type = AU_XATTR_GET,
36414+ .u.get = {
36415+ .name = name,
36416+ .value = value,
36417+ .size = size
36418+ },
36419+ };
36420+
36421+ return au_lgxattr(dentry, &arg);
36422+}
36423+
f2c43d5f
AM
36424+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36425+ const char *name, const void *value, size_t size,
36426+ int flags)
c1595e42 36427+{
f2c43d5f 36428+ struct au_sxattr arg = {
c1595e42
JR
36429+ .type = AU_XATTR_SET,
36430+ .u.set = {
36431+ .name = name,
36432+ .value = value,
36433+ .size = size,
36434+ .flags = flags
36435+ },
36436+ };
36437+
f2c43d5f 36438+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36439+}
36440+
36441+/* ---------------------------------------------------------------------- */
36442+
f2c43d5f
AM
36443+static int au_xattr_get(const struct xattr_handler *handler,
36444+ struct dentry *dentry, struct inode *inode,
36445+ const char *name, void *buffer, size_t size)
c1595e42 36446+{
f2c43d5f 36447+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36448+}
36449+
f2c43d5f
AM
36450+static int au_xattr_set(const struct xattr_handler *handler,
36451+ struct dentry *dentry, struct inode *inode,
36452+ const char *name, const void *value, size_t size,
36453+ int flags)
c1595e42 36454+{
f2c43d5f 36455+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36456+}
36457+
36458+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36459+ .name = "",
36460+ .prefix = "",
c1595e42
JR
36461+ .get = au_xattr_get,
36462+ .set = au_xattr_set
c1595e42
JR
36463+};
36464+
36465+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36466+#ifdef CONFIG_FS_POSIX_ACL
36467+ &posix_acl_access_xattr_handler,
36468+ &posix_acl_default_xattr_handler,
36469+#endif
36470+ &au_xattr_handler, /* must be last */
f2c43d5f 36471+ NULL
c1595e42
JR
36472+};
36473+
36474+void au_xattr_init(struct super_block *sb)
36475+{
f2c43d5f 36476+ sb->s_xattr = au_xattr_handlers;
c1595e42 36477+}
7f207e10
AM
36478diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36479--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
be118d29
JR
36480+++ linux/fs/aufs/xino.c 2018-04-06 07:48:44.207938097 +0200
36481@@ -0,0 +1,1469 @@
1facf9fc 36482+/*
a2654f78 36483+ * Copyright (C) 2005-2017 Junjiro R. Okajima
1facf9fc 36484+ *
36485+ * This program, aufs is free software; you can redistribute it and/or modify
36486+ * it under the terms of the GNU General Public License as published by
36487+ * the Free Software Foundation; either version 2 of the License, or
36488+ * (at your option) any later version.
dece6358
AM
36489+ *
36490+ * This program is distributed in the hope that it will be useful,
36491+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36492+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36493+ * GNU General Public License for more details.
36494+ *
36495+ * You should have received a copy of the GNU General Public License
523b37e3 36496+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36497+ */
36498+
36499+/*
36500+ * external inode number translation table and bitmap
36501+ */
36502+
36503+#include <linux/seq_file.h>
392086de 36504+#include <linux/statfs.h>
1facf9fc 36505+#include "aufs.h"
36506+
be118d29
JR
36507+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
36508+ size_t size, loff_t *pos);
36509+
9dbd164d 36510+/* todo: unnecessary to support mmap_sem since kernel-space? */
5527c038 36511+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
1facf9fc 36512+ loff_t *pos)
36513+{
36514+ ssize_t err;
36515+ mm_segment_t oldfs;
b752ccd1
AM
36516+ union {
36517+ void *k;
36518+ char __user *u;
36519+ } buf;
be118d29
JR
36520+ int i;
36521+ const int prevent_endless = 10;
1facf9fc 36522+
be118d29 36523+ i = 0;
b752ccd1 36524+ buf.k = kbuf;
1facf9fc 36525+ oldfs = get_fs();
36526+ set_fs(KERNEL_DS);
36527+ do {
b752ccd1 36528+ err = func(file, buf.u, size, pos);
be118d29
JR
36529+ if (err == -EINTR
36530+ && !au_wkq_test()
36531+ && fatal_signal_pending(current)) {
36532+ set_fs(oldfs);
36533+ err = xino_fread_wkq(func, file, kbuf, size, pos);
36534+ BUG_ON(err == -EINTR);
36535+ oldfs = get_fs();
36536+ set_fs(KERNEL_DS);
36537+ }
36538+ } while (i++ < prevent_endless
36539+ && (err == -EAGAIN || err == -EINTR));
1facf9fc 36540+ set_fs(oldfs);
36541+
36542+#if 0 /* reserved for future use */
36543+ if (err > 0)
2000de60 36544+ fsnotify_access(file->f_path.dentry);
1facf9fc 36545+#endif
36546+
36547+ return err;
36548+}
36549+
be118d29
JR
36550+struct xino_fread_args {
36551+ ssize_t *errp;
36552+ vfs_readf_t func;
36553+ struct file *file;
36554+ void *buf;
36555+ size_t size;
36556+ loff_t *pos;
36557+};
36558+
36559+static void call_xino_fread(void *args)
36560+{
36561+ struct xino_fread_args *a = args;
36562+ *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
36563+}
36564+
36565+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
36566+ size_t size, loff_t *pos)
36567+{
36568+ ssize_t err;
36569+ int wkq_err;
36570+ struct xino_fread_args args = {
36571+ .errp = &err,
36572+ .func = func,
36573+ .file = file,
36574+ .buf = buf,
36575+ .size = size,
36576+ .pos = pos
36577+ };
36578+
36579+ wkq_err = au_wkq_wait(call_xino_fread, &args);
36580+ if (unlikely(wkq_err))
36581+ err = wkq_err;
36582+
36583+ return err;
36584+}
36585+
1facf9fc 36586+/* ---------------------------------------------------------------------- */
36587+
be52b249
AM
36588+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
36589+ size_t size, loff_t *pos);
36590+
5527c038 36591+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
1facf9fc 36592+ size_t size, loff_t *pos)
36593+{
36594+ ssize_t err;
36595+ mm_segment_t oldfs;
b752ccd1
AM
36596+ union {
36597+ void *k;
36598+ const char __user *u;
36599+ } buf;
be52b249
AM
36600+ int i;
36601+ const int prevent_endless = 10;
1facf9fc 36602+
be52b249 36603+ i = 0;
b752ccd1 36604+ buf.k = kbuf;
1facf9fc 36605+ oldfs = get_fs();
36606+ set_fs(KERNEL_DS);
1facf9fc 36607+ do {
b752ccd1 36608+ err = func(file, buf.u, size, pos);
be52b249
AM
36609+ if (err == -EINTR
36610+ && !au_wkq_test()
36611+ && fatal_signal_pending(current)) {
36612+ set_fs(oldfs);
36613+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
36614+ BUG_ON(err == -EINTR);
36615+ oldfs = get_fs();
36616+ set_fs(KERNEL_DS);
36617+ }
36618+ } while (i++ < prevent_endless
36619+ && (err == -EAGAIN || err == -EINTR));
1facf9fc 36620+ set_fs(oldfs);
36621+
36622+#if 0 /* reserved for future use */
36623+ if (err > 0)
2000de60 36624+ fsnotify_modify(file->f_path.dentry);
1facf9fc 36625+#endif
36626+
36627+ return err;
36628+}
36629+
36630+struct do_xino_fwrite_args {
36631+ ssize_t *errp;
5527c038 36632+ vfs_writef_t func;
1facf9fc 36633+ struct file *file;
36634+ void *buf;
36635+ size_t size;
36636+ loff_t *pos;
36637+};
36638+
36639+static void call_do_xino_fwrite(void *args)
36640+{
36641+ struct do_xino_fwrite_args *a = args;
36642+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
36643+}
36644+
be52b249
AM
36645+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
36646+ size_t size, loff_t *pos)
36647+{
36648+ ssize_t err;
36649+ int wkq_err;
36650+ struct do_xino_fwrite_args args = {
36651+ .errp = &err,
36652+ .func = func,
36653+ .file = file,
36654+ .buf = buf,
36655+ .size = size,
36656+ .pos = pos
36657+ };
36658+
36659+ /*
36660+ * it breaks RLIMIT_FSIZE and normal user's limit,
36661+ * users should care about quota and real 'filesystem full.'
36662+ */
36663+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
36664+ if (unlikely(wkq_err))
36665+ err = wkq_err;
36666+
36667+ return err;
36668+}
36669+
5527c038
JR
36670+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
36671+ size_t size, loff_t *pos)
1facf9fc 36672+{
36673+ ssize_t err;
36674+
b752ccd1
AM
36675+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
36676+ lockdep_off();
36677+ err = do_xino_fwrite(func, file, buf, size, pos);
36678+ lockdep_on();
8b6a4947
AM
36679+ } else {
36680+ lockdep_off();
be52b249 36681+ err = xino_fwrite_wkq(func, file, buf, size, pos);
8b6a4947
AM
36682+ lockdep_on();
36683+ }
1facf9fc 36684+
36685+ return err;
36686+}
36687+
36688+/* ---------------------------------------------------------------------- */
36689+
36690+/*
36691+ * create a new xinofile at the same place/path as @base_file.
36692+ */
36693+struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
36694+{
36695+ struct file *file;
4a4d8108 36696+ struct dentry *base, *parent;
523b37e3 36697+ struct inode *dir, *delegated;
1facf9fc 36698+ struct qstr *name;
1308ab2a 36699+ struct path path;
4a4d8108 36700+ int err;
1facf9fc 36701+
2000de60 36702+ base = base_file->f_path.dentry;
1facf9fc 36703+ parent = base->d_parent; /* dir inode is locked */
5527c038 36704+ dir = d_inode(parent);
1facf9fc 36705+ IMustLock(dir);
36706+
36707+ file = ERR_PTR(-EINVAL);
36708+ name = &base->d_name;
4a4d8108
AM
36709+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
36710+ if (IS_ERR(path.dentry)) {
36711+ file = (void *)path.dentry;
523b37e3
AM
36712+ pr_err("%pd lookup err %ld\n",
36713+ base, PTR_ERR(path.dentry));
1facf9fc 36714+ goto out;
36715+ }
36716+
36717+ /* no need to mnt_want_write() since we call dentry_open() later */
4a4d8108 36718+ err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
1facf9fc 36719+ if (unlikely(err)) {
36720+ file = ERR_PTR(err);
523b37e3 36721+ pr_err("%pd create err %d\n", base, err);
1facf9fc 36722+ goto out_dput;
36723+ }
36724+
c06a8ce3 36725+ path.mnt = base_file->f_path.mnt;
4a4d8108 36726+ file = vfsub_dentry_open(&path,
7f207e10 36727+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
2cbb1c4b 36728+ /* | __FMODE_NONOTIFY */);
1facf9fc 36729+ if (IS_ERR(file)) {
523b37e3 36730+ pr_err("%pd open err %ld\n", base, PTR_ERR(file));
1facf9fc 36731+ goto out_dput;
36732+ }
36733+
523b37e3
AM
36734+ delegated = NULL;
36735+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
36736+ if (unlikely(err == -EWOULDBLOCK)) {
36737+ pr_warn("cannot retry for NFSv4 delegation"
36738+ " for an internal unlink\n");
36739+ iput(delegated);
36740+ }
1facf9fc 36741+ if (unlikely(err)) {
523b37e3 36742+ pr_err("%pd unlink err %d\n", base, err);
1facf9fc 36743+ goto out_fput;
36744+ }
36745+
36746+ if (copy_src) {
36747+ /* no one can touch copy_src xino */
c06a8ce3 36748+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
1facf9fc 36749+ if (unlikely(err)) {
523b37e3 36750+ pr_err("%pd copy err %d\n", base, err);
1facf9fc 36751+ goto out_fput;
36752+ }
36753+ }
36754+ goto out_dput; /* success */
36755+
4f0767ce 36756+out_fput:
1facf9fc 36757+ fput(file);
36758+ file = ERR_PTR(err);
4f0767ce 36759+out_dput:
4a4d8108 36760+ dput(path.dentry);
4f0767ce 36761+out:
1facf9fc 36762+ return file;
36763+}
36764+
36765+struct au_xino_lock_dir {
36766+ struct au_hinode *hdir;
36767+ struct dentry *parent;
febd17d6 36768+ struct inode *dir;
1facf9fc 36769+};
36770+
36771+static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
36772+ struct au_xino_lock_dir *ldir)
36773+{
36774+ aufs_bindex_t brid, bindex;
36775+
36776+ ldir->hdir = NULL;
36777+ bindex = -1;
36778+ brid = au_xino_brid(sb);
36779+ if (brid >= 0)
36780+ bindex = au_br_index(sb, brid);
36781+ if (bindex >= 0) {
5527c038 36782+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 36783+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 36784+ } else {
2000de60 36785+ ldir->parent = dget_parent(xino->f_path.dentry);
febd17d6
JR
36786+ ldir->dir = d_inode(ldir->parent);
36787+ inode_lock_nested(ldir->dir, AuLsc_I_PARENT);
1facf9fc 36788+ }
36789+}
36790+
36791+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36792+{
36793+ if (ldir->hdir)
5afbbe0d 36794+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 36795+ else {
febd17d6 36796+ inode_unlock(ldir->dir);
1facf9fc 36797+ dput(ldir->parent);
36798+ }
36799+}
36800+
36801+/* ---------------------------------------------------------------------- */
36802+
36803+/* trucate xino files asynchronously */
36804+
36805+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
36806+{
36807+ int err;
392086de
AM
36808+ unsigned long jiffy;
36809+ blkcnt_t blocks;
5afbbe0d 36810+ aufs_bindex_t bi, bbot;
392086de 36811+ struct kstatfs *st;
1facf9fc 36812+ struct au_branch *br;
36813+ struct file *new_xino, *file;
36814+ struct super_block *h_sb;
36815+ struct au_xino_lock_dir ldir;
36816+
392086de 36817+ err = -ENOMEM;
be52b249 36818+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
36819+ if (unlikely(!st))
36820+ goto out;
36821+
1facf9fc 36822+ err = -EINVAL;
5afbbe0d
AM
36823+ bbot = au_sbbot(sb);
36824+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 36825+ goto out_st;
1facf9fc 36826+ br = au_sbr(sb, bindex);
36827+ file = br->br_xino.xi_file;
36828+ if (!file)
392086de
AM
36829+ goto out_st;
36830+
36831+ err = vfs_statfs(&file->f_path, st);
36832+ if (unlikely(err))
36833+ AuErr1("statfs err %d, ignored\n", err);
36834+ jiffy = jiffies;
36835+ blocks = file_inode(file)->i_blocks;
36836+ pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36837+ bindex, (u64)blocks, st->f_bfree, st->f_blocks);
1facf9fc 36838+
36839+ au_xino_lock_dir(sb, file, &ldir);
36840+ /* mnt_want_write() is unnecessary here */
36841+ new_xino = au_xino_create2(file, file);
36842+ au_xino_unlock_dir(&ldir);
36843+ err = PTR_ERR(new_xino);
392086de
AM
36844+ if (IS_ERR(new_xino)) {
36845+ pr_err("err %d, ignored\n", err);
36846+ goto out_st;
36847+ }
1facf9fc 36848+ err = 0;
36849+ fput(file);
36850+ br->br_xino.xi_file = new_xino;
36851+
86dc4139 36852+ h_sb = au_br_sb(br);
5afbbe0d 36853+ for (bi = 0; bi <= bbot; bi++) {
1facf9fc 36854+ if (unlikely(bi == bindex))
36855+ continue;
36856+ br = au_sbr(sb, bi);
86dc4139 36857+ if (au_br_sb(br) != h_sb)
1facf9fc 36858+ continue;
36859+
36860+ fput(br->br_xino.xi_file);
36861+ br->br_xino.xi_file = new_xino;
36862+ get_file(new_xino);
36863+ }
36864+
392086de
AM
36865+ err = vfs_statfs(&new_xino->f_path, st);
36866+ if (!err) {
36867+ pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36868+ bindex, (u64)file_inode(new_xino)->i_blocks,
36869+ st->f_bfree, st->f_blocks);
36870+ if (file_inode(new_xino)->i_blocks < blocks)
36871+ au_sbi(sb)->si_xino_jiffy = jiffy;
36872+ } else
36873+ AuErr1("statfs err %d, ignored\n", err);
36874+
36875+out_st:
1c60b727 36876+ kfree(st);
4f0767ce 36877+out:
1facf9fc 36878+ return err;
36879+}
36880+
36881+struct xino_do_trunc_args {
36882+ struct super_block *sb;
36883+ struct au_branch *br;
36884+};
36885+
36886+static void xino_do_trunc(void *_args)
36887+{
36888+ struct xino_do_trunc_args *args = _args;
36889+ struct super_block *sb;
36890+ struct au_branch *br;
36891+ struct inode *dir;
36892+ int err;
36893+ aufs_bindex_t bindex;
36894+
36895+ err = 0;
36896+ sb = args->sb;
5527c038 36897+ dir = d_inode(sb->s_root);
1facf9fc 36898+ br = args->br;
36899+
36900+ si_noflush_write_lock(sb);
36901+ ii_read_lock_parent(dir);
36902+ bindex = au_br_index(sb, br->br_id);
36903+ err = au_xino_trunc(sb, bindex);
1facf9fc 36904+ ii_read_unlock(dir);
36905+ if (unlikely(err))
392086de 36906+ pr_warn("err b%d, (%d)\n", bindex, err);
1facf9fc 36907+ atomic_dec(&br->br_xino_running);
5afbbe0d 36908+ au_br_put(br);
1facf9fc 36909+ si_write_unlock(sb);
027c5e7a 36910+ au_nwt_done(&au_sbi(sb)->si_nowait);
1c60b727 36911+ kfree(args);
1facf9fc 36912+}
36913+
392086de
AM
36914+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
36915+{
36916+ int err;
36917+ struct kstatfs st;
36918+ struct au_sbinfo *sbinfo;
36919+
36920+ /* todo: si_xino_expire and the ratio should be customizable */
36921+ sbinfo = au_sbi(sb);
36922+ if (time_before(jiffies,
36923+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
36924+ return 0;
36925+
36926+ /* truncation border */
36927+ err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
36928+ if (unlikely(err)) {
36929+ AuErr1("statfs err %d, ignored\n", err);
36930+ return 0;
36931+ }
36932+ if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
36933+ return 0;
36934+
36935+ return 1;
36936+}
36937+
1facf9fc 36938+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
36939+{
36940+ struct xino_do_trunc_args *args;
36941+ int wkq_err;
36942+
392086de 36943+ if (!xino_trunc_test(sb, br))
1facf9fc 36944+ return;
36945+
36946+ if (atomic_inc_return(&br->br_xino_running) > 1)
36947+ goto out;
36948+
36949+ /* lock and kfree() will be called in trunc_xino() */
36950+ args = kmalloc(sizeof(*args), GFP_NOFS);
36951+ if (unlikely(!args)) {
36952+ AuErr1("no memory\n");
f0c0a007 36953+ goto out;
1facf9fc 36954+ }
36955+
5afbbe0d 36956+ au_br_get(br);
1facf9fc 36957+ args->sb = sb;
36958+ args->br = br;
53392da6 36959+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 36960+ if (!wkq_err)
36961+ return; /* success */
36962+
4a4d8108 36963+ pr_err("wkq %d\n", wkq_err);
5afbbe0d 36964+ au_br_put(br);
1c60b727 36965+ kfree(args);
1facf9fc 36966+
4f0767ce 36967+out:
e49829fe 36968+ atomic_dec(&br->br_xino_running);
1facf9fc 36969+}
36970+
36971+/* ---------------------------------------------------------------------- */
36972+
5527c038 36973+static int au_xino_do_write(vfs_writef_t write, struct file *file,
1facf9fc 36974+ ino_t h_ino, ino_t ino)
36975+{
36976+ loff_t pos;
36977+ ssize_t sz;
36978+
36979+ pos = h_ino;
36980+ if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
36981+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
36982+ return -EFBIG;
36983+ }
36984+ pos *= sizeof(ino);
36985+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
36986+ if (sz == sizeof(ino))
36987+ return 0; /* success */
36988+
36989+ AuIOErr("write failed (%zd)\n", sz);
36990+ return -EIO;
36991+}
36992+
36993+/*
36994+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
36995+ * at the position of @h_ino.
36996+ * even if @ino is zero, it is written to the xinofile and means no entry.
36997+ * if the size of the xino file on a specific filesystem exceeds the watermark,
36998+ * try truncating it.
36999+ */
37000+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37001+ ino_t ino)
37002+{
37003+ int err;
37004+ unsigned int mnt_flags;
37005+ struct au_branch *br;
37006+
37007+ BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
37008+ || ((loff_t)-1) > 0);
dece6358 37009+ SiMustAnyLock(sb);
1facf9fc 37010+
37011+ mnt_flags = au_mntflags(sb);
37012+ if (!au_opt_test(mnt_flags, XINO))
37013+ return 0;
37014+
37015+ br = au_sbr(sb, bindex);
37016+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
37017+ h_ino, ino);
37018+ if (!err) {
37019+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37020+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37021+ xino_try_trunc(sb, br);
37022+ return 0; /* success */
37023+ }
37024+
37025+ AuIOErr("write failed (%d)\n", err);
37026+ return -EIO;
37027+}
37028+
37029+/* ---------------------------------------------------------------------- */
37030+
37031+/* aufs inode number bitmap */
37032+
37033+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37034+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37035+{
37036+ ino_t ino;
37037+
37038+ AuDebugOn(bit < 0 || page_bits <= bit);
37039+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37040+ return ino;
37041+}
37042+
37043+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37044+{
37045+ AuDebugOn(ino < AUFS_FIRST_INO);
37046+ ino -= AUFS_FIRST_INO;
37047+ *pindex = ino / page_bits;
37048+ *bit = ino % page_bits;
37049+}
37050+
37051+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37052+{
37053+ int err;
37054+ loff_t pos;
37055+ ssize_t sz;
37056+ struct au_sbinfo *sbinfo;
37057+ struct file *xib;
37058+ unsigned long *p;
37059+
37060+ sbinfo = au_sbi(sb);
37061+ MtxMustLock(&sbinfo->si_xib_mtx);
37062+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37063+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37064+
37065+ if (pindex == sbinfo->si_xib_last_pindex)
37066+ return 0;
37067+
37068+ xib = sbinfo->si_xib;
37069+ p = sbinfo->si_xib_buf;
37070+ pos = sbinfo->si_xib_last_pindex;
37071+ pos *= PAGE_SIZE;
37072+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37073+ if (unlikely(sz != PAGE_SIZE))
37074+ goto out;
37075+
37076+ pos = pindex;
37077+ pos *= PAGE_SIZE;
c06a8ce3 37078+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 37079+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37080+ else {
37081+ memset(p, 0, PAGE_SIZE);
37082+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37083+ }
37084+ if (sz == PAGE_SIZE) {
37085+ sbinfo->si_xib_last_pindex = pindex;
37086+ return 0; /* success */
37087+ }
37088+
4f0767ce 37089+out:
b752ccd1
AM
37090+ AuIOErr1("write failed (%zd)\n", sz);
37091+ err = sz;
37092+ if (sz >= 0)
37093+ err = -EIO;
37094+ return err;
37095+}
37096+
37097+/* ---------------------------------------------------------------------- */
37098+
37099+static void au_xib_clear_bit(struct inode *inode)
37100+{
37101+ int err, bit;
37102+ unsigned long pindex;
37103+ struct super_block *sb;
37104+ struct au_sbinfo *sbinfo;
37105+
37106+ AuDebugOn(inode->i_nlink);
37107+
37108+ sb = inode->i_sb;
37109+ xib_calc_bit(inode->i_ino, &pindex, &bit);
37110+ AuDebugOn(page_bits <= bit);
37111+ sbinfo = au_sbi(sb);
37112+ mutex_lock(&sbinfo->si_xib_mtx);
37113+ err = xib_pindex(sb, pindex);
37114+ if (!err) {
37115+ clear_bit(bit, sbinfo->si_xib_buf);
37116+ sbinfo->si_xib_next_bit = bit;
37117+ }
37118+ mutex_unlock(&sbinfo->si_xib_mtx);
37119+}
37120+
37121+/* for s_op->delete_inode() */
37122+void au_xino_delete_inode(struct inode *inode, const int unlinked)
37123+{
37124+ int err;
37125+ unsigned int mnt_flags;
5afbbe0d 37126+ aufs_bindex_t bindex, bbot, bi;
b752ccd1
AM
37127+ unsigned char try_trunc;
37128+ struct au_iinfo *iinfo;
37129+ struct super_block *sb;
37130+ struct au_hinode *hi;
37131+ struct inode *h_inode;
37132+ struct au_branch *br;
5527c038 37133+ vfs_writef_t xwrite;
b752ccd1 37134+
5afbbe0d
AM
37135+ AuDebugOn(au_is_bad_inode(inode));
37136+
b752ccd1
AM
37137+ sb = inode->i_sb;
37138+ mnt_flags = au_mntflags(sb);
37139+ if (!au_opt_test(mnt_flags, XINO)
37140+ || inode->i_ino == AUFS_ROOT_INO)
37141+ return;
37142+
37143+ if (unlinked) {
37144+ au_xigen_inc(inode);
37145+ au_xib_clear_bit(inode);
37146+ }
37147+
37148+ iinfo = au_ii(inode);
5afbbe0d 37149+ bindex = iinfo->ii_btop;
b752ccd1
AM
37150+ if (bindex < 0)
37151+ return;
1facf9fc 37152+
b752ccd1
AM
37153+ xwrite = au_sbi(sb)->si_xwrite;
37154+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
5afbbe0d
AM
37155+ hi = au_hinode(iinfo, bindex);
37156+ bbot = iinfo->ii_bbot;
37157+ for (; bindex <= bbot; bindex++, hi++) {
b752ccd1
AM
37158+ h_inode = hi->hi_inode;
37159+ if (!h_inode
37160+ || (!unlinked && h_inode->i_nlink))
37161+ continue;
1facf9fc 37162+
b752ccd1
AM
37163+ /* inode may not be revalidated */
37164+ bi = au_br_index(sb, hi->hi_id);
37165+ if (bi < 0)
37166+ continue;
1facf9fc 37167+
b752ccd1
AM
37168+ br = au_sbr(sb, bi);
37169+ err = au_xino_do_write(xwrite, br->br_xino.xi_file,
37170+ h_inode->i_ino, /*ino*/0);
37171+ if (!err && try_trunc
86dc4139 37172+ && au_test_fs_trunc_xino(au_br_sb(br)))
b752ccd1 37173+ xino_try_trunc(sb, br);
1facf9fc 37174+ }
1facf9fc 37175+}
37176+
37177+/* get an unused inode number from bitmap */
37178+ino_t au_xino_new_ino(struct super_block *sb)
37179+{
37180+ ino_t ino;
37181+ unsigned long *p, pindex, ul, pend;
37182+ struct au_sbinfo *sbinfo;
37183+ struct file *file;
37184+ int free_bit, err;
37185+
37186+ if (!au_opt_test(au_mntflags(sb), XINO))
37187+ return iunique(sb, AUFS_FIRST_INO);
37188+
37189+ sbinfo = au_sbi(sb);
37190+ mutex_lock(&sbinfo->si_xib_mtx);
37191+ p = sbinfo->si_xib_buf;
37192+ free_bit = sbinfo->si_xib_next_bit;
37193+ if (free_bit < page_bits && !test_bit(free_bit, p))
37194+ goto out; /* success */
37195+ free_bit = find_first_zero_bit(p, page_bits);
37196+ if (free_bit < page_bits)
37197+ goto out; /* success */
37198+
37199+ pindex = sbinfo->si_xib_last_pindex;
37200+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
37201+ err = xib_pindex(sb, ul);
37202+ if (unlikely(err))
37203+ goto out_err;
37204+ free_bit = find_first_zero_bit(p, page_bits);
37205+ if (free_bit < page_bits)
37206+ goto out; /* success */
37207+ }
37208+
37209+ file = sbinfo->si_xib;
c06a8ce3 37210+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
1facf9fc 37211+ for (ul = pindex + 1; ul <= pend; ul++) {
37212+ err = xib_pindex(sb, ul);
37213+ if (unlikely(err))
37214+ goto out_err;
37215+ free_bit = find_first_zero_bit(p, page_bits);
37216+ if (free_bit < page_bits)
37217+ goto out; /* success */
37218+ }
37219+ BUG();
37220+
4f0767ce 37221+out:
1facf9fc 37222+ set_bit(free_bit, p);
7f207e10 37223+ sbinfo->si_xib_next_bit = free_bit + 1;
1facf9fc 37224+ pindex = sbinfo->si_xib_last_pindex;
37225+ mutex_unlock(&sbinfo->si_xib_mtx);
37226+ ino = xib_calc_ino(pindex, free_bit);
37227+ AuDbg("i%lu\n", (unsigned long)ino);
37228+ return ino;
4f0767ce 37229+out_err:
1facf9fc 37230+ mutex_unlock(&sbinfo->si_xib_mtx);
37231+ AuDbg("i0\n");
37232+ return 0;
37233+}
37234+
37235+/*
37236+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37237+ * at the position of @h_ino.
37238+ * if @ino does not exist and @do_new is true, get new one.
37239+ */
37240+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37241+ ino_t *ino)
37242+{
37243+ int err;
37244+ ssize_t sz;
37245+ loff_t pos;
37246+ struct file *file;
37247+ struct au_sbinfo *sbinfo;
37248+
37249+ *ino = 0;
37250+ if (!au_opt_test(au_mntflags(sb), XINO))
37251+ return 0; /* no xino */
37252+
37253+ err = 0;
37254+ sbinfo = au_sbi(sb);
37255+ pos = h_ino;
37256+ if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
37257+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
37258+ return -EFBIG;
37259+ }
37260+ pos *= sizeof(*ino);
37261+
37262+ file = au_sbr(sb, bindex)->br_xino.xi_file;
c06a8ce3 37263+ if (vfsub_f_size_read(file) < pos + sizeof(*ino))
1facf9fc 37264+ return 0; /* no ino */
37265+
37266+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
37267+ if (sz == sizeof(*ino))
37268+ return 0; /* success */
37269+
37270+ err = sz;
37271+ if (unlikely(sz >= 0)) {
37272+ err = -EIO;
37273+ AuIOErr("xino read error (%zd)\n", sz);
37274+ }
37275+
37276+ return err;
37277+}
37278+
37279+/* ---------------------------------------------------------------------- */
37280+
37281+/* create and set a new xino file */
37282+
37283+struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
37284+{
37285+ struct file *file;
37286+ struct dentry *h_parent, *d;
b912730e 37287+ struct inode *h_dir, *inode;
1facf9fc 37288+ int err;
37289+
37290+ /*
37291+ * at mount-time, and the xino file is the default path,
4a4d8108 37292+ * hnotify is disabled so we have no notify events to ignore.
1facf9fc 37293+ * when a user specified the xino, we cannot get au_hdir to be ignored.
37294+ */
7f207e10 37295+ file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
2cbb1c4b 37296+ /* | __FMODE_NONOTIFY */,
1facf9fc 37297+ S_IRUGO | S_IWUGO);
37298+ if (IS_ERR(file)) {
37299+ if (!silent)
4a4d8108 37300+ pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
1facf9fc 37301+ return file;
37302+ }
37303+
37304+ /* keep file count */
b912730e
AM
37305+ err = 0;
37306+ inode = file_inode(file);
2000de60 37307+ h_parent = dget_parent(file->f_path.dentry);
5527c038 37308+ h_dir = d_inode(h_parent);
febd17d6 37309+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
1facf9fc 37310+ /* mnt_want_write() is unnecessary here */
523b37e3 37311+ /* no delegation since it is just created */
b912730e
AM
37312+ if (inode->i_nlink)
37313+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37314+ /*force*/0);
febd17d6 37315+ inode_unlock(h_dir);
1facf9fc 37316+ dput(h_parent);
37317+ if (unlikely(err)) {
37318+ if (!silent)
4a4d8108 37319+ pr_err("unlink %s(%d)\n", fname, err);
1facf9fc 37320+ goto out;
37321+ }
37322+
37323+ err = -EINVAL;
2000de60 37324+ d = file->f_path.dentry;
1facf9fc 37325+ if (unlikely(sb == d->d_sb)) {
37326+ if (!silent)
4a4d8108 37327+ pr_err("%s must be outside\n", fname);
1facf9fc 37328+ goto out;
37329+ }
37330+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37331+ if (!silent)
4a4d8108
AM
37332+ pr_err("xino doesn't support %s(%s)\n",
37333+ fname, au_sbtype(d->d_sb));
1facf9fc 37334+ goto out;
37335+ }
37336+ return file; /* success */
37337+
4f0767ce 37338+out:
1facf9fc 37339+ fput(file);
37340+ file = ERR_PTR(err);
37341+ return file;
37342+}
37343+
37344+/*
37345+ * find another branch who is on the same filesystem of the specified
5afbbe0d 37346+ * branch{@btgt}. search until @bbot.
1facf9fc 37347+ */
37348+static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
5afbbe0d 37349+ aufs_bindex_t bbot)
1facf9fc 37350+{
37351+ aufs_bindex_t bindex;
37352+ struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
37353+
37354+ for (bindex = 0; bindex < btgt; bindex++)
37355+ if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
37356+ return bindex;
5afbbe0d 37357+ for (bindex++; bindex <= bbot; bindex++)
1facf9fc 37358+ if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
37359+ return bindex;
37360+ return -1;
37361+}
37362+
37363+/* ---------------------------------------------------------------------- */
37364+
37365+/*
37366+ * initialize the xinofile for the specified branch @br
37367+ * at the place/path where @base_file indicates.
37368+ * test whether another branch is on the same filesystem or not,
37369+ * if @do_test is true.
37370+ */
37371+int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
37372+ struct file *base_file, int do_test)
37373+{
37374+ int err;
37375+ ino_t ino;
5afbbe0d 37376+ aufs_bindex_t bbot, bindex;
1facf9fc 37377+ struct au_branch *shared_br, *b;
37378+ struct file *file;
37379+ struct super_block *tgt_sb;
37380+
37381+ shared_br = NULL;
5afbbe0d 37382+ bbot = au_sbbot(sb);
1facf9fc 37383+ if (do_test) {
86dc4139 37384+ tgt_sb = au_br_sb(br);
5afbbe0d 37385+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 37386+ b = au_sbr(sb, bindex);
86dc4139 37387+ if (tgt_sb == au_br_sb(b)) {
1facf9fc 37388+ shared_br = b;
37389+ break;
37390+ }
37391+ }
37392+ }
37393+
37394+ if (!shared_br || !shared_br->br_xino.xi_file) {
37395+ struct au_xino_lock_dir ldir;
37396+
37397+ au_xino_lock_dir(sb, base_file, &ldir);
37398+ /* mnt_want_write() is unnecessary here */
37399+ file = au_xino_create2(base_file, NULL);
37400+ au_xino_unlock_dir(&ldir);
37401+ err = PTR_ERR(file);
37402+ if (IS_ERR(file))
37403+ goto out;
37404+ br->br_xino.xi_file = file;
37405+ } else {
37406+ br->br_xino.xi_file = shared_br->br_xino.xi_file;
37407+ get_file(br->br_xino.xi_file);
37408+ }
37409+
37410+ ino = AUFS_ROOT_INO;
37411+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
37412+ h_ino, ino);
b752ccd1
AM
37413+ if (unlikely(err)) {
37414+ fput(br->br_xino.xi_file);
37415+ br->br_xino.xi_file = NULL;
37416+ }
1facf9fc 37417+
4f0767ce 37418+out:
1facf9fc 37419+ return err;
37420+}
37421+
37422+/* ---------------------------------------------------------------------- */
37423+
37424+/* trucate a xino bitmap file */
37425+
37426+/* todo: slow */
37427+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37428+{
37429+ int err, bit;
37430+ ssize_t sz;
37431+ unsigned long pindex;
37432+ loff_t pos, pend;
37433+ struct au_sbinfo *sbinfo;
5527c038 37434+ vfs_readf_t func;
1facf9fc 37435+ ino_t *ino;
37436+ unsigned long *p;
37437+
37438+ err = 0;
37439+ sbinfo = au_sbi(sb);
dece6358 37440+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 37441+ p = sbinfo->si_xib_buf;
37442+ func = sbinfo->si_xread;
c06a8ce3 37443+ pend = vfsub_f_size_read(file);
1facf9fc 37444+ pos = 0;
37445+ while (pos < pend) {
37446+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
37447+ err = sz;
37448+ if (unlikely(sz <= 0))
37449+ goto out;
37450+
37451+ err = 0;
37452+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37453+ if (unlikely(*ino < AUFS_FIRST_INO))
37454+ continue;
37455+
37456+ xib_calc_bit(*ino, &pindex, &bit);
37457+ AuDebugOn(page_bits <= bit);
37458+ err = xib_pindex(sb, pindex);
37459+ if (!err)
37460+ set_bit(bit, p);
37461+ else
37462+ goto out;
37463+ }
37464+ }
37465+
4f0767ce 37466+out:
1facf9fc 37467+ return err;
37468+}
37469+
37470+static int xib_restore(struct super_block *sb)
37471+{
37472+ int err;
5afbbe0d 37473+ aufs_bindex_t bindex, bbot;
1facf9fc 37474+ void *page;
37475+
37476+ err = -ENOMEM;
37477+ page = (void *)__get_free_page(GFP_NOFS);
37478+ if (unlikely(!page))
37479+ goto out;
37480+
37481+ err = 0;
5afbbe0d
AM
37482+ bbot = au_sbbot(sb);
37483+ for (bindex = 0; !err && bindex <= bbot; bindex++)
1facf9fc 37484+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
37485+ err = do_xib_restore
37486+ (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
37487+ else
37488+ AuDbg("b%d\n", bindex);
1c60b727 37489+ free_page((unsigned long)page);
1facf9fc 37490+
4f0767ce 37491+out:
1facf9fc 37492+ return err;
37493+}
37494+
37495+int au_xib_trunc(struct super_block *sb)
37496+{
37497+ int err;
37498+ ssize_t sz;
37499+ loff_t pos;
37500+ struct au_xino_lock_dir ldir;
37501+ struct au_sbinfo *sbinfo;
37502+ unsigned long *p;
37503+ struct file *file;
37504+
dece6358
AM
37505+ SiMustWriteLock(sb);
37506+
1facf9fc 37507+ err = 0;
37508+ sbinfo = au_sbi(sb);
37509+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
37510+ goto out;
37511+
37512+ file = sbinfo->si_xib;
c06a8ce3 37513+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 37514+ goto out;
37515+
37516+ au_xino_lock_dir(sb, file, &ldir);
37517+ /* mnt_want_write() is unnecessary here */
37518+ file = au_xino_create2(sbinfo->si_xib, NULL);
37519+ au_xino_unlock_dir(&ldir);
37520+ err = PTR_ERR(file);
37521+ if (IS_ERR(file))
37522+ goto out;
37523+ fput(sbinfo->si_xib);
37524+ sbinfo->si_xib = file;
37525+
37526+ p = sbinfo->si_xib_buf;
37527+ memset(p, 0, PAGE_SIZE);
37528+ pos = 0;
37529+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
37530+ if (unlikely(sz != PAGE_SIZE)) {
37531+ err = sz;
37532+ AuIOErr("err %d\n", err);
37533+ if (sz >= 0)
37534+ err = -EIO;
37535+ goto out;
37536+ }
37537+
37538+ mutex_lock(&sbinfo->si_xib_mtx);
37539+ /* mnt_want_write() is unnecessary here */
37540+ err = xib_restore(sb);
37541+ mutex_unlock(&sbinfo->si_xib_mtx);
37542+
37543+out:
37544+ return err;
37545+}
37546+
37547+/* ---------------------------------------------------------------------- */
37548+
37549+/*
37550+ * xino mount option handlers
37551+ */
1facf9fc 37552+
37553+/* xino bitmap */
37554+static void xino_clear_xib(struct super_block *sb)
37555+{
37556+ struct au_sbinfo *sbinfo;
37557+
dece6358
AM
37558+ SiMustWriteLock(sb);
37559+
1facf9fc 37560+ sbinfo = au_sbi(sb);
37561+ sbinfo->si_xread = NULL;
37562+ sbinfo->si_xwrite = NULL;
37563+ if (sbinfo->si_xib)
37564+ fput(sbinfo->si_xib);
37565+ sbinfo->si_xib = NULL;
f0c0a007 37566+ if (sbinfo->si_xib_buf)
1c60b727 37567+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 37568+ sbinfo->si_xib_buf = NULL;
37569+}
37570+
37571+static int au_xino_set_xib(struct super_block *sb, struct file *base)
37572+{
37573+ int err;
37574+ loff_t pos;
37575+ struct au_sbinfo *sbinfo;
37576+ struct file *file;
37577+
dece6358
AM
37578+ SiMustWriteLock(sb);
37579+
1facf9fc 37580+ sbinfo = au_sbi(sb);
37581+ file = au_xino_create2(base, sbinfo->si_xib);
37582+ err = PTR_ERR(file);
37583+ if (IS_ERR(file))
37584+ goto out;
37585+ if (sbinfo->si_xib)
37586+ fput(sbinfo->si_xib);
37587+ sbinfo->si_xib = file;
5527c038
JR
37588+ sbinfo->si_xread = vfs_readf(file);
37589+ sbinfo->si_xwrite = vfs_writef(file);
1facf9fc 37590+
37591+ err = -ENOMEM;
37592+ if (!sbinfo->si_xib_buf)
37593+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
37594+ if (unlikely(!sbinfo->si_xib_buf))
37595+ goto out_unset;
37596+
37597+ sbinfo->si_xib_last_pindex = 0;
37598+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 37599+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 37600+ pos = 0;
37601+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
37602+ PAGE_SIZE, &pos);
37603+ if (unlikely(err != PAGE_SIZE))
37604+ goto out_free;
37605+ }
37606+ err = 0;
37607+ goto out; /* success */
37608+
4f0767ce 37609+out_free:
f0c0a007 37610+ if (sbinfo->si_xib_buf)
1c60b727 37611+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
37612+ sbinfo->si_xib_buf = NULL;
37613+ if (err >= 0)
37614+ err = -EIO;
4f0767ce 37615+out_unset:
b752ccd1
AM
37616+ fput(sbinfo->si_xib);
37617+ sbinfo->si_xib = NULL;
37618+ sbinfo->si_xread = NULL;
37619+ sbinfo->si_xwrite = NULL;
4f0767ce 37620+out:
b752ccd1 37621+ return err;
1facf9fc 37622+}
37623+
b752ccd1
AM
37624+/* xino for each branch */
37625+static void xino_clear_br(struct super_block *sb)
37626+{
5afbbe0d 37627+ aufs_bindex_t bindex, bbot;
b752ccd1 37628+ struct au_branch *br;
1facf9fc 37629+
5afbbe0d
AM
37630+ bbot = au_sbbot(sb);
37631+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
37632+ br = au_sbr(sb, bindex);
37633+ if (!br || !br->br_xino.xi_file)
37634+ continue;
37635+
37636+ fput(br->br_xino.xi_file);
37637+ br->br_xino.xi_file = NULL;
37638+ }
37639+}
37640+
37641+static int au_xino_set_br(struct super_block *sb, struct file *base)
1facf9fc 37642+{
37643+ int err;
b752ccd1 37644+ ino_t ino;
5afbbe0d 37645+ aufs_bindex_t bindex, bbot, bshared;
b752ccd1
AM
37646+ struct {
37647+ struct file *old, *new;
37648+ } *fpair, *p;
37649+ struct au_branch *br;
37650+ struct inode *inode;
5527c038 37651+ vfs_writef_t writef;
1facf9fc 37652+
b752ccd1
AM
37653+ SiMustWriteLock(sb);
37654+
37655+ err = -ENOMEM;
5afbbe0d
AM
37656+ bbot = au_sbbot(sb);
37657+ fpair = kcalloc(bbot + 1, sizeof(*fpair), GFP_NOFS);
b752ccd1 37658+ if (unlikely(!fpair))
1facf9fc 37659+ goto out;
37660+
5527c038 37661+ inode = d_inode(sb->s_root);
b752ccd1
AM
37662+ ino = AUFS_ROOT_INO;
37663+ writef = au_sbi(sb)->si_xwrite;
5afbbe0d 37664+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
b752ccd1
AM
37665+ bshared = is_sb_shared(sb, bindex, bindex - 1);
37666+ if (bshared >= 0) {
37667+ /* shared xino */
37668+ *p = fpair[bshared];
37669+ get_file(p->new);
37670+ }
37671+
37672+ if (!p->new) {
37673+ /* new xino */
5afbbe0d 37674+ br = au_sbr(sb, bindex);
b752ccd1
AM
37675+ p->old = br->br_xino.xi_file;
37676+ p->new = au_xino_create2(base, br->br_xino.xi_file);
37677+ err = PTR_ERR(p->new);
37678+ if (IS_ERR(p->new)) {
37679+ p->new = NULL;
37680+ goto out_pair;
37681+ }
37682+ }
37683+
37684+ err = au_xino_do_write(writef, p->new,
37685+ au_h_iptr(inode, bindex)->i_ino, ino);
37686+ if (unlikely(err))
37687+ goto out_pair;
37688+ }
37689+
5afbbe0d 37690+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
b752ccd1
AM
37691+ br = au_sbr(sb, bindex);
37692+ if (br->br_xino.xi_file)
37693+ fput(br->br_xino.xi_file);
37694+ get_file(p->new);
37695+ br->br_xino.xi_file = p->new;
37696+ }
1facf9fc 37697+
4f0767ce 37698+out_pair:
5afbbe0d 37699+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++)
b752ccd1
AM
37700+ if (p->new)
37701+ fput(p->new);
37702+ else
37703+ break;
1c60b727 37704+ kfree(fpair);
4f0767ce 37705+out:
1facf9fc 37706+ return err;
37707+}
b752ccd1
AM
37708+
37709+void au_xino_clr(struct super_block *sb)
37710+{
37711+ struct au_sbinfo *sbinfo;
37712+
37713+ au_xigen_clr(sb);
37714+ xino_clear_xib(sb);
37715+ xino_clear_br(sb);
37716+ sbinfo = au_sbi(sb);
37717+ /* lvalue, do not call au_mntflags() */
37718+ au_opt_clr(sbinfo->si_mntflags, XINO);
37719+}
37720+
37721+int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
37722+{
37723+ int err, skip;
37724+ struct dentry *parent, *cur_parent;
37725+ struct qstr *dname, *cur_name;
37726+ struct file *cur_xino;
37727+ struct inode *dir;
37728+ struct au_sbinfo *sbinfo;
37729+
37730+ SiMustWriteLock(sb);
37731+
37732+ err = 0;
37733+ sbinfo = au_sbi(sb);
2000de60 37734+ parent = dget_parent(xino->file->f_path.dentry);
b752ccd1
AM
37735+ if (remount) {
37736+ skip = 0;
2000de60 37737+ dname = &xino->file->f_path.dentry->d_name;
b752ccd1
AM
37738+ cur_xino = sbinfo->si_xib;
37739+ if (cur_xino) {
2000de60
JR
37740+ cur_parent = dget_parent(cur_xino->f_path.dentry);
37741+ cur_name = &cur_xino->f_path.dentry->d_name;
b752ccd1 37742+ skip = (cur_parent == parent
38d290e6 37743+ && au_qstreq(dname, cur_name));
b752ccd1
AM
37744+ dput(cur_parent);
37745+ }
37746+ if (skip)
37747+ goto out;
37748+ }
37749+
37750+ au_opt_set(sbinfo->si_mntflags, XINO);
5527c038 37751+ dir = d_inode(parent);
febd17d6 37752+ inode_lock_nested(dir, AuLsc_I_PARENT);
b752ccd1
AM
37753+ /* mnt_want_write() is unnecessary here */
37754+ err = au_xino_set_xib(sb, xino->file);
37755+ if (!err)
37756+ err = au_xigen_set(sb, xino->file);
37757+ if (!err)
37758+ err = au_xino_set_br(sb, xino->file);
febd17d6 37759+ inode_unlock(dir);
b752ccd1
AM
37760+ if (!err)
37761+ goto out; /* success */
37762+
37763+ /* reset all */
37764+ AuIOErr("failed creating xino(%d).\n", err);
c1595e42
JR
37765+ au_xigen_clr(sb);
37766+ xino_clear_xib(sb);
b752ccd1 37767+
4f0767ce 37768+out:
b752ccd1
AM
37769+ dput(parent);
37770+ return err;
37771+}
37772+
37773+/* ---------------------------------------------------------------------- */
37774+
37775+/*
37776+ * create a xinofile at the default place/path.
37777+ */
37778+struct file *au_xino_def(struct super_block *sb)
37779+{
37780+ struct file *file;
37781+ char *page, *p;
37782+ struct au_branch *br;
37783+ struct super_block *h_sb;
37784+ struct path path;
5afbbe0d 37785+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
37786+
37787+ br = NULL;
5afbbe0d 37788+ bbot = au_sbbot(sb);
b752ccd1 37789+ bwr = -1;
5afbbe0d 37790+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
37791+ br = au_sbr(sb, bindex);
37792+ if (au_br_writable(br->br_perm)
86dc4139 37793+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
37794+ bwr = bindex;
37795+ break;
37796+ }
37797+ }
37798+
7f207e10
AM
37799+ if (bwr >= 0) {
37800+ file = ERR_PTR(-ENOMEM);
537831f9 37801+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
37802+ if (unlikely(!page))
37803+ goto out;
86dc4139 37804+ path.mnt = au_br_mnt(br);
7f207e10
AM
37805+ path.dentry = au_h_dptr(sb->s_root, bwr);
37806+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
37807+ file = (void *)p;
37808+ if (!IS_ERR(p)) {
37809+ strcat(p, "/" AUFS_XINO_FNAME);
37810+ AuDbg("%s\n", p);
37811+ file = au_xino_create(sb, p, /*silent*/0);
37812+ if (!IS_ERR(file))
37813+ au_xino_brid_set(sb, br->br_id);
37814+ }
1c60b727 37815+ free_page((unsigned long)page);
7f207e10
AM
37816+ } else {
37817+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
37818+ if (IS_ERR(file))
37819+ goto out;
2000de60 37820+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
37821+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
37822+ pr_err("xino doesn't support %s(%s)\n",
37823+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
37824+ fput(file);
37825+ file = ERR_PTR(-EINVAL);
37826+ }
37827+ if (!IS_ERR(file))
37828+ au_xino_brid_set(sb, -1);
37829+ }
0c5527e5 37830+
7f207e10
AM
37831+out:
37832+ return file;
37833+}
37834+
37835+/* ---------------------------------------------------------------------- */
37836+
37837+int au_xino_path(struct seq_file *seq, struct file *file)
37838+{
37839+ int err;
37840+
37841+ err = au_seq_path(seq, &file->f_path);
79b8bda9 37842+ if (unlikely(err))
7f207e10
AM
37843+ goto out;
37844+
7f207e10
AM
37845+#define Deleted "\\040(deleted)"
37846+ seq->count -= sizeof(Deleted) - 1;
37847+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
37848+ sizeof(Deleted) - 1));
37849+#undef Deleted
37850+
37851+out:
37852+ return err;
37853+}
521ced18
JR
37854+
37855+/* ---------------------------------------------------------------------- */
37856+
37857+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
37858+ ino_t h_ino, int idx)
37859+{
37860+ struct au_xino_file *xino;
37861+
37862+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
37863+ xino = &au_sbr(sb, bindex)->br_xino;
37864+ AuDebugOn(idx < 0 || xino->xi_nondir.total <= idx);
37865+
37866+ spin_lock(&xino->xi_nondir.spin);
37867+ AuDebugOn(xino->xi_nondir.array[idx] != h_ino);
37868+ xino->xi_nondir.array[idx] = 0;
37869+ spin_unlock(&xino->xi_nondir.spin);
37870+ wake_up_all(&xino->xi_nondir.wqh);
37871+}
37872+
37873+static int au_xinondir_find(struct au_xino_file *xino, ino_t h_ino)
37874+{
37875+ int found, total, i;
37876+
37877+ found = -1;
37878+ total = xino->xi_nondir.total;
37879+ for (i = 0; i < total; i++) {
37880+ if (xino->xi_nondir.array[i] != h_ino)
37881+ continue;
37882+ found = i;
37883+ break;
37884+ }
37885+
37886+ return found;
37887+}
37888+
37889+static int au_xinondir_expand(struct au_xino_file *xino)
37890+{
37891+ int err, sz;
37892+ ino_t *p;
37893+
37894+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
37895+
37896+ err = -ENOMEM;
37897+ sz = xino->xi_nondir.total * sizeof(ino_t);
37898+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
37899+ goto out;
37900+ p = au_kzrealloc(xino->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
37901+ /*may_shrink*/0);
37902+ if (p) {
37903+ xino->xi_nondir.array = p;
37904+ xino->xi_nondir.total <<= 1;
37905+ AuDbg("xi_nondir.total %d\n", xino->xi_nondir.total);
37906+ err = 0;
37907+ }
37908+
37909+out:
37910+ return err;
37911+}
37912+
37913+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37914+ int *idx)
37915+{
37916+ int err, found, empty;
37917+ struct au_xino_file *xino;
37918+
37919+ err = 0;
37920+ *idx = -1;
37921+ if (!au_opt_test(au_mntflags(sb), XINO))
37922+ goto out; /* no xino */
37923+
37924+ xino = &au_sbr(sb, bindex)->br_xino;
37925+
37926+again:
37927+ spin_lock(&xino->xi_nondir.spin);
37928+ found = au_xinondir_find(xino, h_ino);
37929+ if (found == -1) {
37930+ empty = au_xinondir_find(xino, /*h_ino*/0);
37931+ if (empty == -1) {
37932+ empty = xino->xi_nondir.total;
37933+ err = au_xinondir_expand(xino);
37934+ if (unlikely(err))
37935+ goto out_unlock;
37936+ }
37937+ xino->xi_nondir.array[empty] = h_ino;
37938+ *idx = empty;
37939+ } else {
37940+ spin_unlock(&xino->xi_nondir.spin);
37941+ wait_event(xino->xi_nondir.wqh,
37942+ xino->xi_nondir.array[found] != h_ino);
37943+ goto again;
37944+ }
37945+
37946+out_unlock:
37947+ spin_unlock(&xino->xi_nondir.spin);
37948+out:
37949+ return err;
37950+}
537831f9
AM
37951diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
37952--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
be118d29 37953+++ linux/include/uapi/linux/aufs_type.h 2018-04-06 07:48:44.207938097 +0200
8b6a4947 37954@@ -0,0 +1,447 @@
7f207e10 37955+/*
a2654f78 37956+ * Copyright (C) 2005-2017 Junjiro R. Okajima
7f207e10
AM
37957+ *
37958+ * This program, aufs is free software; you can redistribute it and/or modify
37959+ * it under the terms of the GNU General Public License as published by
37960+ * the Free Software Foundation; either version 2 of the License, or
37961+ * (at your option) any later version.
37962+ *
37963+ * This program is distributed in the hope that it will be useful,
37964+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
37965+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37966+ * GNU General Public License for more details.
37967+ *
37968+ * You should have received a copy of the GNU General Public License
523b37e3 37969+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
37970+ */
37971+
37972+#ifndef __AUFS_TYPE_H__
37973+#define __AUFS_TYPE_H__
37974+
f6c5ef8b
AM
37975+#define AUFS_NAME "aufs"
37976+
9dbd164d 37977+#ifdef __KERNEL__
f6c5ef8b
AM
37978+/*
37979+ * define it before including all other headers.
37980+ * sched.h may use pr_* macros before defining "current", so define the
37981+ * no-current version first, and re-define later.
37982+ */
37983+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
37984+#include <linux/sched.h>
37985+#undef pr_fmt
a2a7ad62
AM
37986+#define pr_fmt(fmt) \
37987+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
37988+ (int)sizeof(current->comm), current->comm, current->pid
9dbd164d
AM
37989+#else
37990+#include <stdint.h>
37991+#include <sys/types.h>
f6c5ef8b 37992+#endif /* __KERNEL__ */
7f207e10 37993+
f6c5ef8b
AM
37994+#include <linux/limits.h>
37995+
be118d29 37996+#define AUFS_VERSION "4.x-rcN-20180312"
7f207e10
AM
37997+
37998+/* todo? move this to linux-2.6.19/include/magic.h */
37999+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38000+
38001+/* ---------------------------------------------------------------------- */
38002+
38003+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 38004+typedef int8_t aufs_bindex_t;
7f207e10
AM
38005+#define AUFS_BRANCH_MAX 127
38006+#else
9dbd164d 38007+typedef int16_t aufs_bindex_t;
7f207e10
AM
38008+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38009+#define AUFS_BRANCH_MAX 511
38010+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38011+#define AUFS_BRANCH_MAX 1023
38012+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38013+#define AUFS_BRANCH_MAX 32767
38014+#endif
38015+#endif
38016+
38017+#ifdef __KERNEL__
38018+#ifndef AUFS_BRANCH_MAX
38019+#error unknown CONFIG_AUFS_BRANCH_MAX value
38020+#endif
38021+#endif /* __KERNEL__ */
38022+
38023+/* ---------------------------------------------------------------------- */
38024+
7f207e10
AM
38025+#define AUFS_FSTYPE AUFS_NAME
38026+
38027+#define AUFS_ROOT_INO 2
38028+#define AUFS_FIRST_INO 11
38029+
38030+#define AUFS_WH_PFX ".wh."
38031+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38032+#define AUFS_WH_TMP_LEN 4
86dc4139 38033+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
38034+#define AUFS_MAX_NAMELEN (NAME_MAX \
38035+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38036+ - 1 /* dot */\
38037+ - AUFS_WH_TMP_LEN) /* hex */
38038+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38039+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
38040+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38041+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
38042+#define AUFS_DIRWH_DEF 3
38043+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 38044+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
38045+#define AUFS_RDBLK_DEF 512 /* bytes */
38046+#define AUFS_RDHASH_DEF 32
38047+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
38048+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38049+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 38050+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 38051+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
38052+
38053+/* pseudo-link maintenace under /proc */
38054+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38055+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38056+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38057+
8b6a4947
AM
38058+/* dirren, renamed dir */
38059+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38060+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38061+/* whiteouted doubly */
38062+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38063+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38064+
7f207e10
AM
38065+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38066+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38067+
38068+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38069+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38070+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38071+
38072+/* doubly whiteouted */
38073+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38074+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38075+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38076+
1e00d052 38077+/* branch permissions and attributes */
7f207e10
AM
38078+#define AUFS_BRPERM_RW "rw"
38079+#define AUFS_BRPERM_RO "ro"
38080+#define AUFS_BRPERM_RR "rr"
076b876e
AM
38081+#define AUFS_BRATTR_COO_REG "coo_reg"
38082+#define AUFS_BRATTR_COO_ALL "coo_all"
38083+#define AUFS_BRATTR_FHSM "fhsm"
38084+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
38085+#define AUFS_BRATTR_ICEX "icex"
38086+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38087+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38088+#define AUFS_BRATTR_ICEX_TR "icextr"
38089+#define AUFS_BRATTR_ICEX_USR "icexusr"
38090+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
38091+#define AUFS_BRRATTR_WH "wh"
38092+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
38093+#define AUFS_BRWATTR_MOO "moo"
38094+
38095+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38096+#define AuBrPerm_RO (1 << 1) /* readonly */
38097+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38098+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38099+
38100+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38101+#define AuBrAttr_COO_ALL (1 << 4)
38102+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38103+
38104+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38105+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
38106+ branch. meaningless since
38107+ linux-3.18-rc1 */
38108+
38109+/* ignore error in copying XATTR */
38110+#define AuBrAttr_ICEX_SEC (1 << 7)
38111+#define AuBrAttr_ICEX_SYS (1 << 8)
38112+#define AuBrAttr_ICEX_TR (1 << 9)
38113+#define AuBrAttr_ICEX_USR (1 << 10)
38114+#define AuBrAttr_ICEX_OTH (1 << 11)
38115+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38116+ | AuBrAttr_ICEX_SYS \
38117+ | AuBrAttr_ICEX_TR \
38118+ | AuBrAttr_ICEX_USR \
38119+ | AuBrAttr_ICEX_OTH)
38120+
38121+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
38122+#define AuBrRAttr_Mask AuBrRAttr_WH
38123+
c1595e42
JR
38124+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
38125+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
38126+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38127+
38128+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38129+
c1595e42 38130+/* #warning test userspace */
076b876e
AM
38131+#ifdef __KERNEL__
38132+#ifndef CONFIG_AUFS_FHSM
38133+#undef AuBrAttr_FHSM
38134+#define AuBrAttr_FHSM 0
38135+#endif
c1595e42
JR
38136+#ifndef CONFIG_AUFS_XATTR
38137+#undef AuBrAttr_ICEX
38138+#define AuBrAttr_ICEX 0
38139+#undef AuBrAttr_ICEX_SEC
38140+#define AuBrAttr_ICEX_SEC 0
38141+#undef AuBrAttr_ICEX_SYS
38142+#define AuBrAttr_ICEX_SYS 0
38143+#undef AuBrAttr_ICEX_TR
38144+#define AuBrAttr_ICEX_TR 0
38145+#undef AuBrAttr_ICEX_USR
38146+#define AuBrAttr_ICEX_USR 0
38147+#undef AuBrAttr_ICEX_OTH
38148+#define AuBrAttr_ICEX_OTH 0
38149+#endif
076b876e
AM
38150+#endif
38151+
38152+/* the longest combination */
c1595e42
JR
38153+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38154+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
38155+ "+" AUFS_BRATTR_COO_REG \
38156+ "+" AUFS_BRATTR_FHSM \
38157+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
38158+ "+" AUFS_BRATTR_ICEX_SEC \
38159+ "+" AUFS_BRATTR_ICEX_SYS \
38160+ "+" AUFS_BRATTR_ICEX_USR \
38161+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
38162+ "+" AUFS_BRWATTR_NLWH)
38163+
38164+typedef struct {
38165+ char a[AuBrPermStrSz];
38166+} au_br_perm_str_t;
38167+
38168+static inline int au_br_writable(int brperm)
38169+{
38170+ return brperm & AuBrPerm_RW;
38171+}
38172+
38173+static inline int au_br_whable(int brperm)
38174+{
38175+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38176+}
38177+
38178+static inline int au_br_wh_linkable(int brperm)
38179+{
38180+ return !(brperm & AuBrWAttr_NoLinkWH);
38181+}
38182+
38183+static inline int au_br_cmoo(int brperm)
38184+{
38185+ return brperm & AuBrAttr_CMOO_Mask;
38186+}
38187+
38188+static inline int au_br_fhsm(int brperm)
38189+{
38190+ return brperm & AuBrAttr_FHSM;
38191+}
7f207e10
AM
38192+
38193+/* ---------------------------------------------------------------------- */
38194+
38195+/* ioctl */
38196+enum {
38197+ /* readdir in userspace */
38198+ AuCtl_RDU,
38199+ AuCtl_RDU_INO,
38200+
076b876e
AM
38201+ AuCtl_WBR_FD, /* pathconf wrapper */
38202+ AuCtl_IBUSY, /* busy inode */
38203+ AuCtl_MVDOWN, /* move-down */
38204+ AuCtl_BR, /* info about branches */
38205+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
38206+};
38207+
38208+/* borrowed from linux/include/linux/kernel.h */
38209+#ifndef ALIGN
38210+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
38211+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
38212+#endif
38213+
38214+/* borrowed from linux/include/linux/compiler-gcc3.h */
38215+#ifndef __aligned
38216+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
38217+#endif
38218+
38219+#ifdef __KERNEL__
38220+#ifndef __packed
7f207e10
AM
38221+#define __packed __attribute__((packed))
38222+#endif
53392da6 38223+#endif
7f207e10
AM
38224+
38225+struct au_rdu_cookie {
9dbd164d
AM
38226+ uint64_t h_pos;
38227+ int16_t bindex;
38228+ uint8_t flags;
38229+ uint8_t pad;
38230+ uint32_t generation;
7f207e10
AM
38231+} __aligned(8);
38232+
38233+struct au_rdu_ent {
9dbd164d
AM
38234+ uint64_t ino;
38235+ int16_t bindex;
38236+ uint8_t type;
38237+ uint8_t nlen;
38238+ uint8_t wh;
7f207e10
AM
38239+ char name[0];
38240+} __aligned(8);
38241+
38242+static inline int au_rdu_len(int nlen)
38243+{
38244+ /* include the terminating NULL */
38245+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 38246+ sizeof(uint64_t));
7f207e10
AM
38247+}
38248+
38249+union au_rdu_ent_ul {
38250+ struct au_rdu_ent __user *e;
9dbd164d 38251+ uint64_t ul;
7f207e10
AM
38252+};
38253+
38254+enum {
38255+ AufsCtlRduV_SZ,
38256+ AufsCtlRduV_End
38257+};
38258+
38259+struct aufs_rdu {
38260+ /* input */
38261+ union {
9dbd164d
AM
38262+ uint64_t sz; /* AuCtl_RDU */
38263+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
38264+ };
38265+ union au_rdu_ent_ul ent;
9dbd164d 38266+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
38267+
38268+ /* input/output */
9dbd164d 38269+ uint32_t blk;
7f207e10
AM
38270+
38271+ /* output */
38272+ union au_rdu_ent_ul tail;
38273+ /* number of entries which were added in a single call */
9dbd164d
AM
38274+ uint64_t rent;
38275+ uint8_t full;
38276+ uint8_t shwh;
7f207e10
AM
38277+
38278+ struct au_rdu_cookie cookie;
38279+} __aligned(8);
38280+
1e00d052
AM
38281+/* ---------------------------------------------------------------------- */
38282+
8b6a4947
AM
38283+/* dirren. the branch is identified by the filename who contains this */
38284+struct au_drinfo {
38285+ uint64_t ino;
38286+ union {
38287+ uint8_t oldnamelen;
38288+ uint64_t _padding;
38289+ };
38290+ uint8_t oldname[0];
38291+} __aligned(8);
38292+
38293+struct au_drinfo_fdata {
38294+ uint32_t magic;
38295+ struct au_drinfo drinfo;
38296+} __aligned(8);
38297+
38298+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
38299+/* future */
38300+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
38301+
38302+/* ---------------------------------------------------------------------- */
38303+
1e00d052 38304+struct aufs_wbr_fd {
9dbd164d
AM
38305+ uint32_t oflags;
38306+ int16_t brid;
1e00d052
AM
38307+} __aligned(8);
38308+
38309+/* ---------------------------------------------------------------------- */
38310+
027c5e7a 38311+struct aufs_ibusy {
9dbd164d
AM
38312+ uint64_t ino, h_ino;
38313+ int16_t bindex;
027c5e7a
AM
38314+} __aligned(8);
38315+
1e00d052
AM
38316+/* ---------------------------------------------------------------------- */
38317+
392086de
AM
38318+/* error code for move-down */
38319+/* the actual message strings are implemented in aufs-util.git */
38320+enum {
38321+ EAU_MVDOWN_OPAQUE = 1,
38322+ EAU_MVDOWN_WHITEOUT,
38323+ EAU_MVDOWN_UPPER,
38324+ EAU_MVDOWN_BOTTOM,
38325+ EAU_MVDOWN_NOUPPER,
38326+ EAU_MVDOWN_NOLOWERBR,
38327+ EAU_Last
38328+};
38329+
c2b27bf2 38330+/* flags for move-down */
392086de
AM
38331+#define AUFS_MVDOWN_DMSG 1
38332+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
38333+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
38334+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
38335+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
38336+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
38337+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
38338+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
38339+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
38340+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
38341+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
38342+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
38343+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 38344+
076b876e 38345+/* index for move-down */
392086de
AM
38346+enum {
38347+ AUFS_MVDOWN_UPPER,
38348+ AUFS_MVDOWN_LOWER,
38349+ AUFS_MVDOWN_NARRAY
38350+};
38351+
076b876e
AM
38352+/*
38353+ * additional info of move-down
38354+ * number of free blocks and inodes.
38355+ * subset of struct kstatfs, but smaller and always 64bit.
38356+ */
38357+struct aufs_stfs {
38358+ uint64_t f_blocks;
38359+ uint64_t f_bavail;
38360+ uint64_t f_files;
38361+ uint64_t f_ffree;
38362+};
38363+
38364+struct aufs_stbr {
38365+ int16_t brid; /* optional input */
38366+ int16_t bindex; /* output */
38367+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
38368+} __aligned(8);
38369+
c2b27bf2 38370+struct aufs_mvdown {
076b876e
AM
38371+ uint32_t flags; /* input/output */
38372+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
38373+ int8_t au_errno; /* output */
38374+} __aligned(8);
38375+
38376+/* ---------------------------------------------------------------------- */
38377+
38378+union aufs_brinfo {
38379+ /* PATH_MAX may differ between kernel-space and user-space */
38380+ char _spacer[4096];
392086de 38381+ struct {
076b876e
AM
38382+ int16_t id;
38383+ int perm;
38384+ char path[0];
38385+ };
c2b27bf2
AM
38386+} __aligned(8);
38387+
38388+/* ---------------------------------------------------------------------- */
38389+
7f207e10
AM
38390+#define AuCtlType 'A'
38391+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
38392+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
38393+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
38394+ struct aufs_wbr_fd)
027c5e7a 38395+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
38396+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
38397+ struct aufs_mvdown)
076b876e
AM
38398+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
38399+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
38400+
38401+#endif /* __AUFS_TYPE_H__ */
2121bcd9 38402SPDX-License-Identifier: GPL-2.0
ffa93bbd 38403aufs4.x-rcN loopback patch
5527c038
JR
38404
38405diff --git a/drivers/block/loop.c b/drivers/block/loop.c
be118d29 38406index 962b2d6..d17f695 100644
5527c038
JR
38407--- a/drivers/block/loop.c
38408+++ b/drivers/block/loop.c
2121bcd9 38409@@ -600,6 +600,15 @@ static inline void loop_update_dio(struct loop_device *lo)
8b6a4947 38410 lo->use_dio);
5527c038
JR
38411 }
38412
5527c038
JR
38413+static struct file *loop_real_file(struct file *file)
38414+{
38415+ struct file *f = NULL;
38416+
38417+ if (file->f_path.dentry->d_sb->s_op->real_loop)
38418+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
38419+ return f;
8b6a4947
AM
38420+}
38421+
c2c0f25c 38422 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
38423 struct block_device *bdev)
38424 {
2121bcd9 38425@@ -634,6 +643,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
38426 unsigned int arg)
38427 {
38428 struct file *file, *old_file;
38429+ struct file *f, *virt_file = NULL, *old_virt_file;
38430 struct inode *inode;
38431 int error;
38432
2121bcd9 38433@@ -650,9 +660,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
38434 file = fget(arg);
38435 if (!file)
38436 goto out;
38437+ f = loop_real_file(file);
38438+ if (f) {
38439+ virt_file = file;
38440+ file = f;
38441+ get_file(file);
38442+ }
38443
38444 inode = file->f_mapping->host;
38445 old_file = lo->lo_backing_file;
38446+ old_virt_file = lo->lo_backing_virt_file;
38447
38448 error = -EINVAL;
38449
2121bcd9 38450@@ -667,6 +684,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
38451 blk_mq_freeze_queue(lo->lo_queue);
38452 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
38453 lo->lo_backing_file = file;
38454+ lo->lo_backing_virt_file = virt_file;
38455 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
38456 mapping_set_gfp_mask(file->f_mapping,
38457 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
2121bcd9 38458@@ -674,12 +692,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947 38459 blk_mq_unfreeze_queue(lo->lo_queue);
5527c038
JR
38460
38461 fput(old_file);
38462+ if (old_virt_file)
38463+ fput(old_virt_file);
38464 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
c2c0f25c 38465 loop_reread_partitions(lo, bdev);
5527c038
JR
38466 return 0;
38467
38468 out_putf:
38469 fput(file);
38470+ if (virt_file)
38471+ fput(virt_file);
38472 out:
38473 return error;
38474 }
2121bcd9 38475@@ -873,7 +895,7 @@ static int loop_prepare_queue(struct loop_device *lo)
5527c038
JR
38476 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38477 struct block_device *bdev, unsigned int arg)
38478 {
38479- struct file *file, *f;
38480+ struct file *file, *f, *virt_file = NULL;
38481 struct inode *inode;
38482 struct address_space *mapping;
8b6a4947 38483 int lo_flags = 0;
2121bcd9 38484@@ -887,6 +909,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38485 file = fget(arg);
38486 if (!file)
38487 goto out;
38488+ f = loop_real_file(file);
38489+ if (f) {
38490+ virt_file = file;
38491+ file = f;
38492+ get_file(file);
38493+ }
38494
38495 error = -EBUSY;
38496 if (lo->lo_state != Lo_unbound)
2121bcd9 38497@@ -935,6 +963,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38498 lo->lo_device = bdev;
38499 lo->lo_flags = lo_flags;
38500 lo->lo_backing_file = file;
38501+ lo->lo_backing_virt_file = virt_file;
38502 lo->transfer = NULL;
38503 lo->ioctl = NULL;
38504 lo->lo_sizelimit = 0;
2121bcd9 38505@@ -968,6 +997,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38506
38507 out_putf:
38508 fput(file);
38509+ if (virt_file)
38510+ fput(virt_file);
38511 out:
38512 /* This is safe: open() is still holding a reference. */
38513 module_put(THIS_MODULE);
2121bcd9 38514@@ -1014,6 +1045,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
5527c038
JR
38515 static int loop_clr_fd(struct loop_device *lo)
38516 {
38517 struct file *filp = lo->lo_backing_file;
38518+ struct file *virt_filp = lo->lo_backing_virt_file;
38519 gfp_t gfp = lo->old_gfp_mask;
38520 struct block_device *bdev = lo->lo_device;
38521
2121bcd9 38522@@ -1045,6 +1077,7 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
38523 spin_lock_irq(&lo->lo_lock);
38524 lo->lo_state = Lo_rundown;
38525 lo->lo_backing_file = NULL;
38526+ lo->lo_backing_virt_file = NULL;
38527 spin_unlock_irq(&lo->lo_lock);
38528
38529 loop_release_xfer(lo);
2121bcd9 38530@@ -1092,6 +1125,8 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
38531 * bd_mutex which is usually taken before lo_ctl_mutex.
38532 */
38533 fput(filp);
38534+ if (virt_filp)
38535+ fput(virt_filp);
38536 return 0;
38537 }
38538
38539diff --git a/drivers/block/loop.h b/drivers/block/loop.h
2121bcd9 38540index 0f45416..101f193 100644
5527c038
JR
38541--- a/drivers/block/loop.h
38542+++ b/drivers/block/loop.h
38543@@ -46,7 +46,7 @@ struct loop_device {
38544 int (*ioctl)(struct loop_device *, int cmd,
38545 unsigned long arg);
38546
38547- struct file * lo_backing_file;
38548+ struct file * lo_backing_file, *lo_backing_virt_file;
38549 struct block_device *lo_device;
5527c038 38550 void *key_data;
8b6a4947 38551
5527c038 38552diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
2121bcd9 38553index 0d4ea929..af293c2 100644
5527c038
JR
38554--- a/fs/aufs/f_op.c
38555+++ b/fs/aufs/f_op.c
2121bcd9 38556@@ -358,7 +358,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
38557 if (IS_ERR(h_file))
38558 goto out;
38559
38560- if (au_test_loopback_kthread()) {
38561+ if (0 && au_test_loopback_kthread()) {
38562 au_warn_loopback(h_file->f_path.dentry->d_sb);
38563 if (file->f_mapping != h_file->f_mapping) {
38564 file->f_mapping = h_file->f_mapping;
38565diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
2121bcd9 38566index 3b217c2..0f5ab22 100644
5527c038
JR
38567--- a/fs/aufs/loop.c
38568+++ b/fs/aufs/loop.c
2121bcd9 38569@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 38570 symbol_put(loop_backing_file);
1c60b727 38571 kfree(au_warn_loopback_array);
5527c038
JR
38572 }
38573+
38574+/* ---------------------------------------------------------------------- */
38575+
38576+/* support the loopback block device insude aufs */
38577+
38578+struct file *aufs_real_loop(struct file *file)
38579+{
38580+ struct file *f;
38581+
38582+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
38583+ fi_read_lock(file);
38584+ f = au_hf_top(file);
38585+ fi_read_unlock(file);
38586+ AuDebugOn(!f);
38587+ return f;
38588+}
38589diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
2121bcd9 38590index 7d7bf34..ba7c188 100644
5527c038
JR
38591--- a/fs/aufs/loop.h
38592+++ b/fs/aufs/loop.h
2121bcd9 38593@@ -26,7 +26,11 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
38594
38595 int au_loopback_init(void);
38596 void au_loopback_fin(void);
38597+
38598+struct file *aufs_real_loop(struct file *file);
38599 #else
38600+AuStub(struct file *, loop_backing_file, return NULL)
38601+
38602 AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
38603 struct dentry *h_adding)
38604 AuStubInt0(au_test_loopback_kthread, void)
2121bcd9 38605@@ -34,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
38606
38607 AuStubInt0(au_loopback_init, void)
38608 AuStubVoid(au_loopback_fin, void)
38609+
38610+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
38611 #endif /* BLK_DEV_LOOP */
38612
38613 #endif /* __KERNEL__ */
38614diff --git a/fs/aufs/super.c b/fs/aufs/super.c
be118d29 38615index eef60a6..e03a658 100644
5527c038
JR
38616--- a/fs/aufs/super.c
38617+++ b/fs/aufs/super.c
be118d29 38618@@ -843,7 +843,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
38619 .statfs = aufs_statfs,
38620 .put_super = aufs_put_super,
38621 .sync_fs = aufs_sync_fs,
38622- .remount_fs = aufs_remount_fs
38623+ .remount_fs = aufs_remount_fs,
38624+#ifdef CONFIG_AUFS_BDEV_LOOP
38625+ .real_loop = aufs_real_loop
38626+#endif
38627 };
38628
38629 /* ---------------------------------------------------------------------- */
38630diff --git a/include/linux/fs.h b/include/linux/fs.h
be118d29 38631index 383ab06..e899b15 100644
5527c038
JR
38632--- a/include/linux/fs.h
38633+++ b/include/linux/fs.h
be118d29 38634@@ -1849,6 +1849,10 @@ struct super_operations {
5527c038
JR
38635 struct shrink_control *);
38636 long (*free_cached_objects)(struct super_block *,
38637 struct shrink_control *);
38638+#if defined(CONFIG_BLK_DEV_LOOP) || defined(CONFIG_BLK_DEV_LOOP_MODULE)
38639+ /* and aufs */
38640+ struct file *(*real_loop)(struct file *);
38641+#endif
38642 };
38643
38644 /*
This page took 7.256946 seconds and 4 git commands to generate.