]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs4.patch
- up to 4.17, apparmor needs update
[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
b00004a5 17index c9375fd..8af5671 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
b00004a5 29index 92be777..138f5e6 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
b00004a5 32@@ -2517,6 +2517,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
b00004a5 53index c9d0449..79902c1 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
b00004a5 82index 86d2de6..213ddcd 100644
c1595e42
JR
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
b00004a5 85@@ -1234,7 +1234,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
b00004a5 95index d737ff0..7550799 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
b00004a5 117index 13ceb98..68b3b45 100644
5afbbe0d
AM
118--- a/fs/inode.c
119+++ b/fs/inode.c
b00004a5 120@@ -1662,7 +1662,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
b00004a5 130index 5f75969..61129ff 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
b00004a5 147index c4eabbf..ddd6e67 100644
5527c038
JR
148--- a/fs/read_write.c
149+++ b/fs/read_write.c
b00004a5 150@@ -489,6 +489,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
b00004a5 180index 005d09c..f617ab0 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
b00004a5 208index b54e054..2860782 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
b00004a5 233index 760d8da..09a2542 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);
b00004a5 244@@ -1724,6 +1725,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);
b00004a5 252@@ -1794,6 +1796,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 *);
b00004a5 265@@ -2199,6 +2207,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;
b00004a5 273@@ -2485,6 +2494,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
b00004a5 337index 0233863..06e0d7a 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
b00004a5 361index 1b2ede6..dc3fd6d 100644
c1595e42
JR
362--- a/fs/proc/base.c
363+++ b/fs/proc/base.c
b00004a5 364@@ -2024,7 +2024,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
b00004a5 390index c486ad4..76b71f8 100644
fb47a38f
JR
391--- a/fs/proc/task_mmu.c
392+++ b/fs/proc/task_mmu.c
b00004a5 393@@ -305,7 +305,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;
b00004a5 405@@ -1726,7 +1729,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
b00004a5 431index 1ac1f06..49997d9 100644
fb47a38f
JR
432--- a/include/linux/mm.h
433+++ b/include/linux/mm.h
b00004a5 434@@ -1380,6 +1380,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
b00004a5 464index 2161234..78aa367 100644
fb47a38f
JR
465--- a/include/linux/mm_types.h
466+++ b/include/linux/mm_types.h
b00004a5 467@@ -251,6 +251,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
b00004a5 475@@ -325,6 +326,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
b00004a5 484index a5d21c4..e965e09 100644
fb47a38f
JR
485--- a/kernel/fork.c
486+++ b/kernel/fork.c
b00004a5 487@@ -473,7 +473,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
b00004a5 497index b4e54a9a..77892ae 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 \
b00004a5 502 compaction.o vmacache.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
b00004a5 510index 0604cb0..45d2369 100644
fb47a38f
JR
511--- a/mm/filemap.c
512+++ b/mm/filemap.c
b00004a5 513@@ -2700,7 +2700,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
b00004a5 523index 188f195..3c1ca1d 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);
b00004a5 544@@ -1779,8 +1779,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);
b00004a5 554@@ -2604,7 +2604,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);
b00004a5 563@@ -2623,7 +2623,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));
b00004a5 572@@ -2785,7 +2785,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);
b00004a5 581@@ -2860,10 +2860,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)
b00004a5 610@@ -3171,7 +3188,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
b00004a5 620index 1372373..6362dde 100644
fb47a38f
JR
621--- a/mm/nommu.c
622+++ b/mm/nommu.c
b00004a5 623@@ -629,7 +629,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 */
b00004a5 632@@ -767,7 +767,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 }
b00004a5 641@@ -1289,7 +1289,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;
b00004a5 650@@ -1364,10 +1364,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
b00004a5 665index 0000000..14efc4f
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+ *
b00004a5 677+ * Copyright (c) 2014-2018 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
b00004a5 759index 213ddcd..137176b 100644
c1595e42
JR
760--- a/fs/dcache.c
761+++ b/fs/dcache.c
b00004a5 762@@ -1342,6 +1342,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
b00004a5 779index 183059c..35adee4 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
b00004a5 791index 7550799..d403576 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
b00004a5 839index 68b3b45..af4551e 100644
5afbbe0d
AM
840--- a/fs/inode.c
841+++ b/fs/inode.c
b00004a5 842@@ -1671,6 +1671,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
b00004a5 851index 61129ff..5d3e0382 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
b00004a5 870@@ -1893,6 +1895,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
b00004a5 951index c5ee7cd..86bfe2d 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 {
5527c038 962diff --git a/fs/read_write.c b/fs/read_write.c
b00004a5 963index ddd6e67..aabf92d 100644
5527c038
JR
964--- a/fs/read_write.c
965+++ b/fs/read_write.c
b00004a5 966@@ -459,6 +459,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
8b6a4947
AM
967
968 return ret;
969 }
970+EXPORT_SYMBOL_GPL(vfs_read);
971
972 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
973 {
b00004a5 974@@ -499,6 +500,7 @@ vfs_readf_t vfs_readf(struct file *file)
5527c038
JR
975 return new_sync_read;
976 return ERR_PTR(-ENOSYS);
977 }
febd17d6 978+EXPORT_SYMBOL_GPL(vfs_readf);
5527c038
JR
979
980 vfs_writef_t vfs_writef(struct file *file)
981 {
b00004a5 982@@ -510,6 +512,7 @@ vfs_writef_t vfs_writef(struct file *file)
5527c038
JR
983 return new_sync_write;
984 return ERR_PTR(-ENOSYS);
985 }
febd17d6 986+EXPORT_SYMBOL_GPL(vfs_writef);
5527c038 987
8b6a4947
AM
988 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
989 {
b00004a5 990@@ -579,6 +582,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
8b6a4947
AM
991
992 return ret;
993 }
994+EXPORT_SYMBOL_GPL(vfs_write);
995
996 static inline loff_t file_pos_read(struct file *file)
5527c038 997 {
7f207e10 998diff --git a/fs/splice.c b/fs/splice.c
b00004a5 999index f617ab0..ec0ad02 100644
7f207e10
AM
1000--- a/fs/splice.c
1001+++ b/fs/splice.c
8b6a4947 1002@@ -850,6 +850,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
392086de
AM
1003
1004 return splice_write(pipe, out, ppos, len, flags);
7f207e10 1005 }
febd17d6 1006+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
1007
1008 /*
1009 * Attempt to initiate a splice from a file to a pipe.
8b6a4947 1010@@ -879,6 +880,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
7f207e10
AM
1011
1012 return splice_read(in, ppos, pipe, len, flags);
1013 }
febd17d6 1014+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
1015
1016 /**
1017 * splice_direct_to_actor - splices data directly between two non-pipes
a2654f78 1018diff --git a/fs/sync.c b/fs/sync.c
b00004a5 1019index 2860782..ffd7ea4 100644
a2654f78
AM
1020--- a/fs/sync.c
1021+++ b/fs/sync.c
2121bcd9 1022@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
a2654f78
AM
1023 sb->s_op->sync_fs(sb, wait);
1024 return __sync_blockdev(sb->s_bdev, wait);
1025 }
1026+EXPORT_SYMBOL_GPL(__sync_filesystem);
1027
1028 /*
1029 * Write out and wait upon all dirty data associated with this
c1595e42 1030diff --git a/fs/xattr.c b/fs/xattr.c
8b6a4947 1031index 61cd28b..35570cd 100644
c1595e42
JR
1032--- a/fs/xattr.c
1033+++ b/fs/xattr.c
8b6a4947 1034@@ -297,6 +297,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
c1595e42
JR
1035 *xattr_value = value;
1036 return error;
1037 }
febd17d6 1038+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 1039
febd17d6 1040 ssize_t
f2c43d5f 1041 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947 1042diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
b00004a5 1043index 06e0d7a..6af91bd 100644
8b6a4947
AM
1044--- a/kernel/locking/lockdep.c
1045+++ b/kernel/locking/lockdep.c
be118d29 1046@@ -151,6 +151,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
8b6a4947
AM
1047 }
1048 return lock_classes + hlock->class_idx - 1;
1049 }
1050+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
1051 #define hlock_class(hlock) lockdep_hlock_class(hlock)
1052
1053 #ifdef CONFIG_LOCK_STAT
8cdd5066 1054diff --git a/kernel/task_work.c b/kernel/task_work.c
2121bcd9 1055index 0fef395..83fb1ec 100644
8cdd5066
JR
1056--- a/kernel/task_work.c
1057+++ b/kernel/task_work.c
2121bcd9 1058@@ -116,3 +116,4 @@ void task_work_run(void)
8cdd5066
JR
1059 } while (work);
1060 }
1061 }
febd17d6 1062+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 1063diff --git a/security/commoncap.c b/security/commoncap.c
be118d29 1064index 48620c9..4981104 100644
7f207e10
AM
1065--- a/security/commoncap.c
1066+++ b/security/commoncap.c
be118d29 1067@@ -1330,12 +1330,14 @@ int cap_mmap_addr(unsigned long addr)
94337f0d 1068 }
7f207e10
AM
1069 return ret;
1070 }
febd17d6 1071+EXPORT_SYMBOL_GPL(cap_mmap_addr);
0c3ec466
AM
1072
1073 int cap_mmap_file(struct file *file, unsigned long reqprot,
1074 unsigned long prot, unsigned long flags)
1075 {
1076 return 0;
1077 }
febd17d6 1078+EXPORT_SYMBOL_GPL(cap_mmap_file);
c2c0f25c
AM
1079
1080 #ifdef CONFIG_SECURITY
1081
7f207e10 1082diff --git a/security/device_cgroup.c b/security/device_cgroup.c
2121bcd9 1083index c65b39b..e363d22 100644
7f207e10
AM
1084--- a/security/device_cgroup.c
1085+++ b/security/device_cgroup.c
2121bcd9 1086@@ -8,6 +8,7 @@
f6c5ef8b
AM
1087 #include <linux/device_cgroup.h>
1088 #include <linux/cgroup.h>
1089 #include <linux/ctype.h>
1090+#include <linux/export.h>
1091 #include <linux/list.h>
1092 #include <linux/uaccess.h>
1093 #include <linux/seq_file.h>
2121bcd9 1094@@ -824,3 +825,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
7f207e10 1095
2121bcd9
AM
1096 return 0;
1097 }
1098+EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
7f207e10 1099diff --git a/security/security.c b/security/security.c
b00004a5 1100index 7bc2fde..6bd0468 100644
7f207e10
AM
1101--- a/security/security.c
1102+++ b/security/security.c
b00004a5 1103@@ -537,6 +537,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 1104 return 0;
c2c0f25c 1105 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 1106 }
febd17d6 1107+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 1108
5afbbe0d 1109 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 1110 {
b00004a5 1111@@ -553,6 +554,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 1112 return 0;
c2c0f25c 1113 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 1114 }
febd17d6 1115+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 1116
5afbbe0d 1117 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1118 struct dentry *new_dentry)
b00004a5 1119@@ -561,6 +563,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1120 return 0;
c2c0f25c 1121 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 1122 }
febd17d6 1123+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 1124
5afbbe0d
AM
1125 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1126 const struct path *new_dir, struct dentry *new_dentry,
b00004a5 1127@@ -588,6 +591,7 @@ int security_path_truncate(const struct path *path)
7f207e10 1128 return 0;
c2c0f25c 1129 return call_int_hook(path_truncate, 0, path);
7f207e10 1130 }
febd17d6 1131+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 1132
5afbbe0d 1133 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 1134 {
b00004a5 1135@@ -595,6 +599,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 1136 return 0;
c2c0f25c 1137 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 1138 }
febd17d6 1139+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 1140
5afbbe0d 1141 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1142 {
b00004a5 1143@@ -602,6 +607,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1144 return 0;
c2c0f25c 1145 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 1146 }
febd17d6 1147+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 1148
5afbbe0d 1149 int security_path_chroot(const struct path *path)
7f207e10 1150 {
b00004a5 1151@@ -687,6 +693,7 @@ int security_inode_readlink(struct dentry *dentry)
7f207e10 1152 return 0;
c2c0f25c 1153 return call_int_hook(inode_readlink, 0, dentry);
7f207e10 1154 }
febd17d6 1155+EXPORT_SYMBOL_GPL(security_inode_readlink);
7f207e10 1156
c2c0f25c
AM
1157 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1158 bool rcu)
b00004a5 1159@@ -702,6 +709,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 1160 return 0;
c2c0f25c 1161 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 1162 }
febd17d6 1163+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 1164
1e00d052 1165 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
7f207e10 1166 {
b00004a5 1167@@ -873,6 +881,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
1168
1169 return fsnotify_perm(file, mask);
1170 }
febd17d6 1171+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
1172
1173 int security_file_alloc(struct file *file)
1174 {
b00004a5 1175@@ -932,6 +941,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
7f207e10
AM
1176 return ret;
1177 return ima_file_mmap(file, prot);
1178 }
febd17d6 1179+EXPORT_SYMBOL_GPL(security_mmap_file);
7f207e10 1180
0c3ec466
AM
1181 int security_mmap_addr(unsigned long addr)
1182 {
7f207e10
AM
1183diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1184--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
b00004a5 1185+++ linux/Documentation/ABI/testing/debugfs-aufs 2017-07-29 12:14:25.893041746 +0200
86dc4139 1186@@ -0,0 +1,50 @@
7f207e10
AM
1187+What: /debug/aufs/si_<id>/
1188+Date: March 2009
f6b6e03d 1189+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1190+Description:
1191+ Under /debug/aufs, a directory named si_<id> is created
1192+ per aufs mount, where <id> is a unique id generated
1193+ internally.
1facf9fc 1194+
86dc4139
AM
1195+What: /debug/aufs/si_<id>/plink
1196+Date: Apr 2013
f6b6e03d 1197+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
1198+Description:
1199+ It has three lines and shows the information about the
1200+ pseudo-link. The first line is a single number
1201+ representing a number of buckets. The second line is a
1202+ number of pseudo-links per buckets (separated by a
1203+ blank). The last line is a single number representing a
1204+ total number of psedo-links.
1205+ When the aufs mount option 'noplink' is specified, it
1206+ will show "1\n0\n0\n".
1207+
7f207e10
AM
1208+What: /debug/aufs/si_<id>/xib
1209+Date: March 2009
f6b6e03d 1210+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1211+Description:
1212+ It shows the consumed blocks by xib (External Inode Number
1213+ Bitmap), its block size and file size.
1214+ When the aufs mount option 'noxino' is specified, it
1215+ will be empty. About XINO files, see the aufs manual.
1216+
1217+What: /debug/aufs/si_<id>/xino0, xino1 ... xinoN
1218+Date: March 2009
f6b6e03d 1219+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1220+Description:
1221+ It shows the consumed blocks by xino (External Inode Number
1222+ Translation Table), its link count, block size and file
1223+ size.
1224+ When the aufs mount option 'noxino' is specified, it
1225+ will be empty. About XINO files, see the aufs manual.
1226+
1227+What: /debug/aufs/si_<id>/xigen
1228+Date: March 2009
f6b6e03d 1229+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1230+Description:
1231+ It shows the consumed blocks by xigen (External Inode
1232+ Generation Table), its block size and file size.
1233+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1234+ be created.
1235+ When the aufs mount option 'noxino' is specified, it
1236+ will be empty. About XINO files, see the aufs manual.
1237diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1238--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
b00004a5 1239+++ linux/Documentation/ABI/testing/sysfs-aufs 2017-07-29 12:14:25.893041746 +0200
392086de 1240@@ -0,0 +1,31 @@
7f207e10
AM
1241+What: /sys/fs/aufs/si_<id>/
1242+Date: March 2009
f6b6e03d 1243+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1244+Description:
1245+ Under /sys/fs/aufs, a directory named si_<id> is created
1246+ per aufs mount, where <id> is a unique id generated
1247+ internally.
1248+
1249+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1250+Date: March 2009
f6b6e03d 1251+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1252+Description:
1253+ It shows the abolute path of a member directory (which
1254+ is called branch) in aufs, and its permission.
1255+
392086de
AM
1256+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1257+Date: July 2013
f6b6e03d 1258+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1259+Description:
1260+ It shows the id of a member directory (which is called
1261+ branch) in aufs.
1262+
7f207e10
AM
1263+What: /sys/fs/aufs/si_<id>/xi_path
1264+Date: March 2009
f6b6e03d 1265+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1266+Description:
1267+ It shows the abolute path of XINO (External Inode Number
1268+ Bitmap, Translation Table and Generation Table) file
1269+ even if it is the default path.
1270+ When the aufs mount option 'noxino' is specified, it
1271+ will be empty. About XINO files, see the aufs manual.
53392da6
AM
1272diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1273--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1274+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2018-04-15 08:49:13.394483860 +0200
1c60b727 1275@@ -0,0 +1,171 @@
53392da6 1276+
b00004a5 1277+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1278+#
1279+# This program is free software; you can redistribute it and/or modify
1280+# it under the terms of the GNU General Public License as published by
1281+# the Free Software Foundation; either version 2 of the License, or
1282+# (at your option) any later version.
1283+#
1284+# This program is distributed in the hope that it will be useful,
1285+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1286+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1287+# GNU General Public License for more details.
1288+#
1289+# You should have received a copy of the GNU General Public License
523b37e3 1290+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1291+
1292+Introduction
1293+----------------------------------------
1294+
3c1bdaff 1295+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1296+1. abbrev. for "advanced multi-layered unification filesystem".
1297+2. abbrev. for "another unionfs".
1298+3. abbrev. for "auf das" in German which means "on the" in English.
1299+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1300+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1301+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1302+
1303+AUFS is a filesystem with features:
1304+- multi layered stackable unification filesystem, the member directory
1305+ is called as a branch.
1306+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1307+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1308+ combination.
1309+- internal "file copy-on-write".
1310+- logical deletion, whiteout.
1311+- dynamic branch manipulation, adding, deleting and changing permission.
1312+- allow bypassing aufs, user's direct branch access.
1313+- external inode number translation table and bitmap which maintains the
1314+ persistent aufs inode number.
1315+- seekable directory, including NFS readdir.
1316+- file mapping, mmap and sharing pages.
1317+- pseudo-link, hardlink over branches.
1318+- loopback mounted filesystem as a branch.
1319+- several policies to select one among multiple writable branches.
1320+- revert a single systemcall when an error occurs in aufs.
1321+- and more...
1322+
1323+
1324+Multi Layered Stackable Unification Filesystem
1325+----------------------------------------------------------------------
1326+Most people already knows what it is.
1327+It is a filesystem which unifies several directories and provides a
1328+merged single directory. When users access a file, the access will be
1329+passed/re-directed/converted (sorry, I am not sure which English word is
1330+correct) to the real file on the member filesystem. The member
1331+filesystem is called 'lower filesystem' or 'branch' and has a mode
1332+'readonly' and 'readwrite.' And the deletion for a file on the lower
1333+readonly branch is handled by creating 'whiteout' on the upper writable
1334+branch.
1335+
1336+On LKML, there have been discussions about UnionMount (Jan Blunck,
1337+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1338+different approaches to implement the merged-view.
1339+The former tries putting it into VFS, and the latter implements as a
1340+separate filesystem.
1341+(If I misunderstand about these implementations, please let me know and
1342+I shall correct it. Because it is a long time ago when I read their
1343+source files last time).
1344+
1345+UnionMount's approach will be able to small, but may be hard to share
1346+branches between several UnionMount since the whiteout in it is
1347+implemented in the inode on branch filesystem and always
1348+shared. According to Bharata's post, readdir does not seems to be
1349+finished yet.
1350+There are several missing features known in this implementations such as
1351+- for users, the inode number may change silently. eg. copy-up.
1352+- link(2) may break by copy-up.
1353+- read(2) may get an obsoleted filedata (fstat(2) too).
1354+- fcntl(F_SETLK) may be broken by copy-up.
1355+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1356+ open(O_RDWR).
1357+
7e9cd9fe
AM
1358+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1359+merged into mainline. This is another implementation of UnionMount as a
1360+separated filesystem. All the limitations and known problems which
1361+UnionMount are equally inherited to "overlay" filesystem.
1362+
1363+Unionfs has a longer history. When I started implementing a stackable
1364+filesystem (Aug 2005), it already existed. It has virtual super_block,
1365+inode, dentry and file objects and they have an array pointing lower
1366+same kind objects. After contributing many patches for Unionfs, I
1367+re-started my project AUFS (Jun 2006).
53392da6
AM
1368+
1369+In AUFS, the structure of filesystem resembles to Unionfs, but I
1370+implemented my own ideas, approaches and enhancements and it became
1371+totally different one.
1372+
1373+Comparing DM snapshot and fs based implementation
1374+- the number of bytes to be copied between devices is much smaller.
1375+- the type of filesystem must be one and only.
1376+- the fs must be writable, no readonly fs, even for the lower original
1377+ device. so the compression fs will not be usable. but if we use
1378+ loopback mount, we may address this issue.
1379+ for instance,
1380+ mount /cdrom/squashfs.img /sq
1381+ losetup /sq/ext2.img
1382+ losetup /somewhere/cow
1383+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1384+- it will be difficult (or needs more operations) to extract the
1385+ difference between the original device and COW.
1386+- DM snapshot-merge may help a lot when users try merging. in the
1387+ fs-layer union, users will use rsync(1).
1388+
7e9cd9fe
AM
1389+You may want to read my old paper "Filesystems in LiveCD"
1390+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1391+
7e9cd9fe
AM
1392+
1393+Several characters/aspects/persona of aufs
53392da6
AM
1394+----------------------------------------------------------------------
1395+
7e9cd9fe 1396+Aufs has several characters, aspects or persona.
53392da6
AM
1397+1. a filesystem, callee of VFS helper
1398+2. sub-VFS, caller of VFS helper for branches
1399+3. a virtual filesystem which maintains persistent inode number
1400+4. reader/writer of files on branches such like an application
1401+
1402+1. Callee of VFS Helper
1403+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1404+unlink(2) from an application reaches sys_unlink() kernel function and
1405+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1406+calls filesystem specific unlink operation. Actually aufs implements the
1407+unlink operation but it behaves like a redirector.
1408+
1409+2. Caller of VFS Helper for Branches
1410+aufs_unlink() passes the unlink request to the branch filesystem as if
1411+it were called from VFS. So the called unlink operation of the branch
1412+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1413+every necessary pre/post operation for the branch filesystem.
1414+- acquire the lock for the parent dir on a branch
1415+- lookup in a branch
1416+- revalidate dentry on a branch
1417+- mnt_want_write() for a branch
1418+- vfs_unlink() for a branch
1419+- mnt_drop_write() for a branch
1420+- release the lock on a branch
1421+
1422+3. Persistent Inode Number
1423+One of the most important issue for a filesystem is to maintain inode
1424+numbers. This is particularly important to support exporting a
1425+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1426+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1427+keep and maintain the inode numbers. It may be a large space and may not
1428+suit to keep in memory. Aufs rents some space from its first writable
1429+branch filesystem (by default) and creates file(s) on it. These files
1430+are created by aufs internally and removed soon (currently) keeping
1431+opened.
53392da6
AM
1432+Note: Because these files are removed, they are totally gone after
1433+ unmounting aufs. It means the inode numbers are not persistent
1434+ across unmount or reboot. I have a plan to make them really
1435+ persistent which will be important for aufs on NFS server.
1436+
1437+4. Read/Write Files Internally (copy-on-write)
1438+Because a branch can be readonly, when you write a file on it, aufs will
1439+"copy-up" it to the upper writable branch internally. And then write the
1440+originally requested thing to the file. Generally kernel doesn't
1441+open/read/write file actively. In aufs, even a single write may cause a
1442+internal "file copy". This behaviour is very similar to cp(1) command.
1443+
1444+Some people may think it is better to pass such work to user space
1445+helper, instead of doing in kernel space. Actually I am still thinking
1446+about it. But currently I have implemented it in kernel space.
1447diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1448--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1449+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 1450@@ -0,0 +1,258 @@
53392da6 1451+
b00004a5 1452+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1453+#
1454+# This program is free software; you can redistribute it and/or modify
1455+# it under the terms of the GNU General Public License as published by
1456+# the Free Software Foundation; either version 2 of the License, or
1457+# (at your option) any later version.
1458+#
1459+# This program is distributed in the hope that it will be useful,
1460+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1461+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1462+# GNU General Public License for more details.
1463+#
1464+# You should have received a copy of the GNU General Public License
523b37e3 1465+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1466+
1467+Basic Aufs Internal Structure
1468+
1469+Superblock/Inode/Dentry/File Objects
1470+----------------------------------------------------------------------
1471+As like an ordinary filesystem, aufs has its own
1472+superblock/inode/dentry/file objects. All these objects have a
1473+dynamically allocated array and store the same kind of pointers to the
1474+lower filesystem, branch.
1475+For example, when you build a union with one readwrite branch and one
1476+readonly, mounted /au, /rw and /ro respectively.
1477+- /au = /rw + /ro
1478+- /ro/fileA exists but /rw/fileA
1479+
1480+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1481+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1482+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1483+- [1] = /ro/fileA
1484+
1485+This style of an array is essentially same to the aufs
1486+superblock/inode/dentry/file objects.
1487+
1488+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1489+branches dynamically, these objects has its own generation. When
1490+branches are changed, the generation in aufs superblock is
1491+incremented. And a generation in other object are compared when it is
1492+accessed. When a generation in other objects are obsoleted, aufs
1493+refreshes the internal array.
53392da6
AM
1494+
1495+
1496+Superblock
1497+----------------------------------------------------------------------
1498+Additionally aufs superblock has some data for policies to select one
1499+among multiple writable branches, XIB files, pseudo-links and kobject.
1500+See below in detail.
7e9cd9fe
AM
1501+About the policies which supports copy-down a directory, see
1502+wbr_policy.txt too.
53392da6
AM
1503+
1504+
1505+Branch and XINO(External Inode Number Translation Table)
1506+----------------------------------------------------------------------
1507+Every branch has its own xino (external inode number translation table)
1508+file. The xino file is created and unlinked by aufs internally. When two
1509+members of a union exist on the same filesystem, they share the single
1510+xino file.
1511+The struct of a xino file is simple, just a sequence of aufs inode
1512+numbers which is indexed by the lower inode number.
1513+In the above sample, assume the inode number of /ro/fileA is i111 and
1514+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1515+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1516+
1517+When the inode numbers are not contiguous, the xino file will be sparse
1518+which has a hole in it and doesn't consume as much disk space as it
1519+might appear. If your branch filesystem consumes disk space for such
1520+holes, then you should specify 'xino=' option at mounting aufs.
1521+
7e9cd9fe
AM
1522+Aufs has a mount option to free the disk blocks for such holes in XINO
1523+files on tmpfs or ramdisk. But it is not so effective actually. If you
1524+meet a problem of disk shortage due to XINO files, then you should try
1525+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1526+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1527+the holes in XINO files.
1528+
53392da6 1529+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1530+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1531+whiteout-ed doubly, so that users will never see their names in aufs
1532+hierarchy.
7e9cd9fe 1533+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1534+2. a directory to store a pseudo-link.
7e9cd9fe 1535+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1536+
1537+1. Whiteout Base
1538+ When you remove a file on a readonly branch, aufs handles it as a
1539+ logical deletion and creates a whiteout on the upper writable branch
1540+ as a hardlink of this file in order not to consume inode on the
1541+ writable branch.
1542+2. Pseudo-link Dir
1543+ See below, Pseudo-link.
1544+3. Step-Parent Dir
1545+ When "fileC" exists on the lower readonly branch only and it is
1546+ opened and removed with its parent dir, and then user writes
1547+ something into it, then aufs copies-up fileC to this
1548+ directory. Because there is no other dir to store fileC. After
1549+ creating a file under this dir, the file is unlinked.
1550+
1551+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1552+dynamically, a branch has its own id. When the branch order changes,
1553+aufs finds the new index by searching the branch id.
53392da6
AM
1554+
1555+
1556+Pseudo-link
1557+----------------------------------------------------------------------
1558+Assume "fileA" exists on the lower readonly branch only and it is
1559+hardlinked to "fileB" on the branch. When you write something to fileA,
1560+aufs copies-up it to the upper writable branch. Additionally aufs
1561+creates a hardlink under the Pseudo-link Directory of the writable
1562+branch. The inode of a pseudo-link is kept in aufs super_block as a
1563+simple list. If fileB is read after unlinking fileA, aufs returns
1564+filedata from the pseudo-link instead of the lower readonly
1565+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1566+inode number by xino (see above) is essentially necessary.
53392da6
AM
1567+
1568+All the hardlinks under the Pseudo-link Directory of the writable branch
1569+should be restored in a proper location later. Aufs provides a utility
1570+to do this. The userspace helpers executed at remounting and unmounting
1571+aufs by default.
1572+During this utility is running, it puts aufs into the pseudo-link
1573+maintenance mode. In this mode, only the process which began the
1574+maintenance mode (and its child processes) is allowed to operate in
1575+aufs. Some other processes which are not related to the pseudo-link will
1576+be allowed to run too, but the rest have to return an error or wait
1577+until the maintenance mode ends. If a process already acquires an inode
1578+mutex (in VFS), it has to return an error.
1579+
1580+
1581+XIB(external inode number bitmap)
1582+----------------------------------------------------------------------
1583+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1584+bitmap in a superblock object. It is also an internal file such like a
1585+xino file.
53392da6
AM
1586+It is a simple bitmap to mark whether the aufs inode number is in-use or
1587+not.
1588+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1589+
7e9cd9fe 1590+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1591+reduce the number of consumed disk blocks for these files.
1592+
1593+
1594+Virtual or Vertical Dir, and Readdir in Userspace
1595+----------------------------------------------------------------------
1596+In order to support multiple layers (branches), aufs readdir operation
1597+constructs a virtual dir block on memory. For readdir, aufs calls
1598+vfs_readdir() internally for each dir on branches, merges their entries
1599+with eliminating the whiteout-ed ones, and sets it to file (dir)
1600+object. So the file object has its entry list until it is closed. The
1601+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1602+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1603+
1604+The dynamically allocated memory block for the name of entries has a
1605+unit of 512 bytes (by default) and stores the names contiguously (no
1606+padding). Another block for each entry is handled by kmem_cache too.
1607+During building dir blocks, aufs creates hash list and judging whether
1608+the entry is whiteouted by its upper branch or already listed.
1609+The merged result is cached in the corresponding inode object and
1610+maintained by a customizable life-time option.
1611+
1612+Some people may call it can be a security hole or invite DoS attack
1613+since the opened and once readdir-ed dir (file object) holds its entry
1614+list and becomes a pressure for system memory. But I'd say it is similar
1615+to files under /proc or /sys. The virtual files in them also holds a
1616+memory page (generally) while they are opened. When an idea to reduce
1617+memory for them is introduced, it will be applied to aufs too.
1618+For those who really hate this situation, I've developed readdir(3)
1619+library which operates this merging in userspace. You just need to set
1620+LD_PRELOAD environment variable, and aufs will not consume no memory in
1621+kernel space for readdir(3).
1622+
1623+
1624+Workqueue
1625+----------------------------------------------------------------------
1626+Aufs sometimes requires privilege access to a branch. For instance,
1627+in copy-up/down operation. When a user process is going to make changes
1628+to a file which exists in the lower readonly branch only, and the mode
1629+of one of ancestor directories may not be writable by a user
1630+process. Here aufs copy-up the file with its ancestors and they may
1631+require privilege to set its owner/group/mode/etc.
1632+This is a typical case of a application character of aufs (see
1633+Introduction).
1634+
1635+Aufs uses workqueue synchronously for this case. It creates its own
1636+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1637+passes the request to call mkdir or write (for example), and wait for
1638+its completion. This approach solves a problem of a signal handler
1639+simply.
1640+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1641+process, then the process may receive the unexpected SIGXFSZ or other
1642+signals.
53392da6
AM
1643+
1644+Also aufs uses the system global workqueue ("events" kernel thread) too
1645+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1646+whiteout base and etc. This is unrelated to a privilege.
1647+Most of aufs operation tries acquiring a rw_semaphore for aufs
1648+superblock at the beginning, at the same time waits for the completion
1649+of all queued asynchronous tasks.
1650+
1651+
1652+Whiteout
1653+----------------------------------------------------------------------
1654+The whiteout in aufs is very similar to Unionfs's. That is represented
1655+by its filename. UnionMount takes an approach of a file mode, but I am
1656+afraid several utilities (find(1) or something) will have to support it.
1657+
1658+Basically the whiteout represents "logical deletion" which stops aufs to
1659+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1660+further lookup.
53392da6
AM
1661+
1662+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1663+In order to make several functions in a single systemcall to be
1664+revertible, aufs adopts an approach to rename a directory to a temporary
1665+unique whiteouted name.
1666+For example, in rename(2) dir where the target dir already existed, aufs
1667+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1668+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1669+update the attributes, etc). If an error happens in these actions, aufs
1670+simply renames the whiteouted name back and returns an error. If all are
1671+succeeded, aufs registers a function to remove the whiteouted unique
1672+temporary name completely and asynchronously to the system global
1673+workqueue.
1674+
1675+
1676+Copy-up
1677+----------------------------------------------------------------------
1678+It is a well-known feature or concept.
1679+When user modifies a file on a readonly branch, aufs operate "copy-up"
1680+internally and makes change to the new file on the upper writable branch.
1681+When the trigger systemcall does not update the timestamps of the parent
1682+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1683+
1684+
1685+Move-down (aufs3.9 and later)
1686+----------------------------------------------------------------------
1687+"Copy-up" is one of the essential feature in aufs. It copies a file from
1688+the lower readonly branch to the upper writable branch when a user
1689+changes something about the file.
1690+"Move-down" is an opposite action of copy-up. Basically this action is
1691+ran manually instead of automatically and internally.
076b876e
AM
1692+For desgin and implementation, aufs has to consider these issues.
1693+- whiteout for the file may exist on the lower branch.
1694+- ancestor directories may not exist on the lower branch.
1695+- diropq for the ancestor directories may exist on the upper branch.
1696+- free space on the lower branch will reduce.
1697+- another access to the file may happen during moving-down, including
7e9cd9fe 1698+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1699+- the file should not be hard-linked nor pseudo-linked. they should be
1700+ handled by auplink utility later.
c2b27bf2
AM
1701+
1702+Sometimes users want to move-down a file from the upper writable branch
1703+to the lower readonly or writable branch. For instance,
1704+- the free space of the upper writable branch is going to run out.
1705+- create a new intermediate branch between the upper and lower branch.
1706+- etc.
1707+
1708+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e
AM
1709diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1710--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1711+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2018-04-15 08:49:13.394483860 +0200
b912730e
AM
1712@@ -0,0 +1,85 @@
1713+
b00004a5 1714+# Copyright (C) 2015-2018 Junjiro R. Okajima
b912730e
AM
1715+#
1716+# This program is free software; you can redistribute it and/or modify
1717+# it under the terms of the GNU General Public License as published by
1718+# the Free Software Foundation; either version 2 of the License, or
1719+# (at your option) any later version.
1720+#
1721+# This program is distributed in the hope that it will be useful,
1722+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1723+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1724+# GNU General Public License for more details.
1725+#
1726+# You should have received a copy of the GNU General Public License
1727+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1728+
1729+Support for a branch who has its ->atomic_open()
1730+----------------------------------------------------------------------
1731+The filesystems who implement its ->atomic_open() are not majority. For
1732+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1733+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1734+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1735+sure whether all filesystems who have ->atomic_open() behave like this,
1736+but NFSv4 surely returns the error.
1737+
1738+In order to support ->atomic_open() for aufs, there are a few
1739+approaches.
1740+
1741+A. Introduce aufs_atomic_open()
1742+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1743+ branch fs.
1744+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1745+ an aufs user Pip Cet's approach
1746+ - calls aufs_create(), VFS finish_open() and notify_change().
1747+ - pass fake-mode to finish_open(), and then correct the mode by
1748+ notify_change().
1749+C. Extend aufs_open() to call branch fs's ->atomic_open()
1750+ - no aufs_atomic_open().
1751+ - aufs_lookup() registers the TID to an aufs internal object.
1752+ - aufs_create() does nothing when the matching TID is registered, but
1753+ registers the mode.
1754+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1755+ TID is registered.
1756+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1757+ credential
1758+ - no aufs_atomic_open().
1759+ - aufs_create() registers the TID to an internal object. this info
1760+ represents "this process created this file just now."
1761+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1762+ registered TID and re-try open() with superuser's credential.
1763+
1764+Pros and cons for each approach.
1765+
1766+A.
1767+ - straightforward but highly depends upon VFS internal.
1768+ - the atomic behavaiour is kept.
1769+ - some of parameters such as nameidata are hard to reproduce for
1770+ branch fs.
1771+ - large overhead.
1772+B.
1773+ - easy to implement.
1774+ - the atomic behavaiour is lost.
1775+C.
1776+ - the atomic behavaiour is kept.
1777+ - dirty and tricky.
1778+ - VFS checks whether the file is created correctly after calling
1779+ ->create(), which means this approach doesn't work.
1780+D.
1781+ - easy to implement.
1782+ - the atomic behavaiour is lost.
1783+ - to open a file with superuser's credential and give it to a user
1784+ process is a bad idea, since the file object keeps the credential
1785+ in it. It may affect LSM or something. This approach doesn't work
1786+ either.
1787+
1788+The approach A is ideal, but it hard to implement. So here is a
1789+variation of A, which is to be implemented.
1790+
1791+A-1. Introduce aufs_atomic_open()
1792+ - calls branch fs ->atomic_open() if exists. otherwise calls
1793+ vfs_create() and finish_open().
1794+ - the demerit is that the several checks after branch fs
1795+ ->atomic_open() are lost. in the ordinary case, the checks are
1796+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1797+ be implemented in aufs, but not all I am afraid.
53392da6
AM
1798diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1799--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1800+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 1801@@ -0,0 +1,113 @@
53392da6 1802+
b00004a5 1803+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1804+#
1805+# This program is free software; you can redistribute it and/or modify
1806+# it under the terms of the GNU General Public License as published by
1807+# the Free Software Foundation; either version 2 of the License, or
1808+# (at your option) any later version.
1809+#
1810+# This program is distributed in the hope that it will be useful,
1811+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1812+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1813+# GNU General Public License for more details.
1814+#
1815+# You should have received a copy of the GNU General Public License
523b37e3 1816+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1817+
1818+Lookup in a Branch
1819+----------------------------------------------------------------------
1820+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1821+lookup for branches as VFS does. It may be a heavy work. But almost all
1822+lookup operation in aufs is the simplest case, ie. lookup only an entry
1823+directly connected to its parent. Digging down the directory hierarchy
1824+is unnecessary. VFS has a function lookup_one_len() for that use, and
1825+aufs calls it.
1826+
1827+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1828+->d_revalidate(), also aufs forces the hardest revalidate tests for
1829+them.
1830+For d_revalidate, aufs implements three levels of revalidate tests. See
1831+"Revalidate Dentry and UDBA" in detail.
1832+
1833+
076b876e
AM
1834+Test Only the Highest One for the Directory Permission (dirperm1 option)
1835+----------------------------------------------------------------------
1836+Let's try case study.
1837+- aufs has two branches, upper readwrite and lower readonly.
1838+ /au = /rw + /ro
1839+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1840+- user invoked "chmod a+rx /au/dirA"
1841+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1842+ permission bits are set to world readable.
076b876e
AM
1843+- then "/au/dirA" becomes world readable?
1844+
1845+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1846+or it may be a natively readonly filesystem. If aufs respects the lower
1847+branch, it should not respond readdir request from other users. But user
1848+allowed it by chmod. Should really aufs rejects showing the entries
1849+under /ro/dirA?
1850+
7e9cd9fe
AM
1851+To be honest, I don't have a good solution for this case. So aufs
1852+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1853+users.
076b876e
AM
1854+When dirperm1 is specified, aufs checks only the highest one for the
1855+directory permission, and shows the entries. Otherwise, as usual, checks
1856+every dir existing on all branches and rejects the request.
1857+
1858+As a side effect, dirperm1 option improves the performance of aufs
1859+because the number of permission check is reduced when the number of
1860+branch is many.
1861+
1862+
53392da6
AM
1863+Revalidate Dentry and UDBA (User's Direct Branch Access)
1864+----------------------------------------------------------------------
1865+Generally VFS helpers re-validate a dentry as a part of lookup.
1866+0. digging down the directory hierarchy.
1867+1. lock the parent dir by its i_mutex.
1868+2. lookup the final (child) entry.
1869+3. revalidate it.
1870+4. call the actual operation (create, unlink, etc.)
1871+5. unlock the parent dir
1872+
1873+If the filesystem implements its ->d_revalidate() (step 3), then it is
1874+called. Actually aufs implements it and checks the dentry on a branch is
1875+still valid.
1876+But it is not enough. Because aufs has to release the lock for the
1877+parent dir on a branch at the end of ->lookup() (step 2) and
1878+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1879+held by VFS.
1880+If the file on a branch is changed directly, eg. bypassing aufs, after
1881+aufs released the lock, then the subsequent operation may cause
1882+something unpleasant result.
1883+
1884+This situation is a result of VFS architecture, ->lookup() and
1885+->d_revalidate() is separated. But I never say it is wrong. It is a good
1886+design from VFS's point of view. It is just not suitable for sub-VFS
1887+character in aufs.
1888+
1889+Aufs supports such case by three level of revalidation which is
1890+selectable by user.
1891+1. Simple Revalidate
1892+ Addition to the native flow in VFS's, confirm the child-parent
1893+ relationship on the branch just after locking the parent dir on the
1894+ branch in the "actual operation" (step 4). When this validation
1895+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1896+ checks the validation of the dentry on branches.
1897+2. Monitor Changes Internally by Inotify/Fsnotify
1898+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1899+ the dentry on the branch, and returns EBUSY if it finds different
1900+ dentry.
1901+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1902+ during it is in cache. When the event is notified, aufs registers a
1903+ function to kernel 'events' thread by schedule_work(). And the
1904+ function sets some special status to the cached aufs dentry and inode
1905+ private data. If they are not cached, then aufs has nothing to
1906+ do. When the same file is accessed through aufs (step 0-3) later,
1907+ aufs will detect the status and refresh all necessary data.
1908+ In this mode, aufs has to ignore the event which is fired by aufs
1909+ itself.
1910+3. No Extra Validation
1911+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1912+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1913+ aufs performance when system surely hide the aufs branches from user,
1914+ by over-mounting something (or another method).
1915diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1916--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1917+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 1918@@ -0,0 +1,74 @@
53392da6 1919+
b00004a5 1920+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1921+#
1922+# This program is free software; you can redistribute it and/or modify
1923+# it under the terms of the GNU General Public License as published by
1924+# the Free Software Foundation; either version 2 of the License, or
1925+# (at your option) any later version.
1926+#
1927+# This program is distributed in the hope that it will be useful,
1928+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1929+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1930+# GNU General Public License for more details.
1931+#
1932+# You should have received a copy of the GNU General Public License
523b37e3 1933+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1934+
1935+Branch Manipulation
1936+
1937+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1938+and changing its permission/attribute, there are a lot of works to do.
1939+
1940+
1941+Add a Branch
1942+----------------------------------------------------------------------
1943+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1944+ mount, and its various attributes.
53392da6
AM
1945+o Initialize the xino file and whiteout bases if necessary.
1946+ See struct.txt.
1947+
1948+o Check the owner/group/mode of the directory
1949+ When the owner/group/mode of the adding directory differs from the
1950+ existing branch, aufs issues a warning because it may impose a
1951+ security risk.
1952+ For example, when a upper writable branch has a world writable empty
1953+ top directory, a malicious user can create any files on the writable
1954+ branch directly, like copy-up and modify manually. If something like
1955+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1956+ writable branch, and the writable branch is world-writable, then a
1957+ malicious guy may create /etc/passwd on the writable branch directly
1958+ and the infected file will be valid in aufs.
7e9cd9fe 1959+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1960+ producing a warning.
1961+
1962+
1963+Delete a Branch
1964+----------------------------------------------------------------------
1965+o Confirm the deleting branch is not busy
1966+ To be general, there is one merit to adopt "remount" interface to
1967+ manipulate branches. It is to discard caches. At deleting a branch,
1968+ aufs checks the still cached (and connected) dentries and inodes. If
1969+ there are any, then they are all in-use. An inode without its
1970+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1971+
1972+ For the cached one, aufs checks whether the same named entry exists on
1973+ other branches.
1974+ If the cached one is a directory, because aufs provides a merged view
1975+ to users, as long as one dir is left on any branch aufs can show the
1976+ dir to users. In this case, the branch can be removed from aufs.
1977+ Otherwise aufs rejects deleting the branch.
1978+
1979+ If any file on the deleting branch is opened by aufs, then aufs
1980+ rejects deleting.
1981+
1982+
1983+Modify the Permission of a Branch
1984+----------------------------------------------------------------------
1985+o Re-initialize or remove the xino file and whiteout bases if necessary.
1986+ See struct.txt.
1987+
1988+o rw --> ro: Confirm the modifying branch is not busy
1989+ Aufs rejects the request if any of these conditions are true.
1990+ - a file on the branch is mmap-ed.
1991+ - a regular file on the branch is opened for write and there is no
1992+ same named entry on the upper branch.
1993diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1994--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1995+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2018-04-15 08:49:13.394483860 +0200
523b37e3 1996@@ -0,0 +1,64 @@
53392da6 1997+
b00004a5 1998+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1999+#
2000+# This program is free software; you can redistribute it and/or modify
2001+# it under the terms of the GNU General Public License as published by
2002+# the Free Software Foundation; either version 2 of the License, or
2003+# (at your option) any later version.
2004+#
2005+# This program is distributed in the hope that it will be useful,
2006+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2007+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2008+# GNU General Public License for more details.
2009+#
2010+# You should have received a copy of the GNU General Public License
523b37e3 2011+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2012+
2013+Policies to Select One among Multiple Writable Branches
2014+----------------------------------------------------------------------
2015+When the number of writable branch is more than one, aufs has to decide
2016+the target branch for file creation or copy-up. By default, the highest
2017+writable branch which has the parent (or ancestor) dir of the target
2018+file is chosen (top-down-parent policy).
2019+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
2020+writable branch, for file creation several policies, round-robin,
2021+most-free-space, and other policies. For copy-up, top-down-parent,
2022+bottom-up-parent, bottom-up and others.
53392da6
AM
2023+
2024+As expected, the round-robin policy selects the branch in circular. When
2025+you have two writable branches and creates 10 new files, 5 files will be
2026+created for each branch. mkdir(2) systemcall is an exception. When you
2027+create 10 new directories, all will be created on the same branch.
2028+And the most-free-space policy selects the one which has most free
2029+space among the writable branches. The amount of free space will be
2030+checked by aufs internally, and users can specify its time interval.
2031+
2032+The policies for copy-up is more simple,
2033+top-down-parent is equivalent to the same named on in create policy,
2034+bottom-up-parent selects the writable branch where the parent dir
2035+exists and the nearest upper one from the copyup-source,
2036+bottom-up selects the nearest upper writable branch from the
2037+copyup-source, regardless the existence of the parent dir.
2038+
2039+There are some rules or exceptions to apply these policies.
2040+- If there is a readonly branch above the policy-selected branch and
2041+ the parent dir is marked as opaque (a variation of whiteout), or the
2042+ target (creating) file is whiteout-ed on the upper readonly branch,
2043+ then the result of the policy is ignored and the target file will be
2044+ created on the nearest upper writable branch than the readonly branch.
2045+- If there is a writable branch above the policy-selected branch and
2046+ the parent dir is marked as opaque or the target file is whiteouted
2047+ on the branch, then the result of the policy is ignored and the target
2048+ file will be created on the highest one among the upper writable
2049+ branches who has diropq or whiteout. In case of whiteout, aufs removes
2050+ it as usual.
2051+- link(2) and rename(2) systemcalls are exceptions in every policy.
2052+ They try selecting the branch where the source exists as possible
2053+ since copyup a large file will take long time. If it can't be,
2054+ ie. the branch where the source exists is readonly, then they will
2055+ follow the copyup policy.
2056+- There is an exception for rename(2) when the target exists.
2057+ If the rename target exists, aufs compares the index of the branches
2058+ where the source and the target exists and selects the higher
2059+ one. If the selected branch is readonly, then aufs follows the
2060+ copyup policy.
8b6a4947
AM
2061diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
2062--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
b00004a5 2063+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2018-04-15 08:49:13.394483860 +0200
8b6a4947
AM
2064@@ -0,0 +1,31 @@
2065+
2066+// to view this graph, run dot(1) command in GRAPHVIZ.
2067+
2068+digraph G {
2069+node [shape=box];
2070+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
2071+
2072+node [shape=oval];
2073+
2074+aufs_rename -> whinfo [label="store/remove"];
2075+
2076+node [shape=oval];
2077+inode_list [label="h_inum list in branch\ncache"];
2078+
2079+node [shape=box];
2080+whinode [label="h_inum list file"];
2081+
2082+node [shape=oval];
2083+brmgmt [label="br_add/del/mod/umount"];
2084+
2085+brmgmt -> inode_list [label="create/remove"];
2086+brmgmt -> whinode [label="load/store"];
2087+
2088+inode_list -> whinode [style=dashed,dir=both];
2089+
2090+aufs_rename -> inode_list [label="add/del"];
2091+
2092+aufs_lookup -> inode_list [label="search"];
2093+
2094+aufs_lookup -> whinfo [label="load/remove"];
2095+}
2096diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
2097--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2098+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2018-04-15 08:49:13.394483860 +0200
8b6a4947
AM
2099@@ -0,0 +1,102 @@
2100+
b00004a5 2101+# Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
2102+#
2103+# This program is free software; you can redistribute it and/or modify
2104+# it under the terms of the GNU General Public License as published by
2105+# the Free Software Foundation; either version 2 of the License, or
2106+# (at your option) any later version.
2107+#
2108+# This program is distributed in the hope that it will be useful,
2109+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2110+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2111+# GNU General Public License for more details.
2112+#
2113+# You should have received a copy of the GNU General Public License
2114+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2115+
2116+Special handling for renaming a directory (DIRREN)
2117+----------------------------------------------------------------------
2118+First, let's assume we have a simple usecase.
2119+
2120+- /u = /rw + /ro
2121+- /rw/dirA exists
2122+- /ro/dirA and /ro/dirA/file exist too
2123+- there is no dirB on both branches
2124+- a user issues rename("dirA", "dirB")
2125+
2126+Now, what should aufs behave against this rename(2)?
2127+There are a few possible cases.
2128+
2129+A. returns EROFS.
2130+ since dirA exists on a readonly branch which cannot be renamed.
2131+B. returns EXDEV.
2132+ it is possible to copy-up dirA (only the dir itself), but the child
2133+ entries ("file" in this case) should not be. it must be a bad
2134+ approach to copy-up recursively.
2135+C. returns a success.
2136+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
2137+ is a violation of aufs' policy.
2138+D. construct an extra information which indicates that /ro/dirA should
2139+ be handled as the name of dirB.
2140+ overlayfs has a similar feature called REDIRECT.
2141+
2142+Until now, aufs implements the case B only which returns EXDEV, and
2143+expects the userspace application behaves like mv(1) which tries
2144+issueing rename(2) recursively.
2145+
2146+A new aufs feature called DIRREN is introduced which implements the case
2147+D. There are several "extra information" added.
2148+
2149+1. detailed info per renamed directory
2150+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2151+2. the inode-number list of directories on a branch
2152+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
2153+
2154+The filename of "detailed info per directory" represents the lower
2155+branch, and its format is
2156+- a type of the branch id
2157+ one of these.
2158+ + uuid (not implemented yet)
2159+ + fsid
2160+ + dev
2161+- the inode-number of the branch root dir
2162+
2163+And it contains these info in a single regular file.
2164+- magic number
2165+- branch's inode-number of the logically renamed dir
2166+- the name of the before-renamed dir
2167+
2168+The "detailed info per directory" file is created in aufs rename(2), and
2169+loaded in any lookup.
2170+The info is considered in lookup for the matching case only. Here
2171+"matching" means that the root of branch (in the info filename) is same
2172+to the current looking-up branch. After looking-up the before-renamed
2173+name, the inode-number is compared. And the matched dentry is used.
2174+
2175+The "inode-number list of directories" is a regular file which contains
2176+simply the inode-numbers on the branch. The file is created or updated
2177+in removing the branch, and loaded in adding the branch. Its lifetime is
2178+equal to the branch.
2179+The list is refered in lookup, and when the current target inode is
2180+found in the list, the aufs tries loading the "detailed info per
2181+directory" and get the changed and valid name of the dir.
2182+
2183+Theoretically these "extra informaiton" may be able to be put into XATTR
2184+in the dir inode. But aufs doesn't choose this way because
2185+1. XATTR may not be supported by the branch (or its configuration)
2186+2. XATTR may have its size limit.
2187+3. XATTR may be less easy to convert than a regular file, when the
2188+ format of the info is changed in the future.
2189+At the same time, I agree that the regular file approach is much slower
2190+than XATTR approach. So, in the future, aufs may take the XATTR or other
2191+better approach.
2192+
2193+This DIRREN feature is enabled by aufs configuration, and is activated
2194+by a new mount option.
2195+
2196+For the more complicated case, there is a work with UDBA option, which
2197+is to dected the direct access to the branches (by-passing aufs) and to
2198+maintain the cashes in aufs. Since a single cached aufs dentry may
2199+contains two names, before- and after-rename, the name comparision in
2200+UDBA handler may not work correctly. In this case, the behaviour will be
2201+equivalen to udba=reval case.
076b876e
AM
2202diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2203--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2204+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2018-04-15 08:49:13.394483860 +0200
076b876e
AM
2205@@ -0,0 +1,120 @@
2206+
b00004a5 2207+# Copyright (C) 2011-2018 Junjiro R. Okajima
076b876e
AM
2208+#
2209+# This program is free software; you can redistribute it and/or modify
2210+# it under the terms of the GNU General Public License as published by
2211+# the Free Software Foundation; either version 2 of the License, or
2212+# (at your option) any later version.
2213+#
2214+# This program is distributed in the hope that it will be useful,
2215+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2216+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2217+# GNU General Public License for more details.
2218+#
2219+# You should have received a copy of the GNU General Public License
2220+# along with this program; if not, write to the Free Software
2221+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222+
2223+
2224+File-based Hierarchical Storage Management (FHSM)
2225+----------------------------------------------------------------------
2226+Hierarchical Storage Management (or HSM) is a well-known feature in the
2227+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 2228+writable branches, based upon the principle of "Colder, the Lower".
076b876e 2229+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 2230+that the position in the order of the stacked branches vertically.
076b876e
AM
2231+These multiple writable branches are prioritized, ie. the topmost one
2232+should be the fastest drive and be used heavily.
2233+
2234+o Characters in aufs FHSM story
2235+- aufs itself and a new branch attribute.
2236+- a new ioctl interface to move-down and to establish a connection with
2237+ the daemon ("move-down" is a converse of "copy-up").
2238+- userspace tool and daemon.
2239+
2240+The userspace daemon establishes a connection with aufs and waits for
2241+the notification. The notified information is very similar to struct
2242+statfs containing the number of consumed blocks and inodes.
2243+When the consumed blocks/inodes of a branch exceeds the user-specified
2244+upper watermark, the daemon activates its move-down process until the
2245+consumed blocks/inodes reaches the user-specified lower watermark.
2246+
2247+The actual move-down is done by aufs based upon the request from
2248+user-space since we need to maintain the inode number and the internal
2249+pointer arrays in aufs.
2250+
2251+Currently aufs FHSM handles the regular files only. Additionally they
2252+must not be hard-linked nor pseudo-linked.
2253+
2254+
2255+o Cowork of aufs and the user-space daemon
2256+ During the userspace daemon established the connection, aufs sends a
2257+ small notification to it whenever aufs writes something into the
2258+ writable branch. But it may cost high since aufs issues statfs(2)
2259+ internally. So user can specify a new option to cache the
2260+ info. Actually the notification is controlled by these factors.
2261+ + the specified cache time.
2262+ + classified as "force" by aufs internally.
2263+ Until the specified time expires, aufs doesn't send the info
2264+ except the forced cases. When aufs decide forcing, the info is always
2265+ notified to userspace.
2266+ For example, the number of free inodes is generally large enough and
2267+ the shortage of it happens rarely. So aufs doesn't force the
2268+ notification when creating a new file, directory and others. This is
2269+ the typical case which aufs doesn't force.
2270+ When aufs writes the actual filedata and the files consumes any of new
2271+ blocks, the aufs forces notifying.
2272+
2273+
2274+o Interfaces in aufs
2275+- New branch attribute.
2276+ + fhsm
2277+ Specifies that the branch is managed by FHSM feature. In other word,
2278+ participant in the FHSM.
2279+ When nofhsm is set to the branch, it will not be the source/target
2280+ branch of the move-down operation. This attribute is set
2281+ independently from coo and moo attributes, and if you want full
2282+ FHSM, you should specify them as well.
2283+- New mount option.
2284+ + fhsm_sec
2285+ Specifies a second to suppress many less important info to be
2286+ notified.
2287+- New ioctl.
2288+ + AUFS_CTL_FHSM_FD
2289+ create a new file descriptor which userspace can read the notification
2290+ (a subset of struct statfs) from aufs.
2291+- Module parameter 'brs'
2292+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2293+ be set.
2294+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2295+ When there are two or more branches with fhsm attributes,
2296+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2297+ terminates it. As a result of remounting and branch-manipulation, the
2298+ number of branches with fhsm attribute can be one. In this case,
2299+ /sbin/mount.aufs will terminate the user-space daemon.
2300+
2301+
2302+Finally the operation is done as these steps in kernel-space.
2303+- make sure that,
2304+ + no one else is using the file.
2305+ + the file is not hard-linked.
2306+ + the file is not pseudo-linked.
2307+ + the file is a regular file.
2308+ + the parent dir is not opaqued.
2309+- find the target writable branch.
2310+- make sure the file is not whiteout-ed by the upper (than the target)
2311+ branch.
2312+- make the parent dir on the target branch.
2313+- mutex lock the inode on the branch.
2314+- unlink the whiteout on the target branch (if exists).
2315+- lookup and create the whiteout-ed temporary name on the target branch.
2316+- copy the file as the whiteout-ed temporary name on the target branch.
2317+- rename the whiteout-ed temporary name to the original name.
2318+- unlink the file on the source branch.
2319+- maintain the internal pointer array and the external inode number
2320+ table (XINO).
2321+- maintain the timestamps and other attributes of the parent dir and the
2322+ file.
2323+
2324+And of course, in every step, an error may happen. So the operation
2325+should restore the original file state after an error happens.
53392da6
AM
2326diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2327--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2328+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2018-04-15 08:49:13.394483860 +0200
b912730e 2329@@ -0,0 +1,72 @@
53392da6 2330+
b00004a5 2331+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
2332+#
2333+# This program is free software; you can redistribute it and/or modify
2334+# it under the terms of the GNU General Public License as published by
2335+# the Free Software Foundation; either version 2 of the License, or
2336+# (at your option) any later version.
2337+#
2338+# This program is distributed in the hope that it will be useful,
2339+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2340+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2341+# GNU General Public License for more details.
2342+#
2343+# You should have received a copy of the GNU General Public License
523b37e3 2344+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2345+
2346+mmap(2) -- File Memory Mapping
2347+----------------------------------------------------------------------
2348+In aufs, the file-mapped pages are handled by a branch fs directly, no
2349+interaction with aufs. It means aufs_mmap() calls the branch fs's
2350+->mmap().
2351+This approach is simple and good, but there is one problem.
7e9cd9fe 2352+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2353+device and inode number), and the printed path will be the path on the
2354+branch fs's instead of virtual aufs's.
2355+This is not a problem in most cases, but some utilities lsof(1) (and its
2356+user) may expect the path on aufs.
2357+
2358+To address this issue, aufs adds a new member called vm_prfile in struct
2359+vm_area_struct (and struct vm_region). The original vm_file points to
2360+the file on the branch fs in order to handle everything correctly as
2361+usual. The new vm_prfile points to a virtual file in aufs, and the
2362+show-functions in procfs refers to vm_prfile if it is set.
2363+Also we need to maintain several other places where touching vm_file
2364+such like
2365+- fork()/clone() copies vma and the reference count of vm_file is
2366+ incremented.
2367+- merging vma maintains the ref count too.
2368+
7e9cd9fe 2369+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2370+leaves all behaviour around f_mapping unchanged. This is surely an
2371+advantage.
2372+Actually aufs had adopted another complicated approach which calls
2373+generic_file_mmap() and handles struct vm_operations_struct. In this
2374+approach, aufs met a hard problem and I could not solve it without
2375+switching the approach.
b912730e
AM
2376+
2377+There may be one more another approach which is
2378+- bind-mount the branch-root onto the aufs-root internally
2379+- grab the new vfsmount (ie. struct mount)
2380+- lazy-umount the branch-root internally
2381+- in open(2) the aufs-file, open the branch-file with the hidden
2382+ vfsmount (instead of the original branch's vfsmount)
2383+- ideally this "bind-mount and lazy-umount" should be done atomically,
2384+ but it may be possible from userspace by the mount helper.
2385+
2386+Adding the internal hidden vfsmount and using it in opening a file, the
2387+file path under /proc will be printed correctly. This approach looks
2388+smarter, but is not possible I am afraid.
2389+- aufs-root may be bind-mount later. when it happens, another hidden
2390+ vfsmount will be required.
2391+- it is hard to get the chance to bind-mount and lazy-umount
2392+ + in kernel-space, FS can have vfsmount in open(2) via
2393+ file->f_path, and aufs can know its vfsmount. But several locks are
2394+ already acquired, and if aufs tries to bind-mount and lazy-umount
2395+ here, then it may cause a deadlock.
2396+ + in user-space, bind-mount doesn't invoke the mount helper.
2397+- since /proc shows dev and ino, aufs has to give vma these info. it
2398+ means a new member vm_prinode will be necessary. this is essentially
2399+ equivalent to vm_prfile described above.
2400+
2401+I have to give up this "looks-smater" approach.
c1595e42
JR
2402diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2403--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2404+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2018-04-15 08:49:13.394483860 +0200
c1595e42
JR
2405@@ -0,0 +1,96 @@
2406+
b00004a5 2407+# Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
2408+#
2409+# This program is free software; you can redistribute it and/or modify
2410+# it under the terms of the GNU General Public License as published by
2411+# the Free Software Foundation; either version 2 of the License, or
2412+# (at your option) any later version.
2413+#
2414+# This program is distributed in the hope that it will be useful,
2415+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2416+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2417+# GNU General Public License for more details.
2418+#
2419+# You should have received a copy of the GNU General Public License
2420+# along with this program; if not, write to the Free Software
2421+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2422+
2423+
2424+Listing XATTR/EA and getting the value
2425+----------------------------------------------------------------------
2426+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2427+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2428+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2429+information. But for the directories, aufs considers all the same named
2430+entries on the lower branches. Which means, if one of the lower entry
2431+rejects readdir call, then aufs returns an error even if the topmost
2432+entry allows it. This behaviour is necessary to respect the branch fs's
2433+security, but can make users confused since the user-visible standard
2434+attributes don't match the behaviour.
2435+To address this issue, aufs has a mount option called dirperm1 which
2436+checks the permission for the topmost entry only, and ignores the lower
2437+entry's permission.
2438+
2439+A similar issue can happen around XATTR.
2440+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2441+always set. Otherwise these very unpleasant situation would happen.
2442+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2443+- users may not be able to remove or reset the XATTR forever,
2444+
2445+
2446+XATTR/EA support in the internal (copy,move)-(up,down)
2447+----------------------------------------------------------------------
7e9cd9fe 2448+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2449+- "security" for LSM and capability.
2450+- "system" for posix ACL, 'acl' mount option is required for the branch
2451+ fs generally.
2452+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2453+- "user" for userspace, 'user_xattr' mount option is required for the
2454+ branch fs generally.
2455+
2456+Moreover there are some other categories. Aufs handles these rather
2457+unpopular categories as the ordinary ones, ie. there is no special
2458+condition nor exception.
2459+
2460+In copy-up, the support for XATTR on the dst branch may differ from the
2461+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2462+the original user operation which triggered the copy-up will fail. It
2463+can happen that even all copy-up will fail.
c1595e42
JR
2464+When both of src and dst branches support XATTR and if an error occurs
2465+during copying XATTR, then the copy-up should fail obviously. That is a
2466+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2467+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2468+For example, the src branch supports ACL but the dst branch doesn't
2469+because the dst branch may natively un-support it or temporary
2470+un-support it due to "noacl" mount option. Of course, the dst branch fs
2471+may NOT return an error even if the XATTR is not supported. It is
2472+totally up to the branch fs.
2473+
2474+Anyway when the aufs internal copy-up gets an error from the dst branch
2475+fs, then aufs tries removing the just copied entry and returns the error
2476+to the userspace. The worst case of this situation will be all copy-up
2477+will fail.
2478+
2479+For the copy-up operation, there two basic approaches.
2480+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2481+ error unconditionally if it happens.
c1595e42
JR
2482+- copy all XATTR, and ignore the error on the specified category only.
2483+
2484+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2485+chooses the latter approach and introduces some new branch attributes,
2486+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2487+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2488+convenient, "icex" is also provided which means all "icex*" attributes
2489+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2490+
2491+The meaning of these attributes is to ignore the error from setting
2492+XATTR on that branch.
2493+Note that aufs tries copying all XATTR unconditionally, and ignores the
2494+error from the dst branch according to the specified attributes.
2495+
2496+Some XATTR may have its default value. The default value may come from
2497+the parent dir or the environment. If the default value is set at the
2498+file creating-time, it will be overwritten by copy-up.
2499+Some contradiction may happen I am afraid.
2500+Do we need another attribute to stop copying XATTR? I am unsure. For
2501+now, aufs implements the branch attributes to ignore the error.
53392da6
AM
2502diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2503--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2504+++ linux/Documentation/filesystems/aufs/design/07export.txt 2018-04-15 08:49:13.394483860 +0200
523b37e3 2505@@ -0,0 +1,58 @@
53392da6 2506+
b00004a5 2507+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
2508+#
2509+# This program is free software; you can redistribute it and/or modify
2510+# it under the terms of the GNU General Public License as published by
2511+# the Free Software Foundation; either version 2 of the License, or
2512+# (at your option) any later version.
2513+#
2514+# This program is distributed in the hope that it will be useful,
2515+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2516+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2517+# GNU General Public License for more details.
2518+#
2519+# You should have received a copy of the GNU General Public License
523b37e3 2520+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2521+
2522+Export Aufs via NFS
2523+----------------------------------------------------------------------
2524+Here is an approach.
2525+- like xino/xib, add a new file 'xigen' which stores aufs inode
2526+ generation.
2527+- iget_locked(): initialize aufs inode generation for a new inode, and
2528+ store it in xigen file.
2529+- destroy_inode(): increment aufs inode generation and store it in xigen
2530+ file. it is necessary even if it is not unlinked, because any data of
2531+ inode may be changed by UDBA.
2532+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2533+ build file handle by
2534+ + branch id (4 bytes)
2535+ + superblock generation (4 bytes)
2536+ + inode number (4 or 8 bytes)
2537+ + parent dir inode number (4 or 8 bytes)
2538+ + inode generation (4 bytes))
2539+ + return value of exportfs_encode_fh() for the parent on a branch (4
2540+ bytes)
2541+ + file handle for a branch (by exportfs_encode_fh())
2542+- fh_to_dentry():
2543+ + find the index of a branch from its id in handle, and check it is
2544+ still exist in aufs.
2545+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2546+ + 2nd level: if not found in cache, get the parent inode number from
2547+ the handle and search it in cache. and then open the found parent
2548+ dir, find the matching inode number by vfs_readdir() and get its
2549+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2550+ + 3rd level: if the parent dir is not cached, call
2551+ exportfs_decode_fh() for a branch and get the parent on a branch,
2552+ build a pathname of it, convert it a pathname in aufs, call
2553+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2554+ the 2nd level.
2555+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2556+ for every branch, but not itself. to get this, (currently) aufs
2557+ searches in current->nsproxy->mnt_ns list. it may not be a good
2558+ idea, but I didn't get other approach.
2559+ + test the generation of the gotten inode.
2560+- every inode operation: they may get EBUSY due to UDBA. in this case,
2561+ convert it into ESTALE for NFSD.
2562+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2563+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2564diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2565--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2566+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2018-04-15 08:49:13.394483860 +0200
523b37e3 2567@@ -0,0 +1,52 @@
53392da6 2568+
b00004a5 2569+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
2570+#
2571+# This program is free software; you can redistribute it and/or modify
2572+# it under the terms of the GNU General Public License as published by
2573+# the Free Software Foundation; either version 2 of the License, or
2574+# (at your option) any later version.
2575+#
2576+# This program is distributed in the hope that it will be useful,
2577+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2578+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2579+# GNU General Public License for more details.
2580+#
2581+# You should have received a copy of the GNU General Public License
523b37e3 2582+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2583+
2584+Show Whiteout Mode (shwh)
2585+----------------------------------------------------------------------
2586+Generally aufs hides the name of whiteouts. But in some cases, to show
2587+them is very useful for users. For instance, creating a new middle layer
2588+(branch) by merging existing layers.
2589+
2590+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2591+When you have three branches,
2592+- Bottom: 'system', squashfs (underlying base system), read-only
2593+- Middle: 'mods', squashfs, read-only
2594+- Top: 'overlay', ram (tmpfs), read-write
2595+
2596+The top layer is loaded at boot time and saved at shutdown, to preserve
2597+the changes made to the system during the session.
2598+When larger changes have been made, or smaller changes have accumulated,
2599+the size of the saved top layer data grows. At this point, it would be
2600+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2601+and rewrite the 'mods' squashfs, clearing the top layer and thus
2602+restoring save and load speed.
2603+
2604+This merging is simplified by the use of another aufs mount, of just the
2605+two overlay branches using the 'shwh' option.
2606+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2607+ aufs /livesys/merge_union
2608+
2609+A merged view of these two branches is then available at
2610+/livesys/merge_union, and the new feature is that the whiteouts are
2611+visible!
2612+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2613+writing to all branches. Also the default mode for all branches is 'ro'.
2614+It is now possible to save the combined contents of the two overlay
2615+branches to a new squashfs, e.g.:
2616+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2617+
2618+This new squashfs archive can be stored on the boot device and the
2619+initramfs will use it to replace the old one at the next boot.
2620diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2621--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2622+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 2623@@ -0,0 +1,47 @@
53392da6 2624+
b00004a5 2625+# Copyright (C) 2010-2018 Junjiro R. Okajima
53392da6
AM
2626+#
2627+# This program is free software; you can redistribute it and/or modify
2628+# it under the terms of the GNU General Public License as published by
2629+# the Free Software Foundation; either version 2 of the License, or
2630+# (at your option) any later version.
2631+#
2632+# This program is distributed in the hope that it will be useful,
2633+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2634+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2635+# GNU General Public License for more details.
2636+#
2637+# You should have received a copy of the GNU General Public License
523b37e3 2638+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2639+
2640+Dynamically customizable FS operations
2641+----------------------------------------------------------------------
2642+Generally FS operations (struct inode_operations, struct
2643+address_space_operations, struct file_operations, etc.) are defined as
2644+"static const", but it never means that FS have only one set of
2645+operation. Some FS have multiple sets of them. For instance, ext2 has
2646+three sets, one for XIP, for NOBH, and for normal.
2647+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2648+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2649+VFS acts differently if a function (member in the struct) is set or
2650+not. It means aufs should have several sets of operations and select one
2651+among them according to the branch FS definition.
2652+
7e9cd9fe 2653+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2654+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2655+dummy direct_IO function for struct address_space_operations, but it may
2656+not be set to the address_space_operations actually. When the branch FS
2657+doesn't have it, aufs doesn't set it to its address_space_operations
2658+while the function definition itself is still alive. So the behaviour
2659+itself will not change, and it will return an error when direct_IO is
2660+not set.
53392da6
AM
2661+
2662+The lifetime of these dynamically generated operation object is
2663+maintained by aufs branch object. When the branch is removed from aufs,
2664+the reference counter of the object is decremented. When it reaches
2665+zero, the dynamically generated operation object will be freed.
2666+
7e9cd9fe
AM
2667+This approach is designed to support AIO (io_submit), Direct I/O and
2668+XIP (DAX) mainly.
2669+Currently this approach is applied to address_space_operations for
2670+regular files only.
53392da6
AM
2671diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2672--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
b00004a5 2673+++ linux/Documentation/filesystems/aufs/README 2017-07-29 12:14:25.893041746 +0200
f2c43d5f 2674@@ -0,0 +1,393 @@
53392da6 2675+
5527c038 2676+Aufs4 -- advanced multi layered unification filesystem version 4.x
53392da6
AM
2677+http://aufs.sf.net
2678+Junjiro R. Okajima
2679+
2680+
2681+0. Introduction
2682+----------------------------------------
2683+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2684+Unionfs Version 1.x series. Adding many original ideas, approaches,
53392da6
AM
2685+improvements and implementations, it becomes totally different from
2686+Unionfs while keeping the basic features.
2687+Recently, Unionfs Version 2.x series begin taking some of the same
2688+approaches to aufs1's.
2689+Unionfs is being developed by Professor Erez Zadok at Stony Brook
2690+University and his team.
2691+
5527c038 2692+Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
53392da6
AM
2693+If you want older kernel version support, try aufs2-2.6.git or
2694+aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2695+
2696+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2697+ According to Christoph Hellwig, linux rejects all union-type
2698+ filesystems but UnionMount.
53392da6
AM
2699+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2700+
38d290e6
JR
2701+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2702+ UnionMount, and he pointed out an issue around a directory mutex
2703+ lock and aufs addressed it. But it is still unsure whether aufs will
2704+ be merged (or any other union solution).
076b876e 2705+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2706+
53392da6
AM
2707+
2708+1. Features
2709+----------------------------------------
2710+- unite several directories into a single virtual filesystem. The member
2711+ directory is called as a branch.
2712+- you can specify the permission flags to the branch, which are 'readonly',
2713+ 'readwrite' and 'whiteout-able.'
2714+- by upper writable branch, internal copyup and whiteout, files/dirs on
2715+ readonly branch are modifiable logically.
2716+- dynamic branch manipulation, add, del.
2717+- etc...
2718+
7e9cd9fe
AM
2719+Also there are many enhancements in aufs, such as:
2720+- test only the highest one for the directory permission (dirperm1)
2721+- copyup on open (coo=)
2722+- 'move' policy for copy-up between two writable branches, after
2723+ checking free space.
2724+- xattr, acl
53392da6
AM
2725+- readdir(3) in userspace.
2726+- keep inode number by external inode number table
2727+- keep the timestamps of file/dir in internal copyup operation
2728+- seekable directory, supporting NFS readdir.
2729+- whiteout is hardlinked in order to reduce the consumption of inodes
2730+ on branch
2731+- do not copyup, nor create a whiteout when it is unnecessary
2732+- revert a single systemcall when an error occurs in aufs
2733+- remount interface instead of ioctl
2734+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2735+- loopback mounted filesystem as a branch
2736+- kernel thread for removing the dir who has a plenty of whiteouts
2737+- support copyup sparse file (a file which has a 'hole' in it)
2738+- default permission flags for branches
2739+- selectable permission flags for ro branch, whether whiteout can
2740+ exist or not
2741+- export via NFS.
2742+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2743+- support multiple writable branches, some policies to select one
2744+ among multiple writable branches.
2745+- a new semantics for link(2) and rename(2) to support multiple
2746+ writable branches.
2747+- no glibc changes are required.
2748+- pseudo hardlink (hardlink over branches)
2749+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2750+ including NFS or remote filesystem branch.
2751+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2752+- and more...
2753+
5527c038 2754+Currently these features are dropped temporary from aufs4.
53392da6 2755+See design/08plan.txt in detail.
53392da6
AM
2756+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2757+ (robr)
2758+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2759+
2760+Features or just an idea in the future (see also design/*.txt),
2761+- reorder the branch index without del/re-add.
2762+- permanent xino files for NFSD
2763+- an option for refreshing the opened files after add/del branches
53392da6
AM
2764+- light version, without branch manipulation. (unnecessary?)
2765+- copyup in userspace
2766+- inotify in userspace
2767+- readv/writev
53392da6
AM
2768+
2769+
2770+2. Download
2771+----------------------------------------
5527c038
JR
2772+There are three GIT trees for aufs4, aufs4-linux.git,
2773+aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
1e00d052 2774+"aufs-util.git."
5527c038
JR
2775+While the aufs-util is always necessary, you need either of aufs4-linux
2776+or aufs4-standalone.
1e00d052 2777+
5527c038 2778+The aufs4-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2779+git://git.kernel.org/.../torvalds/linux.git.
2780+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
5527c038 2781+build aufs4 as an external kernel module.
2000de60 2782+Several extra patches are not included in this tree. Only
be52b249 2783+aufs4-standalone tree contains them. They are described in the later
2000de60 2784+section "Configuration and Compilation."
1e00d052 2785+
5527c038 2786+On the other hand, the aufs4-standalone tree has only aufs source files
53392da6 2787+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2788+But you need to apply all aufs patches manually.
53392da6 2789+
5527c038
JR
2790+You will find GIT branches whose name is in form of "aufs4.x" where "x"
2791+represents the linux kernel version, "linux-4.x". For instance,
2792+"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2793+"aufs4.x-rcN" branch.
1e00d052 2794+
5527c038 2795+o aufs4-linux tree
1e00d052 2796+$ git clone --reference /your/linux/git/tree \
5527c038 2797+ git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
1e00d052 2798+- if you don't have linux GIT tree, then remove "--reference ..."
5527c038
JR
2799+$ cd aufs4-linux.git
2800+$ git checkout origin/aufs4.0
53392da6 2801+
2000de60
JR
2802+Or You may want to directly git-pull aufs into your linux GIT tree, and
2803+leave the patch-work to GIT.
2804+$ cd /your/linux/git/tree
5527c038
JR
2805+$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2806+$ git fetch aufs4
2807+$ git checkout -b my4.0 v4.0
2808+$ (add your local change...)
2809+$ git pull aufs4 aufs4.0
2810+- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2000de60 2811+- you may need to solve some conflicts between your_changes and
5527c038
JR
2812+ aufs4.0. in this case, git-rerere is recommended so that you can
2813+ solve the similar conflicts automatically when you upgrade to 4.1 or
2000de60
JR
2814+ later in the future.
2815+
5527c038
JR
2816+o aufs4-standalone tree
2817+$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2818+$ cd aufs4-standalone.git
2819+$ git checkout origin/aufs4.0
53392da6
AM
2820+
2821+o aufs-util tree
5527c038
JR
2822+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2823+- note that the public aufs-util.git is on SourceForge instead of
2824+ GitHUB.
53392da6 2825+$ cd aufs-util.git
5527c038 2826+$ git checkout origin/aufs4.0
53392da6 2827+
5527c038
JR
2828+Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2829+The minor version number, 'x' in '4.x', of aufs may not always
9dbd164d
AM
2830+follow the minor version number of the kernel.
2831+Because changes in the kernel that cause the use of a new
2832+minor version number do not always require changes to aufs-util.
2833+
2834+Since aufs-util has its own minor version number, you may not be
2835+able to find a GIT branch in aufs-util for your kernel's
2836+exact minor version number.
2837+In this case, you should git-checkout the branch for the
53392da6 2838+nearest lower number.
9dbd164d
AM
2839+
2840+For (an unreleased) example:
5527c038
JR
2841+If you are using "linux-4.10" and the "aufs4.10" branch
2842+does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
9dbd164d
AM
2843+or something numerically smaller is the branch for your kernel.
2844+
53392da6
AM
2845+Also you can view all branches by
2846+ $ git branch -a
2847+
2848+
2849+3. Configuration and Compilation
2850+----------------------------------------
2851+Make sure you have git-checkout'ed the correct branch.
2852+
5527c038 2853+For aufs4-linux tree,
c06a8ce3 2854+- enable CONFIG_AUFS_FS.
1e00d052
AM
2855+- set other aufs configurations if necessary.
2856+
5527c038 2857+For aufs4-standalone tree,
53392da6
AM
2858+There are several ways to build.
2859+
2860+1.
5527c038
JR
2861+- apply ./aufs4-kbuild.patch to your kernel source files.
2862+- apply ./aufs4-base.patch too.
2863+- apply ./aufs4-mmap.patch too.
2864+- apply ./aufs4-standalone.patch too, if you have a plan to set
2865+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
537831f9
AM
2866+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2867+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2868+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2869+ =m or =y.
2870+- and build your kernel as usual.
2871+- install the built kernel.
c06a8ce3
AM
2872+ Note: Since linux-3.9, every filesystem module requires an alias
2873+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2874+ modules.aliases file if you set CONFIG_AUFS_FS=m.
7eafdf33
AM
2875+- install the header files too by "make headers_install" to the
2876+ directory where you specify. By default, it is $PWD/usr.
b4510431 2877+ "make help" shows a brief note for headers_install.
53392da6
AM
2878+- and reboot your system.
2879+
2880+2.
2881+- module only (CONFIG_AUFS_FS=m).
5527c038
JR
2882+- apply ./aufs4-base.patch to your kernel source files.
2883+- apply ./aufs4-mmap.patch too.
2884+- apply ./aufs4-standalone.patch too.
53392da6
AM
2885+- build your kernel, don't forget "make headers_install", and reboot.
2886+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2887+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2888+ every aufs configurations.
2889+- build the module by simple "make".
c06a8ce3
AM
2890+ Note: Since linux-3.9, every filesystem module requires an alias
2891+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2892+ modules.aliases file.
53392da6
AM
2893+- you can specify ${KDIR} make variable which points to your kernel
2894+ source tree.
2895+- install the files
2896+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2897+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2898+ + run "make install_headers" (instead of headers_install) to install
2899+ the modified aufs header file (you can specify DESTDIR which is
2900+ available in aufs standalone version's Makefile only), or copy
2901+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2902+ you like manually. By default, the target directory is $PWD/usr.
5527c038 2903+- no need to apply aufs4-kbuild.patch, nor copying source files to your
53392da6
AM
2904+ kernel source tree.
2905+
b4510431 2906+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2907+ as well as "make headers_install" in the kernel source tree.
2908+ headers_install is subject to be forgotten, but it is essentially
2909+ necessary, not only for building aufs-util.
2910+ You may not meet problems without headers_install in some older
2911+ version though.
2912+
2913+And then,
2914+- read README in aufs-util, build and install it
9dbd164d
AM
2915+- note that your distribution may contain an obsoleted version of
2916+ aufs_type.h in /usr/include/linux or something. When you build aufs
2917+ utilities, make sure that your compiler refers the correct aufs header
2918+ file which is built by "make headers_install."
53392da6
AM
2919+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2920+ then run "make install_ulib" too. And refer to the aufs manual in
2921+ detail.
2922+
5527c038 2923+There several other patches in aufs4-standalone.git. They are all
38d290e6 2924+optional. When you meet some problems, they will help you.
5527c038 2925+- aufs4-loopback.patch
38d290e6
JR
2926+ Supports a nested loopback mount in a branch-fs. This patch is
2927+ unnecessary until aufs produces a message like "you may want to try
2928+ another patch for loopback file".
2929+- vfs-ino.patch
2930+ Modifies a system global kernel internal function get_next_ino() in
2931+ order to stop assigning 0 for an inode-number. Not directly related to
2932+ aufs, but recommended generally.
2933+- tmpfs-idr.patch
2934+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2935+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2936+ duplication of inode number, which is important for backup tools and
2937+ other utilities. When you find aufs XINO files for tmpfs branch
2938+ growing too much, try this patch.
be52b249
AM
2939+- lockdep-debug.patch
2940+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2941+ also a caller of VFS functions for branch filesystems, subclassing of
2942+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2943+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2944+ need to apply this debug patch to expand several constant values.
2945+ If don't know what LOCKDEP, then you don't have apply this patch.
38d290e6 2946+
53392da6
AM
2947+
2948+4. Usage
2949+----------------------------------------
2950+At first, make sure aufs-util are installed, and please read the aufs
2951+manual, aufs.5 in aufs-util.git tree.
2952+$ man -l aufs.5
2953+
2954+And then,
2955+$ mkdir /tmp/rw /tmp/aufs
2956+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2957+
2958+Here is another example. The result is equivalent.
2959+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2960+ Or
2961+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2962+# mount -o remount,append:${HOME} /tmp/aufs
2963+
2964+Then, you can see whole tree of your home dir through /tmp/aufs. If
2965+you modify a file under /tmp/aufs, the one on your home directory is
2966+not affected, instead the same named file will be newly created under
2967+/tmp/rw. And all of your modification to a file will be applied to
2968+the one under /tmp/rw. This is called the file based Copy on Write
2969+(COW) method.
2970+Aufs mount options are described in aufs.5.
2971+If you run chroot or something and make your aufs as a root directory,
2972+then you need to customize the shutdown script. See the aufs manual in
2973+detail.
2974+
2975+Additionally, there are some sample usages of aufs which are a
2976+diskless system with network booting, and LiveCD over NFS.
2977+See sample dir in CVS tree on SourceForge.
2978+
2979+
2980+5. Contact
2981+----------------------------------------
2982+When you have any problems or strange behaviour in aufs, please let me
2983+know with:
2984+- /proc/mounts (instead of the output of mount(8))
2985+- /sys/module/aufs/*
2986+- /sys/fs/aufs/* (if you have them)
2987+- /debug/aufs/* (if you have them)
2988+- linux kernel version
2989+ if your kernel is not plain, for example modified by distributor,
2990+ the url where i can download its source is necessary too.
2991+- aufs version which was printed at loading the module or booting the
2992+ system, instead of the date you downloaded.
2993+- configuration (define/undefine CONFIG_AUFS_xxx)
2994+- kernel configuration or /proc/config.gz (if you have it)
2995+- behaviour which you think to be incorrect
2996+- actual operation, reproducible one is better
2997+- mailto: aufs-users at lists.sourceforge.net
2998+
2999+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
3000+and Feature Requests) on SourceForge. Please join and write to
3001+aufs-users ML.
3002+
3003+
3004+6. Acknowledgements
3005+----------------------------------------
3006+Thanks to everyone who have tried and are using aufs, whoever
3007+have reported a bug or any feedback.
3008+
3009+Especially donators:
3010+Tomas Matejicek(slax.org) made a donation (much more than once).
3011+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
3012+ scripts) is making "doubling" donations.
3013+ Unfortunately I cannot list all of the donators, but I really
b4510431 3014+ appreciate.
53392da6
AM
3015+ It ends Aug 2010, but the ordinary donation URL is still available.
3016+ <http://sourceforge.net/donate/index.php?group_id=167503>
3017+Dai Itasaka made a donation (2007/8).
3018+Chuck Smith made a donation (2008/4, 10 and 12).
3019+Henk Schoneveld made a donation (2008/9).
3020+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
3021+Francois Dupoux made a donation (2008/11).
3022+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
3023+ aufs2 GIT tree (2009/2).
3024+William Grant made a donation (2009/3).
3025+Patrick Lane made a donation (2009/4).
3026+The Mail Archive (mail-archive.com) made donations (2009/5).
3027+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
3028+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
3029+Pavel Pronskiy made a donation (2011/2).
3030+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
3031+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
3032+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
3033+11).
1e00d052 3034+Sam Liddicott made a donation (2011/9).
86dc4139
AM
3035+Era Scarecrow made a donation (2013/4).
3036+Bor Ratajc made a donation (2013/4).
3037+Alessandro Gorreta made a donation (2013/4).
3038+POIRETTE Marc made a donation (2013/4).
3039+Alessandro Gorreta made a donation (2013/4).
3040+lauri kasvandik made a donation (2013/5).
392086de 3041+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
3042+The Parted Magic Project made a donation (2013/9 and 11).
3043+Pavel Barta made a donation (2013/10).
38d290e6 3044+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 3045+James B made a donation (2014/7 and 2015/7).
076b876e 3046+Stefano Di Biase made a donation (2014/8).
2000de60 3047+Daniel Epellei made a donation (2015/1).
8cdd5066 3048+OmegaPhil made a donation (2016/1).
5afbbe0d 3049+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 3050+James Burry made a donation (2016/12).
53392da6
AM
3051+
3052+Thank you very much.
3053+Donations are always, including future donations, very important and
3054+helpful for me to keep on developing aufs.
3055+
3056+
3057+7.
3058+----------------------------------------
3059+If you are an experienced user, no explanation is needed. Aufs is
3060+just a linux filesystem.
3061+
3062+
3063+Enjoy!
3064+
3065+# Local variables: ;
3066+# mode: text;
3067+# End: ;
7f207e10
AM
3068diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
3069--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 3070+++ linux/fs/aufs/aufs.h 2018-04-15 08:49:13.394483860 +0200
8b6a4947 3071@@ -0,0 +1,60 @@
7f207e10 3072+/*
b00004a5 3073+ * Copyright (C) 2005-2018 Junjiro R. Okajima
7f207e10
AM
3074+ *
3075+ * This program, aufs is free software; you can redistribute it and/or modify
3076+ * it under the terms of the GNU General Public License as published by
3077+ * the Free Software Foundation; either version 2 of the License, or
3078+ * (at your option) any later version.
3079+ *
3080+ * This program is distributed in the hope that it will be useful,
3081+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3082+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3083+ * GNU General Public License for more details.
3084+ *
3085+ * You should have received a copy of the GNU General Public License
523b37e3 3086+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3087+ */
3088+
3089+/*
3090+ * all header files
3091+ */
3092+
3093+#ifndef __AUFS_H__
3094+#define __AUFS_H__
3095+
3096+#ifdef __KERNEL__
3097+
3098+#define AuStub(type, name, body, ...) \
3099+ static inline type name(__VA_ARGS__) { body; }
3100+
3101+#define AuStubVoid(name, ...) \
3102+ AuStub(void, name, , __VA_ARGS__)
3103+#define AuStubInt0(name, ...) \
3104+ AuStub(int, name, return 0, __VA_ARGS__)
3105+
3106+#include "debug.h"
3107+
3108+#include "branch.h"
3109+#include "cpup.h"
3110+#include "dcsub.h"
3111+#include "dbgaufs.h"
3112+#include "dentry.h"
3113+#include "dir.h"
8b6a4947 3114+#include "dirren.h"
7f207e10
AM
3115+#include "dynop.h"
3116+#include "file.h"
3117+#include "fstype.h"
8b6a4947 3118+#include "hbl.h"
7f207e10
AM
3119+#include "inode.h"
3120+#include "loop.h"
3121+#include "module.h"
7f207e10
AM
3122+#include "opts.h"
3123+#include "rwsem.h"
7f207e10
AM
3124+#include "super.h"
3125+#include "sysaufs.h"
3126+#include "vfsub.h"
3127+#include "whout.h"
3128+#include "wkq.h"
3129+
3130+#endif /* __KERNEL__ */
3131+#endif /* __AUFS_H__ */
3132diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3133--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 3134+++ linux/fs/aufs/branch.c 2018-04-15 08:49:13.394483860 +0200
8b6a4947 3135@@ -0,0 +1,1432 @@
7f207e10 3136+/*
b00004a5 3137+ * Copyright (C) 2005-2018 Junjiro R. Okajima
7f207e10
AM
3138+ *
3139+ * This program, aufs is free software; you can redistribute it and/or modify
3140+ * it under the terms of the GNU General Public License as published by
3141+ * the Free Software Foundation; either version 2 of the License, or
3142+ * (at your option) any later version.
3143+ *
3144+ * This program is distributed in the hope that it will be useful,
3145+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3146+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3147+ * GNU General Public License for more details.
3148+ *
3149+ * You should have received a copy of the GNU General Public License
523b37e3 3150+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3151+ */
3152+
3153+/*
3154+ * branch management
3155+ */
3156+
027c5e7a 3157+#include <linux/compat.h>
7f207e10
AM
3158+#include <linux/statfs.h>
3159+#include "aufs.h"
3160+
3161+/*
3162+ * free a single branch
1facf9fc 3163+ */
3164+static void au_br_do_free(struct au_branch *br)
3165+{
3166+ int i;
3167+ struct au_wbr *wbr;
4a4d8108 3168+ struct au_dykey **key;
1facf9fc 3169+
027c5e7a 3170+ au_hnotify_fin_br(br);
8b6a4947
AM
3171+ /* always, regardless the mount option */
3172+ au_dr_hino_free(&br->br_dirren);
027c5e7a 3173+
1facf9fc 3174+ if (br->br_xino.xi_file)
3175+ fput(br->br_xino.xi_file);
521ced18
JR
3176+ for (i = br->br_xino.xi_nondir.total - 1; i >= 0; i--)
3177+ AuDebugOn(br->br_xino.xi_nondir.array[i]);
1c60b727 3178+ kfree(br->br_xino.xi_nondir.array);
1facf9fc 3179+
5afbbe0d
AM
3180+ AuDebugOn(au_br_count(br));
3181+ au_br_count_fin(br);
1facf9fc 3182+
3183+ wbr = br->br_wbr;
3184+ if (wbr) {
3185+ for (i = 0; i < AuBrWh_Last; i++)
3186+ dput(wbr->wbr_wh[i]);
3187+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 3188+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 3189+ }
3190+
076b876e
AM
3191+ if (br->br_fhsm) {
3192+ au_br_fhsm_fin(br->br_fhsm);
1c60b727 3193+ kfree(br->br_fhsm);
076b876e
AM
3194+ }
3195+
4a4d8108
AM
3196+ key = br->br_dykey;
3197+ for (i = 0; i < AuBrDynOp; i++, key++)
3198+ if (*key)
3199+ au_dy_put(*key);
3200+ else
3201+ break;
3202+
537831f9
AM
3203+ /* recursive lock, s_umount of branch's */
3204+ lockdep_off();
86dc4139 3205+ path_put(&br->br_path);
537831f9 3206+ lockdep_on();
1c60b727
AM
3207+ kfree(wbr);
3208+ kfree(br);
1facf9fc 3209+}
3210+
3211+/*
3212+ * frees all branches
3213+ */
3214+void au_br_free(struct au_sbinfo *sbinfo)
3215+{
3216+ aufs_bindex_t bmax;
3217+ struct au_branch **br;
3218+
dece6358
AM
3219+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3220+
5afbbe0d 3221+ bmax = sbinfo->si_bbot + 1;
1facf9fc 3222+ br = sbinfo->si_branch;
3223+ while (bmax--)
3224+ au_br_do_free(*br++);
3225+}
3226+
3227+/*
3228+ * find the index of a branch which is specified by @br_id.
3229+ */
3230+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3231+{
5afbbe0d 3232+ aufs_bindex_t bindex, bbot;
1facf9fc 3233+
5afbbe0d
AM
3234+ bbot = au_sbbot(sb);
3235+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3236+ if (au_sbr_id(sb, bindex) == br_id)
3237+ return bindex;
3238+ return -1;
3239+}
3240+
3241+/* ---------------------------------------------------------------------- */
3242+
3243+/*
3244+ * add a branch
3245+ */
3246+
b752ccd1
AM
3247+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3248+ struct dentry *h_root)
1facf9fc 3249+{
b752ccd1
AM
3250+ if (unlikely(h_adding == h_root
3251+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3252+ return 1;
b752ccd1
AM
3253+ if (h_adding->d_sb != h_root->d_sb)
3254+ return 0;
3255+ return au_test_subdir(h_adding, h_root)
3256+ || au_test_subdir(h_root, h_adding);
1facf9fc 3257+}
3258+
3259+/*
3260+ * returns a newly allocated branch. @new_nbranch is a number of branches
3261+ * after adding a branch.
3262+ */
3263+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3264+ int perm)
3265+{
3266+ struct au_branch *add_branch;
3267+ struct dentry *root;
5527c038 3268+ struct inode *inode;
4a4d8108 3269+ int err;
1facf9fc 3270+
4a4d8108 3271+ err = -ENOMEM;
be52b249 3272+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3273+ if (unlikely(!add_branch))
3274+ goto out;
521ced18
JR
3275+ add_branch->br_xino.xi_nondir.total = 8; /* initial size */
3276+ add_branch->br_xino.xi_nondir.array
8b6a4947 3277+ = kcalloc(add_branch->br_xino.xi_nondir.total, sizeof(ino_t),
521ced18
JR
3278+ GFP_NOFS);
3279+ if (unlikely(!add_branch->br_xino.xi_nondir.array))
3280+ goto out_br;
1facf9fc 3281+
027c5e7a
AM
3282+ err = au_hnotify_init_br(add_branch, perm);
3283+ if (unlikely(err))
521ced18 3284+ goto out_xinondir;
027c5e7a 3285+
1facf9fc 3286+ if (au_br_writable(perm)) {
3287+ /* may be freed separately at changing the branch permission */
be52b249 3288+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3289+ GFP_NOFS);
3290+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3291+ goto out_hnotify;
1facf9fc 3292+ }
3293+
076b876e
AM
3294+ if (au_br_fhsm(perm)) {
3295+ err = au_fhsm_br_alloc(add_branch);
3296+ if (unlikely(err))
3297+ goto out_wbr;
3298+ }
3299+
521ced18 3300+ root = sb->s_root;
e2f27e51 3301+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3302+ if (!err)
e2f27e51 3303+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3304+ if (!err) {
3305+ inode = d_inode(root);
1c60b727
AM
3306+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3307+ /*may_shrink*/0);
5527c038 3308+ }
4a4d8108
AM
3309+ if (!err)
3310+ return add_branch; /* success */
1facf9fc 3311+
076b876e 3312+out_wbr:
1c60b727 3313+ kfree(add_branch->br_wbr);
027c5e7a
AM
3314+out_hnotify:
3315+ au_hnotify_fin_br(add_branch);
521ced18 3316+out_xinondir:
1c60b727 3317+ kfree(add_branch->br_xino.xi_nondir.array);
4f0767ce 3318+out_br:
1c60b727 3319+ kfree(add_branch);
4f0767ce 3320+out:
4a4d8108 3321+ return ERR_PTR(err);
1facf9fc 3322+}
3323+
3324+/*
3325+ * test if the branch permission is legal or not.
3326+ */
3327+static int test_br(struct inode *inode, int brperm, char *path)
3328+{
3329+ int err;
3330+
4a4d8108
AM
3331+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3332+ if (!err)
3333+ goto out;
1facf9fc 3334+
4a4d8108
AM
3335+ err = -EINVAL;
3336+ pr_err("write permission for readonly mount or inode, %s\n", path);
3337+
4f0767ce 3338+out:
1facf9fc 3339+ return err;
3340+}
3341+
3342+/*
3343+ * returns:
3344+ * 0: success, the caller will add it
3345+ * plus: success, it is already unified, the caller should ignore it
3346+ * minus: error
3347+ */
3348+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3349+{
3350+ int err;
5afbbe0d 3351+ aufs_bindex_t bbot, bindex;
5527c038 3352+ struct dentry *root, *h_dentry;
1facf9fc 3353+ struct inode *inode, *h_inode;
3354+
3355+ root = sb->s_root;
5afbbe0d
AM
3356+ bbot = au_sbbot(sb);
3357+ if (unlikely(bbot >= 0
1facf9fc 3358+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3359+ err = 1;
3360+ if (!remount) {
3361+ err = -EINVAL;
4a4d8108 3362+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3363+ }
3364+ goto out;
3365+ }
3366+
3367+ err = -ENOSPC; /* -E2BIG; */
3368+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3369+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3370+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3371+ goto out;
3372+ }
3373+
3374+ err = -EDOM;
5afbbe0d 3375+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3376+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3377+ goto out;
3378+ }
3379+
5527c038 3380+ inode = d_inode(add->path.dentry);
1facf9fc 3381+ err = -ENOENT;
3382+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3383+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3384+ goto out;
3385+ }
3386+
3387+ err = -EINVAL;
3388+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3389+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3390+ goto out;
3391+ }
3392+
3393+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3394+ pr_err("unsupported filesystem, %s (%s)\n",
3395+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3396+ goto out;
3397+ }
3398+
c1595e42
JR
3399+ if (unlikely(inode->i_sb->s_stack_depth)) {
3400+ pr_err("already stacked, %s (%s)\n",
3401+ add->pathname, au_sbtype(inode->i_sb));
3402+ goto out;
3403+ }
3404+
5527c038 3405+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3406+ if (unlikely(err))
3407+ goto out;
3408+
5afbbe0d 3409+ if (bbot < 0)
1facf9fc 3410+ return 0; /* success */
3411+
3412+ err = -EINVAL;
5afbbe0d 3413+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3414+ if (unlikely(test_overlap(sb, add->path.dentry,
3415+ au_h_dptr(root, bindex)))) {
4a4d8108 3416+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3417+ goto out;
3418+ }
3419+
3420+ err = 0;
3421+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3422+ h_dentry = au_h_dptr(root, 0);
3423+ h_inode = d_inode(h_dentry);
1facf9fc 3424+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3425+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3426+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3427+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3428+ add->pathname,
3429+ i_uid_read(inode), i_gid_read(inode),
3430+ (inode->i_mode & S_IALLUGO),
3431+ i_uid_read(h_inode), i_gid_read(h_inode),
3432+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3433+ }
3434+
4f0767ce 3435+out:
1facf9fc 3436+ return err;
3437+}
3438+
3439+/*
3440+ * initialize or clean the whiteouts for an adding branch
3441+ */
3442+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3443+ int new_perm)
1facf9fc 3444+{
3445+ int err, old_perm;
3446+ aufs_bindex_t bindex;
febd17d6 3447+ struct inode *h_inode;
1facf9fc 3448+ struct au_wbr *wbr;
3449+ struct au_hinode *hdir;
5527c038 3450+ struct dentry *h_dentry;
1facf9fc 3451+
86dc4139
AM
3452+ err = vfsub_mnt_want_write(au_br_mnt(br));
3453+ if (unlikely(err))
3454+ goto out;
3455+
1facf9fc 3456+ wbr = br->br_wbr;
3457+ old_perm = br->br_perm;
3458+ br->br_perm = new_perm;
3459+ hdir = NULL;
febd17d6 3460+ h_inode = NULL;
1facf9fc 3461+ bindex = au_br_index(sb, br->br_id);
3462+ if (0 <= bindex) {
5527c038 3463+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3464+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3465+ } else {
5527c038 3466+ h_dentry = au_br_dentry(br);
febd17d6
JR
3467+ h_inode = d_inode(h_dentry);
3468+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3469+ }
3470+ if (!wbr)
86dc4139 3471+ err = au_wh_init(br, sb);
1facf9fc 3472+ else {
3473+ wbr_wh_write_lock(wbr);
86dc4139 3474+ err = au_wh_init(br, sb);
1facf9fc 3475+ wbr_wh_write_unlock(wbr);
3476+ }
3477+ if (hdir)
5afbbe0d 3478+ au_hn_inode_unlock(hdir);
1facf9fc 3479+ else
febd17d6 3480+ inode_unlock(h_inode);
86dc4139 3481+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3482+ br->br_perm = old_perm;
3483+
3484+ if (!err && wbr && !au_br_writable(new_perm)) {
1c60b727 3485+ kfree(wbr);
1facf9fc 3486+ br->br_wbr = NULL;
3487+ }
3488+
86dc4139 3489+out:
1facf9fc 3490+ return err;
3491+}
3492+
3493+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3494+ int perm)
1facf9fc 3495+{
3496+ int err;
4a4d8108 3497+ struct kstatfs kst;
1facf9fc 3498+ struct au_wbr *wbr;
3499+
3500+ wbr = br->br_wbr;
dece6358 3501+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3502+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3503+
4a4d8108
AM
3504+ /*
3505+ * a limit for rmdir/rename a dir
523b37e3 3506+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3507+ */
86dc4139 3508+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3509+ if (unlikely(err))
3510+ goto out;
3511+ err = -EINVAL;
3512+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3513+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3514+ else
523b37e3
AM
3515+ pr_err("%pd(%s), unsupported namelen %ld\n",
3516+ au_br_dentry(br),
86dc4139 3517+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3518+
4f0767ce 3519+out:
1facf9fc 3520+ return err;
3521+}
3522+
c1595e42 3523+/* initialize a new branch */
1facf9fc 3524+static int au_br_init(struct au_branch *br, struct super_block *sb,
3525+ struct au_opt_add *add)
3526+{
3527+ int err;
5527c038 3528+ struct inode *h_inode;
1facf9fc 3529+
3530+ err = 0;
521ced18
JR
3531+ spin_lock_init(&br->br_xino.xi_nondir.spin);
3532+ init_waitqueue_head(&br->br_xino.xi_nondir.wqh);
1facf9fc 3533+ br->br_perm = add->perm;
86dc4139 3534+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3535+ spin_lock_init(&br->br_dykey_lock);
5afbbe0d 3536+ au_br_count_init(br);
1facf9fc 3537+ atomic_set(&br->br_xino_running, 0);
3538+ br->br_id = au_new_br_id(sb);
7f207e10 3539+ AuDebugOn(br->br_id < 0);
1facf9fc 3540+
8b6a4947
AM
3541+ /* always, regardless the given option */
3542+ err = au_dr_br_init(sb, br, &add->path);
3543+ if (unlikely(err))
3544+ goto out_err;
3545+
1facf9fc 3546+ if (au_br_writable(add->perm)) {
86dc4139 3547+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3548+ if (unlikely(err))
b752ccd1 3549+ goto out_err;
1facf9fc 3550+ }
3551+
3552+ if (au_opt_test(au_mntflags(sb), XINO)) {
5527c038
JR
3553+ h_inode = d_inode(add->path.dentry);
3554+ err = au_xino_br(sb, br, h_inode->i_ino,
1facf9fc 3555+ au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3556+ if (unlikely(err)) {
3557+ AuDebugOn(br->br_xino.xi_file);
b752ccd1 3558+ goto out_err;
1facf9fc 3559+ }
3560+ }
3561+
3562+ sysaufs_br_init(br);
86dc4139 3563+ path_get(&br->br_path);
b752ccd1 3564+ goto out; /* success */
1facf9fc 3565+
4f0767ce 3566+out_err:
86dc4139 3567+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3568+out:
1facf9fc 3569+ return err;
3570+}
3571+
3572+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3573+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3574+ aufs_bindex_t amount)
3575+{
3576+ struct au_branch **brp;
3577+
dece6358
AM
3578+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3579+
1facf9fc 3580+ brp = sbinfo->si_branch + bindex;
3581+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3582+ *brp = br;
5afbbe0d
AM
3583+ sbinfo->si_bbot++;
3584+ if (unlikely(bbot < 0))
3585+ sbinfo->si_bbot = 0;
1facf9fc 3586+}
3587+
3588+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3589+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3590+{
3591+ struct au_hdentry *hdp;
3592+
1308ab2a 3593+ AuRwMustWriteLock(&dinfo->di_rwsem);
3594+
5afbbe0d 3595+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3596+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3597+ au_h_dentry_init(hdp);
5afbbe0d
AM
3598+ dinfo->di_bbot++;
3599+ if (unlikely(bbot < 0))
3600+ dinfo->di_btop = 0;
1facf9fc 3601+}
3602+
3603+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3604+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3605+{
3606+ struct au_hinode *hip;
3607+
1308ab2a 3608+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3609+
5afbbe0d 3610+ hip = au_hinode(iinfo, bindex);
1facf9fc 3611+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3612+ au_hinode_init(hip);
3613+ iinfo->ii_bbot++;
3614+ if (unlikely(bbot < 0))
3615+ iinfo->ii_btop = 0;
1facf9fc 3616+}
3617+
86dc4139
AM
3618+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3619+ aufs_bindex_t bindex)
1facf9fc 3620+{
86dc4139 3621+ struct dentry *root, *h_dentry;
5527c038 3622+ struct inode *root_inode, *h_inode;
5afbbe0d 3623+ aufs_bindex_t bbot, amount;
1facf9fc 3624+
3625+ root = sb->s_root;
5527c038 3626+ root_inode = d_inode(root);
5afbbe0d
AM
3627+ bbot = au_sbbot(sb);
3628+ amount = bbot + 1 - bindex;
86dc4139 3629+ h_dentry = au_br_dentry(br);
53392da6 3630+ au_sbilist_lock();
5afbbe0d
AM
3631+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3632+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3633+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3634+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3635+ h_inode = d_inode(h_dentry);
3636+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3637+ au_sbilist_unlock();
1facf9fc 3638+}
3639+
3640+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3641+{
3642+ int err;
5afbbe0d 3643+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3644+ struct dentry *root, *h_dentry;
3645+ struct inode *root_inode;
3646+ struct au_branch *add_branch;
3647+
3648+ root = sb->s_root;
5527c038 3649+ root_inode = d_inode(root);
1facf9fc 3650+ IMustLock(root_inode);
5afbbe0d 3651+ IiMustWriteLock(root_inode);
1facf9fc 3652+ err = test_add(sb, add, remount);
3653+ if (unlikely(err < 0))
3654+ goto out;
3655+ if (err) {
3656+ err = 0;
3657+ goto out; /* success */
3658+ }
3659+
5afbbe0d
AM
3660+ bbot = au_sbbot(sb);
3661+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3662+ err = PTR_ERR(add_branch);
3663+ if (IS_ERR(add_branch))
3664+ goto out;
3665+
3666+ err = au_br_init(add_branch, sb, add);
3667+ if (unlikely(err)) {
3668+ au_br_do_free(add_branch);
3669+ goto out;
3670+ }
3671+
3672+ add_bindex = add->bindex;
1facf9fc 3673+ if (!remount)
86dc4139 3674+ au_br_do_add(sb, add_branch, add_bindex);
1facf9fc 3675+ else {
3676+ sysaufs_brs_del(sb, add_bindex);
86dc4139 3677+ au_br_do_add(sb, add_branch, add_bindex);
1facf9fc 3678+ sysaufs_brs_add(sb, add_bindex);
3679+ }
3680+
86dc4139 3681+ h_dentry = add->path.dentry;
1308ab2a 3682+ if (!add_bindex) {
1facf9fc 3683+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3684+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3685+ } else
5527c038 3686+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3687+
3688+ /*
4a4d8108 3689+ * this test/set prevents aufs from handling unnecesary notify events
027c5e7a 3690+ * of xino files, in case of re-adding a writable branch which was
1facf9fc 3691+ * once detached from aufs.
3692+ */
3693+ if (au_xino_brid(sb) < 0
3694+ && au_br_writable(add_branch->br_perm)
3695+ && !au_test_fs_bad_xino(h_dentry->d_sb)
3696+ && add_branch->br_xino.xi_file
2000de60 3697+ && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
1facf9fc 3698+ au_xino_brid_set(sb, add_branch->br_id);
3699+
4f0767ce 3700+out:
1facf9fc 3701+ return err;
3702+}
3703+
3704+/* ---------------------------------------------------------------------- */
3705+
79b8bda9 3706+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3707+ unsigned long long max __maybe_unused,
3708+ void *arg)
3709+{
3710+ unsigned long long n;
3711+ struct file **p, *f;
8b6a4947
AM
3712+ struct hlist_bl_head *files;
3713+ struct hlist_bl_node *pos;
076b876e 3714+ struct au_finfo *finfo;
076b876e
AM
3715+
3716+ n = 0;
3717+ p = a;
3718+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3719+ hlist_bl_lock(files);
3720+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3721+ f = finfo->fi_file;
3722+ if (file_count(f)
3723+ && !special_file(file_inode(f)->i_mode)) {
3724+ get_file(f);
3725+ *p++ = f;
3726+ n++;
3727+ AuDebugOn(n > max);
3728+ }
3729+ }
8b6a4947 3730+ hlist_bl_unlock(files);
076b876e
AM
3731+
3732+ return n;
3733+}
3734+
3735+static struct file **au_farray_alloc(struct super_block *sb,
3736+ unsigned long long *max)
3737+{
5afbbe0d 3738+ *max = au_nfiles(sb);
79b8bda9 3739+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3740+}
3741+
3742+static void au_farray_free(struct file **a, unsigned long long max)
3743+{
3744+ unsigned long long ull;
3745+
3746+ for (ull = 0; ull < max; ull++)
3747+ if (a[ull])
3748+ fput(a[ull]);
be52b249 3749+ kvfree(a);
076b876e
AM
3750+}
3751+
3752+/* ---------------------------------------------------------------------- */
3753+
1facf9fc 3754+/*
3755+ * delete a branch
3756+ */
3757+
3758+/* to show the line number, do not make it inlined function */
4a4d8108 3759+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3760+ if (do_info) \
4a4d8108 3761+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3762+} while (0)
3763+
5afbbe0d
AM
3764+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3765+ aufs_bindex_t bbot)
027c5e7a 3766+{
5afbbe0d 3767+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3768+}
3769+
5afbbe0d
AM
3770+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3771+ aufs_bindex_t bbot)
027c5e7a 3772+{
5afbbe0d 3773+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3774+}
3775+
1facf9fc 3776+/*
3777+ * test if the branch is deletable or not.
3778+ */
3779+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3780+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3781+{
3782+ int err, i, j, ndentry;
5afbbe0d 3783+ aufs_bindex_t btop, bbot;
1facf9fc 3784+ struct au_dcsub_pages dpages;
3785+ struct au_dpage *dpage;
3786+ struct dentry *d;
1facf9fc 3787+
3788+ err = au_dpages_init(&dpages, GFP_NOFS);
3789+ if (unlikely(err))
3790+ goto out;
3791+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3792+ if (unlikely(err))
3793+ goto out_dpages;
3794+
1facf9fc 3795+ for (i = 0; !err && i < dpages.ndpage; i++) {
3796+ dpage = dpages.dpages + i;
3797+ ndentry = dpage->ndentry;
3798+ for (j = 0; !err && j < ndentry; j++) {
3799+ d = dpage->dentries[j];
c1595e42 3800+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3801+ if (!au_digen_test(d, sigen)) {
1facf9fc 3802+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3803+ if (unlikely(au_dbrange_test(d))) {
3804+ di_read_unlock(d, AuLock_IR);
3805+ continue;
3806+ }
3807+ } else {
1facf9fc 3808+ di_write_lock_child(d);
027c5e7a
AM
3809+ if (unlikely(au_dbrange_test(d))) {
3810+ di_write_unlock(d);
3811+ continue;
3812+ }
1facf9fc 3813+ err = au_reval_dpath(d, sigen);
3814+ if (!err)
3815+ di_downgrade_lock(d, AuLock_IR);
3816+ else {
3817+ di_write_unlock(d);
3818+ break;
3819+ }
3820+ }
3821+
027c5e7a 3822+ /* AuDbgDentry(d); */
5afbbe0d
AM
3823+ btop = au_dbtop(d);
3824+ bbot = au_dbbot(d);
3825+ if (btop <= bindex
3826+ && bindex <= bbot
1facf9fc 3827+ && au_h_dptr(d, bindex)
5afbbe0d 3828+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3829+ err = -EBUSY;
523b37e3 3830+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3831+ AuDbgDentry(d);
1facf9fc 3832+ }
3833+ di_read_unlock(d, AuLock_IR);
3834+ }
3835+ }
3836+
4f0767ce 3837+out_dpages:
1facf9fc 3838+ au_dpages_free(&dpages);
4f0767ce 3839+out:
1facf9fc 3840+ return err;
3841+}
3842+
3843+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3844+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3845+{
3846+ int err;
7f207e10
AM
3847+ unsigned long long max, ull;
3848+ struct inode *i, **array;
5afbbe0d 3849+ aufs_bindex_t btop, bbot;
1facf9fc 3850+
7f207e10
AM
3851+ array = au_iarray_alloc(sb, &max);
3852+ err = PTR_ERR(array);
3853+ if (IS_ERR(array))
3854+ goto out;
3855+
1facf9fc 3856+ err = 0;
7f207e10
AM
3857+ AuDbg("b%d\n", bindex);
3858+ for (ull = 0; !err && ull < max; ull++) {
3859+ i = array[ull];
076b876e
AM
3860+ if (unlikely(!i))
3861+ break;
7f207e10 3862+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3863+ continue;
3864+
7f207e10 3865+ /* AuDbgInode(i); */
537831f9 3866+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3867+ ii_read_lock_child(i);
3868+ else {
3869+ ii_write_lock_child(i);
027c5e7a
AM
3870+ err = au_refresh_hinode_self(i);
3871+ au_iigen_dec(i);
1facf9fc 3872+ if (!err)
3873+ ii_downgrade_lock(i);
3874+ else {
3875+ ii_write_unlock(i);
3876+ break;
3877+ }
3878+ }
3879+
5afbbe0d
AM
3880+ btop = au_ibtop(i);
3881+ bbot = au_ibbot(i);
3882+ if (btop <= bindex
3883+ && bindex <= bbot
1facf9fc 3884+ && au_h_iptr(i, bindex)
5afbbe0d 3885+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3886+ err = -EBUSY;
3887+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3888+ AuDbgInode(i);
1facf9fc 3889+ }
3890+ ii_read_unlock(i);
3891+ }
7f207e10 3892+ au_iarray_free(array, max);
1facf9fc 3893+
7f207e10 3894+out:
1facf9fc 3895+ return err;
3896+}
3897+
b752ccd1
AM
3898+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3899+ const unsigned int verbose)
1facf9fc 3900+{
3901+ int err;
3902+ unsigned int sigen;
3903+
3904+ sigen = au_sigen(root->d_sb);
3905+ DiMustNoWaiters(root);
5527c038 3906+ IiMustNoWaiters(d_inode(root));
1facf9fc 3907+ di_write_unlock(root);
b752ccd1 3908+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3909+ if (!err)
b752ccd1 3910+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3911+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3912+
3913+ return err;
3914+}
3915+
076b876e
AM
3916+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3917+ struct file **to_free, int *idx)
3918+{
3919+ int err;
c1595e42 3920+ unsigned char matched, root;
5afbbe0d 3921+ aufs_bindex_t bindex, bbot;
076b876e
AM
3922+ struct au_fidir *fidir;
3923+ struct au_hfile *hfile;
3924+
3925+ err = 0;
2000de60 3926+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3927+ if (root) {
3928+ get_file(file);
3929+ to_free[*idx] = file;
3930+ (*idx)++;
3931+ goto out;
3932+ }
3933+
076b876e 3934+ matched = 0;
076b876e
AM
3935+ fidir = au_fi(file)->fi_hdir;
3936+ AuDebugOn(!fidir);
5afbbe0d
AM
3937+ bbot = au_fbbot_dir(file);
3938+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3939+ hfile = fidir->fd_hfile + bindex;
3940+ if (!hfile->hf_file)
3941+ continue;
3942+
c1595e42 3943+ if (hfile->hf_br->br_id == br_id) {
076b876e 3944+ matched = 1;
076b876e 3945+ break;
c1595e42 3946+ }
076b876e 3947+ }
c1595e42 3948+ if (matched)
076b876e
AM
3949+ err = -EBUSY;
3950+
3951+out:
3952+ return err;
3953+}
3954+
3955+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3956+ struct file **to_free, int opened)
3957+{
3958+ int err, idx;
3959+ unsigned long long ull, max;
5afbbe0d 3960+ aufs_bindex_t btop;
076b876e 3961+ struct file *file, **array;
076b876e
AM
3962+ struct dentry *root;
3963+ struct au_hfile *hfile;
3964+
3965+ array = au_farray_alloc(sb, &max);
3966+ err = PTR_ERR(array);
3967+ if (IS_ERR(array))
3968+ goto out;
3969+
3970+ err = 0;
3971+ idx = 0;
3972+ root = sb->s_root;
3973+ di_write_unlock(root);
3974+ for (ull = 0; ull < max; ull++) {
3975+ file = array[ull];
3976+ if (unlikely(!file))
3977+ break;
3978+
3979+ /* AuDbg("%pD\n", file); */
3980+ fi_read_lock(file);
5afbbe0d 3981+ btop = au_fbtop(file);
2000de60 3982+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3983+ hfile = &au_fi(file)->fi_htop;
3984+ if (hfile->hf_br->br_id == br_id)
3985+ err = -EBUSY;
3986+ } else
3987+ err = test_dir_busy(file, br_id, to_free, &idx);
3988+ fi_read_unlock(file);
3989+ if (unlikely(err))
3990+ break;
3991+ }
3992+ di_write_lock_child(root);
3993+ au_farray_free(array, max);
3994+ AuDebugOn(idx > opened);
3995+
3996+out:
3997+ return err;
3998+}
3999+
4000+static void br_del_file(struct file **to_free, unsigned long long opened,
4001+ aufs_bindex_t br_id)
4002+{
4003+ unsigned long long ull;
5afbbe0d 4004+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
4005+ struct file *file;
4006+ struct au_fidir *fidir;
4007+ struct au_hfile *hfile;
4008+
4009+ for (ull = 0; ull < opened; ull++) {
4010+ file = to_free[ull];
4011+ if (unlikely(!file))
4012+ break;
4013+
4014+ /* AuDbg("%pD\n", file); */
2000de60 4015+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
4016+ bfound = -1;
4017+ fidir = au_fi(file)->fi_hdir;
4018+ AuDebugOn(!fidir);
4019+ fi_write_lock(file);
5afbbe0d
AM
4020+ btop = au_fbtop(file);
4021+ bbot = au_fbbot_dir(file);
4022+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
4023+ hfile = fidir->fd_hfile + bindex;
4024+ if (!hfile->hf_file)
4025+ continue;
4026+
4027+ if (hfile->hf_br->br_id == br_id) {
4028+ bfound = bindex;
4029+ break;
4030+ }
4031+ }
4032+ AuDebugOn(bfound < 0);
4033+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
4034+ if (bfound == btop) {
4035+ for (btop++; btop <= bbot; btop++)
4036+ if (au_hf_dir(file, btop)) {
4037+ au_set_fbtop(file, btop);
076b876e
AM
4038+ break;
4039+ }
4040+ }
4041+ fi_write_unlock(file);
4042+ }
4043+}
4044+
1facf9fc 4045+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
4046+ const aufs_bindex_t bindex,
5afbbe0d 4047+ const aufs_bindex_t bbot)
1facf9fc 4048+{
4049+ struct au_branch **brp, **p;
4050+
dece6358
AM
4051+ AuRwMustWriteLock(&sbinfo->si_rwsem);
4052+
1facf9fc 4053+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
4054+ if (bindex < bbot)
4055+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
4056+ sbinfo->si_branch[0 + bbot] = NULL;
4057+ sbinfo->si_bbot--;
1facf9fc 4058+
e2f27e51
AM
4059+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
4060+ /*may_shrink*/1);
1facf9fc 4061+ if (p)
4062+ sbinfo->si_branch = p;
4a4d8108 4063+ /* harmless error */
1facf9fc 4064+}
4065+
4066+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 4067+ const aufs_bindex_t bbot)
1facf9fc 4068+{
4069+ struct au_hdentry *hdp, *p;
4070+
1308ab2a 4071+ AuRwMustWriteLock(&dinfo->di_rwsem);
4072+
5afbbe0d
AM
4073+ hdp = au_hdentry(dinfo, bindex);
4074+ if (bindex < bbot)
4075+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
4076+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
4077+ dinfo->di_bbot--;
1facf9fc 4078+
e2f27e51
AM
4079+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
4080+ /*may_shrink*/1);
1facf9fc 4081+ if (p)
4082+ dinfo->di_hdentry = p;
4a4d8108 4083+ /* harmless error */
1facf9fc 4084+}
4085+
4086+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 4087+ const aufs_bindex_t bbot)
1facf9fc 4088+{
4089+ struct au_hinode *hip, *p;
4090+
1308ab2a 4091+ AuRwMustWriteLock(&iinfo->ii_rwsem);
4092+
5afbbe0d
AM
4093+ hip = au_hinode(iinfo, bindex);
4094+ if (bindex < bbot)
4095+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
4096+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
4097+ iinfo->ii_bbot--;
1facf9fc 4098+
e2f27e51
AM
4099+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
4100+ /*may_shrink*/1);
1facf9fc 4101+ if (p)
4102+ iinfo->ii_hinode = p;
4a4d8108 4103+ /* harmless error */
1facf9fc 4104+}
4105+
4106+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
4107+ struct au_branch *br)
4108+{
5afbbe0d 4109+ aufs_bindex_t bbot;
1facf9fc 4110+ struct au_sbinfo *sbinfo;
53392da6
AM
4111+ struct dentry *root, *h_root;
4112+ struct inode *inode, *h_inode;
4113+ struct au_hinode *hinode;
1facf9fc 4114+
dece6358
AM
4115+ SiMustWriteLock(sb);
4116+
1facf9fc 4117+ root = sb->s_root;
5527c038 4118+ inode = d_inode(root);
1facf9fc 4119+ sbinfo = au_sbi(sb);
5afbbe0d 4120+ bbot = sbinfo->si_bbot;
1facf9fc 4121+
53392da6
AM
4122+ h_root = au_h_dptr(root, bindex);
4123+ hinode = au_hi(inode, bindex);
4124+ h_inode = au_igrab(hinode->hi_inode);
4125+ au_hiput(hinode);
1facf9fc 4126+
53392da6 4127+ au_sbilist_lock();
5afbbe0d
AM
4128+ au_br_do_del_brp(sbinfo, bindex, bbot);
4129+ au_br_do_del_hdp(au_di(root), bindex, bbot);
4130+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
4131+ au_sbilist_unlock();
4132+
8b6a4947
AM
4133+ /* ignore an error */
4134+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
4135+
53392da6
AM
4136+ dput(h_root);
4137+ iput(h_inode);
4138+ au_br_do_free(br);
1facf9fc 4139+}
4140+
79b8bda9
AM
4141+static unsigned long long empty_cb(struct super_block *sb, void *array,
4142+ unsigned long long max, void *arg)
076b876e
AM
4143+{
4144+ return max;
4145+}
4146+
1facf9fc 4147+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4148+{
4149+ int err, rerr, i;
076b876e 4150+ unsigned long long opened;
1facf9fc 4151+ unsigned int mnt_flags;
5afbbe0d 4152+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 4153+ unsigned char do_wh, verbose;
4154+ struct au_branch *br;
4155+ struct au_wbr *wbr;
076b876e
AM
4156+ struct dentry *root;
4157+ struct file **to_free;
1facf9fc 4158+
4159+ err = 0;
076b876e
AM
4160+ opened = 0;
4161+ to_free = NULL;
4162+ root = sb->s_root;
4163+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 4164+ if (bindex < 0) {
4165+ if (remount)
4166+ goto out; /* success */
4167+ err = -ENOENT;
4a4d8108 4168+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 4169+ goto out;
4170+ }
4171+ AuDbg("bindex b%d\n", bindex);
4172+
4173+ err = -EBUSY;
4174+ mnt_flags = au_mntflags(sb);
4175+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
4176+ bbot = au_sbbot(sb);
4177+ if (unlikely(!bbot)) {
1facf9fc 4178+ AuVerbose(verbose, "no more branches left\n");
4179+ goto out;
4180+ }
4181+ br = au_sbr(sb, bindex);
86dc4139 4182+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
076b876e
AM
4183+
4184+ br_id = br->br_id;
5afbbe0d 4185+ opened = au_br_count(br);
076b876e 4186+ if (unlikely(opened)) {
79b8bda9 4187+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
4188+ err = PTR_ERR(to_free);
4189+ if (IS_ERR(to_free))
4190+ goto out;
4191+
4192+ err = test_file_busy(sb, br_id, to_free, opened);
4193+ if (unlikely(err)) {
4194+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
4195+ goto out;
4196+ }
1facf9fc 4197+ }
4198+
4199+ wbr = br->br_wbr;
4200+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4201+ if (do_wh) {
1308ab2a 4202+ /* instead of WbrWhMustWriteLock(wbr) */
4203+ SiMustWriteLock(sb);
1facf9fc 4204+ for (i = 0; i < AuBrWh_Last; i++) {
4205+ dput(wbr->wbr_wh[i]);
4206+ wbr->wbr_wh[i] = NULL;
4207+ }
4208+ }
4209+
076b876e 4210+ err = test_children_busy(root, bindex, verbose);
1facf9fc 4211+ if (unlikely(err)) {
4212+ if (do_wh)
4213+ goto out_wh;
4214+ goto out;
4215+ }
4216+
4217+ err = 0;
076b876e
AM
4218+ if (to_free) {
4219+ /*
4220+ * now we confirmed the branch is deletable.
4221+ * let's free the remaining opened dirs on the branch.
4222+ */
4223+ di_write_unlock(root);
4224+ br_del_file(to_free, opened, br_id);
4225+ di_write_lock_child(root);
4226+ }
4227+
1facf9fc 4228+ if (!remount)
4229+ au_br_do_del(sb, bindex, br);
4230+ else {
4231+ sysaufs_brs_del(sb, bindex);
4232+ au_br_do_del(sb, bindex, br);
4233+ sysaufs_brs_add(sb, bindex);
4234+ }
4235+
1308ab2a 4236+ if (!bindex) {
5527c038 4237+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4238+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4239+ } else
5527c038 4240+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4241+ if (au_opt_test(mnt_flags, PLINK))
4242+ au_plink_half_refresh(sb, br_id);
4243+
b752ccd1 4244+ if (au_xino_brid(sb) == br_id)
1facf9fc 4245+ au_xino_brid_set(sb, -1);
4246+ goto out; /* success */
4247+
4f0767ce 4248+out_wh:
1facf9fc 4249+ /* revert */
86dc4139 4250+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4251+ if (rerr)
0c3ec466
AM
4252+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4253+ del->pathname, rerr);
4f0767ce 4254+out:
076b876e
AM
4255+ if (to_free)
4256+ au_farray_free(to_free, opened);
1facf9fc 4257+ return err;
4258+}
4259+
4260+/* ---------------------------------------------------------------------- */
4261+
027c5e7a
AM
4262+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4263+{
4264+ int err;
5afbbe0d 4265+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4266+ struct aufs_ibusy ibusy;
4267+ struct inode *inode, *h_inode;
4268+
4269+ err = -EPERM;
4270+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4271+ goto out;
4272+
4273+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4274+ if (!err)
4275+ err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4276+ if (unlikely(err)) {
4277+ err = -EFAULT;
4278+ AuTraceErr(err);
4279+ goto out;
4280+ }
4281+
4282+ err = -EINVAL;
4283+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4284+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4285+ goto out_unlock;
4286+
4287+ err = 0;
4288+ ibusy.h_ino = 0; /* invalid */
4289+ inode = ilookup(sb, ibusy.ino);
4290+ if (!inode
4291+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4292+ || au_is_bad_inode(inode))
027c5e7a
AM
4293+ goto out_unlock;
4294+
4295+ ii_read_lock_child(inode);
5afbbe0d
AM
4296+ btop = au_ibtop(inode);
4297+ bbot = au_ibbot(inode);
4298+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4299+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4300+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4301+ ibusy.h_ino = h_inode->i_ino;
4302+ }
4303+ ii_read_unlock(inode);
4304+ iput(inode);
4305+
4306+out_unlock:
4307+ si_read_unlock(sb);
4308+ if (!err) {
4309+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4310+ if (unlikely(err)) {
4311+ err = -EFAULT;
4312+ AuTraceErr(err);
4313+ }
4314+ }
4315+out:
4316+ return err;
4317+}
4318+
4319+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4320+{
2000de60 4321+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4322+}
4323+
4324+#ifdef CONFIG_COMPAT
4325+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4326+{
2000de60 4327+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4328+}
4329+#endif
4330+
4331+/* ---------------------------------------------------------------------- */
4332+
1facf9fc 4333+/*
4334+ * change a branch permission
4335+ */
4336+
dece6358
AM
4337+static void au_warn_ima(void)
4338+{
4339+#ifdef CONFIG_IMA
1308ab2a 4340+ /* since it doesn't support mark_files_ro() */
027c5e7a 4341+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4342+#endif
4343+}
4344+
1facf9fc 4345+static int do_need_sigen_inc(int a, int b)
4346+{
4347+ return au_br_whable(a) && !au_br_whable(b);
4348+}
4349+
4350+static int need_sigen_inc(int old, int new)
4351+{
4352+ return do_need_sigen_inc(old, new)
4353+ || do_need_sigen_inc(new, old);
4354+}
4355+
4356+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4357+{
7f207e10 4358+ int err, do_warn;
027c5e7a 4359+ unsigned int mnt_flags;
7f207e10 4360+ unsigned long long ull, max;
e49829fe 4361+ aufs_bindex_t br_id;
38d290e6 4362+ unsigned char verbose, writer;
7f207e10 4363+ struct file *file, *hf, **array;
e49829fe 4364+ struct au_hfile *hfile;
1facf9fc 4365+
027c5e7a
AM
4366+ mnt_flags = au_mntflags(sb);
4367+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4368+
7f207e10
AM
4369+ array = au_farray_alloc(sb, &max);
4370+ err = PTR_ERR(array);
4371+ if (IS_ERR(array))
1facf9fc 4372+ goto out;
4373+
7f207e10 4374+ do_warn = 0;
e49829fe 4375+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4376+ for (ull = 0; ull < max; ull++) {
4377+ file = array[ull];
076b876e
AM
4378+ if (unlikely(!file))
4379+ break;
1facf9fc 4380+
523b37e3 4381+ /* AuDbg("%pD\n", file); */
1facf9fc 4382+ fi_read_lock(file);
4383+ if (unlikely(au_test_mmapped(file))) {
4384+ err = -EBUSY;
523b37e3 4385+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4386+ AuDbgFile(file);
1facf9fc 4387+ FiMustNoWaiters(file);
4388+ fi_read_unlock(file);
7f207e10 4389+ goto out_array;
1facf9fc 4390+ }
4391+
e49829fe
JR
4392+ hfile = &au_fi(file)->fi_htop;
4393+ hf = hfile->hf_file;
7e9cd9fe 4394+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4395+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4396+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4397+ || !(hf->f_mode & FMODE_WRITE))
4398+ array[ull] = NULL;
4399+ else {
4400+ do_warn = 1;
4401+ get_file(file);
1facf9fc 4402+ }
4403+
1facf9fc 4404+ FiMustNoWaiters(file);
4405+ fi_read_unlock(file);
7f207e10
AM
4406+ fput(file);
4407+ }
1facf9fc 4408+
4409+ err = 0;
7f207e10 4410+ if (do_warn)
dece6358 4411+ au_warn_ima();
7f207e10
AM
4412+
4413+ for (ull = 0; ull < max; ull++) {
4414+ file = array[ull];
4415+ if (!file)
4416+ continue;
4417+
1facf9fc 4418+ /* todo: already flushed? */
523b37e3
AM
4419+ /*
4420+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4421+ * approach which resets f_mode and calls mnt_drop_write() and
4422+ * file_release_write() for each file, because the branch
4423+ * attribute in aufs world is totally different from the native
4424+ * fs rw/ro mode.
4425+ */
7f207e10
AM
4426+ /* fi_read_lock(file); */
4427+ hfile = &au_fi(file)->fi_htop;
4428+ hf = hfile->hf_file;
4429+ /* fi_read_unlock(file); */
027c5e7a 4430+ spin_lock(&hf->f_lock);
38d290e6
JR
4431+ writer = !!(hf->f_mode & FMODE_WRITER);
4432+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4433+ spin_unlock(&hf->f_lock);
38d290e6
JR
4434+ if (writer) {
4435+ put_write_access(file_inode(hf));
c06a8ce3 4436+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4437+ }
4438+ }
4439+
7f207e10
AM
4440+out_array:
4441+ au_farray_free(array, max);
4f0767ce 4442+out:
7f207e10 4443+ AuTraceErr(err);
1facf9fc 4444+ return err;
4445+}
4446+
4447+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4448+ int *do_refresh)
1facf9fc 4449+{
4450+ int err, rerr;
4451+ aufs_bindex_t bindex;
4452+ struct dentry *root;
4453+ struct au_branch *br;
076b876e 4454+ struct au_br_fhsm *bf;
1facf9fc 4455+
4456+ root = sb->s_root;
1facf9fc 4457+ bindex = au_find_dbindex(root, mod->h_root);
4458+ if (bindex < 0) {
4459+ if (remount)
4460+ return 0; /* success */
4461+ err = -ENOENT;
4a4d8108 4462+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4463+ goto out;
4464+ }
4465+ AuDbg("bindex b%d\n", bindex);
4466+
5527c038 4467+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4468+ if (unlikely(err))
4469+ goto out;
4470+
4471+ br = au_sbr(sb, bindex);
86dc4139 4472+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4473+ if (br->br_perm == mod->perm)
4474+ return 0; /* success */
4475+
076b876e
AM
4476+ /* pre-allocate for non-fhsm --> fhsm */
4477+ bf = NULL;
4478+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4479+ err = au_fhsm_br_alloc(br);
4480+ if (unlikely(err))
4481+ goto out;
4482+ bf = br->br_fhsm;
4483+ br->br_fhsm = NULL;
4484+ }
4485+
1facf9fc 4486+ if (au_br_writable(br->br_perm)) {
4487+ /* remove whiteout base */
86dc4139 4488+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4489+ if (unlikely(err))
076b876e 4490+ goto out_bf;
1facf9fc 4491+
4492+ if (!au_br_writable(mod->perm)) {
4493+ /* rw --> ro, file might be mmapped */
4494+ DiMustNoWaiters(root);
5527c038 4495+ IiMustNoWaiters(d_inode(root));
1facf9fc 4496+ di_write_unlock(root);
4497+ err = au_br_mod_files_ro(sb, bindex);
4498+ /* aufs_write_lock() calls ..._child() */
4499+ di_write_lock_child(root);
4500+
4501+ if (unlikely(err)) {
4502+ rerr = -ENOMEM;
be52b249 4503+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4504+ GFP_NOFS);
86dc4139
AM
4505+ if (br->br_wbr)
4506+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4507+ if (unlikely(rerr)) {
4508+ AuIOErr("nested error %d (%d)\n",
4509+ rerr, err);
4510+ br->br_perm = mod->perm;
4511+ }
4512+ }
4513+ }
4514+ } else if (au_br_writable(mod->perm)) {
4515+ /* ro --> rw */
4516+ err = -ENOMEM;
be52b249 4517+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4518+ if (br->br_wbr) {
86dc4139 4519+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4520+ if (unlikely(err)) {
1c60b727 4521+ kfree(br->br_wbr);
1facf9fc 4522+ br->br_wbr = NULL;
4523+ }
4524+ }
4525+ }
076b876e
AM
4526+ if (unlikely(err))
4527+ goto out_bf;
4528+
4529+ if (au_br_fhsm(br->br_perm)) {
4530+ if (!au_br_fhsm(mod->perm)) {
4531+ /* fhsm --> non-fhsm */
4532+ au_br_fhsm_fin(br->br_fhsm);
1c60b727 4533+ kfree(br->br_fhsm);
076b876e
AM
4534+ br->br_fhsm = NULL;
4535+ }
4536+ } else if (au_br_fhsm(mod->perm))
4537+ /* non-fhsm --> fhsm */
4538+ br->br_fhsm = bf;
4539+
076b876e
AM
4540+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4541+ br->br_perm = mod->perm;
4542+ goto out; /* success */
1facf9fc 4543+
076b876e 4544+out_bf:
8b6a4947 4545+ kfree(bf);
076b876e
AM
4546+out:
4547+ AuTraceErr(err);
4548+ return err;
4549+}
4550+
4551+/* ---------------------------------------------------------------------- */
4552+
4553+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4554+{
4555+ int err;
4556+ struct kstatfs kstfs;
4557+
4558+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4559+ if (!err) {
076b876e
AM
4560+ stfs->f_blocks = kstfs.f_blocks;
4561+ stfs->f_bavail = kstfs.f_bavail;
4562+ stfs->f_files = kstfs.f_files;
4563+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4564+ }
4565+
1facf9fc 4566+ return err;
4567+}
7f207e10
AM
4568diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4569--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 4570+++ linux/fs/aufs/branch.h 2018-06-04 09:08:09.181412645 +0200
8b6a4947 4571@@ -0,0 +1,333 @@
1facf9fc 4572+/*
b00004a5 4573+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 4574+ *
4575+ * This program, aufs is free software; you can redistribute it and/or modify
4576+ * it under the terms of the GNU General Public License as published by
4577+ * the Free Software Foundation; either version 2 of the License, or
4578+ * (at your option) any later version.
dece6358
AM
4579+ *
4580+ * This program is distributed in the hope that it will be useful,
4581+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4582+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4583+ * GNU General Public License for more details.
4584+ *
4585+ * You should have received a copy of the GNU General Public License
523b37e3 4586+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4587+ */
4588+
4589+/*
4590+ * branch filesystems and xino for them
4591+ */
4592+
4593+#ifndef __AUFS_BRANCH_H__
4594+#define __AUFS_BRANCH_H__
4595+
4596+#ifdef __KERNEL__
4597+
1facf9fc 4598+#include <linux/mount.h>
8b6a4947 4599+#include "dirren.h"
4a4d8108 4600+#include "dynop.h"
1facf9fc 4601+#include "rwsem.h"
4602+#include "super.h"
4603+
4604+/* ---------------------------------------------------------------------- */
4605+
4606+/* a xino file */
4607+struct au_xino_file {
4608+ struct file *xi_file;
521ced18
JR
4609+ struct {
4610+ spinlock_t spin;
4611+ ino_t *array;
4612+ int total;
4613+ /* reserved for future use */
4614+ /* unsigned long *bitmap; */
4615+ wait_queue_head_t wqh;
4616+ } xi_nondir;
1facf9fc 4617+
4618+ /* todo: make xino files an array to support huge inode number */
4619+
4620+#ifdef CONFIG_DEBUG_FS
4621+ struct dentry *xi_dbgaufs;
4622+#endif
4623+};
4624+
076b876e
AM
4625+/* File-based Hierarchical Storage Management */
4626+struct au_br_fhsm {
4627+#ifdef CONFIG_AUFS_FHSM
4628+ struct mutex bf_lock;
4629+ unsigned long bf_jiffy;
4630+ struct aufs_stfs bf_stfs;
4631+ int bf_readable;
4632+#endif
4633+};
4634+
1facf9fc 4635+/* members for writable branch only */
4636+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4637+struct au_wbr {
dece6358 4638+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4639+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4640+ atomic_t wbr_wh_running;
1facf9fc 4641+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4642+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4643+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4644+
4645+ /* mfs mode */
4646+ unsigned long long wbr_bytes;
4647+};
4648+
4a4d8108
AM
4649+/* ext2 has 3 types of operations at least, ext3 has 4 */
4650+#define AuBrDynOp (AuDyLast * 4)
4651+
1716fcea
AM
4652+#ifdef CONFIG_AUFS_HFSNOTIFY
4653+/* support for asynchronous destruction */
4654+struct au_br_hfsnotify {
4655+ struct fsnotify_group *hfsn_group;
4656+};
4657+#endif
4658+
392086de
AM
4659+/* sysfs entries */
4660+struct au_brsysfs {
4661+ char name[16];
4662+ struct attribute attr;
4663+};
4664+
4665+enum {
4666+ AuBrSysfs_BR,
4667+ AuBrSysfs_BRID,
4668+ AuBrSysfs_Last
4669+};
4670+
1facf9fc 4671+/* protected by superblock rwsem */
4672+struct au_branch {
4673+ struct au_xino_file br_xino;
4674+
4675+ aufs_bindex_t br_id;
4676+
4677+ int br_perm;
86dc4139 4678+ struct path br_path;
4a4d8108
AM
4679+ spinlock_t br_dykey_lock;
4680+ struct au_dykey *br_dykey[AuBrDynOp];
5afbbe0d 4681+ struct percpu_counter br_count;
1facf9fc 4682+
4683+ struct au_wbr *br_wbr;
076b876e 4684+ struct au_br_fhsm *br_fhsm;
1facf9fc 4685+
4686+ /* xino truncation */
1facf9fc 4687+ atomic_t br_xino_running;
4688+
027c5e7a 4689+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4690+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4691+#endif
4692+
1facf9fc 4693+#ifdef CONFIG_SYSFS
392086de
AM
4694+ /* entries under sysfs per mount-point */
4695+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4696+#endif
8b6a4947
AM
4697+
4698+ struct au_dr_br br_dirren;
1facf9fc 4699+};
4700+
4701+/* ---------------------------------------------------------------------- */
4702+
86dc4139
AM
4703+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4704+{
4705+ return br->br_path.mnt;
4706+}
4707+
4708+static inline struct dentry *au_br_dentry(struct au_branch *br)
4709+{
4710+ return br->br_path.dentry;
4711+}
4712+
4713+static inline struct super_block *au_br_sb(struct au_branch *br)
4714+{
4715+ return au_br_mnt(br)->mnt_sb;
4716+}
4717+
5afbbe0d
AM
4718+static inline void au_br_get(struct au_branch *br)
4719+{
4720+ percpu_counter_inc(&br->br_count);
4721+}
4722+
4723+static inline void au_br_put(struct au_branch *br)
4724+{
4725+ percpu_counter_dec(&br->br_count);
4726+}
4727+
4728+static inline s64 au_br_count(struct au_branch *br)
4729+{
4730+ return percpu_counter_sum(&br->br_count);
4731+}
4732+
4733+static inline void au_br_count_init(struct au_branch *br)
4734+{
4735+ percpu_counter_init(&br->br_count, 0, GFP_NOFS);
4736+}
4737+
4738+static inline void au_br_count_fin(struct au_branch *br)
4739+{
4740+ percpu_counter_destroy(&br->br_count);
4741+}
4742+
1facf9fc 4743+static inline int au_br_rdonly(struct au_branch *br)
4744+{
8b6a4947 4745+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4746+ || !au_br_writable(br->br_perm))
4747+ ? -EROFS : 0;
4748+}
4749+
4a4d8108 4750+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4751+{
4a4d8108 4752+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4753+ return !(brperm & AuBrPerm_RR);
1facf9fc 4754+#else
4755+ return 0;
4756+#endif
4757+}
4758+
b912730e
AM
4759+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4760+{
4761+ int err, exec_flag;
4762+
4763+ err = 0;
4764+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4765+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4766+ err = -EACCES;
4767+
4768+ return err;
4769+}
4770+
1facf9fc 4771+/* ---------------------------------------------------------------------- */
4772+
4773+/* branch.c */
4774+struct au_sbinfo;
4775+void au_br_free(struct au_sbinfo *sinfo);
4776+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4777+struct au_opt_add;
4778+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4779+struct au_opt_del;
4780+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4781+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4782+#ifdef CONFIG_COMPAT
4783+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4784+#endif
1facf9fc 4785+struct au_opt_mod;
4786+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4787+ int *do_refresh);
076b876e
AM
4788+struct aufs_stfs;
4789+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4790+
4791+/* xino.c */
4792+static const loff_t au_loff_max = LLONG_MAX;
4793+
4794+int au_xib_trunc(struct super_block *sb);
5527c038 4795+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4796+ loff_t *pos);
5527c038
JR
4797+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4798+ size_t size, loff_t *pos);
1facf9fc 4799+struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4800+struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4801+ino_t au_xino_new_ino(struct super_block *sb);
b752ccd1 4802+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4803+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4804+ ino_t ino);
4805+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4806+ ino_t *ino);
4807+int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4808+ struct file *base_file, int do_test);
4809+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4810+
4811+struct au_opt_xino;
4812+int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4813+void au_xino_clr(struct super_block *sb);
4814+struct file *au_xino_def(struct super_block *sb);
4815+int au_xino_path(struct seq_file *seq, struct file *file);
4816+
521ced18
JR
4817+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4818+ ino_t h_ino, int idx);
4819+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4820+ int *idx);
4821+
1facf9fc 4822+/* ---------------------------------------------------------------------- */
4823+
4824+/* Superblock to branch */
4825+static inline
4826+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4827+{
4828+ return au_sbr(sb, bindex)->br_id;
4829+}
4830+
4831+static inline
4832+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4833+{
86dc4139 4834+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4835+}
4836+
4837+static inline
4838+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4839+{
86dc4139 4840+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4841+}
4842+
5afbbe0d
AM
4843+static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
4844+{
4845+ au_br_get(au_sbr(sb, bindex));
4846+}
4847+
1facf9fc 4848+static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4849+{
5afbbe0d 4850+ au_br_put(au_sbr(sb, bindex));
1facf9fc 4851+}
4852+
4853+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4854+{
4855+ return au_sbr(sb, bindex)->br_perm;
4856+}
4857+
4858+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4859+{
4860+ return au_br_whable(au_sbr_perm(sb, bindex));
4861+}
4862+
4863+/* ---------------------------------------------------------------------- */
4864+
8b6a4947
AM
4865+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4866+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4867+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4868+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4869+/*
8b6a4947
AM
4870+#define wbr_wh_read_trylock_nested(wbr) \
4871+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4872+#define wbr_wh_write_trylock_nested(wbr) \
4873+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4874+*/
1facf9fc 4875+
8b6a4947
AM
4876+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4877+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4878+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4879+
4880+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4881+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4882+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4883+
076b876e
AM
4884+/* ---------------------------------------------------------------------- */
4885+
4886+#ifdef CONFIG_AUFS_FHSM
4887+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4888+{
4889+ mutex_init(&brfhsm->bf_lock);
4890+ brfhsm->bf_jiffy = 0;
4891+ brfhsm->bf_readable = 0;
4892+}
4893+
4894+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4895+{
4896+ mutex_destroy(&brfhsm->bf_lock);
4897+}
4898+#else
4899+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4900+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4901+#endif
4902+
1facf9fc 4903+#endif /* __KERNEL__ */
4904+#endif /* __AUFS_BRANCH_H__ */
7f207e10
AM
4905diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4906--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
b00004a5 4907+++ linux/fs/aufs/conf.mk 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
4908@@ -0,0 +1,40 @@
4909+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4910+
4911+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4912+
4913+define AuConf
4914+ifdef ${1}
4915+AuConfStr += ${1}=${${1}}
4916+endif
4917+endef
4918+
b752ccd1 4919+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4920+ SBILIST \
7f207e10 4921+ HNOTIFY HFSNOTIFY \
4a4d8108 4922+ EXPORT INO_T_64 \
c1595e42 4923+ XATTR \
076b876e 4924+ FHSM \
4a4d8108 4925+ RDU \
8b6a4947 4926+ DIRREN \
4a4d8108
AM
4927+ SHWH \
4928+ BR_RAMFS \
4929+ BR_FUSE POLL \
4930+ BR_HFSPLUS \
4931+ BDEV_LOOP \
b752ccd1
AM
4932+ DEBUG MAGIC_SYSRQ
4933+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4934+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4935+
4936+AuConfName = ${obj}/conf.str
4937+${AuConfName}.tmp: FORCE
4938+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4939+${AuConfName}: ${AuConfName}.tmp
4940+ @diff -q $< $@ > /dev/null 2>&1 || { \
4941+ echo ' GEN ' $@; \
4942+ cp -p $< $@; \
4943+ }
4944+FORCE:
4945+clean-files += ${AuConfName} ${AuConfName}.tmp
4946+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4947+
4948+-include ${srctree}/${src}/conf_priv.mk
7f207e10
AM
4949diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4950--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 4951+++ linux/fs/aufs/cpup.c 2018-06-04 09:08:09.181412645 +0200
be118d29 4952@@ -0,0 +1,1441 @@
1facf9fc 4953+/*
b00004a5 4954+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 4955+ *
4956+ * This program, aufs is free software; you can redistribute it and/or modify
4957+ * it under the terms of the GNU General Public License as published by
4958+ * the Free Software Foundation; either version 2 of the License, or
4959+ * (at your option) any later version.
dece6358
AM
4960+ *
4961+ * This program is distributed in the hope that it will be useful,
4962+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4963+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4964+ * GNU General Public License for more details.
4965+ *
4966+ * You should have received a copy of the GNU General Public License
523b37e3 4967+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4968+ */
4969+
4970+/*
4971+ * copy-up functions, see wbr_policy.c for copy-down
4972+ */
4973+
4974+#include <linux/fs_stack.h>
dece6358 4975+#include <linux/mm.h>
8cdd5066 4976+#include <linux/task_work.h>
1facf9fc 4977+#include "aufs.h"
4978+
86dc4139 4979+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4980+{
4981+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4982+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4983+
86dc4139
AM
4984+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4985+
4986+ dst->i_flags |= iflags & ~mask;
1facf9fc 4987+ if (au_test_fs_notime(dst->i_sb))
4988+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4989+}
4990+
4991+void au_cpup_attr_timesizes(struct inode *inode)
4992+{
4993+ struct inode *h_inode;
4994+
5afbbe0d 4995+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4996+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 4997+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 4998+}
4999+
5000+void au_cpup_attr_nlink(struct inode *inode, int force)
5001+{
5002+ struct inode *h_inode;
5003+ struct super_block *sb;
5afbbe0d 5004+ aufs_bindex_t bindex, bbot;
1facf9fc 5005+
5006+ sb = inode->i_sb;
5afbbe0d 5007+ bindex = au_ibtop(inode);
1facf9fc 5008+ h_inode = au_h_iptr(inode, bindex);
5009+ if (!force
5010+ && !S_ISDIR(h_inode->i_mode)
5011+ && au_opt_test(au_mntflags(sb), PLINK)
5012+ && au_plink_test(inode))
5013+ return;
5014+
7eafdf33
AM
5015+ /*
5016+ * 0 can happen in revalidating.
38d290e6
JR
5017+ * h_inode->i_mutex may not be held here, but it is harmless since once
5018+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
5019+ * case.
5020+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
5021+ * the incorrect link count.
7eafdf33 5022+ */
92d182d2 5023+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 5024+
5025+ /*
5026+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
5027+ * it may includes whplink directory.
5028+ */
5029+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
5030+ bbot = au_ibbot(inode);
5031+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 5032+ h_inode = au_h_iptr(inode, bindex);
5033+ if (h_inode)
5034+ au_add_nlink(inode, h_inode);
5035+ }
5036+ }
5037+}
5038+
5039+void au_cpup_attr_changeable(struct inode *inode)
5040+{
5041+ struct inode *h_inode;
5042+
5afbbe0d 5043+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5044+ inode->i_mode = h_inode->i_mode;
5045+ inode->i_uid = h_inode->i_uid;
5046+ inode->i_gid = h_inode->i_gid;
5047+ au_cpup_attr_timesizes(inode);
86dc4139 5048+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 5049+}
5050+
5051+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
5052+{
5053+ struct au_iinfo *iinfo = au_ii(inode);
5054+
1308ab2a 5055+ IiMustWriteLock(inode);
5056+
1facf9fc 5057+ iinfo->ii_higen = h_inode->i_generation;
5058+ iinfo->ii_hsb1 = h_inode->i_sb;
5059+}
5060+
5061+void au_cpup_attr_all(struct inode *inode, int force)
5062+{
5063+ struct inode *h_inode;
5064+
5afbbe0d 5065+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5066+ au_cpup_attr_changeable(inode);
5067+ if (inode->i_nlink > 0)
5068+ au_cpup_attr_nlink(inode, force);
5069+ inode->i_rdev = h_inode->i_rdev;
5070+ inode->i_blkbits = h_inode->i_blkbits;
5071+ au_cpup_igen(inode, h_inode);
5072+}
5073+
5074+/* ---------------------------------------------------------------------- */
5075+
5076+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
5077+
5078+/* keep the timestamps of the parent dir when cpup */
5079+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5080+ struct path *h_path)
5081+{
5082+ struct inode *h_inode;
5083+
5084+ dt->dt_dentry = dentry;
5085+ dt->dt_h_path = *h_path;
5527c038 5086+ h_inode = d_inode(h_path->dentry);
1facf9fc 5087+ dt->dt_atime = h_inode->i_atime;
5088+ dt->dt_mtime = h_inode->i_mtime;
5089+ /* smp_mb(); */
5090+}
5091+
5092+void au_dtime_revert(struct au_dtime *dt)
5093+{
5094+ struct iattr attr;
5095+ int err;
5096+
5097+ attr.ia_atime = dt->dt_atime;
5098+ attr.ia_mtime = dt->dt_mtime;
5099+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5100+ | ATTR_ATIME | ATTR_ATIME_SET;
5101+
523b37e3
AM
5102+ /* no delegation since this is a directory */
5103+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 5104+ if (unlikely(err))
0c3ec466 5105+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 5106+}
5107+
5108+/* ---------------------------------------------------------------------- */
5109+
86dc4139
AM
5110+/* internal use only */
5111+struct au_cpup_reg_attr {
5112+ int valid;
5113+ struct kstat st;
5114+ unsigned int iflags; /* inode->i_flags */
5115+};
5116+
1facf9fc 5117+static noinline_for_stack
86dc4139
AM
5118+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5119+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5120+{
c1595e42 5121+ int err, sbits, icex;
7e9cd9fe
AM
5122+ unsigned int mnt_flags;
5123+ unsigned char verbose;
1facf9fc 5124+ struct iattr ia;
5125+ struct path h_path;
1308ab2a 5126+ struct inode *h_isrc, *h_idst;
86dc4139 5127+ struct kstat *h_st;
c1595e42 5128+ struct au_branch *br;
1facf9fc 5129+
5130+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 5131+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
5132+ br = au_sbr(dst->d_sb, bindex);
5133+ h_path.mnt = au_br_mnt(br);
5527c038 5134+ h_isrc = d_inode(h_src);
1308ab2a 5135+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 5136+ | ATTR_ATIME | ATTR_MTIME
5137+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
5138+ if (h_src_attr && h_src_attr->valid) {
5139+ h_st = &h_src_attr->st;
5140+ ia.ia_uid = h_st->uid;
5141+ ia.ia_gid = h_st->gid;
5142+ ia.ia_atime = h_st->atime;
5143+ ia.ia_mtime = h_st->mtime;
5144+ if (h_idst->i_mode != h_st->mode
5145+ && !S_ISLNK(h_idst->i_mode)) {
5146+ ia.ia_valid |= ATTR_MODE;
5147+ ia.ia_mode = h_st->mode;
5148+ }
5149+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5150+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5151+ } else {
5152+ ia.ia_uid = h_isrc->i_uid;
5153+ ia.ia_gid = h_isrc->i_gid;
5154+ ia.ia_atime = h_isrc->i_atime;
5155+ ia.ia_mtime = h_isrc->i_mtime;
5156+ if (h_idst->i_mode != h_isrc->i_mode
5157+ && !S_ISLNK(h_idst->i_mode)) {
5158+ ia.ia_valid |= ATTR_MODE;
5159+ ia.ia_mode = h_isrc->i_mode;
5160+ }
5161+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5162+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 5163+ }
523b37e3
AM
5164+ /* no delegation since it is just created */
5165+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5166+
5167+ /* is this nfs only? */
5168+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5169+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5170+ ia.ia_mode = h_isrc->i_mode;
523b37e3 5171+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5172+ }
5173+
c1595e42 5174+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
5175+ if (!err) {
5176+ mnt_flags = au_mntflags(dst->d_sb);
5177+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5178+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5179+ }
c1595e42 5180+
1facf9fc 5181+ return err;
5182+}
5183+
5184+/* ---------------------------------------------------------------------- */
5185+
5186+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5187+ char *buf, unsigned long blksize)
5188+{
5189+ int err;
5190+ size_t sz, rbytes, wbytes;
5191+ unsigned char all_zero;
5192+ char *p, *zp;
febd17d6 5193+ struct inode *h_inode;
1facf9fc 5194+ /* reduce stack usage */
5195+ struct iattr *ia;
5196+
5197+ zp = page_address(ZERO_PAGE(0));
5198+ if (unlikely(!zp))
5199+ return -ENOMEM; /* possible? */
5200+
5201+ err = 0;
5202+ all_zero = 0;
5203+ while (len) {
5204+ AuDbg("len %lld\n", len);
5205+ sz = blksize;
5206+ if (len < blksize)
5207+ sz = len;
5208+
5209+ rbytes = 0;
5210+ /* todo: signal_pending? */
5211+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5212+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5213+ err = rbytes;
5214+ }
5215+ if (unlikely(err < 0))
5216+ break;
5217+
5218+ all_zero = 0;
5219+ if (len >= rbytes && rbytes == blksize)
5220+ all_zero = !memcmp(buf, zp, rbytes);
5221+ if (!all_zero) {
5222+ wbytes = rbytes;
5223+ p = buf;
5224+ while (wbytes) {
5225+ size_t b;
5226+
5227+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5228+ err = b;
5229+ /* todo: signal_pending? */
5230+ if (unlikely(err == -EAGAIN || err == -EINTR))
5231+ continue;
5232+ if (unlikely(err < 0))
5233+ break;
5234+ wbytes -= b;
5235+ p += b;
5236+ }
392086de
AM
5237+ if (unlikely(err < 0))
5238+ break;
1facf9fc 5239+ } else {
5240+ loff_t res;
5241+
5242+ AuLabel(hole);
5243+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5244+ err = res;
5245+ if (unlikely(res < 0))
5246+ break;
5247+ }
5248+ len -= rbytes;
5249+ err = 0;
5250+ }
5251+
5252+ /* the last block may be a hole */
5253+ if (!err && all_zero) {
5254+ AuLabel(last hole);
5255+
5256+ err = 1;
2000de60 5257+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5258+ /* nfs requires this step to make last hole */
5259+ /* is this only nfs? */
5260+ do {
5261+ /* todo: signal_pending? */
5262+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5263+ } while (err == -EAGAIN || err == -EINTR);
5264+ if (err == 1)
5265+ dst->f_pos--;
5266+ }
5267+
5268+ if (err == 1) {
5269+ ia = (void *)buf;
5270+ ia->ia_size = dst->f_pos;
5271+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5272+ ia->ia_file = dst;
febd17d6
JR
5273+ h_inode = file_inode(dst);
5274+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5275+ /* no delegation since it is just created */
5276+ err = vfsub_notify_change(&dst->f_path, ia,
5277+ /*delegated*/NULL);
febd17d6 5278+ inode_unlock(h_inode);
1facf9fc 5279+ }
5280+ }
5281+
5282+ return err;
5283+}
5284+
5285+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5286+{
5287+ int err;
5288+ unsigned long blksize;
5289+ unsigned char do_kfree;
5290+ char *buf;
5291+
5292+ err = -ENOMEM;
2000de60 5293+ blksize = dst->f_path.dentry->d_sb->s_blocksize;
1facf9fc 5294+ if (!blksize || PAGE_SIZE < blksize)
5295+ blksize = PAGE_SIZE;
5296+ AuDbg("blksize %lu\n", blksize);
5297+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5298+ if (do_kfree)
5299+ buf = kmalloc(blksize, GFP_NOFS);
5300+ else
5301+ buf = (void *)__get_free_page(GFP_NOFS);
5302+ if (unlikely(!buf))
5303+ goto out;
5304+
5305+ if (len > (1 << 22))
5306+ AuDbg("copying a large file %lld\n", (long long)len);
5307+
5308+ src->f_pos = 0;
5309+ dst->f_pos = 0;
5310+ err = au_do_copy_file(dst, src, len, buf, blksize);
5311+ if (do_kfree)
1c60b727 5312+ kfree(buf);
1facf9fc 5313+ else
1c60b727 5314+ free_page((unsigned long)buf);
1facf9fc 5315+
4f0767ce 5316+out:
1facf9fc 5317+ return err;
5318+}
5319+
1c60b727
AM
5320+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5321+{
5322+ int err;
5323+ struct super_block *h_src_sb;
5324+ struct inode *h_src_inode;
5325+
5326+ h_src_inode = file_inode(src);
5327+ h_src_sb = h_src_inode->i_sb;
5328+
5329+ /* XFS acquires inode_lock */
5330+ if (!au_test_xfs(h_src_sb))
5331+ err = au_copy_file(dst, src, len);
5332+ else {
3c1bdaff 5333+ inode_unlock_shared(h_src_inode);
1c60b727 5334+ err = au_copy_file(dst, src, len);
be118d29 5335+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5336+ }
5337+
5338+ return err;
5339+}
5340+
5341+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5342+{
5343+ int err;
5344+ struct super_block *h_src_sb;
5345+ struct inode *h_src_inode;
5346+
5347+ h_src_inode = file_inode(src);
5348+ h_src_sb = h_src_inode->i_sb;
5349+ if (h_src_sb != file_inode(dst)->i_sb
5350+ || !dst->f_op->clone_file_range) {
5351+ err = au_do_copy(dst, src, len);
5352+ goto out;
5353+ }
5354+
5355+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5356+ inode_unlock_shared(h_src_inode);
1c60b727 5357+ err = vfsub_clone_file_range(src, dst, len);
be118d29 5358+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5359+ } else
5360+ err = vfsub_clone_file_range(src, dst, len);
5361+ /* older XFS has a condition in cloning */
5362+ if (unlikely(err != -EOPNOTSUPP))
5363+ goto out;
5364+
5365+ /* the backend fs on NFS may not support cloning */
5366+ err = au_do_copy(dst, src, len);
5367+
5368+out:
5369+ AuTraceErr(err);
5370+ return err;
5371+}
5372+
1facf9fc 5373+/*
5374+ * to support a sparse file which is opened with O_APPEND,
5375+ * we need to close the file.
5376+ */
c2b27bf2 5377+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5378+{
5379+ int err, i;
5380+ enum { SRC, DST };
5381+ struct {
5382+ aufs_bindex_t bindex;
5383+ unsigned int flags;
5384+ struct dentry *dentry;
392086de 5385+ int force_wr;
1facf9fc 5386+ struct file *file;
523b37e3 5387+ void *label;
1facf9fc 5388+ } *f, file[] = {
5389+ {
c2b27bf2 5390+ .bindex = cpg->bsrc,
1facf9fc 5391+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
523b37e3 5392+ .label = &&out
1facf9fc 5393+ },
5394+ {
c2b27bf2 5395+ .bindex = cpg->bdst,
1facf9fc 5396+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5397+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
523b37e3 5398+ .label = &&out_src
1facf9fc 5399+ }
5400+ };
521ced18 5401+ struct super_block *sb, *h_src_sb;
e2f27e51 5402+ struct inode *h_src_inode;
8cdd5066 5403+ struct task_struct *tsk = current;
1facf9fc 5404+
5405+ /* bsrc branch can be ro/rw. */
c2b27bf2 5406+ sb = cpg->dentry->d_sb;
1facf9fc 5407+ f = file;
5408+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5409+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5410+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5411+ /*file*/NULL, f->force_wr);
1facf9fc 5412+ err = PTR_ERR(f->file);
5413+ if (IS_ERR(f->file))
5414+ goto *f->label;
1facf9fc 5415+ }
5416+
5417+ /* try stopping to update while we copyup */
e2f27e51 5418+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5419+ h_src_sb = h_src_inode->i_sb;
5420+ if (!au_test_nfs(h_src_sb))
e2f27e51 5421+ IMustLock(h_src_inode);
1c60b727 5422+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5423+
8cdd5066
JR
5424+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5425+ if (tsk->flags & PF_KTHREAD)
5426+ __fput_sync(file[DST].file);
5427+ else {
2121bcd9 5428+ /* it happend actually */
8cdd5066
JR
5429+ fput(file[DST].file);
5430+ /*
5431+ * too bad.
5432+ * we have to call both since we don't know which place the file
5433+ * was added to.
5434+ */
5435+ task_work_run();
5436+ flush_delayed_fput();
5437+ }
1facf9fc 5438+ au_sbr_put(sb, file[DST].bindex);
523b37e3 5439+
4f0767ce 5440+out_src:
1facf9fc 5441+ fput(file[SRC].file);
5442+ au_sbr_put(sb, file[SRC].bindex);
4f0767ce 5443+out:
1facf9fc 5444+ return err;
5445+}
5446+
c2b27bf2 5447+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5448+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5449+{
5450+ int err, rerr;
5451+ loff_t l;
86dc4139 5452+ struct path h_path;
38d290e6 5453+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5454+
5455+ err = 0;
5527c038 5456+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5457+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5458+ if (cpg->len == -1 || l < cpg->len)
5459+ cpg->len = l;
5460+ if (cpg->len) {
86dc4139 5461+ /* try stopping to update while we are referencing */
be118d29 5462+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5463+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5464+
c2b27bf2
AM
5465+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5466+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5467+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5468+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5469+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5470+ else {
3c1bdaff 5471+ inode_unlock_shared(h_src_inode);
521ced18 5472+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5473+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5474+ }
86dc4139 5475+ if (unlikely(err)) {
3c1bdaff 5476+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5477+ goto out;
5478+ }
5479+ h_src_attr->valid = 1;
e2f27e51
AM
5480+ if (!au_test_nfs(h_src_inode->i_sb)) {
5481+ err = au_cp_regular(cpg);
3c1bdaff 5482+ inode_unlock_shared(h_src_inode);
e2f27e51 5483+ } else {
3c1bdaff 5484+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5485+ err = au_cp_regular(cpg);
5486+ }
c2b27bf2 5487+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5488+ if (!err && rerr)
5489+ err = rerr;
1facf9fc 5490+ }
38d290e6
JR
5491+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5492+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5493+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5494+ spin_lock(&h_dst_inode->i_lock);
5495+ h_dst_inode->i_state |= I_LINKABLE;
5496+ spin_unlock(&h_dst_inode->i_lock);
5497+ }
1facf9fc 5498+
4f0767ce 5499+out:
1facf9fc 5500+ return err;
5501+}
5502+
5503+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5504+ struct inode *h_dir)
5505+{
5506+ int err, symlen;
5507+ mm_segment_t old_fs;
b752ccd1
AM
5508+ union {
5509+ char *k;
5510+ char __user *u;
5511+ } sym;
1facf9fc 5512+
5513+ err = -ENOMEM;
537831f9 5514+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5515+ if (unlikely(!sym.k))
1facf9fc 5516+ goto out;
5517+
9dbd164d 5518+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5519+ old_fs = get_fs();
5520+ set_fs(KERNEL_DS);
a2654f78 5521+ symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5522+ err = symlen;
5523+ set_fs(old_fs);
5524+
5525+ if (symlen > 0) {
b752ccd1
AM
5526+ sym.k[symlen] = 0;
5527+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5528+ }
1c60b727 5529+ free_page((unsigned long)sym.k);
1facf9fc 5530+
4f0767ce 5531+out:
1facf9fc 5532+ return err;
5533+}
5534+
8cdd5066
JR
5535+/*
5536+ * regardless 'acl' option, reset all ACL.
5537+ * All ACL will be copied up later from the original entry on the lower branch.
5538+ */
5539+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5540+{
5541+ int err;
5542+ struct dentry *h_dentry;
5543+ struct inode *h_inode;
5544+
5545+ h_dentry = h_path->dentry;
5546+ h_inode = d_inode(h_dentry);
5547+ /* forget_all_cached_acls(h_inode)); */
5548+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5549+ AuTraceErr(err);
5550+ if (err == -EOPNOTSUPP)
5551+ err = 0;
5552+ if (!err)
5553+ err = vfsub_acl_chmod(h_inode, mode);
5554+
5555+ AuTraceErr(err);
5556+ return err;
5557+}
5558+
5559+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5560+ struct inode *h_dir, struct path *h_path)
5561+{
5562+ int err;
5563+ struct inode *dir, *inode;
5564+
5565+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5566+ AuTraceErr(err);
5567+ if (err == -EOPNOTSUPP)
5568+ err = 0;
5569+ if (unlikely(err))
5570+ goto out;
5571+
5572+ /*
5573+ * strange behaviour from the users view,
5574+ * particularry setattr case
5575+ */
5576+ dir = d_inode(dst_parent);
5afbbe0d 5577+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5578+ au_cpup_attr_nlink(dir, /*force*/1);
5579+ inode = d_inode(cpg->dentry);
5580+ au_cpup_attr_nlink(inode, /*force*/1);
5581+
5582+out:
5583+ return err;
5584+}
5585+
1facf9fc 5586+static noinline_for_stack
c2b27bf2 5587+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5588+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5589+{
5590+ int err;
5591+ umode_t mode;
5592+ unsigned int mnt_flags;
076b876e 5593+ unsigned char isdir, isreg, force;
c2b27bf2 5594+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5595+ struct au_dtime dt;
5596+ struct path h_path;
5597+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5598+ struct inode *h_inode, *h_dir;
1facf9fc 5599+ struct super_block *sb;
5600+
5601+ /* bsrc branch can be ro/rw. */
c2b27bf2 5602+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5603+ h_inode = d_inode(h_src);
5604+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5605+
5606+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5607+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5608+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5609+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5610+ AUFS_WH_PFX_LEN));
1facf9fc 5611+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5612+ h_dir = d_inode(h_parent);
1facf9fc 5613+ IMustLock(h_dir);
5614+ AuDebugOn(h_parent != h_dst->d_parent);
5615+
c2b27bf2
AM
5616+ sb = cpg->dentry->d_sb;
5617+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5618+ if (do_dt) {
5619+ h_path.dentry = h_parent;
5620+ au_dtime_store(&dt, dst_parent, &h_path);
5621+ }
5622+ h_path.dentry = h_dst;
5623+
076b876e 5624+ isreg = 0;
1facf9fc 5625+ isdir = 0;
5626+ mode = h_inode->i_mode;
5627+ switch (mode & S_IFMT) {
5628+ case S_IFREG:
076b876e 5629+ isreg = 1;
8cdd5066 5630+ err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
b4510431 5631+ /*want_excl*/true);
1facf9fc 5632+ if (!err)
c2b27bf2 5633+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5634+ break;
5635+ case S_IFDIR:
5636+ isdir = 1;
5637+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5638+ if (!err)
5639+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5640+ break;
5641+ case S_IFLNK:
5642+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5643+ break;
5644+ case S_IFCHR:
5645+ case S_IFBLK:
5646+ AuDebugOn(!capable(CAP_MKNOD));
5647+ /*FALLTHROUGH*/
5648+ case S_IFIFO:
5649+ case S_IFSOCK:
5650+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5651+ break;
5652+ default:
5653+ AuIOErr("Unknown inode type 0%o\n", mode);
5654+ err = -EIO;
5655+ }
8cdd5066
JR
5656+ if (!err)
5657+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5658+
5659+ mnt_flags = au_mntflags(sb);
5660+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5661+ && !isdir
5662+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5663+ && (h_inode->i_nlink == 1
5664+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5665+ /* todo: unnecessary? */
5527c038 5666+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5667+ && cpg->bdst < cpg->bsrc
5668+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5669+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5670+ /* ignore this error */
5671+
076b876e
AM
5672+ if (!err) {
5673+ force = 0;
5674+ if (isreg) {
5675+ force = !!cpg->len;
5676+ if (cpg->len == -1)
5677+ force = !!i_size_read(h_inode);
5678+ }
5679+ au_fhsm_wrote(sb, cpg->bdst, force);
5680+ }
5681+
1facf9fc 5682+ if (do_dt)
5683+ au_dtime_revert(&dt);
5684+ return err;
5685+}
5686+
392086de 5687+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5688+{
5689+ int err;
392086de 5690+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5691+ struct inode *h_dir;
392086de 5692+ aufs_bindex_t bdst;
86dc4139 5693+
392086de
AM
5694+ dentry = cpg->dentry;
5695+ bdst = cpg->bdst;
5696+ h_dentry = au_h_dptr(dentry, bdst);
5697+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5698+ dget(h_dentry);
5699+ au_set_h_dptr(dentry, bdst, NULL);
5700+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5701+ if (!err)
5702+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5703+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5704+ } else {
5705+ err = 0;
5706+ parent = dget_parent(dentry);
5707+ h_parent = au_h_dptr(parent, bdst);
5708+ dput(parent);
5709+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5710+ if (IS_ERR(h_path->dentry))
5711+ err = PTR_ERR(h_path->dentry);
86dc4139 5712+ }
392086de
AM
5713+ if (unlikely(err))
5714+ goto out;
86dc4139 5715+
86dc4139 5716+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5717+ h_dir = d_inode(h_parent);
86dc4139 5718+ IMustLock(h_dir);
523b37e3
AM
5719+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5720+ /* no delegation since it is just created */
f2c43d5f
AM
5721+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5722+ /*flags*/0);
86dc4139
AM
5723+ dput(h_path->dentry);
5724+
5725+out:
5726+ return err;
5727+}
5728+
1facf9fc 5729+/*
5730+ * copyup the @dentry from @bsrc to @bdst.
5731+ * the caller must set the both of lower dentries.
5732+ * @len is for truncating when it is -1 copyup the entire file.
5733+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5734+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f
AM
5735+ * aufs doesn't touch the credential so
5736+ * security_inode_copy_up{,_xattr}() are unnecrssary.
1facf9fc 5737+ */
c2b27bf2 5738+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5739+{
5740+ int err, rerr;
5afbbe0d 5741+ aufs_bindex_t old_ibtop;
1facf9fc 5742+ unsigned char isdir, plink;
1facf9fc 5743+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5744+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5745+ struct super_block *sb;
86dc4139 5746+ struct au_branch *br;
c2b27bf2
AM
5747+ /* to reuduce stack size */
5748+ struct {
5749+ struct au_dtime dt;
5750+ struct path h_path;
5751+ struct au_cpup_reg_attr h_src_attr;
5752+ } *a;
1facf9fc 5753+
c2b27bf2
AM
5754+ err = -ENOMEM;
5755+ a = kmalloc(sizeof(*a), GFP_NOFS);
5756+ if (unlikely(!a))
5757+ goto out;
5758+ a->h_src_attr.valid = 0;
1facf9fc 5759+
c2b27bf2
AM
5760+ sb = cpg->dentry->d_sb;
5761+ br = au_sbr(sb, cpg->bdst);
5762+ a->h_path.mnt = au_br_mnt(br);
5763+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5764+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5765+ h_dir = d_inode(h_parent);
1facf9fc 5766+ IMustLock(h_dir);
5767+
c2b27bf2 5768+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5769+ inode = d_inode(cpg->dentry);
1facf9fc 5770+
5771+ if (!dst_parent)
c2b27bf2 5772+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5773+ else
5774+ dget(dst_parent);
5775+
5776+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5777+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5778+ if (dst_inode) {
5779+ if (unlikely(!plink)) {
5780+ err = -EIO;
027c5e7a
AM
5781+ AuIOErr("hi%lu(i%lu) exists on b%d "
5782+ "but plink is disabled\n",
c2b27bf2
AM
5783+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5784+ goto out_parent;
1facf9fc 5785+ }
5786+
5787+ if (dst_inode->i_nlink) {
c2b27bf2 5788+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5789+
c2b27bf2 5790+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5791+ err = PTR_ERR(h_src);
5792+ if (IS_ERR(h_src))
c2b27bf2 5793+ goto out_parent;
5527c038 5794+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5795+ err = -EIO;
79b8bda9 5796+ AuIOErr("i%lu exists on b%d "
027c5e7a 5797+ "but not pseudo-linked\n",
79b8bda9 5798+ inode->i_ino, cpg->bdst);
1facf9fc 5799+ dput(h_src);
c2b27bf2 5800+ goto out_parent;
1facf9fc 5801+ }
5802+
5803+ if (do_dt) {
c2b27bf2
AM
5804+ a->h_path.dentry = h_parent;
5805+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5806+ }
86dc4139 5807+
c2b27bf2 5808+ a->h_path.dentry = h_dst;
523b37e3
AM
5809+ delegated = NULL;
5810+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5811+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5812+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5813+ if (do_dt)
c2b27bf2 5814+ au_dtime_revert(&a->dt);
523b37e3
AM
5815+ if (unlikely(err == -EWOULDBLOCK)) {
5816+ pr_warn("cannot retry for NFSv4 delegation"
5817+ " for an internal link\n");
5818+ iput(delegated);
5819+ }
1facf9fc 5820+ dput(h_src);
c2b27bf2 5821+ goto out_parent;
1facf9fc 5822+ } else
5823+ /* todo: cpup_wh_file? */
5824+ /* udba work */
4a4d8108 5825+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5826+ }
5827+
86dc4139 5828+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5829+ old_ibtop = au_ibtop(inode);
c2b27bf2 5830+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5831+ if (unlikely(err))
86dc4139 5832+ goto out_rev;
5527c038 5833+ dst_inode = d_inode(h_dst);
febd17d6 5834+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5835+ /* todo: necessary? */
c2b27bf2 5836+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5837+
c2b27bf2 5838+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5839+ if (unlikely(err)) {
5840+ /* todo: necessary? */
c2b27bf2 5841+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5842+ inode_unlock(dst_inode);
86dc4139
AM
5843+ goto out_rev;
5844+ }
5845+
5afbbe0d 5846+ if (cpg->bdst < old_ibtop) {
86dc4139 5847+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5848+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5849+ if (unlikely(err)) {
c2b27bf2
AM
5850+ /* ignore an error */
5851+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5852+ inode_unlock(dst_inode);
86dc4139 5853+ goto out_rev;
4a4d8108 5854+ }
4a4d8108 5855+ }
5afbbe0d 5856+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5857+ } else
5afbbe0d 5858+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5859+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5860+ au_hi_flags(inode, isdir));
5861+
5862+ /* todo: necessary? */
c2b27bf2 5863+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5864+ inode_unlock(dst_inode);
86dc4139
AM
5865+ if (unlikely(err))
5866+ goto out_rev;
5867+
5527c038 5868+ src_inode = d_inode(h_src);
86dc4139 5869+ if (!isdir
5527c038
JR
5870+ && (src_inode->i_nlink > 1
5871+ || src_inode->i_state & I_LINKABLE)
86dc4139 5872+ && plink)
c2b27bf2 5873+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5874+
c2b27bf2
AM
5875+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5876+ a->h_path.dentry = h_dst;
392086de 5877+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5878+ }
5879+ if (!err)
c2b27bf2 5880+ goto out_parent; /* success */
1facf9fc 5881+
5882+ /* revert */
4a4d8108 5883+out_rev:
c2b27bf2
AM
5884+ a->h_path.dentry = h_parent;
5885+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5886+ a->h_path.dentry = h_dst;
86dc4139 5887+ rerr = 0;
5527c038 5888+ if (d_is_positive(h_dst)) {
523b37e3
AM
5889+ if (!isdir) {
5890+ /* no delegation since it is just created */
5891+ rerr = vfsub_unlink(h_dir, &a->h_path,
5892+ /*delegated*/NULL, /*force*/0);
5893+ } else
c2b27bf2 5894+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5895+ }
c2b27bf2 5896+ au_dtime_revert(&a->dt);
1facf9fc 5897+ if (rerr) {
5898+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5899+ err = -EIO;
5900+ }
c2b27bf2 5901+out_parent:
1facf9fc 5902+ dput(dst_parent);
1c60b727 5903+ kfree(a);
c2b27bf2 5904+out:
1facf9fc 5905+ return err;
5906+}
5907+
7e9cd9fe 5908+#if 0 /* reserved */
1facf9fc 5909+struct au_cpup_single_args {
5910+ int *errp;
c2b27bf2 5911+ struct au_cp_generic *cpg;
1facf9fc 5912+ struct dentry *dst_parent;
5913+};
5914+
5915+static void au_call_cpup_single(void *args)
5916+{
5917+ struct au_cpup_single_args *a = args;
86dc4139 5918+
c2b27bf2
AM
5919+ au_pin_hdir_acquire_nest(a->cpg->pin);
5920+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5921+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5922+}
c2b27bf2 5923+#endif
1facf9fc 5924+
53392da6
AM
5925+/*
5926+ * prevent SIGXFSZ in copy-up.
5927+ * testing CAP_MKNOD is for generic fs,
5928+ * but CAP_FSETID is for xfs only, currently.
5929+ */
86dc4139 5930+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5931+{
5932+ int do_sio;
86dc4139
AM
5933+ struct super_block *sb;
5934+ struct inode *h_dir;
53392da6
AM
5935+
5936+ do_sio = 0;
86dc4139 5937+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5938+ if (!au_wkq_test()
5939+ && (!au_sbi(sb)->si_plink_maint_pid
5940+ || au_plink_maint(sb, AuLock_NOPLM))) {
5941+ switch (mode & S_IFMT) {
5942+ case S_IFREG:
5943+ /* no condition about RLIMIT_FSIZE and the file size */
5944+ do_sio = 1;
5945+ break;
5946+ case S_IFCHR:
5947+ case S_IFBLK:
5948+ do_sio = !capable(CAP_MKNOD);
5949+ break;
5950+ }
5951+ if (!do_sio)
5952+ do_sio = ((mode & (S_ISUID | S_ISGID))
5953+ && !capable(CAP_FSETID));
86dc4139
AM
5954+ /* this workaround may be removed in the future */
5955+ if (!do_sio) {
5956+ h_dir = au_pinned_h_dir(pin);
5957+ do_sio = h_dir->i_mode & S_ISVTX;
5958+ }
53392da6
AM
5959+ }
5960+
5961+ return do_sio;
5962+}
5963+
7e9cd9fe 5964+#if 0 /* reserved */
c2b27bf2 5965+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5966+{
5967+ int err, wkq_err;
1facf9fc 5968+ struct dentry *h_dentry;
5969+
c2b27bf2 5970+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5971+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5972+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5973+ else {
5974+ struct au_cpup_single_args args = {
5975+ .errp = &err,
c2b27bf2
AM
5976+ .cpg = cpg,
5977+ .dst_parent = dst_parent
1facf9fc 5978+ };
5979+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5980+ if (unlikely(wkq_err))
5981+ err = wkq_err;
5982+ }
5983+
5984+ return err;
5985+}
c2b27bf2 5986+#endif
1facf9fc 5987+
5988+/*
5989+ * copyup the @dentry from the first active lower branch to @bdst,
5990+ * using au_cpup_single().
5991+ */
c2b27bf2 5992+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5993+{
5994+ int err;
c2b27bf2
AM
5995+ unsigned int flags_orig;
5996+ struct dentry *dentry;
5997+
5998+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 5999+
c2b27bf2 6000+ dentry = cpg->dentry;
86dc4139 6001+ DiMustWriteLock(dentry);
1facf9fc 6002+
c2b27bf2 6003+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 6004+ if (!err) {
c2b27bf2
AM
6005+ flags_orig = cpg->flags;
6006+ au_fset_cpup(cpg->flags, RENAME);
6007+ err = au_cpup_single(cpg, NULL);
6008+ cpg->flags = flags_orig;
1facf9fc 6009+ if (!err)
6010+ return 0; /* success */
6011+
6012+ /* revert */
c2b27bf2 6013+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 6014+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 6015+ }
6016+
6017+ return err;
6018+}
6019+
6020+struct au_cpup_simple_args {
6021+ int *errp;
c2b27bf2 6022+ struct au_cp_generic *cpg;
1facf9fc 6023+};
6024+
6025+static void au_call_cpup_simple(void *args)
6026+{
6027+ struct au_cpup_simple_args *a = args;
86dc4139 6028+
c2b27bf2
AM
6029+ au_pin_hdir_acquire_nest(a->cpg->pin);
6030+ *a->errp = au_cpup_simple(a->cpg);
6031+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6032+}
6033+
c2b27bf2 6034+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6035+{
6036+ int err, wkq_err;
c2b27bf2
AM
6037+ struct dentry *dentry, *parent;
6038+ struct file *h_file;
1facf9fc 6039+ struct inode *h_dir;
6040+
c2b27bf2
AM
6041+ dentry = cpg->dentry;
6042+ h_file = NULL;
6043+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
6044+ AuDebugOn(cpg->bsrc < 0);
392086de 6045+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
6046+ err = PTR_ERR(h_file);
6047+ if (IS_ERR(h_file))
6048+ goto out;
6049+ }
6050+
1facf9fc 6051+ parent = dget_parent(dentry);
5527c038 6052+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 6053+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 6054+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6055+ err = au_cpup_simple(cpg);
1facf9fc 6056+ else {
6057+ struct au_cpup_simple_args args = {
6058+ .errp = &err,
c2b27bf2 6059+ .cpg = cpg
1facf9fc 6060+ };
6061+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6062+ if (unlikely(wkq_err))
6063+ err = wkq_err;
6064+ }
6065+
6066+ dput(parent);
c2b27bf2
AM
6067+ if (h_file)
6068+ au_h_open_post(dentry, cpg->bsrc, h_file);
6069+
6070+out:
1facf9fc 6071+ return err;
6072+}
6073+
c2b27bf2 6074+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 6075+{
5afbbe0d 6076+ aufs_bindex_t bsrc, bbot;
c2b27bf2 6077+ struct dentry *dentry, *h_dentry;
367653fa 6078+
c2b27bf2
AM
6079+ if (cpg->bsrc < 0) {
6080+ dentry = cpg->dentry;
5afbbe0d
AM
6081+ bbot = au_dbbot(dentry);
6082+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
6083+ h_dentry = au_h_dptr(dentry, bsrc);
6084+ if (h_dentry) {
5527c038 6085+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
6086+ break;
6087+ }
6088+ }
5afbbe0d 6089+ AuDebugOn(bsrc > bbot);
c2b27bf2 6090+ cpg->bsrc = bsrc;
367653fa 6091+ }
c2b27bf2
AM
6092+ AuDebugOn(cpg->bsrc <= cpg->bdst);
6093+ return au_do_sio_cpup_simple(cpg);
6094+}
367653fa 6095+
c2b27bf2
AM
6096+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6097+{
6098+ AuDebugOn(cpg->bdst <= cpg->bsrc);
6099+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
6100+}
6101+
1facf9fc 6102+/* ---------------------------------------------------------------------- */
6103+
6104+/*
6105+ * copyup the deleted file for writing.
6106+ */
c2b27bf2
AM
6107+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6108+ struct file *file)
1facf9fc 6109+{
6110+ int err;
c2b27bf2
AM
6111+ unsigned int flags_orig;
6112+ aufs_bindex_t bsrc_orig;
c2b27bf2 6113+ struct au_dinfo *dinfo;
5afbbe0d
AM
6114+ struct {
6115+ struct au_hdentry *hd;
6116+ struct dentry *h_dentry;
6117+ } hdst, hsrc;
1facf9fc 6118+
c2b27bf2 6119+ dinfo = au_di(cpg->dentry);
1308ab2a 6120+ AuRwMustWriteLock(&dinfo->di_rwsem);
6121+
c2b27bf2 6122+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
6123+ cpg->bsrc = dinfo->di_btop;
6124+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
6125+ hdst.h_dentry = hdst.hd->hd_dentry;
6126+ hdst.hd->hd_dentry = wh_dentry;
6127+ dinfo->di_btop = cpg->bdst;
6128+
6129+ hsrc.h_dentry = NULL;
027c5e7a 6130+ if (file) {
5afbbe0d
AM
6131+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6132+ hsrc.h_dentry = hsrc.hd->hd_dentry;
6133+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 6134+ }
c2b27bf2
AM
6135+ flags_orig = cpg->flags;
6136+ cpg->flags = !AuCpup_DTIME;
6137+ err = au_cpup_single(cpg, /*h_parent*/NULL);
6138+ cpg->flags = flags_orig;
027c5e7a
AM
6139+ if (file) {
6140+ if (!err)
6141+ err = au_reopen_nondir(file);
5afbbe0d 6142+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 6143+ }
5afbbe0d
AM
6144+ hdst.hd->hd_dentry = hdst.h_dentry;
6145+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 6146+ cpg->bsrc = bsrc_orig;
1facf9fc 6147+
6148+ return err;
6149+}
6150+
c2b27bf2 6151+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6152+{
6153+ int err;
c2b27bf2 6154+ aufs_bindex_t bdst;
1facf9fc 6155+ struct au_dtime dt;
c2b27bf2 6156+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6157+ struct au_branch *br;
6158+ struct path h_path;
6159+
c2b27bf2
AM
6160+ dentry = cpg->dentry;
6161+ bdst = cpg->bdst;
1facf9fc 6162+ br = au_sbr(dentry->d_sb, bdst);
6163+ parent = dget_parent(dentry);
6164+ h_parent = au_h_dptr(parent, bdst);
6165+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6166+ err = PTR_ERR(wh_dentry);
6167+ if (IS_ERR(wh_dentry))
6168+ goto out;
6169+
6170+ h_path.dentry = h_parent;
86dc4139 6171+ h_path.mnt = au_br_mnt(br);
1facf9fc 6172+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6173+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6174+ if (unlikely(err))
6175+ goto out_wh;
6176+
6177+ dget(wh_dentry);
6178+ h_path.dentry = wh_dentry;
2000de60 6179+ if (!d_is_dir(wh_dentry)) {
523b37e3 6180+ /* no delegation since it is just created */
5527c038 6181+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6182+ /*delegated*/NULL, /*force*/0);
6183+ } else
5527c038 6184+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6185+ if (unlikely(err)) {
523b37e3
AM
6186+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6187+ wh_dentry, err);
1facf9fc 6188+ err = -EIO;
6189+ }
6190+ au_dtime_revert(&dt);
5527c038 6191+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6192+
4f0767ce 6193+out_wh:
1facf9fc 6194+ dput(wh_dentry);
4f0767ce 6195+out:
1facf9fc 6196+ dput(parent);
6197+ return err;
6198+}
6199+
6200+struct au_cpup_wh_args {
6201+ int *errp;
c2b27bf2 6202+ struct au_cp_generic *cpg;
1facf9fc 6203+ struct file *file;
6204+};
6205+
6206+static void au_call_cpup_wh(void *args)
6207+{
6208+ struct au_cpup_wh_args *a = args;
86dc4139 6209+
c2b27bf2
AM
6210+ au_pin_hdir_acquire_nest(a->cpg->pin);
6211+ *a->errp = au_cpup_wh(a->cpg, a->file);
6212+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6213+}
6214+
c2b27bf2 6215+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6216+{
6217+ int err, wkq_err;
c2b27bf2 6218+ aufs_bindex_t bdst;
c1595e42 6219+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6220+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6221+ struct au_wbr *wbr;
c2b27bf2 6222+ struct au_pin wh_pin, *pin_orig;
1facf9fc 6223+
c2b27bf2
AM
6224+ dentry = cpg->dentry;
6225+ bdst = cpg->bdst;
1facf9fc 6226+ parent = dget_parent(dentry);
5527c038 6227+ dir = d_inode(parent);
1facf9fc 6228+ h_orph = NULL;
6229+ h_parent = NULL;
6230+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6231+ h_tmpdir = h_dir;
c2b27bf2 6232+ pin_orig = NULL;
1facf9fc 6233+ if (!h_dir->i_nlink) {
6234+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6235+ h_orph = wbr->wbr_orph;
6236+
6237+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6238+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6239+ h_tmpdir = d_inode(h_orph);
1facf9fc 6240+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6241+
febd17d6 6242+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6243+ /* todo: au_h_open_pre()? */
86dc4139 6244+
c2b27bf2 6245+ pin_orig = cpg->pin;
86dc4139 6246+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6247+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6248+ cpg->pin = &wh_pin;
1facf9fc 6249+ }
6250+
53392da6 6251+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6252+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6253+ err = au_cpup_wh(cpg, file);
1facf9fc 6254+ else {
6255+ struct au_cpup_wh_args args = {
6256+ .errp = &err,
c2b27bf2
AM
6257+ .cpg = cpg,
6258+ .file = file
1facf9fc 6259+ };
6260+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6261+ if (unlikely(wkq_err))
6262+ err = wkq_err;
6263+ }
6264+
6265+ if (h_orph) {
febd17d6 6266+ inode_unlock(h_tmpdir);
4a4d8108 6267+ /* todo: au_h_open_post()? */
1facf9fc 6268+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6269+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6270+ AuDebugOn(!pin_orig);
6271+ cpg->pin = pin_orig;
1facf9fc 6272+ }
6273+ iput(h_dir);
6274+ dput(parent);
6275+
6276+ return err;
6277+}
6278+
6279+/* ---------------------------------------------------------------------- */
6280+
6281+/*
6282+ * generic routine for both of copy-up and copy-down.
6283+ */
6284+/* cf. revalidate function in file.c */
6285+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6286+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6287+ struct au_pin *pin,
1facf9fc 6288+ struct dentry *h_parent, void *arg),
6289+ void *arg)
6290+{
6291+ int err;
6292+ struct au_pin pin;
5527c038 6293+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6294+
6295+ err = 0;
6296+ parent = dget_parent(dentry);
6297+ if (IS_ROOT(parent))
6298+ goto out;
6299+
6300+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6301+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6302+
6303+ /* do not use au_dpage */
6304+ real_parent = parent;
6305+ while (1) {
6306+ dput(parent);
6307+ parent = dget_parent(dentry);
6308+ h_parent = au_h_dptr(parent, bdst);
6309+ if (h_parent)
6310+ goto out; /* success */
6311+
6312+ /* find top dir which is necessary to cpup */
6313+ do {
6314+ d = parent;
6315+ dput(parent);
6316+ parent = dget_parent(d);
6317+ di_read_lock_parent3(parent, !AuLock_IR);
6318+ h_parent = au_h_dptr(parent, bdst);
6319+ di_read_unlock(parent, !AuLock_IR);
6320+ } while (!h_parent);
6321+
6322+ if (d != real_parent)
6323+ di_write_lock_child3(d);
6324+
6325+ /* somebody else might create while we were sleeping */
5527c038
JR
6326+ h_dentry = au_h_dptr(d, bdst);
6327+ if (!h_dentry || d_is_negative(h_dentry)) {
6328+ if (h_dentry)
5afbbe0d 6329+ au_update_dbtop(d);
1facf9fc 6330+
6331+ au_pin_set_dentry(&pin, d);
6332+ err = au_do_pin(&pin);
6333+ if (!err) {
86dc4139 6334+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6335+ au_unpin(&pin);
6336+ }
6337+ }
6338+
6339+ if (d != real_parent)
6340+ di_write_unlock(d);
6341+ if (unlikely(err))
6342+ break;
6343+ }
6344+
4f0767ce 6345+out:
1facf9fc 6346+ dput(parent);
6347+ return err;
6348+}
6349+
6350+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6351+ struct au_pin *pin,
2000de60 6352+ struct dentry *h_parent __maybe_unused,
1facf9fc 6353+ void *arg __maybe_unused)
6354+{
c2b27bf2
AM
6355+ struct au_cp_generic cpg = {
6356+ .dentry = dentry,
6357+ .bdst = bdst,
6358+ .bsrc = -1,
6359+ .len = 0,
6360+ .pin = pin,
6361+ .flags = AuCpup_DTIME
6362+ };
6363+ return au_sio_cpup_simple(&cpg);
1facf9fc 6364+}
6365+
6366+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6367+{
6368+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6369+}
6370+
6371+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6372+{
6373+ int err;
6374+ struct dentry *parent;
6375+ struct inode *dir;
6376+
6377+ parent = dget_parent(dentry);
5527c038 6378+ dir = d_inode(parent);
1facf9fc 6379+ err = 0;
6380+ if (au_h_iptr(dir, bdst))
6381+ goto out;
6382+
6383+ di_read_unlock(parent, AuLock_IR);
6384+ di_write_lock_parent(parent);
6385+ /* someone else might change our inode while we were sleeping */
6386+ if (!au_h_iptr(dir, bdst))
6387+ err = au_cpup_dirs(dentry, bdst);
6388+ di_downgrade_lock(parent, AuLock_IR);
6389+
4f0767ce 6390+out:
1facf9fc 6391+ dput(parent);
6392+ return err;
6393+}
7f207e10
AM
6394diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6395--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 6396+++ linux/fs/aufs/cpup.h 2018-04-15 08:49:13.397817296 +0200
8b6a4947 6397@@ -0,0 +1,99 @@
1facf9fc 6398+/*
b00004a5 6399+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6400+ *
6401+ * This program, aufs is free software; you can redistribute it and/or modify
6402+ * it under the terms of the GNU General Public License as published by
6403+ * the Free Software Foundation; either version 2 of the License, or
6404+ * (at your option) any later version.
dece6358
AM
6405+ *
6406+ * This program is distributed in the hope that it will be useful,
6407+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6408+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6409+ * GNU General Public License for more details.
6410+ *
6411+ * You should have received a copy of the GNU General Public License
523b37e3 6412+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6413+ */
6414+
6415+/*
6416+ * copy-up/down functions
6417+ */
6418+
6419+#ifndef __AUFS_CPUP_H__
6420+#define __AUFS_CPUP_H__
6421+
6422+#ifdef __KERNEL__
6423+
dece6358 6424+#include <linux/path.h>
1facf9fc 6425+
dece6358
AM
6426+struct inode;
6427+struct file;
86dc4139 6428+struct au_pin;
dece6358 6429+
86dc4139 6430+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6431+void au_cpup_attr_timesizes(struct inode *inode);
6432+void au_cpup_attr_nlink(struct inode *inode, int force);
6433+void au_cpup_attr_changeable(struct inode *inode);
6434+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6435+void au_cpup_attr_all(struct inode *inode, int force);
6436+
6437+/* ---------------------------------------------------------------------- */
6438+
c2b27bf2
AM
6439+struct au_cp_generic {
6440+ struct dentry *dentry;
6441+ aufs_bindex_t bdst, bsrc;
6442+ loff_t len;
6443+ struct au_pin *pin;
6444+ unsigned int flags;
6445+};
6446+
1facf9fc 6447+/* cpup flags */
392086de
AM
6448+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6449+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6450+ for link(2) */
6451+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6452+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6453+ cpup */
6454+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6455+ existing entry */
6456+#define AuCpup_RWDST (1 << 5) /* force write target even if
6457+ the branch is marked as RO */
c2b27bf2 6458+
8b6a4947
AM
6459+#ifndef CONFIG_AUFS_BR_HFSPLUS
6460+#undef AuCpup_HOPEN
6461+#define AuCpup_HOPEN 0
6462+#endif
6463+
1facf9fc 6464+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6465+#define au_fset_cpup(flags, name) \
6466+ do { (flags) |= AuCpup_##name; } while (0)
6467+#define au_fclr_cpup(flags, name) \
6468+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6469+
6470+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6471+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6472+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6473+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6474+
6475+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6476+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6477+ struct au_pin *pin,
1facf9fc 6478+ struct dentry *h_parent, void *arg),
6479+ void *arg);
6480+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6481+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6482+
6483+/* ---------------------------------------------------------------------- */
6484+
6485+/* keep timestamps when copyup */
6486+struct au_dtime {
6487+ struct dentry *dt_dentry;
6488+ struct path dt_h_path;
6489+ struct timespec dt_atime, dt_mtime;
6490+};
6491+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6492+ struct path *h_path);
6493+void au_dtime_revert(struct au_dtime *dt);
6494+
6495+#endif /* __KERNEL__ */
6496+#endif /* __AUFS_CPUP_H__ */
7f207e10
AM
6497diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6498--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 6499+++ linux/fs/aufs/dbgaufs.c 2018-06-04 09:08:09.181412645 +0200
8b6a4947 6500@@ -0,0 +1,437 @@
1facf9fc 6501+/*
b00004a5 6502+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6503+ *
6504+ * This program, aufs is free software; you can redistribute it and/or modify
6505+ * it under the terms of the GNU General Public License as published by
6506+ * the Free Software Foundation; either version 2 of the License, or
6507+ * (at your option) any later version.
dece6358
AM
6508+ *
6509+ * This program is distributed in the hope that it will be useful,
6510+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6511+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6512+ * GNU General Public License for more details.
6513+ *
6514+ * You should have received a copy of the GNU General Public License
523b37e3 6515+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6516+ */
6517+
6518+/*
6519+ * debugfs interface
6520+ */
6521+
6522+#include <linux/debugfs.h>
6523+#include "aufs.h"
6524+
6525+#ifndef CONFIG_SYSFS
6526+#error DEBUG_FS depends upon SYSFS
6527+#endif
6528+
6529+static struct dentry *dbgaufs;
6530+static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
6531+
6532+/* 20 is max digits length of ulong 64 */
6533+struct dbgaufs_arg {
6534+ int n;
6535+ char a[20 * 4];
6536+};
6537+
6538+/*
6539+ * common function for all XINO files
6540+ */
6541+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6542+ struct file *file)
6543+{
1c60b727 6544+ kfree(file->private_data);
1facf9fc 6545+ return 0;
6546+}
6547+
6548+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
6549+{
6550+ int err;
6551+ struct kstat st;
6552+ struct dbgaufs_arg *p;
6553+
6554+ err = -ENOMEM;
6555+ p = kmalloc(sizeof(*p), GFP_NOFS);
6556+ if (unlikely(!p))
6557+ goto out;
6558+
6559+ err = 0;
6560+ p->n = 0;
6561+ file->private_data = p;
6562+ if (!xf)
6563+ goto out;
6564+
521ced18 6565+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6566+ if (!err) {
6567+ if (do_fcnt)
6568+ p->n = snprintf
521ced18 6569+ (p->a, sizeof(p->a), "%ld, %llux%u %lld\n",
1facf9fc 6570+ (long)file_count(xf), st.blocks, st.blksize,
6571+ (long long)st.size);
6572+ else
521ced18 6573+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6574+ st.blocks, st.blksize,
6575+ (long long)st.size);
6576+ AuDebugOn(p->n >= sizeof(p->a));
6577+ } else {
6578+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6579+ err = 0;
6580+ }
6581+
4f0767ce 6582+out:
1facf9fc 6583+ return err;
6584+
6585+}
6586+
6587+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6588+ size_t count, loff_t *ppos)
6589+{
6590+ struct dbgaufs_arg *p;
6591+
6592+ p = file->private_data;
6593+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6594+}
6595+
6596+/* ---------------------------------------------------------------------- */
6597+
86dc4139
AM
6598+struct dbgaufs_plink_arg {
6599+ int n;
6600+ char a[];
6601+};
6602+
6603+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6604+ struct file *file)
6605+{
1c60b727 6606+ free_page((unsigned long)file->private_data);
86dc4139
AM
6607+ return 0;
6608+}
6609+
6610+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6611+{
6612+ int err, i, limit;
6613+ unsigned long n, sum;
6614+ struct dbgaufs_plink_arg *p;
6615+ struct au_sbinfo *sbinfo;
6616+ struct super_block *sb;
8b6a4947 6617+ struct hlist_bl_head *hbl;
86dc4139
AM
6618+
6619+ err = -ENOMEM;
6620+ p = (void *)get_zeroed_page(GFP_NOFS);
6621+ if (unlikely(!p))
6622+ goto out;
6623+
6624+ err = -EFBIG;
6625+ sbinfo = inode->i_private;
6626+ sb = sbinfo->si_sb;
6627+ si_noflush_read_lock(sb);
6628+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6629+ limit = PAGE_SIZE - sizeof(p->n);
6630+
6631+ /* the number of buckets */
6632+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6633+ p->n += n;
6634+ limit -= n;
6635+
6636+ sum = 0;
8b6a4947
AM
6637+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6638+ i++, hbl++) {
6639+ n = au_hbl_count(hbl);
86dc4139
AM
6640+ sum += n;
6641+
6642+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6643+ p->n += n;
6644+ limit -= n;
6645+ if (unlikely(limit <= 0))
6646+ goto out_free;
6647+ }
6648+ p->a[p->n - 1] = '\n';
6649+
6650+ /* the sum of plinks */
6651+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6652+ p->n += n;
6653+ limit -= n;
6654+ if (unlikely(limit <= 0))
6655+ goto out_free;
6656+ } else {
6657+#define str "1\n0\n0\n"
6658+ p->n = sizeof(str) - 1;
6659+ strcpy(p->a, str);
6660+#undef str
6661+ }
6662+ si_read_unlock(sb);
6663+
6664+ err = 0;
6665+ file->private_data = p;
6666+ goto out; /* success */
6667+
6668+out_free:
1c60b727 6669+ free_page((unsigned long)p);
86dc4139
AM
6670+out:
6671+ return err;
6672+}
6673+
6674+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6675+ size_t count, loff_t *ppos)
6676+{
6677+ struct dbgaufs_plink_arg *p;
6678+
6679+ p = file->private_data;
6680+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6681+}
6682+
6683+static const struct file_operations dbgaufs_plink_fop = {
6684+ .owner = THIS_MODULE,
6685+ .open = dbgaufs_plink_open,
6686+ .release = dbgaufs_plink_release,
6687+ .read = dbgaufs_plink_read
6688+};
6689+
6690+/* ---------------------------------------------------------------------- */
6691+
1facf9fc 6692+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6693+{
6694+ int err;
6695+ struct au_sbinfo *sbinfo;
6696+ struct super_block *sb;
6697+
6698+ sbinfo = inode->i_private;
6699+ sb = sbinfo->si_sb;
6700+ si_noflush_read_lock(sb);
6701+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
6702+ si_read_unlock(sb);
6703+ return err;
6704+}
6705+
6706+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6707+ .owner = THIS_MODULE,
1facf9fc 6708+ .open = dbgaufs_xib_open,
6709+ .release = dbgaufs_xi_release,
6710+ .read = dbgaufs_xi_read
6711+};
6712+
6713+/* ---------------------------------------------------------------------- */
6714+
6715+#define DbgaufsXi_PREFIX "xi"
6716+
6717+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6718+{
6719+ int err;
6720+ long l;
6721+ struct au_sbinfo *sbinfo;
6722+ struct super_block *sb;
6723+ struct file *xf;
6724+ struct qstr *name;
6725+
6726+ err = -ENOENT;
6727+ xf = NULL;
2000de60 6728+ name = &file->f_path.dentry->d_name;
1facf9fc 6729+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6730+ || memcmp(name->name, DbgaufsXi_PREFIX,
6731+ sizeof(DbgaufsXi_PREFIX) - 1)))
6732+ goto out;
9dbd164d 6733+ err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6734+ if (unlikely(err))
6735+ goto out;
6736+
6737+ sbinfo = inode->i_private;
6738+ sb = sbinfo->si_sb;
6739+ si_noflush_read_lock(sb);
5afbbe0d 6740+ if (l <= au_sbbot(sb)) {
1facf9fc 6741+ xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
6742+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
6743+ } else
6744+ err = -ENOENT;
6745+ si_read_unlock(sb);
6746+
4f0767ce 6747+out:
1facf9fc 6748+ return err;
6749+}
6750+
6751+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6752+ .owner = THIS_MODULE,
1facf9fc 6753+ .open = dbgaufs_xino_open,
6754+ .release = dbgaufs_xi_release,
6755+ .read = dbgaufs_xi_read
6756+};
6757+
6758+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6759+{
5afbbe0d 6760+ aufs_bindex_t bbot;
1facf9fc 6761+ struct au_branch *br;
6762+ struct au_xino_file *xi;
6763+
6764+ if (!au_sbi(sb)->si_dbgaufs)
6765+ return;
6766+
5afbbe0d
AM
6767+ bbot = au_sbbot(sb);
6768+ for (; bindex <= bbot; bindex++) {
1facf9fc 6769+ br = au_sbr(sb, bindex);
6770+ xi = &br->br_xino;
e2f27e51
AM
6771+ /* debugfs acquires the parent i_mutex */
6772+ lockdep_off();
c06a8ce3 6773+ debugfs_remove(xi->xi_dbgaufs);
e2f27e51 6774+ lockdep_on();
c06a8ce3 6775+ xi->xi_dbgaufs = NULL;
1facf9fc 6776+ }
6777+}
6778+
6779+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6780+{
6781+ struct au_sbinfo *sbinfo;
6782+ struct dentry *parent;
6783+ struct au_branch *br;
6784+ struct au_xino_file *xi;
5afbbe0d 6785+ aufs_bindex_t bbot;
1facf9fc 6786+ char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6787+
6788+ sbinfo = au_sbi(sb);
6789+ parent = sbinfo->si_dbgaufs;
6790+ if (!parent)
6791+ return;
6792+
5afbbe0d
AM
6793+ bbot = au_sbbot(sb);
6794+ for (; bindex <= bbot; bindex++) {
1facf9fc 6795+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6796+ br = au_sbr(sb, bindex);
6797+ xi = &br->br_xino;
6798+ AuDebugOn(xi->xi_dbgaufs);
f0c0a007
AM
6799+ /* debugfs acquires the parent i_mutex */
6800+ lockdep_off();
1facf9fc 6801+ xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6802+ sbinfo, &dbgaufs_xino_fop);
f0c0a007 6803+ lockdep_on();
1facf9fc 6804+ /* ignore an error */
6805+ if (unlikely(!xi->xi_dbgaufs))
6806+ AuWarn1("failed %s under debugfs\n", name);
6807+ }
6808+}
6809+
6810+/* ---------------------------------------------------------------------- */
6811+
6812+#ifdef CONFIG_AUFS_EXPORT
6813+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6814+{
6815+ int err;
6816+ struct au_sbinfo *sbinfo;
6817+ struct super_block *sb;
6818+
6819+ sbinfo = inode->i_private;
6820+ sb = sbinfo->si_sb;
6821+ si_noflush_read_lock(sb);
6822+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6823+ si_read_unlock(sb);
6824+ return err;
6825+}
6826+
6827+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6828+ .owner = THIS_MODULE,
1facf9fc 6829+ .open = dbgaufs_xigen_open,
6830+ .release = dbgaufs_xi_release,
6831+ .read = dbgaufs_xi_read
6832+};
6833+
6834+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6835+{
6836+ int err;
6837+
dece6358 6838+ /*
c1595e42 6839+ * This function is a dynamic '__init' function actually,
dece6358
AM
6840+ * so the tiny check for si_rwsem is unnecessary.
6841+ */
6842+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6843+
1facf9fc 6844+ err = -EIO;
6845+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6846+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6847+ &dbgaufs_xigen_fop);
6848+ if (sbinfo->si_dbgaufs_xigen)
6849+ err = 0;
6850+
6851+ return err;
6852+}
6853+#else
6854+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6855+{
6856+ return 0;
6857+}
6858+#endif /* CONFIG_AUFS_EXPORT */
6859+
6860+/* ---------------------------------------------------------------------- */
6861+
6862+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6863+{
dece6358 6864+ /*
7e9cd9fe 6865+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6866+ * so the tiny check for si_rwsem is unnecessary.
6867+ */
6868+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6869+
1facf9fc 6870+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6871+ sbinfo->si_dbgaufs = NULL;
6872+ kobject_put(&sbinfo->si_kobj);
6873+}
6874+
6875+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6876+{
6877+ int err;
6878+ char name[SysaufsSiNameLen];
6879+
dece6358 6880+ /*
c1595e42 6881+ * This function is a dynamic '__init' function actually,
dece6358
AM
6882+ * so the tiny check for si_rwsem is unnecessary.
6883+ */
6884+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6885+
1facf9fc 6886+ err = -ENOENT;
6887+ if (!dbgaufs) {
6888+ AuErr1("/debug/aufs is uninitialized\n");
6889+ goto out;
6890+ }
6891+
6892+ err = -EIO;
6893+ sysaufs_name(sbinfo, name);
6894+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6895+ if (unlikely(!sbinfo->si_dbgaufs))
6896+ goto out;
6897+ kobject_get(&sbinfo->si_kobj);
6898+
6899+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6900+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6901+ &dbgaufs_xib_fop);
6902+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6903+ goto out_dir;
6904+
86dc4139
AM
6905+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6906+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6907+ &dbgaufs_plink_fop);
6908+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6909+ goto out_dir;
6910+
1facf9fc 6911+ err = dbgaufs_xigen_init(sbinfo);
6912+ if (!err)
6913+ goto out; /* success */
6914+
4f0767ce 6915+out_dir:
1facf9fc 6916+ dbgaufs_si_fin(sbinfo);
4f0767ce 6917+out:
1facf9fc 6918+ return err;
6919+}
6920+
6921+/* ---------------------------------------------------------------------- */
6922+
6923+void dbgaufs_fin(void)
6924+{
6925+ debugfs_remove(dbgaufs);
6926+}
6927+
6928+int __init dbgaufs_init(void)
6929+{
6930+ int err;
6931+
6932+ err = -EIO;
6933+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6934+ if (dbgaufs)
6935+ err = 0;
6936+ return err;
6937+}
7f207e10
AM
6938diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6939--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 6940+++ linux/fs/aufs/dbgaufs.h 2018-04-15 08:49:13.397817296 +0200
523b37e3 6941@@ -0,0 +1,48 @@
1facf9fc 6942+/*
b00004a5 6943+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6944+ *
6945+ * This program, aufs is free software; you can redistribute it and/or modify
6946+ * it under the terms of the GNU General Public License as published by
6947+ * the Free Software Foundation; either version 2 of the License, or
6948+ * (at your option) any later version.
dece6358
AM
6949+ *
6950+ * This program is distributed in the hope that it will be useful,
6951+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6952+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6953+ * GNU General Public License for more details.
6954+ *
6955+ * You should have received a copy of the GNU General Public License
523b37e3 6956+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6957+ */
6958+
6959+/*
6960+ * debugfs interface
6961+ */
6962+
6963+#ifndef __DBGAUFS_H__
6964+#define __DBGAUFS_H__
6965+
6966+#ifdef __KERNEL__
6967+
dece6358 6968+struct super_block;
1facf9fc 6969+struct au_sbinfo;
dece6358 6970+
1facf9fc 6971+#ifdef CONFIG_DEBUG_FS
6972+/* dbgaufs.c */
6973+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6974+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6975+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6976+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6977+void dbgaufs_fin(void);
6978+int __init dbgaufs_init(void);
1facf9fc 6979+#else
4a4d8108
AM
6980+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6981+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6982+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6983+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6984+AuStubVoid(dbgaufs_fin, void)
6985+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 6986+#endif /* CONFIG_DEBUG_FS */
6987+
6988+#endif /* __KERNEL__ */
6989+#endif /* __DBGAUFS_H__ */
7f207e10
AM
6990diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6991--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 6992+++ linux/fs/aufs/dcsub.c 2018-04-15 08:49:13.397817296 +0200
e2f27e51 6993@@ -0,0 +1,225 @@
1facf9fc 6994+/*
b00004a5 6995+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6996+ *
6997+ * This program, aufs is free software; you can redistribute it and/or modify
6998+ * it under the terms of the GNU General Public License as published by
6999+ * the Free Software Foundation; either version 2 of the License, or
7000+ * (at your option) any later version.
dece6358
AM
7001+ *
7002+ * This program is distributed in the hope that it will be useful,
7003+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7004+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7005+ * GNU General Public License for more details.
7006+ *
7007+ * You should have received a copy of the GNU General Public License
523b37e3 7008+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7009+ */
7010+
7011+/*
7012+ * sub-routines for dentry cache
7013+ */
7014+
7015+#include "aufs.h"
7016+
7017+static void au_dpage_free(struct au_dpage *dpage)
7018+{
7019+ int i;
7020+ struct dentry **p;
7021+
7022+ p = dpage->dentries;
7023+ for (i = 0; i < dpage->ndentry; i++)
7024+ dput(*p++);
1c60b727 7025+ free_page((unsigned long)dpage->dentries);
1facf9fc 7026+}
7027+
7028+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7029+{
7030+ int err;
7031+ void *p;
7032+
7033+ err = -ENOMEM;
7034+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7035+ if (unlikely(!dpages->dpages))
7036+ goto out;
7037+
7038+ p = (void *)__get_free_page(gfp);
7039+ if (unlikely(!p))
7040+ goto out_dpages;
7041+
7042+ dpages->dpages[0].ndentry = 0;
7043+ dpages->dpages[0].dentries = p;
7044+ dpages->ndpage = 1;
7045+ return 0; /* success */
7046+
4f0767ce 7047+out_dpages:
1c60b727 7048+ kfree(dpages->dpages);
4f0767ce 7049+out:
1facf9fc 7050+ return err;
7051+}
7052+
7053+void au_dpages_free(struct au_dcsub_pages *dpages)
7054+{
7055+ int i;
7056+ struct au_dpage *p;
7057+
7058+ p = dpages->dpages;
7059+ for (i = 0; i < dpages->ndpage; i++)
7060+ au_dpage_free(p++);
1c60b727 7061+ kfree(dpages->dpages);
1facf9fc 7062+}
7063+
7064+static int au_dpages_append(struct au_dcsub_pages *dpages,
7065+ struct dentry *dentry, gfp_t gfp)
7066+{
7067+ int err, sz;
7068+ struct au_dpage *dpage;
7069+ void *p;
7070+
7071+ dpage = dpages->dpages + dpages->ndpage - 1;
7072+ sz = PAGE_SIZE / sizeof(dentry);
7073+ if (unlikely(dpage->ndentry >= sz)) {
7074+ AuLabel(new dpage);
7075+ err = -ENOMEM;
7076+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7077+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7078+ sz + sizeof(*dpages->dpages), gfp,
7079+ /*may_shrink*/0);
1facf9fc 7080+ if (unlikely(!p))
7081+ goto out;
7082+
7083+ dpages->dpages = p;
7084+ dpage = dpages->dpages + dpages->ndpage;
7085+ p = (void *)__get_free_page(gfp);
7086+ if (unlikely(!p))
7087+ goto out;
7088+
7089+ dpage->ndentry = 0;
7090+ dpage->dentries = p;
7091+ dpages->ndpage++;
7092+ }
7093+
c1595e42 7094+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7095+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7096+ return 0; /* success */
7097+
4f0767ce 7098+out:
1facf9fc 7099+ return err;
7100+}
7101+
c1595e42
JR
7102+/* todo: BAD approach */
7103+/* copied from linux/fs/dcache.c */
7104+enum d_walk_ret {
7105+ D_WALK_CONTINUE,
7106+ D_WALK_QUIT,
7107+ D_WALK_NORETRY,
7108+ D_WALK_SKIP,
7109+};
7110+
7111+extern void d_walk(struct dentry *parent, void *data,
7112+ enum d_walk_ret (*enter)(void *, struct dentry *),
7113+ void (*finish)(void *));
7114+
7115+struct ac_dpages_arg {
1facf9fc 7116+ int err;
c1595e42
JR
7117+ struct au_dcsub_pages *dpages;
7118+ struct super_block *sb;
7119+ au_dpages_test test;
7120+ void *arg;
7121+};
1facf9fc 7122+
c1595e42
JR
7123+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7124+{
7125+ enum d_walk_ret ret;
7126+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7127+
c1595e42
JR
7128+ ret = D_WALK_CONTINUE;
7129+ if (dentry->d_sb == arg->sb
7130+ && !IS_ROOT(dentry)
7131+ && au_dcount(dentry) > 0
7132+ && au_di(dentry)
7133+ && (!arg->test || arg->test(dentry, arg->arg))) {
7134+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7135+ if (unlikely(arg->err))
7136+ ret = D_WALK_QUIT;
1facf9fc 7137+ }
7138+
c1595e42
JR
7139+ return ret;
7140+}
027c5e7a 7141+
c1595e42
JR
7142+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7143+ au_dpages_test test, void *arg)
7144+{
7145+ struct ac_dpages_arg args = {
7146+ .err = 0,
7147+ .dpages = dpages,
7148+ .sb = root->d_sb,
7149+ .test = test,
7150+ .arg = arg
7151+ };
027c5e7a 7152+
c1595e42
JR
7153+ d_walk(root, &args, au_call_dpages_append, NULL);
7154+
7155+ return args.err;
1facf9fc 7156+}
7157+
7158+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7159+ int do_include, au_dpages_test test, void *arg)
7160+{
7161+ int err;
7162+
7163+ err = 0;
027c5e7a
AM
7164+ write_seqlock(&rename_lock);
7165+ spin_lock(&dentry->d_lock);
7166+ if (do_include
c1595e42 7167+ && au_dcount(dentry) > 0
027c5e7a 7168+ && (!test || test(dentry, arg)))
1facf9fc 7169+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7170+ spin_unlock(&dentry->d_lock);
7171+ if (unlikely(err))
7172+ goto out;
7173+
7174+ /*
523b37e3 7175+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7176+ * mount
7177+ */
1facf9fc 7178+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7179+ dentry = dentry->d_parent; /* rename_lock is locked */
7180+ spin_lock(&dentry->d_lock);
c1595e42 7181+ if (au_dcount(dentry) > 0
027c5e7a 7182+ && (!test || test(dentry, arg)))
1facf9fc 7183+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7184+ spin_unlock(&dentry->d_lock);
7185+ if (unlikely(err))
7186+ break;
1facf9fc 7187+ }
7188+
4f0767ce 7189+out:
027c5e7a 7190+ write_sequnlock(&rename_lock);
1facf9fc 7191+ return err;
7192+}
7193+
027c5e7a
AM
7194+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7195+{
7196+ return au_di(dentry) && dentry->d_sb == arg;
7197+}
7198+
7199+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7200+ struct dentry *dentry, int do_include)
7201+{
7202+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7203+ au_dcsub_dpages_aufs, dentry->d_sb);
7204+}
7205+
4a4d8108 7206+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7207+{
4a4d8108
AM
7208+ struct path path[2] = {
7209+ {
7210+ .dentry = d1
7211+ },
7212+ {
7213+ .dentry = d2
7214+ }
7215+ };
1facf9fc 7216+
4a4d8108 7217+ return path_is_under(path + 0, path + 1);
1facf9fc 7218+}
7f207e10
AM
7219diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7220--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 7221+++ linux/fs/aufs/dcsub.h 2018-04-15 08:49:13.397817296 +0200
5527c038 7222@@ -0,0 +1,136 @@
1facf9fc 7223+/*
b00004a5 7224+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7225+ *
7226+ * This program, aufs is free software; you can redistribute it and/or modify
7227+ * it under the terms of the GNU General Public License as published by
7228+ * the Free Software Foundation; either version 2 of the License, or
7229+ * (at your option) any later version.
dece6358
AM
7230+ *
7231+ * This program is distributed in the hope that it will be useful,
7232+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7233+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7234+ * GNU General Public License for more details.
7235+ *
7236+ * You should have received a copy of the GNU General Public License
523b37e3 7237+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7238+ */
7239+
7240+/*
7241+ * sub-routines for dentry cache
7242+ */
7243+
7244+#ifndef __AUFS_DCSUB_H__
7245+#define __AUFS_DCSUB_H__
7246+
7247+#ifdef __KERNEL__
7248+
7f207e10 7249+#include <linux/dcache.h>
027c5e7a 7250+#include <linux/fs.h>
dece6358 7251+
1facf9fc 7252+struct au_dpage {
7253+ int ndentry;
7254+ struct dentry **dentries;
7255+};
7256+
7257+struct au_dcsub_pages {
7258+ int ndpage;
7259+ struct au_dpage *dpages;
7260+};
7261+
7262+/* ---------------------------------------------------------------------- */
7263+
7f207e10 7264+/* dcsub.c */
1facf9fc 7265+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7266+void au_dpages_free(struct au_dcsub_pages *dpages);
7267+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7268+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7269+ au_dpages_test test, void *arg);
7270+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7271+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7272+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7273+ struct dentry *dentry, int do_include);
4a4d8108 7274+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7275+
7f207e10
AM
7276+/* ---------------------------------------------------------------------- */
7277+
523b37e3
AM
7278+/*
7279+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7280+ * include/linux/dcache.h. Try them (in the future).
7281+ */
7282+
027c5e7a
AM
7283+static inline int au_d_hashed_positive(struct dentry *d)
7284+{
7285+ int err;
5527c038 7286+ struct inode *inode = d_inode(d);
076b876e 7287+
027c5e7a 7288+ err = 0;
5527c038
JR
7289+ if (unlikely(d_unhashed(d)
7290+ || d_is_negative(d)
7291+ || !inode->i_nlink))
027c5e7a
AM
7292+ err = -ENOENT;
7293+ return err;
7294+}
7295+
38d290e6
JR
7296+static inline int au_d_linkable(struct dentry *d)
7297+{
7298+ int err;
5527c038 7299+ struct inode *inode = d_inode(d);
076b876e 7300+
38d290e6
JR
7301+ err = au_d_hashed_positive(d);
7302+ if (err
5527c038 7303+ && d_is_positive(d)
38d290e6
JR
7304+ && (inode->i_state & I_LINKABLE))
7305+ err = 0;
7306+ return err;
7307+}
7308+
027c5e7a
AM
7309+static inline int au_d_alive(struct dentry *d)
7310+{
7311+ int err;
7312+ struct inode *inode;
076b876e 7313+
027c5e7a
AM
7314+ err = 0;
7315+ if (!IS_ROOT(d))
7316+ err = au_d_hashed_positive(d);
7317+ else {
5527c038
JR
7318+ inode = d_inode(d);
7319+ if (unlikely(d_unlinked(d)
7320+ || d_is_negative(d)
7321+ || !inode->i_nlink))
027c5e7a
AM
7322+ err = -ENOENT;
7323+ }
7324+ return err;
7325+}
7326+
7327+static inline int au_alive_dir(struct dentry *d)
7f207e10 7328+{
027c5e7a 7329+ int err;
076b876e 7330+
027c5e7a 7331+ err = au_d_alive(d);
5527c038 7332+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7333+ err = -ENOENT;
7334+ return err;
7f207e10
AM
7335+}
7336+
38d290e6
JR
7337+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7338+{
7339+ return a->len == b->len
7340+ && !memcmp(a->name, b->name, a->len);
7341+}
7342+
7e9cd9fe
AM
7343+/*
7344+ * by the commit
7345+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7346+ * taking d_lock
7347+ * the type of d_lockref.count became int, but the inlined function d_count()
7348+ * still returns unsigned int.
7349+ * I don't know why. Maybe it is for every d_count() users?
7350+ * Anyway au_dcount() lives on.
7351+ */
c1595e42
JR
7352+static inline int au_dcount(struct dentry *d)
7353+{
7354+ return (int)d_count(d);
7355+}
7356+
1facf9fc 7357+#endif /* __KERNEL__ */
7358+#endif /* __AUFS_DCSUB_H__ */
7f207e10
AM
7359diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7360--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 7361+++ linux/fs/aufs/debug.c 2018-06-04 09:08:09.181412645 +0200
f0c0a007 7362@@ -0,0 +1,440 @@
1facf9fc 7363+/*
b00004a5 7364+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7365+ *
7366+ * This program, aufs is free software; you can redistribute it and/or modify
7367+ * it under the terms of the GNU General Public License as published by
7368+ * the Free Software Foundation; either version 2 of the License, or
7369+ * (at your option) any later version.
dece6358
AM
7370+ *
7371+ * This program is distributed in the hope that it will be useful,
7372+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7373+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7374+ * GNU General Public License for more details.
7375+ *
7376+ * You should have received a copy of the GNU General Public License
523b37e3 7377+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7378+ */
7379+
7380+/*
7381+ * debug print functions
7382+ */
7383+
7384+#include "aufs.h"
7385+
392086de
AM
7386+/* Returns 0, or -errno. arg is in kp->arg. */
7387+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7388+{
7389+ int err, n;
7390+
7391+ err = kstrtoint(val, 0, &n);
7392+ if (!err) {
7393+ if (n > 0)
7394+ au_debug_on();
7395+ else
7396+ au_debug_off();
7397+ }
7398+ return err;
7399+}
7400+
7401+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7402+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7403+{
7404+ atomic_t *a;
7405+
7406+ a = kp->arg;
7407+ return sprintf(buffer, "%d", atomic_read(a));
7408+}
7409+
7410+static struct kernel_param_ops param_ops_atomic_t = {
7411+ .set = param_atomic_t_set,
7412+ .get = param_atomic_t_get
7413+ /* void (*free)(void *arg) */
7414+};
7415+
7416+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7417+MODULE_PARM_DESC(debug, "debug print");
392086de 7418+module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
1facf9fc 7419+
c1595e42 7420+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7421+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7422+#define dpri(fmt, ...) do { \
7423+ if ((au_plevel \
7424+ && strcmp(au_plevel, KERN_DEBUG)) \
7425+ || au_debug_test()) \
7426+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7427+} while (0)
7428+
7429+/* ---------------------------------------------------------------------- */
7430+
7431+void au_dpri_whlist(struct au_nhash *whlist)
7432+{
7433+ unsigned long ul, n;
7434+ struct hlist_head *head;
c06a8ce3 7435+ struct au_vdir_wh *pos;
1facf9fc 7436+
7437+ n = whlist->nh_num;
7438+ head = whlist->nh_head;
7439+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7440+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7441+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7442+ pos->wh_bindex,
7443+ pos->wh_str.len, pos->wh_str.name,
7444+ pos->wh_str.len);
1facf9fc 7445+ head++;
7446+ }
7447+}
7448+
7449+void au_dpri_vdir(struct au_vdir *vdir)
7450+{
7451+ unsigned long ul;
7452+ union au_vdir_deblk_p p;
7453+ unsigned char *o;
7454+
7455+ if (!vdir || IS_ERR(vdir)) {
7456+ dpri("err %ld\n", PTR_ERR(vdir));
7457+ return;
7458+ }
7459+
be118d29 7460+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7461+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7462+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7463+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7464+ p.deblk = vdir->vd_deblk[ul];
7465+ o = p.deblk;
7466+ dpri("[%lu]: %p\n", ul, o);
7467+ }
7468+}
7469+
53392da6 7470+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7471+ struct dentry *wh)
7472+{
7473+ char *n = NULL;
7474+ int l = 0;
7475+
7476+ if (!inode || IS_ERR(inode)) {
7477+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7478+ return -1;
7479+ }
7480+
c2b27bf2 7481+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7482+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7483+ && sizeof(inode->i_blocks) != sizeof(u64));
7484+ if (wh) {
7485+ n = (void *)wh->d_name.name;
7486+ l = wh->d_name.len;
7487+ }
7488+
53392da6
AM
7489+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7490+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7491+ bindex, inode,
1facf9fc 7492+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7493+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7494+ i_size_read(inode), (unsigned long long)inode->i_blocks,
53392da6 7495+ hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7496+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7497+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7498+ inode->i_generation,
1facf9fc 7499+ l ? ", wh " : "", l, n);
7500+ return 0;
7501+}
7502+
7503+void au_dpri_inode(struct inode *inode)
7504+{
7505+ struct au_iinfo *iinfo;
5afbbe0d 7506+ struct au_hinode *hi;
1facf9fc 7507+ aufs_bindex_t bindex;
53392da6 7508+ int err, hn;
1facf9fc 7509+
53392da6 7510+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7511+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7512+ return;
7513+
7514+ iinfo = au_ii(inode);
5afbbe0d
AM
7515+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7516+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7517+ if (iinfo->ii_btop < 0)
1facf9fc 7518+ return;
53392da6 7519+ hn = 0;
5afbbe0d
AM
7520+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7521+ hi = au_hinode(iinfo, bindex);
7522+ hn = !!au_hn(hi);
7523+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7524+ }
1facf9fc 7525+}
7526+
2cbb1c4b
JR
7527+void au_dpri_dalias(struct inode *inode)
7528+{
7529+ struct dentry *d;
7530+
7531+ spin_lock(&inode->i_lock);
c1595e42 7532+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7533+ au_dpri_dentry(d);
7534+ spin_unlock(&inode->i_lock);
7535+}
7536+
1facf9fc 7537+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7538+{
7539+ struct dentry *wh = NULL;
53392da6 7540+ int hn;
5afbbe0d 7541+ struct inode *inode;
076b876e 7542+ struct au_iinfo *iinfo;
5afbbe0d 7543+ struct au_hinode *hi;
1facf9fc 7544+
7545+ if (!dentry || IS_ERR(dentry)) {
7546+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7547+ return -1;
7548+ }
7549+ /* do not call dget_parent() here */
027c5e7a 7550+ /* note: access d_xxx without d_lock */
523b37e3
AM
7551+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7552+ bindex, dentry, dentry,
1facf9fc 7553+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7554+ au_dcount(dentry), dentry->d_flags,
523b37e3 7555+ d_unhashed(dentry) ? "un" : "");
53392da6 7556+ hn = -1;
5afbbe0d
AM
7557+ inode = NULL;
7558+ if (d_is_positive(dentry))
7559+ inode = d_inode(dentry);
7560+ if (inode
7561+ && au_test_aufs(dentry->d_sb)
7562+ && bindex >= 0
7563+ && !au_is_bad_inode(inode)) {
7564+ iinfo = au_ii(inode);
7565+ hi = au_hinode(iinfo, bindex);
7566+ hn = !!au_hn(hi);
7567+ wh = hi->hi_whdentry;
7568+ }
7569+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7570+ return 0;
7571+}
7572+
7573+void au_dpri_dentry(struct dentry *dentry)
7574+{
7575+ struct au_dinfo *dinfo;
7576+ aufs_bindex_t bindex;
7577+ int err;
7578+
7579+ err = do_pri_dentry(-1, dentry);
7580+ if (err || !au_test_aufs(dentry->d_sb))
7581+ return;
7582+
7583+ dinfo = au_di(dentry);
7584+ if (!dinfo)
7585+ return;
5afbbe0d
AM
7586+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7587+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7588+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7589+ dinfo->di_tmpfile);
5afbbe0d 7590+ if (dinfo->di_btop < 0)
1facf9fc 7591+ return;
5afbbe0d
AM
7592+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7593+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7594+}
7595+
7596+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7597+{
7598+ char a[32];
7599+
7600+ if (!file || IS_ERR(file)) {
7601+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7602+ return -1;
7603+ }
7604+ a[0] = 0;
7605+ if (bindex < 0
b912730e 7606+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7607+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7608+ && au_fi(file))
e49829fe 7609+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7610+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7611+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7612+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7613+ file->f_version, file->f_pos, a);
b912730e 7614+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7615+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7616+ return 0;
7617+}
7618+
7619+void au_dpri_file(struct file *file)
7620+{
7621+ struct au_finfo *finfo;
4a4d8108
AM
7622+ struct au_fidir *fidir;
7623+ struct au_hfile *hfile;
1facf9fc 7624+ aufs_bindex_t bindex;
7625+ int err;
7626+
7627+ err = do_pri_file(-1, file);
2000de60 7628+ if (err
b912730e 7629+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7630+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7631+ return;
7632+
7633+ finfo = au_fi(file);
7634+ if (!finfo)
7635+ return;
4a4d8108 7636+ if (finfo->fi_btop < 0)
1facf9fc 7637+ return;
4a4d8108
AM
7638+ fidir = finfo->fi_hdir;
7639+ if (!fidir)
7640+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7641+ else
e49829fe
JR
7642+ for (bindex = finfo->fi_btop;
7643+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7644+ bindex++) {
7645+ hfile = fidir->fd_hfile + bindex;
7646+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7647+ }
1facf9fc 7648+}
7649+
7650+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7651+{
7652+ struct vfsmount *mnt;
7653+ struct super_block *sb;
7654+
7655+ if (!br || IS_ERR(br))
7656+ goto out;
86dc4139 7657+ mnt = au_br_mnt(br);
1facf9fc 7658+ if (!mnt || IS_ERR(mnt))
7659+ goto out;
7660+ sb = mnt->mnt_sb;
7661+ if (!sb || IS_ERR(sb))
7662+ goto out;
7663+
5afbbe0d 7664+ dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
b752ccd1 7665+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7666+ "xino %d\n",
5afbbe0d 7667+ bindex, br->br_perm, br->br_id, au_br_count(br),
1e00d052 7668+ br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7669+ sb->s_flags, sb->s_count,
1facf9fc 7670+ atomic_read(&sb->s_active), !!br->br_xino.xi_file);
7671+ return 0;
7672+
4f0767ce 7673+out:
1facf9fc 7674+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7675+ return -1;
7676+}
7677+
7678+void au_dpri_sb(struct super_block *sb)
7679+{
7680+ struct au_sbinfo *sbinfo;
7681+ aufs_bindex_t bindex;
7682+ int err;
7683+ /* to reuduce stack size */
7684+ struct {
7685+ struct vfsmount mnt;
7686+ struct au_branch fake;
7687+ } *a;
7688+
7689+ /* this function can be called from magic sysrq */
7690+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7691+ if (unlikely(!a)) {
7692+ dpri("no memory\n");
7693+ return;
7694+ }
7695+
7696+ a->mnt.mnt_sb = sb;
86dc4139 7697+ a->fake.br_path.mnt = &a->mnt;
5afbbe0d 7698+ au_br_count_init(&a->fake);
1facf9fc 7699+ err = do_pri_br(-1, &a->fake);
5afbbe0d 7700+ au_br_count_fin(&a->fake);
1c60b727 7701+ kfree(a);
1facf9fc 7702+ dpri("dev 0x%x\n", sb->s_dev);
7703+ if (err || !au_test_aufs(sb))
7704+ return;
7705+
7706+ sbinfo = au_sbi(sb);
7707+ if (!sbinfo)
7708+ return;
f0c0a007
AM
7709+ dpri("nw %d, gen %u, kobj %d\n",
7710+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7711+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7712+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7713+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7714+}
7715+
7716+/* ---------------------------------------------------------------------- */
7717+
027c5e7a
AM
7718+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7719+{
5527c038 7720+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7721+ struct dentry *h_dentry;
5afbbe0d 7722+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7723+
7724+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7725+ return;
7726+
5afbbe0d
AM
7727+ bbot = au_dbbot(dentry);
7728+ bi = au_ibbot(inode);
7729+ if (bi < bbot)
7730+ bbot = bi;
7731+ bindex = au_dbtop(dentry);
7732+ bi = au_ibtop(inode);
027c5e7a
AM
7733+ if (bi > bindex)
7734+ bindex = bi;
7735+
5afbbe0d 7736+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7737+ h_dentry = au_h_dptr(dentry, bindex);
7738+ if (!h_dentry)
7739+ continue;
7740+ h_inode = au_h_iptr(inode, bindex);
5527c038 7741+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7742+ au_debug_on();
027c5e7a
AM
7743+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7744+ AuDbgDentry(dentry);
7745+ AuDbgInode(inode);
392086de 7746+ au_debug_off();
027c5e7a
AM
7747+ BUG();
7748+ }
7749+ }
7750+}
7751+
1facf9fc 7752+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7753+{
7754+ int err, i, j;
7755+ struct au_dcsub_pages dpages;
7756+ struct au_dpage *dpage;
7757+ struct dentry **dentries;
7758+
7759+ err = au_dpages_init(&dpages, GFP_NOFS);
7760+ AuDebugOn(err);
027c5e7a 7761+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7762+ AuDebugOn(err);
7763+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7764+ dpage = dpages.dpages + i;
7765+ dentries = dpage->dentries;
7766+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7767+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7768+ }
7769+ au_dpages_free(&dpages);
7770+}
7771+
1facf9fc 7772+void au_dbg_verify_kthread(void)
7773+{
53392da6 7774+ if (au_wkq_test()) {
1facf9fc 7775+ au_dbg_blocked();
1e00d052
AM
7776+ /*
7777+ * It may be recursive, but udba=notify between two aufs mounts,
7778+ * where a single ro branch is shared, is not a problem.
7779+ */
7780+ /* WARN_ON(1); */
1facf9fc 7781+ }
7782+}
7783+
7784+/* ---------------------------------------------------------------------- */
7785+
1facf9fc 7786+int __init au_debug_init(void)
7787+{
7788+ aufs_bindex_t bindex;
7789+ struct au_vdir_destr destr;
7790+
7791+ bindex = -1;
7792+ AuDebugOn(bindex >= 0);
7793+
7794+ destr.len = -1;
7795+ AuDebugOn(destr.len < NAME_MAX);
7796+
7797+#ifdef CONFIG_4KSTACKS
0c3ec466 7798+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7799+#endif
7800+
1facf9fc 7801+ return 0;
7802+}
7f207e10
AM
7803diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7804--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 7805+++ linux/fs/aufs/debug.h 2018-04-15 08:49:13.397817296 +0200
5527c038 7806@@ -0,0 +1,225 @@
1facf9fc 7807+/*
b00004a5 7808+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7809+ *
7810+ * This program, aufs is free software; you can redistribute it and/or modify
7811+ * it under the terms of the GNU General Public License as published by
7812+ * the Free Software Foundation; either version 2 of the License, or
7813+ * (at your option) any later version.
dece6358
AM
7814+ *
7815+ * This program is distributed in the hope that it will be useful,
7816+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7817+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7818+ * GNU General Public License for more details.
7819+ *
7820+ * You should have received a copy of the GNU General Public License
523b37e3 7821+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7822+ */
7823+
7824+/*
7825+ * debug print functions
7826+ */
7827+
7828+#ifndef __AUFS_DEBUG_H__
7829+#define __AUFS_DEBUG_H__
7830+
7831+#ifdef __KERNEL__
7832+
392086de 7833+#include <linux/atomic.h>
4a4d8108
AM
7834+#include <linux/module.h>
7835+#include <linux/kallsyms.h>
1facf9fc 7836+#include <linux/sysrq.h>
4a4d8108 7837+
1facf9fc 7838+#ifdef CONFIG_AUFS_DEBUG
7839+#define AuDebugOn(a) BUG_ON(a)
7840+
7841+/* module parameter */
392086de
AM
7842+extern atomic_t aufs_debug;
7843+static inline void au_debug_on(void)
1facf9fc 7844+{
392086de
AM
7845+ atomic_inc(&aufs_debug);
7846+}
7847+static inline void au_debug_off(void)
7848+{
7849+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7850+}
7851+
7852+static inline int au_debug_test(void)
7853+{
392086de 7854+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7855+}
7856+#else
7857+#define AuDebugOn(a) do {} while (0)
392086de
AM
7858+AuStubVoid(au_debug_on, void)
7859+AuStubVoid(au_debug_off, void)
4a4d8108 7860+AuStubInt0(au_debug_test, void)
1facf9fc 7861+#endif /* CONFIG_AUFS_DEBUG */
7862+
392086de
AM
7863+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7864+
1facf9fc 7865+/* ---------------------------------------------------------------------- */
7866+
7867+/* debug print */
7868+
4a4d8108 7869+#define AuDbg(fmt, ...) do { \
1facf9fc 7870+ if (au_debug_test()) \
4a4d8108 7871+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7872+} while (0)
4a4d8108
AM
7873+#define AuLabel(l) AuDbg(#l "\n")
7874+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7875+#define AuWarn1(fmt, ...) do { \
1facf9fc 7876+ static unsigned char _c; \
7877+ if (!_c++) \
0c3ec466 7878+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7879+} while (0)
7880+
4a4d8108 7881+#define AuErr1(fmt, ...) do { \
1facf9fc 7882+ static unsigned char _c; \
7883+ if (!_c++) \
4a4d8108 7884+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7885+} while (0)
7886+
4a4d8108 7887+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7888+ static unsigned char _c; \
7889+ if (!_c++) \
4a4d8108 7890+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7891+} while (0)
7892+
7893+#define AuUnsupportMsg "This operation is not supported." \
7894+ " Please report this application to aufs-users ML."
4a4d8108
AM
7895+#define AuUnsupport(fmt, ...) do { \
7896+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7897+ dump_stack(); \
7898+} while (0)
7899+
7900+#define AuTraceErr(e) do { \
7901+ if (unlikely((e) < 0)) \
7902+ AuDbg("err %d\n", (int)(e)); \
7903+} while (0)
7904+
7905+#define AuTraceErrPtr(p) do { \
7906+ if (IS_ERR(p)) \
7907+ AuDbg("err %ld\n", PTR_ERR(p)); \
7908+} while (0)
7909+
7910+/* dirty macros for debug print, use with "%.*s" and caution */
7911+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 7912+
7913+/* ---------------------------------------------------------------------- */
7914+
dece6358 7915+struct dentry;
1facf9fc 7916+#ifdef CONFIG_AUFS_DEBUG
c1595e42 7917+extern struct mutex au_dbg_mtx;
1facf9fc 7918+extern char *au_plevel;
7919+struct au_nhash;
7920+void au_dpri_whlist(struct au_nhash *whlist);
7921+struct au_vdir;
7922+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 7923+struct inode;
1facf9fc 7924+void au_dpri_inode(struct inode *inode);
2cbb1c4b 7925+void au_dpri_dalias(struct inode *inode);
1facf9fc 7926+void au_dpri_dentry(struct dentry *dentry);
dece6358 7927+struct file;
1facf9fc 7928+void au_dpri_file(struct file *filp);
dece6358 7929+struct super_block;
1facf9fc 7930+void au_dpri_sb(struct super_block *sb);
7931+
027c5e7a
AM
7932+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7933+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 7934+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 7935+void au_dbg_verify_kthread(void);
7936+
7937+int __init au_debug_init(void);
7e9cd9fe 7938+
1facf9fc 7939+#define AuDbgWhlist(w) do { \
c1595e42 7940+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7941+ AuDbg(#w "\n"); \
7942+ au_dpri_whlist(w); \
c1595e42 7943+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7944+} while (0)
7945+
7946+#define AuDbgVdir(v) do { \
c1595e42 7947+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7948+ AuDbg(#v "\n"); \
7949+ au_dpri_vdir(v); \
c1595e42 7950+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7951+} while (0)
7952+
7953+#define AuDbgInode(i) do { \
c1595e42 7954+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7955+ AuDbg(#i "\n"); \
7956+ au_dpri_inode(i); \
c1595e42 7957+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7958+} while (0)
7959+
2cbb1c4b 7960+#define AuDbgDAlias(i) do { \
c1595e42 7961+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
7962+ AuDbg(#i "\n"); \
7963+ au_dpri_dalias(i); \
c1595e42 7964+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
7965+} while (0)
7966+
1facf9fc 7967+#define AuDbgDentry(d) do { \
c1595e42 7968+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7969+ AuDbg(#d "\n"); \
7970+ au_dpri_dentry(d); \
c1595e42 7971+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7972+} while (0)
7973+
7974+#define AuDbgFile(f) do { \
c1595e42 7975+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7976+ AuDbg(#f "\n"); \
7977+ au_dpri_file(f); \
c1595e42 7978+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7979+} while (0)
7980+
7981+#define AuDbgSb(sb) do { \
c1595e42 7982+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7983+ AuDbg(#sb "\n"); \
7984+ au_dpri_sb(sb); \
c1595e42 7985+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7986+} while (0)
7987+
4a4d8108
AM
7988+#define AuDbgSym(addr) do { \
7989+ char sym[KSYM_SYMBOL_LEN]; \
7990+ sprint_symbol(sym, (unsigned long)addr); \
7991+ AuDbg("%s\n", sym); \
7992+} while (0)
1facf9fc 7993+#else
027c5e7a 7994+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
7995+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7996+AuStubVoid(au_dbg_verify_kthread, void)
7997+AuStubInt0(__init au_debug_init, void)
1facf9fc 7998+
1facf9fc 7999+#define AuDbgWhlist(w) do {} while (0)
8000+#define AuDbgVdir(v) do {} while (0)
8001+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 8002+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 8003+#define AuDbgDentry(d) do {} while (0)
8004+#define AuDbgFile(f) do {} while (0)
8005+#define AuDbgSb(sb) do {} while (0)
4a4d8108 8006+#define AuDbgSym(addr) do {} while (0)
1facf9fc 8007+#endif /* CONFIG_AUFS_DEBUG */
8008+
8009+/* ---------------------------------------------------------------------- */
8010+
8011+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8012+int __init au_sysrq_init(void);
8013+void au_sysrq_fin(void);
8014+
8015+#ifdef CONFIG_HW_CONSOLE
8016+#define au_dbg_blocked() do { \
8017+ WARN_ON(1); \
0c5527e5 8018+ handle_sysrq('w'); \
1facf9fc 8019+} while (0)
8020+#else
4a4d8108 8021+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8022+#endif
8023+
8024+#else
4a4d8108
AM
8025+AuStubInt0(__init au_sysrq_init, void)
8026+AuStubVoid(au_sysrq_fin, void)
8027+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8028+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8029+
8030+#endif /* __KERNEL__ */
8031+#endif /* __AUFS_DEBUG_H__ */
7f207e10
AM
8032diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8033--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 8034+++ linux/fs/aufs/dentry.c 2018-06-04 09:08:09.184746078 +0200
8b6a4947 8035@@ -0,0 +1,1152 @@
1facf9fc 8036+/*
b00004a5 8037+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 8038+ *
8039+ * This program, aufs is free software; you can redistribute it and/or modify
8040+ * it under the terms of the GNU General Public License as published by
8041+ * the Free Software Foundation; either version 2 of the License, or
8042+ * (at your option) any later version.
dece6358
AM
8043+ *
8044+ * This program is distributed in the hope that it will be useful,
8045+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8046+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8047+ * GNU General Public License for more details.
8048+ *
8049+ * You should have received a copy of the GNU General Public License
523b37e3 8050+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8051+ */
8052+
8053+/*
8054+ * lookup and dentry operations
8055+ */
8056+
dece6358 8057+#include <linux/namei.h>
1facf9fc 8058+#include "aufs.h"
8059+
1facf9fc 8060+/*
8061+ * returns positive/negative dentry, NULL or an error.
8062+ * NULL means whiteout-ed or not-found.
8063+ */
8064+static struct dentry*
8065+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8066+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8067+{
8068+ struct dentry *h_dentry;
2000de60 8069+ struct inode *h_inode;
1facf9fc 8070+ struct au_branch *br;
8071+ int wh_found, opq;
8072+ unsigned char wh_able;
8073+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8074+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8075+ IGNORE_PERM);
1facf9fc 8076+
1facf9fc 8077+ wh_found = 0;
8078+ br = au_sbr(dentry->d_sb, bindex);
8079+ wh_able = !!au_br_whable(br->br_perm);
8080+ if (wh_able)
8b6a4947 8081+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
1facf9fc 8082+ h_dentry = ERR_PTR(wh_found);
8083+ if (!wh_found)
8084+ goto real_lookup;
8085+ if (unlikely(wh_found < 0))
8086+ goto out;
8087+
8088+ /* We found a whiteout */
5afbbe0d 8089+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8090+ au_set_dbwh(dentry, bindex);
8091+ if (!allow_neg)
8092+ return NULL; /* success */
8093+
4f0767ce 8094+real_lookup:
076b876e 8095+ if (!ignore_perm)
8b6a4947 8096+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8097+ else
8b6a4947 8098+ h_dentry = au_sio_lkup_one(args->name, h_parent);
2000de60
JR
8099+ if (IS_ERR(h_dentry)) {
8100+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8101+ && !allow_neg)
8102+ h_dentry = NULL;
1facf9fc 8103+ goto out;
2000de60 8104+ }
1facf9fc 8105+
5527c038
JR
8106+ h_inode = d_inode(h_dentry);
8107+ if (d_is_negative(h_dentry)) {
1facf9fc 8108+ if (!allow_neg)
8109+ goto out_neg;
8110+ } else if (wh_found
8111+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8112+ goto out_neg;
8b6a4947
AM
8113+ else if (au_ftest_lkup(args->flags, DIRREN)
8114+ /* && h_inode */
8115+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8116+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8117+ (unsigned long long)h_inode->i_ino);
8118+ goto out_neg;
8119+ }
1facf9fc 8120+
5afbbe0d
AM
8121+ if (au_dbbot(dentry) <= bindex)
8122+ au_set_dbbot(dentry, bindex);
8123+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8124+ au_set_dbtop(dentry, bindex);
1facf9fc 8125+ au_set_h_dptr(dentry, bindex, h_dentry);
8126+
2000de60
JR
8127+ if (!d_is_dir(h_dentry)
8128+ || !wh_able
5527c038 8129+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8130+ goto out; /* success */
8131+
be118d29 8132+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
076b876e 8133+ opq = au_diropq_test(h_dentry);
3c1bdaff 8134+ inode_unlock_shared(h_inode);
1facf9fc 8135+ if (opq > 0)
8136+ au_set_dbdiropq(dentry, bindex);
8137+ else if (unlikely(opq < 0)) {
8138+ au_set_h_dptr(dentry, bindex, NULL);
8139+ h_dentry = ERR_PTR(opq);
8140+ }
8141+ goto out;
8142+
4f0767ce 8143+out_neg:
1facf9fc 8144+ dput(h_dentry);
8145+ h_dentry = NULL;
4f0767ce 8146+out:
1facf9fc 8147+ return h_dentry;
8148+}
8149+
dece6358
AM
8150+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8151+{
8152+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8153+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8154+ return -EPERM;
8155+ return 0;
8156+}
8157+
1facf9fc 8158+/*
8159+ * returns the number of lower positive dentries,
8160+ * otherwise an error.
8161+ * can be called at unlinking with @type is zero.
8162+ */
5afbbe0d
AM
8163+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8164+ unsigned int flags)
1facf9fc 8165+{
8166+ int npositive, err;
8167+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8168+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8169+ struct au_do_lookup_args args = {
8b6a4947
AM
8170+ .flags = flags,
8171+ .name = &dentry->d_name
1facf9fc 8172+ };
1facf9fc 8173+ struct dentry *parent;
076b876e 8174+ struct super_block *sb;
1facf9fc 8175+
076b876e 8176+ sb = dentry->d_sb;
8b6a4947 8177+ err = au_test_shwh(sb, args.name);
dece6358 8178+ if (unlikely(err))
1facf9fc 8179+ goto out;
8180+
8b6a4947 8181+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8182+ if (unlikely(err))
8183+ goto out;
8184+
2000de60 8185+ isdir = !!d_is_dir(dentry);
076b876e 8186+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8187+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8188+ if (dirren)
8189+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8190+
8191+ npositive = 0;
4a4d8108 8192+ parent = dget_parent(dentry);
1facf9fc 8193+ btail = au_dbtaildir(parent);
5afbbe0d 8194+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8195+ struct dentry *h_parent, *h_dentry;
8196+ struct inode *h_inode, *h_dir;
8b6a4947 8197+ struct au_branch *br;
1facf9fc 8198+
8199+ h_dentry = au_h_dptr(dentry, bindex);
8200+ if (h_dentry) {
5527c038 8201+ if (d_is_positive(h_dentry))
1facf9fc 8202+ npositive++;
5afbbe0d 8203+ break;
1facf9fc 8204+ }
8205+ h_parent = au_h_dptr(parent, bindex);
2000de60 8206+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8207+ continue;
8208+
8b6a4947
AM
8209+ if (dirren) {
8210+ /* if the inum matches, then use the prepared name */
8211+ err = au_dr_lkup_name(&args, bindex);
8212+ if (unlikely(err))
8213+ goto out_parent;
8214+ }
8215+
5527c038 8216+ h_dir = d_inode(h_parent);
be118d29 8217+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8218+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8219+ inode_unlock_shared(h_dir);
1facf9fc 8220+ err = PTR_ERR(h_dentry);
8221+ if (IS_ERR(h_dentry))
4a4d8108 8222+ goto out_parent;
2000de60
JR
8223+ if (h_dentry)
8224+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8225+ if (dirperm1)
8226+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8227+
79b8bda9 8228+ if (au_dbwh(dentry) == bindex)
1facf9fc 8229+ break;
8230+ if (!h_dentry)
8231+ continue;
5527c038 8232+ if (d_is_negative(h_dentry))
1facf9fc 8233+ continue;
5527c038 8234+ h_inode = d_inode(h_dentry);
1facf9fc 8235+ npositive++;
8236+ if (!args.type)
8237+ args.type = h_inode->i_mode & S_IFMT;
8238+ if (args.type != S_IFDIR)
8239+ break;
8240+ else if (isdir) {
8241+ /* the type of lower may be different */
8242+ bdiropq = au_dbdiropq(dentry);
8243+ if (bdiropq >= 0 && bdiropq <= bindex)
8244+ break;
8245+ }
8b6a4947
AM
8246+ br = au_sbr(sb, bindex);
8247+ if (dirren
8248+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8249+ /*add_ent*/NULL)) {
8250+ /* prepare next name to lookup */
8251+ err = au_dr_lkup(&args, dentry, bindex);
8252+ if (unlikely(err))
8253+ goto out_parent;
8254+ }
1facf9fc 8255+ }
8256+
8257+ if (npositive) {
8258+ AuLabel(positive);
5afbbe0d 8259+ au_update_dbtop(dentry);
1facf9fc 8260+ }
8261+ err = npositive;
076b876e 8262+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8263+ && au_dbtop(dentry) < 0)) {
1facf9fc 8264+ err = -EIO;
523b37e3
AM
8265+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8266+ dentry, err);
027c5e7a 8267+ }
1facf9fc 8268+
4f0767ce 8269+out_parent:
4a4d8108 8270+ dput(parent);
8b6a4947
AM
8271+ kfree(args.whname.name);
8272+ if (dirren)
8273+ au_dr_lkup_fin(&args);
4f0767ce 8274+out:
1facf9fc 8275+ return err;
8276+}
8277+
076b876e 8278+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 8279+{
8280+ struct dentry *dentry;
8281+ int wkq_err;
8282+
5527c038 8283+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 8284+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8285+ else {
b4510431
AM
8286+ struct vfsub_lkup_one_args args = {
8287+ .errp = &dentry,
8288+ .name = name,
8289+ .parent = parent
1facf9fc 8290+ };
8291+
b4510431 8292+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8293+ if (unlikely(wkq_err))
8294+ dentry = ERR_PTR(wkq_err);
8295+ }
8296+
8297+ return dentry;
8298+}
8299+
8300+/*
8301+ * lookup @dentry on @bindex which should be negative.
8302+ */
86dc4139 8303+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8304+{
8305+ int err;
8306+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8307+ struct au_branch *br;
1facf9fc 8308+
1facf9fc 8309+ parent = dget_parent(dentry);
8310+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
8311+ br = au_sbr(dentry->d_sb, bindex);
8312+ if (wh)
8313+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8314+ else
076b876e 8315+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 8316+ err = PTR_ERR(h_dentry);
8317+ if (IS_ERR(h_dentry))
8318+ goto out;
5527c038 8319+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8320+ err = -EIO;
523b37e3 8321+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8322+ dput(h_dentry);
8323+ goto out;
8324+ }
8325+
4a4d8108 8326+ err = 0;
5afbbe0d
AM
8327+ if (bindex < au_dbtop(dentry))
8328+ au_set_dbtop(dentry, bindex);
8329+ if (au_dbbot(dentry) < bindex)
8330+ au_set_dbbot(dentry, bindex);
1facf9fc 8331+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8332+
4f0767ce 8333+out:
1facf9fc 8334+ dput(parent);
8335+ return err;
8336+}
8337+
8338+/* ---------------------------------------------------------------------- */
8339+
8340+/* subset of struct inode */
8341+struct au_iattr {
8342+ unsigned long i_ino;
8343+ /* unsigned int i_nlink; */
0c3ec466
AM
8344+ kuid_t i_uid;
8345+ kgid_t i_gid;
1facf9fc 8346+ u64 i_version;
8347+/*
8348+ loff_t i_size;
8349+ blkcnt_t i_blocks;
8350+*/
8351+ umode_t i_mode;
8352+};
8353+
8354+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8355+{
8356+ ia->i_ino = h_inode->i_ino;
8357+ /* ia->i_nlink = h_inode->i_nlink; */
8358+ ia->i_uid = h_inode->i_uid;
8359+ ia->i_gid = h_inode->i_gid;
be118d29 8360+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8361+/*
8362+ ia->i_size = h_inode->i_size;
8363+ ia->i_blocks = h_inode->i_blocks;
8364+*/
8365+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8366+}
8367+
8368+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8369+{
8370+ return ia->i_ino != h_inode->i_ino
8371+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8372+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8373+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8374+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8375+/*
8376+ || ia->i_size != h_inode->i_size
8377+ || ia->i_blocks != h_inode->i_blocks
8378+*/
8379+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8380+}
8381+
8382+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8383+ struct au_branch *br)
8384+{
8385+ int err;
8386+ struct au_iattr ia;
8387+ struct inode *h_inode;
8388+ struct dentry *h_d;
8389+ struct super_block *h_sb;
8390+
8391+ err = 0;
8392+ memset(&ia, -1, sizeof(ia));
8393+ h_sb = h_dentry->d_sb;
5527c038
JR
8394+ h_inode = NULL;
8395+ if (d_is_positive(h_dentry)) {
8396+ h_inode = d_inode(h_dentry);
1facf9fc 8397+ au_iattr_save(&ia, h_inode);
5527c038 8398+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8399+ /* nfs d_revalidate may return 0 for negative dentry */
8400+ /* fuse d_revalidate always return 0 for negative dentry */
8401+ goto out;
8402+
8403+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8404+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8405+ err = PTR_ERR(h_d);
8406+ if (IS_ERR(h_d))
8407+ goto out;
8408+
8409+ err = 0;
8410+ if (unlikely(h_d != h_dentry
5527c038 8411+ || d_inode(h_d) != h_inode
1facf9fc 8412+ || (h_inode && au_iattr_test(&ia, h_inode))))
8413+ err = au_busy_or_stale();
8414+ dput(h_d);
8415+
4f0767ce 8416+out:
1facf9fc 8417+ AuTraceErr(err);
8418+ return err;
8419+}
8420+
8421+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8422+ struct dentry *h_parent, struct au_branch *br)
8423+{
8424+ int err;
8425+
8426+ err = 0;
027c5e7a
AM
8427+ if (udba == AuOpt_UDBA_REVAL
8428+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8429+ IMustLock(h_dir);
5527c038 8430+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8431+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8432+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8433+
8434+ return err;
8435+}
8436+
8437+/* ---------------------------------------------------------------------- */
8438+
027c5e7a 8439+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8440+{
027c5e7a 8441+ int err;
5afbbe0d 8442+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8443+ struct au_hdentry tmp, *p, *q;
8444+ struct au_dinfo *dinfo;
8445+ struct super_block *sb;
1facf9fc 8446+
027c5e7a 8447+ DiMustWriteLock(dentry);
1308ab2a 8448+
027c5e7a
AM
8449+ sb = dentry->d_sb;
8450+ dinfo = au_di(dentry);
5afbbe0d 8451+ bbot = dinfo->di_bbot;
1facf9fc 8452+ bwh = dinfo->di_bwh;
8453+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8454+ bindex = dinfo->di_btop;
8455+ p = au_hdentry(dinfo, bindex);
8456+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8457+ if (!p->hd_dentry)
1facf9fc 8458+ continue;
8459+
027c5e7a
AM
8460+ new_bindex = au_br_index(sb, p->hd_id);
8461+ if (new_bindex == bindex)
1facf9fc 8462+ continue;
1facf9fc 8463+
1facf9fc 8464+ if (dinfo->di_bwh == bindex)
8465+ bwh = new_bindex;
8466+ if (dinfo->di_bdiropq == bindex)
8467+ bdiropq = new_bindex;
8468+ if (new_bindex < 0) {
8469+ au_hdput(p);
8470+ p->hd_dentry = NULL;
8471+ continue;
8472+ }
8473+
8474+ /* swap two lower dentries, and loop again */
5afbbe0d 8475+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8476+ tmp = *q;
8477+ *q = *p;
8478+ *p = tmp;
8479+ if (tmp.hd_dentry) {
8480+ bindex--;
8481+ p--;
8482+ }
8483+ }
8484+
1facf9fc 8485+ dinfo->di_bwh = -1;
5afbbe0d 8486+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8487+ dinfo->di_bwh = bwh;
8488+
8489+ dinfo->di_bdiropq = -1;
8490+ if (bdiropq >= 0
5afbbe0d 8491+ && bdiropq <= au_sbbot(sb)
1facf9fc 8492+ && au_sbr_whable(sb, bdiropq))
8493+ dinfo->di_bdiropq = bdiropq;
8494+
027c5e7a 8495+ err = -EIO;
5afbbe0d
AM
8496+ dinfo->di_btop = -1;
8497+ dinfo->di_bbot = -1;
8498+ bbot = au_dbbot(parent);
8499+ bindex = 0;
8500+ p = au_hdentry(dinfo, bindex);
8501+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8502+ if (p->hd_dentry) {
5afbbe0d 8503+ dinfo->di_btop = bindex;
1facf9fc 8504+ break;
8505+ }
8506+
5afbbe0d
AM
8507+ if (dinfo->di_btop >= 0) {
8508+ bindex = bbot;
8509+ p = au_hdentry(dinfo, bindex);
8510+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8511+ if (p->hd_dentry) {
5afbbe0d 8512+ dinfo->di_bbot = bindex;
027c5e7a
AM
8513+ err = 0;
8514+ break;
8515+ }
8516+ }
8517+
8518+ return err;
1facf9fc 8519+}
8520+
027c5e7a 8521+static void au_do_hide(struct dentry *dentry)
1facf9fc 8522+{
027c5e7a 8523+ struct inode *inode;
1facf9fc 8524+
5527c038
JR
8525+ if (d_really_is_positive(dentry)) {
8526+ inode = d_inode(dentry);
8527+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8528+ if (inode->i_nlink && !d_unhashed(dentry))
8529+ drop_nlink(inode);
8530+ } else {
8531+ clear_nlink(inode);
8532+ /* stop next lookup */
8533+ inode->i_flags |= S_DEAD;
8534+ }
8535+ smp_mb(); /* necessary? */
8536+ }
8537+ d_drop(dentry);
8538+}
1308ab2a 8539+
027c5e7a
AM
8540+static int au_hide_children(struct dentry *parent)
8541+{
8542+ int err, i, j, ndentry;
8543+ struct au_dcsub_pages dpages;
8544+ struct au_dpage *dpage;
8545+ struct dentry *dentry;
1facf9fc 8546+
027c5e7a 8547+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8548+ if (unlikely(err))
8549+ goto out;
027c5e7a
AM
8550+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8551+ if (unlikely(err))
8552+ goto out_dpages;
1facf9fc 8553+
027c5e7a
AM
8554+ /* in reverse order */
8555+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8556+ dpage = dpages.dpages + i;
8557+ ndentry = dpage->ndentry;
8558+ for (j = ndentry - 1; j >= 0; j--) {
8559+ dentry = dpage->dentries[j];
8560+ if (dentry != parent)
8561+ au_do_hide(dentry);
8562+ }
8563+ }
1facf9fc 8564+
027c5e7a
AM
8565+out_dpages:
8566+ au_dpages_free(&dpages);
4f0767ce 8567+out:
027c5e7a 8568+ return err;
1facf9fc 8569+}
8570+
027c5e7a 8571+static void au_hide(struct dentry *dentry)
1facf9fc 8572+{
027c5e7a 8573+ int err;
1facf9fc 8574+
027c5e7a 8575+ AuDbgDentry(dentry);
2000de60 8576+ if (d_is_dir(dentry)) {
027c5e7a
AM
8577+ /* shrink_dcache_parent(dentry); */
8578+ err = au_hide_children(dentry);
8579+ if (unlikely(err))
523b37e3
AM
8580+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8581+ dentry, err);
027c5e7a
AM
8582+ }
8583+ au_do_hide(dentry);
8584+}
1facf9fc 8585+
027c5e7a
AM
8586+/*
8587+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8588+ *
8589+ * a dirty branch is added
8590+ * - on the top of layers
8591+ * - in the middle of layers
8592+ * - to the bottom of layers
8593+ *
8594+ * on the added branch there exists
8595+ * - a whiteout
8596+ * - a diropq
8597+ * - a same named entry
8598+ * + exist
8599+ * * negative --> positive
8600+ * * positive --> positive
8601+ * - type is unchanged
8602+ * - type is changed
8603+ * + doesn't exist
8604+ * * negative --> negative
8605+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8606+ * - none
8607+ */
8608+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8609+ struct au_dinfo *tmp)
8610+{
8611+ int err;
5afbbe0d 8612+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8613+ struct {
8614+ struct dentry *dentry;
8615+ struct inode *inode;
8616+ mode_t mode;
be52b249
AM
8617+ } orig_h, tmp_h = {
8618+ .dentry = NULL
8619+ };
027c5e7a
AM
8620+ struct au_hdentry *hd;
8621+ struct inode *inode, *h_inode;
8622+ struct dentry *h_dentry;
8623+
8624+ err = 0;
5afbbe0d 8625+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8626+ orig_h.mode = 0;
5afbbe0d 8627+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8628+ orig_h.inode = NULL;
8629+ if (d_is_positive(orig_h.dentry)) {
8630+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8631+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8632+ }
5afbbe0d
AM
8633+ if (tmp->di_btop >= 0) {
8634+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8635+ if (d_is_positive(tmp_h.dentry)) {
8636+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8637+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8638+ }
027c5e7a
AM
8639+ }
8640+
5527c038
JR
8641+ inode = NULL;
8642+ if (d_really_is_positive(dentry))
8643+ inode = d_inode(dentry);
027c5e7a
AM
8644+ if (!orig_h.inode) {
8645+ AuDbg("nagative originally\n");
8646+ if (inode) {
8647+ au_hide(dentry);
8648+ goto out;
8649+ }
8650+ AuDebugOn(inode);
5afbbe0d 8651+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8652+ AuDebugOn(dinfo->di_bdiropq != -1);
8653+
8654+ if (!tmp_h.inode) {
8655+ AuDbg("negative --> negative\n");
8656+ /* should have only one negative lower */
5afbbe0d
AM
8657+ if (tmp->di_btop >= 0
8658+ && tmp->di_btop < dinfo->di_btop) {
8659+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8660+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8661+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8662+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8663+ hd = au_hdentry(tmp, tmp->di_btop);
8664+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8665+ dget(hd->hd_dentry));
8666+ }
8667+ au_dbg_verify_dinode(dentry);
8668+ } else {
8669+ AuDbg("negative --> positive\n");
8670+ /*
8671+ * similar to the behaviour of creating with bypassing
8672+ * aufs.
8673+ * unhash it in order to force an error in the
8674+ * succeeding create operation.
8675+ * we should not set S_DEAD here.
8676+ */
8677+ d_drop(dentry);
8678+ /* au_di_swap(tmp, dinfo); */
8679+ au_dbg_verify_dinode(dentry);
8680+ }
8681+ } else {
8682+ AuDbg("positive originally\n");
8683+ /* inode may be NULL */
8684+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8685+ if (!tmp_h.inode) {
8686+ AuDbg("positive --> negative\n");
8687+ /* or bypassing aufs */
8688+ au_hide(dentry);
5afbbe0d 8689+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8690+ dinfo->di_bwh = tmp->di_bwh;
8691+ if (inode)
8692+ err = au_refresh_hinode_self(inode);
8693+ au_dbg_verify_dinode(dentry);
8694+ } else if (orig_h.mode == tmp_h.mode) {
8695+ AuDbg("positive --> positive, same type\n");
8696+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8697+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8698+ /*
8699+ * similar to the behaviour of removing and
8700+ * creating.
8701+ */
8702+ au_hide(dentry);
8703+ if (inode)
8704+ err = au_refresh_hinode_self(inode);
8705+ au_dbg_verify_dinode(dentry);
8706+ } else {
8707+ /* fill empty slots */
5afbbe0d
AM
8708+ if (dinfo->di_btop > tmp->di_btop)
8709+ dinfo->di_btop = tmp->di_btop;
8710+ if (dinfo->di_bbot < tmp->di_bbot)
8711+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8712+ dinfo->di_bwh = tmp->di_bwh;
8713+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8714+ bbot = dinfo->di_bbot;
8715+ bindex = tmp->di_btop;
8716+ hd = au_hdentry(tmp, bindex);
8717+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8718+ if (au_h_dptr(dentry, bindex))
8719+ continue;
5afbbe0d 8720+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8721+ if (!h_dentry)
8722+ continue;
5527c038
JR
8723+ AuDebugOn(d_is_negative(h_dentry));
8724+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8725+ AuDebugOn(orig_h.mode
8726+ != (h_inode->i_mode
8727+ & S_IFMT));
8728+ au_set_h_dptr(dentry, bindex,
8729+ dget(h_dentry));
8730+ }
5afbbe0d
AM
8731+ if (inode)
8732+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8733+ au_dbg_verify_dinode(dentry);
8734+ }
8735+ } else {
8736+ AuDbg("positive --> positive, different type\n");
8737+ /* similar to the behaviour of removing and creating */
8738+ au_hide(dentry);
8739+ if (inode)
8740+ err = au_refresh_hinode_self(inode);
8741+ au_dbg_verify_dinode(dentry);
8742+ }
8743+ }
8744+
8745+out:
8746+ return err;
8747+}
8748+
79b8bda9
AM
8749+void au_refresh_dop(struct dentry *dentry, int force_reval)
8750+{
8751+ const struct dentry_operations *dop
8752+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8753+ static const unsigned int mask
8754+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8755+
8756+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8757+
8758+ if (dentry->d_op == dop)
8759+ return;
8760+
8761+ AuDbg("%pd\n", dentry);
8762+ spin_lock(&dentry->d_lock);
8763+ if (dop == &aufs_dop)
8764+ dentry->d_flags |= mask;
8765+ else
8766+ dentry->d_flags &= ~mask;
8767+ dentry->d_op = dop;
8768+ spin_unlock(&dentry->d_lock);
8769+}
8770+
027c5e7a
AM
8771+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8772+{
e2f27e51 8773+ int err, ebrange, nbr;
027c5e7a
AM
8774+ unsigned int sigen;
8775+ struct au_dinfo *dinfo, *tmp;
8776+ struct super_block *sb;
8777+ struct inode *inode;
8778+
8779+ DiMustWriteLock(dentry);
8780+ AuDebugOn(IS_ROOT(dentry));
5527c038 8781+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8782+
8783+ sb = dentry->d_sb;
027c5e7a
AM
8784+ sigen = au_sigen(sb);
8785+ err = au_digen_test(parent, sigen);
8786+ if (unlikely(err))
8787+ goto out;
8788+
e2f27e51 8789+ nbr = au_sbbot(sb) + 1;
027c5e7a 8790+ dinfo = au_di(dentry);
e2f27e51 8791+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8792+ if (unlikely(err))
8793+ goto out;
8794+ ebrange = au_dbrange_test(dentry);
8795+ if (!ebrange)
8796+ ebrange = au_do_refresh_hdentry(dentry, parent);
8797+
38d290e6 8798+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8799+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8800+ if (d_really_is_positive(dentry)) {
8801+ inode = d_inode(dentry);
027c5e7a 8802+ err = au_refresh_hinode_self(inode);
5527c038 8803+ }
027c5e7a
AM
8804+ au_dbg_verify_dinode(dentry);
8805+ if (!err)
8806+ goto out_dgen; /* success */
8807+ goto out;
8808+ }
8809+
8810+ /* temporary dinfo */
8811+ AuDbgDentry(dentry);
8812+ err = -ENOMEM;
8813+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8814+ if (unlikely(!tmp))
8815+ goto out;
8816+ au_di_swap(tmp, dinfo);
8817+ /* returns the number of positive dentries */
8818+ /*
8819+ * if current working dir is removed, it returns an error.
8820+ * but the dentry is legal.
8821+ */
5afbbe0d 8822+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8823+ AuDbgDentry(dentry);
8824+ au_di_swap(tmp, dinfo);
8825+ if (err == -ENOENT)
8826+ err = 0;
8827+ if (err >= 0) {
8828+ /* compare/refresh by dinfo */
8829+ AuDbgDentry(dentry);
8830+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8831+ au_dbg_verify_dinode(dentry);
8832+ AuTraceErr(err);
8833+ }
e2f27e51 8834+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8835+ au_rw_write_unlock(&tmp->di_rwsem);
8836+ au_di_free(tmp);
8837+ if (unlikely(err))
8838+ goto out;
8839+
8840+out_dgen:
8841+ au_update_digen(dentry);
8842+out:
8843+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8844+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8845+ AuDbgDentry(dentry);
8846+ }
8847+ AuTraceErr(err);
8848+ return err;
8849+}
8850+
b4510431
AM
8851+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8852+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8853+{
8854+ int err, valid;
027c5e7a
AM
8855+
8856+ err = 0;
8857+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8858+ goto out;
027c5e7a
AM
8859+
8860+ AuDbg("b%d\n", bindex);
b4510431
AM
8861+ /*
8862+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8863+ * due to whiteout and branch permission.
8864+ */
8865+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8866+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8867+ /* it may return tri-state */
8868+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8869+
8870+ if (unlikely(valid < 0))
8871+ err = valid;
8872+ else if (!valid)
8873+ err = -EINVAL;
8874+
4f0767ce 8875+out:
1facf9fc 8876+ AuTraceErr(err);
8877+ return err;
8878+}
8879+
8880+/* todo: remove this */
8881+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8882+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8883+{
8884+ int err;
8885+ umode_t mode, h_mode;
5afbbe0d 8886+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8887+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8888+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8889+ struct dentry *h_dentry;
8890+ struct qstr *name, *h_name;
8891+
8892+ err = 0;
8893+ plus = 0;
8894+ mode = 0;
1facf9fc 8895+ ibs = -1;
8896+ ibe = -1;
8897+ unhashed = !!d_unhashed(dentry);
8898+ is_root = !!IS_ROOT(dentry);
8899+ name = &dentry->d_name;
38d290e6 8900+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8901+
8902+ /*
7f207e10
AM
8903+ * Theoretically, REVAL test should be unnecessary in case of
8904+ * {FS,I}NOTIFY.
8905+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8906+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8907+ * Let's do REVAL test too.
8908+ */
8909+ if (do_udba && inode) {
8910+ mode = (inode->i_mode & S_IFMT);
8911+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
8912+ ibs = au_ibtop(inode);
8913+ ibe = au_ibbot(inode);
1facf9fc 8914+ }
8915+
5afbbe0d
AM
8916+ btop = au_dbtop(dentry);
8917+ btail = btop;
1facf9fc 8918+ if (inode && S_ISDIR(inode->i_mode))
8919+ btail = au_dbtaildir(dentry);
5afbbe0d 8920+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8921+ h_dentry = au_h_dptr(dentry, bindex);
8922+ if (!h_dentry)
8923+ continue;
8924+
523b37e3
AM
8925+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8926+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 8927+ spin_lock(&h_dentry->d_lock);
1facf9fc 8928+ h_name = &h_dentry->d_name;
8929+ if (unlikely(do_udba
8930+ && !is_root
523b37e3
AM
8931+ && ((!h_nfs
8932+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 8933+ || (!tmpfile && !dirren
38d290e6
JR
8934+ && !au_qstreq(name, h_name))
8935+ ))
523b37e3
AM
8936+ || (h_nfs
8937+ && !(flags & LOOKUP_OPEN)
8938+ && (h_dentry->d_flags
8939+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 8940+ )) {
38d290e6
JR
8941+ int h_unhashed;
8942+
8943+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 8944+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
8945+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8946+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 8947+ goto err;
8948+ }
027c5e7a 8949+ spin_unlock(&h_dentry->d_lock);
1facf9fc 8950+
b4510431 8951+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 8952+ if (unlikely(err))
8953+ /* do not goto err, to keep the errno */
8954+ break;
8955+
8956+ /* todo: plink too? */
8957+ if (!do_udba)
8958+ continue;
8959+
8960+ /* UDBA tests */
5527c038 8961+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 8962+ goto err;
8963+
5527c038
JR
8964+ h_inode = NULL;
8965+ if (d_is_positive(h_dentry))
8966+ h_inode = d_inode(h_dentry);
1facf9fc 8967+ h_plus = plus;
8968+ h_mode = mode;
8969+ h_cached_inode = h_inode;
8970+ if (h_inode) {
8971+ h_mode = (h_inode->i_mode & S_IFMT);
8972+ h_plus = (h_inode->i_nlink > 0);
8973+ }
8974+ if (inode && ibs <= bindex && bindex <= ibe)
8975+ h_cached_inode = au_h_iptr(inode, bindex);
8976+
523b37e3 8977+ if (!h_nfs) {
38d290e6 8978+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
8979+ goto err;
8980+ } else {
8981+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8982+ && !is_root
8983+ && !IS_ROOT(h_dentry)
8984+ && unhashed != d_unhashed(h_dentry)))
8985+ goto err;
8986+ }
8987+ if (unlikely(mode != h_mode
1facf9fc 8988+ || h_cached_inode != h_inode))
8989+ goto err;
8990+ continue;
8991+
f6b6e03d 8992+err:
1facf9fc 8993+ err = -EINVAL;
8994+ break;
8995+ }
8996+
523b37e3 8997+ AuTraceErr(err);
1facf9fc 8998+ return err;
8999+}
9000+
027c5e7a 9001+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 9002+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9003+{
9004+ int err;
9005+ struct dentry *parent;
1facf9fc 9006+
027c5e7a 9007+ if (!au_digen_test(dentry, sigen))
1facf9fc 9008+ return 0;
9009+
9010+ parent = dget_parent(dentry);
9011+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9012+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 9013+ au_dbg_verify_gen(parent, sigen);
027c5e7a 9014+ err = au_refresh_dentry(dentry, parent);
1facf9fc 9015+ di_read_unlock(parent, AuLock_IR);
9016+ dput(parent);
027c5e7a 9017+ AuTraceErr(err);
1facf9fc 9018+ return err;
9019+}
9020+
9021+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9022+{
9023+ int err;
9024+ struct dentry *d, *parent;
1facf9fc 9025+
027c5e7a 9026+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 9027+ return simple_reval_dpath(dentry, sigen);
9028+
9029+ /* slow loop, keep it simple and stupid */
9030+ /* cf: au_cpup_dirs() */
9031+ err = 0;
9032+ parent = NULL;
027c5e7a 9033+ while (au_digen_test(dentry, sigen)) {
1facf9fc 9034+ d = dentry;
9035+ while (1) {
9036+ dput(parent);
9037+ parent = dget_parent(d);
027c5e7a 9038+ if (!au_digen_test(parent, sigen))
1facf9fc 9039+ break;
9040+ d = parent;
9041+ }
9042+
1facf9fc 9043+ if (d != dentry)
027c5e7a 9044+ di_write_lock_child2(d);
1facf9fc 9045+
9046+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9047+ if (au_digen_test(d, sigen)) {
9048+ /*
9049+ * todo: consolidate with simple_reval_dpath(),
9050+ * do_refresh() and au_reval_for_attr().
9051+ */
1facf9fc 9052+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9053+ err = au_refresh_dentry(d, parent);
1facf9fc 9054+ di_read_unlock(parent, AuLock_IR);
9055+ }
9056+
9057+ if (d != dentry)
9058+ di_write_unlock(d);
9059+ dput(parent);
9060+ if (unlikely(err))
9061+ break;
9062+ }
9063+
9064+ return err;
9065+}
9066+
9067+/*
9068+ * if valid returns 1, otherwise 0.
9069+ */
b4510431 9070+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9071+{
9072+ int valid, err;
9073+ unsigned int sigen;
8b6a4947 9074+ unsigned char do_udba, dirren;
1facf9fc 9075+ struct super_block *sb;
9076+ struct inode *inode;
9077+
027c5e7a 9078+ /* todo: support rcu-walk? */
b4510431 9079+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9080+ return -ECHILD;
9081+
9082+ valid = 0;
9083+ if (unlikely(!au_di(dentry)))
9084+ goto out;
9085+
e49829fe 9086+ valid = 1;
1facf9fc 9087+ sb = dentry->d_sb;
e49829fe
JR
9088+ /*
9089+ * todo: very ugly
9090+ * i_mutex of parent dir may be held,
9091+ * but we should not return 'invalid' due to busy.
9092+ */
9093+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9094+ if (unlikely(err)) {
9095+ valid = err;
027c5e7a 9096+ AuTraceErr(err);
e49829fe
JR
9097+ goto out;
9098+ }
5527c038
JR
9099+ inode = NULL;
9100+ if (d_really_is_positive(dentry))
9101+ inode = d_inode(dentry);
5afbbe0d 9102+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9103+ err = -EINVAL;
9104+ AuTraceErr(err);
9105+ goto out_dgrade;
9106+ }
027c5e7a
AM
9107+ if (unlikely(au_dbrange_test(dentry))) {
9108+ err = -EINVAL;
9109+ AuTraceErr(err);
9110+ goto out_dgrade;
1facf9fc 9111+ }
027c5e7a
AM
9112+
9113+ sigen = au_sigen(sb);
9114+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9115+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9116+ err = au_reval_dpath(dentry, sigen);
9117+ if (unlikely(err)) {
9118+ AuTraceErr(err);
1facf9fc 9119+ goto out_dgrade;
027c5e7a 9120+ }
1facf9fc 9121+ }
9122+ di_downgrade_lock(dentry, AuLock_IR);
9123+
1facf9fc 9124+ err = -EINVAL;
c1595e42 9125+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9126+ && inode
38d290e6 9127+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9128+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9129+ AuTraceErr(err);
027c5e7a 9130+ goto out_inval;
79b8bda9 9131+ }
027c5e7a 9132+
1facf9fc 9133+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9134+ if (do_udba && inode) {
5afbbe0d 9135+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9136+ struct inode *h_inode;
1facf9fc 9137+
5afbbe0d
AM
9138+ if (btop >= 0) {
9139+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9140+ if (h_inode && au_test_higen(inode, h_inode)) {
9141+ AuTraceErr(err);
027c5e7a 9142+ goto out_inval;
79b8bda9 9143+ }
027c5e7a 9144+ }
1facf9fc 9145+ }
9146+
8b6a4947
AM
9147+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9148+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9149+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9150+ err = -EIO;
523b37e3
AM
9151+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9152+ dentry, err);
027c5e7a 9153+ }
e49829fe 9154+ goto out_inval;
1facf9fc 9155+
4f0767ce 9156+out_dgrade:
1facf9fc 9157+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9158+out_inval:
1facf9fc 9159+ aufs_read_unlock(dentry, AuLock_IR);
9160+ AuTraceErr(err);
9161+ valid = !err;
e49829fe 9162+out:
027c5e7a 9163+ if (!valid) {
523b37e3 9164+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9165+ d_drop(dentry);
9166+ }
1facf9fc 9167+ return valid;
9168+}
9169+
9170+static void aufs_d_release(struct dentry *dentry)
9171+{
027c5e7a 9172+ if (au_di(dentry)) {
4a4d8108
AM
9173+ au_di_fin(dentry);
9174+ au_hn_di_reinit(dentry);
1facf9fc 9175+ }
1facf9fc 9176+}
9177+
4a4d8108 9178+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9179+ .d_revalidate = aufs_d_revalidate,
9180+ .d_weak_revalidate = aufs_d_revalidate,
9181+ .d_release = aufs_d_release
1facf9fc 9182+};
79b8bda9
AM
9183+
9184+/* aufs_dop without d_revalidate */
9185+const struct dentry_operations aufs_dop_noreval = {
9186+ .d_release = aufs_d_release
9187+};
7f207e10
AM
9188diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9189--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 9190+++ linux/fs/aufs/dentry.h 2018-04-15 08:49:13.397817296 +0200
8b6a4947 9191@@ -0,0 +1,266 @@
1facf9fc 9192+/*
b00004a5 9193+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 9194+ *
9195+ * This program, aufs is free software; you can redistribute it and/or modify
9196+ * it under the terms of the GNU General Public License as published by
9197+ * the Free Software Foundation; either version 2 of the License, or
9198+ * (at your option) any later version.
dece6358
AM
9199+ *
9200+ * This program is distributed in the hope that it will be useful,
9201+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9202+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9203+ * GNU General Public License for more details.
9204+ *
9205+ * You should have received a copy of the GNU General Public License
523b37e3 9206+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9207+ */
9208+
9209+/*
9210+ * lookup and dentry operations
9211+ */
9212+
9213+#ifndef __AUFS_DENTRY_H__
9214+#define __AUFS_DENTRY_H__
9215+
9216+#ifdef __KERNEL__
9217+
dece6358 9218+#include <linux/dcache.h>
8b6a4947 9219+#include "dirren.h"
1facf9fc 9220+#include "rwsem.h"
9221+
1facf9fc 9222+struct au_hdentry {
9223+ struct dentry *hd_dentry;
027c5e7a 9224+ aufs_bindex_t hd_id;
1facf9fc 9225+};
9226+
9227+struct au_dinfo {
9228+ atomic_t di_generation;
9229+
dece6358 9230+ struct au_rwsem di_rwsem;
5afbbe0d 9231+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9232+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9233+ struct au_hdentry *di_hdentry;
4a4d8108 9234+} ____cacheline_aligned_in_smp;
1facf9fc 9235+
9236+/* ---------------------------------------------------------------------- */
9237+
5afbbe0d
AM
9238+/* flags for au_lkup_dentry() */
9239+#define AuLkup_ALLOW_NEG 1
9240+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9241+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9242+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9243+#define au_fset_lkup(flags, name) \
9244+ do { (flags) |= AuLkup_##name; } while (0)
9245+#define au_fclr_lkup(flags, name) \
9246+ do { (flags) &= ~AuLkup_##name; } while (0)
9247+
8b6a4947
AM
9248+#ifndef CONFIG_AUFS_DIRREN
9249+#undef AuLkup_DIRREN
9250+#define AuLkup_DIRREN 0
9251+#endif
9252+
9253+struct au_do_lookup_args {
9254+ unsigned int flags;
9255+ mode_t type;
9256+ struct qstr whname, *name;
9257+ struct au_dr_lookup dirren;
9258+};
9259+
5afbbe0d
AM
9260+/* ---------------------------------------------------------------------- */
9261+
1facf9fc 9262+/* dentry.c */
79b8bda9 9263+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9264+struct au_branch;
076b876e 9265+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 9266+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9267+ struct dentry *h_parent, struct au_branch *br);
9268+
5afbbe0d
AM
9269+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9270+ unsigned int flags);
86dc4139 9271+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9272+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9273+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9274+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9275+
9276+/* dinfo.c */
4a4d8108 9277+void au_di_init_once(void *_di);
027c5e7a
AM
9278+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9279+void au_di_free(struct au_dinfo *dinfo);
9280+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9281+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9282+int au_di_init(struct dentry *dentry);
9283+void au_di_fin(struct dentry *dentry);
e2f27e51 9284+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9285+
9286+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9287+void di_read_unlock(struct dentry *d, int flags);
9288+void di_downgrade_lock(struct dentry *d, int flags);
9289+void di_write_lock(struct dentry *d, unsigned int lsc);
9290+void di_write_unlock(struct dentry *d);
9291+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9292+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9293+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9294+
9295+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9296+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9297+aufs_bindex_t au_dbtail(struct dentry *dentry);
9298+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9299+
9300+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9301+ struct dentry *h_dentry);
027c5e7a
AM
9302+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9303+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9304+void au_update_digen(struct dentry *dentry);
9305+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9306+void au_update_dbtop(struct dentry *dentry);
9307+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9308+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9309+
9310+/* ---------------------------------------------------------------------- */
9311+
9312+static inline struct au_dinfo *au_di(struct dentry *dentry)
9313+{
9314+ return dentry->d_fsdata;
9315+}
9316+
9317+/* ---------------------------------------------------------------------- */
9318+
9319+/* lock subclass for dinfo */
9320+enum {
9321+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9322+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9323+ AuLsc_DI_CHILD3, /* copyup dirs */
9324+ AuLsc_DI_PARENT,
9325+ AuLsc_DI_PARENT2,
027c5e7a
AM
9326+ AuLsc_DI_PARENT3,
9327+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9328+};
9329+
9330+/*
9331+ * di_read_lock_child, di_write_lock_child,
9332+ * di_read_lock_child2, di_write_lock_child2,
9333+ * di_read_lock_child3, di_write_lock_child3,
9334+ * di_read_lock_parent, di_write_lock_parent,
9335+ * di_read_lock_parent2, di_write_lock_parent2,
9336+ * di_read_lock_parent3, di_write_lock_parent3,
9337+ */
9338+#define AuReadLockFunc(name, lsc) \
9339+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9340+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9341+
9342+#define AuWriteLockFunc(name, lsc) \
9343+static inline void di_write_lock_##name(struct dentry *d) \
9344+{ di_write_lock(d, AuLsc_DI_##lsc); }
9345+
9346+#define AuRWLockFuncs(name, lsc) \
9347+ AuReadLockFunc(name, lsc) \
9348+ AuWriteLockFunc(name, lsc)
9349+
9350+AuRWLockFuncs(child, CHILD);
9351+AuRWLockFuncs(child2, CHILD2);
9352+AuRWLockFuncs(child3, CHILD3);
9353+AuRWLockFuncs(parent, PARENT);
9354+AuRWLockFuncs(parent2, PARENT2);
9355+AuRWLockFuncs(parent3, PARENT3);
9356+
9357+#undef AuReadLockFunc
9358+#undef AuWriteLockFunc
9359+#undef AuRWLockFuncs
9360+
9361+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9362+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9363+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9364+
9365+/* ---------------------------------------------------------------------- */
9366+
9367+/* todo: memory barrier? */
9368+static inline unsigned int au_digen(struct dentry *d)
9369+{
9370+ return atomic_read(&au_di(d)->di_generation);
9371+}
9372+
9373+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9374+{
9375+ hdentry->hd_dentry = NULL;
9376+}
9377+
5afbbe0d
AM
9378+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9379+ aufs_bindex_t bindex)
9380+{
9381+ return di->di_hdentry + bindex;
9382+}
9383+
1facf9fc 9384+static inline void au_hdput(struct au_hdentry *hd)
9385+{
4a4d8108
AM
9386+ if (hd)
9387+ dput(hd->hd_dentry);
1facf9fc 9388+}
9389+
5afbbe0d 9390+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9391+{
1308ab2a 9392+ DiMustAnyLock(dentry);
5afbbe0d 9393+ return au_di(dentry)->di_btop;
1facf9fc 9394+}
9395+
5afbbe0d 9396+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9397+{
1308ab2a 9398+ DiMustAnyLock(dentry);
5afbbe0d 9399+ return au_di(dentry)->di_bbot;
1facf9fc 9400+}
9401+
9402+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9403+{
1308ab2a 9404+ DiMustAnyLock(dentry);
1facf9fc 9405+ return au_di(dentry)->di_bwh;
9406+}
9407+
9408+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9409+{
1308ab2a 9410+ DiMustAnyLock(dentry);
1facf9fc 9411+ return au_di(dentry)->di_bdiropq;
9412+}
9413+
9414+/* todo: hard/soft set? */
5afbbe0d 9415+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9416+{
1308ab2a 9417+ DiMustWriteLock(dentry);
5afbbe0d 9418+ au_di(dentry)->di_btop = bindex;
1facf9fc 9419+}
9420+
5afbbe0d 9421+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9422+{
1308ab2a 9423+ DiMustWriteLock(dentry);
5afbbe0d 9424+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9425+}
9426+
9427+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9428+{
1308ab2a 9429+ DiMustWriteLock(dentry);
5afbbe0d 9430+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9431+ au_di(dentry)->di_bwh = bindex;
9432+}
9433+
9434+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9435+{
1308ab2a 9436+ DiMustWriteLock(dentry);
1facf9fc 9437+ au_di(dentry)->di_bdiropq = bindex;
9438+}
9439+
9440+/* ---------------------------------------------------------------------- */
9441+
4a4d8108 9442+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9443+static inline void au_digen_dec(struct dentry *d)
9444+{
e49829fe 9445+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9446+}
9447+
4a4d8108 9448+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9449+{
9450+ dentry->d_fsdata = NULL;
9451+}
9452+#else
4a4d8108
AM
9453+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9454+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9455+
9456+#endif /* __KERNEL__ */
9457+#endif /* __AUFS_DENTRY_H__ */
7f207e10
AM
9458diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9459--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 9460+++ linux/fs/aufs/dinfo.c 2018-04-15 08:49:13.397817296 +0200
e2f27e51 9461@@ -0,0 +1,553 @@
1facf9fc 9462+/*
b00004a5 9463+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 9464+ *
9465+ * This program, aufs is free software; you can redistribute it and/or modify
9466+ * it under the terms of the GNU General Public License as published by
9467+ * the Free Software Foundation; either version 2 of the License, or
9468+ * (at your option) any later version.
dece6358
AM
9469+ *
9470+ * This program is distributed in the hope that it will be useful,
9471+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9472+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9473+ * GNU General Public License for more details.
9474+ *
9475+ * You should have received a copy of the GNU General Public License
523b37e3 9476+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9477+ */
9478+
9479+/*
9480+ * dentry private data
9481+ */
9482+
9483+#include "aufs.h"
9484+
e49829fe 9485+void au_di_init_once(void *_dinfo)
4a4d8108 9486+{
e49829fe 9487+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9488+
e49829fe 9489+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9490+}
9491+
027c5e7a 9492+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9493+{
9494+ struct au_dinfo *dinfo;
027c5e7a 9495+ int nbr, i;
1facf9fc 9496+
9497+ dinfo = au_cache_alloc_dinfo();
9498+ if (unlikely(!dinfo))
9499+ goto out;
9500+
5afbbe0d 9501+ nbr = au_sbbot(sb) + 1;
1facf9fc 9502+ if (nbr <= 0)
9503+ nbr = 1;
9504+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9505+ if (dinfo->di_hdentry) {
9506+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9507+ dinfo->di_btop = -1;
9508+ dinfo->di_bbot = -1;
027c5e7a
AM
9509+ dinfo->di_bwh = -1;
9510+ dinfo->di_bdiropq = -1;
38d290e6 9511+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9512+ for (i = 0; i < nbr; i++)
9513+ dinfo->di_hdentry[i].hd_id = -1;
9514+ goto out;
9515+ }
1facf9fc 9516+
1c60b727 9517+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9518+ dinfo = NULL;
9519+
4f0767ce 9520+out:
027c5e7a 9521+ return dinfo;
1facf9fc 9522+}
9523+
027c5e7a 9524+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9525+{
4a4d8108 9526+ struct au_hdentry *p;
5afbbe0d 9527+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9528+
9529+ /* dentry may not be revalidated */
5afbbe0d 9530+ bindex = dinfo->di_btop;
4a4d8108 9531+ if (bindex >= 0) {
5afbbe0d
AM
9532+ bbot = dinfo->di_bbot;
9533+ p = au_hdentry(dinfo, bindex);
9534+ while (bindex++ <= bbot)
4a4d8108
AM
9535+ au_hdput(p++);
9536+ }
1c60b727
AM
9537+ kfree(dinfo->di_hdentry);
9538+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9539+}
9540+
9541+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9542+{
9543+ struct au_hdentry *p;
9544+ aufs_bindex_t bi;
9545+
9546+ AuRwMustWriteLock(&a->di_rwsem);
9547+ AuRwMustWriteLock(&b->di_rwsem);
9548+
9549+#define DiSwap(v, name) \
9550+ do { \
9551+ v = a->di_##name; \
9552+ a->di_##name = b->di_##name; \
9553+ b->di_##name = v; \
9554+ } while (0)
9555+
9556+ DiSwap(p, hdentry);
5afbbe0d
AM
9557+ DiSwap(bi, btop);
9558+ DiSwap(bi, bbot);
027c5e7a
AM
9559+ DiSwap(bi, bwh);
9560+ DiSwap(bi, bdiropq);
9561+ /* smp_mb(); */
9562+
9563+#undef DiSwap
9564+}
9565+
9566+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9567+{
9568+ AuRwMustWriteLock(&dst->di_rwsem);
9569+ AuRwMustWriteLock(&src->di_rwsem);
9570+
5afbbe0d
AM
9571+ dst->di_btop = src->di_btop;
9572+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9573+ dst->di_bwh = src->di_bwh;
9574+ dst->di_bdiropq = src->di_bdiropq;
9575+ /* smp_mb(); */
9576+}
9577+
9578+int au_di_init(struct dentry *dentry)
9579+{
9580+ int err;
9581+ struct super_block *sb;
9582+ struct au_dinfo *dinfo;
9583+
9584+ err = 0;
9585+ sb = dentry->d_sb;
9586+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9587+ if (dinfo) {
9588+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9589+ /* smp_mb(); */ /* atomic_set */
9590+ dentry->d_fsdata = dinfo;
9591+ } else
9592+ err = -ENOMEM;
9593+
9594+ return err;
9595+}
9596+
9597+void au_di_fin(struct dentry *dentry)
9598+{
9599+ struct au_dinfo *dinfo;
9600+
9601+ dinfo = au_di(dentry);
9602+ AuRwDestroy(&dinfo->di_rwsem);
9603+ au_di_free(dinfo);
4a4d8108
AM
9604+}
9605+
e2f27e51 9606+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9607+{
9608+ int err, sz;
9609+ struct au_hdentry *hdp;
9610+
1308ab2a 9611+ AuRwMustWriteLock(&dinfo->di_rwsem);
9612+
1facf9fc 9613+ err = -ENOMEM;
5afbbe0d 9614+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9615+ if (!sz)
9616+ sz = sizeof(*hdp);
e2f27e51
AM
9617+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9618+ may_shrink);
1facf9fc 9619+ if (hdp) {
9620+ dinfo->di_hdentry = hdp;
9621+ err = 0;
9622+ }
9623+
9624+ return err;
9625+}
9626+
9627+/* ---------------------------------------------------------------------- */
9628+
9629+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9630+{
9631+ switch (lsc) {
9632+ case AuLsc_DI_CHILD:
9633+ ii_write_lock_child(inode);
9634+ break;
9635+ case AuLsc_DI_CHILD2:
9636+ ii_write_lock_child2(inode);
9637+ break;
9638+ case AuLsc_DI_CHILD3:
9639+ ii_write_lock_child3(inode);
9640+ break;
9641+ case AuLsc_DI_PARENT:
9642+ ii_write_lock_parent(inode);
9643+ break;
9644+ case AuLsc_DI_PARENT2:
9645+ ii_write_lock_parent2(inode);
9646+ break;
9647+ case AuLsc_DI_PARENT3:
9648+ ii_write_lock_parent3(inode);
9649+ break;
9650+ default:
9651+ BUG();
9652+ }
9653+}
9654+
9655+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9656+{
9657+ switch (lsc) {
9658+ case AuLsc_DI_CHILD:
9659+ ii_read_lock_child(inode);
9660+ break;
9661+ case AuLsc_DI_CHILD2:
9662+ ii_read_lock_child2(inode);
9663+ break;
9664+ case AuLsc_DI_CHILD3:
9665+ ii_read_lock_child3(inode);
9666+ break;
9667+ case AuLsc_DI_PARENT:
9668+ ii_read_lock_parent(inode);
9669+ break;
9670+ case AuLsc_DI_PARENT2:
9671+ ii_read_lock_parent2(inode);
9672+ break;
9673+ case AuLsc_DI_PARENT3:
9674+ ii_read_lock_parent3(inode);
9675+ break;
9676+ default:
9677+ BUG();
9678+ }
9679+}
9680+
9681+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9682+{
5527c038
JR
9683+ struct inode *inode;
9684+
dece6358 9685+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9686+ if (d_really_is_positive(d)) {
9687+ inode = d_inode(d);
1facf9fc 9688+ if (au_ftest_lock(flags, IW))
5527c038 9689+ do_ii_write_lock(inode, lsc);
1facf9fc 9690+ else if (au_ftest_lock(flags, IR))
5527c038 9691+ do_ii_read_lock(inode, lsc);
1facf9fc 9692+ }
9693+}
9694+
9695+void di_read_unlock(struct dentry *d, int flags)
9696+{
5527c038
JR
9697+ struct inode *inode;
9698+
9699+ if (d_really_is_positive(d)) {
9700+ inode = d_inode(d);
027c5e7a
AM
9701+ if (au_ftest_lock(flags, IW)) {
9702+ au_dbg_verify_dinode(d);
5527c038 9703+ ii_write_unlock(inode);
027c5e7a
AM
9704+ } else if (au_ftest_lock(flags, IR)) {
9705+ au_dbg_verify_dinode(d);
5527c038 9706+ ii_read_unlock(inode);
027c5e7a 9707+ }
1facf9fc 9708+ }
dece6358 9709+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9710+}
9711+
9712+void di_downgrade_lock(struct dentry *d, int flags)
9713+{
5527c038
JR
9714+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9715+ ii_downgrade_lock(d_inode(d));
dece6358 9716+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9717+}
9718+
9719+void di_write_lock(struct dentry *d, unsigned int lsc)
9720+{
dece6358 9721+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9722+ if (d_really_is_positive(d))
9723+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9724+}
9725+
9726+void di_write_unlock(struct dentry *d)
9727+{
027c5e7a 9728+ au_dbg_verify_dinode(d);
5527c038
JR
9729+ if (d_really_is_positive(d))
9730+ ii_write_unlock(d_inode(d));
dece6358 9731+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9732+}
9733+
9734+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9735+{
9736+ AuDebugOn(d1 == d2
5527c038 9737+ || d_inode(d1) == d_inode(d2)
1facf9fc 9738+ || d1->d_sb != d2->d_sb);
9739+
521ced18
JR
9740+ if ((isdir && au_test_subdir(d1, d2))
9741+ || d1 < d2) {
1facf9fc 9742+ di_write_lock_child(d1);
9743+ di_write_lock_child2(d2);
9744+ } else {
1facf9fc 9745+ di_write_lock_child(d2);
9746+ di_write_lock_child2(d1);
9747+ }
9748+}
9749+
9750+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9751+{
9752+ AuDebugOn(d1 == d2
5527c038 9753+ || d_inode(d1) == d_inode(d2)
1facf9fc 9754+ || d1->d_sb != d2->d_sb);
9755+
521ced18
JR
9756+ if ((isdir && au_test_subdir(d1, d2))
9757+ || d1 < d2) {
1facf9fc 9758+ di_write_lock_parent(d1);
9759+ di_write_lock_parent2(d2);
9760+ } else {
1facf9fc 9761+ di_write_lock_parent(d2);
9762+ di_write_lock_parent2(d1);
9763+ }
9764+}
9765+
9766+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9767+{
9768+ di_write_unlock(d1);
5527c038 9769+ if (d_inode(d1) == d_inode(d2))
dece6358 9770+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9771+ else
9772+ di_write_unlock(d2);
9773+}
9774+
9775+/* ---------------------------------------------------------------------- */
9776+
9777+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9778+{
9779+ struct dentry *d;
9780+
1308ab2a 9781+ DiMustAnyLock(dentry);
9782+
5afbbe0d 9783+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9784+ return NULL;
9785+ AuDebugOn(bindex < 0);
5afbbe0d 9786+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9787+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9788+ return d;
9789+}
9790+
2cbb1c4b
JR
9791+/*
9792+ * extended version of au_h_dptr().
38d290e6
JR
9793+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9794+ * error.
2cbb1c4b
JR
9795+ */
9796+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9797+{
9798+ struct dentry *h_dentry;
9799+ struct inode *inode, *h_inode;
9800+
5527c038 9801+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9802+
9803+ h_dentry = NULL;
5afbbe0d
AM
9804+ if (au_dbtop(dentry) <= bindex
9805+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9806+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9807+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9808+ dget(h_dentry);
9809+ goto out; /* success */
9810+ }
9811+
5527c038 9812+ inode = d_inode(dentry);
5afbbe0d
AM
9813+ AuDebugOn(bindex < au_ibtop(inode));
9814+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9815+ h_inode = au_h_iptr(inode, bindex);
9816+ h_dentry = d_find_alias(h_inode);
9817+ if (h_dentry) {
9818+ if (!IS_ERR(h_dentry)) {
38d290e6 9819+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9820+ goto out; /* success */
9821+ dput(h_dentry);
9822+ } else
9823+ goto out;
9824+ }
9825+
9826+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9827+ h_dentry = au_plink_lkup(inode, bindex);
9828+ AuDebugOn(!h_dentry);
9829+ if (!IS_ERR(h_dentry)) {
9830+ if (!au_d_hashed_positive(h_dentry))
9831+ goto out; /* success */
9832+ dput(h_dentry);
9833+ h_dentry = NULL;
9834+ }
9835+ }
9836+
9837+out:
9838+ AuDbgDentry(h_dentry);
9839+ return h_dentry;
9840+}
9841+
1facf9fc 9842+aufs_bindex_t au_dbtail(struct dentry *dentry)
9843+{
5afbbe0d 9844+ aufs_bindex_t bbot, bwh;
1facf9fc 9845+
5afbbe0d
AM
9846+ bbot = au_dbbot(dentry);
9847+ if (0 <= bbot) {
1facf9fc 9848+ bwh = au_dbwh(dentry);
9849+ if (!bwh)
9850+ return bwh;
5afbbe0d 9851+ if (0 < bwh && bwh < bbot)
1facf9fc 9852+ return bwh - 1;
9853+ }
5afbbe0d 9854+ return bbot;
1facf9fc 9855+}
9856+
9857+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9858+{
5afbbe0d 9859+ aufs_bindex_t bbot, bopq;
1facf9fc 9860+
5afbbe0d
AM
9861+ bbot = au_dbtail(dentry);
9862+ if (0 <= bbot) {
1facf9fc 9863+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9864+ if (0 <= bopq && bopq < bbot)
9865+ bbot = bopq;
1facf9fc 9866+ }
5afbbe0d 9867+ return bbot;
1facf9fc 9868+}
9869+
9870+/* ---------------------------------------------------------------------- */
9871+
9872+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9873+ struct dentry *h_dentry)
9874+{
5afbbe0d
AM
9875+ struct au_dinfo *dinfo;
9876+ struct au_hdentry *hd;
027c5e7a 9877+ struct au_branch *br;
1facf9fc 9878+
1308ab2a 9879+ DiMustWriteLock(dentry);
9880+
5afbbe0d
AM
9881+ dinfo = au_di(dentry);
9882+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9883+ au_hdput(hd);
1facf9fc 9884+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9885+ if (h_dentry) {
9886+ br = au_sbr(dentry->d_sb, bindex);
9887+ hd->hd_id = br->br_id;
9888+ }
9889+}
9890+
9891+int au_dbrange_test(struct dentry *dentry)
9892+{
9893+ int err;
5afbbe0d 9894+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9895+
9896+ err = 0;
5afbbe0d
AM
9897+ btop = au_dbtop(dentry);
9898+ bbot = au_dbbot(dentry);
9899+ if (btop >= 0)
9900+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9901+ else {
9902+ err = -EIO;
5afbbe0d 9903+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9904+ }
9905+
9906+ return err;
9907+}
9908+
9909+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9910+{
9911+ int err;
9912+
9913+ err = 0;
9914+ if (unlikely(au_digen(dentry) != sigen
5527c038 9915+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
9916+ err = -EIO;
9917+
9918+ return err;
1facf9fc 9919+}
9920+
9921+void au_update_digen(struct dentry *dentry)
9922+{
9923+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9924+ /* smp_mb(); */ /* atomic_set */
9925+}
9926+
9927+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9928+{
9929+ struct au_dinfo *dinfo;
9930+ struct dentry *h_d;
4a4d8108 9931+ struct au_hdentry *hdp;
5afbbe0d 9932+ aufs_bindex_t bindex, bbot;
1facf9fc 9933+
1308ab2a 9934+ DiMustWriteLock(dentry);
9935+
1facf9fc 9936+ dinfo = au_di(dentry);
5afbbe0d 9937+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 9938+ return;
9939+
9940+ if (do_put_zero) {
5afbbe0d
AM
9941+ bbot = dinfo->di_bbot;
9942+ bindex = dinfo->di_btop;
9943+ hdp = au_hdentry(dinfo, bindex);
9944+ for (; bindex <= bbot; bindex++, hdp++) {
9945+ h_d = hdp->hd_dentry;
5527c038 9946+ if (h_d && d_is_negative(h_d))
1facf9fc 9947+ au_set_h_dptr(dentry, bindex, NULL);
9948+ }
9949+ }
9950+
5afbbe0d
AM
9951+ dinfo->di_btop = 0;
9952+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9953+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9954+ if (hdp->hd_dentry)
1facf9fc 9955+ break;
5afbbe0d
AM
9956+ if (dinfo->di_btop > dinfo->di_bbot) {
9957+ dinfo->di_btop = -1;
9958+ dinfo->di_bbot = -1;
1facf9fc 9959+ return;
9960+ }
9961+
5afbbe0d
AM
9962+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9963+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9964+ if (hdp->hd_dentry)
1facf9fc 9965+ break;
5afbbe0d 9966+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 9967+}
9968+
5afbbe0d 9969+void au_update_dbtop(struct dentry *dentry)
1facf9fc 9970+{
5afbbe0d 9971+ aufs_bindex_t bindex, bbot;
1facf9fc 9972+ struct dentry *h_dentry;
9973+
5afbbe0d
AM
9974+ bbot = au_dbbot(dentry);
9975+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 9976+ h_dentry = au_h_dptr(dentry, bindex);
9977+ if (!h_dentry)
9978+ continue;
5527c038 9979+ if (d_is_positive(h_dentry)) {
5afbbe0d 9980+ au_set_dbtop(dentry, bindex);
1facf9fc 9981+ return;
9982+ }
9983+ au_set_h_dptr(dentry, bindex, NULL);
9984+ }
9985+}
9986+
5afbbe0d 9987+void au_update_dbbot(struct dentry *dentry)
1facf9fc 9988+{
5afbbe0d 9989+ aufs_bindex_t bindex, btop;
1facf9fc 9990+ struct dentry *h_dentry;
9991+
5afbbe0d
AM
9992+ btop = au_dbtop(dentry);
9993+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 9994+ h_dentry = au_h_dptr(dentry, bindex);
9995+ if (!h_dentry)
9996+ continue;
5527c038 9997+ if (d_is_positive(h_dentry)) {
5afbbe0d 9998+ au_set_dbbot(dentry, bindex);
1facf9fc 9999+ return;
10000+ }
10001+ au_set_h_dptr(dentry, bindex, NULL);
10002+ }
10003+}
10004+
10005+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10006+{
5afbbe0d 10007+ aufs_bindex_t bindex, bbot;
1facf9fc 10008+
5afbbe0d
AM
10009+ bbot = au_dbbot(dentry);
10010+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 10011+ if (au_h_dptr(dentry, bindex) == h_dentry)
10012+ return bindex;
10013+ return -1;
10014+}
7f207e10
AM
10015diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10016--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 10017+++ linux/fs/aufs/dir.c 2018-06-04 09:08:09.184746078 +0200
1c60b727 10018@@ -0,0 +1,759 @@
1facf9fc 10019+/*
b00004a5 10020+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 10021+ *
10022+ * This program, aufs is free software; you can redistribute it and/or modify
10023+ * it under the terms of the GNU General Public License as published by
10024+ * the Free Software Foundation; either version 2 of the License, or
10025+ * (at your option) any later version.
dece6358
AM
10026+ *
10027+ * This program is distributed in the hope that it will be useful,
10028+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10029+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10030+ * GNU General Public License for more details.
10031+ *
10032+ * You should have received a copy of the GNU General Public License
523b37e3 10033+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10034+ */
10035+
10036+/*
10037+ * directory operations
10038+ */
10039+
10040+#include <linux/fs_stack.h>
10041+#include "aufs.h"
10042+
10043+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10044+{
9dbd164d
AM
10045+ unsigned int nlink;
10046+
1facf9fc 10047+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10048+
9dbd164d
AM
10049+ nlink = dir->i_nlink;
10050+ nlink += h_dir->i_nlink - 2;
1facf9fc 10051+ if (h_dir->i_nlink < 2)
9dbd164d 10052+ nlink += 2;
f6b6e03d 10053+ smp_mb(); /* for i_nlink */
7eafdf33 10054+ /* 0 can happen in revaliding */
92d182d2 10055+ set_nlink(dir, nlink);
1facf9fc 10056+}
10057+
10058+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10059+{
9dbd164d
AM
10060+ unsigned int nlink;
10061+
1facf9fc 10062+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10063+
9dbd164d
AM
10064+ nlink = dir->i_nlink;
10065+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10066+ if (h_dir->i_nlink < 2)
9dbd164d 10067+ nlink -= 2;
f6b6e03d 10068+ smp_mb(); /* for i_nlink */
92d182d2 10069+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10070+ set_nlink(dir, nlink);
1facf9fc 10071+}
10072+
1308ab2a 10073+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10074+{
10075+ loff_t sz;
5afbbe0d 10076+ aufs_bindex_t bindex, bbot;
1308ab2a 10077+ struct file *h_file;
10078+ struct dentry *h_dentry;
10079+
10080+ sz = 0;
10081+ if (file) {
2000de60 10082+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10083+
5afbbe0d
AM
10084+ bbot = au_fbbot_dir(file);
10085+ for (bindex = au_fbtop(file);
10086+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10087+ bindex++) {
4a4d8108 10088+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10089+ if (h_file && file_inode(h_file))
10090+ sz += vfsub_f_size_read(h_file);
1308ab2a 10091+ }
10092+ } else {
10093+ AuDebugOn(!dentry);
2000de60 10094+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10095+
5afbbe0d
AM
10096+ bbot = au_dbtaildir(dentry);
10097+ for (bindex = au_dbtop(dentry);
10098+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10099+ bindex++) {
10100+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10101+ if (h_dentry && d_is_positive(h_dentry))
10102+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10103+ }
10104+ }
10105+ if (sz < KMALLOC_MAX_SIZE)
10106+ sz = roundup_pow_of_two(sz);
10107+ if (sz > KMALLOC_MAX_SIZE)
10108+ sz = KMALLOC_MAX_SIZE;
10109+ else if (sz < NAME_MAX) {
10110+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10111+ sz = AUFS_RDBLK_DEF;
10112+ }
10113+ return sz;
10114+}
10115+
b912730e
AM
10116+struct au_dir_ts_arg {
10117+ struct dentry *dentry;
10118+ aufs_bindex_t brid;
10119+};
10120+
10121+static void au_do_dir_ts(void *arg)
10122+{
10123+ struct au_dir_ts_arg *a = arg;
10124+ struct au_dtime dt;
10125+ struct path h_path;
10126+ struct inode *dir, *h_dir;
10127+ struct super_block *sb;
10128+ struct au_branch *br;
10129+ struct au_hinode *hdir;
10130+ int err;
5afbbe0d 10131+ aufs_bindex_t btop, bindex;
b912730e
AM
10132+
10133+ sb = a->dentry->d_sb;
5527c038 10134+ if (d_really_is_negative(a->dentry))
b912730e 10135+ goto out;
5527c038 10136+ /* no dir->i_mutex lock */
b95c5147
AM
10137+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10138+
5527c038 10139+ dir = d_inode(a->dentry);
5afbbe0d 10140+ btop = au_ibtop(dir);
b912730e 10141+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10142+ if (bindex < btop)
b912730e
AM
10143+ goto out_unlock;
10144+
10145+ br = au_sbr(sb, bindex);
10146+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10147+ if (!h_path.dentry)
10148+ goto out_unlock;
10149+ h_path.mnt = au_br_mnt(br);
10150+ au_dtime_store(&dt, a->dentry, &h_path);
10151+
5afbbe0d 10152+ br = au_sbr(sb, btop);
b912730e
AM
10153+ if (!au_br_writable(br->br_perm))
10154+ goto out_unlock;
5afbbe0d 10155+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10156+ h_path.mnt = au_br_mnt(br);
10157+ err = vfsub_mnt_want_write(h_path.mnt);
10158+ if (err)
10159+ goto out_unlock;
5afbbe0d
AM
10160+ hdir = au_hi(dir, btop);
10161+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10162+ h_dir = au_h_iptr(dir, btop);
b912730e
AM
10163+ if (h_dir->i_nlink
10164+ && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10165+ dt.dt_h_path = h_path;
10166+ au_dtime_revert(&dt);
10167+ }
5afbbe0d 10168+ au_hn_inode_unlock(hdir);
b912730e
AM
10169+ vfsub_mnt_drop_write(h_path.mnt);
10170+ au_cpup_attr_timesizes(dir);
10171+
10172+out_unlock:
10173+ aufs_read_unlock(a->dentry, AuLock_DW);
10174+out:
10175+ dput(a->dentry);
10176+ au_nwt_done(&au_sbi(sb)->si_nowait);
1c60b727 10177+ kfree(arg);
b912730e
AM
10178+}
10179+
10180+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10181+{
10182+ int perm, wkq_err;
5afbbe0d 10183+ aufs_bindex_t btop;
b912730e
AM
10184+ struct au_dir_ts_arg *arg;
10185+ struct dentry *dentry;
10186+ struct super_block *sb;
10187+
10188+ IMustLock(dir);
10189+
10190+ dentry = d_find_any_alias(dir);
10191+ AuDebugOn(!dentry);
10192+ sb = dentry->d_sb;
5afbbe0d
AM
10193+ btop = au_ibtop(dir);
10194+ if (btop == bindex) {
b912730e
AM
10195+ au_cpup_attr_timesizes(dir);
10196+ goto out;
10197+ }
10198+
5afbbe0d 10199+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10200+ if (!au_br_writable(perm))
10201+ goto out;
10202+
10203+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10204+ if (!arg)
10205+ goto out;
10206+
10207+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10208+ arg->brid = au_sbr_id(sb, bindex);
10209+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10210+ if (unlikely(wkq_err)) {
10211+ pr_err("wkq %d\n", wkq_err);
10212+ dput(dentry);
1c60b727 10213+ kfree(arg);
b912730e
AM
10214+ }
10215+
10216+out:
10217+ dput(dentry);
10218+}
10219+
1facf9fc 10220+/* ---------------------------------------------------------------------- */
10221+
10222+static int reopen_dir(struct file *file)
10223+{
10224+ int err;
10225+ unsigned int flags;
5afbbe0d 10226+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10227+ struct dentry *dentry, *h_dentry;
10228+ struct file *h_file;
10229+
10230+ /* open all lower dirs */
2000de60 10231+ dentry = file->f_path.dentry;
5afbbe0d
AM
10232+ btop = au_dbtop(dentry);
10233+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10234+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10235+ au_set_fbtop(file, btop);
1facf9fc 10236+
10237+ btail = au_dbtaildir(dentry);
5afbbe0d 10238+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10239+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10240+ au_set_fbbot_dir(file, btail);
1facf9fc 10241+
4a4d8108 10242+ flags = vfsub_file_flags(file);
5afbbe0d 10243+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10244+ h_dentry = au_h_dptr(dentry, bindex);
10245+ if (!h_dentry)
10246+ continue;
4a4d8108 10247+ h_file = au_hf_dir(file, bindex);
1facf9fc 10248+ if (h_file)
10249+ continue;
10250+
392086de 10251+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10252+ err = PTR_ERR(h_file);
10253+ if (IS_ERR(h_file))
10254+ goto out; /* close all? */
10255+ au_set_h_fptr(file, bindex, h_file);
10256+ }
10257+ au_update_figen(file);
10258+ /* todo: necessary? */
10259+ /* file->f_ra = h_file->f_ra; */
10260+ err = 0;
10261+
4f0767ce 10262+out:
1facf9fc 10263+ return err;
10264+}
10265+
b912730e 10266+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10267+{
10268+ int err;
10269+ aufs_bindex_t bindex, btail;
10270+ struct dentry *dentry, *h_dentry;
8cdd5066 10271+ struct vfsmount *mnt;
1facf9fc 10272+
1308ab2a 10273+ FiMustWriteLock(file);
b912730e 10274+ AuDebugOn(h_file);
1308ab2a 10275+
523b37e3 10276+ err = 0;
8cdd5066 10277+ mnt = file->f_path.mnt;
2000de60 10278+ dentry = file->f_path.dentry;
be118d29 10279+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10280+ bindex = au_dbtop(dentry);
10281+ au_set_fbtop(file, bindex);
1facf9fc 10282+ btail = au_dbtaildir(dentry);
5afbbe0d 10283+ au_set_fbbot_dir(file, btail);
1facf9fc 10284+ for (; !err && bindex <= btail; bindex++) {
10285+ h_dentry = au_h_dptr(dentry, bindex);
10286+ if (!h_dentry)
10287+ continue;
10288+
8cdd5066
JR
10289+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10290+ if (unlikely(err))
10291+ break;
392086de 10292+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10293+ if (IS_ERR(h_file)) {
10294+ err = PTR_ERR(h_file);
10295+ break;
10296+ }
10297+ au_set_h_fptr(file, bindex, h_file);
10298+ }
10299+ au_update_figen(file);
10300+ /* todo: necessary? */
10301+ /* file->f_ra = h_file->f_ra; */
10302+ if (!err)
10303+ return 0; /* success */
10304+
10305+ /* close all */
5afbbe0d 10306+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10307+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10308+ au_set_fbtop(file, -1);
10309+ au_set_fbbot_dir(file, -1);
4a4d8108 10310+
1facf9fc 10311+ return err;
10312+}
10313+
10314+static int aufs_open_dir(struct inode *inode __maybe_unused,
10315+ struct file *file)
10316+{
4a4d8108
AM
10317+ int err;
10318+ struct super_block *sb;
10319+ struct au_fidir *fidir;
10320+
10321+ err = -ENOMEM;
2000de60 10322+ sb = file->f_path.dentry->d_sb;
4a4d8108 10323+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10324+ fidir = au_fidir_alloc(sb);
4a4d8108 10325+ if (fidir) {
b912730e
AM
10326+ struct au_do_open_args args = {
10327+ .open = do_open_dir,
10328+ .fidir = fidir
10329+ };
10330+ err = au_do_open(file, &args);
4a4d8108 10331+ if (unlikely(err))
1c60b727 10332+ kfree(fidir);
4a4d8108
AM
10333+ }
10334+ si_read_unlock(sb);
10335+ return err;
1facf9fc 10336+}
10337+
10338+static int aufs_release_dir(struct inode *inode __maybe_unused,
10339+ struct file *file)
10340+{
10341+ struct au_vdir *vdir_cache;
4a4d8108
AM
10342+ struct au_finfo *finfo;
10343+ struct au_fidir *fidir;
f0c0a007 10344+ struct au_hfile *hf;
5afbbe0d 10345+ aufs_bindex_t bindex, bbot;
1facf9fc 10346+
4a4d8108
AM
10347+ finfo = au_fi(file);
10348+ fidir = finfo->fi_hdir;
10349+ if (fidir) {
8b6a4947
AM
10350+ au_hbl_del(&finfo->fi_hlist,
10351+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10352+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10353+ if (vdir_cache)
1c60b727 10354+ au_vdir_free(vdir_cache);
4a4d8108
AM
10355+
10356+ bindex = finfo->fi_btop;
10357+ if (bindex >= 0) {
f0c0a007 10358+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10359+ /*
10360+ * calls fput() instead of filp_close(),
10361+ * since no dnotify or lock for the lower file.
10362+ */
5afbbe0d 10363+ bbot = fidir->fd_bbot;
f0c0a007
AM
10364+ for (; bindex <= bbot; bindex++, hf++)
10365+ if (hf->hf_file)
1c60b727 10366+ au_hfput(hf, /*execed*/0);
4a4d8108 10367+ }
1c60b727 10368+ kfree(fidir);
4a4d8108 10369+ finfo->fi_hdir = NULL;
1facf9fc 10370+ }
1c60b727 10371+ au_finfo_fin(file);
1facf9fc 10372+ return 0;
10373+}
10374+
10375+/* ---------------------------------------------------------------------- */
10376+
4a4d8108
AM
10377+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10378+{
10379+ int err;
5afbbe0d 10380+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10381+ struct file *h_file;
10382+
10383+ err = 0;
5afbbe0d
AM
10384+ bbot = au_fbbot_dir(file);
10385+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10386+ h_file = au_hf_dir(file, bindex);
10387+ if (h_file)
10388+ err = vfsub_flush(h_file, id);
10389+ }
10390+ return err;
10391+}
10392+
10393+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10394+{
10395+ return au_do_flush(file, id, au_do_flush_dir);
10396+}
10397+
10398+/* ---------------------------------------------------------------------- */
10399+
1facf9fc 10400+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10401+{
10402+ int err;
5afbbe0d 10403+ aufs_bindex_t bbot, bindex;
1facf9fc 10404+ struct inode *inode;
10405+ struct super_block *sb;
10406+
10407+ err = 0;
10408+ sb = dentry->d_sb;
5527c038 10409+ inode = d_inode(dentry);
1facf9fc 10410+ IMustLock(inode);
5afbbe0d
AM
10411+ bbot = au_dbbot(dentry);
10412+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10413+ struct path h_path;
1facf9fc 10414+
10415+ if (au_test_ro(sb, bindex, inode))
10416+ continue;
10417+ h_path.dentry = au_h_dptr(dentry, bindex);
10418+ if (!h_path.dentry)
10419+ continue;
1facf9fc 10420+
1facf9fc 10421+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10422+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10423+ }
10424+
10425+ return err;
10426+}
10427+
10428+static int au_do_fsync_dir(struct file *file, int datasync)
10429+{
10430+ int err;
5afbbe0d 10431+ aufs_bindex_t bbot, bindex;
1facf9fc 10432+ struct file *h_file;
10433+ struct super_block *sb;
10434+ struct inode *inode;
1facf9fc 10435+
521ced18 10436+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10437+ if (unlikely(err))
10438+ goto out;
10439+
c06a8ce3 10440+ inode = file_inode(file);
b912730e 10441+ sb = inode->i_sb;
5afbbe0d
AM
10442+ bbot = au_fbbot_dir(file);
10443+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10444+ h_file = au_hf_dir(file, bindex);
1facf9fc 10445+ if (!h_file || au_test_ro(sb, bindex, inode))
10446+ continue;
10447+
53392da6 10448+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10449+ }
10450+
4f0767ce 10451+out:
1facf9fc 10452+ return err;
10453+}
10454+
10455+/*
10456+ * @file may be NULL
10457+ */
1e00d052
AM
10458+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10459+ int datasync)
1facf9fc 10460+{
10461+ int err;
b752ccd1 10462+ struct dentry *dentry;
5527c038 10463+ struct inode *inode;
1facf9fc 10464+ struct super_block *sb;
1facf9fc 10465+
10466+ err = 0;
2000de60 10467+ dentry = file->f_path.dentry;
5527c038 10468+ inode = d_inode(dentry);
febd17d6 10469+ inode_lock(inode);
1facf9fc 10470+ sb = dentry->d_sb;
10471+ si_noflush_read_lock(sb);
10472+ if (file)
10473+ err = au_do_fsync_dir(file, datasync);
10474+ else {
10475+ di_write_lock_child(dentry);
10476+ err = au_do_fsync_dir_no_file(dentry, datasync);
10477+ }
5527c038 10478+ au_cpup_attr_timesizes(inode);
1facf9fc 10479+ di_write_unlock(dentry);
10480+ if (file)
10481+ fi_write_unlock(file);
10482+
10483+ si_read_unlock(sb);
febd17d6 10484+ inode_unlock(inode);
1facf9fc 10485+ return err;
10486+}
10487+
10488+/* ---------------------------------------------------------------------- */
10489+
5afbbe0d 10490+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10491+{
10492+ int err;
10493+ struct dentry *dentry;
9dbd164d 10494+ struct inode *inode, *h_inode;
1facf9fc 10495+ struct super_block *sb;
10496+
523b37e3 10497+ AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10498+
2000de60 10499+ dentry = file->f_path.dentry;
5527c038 10500+ inode = d_inode(dentry);
1facf9fc 10501+ IMustLock(inode);
10502+
10503+ sb = dentry->d_sb;
10504+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10505+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10506+ if (unlikely(err))
10507+ goto out;
027c5e7a
AM
10508+ err = au_alive_dir(dentry);
10509+ if (!err)
10510+ err = au_vdir_init(file);
1facf9fc 10511+ di_downgrade_lock(dentry, AuLock_IR);
10512+ if (unlikely(err))
10513+ goto out_unlock;
10514+
5afbbe0d 10515+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10516+ if (!au_test_nfsd()) {
392086de 10517+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10518+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10519+ } else {
10520+ /*
10521+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10522+ * encode_fh() and others.
10523+ */
9dbd164d 10524+ atomic_inc(&h_inode->i_count);
1facf9fc 10525+ di_read_unlock(dentry, AuLock_IR);
10526+ si_read_unlock(sb);
392086de 10527+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10528+ fsstack_copy_attr_atime(inode, h_inode);
10529+ fi_write_unlock(file);
9dbd164d 10530+ iput(h_inode);
1facf9fc 10531+
10532+ AuTraceErr(err);
10533+ return err;
10534+ }
10535+
4f0767ce 10536+out_unlock:
1facf9fc 10537+ di_read_unlock(dentry, AuLock_IR);
10538+ fi_write_unlock(file);
4f0767ce 10539+out:
1facf9fc 10540+ si_read_unlock(sb);
10541+ return err;
10542+}
10543+
10544+/* ---------------------------------------------------------------------- */
10545+
10546+#define AuTestEmpty_WHONLY 1
dece6358
AM
10547+#define AuTestEmpty_CALLED (1 << 1)
10548+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10549+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10550+#define au_fset_testempty(flags, name) \
10551+ do { (flags) |= AuTestEmpty_##name; } while (0)
10552+#define au_fclr_testempty(flags, name) \
10553+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10554+
dece6358
AM
10555+#ifndef CONFIG_AUFS_SHWH
10556+#undef AuTestEmpty_SHWH
10557+#define AuTestEmpty_SHWH 0
10558+#endif
10559+
1facf9fc 10560+struct test_empty_arg {
392086de 10561+ struct dir_context ctx;
1308ab2a 10562+ struct au_nhash *whlist;
1facf9fc 10563+ unsigned int flags;
10564+ int err;
10565+ aufs_bindex_t bindex;
10566+};
10567+
392086de
AM
10568+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10569+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10570+ unsigned int d_type)
1facf9fc 10571+{
392086de
AM
10572+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10573+ ctx);
1facf9fc 10574+ char *name = (void *)__name;
10575+
10576+ arg->err = 0;
10577+ au_fset_testempty(arg->flags, CALLED);
10578+ /* smp_mb(); */
10579+ if (name[0] == '.'
10580+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10581+ goto out; /* success */
10582+
10583+ if (namelen <= AUFS_WH_PFX_LEN
10584+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10585+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10586+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10587+ arg->err = -ENOTEMPTY;
10588+ goto out;
10589+ }
10590+
10591+ name += AUFS_WH_PFX_LEN;
10592+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10593+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10594+ arg->err = au_nhash_append_wh
1308ab2a 10595+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10596+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10597+
4f0767ce 10598+out:
1facf9fc 10599+ /* smp_mb(); */
10600+ AuTraceErr(arg->err);
10601+ return arg->err;
10602+}
10603+
10604+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10605+{
10606+ int err;
10607+ struct file *h_file;
10608+
10609+ h_file = au_h_open(dentry, arg->bindex,
10610+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10611+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10612+ err = PTR_ERR(h_file);
10613+ if (IS_ERR(h_file))
10614+ goto out;
10615+
10616+ err = 0;
10617+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10618+ && !file_inode(h_file)->i_nlink)
1facf9fc 10619+ goto out_put;
10620+
10621+ do {
10622+ arg->err = 0;
10623+ au_fclr_testempty(arg->flags, CALLED);
10624+ /* smp_mb(); */
392086de 10625+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10626+ if (err >= 0)
10627+ err = arg->err;
10628+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10629+
4f0767ce 10630+out_put:
1facf9fc 10631+ fput(h_file);
10632+ au_sbr_put(dentry->d_sb, arg->bindex);
4f0767ce 10633+out:
1facf9fc 10634+ return err;
10635+}
10636+
10637+struct do_test_empty_args {
10638+ int *errp;
10639+ struct dentry *dentry;
10640+ struct test_empty_arg *arg;
10641+};
10642+
10643+static void call_do_test_empty(void *args)
10644+{
10645+ struct do_test_empty_args *a = args;
10646+ *a->errp = do_test_empty(a->dentry, a->arg);
10647+}
10648+
10649+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10650+{
10651+ int err, wkq_err;
10652+ struct dentry *h_dentry;
10653+ struct inode *h_inode;
10654+
10655+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10656+ h_inode = d_inode(h_dentry);
53392da6 10657+ /* todo: i_mode changes anytime? */
be118d29 10658+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10659+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10660+ inode_unlock_shared(h_inode);
1facf9fc 10661+ if (!err)
10662+ err = do_test_empty(dentry, arg);
10663+ else {
10664+ struct do_test_empty_args args = {
10665+ .errp = &err,
10666+ .dentry = dentry,
10667+ .arg = arg
10668+ };
10669+ unsigned int flags = arg->flags;
10670+
10671+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10672+ if (unlikely(wkq_err))
10673+ err = wkq_err;
10674+ arg->flags = flags;
10675+ }
10676+
10677+ return err;
10678+}
10679+
10680+int au_test_empty_lower(struct dentry *dentry)
10681+{
10682+ int err;
1308ab2a 10683+ unsigned int rdhash;
5afbbe0d 10684+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10685+ struct au_nhash whlist;
392086de
AM
10686+ struct test_empty_arg arg = {
10687+ .ctx = {
2000de60 10688+ .actor = test_empty_cb
392086de
AM
10689+ }
10690+ };
076b876e 10691+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10692+
dece6358
AM
10693+ SiMustAnyLock(dentry->d_sb);
10694+
1308ab2a 10695+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10696+ if (!rdhash)
10697+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10698+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10699+ if (unlikely(err))
1facf9fc 10700+ goto out;
10701+
1facf9fc 10702+ arg.flags = 0;
1308ab2a 10703+ arg.whlist = &whlist;
5afbbe0d 10704+ btop = au_dbtop(dentry);
dece6358
AM
10705+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10706+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10707+ test_empty = do_test_empty;
10708+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10709+ test_empty = sio_test_empty;
5afbbe0d 10710+ arg.bindex = btop;
076b876e 10711+ err = test_empty(dentry, &arg);
1facf9fc 10712+ if (unlikely(err))
10713+ goto out_whlist;
10714+
10715+ au_fset_testempty(arg.flags, WHONLY);
10716+ btail = au_dbtaildir(dentry);
5afbbe0d 10717+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10718+ struct dentry *h_dentry;
10719+
10720+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10721+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10722+ arg.bindex = bindex;
076b876e 10723+ err = test_empty(dentry, &arg);
1facf9fc 10724+ }
10725+ }
10726+
4f0767ce 10727+out_whlist:
1308ab2a 10728+ au_nhash_wh_free(&whlist);
4f0767ce 10729+out:
1facf9fc 10730+ return err;
10731+}
10732+
10733+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10734+{
10735+ int err;
392086de
AM
10736+ struct test_empty_arg arg = {
10737+ .ctx = {
2000de60 10738+ .actor = test_empty_cb
392086de
AM
10739+ }
10740+ };
1facf9fc 10741+ aufs_bindex_t bindex, btail;
10742+
10743+ err = 0;
1308ab2a 10744+ arg.whlist = whlist;
1facf9fc 10745+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10746+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10747+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10748+ btail = au_dbtaildir(dentry);
5afbbe0d 10749+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10750+ struct dentry *h_dentry;
10751+
10752+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10753+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10754+ arg.bindex = bindex;
10755+ err = sio_test_empty(dentry, &arg);
10756+ }
10757+ }
10758+
10759+ return err;
10760+}
10761+
10762+/* ---------------------------------------------------------------------- */
10763+
10764+const struct file_operations aufs_dir_fop = {
4a4d8108 10765+ .owner = THIS_MODULE,
027c5e7a 10766+ .llseek = default_llseek,
1facf9fc 10767+ .read = generic_read_dir,
5afbbe0d 10768+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10769+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10770+#ifdef CONFIG_COMPAT
10771+ .compat_ioctl = aufs_compat_ioctl_dir,
10772+#endif
1facf9fc 10773+ .open = aufs_open_dir,
10774+ .release = aufs_release_dir,
4a4d8108 10775+ .flush = aufs_flush_dir,
1facf9fc 10776+ .fsync = aufs_fsync_dir
10777+};
7f207e10
AM
10778diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10779--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 10780+++ linux/fs/aufs/dir.h 2018-06-04 09:08:09.184746078 +0200
1c60b727 10781@@ -0,0 +1,131 @@
1facf9fc 10782+/*
b00004a5 10783+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 10784+ *
10785+ * This program, aufs is free software; you can redistribute it and/or modify
10786+ * it under the terms of the GNU General Public License as published by
10787+ * the Free Software Foundation; either version 2 of the License, or
10788+ * (at your option) any later version.
dece6358
AM
10789+ *
10790+ * This program is distributed in the hope that it will be useful,
10791+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10792+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10793+ * GNU General Public License for more details.
10794+ *
10795+ * You should have received a copy of the GNU General Public License
523b37e3 10796+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10797+ */
10798+
10799+/*
10800+ * directory operations
10801+ */
10802+
10803+#ifndef __AUFS_DIR_H__
10804+#define __AUFS_DIR_H__
10805+
10806+#ifdef __KERNEL__
10807+
10808+#include <linux/fs.h>
1facf9fc 10809+
10810+/* ---------------------------------------------------------------------- */
10811+
10812+/* need to be faster and smaller */
10813+
10814+struct au_nhash {
dece6358
AM
10815+ unsigned int nh_num;
10816+ struct hlist_head *nh_head;
1facf9fc 10817+};
10818+
10819+struct au_vdir_destr {
10820+ unsigned char len;
10821+ unsigned char name[0];
10822+} __packed;
10823+
10824+struct au_vdir_dehstr {
10825+ struct hlist_node hash;
1c60b727 10826+ struct au_vdir_destr *str;
4a4d8108 10827+} ____cacheline_aligned_in_smp;
1facf9fc 10828+
10829+struct au_vdir_de {
10830+ ino_t de_ino;
10831+ unsigned char de_type;
10832+ /* caution: packed */
10833+ struct au_vdir_destr de_str;
10834+} __packed;
10835+
10836+struct au_vdir_wh {
10837+ struct hlist_node wh_hash;
dece6358
AM
10838+#ifdef CONFIG_AUFS_SHWH
10839+ ino_t wh_ino;
1facf9fc 10840+ aufs_bindex_t wh_bindex;
dece6358
AM
10841+ unsigned char wh_type;
10842+#else
10843+ aufs_bindex_t wh_bindex;
10844+#endif
10845+ /* caution: packed */
1facf9fc 10846+ struct au_vdir_destr wh_str;
10847+} __packed;
10848+
10849+union au_vdir_deblk_p {
10850+ unsigned char *deblk;
10851+ struct au_vdir_de *de;
10852+};
10853+
10854+struct au_vdir {
10855+ unsigned char **vd_deblk;
10856+ unsigned long vd_nblk;
1facf9fc 10857+ struct {
10858+ unsigned long ul;
10859+ union au_vdir_deblk_p p;
10860+ } vd_last;
10861+
be118d29 10862+ u64 vd_version;
dece6358 10863+ unsigned int vd_deblk_sz;
1c60b727 10864+ unsigned long vd_jiffy;
4a4d8108 10865+} ____cacheline_aligned_in_smp;
1facf9fc 10866+
10867+/* ---------------------------------------------------------------------- */
10868+
10869+/* dir.c */
10870+extern const struct file_operations aufs_dir_fop;
10871+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10872+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10873+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10874+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10875+int au_test_empty_lower(struct dentry *dentry);
10876+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10877+
10878+/* vdir.c */
1308ab2a 10879+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10880+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10881+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10882+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10883+ int limit);
dece6358
AM
10884+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10885+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10886+ unsigned int d_type, aufs_bindex_t bindex,
10887+ unsigned char shwh);
1c60b727 10888+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 10889+int au_vdir_init(struct file *file);
392086de 10890+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10891+
10892+/* ioctl.c */
10893+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10894+
1308ab2a 10895+#ifdef CONFIG_AUFS_RDU
10896+/* rdu.c */
10897+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10898+#ifdef CONFIG_COMPAT
10899+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10900+ unsigned long arg);
10901+#endif
1308ab2a 10902+#else
c1595e42
JR
10903+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10904+ unsigned int cmd, unsigned long arg)
b752ccd1 10905+#ifdef CONFIG_COMPAT
c1595e42
JR
10906+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10907+ unsigned int cmd, unsigned long arg)
b752ccd1 10908+#endif
1308ab2a 10909+#endif
10910+
1facf9fc 10911+#endif /* __KERNEL__ */
10912+#endif /* __AUFS_DIR_H__ */
8b6a4947
AM
10913diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10914--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 10915+++ linux/fs/aufs/dirren.c 2018-06-04 09:08:09.184746078 +0200
8b6a4947
AM
10916@@ -0,0 +1,1315 @@
10917+/*
b00004a5 10918+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
10919+ *
10920+ * This program, aufs is free software; you can redistribute it and/or modify
10921+ * it under the terms of the GNU General Public License as published by
10922+ * the Free Software Foundation; either version 2 of the License, or
10923+ * (at your option) any later version.
10924+ *
10925+ * This program is distributed in the hope that it will be useful,
10926+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10927+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10928+ * GNU General Public License for more details.
10929+ *
10930+ * You should have received a copy of the GNU General Public License
10931+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10932+ */
10933+
10934+/*
10935+ * special handling in renaming a directoy
10936+ * in order to support looking-up the before-renamed name on the lower readonly
10937+ * branches
10938+ */
10939+
10940+#include <linux/byteorder/generic.h>
10941+#include "aufs.h"
10942+
10943+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10944+{
10945+ int idx;
10946+
10947+ idx = au_dr_ihash(ent->dr_h_ino);
10948+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10949+}
10950+
10951+static int au_dr_hino_test_empty(struct au_dr_br *dr)
10952+{
10953+ int ret, i;
10954+ struct hlist_bl_head *hbl;
10955+
10956+ ret = 1;
10957+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
10958+ hbl = dr->dr_h_ino + i;
10959+ hlist_bl_lock(hbl);
10960+ ret &= hlist_bl_empty(hbl);
10961+ hlist_bl_unlock(hbl);
10962+ }
10963+
10964+ return ret;
10965+}
10966+
10967+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10968+{
10969+ struct au_dr_hino *found, *ent;
10970+ struct hlist_bl_head *hbl;
10971+ struct hlist_bl_node *pos;
10972+ int idx;
10973+
10974+ found = NULL;
10975+ idx = au_dr_ihash(ino);
10976+ hbl = dr->dr_h_ino + idx;
10977+ hlist_bl_lock(hbl);
10978+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10979+ if (ent->dr_h_ino == ino) {
10980+ found = ent;
10981+ break;
10982+ }
10983+ hlist_bl_unlock(hbl);
10984+
10985+ return found;
10986+}
10987+
10988+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10989+ struct au_dr_hino *add_ent)
10990+{
10991+ int found, idx;
10992+ struct hlist_bl_head *hbl;
10993+ struct hlist_bl_node *pos;
10994+ struct au_dr_hino *ent;
10995+
10996+ found = 0;
10997+ idx = au_dr_ihash(ino);
10998+ hbl = dr->dr_h_ino + idx;
10999+#if 0
11000+ {
11001+ struct hlist_bl_node *tmp;
11002+
11003+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11004+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11005+ }
11006+#endif
11007+ hlist_bl_lock(hbl);
11008+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11009+ if (ent->dr_h_ino == ino) {
11010+ found = 1;
11011+ break;
11012+ }
11013+ if (!found && add_ent)
11014+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11015+ hlist_bl_unlock(hbl);
11016+
11017+ if (!found && add_ent)
11018+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11019+
11020+ return found;
11021+}
11022+
11023+void au_dr_hino_free(struct au_dr_br *dr)
11024+{
11025+ int i;
11026+ struct hlist_bl_head *hbl;
11027+ struct hlist_bl_node *pos, *tmp;
11028+ struct au_dr_hino *ent;
11029+
11030+ /* SiMustWriteLock(sb); */
11031+
11032+ for (i = 0; i < AuDirren_NHASH; i++) {
11033+ hbl = dr->dr_h_ino + i;
11034+ /* no spinlock since sbinfo must be write-locked */
11035+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11036+ kfree(ent);
11037+ INIT_HLIST_BL_HEAD(hbl);
11038+ }
11039+}
11040+
11041+/* returns the number of inodes or an error */
11042+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11043+ struct file *hinofile)
11044+{
11045+ int err, i;
11046+ ssize_t ssz;
11047+ loff_t pos, oldsize;
11048+ __be64 u64;
11049+ struct inode *hinoinode;
11050+ struct hlist_bl_head *hbl;
11051+ struct hlist_bl_node *n1, *n2;
11052+ struct au_dr_hino *ent;
11053+
11054+ SiMustWriteLock(sb);
11055+ AuDebugOn(!au_br_writable(br->br_perm));
11056+
11057+ hinoinode = file_inode(hinofile);
11058+ oldsize = i_size_read(hinoinode);
11059+
11060+ err = 0;
11061+ pos = 0;
11062+ hbl = br->br_dirren.dr_h_ino;
11063+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11064+ /* no bit-lock since sbinfo must be write-locked */
11065+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11066+ AuDbg("hi%llu, %pD2\n",
11067+ (unsigned long long)ent->dr_h_ino, hinofile);
11068+ u64 = cpu_to_be64(ent->dr_h_ino);
11069+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11070+ if (ssz == sizeof(u64))
11071+ continue;
11072+
11073+ /* write error */
11074+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11075+ err = -ENOSPC;
11076+ if (ssz < 0)
11077+ err = ssz;
11078+ break;
11079+ }
11080+ }
11081+ /* regardless the error */
11082+ if (pos < oldsize) {
11083+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11084+ AuTraceErr(err);
11085+ }
11086+
11087+ AuTraceErr(err);
11088+ return err;
11089+}
11090+
11091+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11092+{
11093+ int err, hidx;
11094+ ssize_t ssz;
11095+ size_t sz, n;
11096+ loff_t pos;
11097+ uint64_t u64;
11098+ struct au_dr_hino *ent;
11099+ struct inode *hinoinode;
11100+ struct hlist_bl_head *hbl;
11101+
11102+ err = 0;
11103+ pos = 0;
11104+ hbl = dr->dr_h_ino;
11105+ hinoinode = file_inode(hinofile);
11106+ sz = i_size_read(hinoinode);
11107+ AuDebugOn(sz % sizeof(u64));
11108+ n = sz / sizeof(u64);
11109+ while (n--) {
11110+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11111+ if (unlikely(ssz != sizeof(u64))) {
11112+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11113+ err = -EINVAL;
11114+ if (ssz < 0)
11115+ err = ssz;
11116+ goto out_free;
11117+ }
11118+
11119+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11120+ if (!ent) {
11121+ err = -ENOMEM;
11122+ AuTraceErr(err);
11123+ goto out_free;
11124+ }
11125+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11126+ AuDbg("hi%llu, %pD2\n",
11127+ (unsigned long long)ent->dr_h_ino, hinofile);
11128+ hidx = au_dr_ihash(ent->dr_h_ino);
11129+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11130+ }
11131+ goto out; /* success */
11132+
11133+out_free:
11134+ au_dr_hino_free(dr);
11135+out:
11136+ AuTraceErr(err);
11137+ return err;
11138+}
11139+
11140+/*
11141+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11142+ * @path is a switch to distinguish load and store.
11143+ */
11144+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11145+ struct au_branch *br, const struct path *path)
11146+{
11147+ int err, flags;
11148+ unsigned char load, suspend;
11149+ struct file *hinofile;
11150+ struct au_hinode *hdir;
11151+ struct inode *dir, *delegated;
11152+ struct path hinopath;
11153+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11154+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11155+
11156+ AuDebugOn(bindex < 0 && !br);
11157+ AuDebugOn(bindex >= 0 && br);
11158+
11159+ err = -EINVAL;
11160+ suspend = !br;
11161+ if (suspend)
11162+ br = au_sbr(sb, bindex);
11163+ load = !!path;
11164+ if (!load) {
11165+ path = &br->br_path;
11166+ AuDebugOn(!au_br_writable(br->br_perm));
11167+ if (unlikely(!au_br_writable(br->br_perm)))
11168+ goto out;
11169+ }
11170+
11171+ hdir = NULL;
11172+ if (suspend) {
11173+ dir = d_inode(sb->s_root);
11174+ hdir = au_hinode(au_ii(dir), bindex);
11175+ dir = hdir->hi_inode;
11176+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11177+ } else {
11178+ dir = d_inode(path->dentry);
11179+ inode_lock_nested(dir, AuLsc_I_CHILD);
11180+ }
11181+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11182+ err = PTR_ERR(hinopath.dentry);
11183+ if (IS_ERR(hinopath.dentry))
11184+ goto out_unlock;
11185+
11186+ err = 0;
11187+ flags = O_RDONLY;
11188+ if (load) {
11189+ if (d_is_negative(hinopath.dentry))
11190+ goto out_dput; /* success */
11191+ } else {
11192+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11193+ if (d_is_positive(hinopath.dentry)) {
11194+ delegated = NULL;
11195+ err = vfsub_unlink(dir, &hinopath, &delegated,
11196+ /*force*/0);
11197+ AuTraceErr(err);
11198+ if (unlikely(err))
11199+ pr_err("ignored err %d, %pd2\n",
11200+ err, hinopath.dentry);
11201+ if (unlikely(err == -EWOULDBLOCK))
11202+ iput(delegated);
11203+ err = 0;
11204+ }
11205+ goto out_dput;
11206+ } else if (!d_is_positive(hinopath.dentry)) {
11207+ err = vfsub_create(dir, &hinopath, 0600,
11208+ /*want_excl*/false);
11209+ AuTraceErr(err);
11210+ if (unlikely(err))
11211+ goto out_dput;
11212+ }
11213+ flags = O_WRONLY;
11214+ }
11215+ hinopath.mnt = path->mnt;
11216+ hinofile = vfsub_dentry_open(&hinopath, flags);
11217+ if (suspend)
11218+ au_hn_inode_unlock(hdir);
11219+ else
11220+ inode_unlock(dir);
11221+ dput(hinopath.dentry);
11222+ AuTraceErrPtr(hinofile);
11223+ if (IS_ERR(hinofile)) {
11224+ err = PTR_ERR(hinofile);
11225+ goto out;
11226+ }
11227+
11228+ if (load)
11229+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11230+ else
11231+ err = au_dr_hino_store(sb, br, hinofile);
11232+ fput(hinofile);
11233+ goto out;
11234+
11235+out_dput:
11236+ dput(hinopath.dentry);
11237+out_unlock:
11238+ if (suspend)
11239+ au_hn_inode_unlock(hdir);
11240+ else
11241+ inode_unlock(dir);
11242+out:
11243+ AuTraceErr(err);
11244+ return err;
11245+}
11246+
11247+/* ---------------------------------------------------------------------- */
11248+
11249+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11250+{
11251+ int err;
11252+ struct kstatfs kstfs;
11253+ dev_t dev;
11254+ struct dentry *dentry;
11255+ struct super_block *sb;
11256+
11257+ err = vfs_statfs((void *)path, &kstfs);
11258+ AuTraceErr(err);
11259+ if (unlikely(err))
11260+ goto out;
11261+
11262+ /* todo: support for UUID */
11263+
11264+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11265+ brid->type = AuBrid_FSID;
11266+ brid->fsid = kstfs.f_fsid;
11267+ } else {
11268+ dentry = path->dentry;
11269+ sb = dentry->d_sb;
11270+ dev = sb->s_dev;
11271+ if (dev) {
11272+ brid->type = AuBrid_DEV;
11273+ brid->dev = dev;
11274+ }
11275+ }
11276+
11277+out:
11278+ return err;
11279+}
11280+
11281+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11282+ const struct path *path)
11283+{
11284+ int err, i;
11285+ struct au_dr_br *dr;
11286+ struct hlist_bl_head *hbl;
11287+
11288+ dr = &br->br_dirren;
11289+ hbl = dr->dr_h_ino;
11290+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11291+ INIT_HLIST_BL_HEAD(hbl);
11292+
11293+ err = au_dr_brid_init(&dr->dr_brid, path);
11294+ if (unlikely(err))
11295+ goto out;
11296+
11297+ if (au_opt_test(au_mntflags(sb), DIRREN))
11298+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11299+
11300+out:
11301+ AuTraceErr(err);
11302+ return err;
11303+}
11304+
11305+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11306+{
11307+ int err;
11308+
11309+ err = 0;
11310+ if (au_br_writable(br->br_perm))
11311+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11312+ if (!err)
11313+ au_dr_hino_free(&br->br_dirren);
11314+
11315+ return err;
11316+}
11317+
11318+/* ---------------------------------------------------------------------- */
11319+
11320+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11321+ char *buf, size_t sz)
11322+{
11323+ int err;
11324+ unsigned int major, minor;
11325+ char *p;
11326+
11327+ p = buf;
11328+ err = snprintf(p, sz, "%d_", brid->type);
11329+ AuDebugOn(err > sz);
11330+ p += err;
11331+ sz -= err;
11332+ switch (brid->type) {
11333+ case AuBrid_Unset:
11334+ return -EINVAL;
11335+ case AuBrid_UUID:
11336+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11337+ break;
11338+ case AuBrid_FSID:
11339+ err = snprintf(p, sz, "%08x-%08x",
11340+ brid->fsid.val[0], brid->fsid.val[1]);
11341+ break;
11342+ case AuBrid_DEV:
11343+ major = MAJOR(brid->dev);
11344+ minor = MINOR(brid->dev);
11345+ if (major <= 0xff && minor <= 0xff)
11346+ err = snprintf(p, sz, "%02x%02x", major, minor);
11347+ else
11348+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11349+ break;
11350+ }
11351+ AuDebugOn(err > sz);
11352+ p += err;
11353+ sz -= err;
11354+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11355+ AuDebugOn(err > sz);
11356+ p += err;
11357+ sz -= err;
11358+
11359+ return p - buf;
11360+}
11361+
11362+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11363+{
11364+ int rlen;
11365+ struct dentry *br_dentry;
11366+ struct inode *br_inode;
11367+
11368+ br_dentry = au_br_dentry(br);
11369+ br_inode = d_inode(br_dentry);
11370+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11371+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11372+ AuDebugOn(rlen > len);
11373+
11374+ return rlen;
11375+}
11376+
11377+/* ---------------------------------------------------------------------- */
11378+
11379+/*
11380+ * from the given @h_dentry, construct drinfo at @*fdata.
11381+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11382+ * @allocated.
11383+ */
11384+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11385+ struct dentry *h_dentry,
11386+ unsigned char *allocated)
11387+{
11388+ int err, v;
11389+ struct au_drinfo_fdata *f, *p;
11390+ struct au_drinfo *drinfo;
11391+ struct inode *h_inode;
11392+ struct qstr *qname;
11393+
11394+ err = 0;
11395+ f = *fdata;
11396+ h_inode = d_inode(h_dentry);
11397+ qname = &h_dentry->d_name;
11398+ drinfo = &f->drinfo;
11399+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11400+ drinfo->oldnamelen = qname->len;
11401+ if (*allocated < sizeof(*f) + qname->len) {
11402+ v = roundup_pow_of_two(*allocated + qname->len);
11403+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11404+ if (unlikely(!p)) {
11405+ err = -ENOMEM;
11406+ AuTraceErr(err);
11407+ goto out;
11408+ }
11409+ f = p;
11410+ *fdata = f;
11411+ *allocated = v;
11412+ drinfo = &f->drinfo;
11413+ }
11414+ memcpy(drinfo->oldname, qname->name, qname->len);
11415+ AuDbg("i%llu, %.*s\n",
11416+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11417+ drinfo->oldname);
11418+
11419+out:
11420+ AuTraceErr(err);
11421+ return err;
11422+}
11423+
11424+/* callers have to free the return value */
11425+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11426+{
11427+ struct au_drinfo *ret, *drinfo;
11428+ struct au_drinfo_fdata fdata;
11429+ int len;
11430+ loff_t pos;
11431+ ssize_t ssz;
11432+
11433+ ret = ERR_PTR(-EIO);
11434+ pos = 0;
11435+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11436+ if (unlikely(ssz != sizeof(fdata))) {
11437+ AuIOErr("ssz %zd, %u, %pD2\n",
11438+ ssz, (unsigned int)sizeof(fdata), file);
11439+ goto out;
11440+ }
11441+
11442+ fdata.magic = ntohl((__force __be32)fdata.magic);
11443+ switch (fdata.magic) {
11444+ case AUFS_DRINFO_MAGIC_V1:
11445+ break;
11446+ default:
11447+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11448+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11449+ goto out;
11450+ }
11451+
11452+ drinfo = &fdata.drinfo;
11453+ len = drinfo->oldnamelen;
11454+ if (!len) {
11455+ AuIOErr("broken drinfo %pD2\n", file);
11456+ goto out;
11457+ }
11458+
11459+ ret = NULL;
11460+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11461+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11462+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11463+ (unsigned long long)drinfo->ino,
11464+ (unsigned long long)h_ino, file);
11465+ goto out; /* success */
11466+ }
11467+
11468+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11469+ if (unlikely(!ret)) {
11470+ ret = ERR_PTR(-ENOMEM);
11471+ AuTraceErrPtr(ret);
11472+ goto out;
11473+ }
11474+
11475+ *ret = *drinfo;
11476+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11477+ if (unlikely(ssz != len)) {
11478+ kfree(ret);
11479+ ret = ERR_PTR(-EIO);
11480+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11481+ goto out;
11482+ }
11483+
11484+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11485+
11486+out:
11487+ return ret;
11488+}
11489+
11490+/* ---------------------------------------------------------------------- */
11491+
11492+/* in order to be revertible */
11493+struct au_drinfo_rev_elm {
11494+ int created;
11495+ struct dentry *info_dentry;
11496+ struct au_drinfo *info_last;
11497+};
11498+
11499+struct au_drinfo_rev {
11500+ unsigned char already;
11501+ aufs_bindex_t nelm;
11502+ struct au_drinfo_rev_elm elm[0];
11503+};
11504+
11505+/* todo: isn't it too large? */
11506+struct au_drinfo_store {
11507+ struct path h_ppath;
11508+ struct dentry *h_dentry;
11509+ struct au_drinfo_fdata *fdata;
11510+ char *infoname; /* inside of whname, just after PFX */
11511+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11512+ aufs_bindex_t btgt, btail;
11513+ unsigned char no_sio,
11514+ allocated, /* current size of *fdata */
11515+ infonamelen, /* room size for p */
11516+ whnamelen, /* length of the genarated name */
11517+ renameback; /* renamed back */
11518+};
11519+
11520+/* on rename(2) error, the caller should revert it using @elm */
11521+static int au_drinfo_do_store(struct au_drinfo_store *w,
11522+ struct au_drinfo_rev_elm *elm)
11523+{
11524+ int err, len;
11525+ ssize_t ssz;
11526+ loff_t pos;
11527+ struct path infopath = {
11528+ .mnt = w->h_ppath.mnt
11529+ };
11530+ struct inode *h_dir, *h_inode, *delegated;
11531+ struct file *infofile;
11532+ struct qstr *qname;
11533+
11534+ AuDebugOn(elm
11535+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11536+
11537+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11538+ w->whnamelen);
11539+ AuTraceErrPtr(infopath.dentry);
11540+ if (IS_ERR(infopath.dentry)) {
11541+ err = PTR_ERR(infopath.dentry);
11542+ goto out;
11543+ }
11544+
11545+ err = 0;
11546+ h_dir = d_inode(w->h_ppath.dentry);
11547+ if (elm && d_is_negative(infopath.dentry)) {
11548+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11549+ AuTraceErr(err);
11550+ if (unlikely(err))
11551+ goto out_dput;
11552+ elm->created = 1;
11553+ elm->info_dentry = dget(infopath.dentry);
11554+ }
11555+
11556+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11557+ AuTraceErrPtr(infofile);
11558+ if (IS_ERR(infofile)) {
11559+ err = PTR_ERR(infofile);
11560+ goto out_dput;
11561+ }
11562+
11563+ h_inode = d_inode(infopath.dentry);
11564+ if (elm && i_size_read(h_inode)) {
11565+ h_inode = d_inode(w->h_dentry);
11566+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11567+ AuTraceErrPtr(elm->info_last);
11568+ if (IS_ERR(elm->info_last)) {
11569+ err = PTR_ERR(elm->info_last);
11570+ elm->info_last = NULL;
11571+ AuDebugOn(elm->info_dentry);
11572+ goto out_fput;
11573+ }
11574+ }
11575+
11576+ if (elm && w->renameback) {
11577+ delegated = NULL;
11578+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11579+ AuTraceErr(err);
11580+ if (unlikely(err == -EWOULDBLOCK))
11581+ iput(delegated);
11582+ goto out_fput;
11583+ }
11584+
11585+ pos = 0;
11586+ qname = &w->h_dentry->d_name;
11587+ len = sizeof(*w->fdata) + qname->len;
11588+ if (!elm)
11589+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11590+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11591+ if (ssz == len) {
11592+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11593+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11594+ goto out_fput; /* success */
11595+ } else {
11596+ err = -EIO;
11597+ if (ssz < 0)
11598+ err = ssz;
11599+ /* the caller should revert it using @elm */
11600+ }
11601+
11602+out_fput:
11603+ fput(infofile);
11604+out_dput:
11605+ dput(infopath.dentry);
11606+out:
11607+ AuTraceErr(err);
11608+ return err;
11609+}
11610+
11611+struct au_call_drinfo_do_store_args {
11612+ int *errp;
11613+ struct au_drinfo_store *w;
11614+ struct au_drinfo_rev_elm *elm;
11615+};
11616+
11617+static void au_call_drinfo_do_store(void *args)
11618+{
11619+ struct au_call_drinfo_do_store_args *a = args;
11620+
11621+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11622+}
11623+
11624+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11625+ struct au_drinfo_rev_elm *elm)
11626+{
11627+ int err, wkq_err;
11628+
11629+ if (w->no_sio)
11630+ err = au_drinfo_do_store(w, elm);
11631+ else {
11632+ struct au_call_drinfo_do_store_args a = {
11633+ .errp = &err,
11634+ .w = w,
11635+ .elm = elm
11636+ };
11637+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11638+ if (unlikely(wkq_err))
11639+ err = wkq_err;
11640+ }
11641+ AuTraceErr(err);
11642+
11643+ return err;
11644+}
11645+
11646+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11647+ aufs_bindex_t btgt)
11648+{
11649+ int err;
11650+
11651+ memset(w, 0, sizeof(*w));
11652+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11653+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11654+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11655+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11656+ w->btgt = btgt;
11657+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11658+
11659+ err = -ENOMEM;
11660+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11661+ if (unlikely(!w->fdata)) {
11662+ AuTraceErr(err);
11663+ goto out;
11664+ }
11665+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11666+ err = 0;
11667+
11668+out:
11669+ return err;
11670+}
11671+
11672+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11673+{
11674+ kfree(w->fdata);
11675+}
11676+
11677+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11678+ struct au_drinfo_store *w)
11679+{
11680+ struct au_drinfo_rev_elm *elm;
11681+ struct inode *h_dir, *delegated;
11682+ int err, nelm;
11683+ struct path infopath = {
11684+ .mnt = w->h_ppath.mnt
11685+ };
11686+
11687+ h_dir = d_inode(w->h_ppath.dentry);
11688+ IMustLock(h_dir);
11689+
11690+ err = 0;
11691+ elm = rev->elm;
11692+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11693+ AuDebugOn(elm->created && elm->info_last);
11694+ if (elm->created) {
11695+ AuDbg("here\n");
11696+ delegated = NULL;
11697+ infopath.dentry = elm->info_dentry;
11698+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11699+ !w->no_sio);
11700+ AuTraceErr(err);
11701+ if (unlikely(err == -EWOULDBLOCK))
11702+ iput(delegated);
11703+ dput(elm->info_dentry);
11704+ } else if (elm->info_last) {
11705+ AuDbg("here\n");
11706+ w->fdata->drinfo = *elm->info_last;
11707+ memcpy(w->fdata->drinfo.oldname,
11708+ elm->info_last->oldname,
11709+ elm->info_last->oldnamelen);
11710+ err = au_drinfo_store_sio(w, /*elm*/NULL);
11711+ kfree(elm->info_last);
11712+ }
11713+ if (unlikely(err))
11714+ AuIOErr("%d, %s\n", err, w->whname);
11715+ /* go on even if err */
11716+ }
11717+}
11718+
11719+/* caller has to call au_dr_rename_fin() later */
11720+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11721+ struct qstr *dst_name, void *_rev)
11722+{
11723+ int err, sz, nelm;
11724+ aufs_bindex_t bindex, btail;
11725+ struct au_drinfo_store work;
11726+ struct au_drinfo_rev *rev, **p;
11727+ struct au_drinfo_rev_elm *elm;
11728+ struct super_block *sb;
11729+ struct au_branch *br;
11730+ struct au_hinode *hdir;
11731+
11732+ err = au_drinfo_store_work_init(&work, btgt);
11733+ AuTraceErr(err);
11734+ if (unlikely(err))
11735+ goto out;
11736+
11737+ err = -ENOMEM;
11738+ btail = au_dbtaildir(dentry);
11739+ nelm = btail - btgt;
11740+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11741+ rev = kcalloc(1, sz, GFP_NOFS);
11742+ if (unlikely(!rev)) {
11743+ AuTraceErr(err);
11744+ goto out_args;
11745+ }
11746+ rev->nelm = nelm;
11747+ elm = rev->elm;
11748+ p = _rev;
11749+ *p = rev;
11750+
11751+ err = 0;
11752+ sb = dentry->d_sb;
11753+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11754+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11755+ hdir = au_hi(d_inode(dentry), btgt);
11756+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11757+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11758+ work.h_dentry = au_h_dptr(dentry, bindex);
11759+ if (!work.h_dentry)
11760+ continue;
11761+
11762+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11763+ &work.allocated);
11764+ AuTraceErr(err);
11765+ if (unlikely(err))
11766+ break;
11767+
11768+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11769+ br = au_sbr(sb, bindex);
11770+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11771+ work.whnamelen += au_drinfo_name(br, work.infoname,
11772+ work.infonamelen);
11773+ AuDbg("whname %.*s, i%llu, %.*s\n",
11774+ work.whnamelen, work.whname,
11775+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11776+ work.fdata->drinfo.oldnamelen,
11777+ work.fdata->drinfo.oldname);
11778+
11779+ err = au_drinfo_store_sio(&work, elm);
11780+ AuTraceErr(err);
11781+ if (unlikely(err))
11782+ break;
11783+ }
11784+ if (unlikely(err)) {
11785+ /* revert all drinfo */
11786+ au_drinfo_store_rev(rev, &work);
11787+ kfree(rev);
11788+ *p = NULL;
11789+ }
11790+ au_hn_inode_unlock(hdir);
11791+
11792+out_args:
11793+ au_drinfo_store_work_fin(&work);
11794+out:
11795+ return err;
11796+}
11797+
11798+/* ---------------------------------------------------------------------- */
11799+
11800+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11801+ struct qstr *dst_name, void *_rev)
11802+{
11803+ int err, already;
11804+ ino_t ino;
11805+ struct super_block *sb;
11806+ struct au_branch *br;
11807+ struct au_dr_br *dr;
11808+ struct dentry *h_dentry;
11809+ struct inode *h_inode;
11810+ struct au_dr_hino *ent;
11811+ struct au_drinfo_rev *rev, **p;
11812+
11813+ AuDbg("bindex %d\n", bindex);
11814+
11815+ err = -ENOMEM;
11816+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11817+ if (unlikely(!ent))
11818+ goto out;
11819+
11820+ sb = src->d_sb;
11821+ br = au_sbr(sb, bindex);
11822+ dr = &br->br_dirren;
11823+ h_dentry = au_h_dptr(src, bindex);
11824+ h_inode = d_inode(h_dentry);
11825+ ino = h_inode->i_ino;
11826+ ent->dr_h_ino = ino;
11827+ already = au_dr_hino_test_add(dr, ino, ent);
11828+ AuDbg("b%d, hi%llu, already %d\n",
11829+ bindex, (unsigned long long)ino, already);
11830+
11831+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11832+ AuTraceErr(err);
11833+ if (!err) {
11834+ p = _rev;
11835+ rev = *p;
11836+ rev->already = already;
11837+ goto out; /* success */
11838+ }
11839+
11840+ /* revert */
11841+ if (!already)
11842+ au_dr_hino_del(dr, ent);
11843+ kfree(ent);
11844+
11845+out:
11846+ AuTraceErr(err);
11847+ return err;
11848+}
11849+
11850+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11851+{
11852+ struct au_drinfo_rev *rev;
11853+ struct au_drinfo_rev_elm *elm;
11854+ int nelm;
11855+
11856+ rev = _rev;
11857+ elm = rev->elm;
11858+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11859+ dput(elm->info_dentry);
11860+ kfree(elm->info_last);
11861+ }
11862+ kfree(rev);
11863+}
11864+
11865+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11866+{
11867+ int err;
11868+ struct au_drinfo_store work;
11869+ struct au_drinfo_rev *rev = _rev;
11870+ struct super_block *sb;
11871+ struct au_branch *br;
11872+ struct inode *h_inode;
11873+ struct au_dr_br *dr;
11874+ struct au_dr_hino *ent;
11875+
11876+ err = au_drinfo_store_work_init(&work, btgt);
11877+ if (unlikely(err))
11878+ goto out;
11879+
11880+ sb = src->d_sb;
11881+ br = au_sbr(sb, btgt);
11882+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11883+ work.h_ppath.mnt = au_br_mnt(br);
11884+ au_drinfo_store_rev(rev, &work);
11885+ au_drinfo_store_work_fin(&work);
11886+ if (rev->already)
11887+ goto out;
11888+
11889+ dr = &br->br_dirren;
11890+ h_inode = d_inode(work.h_ppath.dentry);
11891+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11892+ BUG_ON(!ent);
11893+ au_dr_hino_del(dr, ent);
11894+ kfree(ent);
11895+
11896+out:
11897+ kfree(rev);
11898+ if (unlikely(err))
11899+ pr_err("failed to remove dirren info\n");
11900+}
11901+
11902+/* ---------------------------------------------------------------------- */
11903+
11904+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11905+ char *whname, int whnamelen,
11906+ struct dentry **info_dentry)
11907+{
11908+ struct au_drinfo *drinfo;
11909+ struct file *f;
11910+ struct inode *h_dir;
11911+ struct path infopath;
11912+ int unlocked;
11913+
11914+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11915+
11916+ *info_dentry = NULL;
11917+ drinfo = NULL;
11918+ unlocked = 0;
11919+ h_dir = d_inode(h_ppath->dentry);
be118d29 11920+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947
AM
11921+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11922+ whnamelen);
11923+ if (IS_ERR(infopath.dentry)) {
11924+ drinfo = (void *)infopath.dentry;
11925+ goto out;
11926+ }
11927+
11928+ if (d_is_negative(infopath.dentry))
11929+ goto out_dput; /* success */
11930+
11931+ infopath.mnt = h_ppath->mnt;
11932+ f = vfsub_dentry_open(&infopath, O_RDONLY);
11933+ inode_unlock_shared(h_dir);
11934+ unlocked = 1;
11935+ if (IS_ERR(f)) {
11936+ drinfo = (void *)f;
11937+ goto out_dput;
11938+ }
11939+
11940+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11941+ if (IS_ERR_OR_NULL(drinfo))
11942+ goto out_fput;
11943+
11944+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11945+ *info_dentry = dget(infopath.dentry); /* keep it alive */
11946+
11947+out_fput:
11948+ fput(f);
11949+out_dput:
11950+ dput(infopath.dentry);
11951+out:
11952+ if (!unlocked)
11953+ inode_unlock_shared(h_dir);
11954+ AuTraceErrPtr(drinfo);
11955+ return drinfo;
11956+}
11957+
11958+struct au_drinfo_do_load_args {
11959+ struct au_drinfo **drinfop;
11960+ struct path *h_ppath;
11961+ char *whname;
11962+ int whnamelen;
11963+ struct dentry **info_dentry;
11964+};
11965+
11966+static void au_call_drinfo_do_load(void *args)
11967+{
11968+ struct au_drinfo_do_load_args *a = args;
11969+
11970+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11971+ a->info_dentry);
11972+}
11973+
11974+struct au_drinfo_load {
11975+ struct path h_ppath;
11976+ struct qstr *qname;
11977+ unsigned char no_sio;
11978+
11979+ aufs_bindex_t ninfo;
11980+ struct au_drinfo **drinfo;
11981+};
11982+
11983+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11984+ struct au_branch *br)
11985+{
11986+ int err, wkq_err, whnamelen, e;
11987+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11988+ = AUFS_WH_DR_INFO_PFX;
11989+ struct au_drinfo *drinfo;
11990+ struct qstr oldname;
11991+ struct inode *h_dir, *delegated;
11992+ struct dentry *info_dentry;
11993+ struct path infopath;
11994+
11995+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11996+ whnamelen += au_drinfo_name(br, whname + whnamelen,
11997+ sizeof(whname) - whnamelen);
11998+ if (w->no_sio)
11999+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12000+ &info_dentry);
12001+ else {
12002+ struct au_drinfo_do_load_args args = {
12003+ .drinfop = &drinfo,
12004+ .h_ppath = &w->h_ppath,
12005+ .whname = whname,
12006+ .whnamelen = whnamelen,
12007+ .info_dentry = &info_dentry
12008+ };
12009+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12010+ if (unlikely(wkq_err))
12011+ drinfo = ERR_PTR(wkq_err);
12012+ }
12013+ err = PTR_ERR(drinfo);
12014+ if (IS_ERR_OR_NULL(drinfo))
12015+ goto out;
12016+
12017+ err = 0;
12018+ oldname.len = drinfo->oldnamelen;
12019+ oldname.name = drinfo->oldname;
12020+ if (au_qstreq(w->qname, &oldname)) {
12021+ /* the name is renamed back */
12022+ kfree(drinfo);
12023+ drinfo = NULL;
12024+
12025+ infopath.dentry = info_dentry;
12026+ infopath.mnt = w->h_ppath.mnt;
12027+ h_dir = d_inode(w->h_ppath.dentry);
12028+ delegated = NULL;
12029+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12030+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12031+ inode_unlock(h_dir);
12032+ if (unlikely(e))
12033+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12034+ if (unlikely(e == -EWOULDBLOCK))
12035+ iput(delegated);
12036+ }
12037+ kfree(w->drinfo[bindex]);
12038+ w->drinfo[bindex] = drinfo;
12039+ dput(info_dentry);
12040+
12041+out:
12042+ AuTraceErr(err);
12043+ return err;
12044+}
12045+
12046+/* ---------------------------------------------------------------------- */
12047+
12048+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12049+{
12050+ struct au_drinfo **p = drinfo;
12051+
12052+ while (n-- > 0)
12053+ kfree(*drinfo++);
12054+ kfree(p);
12055+}
12056+
12057+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12058+ aufs_bindex_t btgt)
12059+{
12060+ int err, ninfo;
12061+ struct au_drinfo_load w;
12062+ aufs_bindex_t bindex, bbot;
12063+ struct au_branch *br;
12064+ struct inode *h_dir;
12065+ struct au_dr_hino *ent;
12066+ struct super_block *sb;
12067+
12068+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12069+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12070+ AuLNPair(&lkup->whname), btgt);
12071+
12072+ sb = dentry->d_sb;
12073+ bbot = au_sbbot(sb);
12074+ w.ninfo = bbot + 1;
12075+ if (!lkup->dirren.drinfo) {
12076+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12077+ sizeof(*lkup->dirren.drinfo),
12078+ GFP_NOFS);
12079+ if (unlikely(!lkup->dirren.drinfo)) {
12080+ err = -ENOMEM;
12081+ goto out;
12082+ }
12083+ lkup->dirren.ninfo = w.ninfo;
12084+ }
12085+ w.drinfo = lkup->dirren.drinfo;
12086+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12087+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12088+ AuDebugOn(!w.h_ppath.dentry);
12089+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12090+ w.qname = &dentry->d_name;
12091+
12092+ ninfo = 0;
12093+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12094+ br = au_sbr(sb, bindex);
12095+ err = au_drinfo_load(&w, bindex, br);
12096+ if (unlikely(err))
12097+ goto out_free;
12098+ if (w.drinfo[bindex])
12099+ ninfo++;
12100+ }
12101+ if (!ninfo) {
12102+ br = au_sbr(sb, btgt);
12103+ h_dir = d_inode(w.h_ppath.dentry);
12104+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12105+ AuDebugOn(!ent);
12106+ au_dr_hino_del(&br->br_dirren, ent);
12107+ kfree(ent);
12108+ }
12109+ goto out; /* success */
12110+
12111+out_free:
12112+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12113+ lkup->dirren.ninfo = 0;
12114+ lkup->dirren.drinfo = NULL;
12115+out:
12116+ AuTraceErr(err);
12117+ return err;
12118+}
12119+
12120+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12121+{
12122+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12123+}
12124+
12125+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12126+{
12127+ int err;
12128+ struct au_drinfo *drinfo;
12129+
12130+ err = 0;
12131+ if (!lkup->dirren.drinfo)
12132+ goto out;
12133+ AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12134+ drinfo = lkup->dirren.drinfo[btgt + 1];
12135+ if (!drinfo)
12136+ goto out;
12137+
12138+ kfree(lkup->whname.name);
12139+ lkup->whname.name = NULL;
12140+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12141+ lkup->dirren.dr_name.name = drinfo->oldname;
12142+ lkup->name = &lkup->dirren.dr_name;
12143+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12144+ if (!err)
12145+ AuDbg("name %.*s, whname %.*s, b%d\n",
12146+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12147+ btgt);
12148+
12149+out:
12150+ AuTraceErr(err);
12151+ return err;
12152+}
12153+
12154+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12155+ ino_t h_ino)
12156+{
12157+ int match;
12158+ struct au_drinfo *drinfo;
12159+
12160+ match = 1;
12161+ if (!lkup->dirren.drinfo)
12162+ goto out;
12163+ AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12164+ drinfo = lkup->dirren.drinfo[bindex + 1];
12165+ if (!drinfo)
12166+ goto out;
12167+
12168+ match = (drinfo->ino == h_ino);
12169+ AuDbg("match %d\n", match);
12170+
12171+out:
12172+ return match;
12173+}
12174+
12175+/* ---------------------------------------------------------------------- */
12176+
12177+int au_dr_opt_set(struct super_block *sb)
12178+{
12179+ int err;
12180+ aufs_bindex_t bindex, bbot;
12181+ struct au_branch *br;
12182+
12183+ err = 0;
12184+ bbot = au_sbbot(sb);
12185+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12186+ br = au_sbr(sb, bindex);
12187+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12188+ }
12189+
12190+ return err;
12191+}
12192+
12193+int au_dr_opt_flush(struct super_block *sb)
12194+{
12195+ int err;
12196+ aufs_bindex_t bindex, bbot;
12197+ struct au_branch *br;
12198+
12199+ err = 0;
12200+ bbot = au_sbbot(sb);
12201+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12202+ br = au_sbr(sb, bindex);
12203+ if (au_br_writable(br->br_perm))
12204+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12205+ }
12206+
12207+ return err;
12208+}
12209+
12210+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12211+{
12212+ int err;
12213+ aufs_bindex_t bindex, bbot;
12214+ struct au_branch *br;
12215+
12216+ err = 0;
12217+ if (!no_flush) {
12218+ err = au_dr_opt_flush(sb);
12219+ if (unlikely(err))
12220+ goto out;
12221+ }
12222+
12223+ bbot = au_sbbot(sb);
12224+ for (bindex = 0; bindex <= bbot; bindex++) {
12225+ br = au_sbr(sb, bindex);
12226+ au_dr_hino_free(&br->br_dirren);
12227+ }
12228+
12229+out:
12230+ return err;
12231+}
12232diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12233--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 12234+++ linux/fs/aufs/dirren.h 2018-06-04 09:08:09.184746078 +0200
8b6a4947
AM
12235@@ -0,0 +1,139 @@
12236+/*
b00004a5 12237+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
12238+ *
12239+ * This program, aufs is free software; you can redistribute it and/or modify
12240+ * it under the terms of the GNU General Public License as published by
12241+ * the Free Software Foundation; either version 2 of the License, or
12242+ * (at your option) any later version.
12243+ *
12244+ * This program is distributed in the hope that it will be useful,
12245+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12246+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12247+ * GNU General Public License for more details.
12248+ *
12249+ * You should have received a copy of the GNU General Public License
12250+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12251+ */
12252+
12253+/*
12254+ * renamed dir info
12255+ */
12256+
12257+#ifndef __AUFS_DIRREN_H__
12258+#define __AUFS_DIRREN_H__
12259+
12260+#ifdef __KERNEL__
12261+
12262+#include <linux/dcache.h>
12263+#include <linux/statfs.h>
12264+#include <linux/uuid.h>
12265+#include "hbl.h"
12266+
12267+#define AuDirren_NHASH 100
12268+
12269+#ifdef CONFIG_AUFS_DIRREN
12270+enum au_brid_type {
12271+ AuBrid_Unset,
12272+ AuBrid_UUID,
12273+ AuBrid_FSID,
12274+ AuBrid_DEV
12275+};
12276+
12277+struct au_dr_brid {
12278+ enum au_brid_type type;
12279+ union {
12280+ uuid_t uuid; /* unimplemented yet */
12281+ fsid_t fsid;
12282+ dev_t dev;
12283+ };
12284+};
12285+
12286+/* 20 is the max digits length of ulong 64 */
12287+/* brid-type "_" uuid "_" inum */
12288+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12289+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12290+
12291+struct au_dr_hino {
12292+ struct hlist_bl_node dr_hnode;
12293+ ino_t dr_h_ino;
12294+};
12295+
12296+struct au_dr_br {
12297+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12298+ struct au_dr_brid dr_brid;
12299+};
12300+
12301+struct au_dr_lookup {
12302+ /* dr_name is pointed by struct au_do_lookup_args.name */
12303+ struct qstr dr_name; /* subset of dr_info */
12304+ aufs_bindex_t ninfo;
12305+ struct au_drinfo **drinfo;
12306+};
12307+#else
12308+struct au_dr_hino;
12309+/* empty */
12310+struct au_dr_br { };
12311+struct au_dr_lookup { };
12312+#endif
12313+
12314+/* ---------------------------------------------------------------------- */
12315+
12316+struct au_branch;
12317+struct au_do_lookup_args;
12318+struct au_hinode;
12319+#ifdef CONFIG_AUFS_DIRREN
12320+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12321+ struct au_dr_hino *add_ent);
12322+void au_dr_hino_free(struct au_dr_br *dr);
12323+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12324+ const struct path *path);
12325+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12326+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12327+ struct qstr *dst_name, void *_rev);
12328+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12329+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12330+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12331+ aufs_bindex_t bindex);
12332+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12333+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12334+ ino_t h_ino);
12335+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12336+int au_dr_opt_set(struct super_block *sb);
12337+int au_dr_opt_flush(struct super_block *sb);
12338+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12339+#else
12340+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12341+ struct au_dr_hino *add_ent);
12342+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12343+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12344+ const struct path *path);
12345+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12346+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12347+ struct qstr *dst_name, void *_rev);
12348+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12349+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12350+ void *rev);
12351+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12352+ aufs_bindex_t bindex);
12353+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12354+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12355+ aufs_bindex_t bindex, ino_t h_ino);
12356+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12357+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12358+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12359+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12360+#endif
12361+
12362+/* ---------------------------------------------------------------------- */
12363+
12364+#ifdef CONFIG_AUFS_DIRREN
12365+static inline int au_dr_ihash(ino_t h_ino)
12366+{
12367+ return h_ino % AuDirren_NHASH;
12368+}
12369+#else
12370+AuStubInt0(au_dr_ihash, ino_t h_ino);
12371+#endif
12372+
12373+#endif /* __KERNEL__ */
12374+#endif /* __AUFS_DIRREN_H__ */
7f207e10
AM
12375diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12376--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 12377+++ linux/fs/aufs/dynop.c 2018-04-15 08:49:13.397817296 +0200
8b6a4947 12378@@ -0,0 +1,369 @@
1facf9fc 12379+/*
b00004a5 12380+ * Copyright (C) 2010-2018 Junjiro R. Okajima
1facf9fc 12381+ *
12382+ * This program, aufs is free software; you can redistribute it and/or modify
12383+ * it under the terms of the GNU General Public License as published by
12384+ * the Free Software Foundation; either version 2 of the License, or
12385+ * (at your option) any later version.
dece6358
AM
12386+ *
12387+ * This program is distributed in the hope that it will be useful,
12388+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12389+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12390+ * GNU General Public License for more details.
12391+ *
12392+ * You should have received a copy of the GNU General Public License
523b37e3 12393+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12394+ */
12395+
12396+/*
4a4d8108 12397+ * dynamically customizable operations for regular files
1facf9fc 12398+ */
12399+
1facf9fc 12400+#include "aufs.h"
12401+
4a4d8108 12402+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12403+
4a4d8108
AM
12404+/*
12405+ * How large will these lists be?
12406+ * Usually just a few elements, 20-30 at most for each, I guess.
12407+ */
8b6a4947 12408+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12409+
8b6a4947
AM
12410+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12411+ const void *h_op)
1facf9fc 12412+{
4a4d8108 12413+ struct au_dykey *key, *tmp;
8b6a4947 12414+ struct hlist_bl_node *pos;
1facf9fc 12415+
4a4d8108 12416+ key = NULL;
8b6a4947
AM
12417+ hlist_bl_lock(hbl);
12418+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12419+ if (tmp->dk_op.dy_hop == h_op) {
12420+ key = tmp;
12421+ kref_get(&key->dk_kref);
12422+ break;
12423+ }
8b6a4947 12424+ hlist_bl_unlock(hbl);
4a4d8108
AM
12425+
12426+ return key;
1facf9fc 12427+}
12428+
4a4d8108 12429+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12430+{
4a4d8108
AM
12431+ struct au_dykey **k, *found;
12432+ const void *h_op = key->dk_op.dy_hop;
12433+ int i;
1facf9fc 12434+
4a4d8108
AM
12435+ found = NULL;
12436+ k = br->br_dykey;
12437+ for (i = 0; i < AuBrDynOp; i++)
12438+ if (k[i]) {
12439+ if (k[i]->dk_op.dy_hop == h_op) {
12440+ found = k[i];
12441+ break;
12442+ }
12443+ } else
12444+ break;
12445+ if (!found) {
12446+ spin_lock(&br->br_dykey_lock);
12447+ for (; i < AuBrDynOp; i++)
12448+ if (k[i]) {
12449+ if (k[i]->dk_op.dy_hop == h_op) {
12450+ found = k[i];
12451+ break;
12452+ }
12453+ } else {
12454+ k[i] = key;
12455+ break;
12456+ }
12457+ spin_unlock(&br->br_dykey_lock);
12458+ BUG_ON(i == AuBrDynOp); /* expand the array */
12459+ }
12460+
12461+ return found;
1facf9fc 12462+}
12463+
4a4d8108 12464+/* kref_get() if @key is already added */
8b6a4947 12465+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12466+{
12467+ struct au_dykey *tmp, *found;
8b6a4947 12468+ struct hlist_bl_node *pos;
4a4d8108 12469+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12470+
4a4d8108 12471+ found = NULL;
8b6a4947
AM
12472+ hlist_bl_lock(hbl);
12473+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12474+ if (tmp->dk_op.dy_hop == h_op) {
12475+ kref_get(&tmp->dk_kref);
12476+ found = tmp;
12477+ break;
12478+ }
12479+ if (!found)
8b6a4947
AM
12480+ hlist_bl_add_head(&key->dk_hnode, hbl);
12481+ hlist_bl_unlock(hbl);
1facf9fc 12482+
4a4d8108
AM
12483+ if (!found)
12484+ DyPrSym(key);
12485+ return found;
12486+}
12487+
12488+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12489+{
4a4d8108
AM
12490+ struct au_dykey *key;
12491+
12492+ key = container_of(rcu, struct au_dykey, dk_rcu);
12493+ DyPrSym(key);
1c60b727 12494+ kfree(key);
1facf9fc 12495+}
12496+
4a4d8108
AM
12497+static void dy_free(struct kref *kref)
12498+{
12499+ struct au_dykey *key;
8b6a4947 12500+ struct hlist_bl_head *hbl;
1facf9fc 12501+
4a4d8108 12502+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12503+ hbl = dynop + key->dk_op.dy_type;
12504+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12505+ call_rcu(&key->dk_rcu, dy_free_rcu);
12506+}
12507+
12508+void au_dy_put(struct au_dykey *key)
1facf9fc 12509+{
4a4d8108
AM
12510+ kref_put(&key->dk_kref, dy_free);
12511+}
1facf9fc 12512+
4a4d8108
AM
12513+/* ---------------------------------------------------------------------- */
12514+
12515+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12516+
12517+#ifdef CONFIG_AUFS_DEBUG
12518+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12519+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12520+#else
12521+#define DyDbgDeclare(cnt) do {} while (0)
12522+#define DyDbgInc(cnt) do {} while (0)
12523+#endif
12524+
12525+#define DySet(func, dst, src, h_op, h_sb) do { \
12526+ DyDbgInc(cnt); \
12527+ if (h_op->func) { \
12528+ if (src.func) \
12529+ dst.func = src.func; \
12530+ else \
12531+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12532+ } \
12533+} while (0)
12534+
12535+#define DySetForce(func, dst, src) do { \
12536+ AuDebugOn(!src.func); \
12537+ DyDbgInc(cnt); \
12538+ dst.func = src.func; \
12539+} while (0)
12540+
12541+#define DySetAop(func) \
12542+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12543+#define DySetAopForce(func) \
12544+ DySetForce(func, dyaop->da_op, aufs_aop)
12545+
12546+static void dy_aop(struct au_dykey *key, const void *h_op,
12547+ struct super_block *h_sb __maybe_unused)
12548+{
12549+ struct au_dyaop *dyaop = (void *)key;
12550+ const struct address_space_operations *h_aop = h_op;
12551+ DyDbgDeclare(cnt);
12552+
12553+ AuDbg("%s\n", au_sbtype(h_sb));
12554+
12555+ DySetAop(writepage);
12556+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12557+ DySetAop(writepages);
12558+ DySetAop(set_page_dirty);
12559+ DySetAop(readpages);
12560+ DySetAop(write_begin);
12561+ DySetAop(write_end);
12562+ DySetAop(bmap);
12563+ DySetAop(invalidatepage);
12564+ DySetAop(releasepage);
027c5e7a 12565+ DySetAop(freepage);
7e9cd9fe 12566+ /* this one will be changed according to an aufs mount option */
4a4d8108 12567+ DySetAop(direct_IO);
4a4d8108 12568+ DySetAop(migratepage);
e2f27e51
AM
12569+ DySetAop(isolate_page);
12570+ DySetAop(putback_page);
4a4d8108
AM
12571+ DySetAop(launder_page);
12572+ DySetAop(is_partially_uptodate);
392086de 12573+ DySetAop(is_dirty_writeback);
4a4d8108 12574+ DySetAop(error_remove_page);
b4510431
AM
12575+ DySetAop(swap_activate);
12576+ DySetAop(swap_deactivate);
4a4d8108
AM
12577+
12578+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12579+}
12580+
4a4d8108
AM
12581+/* ---------------------------------------------------------------------- */
12582+
12583+static void dy_bug(struct kref *kref)
12584+{
12585+ BUG();
12586+}
12587+
12588+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12589+{
12590+ struct au_dykey *key, *old;
8b6a4947 12591+ struct hlist_bl_head *hbl;
b752ccd1 12592+ struct op {
4a4d8108 12593+ unsigned int sz;
b752ccd1
AM
12594+ void (*set)(struct au_dykey *key, const void *h_op,
12595+ struct super_block *h_sb __maybe_unused);
12596+ };
12597+ static const struct op a[] = {
4a4d8108
AM
12598+ [AuDy_AOP] = {
12599+ .sz = sizeof(struct au_dyaop),
b752ccd1 12600+ .set = dy_aop
4a4d8108 12601+ }
b752ccd1
AM
12602+ };
12603+ const struct op *p;
4a4d8108 12604+
8b6a4947
AM
12605+ hbl = dynop + op->dy_type;
12606+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12607+ if (key)
12608+ goto out_add; /* success */
12609+
12610+ p = a + op->dy_type;
12611+ key = kzalloc(p->sz, GFP_NOFS);
12612+ if (unlikely(!key)) {
12613+ key = ERR_PTR(-ENOMEM);
12614+ goto out;
12615+ }
12616+
12617+ key->dk_op.dy_hop = op->dy_hop;
12618+ kref_init(&key->dk_kref);
86dc4139 12619+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12620+ old = dy_gadd(hbl, key);
4a4d8108 12621+ if (old) {
1c60b727 12622+ kfree(key);
4a4d8108
AM
12623+ key = old;
12624+ }
12625+
12626+out_add:
12627+ old = dy_bradd(br, key);
12628+ if (old)
12629+ /* its ref-count should never be zero here */
12630+ kref_put(&key->dk_kref, dy_bug);
12631+out:
12632+ return key;
12633+}
12634+
12635+/* ---------------------------------------------------------------------- */
12636+/*
12637+ * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
c1595e42 12638+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12639+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12640+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12641+ * See the aufs manual in detail.
4a4d8108
AM
12642+ */
12643+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12644+{
7e9cd9fe 12645+ if (!do_dx)
4a4d8108 12646+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12647+ else
4a4d8108 12648+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12649+}
12650+
12651+static struct au_dyaop *dy_aget(struct au_branch *br,
12652+ const struct address_space_operations *h_aop,
12653+ int do_dx)
12654+{
12655+ struct au_dyaop *dyaop;
12656+ struct au_dynop op;
12657+
12658+ op.dy_type = AuDy_AOP;
12659+ op.dy_haop = h_aop;
12660+ dyaop = (void *)dy_get(&op, br);
12661+ if (IS_ERR(dyaop))
12662+ goto out;
12663+ dy_adx(dyaop, do_dx);
12664+
12665+out:
12666+ return dyaop;
12667+}
12668+
12669+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12670+ struct inode *h_inode)
12671+{
12672+ int err, do_dx;
12673+ struct super_block *sb;
12674+ struct au_branch *br;
12675+ struct au_dyaop *dyaop;
12676+
12677+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12678+ IiMustWriteLock(inode);
12679+
12680+ sb = inode->i_sb;
12681+ br = au_sbr(sb, bindex);
12682+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12683+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12684+ err = PTR_ERR(dyaop);
12685+ if (IS_ERR(dyaop))
12686+ /* unnecessary to call dy_fput() */
12687+ goto out;
12688+
12689+ err = 0;
12690+ inode->i_mapping->a_ops = &dyaop->da_op;
12691+
12692+out:
12693+ return err;
12694+}
12695+
b752ccd1
AM
12696+/*
12697+ * Is it safe to replace a_ops during the inode/file is in operation?
12698+ * Yes, I hope so.
12699+ */
12700+int au_dy_irefresh(struct inode *inode)
12701+{
12702+ int err;
5afbbe0d 12703+ aufs_bindex_t btop;
b752ccd1
AM
12704+ struct inode *h_inode;
12705+
12706+ err = 0;
12707+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12708+ btop = au_ibtop(inode);
12709+ h_inode = au_h_iptr(inode, btop);
12710+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12711+ }
12712+ return err;
12713+}
12714+
4a4d8108
AM
12715+void au_dy_arefresh(int do_dx)
12716+{
8b6a4947
AM
12717+ struct hlist_bl_head *hbl;
12718+ struct hlist_bl_node *pos;
4a4d8108
AM
12719+ struct au_dykey *key;
12720+
8b6a4947
AM
12721+ hbl = dynop + AuDy_AOP;
12722+ hlist_bl_lock(hbl);
12723+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12724+ dy_adx((void *)key, do_dx);
8b6a4947 12725+ hlist_bl_unlock(hbl);
4a4d8108
AM
12726+}
12727+
4a4d8108
AM
12728+/* ---------------------------------------------------------------------- */
12729+
12730+void __init au_dy_init(void)
12731+{
12732+ int i;
12733+
12734+ /* make sure that 'struct au_dykey *' can be any type */
12735+ BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
4a4d8108
AM
12736+
12737+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12738+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12739+}
12740+
12741+void au_dy_fin(void)
12742+{
12743+ int i;
12744+
12745+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12746+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12747+}
7f207e10
AM
12748diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12749--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 12750+++ linux/fs/aufs/dynop.h 2018-04-15 08:49:13.397817296 +0200
7e9cd9fe 12751@@ -0,0 +1,74 @@
4a4d8108 12752+/*
b00004a5 12753+ * Copyright (C) 2010-2018 Junjiro R. Okajima
4a4d8108
AM
12754+ *
12755+ * This program, aufs is free software; you can redistribute it and/or modify
12756+ * it under the terms of the GNU General Public License as published by
12757+ * the Free Software Foundation; either version 2 of the License, or
12758+ * (at your option) any later version.
12759+ *
12760+ * This program is distributed in the hope that it will be useful,
12761+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12762+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12763+ * GNU General Public License for more details.
12764+ *
12765+ * You should have received a copy of the GNU General Public License
523b37e3 12766+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12767+ */
12768+
12769+/*
12770+ * dynamically customizable operations (for regular files only)
12771+ */
12772+
12773+#ifndef __AUFS_DYNOP_H__
12774+#define __AUFS_DYNOP_H__
12775+
12776+#ifdef __KERNEL__
12777+
7e9cd9fe
AM
12778+#include <linux/fs.h>
12779+#include <linux/kref.h>
4a4d8108 12780+
2cbb1c4b 12781+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12782+
12783+struct au_dynop {
12784+ int dy_type;
12785+ union {
12786+ const void *dy_hop;
12787+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12788+ };
12789+};
12790+
12791+struct au_dykey {
12792+ union {
8b6a4947 12793+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12794+ struct rcu_head dk_rcu;
12795+ };
12796+ struct au_dynop dk_op;
12797+
12798+ /*
12799+ * during I am in the branch local array, kref is gotten. when the
12800+ * branch is removed, kref is put.
12801+ */
12802+ struct kref dk_kref;
12803+};
12804+
12805+/* stop unioning since their sizes are very different from each other */
12806+struct au_dyaop {
12807+ struct au_dykey da_key;
12808+ struct address_space_operations da_op; /* not const */
4a4d8108
AM
12809+};
12810+
4a4d8108
AM
12811+/* ---------------------------------------------------------------------- */
12812+
12813+/* dynop.c */
12814+struct au_branch;
12815+void au_dy_put(struct au_dykey *key);
12816+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12817+ struct inode *h_inode);
b752ccd1 12818+int au_dy_irefresh(struct inode *inode);
4a4d8108 12819+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12820+
12821+void __init au_dy_init(void);
12822+void au_dy_fin(void);
12823+
4a4d8108
AM
12824+#endif /* __KERNEL__ */
12825+#endif /* __AUFS_DYNOP_H__ */
7f207e10
AM
12826diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12827--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 12828+++ linux/fs/aufs/export.c 2018-04-15 08:49:13.397817296 +0200
f2c43d5f 12829@@ -0,0 +1,836 @@
4a4d8108 12830+/*
b00004a5 12831+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
12832+ *
12833+ * This program, aufs is free software; you can redistribute it and/or modify
12834+ * it under the terms of the GNU General Public License as published by
12835+ * the Free Software Foundation; either version 2 of the License, or
12836+ * (at your option) any later version.
12837+ *
12838+ * This program is distributed in the hope that it will be useful,
12839+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12840+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12841+ * GNU General Public License for more details.
12842+ *
12843+ * You should have received a copy of the GNU General Public License
523b37e3 12844+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12845+ */
12846+
12847+/*
12848+ * export via nfs
12849+ */
12850+
12851+#include <linux/exportfs.h>
7eafdf33 12852+#include <linux/fs_struct.h>
4a4d8108
AM
12853+#include <linux/namei.h>
12854+#include <linux/nsproxy.h>
12855+#include <linux/random.h>
12856+#include <linux/writeback.h>
12857+#include "aufs.h"
12858+
12859+union conv {
12860+#ifdef CONFIG_AUFS_INO_T_64
12861+ __u32 a[2];
12862+#else
12863+ __u32 a[1];
12864+#endif
12865+ ino_t ino;
12866+};
12867+
12868+static ino_t decode_ino(__u32 *a)
12869+{
12870+ union conv u;
12871+
12872+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12873+ u.a[0] = a[0];
12874+#ifdef CONFIG_AUFS_INO_T_64
12875+ u.a[1] = a[1];
12876+#endif
12877+ return u.ino;
12878+}
12879+
12880+static void encode_ino(__u32 *a, ino_t ino)
12881+{
12882+ union conv u;
12883+
12884+ u.ino = ino;
12885+ a[0] = u.a[0];
12886+#ifdef CONFIG_AUFS_INO_T_64
12887+ a[1] = u.a[1];
12888+#endif
12889+}
12890+
12891+/* NFS file handle */
12892+enum {
12893+ Fh_br_id,
12894+ Fh_sigen,
12895+#ifdef CONFIG_AUFS_INO_T_64
12896+ /* support 64bit inode number */
12897+ Fh_ino1,
12898+ Fh_ino2,
12899+ Fh_dir_ino1,
12900+ Fh_dir_ino2,
12901+#else
12902+ Fh_ino1,
12903+ Fh_dir_ino1,
12904+#endif
12905+ Fh_igen,
12906+ Fh_h_type,
12907+ Fh_tail,
12908+
12909+ Fh_ino = Fh_ino1,
12910+ Fh_dir_ino = Fh_dir_ino1
12911+};
12912+
12913+static int au_test_anon(struct dentry *dentry)
12914+{
027c5e7a 12915+ /* note: read d_flags without d_lock */
4a4d8108
AM
12916+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12917+}
12918+
a2a7ad62
AM
12919+int au_test_nfsd(void)
12920+{
12921+ int ret;
12922+ struct task_struct *tsk = current;
12923+ char comm[sizeof(tsk->comm)];
12924+
12925+ ret = 0;
12926+ if (tsk->flags & PF_KTHREAD) {
12927+ get_task_comm(comm, tsk);
12928+ ret = !strcmp(comm, "nfsd");
12929+ }
12930+
12931+ return ret;
12932+}
12933+
4a4d8108
AM
12934+/* ---------------------------------------------------------------------- */
12935+/* inode generation external table */
12936+
b752ccd1 12937+void au_xigen_inc(struct inode *inode)
4a4d8108 12938+{
4a4d8108
AM
12939+ loff_t pos;
12940+ ssize_t sz;
12941+ __u32 igen;
12942+ struct super_block *sb;
12943+ struct au_sbinfo *sbinfo;
12944+
4a4d8108 12945+ sb = inode->i_sb;
b752ccd1 12946+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 12947+
b752ccd1 12948+ sbinfo = au_sbi(sb);
1facf9fc 12949+ pos = inode->i_ino;
12950+ pos *= sizeof(igen);
12951+ igen = inode->i_generation + 1;
1facf9fc 12952+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
12953+ sizeof(igen), &pos);
12954+ if (sz == sizeof(igen))
b752ccd1 12955+ return; /* success */
1facf9fc 12956+
b752ccd1 12957+ if (unlikely(sz >= 0))
1facf9fc 12958+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 12959+}
12960+
12961+int au_xigen_new(struct inode *inode)
12962+{
12963+ int err;
12964+ loff_t pos;
12965+ ssize_t sz;
12966+ struct super_block *sb;
12967+ struct au_sbinfo *sbinfo;
12968+ struct file *file;
12969+
12970+ err = 0;
12971+ /* todo: dirty, at mount time */
12972+ if (inode->i_ino == AUFS_ROOT_INO)
12973+ goto out;
12974+ sb = inode->i_sb;
dece6358 12975+ SiMustAnyLock(sb);
1facf9fc 12976+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12977+ goto out;
12978+
12979+ err = -EFBIG;
12980+ pos = inode->i_ino;
12981+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12982+ AuIOErr1("too large i%lld\n", pos);
12983+ goto out;
12984+ }
12985+ pos *= sizeof(inode->i_generation);
12986+
12987+ err = 0;
12988+ sbinfo = au_sbi(sb);
12989+ file = sbinfo->si_xigen;
12990+ BUG_ON(!file);
12991+
c06a8ce3 12992+ if (vfsub_f_size_read(file)
1facf9fc 12993+ < pos + sizeof(inode->i_generation)) {
12994+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12995+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
12996+ sizeof(inode->i_generation), &pos);
12997+ } else
12998+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
12999+ sizeof(inode->i_generation), &pos);
13000+ if (sz == sizeof(inode->i_generation))
13001+ goto out; /* success */
13002+
13003+ err = sz;
13004+ if (unlikely(sz >= 0)) {
13005+ err = -EIO;
13006+ AuIOErr("xigen error (%zd)\n", sz);
13007+ }
13008+
4f0767ce 13009+out:
1facf9fc 13010+ return err;
13011+}
13012+
13013+int au_xigen_set(struct super_block *sb, struct file *base)
13014+{
13015+ int err;
13016+ struct au_sbinfo *sbinfo;
13017+ struct file *file;
13018+
dece6358
AM
13019+ SiMustWriteLock(sb);
13020+
1facf9fc 13021+ sbinfo = au_sbi(sb);
13022+ file = au_xino_create2(base, sbinfo->si_xigen);
13023+ err = PTR_ERR(file);
13024+ if (IS_ERR(file))
13025+ goto out;
13026+ err = 0;
13027+ if (sbinfo->si_xigen)
13028+ fput(sbinfo->si_xigen);
13029+ sbinfo->si_xigen = file;
13030+
4f0767ce 13031+out:
1facf9fc 13032+ return err;
13033+}
13034+
13035+void au_xigen_clr(struct super_block *sb)
13036+{
13037+ struct au_sbinfo *sbinfo;
13038+
dece6358
AM
13039+ SiMustWriteLock(sb);
13040+
1facf9fc 13041+ sbinfo = au_sbi(sb);
13042+ if (sbinfo->si_xigen) {
13043+ fput(sbinfo->si_xigen);
13044+ sbinfo->si_xigen = NULL;
13045+ }
13046+}
13047+
13048+/* ---------------------------------------------------------------------- */
13049+
13050+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13051+ ino_t dir_ino)
13052+{
13053+ struct dentry *dentry, *d;
13054+ struct inode *inode;
13055+ unsigned int sigen;
13056+
13057+ dentry = NULL;
13058+ inode = ilookup(sb, ino);
13059+ if (!inode)
13060+ goto out;
13061+
13062+ dentry = ERR_PTR(-ESTALE);
13063+ sigen = au_sigen(sb);
5afbbe0d 13064+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13065+ || IS_DEADDIR(inode)
537831f9 13066+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13067+ goto out_iput;
13068+
13069+ dentry = NULL;
13070+ if (!dir_ino || S_ISDIR(inode->i_mode))
13071+ dentry = d_find_alias(inode);
13072+ else {
027c5e7a 13073+ spin_lock(&inode->i_lock);
c1595e42 13074+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13075+ spin_lock(&d->d_lock);
1facf9fc 13076+ if (!au_test_anon(d)
5527c038 13077+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13078+ dentry = dget_dlock(d);
13079+ spin_unlock(&d->d_lock);
1facf9fc 13080+ break;
13081+ }
027c5e7a
AM
13082+ spin_unlock(&d->d_lock);
13083+ }
13084+ spin_unlock(&inode->i_lock);
1facf9fc 13085+ }
027c5e7a 13086+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13087+ /* need to refresh */
1facf9fc 13088+ dput(dentry);
2cbb1c4b 13089+ dentry = NULL;
1facf9fc 13090+ }
13091+
4f0767ce 13092+out_iput:
1facf9fc 13093+ iput(inode);
4f0767ce 13094+out:
2cbb1c4b 13095+ AuTraceErrPtr(dentry);
1facf9fc 13096+ return dentry;
13097+}
13098+
13099+/* ---------------------------------------------------------------------- */
13100+
13101+/* todo: dirty? */
13102+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13103+
13104+struct au_compare_mnt_args {
13105+ /* input */
13106+ struct super_block *sb;
13107+
13108+ /* output */
13109+ struct vfsmount *mnt;
13110+};
13111+
13112+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13113+{
13114+ struct au_compare_mnt_args *a = arg;
13115+
13116+ if (mnt->mnt_sb != a->sb)
13117+ return 0;
13118+ a->mnt = mntget(mnt);
13119+ return 1;
13120+}
13121+
1facf9fc 13122+static struct vfsmount *au_mnt_get(struct super_block *sb)
13123+{
4a4d8108 13124+ int err;
7eafdf33 13125+ struct path root;
4a4d8108
AM
13126+ struct au_compare_mnt_args args = {
13127+ .sb = sb
13128+ };
1facf9fc 13129+
7eafdf33 13130+ get_fs_root(current->fs, &root);
523b37e3 13131+ rcu_read_lock();
7eafdf33 13132+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13133+ rcu_read_unlock();
7eafdf33 13134+ path_put(&root);
4a4d8108
AM
13135+ AuDebugOn(!err);
13136+ AuDebugOn(!args.mnt);
13137+ return args.mnt;
1facf9fc 13138+}
13139+
13140+struct au_nfsd_si_lock {
4a4d8108 13141+ unsigned int sigen;
027c5e7a 13142+ aufs_bindex_t bindex, br_id;
1facf9fc 13143+ unsigned char force_lock;
13144+};
13145+
027c5e7a
AM
13146+static int si_nfsd_read_lock(struct super_block *sb,
13147+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13148+{
027c5e7a 13149+ int err;
1facf9fc 13150+ aufs_bindex_t bindex;
13151+
13152+ si_read_lock(sb, AuLock_FLUSH);
13153+
13154+ /* branch id may be wrapped around */
027c5e7a 13155+ err = 0;
1facf9fc 13156+ bindex = au_br_index(sb, nsi_lock->br_id);
13157+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13158+ goto out; /* success */
13159+
027c5e7a
AM
13160+ err = -ESTALE;
13161+ bindex = -1;
1facf9fc 13162+ if (!nsi_lock->force_lock)
13163+ si_read_unlock(sb);
1facf9fc 13164+
4f0767ce 13165+out:
027c5e7a
AM
13166+ nsi_lock->bindex = bindex;
13167+ return err;
1facf9fc 13168+}
13169+
13170+struct find_name_by_ino {
392086de 13171+ struct dir_context ctx;
1facf9fc 13172+ int called, found;
13173+ ino_t ino;
13174+ char *name;
13175+ int namelen;
13176+};
13177+
13178+static int
392086de
AM
13179+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13180+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13181+{
392086de
AM
13182+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13183+ ctx);
1facf9fc 13184+
13185+ a->called++;
13186+ if (a->ino != ino)
13187+ return 0;
13188+
13189+ memcpy(a->name, name, namelen);
13190+ a->namelen = namelen;
13191+ a->found = 1;
13192+ return 1;
13193+}
13194+
13195+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13196+ struct au_nfsd_si_lock *nsi_lock)
13197+{
13198+ struct dentry *dentry, *parent;
13199+ struct file *file;
13200+ struct inode *dir;
392086de
AM
13201+ struct find_name_by_ino arg = {
13202+ .ctx = {
2000de60 13203+ .actor = find_name_by_ino
392086de
AM
13204+ }
13205+ };
1facf9fc 13206+ int err;
13207+
13208+ parent = path->dentry;
13209+ if (nsi_lock)
13210+ si_read_unlock(parent->d_sb);
4a4d8108 13211+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13212+ dentry = (void *)file;
13213+ if (IS_ERR(file))
13214+ goto out;
13215+
13216+ dentry = ERR_PTR(-ENOMEM);
537831f9 13217+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13218+ if (unlikely(!arg.name))
13219+ goto out_file;
13220+ arg.ino = ino;
13221+ arg.found = 0;
13222+ do {
13223+ arg.called = 0;
13224+ /* smp_mb(); */
392086de 13225+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13226+ } while (!err && !arg.found && arg.called);
13227+ dentry = ERR_PTR(err);
13228+ if (unlikely(err))
13229+ goto out_name;
1716fcea
AM
13230+ /* instead of ENOENT */
13231+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13232+ if (!arg.found)
13233+ goto out_name;
13234+
b4510431 13235+ /* do not call vfsub_lkup_one() */
5527c038 13236+ dir = d_inode(parent);
febd17d6 13237+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13238+ AuTraceErrPtr(dentry);
13239+ if (IS_ERR(dentry))
13240+ goto out_name;
13241+ AuDebugOn(au_test_anon(dentry));
5527c038 13242+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13243+ dput(dentry);
13244+ dentry = ERR_PTR(-ENOENT);
13245+ }
13246+
4f0767ce 13247+out_name:
1c60b727 13248+ free_page((unsigned long)arg.name);
4f0767ce 13249+out_file:
1facf9fc 13250+ fput(file);
4f0767ce 13251+out:
1facf9fc 13252+ if (unlikely(nsi_lock
13253+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13254+ if (!IS_ERR(dentry)) {
13255+ dput(dentry);
13256+ dentry = ERR_PTR(-ESTALE);
13257+ }
13258+ AuTraceErrPtr(dentry);
13259+ return dentry;
13260+}
13261+
13262+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13263+ ino_t dir_ino,
13264+ struct au_nfsd_si_lock *nsi_lock)
13265+{
13266+ struct dentry *dentry;
13267+ struct path path;
13268+
13269+ if (dir_ino != AUFS_ROOT_INO) {
13270+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13271+ dentry = path.dentry;
13272+ if (!path.dentry || IS_ERR(path.dentry))
13273+ goto out;
13274+ AuDebugOn(au_test_anon(path.dentry));
13275+ } else
13276+ path.dentry = dget(sb->s_root);
13277+
13278+ path.mnt = au_mnt_get(sb);
13279+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13280+ path_put(&path);
13281+
4f0767ce 13282+out:
1facf9fc 13283+ AuTraceErrPtr(dentry);
13284+ return dentry;
13285+}
13286+
13287+/* ---------------------------------------------------------------------- */
13288+
13289+static int h_acceptable(void *expv, struct dentry *dentry)
13290+{
13291+ return 1;
13292+}
13293+
13294+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13295+ char *buf, int len, struct super_block *sb)
13296+{
13297+ char *p;
13298+ int n;
13299+ struct path path;
13300+
13301+ p = d_path(h_rootpath, buf, len);
13302+ if (IS_ERR(p))
13303+ goto out;
13304+ n = strlen(p);
13305+
13306+ path.mnt = h_rootpath->mnt;
13307+ path.dentry = h_parent;
13308+ p = d_path(&path, buf, len);
13309+ if (IS_ERR(p))
13310+ goto out;
13311+ if (n != 1)
13312+ p += n;
13313+
13314+ path.mnt = au_mnt_get(sb);
13315+ path.dentry = sb->s_root;
13316+ p = d_path(&path, buf, len - strlen(p));
13317+ mntput(path.mnt);
13318+ if (IS_ERR(p))
13319+ goto out;
13320+ if (n != 1)
13321+ p[strlen(p)] = '/';
13322+
4f0767ce 13323+out:
1facf9fc 13324+ AuTraceErrPtr(p);
13325+ return p;
13326+}
13327+
13328+static
027c5e7a
AM
13329+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13330+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13331+{
13332+ struct dentry *dentry, *h_parent, *root;
13333+ struct super_block *h_sb;
13334+ char *pathname, *p;
13335+ struct vfsmount *h_mnt;
13336+ struct au_branch *br;
13337+ int err;
13338+ struct path path;
13339+
027c5e7a 13340+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13341+ h_mnt = au_br_mnt(br);
1facf9fc 13342+ h_sb = h_mnt->mnt_sb;
13343+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13344+ lockdep_off();
1facf9fc 13345+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13346+ fh_len - Fh_tail, fh[Fh_h_type],
13347+ h_acceptable, /*context*/NULL);
5afbbe0d 13348+ lockdep_on();
1facf9fc 13349+ dentry = h_parent;
13350+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13351+ AuWarn1("%s decode_fh failed, %ld\n",
13352+ au_sbtype(h_sb), PTR_ERR(h_parent));
13353+ goto out;
13354+ }
13355+ dentry = NULL;
13356+ if (unlikely(au_test_anon(h_parent))) {
13357+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13358+ au_sbtype(h_sb));
13359+ goto out_h_parent;
13360+ }
13361+
13362+ dentry = ERR_PTR(-ENOMEM);
13363+ pathname = (void *)__get_free_page(GFP_NOFS);
13364+ if (unlikely(!pathname))
13365+ goto out_h_parent;
13366+
13367+ root = sb->s_root;
13368+ path.mnt = h_mnt;
13369+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13370+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13371+ di_read_unlock(root, !AuLock_IR);
13372+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13373+ dentry = (void *)p;
13374+ if (IS_ERR(p))
13375+ goto out_pathname;
13376+
13377+ si_read_unlock(sb);
13378+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13379+ dentry = ERR_PTR(err);
13380+ if (unlikely(err))
13381+ goto out_relock;
13382+
13383+ dentry = ERR_PTR(-ENOENT);
13384+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13385+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13386+ goto out_path;
13387+
5527c038 13388+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13389+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13390+ else
13391+ dentry = dget(path.dentry);
13392+
4f0767ce 13393+out_path:
1facf9fc 13394+ path_put(&path);
4f0767ce 13395+out_relock:
1facf9fc 13396+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13397+ if (!IS_ERR(dentry)) {
13398+ dput(dentry);
13399+ dentry = ERR_PTR(-ESTALE);
13400+ }
4f0767ce 13401+out_pathname:
1c60b727 13402+ free_page((unsigned long)pathname);
4f0767ce 13403+out_h_parent:
1facf9fc 13404+ dput(h_parent);
4f0767ce 13405+out:
1facf9fc 13406+ AuTraceErrPtr(dentry);
13407+ return dentry;
13408+}
13409+
13410+/* ---------------------------------------------------------------------- */
13411+
13412+static struct dentry *
13413+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13414+ int fh_type)
13415+{
13416+ struct dentry *dentry;
13417+ __u32 *fh = fid->raw;
027c5e7a 13418+ struct au_branch *br;
1facf9fc 13419+ ino_t ino, dir_ino;
1facf9fc 13420+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13421+ .force_lock = 0
13422+ };
13423+
1facf9fc 13424+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13425+ /* it should never happen, but the file handle is unreliable */
13426+ if (unlikely(fh_len < Fh_tail))
13427+ goto out;
13428+ nsi_lock.sigen = fh[Fh_sigen];
13429+ nsi_lock.br_id = fh[Fh_br_id];
13430+
1facf9fc 13431+ /* branch id may be wrapped around */
027c5e7a
AM
13432+ br = NULL;
13433+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13434+ goto out;
13435+ nsi_lock.force_lock = 1;
13436+
13437+ /* is this inode still cached? */
13438+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13439+ /* it should never happen */
13440+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13441+ goto out_unlock;
4a4d8108 13442+
1facf9fc 13443+ dir_ino = decode_ino(fh + Fh_dir_ino);
13444+ dentry = decode_by_ino(sb, ino, dir_ino);
13445+ if (IS_ERR(dentry))
13446+ goto out_unlock;
13447+ if (dentry)
13448+ goto accept;
13449+
13450+ /* is the parent dir cached? */
027c5e7a 13451+ br = au_sbr(sb, nsi_lock.bindex);
5afbbe0d 13452+ au_br_get(br);
1facf9fc 13453+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13454+ if (IS_ERR(dentry))
13455+ goto out_unlock;
13456+ if (dentry)
13457+ goto accept;
13458+
13459+ /* lookup path */
027c5e7a 13460+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13461+ if (IS_ERR(dentry))
13462+ goto out_unlock;
13463+ if (unlikely(!dentry))
13464+ /* todo?: make it ESTALE */
13465+ goto out_unlock;
13466+
4f0767ce 13467+accept:
027c5e7a 13468+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13469+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13470+ goto out_unlock; /* success */
13471+
13472+ dput(dentry);
13473+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13474+out_unlock:
027c5e7a 13475+ if (br)
5afbbe0d 13476+ au_br_put(br);
1facf9fc 13477+ si_read_unlock(sb);
4f0767ce 13478+out:
1facf9fc 13479+ AuTraceErrPtr(dentry);
13480+ return dentry;
13481+}
13482+
13483+#if 0 /* reserved for future use */
13484+/* support subtreecheck option */
13485+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13486+ int fh_len, int fh_type)
13487+{
13488+ struct dentry *parent;
13489+ __u32 *fh = fid->raw;
13490+ ino_t dir_ino;
13491+
13492+ dir_ino = decode_ino(fh + Fh_dir_ino);
13493+ parent = decode_by_ino(sb, dir_ino, 0);
13494+ if (IS_ERR(parent))
13495+ goto out;
13496+ if (!parent)
13497+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13498+ dir_ino, fh, fh_len);
13499+
4f0767ce 13500+out:
1facf9fc 13501+ AuTraceErrPtr(parent);
13502+ return parent;
13503+}
13504+#endif
13505+
13506+/* ---------------------------------------------------------------------- */
13507+
0c3ec466
AM
13508+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13509+ struct inode *dir)
1facf9fc 13510+{
13511+ int err;
0c3ec466 13512+ aufs_bindex_t bindex;
1facf9fc 13513+ struct super_block *sb, *h_sb;
0c3ec466
AM
13514+ struct dentry *dentry, *parent, *h_parent;
13515+ struct inode *h_dir;
1facf9fc 13516+ struct au_branch *br;
13517+
1facf9fc 13518+ err = -ENOSPC;
13519+ if (unlikely(*max_len <= Fh_tail)) {
13520+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13521+ goto out;
13522+ }
13523+
13524+ err = FILEID_ROOT;
0c3ec466
AM
13525+ if (inode->i_ino == AUFS_ROOT_INO) {
13526+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13527+ goto out;
13528+ }
13529+
1facf9fc 13530+ h_parent = NULL;
0c3ec466
AM
13531+ sb = inode->i_sb;
13532+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13533+ if (unlikely(err))
13534+ goto out;
13535+
1facf9fc 13536+#ifdef CONFIG_AUFS_DEBUG
13537+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13538+ AuWarn1("NFS-exporting requires xino\n");
13539+#endif
027c5e7a 13540+ err = -EIO;
0c3ec466
AM
13541+ parent = NULL;
13542+ ii_read_lock_child(inode);
5afbbe0d 13543+ bindex = au_ibtop(inode);
0c3ec466 13544+ if (!dir) {
c1595e42 13545+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13546+ if (unlikely(!dentry))
13547+ goto out_unlock;
13548+ AuDebugOn(au_test_anon(dentry));
13549+ parent = dget_parent(dentry);
13550+ dput(dentry);
13551+ if (unlikely(!parent))
13552+ goto out_unlock;
5527c038
JR
13553+ if (d_really_is_positive(parent))
13554+ dir = d_inode(parent);
1facf9fc 13555+ }
0c3ec466
AM
13556+
13557+ ii_read_lock_parent(dir);
13558+ h_dir = au_h_iptr(dir, bindex);
13559+ ii_read_unlock(dir);
13560+ if (unlikely(!h_dir))
13561+ goto out_parent;
c1595e42 13562+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13563+ if (unlikely(!h_parent))
0c3ec466 13564+ goto out_hparent;
1facf9fc 13565+
13566+ err = -EPERM;
13567+ br = au_sbr(sb, bindex);
86dc4139 13568+ h_sb = au_br_sb(br);
1facf9fc 13569+ if (unlikely(!h_sb->s_export_op)) {
13570+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13571+ goto out_hparent;
1facf9fc 13572+ }
13573+
13574+ fh[Fh_br_id] = br->br_id;
13575+ fh[Fh_sigen] = au_sigen(sb);
13576+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13577+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13578+ fh[Fh_igen] = inode->i_generation;
13579+
13580+ *max_len -= Fh_tail;
13581+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13582+ max_len,
13583+ /*connectable or subtreecheck*/0);
13584+ err = fh[Fh_h_type];
13585+ *max_len += Fh_tail;
13586+ /* todo: macros? */
1716fcea 13587+ if (err != FILEID_INVALID)
1facf9fc 13588+ err = 99;
13589+ else
13590+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13591+
0c3ec466 13592+out_hparent:
1facf9fc 13593+ dput(h_parent);
0c3ec466 13594+out_parent:
1facf9fc 13595+ dput(parent);
0c3ec466
AM
13596+out_unlock:
13597+ ii_read_unlock(inode);
13598+ si_read_unlock(sb);
4f0767ce 13599+out:
1facf9fc 13600+ if (unlikely(err < 0))
1716fcea 13601+ err = FILEID_INVALID;
1facf9fc 13602+ return err;
13603+}
13604+
13605+/* ---------------------------------------------------------------------- */
13606+
4a4d8108
AM
13607+static int aufs_commit_metadata(struct inode *inode)
13608+{
13609+ int err;
13610+ aufs_bindex_t bindex;
13611+ struct super_block *sb;
13612+ struct inode *h_inode;
13613+ int (*f)(struct inode *inode);
13614+
13615+ sb = inode->i_sb;
e49829fe 13616+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13617+ ii_write_lock_child(inode);
5afbbe0d 13618+ bindex = au_ibtop(inode);
4a4d8108
AM
13619+ AuDebugOn(bindex < 0);
13620+ h_inode = au_h_iptr(inode, bindex);
13621+
13622+ f = h_inode->i_sb->s_export_op->commit_metadata;
13623+ if (f)
13624+ err = f(h_inode);
13625+ else {
13626+ struct writeback_control wbc = {
13627+ .sync_mode = WB_SYNC_ALL,
13628+ .nr_to_write = 0 /* metadata only */
13629+ };
13630+
13631+ err = sync_inode(h_inode, &wbc);
13632+ }
13633+
13634+ au_cpup_attr_timesizes(inode);
13635+ ii_write_unlock(inode);
13636+ si_read_unlock(sb);
13637+ return err;
13638+}
13639+
13640+/* ---------------------------------------------------------------------- */
13641+
1facf9fc 13642+static struct export_operations aufs_export_op = {
4a4d8108 13643+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13644+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13645+ .encode_fh = aufs_encode_fh,
13646+ .commit_metadata = aufs_commit_metadata
1facf9fc 13647+};
13648+
13649+void au_export_init(struct super_block *sb)
13650+{
13651+ struct au_sbinfo *sbinfo;
13652+ __u32 u;
13653+
5afbbe0d
AM
13654+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13655+ && IS_MODULE(CONFIG_EXPORTFS),
13656+ AUFS_NAME ": unsupported configuration "
13657+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13658+
1facf9fc 13659+ sb->s_export_op = &aufs_export_op;
13660+ sbinfo = au_sbi(sb);
13661+ sbinfo->si_xigen = NULL;
13662+ get_random_bytes(&u, sizeof(u));
13663+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13664+ atomic_set(&sbinfo->si_xigen_next, u);
13665+}
076b876e
AM
13666diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13667--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
b00004a5
AM
13668+++ linux/fs/aufs/fhsm.c 2018-06-04 09:08:09.184746078 +0200
13669@@ -0,0 +1,426 @@
076b876e 13670+/*
b00004a5 13671+ * Copyright (C) 2011-2018 Junjiro R. Okajima
076b876e
AM
13672+ *
13673+ * This program, aufs is free software; you can redistribute it and/or modify
13674+ * it under the terms of the GNU General Public License as published by
13675+ * the Free Software Foundation; either version 2 of the License, or
13676+ * (at your option) any later version.
13677+ *
13678+ * This program is distributed in the hope that it will be useful,
13679+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13680+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13681+ * GNU General Public License for more details.
13682+ *
13683+ * You should have received a copy of the GNU General Public License
13684+ * along with this program; if not, write to the Free Software
13685+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13686+ */
13687+
13688+/*
13689+ * File-based Hierarchy Storage Management
13690+ */
13691+
13692+#include <linux/anon_inodes.h>
13693+#include <linux/poll.h>
13694+#include <linux/seq_file.h>
13695+#include <linux/statfs.h>
13696+#include "aufs.h"
13697+
c1595e42
JR
13698+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13699+{
13700+ struct au_sbinfo *sbinfo;
13701+ struct au_fhsm *fhsm;
13702+
13703+ SiMustAnyLock(sb);
13704+
13705+ sbinfo = au_sbi(sb);
13706+ fhsm = &sbinfo->si_fhsm;
13707+ AuDebugOn(!fhsm);
13708+ return fhsm->fhsm_bottom;
13709+}
13710+
13711+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13712+{
13713+ struct au_sbinfo *sbinfo;
13714+ struct au_fhsm *fhsm;
13715+
13716+ SiMustWriteLock(sb);
13717+
13718+ sbinfo = au_sbi(sb);
13719+ fhsm = &sbinfo->si_fhsm;
13720+ AuDebugOn(!fhsm);
13721+ fhsm->fhsm_bottom = bindex;
13722+}
13723+
13724+/* ---------------------------------------------------------------------- */
13725+
076b876e
AM
13726+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13727+{
13728+ struct au_br_fhsm *bf;
13729+
13730+ bf = br->br_fhsm;
13731+ MtxMustLock(&bf->bf_lock);
13732+
13733+ return !bf->bf_readable
13734+ || time_after(jiffies,
13735+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13736+}
13737+
13738+/* ---------------------------------------------------------------------- */
13739+
13740+static void au_fhsm_notify(struct super_block *sb, int val)
13741+{
13742+ struct au_sbinfo *sbinfo;
13743+ struct au_fhsm *fhsm;
13744+
13745+ SiMustAnyLock(sb);
13746+
13747+ sbinfo = au_sbi(sb);
13748+ fhsm = &sbinfo->si_fhsm;
13749+ if (au_fhsm_pid(fhsm)
13750+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13751+ atomic_set(&fhsm->fhsm_readable, val);
13752+ if (val)
13753+ wake_up(&fhsm->fhsm_wqh);
13754+ }
13755+}
13756+
13757+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13758+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13759+{
13760+ int err;
13761+ struct au_branch *br;
13762+ struct au_br_fhsm *bf;
13763+
13764+ br = au_sbr(sb, bindex);
13765+ AuDebugOn(au_br_rdonly(br));
13766+ bf = br->br_fhsm;
13767+ AuDebugOn(!bf);
13768+
13769+ if (do_lock)
13770+ mutex_lock(&bf->bf_lock);
13771+ else
13772+ MtxMustLock(&bf->bf_lock);
13773+
13774+ /* sb->s_root for NFS is unreliable */
13775+ err = au_br_stfs(br, &bf->bf_stfs);
13776+ if (unlikely(err)) {
13777+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13778+ goto out;
13779+ }
13780+
13781+ bf->bf_jiffy = jiffies;
13782+ bf->bf_readable = 1;
13783+ if (do_notify)
13784+ au_fhsm_notify(sb, /*val*/1);
13785+ if (rstfs)
13786+ *rstfs = bf->bf_stfs;
13787+
13788+out:
13789+ if (do_lock)
13790+ mutex_unlock(&bf->bf_lock);
13791+ au_fhsm_notify(sb, /*val*/1);
13792+
13793+ return err;
13794+}
13795+
13796+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13797+{
13798+ int err;
076b876e
AM
13799+ struct au_sbinfo *sbinfo;
13800+ struct au_fhsm *fhsm;
13801+ struct au_branch *br;
13802+ struct au_br_fhsm *bf;
13803+
13804+ AuDbg("b%d, force %d\n", bindex, force);
13805+ SiMustAnyLock(sb);
13806+
13807+ sbinfo = au_sbi(sb);
13808+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13809+ if (!au_ftest_si(sbinfo, FHSM)
13810+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13811+ return;
13812+
13813+ br = au_sbr(sb, bindex);
13814+ bf = br->br_fhsm;
13815+ AuDebugOn(!bf);
13816+ mutex_lock(&bf->bf_lock);
13817+ if (force
13818+ || au_fhsm_pid(fhsm)
13819+ || au_fhsm_test_jiffy(sbinfo, br))
13820+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13821+ /*do_notify*/1);
13822+ mutex_unlock(&bf->bf_lock);
13823+}
13824+
13825+void au_fhsm_wrote_all(struct super_block *sb, int force)
13826+{
5afbbe0d 13827+ aufs_bindex_t bindex, bbot;
076b876e
AM
13828+ struct au_branch *br;
13829+
13830+ /* exclude the bottom */
5afbbe0d
AM
13831+ bbot = au_fhsm_bottom(sb);
13832+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13833+ br = au_sbr(sb, bindex);
13834+ if (au_br_fhsm(br->br_perm))
13835+ au_fhsm_wrote(sb, bindex, force);
13836+ }
13837+}
13838+
13839+/* ---------------------------------------------------------------------- */
13840+
be118d29 13841+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13842+{
be118d29 13843+ __poll_t mask;
076b876e
AM
13844+ struct au_sbinfo *sbinfo;
13845+ struct au_fhsm *fhsm;
13846+
13847+ mask = 0;
13848+ sbinfo = file->private_data;
13849+ fhsm = &sbinfo->si_fhsm;
13850+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13851+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13852+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13853+
b00004a5
AM
13854+ if (!mask)
13855+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13856+ return mask;
13857+}
13858+
13859+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13860+ struct aufs_stfs *stfs, __s16 brid)
13861+{
13862+ int err;
13863+
13864+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13865+ if (!err)
13866+ err = __put_user(brid, &stbr->brid);
13867+ if (unlikely(err))
13868+ err = -EFAULT;
13869+
13870+ return err;
13871+}
13872+
13873+static ssize_t au_fhsm_do_read(struct super_block *sb,
13874+ struct aufs_stbr __user *stbr, size_t count)
13875+{
13876+ ssize_t err;
13877+ int nstbr;
5afbbe0d 13878+ aufs_bindex_t bindex, bbot;
076b876e
AM
13879+ struct au_branch *br;
13880+ struct au_br_fhsm *bf;
13881+
13882+ /* except the bottom branch */
13883+ err = 0;
13884+ nstbr = 0;
5afbbe0d
AM
13885+ bbot = au_fhsm_bottom(sb);
13886+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
13887+ br = au_sbr(sb, bindex);
13888+ if (!au_br_fhsm(br->br_perm))
13889+ continue;
13890+
13891+ bf = br->br_fhsm;
13892+ mutex_lock(&bf->bf_lock);
13893+ if (bf->bf_readable) {
13894+ err = -EFAULT;
13895+ if (count >= sizeof(*stbr))
13896+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13897+ br->br_id);
13898+ if (!err) {
13899+ bf->bf_readable = 0;
13900+ count -= sizeof(*stbr);
13901+ nstbr++;
13902+ }
13903+ }
13904+ mutex_unlock(&bf->bf_lock);
13905+ }
13906+ if (!err)
13907+ err = sizeof(*stbr) * nstbr;
13908+
13909+ return err;
13910+}
13911+
13912+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13913+ loff_t *pos)
13914+{
13915+ ssize_t err;
13916+ int readable;
5afbbe0d 13917+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
13918+ struct au_sbinfo *sbinfo;
13919+ struct au_fhsm *fhsm;
13920+ struct au_branch *br;
13921+ struct super_block *sb;
13922+
13923+ err = 0;
13924+ sbinfo = file->private_data;
13925+ fhsm = &sbinfo->si_fhsm;
13926+need_data:
13927+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
13928+ if (!atomic_read(&fhsm->fhsm_readable)) {
13929+ if (vfsub_file_flags(file) & O_NONBLOCK)
13930+ err = -EAGAIN;
13931+ else
13932+ err = wait_event_interruptible_locked_irq
13933+ (fhsm->fhsm_wqh,
13934+ atomic_read(&fhsm->fhsm_readable));
13935+ }
13936+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13937+ if (unlikely(err))
13938+ goto out;
13939+
13940+ /* sb may already be dead */
13941+ au_rw_read_lock(&sbinfo->si_rwsem);
13942+ readable = atomic_read(&fhsm->fhsm_readable);
13943+ if (readable > 0) {
13944+ sb = sbinfo->si_sb;
13945+ AuDebugOn(!sb);
13946+ /* exclude the bottom branch */
13947+ nfhsm = 0;
5afbbe0d
AM
13948+ bbot = au_fhsm_bottom(sb);
13949+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13950+ br = au_sbr(sb, bindex);
13951+ if (au_br_fhsm(br->br_perm))
13952+ nfhsm++;
13953+ }
13954+ err = -EMSGSIZE;
13955+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13956+ atomic_set(&fhsm->fhsm_readable, 0);
13957+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13958+ count);
13959+ }
13960+ }
13961+ au_rw_read_unlock(&sbinfo->si_rwsem);
13962+ if (!readable)
13963+ goto need_data;
13964+
13965+out:
13966+ return err;
13967+}
13968+
13969+static int au_fhsm_release(struct inode *inode, struct file *file)
13970+{
13971+ struct au_sbinfo *sbinfo;
13972+ struct au_fhsm *fhsm;
13973+
13974+ /* sb may already be dead */
13975+ sbinfo = file->private_data;
13976+ fhsm = &sbinfo->si_fhsm;
13977+ spin_lock(&fhsm->fhsm_spin);
13978+ fhsm->fhsm_pid = 0;
13979+ spin_unlock(&fhsm->fhsm_spin);
13980+ kobject_put(&sbinfo->si_kobj);
13981+
13982+ return 0;
13983+}
13984+
13985+static const struct file_operations au_fhsm_fops = {
13986+ .owner = THIS_MODULE,
13987+ .llseek = noop_llseek,
13988+ .read = au_fhsm_read,
13989+ .poll = au_fhsm_poll,
13990+ .release = au_fhsm_release
13991+};
13992+
13993+int au_fhsm_fd(struct super_block *sb, int oflags)
13994+{
13995+ int err, fd;
13996+ struct au_sbinfo *sbinfo;
13997+ struct au_fhsm *fhsm;
13998+
13999+ err = -EPERM;
14000+ if (unlikely(!capable(CAP_SYS_ADMIN)))
14001+ goto out;
14002+
14003+ err = -EINVAL;
14004+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14005+ goto out;
14006+
14007+ err = 0;
14008+ sbinfo = au_sbi(sb);
14009+ fhsm = &sbinfo->si_fhsm;
14010+ spin_lock(&fhsm->fhsm_spin);
14011+ if (!fhsm->fhsm_pid)
14012+ fhsm->fhsm_pid = current->pid;
14013+ else
14014+ err = -EBUSY;
14015+ spin_unlock(&fhsm->fhsm_spin);
14016+ if (unlikely(err))
14017+ goto out;
14018+
14019+ oflags |= O_RDONLY;
14020+ /* oflags |= FMODE_NONOTIFY; */
14021+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14022+ err = fd;
14023+ if (unlikely(fd < 0))
14024+ goto out_pid;
14025+
14026+ /* succeed reglardless 'fhsm' status */
14027+ kobject_get(&sbinfo->si_kobj);
14028+ si_noflush_read_lock(sb);
14029+ if (au_ftest_si(sbinfo, FHSM))
14030+ au_fhsm_wrote_all(sb, /*force*/0);
14031+ si_read_unlock(sb);
14032+ goto out; /* success */
14033+
14034+out_pid:
14035+ spin_lock(&fhsm->fhsm_spin);
14036+ fhsm->fhsm_pid = 0;
14037+ spin_unlock(&fhsm->fhsm_spin);
14038+out:
14039+ AuTraceErr(err);
14040+ return err;
14041+}
14042+
14043+/* ---------------------------------------------------------------------- */
14044+
14045+int au_fhsm_br_alloc(struct au_branch *br)
14046+{
14047+ int err;
14048+
14049+ err = 0;
14050+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14051+ if (br->br_fhsm)
14052+ au_br_fhsm_init(br->br_fhsm);
14053+ else
14054+ err = -ENOMEM;
14055+
14056+ return err;
14057+}
14058+
14059+/* ---------------------------------------------------------------------- */
14060+
14061+void au_fhsm_fin(struct super_block *sb)
14062+{
14063+ au_fhsm_notify(sb, /*val*/-1);
14064+}
14065+
14066+void au_fhsm_init(struct au_sbinfo *sbinfo)
14067+{
14068+ struct au_fhsm *fhsm;
14069+
14070+ fhsm = &sbinfo->si_fhsm;
14071+ spin_lock_init(&fhsm->fhsm_spin);
14072+ init_waitqueue_head(&fhsm->fhsm_wqh);
14073+ atomic_set(&fhsm->fhsm_readable, 0);
14074+ fhsm->fhsm_expire
14075+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14076+ fhsm->fhsm_bottom = -1;
076b876e
AM
14077+}
14078+
14079+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14080+{
14081+ sbinfo->si_fhsm.fhsm_expire
14082+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14083+}
14084+
14085+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14086+{
14087+ unsigned int u;
14088+
14089+ if (!au_ftest_si(sbinfo, FHSM))
14090+ return;
14091+
14092+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14093+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14094+ seq_printf(seq, ",fhsm_sec=%u", u);
14095+}
7f207e10
AM
14096diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14097--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 14098+++ linux/fs/aufs/file.c 2018-06-04 09:08:09.184746078 +0200
8b6a4947 14099@@ -0,0 +1,856 @@
1facf9fc 14100+/*
b00004a5 14101+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 14102+ *
14103+ * This program, aufs is free software; you can redistribute it and/or modify
14104+ * it under the terms of the GNU General Public License as published by
14105+ * the Free Software Foundation; either version 2 of the License, or
14106+ * (at your option) any later version.
dece6358
AM
14107+ *
14108+ * This program is distributed in the hope that it will be useful,
14109+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14110+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14111+ * GNU General Public License for more details.
14112+ *
14113+ * You should have received a copy of the GNU General Public License
523b37e3 14114+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14115+ */
14116+
14117+/*
4a4d8108 14118+ * handling file/dir, and address_space operation
1facf9fc 14119+ */
14120+
7eafdf33
AM
14121+#ifdef CONFIG_AUFS_DEBUG
14122+#include <linux/migrate.h>
14123+#endif
4a4d8108 14124+#include <linux/pagemap.h>
1facf9fc 14125+#include "aufs.h"
14126+
4a4d8108
AM
14127+/* drop flags for writing */
14128+unsigned int au_file_roflags(unsigned int flags)
14129+{
14130+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14131+ flags |= O_RDONLY | O_NOATIME;
14132+ return flags;
14133+}
14134+
14135+/* common functions to regular file and dir */
14136+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14137+ struct file *file, int force_wr)
1facf9fc 14138+{
1308ab2a 14139+ struct file *h_file;
4a4d8108
AM
14140+ struct dentry *h_dentry;
14141+ struct inode *h_inode;
14142+ struct super_block *sb;
14143+ struct au_branch *br;
14144+ struct path h_path;
b912730e 14145+ int err;
1facf9fc 14146+
4a4d8108
AM
14147+ /* a race condition can happen between open and unlink/rmdir */
14148+ h_file = ERR_PTR(-ENOENT);
14149+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14150+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14151+ goto out;
5527c038 14152+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14153+ spin_lock(&h_dentry->d_lock);
14154+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14155+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14156+ ;
14157+ spin_unlock(&h_dentry->d_lock);
14158+ if (unlikely(err))
4a4d8108 14159+ goto out;
1facf9fc 14160+
4a4d8108
AM
14161+ sb = dentry->d_sb;
14162+ br = au_sbr(sb, bindex);
b912730e
AM
14163+ err = au_br_test_oflag(flags, br);
14164+ h_file = ERR_PTR(err);
14165+ if (unlikely(err))
027c5e7a 14166+ goto out;
1facf9fc 14167+
4a4d8108 14168+ /* drop flags for writing */
5527c038 14169+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14170+ if (force_wr && !(flags & O_WRONLY))
14171+ force_wr = 0;
4a4d8108 14172+ flags = au_file_roflags(flags);
392086de
AM
14173+ if (force_wr) {
14174+ h_file = ERR_PTR(-EROFS);
14175+ flags = au_file_roflags(flags);
14176+ if (unlikely(vfsub_native_ro(h_inode)
14177+ || IS_APPEND(h_inode)))
14178+ goto out;
14179+ flags &= ~O_ACCMODE;
14180+ flags |= O_WRONLY;
14181+ }
14182+ }
4a4d8108 14183+ flags &= ~O_CREAT;
5afbbe0d 14184+ au_br_get(br);
4a4d8108 14185+ h_path.dentry = h_dentry;
86dc4139 14186+ h_path.mnt = au_br_mnt(br);
38d290e6 14187+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14188+ if (IS_ERR(h_file))
14189+ goto out_br;
dece6358 14190+
b912730e 14191+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14192+ err = deny_write_access(h_file);
14193+ if (unlikely(err)) {
14194+ fput(h_file);
14195+ h_file = ERR_PTR(err);
14196+ goto out_br;
14197+ }
14198+ }
953406b4 14199+ fsnotify_open(h_file);
4a4d8108 14200+ goto out; /* success */
1facf9fc 14201+
4f0767ce 14202+out_br:
5afbbe0d 14203+ au_br_put(br);
4f0767ce 14204+out:
4a4d8108
AM
14205+ return h_file;
14206+}
1308ab2a 14207+
076b876e
AM
14208+static int au_cmoo(struct dentry *dentry)
14209+{
8b6a4947 14210+ int err, cmoo, matched;
076b876e
AM
14211+ unsigned int udba;
14212+ struct path h_path;
14213+ struct au_pin pin;
14214+ struct au_cp_generic cpg = {
14215+ .dentry = dentry,
14216+ .bdst = -1,
14217+ .bsrc = -1,
14218+ .len = -1,
14219+ .pin = &pin,
14220+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14221+ };
7e9cd9fe 14222+ struct inode *delegated;
076b876e
AM
14223+ struct super_block *sb;
14224+ struct au_sbinfo *sbinfo;
14225+ struct au_fhsm *fhsm;
14226+ pid_t pid;
14227+ struct au_branch *br;
14228+ struct dentry *parent;
14229+ struct au_hinode *hdir;
14230+
14231+ DiMustWriteLock(dentry);
5527c038 14232+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14233+
14234+ err = 0;
14235+ if (IS_ROOT(dentry))
14236+ goto out;
5afbbe0d 14237+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14238+ if (!cpg.bsrc)
14239+ goto out;
14240+
14241+ sb = dentry->d_sb;
14242+ sbinfo = au_sbi(sb);
14243+ fhsm = &sbinfo->si_fhsm;
14244+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14245+ rcu_read_lock();
14246+ matched = (pid
14247+ && (current->pid == pid
14248+ || rcu_dereference(current->real_parent)->pid == pid));
14249+ rcu_read_unlock();
14250+ if (matched)
076b876e
AM
14251+ goto out;
14252+
14253+ br = au_sbr(sb, cpg.bsrc);
14254+ cmoo = au_br_cmoo(br->br_perm);
14255+ if (!cmoo)
14256+ goto out;
7e9cd9fe 14257+ if (!d_is_reg(dentry))
076b876e
AM
14258+ cmoo &= AuBrAttr_COO_ALL;
14259+ if (!cmoo)
14260+ goto out;
14261+
14262+ parent = dget_parent(dentry);
14263+ di_write_lock_parent(parent);
14264+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14265+ cpg.bdst = err;
14266+ if (unlikely(err < 0)) {
14267+ err = 0; /* there is no upper writable branch */
14268+ goto out_dgrade;
14269+ }
14270+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14271+
14272+ /* do not respect the coo attrib for the target branch */
14273+ err = au_cpup_dirs(dentry, cpg.bdst);
14274+ if (unlikely(err))
14275+ goto out_dgrade;
14276+
14277+ di_downgrade_lock(parent, AuLock_IR);
14278+ udba = au_opt_udba(sb);
14279+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14280+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14281+ if (unlikely(err))
14282+ goto out_parent;
14283+
14284+ err = au_sio_cpup_simple(&cpg);
14285+ au_unpin(&pin);
14286+ if (unlikely(err))
14287+ goto out_parent;
14288+ if (!(cmoo & AuBrWAttr_MOO))
14289+ goto out_parent; /* success */
14290+
14291+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14292+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14293+ if (unlikely(err))
14294+ goto out_parent;
14295+
14296+ h_path.mnt = au_br_mnt(br);
14297+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14298+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14299+ delegated = NULL;
14300+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14301+ au_unpin(&pin);
14302+ /* todo: keep h_dentry or not? */
14303+ if (unlikely(err == -EWOULDBLOCK)) {
14304+ pr_warn("cannot retry for NFSv4 delegation"
14305+ " for an internal unlink\n");
14306+ iput(delegated);
14307+ }
14308+ if (unlikely(err)) {
14309+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14310+ dentry, err);
14311+ err = 0;
14312+ }
14313+ goto out_parent; /* success */
14314+
14315+out_dgrade:
14316+ di_downgrade_lock(parent, AuLock_IR);
14317+out_parent:
14318+ di_read_unlock(parent, AuLock_IR);
14319+ dput(parent);
14320+out:
14321+ AuTraceErr(err);
14322+ return err;
14323+}
14324+
b912730e 14325+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14326+{
8b6a4947 14327+ int err, aopen = args->aopen;
1facf9fc 14328+ struct dentry *dentry;
076b876e 14329+ struct au_finfo *finfo;
1308ab2a 14330+
8b6a4947 14331+ if (!aopen)
b912730e
AM
14332+ err = au_finfo_init(file, args->fidir);
14333+ else {
14334+ lockdep_off();
14335+ err = au_finfo_init(file, args->fidir);
14336+ lockdep_on();
14337+ }
4a4d8108
AM
14338+ if (unlikely(err))
14339+ goto out;
1facf9fc 14340+
2000de60 14341+ dentry = file->f_path.dentry;
b912730e 14342+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14343+ di_write_lock_child(dentry);
14344+ err = au_cmoo(dentry);
14345+ di_downgrade_lock(dentry, AuLock_IR);
14346+ if (!err) {
14347+ if (!aopen)
b912730e 14348+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14349+ else {
14350+ lockdep_off();
14351+ err = args->open(file, vfsub_file_flags(file), NULL);
14352+ lockdep_on();
14353+ }
b912730e 14354+ }
8b6a4947 14355+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14356+
076b876e
AM
14357+ finfo = au_fi(file);
14358+ if (!err) {
14359+ finfo->fi_file = file;
8b6a4947
AM
14360+ au_hbl_add(&finfo->fi_hlist,
14361+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14362+ }
8b6a4947 14363+ if (!aopen)
b912730e
AM
14364+ fi_write_unlock(file);
14365+ else {
14366+ lockdep_off();
14367+ fi_write_unlock(file);
14368+ lockdep_on();
14369+ }
4a4d8108 14370+ if (unlikely(err)) {
076b876e 14371+ finfo->fi_hdir = NULL;
1c60b727 14372+ au_finfo_fin(file);
1308ab2a 14373+ }
4a4d8108 14374+
4f0767ce 14375+out:
8b6a4947 14376+ AuTraceErr(err);
1308ab2a 14377+ return err;
14378+}
dece6358 14379+
4a4d8108 14380+int au_reopen_nondir(struct file *file)
1308ab2a 14381+{
4a4d8108 14382+ int err;
5afbbe0d 14383+ aufs_bindex_t btop;
4a4d8108
AM
14384+ struct dentry *dentry;
14385+ struct file *h_file, *h_file_tmp;
1308ab2a 14386+
2000de60 14387+ dentry = file->f_path.dentry;
5afbbe0d 14388+ btop = au_dbtop(dentry);
4a4d8108 14389+ h_file_tmp = NULL;
5afbbe0d 14390+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14391+ h_file = au_hf_top(file);
14392+ if (file->f_mode == h_file->f_mode)
14393+ return 0; /* success */
14394+ h_file_tmp = h_file;
14395+ get_file(h_file_tmp);
5afbbe0d 14396+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14397+ }
14398+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14399+ /*
14400+ * it can happen
14401+ * file exists on both of rw and ro
5afbbe0d 14402+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14403+ * prepend a branch as rw, "rw" become ro
14404+ * remove rw/file
14405+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14406+ * --> dbtop is 1, fbtop is still 0
14407+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14408+ */
5afbbe0d 14409+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14410+
5afbbe0d 14411+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14412+ file, /*force_wr*/0);
4a4d8108 14413+ err = PTR_ERR(h_file);
86dc4139
AM
14414+ if (IS_ERR(h_file)) {
14415+ if (h_file_tmp) {
5afbbe0d
AM
14416+ au_sbr_get(dentry->d_sb, btop);
14417+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14418+ h_file_tmp = NULL;
14419+ }
4a4d8108 14420+ goto out; /* todo: close all? */
86dc4139 14421+ }
4a4d8108
AM
14422+
14423+ err = 0;
5afbbe0d
AM
14424+ au_set_fbtop(file, btop);
14425+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14426+ au_update_figen(file);
14427+ /* todo: necessary? */
14428+ /* file->f_ra = h_file->f_ra; */
14429+
4f0767ce 14430+out:
4a4d8108
AM
14431+ if (h_file_tmp)
14432+ fput(h_file_tmp);
14433+ return err;
1facf9fc 14434+}
14435+
1308ab2a 14436+/* ---------------------------------------------------------------------- */
14437+
4a4d8108
AM
14438+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14439+ struct dentry *hi_wh)
1facf9fc 14440+{
4a4d8108 14441+ int err;
5afbbe0d 14442+ aufs_bindex_t btop;
4a4d8108
AM
14443+ struct au_dinfo *dinfo;
14444+ struct dentry *h_dentry;
14445+ struct au_hdentry *hdp;
1facf9fc 14446+
2000de60 14447+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14448+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14449+
5afbbe0d
AM
14450+ btop = dinfo->di_btop;
14451+ dinfo->di_btop = btgt;
14452+ hdp = au_hdentry(dinfo, btgt);
14453+ h_dentry = hdp->hd_dentry;
14454+ hdp->hd_dentry = hi_wh;
4a4d8108 14455+ err = au_reopen_nondir(file);
5afbbe0d
AM
14456+ hdp->hd_dentry = h_dentry;
14457+ dinfo->di_btop = btop;
1facf9fc 14458+
1facf9fc 14459+ return err;
14460+}
14461+
4a4d8108 14462+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14463+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14464+{
4a4d8108 14465+ int err;
027c5e7a 14466+ struct inode *inode, *h_inode;
c2b27bf2
AM
14467+ struct dentry *h_dentry, *hi_wh;
14468+ struct au_cp_generic cpg = {
2000de60 14469+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14470+ .bdst = bcpup,
14471+ .bsrc = -1,
14472+ .len = len,
14473+ .pin = pin
14474+ };
1facf9fc 14475+
5afbbe0d 14476+ au_update_dbtop(cpg.dentry);
5527c038 14477+ inode = d_inode(cpg.dentry);
027c5e7a 14478+ h_inode = NULL;
5afbbe0d
AM
14479+ if (au_dbtop(cpg.dentry) <= bcpup
14480+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14481+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14482+ if (h_dentry && d_is_positive(h_dentry))
14483+ h_inode = d_inode(h_dentry);
027c5e7a 14484+ }
4a4d8108 14485+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14486+ if (!hi_wh && !h_inode)
c2b27bf2 14487+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14488+ else
14489+ /* already copied-up after unlink */
14490+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14491+
4a4d8108 14492+ if (!err
38d290e6
JR
14493+ && (inode->i_nlink > 1
14494+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14495+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14496+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14497+
dece6358 14498+ return err;
1facf9fc 14499+}
14500+
4a4d8108
AM
14501+/*
14502+ * prepare the @file for writing.
14503+ */
14504+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14505+{
4a4d8108 14506+ int err;
5afbbe0d 14507+ aufs_bindex_t dbtop;
c1595e42 14508+ struct dentry *parent;
86dc4139 14509+ struct inode *inode;
1facf9fc 14510+ struct super_block *sb;
4a4d8108 14511+ struct file *h_file;
c2b27bf2 14512+ struct au_cp_generic cpg = {
2000de60 14513+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14514+ .bdst = -1,
14515+ .bsrc = -1,
14516+ .len = len,
14517+ .pin = pin,
14518+ .flags = AuCpup_DTIME
14519+ };
1facf9fc 14520+
c2b27bf2 14521+ sb = cpg.dentry->d_sb;
5527c038 14522+ inode = d_inode(cpg.dentry);
5afbbe0d 14523+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14524+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14525+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14526+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14527+ /*flags*/0);
1facf9fc 14528+ goto out;
4a4d8108 14529+ }
1facf9fc 14530+
027c5e7a 14531+ /* need to cpup or reopen */
c2b27bf2 14532+ parent = dget_parent(cpg.dentry);
4a4d8108 14533+ di_write_lock_parent(parent);
c2b27bf2
AM
14534+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14535+ cpg.bdst = err;
4a4d8108
AM
14536+ if (unlikely(err < 0))
14537+ goto out_dgrade;
14538+ err = 0;
14539+
c2b27bf2
AM
14540+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14541+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14542+ if (unlikely(err))
4a4d8108
AM
14543+ goto out_dgrade;
14544+ }
14545+
c2b27bf2 14546+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14547+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14548+ if (unlikely(err))
14549+ goto out_dgrade;
14550+
5afbbe0d
AM
14551+ dbtop = au_dbtop(cpg.dentry);
14552+ if (dbtop <= cpg.bdst)
c2b27bf2 14553+ cpg.bsrc = cpg.bdst;
027c5e7a 14554+
5afbbe0d 14555+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14556+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14557+ ) {
392086de 14558+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14559+ if (IS_ERR(h_file))
027c5e7a 14560+ err = PTR_ERR(h_file);
86dc4139 14561+ else {
027c5e7a 14562+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14563+ if (dbtop > cpg.bdst)
c2b27bf2 14564+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14565+ if (!err)
14566+ err = au_reopen_nondir(file);
c2b27bf2 14567+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14568+ }
027c5e7a
AM
14569+ } else { /* copyup as wh and reopen */
14570+ /*
14571+ * since writable hfsplus branch is not supported,
14572+ * h_open_pre/post() are unnecessary.
14573+ */
c2b27bf2 14574+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14575+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14576+ }
4a4d8108
AM
14577+
14578+ if (!err) {
14579+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14580+ goto out_dput; /* success */
14581+ }
14582+ au_unpin(pin);
14583+ goto out_unlock;
1facf9fc 14584+
4f0767ce 14585+out_dgrade:
4a4d8108 14586+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14587+out_unlock:
4a4d8108 14588+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14589+out_dput:
4a4d8108 14590+ dput(parent);
4f0767ce 14591+out:
1facf9fc 14592+ return err;
14593+}
14594+
4a4d8108
AM
14595+/* ---------------------------------------------------------------------- */
14596+
14597+int au_do_flush(struct file *file, fl_owner_t id,
14598+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14599+{
4a4d8108 14600+ int err;
1facf9fc 14601+ struct super_block *sb;
4a4d8108 14602+ struct inode *inode;
1facf9fc 14603+
c06a8ce3
AM
14604+ inode = file_inode(file);
14605+ sb = inode->i_sb;
4a4d8108
AM
14606+ si_noflush_read_lock(sb);
14607+ fi_read_lock(file);
b752ccd1 14608+ ii_read_lock_child(inode);
1facf9fc 14609+
4a4d8108
AM
14610+ err = flush(file, id);
14611+ au_cpup_attr_timesizes(inode);
1facf9fc 14612+
b752ccd1 14613+ ii_read_unlock(inode);
4a4d8108 14614+ fi_read_unlock(file);
1308ab2a 14615+ si_read_unlock(sb);
dece6358 14616+ return err;
1facf9fc 14617+}
14618+
4a4d8108
AM
14619+/* ---------------------------------------------------------------------- */
14620+
14621+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14622+{
4a4d8108 14623+ int err;
4a4d8108
AM
14624+ struct au_pin pin;
14625+ struct au_finfo *finfo;
c2b27bf2 14626+ struct dentry *parent, *hi_wh;
4a4d8108 14627+ struct inode *inode;
1facf9fc 14628+ struct super_block *sb;
c2b27bf2 14629+ struct au_cp_generic cpg = {
2000de60 14630+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14631+ .bdst = -1,
14632+ .bsrc = -1,
14633+ .len = -1,
14634+ .pin = &pin,
14635+ .flags = AuCpup_DTIME
14636+ };
1facf9fc 14637+
4a4d8108
AM
14638+ FiMustWriteLock(file);
14639+
14640+ err = 0;
14641+ finfo = au_fi(file);
c2b27bf2 14642+ sb = cpg.dentry->d_sb;
5527c038 14643+ inode = d_inode(cpg.dentry);
5afbbe0d 14644+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14645+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14646+ goto out;
dece6358 14647+
c2b27bf2
AM
14648+ parent = dget_parent(cpg.dentry);
14649+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14650+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14651+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14652+ cpg.bdst = err;
4a4d8108
AM
14653+ di_read_unlock(parent, !AuLock_IR);
14654+ if (unlikely(err < 0))
14655+ goto out_parent;
14656+ err = 0;
1facf9fc 14657+ }
1facf9fc 14658+
4a4d8108 14659+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14660+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14661+ if (!S_ISDIR(inode->i_mode)
14662+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14663+ && au_plink_test(inode)
c2b27bf2 14664+ && !d_unhashed(cpg.dentry)
5afbbe0d 14665+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14666+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14667+ if (unlikely(err))
14668+ goto out_unlock;
14669+
14670+ /* always superio. */
c2b27bf2 14671+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14672+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14673+ if (!err) {
c2b27bf2 14674+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14675+ au_unpin(&pin);
14676+ }
4a4d8108
AM
14677+ } else if (hi_wh) {
14678+ /* already copied-up after unlink */
c2b27bf2 14679+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14680+ *need_reopen = 0;
14681+ }
1facf9fc 14682+
4f0767ce 14683+out_unlock:
4a4d8108 14684+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14685+out_parent:
4a4d8108 14686+ dput(parent);
4f0767ce 14687+out:
1308ab2a 14688+ return err;
dece6358 14689+}
1facf9fc 14690+
4a4d8108 14691+static void au_do_refresh_dir(struct file *file)
dece6358 14692+{
5afbbe0d 14693+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14694+ struct au_hfile *p, tmp, *q;
14695+ struct au_finfo *finfo;
1308ab2a 14696+ struct super_block *sb;
4a4d8108 14697+ struct au_fidir *fidir;
1facf9fc 14698+
4a4d8108 14699+ FiMustWriteLock(file);
1facf9fc 14700+
2000de60 14701+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14702+ finfo = au_fi(file);
14703+ fidir = finfo->fi_hdir;
14704+ AuDebugOn(!fidir);
14705+ p = fidir->fd_hfile + finfo->fi_btop;
14706+ brid = p->hf_br->br_id;
5afbbe0d
AM
14707+ bbot = fidir->fd_bbot;
14708+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14709+ if (!p->hf_file)
14710+ continue;
1308ab2a 14711+
4a4d8108
AM
14712+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14713+ if (new_bindex == bindex)
14714+ continue;
14715+ if (new_bindex < 0) {
14716+ au_set_h_fptr(file, bindex, NULL);
14717+ continue;
14718+ }
1308ab2a 14719+
4a4d8108
AM
14720+ /* swap two lower inode, and loop again */
14721+ q = fidir->fd_hfile + new_bindex;
14722+ tmp = *q;
14723+ *q = *p;
14724+ *p = tmp;
14725+ if (tmp.hf_file) {
14726+ bindex--;
14727+ p--;
14728+ }
14729+ }
1308ab2a 14730+
4a4d8108 14731+ p = fidir->fd_hfile;
2000de60 14732+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14733+ bbot = au_sbbot(sb);
14734+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14735+ finfo->fi_btop++, p++)
14736+ if (p->hf_file) {
c06a8ce3 14737+ if (file_inode(p->hf_file))
4a4d8108 14738+ break;
1c60b727 14739+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14740+ }
14741+ } else {
5afbbe0d
AM
14742+ bbot = au_br_index(sb, brid);
14743+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14744+ finfo->fi_btop++, p++)
14745+ if (p->hf_file)
1c60b727 14746+ au_hfput(p, /*execed*/0);
5afbbe0d 14747+ bbot = au_sbbot(sb);
4a4d8108 14748+ }
1308ab2a 14749+
5afbbe0d
AM
14750+ p = fidir->fd_hfile + bbot;
14751+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14752+ fidir->fd_bbot--, p--)
14753+ if (p->hf_file) {
c06a8ce3 14754+ if (file_inode(p->hf_file))
4a4d8108 14755+ break;
1c60b727 14756+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14757+ }
14758+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14759+}
14760+
4a4d8108
AM
14761+/*
14762+ * after branch manipulating, refresh the file.
14763+ */
14764+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14765+{
e2f27e51 14766+ int err, need_reopen, nbr;
5afbbe0d 14767+ aufs_bindex_t bbot, bindex;
4a4d8108 14768+ struct dentry *dentry;
e2f27e51 14769+ struct super_block *sb;
1308ab2a 14770+ struct au_finfo *finfo;
4a4d8108 14771+ struct au_hfile *hfile;
1facf9fc 14772+
2000de60 14773+ dentry = file->f_path.dentry;
e2f27e51
AM
14774+ sb = dentry->d_sb;
14775+ nbr = au_sbbot(sb) + 1;
1308ab2a 14776+ finfo = au_fi(file);
4a4d8108
AM
14777+ if (!finfo->fi_hdir) {
14778+ hfile = &finfo->fi_htop;
14779+ AuDebugOn(!hfile->hf_file);
e2f27e51 14780+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14781+ AuDebugOn(bindex < 0);
14782+ if (bindex != finfo->fi_btop)
5afbbe0d 14783+ au_set_fbtop(file, bindex);
4a4d8108 14784+ } else {
e2f27e51 14785+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14786+ if (unlikely(err))
14787+ goto out;
14788+ au_do_refresh_dir(file);
14789+ }
1facf9fc 14790+
4a4d8108
AM
14791+ err = 0;
14792+ need_reopen = 1;
14793+ if (!au_test_mmapped(file))
14794+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14795+ if (finfo->fi_hdir)
14796+ /* harmless if err */
14797+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14798+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14799+ err = reopen(file);
14800+ if (!err) {
14801+ au_update_figen(file);
14802+ goto out; /* success */
14803+ }
14804+
14805+ /* error, close all lower files */
14806+ if (finfo->fi_hdir) {
5afbbe0d
AM
14807+ bbot = au_fbbot_dir(file);
14808+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14809+ au_set_h_fptr(file, bindex, NULL);
14810+ }
1facf9fc 14811+
4f0767ce 14812+out:
1facf9fc 14813+ return err;
14814+}
14815+
4a4d8108
AM
14816+/* common function to regular file and dir */
14817+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14818+ int wlock, unsigned int fi_lsc)
dece6358 14819+{
1308ab2a 14820+ int err;
4a4d8108 14821+ unsigned int sigen, figen;
5afbbe0d 14822+ aufs_bindex_t btop;
4a4d8108
AM
14823+ unsigned char pseudo_link;
14824+ struct dentry *dentry;
14825+ struct inode *inode;
1facf9fc 14826+
4a4d8108 14827+ err = 0;
2000de60 14828+ dentry = file->f_path.dentry;
5527c038 14829+ inode = d_inode(dentry);
4a4d8108 14830+ sigen = au_sigen(dentry->d_sb);
521ced18 14831+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14832+ figen = au_figen(file);
521ced18
JR
14833+ if (!fi_lsc)
14834+ di_write_lock_child(dentry);
14835+ else
14836+ di_write_lock_child2(dentry);
5afbbe0d
AM
14837+ btop = au_dbtop(dentry);
14838+ pseudo_link = (btop != au_ibtop(inode));
14839+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14840+ if (!wlock) {
14841+ di_downgrade_lock(dentry, AuLock_IR);
14842+ fi_downgrade_lock(file);
14843+ }
14844+ goto out; /* success */
14845+ }
dece6358 14846+
4a4d8108 14847+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14848+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14849+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14850+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14851+ }
dece6358 14852+
027c5e7a
AM
14853+ if (!err)
14854+ err = refresh_file(file, reopen);
4a4d8108
AM
14855+ if (!err) {
14856+ if (!wlock) {
14857+ di_downgrade_lock(dentry, AuLock_IR);
14858+ fi_downgrade_lock(file);
14859+ }
14860+ } else {
14861+ di_write_unlock(dentry);
14862+ fi_write_unlock(file);
14863+ }
1facf9fc 14864+
4f0767ce 14865+out:
1308ab2a 14866+ return err;
14867+}
1facf9fc 14868+
4a4d8108
AM
14869+/* ---------------------------------------------------------------------- */
14870+
14871+/* cf. aufs_nopage() */
14872+/* for madvise(2) */
14873+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 14874+{
4a4d8108
AM
14875+ unlock_page(page);
14876+ return 0;
14877+}
1facf9fc 14878+
4a4d8108 14879+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 14880+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 14881+{ BUG(); return 0; }
1facf9fc 14882+
4a4d8108
AM
14883+/* they will never be called. */
14884+#ifdef CONFIG_AUFS_DEBUG
14885+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14886+ loff_t pos, unsigned len, unsigned flags,
14887+ struct page **pagep, void **fsdata)
14888+{ AuUnsupport(); return 0; }
14889+static int aufs_write_end(struct file *file, struct address_space *mapping,
14890+ loff_t pos, unsigned len, unsigned copied,
14891+ struct page *page, void *fsdata)
14892+{ AuUnsupport(); return 0; }
14893+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14894+{ AuUnsupport(); return 0; }
1308ab2a 14895+
4a4d8108
AM
14896+static int aufs_set_page_dirty(struct page *page)
14897+{ AuUnsupport(); return 0; }
392086de
AM
14898+static void aufs_invalidatepage(struct page *page, unsigned int offset,
14899+ unsigned int length)
4a4d8108
AM
14900+{ AuUnsupport(); }
14901+static int aufs_releasepage(struct page *page, gfp_t gfp)
14902+{ AuUnsupport(); return 0; }
79b8bda9 14903+#if 0 /* called by memory compaction regardless file */
4a4d8108 14904+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 14905+ struct page *page, enum migrate_mode mode)
4a4d8108 14906+{ AuUnsupport(); return 0; }
79b8bda9 14907+#endif
e2f27e51
AM
14908+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14909+{ AuUnsupport(); return true; }
14910+static void aufs_putback_page(struct page *page)
14911+{ AuUnsupport(); }
4a4d8108
AM
14912+static int aufs_launder_page(struct page *page)
14913+{ AuUnsupport(); return 0; }
14914+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
14915+ unsigned long from,
14916+ unsigned long count)
4a4d8108 14917+{ AuUnsupport(); return 0; }
392086de
AM
14918+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14919+ bool *writeback)
14920+{ AuUnsupport(); }
4a4d8108
AM
14921+static int aufs_error_remove_page(struct address_space *mapping,
14922+ struct page *page)
14923+{ AuUnsupport(); return 0; }
b4510431
AM
14924+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14925+ sector_t *span)
14926+{ AuUnsupport(); return 0; }
14927+static void aufs_swap_deactivate(struct file *file)
14928+{ AuUnsupport(); }
4a4d8108
AM
14929+#endif /* CONFIG_AUFS_DEBUG */
14930+
14931+const struct address_space_operations aufs_aop = {
14932+ .readpage = aufs_readpage,
14933+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
14934+#ifdef CONFIG_AUFS_DEBUG
14935+ .writepage = aufs_writepage,
4a4d8108
AM
14936+ /* no writepages, because of writepage */
14937+ .set_page_dirty = aufs_set_page_dirty,
14938+ /* no readpages, because of readpage */
14939+ .write_begin = aufs_write_begin,
14940+ .write_end = aufs_write_end,
14941+ /* no bmap, no block device */
14942+ .invalidatepage = aufs_invalidatepage,
14943+ .releasepage = aufs_releasepage,
79b8bda9
AM
14944+ /* is fallback_migrate_page ok? */
14945+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
14946+ .isolate_page = aufs_isolate_page,
14947+ .putback_page = aufs_putback_page,
4a4d8108
AM
14948+ .launder_page = aufs_launder_page,
14949+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 14950+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
14951+ .error_remove_page = aufs_error_remove_page,
14952+ .swap_activate = aufs_swap_activate,
14953+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 14954+#endif /* CONFIG_AUFS_DEBUG */
dece6358 14955+};
7f207e10
AM
14956diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14957--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 14958+++ linux/fs/aufs/file.h 2018-06-04 09:08:09.184746078 +0200
8b6a4947 14959@@ -0,0 +1,340 @@
4a4d8108 14960+/*
b00004a5 14961+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
14962+ *
14963+ * This program, aufs is free software; you can redistribute it and/or modify
14964+ * it under the terms of the GNU General Public License as published by
14965+ * the Free Software Foundation; either version 2 of the License, or
14966+ * (at your option) any later version.
14967+ *
14968+ * This program is distributed in the hope that it will be useful,
14969+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14970+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14971+ * GNU General Public License for more details.
14972+ *
14973+ * You should have received a copy of the GNU General Public License
523b37e3 14974+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 14975+ */
1facf9fc 14976+
4a4d8108
AM
14977+/*
14978+ * file operations
14979+ */
1facf9fc 14980+
4a4d8108
AM
14981+#ifndef __AUFS_FILE_H__
14982+#define __AUFS_FILE_H__
1facf9fc 14983+
4a4d8108 14984+#ifdef __KERNEL__
1facf9fc 14985+
2cbb1c4b 14986+#include <linux/file.h>
4a4d8108 14987+#include <linux/fs.h>
3c1bdaff 14988+#include <linux/mm_types.h>
4a4d8108 14989+#include <linux/poll.h>
4a4d8108 14990+#include "rwsem.h"
1facf9fc 14991+
4a4d8108
AM
14992+struct au_branch;
14993+struct au_hfile {
14994+ struct file *hf_file;
14995+ struct au_branch *hf_br;
14996+};
1facf9fc 14997+
4a4d8108
AM
14998+struct au_vdir;
14999+struct au_fidir {
15000+ aufs_bindex_t fd_bbot;
15001+ aufs_bindex_t fd_nent;
15002+ struct au_vdir *fd_vdir_cache;
15003+ struct au_hfile fd_hfile[];
15004+};
1facf9fc 15005+
4a4d8108 15006+static inline int au_fidir_sz(int nent)
dece6358 15007+{
4f0767ce
JR
15008+ AuDebugOn(nent < 0);
15009+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 15010+}
1facf9fc 15011+
4a4d8108
AM
15012+struct au_finfo {
15013+ atomic_t fi_generation;
dece6358 15014+
4a4d8108
AM
15015+ struct au_rwsem fi_rwsem;
15016+ aufs_bindex_t fi_btop;
15017+
15018+ /* do not union them */
15019+ struct { /* for non-dir */
15020+ struct au_hfile fi_htop;
2cbb1c4b 15021+ atomic_t fi_mmapped;
4a4d8108
AM
15022+ };
15023+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15024+
8b6a4947 15025+ struct hlist_bl_node fi_hlist;
1c60b727 15026+ struct file *fi_file; /* very ugly */
4a4d8108 15027+} ____cacheline_aligned_in_smp;
1facf9fc 15028+
4a4d8108 15029+/* ---------------------------------------------------------------------- */
1facf9fc 15030+
4a4d8108
AM
15031+/* file.c */
15032+extern const struct address_space_operations aufs_aop;
15033+unsigned int au_file_roflags(unsigned int flags);
15034+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15035+ struct file *file, int force_wr);
b912730e 15036+struct au_do_open_args {
8b6a4947 15037+ int aopen;
b912730e
AM
15038+ int (*open)(struct file *file, int flags,
15039+ struct file *h_file);
15040+ struct au_fidir *fidir;
15041+ struct file *h_file;
15042+};
15043+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15044+int au_reopen_nondir(struct file *file);
15045+struct au_pin;
15046+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15047+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15048+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15049+int au_do_flush(struct file *file, fl_owner_t id,
15050+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15051+
4a4d8108
AM
15052+/* poll.c */
15053+#ifdef CONFIG_AUFS_POLL
be118d29 15054+__poll_t aufs_poll(struct file *file, poll_table *wait);
4a4d8108 15055+#endif
1facf9fc 15056+
4a4d8108
AM
15057+#ifdef CONFIG_AUFS_BR_HFSPLUS
15058+/* hfsplus.c */
392086de
AM
15059+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15060+ int force_wr);
4a4d8108
AM
15061+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15062+ struct file *h_file);
15063+#else
c1595e42
JR
15064+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15065+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15066+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15067+ struct file *h_file);
15068+#endif
1facf9fc 15069+
4a4d8108
AM
15070+/* f_op.c */
15071+extern const struct file_operations aufs_file_fop;
b912730e 15072+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15073+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15074+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15075+
4a4d8108 15076+/* finfo.c */
f0c0a007 15077+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15078+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15079+ struct file *h_file);
1facf9fc 15080+
4a4d8108 15081+void au_update_figen(struct file *file);
4a4d8108 15082+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15083+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15084+
4a4d8108 15085+void au_fi_init_once(void *_fi);
1c60b727 15086+void au_finfo_fin(struct file *file);
4a4d8108 15087+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15088+
4a4d8108
AM
15089+/* ioctl.c */
15090+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15091+#ifdef CONFIG_COMPAT
15092+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15093+ unsigned long arg);
c2b27bf2
AM
15094+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15095+ unsigned long arg);
b752ccd1 15096+#endif
1facf9fc 15097+
4a4d8108 15098+/* ---------------------------------------------------------------------- */
1facf9fc 15099+
4a4d8108
AM
15100+static inline struct au_finfo *au_fi(struct file *file)
15101+{
38d290e6 15102+ return file->private_data;
4a4d8108 15103+}
1facf9fc 15104+
4a4d8108 15105+/* ---------------------------------------------------------------------- */
1facf9fc 15106+
8b6a4947
AM
15107+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15108+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15109+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15110+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15111+/*
8b6a4947
AM
15112+#define fi_read_trylock_nested(f) \
15113+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15114+#define fi_write_trylock_nested(f) \
15115+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15116+*/
15117+
15118+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15119+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15120+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15121+
521ced18
JR
15122+/* lock subclass for finfo */
15123+enum {
15124+ AuLsc_FI_1,
15125+ AuLsc_FI_2
15126+};
15127+
15128+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15129+{
15130+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15131+}
15132+
15133+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15134+{
15135+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15136+}
15137+
15138+/*
15139+ * fi_read_lock_1, fi_write_lock_1,
15140+ * fi_read_lock_2, fi_write_lock_2
15141+ */
15142+#define AuReadLockFunc(name) \
15143+static inline void fi_read_lock_##name(struct file *f) \
15144+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15145+
15146+#define AuWriteLockFunc(name) \
15147+static inline void fi_write_lock_##name(struct file *f) \
15148+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15149+
15150+#define AuRWLockFuncs(name) \
15151+ AuReadLockFunc(name) \
15152+ AuWriteLockFunc(name)
15153+
15154+AuRWLockFuncs(1);
15155+AuRWLockFuncs(2);
15156+
15157+#undef AuReadLockFunc
15158+#undef AuWriteLockFunc
15159+#undef AuRWLockFuncs
15160+
4a4d8108
AM
15161+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15162+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15163+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15164+
1308ab2a 15165+/* ---------------------------------------------------------------------- */
15166+
4a4d8108 15167+/* todo: hard/soft set? */
5afbbe0d 15168+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15169+{
4a4d8108
AM
15170+ FiMustAnyLock(file);
15171+ return au_fi(file)->fi_btop;
15172+}
dece6358 15173+
5afbbe0d 15174+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15175+{
15176+ FiMustAnyLock(file);
15177+ AuDebugOn(!au_fi(file)->fi_hdir);
15178+ return au_fi(file)->fi_hdir->fd_bbot;
15179+}
1facf9fc 15180+
4a4d8108
AM
15181+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15182+{
15183+ FiMustAnyLock(file);
15184+ AuDebugOn(!au_fi(file)->fi_hdir);
15185+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15186+}
1facf9fc 15187+
5afbbe0d 15188+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15189+{
15190+ FiMustWriteLock(file);
15191+ au_fi(file)->fi_btop = bindex;
15192+}
1facf9fc 15193+
5afbbe0d 15194+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15195+{
15196+ FiMustWriteLock(file);
15197+ AuDebugOn(!au_fi(file)->fi_hdir);
15198+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15199+}
1308ab2a 15200+
4a4d8108
AM
15201+static inline void au_set_fvdir_cache(struct file *file,
15202+ struct au_vdir *vdir_cache)
15203+{
15204+ FiMustWriteLock(file);
15205+ AuDebugOn(!au_fi(file)->fi_hdir);
15206+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15207+}
dece6358 15208+
4a4d8108
AM
15209+static inline struct file *au_hf_top(struct file *file)
15210+{
15211+ FiMustAnyLock(file);
15212+ AuDebugOn(au_fi(file)->fi_hdir);
15213+ return au_fi(file)->fi_htop.hf_file;
15214+}
1facf9fc 15215+
4a4d8108
AM
15216+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15217+{
15218+ FiMustAnyLock(file);
15219+ AuDebugOn(!au_fi(file)->fi_hdir);
15220+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15221+}
15222+
4a4d8108
AM
15223+/* todo: memory barrier? */
15224+static inline unsigned int au_figen(struct file *f)
dece6358 15225+{
4a4d8108
AM
15226+ return atomic_read(&au_fi(f)->fi_generation);
15227+}
dece6358 15228+
2cbb1c4b
JR
15229+static inline void au_set_mmapped(struct file *f)
15230+{
15231+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15232+ return;
0c3ec466 15233+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15234+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15235+ ;
15236+}
15237+
15238+static inline void au_unset_mmapped(struct file *f)
15239+{
15240+ atomic_dec(&au_fi(f)->fi_mmapped);
15241+}
15242+
4a4d8108
AM
15243+static inline int au_test_mmapped(struct file *f)
15244+{
2cbb1c4b
JR
15245+ return atomic_read(&au_fi(f)->fi_mmapped);
15246+}
15247+
15248+/* customize vma->vm_file */
15249+
15250+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15251+ struct file *file)
15252+{
53392da6
AM
15253+ struct file *f;
15254+
15255+ f = vma->vm_file;
2cbb1c4b
JR
15256+ get_file(file);
15257+ vma->vm_file = file;
53392da6 15258+ fput(f);
2cbb1c4b
JR
15259+}
15260+
15261+#ifdef CONFIG_MMU
15262+#define AuDbgVmRegion(file, vma) do {} while (0)
15263+
15264+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15265+ struct file *file)
15266+{
15267+ au_do_vm_file_reset(vma, file);
15268+}
15269+#else
15270+#define AuDbgVmRegion(file, vma) \
15271+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15272+
15273+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15274+ struct file *file)
15275+{
53392da6
AM
15276+ struct file *f;
15277+
2cbb1c4b 15278+ au_do_vm_file_reset(vma, file);
53392da6 15279+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15280+ get_file(file);
15281+ vma->vm_region->vm_file = file;
53392da6 15282+ fput(f);
2cbb1c4b
JR
15283+}
15284+#endif /* CONFIG_MMU */
15285+
15286+/* handle vma->vm_prfile */
fb47a38f 15287+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15288+ struct file *file)
15289+{
2cbb1c4b
JR
15290+ get_file(file);
15291+ vma->vm_prfile = file;
15292+#ifndef CONFIG_MMU
15293+ get_file(file);
15294+ vma->vm_region->vm_prfile = file;
15295+#endif
fb47a38f 15296+}
1308ab2a 15297+
4a4d8108
AM
15298+#endif /* __KERNEL__ */
15299+#endif /* __AUFS_FILE_H__ */
7f207e10
AM
15300diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15301--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 15302+++ linux/fs/aufs/finfo.c 2018-04-15 08:49:13.397817296 +0200
1c60b727 15303@@ -0,0 +1,148 @@
4a4d8108 15304+/*
b00004a5 15305+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
15306+ *
15307+ * This program, aufs is free software; you can redistribute it and/or modify
15308+ * it under the terms of the GNU General Public License as published by
15309+ * the Free Software Foundation; either version 2 of the License, or
15310+ * (at your option) any later version.
15311+ *
15312+ * This program is distributed in the hope that it will be useful,
15313+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15314+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15315+ * GNU General Public License for more details.
15316+ *
15317+ * You should have received a copy of the GNU General Public License
523b37e3 15318+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15319+ */
1308ab2a 15320+
4a4d8108
AM
15321+/*
15322+ * file private data
15323+ */
1facf9fc 15324+
4a4d8108 15325+#include "aufs.h"
1facf9fc 15326+
f0c0a007 15327+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15328+{
f0c0a007 15329+ if (execed)
4a4d8108
AM
15330+ allow_write_access(hf->hf_file);
15331+ fput(hf->hf_file);
15332+ hf->hf_file = NULL;
5afbbe0d 15333+ au_br_put(hf->hf_br);
4a4d8108
AM
15334+ hf->hf_br = NULL;
15335+}
1facf9fc 15336+
4a4d8108
AM
15337+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15338+{
15339+ struct au_finfo *finfo = au_fi(file);
15340+ struct au_hfile *hf;
15341+ struct au_fidir *fidir;
15342+
15343+ fidir = finfo->fi_hdir;
15344+ if (!fidir) {
15345+ AuDebugOn(finfo->fi_btop != bindex);
15346+ hf = &finfo->fi_htop;
15347+ } else
15348+ hf = fidir->fd_hfile + bindex;
15349+
15350+ if (hf && hf->hf_file)
f0c0a007 15351+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15352+ if (val) {
15353+ FiMustWriteLock(file);
b912730e 15354+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15355+ hf->hf_file = val;
2000de60 15356+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15357+ }
4a4d8108 15358+}
1facf9fc 15359+
4a4d8108
AM
15360+void au_update_figen(struct file *file)
15361+{
2000de60 15362+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15363+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15364+}
15365+
4a4d8108
AM
15366+/* ---------------------------------------------------------------------- */
15367+
4a4d8108
AM
15368+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15369+{
15370+ struct au_fidir *fidir;
15371+ int nbr;
15372+
5afbbe0d 15373+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15374+ if (nbr < 2)
15375+ nbr = 2; /* initial allocate for 2 branches */
15376+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15377+ if (fidir) {
15378+ fidir->fd_bbot = -1;
15379+ fidir->fd_nent = nbr;
4a4d8108
AM
15380+ }
15381+
15382+ return fidir;
15383+}
15384+
e2f27e51 15385+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15386+{
15387+ int err;
15388+ struct au_fidir *fidir, *p;
15389+
15390+ AuRwMustWriteLock(&finfo->fi_rwsem);
15391+ fidir = finfo->fi_hdir;
15392+ AuDebugOn(!fidir);
15393+
15394+ err = -ENOMEM;
15395+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15396+ GFP_NOFS, may_shrink);
4a4d8108
AM
15397+ if (p) {
15398+ p->fd_nent = nbr;
15399+ finfo->fi_hdir = p;
15400+ err = 0;
15401+ }
1facf9fc 15402+
dece6358 15403+ return err;
1facf9fc 15404+}
1308ab2a 15405+
15406+/* ---------------------------------------------------------------------- */
15407+
1c60b727 15408+void au_finfo_fin(struct file *file)
1308ab2a 15409+{
4a4d8108
AM
15410+ struct au_finfo *finfo;
15411+
2000de60 15412+ au_nfiles_dec(file->f_path.dentry->d_sb);
7f207e10 15413+
4a4d8108
AM
15414+ finfo = au_fi(file);
15415+ AuDebugOn(finfo->fi_hdir);
15416+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15417+ au_cache_free_finfo(finfo);
1308ab2a 15418+}
1308ab2a 15419+
e49829fe 15420+void au_fi_init_once(void *_finfo)
4a4d8108 15421+{
e49829fe 15422+ struct au_finfo *finfo = _finfo;
1308ab2a 15423+
e49829fe 15424+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15425+}
1308ab2a 15426+
4a4d8108
AM
15427+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15428+{
1716fcea 15429+ int err;
4a4d8108
AM
15430+ struct au_finfo *finfo;
15431+ struct dentry *dentry;
15432+
15433+ err = -ENOMEM;
2000de60 15434+ dentry = file->f_path.dentry;
4a4d8108
AM
15435+ finfo = au_cache_alloc_finfo();
15436+ if (unlikely(!finfo))
15437+ goto out;
15438+
15439+ err = 0;
7f207e10 15440+ au_nfiles_inc(dentry->d_sb);
4a4d8108
AM
15441+ au_rw_write_lock(&finfo->fi_rwsem);
15442+ finfo->fi_btop = -1;
15443+ finfo->fi_hdir = fidir;
15444+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15445+ /* smp_mb(); */ /* atomic_set */
15446+
15447+ file->private_data = finfo;
15448+
15449+out:
15450+ return err;
15451+}
7f207e10
AM
15452diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15453--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 15454+++ linux/fs/aufs/f_op.c 2018-06-04 09:08:09.184746078 +0200
1c60b727 15455@@ -0,0 +1,817 @@
dece6358 15456+/*
b00004a5 15457+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
15458+ *
15459+ * This program, aufs is free software; you can redistribute it and/or modify
15460+ * it under the terms of the GNU General Public License as published by
15461+ * the Free Software Foundation; either version 2 of the License, or
15462+ * (at your option) any later version.
15463+ *
15464+ * This program is distributed in the hope that it will be useful,
15465+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15466+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15467+ * GNU General Public License for more details.
15468+ *
15469+ * You should have received a copy of the GNU General Public License
523b37e3 15470+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15471+ */
1facf9fc 15472+
15473+/*
4a4d8108 15474+ * file and vm operations
1facf9fc 15475+ */
dece6358 15476+
86dc4139 15477+#include <linux/aio.h>
4a4d8108
AM
15478+#include <linux/fs_stack.h>
15479+#include <linux/mman.h>
4a4d8108 15480+#include <linux/security.h>
dece6358
AM
15481+#include "aufs.h"
15482+
b912730e 15483+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15484+{
4a4d8108
AM
15485+ int err;
15486+ aufs_bindex_t bindex;
8cdd5066 15487+ struct dentry *dentry, *h_dentry;
4a4d8108 15488+ struct au_finfo *finfo;
38d290e6 15489+ struct inode *h_inode;
4a4d8108
AM
15490+
15491+ FiMustWriteLock(file);
15492+
523b37e3 15493+ err = 0;
2000de60 15494+ dentry = file->f_path.dentry;
b912730e 15495+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15496+ finfo = au_fi(file);
15497+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15498+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15499+ bindex = au_dbtop(dentry);
8cdd5066
JR
15500+ if (!h_file) {
15501+ h_dentry = au_h_dptr(dentry, bindex);
15502+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15503+ if (unlikely(err))
15504+ goto out;
b912730e 15505+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
8cdd5066
JR
15506+ } else {
15507+ h_dentry = h_file->f_path.dentry;
15508+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15509+ if (unlikely(err))
15510+ goto out;
b912730e 15511+ get_file(h_file);
8cdd5066 15512+ }
4a4d8108
AM
15513+ if (IS_ERR(h_file))
15514+ err = PTR_ERR(h_file);
15515+ else {
38d290e6
JR
15516+ if ((flags & __O_TMPFILE)
15517+ && !(flags & O_EXCL)) {
15518+ h_inode = file_inode(h_file);
15519+ spin_lock(&h_inode->i_lock);
15520+ h_inode->i_state |= I_LINKABLE;
15521+ spin_unlock(&h_inode->i_lock);
15522+ }
5afbbe0d 15523+ au_set_fbtop(file, bindex);
4a4d8108
AM
15524+ au_set_h_fptr(file, bindex, h_file);
15525+ au_update_figen(file);
15526+ /* todo: necessary? */
15527+ /* file->f_ra = h_file->f_ra; */
15528+ }
027c5e7a 15529+
8cdd5066 15530+out:
4a4d8108 15531+ return err;
1facf9fc 15532+}
15533+
4a4d8108
AM
15534+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15535+ struct file *file)
1facf9fc 15536+{
4a4d8108 15537+ int err;
1308ab2a 15538+ struct super_block *sb;
b912730e
AM
15539+ struct au_do_open_args args = {
15540+ .open = au_do_open_nondir
15541+ };
1facf9fc 15542+
523b37e3
AM
15543+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15544+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15545+
2000de60 15546+ sb = file->f_path.dentry->d_sb;
4a4d8108 15547+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15548+ err = au_do_open(file, &args);
4a4d8108
AM
15549+ si_read_unlock(sb);
15550+ return err;
15551+}
1facf9fc 15552+
4a4d8108
AM
15553+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15554+{
15555+ struct au_finfo *finfo;
15556+ aufs_bindex_t bindex;
1facf9fc 15557+
4a4d8108 15558+ finfo = au_fi(file);
8b6a4947
AM
15559+ au_hbl_del(&finfo->fi_hlist,
15560+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15561+ bindex = finfo->fi_btop;
b4510431 15562+ if (bindex >= 0)
4a4d8108 15563+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15564+
1c60b727 15565+ au_finfo_fin(file);
4a4d8108 15566+ return 0;
1facf9fc 15567+}
15568+
4a4d8108
AM
15569+/* ---------------------------------------------------------------------- */
15570+
15571+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15572+{
1308ab2a 15573+ int err;
4a4d8108
AM
15574+ struct file *h_file;
15575+
15576+ err = 0;
15577+ h_file = au_hf_top(file);
15578+ if (h_file)
15579+ err = vfsub_flush(h_file, id);
15580+ return err;
15581+}
15582+
15583+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15584+{
15585+ return au_do_flush(file, id, au_do_flush_nondir);
15586+}
15587+
15588+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15589+/*
15590+ * read and write functions acquire [fdi]_rwsem once, but release before
15591+ * mmap_sem. This is because to stop a race condition between mmap(2).
15592+ * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
15593+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15594+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15595+ */
4a4d8108 15596+
b912730e 15597+/* Callers should call au_read_post() or fput() in the end */
521ced18 15598+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15599+{
4a4d8108 15600+ struct file *h_file;
b912730e 15601+ int err;
1facf9fc 15602+
521ced18 15603+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15604+ if (!err) {
15605+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15606+ h_file = au_hf_top(file);
15607+ get_file(h_file);
15608+ if (!keep_fi)
15609+ fi_read_unlock(file);
15610+ } else
15611+ h_file = ERR_PTR(err);
15612+
15613+ return h_file;
15614+}
15615+
15616+static void au_read_post(struct inode *inode, struct file *h_file)
15617+{
15618+ /* update without lock, I don't think it a problem */
15619+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15620+ fput(h_file);
15621+}
15622+
15623+struct au_write_pre {
521ced18
JR
15624+ /* input */
15625+ unsigned int lsc;
15626+
15627+ /* output */
b912730e 15628+ blkcnt_t blks;
5afbbe0d 15629+ aufs_bindex_t btop;
b912730e
AM
15630+};
15631+
15632+/*
15633+ * return with iinfo is write-locked
15634+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15635+ * end
15636+ */
15637+static struct file *au_write_pre(struct file *file, int do_ready,
15638+ struct au_write_pre *wpre)
15639+{
15640+ struct file *h_file;
15641+ struct dentry *dentry;
15642+ int err;
521ced18 15643+ unsigned int lsc;
b912730e
AM
15644+ struct au_pin pin;
15645+
521ced18
JR
15646+ lsc = 0;
15647+ if (wpre)
15648+ lsc = wpre->lsc;
15649+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15650+ h_file = ERR_PTR(err);
dece6358
AM
15651+ if (unlikely(err))
15652+ goto out;
1facf9fc 15653+
b912730e
AM
15654+ dentry = file->f_path.dentry;
15655+ if (do_ready) {
15656+ err = au_ready_to_write(file, -1, &pin);
15657+ if (unlikely(err)) {
15658+ h_file = ERR_PTR(err);
15659+ di_write_unlock(dentry);
15660+ goto out_fi;
15661+ }
15662+ }
15663+
15664+ di_downgrade_lock(dentry, /*flags*/0);
15665+ if (wpre)
5afbbe0d 15666+ wpre->btop = au_fbtop(file);
4a4d8108 15667+ h_file = au_hf_top(file);
9dbd164d 15668+ get_file(h_file);
b912730e
AM
15669+ if (wpre)
15670+ wpre->blks = file_inode(h_file)->i_blocks;
15671+ if (do_ready)
15672+ au_unpin(&pin);
15673+ di_read_unlock(dentry, /*flags*/0);
15674+
15675+out_fi:
15676+ fi_write_unlock(file);
15677+out:
15678+ return h_file;
15679+}
15680+
15681+static void au_write_post(struct inode *inode, struct file *h_file,
15682+ struct au_write_pre *wpre, ssize_t written)
15683+{
15684+ struct inode *h_inode;
15685+
15686+ au_cpup_attr_timesizes(inode);
5afbbe0d 15687+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15688+ h_inode = file_inode(h_file);
15689+ inode->i_mode = h_inode->i_mode;
15690+ ii_write_unlock(inode);
b912730e
AM
15691+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15692+ if (written > 0)
5afbbe0d 15693+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15694+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15695+ fput(h_file);
b912730e
AM
15696+}
15697+
15698+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15699+ loff_t *ppos)
15700+{
15701+ ssize_t err;
15702+ struct inode *inode;
15703+ struct file *h_file;
15704+ struct super_block *sb;
15705+
15706+ inode = file_inode(file);
15707+ sb = inode->i_sb;
15708+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15709+
521ced18 15710+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15711+ err = PTR_ERR(h_file);
15712+ if (IS_ERR(h_file))
15713+ goto out;
9dbd164d
AM
15714+
15715+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
15716+ err = vfsub_read_u(h_file, buf, count, ppos);
15717+ /* todo: necessary? */
15718+ /* file->f_ra = h_file->f_ra; */
b912730e 15719+ au_read_post(inode, h_file);
1308ab2a 15720+
4f0767ce 15721+out:
dece6358
AM
15722+ si_read_unlock(sb);
15723+ return err;
15724+}
1facf9fc 15725+
e49829fe
JR
15726+/*
15727+ * todo: very ugly
15728+ * it locks both of i_mutex and si_rwsem for read in safe.
15729+ * if the plink maintenance mode continues forever (that is the problem),
15730+ * may loop forever.
15731+ */
15732+static void au_mtx_and_read_lock(struct inode *inode)
15733+{
15734+ int err;
15735+ struct super_block *sb = inode->i_sb;
15736+
15737+ while (1) {
febd17d6 15738+ inode_lock(inode);
e49829fe
JR
15739+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15740+ if (!err)
15741+ break;
febd17d6 15742+ inode_unlock(inode);
e49829fe
JR
15743+ si_read_lock(sb, AuLock_NOPLMW);
15744+ si_read_unlock(sb);
15745+ }
15746+}
15747+
4a4d8108
AM
15748+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15749+ size_t count, loff_t *ppos)
dece6358 15750+{
4a4d8108 15751+ ssize_t err;
b912730e
AM
15752+ struct au_write_pre wpre;
15753+ struct inode *inode;
4a4d8108
AM
15754+ struct file *h_file;
15755+ char __user *buf = (char __user *)ubuf;
1facf9fc 15756+
b912730e 15757+ inode = file_inode(file);
e49829fe 15758+ au_mtx_and_read_lock(inode);
1facf9fc 15759+
521ced18 15760+ wpre.lsc = 0;
b912730e
AM
15761+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15762+ err = PTR_ERR(h_file);
15763+ if (IS_ERR(h_file))
9dbd164d 15764+ goto out;
9dbd164d 15765+
4a4d8108 15766+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 15767+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15768+
4f0767ce 15769+out:
b912730e 15770+ si_read_unlock(inode->i_sb);
febd17d6 15771+ inode_unlock(inode);
dece6358
AM
15772+ return err;
15773+}
1facf9fc 15774+
076b876e
AM
15775+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15776+ struct iov_iter *iov_iter)
dece6358 15777+{
4a4d8108
AM
15778+ ssize_t err;
15779+ struct file *file;
076b876e 15780+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15781+
4a4d8108
AM
15782+ err = security_file_permission(h_file, rw);
15783+ if (unlikely(err))
15784+ goto out;
1facf9fc 15785+
4a4d8108 15786+ err = -ENOSYS;
076b876e 15787+ iter = NULL;
5527c038 15788+ if (rw == MAY_READ)
076b876e 15789+ iter = h_file->f_op->read_iter;
5527c038 15790+ else if (rw == MAY_WRITE)
076b876e 15791+ iter = h_file->f_op->write_iter;
076b876e
AM
15792+
15793+ file = kio->ki_filp;
15794+ kio->ki_filp = h_file;
15795+ if (iter) {
2cbb1c4b 15796+ lockdep_off();
076b876e
AM
15797+ err = iter(kio, iov_iter);
15798+ lockdep_on();
4a4d8108
AM
15799+ } else
15800+ /* currently there is no such fs */
15801+ WARN_ON_ONCE(1);
076b876e 15802+ kio->ki_filp = file;
1facf9fc 15803+
4f0767ce 15804+out:
dece6358
AM
15805+ return err;
15806+}
1facf9fc 15807+
076b876e 15808+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15809+{
4a4d8108
AM
15810+ ssize_t err;
15811+ struct file *file, *h_file;
b912730e 15812+ struct inode *inode;
dece6358 15813+ struct super_block *sb;
1facf9fc 15814+
4a4d8108 15815+ file = kio->ki_filp;
b912730e
AM
15816+ inode = file_inode(file);
15817+ sb = inode->i_sb;
e49829fe 15818+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15819+
521ced18 15820+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15821+ err = PTR_ERR(h_file);
15822+ if (IS_ERR(h_file))
15823+ goto out;
9dbd164d 15824+
5afbbe0d
AM
15825+ if (au_test_loopback_kthread()) {
15826+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15827+ if (file->f_mapping != h_file->f_mapping) {
15828+ file->f_mapping = h_file->f_mapping;
15829+ smp_mb(); /* unnecessary? */
15830+ }
15831+ }
15832+ fi_read_unlock(file);
15833+
076b876e 15834+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15835+ /* todo: necessary? */
15836+ /* file->f_ra = h_file->f_ra; */
b912730e 15837+ au_read_post(inode, h_file);
1facf9fc 15838+
4f0767ce 15839+out:
4a4d8108 15840+ si_read_unlock(sb);
1308ab2a 15841+ return err;
15842+}
1facf9fc 15843+
076b876e 15844+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15845+{
4a4d8108 15846+ ssize_t err;
b912730e
AM
15847+ struct au_write_pre wpre;
15848+ struct inode *inode;
4a4d8108 15849+ struct file *file, *h_file;
1308ab2a 15850+
4a4d8108 15851+ file = kio->ki_filp;
b912730e 15852+ inode = file_inode(file);
e49829fe
JR
15853+ au_mtx_and_read_lock(inode);
15854+
521ced18 15855+ wpre.lsc = 0;
b912730e
AM
15856+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15857+ err = PTR_ERR(h_file);
15858+ if (IS_ERR(h_file))
9dbd164d 15859+ goto out;
9dbd164d 15860+
076b876e 15861+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 15862+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15863+
4f0767ce 15864+out:
b912730e 15865+ si_read_unlock(inode->i_sb);
febd17d6 15866+ inode_unlock(inode);
dece6358 15867+ return err;
1facf9fc 15868+}
15869+
4a4d8108
AM
15870+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15871+ struct pipe_inode_info *pipe, size_t len,
15872+ unsigned int flags)
1facf9fc 15873+{
4a4d8108
AM
15874+ ssize_t err;
15875+ struct file *h_file;
b912730e 15876+ struct inode *inode;
dece6358 15877+ struct super_block *sb;
1facf9fc 15878+
b912730e
AM
15879+ inode = file_inode(file);
15880+ sb = inode->i_sb;
e49829fe 15881+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 15882+
521ced18 15883+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15884+ err = PTR_ERR(h_file);
15885+ if (IS_ERR(h_file))
dece6358 15886+ goto out;
1facf9fc 15887+
4a4d8108
AM
15888+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15889+ /* todo: necessasry? */
15890+ /* file->f_ra = h_file->f_ra; */
b912730e 15891+ au_read_post(inode, h_file);
1facf9fc 15892+
4f0767ce 15893+out:
4a4d8108 15894+ si_read_unlock(sb);
dece6358 15895+ return err;
1facf9fc 15896+}
15897+
4a4d8108
AM
15898+static ssize_t
15899+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15900+ size_t len, unsigned int flags)
1facf9fc 15901+{
4a4d8108 15902+ ssize_t err;
b912730e
AM
15903+ struct au_write_pre wpre;
15904+ struct inode *inode;
076b876e 15905+ struct file *h_file;
1facf9fc 15906+
b912730e 15907+ inode = file_inode(file);
e49829fe 15908+ au_mtx_and_read_lock(inode);
9dbd164d 15909+
521ced18 15910+ wpre.lsc = 0;
b912730e
AM
15911+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15912+ err = PTR_ERR(h_file);
15913+ if (IS_ERR(h_file))
9dbd164d 15914+ goto out;
9dbd164d 15915+
4a4d8108 15916+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 15917+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15918+
4f0767ce 15919+out:
b912730e 15920+ si_read_unlock(inode->i_sb);
febd17d6 15921+ inode_unlock(inode);
4a4d8108
AM
15922+ return err;
15923+}
1facf9fc 15924+
38d290e6
JR
15925+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15926+ loff_t len)
15927+{
15928+ long err;
b912730e 15929+ struct au_write_pre wpre;
38d290e6
JR
15930+ struct inode *inode;
15931+ struct file *h_file;
15932+
b912730e 15933+ inode = file_inode(file);
38d290e6
JR
15934+ au_mtx_and_read_lock(inode);
15935+
521ced18 15936+ wpre.lsc = 0;
b912730e
AM
15937+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15938+ err = PTR_ERR(h_file);
15939+ if (IS_ERR(h_file))
38d290e6 15940+ goto out;
38d290e6
JR
15941+
15942+ lockdep_off();
03673fb0 15943+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 15944+ lockdep_on();
b912730e 15945+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
15946+
15947+out:
b912730e 15948+ si_read_unlock(inode->i_sb);
febd17d6 15949+ inode_unlock(inode);
38d290e6
JR
15950+ return err;
15951+}
15952+
521ced18
JR
15953+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15954+ struct file *dst, loff_t dst_pos,
15955+ size_t len, unsigned int flags)
15956+{
15957+ ssize_t err;
15958+ struct au_write_pre wpre;
15959+ enum { SRC, DST };
15960+ struct {
15961+ struct inode *inode;
15962+ struct file *h_file;
15963+ struct super_block *h_sb;
15964+ } a[2];
15965+#define a_src a[SRC]
15966+#define a_dst a[DST]
15967+
15968+ err = -EINVAL;
15969+ a_src.inode = file_inode(src);
15970+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15971+ goto out;
15972+ a_dst.inode = file_inode(dst);
15973+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15974+ goto out;
15975+
15976+ au_mtx_and_read_lock(a_dst.inode);
15977+ /*
15978+ * in order to match the order in di_write_lock2_{child,parent}(),
15979+ * use f_path.dentry for this comparision.
15980+ */
15981+ if (src->f_path.dentry < dst->f_path.dentry) {
15982+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15983+ err = PTR_ERR(a_src.h_file);
15984+ if (IS_ERR(a_src.h_file))
15985+ goto out_si;
15986+
15987+ wpre.lsc = AuLsc_FI_2;
15988+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15989+ err = PTR_ERR(a_dst.h_file);
15990+ if (IS_ERR(a_dst.h_file)) {
15991+ au_read_post(a_src.inode, a_src.h_file);
15992+ goto out_si;
15993+ }
15994+ } else {
15995+ wpre.lsc = AuLsc_FI_1;
15996+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15997+ err = PTR_ERR(a_dst.h_file);
15998+ if (IS_ERR(a_dst.h_file))
15999+ goto out_si;
16000+
16001+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16002+ err = PTR_ERR(a_src.h_file);
16003+ if (IS_ERR(a_src.h_file)) {
16004+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16005+ /*written*/0);
16006+ goto out_si;
16007+ }
16008+ }
16009+
16010+ err = -EXDEV;
16011+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16012+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16013+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16014+ AuDbgFile(src);
16015+ AuDbgFile(dst);
16016+ goto out_file;
16017+ }
16018+
16019+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16020+ dst_pos, len, flags);
16021+
16022+out_file:
16023+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16024+ fi_read_unlock(src);
16025+ au_read_post(a_src.inode, a_src.h_file);
16026+out_si:
16027+ si_read_unlock(a_dst.inode->i_sb);
16028+ inode_unlock(a_dst.inode);
16029+out:
16030+ return err;
16031+#undef a_src
16032+#undef a_dst
16033+}
16034+
4a4d8108
AM
16035+/* ---------------------------------------------------------------------- */
16036+
9dbd164d
AM
16037+/*
16038+ * The locking order around current->mmap_sem.
16039+ * - in most and regular cases
16040+ * file I/O syscall -- aufs_read() or something
16041+ * -- si_rwsem for read -- mmap_sem
16042+ * (Note that [fdi]i_rwsem are released before mmap_sem).
16043+ * - in mmap case
16044+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16045+ * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
16046+ * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
16047+ * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16048+ * It means that when aufs acquires si_rwsem for write, the process should never
16049+ * acquire mmap_sem.
16050+ *
392086de 16051+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
16052+ * problem either since any directory is not able to be mmap-ed.
16053+ * The similar scenario is applied to aufs_readlink() too.
16054+ */
16055+
38d290e6 16056+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16057+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16058+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16059+
16060+static unsigned long au_arch_prot_conv(unsigned long flags)
16061+{
16062+ /* currently ppc64 only */
16063+#ifdef CONFIG_PPC64
16064+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16065+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16066+ return AuConv_VM_PROT(flags, SAO);
16067+#else
16068+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16069+ return 0;
16070+#endif
16071+}
16072+
16073+static unsigned long au_prot_conv(unsigned long flags)
16074+{
16075+ return AuConv_VM_PROT(flags, READ)
16076+ | AuConv_VM_PROT(flags, WRITE)
16077+ | AuConv_VM_PROT(flags, EXEC)
16078+ | au_arch_prot_conv(flags);
16079+}
16080+
16081+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16082+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16083+
16084+static unsigned long au_flag_conv(unsigned long flags)
16085+{
16086+ return AuConv_VM_MAP(flags, GROWSDOWN)
16087+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16088+ | AuConv_VM_MAP(flags, LOCKED);
16089+}
38d290e6 16090+#endif
2dfbb274 16091+
9dbd164d 16092+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16093+{
4a4d8108 16094+ int err;
4a4d8108 16095+ const unsigned char wlock
9dbd164d 16096+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16097+ struct super_block *sb;
9dbd164d 16098+ struct file *h_file;
b912730e 16099+ struct inode *inode;
9dbd164d
AM
16100+
16101+ AuDbgVmRegion(file, vma);
1308ab2a 16102+
b912730e
AM
16103+ inode = file_inode(file);
16104+ sb = inode->i_sb;
9dbd164d 16105+ lockdep_off();
e49829fe 16106+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16107+
b912730e 16108+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16109+ lockdep_on();
b912730e
AM
16110+ err = PTR_ERR(h_file);
16111+ if (IS_ERR(h_file))
16112+ goto out;
1308ab2a 16113+
b912730e
AM
16114+ err = 0;
16115+ au_set_mmapped(file);
9dbd164d 16116+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16117+ /*
16118+ * we cannot call security_mmap_file() here since it may acquire
16119+ * mmap_sem or i_mutex.
16120+ *
16121+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16122+ * au_flag_conv(vma->vm_flags));
16123+ */
9dbd164d 16124+ if (!err)
521ced18 16125+ err = call_mmap(h_file, vma);
b912730e
AM
16126+ if (!err) {
16127+ au_vm_prfile_set(vma, file);
16128+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16129+ goto out_fput; /* success */
16130+ }
2cbb1c4b
JR
16131+ au_unset_mmapped(file);
16132+ au_vm_file_reset(vma, file);
b912730e 16133+
2cbb1c4b 16134+out_fput:
9dbd164d 16135+ lockdep_off();
b912730e
AM
16136+ ii_write_unlock(inode);
16137+ lockdep_on();
16138+ fput(h_file);
4f0767ce 16139+out:
b912730e 16140+ lockdep_off();
9dbd164d
AM
16141+ si_read_unlock(sb);
16142+ lockdep_on();
16143+ AuTraceErr(err);
4a4d8108
AM
16144+ return err;
16145+}
16146+
16147+/* ---------------------------------------------------------------------- */
16148+
1e00d052
AM
16149+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16150+ int datasync)
4a4d8108
AM
16151+{
16152+ int err;
b912730e 16153+ struct au_write_pre wpre;
4a4d8108
AM
16154+ struct inode *inode;
16155+ struct file *h_file;
4a4d8108
AM
16156+
16157+ err = 0; /* -EBADF; */ /* posix? */
16158+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16159+ goto out;
4a4d8108 16160+
b912730e
AM
16161+ inode = file_inode(file);
16162+ au_mtx_and_read_lock(inode);
16163+
521ced18 16164+ wpre.lsc = 0;
b912730e
AM
16165+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16166+ err = PTR_ERR(h_file);
16167+ if (IS_ERR(h_file))
4a4d8108 16168+ goto out_unlock;
4a4d8108 16169+
53392da6 16170+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16171+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16172+
4f0767ce 16173+out_unlock:
b912730e 16174+ si_read_unlock(inode->i_sb);
febd17d6 16175+ inode_unlock(inode);
b912730e 16176+out:
4a4d8108 16177+ return err;
dece6358
AM
16178+}
16179+
4a4d8108 16180+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16181+{
4a4d8108
AM
16182+ int err;
16183+ struct file *h_file;
4a4d8108 16184+ struct super_block *sb;
1308ab2a 16185+
b912730e 16186+ sb = file->f_path.dentry->d_sb;
e49829fe 16187+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16188+
521ced18 16189+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16190+ err = PTR_ERR(h_file);
16191+ if (IS_ERR(h_file))
4a4d8108
AM
16192+ goto out;
16193+
523b37e3 16194+ if (h_file->f_op->fasync)
4a4d8108 16195+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16196+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16197+
4f0767ce 16198+out:
4a4d8108 16199+ si_read_unlock(sb);
1308ab2a 16200+ return err;
dece6358 16201+}
4a4d8108 16202+
febd17d6
JR
16203+static int aufs_setfl(struct file *file, unsigned long arg)
16204+{
16205+ int err;
16206+ struct file *h_file;
16207+ struct super_block *sb;
16208+
16209+ sb = file->f_path.dentry->d_sb;
16210+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16211+
521ced18 16212+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16213+ err = PTR_ERR(h_file);
16214+ if (IS_ERR(h_file))
16215+ goto out;
16216+
1c60b727
AM
16217+ /* stop calling h_file->fasync */
16218+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16219+ err = setfl(/*unused fd*/-1, h_file, arg);
16220+ fput(h_file); /* instead of au_read_post() */
16221+
16222+out:
16223+ si_read_unlock(sb);
16224+ return err;
16225+}
16226+
4a4d8108
AM
16227+/* ---------------------------------------------------------------------- */
16228+
16229+/* no one supports this operation, currently */
16230+#if 0
16231+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16232+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16233+{
16234+}
16235+#endif
16236+
16237+/* ---------------------------------------------------------------------- */
16238+
16239+const struct file_operations aufs_file_fop = {
16240+ .owner = THIS_MODULE,
2cbb1c4b 16241+
027c5e7a 16242+ .llseek = default_llseek,
4a4d8108
AM
16243+
16244+ .read = aufs_read,
16245+ .write = aufs_write,
076b876e
AM
16246+ .read_iter = aufs_read_iter,
16247+ .write_iter = aufs_write_iter,
16248+
4a4d8108
AM
16249+#ifdef CONFIG_AUFS_POLL
16250+ .poll = aufs_poll,
16251+#endif
16252+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16253+#ifdef CONFIG_COMPAT
c2b27bf2 16254+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16255+#endif
4a4d8108
AM
16256+ .mmap = aufs_mmap,
16257+ .open = aufs_open_nondir,
16258+ .flush = aufs_flush_nondir,
16259+ .release = aufs_release_nondir,
16260+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16261+ .fasync = aufs_fasync,
16262+ /* .sendpage = aufs_sendpage, */
febd17d6 16263+ .setfl = aufs_setfl,
4a4d8108
AM
16264+ .splice_write = aufs_splice_write,
16265+ .splice_read = aufs_splice_read,
16266+#if 0
16267+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16268+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16269+#endif
521ced18
JR
16270+ .fallocate = aufs_fallocate,
16271+ .copy_file_range = aufs_copy_file_range
4a4d8108 16272+};
7f207e10
AM
16273diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16274--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 16275+++ linux/fs/aufs/fstype.h 2018-06-04 09:08:09.184746078 +0200
b912730e 16276@@ -0,0 +1,400 @@
4a4d8108 16277+/*
b00004a5 16278+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
16279+ *
16280+ * This program, aufs is free software; you can redistribute it and/or modify
16281+ * it under the terms of the GNU General Public License as published by
16282+ * the Free Software Foundation; either version 2 of the License, or
16283+ * (at your option) any later version.
16284+ *
16285+ * This program is distributed in the hope that it will be useful,
16286+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16287+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16288+ * GNU General Public License for more details.
16289+ *
16290+ * You should have received a copy of the GNU General Public License
523b37e3 16291+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16292+ */
16293+
16294+/*
16295+ * judging filesystem type
16296+ */
16297+
16298+#ifndef __AUFS_FSTYPE_H__
16299+#define __AUFS_FSTYPE_H__
16300+
16301+#ifdef __KERNEL__
16302+
16303+#include <linux/fs.h>
16304+#include <linux/magic.h>
b912730e 16305+#include <linux/nfs_fs.h>
b95c5147 16306+#include <linux/romfs_fs.h>
4a4d8108
AM
16307+
16308+static inline int au_test_aufs(struct super_block *sb)
16309+{
16310+ return sb->s_magic == AUFS_SUPER_MAGIC;
16311+}
16312+
16313+static inline const char *au_sbtype(struct super_block *sb)
16314+{
16315+ return sb->s_type->name;
16316+}
1308ab2a 16317+
16318+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16319+{
f0c0a007 16320+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16321+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16322+#else
16323+ return 0;
16324+#endif
16325+}
16326+
1308ab2a 16327+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16328+{
f0c0a007 16329+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16330+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16331+#else
16332+ return 0;
16333+#endif
16334+}
16335+
1308ab2a 16336+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16337+{
f0c0a007 16338+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16339+ return sb->s_magic == CRAMFS_MAGIC;
16340+#endif
16341+ return 0;
16342+}
16343+
16344+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16345+{
f0c0a007 16346+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16347+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16348+#else
16349+ return 0;
16350+#endif
16351+}
16352+
1308ab2a 16353+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16354+{
f0c0a007 16355+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16356+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16357+#else
16358+ return 0;
16359+#endif
16360+}
16361+
1308ab2a 16362+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16363+{
f0c0a007 16364+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16365+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16366+#else
16367+ return 0;
16368+#endif
16369+}
16370+
1308ab2a 16371+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16372+{
1308ab2a 16373+#ifdef CONFIG_TMPFS
16374+ return sb->s_magic == TMPFS_MAGIC;
16375+#else
16376+ return 0;
dece6358 16377+#endif
dece6358
AM
16378+}
16379+
1308ab2a 16380+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16381+{
f0c0a007 16382+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16383+ return !strcmp(au_sbtype(sb), "ecryptfs");
16384+#else
16385+ return 0;
16386+#endif
1facf9fc 16387+}
16388+
1308ab2a 16389+static inline int au_test_ramfs(struct super_block *sb)
16390+{
16391+ return sb->s_magic == RAMFS_MAGIC;
16392+}
16393+
16394+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16395+{
f0c0a007 16396+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16397+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16398+#else
16399+ return 0;
16400+#endif
16401+}
16402+
16403+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16404+{
16405+#ifdef CONFIG_PROC_FS
16406+ return sb->s_magic == PROC_SUPER_MAGIC;
16407+#else
16408+ return 0;
16409+#endif
16410+}
16411+
16412+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16413+{
16414+#ifdef CONFIG_SYSFS
16415+ return sb->s_magic == SYSFS_MAGIC;
16416+#else
16417+ return 0;
16418+#endif
16419+}
16420+
16421+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16422+{
f0c0a007 16423+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16424+ return sb->s_magic == CONFIGFS_MAGIC;
16425+#else
16426+ return 0;
16427+#endif
16428+}
16429+
16430+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16431+{
f0c0a007 16432+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16433+ return sb->s_magic == MINIX3_SUPER_MAGIC
16434+ || sb->s_magic == MINIX2_SUPER_MAGIC
16435+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16436+ || sb->s_magic == MINIX_SUPER_MAGIC
16437+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16438+#else
16439+ return 0;
16440+#endif
16441+}
16442+
1308ab2a 16443+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16444+{
f0c0a007 16445+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16446+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16447+#else
16448+ return 0;
16449+#endif
16450+}
16451+
16452+static inline int au_test_msdos(struct super_block *sb)
16453+{
16454+ return au_test_fat(sb);
16455+}
16456+
16457+static inline int au_test_vfat(struct super_block *sb)
16458+{
16459+ return au_test_fat(sb);
16460+}
16461+
16462+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16463+{
16464+#ifdef CONFIG_SECURITYFS
16465+ return sb->s_magic == SECURITYFS_MAGIC;
16466+#else
16467+ return 0;
16468+#endif
16469+}
16470+
16471+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16472+{
f0c0a007 16473+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16474+ return sb->s_magic == SQUASHFS_MAGIC;
16475+#else
16476+ return 0;
16477+#endif
16478+}
16479+
16480+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16481+{
f0c0a007 16482+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16483+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16484+#else
16485+ return 0;
16486+#endif
16487+}
16488+
16489+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16490+{
f0c0a007 16491+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16492+ return sb->s_magic == XENFS_SUPER_MAGIC;
16493+#else
16494+ return 0;
16495+#endif
16496+}
16497+
16498+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16499+{
16500+#ifdef CONFIG_DEBUG_FS
16501+ return sb->s_magic == DEBUGFS_MAGIC;
16502+#else
16503+ return 0;
16504+#endif
16505+}
16506+
16507+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16508+{
f0c0a007 16509+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16510+ return sb->s_magic == NILFS_SUPER_MAGIC;
16511+#else
16512+ return 0;
16513+#endif
16514+}
16515+
4a4d8108
AM
16516+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16517+{
f0c0a007 16518+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16519+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16520+#else
16521+ return 0;
16522+#endif
16523+}
16524+
1308ab2a 16525+/* ---------------------------------------------------------------------- */
16526+/*
16527+ * they can't be an aufs branch.
16528+ */
16529+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16530+{
16531+ return
16532+#ifndef CONFIG_AUFS_BR_RAMFS
16533+ au_test_ramfs(sb) ||
16534+#endif
16535+ au_test_procfs(sb)
16536+ || au_test_sysfs(sb)
16537+ || au_test_configfs(sb)
16538+ || au_test_debugfs(sb)
16539+ || au_test_securityfs(sb)
16540+ || au_test_xenfs(sb)
16541+ || au_test_ecryptfs(sb)
16542+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16543+ || au_test_aufs(sb); /* will be supported in next version */
16544+}
16545+
1308ab2a 16546+static inline int au_test_fs_remote(struct super_block *sb)
16547+{
16548+ return !au_test_tmpfs(sb)
16549+#ifdef CONFIG_AUFS_BR_RAMFS
16550+ && !au_test_ramfs(sb)
16551+#endif
16552+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16553+}
16554+
16555+/* ---------------------------------------------------------------------- */
16556+
16557+/*
16558+ * Note: these functions (below) are created after reading ->getattr() in all
16559+ * filesystems under linux/fs. it means we have to do so in every update...
16560+ */
16561+
16562+/*
16563+ * some filesystems require getattr to refresh the inode attributes before
16564+ * referencing.
16565+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16566+ * and leave the work for d_revalidate()
16567+ */
16568+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16569+{
16570+ return au_test_nfs(sb)
16571+ || au_test_fuse(sb)
1308ab2a 16572+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16573+ ;
16574+}
16575+
16576+/*
16577+ * filesystems which don't maintain i_size or i_blocks.
16578+ */
16579+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16580+{
16581+ return au_test_xfs(sb)
4a4d8108
AM
16582+ || au_test_btrfs(sb)
16583+ || au_test_ubifs(sb)
16584+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16585+ /* || au_test_minix(sb) */ /* untested */
16586+ ;
16587+}
16588+
16589+/*
16590+ * filesystems which don't store the correct value in some of their inode
16591+ * attributes.
16592+ */
16593+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16594+{
16595+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16596+ || au_test_fat(sb)
16597+ || au_test_msdos(sb)
16598+ || au_test_vfat(sb);
1facf9fc 16599+}
16600+
16601+/* they don't check i_nlink in link(2) */
16602+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16603+{
16604+ return au_test_tmpfs(sb)
16605+#ifdef CONFIG_AUFS_BR_RAMFS
16606+ || au_test_ramfs(sb)
16607+#endif
4a4d8108 16608+ || au_test_ubifs(sb)
4a4d8108 16609+ || au_test_hfsplus(sb);
1facf9fc 16610+}
16611+
16612+/*
16613+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16614+ */
16615+static inline int au_test_fs_notime(struct super_block *sb)
16616+{
16617+ return au_test_nfs(sb)
16618+ || au_test_fuse(sb)
dece6358 16619+ || au_test_ubifs(sb)
1facf9fc 16620+ ;
16621+}
16622+
1facf9fc 16623+/* temporary support for i#1 in cramfs */
16624+static inline int au_test_fs_unique_ino(struct inode *inode)
16625+{
16626+ if (au_test_cramfs(inode->i_sb))
16627+ return inode->i_ino != 1;
16628+ return 1;
16629+}
16630+
16631+/* ---------------------------------------------------------------------- */
16632+
16633+/*
16634+ * the filesystem where the xino files placed must support i/o after unlink and
16635+ * maintain i_size and i_blocks.
16636+ */
16637+static inline int au_test_fs_bad_xino(struct super_block *sb)
16638+{
16639+ return au_test_fs_remote(sb)
16640+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16641+ /* don't want unnecessary work for xino */
16642+ || au_test_aufs(sb)
1308ab2a 16643+ || au_test_ecryptfs(sb)
16644+ || au_test_nilfs(sb);
1facf9fc 16645+}
16646+
16647+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16648+{
16649+ return au_test_tmpfs(sb)
16650+ || au_test_ramfs(sb);
16651+}
16652+
16653+/*
16654+ * test if the @sb is real-readonly.
16655+ */
16656+static inline int au_test_fs_rr(struct super_block *sb)
16657+{
16658+ return au_test_squashfs(sb)
16659+ || au_test_iso9660(sb)
16660+ || au_test_cramfs(sb)
16661+ || au_test_romfs(sb);
16662+}
16663+
b912730e
AM
16664+/*
16665+ * test if the @inode is nfs with 'noacl' option
2121bcd9 16666+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
16667+ */
16668+static inline int au_test_nfs_noacl(struct inode *inode)
16669+{
16670+ return au_test_nfs(inode->i_sb)
16671+ /* && IS_POSIXACL(inode) */
16672+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16673+}
16674+
1facf9fc 16675+#endif /* __KERNEL__ */
16676+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947
AM
16677diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16678--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 16679+++ linux/fs/aufs/hbl.h 2018-04-15 08:49:13.401150731 +0200
8b6a4947
AM
16680@@ -0,0 +1,64 @@
16681+/*
b00004a5 16682+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
16683+ *
16684+ * This program, aufs is free software; you can redistribute it and/or modify
16685+ * it under the terms of the GNU General Public License as published by
16686+ * the Free Software Foundation; either version 2 of the License, or
16687+ * (at your option) any later version.
16688+ *
16689+ * This program is distributed in the hope that it will be useful,
16690+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16691+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16692+ * GNU General Public License for more details.
16693+ *
16694+ * You should have received a copy of the GNU General Public License
16695+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16696+ */
16697+
16698+/*
16699+ * helpers for hlist_bl.h
16700+ */
16701+
16702+#ifndef __AUFS_HBL_H__
16703+#define __AUFS_HBL_H__
16704+
16705+#ifdef __KERNEL__
16706+
16707+#include <linux/list_bl.h>
16708+
16709+static inline void au_hbl_add(struct hlist_bl_node *node,
16710+ struct hlist_bl_head *hbl)
16711+{
16712+ hlist_bl_lock(hbl);
16713+ hlist_bl_add_head(node, hbl);
16714+ hlist_bl_unlock(hbl);
16715+}
16716+
16717+static inline void au_hbl_del(struct hlist_bl_node *node,
16718+ struct hlist_bl_head *hbl)
16719+{
16720+ hlist_bl_lock(hbl);
16721+ hlist_bl_del(node);
16722+ hlist_bl_unlock(hbl);
16723+}
16724+
16725+#define au_hbl_for_each(pos, head) \
16726+ for (pos = hlist_bl_first(head); \
16727+ pos; \
16728+ pos = pos->next)
16729+
16730+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16731+{
16732+ unsigned long cnt;
16733+ struct hlist_bl_node *pos;
16734+
16735+ cnt = 0;
16736+ hlist_bl_lock(hbl);
16737+ au_hbl_for_each(pos, hbl)
16738+ cnt++;
16739+ hlist_bl_unlock(hbl);
16740+ return cnt;
16741+}
16742+
16743+#endif /* __KERNEL__ */
16744+#endif /* __AUFS_HBL_H__ */
7f207e10
AM
16745diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16746--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 16747+++ linux/fs/aufs/hfsnotify.c 2018-06-04 09:08:09.184746078 +0200
ffa93bbd 16748@@ -0,0 +1,289 @@
1facf9fc 16749+/*
b00004a5 16750+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 16751+ *
16752+ * This program, aufs is free software; you can redistribute it and/or modify
16753+ * it under the terms of the GNU General Public License as published by
16754+ * the Free Software Foundation; either version 2 of the License, or
16755+ * (at your option) any later version.
dece6358
AM
16756+ *
16757+ * This program is distributed in the hope that it will be useful,
16758+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16759+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16760+ * GNU General Public License for more details.
16761+ *
16762+ * You should have received a copy of the GNU General Public License
523b37e3 16763+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16764+ */
16765+
16766+/*
4a4d8108 16767+ * fsnotify for the lower directories
1facf9fc 16768+ */
16769+
16770+#include "aufs.h"
16771+
4a4d8108
AM
16772+/* FS_IN_IGNORED is unnecessary */
16773+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16774+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16775+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16776+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16777+
0c5527e5 16778+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16779+{
0c5527e5
AM
16780+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16781+ hn_mark);
5afbbe0d 16782+ /* AuDbg("here\n"); */
1c60b727 16783+ au_cache_free_hnotify(hn);
8b6a4947 16784+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16785+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16786+ wake_up(&au_hfsn_wq);
4a4d8108 16787+}
1facf9fc 16788+
027c5e7a 16789+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16790+{
1716fcea 16791+ int err;
027c5e7a
AM
16792+ struct au_hnotify *hn;
16793+ struct super_block *sb;
16794+ struct au_branch *br;
0c5527e5 16795+ struct fsnotify_mark *mark;
027c5e7a 16796+ aufs_bindex_t bindex;
1facf9fc 16797+
027c5e7a
AM
16798+ hn = hinode->hi_notify;
16799+ sb = hn->hn_aufs_inode->i_sb;
16800+ bindex = au_br_index(sb, hinode->hi_id);
16801+ br = au_sbr(sb, bindex);
1716fcea
AM
16802+ AuDebugOn(!br->br_hfsn);
16803+
0c5527e5 16804+ mark = &hn->hn_mark;
ffa93bbd 16805+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16806+ mark->mask = AuHfsnMask;
7f207e10
AM
16807+ /*
16808+ * by udba rename or rmdir, aufs assign a new inode to the known
16809+ * h_inode, so specify 1 to allow dups.
16810+ */
c1595e42 16811+ lockdep_off();
ffa93bbd
AM
16812+ err = fsnotify_add_mark(mark, hinode->hi_inode, /*mnt*/NULL,
16813+ /*allow_dups*/1);
c1595e42 16814+ lockdep_on();
1716fcea
AM
16815+
16816+ return err;
1facf9fc 16817+}
16818+
7eafdf33 16819+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16820+{
0c5527e5 16821+ struct fsnotify_mark *mark;
7eafdf33 16822+ unsigned long long ull;
1716fcea 16823+ struct fsnotify_group *group;
7eafdf33
AM
16824+
16825+ ull = atomic64_inc_return(&au_hfsn_ifree);
16826+ BUG_ON(!ull);
953406b4 16827+
0c5527e5 16828+ mark = &hn->hn_mark;
1716fcea
AM
16829+ spin_lock(&mark->lock);
16830+ group = mark->group;
16831+ fsnotify_get_group(group);
16832+ spin_unlock(&mark->lock);
c1595e42 16833+ lockdep_off();
1716fcea 16834+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16835+ fsnotify_put_mark(mark);
1716fcea 16836+ fsnotify_put_group(group);
c1595e42 16837+ lockdep_on();
7f207e10 16838+
7eafdf33
AM
16839+ /* free hn by myself */
16840+ return 0;
1facf9fc 16841+}
16842+
16843+/* ---------------------------------------------------------------------- */
16844+
4a4d8108 16845+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16846+{
0c5527e5 16847+ struct fsnotify_mark *mark;
1facf9fc 16848+
0c5527e5
AM
16849+ mark = &hinode->hi_notify->hn_mark;
16850+ spin_lock(&mark->lock);
1facf9fc 16851+ if (do_set) {
0c5527e5
AM
16852+ AuDebugOn(mark->mask & AuHfsnMask);
16853+ mark->mask |= AuHfsnMask;
1facf9fc 16854+ } else {
0c5527e5
AM
16855+ AuDebugOn(!(mark->mask & AuHfsnMask));
16856+ mark->mask &= ~AuHfsnMask;
1facf9fc 16857+ }
0c5527e5 16858+ spin_unlock(&mark->lock);
4a4d8108 16859+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 16860+}
16861+
4a4d8108 16862+/* ---------------------------------------------------------------------- */
1facf9fc 16863+
4a4d8108
AM
16864+/* #define AuDbgHnotify */
16865+#ifdef AuDbgHnotify
16866+static char *au_hfsn_name(u32 mask)
16867+{
16868+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
16869+#define test_ret(flag) \
16870+ do { \
16871+ if (mask & flag) \
16872+ return #flag; \
16873+ } while (0)
4a4d8108
AM
16874+ test_ret(FS_ACCESS);
16875+ test_ret(FS_MODIFY);
16876+ test_ret(FS_ATTRIB);
16877+ test_ret(FS_CLOSE_WRITE);
16878+ test_ret(FS_CLOSE_NOWRITE);
16879+ test_ret(FS_OPEN);
16880+ test_ret(FS_MOVED_FROM);
16881+ test_ret(FS_MOVED_TO);
16882+ test_ret(FS_CREATE);
16883+ test_ret(FS_DELETE);
16884+ test_ret(FS_DELETE_SELF);
16885+ test_ret(FS_MOVE_SELF);
16886+ test_ret(FS_UNMOUNT);
16887+ test_ret(FS_Q_OVERFLOW);
16888+ test_ret(FS_IN_IGNORED);
b912730e 16889+ test_ret(FS_ISDIR);
4a4d8108
AM
16890+ test_ret(FS_IN_ONESHOT);
16891+ test_ret(FS_EVENT_ON_CHILD);
16892+ return "";
16893+#undef test_ret
16894+#else
16895+ return "??";
16896+#endif
1facf9fc 16897+}
4a4d8108 16898+#endif
1facf9fc 16899+
16900+/* ---------------------------------------------------------------------- */
16901+
1716fcea
AM
16902+static void au_hfsn_free_group(struct fsnotify_group *group)
16903+{
16904+ struct au_br_hfsnotify *hfsn = group->private;
16905+
5afbbe0d 16906+ /* AuDbg("here\n"); */
1c60b727 16907+ kfree(hfsn);
1716fcea
AM
16908+}
16909+
4a4d8108 16910+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 16911+ struct inode *inode,
0c5527e5
AM
16912+ struct fsnotify_mark *inode_mark,
16913+ struct fsnotify_mark *vfsmount_mark,
a2654f78 16914+ u32 mask, const void *data, int data_type,
ffa93bbd
AM
16915+ const unsigned char *file_name, u32 cookie,
16916+ struct fsnotify_iter_info *iter_info)
1facf9fc 16917+{
16918+ int err;
4a4d8108
AM
16919+ struct au_hnotify *hnotify;
16920+ struct inode *h_dir, *h_inode;
fb47a38f 16921+ struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
4a4d8108 16922+
fb47a38f 16923+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 16924+
16925+ err = 0;
0c5527e5 16926+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 16927+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 16928+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 16929+ goto out;
1facf9fc 16930+
fb47a38f
JR
16931+ h_dir = inode;
16932+ h_inode = NULL;
4a4d8108 16933+#ifdef AuDbgHnotify
392086de 16934+ au_debug_on();
4a4d8108
AM
16935+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16936+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16937+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16938+ h_dir->i_ino, mask, au_hfsn_name(mask),
16939+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16940+ /* WARN_ON(1); */
1facf9fc 16941+ }
392086de 16942+ au_debug_off();
1facf9fc 16943+#endif
4a4d8108 16944+
0c5527e5
AM
16945+ AuDebugOn(!inode_mark);
16946+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16947+ err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
1facf9fc 16948+
4a4d8108
AM
16949+out:
16950+ return err;
16951+}
1facf9fc 16952+
4a4d8108 16953+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 16954+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
16955+ .free_group_priv = au_hfsn_free_group,
16956+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
16957+};
16958+
16959+/* ---------------------------------------------------------------------- */
16960+
027c5e7a
AM
16961+static void au_hfsn_fin_br(struct au_branch *br)
16962+{
1716fcea 16963+ struct au_br_hfsnotify *hfsn;
027c5e7a 16964+
1716fcea 16965+ hfsn = br->br_hfsn;
c1595e42
JR
16966+ if (hfsn) {
16967+ lockdep_off();
1716fcea 16968+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
16969+ lockdep_on();
16970+ }
027c5e7a
AM
16971+}
16972+
1716fcea 16973+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
16974+{
16975+ int err;
1716fcea
AM
16976+ struct fsnotify_group *group;
16977+ struct au_br_hfsnotify *hfsn;
1facf9fc 16978+
4a4d8108 16979+ err = 0;
1716fcea
AM
16980+ br->br_hfsn = NULL;
16981+ if (!au_br_hnotifyable(perm))
027c5e7a 16982+ goto out;
027c5e7a 16983+
1716fcea
AM
16984+ err = -ENOMEM;
16985+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16986+ if (unlikely(!hfsn))
027c5e7a
AM
16987+ goto out;
16988+
1716fcea
AM
16989+ err = 0;
16990+ group = fsnotify_alloc_group(&au_hfsn_ops);
16991+ if (IS_ERR(group)) {
16992+ err = PTR_ERR(group);
0c5527e5 16993+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 16994+ goto out_hfsn;
4a4d8108 16995+ }
1facf9fc 16996+
1716fcea
AM
16997+ group->private = hfsn;
16998+ hfsn->hfsn_group = group;
16999+ br->br_hfsn = hfsn;
17000+ goto out; /* success */
17001+
17002+out_hfsn:
1c60b727 17003+ kfree(hfsn);
027c5e7a 17004+out:
1716fcea
AM
17005+ return err;
17006+}
17007+
17008+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17009+{
17010+ int err;
17011+
17012+ err = 0;
17013+ if (!br->br_hfsn)
17014+ err = au_hfsn_init_br(br, perm);
17015+
1facf9fc 17016+ return err;
17017+}
17018+
7eafdf33
AM
17019+/* ---------------------------------------------------------------------- */
17020+
17021+static void au_hfsn_fin(void)
17022+{
17023+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17024+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17025+}
17026+
4a4d8108
AM
17027+const struct au_hnotify_op au_hnotify_op = {
17028+ .ctl = au_hfsn_ctl,
17029+ .alloc = au_hfsn_alloc,
17030+ .free = au_hfsn_free,
1facf9fc 17031+
7eafdf33
AM
17032+ .fin = au_hfsn_fin,
17033+
027c5e7a
AM
17034+ .reset_br = au_hfsn_reset_br,
17035+ .fin_br = au_hfsn_fin_br,
17036+ .init_br = au_hfsn_init_br
4a4d8108 17037+};
7f207e10
AM
17038diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17039--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 17040+++ linux/fs/aufs/hfsplus.c 2018-04-15 08:49:13.401150731 +0200
523b37e3 17041@@ -0,0 +1,56 @@
4a4d8108 17042+/*
b00004a5 17043+ * Copyright (C) 2010-2018 Junjiro R. Okajima
4a4d8108
AM
17044+ *
17045+ * This program, aufs is free software; you can redistribute it and/or modify
17046+ * it under the terms of the GNU General Public License as published by
17047+ * the Free Software Foundation; either version 2 of the License, or
17048+ * (at your option) any later version.
17049+ *
17050+ * This program is distributed in the hope that it will be useful,
17051+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17052+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17053+ * GNU General Public License for more details.
17054+ *
17055+ * You should have received a copy of the GNU General Public License
523b37e3 17056+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17057+ */
1facf9fc 17058+
4a4d8108
AM
17059+/*
17060+ * special support for filesystems which aqucires an inode mutex
17061+ * at final closing a file, eg, hfsplus.
17062+ *
17063+ * This trick is very simple and stupid, just to open the file before really
17064+ * neceeary open to tell hfsplus that this is not the final closing.
17065+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17066+ * and au_h_open_post() after releasing it.
17067+ */
1facf9fc 17068+
4a4d8108 17069+#include "aufs.h"
1facf9fc 17070+
392086de
AM
17071+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17072+ int force_wr)
4a4d8108
AM
17073+{
17074+ struct file *h_file;
17075+ struct dentry *h_dentry;
1facf9fc 17076+
4a4d8108
AM
17077+ h_dentry = au_h_dptr(dentry, bindex);
17078+ AuDebugOn(!h_dentry);
5527c038 17079+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17080+
17081+ h_file = NULL;
17082+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17083+ && d_is_reg(h_dentry))
4a4d8108
AM
17084+ h_file = au_h_open(dentry, bindex,
17085+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17086+ /*file*/NULL, force_wr);
4a4d8108 17087+ return h_file;
1facf9fc 17088+}
17089+
4a4d8108
AM
17090+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17091+ struct file *h_file)
17092+{
17093+ if (h_file) {
17094+ fput(h_file);
17095+ au_sbr_put(dentry->d_sb, bindex);
17096+ }
17097+}
7f207e10
AM
17098diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17099--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 17100+++ linux/fs/aufs/hnotify.c 2018-06-04 09:08:09.184746078 +0200
8b6a4947 17101@@ -0,0 +1,719 @@
e49829fe 17102+/*
b00004a5 17103+ * Copyright (C) 2005-2018 Junjiro R. Okajima
e49829fe
JR
17104+ *
17105+ * This program, aufs is free software; you can redistribute it and/or modify
17106+ * it under the terms of the GNU General Public License as published by
17107+ * the Free Software Foundation; either version 2 of the License, or
17108+ * (at your option) any later version.
17109+ *
17110+ * This program is distributed in the hope that it will be useful,
17111+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17112+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17113+ * GNU General Public License for more details.
17114+ *
17115+ * You should have received a copy of the GNU General Public License
523b37e3 17116+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17117+ */
17118+
17119+/*
7f207e10 17120+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17121+ */
17122+
17123+#include "aufs.h"
17124+
027c5e7a 17125+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17126+{
17127+ int err;
7f207e10 17128+ struct au_hnotify *hn;
1facf9fc 17129+
4a4d8108
AM
17130+ err = -ENOMEM;
17131+ hn = au_cache_alloc_hnotify();
17132+ if (hn) {
17133+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17134+ hinode->hi_notify = hn;
17135+ err = au_hnotify_op.alloc(hinode);
17136+ AuTraceErr(err);
17137+ if (unlikely(err)) {
17138+ hinode->hi_notify = NULL;
1c60b727 17139+ au_cache_free_hnotify(hn);
4a4d8108
AM
17140+ /*
17141+ * The upper dir was removed by udba, but the same named
17142+ * dir left. In this case, aufs assignes a new inode
17143+ * number and set the monitor again.
17144+ * For the lower dir, the old monitnor is still left.
17145+ */
17146+ if (err == -EEXIST)
17147+ err = 0;
17148+ }
1308ab2a 17149+ }
1308ab2a 17150+
027c5e7a 17151+ AuTraceErr(err);
1308ab2a 17152+ return err;
dece6358 17153+}
1facf9fc 17154+
4a4d8108 17155+void au_hn_free(struct au_hinode *hinode)
dece6358 17156+{
4a4d8108 17157+ struct au_hnotify *hn;
1facf9fc 17158+
4a4d8108
AM
17159+ hn = hinode->hi_notify;
17160+ if (hn) {
4a4d8108 17161+ hinode->hi_notify = NULL;
7eafdf33 17162+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17163+ au_cache_free_hnotify(hn);
4a4d8108
AM
17164+ }
17165+}
dece6358 17166+
4a4d8108 17167+/* ---------------------------------------------------------------------- */
dece6358 17168+
4a4d8108
AM
17169+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17170+{
17171+ if (hinode->hi_notify)
17172+ au_hnotify_op.ctl(hinode, do_set);
17173+}
17174+
17175+void au_hn_reset(struct inode *inode, unsigned int flags)
17176+{
5afbbe0d 17177+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17178+ struct inode *hi;
17179+ struct dentry *iwhdentry;
1facf9fc 17180+
5afbbe0d
AM
17181+ bbot = au_ibbot(inode);
17182+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17183+ hi = au_h_iptr(inode, bindex);
17184+ if (!hi)
17185+ continue;
1308ab2a 17186+
febd17d6 17187+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17188+ iwhdentry = au_hi_wh(inode, bindex);
17189+ if (iwhdentry)
17190+ dget(iwhdentry);
17191+ au_igrab(hi);
17192+ au_set_h_iptr(inode, bindex, NULL, 0);
17193+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17194+ flags & ~AuHi_XINO);
17195+ iput(hi);
17196+ dput(iwhdentry);
febd17d6 17197+ /* inode_unlock(hi); */
1facf9fc 17198+ }
1facf9fc 17199+}
17200+
1308ab2a 17201+/* ---------------------------------------------------------------------- */
1facf9fc 17202+
4a4d8108 17203+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17204+{
4a4d8108 17205+ int err;
5afbbe0d 17206+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17207+ struct inode *h_i;
1facf9fc 17208+
4a4d8108
AM
17209+ err = 0;
17210+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17211+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17212+ goto out;
17213+ }
1facf9fc 17214+
4a4d8108 17215+ bfound = -1;
5afbbe0d
AM
17216+ bbot = au_ibbot(inode);
17217+ btop = au_ibtop(inode);
4a4d8108 17218+#if 0 /* reserved for future use */
5afbbe0d 17219+ if (bindex == bbot) {
4a4d8108
AM
17220+ /* keep this ino in rename case */
17221+ goto out;
17222+ }
17223+#endif
5afbbe0d 17224+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17225+ if (au_h_iptr(inode, bindex) == h_inode) {
17226+ bfound = bindex;
17227+ break;
17228+ }
17229+ if (bfound < 0)
1308ab2a 17230+ goto out;
1facf9fc 17231+
5afbbe0d 17232+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17233+ h_i = au_h_iptr(inode, bindex);
17234+ if (!h_i)
17235+ continue;
1facf9fc 17236+
4a4d8108
AM
17237+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17238+ /* ignore this error */
17239+ /* bad action? */
1facf9fc 17240+ }
1facf9fc 17241+
4a4d8108 17242+ /* children inode number will be broken */
1facf9fc 17243+
4f0767ce 17244+out:
4a4d8108
AM
17245+ AuTraceErr(err);
17246+ return err;
1facf9fc 17247+}
17248+
4a4d8108 17249+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17250+{
4a4d8108
AM
17251+ int err, i, j, ndentry;
17252+ struct au_dcsub_pages dpages;
17253+ struct au_dpage *dpage;
17254+ struct dentry **dentries;
1facf9fc 17255+
4a4d8108
AM
17256+ err = au_dpages_init(&dpages, GFP_NOFS);
17257+ if (unlikely(err))
17258+ goto out;
17259+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17260+ if (unlikely(err))
17261+ goto out_dpages;
1facf9fc 17262+
4a4d8108
AM
17263+ for (i = 0; i < dpages.ndpage; i++) {
17264+ dpage = dpages.dpages + i;
17265+ dentries = dpage->dentries;
17266+ ndentry = dpage->ndentry;
17267+ for (j = 0; j < ndentry; j++) {
17268+ struct dentry *d;
17269+
17270+ d = dentries[j];
17271+ if (IS_ROOT(d))
17272+ continue;
17273+
4a4d8108 17274+ au_digen_dec(d);
5527c038 17275+ if (d_really_is_positive(d))
4a4d8108
AM
17276+ /* todo: reset children xino?
17277+ cached children only? */
5527c038 17278+ au_iigen_dec(d_inode(d));
1308ab2a 17279+ }
dece6358 17280+ }
1facf9fc 17281+
4f0767ce 17282+out_dpages:
4a4d8108 17283+ au_dpages_free(&dpages);
dece6358 17284+
027c5e7a 17285+#if 0
4a4d8108
AM
17286+ /* discard children */
17287+ dentry_unhash(dentry);
17288+ dput(dentry);
027c5e7a 17289+#endif
4f0767ce 17290+out:
dece6358
AM
17291+ return err;
17292+}
17293+
1308ab2a 17294+/*
4a4d8108 17295+ * return 0 if processed.
1308ab2a 17296+ */
4a4d8108
AM
17297+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17298+ const unsigned int isdir)
dece6358 17299+{
1308ab2a 17300+ int err;
4a4d8108
AM
17301+ struct dentry *d;
17302+ struct qstr *dname;
1facf9fc 17303+
4a4d8108
AM
17304+ err = 1;
17305+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17306+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17307+ err = 0;
17308+ goto out;
17309+ }
dece6358 17310+
4a4d8108
AM
17311+ if (!isdir) {
17312+ AuDebugOn(!name);
17313+ au_iigen_dec(inode);
027c5e7a 17314+ spin_lock(&inode->i_lock);
c1595e42 17315+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17316+ spin_lock(&d->d_lock);
4a4d8108
AM
17317+ dname = &d->d_name;
17318+ if (dname->len != nlen
027c5e7a
AM
17319+ && memcmp(dname->name, name, nlen)) {
17320+ spin_unlock(&d->d_lock);
4a4d8108 17321+ continue;
027c5e7a 17322+ }
4a4d8108 17323+ err = 0;
4a4d8108
AM
17324+ au_digen_dec(d);
17325+ spin_unlock(&d->d_lock);
17326+ break;
1facf9fc 17327+ }
027c5e7a 17328+ spin_unlock(&inode->i_lock);
1308ab2a 17329+ } else {
027c5e7a 17330+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17331+ d = d_find_any_alias(inode);
4a4d8108
AM
17332+ if (!d) {
17333+ au_iigen_dec(inode);
17334+ goto out;
17335+ }
1facf9fc 17336+
027c5e7a 17337+ spin_lock(&d->d_lock);
4a4d8108 17338+ dname = &d->d_name;
027c5e7a
AM
17339+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17340+ spin_unlock(&d->d_lock);
4a4d8108 17341+ err = hn_gen_tree(d);
027c5e7a
AM
17342+ spin_lock(&d->d_lock);
17343+ }
17344+ spin_unlock(&d->d_lock);
4a4d8108
AM
17345+ dput(d);
17346+ }
1facf9fc 17347+
4f0767ce 17348+out:
4a4d8108 17349+ AuTraceErr(err);
1308ab2a 17350+ return err;
17351+}
dece6358 17352+
4a4d8108 17353+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17354+{
4a4d8108 17355+ int err;
1facf9fc 17356+
5527c038 17357+ if (IS_ROOT(dentry)) {
0c3ec466 17358+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17359+ return 0;
17360+ }
1308ab2a 17361+
4a4d8108
AM
17362+ err = 0;
17363+ if (!isdir) {
4a4d8108 17364+ au_digen_dec(dentry);
5527c038
JR
17365+ if (d_really_is_positive(dentry))
17366+ au_iigen_dec(d_inode(dentry));
4a4d8108 17367+ } else {
027c5e7a 17368+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17369+ if (d_really_is_positive(dentry))
4a4d8108
AM
17370+ err = hn_gen_tree(dentry);
17371+ }
17372+
17373+ AuTraceErr(err);
17374+ return err;
1facf9fc 17375+}
17376+
4a4d8108 17377+/* ---------------------------------------------------------------------- */
1facf9fc 17378+
4a4d8108
AM
17379+/* hnotify job flags */
17380+#define AuHnJob_XINO0 1
17381+#define AuHnJob_GEN (1 << 1)
17382+#define AuHnJob_DIRENT (1 << 2)
17383+#define AuHnJob_ISDIR (1 << 3)
17384+#define AuHnJob_TRYXINO0 (1 << 4)
17385+#define AuHnJob_MNTPNT (1 << 5)
17386+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17387+#define au_fset_hnjob(flags, name) \
17388+ do { (flags) |= AuHnJob_##name; } while (0)
17389+#define au_fclr_hnjob(flags, name) \
17390+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17391+
4a4d8108
AM
17392+enum {
17393+ AuHn_CHILD,
17394+ AuHn_PARENT,
17395+ AuHnLast
17396+};
1facf9fc 17397+
4a4d8108
AM
17398+struct au_hnotify_args {
17399+ struct inode *h_dir, *dir, *h_child_inode;
17400+ u32 mask;
17401+ unsigned int flags[AuHnLast];
17402+ unsigned int h_child_nlen;
17403+ char h_child_name[];
17404+};
1facf9fc 17405+
4a4d8108
AM
17406+struct hn_job_args {
17407+ unsigned int flags;
17408+ struct inode *inode, *h_inode, *dir, *h_dir;
17409+ struct dentry *dentry;
17410+ char *h_name;
17411+ int h_nlen;
17412+};
1308ab2a 17413+
4a4d8108
AM
17414+static int hn_job(struct hn_job_args *a)
17415+{
17416+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17417+ int e;
1308ab2a 17418+
4a4d8108
AM
17419+ /* reset xino */
17420+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17421+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17422+
4a4d8108
AM
17423+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17424+ && a->inode
17425+ && a->h_inode) {
be118d29 17426+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17427+ if (!a->h_inode->i_nlink
17428+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17429+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17430+ inode_unlock_shared(a->h_inode);
1308ab2a 17431+ }
1facf9fc 17432+
4a4d8108
AM
17433+ /* make the generation obsolete */
17434+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17435+ e = -1;
4a4d8108 17436+ if (a->inode)
076b876e 17437+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17438+ isdir);
076b876e 17439+ if (e && a->dentry)
4a4d8108
AM
17440+ hn_gen_by_name(a->dentry, isdir);
17441+ /* ignore this error */
1facf9fc 17442+ }
1facf9fc 17443+
4a4d8108
AM
17444+ /* make dir entries obsolete */
17445+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17446+ struct au_vdir *vdir;
1facf9fc 17447+
4a4d8108
AM
17448+ vdir = au_ivdir(a->inode);
17449+ if (vdir)
17450+ vdir->vd_jiffy = 0;
17451+ /* IMustLock(a->inode); */
be118d29 17452+ /* inode_inc_iversion(a->inode); */
4a4d8108 17453+ }
1facf9fc 17454+
4a4d8108
AM
17455+ /* can do nothing but warn */
17456+ if (au_ftest_hnjob(a->flags, MNTPNT)
17457+ && a->dentry
17458+ && d_mountpoint(a->dentry))
523b37e3 17459+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17460+
4a4d8108 17461+ return 0;
1308ab2a 17462+}
1facf9fc 17463+
1308ab2a 17464+/* ---------------------------------------------------------------------- */
1facf9fc 17465+
4a4d8108
AM
17466+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17467+ struct inode *dir)
1308ab2a 17468+{
4a4d8108
AM
17469+ struct dentry *dentry, *d, *parent;
17470+ struct qstr *dname;
1308ab2a 17471+
c1595e42 17472+ parent = d_find_any_alias(dir);
4a4d8108
AM
17473+ if (!parent)
17474+ return NULL;
1308ab2a 17475+
4a4d8108 17476+ dentry = NULL;
027c5e7a 17477+ spin_lock(&parent->d_lock);
c1595e42 17478+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17479+ /* AuDbg("%pd\n", d); */
027c5e7a 17480+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17481+ dname = &d->d_name;
17482+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17483+ goto cont_unlock;
17484+ if (au_di(d))
17485+ au_digen_dec(d);
17486+ else
17487+ goto cont_unlock;
c1595e42 17488+ if (au_dcount(d) > 0) {
027c5e7a 17489+ dentry = dget_dlock(d);
4a4d8108 17490+ spin_unlock(&d->d_lock);
027c5e7a 17491+ break;
dece6358 17492+ }
1facf9fc 17493+
f6b6e03d 17494+cont_unlock:
027c5e7a 17495+ spin_unlock(&d->d_lock);
1308ab2a 17496+ }
027c5e7a 17497+ spin_unlock(&parent->d_lock);
4a4d8108 17498+ dput(parent);
1facf9fc 17499+
4a4d8108
AM
17500+ if (dentry)
17501+ di_write_lock_child(dentry);
1308ab2a 17502+
4a4d8108
AM
17503+ return dentry;
17504+}
dece6358 17505+
4a4d8108
AM
17506+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17507+ aufs_bindex_t bindex, ino_t h_ino)
17508+{
17509+ struct inode *inode;
17510+ ino_t ino;
17511+ int err;
17512+
17513+ inode = NULL;
17514+ err = au_xino_read(sb, bindex, h_ino, &ino);
17515+ if (!err && ino)
17516+ inode = ilookup(sb, ino);
17517+ if (!inode)
17518+ goto out;
17519+
17520+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17521+ pr_warn("wrong root branch\n");
4a4d8108
AM
17522+ iput(inode);
17523+ inode = NULL;
17524+ goto out;
1308ab2a 17525+ }
17526+
4a4d8108 17527+ ii_write_lock_child(inode);
1308ab2a 17528+
4f0767ce 17529+out:
4a4d8108 17530+ return inode;
dece6358
AM
17531+}
17532+
4a4d8108 17533+static void au_hn_bh(void *_args)
1facf9fc 17534+{
4a4d8108
AM
17535+ struct au_hnotify_args *a = _args;
17536+ struct super_block *sb;
5afbbe0d 17537+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17538+ unsigned char xino, try_iput;
1facf9fc 17539+ int err;
1308ab2a 17540+ struct inode *inode;
4a4d8108
AM
17541+ ino_t h_ino;
17542+ struct hn_job_args args;
17543+ struct dentry *dentry;
17544+ struct au_sbinfo *sbinfo;
1facf9fc 17545+
4a4d8108
AM
17546+ AuDebugOn(!_args);
17547+ AuDebugOn(!a->h_dir);
17548+ AuDebugOn(!a->dir);
17549+ AuDebugOn(!a->mask);
17550+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17551+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17552+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17553+
4a4d8108
AM
17554+ inode = NULL;
17555+ dentry = NULL;
17556+ /*
17557+ * do not lock a->dir->i_mutex here
17558+ * because of d_revalidate() may cause a deadlock.
17559+ */
17560+ sb = a->dir->i_sb;
17561+ AuDebugOn(!sb);
17562+ sbinfo = au_sbi(sb);
17563+ AuDebugOn(!sbinfo);
7f207e10 17564+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17565+
8b6a4947
AM
17566+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17567+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17568+ case FS_MOVED_FROM:
17569+ case FS_MOVED_TO:
17570+ AuWarn1("DIRREN with UDBA may not work correctly "
17571+ "for the direct rename(2)\n");
17572+ }
17573+
4a4d8108
AM
17574+ ii_read_lock_parent(a->dir);
17575+ bfound = -1;
5afbbe0d
AM
17576+ bbot = au_ibbot(a->dir);
17577+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17578+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17579+ bfound = bindex;
17580+ break;
17581+ }
17582+ ii_read_unlock(a->dir);
17583+ if (unlikely(bfound < 0))
17584+ goto out;
1facf9fc 17585+
4a4d8108
AM
17586+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17587+ h_ino = 0;
17588+ if (a->h_child_inode)
17589+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17590+
4a4d8108
AM
17591+ if (a->h_child_nlen
17592+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17593+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17594+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17595+ a->dir);
17596+ try_iput = 0;
5527c038
JR
17597+ if (dentry && d_really_is_positive(dentry))
17598+ inode = d_inode(dentry);
4a4d8108
AM
17599+ if (xino && !inode && h_ino
17600+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17601+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17602+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17603+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17604+ try_iput = 1;
f0c0a007 17605+ }
1facf9fc 17606+
4a4d8108
AM
17607+ args.flags = a->flags[AuHn_CHILD];
17608+ args.dentry = dentry;
17609+ args.inode = inode;
17610+ args.h_inode = a->h_child_inode;
17611+ args.dir = a->dir;
17612+ args.h_dir = a->h_dir;
17613+ args.h_name = a->h_child_name;
17614+ args.h_nlen = a->h_child_nlen;
17615+ err = hn_job(&args);
17616+ if (dentry) {
027c5e7a 17617+ if (au_di(dentry))
4a4d8108
AM
17618+ di_write_unlock(dentry);
17619+ dput(dentry);
17620+ }
17621+ if (inode && try_iput) {
17622+ ii_write_unlock(inode);
17623+ iput(inode);
17624+ }
1facf9fc 17625+
4a4d8108
AM
17626+ ii_write_lock_parent(a->dir);
17627+ args.flags = a->flags[AuHn_PARENT];
17628+ args.dentry = NULL;
17629+ args.inode = a->dir;
17630+ args.h_inode = a->h_dir;
17631+ args.dir = NULL;
17632+ args.h_dir = NULL;
17633+ args.h_name = NULL;
17634+ args.h_nlen = 0;
17635+ err = hn_job(&args);
17636+ ii_write_unlock(a->dir);
1facf9fc 17637+
4f0767ce 17638+out:
4a4d8108
AM
17639+ iput(a->h_child_inode);
17640+ iput(a->h_dir);
17641+ iput(a->dir);
027c5e7a
AM
17642+ si_write_unlock(sb);
17643+ au_nwt_done(&sbinfo->si_nowait);
1c60b727 17644+ kfree(a);
dece6358 17645+}
1facf9fc 17646+
4a4d8108
AM
17647+/* ---------------------------------------------------------------------- */
17648+
17649+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17650+ struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17651+{
4a4d8108 17652+ int err, len;
53392da6 17653+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17654+ unsigned char isdir, isroot, wh;
17655+ struct inode *dir;
17656+ struct au_hnotify_args *args;
17657+ char *p, *h_child_name;
dece6358 17658+
1308ab2a 17659+ err = 0;
4a4d8108
AM
17660+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17661+ dir = igrab(hnotify->hn_aufs_inode);
17662+ if (!dir)
17663+ goto out;
1facf9fc 17664+
4a4d8108
AM
17665+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17666+ wh = 0;
17667+ h_child_name = (void *)h_child_qstr->name;
17668+ len = h_child_qstr->len;
17669+ if (h_child_name) {
17670+ if (len > AUFS_WH_PFX_LEN
17671+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17672+ h_child_name += AUFS_WH_PFX_LEN;
17673+ len -= AUFS_WH_PFX_LEN;
17674+ wh = 1;
17675+ }
1facf9fc 17676+ }
dece6358 17677+
4a4d8108
AM
17678+ isdir = 0;
17679+ if (h_child_inode)
17680+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17681+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17682+ flags[AuHn_CHILD] = 0;
17683+ if (isdir)
17684+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17685+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17686+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17687+ switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17688+ case FS_MOVED_FROM:
17689+ case FS_MOVED_TO:
17690+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17691+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17692+ /*FALLTHROUGH*/
17693+ case FS_CREATE:
fb47a38f 17694+ AuDebugOn(!h_child_name);
4a4d8108 17695+ break;
1facf9fc 17696+
4a4d8108
AM
17697+ case FS_DELETE:
17698+ /*
17699+ * aufs never be able to get this child inode.
17700+ * revalidation should be in d_revalidate()
17701+ * by checking i_nlink, i_generation or d_unhashed().
17702+ */
17703+ AuDebugOn(!h_child_name);
17704+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17705+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17706+ break;
dece6358 17707+
4a4d8108
AM
17708+ default:
17709+ AuDebugOn(1);
17710+ }
1308ab2a 17711+
4a4d8108
AM
17712+ if (wh)
17713+ h_child_inode = NULL;
1308ab2a 17714+
4a4d8108
AM
17715+ err = -ENOMEM;
17716+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17717+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17718+ if (unlikely(!args)) {
17719+ AuErr1("no memory\n");
17720+ iput(dir);
17721+ goto out;
17722+ }
17723+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17724+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17725+ args->mask = mask;
17726+ args->dir = dir;
17727+ args->h_dir = igrab(h_dir);
17728+ if (h_child_inode)
17729+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17730+ args->h_child_inode = h_child_inode;
17731+ args->h_child_nlen = len;
17732+ if (len) {
17733+ p = (void *)args;
17734+ p += sizeof(*args);
17735+ memcpy(p, h_child_name, len);
17736+ p[len] = 0;
1308ab2a 17737+ }
1308ab2a 17738+
38d290e6 17739+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17740+ f = 0;
38d290e6
JR
17741+ if (!dir->i_nlink
17742+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17743+ f = AuWkq_NEST;
17744+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17745+ if (unlikely(err)) {
17746+ pr_err("wkq %d\n", err);
17747+ iput(args->h_child_inode);
17748+ iput(args->h_dir);
17749+ iput(args->dir);
1c60b727 17750+ kfree(args);
1facf9fc 17751+ }
1facf9fc 17752+
4a4d8108 17753+out:
1facf9fc 17754+ return err;
17755+}
17756+
027c5e7a
AM
17757+/* ---------------------------------------------------------------------- */
17758+
17759+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17760+{
17761+ int err;
17762+
17763+ AuDebugOn(!(udba & AuOptMask_UDBA));
17764+
17765+ err = 0;
17766+ if (au_hnotify_op.reset_br)
17767+ err = au_hnotify_op.reset_br(udba, br, perm);
17768+
17769+ return err;
17770+}
17771+
17772+int au_hnotify_init_br(struct au_branch *br, int perm)
17773+{
17774+ int err;
17775+
17776+ err = 0;
17777+ if (au_hnotify_op.init_br)
17778+ err = au_hnotify_op.init_br(br, perm);
17779+
17780+ return err;
17781+}
17782+
17783+void au_hnotify_fin_br(struct au_branch *br)
17784+{
17785+ if (au_hnotify_op.fin_br)
17786+ au_hnotify_op.fin_br(br);
17787+}
17788+
4a4d8108
AM
17789+static void au_hn_destroy_cache(void)
17790+{
1c60b727
AM
17791+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17792+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17793+}
1308ab2a 17794+
4a4d8108 17795+int __init au_hnotify_init(void)
1facf9fc 17796+{
1308ab2a 17797+ int err;
1308ab2a 17798+
4a4d8108 17799+ err = -ENOMEM;
1c60b727
AM
17800+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17801+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17802+ err = 0;
17803+ if (au_hnotify_op.init)
17804+ err = au_hnotify_op.init();
4a4d8108
AM
17805+ if (unlikely(err))
17806+ au_hn_destroy_cache();
1308ab2a 17807+ }
1308ab2a 17808+ AuTraceErr(err);
4a4d8108 17809+ return err;
1308ab2a 17810+}
17811+
4a4d8108 17812+void au_hnotify_fin(void)
1308ab2a 17813+{
027c5e7a
AM
17814+ if (au_hnotify_op.fin)
17815+ au_hnotify_op.fin();
f0c0a007 17816+
4a4d8108 17817+ /* cf. au_cache_fin() */
1c60b727 17818+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17819+ au_hn_destroy_cache();
dece6358 17820+}
7f207e10
AM
17821diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17822--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 17823+++ linux/fs/aufs/iinfo.c 2018-04-15 08:49:13.401150731 +0200
e2f27e51 17824@@ -0,0 +1,285 @@
dece6358 17825+/*
b00004a5 17826+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
17827+ *
17828+ * This program, aufs is free software; you can redistribute it and/or modify
17829+ * it under the terms of the GNU General Public License as published by
17830+ * the Free Software Foundation; either version 2 of the License, or
17831+ * (at your option) any later version.
17832+ *
17833+ * This program is distributed in the hope that it will be useful,
17834+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17835+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17836+ * GNU General Public License for more details.
17837+ *
17838+ * You should have received a copy of the GNU General Public License
523b37e3 17839+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17840+ */
1facf9fc 17841+
dece6358 17842+/*
4a4d8108 17843+ * inode private data
dece6358 17844+ */
1facf9fc 17845+
1308ab2a 17846+#include "aufs.h"
1facf9fc 17847+
4a4d8108 17848+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17849+{
4a4d8108 17850+ struct inode *h_inode;
5afbbe0d 17851+ struct au_hinode *hinode;
1facf9fc 17852+
4a4d8108 17853+ IiMustAnyLock(inode);
1facf9fc 17854+
5afbbe0d
AM
17855+ hinode = au_hinode(au_ii(inode), bindex);
17856+ h_inode = hinode->hi_inode;
4a4d8108
AM
17857+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17858+ return h_inode;
17859+}
1facf9fc 17860+
4a4d8108
AM
17861+/* todo: hard/soft set? */
17862+void au_hiput(struct au_hinode *hinode)
17863+{
17864+ au_hn_free(hinode);
17865+ dput(hinode->hi_whdentry);
17866+ iput(hinode->hi_inode);
17867+}
1facf9fc 17868+
4a4d8108
AM
17869+unsigned int au_hi_flags(struct inode *inode, int isdir)
17870+{
17871+ unsigned int flags;
17872+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 17873+
4a4d8108
AM
17874+ flags = 0;
17875+ if (au_opt_test(mnt_flags, XINO))
17876+ au_fset_hi(flags, XINO);
17877+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17878+ au_fset_hi(flags, HNOTIFY);
17879+ return flags;
1facf9fc 17880+}
17881+
4a4d8108
AM
17882+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17883+ struct inode *h_inode, unsigned int flags)
1308ab2a 17884+{
4a4d8108
AM
17885+ struct au_hinode *hinode;
17886+ struct inode *hi;
17887+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 17888+
4a4d8108 17889+ IiMustWriteLock(inode);
dece6358 17890+
5afbbe0d 17891+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
17892+ hi = hinode->hi_inode;
17893+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17894+
17895+ if (hi)
17896+ au_hiput(hinode);
17897+ hinode->hi_inode = h_inode;
17898+ if (h_inode) {
17899+ int err;
17900+ struct super_block *sb = inode->i_sb;
17901+ struct au_branch *br;
17902+
027c5e7a
AM
17903+ AuDebugOn(inode->i_mode
17904+ && (h_inode->i_mode & S_IFMT)
17905+ != (inode->i_mode & S_IFMT));
5afbbe0d 17906+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
17907+ au_cpup_igen(inode, h_inode);
17908+ br = au_sbr(sb, bindex);
17909+ hinode->hi_id = br->br_id;
17910+ if (au_ftest_hi(flags, XINO)) {
17911+ err = au_xino_write(sb, bindex, h_inode->i_ino,
17912+ inode->i_ino);
17913+ if (unlikely(err))
17914+ AuIOErr1("failed au_xino_write() %d\n", err);
17915+ }
17916+
17917+ if (au_ftest_hi(flags, HNOTIFY)
17918+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 17919+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
17920+ if (unlikely(err))
17921+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 17922+ }
17923+ }
4a4d8108 17924+}
dece6358 17925+
4a4d8108
AM
17926+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17927+ struct dentry *h_wh)
17928+{
17929+ struct au_hinode *hinode;
dece6358 17930+
4a4d8108
AM
17931+ IiMustWriteLock(inode);
17932+
5afbbe0d 17933+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
17934+ AuDebugOn(hinode->hi_whdentry);
17935+ hinode->hi_whdentry = h_wh;
1facf9fc 17936+}
17937+
537831f9 17938+void au_update_iigen(struct inode *inode, int half)
1308ab2a 17939+{
537831f9
AM
17940+ struct au_iinfo *iinfo;
17941+ struct au_iigen *iigen;
17942+ unsigned int sigen;
17943+
17944+ sigen = au_sigen(inode->i_sb);
17945+ iinfo = au_ii(inode);
17946+ iigen = &iinfo->ii_generation;
be52b249 17947+ spin_lock(&iigen->ig_spin);
537831f9
AM
17948+ iigen->ig_generation = sigen;
17949+ if (half)
17950+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17951+ else
17952+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 17953+ spin_unlock(&iigen->ig_spin);
4a4d8108 17954+}
1facf9fc 17955+
4a4d8108
AM
17956+/* it may be called at remount time, too */
17957+void au_update_ibrange(struct inode *inode, int do_put_zero)
17958+{
17959+ struct au_iinfo *iinfo;
5afbbe0d 17960+ aufs_bindex_t bindex, bbot;
1facf9fc 17961+
5afbbe0d 17962+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 17963+ IiMustWriteLock(inode);
1facf9fc 17964+
5afbbe0d
AM
17965+ iinfo = au_ii(inode);
17966+ if (do_put_zero && iinfo->ii_btop >= 0) {
17967+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
17968+ bindex++) {
17969+ struct inode *h_i;
1facf9fc 17970+
5afbbe0d 17971+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
17972+ if (h_i
17973+ && !h_i->i_nlink
17974+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
17975+ au_set_h_iptr(inode, bindex, NULL, 0);
17976+ }
4a4d8108
AM
17977+ }
17978+
5afbbe0d
AM
17979+ iinfo->ii_btop = -1;
17980+ iinfo->ii_bbot = -1;
17981+ bbot = au_sbbot(inode->i_sb);
17982+ for (bindex = 0; bindex <= bbot; bindex++)
17983+ if (au_hinode(iinfo, bindex)->hi_inode) {
17984+ iinfo->ii_btop = bindex;
4a4d8108 17985+ break;
027c5e7a 17986+ }
5afbbe0d
AM
17987+ if (iinfo->ii_btop >= 0)
17988+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17989+ if (au_hinode(iinfo, bindex)->hi_inode) {
17990+ iinfo->ii_bbot = bindex;
027c5e7a
AM
17991+ break;
17992+ }
5afbbe0d 17993+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 17994+}
1facf9fc 17995+
dece6358 17996+/* ---------------------------------------------------------------------- */
1facf9fc 17997+
4a4d8108 17998+void au_icntnr_init_once(void *_c)
dece6358 17999+{
4a4d8108
AM
18000+ struct au_icntnr *c = _c;
18001+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 18002+
be52b249 18003+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
18004+ au_rw_init(&iinfo->ii_rwsem);
18005+ inode_init_once(&c->vfs_inode);
18006+}
1facf9fc 18007+
5afbbe0d
AM
18008+void au_hinode_init(struct au_hinode *hinode)
18009+{
18010+ hinode->hi_inode = NULL;
18011+ hinode->hi_id = -1;
18012+ au_hn_init(hinode);
18013+ hinode->hi_whdentry = NULL;
18014+}
18015+
4a4d8108
AM
18016+int au_iinfo_init(struct inode *inode)
18017+{
18018+ struct au_iinfo *iinfo;
18019+ struct super_block *sb;
5afbbe0d 18020+ struct au_hinode *hi;
4a4d8108 18021+ int nbr, i;
1facf9fc 18022+
4a4d8108
AM
18023+ sb = inode->i_sb;
18024+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 18025+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
18026+ if (unlikely(nbr <= 0))
18027+ nbr = 1;
5afbbe0d
AM
18028+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18029+ if (hi) {
7f207e10 18030+ au_ninodes_inc(sb);
5afbbe0d
AM
18031+
18032+ iinfo->ii_hinode = hi;
18033+ for (i = 0; i < nbr; i++, hi++)
18034+ au_hinode_init(hi);
1facf9fc 18035+
537831f9 18036+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
18037+ iinfo->ii_btop = -1;
18038+ iinfo->ii_bbot = -1;
4a4d8108
AM
18039+ iinfo->ii_vdir = NULL;
18040+ return 0;
1308ab2a 18041+ }
4a4d8108
AM
18042+ return -ENOMEM;
18043+}
1facf9fc 18044+
e2f27e51 18045+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 18046+{
5afbbe0d 18047+ int err, i;
4a4d8108 18048+ struct au_hinode *hip;
1facf9fc 18049+
4a4d8108
AM
18050+ AuRwMustWriteLock(&iinfo->ii_rwsem);
18051+
18052+ err = -ENOMEM;
e2f27e51
AM
18053+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18054+ may_shrink);
4a4d8108
AM
18055+ if (hip) {
18056+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18057+ i = iinfo->ii_bbot + 1;
18058+ hip += i;
18059+ for (; i < nbr; i++, hip++)
18060+ au_hinode_init(hip);
4a4d8108 18061+ err = 0;
1308ab2a 18062+ }
4a4d8108 18063+
1308ab2a 18064+ return err;
1facf9fc 18065+}
18066+
4a4d8108 18067+void au_iinfo_fin(struct inode *inode)
1facf9fc 18068+{
4a4d8108
AM
18069+ struct au_iinfo *iinfo;
18070+ struct au_hinode *hi;
18071+ struct super_block *sb;
5afbbe0d 18072+ aufs_bindex_t bindex, bbot;
b752ccd1 18073+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18074+
5afbbe0d 18075+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18076+
b752ccd1 18077+ sb = inode->i_sb;
7f207e10 18078+ au_ninodes_dec(sb);
b752ccd1
AM
18079+ if (si_pid_test(sb))
18080+ au_xino_delete_inode(inode, unlinked);
18081+ else {
18082+ /*
18083+ * it is safe to hide the dependency between sbinfo and
18084+ * sb->s_umount.
18085+ */
18086+ lockdep_off();
18087+ si_noflush_read_lock(sb);
18088+ au_xino_delete_inode(inode, unlinked);
18089+ si_read_unlock(sb);
18090+ lockdep_on();
18091+ }
18092+
5afbbe0d 18093+ iinfo = au_ii(inode);
4a4d8108 18094+ if (iinfo->ii_vdir)
1c60b727 18095+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18096+
5afbbe0d 18097+ bindex = iinfo->ii_btop;
b752ccd1 18098+ if (bindex >= 0) {
5afbbe0d
AM
18099+ hi = au_hinode(iinfo, bindex);
18100+ bbot = iinfo->ii_bbot;
18101+ while (bindex++ <= bbot) {
b752ccd1 18102+ if (hi->hi_inode)
4a4d8108 18103+ au_hiput(hi);
4a4d8108
AM
18104+ hi++;
18105+ }
18106+ }
1c60b727 18107+ kfree(iinfo->ii_hinode);
4a4d8108 18108+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18109+}
7f207e10
AM
18110diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18111--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 18112+++ linux/fs/aufs/inode.c 2018-06-04 09:08:09.184746078 +0200
521ced18 18113@@ -0,0 +1,527 @@
4a4d8108 18114+/*
b00004a5 18115+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
18116+ *
18117+ * This program, aufs is free software; you can redistribute it and/or modify
18118+ * it under the terms of the GNU General Public License as published by
18119+ * the Free Software Foundation; either version 2 of the License, or
18120+ * (at your option) any later version.
18121+ *
18122+ * This program is distributed in the hope that it will be useful,
18123+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18124+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18125+ * GNU General Public License for more details.
18126+ *
18127+ * You should have received a copy of the GNU General Public License
523b37e3 18128+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18129+ */
1facf9fc 18130+
4a4d8108
AM
18131+/*
18132+ * inode functions
18133+ */
1facf9fc 18134+
4a4d8108 18135+#include "aufs.h"
1308ab2a 18136+
4a4d8108
AM
18137+struct inode *au_igrab(struct inode *inode)
18138+{
18139+ if (inode) {
18140+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18141+ ihold(inode);
1facf9fc 18142+ }
4a4d8108
AM
18143+ return inode;
18144+}
1facf9fc 18145+
4a4d8108
AM
18146+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18147+{
18148+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18149+ au_update_iigen(inode, /*half*/1);
4a4d8108 18150+ if (do_version)
be118d29 18151+ inode_inc_iversion(inode);
dece6358 18152+}
1facf9fc 18153+
027c5e7a 18154+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18155+{
e2f27e51 18156+ int err, e, nbr;
027c5e7a 18157+ umode_t type;
4a4d8108 18158+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18159+ struct super_block *sb;
4a4d8108 18160+ struct au_iinfo *iinfo;
027c5e7a 18161+ struct au_hinode *p, *q, tmp;
1facf9fc 18162+
5afbbe0d 18163+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18164+ IiMustWriteLock(inode);
1facf9fc 18165+
027c5e7a 18166+ *update = 0;
4a4d8108 18167+ sb = inode->i_sb;
e2f27e51 18168+ nbr = au_sbbot(sb) + 1;
027c5e7a 18169+ type = inode->i_mode & S_IFMT;
4a4d8108 18170+ iinfo = au_ii(inode);
e2f27e51 18171+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18172+ if (unlikely(err))
1308ab2a 18173+ goto out;
1facf9fc 18174+
5afbbe0d
AM
18175+ AuDebugOn(iinfo->ii_btop < 0);
18176+ p = au_hinode(iinfo, iinfo->ii_btop);
18177+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18178+ bindex++, p++) {
18179+ if (!p->hi_inode)
18180+ continue;
1facf9fc 18181+
027c5e7a 18182+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18183+ new_bindex = au_br_index(sb, p->hi_id);
18184+ if (new_bindex == bindex)
18185+ continue;
1facf9fc 18186+
4a4d8108 18187+ if (new_bindex < 0) {
027c5e7a 18188+ *update = 1;
4a4d8108
AM
18189+ au_hiput(p);
18190+ p->hi_inode = NULL;
18191+ continue;
1308ab2a 18192+ }
4a4d8108 18193+
5afbbe0d
AM
18194+ if (new_bindex < iinfo->ii_btop)
18195+ iinfo->ii_btop = new_bindex;
18196+ if (iinfo->ii_bbot < new_bindex)
18197+ iinfo->ii_bbot = new_bindex;
4a4d8108 18198+ /* swap two lower inode, and loop again */
5afbbe0d 18199+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18200+ tmp = *q;
18201+ *q = *p;
18202+ *p = tmp;
18203+ if (tmp.hi_inode) {
18204+ bindex--;
18205+ p--;
1308ab2a 18206+ }
18207+ }
4a4d8108 18208+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18209+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18210+ e = au_dy_irefresh(inode);
18211+ if (unlikely(e && !err))
18212+ err = e;
1facf9fc 18213+
4f0767ce 18214+out:
027c5e7a
AM
18215+ AuTraceErr(err);
18216+ return err;
18217+}
18218+
b95c5147
AM
18219+void au_refresh_iop(struct inode *inode, int force_getattr)
18220+{
18221+ int type;
18222+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18223+ const struct inode_operations *iop
18224+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18225+
18226+ if (inode->i_op == iop)
18227+ return;
18228+
18229+ switch (inode->i_mode & S_IFMT) {
18230+ case S_IFDIR:
18231+ type = AuIop_DIR;
18232+ break;
18233+ case S_IFLNK:
18234+ type = AuIop_SYMLINK;
18235+ break;
18236+ default:
18237+ type = AuIop_OTHER;
18238+ break;
18239+ }
18240+
18241+ inode->i_op = iop + type;
18242+ /* unnecessary smp_wmb() */
18243+}
18244+
027c5e7a
AM
18245+int au_refresh_hinode_self(struct inode *inode)
18246+{
18247+ int err, update;
18248+
18249+ err = au_ii_refresh(inode, &update);
18250+ if (!err)
18251+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18252+
18253+ AuTraceErr(err);
4a4d8108
AM
18254+ return err;
18255+}
1facf9fc 18256+
4a4d8108
AM
18257+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18258+{
027c5e7a 18259+ int err, e, update;
4a4d8108 18260+ unsigned int flags;
027c5e7a 18261+ umode_t mode;
5afbbe0d 18262+ aufs_bindex_t bindex, bbot;
027c5e7a 18263+ unsigned char isdir;
4a4d8108
AM
18264+ struct au_hinode *p;
18265+ struct au_iinfo *iinfo;
1facf9fc 18266+
027c5e7a 18267+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18268+ if (unlikely(err))
18269+ goto out;
18270+
18271+ update = 0;
18272+ iinfo = au_ii(inode);
5afbbe0d 18273+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18274+ mode = (inode->i_mode & S_IFMT);
18275+ isdir = S_ISDIR(mode);
4a4d8108 18276+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18277+ bbot = au_dbbot(dentry);
18278+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18279+ struct inode *h_i, *h_inode;
4a4d8108
AM
18280+ struct dentry *h_d;
18281+
18282+ h_d = au_h_dptr(dentry, bindex);
5527c038 18283+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18284+ continue;
18285+
5527c038
JR
18286+ h_inode = d_inode(h_d);
18287+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18288+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18289+ h_i = au_h_iptr(inode, bindex);
18290+ if (h_i) {
5527c038 18291+ if (h_i == h_inode)
4a4d8108
AM
18292+ continue;
18293+ err = -EIO;
18294+ break;
18295+ }
18296+ }
5afbbe0d
AM
18297+ if (bindex < iinfo->ii_btop)
18298+ iinfo->ii_btop = bindex;
18299+ if (iinfo->ii_bbot < bindex)
18300+ iinfo->ii_bbot = bindex;
5527c038 18301+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18302+ update = 1;
1308ab2a 18303+ }
4a4d8108
AM
18304+ au_update_ibrange(inode, /*do_put_zero*/0);
18305+ e = au_dy_irefresh(inode);
18306+ if (unlikely(e && !err))
18307+ err = e;
027c5e7a
AM
18308+ if (!err)
18309+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18310+
4f0767ce 18311+out:
4a4d8108 18312+ AuTraceErr(err);
1308ab2a 18313+ return err;
dece6358
AM
18314+}
18315+
4a4d8108 18316+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18317+{
4a4d8108
AM
18318+ int err;
18319+ unsigned int flags;
18320+ umode_t mode;
5afbbe0d 18321+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18322+ unsigned char isdir;
18323+ struct dentry *h_dentry;
18324+ struct inode *h_inode;
18325+ struct au_iinfo *iinfo;
b95c5147 18326+ struct inode_operations *iop;
dece6358 18327+
4a4d8108 18328+ IiMustWriteLock(inode);
dece6358 18329+
4a4d8108
AM
18330+ err = 0;
18331+ isdir = 0;
b95c5147 18332+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18333+ btop = au_dbtop(dentry);
18334+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18335+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18336+ mode = h_inode->i_mode;
18337+ switch (mode & S_IFMT) {
18338+ case S_IFREG:
18339+ btail = au_dbtail(dentry);
b95c5147 18340+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18341+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18342+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18343+ if (unlikely(err))
18344+ goto out;
18345+ break;
18346+ case S_IFDIR:
18347+ isdir = 1;
18348+ btail = au_dbtaildir(dentry);
b95c5147 18349+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18350+ inode->i_fop = &aufs_dir_fop;
18351+ break;
18352+ case S_IFLNK:
18353+ btail = au_dbtail(dentry);
b95c5147 18354+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18355+ break;
18356+ case S_IFBLK:
18357+ case S_IFCHR:
18358+ case S_IFIFO:
18359+ case S_IFSOCK:
18360+ btail = au_dbtail(dentry);
b95c5147 18361+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18362+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18363+ break;
18364+ default:
18365+ AuIOErr("Unknown file type 0%o\n", mode);
18366+ err = -EIO;
1308ab2a 18367+ goto out;
4a4d8108 18368+ }
dece6358 18369+
4a4d8108
AM
18370+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18371+ flags = au_hi_flags(inode, isdir);
18372+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18373+ && au_ftest_hi(flags, HNOTIFY)
18374+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18375+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18376+ au_fclr_hi(flags, HNOTIFY);
18377+ iinfo = au_ii(inode);
5afbbe0d
AM
18378+ iinfo->ii_btop = btop;
18379+ iinfo->ii_bbot = btail;
18380+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18381+ h_dentry = au_h_dptr(dentry, bindex);
18382+ if (h_dentry)
18383+ au_set_h_iptr(inode, bindex,
5527c038 18384+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18385+ }
18386+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18387+ /*
18388+ * to force calling aufs_get_acl() every time,
18389+ * do not call cache_no_acl() for aufs inode.
18390+ */
dece6358 18391+
4f0767ce 18392+out:
4a4d8108
AM
18393+ return err;
18394+}
dece6358 18395+
027c5e7a
AM
18396+/*
18397+ * successful returns with iinfo write_locked
18398+ * minus: errno
18399+ * zero: success, matched
18400+ * plus: no error, but unmatched
18401+ */
18402+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18403+{
18404+ int err;
cfc41e69 18405+ unsigned int gen, igflags;
5afbbe0d 18406+ aufs_bindex_t bindex, bbot;
4a4d8108 18407+ struct inode *h_inode, *h_dinode;
5527c038 18408+ struct dentry *h_dentry;
dece6358 18409+
4a4d8108
AM
18410+ /*
18411+ * before this function, if aufs got any iinfo lock, it must be only
18412+ * one, the parent dir.
18413+ * it can happen by UDBA and the obsoleted inode number.
18414+ */
18415+ err = -EIO;
18416+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18417+ goto out;
18418+
027c5e7a 18419+ err = 1;
4a4d8108 18420+ ii_write_lock_new_child(inode);
5afbbe0d 18421+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18422+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18423+ bbot = au_ibbot(inode);
18424+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18425+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18426+ if (!h_inode || h_inode != h_dinode)
18427+ continue;
18428+
18429+ err = 0;
cfc41e69 18430+ gen = au_iigen(inode, &igflags);
537831f9 18431+ if (gen == au_digen(dentry)
cfc41e69 18432+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18433+ break;
537831f9
AM
18434+
18435+ /* fully refresh inode using dentry */
18436+ err = au_refresh_hinode(inode, dentry);
18437+ if (!err)
18438+ au_update_iigen(inode, /*half*/0);
18439+ break;
1facf9fc 18440+ }
dece6358 18441+
4a4d8108
AM
18442+ if (unlikely(err))
18443+ ii_write_unlock(inode);
4f0767ce 18444+out:
1facf9fc 18445+ return err;
18446+}
1facf9fc 18447+
4a4d8108
AM
18448+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18449+ unsigned int d_type, ino_t *ino)
1facf9fc 18450+{
521ced18
JR
18451+ int err, idx;
18452+ const int isnondir = d_type != DT_DIR;
1facf9fc 18453+
b752ccd1 18454+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18455+ if (isnondir) {
18456+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18457+ if (unlikely(err))
18458+ goto out;
4a4d8108 18459+ }
521ced18 18460+
4a4d8108
AM
18461+ err = au_xino_read(sb, bindex, h_ino, ino);
18462+ if (unlikely(err))
521ced18 18463+ goto out_xinondir;
1308ab2a 18464+
4a4d8108
AM
18465+ if (!*ino) {
18466+ err = -EIO;
18467+ *ino = au_xino_new_ino(sb);
18468+ if (unlikely(!*ino))
521ced18 18469+ goto out_xinondir;
4a4d8108
AM
18470+ err = au_xino_write(sb, bindex, h_ino, *ino);
18471+ if (unlikely(err))
521ced18 18472+ goto out_xinondir;
1308ab2a 18473+ }
1facf9fc 18474+
521ced18
JR
18475+out_xinondir:
18476+ if (isnondir && idx >= 0)
18477+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18478+out:
1facf9fc 18479+ return err;
18480+}
18481+
4a4d8108
AM
18482+/* successful returns with iinfo write_locked */
18483+/* todo: return with unlocked? */
18484+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18485+{
5527c038 18486+ struct inode *inode, *h_inode;
4a4d8108
AM
18487+ struct dentry *h_dentry;
18488+ struct super_block *sb;
18489+ ino_t h_ino, ino;
521ced18 18490+ int err, idx, hlinked;
5afbbe0d 18491+ aufs_bindex_t btop;
1facf9fc 18492+
4a4d8108 18493+ sb = dentry->d_sb;
5afbbe0d
AM
18494+ btop = au_dbtop(dentry);
18495+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18496+ h_inode = d_inode(h_dentry);
18497+ h_ino = h_inode->i_ino;
521ced18 18498+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18499+
521ced18 18500+new_ino:
b752ccd1
AM
18501+ /*
18502+ * stop 'race'-ing between hardlinks under different
18503+ * parents.
18504+ */
521ced18
JR
18505+ if (hlinked) {
18506+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18507+ inode = ERR_PTR(err);
18508+ if (unlikely(err))
18509+ goto out;
18510+ }
b752ccd1 18511+
5afbbe0d 18512+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18513+ inode = ERR_PTR(err);
18514+ if (unlikely(err))
521ced18 18515+ goto out_xinondir;
b752ccd1 18516+
4a4d8108
AM
18517+ if (!ino) {
18518+ ino = au_xino_new_ino(sb);
18519+ if (unlikely(!ino)) {
18520+ inode = ERR_PTR(-EIO);
521ced18 18521+ goto out_xinondir;
dece6358
AM
18522+ }
18523+ }
1facf9fc 18524+
4a4d8108
AM
18525+ AuDbg("i%lu\n", (unsigned long)ino);
18526+ inode = au_iget_locked(sb, ino);
18527+ err = PTR_ERR(inode);
18528+ if (IS_ERR(inode))
521ced18 18529+ goto out_xinondir;
1facf9fc 18530+
4a4d8108
AM
18531+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18532+ if (inode->i_state & I_NEW) {
18533+ ii_write_lock_new_child(inode);
18534+ err = set_inode(inode, dentry);
18535+ if (!err) {
18536+ unlock_new_inode(inode);
521ced18 18537+ goto out_xinondir; /* success */
4a4d8108 18538+ }
1308ab2a 18539+
027c5e7a
AM
18540+ /*
18541+ * iget_failed() calls iput(), but we need to call
18542+ * ii_write_unlock() after iget_failed(). so dirty hack for
18543+ * i_count.
18544+ */
18545+ atomic_inc(&inode->i_count);
4a4d8108 18546+ iget_failed(inode);
027c5e7a 18547+ ii_write_unlock(inode);
5afbbe0d 18548+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18549+ /* ignore this error */
18550+ goto out_iput;
18551+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18552+ /*
18553+ * horrible race condition between lookup, readdir and copyup
18554+ * (or something).
18555+ */
521ced18
JR
18556+ if (hlinked && idx >= 0)
18557+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18558+ err = reval_inode(inode, dentry);
18559+ if (unlikely(err < 0)) {
521ced18 18560+ hlinked = 0;
027c5e7a
AM
18561+ goto out_iput;
18562+ }
521ced18 18563+ if (!err)
4a4d8108 18564+ goto out; /* success */
521ced18
JR
18565+ else if (hlinked && idx >= 0) {
18566+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18567+ if (unlikely(err)) {
18568+ iput(inode);
18569+ inode = ERR_PTR(err);
18570+ goto out;
18571+ }
18572+ }
4a4d8108
AM
18573+ }
18574+
5527c038 18575+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18576+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18577+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18578+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18579+ (unsigned long)h_ino, (unsigned long)ino);
18580+ ino = 0;
5afbbe0d 18581+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18582+ if (!err) {
18583+ iput(inode);
521ced18
JR
18584+ if (hlinked && idx >= 0)
18585+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18586+ goto new_ino;
18587+ }
1308ab2a 18588+
4f0767ce 18589+out_iput:
4a4d8108 18590+ iput(inode);
4a4d8108 18591+ inode = ERR_PTR(err);
521ced18
JR
18592+out_xinondir:
18593+ if (hlinked && idx >= 0)
18594+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18595+out:
4a4d8108 18596+ return inode;
1facf9fc 18597+}
18598+
4a4d8108 18599+/* ---------------------------------------------------------------------- */
1facf9fc 18600+
4a4d8108
AM
18601+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18602+ struct inode *inode)
18603+{
18604+ int err;
076b876e 18605+ struct inode *hi;
1facf9fc 18606+
4a4d8108 18607+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18608+
4a4d8108
AM
18609+ /* pseudo-link after flushed may happen out of bounds */
18610+ if (!err
18611+ && inode
5afbbe0d
AM
18612+ && au_ibtop(inode) <= bindex
18613+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18614+ /*
18615+ * permission check is unnecessary since vfsub routine
18616+ * will be called later
18617+ */
076b876e 18618+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18619+ if (hi)
18620+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18621+ }
18622+
4a4d8108
AM
18623+ return err;
18624+}
dece6358 18625+
4a4d8108
AM
18626+int au_test_h_perm(struct inode *h_inode, int mask)
18627+{
2dfbb274 18628+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
18629+ return 0;
18630+ return inode_permission(h_inode, mask);
18631+}
1facf9fc 18632+
4a4d8108
AM
18633+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18634+{
18635+ if (au_test_nfs(h_inode->i_sb)
18636+ && (mask & MAY_WRITE)
18637+ && S_ISDIR(h_inode->i_mode))
18638+ mask |= MAY_READ; /* force permission check */
18639+ return au_test_h_perm(h_inode, mask);
1facf9fc 18640+}
7f207e10
AM
18641diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18642--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 18643+++ linux/fs/aufs/inode.h 2018-06-04 09:08:09.188079511 +0200
8b6a4947 18644@@ -0,0 +1,695 @@
4a4d8108 18645+/*
b00004a5 18646+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
18647+ *
18648+ * This program, aufs is free software; you can redistribute it and/or modify
18649+ * it under the terms of the GNU General Public License as published by
18650+ * the Free Software Foundation; either version 2 of the License, or
18651+ * (at your option) any later version.
18652+ *
18653+ * This program is distributed in the hope that it will be useful,
18654+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18655+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18656+ * GNU General Public License for more details.
18657+ *
18658+ * You should have received a copy of the GNU General Public License
523b37e3 18659+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18660+ */
1facf9fc 18661+
1308ab2a 18662+/*
4a4d8108 18663+ * inode operations
1308ab2a 18664+ */
dece6358 18665+
4a4d8108
AM
18666+#ifndef __AUFS_INODE_H__
18667+#define __AUFS_INODE_H__
dece6358 18668+
4a4d8108 18669+#ifdef __KERNEL__
1308ab2a 18670+
4a4d8108 18671+#include <linux/fsnotify.h>
4a4d8108 18672+#include "rwsem.h"
1308ab2a 18673+
4a4d8108 18674+struct vfsmount;
1facf9fc 18675+
4a4d8108
AM
18676+struct au_hnotify {
18677+#ifdef CONFIG_AUFS_HNOTIFY
18678+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18679+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18680+ struct fsnotify_mark hn_mark;
4a4d8108 18681+#endif
1c60b727 18682+ struct inode *hn_aufs_inode; /* no get/put */
4a4d8108
AM
18683+#endif
18684+} ____cacheline_aligned_in_smp;
1facf9fc 18685+
4a4d8108
AM
18686+struct au_hinode {
18687+ struct inode *hi_inode;
18688+ aufs_bindex_t hi_id;
18689+#ifdef CONFIG_AUFS_HNOTIFY
18690+ struct au_hnotify *hi_notify;
18691+#endif
dece6358 18692+
4a4d8108
AM
18693+ /* reference to the copied-up whiteout with get/put */
18694+ struct dentry *hi_whdentry;
18695+};
dece6358 18696+
537831f9
AM
18697+/* ig_flags */
18698+#define AuIG_HALF_REFRESHED 1
18699+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18700+#define au_ig_fset(flags, name) \
18701+ do { (flags) |= AuIG_##name; } while (0)
18702+#define au_ig_fclr(flags, name) \
18703+ do { (flags) &= ~AuIG_##name; } while (0)
18704+
18705+struct au_iigen {
be52b249 18706+ spinlock_t ig_spin;
537831f9
AM
18707+ __u32 ig_generation, ig_flags;
18708+};
18709+
4a4d8108
AM
18710+struct au_vdir;
18711+struct au_iinfo {
7a9e40b8 18712+ struct au_iigen ii_generation;
4a4d8108 18713+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18714+
4a4d8108 18715+ struct au_rwsem ii_rwsem;
5afbbe0d 18716+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18717+ __u32 ii_higen;
18718+ struct au_hinode *ii_hinode;
18719+ struct au_vdir *ii_vdir;
18720+};
1facf9fc 18721+
4a4d8108
AM
18722+struct au_icntnr {
18723+ struct au_iinfo iinfo;
18724+ struct inode vfs_inode;
8b6a4947 18725+ struct hlist_bl_node plink;
4a4d8108 18726+} ____cacheline_aligned_in_smp;
1308ab2a 18727+
4a4d8108
AM
18728+/* au_pin flags */
18729+#define AuPin_DI_LOCKED 1
18730+#define AuPin_MNT_WRITE (1 << 1)
18731+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18732+#define au_fset_pin(flags, name) \
18733+ do { (flags) |= AuPin_##name; } while (0)
18734+#define au_fclr_pin(flags, name) \
18735+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18736+
18737+struct au_pin {
18738+ /* input */
18739+ struct dentry *dentry;
18740+ unsigned int udba;
18741+ unsigned char lsc_di, lsc_hi, flags;
18742+ aufs_bindex_t bindex;
18743+
18744+ /* output */
18745+ struct dentry *parent;
18746+ struct au_hinode *hdir;
18747+ struct vfsmount *h_mnt;
86dc4139
AM
18748+
18749+ /* temporary unlock/relock for copyup */
18750+ struct dentry *h_dentry, *h_parent;
18751+ struct au_branch *br;
18752+ struct task_struct *task;
4a4d8108 18753+};
1facf9fc 18754+
86dc4139 18755+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18756+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18757+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18758+void au_pin_hdir_acquire_nest(struct au_pin *p);
18759+void au_pin_hdir_release(struct au_pin *p);
18760+
1308ab2a 18761+/* ---------------------------------------------------------------------- */
18762+
4a4d8108 18763+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18764+{
5afbbe0d
AM
18765+ BUG_ON(is_bad_inode(inode));
18766+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18767+}
1facf9fc 18768+
4a4d8108 18769+/* ---------------------------------------------------------------------- */
1facf9fc 18770+
4a4d8108
AM
18771+/* inode.c */
18772+struct inode *au_igrab(struct inode *inode);
b95c5147 18773+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18774+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18775+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18776+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18777+ unsigned int d_type, ino_t *ino);
18778+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18779+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18780+ struct inode *inode);
18781+int au_test_h_perm(struct inode *h_inode, int mask);
18782+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 18783+
4a4d8108
AM
18784+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18785+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18786+{
18787+#ifdef CONFIG_AUFS_SHWH
18788+ return au_ino(sb, bindex, h_ino, d_type, ino);
18789+#else
18790+ return 0;
18791+#endif
18792+}
1facf9fc 18793+
4a4d8108 18794+/* i_op.c */
b95c5147
AM
18795+enum {
18796+ AuIop_SYMLINK,
18797+ AuIop_DIR,
18798+ AuIop_OTHER,
18799+ AuIop_Last
18800+};
18801+extern struct inode_operations aufs_iop[AuIop_Last],
18802+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18803+
4a4d8108
AM
18804+/* au_wr_dir flags */
18805+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18806+#define AuWrDir_ISDIR (1 << 1)
18807+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18808+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18809+#define au_fset_wrdir(flags, name) \
18810+ do { (flags) |= AuWrDir_##name; } while (0)
18811+#define au_fclr_wrdir(flags, name) \
18812+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18813+
4a4d8108
AM
18814+struct au_wr_dir_args {
18815+ aufs_bindex_t force_btgt;
18816+ unsigned char flags;
18817+};
18818+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18819+ struct au_wr_dir_args *args);
dece6358 18820+
4a4d8108
AM
18821+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18822+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18823+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18824+ unsigned int udba, unsigned char flags);
18825+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18826+ unsigned int udba, unsigned char flags) __must_check;
18827+int au_do_pin(struct au_pin *pin) __must_check;
18828+void au_unpin(struct au_pin *pin);
c1595e42
JR
18829+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18830+
18831+#define AuIcpup_DID_CPUP 1
18832+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18833+#define au_fset_icpup(flags, name) \
18834+ do { (flags) |= AuIcpup_##name; } while (0)
18835+#define au_fclr_icpup(flags, name) \
18836+ do { (flags) &= ~AuIcpup_##name; } while (0)
18837+
18838+struct au_icpup_args {
18839+ unsigned char flags;
18840+ unsigned char pin_flags;
18841+ aufs_bindex_t btgt;
18842+ unsigned int udba;
18843+ struct au_pin pin;
18844+ struct path h_path;
18845+ struct inode *h_inode;
18846+};
18847+
18848+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18849+ struct au_icpup_args *a);
18850+
a2654f78
AM
18851+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
18852+ int locked);
1facf9fc 18853+
4a4d8108
AM
18854+/* i_op_add.c */
18855+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18856+ struct dentry *h_parent, int isdir);
7eafdf33
AM
18857+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18858+ dev_t dev);
4a4d8108 18859+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 18860+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 18861+ bool want_excl);
b912730e
AM
18862+struct vfsub_aopen_args;
18863+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18864+ struct vfsub_aopen_args *args);
38d290e6 18865+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
18866+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18867+ struct dentry *dentry);
7eafdf33 18868+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 18869+
4a4d8108
AM
18870+/* i_op_del.c */
18871+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18872+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18873+ struct dentry *h_parent, int isdir);
18874+int aufs_unlink(struct inode *dir, struct dentry *dentry);
18875+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 18876+
4a4d8108
AM
18877+/* i_op_ren.c */
18878+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18879+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
f2c43d5f
AM
18880+ struct inode *dir, struct dentry *dentry,
18881+ unsigned int flags);
1facf9fc 18882+
4a4d8108
AM
18883+/* iinfo.c */
18884+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18885+void au_hiput(struct au_hinode *hinode);
18886+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18887+ struct dentry *h_wh);
18888+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 18889+
4a4d8108
AM
18890+/* hinode flags */
18891+#define AuHi_XINO 1
18892+#define AuHi_HNOTIFY (1 << 1)
18893+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
18894+#define au_fset_hi(flags, name) \
18895+ do { (flags) |= AuHi_##name; } while (0)
18896+#define au_fclr_hi(flags, name) \
18897+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 18898+
4a4d8108
AM
18899+#ifndef CONFIG_AUFS_HNOTIFY
18900+#undef AuHi_HNOTIFY
18901+#define AuHi_HNOTIFY 0
18902+#endif
1facf9fc 18903+
4a4d8108
AM
18904+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18905+ struct inode *h_inode, unsigned int flags);
1facf9fc 18906+
537831f9 18907+void au_update_iigen(struct inode *inode, int half);
4a4d8108 18908+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 18909+
4a4d8108 18910+void au_icntnr_init_once(void *_c);
5afbbe0d 18911+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
18912+int au_iinfo_init(struct inode *inode);
18913+void au_iinfo_fin(struct inode *inode);
e2f27e51 18914+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 18915+
e49829fe 18916+#ifdef CONFIG_PROC_FS
4a4d8108 18917+/* plink.c */
e49829fe 18918+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 18919+struct au_sbinfo;
e49829fe
JR
18920+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18921+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
18922+#ifdef CONFIG_AUFS_DEBUG
18923+void au_plink_list(struct super_block *sb);
18924+#else
18925+AuStubVoid(au_plink_list, struct super_block *sb)
18926+#endif
18927+int au_plink_test(struct inode *inode);
18928+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18929+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18930+ struct dentry *h_dentry);
e49829fe
JR
18931+void au_plink_put(struct super_block *sb, int verbose);
18932+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 18933+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
18934+#else
18935+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18936+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18937+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18938+AuStubVoid(au_plink_list, struct super_block *sb);
18939+AuStubInt0(au_plink_test, struct inode *inode);
18940+AuStub(struct dentry *, au_plink_lkup, return NULL,
18941+ struct inode *inode, aufs_bindex_t bindex);
18942+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18943+ struct dentry *h_dentry);
18944+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18945+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18946+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18947+#endif /* CONFIG_PROC_FS */
1facf9fc 18948+
c1595e42
JR
18949+#ifdef CONFIG_AUFS_XATTR
18950+/* xattr.c */
7e9cd9fe
AM
18951+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
18952+ unsigned int verbose);
c1595e42 18953+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 18954+void au_xattr_init(struct super_block *sb);
c1595e42
JR
18955+#else
18956+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 18957+ int ignore_flags, unsigned int verbose);
f2c43d5f 18958+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
18959+#endif
18960+
18961+#ifdef CONFIG_FS_POSIX_ACL
18962+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
18963+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
18964+#endif
18965+
18966+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18967+enum {
18968+ AU_XATTR_SET,
c1595e42
JR
18969+ AU_ACL_SET
18970+};
18971+
f2c43d5f 18972+struct au_sxattr {
c1595e42
JR
18973+ int type;
18974+ union {
18975+ struct {
18976+ const char *name;
18977+ const void *value;
18978+ size_t size;
18979+ int flags;
18980+ } set;
18981+ struct {
c1595e42
JR
18982+ struct posix_acl *acl;
18983+ int type;
18984+ } acl_set;
18985+ } u;
18986+};
f2c43d5f
AM
18987+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18988+ struct au_sxattr *arg);
c1595e42
JR
18989+#endif
18990+
4a4d8108 18991+/* ---------------------------------------------------------------------- */
1308ab2a 18992+
4a4d8108
AM
18993+/* lock subclass for iinfo */
18994+enum {
18995+ AuLsc_II_CHILD, /* child first */
18996+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
18997+ AuLsc_II_CHILD3, /* copyup dirs */
18998+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
18999+ AuLsc_II_PARENT2,
19000+ AuLsc_II_PARENT3, /* copyup dirs */
19001+ AuLsc_II_NEW_CHILD
19002+};
1308ab2a 19003+
1facf9fc 19004+/*
4a4d8108
AM
19005+ * ii_read_lock_child, ii_write_lock_child,
19006+ * ii_read_lock_child2, ii_write_lock_child2,
19007+ * ii_read_lock_child3, ii_write_lock_child3,
19008+ * ii_read_lock_parent, ii_write_lock_parent,
19009+ * ii_read_lock_parent2, ii_write_lock_parent2,
19010+ * ii_read_lock_parent3, ii_write_lock_parent3,
19011+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 19012+ */
4a4d8108
AM
19013+#define AuReadLockFunc(name, lsc) \
19014+static inline void ii_read_lock_##name(struct inode *i) \
19015+{ \
19016+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19017+}
19018+
19019+#define AuWriteLockFunc(name, lsc) \
19020+static inline void ii_write_lock_##name(struct inode *i) \
19021+{ \
19022+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19023+}
19024+
19025+#define AuRWLockFuncs(name, lsc) \
19026+ AuReadLockFunc(name, lsc) \
19027+ AuWriteLockFunc(name, lsc)
19028+
19029+AuRWLockFuncs(child, CHILD);
19030+AuRWLockFuncs(child2, CHILD2);
19031+AuRWLockFuncs(child3, CHILD3);
19032+AuRWLockFuncs(parent, PARENT);
19033+AuRWLockFuncs(parent2, PARENT2);
19034+AuRWLockFuncs(parent3, PARENT3);
19035+AuRWLockFuncs(new_child, NEW_CHILD);
19036+
19037+#undef AuReadLockFunc
19038+#undef AuWriteLockFunc
19039+#undef AuRWLockFuncs
1facf9fc 19040+
8b6a4947
AM
19041+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19042+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19043+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19044+
4a4d8108
AM
19045+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19046+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19047+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19048+
4a4d8108 19049+/* ---------------------------------------------------------------------- */
1308ab2a 19050+
027c5e7a
AM
19051+static inline void au_icntnr_init(struct au_icntnr *c)
19052+{
19053+#ifdef CONFIG_AUFS_DEBUG
19054+ c->vfs_inode.i_mode = 0;
19055+#endif
19056+}
19057+
cfc41e69 19058+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19059+{
537831f9
AM
19060+ unsigned int gen;
19061+ struct au_iinfo *iinfo;
be52b249 19062+ struct au_iigen *iigen;
537831f9
AM
19063+
19064+ iinfo = au_ii(inode);
be52b249
AM
19065+ iigen = &iinfo->ii_generation;
19066+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19067+ if (igflags)
19068+ *igflags = iigen->ig_flags;
be52b249
AM
19069+ gen = iigen->ig_generation;
19070+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19071+
19072+ return gen;
4a4d8108 19073+}
1308ab2a 19074+
4a4d8108
AM
19075+/* tiny test for inode number */
19076+/* tmpfs generation is too rough */
19077+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19078+{
19079+ struct au_iinfo *iinfo;
1308ab2a 19080+
4a4d8108
AM
19081+ iinfo = au_ii(inode);
19082+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19083+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19084+ && iinfo->ii_higen == h_inode->i_generation);
19085+}
1308ab2a 19086+
4a4d8108
AM
19087+static inline void au_iigen_dec(struct inode *inode)
19088+{
537831f9 19089+ struct au_iinfo *iinfo;
be52b249 19090+ struct au_iigen *iigen;
537831f9
AM
19091+
19092+ iinfo = au_ii(inode);
be52b249
AM
19093+ iigen = &iinfo->ii_generation;
19094+ spin_lock(&iigen->ig_spin);
19095+ iigen->ig_generation--;
19096+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19097+}
19098+
19099+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19100+{
19101+ int err;
19102+
19103+ err = 0;
537831f9 19104+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19105+ err = -EIO;
19106+
19107+ return err;
4a4d8108 19108+}
1308ab2a 19109+
4a4d8108 19110+/* ---------------------------------------------------------------------- */
1308ab2a 19111+
5afbbe0d
AM
19112+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19113+ aufs_bindex_t bindex)
19114+{
19115+ return iinfo->ii_hinode + bindex;
19116+}
19117+
19118+static inline int au_is_bad_inode(struct inode *inode)
19119+{
19120+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19121+}
19122+
4a4d8108
AM
19123+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19124+ aufs_bindex_t bindex)
19125+{
19126+ IiMustAnyLock(inode);
5afbbe0d 19127+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19128+}
1308ab2a 19129+
5afbbe0d 19130+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19131+{
19132+ IiMustAnyLock(inode);
5afbbe0d 19133+ return au_ii(inode)->ii_btop;
4a4d8108 19134+}
1308ab2a 19135+
5afbbe0d 19136+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19137+{
19138+ IiMustAnyLock(inode);
5afbbe0d 19139+ return au_ii(inode)->ii_bbot;
4a4d8108 19140+}
1308ab2a 19141+
4a4d8108
AM
19142+static inline struct au_vdir *au_ivdir(struct inode *inode)
19143+{
19144+ IiMustAnyLock(inode);
19145+ return au_ii(inode)->ii_vdir;
19146+}
1308ab2a 19147+
4a4d8108
AM
19148+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19149+{
19150+ IiMustAnyLock(inode);
5afbbe0d 19151+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19152+}
1308ab2a 19153+
5afbbe0d 19154+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19155+{
4a4d8108 19156+ IiMustWriteLock(inode);
5afbbe0d 19157+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19158+}
1308ab2a 19159+
5afbbe0d 19160+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19161+{
19162+ IiMustWriteLock(inode);
5afbbe0d 19163+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19164+}
19165+
4a4d8108
AM
19166+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19167+{
19168+ IiMustWriteLock(inode);
19169+ au_ii(inode)->ii_vdir = vdir;
19170+}
1facf9fc 19171+
4a4d8108 19172+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19173+{
4a4d8108 19174+ IiMustAnyLock(inode);
5afbbe0d 19175+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19176+}
dece6358 19177+
4a4d8108 19178+/* ---------------------------------------------------------------------- */
1facf9fc 19179+
4a4d8108
AM
19180+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19181+{
19182+ if (pin)
19183+ return pin->parent;
19184+ return NULL;
1facf9fc 19185+}
19186+
4a4d8108 19187+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19188+{
4a4d8108
AM
19189+ if (pin && pin->hdir)
19190+ return pin->hdir->hi_inode;
19191+ return NULL;
1308ab2a 19192+}
1facf9fc 19193+
4a4d8108
AM
19194+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19195+{
19196+ if (pin)
19197+ return pin->hdir;
19198+ return NULL;
19199+}
1facf9fc 19200+
4a4d8108 19201+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19202+{
4a4d8108
AM
19203+ if (pin)
19204+ pin->dentry = dentry;
19205+}
1308ab2a 19206+
4a4d8108
AM
19207+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19208+ unsigned char lflag)
19209+{
19210+ if (pin) {
7f207e10 19211+ if (lflag)
4a4d8108 19212+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19213+ else
4a4d8108 19214+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19215+ }
4a4d8108
AM
19216+}
19217+
7e9cd9fe 19218+#if 0 /* reserved */
4a4d8108
AM
19219+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19220+{
19221+ if (pin) {
19222+ dput(pin->parent);
19223+ pin->parent = dget(parent);
1facf9fc 19224+ }
4a4d8108 19225+}
7e9cd9fe 19226+#endif
1facf9fc 19227+
4a4d8108
AM
19228+/* ---------------------------------------------------------------------- */
19229+
027c5e7a 19230+struct au_branch;
4a4d8108
AM
19231+#ifdef CONFIG_AUFS_HNOTIFY
19232+struct au_hnotify_op {
19233+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19234+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19235+
19236+ /*
19237+ * if it returns true, the the caller should free hinode->hi_notify,
19238+ * otherwise ->free() frees it.
19239+ */
19240+ int (*free)(struct au_hinode *hinode,
19241+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19242+
19243+ void (*fin)(void);
19244+ int (*init)(void);
027c5e7a
AM
19245+
19246+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19247+ void (*fin_br)(struct au_branch *br);
19248+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19249+};
19250+
19251+/* hnotify.c */
027c5e7a 19252+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19253+void au_hn_free(struct au_hinode *hinode);
19254+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19255+void au_hn_reset(struct inode *inode, unsigned int flags);
19256+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19257+ struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19258+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19259+int au_hnotify_init_br(struct au_branch *br, int perm);
19260+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19261+int __init au_hnotify_init(void);
19262+void au_hnotify_fin(void);
19263+
7f207e10 19264+/* hfsnotify.c */
4a4d8108
AM
19265+extern const struct au_hnotify_op au_hnotify_op;
19266+
19267+static inline
19268+void au_hn_init(struct au_hinode *hinode)
19269+{
19270+ hinode->hi_notify = NULL;
1308ab2a 19271+}
19272+
53392da6
AM
19273+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19274+{
19275+ return hinode->hi_notify;
19276+}
19277+
4a4d8108 19278+#else
c1595e42
JR
19279+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19280+ struct au_hinode *hinode __maybe_unused,
19281+ struct inode *inode __maybe_unused)
19282+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19283+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19284+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19285+ int do_set __maybe_unused)
19286+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19287+ unsigned int flags __maybe_unused)
027c5e7a
AM
19288+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19289+ struct au_branch *br __maybe_unused,
19290+ int perm __maybe_unused)
19291+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19292+ int perm __maybe_unused)
19293+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19294+AuStubInt0(__init au_hnotify_init, void)
19295+AuStubVoid(au_hnotify_fin, void)
19296+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19297+#endif /* CONFIG_AUFS_HNOTIFY */
19298+
19299+static inline void au_hn_suspend(struct au_hinode *hdir)
19300+{
19301+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19302+}
19303+
4a4d8108 19304+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19305+{
4a4d8108
AM
19306+ au_hn_ctl(hdir, /*do_set*/1);
19307+}
1308ab2a 19308+
5afbbe0d 19309+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19310+{
febd17d6 19311+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19312+ au_hn_suspend(hdir);
19313+}
dece6358 19314+
5afbbe0d 19315+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19316+ unsigned int sc __maybe_unused)
19317+{
febd17d6 19318+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19319+ au_hn_suspend(hdir);
1facf9fc 19320+}
1facf9fc 19321+
8b6a4947
AM
19322+#if 0 /* unused */
19323+#include "vfsub.h"
3c1bdaff
AM
19324+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19325+ unsigned int sc)
19326+{
be118d29 19327+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
19328+ au_hn_suspend(hdir);
19329+}
8b6a4947 19330+#endif
3c1bdaff 19331+
5afbbe0d 19332+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19333+{
19334+ au_hn_resume(hdir);
febd17d6 19335+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19336+}
19337+
19338+#endif /* __KERNEL__ */
19339+#endif /* __AUFS_INODE_H__ */
7f207e10
AM
19340diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19341--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 19342+++ linux/fs/aufs/ioctl.c 2018-04-15 08:49:13.401150731 +0200
c1595e42 19343@@ -0,0 +1,219 @@
4a4d8108 19344+/*
b00004a5 19345+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
19346+ *
19347+ * This program, aufs is free software; you can redistribute it and/or modify
19348+ * it under the terms of the GNU General Public License as published by
19349+ * the Free Software Foundation; either version 2 of the License, or
19350+ * (at your option) any later version.
19351+ *
19352+ * This program is distributed in the hope that it will be useful,
19353+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19354+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19355+ * GNU General Public License for more details.
19356+ *
19357+ * You should have received a copy of the GNU General Public License
523b37e3 19358+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19359+ */
19360+
19361+/*
19362+ * ioctl
19363+ * plink-management and readdir in userspace.
19364+ * assist the pathconf(3) wrapper library.
c2b27bf2 19365+ * move-down
076b876e 19366+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19367+ */
19368+
c2b27bf2
AM
19369+#include <linux/compat.h>
19370+#include <linux/file.h>
4a4d8108
AM
19371+#include "aufs.h"
19372+
1e00d052 19373+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19374+{
19375+ int err, fd;
5afbbe0d 19376+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19377+ struct file *h_file;
19378+ struct super_block *sb;
19379+ struct dentry *root;
1e00d052
AM
19380+ struct au_branch *br;
19381+ struct aufs_wbr_fd wbrfd = {
19382+ .oflags = au_dir_roflags,
19383+ .brid = -1
19384+ };
19385+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19386+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19387+
1e00d052
AM
19388+ AuDebugOn(wbrfd.oflags & ~valid);
19389+
19390+ if (arg) {
19391+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19392+ if (unlikely(err)) {
19393+ err = -EFAULT;
19394+ goto out;
19395+ }
19396+
19397+ err = -EINVAL;
19398+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19399+ wbrfd.oflags |= au_dir_roflags;
19400+ AuDbg("0%o\n", wbrfd.oflags);
19401+ if (unlikely(wbrfd.oflags & ~valid))
19402+ goto out;
19403+ }
19404+
2000de60 19405+ fd = get_unused_fd_flags(0);
1e00d052
AM
19406+ err = fd;
19407+ if (unlikely(fd < 0))
4a4d8108 19408+ goto out;
4a4d8108 19409+
1e00d052 19410+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19411+ wbi = 0;
1e00d052 19412+ br = NULL;
4a4d8108
AM
19413+ sb = path->dentry->d_sb;
19414+ root = sb->s_root;
19415+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19416+ bbot = au_sbbot(sb);
1e00d052
AM
19417+ if (wbrfd.brid >= 0) {
19418+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19419+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19420+ goto out_unlock;
19421+ }
19422+
19423+ h_file = ERR_PTR(-ENOENT);
19424+ br = au_sbr(sb, wbi);
19425+ if (!au_br_writable(br->br_perm)) {
19426+ if (arg)
19427+ goto out_unlock;
19428+
19429+ bindex = wbi + 1;
19430+ wbi = -1;
5afbbe0d 19431+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19432+ br = au_sbr(sb, bindex);
19433+ if (au_br_writable(br->br_perm)) {
4a4d8108 19434+ wbi = bindex;
1e00d052 19435+ br = au_sbr(sb, wbi);
4a4d8108
AM
19436+ break;
19437+ }
19438+ }
4a4d8108
AM
19439+ }
19440+ AuDbg("wbi %d\n", wbi);
1e00d052 19441+ if (wbi >= 0)
392086de
AM
19442+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19443+ /*force_wr*/0);
1e00d052
AM
19444+
19445+out_unlock:
4a4d8108
AM
19446+ aufs_read_unlock(root, AuLock_IR);
19447+ err = PTR_ERR(h_file);
19448+ if (IS_ERR(h_file))
19449+ goto out_fd;
19450+
5afbbe0d 19451+ au_br_put(br); /* cf. au_h_open() */
4a4d8108
AM
19452+ fd_install(fd, h_file);
19453+ err = fd;
19454+ goto out; /* success */
19455+
4f0767ce 19456+out_fd:
4a4d8108 19457+ put_unused_fd(fd);
4f0767ce 19458+out:
1e00d052 19459+ AuTraceErr(err);
4a4d8108
AM
19460+ return err;
19461+}
19462+
19463+/* ---------------------------------------------------------------------- */
19464+
19465+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19466+{
19467+ long err;
c1595e42 19468+ struct dentry *dentry;
4a4d8108
AM
19469+
19470+ switch (cmd) {
4a4d8108
AM
19471+ case AUFS_CTL_RDU:
19472+ case AUFS_CTL_RDU_INO:
19473+ err = au_rdu_ioctl(file, cmd, arg);
19474+ break;
19475+
19476+ case AUFS_CTL_WBR_FD:
1e00d052 19477+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19478+ break;
19479+
027c5e7a
AM
19480+ case AUFS_CTL_IBUSY:
19481+ err = au_ibusy_ioctl(file, arg);
19482+ break;
19483+
076b876e
AM
19484+ case AUFS_CTL_BRINFO:
19485+ err = au_brinfo_ioctl(file, arg);
19486+ break;
19487+
19488+ case AUFS_CTL_FHSM_FD:
2000de60 19489+ dentry = file->f_path.dentry;
c1595e42
JR
19490+ if (IS_ROOT(dentry))
19491+ err = au_fhsm_fd(dentry->d_sb, arg);
19492+ else
19493+ err = -ENOTTY;
076b876e
AM
19494+ break;
19495+
4a4d8108
AM
19496+ default:
19497+ /* do not call the lower */
19498+ AuDbg("0x%x\n", cmd);
19499+ err = -ENOTTY;
19500+ }
19501+
19502+ AuTraceErr(err);
19503+ return err;
19504+}
19505+
19506+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19507+{
19508+ long err;
19509+
19510+ switch (cmd) {
c2b27bf2 19511+ case AUFS_CTL_MVDOWN:
2000de60 19512+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19513+ break;
19514+
4a4d8108 19515+ case AUFS_CTL_WBR_FD:
1e00d052 19516+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19517+ break;
19518+
19519+ default:
19520+ /* do not call the lower */
19521+ AuDbg("0x%x\n", cmd);
19522+ err = -ENOTTY;
19523+ }
19524+
19525+ AuTraceErr(err);
19526+ return err;
19527+}
b752ccd1
AM
19528+
19529+#ifdef CONFIG_COMPAT
19530+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19531+ unsigned long arg)
19532+{
19533+ long err;
19534+
19535+ switch (cmd) {
19536+ case AUFS_CTL_RDU:
19537+ case AUFS_CTL_RDU_INO:
19538+ err = au_rdu_compat_ioctl(file, cmd, arg);
19539+ break;
19540+
027c5e7a
AM
19541+ case AUFS_CTL_IBUSY:
19542+ err = au_ibusy_compat_ioctl(file, arg);
19543+ break;
19544+
076b876e
AM
19545+ case AUFS_CTL_BRINFO:
19546+ err = au_brinfo_compat_ioctl(file, arg);
19547+ break;
19548+
b752ccd1
AM
19549+ default:
19550+ err = aufs_ioctl_dir(file, cmd, arg);
19551+ }
19552+
19553+ AuTraceErr(err);
19554+ return err;
19555+}
19556+
b752ccd1
AM
19557+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19558+ unsigned long arg)
19559+{
19560+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19561+}
19562+#endif
7f207e10
AM
19563diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19564--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 19565+++ linux/fs/aufs/i_op_add.c 2018-06-04 09:08:09.184746078 +0200
521ced18 19566@@ -0,0 +1,920 @@
4a4d8108 19567+/*
b00004a5 19568+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
19569+ *
19570+ * This program, aufs is free software; you can redistribute it and/or modify
19571+ * it under the terms of the GNU General Public License as published by
19572+ * the Free Software Foundation; either version 2 of the License, or
19573+ * (at your option) any later version.
19574+ *
19575+ * This program is distributed in the hope that it will be useful,
19576+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19577+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19578+ * GNU General Public License for more details.
19579+ *
19580+ * You should have received a copy of the GNU General Public License
523b37e3 19581+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19582+ */
19583+
19584+/*
19585+ * inode operations (add entry)
19586+ */
19587+
19588+#include "aufs.h"
19589+
19590+/*
19591+ * final procedure of adding a new entry, except link(2).
19592+ * remove whiteout, instantiate, copyup the parent dir's times and size
19593+ * and update version.
19594+ * if it failed, re-create the removed whiteout.
19595+ */
19596+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19597+ struct dentry *wh_dentry, struct dentry *dentry)
19598+{
19599+ int err, rerr;
19600+ aufs_bindex_t bwh;
19601+ struct path h_path;
076b876e 19602+ struct super_block *sb;
4a4d8108
AM
19603+ struct inode *inode, *h_dir;
19604+ struct dentry *wh;
19605+
19606+ bwh = -1;
076b876e 19607+ sb = dir->i_sb;
4a4d8108 19608+ if (wh_dentry) {
5527c038 19609+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19610+ IMustLock(h_dir);
19611+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19612+ bwh = au_dbwh(dentry);
19613+ h_path.dentry = wh_dentry;
076b876e 19614+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19615+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19616+ dentry);
19617+ if (unlikely(err))
19618+ goto out;
19619+ }
19620+
19621+ inode = au_new_inode(dentry, /*must_new*/1);
19622+ if (!IS_ERR(inode)) {
19623+ d_instantiate(dentry, inode);
5527c038 19624+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19625+ IMustLock(dir);
b912730e 19626+ au_dir_ts(dir, bindex);
be118d29 19627+ inode_inc_iversion(dir);
076b876e 19628+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19629+ return 0; /* success */
19630+ }
19631+
19632+ err = PTR_ERR(inode);
19633+ if (!wh_dentry)
19634+ goto out;
19635+
19636+ /* revert */
19637+ /* dir inode is locked */
19638+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19639+ rerr = PTR_ERR(wh);
19640+ if (IS_ERR(wh)) {
523b37e3
AM
19641+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19642+ dentry, err, rerr);
4a4d8108
AM
19643+ err = -EIO;
19644+ } else
19645+ dput(wh);
19646+
4f0767ce 19647+out:
4a4d8108
AM
19648+ return err;
19649+}
19650+
027c5e7a
AM
19651+static int au_d_may_add(struct dentry *dentry)
19652+{
19653+ int err;
19654+
19655+ err = 0;
19656+ if (unlikely(d_unhashed(dentry)))
19657+ err = -ENOENT;
5527c038 19658+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19659+ err = -EEXIST;
19660+ return err;
19661+}
19662+
4a4d8108
AM
19663+/*
19664+ * simple tests for the adding inode operations.
19665+ * following the checks in vfs, plus the parent-child relationship.
19666+ */
19667+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19668+ struct dentry *h_parent, int isdir)
19669+{
19670+ int err;
19671+ umode_t h_mode;
19672+ struct dentry *h_dentry;
19673+ struct inode *h_inode;
19674+
19675+ err = -ENAMETOOLONG;
19676+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19677+ goto out;
19678+
19679+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19680+ if (d_really_is_negative(dentry)) {
4a4d8108 19681+ err = -EEXIST;
5527c038 19682+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19683+ goto out;
19684+ } else {
19685+ /* rename(2) case */
19686+ err = -EIO;
5527c038
JR
19687+ if (unlikely(d_is_negative(h_dentry)))
19688+ goto out;
19689+ h_inode = d_inode(h_dentry);
19690+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19691+ goto out;
19692+
19693+ h_mode = h_inode->i_mode;
19694+ if (!isdir) {
19695+ err = -EISDIR;
19696+ if (unlikely(S_ISDIR(h_mode)))
19697+ goto out;
19698+ } else if (unlikely(!S_ISDIR(h_mode))) {
19699+ err = -ENOTDIR;
19700+ goto out;
19701+ }
19702+ }
19703+
19704+ err = 0;
19705+ /* expected parent dir is locked */
19706+ if (unlikely(h_parent != h_dentry->d_parent))
19707+ err = -EIO;
19708+
4f0767ce 19709+out:
4a4d8108
AM
19710+ AuTraceErr(err);
19711+ return err;
19712+}
19713+
19714+/*
19715+ * initial procedure of adding a new entry.
19716+ * prepare writable branch and the parent dir, lock it,
19717+ * and lookup whiteout for the new entry.
19718+ */
19719+static struct dentry*
19720+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19721+ struct dentry *src_dentry, struct au_pin *pin,
19722+ struct au_wr_dir_args *wr_dir_args)
19723+{
19724+ struct dentry *wh_dentry, *h_parent;
19725+ struct super_block *sb;
19726+ struct au_branch *br;
19727+ int err;
19728+ unsigned int udba;
19729+ aufs_bindex_t bcpup;
19730+
523b37e3 19731+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19732+
19733+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19734+ bcpup = err;
19735+ wh_dentry = ERR_PTR(err);
19736+ if (unlikely(err < 0))
19737+ goto out;
19738+
19739+ sb = dentry->d_sb;
19740+ udba = au_opt_udba(sb);
19741+ err = au_pin(pin, dentry, bcpup, udba,
19742+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19743+ wh_dentry = ERR_PTR(err);
19744+ if (unlikely(err))
19745+ goto out;
19746+
19747+ h_parent = au_pinned_h_parent(pin);
19748+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19749+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19750+ err = au_may_add(dentry, bcpup, h_parent,
19751+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19752+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19753+ err = -ENAMETOOLONG;
19754+ wh_dentry = ERR_PTR(err);
19755+ if (unlikely(err))
19756+ goto out_unpin;
19757+
19758+ br = au_sbr(sb, bcpup);
19759+ if (dt) {
19760+ struct path tmp = {
19761+ .dentry = h_parent,
86dc4139 19762+ .mnt = au_br_mnt(br)
4a4d8108
AM
19763+ };
19764+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19765+ }
19766+
19767+ wh_dentry = NULL;
19768+ if (bcpup != au_dbwh(dentry))
19769+ goto out; /* success */
19770+
2000de60
JR
19771+ /*
19772+ * ENAMETOOLONG here means that if we allowed create such name, then it
19773+ * would not be able to removed in the future. So we don't allow such
19774+ * name here and we don't handle ENAMETOOLONG differently here.
19775+ */
4a4d8108
AM
19776+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19777+
4f0767ce 19778+out_unpin:
4a4d8108
AM
19779+ if (IS_ERR(wh_dentry))
19780+ au_unpin(pin);
4f0767ce 19781+out:
4a4d8108
AM
19782+ return wh_dentry;
19783+}
19784+
19785+/* ---------------------------------------------------------------------- */
19786+
19787+enum { Mknod, Symlink, Creat };
19788+struct simple_arg {
19789+ int type;
19790+ union {
19791+ struct {
b912730e
AM
19792+ umode_t mode;
19793+ bool want_excl;
19794+ bool try_aopen;
19795+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19796+ } c;
19797+ struct {
19798+ const char *symname;
19799+ } s;
19800+ struct {
7eafdf33 19801+ umode_t mode;
4a4d8108
AM
19802+ dev_t dev;
19803+ } m;
19804+ } u;
19805+};
19806+
19807+static int add_simple(struct inode *dir, struct dentry *dentry,
19808+ struct simple_arg *arg)
19809+{
076b876e 19810+ int err, rerr;
5afbbe0d 19811+ aufs_bindex_t btop;
4a4d8108 19812+ unsigned char created;
b912730e
AM
19813+ const unsigned char try_aopen
19814+ = (arg->type == Creat && arg->u.c.try_aopen);
4a4d8108
AM
19815+ struct dentry *wh_dentry, *parent;
19816+ struct inode *h_dir;
b912730e
AM
19817+ struct super_block *sb;
19818+ struct au_branch *br;
c2b27bf2
AM
19819+ /* to reuduce stack size */
19820+ struct {
19821+ struct au_dtime dt;
19822+ struct au_pin pin;
19823+ struct path h_path;
19824+ struct au_wr_dir_args wr_dir_args;
19825+ } *a;
4a4d8108 19826+
523b37e3 19827+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19828+ IMustLock(dir);
19829+
c2b27bf2
AM
19830+ err = -ENOMEM;
19831+ a = kmalloc(sizeof(*a), GFP_NOFS);
19832+ if (unlikely(!a))
19833+ goto out;
19834+ a->wr_dir_args.force_btgt = -1;
19835+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19836+
4a4d8108 19837+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
19838+ if (!try_aopen) {
19839+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19840+ if (unlikely(err))
19841+ goto out_free;
19842+ }
027c5e7a
AM
19843+ err = au_d_may_add(dentry);
19844+ if (unlikely(err))
19845+ goto out_unlock;
b912730e
AM
19846+ if (!try_aopen)
19847+ di_write_lock_parent(parent);
c2b27bf2
AM
19848+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19849+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
19850+ err = PTR_ERR(wh_dentry);
19851+ if (IS_ERR(wh_dentry))
027c5e7a 19852+ goto out_parent;
4a4d8108 19853+
5afbbe0d 19854+ btop = au_dbtop(dentry);
b912730e 19855+ sb = dentry->d_sb;
5afbbe0d
AM
19856+ br = au_sbr(sb, btop);
19857+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 19858+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 19859+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
19860+ switch (arg->type) {
19861+ case Creat:
b912730e
AM
19862+ err = 0;
19863+ if (!try_aopen || !h_dir->i_op->atomic_open)
19864+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19865+ arg->u.c.want_excl);
19866+ else
19867+ err = vfsub_atomic_open(h_dir, a->h_path.dentry,
19868+ arg->u.c.aopen, br);
4a4d8108
AM
19869+ break;
19870+ case Symlink:
c2b27bf2 19871+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
4a4d8108
AM
19872+ break;
19873+ case Mknod:
c2b27bf2
AM
19874+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19875+ arg->u.m.dev);
4a4d8108
AM
19876+ break;
19877+ default:
19878+ BUG();
19879+ }
19880+ created = !err;
19881+ if (!err)
5afbbe0d 19882+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108
AM
19883+
19884+ /* revert */
5527c038 19885+ if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
523b37e3
AM
19886+ /* no delegation since it is just created */
19887+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19888+ /*force*/0);
4a4d8108 19889+ if (rerr) {
523b37e3
AM
19890+ AuIOErr("%pd revert failure(%d, %d)\n",
19891+ dentry, err, rerr);
4a4d8108
AM
19892+ err = -EIO;
19893+ }
c2b27bf2 19894+ au_dtime_revert(&a->dt);
4a4d8108
AM
19895+ }
19896+
b912730e
AM
19897+ if (!err && try_aopen && !h_dir->i_op->atomic_open)
19898+ *arg->u.c.aopen->opened |= FILE_CREATED;
19899+
c2b27bf2 19900+ au_unpin(&a->pin);
4a4d8108
AM
19901+ dput(wh_dentry);
19902+
027c5e7a 19903+out_parent:
b912730e
AM
19904+ if (!try_aopen)
19905+ di_write_unlock(parent);
027c5e7a 19906+out_unlock:
4a4d8108 19907+ if (unlikely(err)) {
5afbbe0d 19908+ au_update_dbtop(dentry);
4a4d8108
AM
19909+ d_drop(dentry);
19910+ }
b912730e
AM
19911+ if (!try_aopen)
19912+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 19913+out_free:
1c60b727 19914+ kfree(a);
027c5e7a 19915+out:
4a4d8108
AM
19916+ return err;
19917+}
19918+
7eafdf33
AM
19919+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19920+ dev_t dev)
4a4d8108
AM
19921+{
19922+ struct simple_arg arg = {
19923+ .type = Mknod,
19924+ .u.m = {
19925+ .mode = mode,
19926+ .dev = dev
19927+ }
19928+ };
19929+ return add_simple(dir, dentry, &arg);
19930+}
19931+
19932+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
19933+{
19934+ struct simple_arg arg = {
19935+ .type = Symlink,
19936+ .u.s.symname = symname
19937+ };
19938+ return add_simple(dir, dentry, &arg);
19939+}
19940+
7eafdf33 19941+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 19942+ bool want_excl)
4a4d8108
AM
19943+{
19944+ struct simple_arg arg = {
19945+ .type = Creat,
19946+ .u.c = {
b4510431
AM
19947+ .mode = mode,
19948+ .want_excl = want_excl
4a4d8108
AM
19949+ }
19950+ };
19951+ return add_simple(dir, dentry, &arg);
19952+}
19953+
b912730e
AM
19954+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19955+ struct vfsub_aopen_args *aopen_args)
19956+{
19957+ struct simple_arg arg = {
19958+ .type = Creat,
19959+ .u.c = {
19960+ .mode = aopen_args->create_mode,
19961+ .want_excl = aopen_args->open_flag & O_EXCL,
19962+ .try_aopen = true,
19963+ .aopen = aopen_args
19964+ }
19965+ };
19966+ return add_simple(dir, dentry, &arg);
19967+}
19968+
38d290e6
JR
19969+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
19970+{
19971+ int err;
19972+ aufs_bindex_t bindex;
19973+ struct super_block *sb;
19974+ struct dentry *parent, *h_parent, *h_dentry;
19975+ struct inode *h_dir, *inode;
19976+ struct vfsmount *h_mnt;
19977+ struct au_wr_dir_args wr_dir_args = {
19978+ .force_btgt = -1,
19979+ .flags = AuWrDir_TMPFILE
19980+ };
19981+
19982+ /* copy-up may happen */
febd17d6 19983+ inode_lock(dir);
38d290e6
JR
19984+
19985+ sb = dir->i_sb;
19986+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19987+ if (unlikely(err))
19988+ goto out;
19989+
19990+ err = au_di_init(dentry);
19991+ if (unlikely(err))
19992+ goto out_si;
19993+
19994+ err = -EBUSY;
19995+ parent = d_find_any_alias(dir);
19996+ AuDebugOn(!parent);
19997+ di_write_lock_parent(parent);
5527c038 19998+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
19999+ goto out_parent;
20000+
20001+ err = au_digen_test(parent, au_sigen(sb));
20002+ if (unlikely(err))
20003+ goto out_parent;
20004+
5afbbe0d
AM
20005+ bindex = au_dbtop(parent);
20006+ au_set_dbtop(dentry, bindex);
20007+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20008+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20009+ bindex = err;
20010+ if (unlikely(err < 0))
20011+ goto out_parent;
20012+
20013+ err = -EOPNOTSUPP;
20014+ h_dir = au_h_iptr(dir, bindex);
20015+ if (unlikely(!h_dir->i_op->tmpfile))
20016+ goto out_parent;
20017+
20018+ h_mnt = au_sbr_mnt(sb, bindex);
20019+ err = vfsub_mnt_want_write(h_mnt);
20020+ if (unlikely(err))
20021+ goto out_parent;
20022+
20023+ h_parent = au_h_dptr(parent, bindex);
521ced18
JR
20024+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20025+ if (IS_ERR(h_dentry)) {
20026+ err = PTR_ERR(h_dentry);
38d290e6 20027+ goto out_mnt;
521ced18 20028+ }
38d290e6 20029+
5afbbe0d
AM
20030+ au_set_dbtop(dentry, bindex);
20031+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20032+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20033+ inode = au_new_inode(dentry, /*must_new*/1);
20034+ if (IS_ERR(inode)) {
20035+ err = PTR_ERR(inode);
20036+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20037+ au_set_dbtop(dentry, -1);
20038+ au_set_dbbot(dentry, -1);
38d290e6
JR
20039+ } else {
20040+ if (!inode->i_nlink)
20041+ set_nlink(inode, 1);
20042+ d_tmpfile(dentry, inode);
20043+ au_di(dentry)->di_tmpfile = 1;
20044+
20045+ /* update without i_mutex */
5afbbe0d 20046+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20047+ au_cpup_attr_timesizes(dir);
20048+ }
38d290e6 20049+ dput(h_dentry);
521ced18 20050+
38d290e6
JR
20051+out_mnt:
20052+ vfsub_mnt_drop_write(h_mnt);
20053+out_parent:
20054+ di_write_unlock(parent);
20055+ dput(parent);
20056+ di_write_unlock(dentry);
5afbbe0d 20057+ if (unlikely(err)) {
38d290e6
JR
20058+ au_di_fin(dentry);
20059+ dentry->d_fsdata = NULL;
20060+ }
20061+out_si:
20062+ si_read_unlock(sb);
20063+out:
febd17d6 20064+ inode_unlock(dir);
38d290e6
JR
20065+ return err;
20066+}
20067+
4a4d8108
AM
20068+/* ---------------------------------------------------------------------- */
20069+
20070+struct au_link_args {
20071+ aufs_bindex_t bdst, bsrc;
20072+ struct au_pin pin;
20073+ struct path h_path;
20074+ struct dentry *src_parent, *parent;
20075+};
20076+
20077+static int au_cpup_before_link(struct dentry *src_dentry,
20078+ struct au_link_args *a)
20079+{
20080+ int err;
20081+ struct dentry *h_src_dentry;
c2b27bf2
AM
20082+ struct au_cp_generic cpg = {
20083+ .dentry = src_dentry,
20084+ .bdst = a->bdst,
20085+ .bsrc = a->bsrc,
20086+ .len = -1,
20087+ .pin = &a->pin,
20088+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20089+ };
4a4d8108
AM
20090+
20091+ di_read_lock_parent(a->src_parent, AuLock_IR);
20092+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20093+ if (unlikely(err))
20094+ goto out;
20095+
20096+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20097+ err = au_pin(&a->pin, src_dentry, a->bdst,
20098+ au_opt_udba(src_dentry->d_sb),
20099+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20100+ if (unlikely(err))
20101+ goto out;
367653fa 20102+
c2b27bf2 20103+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20104+ au_unpin(&a->pin);
20105+
4f0767ce 20106+out:
4a4d8108
AM
20107+ di_read_unlock(a->src_parent, AuLock_IR);
20108+ return err;
20109+}
20110+
86dc4139
AM
20111+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20112+ struct au_link_args *a)
4a4d8108
AM
20113+{
20114+ int err;
20115+ unsigned char plink;
5afbbe0d 20116+ aufs_bindex_t bbot;
4a4d8108 20117+ struct dentry *h_src_dentry;
523b37e3 20118+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20119+ struct super_block *sb;
20120+ struct file *h_file;
20121+
20122+ plink = 0;
20123+ h_inode = NULL;
20124+ sb = src_dentry->d_sb;
5527c038 20125+ inode = d_inode(src_dentry);
5afbbe0d 20126+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20127+ h_inode = au_h_iptr(inode, a->bdst);
20128+ if (!h_inode || !h_inode->i_nlink) {
20129+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20130+ bbot = au_dbbot(dentry);
20131+ if (bbot < a->bsrc)
20132+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20133+ au_set_h_dptr(dentry, a->bsrc,
20134+ dget(au_h_dptr(src_dentry, a->bsrc)));
20135+ dget(a->h_path.dentry);
20136+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20137+ AuDbg("temporary d_inode...\n");
20138+ spin_lock(&dentry->d_lock);
5527c038 20139+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20140+ spin_unlock(&dentry->d_lock);
392086de 20141+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20142+ if (IS_ERR(h_file))
4a4d8108 20143+ err = PTR_ERR(h_file);
86dc4139 20144+ else {
c2b27bf2
AM
20145+ struct au_cp_generic cpg = {
20146+ .dentry = dentry,
20147+ .bdst = a->bdst,
20148+ .bsrc = -1,
20149+ .len = -1,
20150+ .pin = &a->pin,
20151+ .flags = AuCpup_KEEPLINO
20152+ };
20153+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20154+ au_h_open_post(dentry, a->bsrc, h_file);
20155+ if (!err) {
20156+ dput(a->h_path.dentry);
20157+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20158+ } else
20159+ au_set_h_dptr(dentry, a->bdst,
20160+ a->h_path.dentry);
20161+ }
c1595e42 20162+ spin_lock(&dentry->d_lock);
86dc4139 20163+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20164+ spin_unlock(&dentry->d_lock);
20165+ AuDbg("temporary d_inode...done\n");
86dc4139 20166+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20167+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20168+ } else {
20169+ /* the inode of src_dentry already exists on a.bdst branch */
20170+ h_src_dentry = d_find_alias(h_inode);
20171+ if (!h_src_dentry && au_plink_test(inode)) {
20172+ plink = 1;
20173+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20174+ err = PTR_ERR(h_src_dentry);
20175+ if (IS_ERR(h_src_dentry))
20176+ goto out;
20177+
5527c038 20178+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20179+ dput(h_src_dentry);
20180+ h_src_dentry = NULL;
20181+ }
20182+
20183+ }
20184+ if (h_src_dentry) {
523b37e3 20185+ delegated = NULL;
4a4d8108 20186+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20187+ &a->h_path, &delegated);
20188+ if (unlikely(err == -EWOULDBLOCK)) {
20189+ pr_warn("cannot retry for NFSv4 delegation"
20190+ " for an internal link\n");
20191+ iput(delegated);
20192+ }
4a4d8108
AM
20193+ dput(h_src_dentry);
20194+ } else {
20195+ AuIOErr("no dentry found for hi%lu on b%d\n",
20196+ h_inode->i_ino, a->bdst);
20197+ err = -EIO;
20198+ }
20199+ }
20200+
20201+ if (!err && !plink)
20202+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20203+
20204+out:
2cbb1c4b 20205+ AuTraceErr(err);
4a4d8108
AM
20206+ return err;
20207+}
20208+
20209+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20210+ struct dentry *dentry)
20211+{
20212+ int err, rerr;
20213+ struct au_dtime dt;
20214+ struct au_link_args *a;
20215+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20216+ struct inode *inode, *delegated;
4a4d8108
AM
20217+ struct super_block *sb;
20218+ struct au_wr_dir_args wr_dir_args = {
20219+ /* .force_btgt = -1, */
20220+ .flags = AuWrDir_ADD_ENTRY
20221+ };
20222+
20223+ IMustLock(dir);
5527c038 20224+ inode = d_inode(src_dentry);
4a4d8108
AM
20225+ IMustLock(inode);
20226+
4a4d8108
AM
20227+ err = -ENOMEM;
20228+ a = kzalloc(sizeof(*a), GFP_NOFS);
20229+ if (unlikely(!a))
20230+ goto out;
20231+
20232+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20233+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20234+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20235+ if (unlikely(err))
20236+ goto out_kfree;
38d290e6 20237+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20238+ if (unlikely(err))
20239+ goto out_unlock;
20240+ err = au_d_may_add(dentry);
20241+ if (unlikely(err))
20242+ goto out_unlock;
e49829fe 20243+
4a4d8108 20244+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20245+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20246+
20247+ di_write_lock_parent(a->parent);
20248+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20249+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20250+ &wr_dir_args);
20251+ err = PTR_ERR(wh_dentry);
20252+ if (IS_ERR(wh_dentry))
027c5e7a 20253+ goto out_parent;
4a4d8108
AM
20254+
20255+ err = 0;
20256+ sb = dentry->d_sb;
5afbbe0d 20257+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20258+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20259+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20260+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20261+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20262+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20263+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20264+ if (!h_src_dentry) {
5afbbe0d 20265+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20266+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20267+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20268+ } else if (IS_ERR(h_src_dentry)) {
20269+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20270+ goto out_parent;
38d290e6 20271+ }
2cbb1c4b 20272+
f2c43d5f
AM
20273+ /*
20274+ * aufs doesn't touch the credential so
20275+ * security_dentry_create_files_as() is unnecrssary.
20276+ */
4a4d8108
AM
20277+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20278+ if (a->bdst < a->bsrc
20279+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20280+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20281+ else {
20282+ delegated = NULL;
4a4d8108 20283+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20284+ &a->h_path, &delegated);
20285+ if (unlikely(err == -EWOULDBLOCK)) {
20286+ pr_warn("cannot retry for NFSv4 delegation"
20287+ " for an internal link\n");
20288+ iput(delegated);
20289+ }
20290+ }
2cbb1c4b 20291+ dput(h_src_dentry);
4a4d8108
AM
20292+ } else {
20293+ /*
20294+ * copyup src_dentry to the branch we process,
20295+ * and then link(2) to it.
20296+ */
2cbb1c4b 20297+ dput(h_src_dentry);
4a4d8108
AM
20298+ if (a->bdst < a->bsrc
20299+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20300+ au_unpin(&a->pin);
20301+ di_write_unlock(a->parent);
20302+ err = au_cpup_before_link(src_dentry, a);
20303+ di_write_lock_parent(a->parent);
20304+ if (!err)
20305+ err = au_pin(&a->pin, dentry, a->bdst,
20306+ au_opt_udba(sb),
20307+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20308+ if (unlikely(err))
20309+ goto out_wh;
20310+ }
20311+ if (!err) {
20312+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20313+ err = -ENOENT;
5527c038 20314+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20315+ delegated = NULL;
4a4d8108
AM
20316+ err = vfsub_link(h_src_dentry,
20317+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20318+ &a->h_path, &delegated);
20319+ if (unlikely(err == -EWOULDBLOCK)) {
20320+ pr_warn("cannot retry"
20321+ " for NFSv4 delegation"
20322+ " for an internal link\n");
20323+ iput(delegated);
20324+ }
20325+ }
4a4d8108
AM
20326+ }
20327+ }
20328+ if (unlikely(err))
20329+ goto out_unpin;
20330+
20331+ if (wh_dentry) {
20332+ a->h_path.dentry = wh_dentry;
20333+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20334+ dentry);
20335+ if (unlikely(err))
20336+ goto out_revert;
20337+ }
20338+
b912730e 20339+ au_dir_ts(dir, a->bdst);
be118d29 20340+ inode_inc_iversion(dir);
4a4d8108
AM
20341+ inc_nlink(inode);
20342+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20343+ d_instantiate(dentry, au_igrab(inode));
20344+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20345+ /* some filesystem calls d_drop() */
20346+ d_drop(dentry);
076b876e
AM
20347+ /* some filesystems consume an inode even hardlink */
20348+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20349+ goto out_unpin; /* success */
20350+
4f0767ce 20351+out_revert:
523b37e3
AM
20352+ /* no delegation since it is just created */
20353+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20354+ /*delegated*/NULL, /*force*/0);
027c5e7a 20355+ if (unlikely(rerr)) {
523b37e3 20356+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20357+ err = -EIO;
20358+ }
4a4d8108 20359+ au_dtime_revert(&dt);
4f0767ce 20360+out_unpin:
4a4d8108 20361+ au_unpin(&a->pin);
4f0767ce 20362+out_wh:
4a4d8108 20363+ dput(wh_dentry);
027c5e7a
AM
20364+out_parent:
20365+ di_write_unlock(a->parent);
20366+ dput(a->src_parent);
4f0767ce 20367+out_unlock:
4a4d8108 20368+ if (unlikely(err)) {
5afbbe0d 20369+ au_update_dbtop(dentry);
4a4d8108
AM
20370+ d_drop(dentry);
20371+ }
4a4d8108 20372+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20373+out_kfree:
1c60b727 20374+ kfree(a);
4f0767ce 20375+out:
86dc4139 20376+ AuTraceErr(err);
4a4d8108
AM
20377+ return err;
20378+}
20379+
7eafdf33 20380+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
20381+{
20382+ int err, rerr;
20383+ aufs_bindex_t bindex;
20384+ unsigned char diropq;
20385+ struct path h_path;
20386+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20387+ struct inode *h_inode;
4a4d8108
AM
20388+ struct super_block *sb;
20389+ struct {
20390+ struct au_pin pin;
20391+ struct au_dtime dt;
20392+ } *a; /* reduce the stack usage */
20393+ struct au_wr_dir_args wr_dir_args = {
20394+ .force_btgt = -1,
20395+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20396+ };
20397+
20398+ IMustLock(dir);
20399+
20400+ err = -ENOMEM;
20401+ a = kmalloc(sizeof(*a), GFP_NOFS);
20402+ if (unlikely(!a))
20403+ goto out;
20404+
027c5e7a
AM
20405+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20406+ if (unlikely(err))
20407+ goto out_free;
20408+ err = au_d_may_add(dentry);
20409+ if (unlikely(err))
20410+ goto out_unlock;
20411+
4a4d8108
AM
20412+ parent = dentry->d_parent; /* dir inode is locked */
20413+ di_write_lock_parent(parent);
20414+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20415+ &a->pin, &wr_dir_args);
20416+ err = PTR_ERR(wh_dentry);
20417+ if (IS_ERR(wh_dentry))
027c5e7a 20418+ goto out_parent;
4a4d8108
AM
20419+
20420+ sb = dentry->d_sb;
5afbbe0d 20421+ bindex = au_dbtop(dentry);
4a4d8108
AM
20422+ h_path.dentry = au_h_dptr(dentry, bindex);
20423+ h_path.mnt = au_sbr_mnt(sb, bindex);
20424+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20425+ if (unlikely(err))
027c5e7a 20426+ goto out_unpin;
4a4d8108
AM
20427+
20428+ /* make the dir opaque */
20429+ diropq = 0;
febd17d6 20430+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20431+ if (wh_dentry
20432+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20433+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20434+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20435+ inode_unlock(h_inode);
4a4d8108
AM
20436+ err = PTR_ERR(opq_dentry);
20437+ if (IS_ERR(opq_dentry))
20438+ goto out_dir;
20439+ dput(opq_dentry);
20440+ diropq = 1;
20441+ }
20442+
20443+ err = epilog(dir, bindex, wh_dentry, dentry);
20444+ if (!err) {
20445+ inc_nlink(dir);
027c5e7a 20446+ goto out_unpin; /* success */
4a4d8108
AM
20447+ }
20448+
20449+ /* revert */
20450+ if (diropq) {
20451+ AuLabel(revert opq);
febd17d6 20452+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20453+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20454+ inode_unlock(h_inode);
4a4d8108 20455+ if (rerr) {
523b37e3
AM
20456+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20457+ dentry, err, rerr);
4a4d8108
AM
20458+ err = -EIO;
20459+ }
20460+ }
20461+
4f0767ce 20462+out_dir:
4a4d8108
AM
20463+ AuLabel(revert dir);
20464+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20465+ if (rerr) {
523b37e3
AM
20466+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20467+ dentry, err, rerr);
4a4d8108
AM
20468+ err = -EIO;
20469+ }
4a4d8108 20470+ au_dtime_revert(&a->dt);
027c5e7a 20471+out_unpin:
4a4d8108
AM
20472+ au_unpin(&a->pin);
20473+ dput(wh_dentry);
027c5e7a
AM
20474+out_parent:
20475+ di_write_unlock(parent);
20476+out_unlock:
4a4d8108 20477+ if (unlikely(err)) {
5afbbe0d 20478+ au_update_dbtop(dentry);
4a4d8108
AM
20479+ d_drop(dentry);
20480+ }
4a4d8108 20481+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20482+out_free:
1c60b727 20483+ kfree(a);
4f0767ce 20484+out:
4a4d8108
AM
20485+ return err;
20486+}
7f207e10
AM
20487diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20488--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 20489+++ linux/fs/aufs/i_op.c 2018-06-04 09:08:09.184746078 +0200
8b6a4947 20490@@ -0,0 +1,1459 @@
4a4d8108 20491+/*
b00004a5 20492+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
20493+ *
20494+ * This program, aufs is free software; you can redistribute it and/or modify
20495+ * it under the terms of the GNU General Public License as published by
20496+ * the Free Software Foundation; either version 2 of the License, or
20497+ * (at your option) any later version.
20498+ *
20499+ * This program is distributed in the hope that it will be useful,
20500+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20501+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20502+ * GNU General Public License for more details.
20503+ *
20504+ * You should have received a copy of the GNU General Public License
523b37e3 20505+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20506+ */
1facf9fc 20507+
1308ab2a 20508+/*
4a4d8108 20509+ * inode operations (except add/del/rename)
1308ab2a 20510+ */
4a4d8108
AM
20511+
20512+#include <linux/device_cgroup.h>
20513+#include <linux/fs_stack.h>
4a4d8108
AM
20514+#include <linux/namei.h>
20515+#include <linux/security.h>
4a4d8108
AM
20516+#include "aufs.h"
20517+
1e00d052 20518+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20519+ struct path *h_path, int brperm)
1facf9fc 20520+{
1308ab2a 20521+ int err;
4a4d8108 20522+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 20523+
e2f27e51
AM
20524+ err = -EPERM;
20525+ if (write_mask && IS_IMMUTABLE(h_inode))
20526+ goto out;
20527+
4a4d8108 20528+ err = -EACCES;
e2f27e51
AM
20529+ if (((mask & MAY_EXEC)
20530+ && S_ISREG(h_inode->i_mode)
20531+ && (path_noexec(h_path)
20532+ || !(h_inode->i_mode & S_IXUGO))))
4a4d8108
AM
20533+ goto out;
20534+
20535+ /*
20536+ * - skip the lower fs test in the case of write to ro branch.
20537+ * - nfs dir permission write check is optimized, but a policy for
20538+ * link/rename requires a real check.
2121bcd9 20539+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 20540+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
20541+ */
20542+ if ((write_mask && !au_br_writable(brperm))
20543+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20544+ && write_mask && !(mask & MAY_READ))
20545+ || !h_inode->i_op->permission) {
20546+ /* AuLabel(generic_permission); */
b912730e 20547+ /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
1e00d052 20548+ err = generic_permission(h_inode, mask);
b912730e
AM
20549+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20550+ err = h_inode->i_op->permission(h_inode, mask);
20551+ AuTraceErr(err);
1308ab2a 20552+ } else {
4a4d8108 20553+ /* AuLabel(h_inode->permission); */
1e00d052 20554+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
20555+ AuTraceErr(err);
20556+ }
1facf9fc 20557+
4a4d8108
AM
20558+ if (!err)
20559+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20560+ if (!err)
4a4d8108 20561+ err = security_inode_permission(h_inode, mask);
4a4d8108
AM
20562+
20563+#if 0
20564+ if (!err) {
20565+ /* todo: do we need to call ima_path_check()? */
20566+ struct path h_path = {
20567+ .dentry =
20568+ .mnt = h_mnt
20569+ };
20570+ err = ima_path_check(&h_path,
20571+ mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20572+ IMA_COUNT_LEAVE);
1308ab2a 20573+ }
4a4d8108 20574+#endif
dece6358 20575+
4f0767ce 20576+out:
1308ab2a 20577+ return err;
20578+}
dece6358 20579+
1e00d052 20580+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 20581+{
20582+ int err;
5afbbe0d 20583+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20584+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20585+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20586+ struct inode *h_inode;
20587+ struct super_block *sb;
20588+ struct au_branch *br;
1facf9fc 20589+
027c5e7a 20590+ /* todo: support rcu-walk? */
1e00d052 20591+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20592+ return -ECHILD;
20593+
4a4d8108
AM
20594+ sb = inode->i_sb;
20595+ si_read_lock(sb, AuLock_FLUSH);
20596+ ii_read_lock_child(inode);
027c5e7a
AM
20597+#if 0
20598+ err = au_iigen_test(inode, au_sigen(sb));
20599+ if (unlikely(err))
20600+ goto out;
20601+#endif
dece6358 20602+
076b876e
AM
20603+ if (!isdir
20604+ || write_mask
20605+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20606+ err = au_busy_or_stale();
5afbbe0d 20607+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20608+ if (unlikely(!h_inode
20609+ || (h_inode->i_mode & S_IFMT)
20610+ != (inode->i_mode & S_IFMT)))
20611+ goto out;
1facf9fc 20612+
4a4d8108 20613+ err = 0;
5afbbe0d 20614+ bindex = au_ibtop(inode);
4a4d8108 20615+ br = au_sbr(sb, bindex);
79b8bda9 20616+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20617+ if (write_mask
20618+ && !err
20619+ && !special_file(h_inode->i_mode)) {
20620+ /* test whether the upper writable branch exists */
20621+ err = -EROFS;
20622+ for (; bindex >= 0; bindex--)
20623+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20624+ err = 0;
20625+ break;
20626+ }
20627+ }
20628+ goto out;
20629+ }
dece6358 20630+
4a4d8108 20631+ /* non-write to dir */
1308ab2a 20632+ err = 0;
5afbbe0d
AM
20633+ bbot = au_ibbot(inode);
20634+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20635+ h_inode = au_h_iptr(inode, bindex);
20636+ if (h_inode) {
20637+ err = au_busy_or_stale();
20638+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20639+ break;
20640+
20641+ br = au_sbr(sb, bindex);
79b8bda9 20642+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20643+ br->br_perm);
20644+ }
20645+ }
1308ab2a 20646+
4f0767ce 20647+out:
4a4d8108
AM
20648+ ii_read_unlock(inode);
20649+ si_read_unlock(sb);
1308ab2a 20650+ return err;
20651+}
20652+
4a4d8108 20653+/* ---------------------------------------------------------------------- */
1facf9fc 20654+
4a4d8108 20655+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20656+ unsigned int flags)
4a4d8108
AM
20657+{
20658+ struct dentry *ret, *parent;
b752ccd1 20659+ struct inode *inode;
4a4d8108 20660+ struct super_block *sb;
1716fcea 20661+ int err, npositive;
dece6358 20662+
4a4d8108 20663+ IMustLock(dir);
1308ab2a 20664+
537831f9
AM
20665+ /* todo: support rcu-walk? */
20666+ ret = ERR_PTR(-ECHILD);
20667+ if (flags & LOOKUP_RCU)
20668+ goto out;
20669+
20670+ ret = ERR_PTR(-ENAMETOOLONG);
20671+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20672+ goto out;
20673+
4a4d8108 20674+ sb = dir->i_sb;
7f207e10
AM
20675+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20676+ ret = ERR_PTR(err);
20677+ if (unlikely(err))
20678+ goto out;
20679+
4a4d8108
AM
20680+ err = au_di_init(dentry);
20681+ ret = ERR_PTR(err);
20682+ if (unlikely(err))
7f207e10 20683+ goto out_si;
1308ab2a 20684+
9dbd164d 20685+ inode = NULL;
027c5e7a 20686+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20687+ parent = dentry->d_parent; /* dir inode is locked */
20688+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20689+ err = au_alive_dir(parent);
20690+ if (!err)
20691+ err = au_digen_test(parent, au_sigen(sb));
20692+ if (!err) {
5afbbe0d
AM
20693+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20694+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20695+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20696+ err = npositive;
20697+ }
4a4d8108 20698+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20699+ ret = ERR_PTR(err);
20700+ if (unlikely(err < 0))
20701+ goto out_unlock;
1308ab2a 20702+
4a4d8108 20703+ if (npositive) {
b752ccd1 20704+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20705+ if (IS_ERR(inode)) {
20706+ ret = (void *)inode;
20707+ inode = NULL;
20708+ goto out_unlock;
20709+ }
9dbd164d 20710+ }
4a4d8108 20711+
c1595e42
JR
20712+ if (inode)
20713+ atomic_inc(&inode->i_count);
4a4d8108 20714+ ret = d_splice_alias(inode, dentry);
537831f9
AM
20715+#if 0
20716+ if (unlikely(d_need_lookup(dentry))) {
20717+ spin_lock(&dentry->d_lock);
20718+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20719+ spin_unlock(&dentry->d_lock);
20720+ } else
20721+#endif
c1595e42 20722+ if (inode) {
2000de60 20723+ if (!IS_ERR(ret)) {
c1595e42 20724+ iput(inode);
2000de60
JR
20725+ if (ret && ret != dentry)
20726+ ii_write_unlock(inode);
20727+ } else {
c1595e42
JR
20728+ ii_write_unlock(inode);
20729+ iput(inode);
20730+ inode = NULL;
20731+ }
7f207e10 20732+ }
1facf9fc 20733+
4f0767ce 20734+out_unlock:
4a4d8108 20735+ di_write_unlock(dentry);
7f207e10 20736+out_si:
4a4d8108 20737+ si_read_unlock(sb);
7f207e10 20738+out:
4a4d8108
AM
20739+ return ret;
20740+}
1facf9fc 20741+
4a4d8108 20742+/* ---------------------------------------------------------------------- */
1facf9fc 20743+
b912730e 20744+struct aopen_node {
8b6a4947 20745+ struct hlist_bl_node hblist;
b912730e
AM
20746+ struct file *file, *h_file;
20747+};
20748+
20749+static int au_do_aopen(struct inode *inode, struct file *file)
20750+{
8b6a4947
AM
20751+ struct hlist_bl_head *aopen;
20752+ struct hlist_bl_node *pos;
b912730e
AM
20753+ struct aopen_node *node;
20754+ struct au_do_open_args args = {
8b6a4947
AM
20755+ .aopen = 1,
20756+ .open = au_do_open_nondir
b912730e
AM
20757+ };
20758+
20759+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20760+ hlist_bl_lock(aopen);
20761+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20762+ if (node->file == file) {
20763+ args.h_file = node->h_file;
20764+ break;
20765+ }
8b6a4947 20766+ hlist_bl_unlock(aopen);
b912730e
AM
20767+ /* AuDebugOn(!args.h_file); */
20768+
20769+ return au_do_open(file, &args);
20770+}
20771+
20772+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20773+ struct file *file, unsigned int open_flag,
20774+ umode_t create_mode, int *opened)
20775+{
8b6a4947 20776+ int err, unlocked, h_opened = *opened;
5afbbe0d 20777+ unsigned int lkup_flags;
f0c0a007 20778+ struct dentry *parent, *d;
8b6a4947 20779+ struct hlist_bl_head *aopen;
b912730e
AM
20780+ struct vfsub_aopen_args args = {
20781+ .open_flag = open_flag,
20782+ .create_mode = create_mode,
20783+ .opened = &h_opened
20784+ };
20785+ struct aopen_node aopen_node = {
20786+ .file = file
20787+ };
20788+
20789+ IMustLock(dir);
5afbbe0d 20790+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
20791+ AuDbgDentry(dentry);
20792+
20793+ err = 0;
20794+ if (!au_di(dentry)) {
5afbbe0d
AM
20795+ lkup_flags = LOOKUP_OPEN;
20796+ if (open_flag & O_CREAT)
20797+ lkup_flags |= LOOKUP_CREATE;
20798+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
20799+ if (IS_ERR(d)) {
20800+ err = PTR_ERR(d);
5afbbe0d 20801+ AuTraceErr(err);
b912730e
AM
20802+ goto out;
20803+ } else if (d) {
20804+ /*
20805+ * obsoleted dentry found.
20806+ * another error will be returned later.
20807+ */
20808+ d_drop(d);
b912730e 20809+ AuDbgDentry(d);
5afbbe0d 20810+ dput(d);
b912730e
AM
20811+ }
20812+ AuDbgDentry(dentry);
20813+ }
20814+
20815+ if (d_is_positive(dentry)
20816+ || d_unhashed(dentry)
20817+ || d_unlinked(dentry)
20818+ || !(open_flag & O_CREAT))
20819+ goto out_no_open;
20820+
8b6a4947 20821+ unlocked = 0;
b912730e
AM
20822+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20823+ if (unlikely(err))
20824+ goto out;
20825+
20826+ parent = dentry->d_parent; /* dir is locked */
20827+ di_write_lock_parent(parent);
5afbbe0d 20828+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
b912730e
AM
20829+ if (unlikely(err))
20830+ goto out_unlock;
20831+
20832+ AuDbgDentry(dentry);
20833+ if (d_is_positive(dentry))
20834+ goto out_unlock;
20835+
20836+ args.file = get_empty_filp();
20837+ err = PTR_ERR(args.file);
20838+ if (IS_ERR(args.file))
20839+ goto out_unlock;
20840+
20841+ args.file->f_flags = file->f_flags;
20842+ err = au_aopen_or_create(dir, dentry, &args);
20843+ AuTraceErr(err);
20844+ AuDbgFile(args.file);
20845+ if (unlikely(err < 0)) {
20846+ if (h_opened & FILE_OPENED)
20847+ fput(args.file);
20848+ else
20849+ put_filp(args.file);
20850+ goto out_unlock;
20851+ }
8b6a4947
AM
20852+ di_write_unlock(parent);
20853+ di_write_unlock(dentry);
20854+ unlocked = 1;
b912730e
AM
20855+
20856+ /* some filesystems don't set FILE_CREATED while succeeded? */
20857+ *opened |= FILE_CREATED;
20858+ if (h_opened & FILE_OPENED)
20859+ aopen_node.h_file = args.file;
20860+ else {
20861+ put_filp(args.file);
20862+ args.file = NULL;
20863+ }
20864+ aopen = &au_sbi(dir->i_sb)->si_aopen;
8b6a4947 20865+ au_hbl_add(&aopen_node.hblist, aopen);
b912730e 20866+ err = finish_open(file, dentry, au_do_aopen, opened);
8b6a4947 20867+ au_hbl_del(&aopen_node.hblist, aopen);
b912730e
AM
20868+ AuTraceErr(err);
20869+ AuDbgFile(file);
20870+ if (aopen_node.h_file)
20871+ fput(aopen_node.h_file);
20872+
20873+out_unlock:
8b6a4947
AM
20874+ if (unlocked)
20875+ si_read_unlock(dentry->d_sb);
20876+ else {
20877+ di_write_unlock(parent);
20878+ aufs_read_unlock(dentry, AuLock_DW);
20879+ }
b912730e 20880+ AuDbgDentry(dentry);
f0c0a007 20881+ if (unlikely(err < 0))
b912730e
AM
20882+ goto out;
20883+out_no_open:
f0c0a007 20884+ if (err >= 0 && !(*opened & FILE_CREATED)) {
b912730e
AM
20885+ AuLabel(out_no_open);
20886+ dget(dentry);
20887+ err = finish_no_open(file, dentry);
20888+ }
20889+out:
20890+ AuDbg("%pd%s%s\n", dentry,
20891+ (*opened & FILE_CREATED) ? " created" : "",
20892+ (*opened & FILE_OPENED) ? " opened" : "");
20893+ AuTraceErr(err);
20894+ return err;
20895+}
20896+
20897+
20898+/* ---------------------------------------------------------------------- */
20899+
4a4d8108
AM
20900+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20901+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 20902+ aufs_bindex_t btop)
4a4d8108
AM
20903+{
20904+ int err;
20905+ struct dentry *h_parent;
20906+ struct inode *h_dir;
1facf9fc 20907+
027c5e7a 20908+ if (add_entry)
5527c038 20909+ IMustLock(d_inode(parent));
027c5e7a 20910+ else
4a4d8108
AM
20911+ di_write_lock_parent(parent);
20912+
20913+ err = 0;
20914+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 20915+ if (btop > bcpup)
c2b27bf2 20916+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 20917+ else if (btop < bcpup)
4a4d8108
AM
20918+ err = au_cpdown_dirs(dentry, bcpup);
20919+ else
c2b27bf2 20920+ BUG();
4a4d8108 20921+ }
38d290e6 20922+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 20923+ h_parent = au_h_dptr(parent, bcpup);
5527c038 20924+ h_dir = d_inode(h_parent);
be118d29 20925+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 20926+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 20927+ /* todo: no unlock here */
3c1bdaff 20928+ inode_unlock_shared(h_dir);
027c5e7a
AM
20929+
20930+ AuDbg("bcpup %d\n", bcpup);
20931+ if (!err) {
5527c038 20932+ if (d_really_is_negative(dentry))
5afbbe0d 20933+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
20934+ au_update_dbrange(dentry, /*do_put_zero*/0);
20935+ }
1308ab2a 20936+ }
1facf9fc 20937+
4a4d8108
AM
20938+ if (!add_entry)
20939+ di_write_unlock(parent);
20940+ if (!err)
20941+ err = bcpup; /* success */
1308ab2a 20942+
027c5e7a 20943+ AuTraceErr(err);
4a4d8108
AM
20944+ return err;
20945+}
1facf9fc 20946+
4a4d8108
AM
20947+/*
20948+ * decide the branch and the parent dir where we will create a new entry.
20949+ * returns new bindex or an error.
20950+ * copyup the parent dir if needed.
20951+ */
20952+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20953+ struct au_wr_dir_args *args)
20954+{
20955+ int err;
392086de 20956+ unsigned int flags;
5afbbe0d 20957+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
20958+ const unsigned char add_entry
20959+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 20960+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
20961+ struct super_block *sb;
20962+ struct dentry *parent;
20963+ struct au_sbinfo *sbinfo;
1facf9fc 20964+
4a4d8108
AM
20965+ sb = dentry->d_sb;
20966+ sbinfo = au_sbi(sb);
20967+ parent = dget_parent(dentry);
5afbbe0d
AM
20968+ btop = au_dbtop(dentry);
20969+ bcpup = btop;
4a4d8108
AM
20970+ if (args->force_btgt < 0) {
20971+ if (src_dentry) {
5afbbe0d
AM
20972+ src_btop = au_dbtop(src_dentry);
20973+ if (src_btop < btop)
20974+ bcpup = src_btop;
4a4d8108 20975+ } else if (add_entry) {
392086de
AM
20976+ flags = 0;
20977+ if (au_ftest_wrdir(args->flags, ISDIR))
20978+ au_fset_wbr(flags, DIR);
20979+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
20980+ bcpup = err;
20981+ }
1facf9fc 20982+
5527c038 20983+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
20984+ if (add_entry)
20985+ err = AuWbrCopyup(sbinfo, dentry);
20986+ else {
20987+ if (!IS_ROOT(dentry)) {
20988+ di_read_lock_parent(parent, !AuLock_IR);
20989+ err = AuWbrCopyup(sbinfo, dentry);
20990+ di_read_unlock(parent, !AuLock_IR);
20991+ } else
20992+ err = AuWbrCopyup(sbinfo, dentry);
20993+ }
20994+ bcpup = err;
20995+ if (unlikely(err < 0))
20996+ goto out;
20997+ }
20998+ } else {
20999+ bcpup = args->force_btgt;
5527c038 21000+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21001+ }
027c5e7a 21002+
5afbbe0d 21003+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21004+ err = bcpup;
5afbbe0d 21005+ if (bcpup == btop)
4a4d8108 21006+ goto out; /* success */
4a4d8108
AM
21007+
21008+ /* copyup the new parent into the branch we process */
5afbbe0d 21009+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21010+ if (err >= 0) {
5527c038 21011+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21012+ au_set_h_dptr(dentry, btop, NULL);
21013+ au_set_dbtop(dentry, bcpup);
21014+ au_set_dbbot(dentry, bcpup);
027c5e7a 21015+ }
38d290e6
JR
21016+ AuDebugOn(add_entry
21017+ && !au_ftest_wrdir(args->flags, TMPFILE)
21018+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21019+ }
86dc4139
AM
21020+
21021+out:
21022+ dput(parent);
21023+ return err;
21024+}
21025+
21026+/* ---------------------------------------------------------------------- */
21027+
21028+void au_pin_hdir_unlock(struct au_pin *p)
21029+{
21030+ if (p->hdir)
5afbbe0d 21031+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21032+}
21033+
c1595e42 21034+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21035+{
21036+ int err;
21037+
21038+ err = 0;
21039+ if (!p->hdir)
21040+ goto out;
21041+
21042+ /* even if an error happens later, keep this lock */
5afbbe0d 21043+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21044+
21045+ err = -EBUSY;
5527c038 21046+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21047+ goto out;
21048+
21049+ err = 0;
21050+ if (p->h_dentry)
21051+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21052+ p->h_parent, p->br);
21053+
21054+out:
21055+ return err;
21056+}
21057+
21058+int au_pin_hdir_relock(struct au_pin *p)
21059+{
21060+ int err, i;
21061+ struct inode *h_i;
21062+ struct dentry *h_d[] = {
21063+ p->h_dentry,
21064+ p->h_parent
21065+ };
21066+
21067+ err = au_pin_hdir_lock(p);
21068+ if (unlikely(err))
21069+ goto out;
21070+
21071+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21072+ if (!h_d[i])
21073+ continue;
5527c038
JR
21074+ if (d_is_positive(h_d[i])) {
21075+ h_i = d_inode(h_d[i]);
86dc4139 21076+ err = !h_i->i_nlink;
5527c038 21077+ }
86dc4139
AM
21078+ }
21079+
21080+out:
21081+ return err;
21082+}
21083+
5afbbe0d 21084+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21085+{
5afbbe0d
AM
21086+#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
21087+ p->hdir->hi_inode->i_rwsem.owner = task;
86dc4139
AM
21088+#endif
21089+}
21090+
21091+void au_pin_hdir_acquire_nest(struct au_pin *p)
21092+{
21093+ if (p->hdir) {
5afbbe0d 21094+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21095+ p->lsc_hi, 0, NULL, _RET_IP_);
21096+ au_pin_hdir_set_owner(p, current);
21097+ }
dece6358 21098+}
1facf9fc 21099+
86dc4139
AM
21100+void au_pin_hdir_release(struct au_pin *p)
21101+{
21102+ if (p->hdir) {
21103+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 21104+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
21105+ }
21106+}
1308ab2a 21107+
4a4d8108 21108+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21109+{
4a4d8108
AM
21110+ if (pin && pin->parent)
21111+ return au_h_dptr(pin->parent, pin->bindex);
21112+ return NULL;
dece6358 21113+}
1facf9fc 21114+
4a4d8108 21115+void au_unpin(struct au_pin *p)
dece6358 21116+{
86dc4139
AM
21117+ if (p->hdir)
21118+ au_pin_hdir_unlock(p);
e49829fe 21119+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21120+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21121+ if (!p->hdir)
21122+ return;
1facf9fc 21123+
4a4d8108
AM
21124+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21125+ di_read_unlock(p->parent, AuLock_IR);
21126+ iput(p->hdir->hi_inode);
21127+ dput(p->parent);
21128+ p->parent = NULL;
21129+ p->hdir = NULL;
21130+ p->h_mnt = NULL;
86dc4139 21131+ /* do not clear p->task */
4a4d8108 21132+}
1308ab2a 21133+
4a4d8108
AM
21134+int au_do_pin(struct au_pin *p)
21135+{
21136+ int err;
21137+ struct super_block *sb;
4a4d8108
AM
21138+ struct inode *h_dir;
21139+
21140+ err = 0;
21141+ sb = p->dentry->d_sb;
86dc4139 21142+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21143+ if (IS_ROOT(p->dentry)) {
21144+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21145+ p->h_mnt = au_br_mnt(p->br);
b4510431 21146+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21147+ if (unlikely(err)) {
21148+ au_fclr_pin(p->flags, MNT_WRITE);
21149+ goto out_err;
21150+ }
21151+ }
dece6358 21152+ goto out;
1facf9fc 21153+ }
21154+
86dc4139 21155+ p->h_dentry = NULL;
5afbbe0d 21156+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21157+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21158+
4a4d8108
AM
21159+ p->parent = dget_parent(p->dentry);
21160+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21161+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21162+
4a4d8108 21163+ h_dir = NULL;
86dc4139 21164+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21165+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21166+ if (p->hdir)
21167+ h_dir = p->hdir->hi_inode;
dece6358 21168+
b752ccd1
AM
21169+ /*
21170+ * udba case, or
21171+ * if DI_LOCKED is not set, then p->parent may be different
21172+ * and h_parent can be NULL.
21173+ */
86dc4139 21174+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21175+ err = -EBUSY;
4a4d8108
AM
21176+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21177+ di_read_unlock(p->parent, AuLock_IR);
21178+ dput(p->parent);
21179+ p->parent = NULL;
21180+ goto out_err;
21181+ }
1308ab2a 21182+
4a4d8108 21183+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21184+ p->h_mnt = au_br_mnt(p->br);
b4510431 21185+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21186+ if (unlikely(err)) {
4a4d8108 21187+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
21188+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21189+ di_read_unlock(p->parent, AuLock_IR);
21190+ dput(p->parent);
21191+ p->parent = NULL;
21192+ goto out_err;
dece6358
AM
21193+ }
21194+ }
4a4d8108 21195+
86dc4139
AM
21196+ au_igrab(h_dir);
21197+ err = au_pin_hdir_lock(p);
21198+ if (!err)
21199+ goto out; /* success */
21200+
076b876e
AM
21201+ au_unpin(p);
21202+
4f0767ce 21203+out_err:
4a4d8108
AM
21204+ pr_err("err %d\n", err);
21205+ err = au_busy_or_stale();
4f0767ce 21206+out:
1facf9fc 21207+ return err;
21208+}
21209+
4a4d8108
AM
21210+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21211+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21212+ unsigned int udba, unsigned char flags)
21213+{
21214+ p->dentry = dentry;
21215+ p->udba = udba;
21216+ p->lsc_di = lsc_di;
21217+ p->lsc_hi = lsc_hi;
21218+ p->flags = flags;
21219+ p->bindex = bindex;
21220+
21221+ p->parent = NULL;
21222+ p->hdir = NULL;
21223+ p->h_mnt = NULL;
86dc4139
AM
21224+
21225+ p->h_dentry = NULL;
21226+ p->h_parent = NULL;
21227+ p->br = NULL;
21228+ p->task = current;
4a4d8108
AM
21229+}
21230+
21231+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21232+ unsigned int udba, unsigned char flags)
21233+{
21234+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21235+ udba, flags);
21236+ return au_do_pin(pin);
21237+}
21238+
dece6358
AM
21239+/* ---------------------------------------------------------------------- */
21240+
1308ab2a 21241+/*
4a4d8108
AM
21242+ * ->setattr() and ->getattr() are called in various cases.
21243+ * chmod, stat: dentry is revalidated.
21244+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21245+ * unhashed.
21246+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21247+ */
027c5e7a 21248+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21249+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21250+{
4a4d8108 21251+ int err;
4a4d8108 21252+ struct dentry *parent;
1facf9fc 21253+
1308ab2a 21254+ err = 0;
027c5e7a 21255+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21256+ parent = dget_parent(dentry);
21257+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21258+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21259+ di_read_unlock(parent, AuLock_IR);
21260+ dput(parent);
dece6358 21261+ }
1facf9fc 21262+
4a4d8108 21263+ AuTraceErr(err);
1308ab2a 21264+ return err;
21265+}
dece6358 21266+
c1595e42
JR
21267+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21268+ struct au_icpup_args *a)
1308ab2a 21269+{
21270+ int err;
4a4d8108 21271+ loff_t sz;
5afbbe0d 21272+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21273+ struct dentry *hi_wh, *parent;
21274+ struct inode *inode;
4a4d8108
AM
21275+ struct au_wr_dir_args wr_dir_args = {
21276+ .force_btgt = -1,
21277+ .flags = 0
21278+ };
21279+
2000de60 21280+ if (d_is_dir(dentry))
4a4d8108
AM
21281+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21282+ /* plink or hi_wh() case */
5afbbe0d 21283+ btop = au_dbtop(dentry);
5527c038 21284+ inode = d_inode(dentry);
5afbbe0d
AM
21285+ ibtop = au_ibtop(inode);
21286+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21287+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21288+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21289+ if (unlikely(err < 0))
21290+ goto out;
21291+ a->btgt = err;
5afbbe0d 21292+ if (err != btop)
4a4d8108
AM
21293+ au_fset_icpup(a->flags, DID_CPUP);
21294+
21295+ err = 0;
21296+ a->pin_flags = AuPin_MNT_WRITE;
21297+ parent = NULL;
21298+ if (!IS_ROOT(dentry)) {
21299+ au_fset_pin(a->pin_flags, DI_LOCKED);
21300+ parent = dget_parent(dentry);
21301+ di_write_lock_parent(parent);
21302+ }
21303+
21304+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21305+ if (unlikely(err))
21306+ goto out_parent;
21307+
4a4d8108 21308+ sz = -1;
5afbbe0d 21309+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21310+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21311+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 21312+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21313+ if (ia->ia_size < i_size_read(a->h_inode))
21314+ sz = ia->ia_size;
3c1bdaff 21315+ inode_unlock_shared(a->h_inode);
c1595e42 21316+ }
4a4d8108 21317+
4a4d8108 21318+ hi_wh = NULL;
027c5e7a 21319+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21320+ hi_wh = au_hi_wh(inode, a->btgt);
21321+ if (!hi_wh) {
c2b27bf2
AM
21322+ struct au_cp_generic cpg = {
21323+ .dentry = dentry,
21324+ .bdst = a->btgt,
21325+ .bsrc = -1,
21326+ .len = sz,
21327+ .pin = &a->pin
21328+ };
21329+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21330+ if (unlikely(err))
21331+ goto out_unlock;
21332+ hi_wh = au_hi_wh(inode, a->btgt);
21333+ /* todo: revalidate hi_wh? */
21334+ }
21335+ }
21336+
21337+ if (parent) {
21338+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21339+ di_downgrade_lock(parent, AuLock_IR);
21340+ dput(parent);
21341+ parent = NULL;
21342+ }
21343+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21344+ goto out; /* success */
21345+
21346+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21347+ struct au_cp_generic cpg = {
21348+ .dentry = dentry,
21349+ .bdst = a->btgt,
5afbbe0d 21350+ .bsrc = btop,
c2b27bf2
AM
21351+ .len = sz,
21352+ .pin = &a->pin,
21353+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21354+ };
21355+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21356+ if (!err)
21357+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21358+ } else if (!hi_wh)
21359+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21360+ else
21361+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21362+
4f0767ce 21363+out_unlock:
5527c038 21364+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21365+ if (!err)
dece6358 21366+ goto out; /* success */
4a4d8108 21367+ au_unpin(&a->pin);
4f0767ce 21368+out_parent:
4a4d8108
AM
21369+ if (parent) {
21370+ di_write_unlock(parent);
21371+ dput(parent);
21372+ }
4f0767ce 21373+out:
86dc4139 21374+ if (!err)
febd17d6 21375+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21376+ return err;
21377+}
21378+
4a4d8108 21379+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 21380+{
4a4d8108 21381+ int err;
523b37e3 21382+ struct inode *inode, *delegated;
4a4d8108
AM
21383+ struct super_block *sb;
21384+ struct file *file;
21385+ struct au_icpup_args *a;
1facf9fc 21386+
5527c038 21387+ inode = d_inode(dentry);
4a4d8108 21388+ IMustLock(inode);
dece6358 21389+
f2c43d5f
AM
21390+ err = setattr_prepare(dentry, ia);
21391+ if (unlikely(err))
21392+ goto out;
21393+
4a4d8108
AM
21394+ err = -ENOMEM;
21395+ a = kzalloc(sizeof(*a), GFP_NOFS);
21396+ if (unlikely(!a))
21397+ goto out;
1facf9fc 21398+
4a4d8108
AM
21399+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21400+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21401+
4a4d8108
AM
21402+ file = NULL;
21403+ sb = dentry->d_sb;
e49829fe
JR
21404+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21405+ if (unlikely(err))
21406+ goto out_kfree;
21407+
4a4d8108
AM
21408+ if (ia->ia_valid & ATTR_FILE) {
21409+ /* currently ftruncate(2) only */
7e9cd9fe 21410+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21411+ file = ia->ia_file;
521ced18
JR
21412+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21413+ /*fi_lsc*/0);
4a4d8108
AM
21414+ if (unlikely(err))
21415+ goto out_si;
21416+ ia->ia_file = au_hf_top(file);
21417+ a->udba = AuOpt_UDBA_NONE;
21418+ } else {
21419+ /* fchmod() doesn't pass ia_file */
21420+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21421+ di_write_lock_child(dentry);
21422+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21423+ if (d_unhashed(dentry))
21424+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21425+ if (a->udba != AuOpt_UDBA_NONE) {
21426+ AuDebugOn(IS_ROOT(dentry));
21427+ err = au_reval_for_attr(dentry, au_sigen(sb));
21428+ if (unlikely(err))
21429+ goto out_dentry;
21430+ }
dece6358 21431+ }
dece6358 21432+
4a4d8108
AM
21433+ err = au_pin_and_icpup(dentry, ia, a);
21434+ if (unlikely(err < 0))
21435+ goto out_dentry;
21436+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21437+ ia->ia_file = NULL;
21438+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21439+ }
dece6358 21440+
4a4d8108
AM
21441+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21442+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21443+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21444+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21445+ if (unlikely(err))
21446+ goto out_unlock;
21447+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21448+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21449+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21450+ if (unlikely(err))
21451+ goto out_unlock;
21452+ }
dece6358 21453+
4a4d8108
AM
21454+ if (ia->ia_valid & ATTR_SIZE) {
21455+ struct file *f;
1308ab2a 21456+
953406b4 21457+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21458+ /* unmap only */
953406b4 21459+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21460+
4a4d8108
AM
21461+ f = NULL;
21462+ if (ia->ia_valid & ATTR_FILE)
21463+ f = ia->ia_file;
febd17d6 21464+ inode_unlock(a->h_inode);
4a4d8108 21465+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21466+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21467+ } else {
21468+ delegated = NULL;
21469+ while (1) {
21470+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21471+ if (delegated) {
21472+ err = break_deleg_wait(&delegated);
21473+ if (!err)
21474+ continue;
21475+ }
21476+ break;
21477+ }
21478+ }
8cdd5066
JR
21479+ /*
21480+ * regardless aufs 'acl' option setting.
21481+ * why don't all acl-aware fs call this func from their ->setattr()?
21482+ */
21483+ if (!err && (ia->ia_valid & ATTR_MODE))
21484+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
21485+ if (!err)
21486+ au_cpup_attr_changeable(inode);
1308ab2a 21487+
4f0767ce 21488+out_unlock:
febd17d6 21489+ inode_unlock(a->h_inode);
4a4d8108 21490+ au_unpin(&a->pin);
027c5e7a 21491+ if (unlikely(err))
5afbbe0d 21492+ au_update_dbtop(dentry);
4f0767ce 21493+out_dentry:
4a4d8108
AM
21494+ di_write_unlock(dentry);
21495+ if (file) {
21496+ fi_write_unlock(file);
21497+ ia->ia_file = file;
21498+ ia->ia_valid |= ATTR_FILE;
21499+ }
4f0767ce 21500+out_si:
4a4d8108 21501+ si_read_unlock(sb);
e49829fe 21502+out_kfree:
1c60b727 21503+ kfree(a);
4f0767ce 21504+out:
4a4d8108
AM
21505+ AuTraceErr(err);
21506+ return err;
1facf9fc 21507+}
21508+
c1595e42
JR
21509+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21510+static int au_h_path_to_set_attr(struct dentry *dentry,
21511+ struct au_icpup_args *a, struct path *h_path)
21512+{
21513+ int err;
21514+ struct super_block *sb;
21515+
21516+ sb = dentry->d_sb;
21517+ a->udba = au_opt_udba(sb);
21518+ /* no d_unlinked(), to set UDBA_NONE for root */
21519+ if (d_unhashed(dentry))
21520+ a->udba = AuOpt_UDBA_NONE;
21521+ if (a->udba != AuOpt_UDBA_NONE) {
21522+ AuDebugOn(IS_ROOT(dentry));
21523+ err = au_reval_for_attr(dentry, au_sigen(sb));
21524+ if (unlikely(err))
21525+ goto out;
21526+ }
21527+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21528+ if (unlikely(err < 0))
21529+ goto out;
21530+
21531+ h_path->dentry = a->h_path.dentry;
21532+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21533+
21534+out:
21535+ return err;
21536+}
21537+
f2c43d5f
AM
21538+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21539+ struct au_sxattr *arg)
c1595e42
JR
21540+{
21541+ int err;
21542+ struct path h_path;
21543+ struct super_block *sb;
21544+ struct au_icpup_args *a;
5afbbe0d 21545+ struct inode *h_inode;
c1595e42 21546+
c1595e42
JR
21547+ IMustLock(inode);
21548+
21549+ err = -ENOMEM;
21550+ a = kzalloc(sizeof(*a), GFP_NOFS);
21551+ if (unlikely(!a))
21552+ goto out;
21553+
21554+ sb = dentry->d_sb;
21555+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21556+ if (unlikely(err))
21557+ goto out_kfree;
21558+
21559+ h_path.dentry = NULL; /* silence gcc */
21560+ di_write_lock_child(dentry);
21561+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21562+ if (unlikely(err))
21563+ goto out_di;
21564+
febd17d6 21565+ inode_unlock(a->h_inode);
c1595e42
JR
21566+ switch (arg->type) {
21567+ case AU_XATTR_SET:
5afbbe0d 21568+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
21569+ err = vfsub_setxattr(h_path.dentry,
21570+ arg->u.set.name, arg->u.set.value,
21571+ arg->u.set.size, arg->u.set.flags);
21572+ break;
c1595e42
JR
21573+ case AU_ACL_SET:
21574+ err = -EOPNOTSUPP;
5527c038 21575+ h_inode = d_inode(h_path.dentry);
c1595e42 21576+ if (h_inode->i_op->set_acl)
f2c43d5f 21577+ /* this will call posix_acl_update_mode */
c1595e42
JR
21578+ err = h_inode->i_op->set_acl(h_inode,
21579+ arg->u.acl_set.acl,
21580+ arg->u.acl_set.type);
21581+ break;
21582+ }
21583+ if (!err)
21584+ au_cpup_attr_timesizes(inode);
21585+
21586+ au_unpin(&a->pin);
21587+ if (unlikely(err))
5afbbe0d 21588+ au_update_dbtop(dentry);
c1595e42
JR
21589+
21590+out_di:
21591+ di_write_unlock(dentry);
21592+ si_read_unlock(sb);
21593+out_kfree:
1c60b727 21594+ kfree(a);
c1595e42
JR
21595+out:
21596+ AuTraceErr(err);
21597+ return err;
21598+}
21599+#endif
21600+
4a4d8108
AM
21601+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21602+ unsigned int nlink)
1facf9fc 21603+{
9dbd164d
AM
21604+ unsigned int n;
21605+
4a4d8108 21606+ inode->i_mode = st->mode;
86dc4139
AM
21607+ /* don't i_[ug]id_write() here */
21608+ inode->i_uid = st->uid;
21609+ inode->i_gid = st->gid;
4a4d8108
AM
21610+ inode->i_atime = st->atime;
21611+ inode->i_mtime = st->mtime;
21612+ inode->i_ctime = st->ctime;
1facf9fc 21613+
4a4d8108
AM
21614+ au_cpup_attr_nlink(inode, /*force*/0);
21615+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21616+ n = inode->i_nlink;
21617+ n -= nlink;
21618+ n += st->nlink;
f6b6e03d 21619+ smp_mb(); /* for i_nlink */
7eafdf33 21620+ /* 0 can happen */
92d182d2 21621+ set_nlink(inode, n);
4a4d8108 21622+ }
1facf9fc 21623+
4a4d8108
AM
21624+ spin_lock(&inode->i_lock);
21625+ inode->i_blocks = st->blocks;
21626+ i_size_write(inode, st->size);
21627+ spin_unlock(&inode->i_lock);
1facf9fc 21628+}
21629+
c1595e42 21630+/*
f2c43d5f 21631+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21632+ * returns zero or negative (an error).
21633+ * @dentry will be read-locked in success.
21634+ */
a2654f78
AM
21635+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21636+ int locked)
1facf9fc 21637+{
4a4d8108 21638+ int err;
076b876e 21639+ unsigned int mnt_flags, sigen;
c1595e42 21640+ unsigned char udba_none;
4a4d8108 21641+ aufs_bindex_t bindex;
4a4d8108
AM
21642+ struct super_block *sb, *h_sb;
21643+ struct inode *inode;
1facf9fc 21644+
c1595e42
JR
21645+ h_path->mnt = NULL;
21646+ h_path->dentry = NULL;
21647+
21648+ err = 0;
4a4d8108 21649+ sb = dentry->d_sb;
4a4d8108
AM
21650+ mnt_flags = au_mntflags(sb);
21651+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21652+
a2654f78
AM
21653+ if (unlikely(locked))
21654+ goto body; /* skip locking dinfo */
21655+
4a4d8108 21656+ /* support fstat(2) */
027c5e7a 21657+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21658+ sigen = au_sigen(sb);
027c5e7a
AM
21659+ err = au_digen_test(dentry, sigen);
21660+ if (!err) {
4a4d8108 21661+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21662+ err = au_dbrange_test(dentry);
c1595e42
JR
21663+ if (unlikely(err)) {
21664+ di_read_unlock(dentry, AuLock_IR);
21665+ goto out;
21666+ }
027c5e7a 21667+ } else {
4a4d8108
AM
21668+ AuDebugOn(IS_ROOT(dentry));
21669+ di_write_lock_child(dentry);
027c5e7a
AM
21670+ err = au_dbrange_test(dentry);
21671+ if (!err)
21672+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21673+ if (!err)
21674+ di_downgrade_lock(dentry, AuLock_IR);
21675+ else {
21676+ di_write_unlock(dentry);
21677+ goto out;
21678+ }
4a4d8108
AM
21679+ }
21680+ } else
21681+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21682+
a2654f78 21683+body:
5527c038 21684+ inode = d_inode(dentry);
5afbbe0d 21685+ bindex = au_ibtop(inode);
c1595e42
JR
21686+ h_path->mnt = au_sbr_mnt(sb, bindex);
21687+ h_sb = h_path->mnt->mnt_sb;
21688+ if (!force
21689+ && !au_test_fs_bad_iattr(h_sb)
21690+ && udba_none)
21691+ goto out; /* success */
1facf9fc 21692+
5afbbe0d 21693+ if (au_dbtop(dentry) == bindex)
c1595e42 21694+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21695+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21696+ h_path->dentry = au_plink_lkup(inode, bindex);
21697+ if (IS_ERR(h_path->dentry))
21698+ /* pretending success */
21699+ h_path->dentry = NULL;
21700+ else
21701+ dput(h_path->dentry);
4a4d8108 21702+ }
c1595e42
JR
21703+
21704+out:
21705+ return err;
21706+}
21707+
521ced18
JR
21708+static int aufs_getattr(const struct path *path, struct kstat *st,
21709+ u32 request, unsigned int query)
c1595e42
JR
21710+{
21711+ int err;
21712+ unsigned char positive;
21713+ struct path h_path;
521ced18 21714+ struct dentry *dentry;
c1595e42
JR
21715+ struct inode *inode;
21716+ struct super_block *sb;
21717+
521ced18 21718+ dentry = path->dentry;
5527c038 21719+ inode = d_inode(dentry);
c1595e42
JR
21720+ sb = dentry->d_sb;
21721+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21722+ if (unlikely(err))
21723+ goto out;
a2654f78 21724+ err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
c1595e42
JR
21725+ if (unlikely(err))
21726+ goto out_si;
c06a8ce3 21727+ if (unlikely(!h_path.dentry))
c1595e42 21728+ /* illegally overlapped or something */
4a4d8108
AM
21729+ goto out_fill; /* pretending success */
21730+
5527c038 21731+ positive = d_is_positive(h_path.dentry);
4a4d8108 21732+ if (positive)
521ced18
JR
21733+ /* no vfsub version */
21734+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21735+ if (!err) {
21736+ if (positive)
c06a8ce3 21737+ au_refresh_iattr(inode, st,
5527c038 21738+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21739+ goto out_fill; /* success */
1facf9fc 21740+ }
7f207e10 21741+ AuTraceErr(err);
c1595e42 21742+ goto out_di;
4a4d8108 21743+
4f0767ce 21744+out_fill:
4a4d8108 21745+ generic_fillattr(inode, st);
c1595e42 21746+out_di:
4a4d8108 21747+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21748+out_si:
4a4d8108 21749+ si_read_unlock(sb);
7f207e10
AM
21750+out:
21751+ AuTraceErr(err);
4a4d8108 21752+ return err;
1facf9fc 21753+}
21754+
21755+/* ---------------------------------------------------------------------- */
21756+
febd17d6
JR
21757+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21758+ struct delayed_call *done)
4a4d8108 21759+{
c2c0f25c 21760+ const char *ret;
c2c0f25c 21761+ struct dentry *h_dentry;
febd17d6 21762+ struct inode *h_inode;
4a4d8108 21763+ int err;
c2c0f25c 21764+ aufs_bindex_t bindex;
1facf9fc 21765+
79b8bda9 21766+ ret = NULL; /* suppress a warning */
febd17d6
JR
21767+ err = -ECHILD;
21768+ if (!dentry)
21769+ goto out;
21770+
027c5e7a
AM
21771+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21772+ if (unlikely(err))
c2c0f25c 21773+ goto out;
027c5e7a
AM
21774+
21775+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
21776+ if (unlikely(err))
21777+ goto out_unlock;
21778+
21779+ err = -EINVAL;
21780+ inode = d_inode(dentry);
5afbbe0d 21781+ bindex = au_ibtop(inode);
c2c0f25c 21782+ h_inode = au_h_iptr(inode, bindex);
febd17d6 21783+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
21784+ goto out_unlock;
21785+
21786+ err = -EBUSY;
21787+ h_dentry = NULL;
5afbbe0d 21788+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
21789+ h_dentry = au_h_dptr(dentry, bindex);
21790+ if (h_dentry)
21791+ dget(h_dentry);
027c5e7a 21792+ }
c2c0f25c
AM
21793+ if (!h_dentry) {
21794+ h_dentry = d_find_any_alias(h_inode);
21795+ if (IS_ERR(h_dentry)) {
21796+ err = PTR_ERR(h_dentry);
febd17d6 21797+ goto out_unlock;
c2c0f25c
AM
21798+ }
21799+ }
21800+ if (unlikely(!h_dentry))
febd17d6 21801+ goto out_unlock;
1facf9fc 21802+
c2c0f25c 21803+ err = 0;
febd17d6 21804+ AuDbg("%pf\n", h_inode->i_op->get_link);
c2c0f25c 21805+ AuDbgDentry(h_dentry);
f2c43d5f 21806+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 21807+ dput(h_dentry);
febd17d6
JR
21808+ if (IS_ERR(ret))
21809+ err = PTR_ERR(ret);
c2c0f25c 21810+
c2c0f25c
AM
21811+out_unlock:
21812+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 21813+out:
c2c0f25c
AM
21814+ if (unlikely(err))
21815+ ret = ERR_PTR(err);
21816+ AuTraceErrPtr(ret);
21817+ return ret;
4a4d8108 21818+}
1facf9fc 21819+
4a4d8108 21820+/* ---------------------------------------------------------------------- */
1facf9fc 21821+
e2f27e51
AM
21822+static int au_is_special(struct inode *inode)
21823+{
21824+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21825+}
21826+
0c3ec466 21827+static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
4a4d8108 21828+{
0c3ec466 21829+ int err;
e2f27e51 21830+ aufs_bindex_t bindex;
0c3ec466
AM
21831+ struct super_block *sb;
21832+ struct inode *h_inode;
e2f27e51 21833+ struct vfsmount *h_mnt;
0c3ec466
AM
21834+
21835+ sb = inode->i_sb;
e2f27e51
AM
21836+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21837+ "unexpected s_flags 0x%lx", sb->s_flags);
21838+
0c3ec466
AM
21839+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
21840+ lockdep_off();
21841+ si_read_lock(sb, AuLock_FLUSH);
21842+ ii_write_lock_child(inode);
e2f27e51
AM
21843+
21844+ err = 0;
21845+ bindex = au_ibtop(inode);
21846+ h_inode = au_h_iptr(inode, bindex);
21847+ if (!au_test_ro(sb, bindex, inode)) {
21848+ h_mnt = au_sbr_mnt(sb, bindex);
21849+ err = vfsub_mnt_want_write(h_mnt);
21850+ if (!err) {
21851+ err = vfsub_update_time(h_inode, ts, flags);
21852+ vfsub_mnt_drop_write(h_mnt);
21853+ }
21854+ } else if (au_is_special(h_inode)) {
21855+ /*
21856+ * Never copy-up here.
21857+ * These special files may already be opened and used for
21858+ * communicating. If we copied it up, then the communication
21859+ * would be corrupted.
21860+ */
21861+ AuWarn1("timestamps for i%lu are ignored "
21862+ "since it is on readonly branch (hi%lu).\n",
21863+ inode->i_ino, h_inode->i_ino);
21864+ } else if (flags & ~S_ATIME) {
21865+ err = -EIO;
21866+ AuIOErr1("unexpected flags 0x%x\n", flags);
21867+ AuDebugOn(1);
21868+ }
21869+
38d290e6
JR
21870+ if (!err)
21871+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
21872+ ii_write_unlock(inode);
21873+ si_read_unlock(sb);
21874+ lockdep_on();
38d290e6
JR
21875+
21876+ if (!err && (flags & S_VERSION))
21877+ inode_inc_iversion(inode);
21878+
0c3ec466 21879+ return err;
4a4d8108 21880+}
1facf9fc 21881+
4a4d8108 21882+/* ---------------------------------------------------------------------- */
1308ab2a 21883+
b95c5147
AM
21884+/* no getattr version will be set by module.c:aufs_init() */
21885+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21886+ aufs_iop[] = {
21887+ [AuIop_SYMLINK] = {
21888+ .permission = aufs_permission,
c1595e42 21889+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21890+ .get_acl = aufs_get_acl,
21891+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
21892+#endif
21893+
b95c5147
AM
21894+ .setattr = aufs_setattr,
21895+ .getattr = aufs_getattr,
0c3ec466 21896+
c1595e42 21897+#ifdef CONFIG_AUFS_XATTR
b95c5147 21898+ .listxattr = aufs_listxattr,
c1595e42
JR
21899+#endif
21900+
febd17d6 21901+ .get_link = aufs_get_link,
0c3ec466 21902+
b95c5147
AM
21903+ /* .update_time = aufs_update_time */
21904+ },
21905+ [AuIop_DIR] = {
21906+ .create = aufs_create,
21907+ .lookup = aufs_lookup,
21908+ .link = aufs_link,
21909+ .unlink = aufs_unlink,
21910+ .symlink = aufs_symlink,
21911+ .mkdir = aufs_mkdir,
21912+ .rmdir = aufs_rmdir,
21913+ .mknod = aufs_mknod,
21914+ .rename = aufs_rename,
21915+
21916+ .permission = aufs_permission,
c1595e42 21917+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21918+ .get_acl = aufs_get_acl,
21919+ .set_acl = aufs_set_acl,
c1595e42
JR
21920+#endif
21921+
b95c5147
AM
21922+ .setattr = aufs_setattr,
21923+ .getattr = aufs_getattr,
0c3ec466 21924+
c1595e42 21925+#ifdef CONFIG_AUFS_XATTR
b95c5147 21926+ .listxattr = aufs_listxattr,
c1595e42
JR
21927+#endif
21928+
b95c5147
AM
21929+ .update_time = aufs_update_time,
21930+ .atomic_open = aufs_atomic_open,
21931+ .tmpfile = aufs_tmpfile
21932+ },
21933+ [AuIop_OTHER] = {
21934+ .permission = aufs_permission,
c1595e42 21935+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21936+ .get_acl = aufs_get_acl,
21937+ .set_acl = aufs_set_acl,
c1595e42
JR
21938+#endif
21939+
b95c5147
AM
21940+ .setattr = aufs_setattr,
21941+ .getattr = aufs_getattr,
0c3ec466 21942+
c1595e42 21943+#ifdef CONFIG_AUFS_XATTR
b95c5147 21944+ .listxattr = aufs_listxattr,
c1595e42
JR
21945+#endif
21946+
b95c5147
AM
21947+ .update_time = aufs_update_time
21948+ }
4a4d8108 21949+};
7f207e10
AM
21950diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
21951--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 21952+++ linux/fs/aufs/i_op_del.c 2018-06-04 09:08:09.184746078 +0200
5afbbe0d 21953@@ -0,0 +1,511 @@
1facf9fc 21954+/*
b00004a5 21955+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 21956+ *
21957+ * This program, aufs is free software; you can redistribute it and/or modify
21958+ * it under the terms of the GNU General Public License as published by
21959+ * the Free Software Foundation; either version 2 of the License, or
21960+ * (at your option) any later version.
dece6358
AM
21961+ *
21962+ * This program is distributed in the hope that it will be useful,
21963+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21964+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21965+ * GNU General Public License for more details.
21966+ *
21967+ * You should have received a copy of the GNU General Public License
523b37e3 21968+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 21969+ */
21970+
21971+/*
4a4d8108 21972+ * inode operations (del entry)
1308ab2a 21973+ */
dece6358 21974+
1308ab2a 21975+#include "aufs.h"
dece6358 21976+
4a4d8108
AM
21977+/*
21978+ * decide if a new whiteout for @dentry is necessary or not.
21979+ * when it is necessary, prepare the parent dir for the upper branch whose
21980+ * branch index is @bcpup for creation. the actual creation of the whiteout will
21981+ * be done by caller.
21982+ * return value:
21983+ * 0: wh is unnecessary
21984+ * plus: wh is necessary
21985+ * minus: error
21986+ */
21987+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 21988+{
4a4d8108 21989+ int need_wh, err;
5afbbe0d 21990+ aufs_bindex_t btop;
4a4d8108 21991+ struct super_block *sb;
dece6358 21992+
4a4d8108 21993+ sb = dentry->d_sb;
5afbbe0d 21994+ btop = au_dbtop(dentry);
4a4d8108 21995+ if (*bcpup < 0) {
5afbbe0d
AM
21996+ *bcpup = btop;
21997+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
21998+ err = AuWbrCopyup(au_sbi(sb), dentry);
21999+ *bcpup = err;
22000+ if (unlikely(err < 0))
22001+ goto out;
22002+ }
22003+ } else
5afbbe0d 22004+ AuDebugOn(btop < *bcpup
5527c038 22005+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22006+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22007+
5afbbe0d 22008+ if (*bcpup != btop) {
4a4d8108
AM
22009+ err = au_cpup_dirs(dentry, *bcpup);
22010+ if (unlikely(err))
22011+ goto out;
22012+ need_wh = 1;
22013+ } else {
027c5e7a 22014+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22015+
027c5e7a
AM
22016+ need_wh = -ENOMEM;
22017+ dinfo = au_di(dentry);
22018+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22019+ if (tmp) {
22020+ au_di_cp(tmp, dinfo);
22021+ au_di_swap(tmp, dinfo);
22022+ /* returns the number of positive dentries */
5afbbe0d
AM
22023+ need_wh = au_lkup_dentry(dentry, btop + 1,
22024+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22025+ au_di_swap(tmp, dinfo);
22026+ au_rw_write_unlock(&tmp->di_rwsem);
22027+ au_di_free(tmp);
4a4d8108
AM
22028+ }
22029+ }
22030+ AuDbg("need_wh %d\n", need_wh);
22031+ err = need_wh;
22032+
4f0767ce 22033+out:
4a4d8108 22034+ return err;
1facf9fc 22035+}
22036+
4a4d8108
AM
22037+/*
22038+ * simple tests for the del-entry operations.
22039+ * following the checks in vfs, plus the parent-child relationship.
22040+ */
22041+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22042+ struct dentry *h_parent, int isdir)
1facf9fc 22043+{
4a4d8108
AM
22044+ int err;
22045+ umode_t h_mode;
22046+ struct dentry *h_dentry, *h_latest;
1308ab2a 22047+ struct inode *h_inode;
1facf9fc 22048+
4a4d8108 22049+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22050+ if (d_really_is_positive(dentry)) {
4a4d8108 22051+ err = -ENOENT;
5527c038
JR
22052+ if (unlikely(d_is_negative(h_dentry)))
22053+ goto out;
22054+ h_inode = d_inode(h_dentry);
22055+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22056+ goto out;
1facf9fc 22057+
4a4d8108
AM
22058+ h_mode = h_inode->i_mode;
22059+ if (!isdir) {
22060+ err = -EISDIR;
22061+ if (unlikely(S_ISDIR(h_mode)))
22062+ goto out;
22063+ } else if (unlikely(!S_ISDIR(h_mode))) {
22064+ err = -ENOTDIR;
22065+ goto out;
22066+ }
22067+ } else {
22068+ /* rename(2) case */
22069+ err = -EIO;
5527c038 22070+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22071+ goto out;
22072+ }
1facf9fc 22073+
4a4d8108
AM
22074+ err = -ENOENT;
22075+ /* expected parent dir is locked */
22076+ if (unlikely(h_parent != h_dentry->d_parent))
22077+ goto out;
22078+ err = 0;
22079+
22080+ /*
22081+ * rmdir a dir may break the consistency on some filesystem.
22082+ * let's try heavy test.
22083+ */
22084+ err = -EACCES;
076b876e 22085+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 22086+ && au_test_h_perm(d_inode(h_parent),
076b876e 22087+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22088+ goto out;
22089+
076b876e 22090+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
22091+ err = -EIO;
22092+ if (IS_ERR(h_latest))
22093+ goto out;
22094+ if (h_latest == h_dentry)
22095+ err = 0;
22096+ dput(h_latest);
22097+
4f0767ce 22098+out:
4a4d8108 22099+ return err;
1308ab2a 22100+}
1facf9fc 22101+
4a4d8108
AM
22102+/*
22103+ * decide the branch where we operate for @dentry. the branch index will be set
22104+ * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
22105+ * dir for reverting.
22106+ * when a new whiteout is necessary, create it.
22107+ */
22108+static struct dentry*
22109+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22110+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22111+{
4a4d8108
AM
22112+ struct dentry *wh_dentry;
22113+ struct super_block *sb;
22114+ struct path h_path;
22115+ int err, need_wh;
22116+ unsigned int udba;
22117+ aufs_bindex_t bcpup;
dece6358 22118+
4a4d8108
AM
22119+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22120+ wh_dentry = ERR_PTR(need_wh);
22121+ if (unlikely(need_wh < 0))
22122+ goto out;
22123+
22124+ sb = dentry->d_sb;
22125+ udba = au_opt_udba(sb);
22126+ bcpup = *rbcpup;
22127+ err = au_pin(pin, dentry, bcpup, udba,
22128+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22129+ wh_dentry = ERR_PTR(err);
22130+ if (unlikely(err))
22131+ goto out;
22132+
22133+ h_path.dentry = au_pinned_h_parent(pin);
22134+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22135+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22136+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22137+ wh_dentry = ERR_PTR(err);
22138+ if (unlikely(err))
22139+ goto out_unpin;
22140+ }
22141+
22142+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22143+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22144+ wh_dentry = NULL;
22145+ if (!need_wh)
22146+ goto out; /* success, no need to create whiteout */
22147+
22148+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22149+ if (IS_ERR(wh_dentry))
22150+ goto out_unpin;
22151+
22152+ /* returns with the parent is locked and wh_dentry is dget-ed */
22153+ goto out; /* success */
22154+
4f0767ce 22155+out_unpin:
4a4d8108 22156+ au_unpin(pin);
4f0767ce 22157+out:
4a4d8108 22158+ return wh_dentry;
1facf9fc 22159+}
22160+
4a4d8108
AM
22161+/*
22162+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22163+ * in order to be revertible and save time for removing many child whiteouts
22164+ * under the dir.
22165+ * returns 1 when there are too many child whiteout and caller should remove
22166+ * them asynchronously. returns 0 when the number of children is enough small to
22167+ * remove now or the branch fs is a remote fs.
22168+ * otherwise return an error.
22169+ */
22170+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22171+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22172+{
4a4d8108
AM
22173+ int rmdir_later, err, dirwh;
22174+ struct dentry *h_dentry;
22175+ struct super_block *sb;
5527c038 22176+ struct inode *inode;
4a4d8108
AM
22177+
22178+ sb = dentry->d_sb;
22179+ SiMustAnyLock(sb);
22180+ h_dentry = au_h_dptr(dentry, bindex);
22181+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22182+ if (unlikely(err))
22183+ goto out;
22184+
22185+ /* stop monitoring */
5527c038
JR
22186+ inode = d_inode(dentry);
22187+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22188+
22189+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22190+ dirwh = au_sbi(sb)->si_dirwh;
22191+ rmdir_later = (dirwh <= 1);
22192+ if (!rmdir_later)
22193+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22194+ dirwh);
22195+ if (rmdir_later)
22196+ return rmdir_later;
22197+ }
1facf9fc 22198+
4a4d8108
AM
22199+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22200+ if (unlikely(err)) {
523b37e3
AM
22201+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22202+ h_dentry, bindex, err);
4a4d8108
AM
22203+ err = 0;
22204+ }
dece6358 22205+
4f0767ce 22206+out:
4a4d8108
AM
22207+ AuTraceErr(err);
22208+ return err;
22209+}
1308ab2a 22210+
4a4d8108
AM
22211+/*
22212+ * final procedure for deleting a entry.
22213+ * maintain dentry and iattr.
22214+ */
22215+static void epilog(struct inode *dir, struct dentry *dentry,
22216+ aufs_bindex_t bindex)
22217+{
22218+ struct inode *inode;
1308ab2a 22219+
5527c038 22220+ inode = d_inode(dentry);
4a4d8108
AM
22221+ d_drop(dentry);
22222+ inode->i_ctime = dir->i_ctime;
1308ab2a 22223+
b912730e 22224+ au_dir_ts(dir, bindex);
be118d29 22225+ inode_inc_iversion(dir);
1facf9fc 22226+}
22227+
4a4d8108
AM
22228+/*
22229+ * when an error happened, remove the created whiteout and revert everything.
22230+ */
7f207e10
AM
22231+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22232+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22233+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22234+{
4a4d8108
AM
22235+ int rerr;
22236+ struct path h_path = {
22237+ .dentry = wh_dentry,
7f207e10 22238+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22239+ };
dece6358 22240+
7f207e10 22241+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22242+ if (!rerr) {
22243+ au_set_dbwh(dentry, bwh);
22244+ au_dtime_revert(dt);
22245+ return 0;
22246+ }
dece6358 22247+
523b37e3 22248+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22249+ return -EIO;
1facf9fc 22250+}
22251+
4a4d8108 22252+/* ---------------------------------------------------------------------- */
1facf9fc 22253+
4a4d8108 22254+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22255+{
4a4d8108 22256+ int err;
5afbbe0d 22257+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22258+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22259+ struct dentry *parent, *wh_dentry;
c2b27bf2
AM
22260+ /* to reuduce stack size */
22261+ struct {
22262+ struct au_dtime dt;
22263+ struct au_pin pin;
22264+ struct path h_path;
22265+ } *a;
1facf9fc 22266+
4a4d8108 22267+ IMustLock(dir);
027c5e7a 22268+
c2b27bf2
AM
22269+ err = -ENOMEM;
22270+ a = kmalloc(sizeof(*a), GFP_NOFS);
22271+ if (unlikely(!a))
22272+ goto out;
22273+
027c5e7a
AM
22274+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22275+ if (unlikely(err))
c2b27bf2 22276+ goto out_free;
027c5e7a
AM
22277+ err = au_d_hashed_positive(dentry);
22278+ if (unlikely(err))
22279+ goto out_unlock;
5527c038 22280+ inode = d_inode(dentry);
4a4d8108 22281+ IMustLock(inode);
027c5e7a 22282+ err = -EISDIR;
2000de60 22283+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22284+ goto out_unlock; /* possible? */
1facf9fc 22285+
5afbbe0d 22286+ btop = au_dbtop(dentry);
4a4d8108
AM
22287+ bwh = au_dbwh(dentry);
22288+ bindex = -1;
027c5e7a
AM
22289+ parent = dentry->d_parent; /* dir inode is locked */
22290+ di_write_lock_parent(parent);
c2b27bf2
AM
22291+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22292+ &a->pin);
4a4d8108
AM
22293+ err = PTR_ERR(wh_dentry);
22294+ if (IS_ERR(wh_dentry))
027c5e7a 22295+ goto out_parent;
1facf9fc 22296+
5afbbe0d
AM
22297+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22298+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22299+ dget(a->h_path.dentry);
5afbbe0d 22300+ if (bindex == btop) {
c2b27bf2 22301+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22302+ delegated = NULL;
22303+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22304+ if (unlikely(err == -EWOULDBLOCK)) {
22305+ pr_warn("cannot retry for NFSv4 delegation"
22306+ " for an internal unlink\n");
22307+ iput(delegated);
22308+ }
4a4d8108
AM
22309+ } else {
22310+ /* dir inode is locked */
5527c038 22311+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22312+ IMustLock(h_dir);
22313+ err = 0;
22314+ }
dece6358 22315+
4a4d8108 22316+ if (!err) {
7f207e10 22317+ vfsub_drop_nlink(inode);
4a4d8108
AM
22318+ epilog(dir, dentry, bindex);
22319+
22320+ /* update target timestamps */
5afbbe0d 22321+ if (bindex == btop) {
c2b27bf2
AM
22322+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22323+ /*ignore*/
5527c038 22324+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22325+ } else
22326+ /* todo: this timestamp may be reverted later */
22327+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22328+ goto out_unpin; /* success */
1facf9fc 22329+ }
22330+
4a4d8108
AM
22331+ /* revert */
22332+ if (wh_dentry) {
22333+ int rerr;
22334+
c2b27bf2
AM
22335+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22336+ &a->dt);
4a4d8108
AM
22337+ if (rerr)
22338+ err = rerr;
dece6358 22339+ }
1facf9fc 22340+
027c5e7a 22341+out_unpin:
c2b27bf2 22342+ au_unpin(&a->pin);
4a4d8108 22343+ dput(wh_dentry);
c2b27bf2 22344+ dput(a->h_path.dentry);
027c5e7a 22345+out_parent:
4a4d8108 22346+ di_write_unlock(parent);
027c5e7a 22347+out_unlock:
4a4d8108 22348+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22349+out_free:
1c60b727 22350+ kfree(a);
027c5e7a 22351+out:
4a4d8108 22352+ return err;
dece6358
AM
22353+}
22354+
4a4d8108 22355+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22356+{
4a4d8108 22357+ int err, rmdir_later;
5afbbe0d 22358+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22359+ struct inode *inode;
22360+ struct dentry *parent, *wh_dentry, *h_dentry;
22361+ struct au_whtmp_rmdir *args;
c2b27bf2
AM
22362+ /* to reuduce stack size */
22363+ struct {
22364+ struct au_dtime dt;
22365+ struct au_pin pin;
22366+ } *a;
1facf9fc 22367+
4a4d8108 22368+ IMustLock(dir);
027c5e7a 22369+
c2b27bf2
AM
22370+ err = -ENOMEM;
22371+ a = kmalloc(sizeof(*a), GFP_NOFS);
22372+ if (unlikely(!a))
22373+ goto out;
22374+
027c5e7a
AM
22375+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22376+ if (unlikely(err))
c2b27bf2 22377+ goto out_free;
53392da6
AM
22378+ err = au_alive_dir(dentry);
22379+ if (unlikely(err))
027c5e7a 22380+ goto out_unlock;
5527c038 22381+ inode = d_inode(dentry);
4a4d8108 22382+ IMustLock(inode);
027c5e7a 22383+ err = -ENOTDIR;
2000de60 22384+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22385+ goto out_unlock; /* possible? */
dece6358 22386+
4a4d8108
AM
22387+ err = -ENOMEM;
22388+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22389+ if (unlikely(!args))
22390+ goto out_unlock;
dece6358 22391+
4a4d8108
AM
22392+ parent = dentry->d_parent; /* dir inode is locked */
22393+ di_write_lock_parent(parent);
22394+ err = au_test_empty(dentry, &args->whlist);
22395+ if (unlikely(err))
027c5e7a 22396+ goto out_parent;
1facf9fc 22397+
5afbbe0d 22398+ btop = au_dbtop(dentry);
4a4d8108
AM
22399+ bwh = au_dbwh(dentry);
22400+ bindex = -1;
c2b27bf2
AM
22401+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22402+ &a->pin);
4a4d8108
AM
22403+ err = PTR_ERR(wh_dentry);
22404+ if (IS_ERR(wh_dentry))
027c5e7a 22405+ goto out_parent;
1facf9fc 22406+
5afbbe0d 22407+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22408+ dget(h_dentry);
22409+ rmdir_later = 0;
5afbbe0d
AM
22410+ if (bindex == btop) {
22411+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22412+ if (err > 0) {
22413+ rmdir_later = err;
22414+ err = 0;
22415+ }
22416+ } else {
22417+ /* stop monitoring */
5afbbe0d 22418+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22419+
22420+ /* dir inode is locked */
5527c038 22421+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22422+ err = 0;
22423+ }
22424+
4a4d8108 22425+ if (!err) {
027c5e7a 22426+ vfsub_dead_dir(inode);
4a4d8108
AM
22427+ au_set_dbdiropq(dentry, -1);
22428+ epilog(dir, dentry, bindex);
1308ab2a 22429+
4a4d8108 22430+ if (rmdir_later) {
5afbbe0d 22431+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22432+ args = NULL;
22433+ }
1308ab2a 22434+
4a4d8108 22435+ goto out_unpin; /* success */
1facf9fc 22436+ }
22437+
4a4d8108
AM
22438+ /* revert */
22439+ AuLabel(revert);
22440+ if (wh_dentry) {
22441+ int rerr;
1308ab2a 22442+
c2b27bf2
AM
22443+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22444+ &a->dt);
4a4d8108
AM
22445+ if (rerr)
22446+ err = rerr;
1facf9fc 22447+ }
22448+
4f0767ce 22449+out_unpin:
c2b27bf2 22450+ au_unpin(&a->pin);
4a4d8108
AM
22451+ dput(wh_dentry);
22452+ dput(h_dentry);
027c5e7a 22453+out_parent:
4a4d8108
AM
22454+ di_write_unlock(parent);
22455+ if (args)
22456+ au_whtmp_rmdir_free(args);
4f0767ce 22457+out_unlock:
4a4d8108 22458+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22459+out_free:
1c60b727 22460+ kfree(a);
4f0767ce 22461+out:
4a4d8108
AM
22462+ AuTraceErr(err);
22463+ return err;
dece6358 22464+}
7f207e10
AM
22465diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22466--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 22467+++ linux/fs/aufs/i_op_ren.c 2018-06-04 09:08:09.184746078 +0200
8b6a4947 22468@@ -0,0 +1,1246 @@
1facf9fc 22469+/*
b00004a5 22470+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 22471+ *
22472+ * This program, aufs is free software; you can redistribute it and/or modify
22473+ * it under the terms of the GNU General Public License as published by
22474+ * the Free Software Foundation; either version 2 of the License, or
22475+ * (at your option) any later version.
dece6358
AM
22476+ *
22477+ * This program is distributed in the hope that it will be useful,
22478+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22479+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22480+ * GNU General Public License for more details.
22481+ *
22482+ * You should have received a copy of the GNU General Public License
523b37e3 22483+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22484+ */
22485+
22486+/*
4a4d8108
AM
22487+ * inode operation (rename entry)
22488+ * todo: this is crazy monster
1facf9fc 22489+ */
22490+
22491+#include "aufs.h"
22492+
4a4d8108
AM
22493+enum { AuSRC, AuDST, AuSrcDst };
22494+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22495+
f2c43d5f
AM
22496+#define AuRen_ISDIR_SRC 1
22497+#define AuRen_ISDIR_DST (1 << 1)
22498+#define AuRen_ISSAMEDIR (1 << 2)
22499+#define AuRen_WHSRC (1 << 3)
22500+#define AuRen_WHDST (1 << 4)
22501+#define AuRen_MNT_WRITE (1 << 5)
22502+#define AuRen_DT_DSTDIR (1 << 6)
22503+#define AuRen_DIROPQ_SRC (1 << 7)
22504+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22505+#define AuRen_DIRREN (1 << 9)
22506+#define AuRen_DROPPED_SRC (1 << 10)
22507+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22508+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22509+#define au_fset_ren(flags, name) \
22510+ do { (flags) |= AuRen_##name; } while (0)
22511+#define au_fclr_ren(flags, name) \
22512+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22513+
8b6a4947
AM
22514+#ifndef CONFIG_AUFS_DIRREN
22515+#undef AuRen_DIRREN
22516+#define AuRen_DIRREN 0
22517+#endif
22518+
4a4d8108
AM
22519+struct au_ren_args {
22520+ struct {
22521+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22522+ *wh_dentry;
22523+ struct inode *dir, *inode;
f2c43d5f 22524+ struct au_hinode *hdir, *hinode;
4a4d8108 22525+ struct au_dtime dt[AuParentChild];
f2c43d5f 22526+ aufs_bindex_t btop, bdiropq;
4a4d8108 22527+ } sd[AuSrcDst];
1facf9fc 22528+
4a4d8108
AM
22529+#define src_dentry sd[AuSRC].dentry
22530+#define src_dir sd[AuSRC].dir
22531+#define src_inode sd[AuSRC].inode
22532+#define src_h_dentry sd[AuSRC].h_dentry
22533+#define src_parent sd[AuSRC].parent
22534+#define src_h_parent sd[AuSRC].h_parent
22535+#define src_wh_dentry sd[AuSRC].wh_dentry
22536+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22537+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22538+#define src_h_dir sd[AuSRC].hdir->hi_inode
22539+#define src_dt sd[AuSRC].dt
5afbbe0d 22540+#define src_btop sd[AuSRC].btop
f2c43d5f 22541+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22542+
4a4d8108
AM
22543+#define dst_dentry sd[AuDST].dentry
22544+#define dst_dir sd[AuDST].dir
22545+#define dst_inode sd[AuDST].inode
22546+#define dst_h_dentry sd[AuDST].h_dentry
22547+#define dst_parent sd[AuDST].parent
22548+#define dst_h_parent sd[AuDST].h_parent
22549+#define dst_wh_dentry sd[AuDST].wh_dentry
22550+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22551+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22552+#define dst_h_dir sd[AuDST].hdir->hi_inode
22553+#define dst_dt sd[AuDST].dt
5afbbe0d 22554+#define dst_btop sd[AuDST].btop
f2c43d5f 22555+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22556+
22557+ struct dentry *h_trap;
22558+ struct au_branch *br;
4a4d8108
AM
22559+ struct path h_path;
22560+ struct au_nhash whlist;
f2c43d5f 22561+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22562+
f2c43d5f
AM
22563+ struct {
22564+ unsigned short auren_flags;
22565+ unsigned char flags; /* syscall parameter */
22566+ unsigned char exchange;
22567+ } __packed;
1facf9fc 22568+
4a4d8108
AM
22569+ struct au_whtmp_rmdir *thargs;
22570+ struct dentry *h_dst;
8b6a4947 22571+ struct au_hinode *h_root;
4a4d8108 22572+};
1308ab2a 22573+
4a4d8108 22574+/* ---------------------------------------------------------------------- */
1308ab2a 22575+
4a4d8108
AM
22576+/*
22577+ * functions for reverting.
22578+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22579+ * everything as if nothing happened.
4a4d8108
AM
22580+ * we don't need to revert the copied-up/down the parent dir since they are
22581+ * harmless.
22582+ */
1facf9fc 22583+
4a4d8108
AM
22584+#define RevertFailure(fmt, ...) do { \
22585+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22586+ ##__VA_ARGS__, err, rerr); \
22587+ err = -EIO; \
22588+} while (0)
1facf9fc 22589+
f2c43d5f 22590+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22591+{
4a4d8108 22592+ int rerr;
f2c43d5f
AM
22593+ struct dentry *d;
22594+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22595+
f2c43d5f
AM
22596+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22597+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22598+ rerr = au_diropq_remove(d, a->btgt);
22599+ au_hn_inode_unlock(src_or_dst(hinode));
22600+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22601+ if (rerr)
f2c43d5f
AM
22602+ RevertFailure("remove diropq %pd", d);
22603+
22604+#undef src_or_dst_
22605+}
22606+
22607+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22608+{
22609+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22610+ au_ren_do_rev_diropq(err, a, AuSRC);
22611+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22612+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22613+}
1facf9fc 22614+
4a4d8108
AM
22615+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22616+{
22617+ int rerr;
523b37e3 22618+ struct inode *delegated;
1facf9fc 22619+
b4510431
AM
22620+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22621+ a->src_h_parent);
4a4d8108
AM
22622+ rerr = PTR_ERR(a->h_path.dentry);
22623+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22624+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22625+ return;
1facf9fc 22626+ }
22627+
523b37e3 22628+ delegated = NULL;
4a4d8108
AM
22629+ rerr = vfsub_rename(a->dst_h_dir,
22630+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22631+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22632+ if (unlikely(rerr == -EWOULDBLOCK)) {
22633+ pr_warn("cannot retry for NFSv4 delegation"
22634+ " for an internal rename\n");
22635+ iput(delegated);
22636+ }
4a4d8108
AM
22637+ d_drop(a->h_path.dentry);
22638+ dput(a->h_path.dentry);
22639+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22640+ if (rerr)
523b37e3 22641+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22642+}
22643+
4a4d8108 22644+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22645+{
4a4d8108 22646+ int rerr;
523b37e3 22647+ struct inode *delegated;
dece6358 22648+
b4510431
AM
22649+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22650+ a->dst_h_parent);
4a4d8108
AM
22651+ rerr = PTR_ERR(a->h_path.dentry);
22652+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22653+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22654+ return;
22655+ }
5527c038 22656+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22657+ d_drop(a->h_path.dentry);
22658+ dput(a->h_path.dentry);
22659+ return;
dece6358
AM
22660+ }
22661+
523b37e3
AM
22662+ delegated = NULL;
22663+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22664+ &delegated, a->flags);
523b37e3
AM
22665+ if (unlikely(rerr == -EWOULDBLOCK)) {
22666+ pr_warn("cannot retry for NFSv4 delegation"
22667+ " for an internal rename\n");
22668+ iput(delegated);
22669+ }
4a4d8108
AM
22670+ d_drop(a->h_path.dentry);
22671+ dput(a->h_path.dentry);
22672+ if (!rerr)
22673+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22674+ else
523b37e3 22675+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22676+}
1308ab2a 22677+
4a4d8108
AM
22678+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22679+{
22680+ int rerr;
1308ab2a 22681+
4a4d8108
AM
22682+ a->h_path.dentry = a->src_wh_dentry;
22683+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22684+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22685+ if (rerr)
523b37e3 22686+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22687+}
4a4d8108 22688+#undef RevertFailure
1facf9fc 22689+
1308ab2a 22690+/* ---------------------------------------------------------------------- */
22691+
4a4d8108
AM
22692+/*
22693+ * when we have to copyup the renaming entry, do it with the rename-target name
22694+ * in order to minimize the cost (the later actual rename is unnecessary).
22695+ * otherwise rename it on the target branch.
22696+ */
22697+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22698+{
dece6358 22699+ int err;
4a4d8108 22700+ struct dentry *d;
523b37e3 22701+ struct inode *delegated;
1facf9fc 22702+
4a4d8108 22703+ d = a->src_dentry;
5afbbe0d 22704+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22705+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22706+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22707+ delegated = NULL;
4a4d8108 22708+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22709+ a->dst_h_dir, &a->h_path, &delegated,
22710+ a->flags);
523b37e3
AM
22711+ if (unlikely(err == -EWOULDBLOCK)) {
22712+ pr_warn("cannot retry for NFSv4 delegation"
22713+ " for an internal rename\n");
22714+ iput(delegated);
22715+ }
c2b27bf2 22716+ } else
86dc4139 22717+ BUG();
1308ab2a 22718+
027c5e7a
AM
22719+ if (!err && a->h_dst)
22720+ /* it will be set to dinfo later */
22721+ dget(a->h_dst);
1facf9fc 22722+
dece6358
AM
22723+ return err;
22724+}
1facf9fc 22725+
4a4d8108
AM
22726+/* cf. aufs_rmdir() */
22727+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22728+{
4a4d8108
AM
22729+ int err;
22730+ struct inode *dir;
1facf9fc 22731+
4a4d8108
AM
22732+ dir = a->dst_dir;
22733+ SiMustAnyLock(dir->i_sb);
22734+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22735+ au_sbi(dir->i_sb)->si_dirwh)
22736+ || au_test_fs_remote(a->h_dst->d_sb)) {
22737+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22738+ if (unlikely(err))
523b37e3
AM
22739+ pr_warn("failed removing whtmp dir %pd (%d), "
22740+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22741+ } else {
22742+ au_nhash_wh_free(&a->thargs->whlist);
22743+ a->thargs->whlist = a->whlist;
22744+ a->whlist.nh_num = 0;
22745+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22746+ dput(a->h_dst);
22747+ a->thargs = NULL;
22748+ }
22749+
22750+ return 0;
1308ab2a 22751+}
1facf9fc 22752+
4a4d8108 22753+/* make it 'opaque' dir. */
f2c43d5f 22754+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22755+{
22756+ int err;
f2c43d5f
AM
22757+ struct dentry *d, *diropq;
22758+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22759+
4a4d8108 22760+ err = 0;
f2c43d5f
AM
22761+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22762+ src_or_dst(bdiropq) = au_dbdiropq(d);
22763+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22764+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22765+ diropq = au_diropq_create(d, a->btgt);
22766+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22767+ if (IS_ERR(diropq))
22768+ err = PTR_ERR(diropq);
076b876e
AM
22769+ else
22770+ dput(diropq);
1facf9fc 22771+
f2c43d5f 22772+#undef src_or_dst_
4a4d8108
AM
22773+ return err;
22774+}
1facf9fc 22775+
f2c43d5f 22776+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
22777+{
22778+ int err;
f2c43d5f
AM
22779+ unsigned char always;
22780+ struct dentry *d;
1facf9fc 22781+
f2c43d5f
AM
22782+ err = 0;
22783+ d = a->dst_dentry; /* already renamed on the branch */
22784+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22785+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 22786+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
22787+ && a->btgt != au_dbdiropq(a->src_dentry)
22788+ && (a->dst_wh_dentry
22789+ || a->btgt <= au_dbdiropq(d)
22790+ /* hide the lower to keep xino */
22791+ /* the lowers may not be a dir, but we hide them anyway */
22792+ || a->btgt < au_dbbot(d)
22793+ || always)) {
22794+ AuDbg("here\n");
22795+ err = au_ren_do_diropq(a, AuSRC);
22796+ if (unlikely(err))
4a4d8108 22797+ goto out;
f2c43d5f 22798+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 22799+ }
f2c43d5f
AM
22800+ if (!a->exchange)
22801+ goto out; /* success */
1facf9fc 22802+
f2c43d5f
AM
22803+ d = a->src_dentry; /* already renamed on the branch */
22804+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22805+ && a->btgt != au_dbdiropq(a->dst_dentry)
22806+ && (a->btgt < au_dbdiropq(d)
22807+ || a->btgt < au_dbbot(d)
22808+ || always)) {
22809+ AuDbgDentry(a->src_dentry);
22810+ AuDbgDentry(a->dst_dentry);
22811+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 22812+ if (unlikely(err))
f2c43d5f
AM
22813+ goto out_rev_src;
22814+ au_fset_ren(a->auren_flags, DIROPQ_DST);
22815+ }
22816+ goto out; /* success */
dece6358 22817+
f2c43d5f
AM
22818+out_rev_src:
22819+ AuDbg("err %d, reverting src\n", err);
22820+ au_ren_rev_diropq(err, a);
22821+out:
22822+ return err;
22823+}
22824+
22825+static int do_rename(struct au_ren_args *a)
22826+{
22827+ int err;
22828+ struct dentry *d, *h_d;
22829+
22830+ if (!a->exchange) {
22831+ /* prepare workqueue args for asynchronous rmdir */
22832+ h_d = a->dst_h_dentry;
22833+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 22834+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
22835+ && d_is_positive(h_d)) {
22836+ err = -ENOMEM;
22837+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22838+ GFP_NOFS);
22839+ if (unlikely(!a->thargs))
22840+ goto out;
22841+ a->h_dst = dget(h_d);
22842+ }
22843+
22844+ /* create whiteout for src_dentry */
22845+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
22846+ a->src_bwh = au_dbwh(a->src_dentry);
22847+ AuDebugOn(a->src_bwh >= 0);
22848+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22849+ a->src_h_parent);
22850+ err = PTR_ERR(a->src_wh_dentry);
22851+ if (IS_ERR(a->src_wh_dentry))
22852+ goto out_thargs;
22853+ }
22854+
22855+ /* lookup whiteout for dentry */
22856+ if (au_ftest_ren(a->auren_flags, WHDST)) {
22857+ h_d = au_wh_lkup(a->dst_h_parent,
22858+ &a->dst_dentry->d_name, a->br);
22859+ err = PTR_ERR(h_d);
22860+ if (IS_ERR(h_d))
22861+ goto out_whsrc;
22862+ if (d_is_negative(h_d))
22863+ dput(h_d);
22864+ else
22865+ a->dst_wh_dentry = h_d;
22866+ }
22867+
22868+ /* rename dentry to tmpwh */
22869+ if (a->thargs) {
22870+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
22871+ if (unlikely(err))
22872+ goto out_whdst;
22873+
22874+ d = a->dst_dentry;
22875+ au_set_h_dptr(d, a->btgt, NULL);
22876+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
22877+ if (unlikely(err))
22878+ goto out_whtmp;
22879+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
22880+ }
4a4d8108 22881+ }
1facf9fc 22882+
5afbbe0d 22883+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
8b6a4947
AM
22884+#if 0
22885+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22886+ && d_is_positive(a->dst_h_dentry)
22887+ && a->src_btop != a->btgt);
22888+#endif
1facf9fc 22889+
4a4d8108 22890+ /* rename by vfs_rename or cpup */
4a4d8108
AM
22891+ err = au_ren_or_cpup(a);
22892+ if (unlikely(err))
22893+ /* leave the copied-up one */
22894+ goto out_whtmp;
1308ab2a 22895+
4a4d8108 22896+ /* make dir opaque */
f2c43d5f
AM
22897+ err = au_ren_diropq(a);
22898+ if (unlikely(err))
22899+ goto out_rename;
1308ab2a 22900+
4a4d8108 22901+ /* update target timestamps */
f2c43d5f
AM
22902+ if (a->exchange) {
22903+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22904+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22905+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22906+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22907+ }
5afbbe0d 22908+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
22909+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22910+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 22911+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 22912+
f2c43d5f
AM
22913+ if (!a->exchange) {
22914+ /* remove whiteout for dentry */
22915+ if (a->dst_wh_dentry) {
22916+ a->h_path.dentry = a->dst_wh_dentry;
22917+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22918+ a->dst_dentry);
22919+ if (unlikely(err))
22920+ goto out_diropq;
22921+ }
1facf9fc 22922+
f2c43d5f
AM
22923+ /* remove whtmp */
22924+ if (a->thargs)
22925+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 22926+
f2c43d5f
AM
22927+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22928+ }
4a4d8108
AM
22929+ err = 0;
22930+ goto out_success;
22931+
4f0767ce 22932+out_diropq:
f2c43d5f 22933+ au_ren_rev_diropq(err, a);
4f0767ce 22934+out_rename:
7e9cd9fe 22935+ au_ren_rev_rename(err, a);
027c5e7a 22936+ dput(a->h_dst);
4f0767ce 22937+out_whtmp:
4a4d8108
AM
22938+ if (a->thargs)
22939+ au_ren_rev_whtmp(err, a);
4f0767ce 22940+out_whdst:
4a4d8108
AM
22941+ dput(a->dst_wh_dentry);
22942+ a->dst_wh_dentry = NULL;
4f0767ce 22943+out_whsrc:
4a4d8108
AM
22944+ if (a->src_wh_dentry)
22945+ au_ren_rev_whsrc(err, a);
4f0767ce 22946+out_success:
4a4d8108
AM
22947+ dput(a->src_wh_dentry);
22948+ dput(a->dst_wh_dentry);
4f0767ce 22949+out_thargs:
4a4d8108
AM
22950+ if (a->thargs) {
22951+ dput(a->h_dst);
22952+ au_whtmp_rmdir_free(a->thargs);
22953+ a->thargs = NULL;
22954+ }
4f0767ce 22955+out:
4a4d8108 22956+ return err;
dece6358 22957+}
1facf9fc 22958+
1308ab2a 22959+/* ---------------------------------------------------------------------- */
1facf9fc 22960+
4a4d8108
AM
22961+/*
22962+ * test if @dentry dir can be rename destination or not.
22963+ * success means, it is a logically empty dir.
22964+ */
22965+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 22966+{
4a4d8108 22967+ return au_test_empty(dentry, whlist);
1308ab2a 22968+}
1facf9fc 22969+
4a4d8108 22970+/*
8b6a4947
AM
22971+ * test if @a->src_dentry dir can be rename source or not.
22972+ * if it can, return 0.
4a4d8108
AM
22973+ * success means,
22974+ * - it is a logically empty dir.
22975+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 22976+ * on the lower branch unless DIRREN is on.
4a4d8108 22977+ */
8b6a4947 22978+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
22979+{
22980+ int err;
22981+ unsigned int rdhash;
8b6a4947
AM
22982+ aufs_bindex_t btop, btgt;
22983+ struct dentry *dentry;
22984+ struct super_block *sb;
22985+ struct au_sbinfo *sbinfo;
1facf9fc 22986+
8b6a4947
AM
22987+ dentry = a->src_dentry;
22988+ sb = dentry->d_sb;
22989+ sbinfo = au_sbi(sb);
22990+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
22991+ au_fset_ren(a->auren_flags, DIRREN);
22992+
22993+ btgt = a->btgt;
5afbbe0d
AM
22994+ btop = au_dbtop(dentry);
22995+ if (btop != btgt) {
4a4d8108 22996+ struct au_nhash whlist;
dece6358 22997+
8b6a4947
AM
22998+ SiMustAnyLock(sb);
22999+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23000+ if (!rdhash)
23001+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23002+ dentry));
23003+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23004+ if (unlikely(err))
23005+ goto out;
23006+ err = au_test_empty(dentry, &whlist);
23007+ au_nhash_wh_free(&whlist);
23008+ goto out;
23009+ }
dece6358 23010+
5afbbe0d 23011+ if (btop == au_dbtaildir(dentry))
4a4d8108 23012+ return 0; /* success */
dece6358 23013+
4a4d8108 23014+ err = au_test_empty_lower(dentry);
1facf9fc 23015+
4f0767ce 23016+out:
4a4d8108 23017+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23018+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23019+ err = 0;
23020+ } else {
23021+ AuWarn1("renaming dir who has child(ren) on multiple "
23022+ "branches, is not supported\n");
23023+ err = -EXDEV;
23024+ }
4a4d8108
AM
23025+ }
23026+ return err;
23027+}
1308ab2a 23028+
4a4d8108
AM
23029+/* side effect: sets whlist and h_dentry */
23030+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23031+{
4a4d8108
AM
23032+ int err;
23033+ unsigned int rdhash;
23034+ struct dentry *d;
1facf9fc 23035+
4a4d8108
AM
23036+ d = a->dst_dentry;
23037+ SiMustAnyLock(d->d_sb);
1facf9fc 23038+
4a4d8108 23039+ err = 0;
f2c43d5f 23040+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23041+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23042+ if (!rdhash)
23043+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23044+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23045+ if (unlikely(err))
23046+ goto out;
1308ab2a 23047+
f2c43d5f
AM
23048+ if (!a->exchange) {
23049+ au_set_dbtop(d, a->dst_btop);
23050+ err = may_rename_dstdir(d, &a->whlist);
23051+ au_set_dbtop(d, a->btgt);
23052+ } else
8b6a4947 23053+ err = may_rename_srcdir(a);
4a4d8108 23054+ }
5afbbe0d 23055+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23056+ if (unlikely(err))
23057+ goto out;
23058+
23059+ d = a->src_dentry;
5afbbe0d 23060+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23061+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23062+ err = may_rename_srcdir(a);
4a4d8108
AM
23063+ if (unlikely(err)) {
23064+ au_nhash_wh_free(&a->whlist);
23065+ a->whlist.nh_num = 0;
23066+ }
23067+ }
4f0767ce 23068+out:
4a4d8108 23069+ return err;
1facf9fc 23070+}
23071+
4a4d8108 23072+/* ---------------------------------------------------------------------- */
1facf9fc 23073+
4a4d8108
AM
23074+/*
23075+ * simple tests for rename.
23076+ * following the checks in vfs, plus the parent-child relationship.
23077+ */
23078+static int au_may_ren(struct au_ren_args *a)
23079+{
23080+ int err, isdir;
23081+ struct inode *h_inode;
1facf9fc 23082+
5afbbe0d 23083+ if (a->src_btop == a->btgt) {
4a4d8108 23084+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23085+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23086+ if (unlikely(err))
23087+ goto out;
23088+ err = -EINVAL;
23089+ if (unlikely(a->src_h_dentry == a->h_trap))
23090+ goto out;
23091+ }
1facf9fc 23092+
4a4d8108 23093+ err = 0;
5afbbe0d 23094+ if (a->dst_btop != a->btgt)
4a4d8108 23095+ goto out;
1facf9fc 23096+
027c5e7a
AM
23097+ err = -ENOTEMPTY;
23098+ if (unlikely(a->dst_h_dentry == a->h_trap))
23099+ goto out;
23100+
4a4d8108 23101+ err = -EIO;
f2c43d5f 23102+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23103+ if (d_really_is_negative(a->dst_dentry)) {
23104+ if (d_is_negative(a->dst_h_dentry))
23105+ err = au_may_add(a->dst_dentry, a->btgt,
23106+ a->dst_h_parent, isdir);
4a4d8108 23107+ } else {
5527c038 23108+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23109+ goto out;
5527c038
JR
23110+ h_inode = d_inode(a->dst_h_dentry);
23111+ if (h_inode->i_nlink)
23112+ err = au_may_del(a->dst_dentry, a->btgt,
23113+ a->dst_h_parent, isdir);
4a4d8108 23114+ }
1facf9fc 23115+
4f0767ce 23116+out:
4a4d8108
AM
23117+ if (unlikely(err == -ENOENT || err == -EEXIST))
23118+ err = -EIO;
23119+ AuTraceErr(err);
23120+ return err;
23121+}
1facf9fc 23122+
1308ab2a 23123+/* ---------------------------------------------------------------------- */
1facf9fc 23124+
4a4d8108
AM
23125+/*
23126+ * locking order
23127+ * (VFS)
23128+ * - src_dir and dir by lock_rename()
23129+ * - inode if exitsts
23130+ * (aufs)
23131+ * - lock all
23132+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23133+ * + si_read_lock
23134+ * + di_write_lock2_child()
23135+ * + di_write_lock_child()
23136+ * + ii_write_lock_child()
23137+ * + di_write_lock_child2()
23138+ * + ii_write_lock_child2()
23139+ * + src_parent and parent
23140+ * + di_write_lock_parent()
23141+ * + ii_write_lock_parent()
23142+ * + di_write_lock_parent2()
23143+ * + ii_write_lock_parent2()
23144+ * + lower src_dir and dir by vfsub_lock_rename()
23145+ * + verify the every relationships between child and parent. if any
23146+ * of them failed, unlock all and return -EBUSY.
23147+ */
23148+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23149+{
4a4d8108
AM
23150+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23151+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23152+ if (au_ftest_ren(a->auren_flags, DIRREN)
23153+ && a->h_root)
23154+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23155+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23156+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23157+}
23158+
4a4d8108 23159+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23160+{
4a4d8108
AM
23161+ int err;
23162+ unsigned int udba;
1308ab2a 23163+
4a4d8108
AM
23164+ err = 0;
23165+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23166+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23167+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23168+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23169+
23170+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23171+ if (unlikely(err))
23172+ goto out;
f2c43d5f 23173+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23174+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23175+ struct dentry *root;
23176+ struct inode *dir;
23177+
23178+ /*
23179+ * sbinfo is already locked, so this ii_read_lock is
23180+ * unnecessary. but our debugging feature checks it.
23181+ */
23182+ root = a->src_inode->i_sb->s_root;
23183+ if (root != a->src_parent && root != a->dst_parent) {
23184+ dir = d_inode(root);
23185+ ii_read_lock_parent3(dir);
23186+ a->h_root = au_hi(dir, a->btgt);
23187+ ii_read_unlock(dir);
23188+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23189+ }
23190+ }
4a4d8108
AM
23191+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23192+ a->dst_h_parent, a->dst_hdir);
23193+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23194+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23195+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23196+ err = au_busy_or_stale();
5afbbe0d 23197+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23198+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23199+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23200+ a->br);
5afbbe0d 23201+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23202+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23203+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23204+ a->br);
86dc4139 23205+ if (!err)
4a4d8108 23206+ goto out; /* success */
4a4d8108
AM
23207+
23208+ err = au_busy_or_stale();
4a4d8108 23209+ au_ren_unlock(a);
86dc4139 23210+
4f0767ce 23211+out:
4a4d8108 23212+ return err;
1facf9fc 23213+}
23214+
23215+/* ---------------------------------------------------------------------- */
23216+
4a4d8108 23217+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23218+{
4a4d8108 23219+ struct inode *dir;
dece6358 23220+
4a4d8108 23221+ dir = a->dst_dir;
be118d29 23222+ inode_inc_iversion(dir);
f2c43d5f 23223+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23224+ /* is this updating defined in POSIX? */
23225+ au_cpup_attr_timesizes(a->src_inode);
23226+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23227+ }
b912730e 23228+ au_dir_ts(dir, a->btgt);
dece6358 23229+
f2c43d5f
AM
23230+ if (a->exchange) {
23231+ dir = a->src_dir;
be118d29 23232+ inode_inc_iversion(dir);
f2c43d5f
AM
23233+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23234+ /* is this updating defined in POSIX? */
23235+ au_cpup_attr_timesizes(a->dst_inode);
23236+ au_cpup_attr_nlink(dir, /*force*/1);
23237+ }
23238+ au_dir_ts(dir, a->btgt);
23239+ }
23240+
23241+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23242+ return;
dece6358 23243+
4a4d8108 23244+ dir = a->src_dir;
be118d29 23245+ inode_inc_iversion(dir);
f2c43d5f 23246+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23247+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23248+ au_dir_ts(dir, a->btgt);
1facf9fc 23249+}
23250+
4a4d8108 23251+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23252+{
5afbbe0d 23253+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23254+ struct dentry *d, *h_d;
23255+ struct inode *i, *h_i;
23256+ struct super_block *sb;
dece6358 23257+
027c5e7a
AM
23258+ d = a->dst_dentry;
23259+ d_drop(d);
23260+ if (a->h_dst)
23261+ /* already dget-ed by au_ren_or_cpup() */
23262+ au_set_h_dptr(d, a->btgt, a->h_dst);
23263+
23264+ i = a->dst_inode;
23265+ if (i) {
f2c43d5f
AM
23266+ if (!a->exchange) {
23267+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23268+ vfsub_drop_nlink(i);
23269+ else {
23270+ vfsub_dead_dir(i);
23271+ au_cpup_attr_timesizes(i);
23272+ }
23273+ au_update_dbrange(d, /*do_put_zero*/1);
23274+ } else
23275+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23276+ } else {
5afbbe0d
AM
23277+ bbot = a->btgt;
23278+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23279+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23280+ bbot = au_dbbot(d);
23281+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23282+ au_set_h_dptr(d, bindex, NULL);
23283+ au_update_dbrange(d, /*do_put_zero*/0);
23284+ }
23285+
8b6a4947
AM
23286+ if (a->exchange
23287+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23288+ d_drop(a->src_dentry);
23289+ if (au_ftest_ren(a->auren_flags, DIRREN))
23290+ au_set_dbwh(a->src_dentry, -1);
23291+ return;
23292+ }
23293+
4a4d8108 23294+ d = a->src_dentry;
8b6a4947
AM
23295+ au_set_dbwh(d, -1);
23296+ bbot = au_dbbot(d);
23297+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23298+ h_d = au_h_dptr(d, bindex);
23299+ if (h_d)
23300+ au_set_h_dptr(d, bindex, NULL);
23301+ }
23302+ au_set_dbbot(d, a->btgt);
4a4d8108 23303+
8b6a4947
AM
23304+ sb = d->d_sb;
23305+ i = a->src_inode;
23306+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23307+ return; /* success */
4a4d8108 23308+
8b6a4947
AM
23309+ bbot = au_ibbot(i);
23310+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23311+ h_i = au_h_iptr(i, bindex);
23312+ if (h_i) {
23313+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23314+ /* ignore this error */
23315+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23316+ }
23317+ }
8b6a4947 23318+ au_set_ibbot(i, a->btgt);
1308ab2a 23319+}
dece6358 23320+
4a4d8108
AM
23321+/* ---------------------------------------------------------------------- */
23322+
23323+/* mainly for link(2) and rename(2) */
23324+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23325+{
4a4d8108
AM
23326+ aufs_bindex_t bdiropq, bwh;
23327+ struct dentry *parent;
23328+ struct au_branch *br;
23329+
23330+ parent = dentry->d_parent;
5527c038 23331+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23332+
23333+ bdiropq = au_dbdiropq(parent);
23334+ bwh = au_dbwh(dentry);
23335+ br = au_sbr(dentry->d_sb, btgt);
23336+ if (au_br_rdonly(br)
23337+ || (0 <= bdiropq && bdiropq < btgt)
23338+ || (0 <= bwh && bwh < btgt))
23339+ btgt = -1;
23340+
23341+ AuDbg("btgt %d\n", btgt);
23342+ return btgt;
1facf9fc 23343+}
23344+
5afbbe0d 23345+/* sets src_btop, dst_btop and btgt */
4a4d8108 23346+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23347+{
4a4d8108
AM
23348+ int err;
23349+ struct au_wr_dir_args wr_dir_args = {
23350+ /* .force_btgt = -1, */
23351+ .flags = AuWrDir_ADD_ENTRY
23352+ };
dece6358 23353+
5afbbe0d
AM
23354+ a->src_btop = au_dbtop(a->src_dentry);
23355+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23356+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23357+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23358+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23359+ wr_dir_args.force_btgt = a->src_btop;
23360+ if (a->dst_inode && a->dst_btop < a->src_btop)
23361+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23362+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23363+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23364+ a->btgt = err;
f2c43d5f
AM
23365+ if (a->exchange)
23366+ au_update_dbtop(a->dst_dentry);
dece6358 23367+
4a4d8108 23368+ return err;
1facf9fc 23369+}
23370+
4a4d8108 23371+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23372+{
4a4d8108
AM
23373+ a->h_path.dentry = a->src_h_parent;
23374+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23375+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23376+ a->h_path.dentry = a->dst_h_parent;
23377+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23378+ }
1facf9fc 23379+
f2c43d5f
AM
23380+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23381+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23382+ && !a->exchange)
4a4d8108 23383+ return;
dece6358 23384+
4a4d8108
AM
23385+ a->h_path.dentry = a->src_h_dentry;
23386+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23387+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23388+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23389+ a->h_path.dentry = a->dst_h_dentry;
23390+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23391+ }
1308ab2a 23392+}
dece6358 23393+
4a4d8108 23394+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23395+{
4a4d8108 23396+ struct dentry *h_d;
febd17d6 23397+ struct inode *h_inode;
4a4d8108
AM
23398+
23399+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23400+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23401+ au_dtime_revert(a->dst_dt + AuPARENT);
23402+
f2c43d5f 23403+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23404+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23405+ h_inode = d_inode(h_d);
23406+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23407+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23408+ inode_unlock(h_inode);
4a4d8108 23409+
f2c43d5f 23410+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23411+ h_d = a->dst_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->dst_dt + AuCHILD);
febd17d6 23415+ inode_unlock(h_inode);
1facf9fc 23416+ }
23417+ }
23418+}
23419+
4a4d8108
AM
23420+/* ---------------------------------------------------------------------- */
23421+
23422+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23423+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23424+ unsigned int _flags)
1facf9fc 23425+{
f2c43d5f 23426+ int err, lock_flags;
8b6a4947 23427+ void *rev;
4a4d8108
AM
23428+ /* reduce stack space */
23429+ struct au_ren_args *a;
f2c43d5f 23430+ struct au_pin pin;
4a4d8108 23431+
f2c43d5f 23432+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23433+ IMustLock(_src_dir);
23434+ IMustLock(_dst_dir);
23435+
f2c43d5f
AM
23436+ err = -EINVAL;
23437+ if (unlikely(_flags & RENAME_WHITEOUT))
23438+ goto out;
23439+
4a4d8108
AM
23440+ err = -ENOMEM;
23441+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23442+ a = kzalloc(sizeof(*a), GFP_NOFS);
23443+ if (unlikely(!a))
23444+ goto out;
23445+
f2c43d5f
AM
23446+ a->flags = _flags;
23447+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23448+ a->src_dir = _src_dir;
23449+ a->src_dentry = _src_dentry;
5527c038
JR
23450+ a->src_inode = NULL;
23451+ if (d_really_is_positive(a->src_dentry))
23452+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23453+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23454+ a->dst_dir = _dst_dir;
23455+ a->dst_dentry = _dst_dentry;
5527c038
JR
23456+ a->dst_inode = NULL;
23457+ if (d_really_is_positive(a->dst_dentry))
23458+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23459+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23460+ if (a->dst_inode) {
f2c43d5f
AM
23461+ /*
23462+ * if EXCHANGE && src is non-dir && dst is dir,
23463+ * dst is not locked.
23464+ */
23465+ /* IMustLock(a->dst_inode); */
4a4d8108 23466+ au_igrab(a->dst_inode);
1facf9fc 23467+ }
1facf9fc 23468+
4a4d8108 23469+ err = -ENOTDIR;
f2c43d5f 23470+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23471+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23472+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23473+ if (unlikely(!a->exchange
23474+ && d_really_is_positive(a->dst_dentry)
2000de60 23475+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23476+ goto out_free;
f2c43d5f
AM
23477+ lock_flags |= AuLock_DIRS;
23478+ }
23479+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23480+ au_fset_ren(a->auren_flags, ISDIR_DST);
23481+ if (unlikely(!a->exchange
23482+ && d_really_is_positive(a->src_dentry)
23483+ && !d_is_dir(a->src_dentry)))
23484+ goto out_free;
23485+ lock_flags |= AuLock_DIRS;
b95c5147 23486+ }
8b6a4947
AM
23487+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23488+ lock_flags);
e49829fe
JR
23489+ if (unlikely(err))
23490+ goto out_free;
1facf9fc 23491+
027c5e7a
AM
23492+ err = au_d_hashed_positive(a->src_dentry);
23493+ if (unlikely(err))
23494+ goto out_unlock;
23495+ err = -ENOENT;
23496+ if (a->dst_inode) {
23497+ /*
f2c43d5f 23498+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23499+ * function. It means we cannot rely upon d_unhashed().
23500+ */
23501+ if (unlikely(!a->dst_inode->i_nlink))
23502+ goto out_unlock;
f2c43d5f 23503+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23504+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23505+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23506+ goto out_unlock;
23507+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23508+ goto out_unlock;
23509+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23510+ goto out_unlock;
23511+
7eafdf33
AM
23512+ /*
23513+ * is it possible?
79b8bda9 23514+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23515+ * there may exist a problem somewhere else.
23516+ */
23517+ err = -EINVAL;
5527c038 23518+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23519+ goto out_unlock;
23520+
f2c43d5f 23521+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23522+ di_write_lock_parent(a->dst_parent);
1facf9fc 23523+
4a4d8108
AM
23524+ /* which branch we process */
23525+ err = au_ren_wbr(a);
23526+ if (unlikely(err < 0))
027c5e7a 23527+ goto out_parent;
4a4d8108 23528+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23529+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23530+
4a4d8108
AM
23531+ /* are they available to be renamed */
23532+ err = au_ren_may_dir(a);
23533+ if (unlikely(err))
23534+ goto out_children;
1facf9fc 23535+
4a4d8108 23536+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23537+ if (a->dst_btop == a->btgt) {
f2c43d5f 23538+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23539+ } else {
23540+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23541+ if (unlikely(err))
23542+ goto out_children;
23543+ }
1facf9fc 23544+
f2c43d5f
AM
23545+ err = 0;
23546+ if (!a->exchange) {
23547+ if (a->src_dir != a->dst_dir) {
23548+ /*
23549+ * this temporary unlock is safe,
23550+ * because both dir->i_mutex are locked.
23551+ */
23552+ di_write_unlock(a->dst_parent);
23553+ di_write_lock_parent(a->src_parent);
23554+ err = au_wr_dir_need_wh(a->src_dentry,
23555+ au_ftest_ren(a->auren_flags,
23556+ ISDIR_SRC),
23557+ &a->btgt);
23558+ di_write_unlock(a->src_parent);
23559+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23560+ /*isdir*/1);
23561+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23562+ } else
23563+ err = au_wr_dir_need_wh(a->src_dentry,
23564+ au_ftest_ren(a->auren_flags,
23565+ ISDIR_SRC),
23566+ &a->btgt);
23567+ }
4a4d8108
AM
23568+ if (unlikely(err < 0))
23569+ goto out_children;
23570+ if (err)
f2c43d5f 23571+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23572+
86dc4139 23573+ /* cpup src */
5afbbe0d 23574+ if (a->src_btop != a->btgt) {
86dc4139
AM
23575+ err = au_pin(&pin, a->src_dentry, a->btgt,
23576+ au_opt_udba(a->src_dentry->d_sb),
23577+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23578+ if (!err) {
c2b27bf2
AM
23579+ struct au_cp_generic cpg = {
23580+ .dentry = a->src_dentry,
23581+ .bdst = a->btgt,
5afbbe0d 23582+ .bsrc = a->src_btop,
c2b27bf2
AM
23583+ .len = -1,
23584+ .pin = &pin,
23585+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23586+ };
5afbbe0d 23587+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23588+ err = au_sio_cpup_simple(&cpg);
367653fa 23589+ au_unpin(&pin);
86dc4139 23590+ }
86dc4139
AM
23591+ if (unlikely(err))
23592+ goto out_children;
5afbbe0d 23593+ a->src_btop = a->btgt;
86dc4139 23594+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23595+ if (!a->exchange)
23596+ au_fset_ren(a->auren_flags, WHSRC);
23597+ }
23598+
23599+ /* cpup dst */
23600+ if (a->exchange && a->dst_inode
23601+ && a->dst_btop != a->btgt) {
23602+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23603+ au_opt_udba(a->dst_dentry->d_sb),
23604+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23605+ if (!err) {
23606+ struct au_cp_generic cpg = {
23607+ .dentry = a->dst_dentry,
23608+ .bdst = a->btgt,
23609+ .bsrc = a->dst_btop,
23610+ .len = -1,
23611+ .pin = &pin,
23612+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23613+ };
23614+ err = au_sio_cpup_simple(&cpg);
23615+ au_unpin(&pin);
23616+ }
23617+ if (unlikely(err))
23618+ goto out_children;
23619+ a->dst_btop = a->btgt;
23620+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23621+ }
23622+
4a4d8108
AM
23623+ /* lock them all */
23624+ err = au_ren_lock(a);
23625+ if (unlikely(err))
86dc4139 23626+ /* leave the copied-up one */
4a4d8108 23627+ goto out_children;
1facf9fc 23628+
f2c43d5f
AM
23629+ if (!a->exchange) {
23630+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23631+ err = au_may_ren(a);
23632+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23633+ err = -ENAMETOOLONG;
23634+ if (unlikely(err))
23635+ goto out_hdir;
23636+ }
1facf9fc 23637+
4a4d8108
AM
23638+ /* store timestamps to be revertible */
23639+ au_ren_dt(a);
1facf9fc 23640+
8b6a4947
AM
23641+ /* store dirren info */
23642+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23643+ err = au_dr_rename(a->src_dentry, a->btgt,
23644+ &a->dst_dentry->d_name, &rev);
23645+ AuTraceErr(err);
23646+ if (unlikely(err))
23647+ goto out_dt;
23648+ }
23649+
4a4d8108
AM
23650+ /* here we go */
23651+ err = do_rename(a);
23652+ if (unlikely(err))
8b6a4947
AM
23653+ goto out_dirren;
23654+
23655+ if (au_ftest_ren(a->auren_flags, DIRREN))
23656+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23657+
23658+ /* update dir attributes */
23659+ au_ren_refresh_dir(a);
23660+
23661+ /* dput/iput all lower dentries */
23662+ au_ren_refresh(a);
23663+
23664+ goto out_hdir; /* success */
23665+
8b6a4947
AM
23666+out_dirren:
23667+ if (au_ftest_ren(a->auren_flags, DIRREN))
23668+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23669+out_dt:
4a4d8108 23670+ au_ren_rev_dt(err, a);
4f0767ce 23671+out_hdir:
4a4d8108 23672+ au_ren_unlock(a);
4f0767ce 23673+out_children:
4a4d8108 23674+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23675+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23676+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23677+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23678+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23679+ }
027c5e7a 23680+out_parent:
f2c43d5f 23681+ if (!err) {
8b6a4947
AM
23682+ if (d_unhashed(a->src_dentry))
23683+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23684+ if (d_unhashed(a->dst_dentry))
23685+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23686+ if (!a->exchange)
23687+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23688+ else {
f2c43d5f 23689+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23690+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23691+ d_drop(a->dst_dentry);
23692+ }
23693+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23694+ d_drop(a->src_dentry);
f2c43d5f 23695+ } else {
5afbbe0d 23696+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23697+ if (!a->dst_inode)
23698+ d_drop(a->dst_dentry);
23699+ }
f2c43d5f 23700+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23701+ di_write_unlock(a->dst_parent);
23702+ else
23703+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23704+out_unlock:
4a4d8108 23705+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23706+out_free:
4a4d8108
AM
23707+ iput(a->dst_inode);
23708+ if (a->thargs)
23709+ au_whtmp_rmdir_free(a->thargs);
1c60b727 23710+ kfree(a);
4f0767ce 23711+out:
4a4d8108
AM
23712+ AuTraceErr(err);
23713+ return err;
1308ab2a 23714+}
7f207e10
AM
23715diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23716--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
b00004a5 23717+++ linux/fs/aufs/Kconfig 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
23718@@ -0,0 +1,199 @@
23719+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
23720+config AUFS_FS
23721+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23722+ help
23723+ Aufs is a stackable unification filesystem such as Unionfs,
23724+ which unifies several directories and provides a merged single
23725+ directory.
23726+ In the early days, aufs was entirely re-designed and
23727+ re-implemented Unionfs Version 1.x series. Introducing many
23728+ original ideas, approaches and improvements, it becomes totally
23729+ different from Unionfs while keeping the basic features.
1facf9fc 23730+
4a4d8108
AM
23731+if AUFS_FS
23732+choice
23733+ prompt "Maximum number of branches"
23734+ default AUFS_BRANCH_MAX_127
23735+ help
23736+ Specifies the maximum number of branches (or member directories)
23737+ in a single aufs. The larger value consumes more system
23738+ resources and has a minor impact to performance.
23739+config AUFS_BRANCH_MAX_127
23740+ bool "127"
23741+ help
23742+ Specifies the maximum number of branches (or member directories)
23743+ in a single aufs. The larger value consumes more system
23744+ resources and has a minor impact to performance.
23745+config AUFS_BRANCH_MAX_511
23746+ bool "511"
23747+ help
23748+ Specifies the maximum number of branches (or member directories)
23749+ in a single aufs. The larger value consumes more system
23750+ resources and has a minor impact to performance.
23751+config AUFS_BRANCH_MAX_1023
23752+ bool "1023"
23753+ help
23754+ Specifies the maximum number of branches (or member directories)
23755+ in a single aufs. The larger value consumes more system
23756+ resources and has a minor impact to performance.
23757+config AUFS_BRANCH_MAX_32767
23758+ bool "32767"
23759+ help
23760+ Specifies the maximum number of branches (or member directories)
23761+ in a single aufs. The larger value consumes more system
23762+ resources and has a minor impact to performance.
23763+endchoice
1facf9fc 23764+
e49829fe
JR
23765+config AUFS_SBILIST
23766+ bool
23767+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23768+ default y
23769+ help
23770+ Automatic configuration for internal use.
23771+ When aufs supports Magic SysRq or /proc, enabled automatically.
23772+
4a4d8108
AM
23773+config AUFS_HNOTIFY
23774+ bool "Detect direct branch access (bypassing aufs)"
23775+ help
23776+ If you want to modify files on branches directly, eg. bypassing aufs,
23777+ and want aufs to detect the changes of them fully, then enable this
23778+ option and use 'udba=notify' mount option.
7f207e10 23779+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
23780+ It will have a negative impact to the performance.
23781+ See detail in aufs.5.
dece6358 23782+
4a4d8108
AM
23783+choice
23784+ prompt "method" if AUFS_HNOTIFY
23785+ default AUFS_HFSNOTIFY
23786+config AUFS_HFSNOTIFY
23787+ bool "fsnotify"
23788+ select FSNOTIFY
4a4d8108 23789+endchoice
1facf9fc 23790+
4a4d8108
AM
23791+config AUFS_EXPORT
23792+ bool "NFS-exportable aufs"
2cbb1c4b 23793+ depends on EXPORTFS
4a4d8108
AM
23794+ help
23795+ If you want to export your mounted aufs via NFS, then enable this
23796+ option. There are several requirements for this configuration.
23797+ See detail in aufs.5.
1facf9fc 23798+
4a4d8108
AM
23799+config AUFS_INO_T_64
23800+ bool
23801+ depends on AUFS_EXPORT
23802+ depends on 64BIT && !(ALPHA || S390)
23803+ default y
23804+ help
23805+ Automatic configuration for internal use.
23806+ /* typedef unsigned long/int __kernel_ino_t */
23807+ /* alpha and s390x are int */
1facf9fc 23808+
c1595e42
JR
23809+config AUFS_XATTR
23810+ bool "support for XATTR/EA (including Security Labels)"
23811+ help
23812+ If your branch fs supports XATTR/EA and you want to make them
23813+ available in aufs too, then enable this opsion and specify the
23814+ branch attributes for EA.
23815+ See detail in aufs.5.
23816+
076b876e
AM
23817+config AUFS_FHSM
23818+ bool "File-based Hierarchical Storage Management"
23819+ help
23820+ Hierarchical Storage Management (or HSM) is a well-known feature
23821+ in the storage world. Aufs provides this feature as file-based.
23822+ with multiple branches.
23823+ These multiple branches are prioritized, ie. the topmost one
23824+ should be the fastest drive and be used heavily.
23825+
4a4d8108
AM
23826+config AUFS_RDU
23827+ bool "Readdir in userspace"
23828+ help
23829+ Aufs has two methods to provide a merged view for a directory,
23830+ by a user-space library and by kernel-space natively. The latter
23831+ is always enabled but sometimes large and slow.
23832+ If you enable this option, install the library in aufs2-util
23833+ package, and set some environment variables for your readdir(3),
23834+ then the work will be handled in user-space which generally
23835+ shows better performance in most cases.
23836+ See detail in aufs.5.
1facf9fc 23837+
8b6a4947
AM
23838+config AUFS_DIRREN
23839+ bool "Workaround for rename(2)-ing a directory"
23840+ help
23841+ By default, aufs returns EXDEV error in renameing a dir who has
23842+ his child on the lower branch, since it is a bad idea to issue
23843+ rename(2) internally for every lower branch. But user may not
23844+ accept this behaviour. So here is a workaround to allow such
23845+ rename(2) and store some extra infromation on the writable
23846+ branch. Obviously this costs high (and I don't like it).
23847+ To use this feature, you need to enable this configuration AND
23848+ to specify the mount option `dirren.'
23849+ See details in aufs.5 and the design documents.
23850+
4a4d8108
AM
23851+config AUFS_SHWH
23852+ bool "Show whiteouts"
23853+ help
23854+ If you want to make the whiteouts in aufs visible, then enable
23855+ this option and specify 'shwh' mount option. Although it may
23856+ sounds like philosophy or something, but in technically it
23857+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 23858+
4a4d8108
AM
23859+config AUFS_BR_RAMFS
23860+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
23861+ help
23862+ If you want to use ramfs as an aufs branch fs, then enable this
23863+ option. Generally tmpfs is recommended.
23864+ Aufs prohibited them to be a branch fs by default, because
23865+ initramfs becomes unusable after switch_root or something
23866+ generally. If you sets initramfs as an aufs branch and boot your
23867+ system by switch_root, you will meet a problem easily since the
23868+ files in initramfs may be inaccessible.
23869+ Unless you are going to use ramfs as an aufs branch fs without
23870+ switch_root or something, leave it N.
1facf9fc 23871+
4a4d8108
AM
23872+config AUFS_BR_FUSE
23873+ bool "Fuse fs as an aufs branch"
23874+ depends on FUSE_FS
23875+ select AUFS_POLL
23876+ help
23877+ If you want to use fuse-based userspace filesystem as an aufs
23878+ branch fs, then enable this option.
23879+ It implements the internal poll(2) operation which is
23880+ implemented by fuse only (curretnly).
1facf9fc 23881+
4a4d8108
AM
23882+config AUFS_POLL
23883+ bool
23884+ help
23885+ Automatic configuration for internal use.
1facf9fc 23886+
4a4d8108
AM
23887+config AUFS_BR_HFSPLUS
23888+ bool "Hfsplus as an aufs branch"
23889+ depends on HFSPLUS_FS
23890+ default y
23891+ help
23892+ If you want to use hfsplus fs as an aufs branch fs, then enable
23893+ this option. This option introduces a small overhead at
23894+ copying-up a file on hfsplus.
1facf9fc 23895+
4a4d8108
AM
23896+config AUFS_BDEV_LOOP
23897+ bool
23898+ depends on BLK_DEV_LOOP
23899+ default y
23900+ help
23901+ Automatic configuration for internal use.
23902+ Convert =[ym] into =y.
1308ab2a 23903+
4a4d8108
AM
23904+config AUFS_DEBUG
23905+ bool "Debug aufs"
23906+ help
23907+ Enable this to compile aufs internal debug code.
23908+ It will have a negative impact to the performance.
23909+
23910+config AUFS_MAGIC_SYSRQ
23911+ bool
23912+ depends on AUFS_DEBUG && MAGIC_SYSRQ
23913+ default y
23914+ help
23915+ Automatic configuration for internal use.
23916+ When aufs supports Magic SysRq, enabled automatically.
23917+endif
7f207e10
AM
23918diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
23919--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 23920+++ linux/fs/aufs/loop.c 2018-04-15 08:49:13.401150731 +0200
e2f27e51 23921@@ -0,0 +1,147 @@
1facf9fc 23922+/*
b00004a5 23923+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 23924+ *
23925+ * This program, aufs is free software; you can redistribute it and/or modify
23926+ * it under the terms of the GNU General Public License as published by
23927+ * the Free Software Foundation; either version 2 of the License, or
23928+ * (at your option) any later version.
dece6358
AM
23929+ *
23930+ * This program is distributed in the hope that it will be useful,
23931+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23932+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23933+ * GNU General Public License for more details.
23934+ *
23935+ * You should have received a copy of the GNU General Public License
523b37e3 23936+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 23937+ */
23938+
23939+/*
23940+ * support for loopback block device as a branch
23941+ */
23942+
1facf9fc 23943+#include "aufs.h"
23944+
392086de
AM
23945+/* added into drivers/block/loop.c */
23946+static struct file *(*backing_file_func)(struct super_block *sb);
23947+
1facf9fc 23948+/*
23949+ * test if two lower dentries have overlapping branches.
23950+ */
b752ccd1 23951+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 23952+{
b752ccd1 23953+ struct super_block *h_sb;
392086de
AM
23954+ struct file *backing_file;
23955+
23956+ if (unlikely(!backing_file_func)) {
23957+ /* don't load "loop" module here */
23958+ backing_file_func = symbol_get(loop_backing_file);
23959+ if (unlikely(!backing_file_func))
23960+ /* "loop" module is not loaded */
23961+ return 0;
23962+ }
1facf9fc 23963+
b752ccd1 23964+ h_sb = h_adding->d_sb;
392086de
AM
23965+ backing_file = backing_file_func(h_sb);
23966+ if (!backing_file)
1facf9fc 23967+ return 0;
23968+
2000de60 23969+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
23970+ /*
23971+ * h_adding can be local NFS.
23972+ * in this case aufs cannot detect the loop.
23973+ */
23974+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 23975+ return 1;
b752ccd1 23976+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 23977+}
23978+
23979+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
23980+int au_test_loopback_kthread(void)
23981+{
b752ccd1
AM
23982+ int ret;
23983+ struct task_struct *tsk = current;
a2a7ad62 23984+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
23985+
23986+ ret = 0;
23987+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
23988+ get_task_comm(comm, tsk);
23989+ c = comm[4];
b752ccd1 23990+ ret = ('0' <= c && c <= '9'
a2a7ad62 23991+ && !strncmp(comm, "loop", 4));
b752ccd1 23992+ }
1facf9fc 23993+
b752ccd1 23994+ return ret;
1facf9fc 23995+}
87a755f4
AM
23996+
23997+/* ---------------------------------------------------------------------- */
23998+
23999+#define au_warn_loopback_step 16
24000+static int au_warn_loopback_nelem = au_warn_loopback_step;
24001+static unsigned long *au_warn_loopback_array;
24002+
24003+void au_warn_loopback(struct super_block *h_sb)
24004+{
24005+ int i, new_nelem;
24006+ unsigned long *a, magic;
24007+ static DEFINE_SPINLOCK(spin);
24008+
24009+ magic = h_sb->s_magic;
24010+ spin_lock(&spin);
24011+ a = au_warn_loopback_array;
24012+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24013+ if (a[i] == magic) {
24014+ spin_unlock(&spin);
24015+ return;
24016+ }
24017+
24018+ /* h_sb is new to us, print it */
24019+ if (i < au_warn_loopback_nelem) {
24020+ a[i] = magic;
24021+ goto pr;
24022+ }
24023+
24024+ /* expand the array */
24025+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24026+ a = au_kzrealloc(au_warn_loopback_array,
24027+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24028+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24029+ /*may_shrink*/0);
87a755f4
AM
24030+ if (a) {
24031+ au_warn_loopback_nelem = new_nelem;
24032+ au_warn_loopback_array = a;
24033+ a[i] = magic;
24034+ goto pr;
24035+ }
24036+
24037+ spin_unlock(&spin);
24038+ AuWarn1("realloc failed, ignored\n");
24039+ return;
24040+
24041+pr:
24042+ spin_unlock(&spin);
0c3ec466
AM
24043+ pr_warn("you may want to try another patch for loopback file "
24044+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24045+}
24046+
24047+int au_loopback_init(void)
24048+{
24049+ int err;
24050+ struct super_block *sb __maybe_unused;
24051+
79b8bda9 24052+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
87a755f4
AM
24053+
24054+ err = 0;
24055+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24056+ sizeof(unsigned long), GFP_NOFS);
24057+ if (unlikely(!au_warn_loopback_array))
24058+ err = -ENOMEM;
24059+
24060+ return err;
24061+}
24062+
24063+void au_loopback_fin(void)
24064+{
79b8bda9
AM
24065+ if (backing_file_func)
24066+ symbol_put(loop_backing_file);
1c60b727 24067+ kfree(au_warn_loopback_array);
87a755f4 24068+}
7f207e10
AM
24069diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24070--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 24071+++ linux/fs/aufs/loop.h 2018-04-15 08:49:13.401150731 +0200
523b37e3 24072@@ -0,0 +1,52 @@
1facf9fc 24073+/*
b00004a5 24074+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24075+ *
24076+ * This program, aufs is free software; you can redistribute it and/or modify
24077+ * it under the terms of the GNU General Public License as published by
24078+ * the Free Software Foundation; either version 2 of the License, or
24079+ * (at your option) any later version.
dece6358
AM
24080+ *
24081+ * This program is distributed in the hope that it will be useful,
24082+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24083+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24084+ * GNU General Public License for more details.
24085+ *
24086+ * You should have received a copy of the GNU General Public License
523b37e3 24087+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24088+ */
24089+
24090+/*
24091+ * support for loopback mount as a branch
24092+ */
24093+
24094+#ifndef __AUFS_LOOP_H__
24095+#define __AUFS_LOOP_H__
24096+
24097+#ifdef __KERNEL__
24098+
dece6358
AM
24099+struct dentry;
24100+struct super_block;
1facf9fc 24101+
24102+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24103+/* drivers/block/loop.c */
24104+struct file *loop_backing_file(struct super_block *sb);
24105+
1facf9fc 24106+/* loop.c */
b752ccd1 24107+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24108+int au_test_loopback_kthread(void);
87a755f4
AM
24109+void au_warn_loopback(struct super_block *h_sb);
24110+
24111+int au_loopback_init(void);
24112+void au_loopback_fin(void);
1facf9fc 24113+#else
4a4d8108 24114+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24115+ struct dentry *h_adding)
4a4d8108 24116+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24117+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24118+
24119+AuStubInt0(au_loopback_init, void)
24120+AuStubVoid(au_loopback_fin, void)
1facf9fc 24121+#endif /* BLK_DEV_LOOP */
24122+
24123+#endif /* __KERNEL__ */
24124+#endif /* __AUFS_LOOP_H__ */
7f207e10
AM
24125diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24126--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
b00004a5 24127+++ linux/fs/aufs/magic.mk 2018-06-04 09:08:09.188079511 +0200
2121bcd9
AM
24128@@ -0,0 +1,31 @@
24129+# SPDX-License-Identifier: GPL-2.0
1facf9fc 24130+
24131+# defined in ${srctree}/fs/fuse/inode.c
24132+# tristate
24133+ifdef CONFIG_FUSE_FS
24134+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24135+endif
24136+
1facf9fc 24137+# defined in ${srctree}/fs/xfs/xfs_sb.h
24138+# tristate
24139+ifdef CONFIG_XFS_FS
24140+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24141+endif
24142+
24143+# defined in ${srctree}/fs/configfs/mount.c
24144+# tristate
24145+ifdef CONFIG_CONFIGFS_FS
24146+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24147+endif
24148+
1facf9fc 24149+# defined in ${srctree}/fs/ubifs/ubifs.h
24150+# tristate
24151+ifdef CONFIG_UBIFS_FS
24152+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24153+endif
4a4d8108
AM
24154+
24155+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24156+# tristate
24157+ifdef CONFIG_HFSPLUS_FS
24158+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24159+endif
7f207e10
AM
24160diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24161--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
b00004a5 24162+++ linux/fs/aufs/Makefile 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
24163@@ -0,0 +1,46 @@
24164+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
24165+
24166+include ${src}/magic.mk
24167+ifeq (${CONFIG_AUFS_FS},m)
24168+include ${src}/conf.mk
24169+endif
24170+-include ${src}/priv_def.mk
24171+
24172+# cf. include/linux/kernel.h
24173+# enable pr_debug
24174+ccflags-y += -DDEBUG
f6c5ef8b
AM
24175+# sparse requires the full pathname
24176+ifdef M
523b37e3 24177+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24178+else
523b37e3 24179+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24180+endif
4a4d8108
AM
24181+
24182+obj-$(CONFIG_AUFS_FS) += aufs.o
24183+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24184+ wkq.o vfsub.o dcsub.o \
e49829fe 24185+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24186+ dinfo.o dentry.o \
24187+ dynop.o \
24188+ finfo.o file.o f_op.o \
24189+ dir.o vdir.o \
24190+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24191+ mvdown.o ioctl.o
4a4d8108
AM
24192+
24193+# all are boolean
e49829fe 24194+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24195+aufs-$(CONFIG_SYSFS) += sysfs.o
24196+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24197+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24198+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24199+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24200+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24201+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24202+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24203+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24204+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24205+aufs-$(CONFIG_AUFS_POLL) += poll.o
24206+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24207+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24208+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24209+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10
AM
24210diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24211--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 24212+++ linux/fs/aufs/module.c 2018-04-15 08:49:13.401150731 +0200
1c60b727 24213@@ -0,0 +1,266 @@
1facf9fc 24214+/*
b00004a5 24215+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24216+ *
24217+ * This program, aufs is free software; you can redistribute it and/or modify
24218+ * it under the terms of the GNU General Public License as published by
24219+ * the Free Software Foundation; either version 2 of the License, or
24220+ * (at your option) any later version.
dece6358
AM
24221+ *
24222+ * This program is distributed in the hope that it will be useful,
24223+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24224+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24225+ * GNU General Public License for more details.
24226+ *
24227+ * You should have received a copy of the GNU General Public License
523b37e3 24228+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24229+ */
24230+
24231+/*
24232+ * module global variables and operations
24233+ */
24234+
24235+#include <linux/module.h>
24236+#include <linux/seq_file.h>
24237+#include "aufs.h"
24238+
e2f27e51
AM
24239+/* shrinkable realloc */
24240+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24241+{
e2f27e51
AM
24242+ size_t sz;
24243+ int diff;
1facf9fc 24244+
e2f27e51
AM
24245+ sz = 0;
24246+ diff = -1;
24247+ if (p) {
24248+#if 0 /* unused */
24249+ if (!new_sz) {
1c60b727 24250+ kfree(p);
e2f27e51
AM
24251+ p = NULL;
24252+ goto out;
24253+ }
24254+#else
24255+ AuDebugOn(!new_sz);
24256+#endif
24257+ sz = ksize(p);
24258+ diff = au_kmidx_sub(sz, new_sz);
24259+ }
24260+ if (sz && !diff)
24261+ goto out;
24262+
24263+ if (sz < new_sz)
24264+ /* expand or SLOB */
24265+ p = krealloc(p, new_sz, gfp);
24266+ else if (new_sz < sz && may_shrink) {
24267+ /* shrink */
24268+ void *q;
24269+
24270+ q = kmalloc(new_sz, gfp);
24271+ if (q) {
24272+ if (p) {
24273+ memcpy(q, p, new_sz);
1c60b727 24274+ kfree(p);
e2f27e51
AM
24275+ }
24276+ p = q;
24277+ } else
24278+ p = NULL;
24279+ }
24280+
24281+out:
24282+ return p;
24283+}
24284+
24285+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24286+ int may_shrink)
24287+{
24288+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24289+ if (p && new_sz > nused)
1facf9fc 24290+ memset(p + nused, 0, new_sz - nused);
24291+ return p;
24292+}
24293+
24294+/* ---------------------------------------------------------------------- */
1facf9fc 24295+/*
24296+ * aufs caches
24297+ */
1c60b727 24298+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24299+
24300+static void au_cache_fin(void)
24301+{
24302+ int i;
24303+
24304+ /*
24305+ * Make sure all delayed rcu free inodes are flushed before we
24306+ * destroy cache.
24307+ */
24308+ rcu_barrier();
24309+
24310+ /* excluding AuCache_HNOTIFY */
24311+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24312+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24313+ kmem_cache_destroy(au_cache[i]);
24314+ au_cache[i] = NULL;
5afbbe0d
AM
24315+ }
24316+}
24317+
1facf9fc 24318+static int __init au_cache_init(void)
24319+{
1c60b727
AM
24320+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24321+ if (au_cache[AuCache_DINFO])
027c5e7a 24322+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24323+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24324+ au_icntnr_init_once);
1c60b727
AM
24325+ if (au_cache[AuCache_ICNTNR])
24326+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24327+ au_fi_init_once);
1c60b727
AM
24328+ if (au_cache[AuCache_FINFO])
24329+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24330+ if (au_cache[AuCache_VDIR])
24331+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24332+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24333+ return 0;
24334+
5afbbe0d 24335+ au_cache_fin();
1facf9fc 24336+ return -ENOMEM;
24337+}
24338+
1facf9fc 24339+/* ---------------------------------------------------------------------- */
24340+
24341+int au_dir_roflags;
24342+
e49829fe 24343+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24344+/*
24345+ * iterate_supers_type() doesn't protect us from
24346+ * remounting (branch management)
24347+ */
8b6a4947 24348+struct hlist_bl_head au_sbilist;
e49829fe
JR
24349+#endif
24350+
1facf9fc 24351+/*
24352+ * functions for module interface.
24353+ */
24354+MODULE_LICENSE("GPL");
24355+/* MODULE_LICENSE("GPL v2"); */
dece6358 24356+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24357+MODULE_DESCRIPTION(AUFS_NAME
24358+ " -- Advanced multi layered unification filesystem");
24359+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24360+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24361+
1facf9fc 24362+/* this module parameter has no meaning when SYSFS is disabled */
24363+int sysaufs_brs = 1;
24364+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24365+module_param_named(brs, sysaufs_brs, int, S_IRUGO);
24366+
076b876e 24367+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24368+bool au_userns;
076b876e
AM
24369+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24370+module_param_named(allow_userns, au_userns, bool, S_IRUGO);
24371+
1facf9fc 24372+/* ---------------------------------------------------------------------- */
24373+
24374+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24375+
24376+int au_seq_path(struct seq_file *seq, struct path *path)
24377+{
79b8bda9
AM
24378+ int err;
24379+
24380+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24381+ if (err >= 0)
79b8bda9 24382+ err = 0;
1c60b727 24383+ else
79b8bda9
AM
24384+ err = -ENOMEM;
24385+
24386+ return err;
1facf9fc 24387+}
24388+
24389+/* ---------------------------------------------------------------------- */
24390+
24391+static int __init aufs_init(void)
24392+{
24393+ int err, i;
24394+ char *p;
24395+
24396+ p = au_esc_chars;
24397+ for (i = 1; i <= ' '; i++)
24398+ *p++ = i;
24399+ *p++ = '\\';
24400+ *p++ = '\x7f';
24401+ *p = 0;
24402+
24403+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24404+
b95c5147
AM
24405+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24406+ for (i = 0; i < AuIop_Last; i++)
24407+ aufs_iop_nogetattr[i].getattr = NULL;
24408+
1c60b727 24409+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24410+
e49829fe 24411+ au_sbilist_init();
1facf9fc 24412+ sysaufs_brs_init();
24413+ au_debug_init();
4a4d8108 24414+ au_dy_init();
1facf9fc 24415+ err = sysaufs_init();
24416+ if (unlikely(err))
24417+ goto out;
e49829fe 24418+ err = au_procfs_init();
4f0767ce 24419+ if (unlikely(err))
953406b4 24420+ goto out_sysaufs;
e49829fe
JR
24421+ err = au_wkq_init();
24422+ if (unlikely(err))
24423+ goto out_procfs;
87a755f4 24424+ err = au_loopback_init();
1facf9fc 24425+ if (unlikely(err))
24426+ goto out_wkq;
87a755f4
AM
24427+ err = au_hnotify_init();
24428+ if (unlikely(err))
24429+ goto out_loopback;
1facf9fc 24430+ err = au_sysrq_init();
24431+ if (unlikely(err))
24432+ goto out_hin;
24433+ err = au_cache_init();
24434+ if (unlikely(err))
24435+ goto out_sysrq;
076b876e
AM
24436+
24437+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24438+ err = register_filesystem(&aufs_fs_type);
24439+ if (unlikely(err))
24440+ goto out_cache;
076b876e 24441+
4a4d8108
AM
24442+ /* since we define pr_fmt, call printk directly */
24443+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24444+ goto out; /* success */
24445+
4f0767ce 24446+out_cache:
1facf9fc 24447+ au_cache_fin();
4f0767ce 24448+out_sysrq:
1facf9fc 24449+ au_sysrq_fin();
4f0767ce 24450+out_hin:
4a4d8108 24451+ au_hnotify_fin();
87a755f4
AM
24452+out_loopback:
24453+ au_loopback_fin();
4f0767ce 24454+out_wkq:
1facf9fc 24455+ au_wkq_fin();
e49829fe
JR
24456+out_procfs:
24457+ au_procfs_fin();
4f0767ce 24458+out_sysaufs:
1facf9fc 24459+ sysaufs_fin();
4a4d8108 24460+ au_dy_fin();
4f0767ce 24461+out:
1facf9fc 24462+ return err;
24463+}
24464+
24465+static void __exit aufs_exit(void)
24466+{
24467+ unregister_filesystem(&aufs_fs_type);
24468+ au_cache_fin();
24469+ au_sysrq_fin();
4a4d8108 24470+ au_hnotify_fin();
87a755f4 24471+ au_loopback_fin();
1facf9fc 24472+ au_wkq_fin();
e49829fe 24473+ au_procfs_fin();
1facf9fc 24474+ sysaufs_fin();
4a4d8108 24475+ au_dy_fin();
1facf9fc 24476+}
24477+
24478+module_init(aufs_init);
24479+module_exit(aufs_exit);
7f207e10
AM
24480diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24481--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 24482+++ linux/fs/aufs/module.h 2018-04-15 08:49:13.401150731 +0200
1c60b727 24483@@ -0,0 +1,101 @@
1facf9fc 24484+/*
b00004a5 24485+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24486+ *
24487+ * This program, aufs is free software; you can redistribute it and/or modify
24488+ * it under the terms of the GNU General Public License as published by
24489+ * the Free Software Foundation; either version 2 of the License, or
24490+ * (at your option) any later version.
dece6358
AM
24491+ *
24492+ * This program is distributed in the hope that it will be useful,
24493+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24494+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24495+ * GNU General Public License for more details.
24496+ *
24497+ * You should have received a copy of the GNU General Public License
523b37e3 24498+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24499+ */
24500+
24501+/*
24502+ * module initialization and module-global
24503+ */
24504+
24505+#ifndef __AUFS_MODULE_H__
24506+#define __AUFS_MODULE_H__
24507+
24508+#ifdef __KERNEL__
24509+
24510+#include <linux/slab.h>
24511+
dece6358
AM
24512+struct path;
24513+struct seq_file;
24514+
1facf9fc 24515+/* module parameters */
1facf9fc 24516+extern int sysaufs_brs;
8cdd5066 24517+extern bool au_userns;
1facf9fc 24518+
24519+/* ---------------------------------------------------------------------- */
24520+
24521+extern int au_dir_roflags;
24522+
e2f27e51
AM
24523+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24524+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24525+ int may_shrink);
24526+
24527+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24528+{
24529+#ifndef CONFIG_SLOB
24530+ return kmalloc_index(sz) - kmalloc_index(new_sz);
24531+#else
24532+ return -1; /* SLOB is untested */
24533+#endif
24534+}
24535+
1facf9fc 24536+int au_seq_path(struct seq_file *seq, struct path *path);
24537+
e49829fe
JR
24538+#ifdef CONFIG_PROC_FS
24539+/* procfs.c */
24540+int __init au_procfs_init(void);
24541+void au_procfs_fin(void);
24542+#else
24543+AuStubInt0(au_procfs_init, void);
24544+AuStubVoid(au_procfs_fin, void);
24545+#endif
24546+
4f0767ce
JR
24547+/* ---------------------------------------------------------------------- */
24548+
1c60b727 24549+/* kmem cache */
1facf9fc 24550+enum {
24551+ AuCache_DINFO,
24552+ AuCache_ICNTNR,
24553+ AuCache_FINFO,
24554+ AuCache_VDIR,
24555+ AuCache_DEHSTR,
7eafdf33 24556+ AuCache_HNOTIFY, /* must be last */
1facf9fc 24557+ AuCache_Last
24558+};
24559+
1c60b727 24560+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 24561+
4a4d8108
AM
24562+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24563+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
24564+#define AuCacheCtor(type, ctor) \
24565+ kmem_cache_create(#type, sizeof(struct type), \
24566+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 24567+
1facf9fc 24568+#define AuCacheFuncs(name, index) \
4a4d8108 24569+static inline struct au_##name *au_cache_alloc_##name(void) \
1c60b727 24570+{ return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
4a4d8108 24571+static inline void au_cache_free_##name(struct au_##name *p) \
1c60b727 24572+{ kmem_cache_free(au_cache[AuCache_##index], p); }
1facf9fc 24573+
24574+AuCacheFuncs(dinfo, DINFO);
24575+AuCacheFuncs(icntnr, ICNTNR);
24576+AuCacheFuncs(finfo, FINFO);
24577+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
24578+AuCacheFuncs(vdir_dehstr, DEHSTR);
24579+#ifdef CONFIG_AUFS_HNOTIFY
24580+AuCacheFuncs(hnotify, HNOTIFY);
24581+#endif
1facf9fc 24582+
4a4d8108
AM
24583+#endif /* __KERNEL__ */
24584+#endif /* __AUFS_MODULE_H__ */
c2b27bf2
AM
24585diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24586--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 24587+++ linux/fs/aufs/mvdown.c 2018-06-04 09:08:09.188079511 +0200
5afbbe0d 24588@@ -0,0 +1,704 @@
c2b27bf2 24589+/*
b00004a5 24590+ * Copyright (C) 2011-2018 Junjiro R. Okajima
c2b27bf2
AM
24591+ *
24592+ * This program, aufs is free software; you can redistribute it and/or modify
24593+ * it under the terms of the GNU General Public License as published by
24594+ * the Free Software Foundation; either version 2 of the License, or
24595+ * (at your option) any later version.
24596+ *
24597+ * This program is distributed in the hope that it will be useful,
24598+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24599+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24600+ * GNU General Public License for more details.
24601+ *
24602+ * You should have received a copy of the GNU General Public License
523b37e3
AM
24603+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24604+ */
24605+
24606+/*
24607+ * move-down, opposite of copy-up
c2b27bf2
AM
24608+ */
24609+
24610+#include "aufs.h"
24611+
c2b27bf2
AM
24612+struct au_mvd_args {
24613+ struct {
c2b27bf2
AM
24614+ struct super_block *h_sb;
24615+ struct dentry *h_parent;
24616+ struct au_hinode *hdir;
392086de 24617+ struct inode *h_dir, *h_inode;
c1595e42 24618+ struct au_pin pin;
c2b27bf2
AM
24619+ } info[AUFS_MVDOWN_NARRAY];
24620+
24621+ struct aufs_mvdown mvdown;
24622+ struct dentry *dentry, *parent;
24623+ struct inode *inode, *dir;
24624+ struct super_block *sb;
24625+ aufs_bindex_t bopq, bwh, bfound;
24626+ unsigned char rename_lock;
c2b27bf2
AM
24627+};
24628+
392086de 24629+#define mvd_errno mvdown.au_errno
076b876e
AM
24630+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24631+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24632+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24633+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 24634+
392086de
AM
24635+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
24636+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
24637+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
24638+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
24639+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 24640+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
24641+
24642+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
24643+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
24644+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
24645+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
24646+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 24647+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
24648+
24649+#define AU_MVD_PR(flag, ...) do { \
24650+ if (flag) \
24651+ pr_err(__VA_ARGS__); \
24652+ } while (0)
24653+
076b876e
AM
24654+static int find_lower_writable(struct au_mvd_args *a)
24655+{
24656+ struct super_block *sb;
5afbbe0d 24657+ aufs_bindex_t bindex, bbot;
076b876e
AM
24658+ struct au_branch *br;
24659+
24660+ sb = a->sb;
24661+ bindex = a->mvd_bsrc;
5afbbe0d 24662+ bbot = au_sbbot(sb);
076b876e 24663+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 24664+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24665+ br = au_sbr(sb, bindex);
24666+ if (au_br_fhsm(br->br_perm)
8b6a4947 24667+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
24668+ return bindex;
24669+ }
24670+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 24671+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24672+ br = au_sbr(sb, bindex);
24673+ if (!au_br_rdonly(br))
24674+ return bindex;
24675+ }
24676+ else
5afbbe0d 24677+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 24678+ br = au_sbr(sb, bindex);
8b6a4947 24679+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
24680+ if (au_br_rdonly(br))
24681+ a->mvdown.flags
24682+ |= AUFS_MVDOWN_ROLOWER_R;
24683+ return bindex;
24684+ }
24685+ }
24686+
24687+ return -1;
24688+}
24689+
c2b27bf2 24690+/* make the parent dir on bdst */
392086de 24691+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24692+{
24693+ int err;
24694+
24695+ err = 0;
24696+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24697+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24698+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24699+ a->mvd_h_dst_parent = NULL;
5afbbe0d 24700+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
24701+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24702+ if (!a->mvd_h_dst_parent) {
24703+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
24704+ if (unlikely(err)) {
392086de 24705+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
24706+ goto out;
24707+ }
24708+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24709+ }
24710+
24711+out:
24712+ AuTraceErr(err);
24713+ return err;
24714+}
24715+
24716+/* lock them all */
392086de 24717+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24718+{
24719+ int err;
24720+ struct dentry *h_trap;
24721+
24722+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
24723+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
24724+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
24725+ au_opt_udba(a->sb),
24726+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24727+ AuTraceErr(err);
24728+ if (unlikely(err)) {
24729+ AU_MVD_PR(dmsg, "pin_dst failed\n");
24730+ goto out;
24731+ }
24732+
c2b27bf2
AM
24733+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
24734+ a->rename_lock = 0;
c1595e42
JR
24735+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24736+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
24737+ au_opt_udba(a->sb),
24738+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24739+ err = au_do_pin(&a->mvd_pin_src);
24740+ AuTraceErr(err);
5527c038 24741+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
24742+ if (unlikely(err)) {
24743+ AU_MVD_PR(dmsg, "pin_src failed\n");
24744+ goto out_dst;
24745+ }
24746+ goto out; /* success */
c2b27bf2
AM
24747+ }
24748+
c2b27bf2 24749+ a->rename_lock = 1;
c1595e42
JR
24750+ au_pin_hdir_unlock(&a->mvd_pin_dst);
24751+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24752+ au_opt_udba(a->sb),
24753+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24754+ AuTraceErr(err);
5527c038 24755+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
24756+ if (unlikely(err)) {
24757+ AU_MVD_PR(dmsg, "pin_src failed\n");
24758+ au_pin_hdir_lock(&a->mvd_pin_dst);
24759+ goto out_dst;
24760+ }
24761+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
24762+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24763+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
24764+ if (h_trap) {
24765+ err = (h_trap != a->mvd_h_src_parent);
24766+ if (err)
24767+ err = (h_trap != a->mvd_h_dst_parent);
24768+ }
24769+ BUG_ON(err); /* it should never happen */
c1595e42
JR
24770+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
24771+ err = -EBUSY;
24772+ AuTraceErr(err);
24773+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24774+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
24775+ au_pin_hdir_lock(&a->mvd_pin_src);
24776+ au_unpin(&a->mvd_pin_src);
24777+ au_pin_hdir_lock(&a->mvd_pin_dst);
24778+ goto out_dst;
24779+ }
24780+ goto out; /* success */
c2b27bf2 24781+
c1595e42
JR
24782+out_dst:
24783+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
24784+out:
24785+ AuTraceErr(err);
24786+ return err;
24787+}
24788+
392086de 24789+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 24790+{
c1595e42
JR
24791+ if (!a->rename_lock)
24792+ au_unpin(&a->mvd_pin_src);
24793+ else {
c2b27bf2
AM
24794+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24795+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
24796+ au_pin_hdir_lock(&a->mvd_pin_src);
24797+ au_unpin(&a->mvd_pin_src);
24798+ au_pin_hdir_lock(&a->mvd_pin_dst);
24799+ }
24800+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
24801+}
24802+
24803+/* copy-down the file */
392086de 24804+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24805+{
24806+ int err;
24807+ struct au_cp_generic cpg = {
24808+ .dentry = a->dentry,
24809+ .bdst = a->mvd_bdst,
24810+ .bsrc = a->mvd_bsrc,
24811+ .len = -1,
c1595e42 24812+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
24813+ .flags = AuCpup_DTIME | AuCpup_HOPEN
24814+ };
24815+
24816+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
24817+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
24818+ au_fset_cpup(cpg.flags, OVERWRITE);
24819+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
24820+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
24821+ err = au_sio_cpdown_simple(&cpg);
24822+ if (unlikely(err))
392086de 24823+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
24824+
24825+ AuTraceErr(err);
24826+ return err;
24827+}
24828+
24829+/*
24830+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
24831+ * were sleeping
24832+ */
392086de 24833+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24834+{
24835+ int err;
24836+ struct path h_path;
24837+ struct au_branch *br;
523b37e3 24838+ struct inode *delegated;
c2b27bf2
AM
24839+
24840+ br = au_sbr(a->sb, a->mvd_bdst);
24841+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
24842+ err = PTR_ERR(h_path.dentry);
24843+ if (IS_ERR(h_path.dentry)) {
392086de 24844+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
24845+ goto out;
24846+ }
24847+
24848+ err = 0;
5527c038 24849+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 24850+ h_path.mnt = au_br_mnt(br);
523b37e3 24851+ delegated = NULL;
5527c038 24852+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
24853+ &delegated, /*force*/0);
24854+ if (unlikely(err == -EWOULDBLOCK)) {
24855+ pr_warn("cannot retry for NFSv4 delegation"
24856+ " for an internal unlink\n");
24857+ iput(delegated);
24858+ }
c2b27bf2 24859+ if (unlikely(err))
392086de 24860+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
24861+ }
24862+ dput(h_path.dentry);
24863+
24864+out:
24865+ AuTraceErr(err);
24866+ return err;
24867+}
24868+
24869+/*
24870+ * unlink the topmost h_dentry
c2b27bf2 24871+ */
392086de 24872+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24873+{
24874+ int err;
24875+ struct path h_path;
523b37e3 24876+ struct inode *delegated;
c2b27bf2
AM
24877+
24878+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
24879+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
24880+ delegated = NULL;
24881+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
24882+ if (unlikely(err == -EWOULDBLOCK)) {
24883+ pr_warn("cannot retry for NFSv4 delegation"
24884+ " for an internal unlink\n");
24885+ iput(delegated);
24886+ }
c2b27bf2 24887+ if (unlikely(err))
392086de 24888+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
24889+
24890+ AuTraceErr(err);
24891+ return err;
24892+}
24893+
076b876e
AM
24894+/* Since mvdown succeeded, we ignore an error of this function */
24895+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
24896+{
24897+ int err;
24898+ struct au_branch *br;
24899+
24900+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
24901+ br = au_sbr(a->sb, a->mvd_bsrc);
24902+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
24903+ if (!err) {
24904+ br = au_sbr(a->sb, a->mvd_bdst);
24905+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
24906+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
24907+ }
24908+ if (!err)
24909+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
24910+ else
24911+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
24912+}
24913+
c2b27bf2
AM
24914+/*
24915+ * copy-down the file and unlink the bsrc file.
24916+ * - unlink the bdst whout if exist
24917+ * - copy-down the file (with whtmp name and rename)
24918+ * - unlink the bsrc file
24919+ */
392086de 24920+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24921+{
24922+ int err;
24923+
392086de 24924+ err = au_do_mkdir(dmsg, a);
c2b27bf2 24925+ if (!err)
392086de 24926+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
24927+ if (unlikely(err))
24928+ goto out;
24929+
24930+ /*
24931+ * do not revert the activities we made on bdst since they should be
24932+ * harmless in aufs.
24933+ */
24934+
392086de 24935+ err = au_do_cpdown(dmsg, a);
c2b27bf2 24936+ if (!err)
392086de
AM
24937+ err = au_do_unlink_wh(dmsg, a);
24938+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
24939+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
24940+ if (unlikely(err))
24941+ goto out_unlock;
24942+
c1595e42
JR
24943+ AuDbg("%pd2, 0x%x, %d --> %d\n",
24944+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
24945+ if (find_lower_writable(a) < 0)
24946+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
24947+
24948+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
24949+ au_do_stfs(dmsg, a);
24950+
c2b27bf2 24951+ /* maintain internal array */
392086de
AM
24952+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
24953+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 24954+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 24955+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 24956+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
24957+ } else {
24958+ /* hide the lower */
24959+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 24960+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 24961+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 24962+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 24963+ }
5afbbe0d
AM
24964+ if (au_dbbot(a->dentry) < a->mvd_bdst)
24965+ au_set_dbbot(a->dentry, a->mvd_bdst);
24966+ if (au_ibbot(a->inode) < a->mvd_bdst)
24967+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
24968+
24969+out_unlock:
392086de 24970+ au_do_unlock(dmsg, a);
c2b27bf2
AM
24971+out:
24972+ AuTraceErr(err);
24973+ return err;
24974+}
24975+
24976+/* ---------------------------------------------------------------------- */
24977+
c2b27bf2 24978+/* make sure the file is idle */
392086de 24979+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24980+{
24981+ int err, plinked;
c2b27bf2
AM
24982+
24983+ err = 0;
c2b27bf2 24984+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 24985+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 24986+ && au_dcount(a->dentry) == 1
c2b27bf2 24987+ && atomic_read(&a->inode->i_count) == 1
392086de 24988+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
24989+ && (!plinked || !au_plink_test(a->inode))
24990+ && a->inode->i_nlink == 1)
24991+ goto out;
24992+
24993+ err = -EBUSY;
392086de 24994+ AU_MVD_PR(dmsg,
c1595e42 24995+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 24996+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 24997+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 24998+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
24999+ plinked, plinked ? au_plink_test(a->inode) : 0);
25000+
25001+out:
25002+ AuTraceErr(err);
25003+ return err;
25004+}
25005+
25006+/* make sure the parent dir is fine */
392086de 25007+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25008+ struct au_mvd_args *a)
25009+{
25010+ int err;
25011+ aufs_bindex_t bindex;
25012+
25013+ err = 0;
25014+ if (unlikely(au_alive_dir(a->parent))) {
25015+ err = -ENOENT;
392086de 25016+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25017+ goto out;
25018+ }
25019+
25020+ a->bopq = au_dbdiropq(a->parent);
25021+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25022+ AuDbg("b%d\n", bindex);
25023+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25024+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25025+ err = -EINVAL;
392086de
AM
25026+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25027+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25028+ a->bopq, a->mvd_bdst);
25029+ }
25030+
25031+out:
25032+ AuTraceErr(err);
25033+ return err;
25034+}
25035+
392086de 25036+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25037+ struct au_mvd_args *a)
25038+{
25039+ int err;
25040+ struct au_dinfo *dinfo, *tmp;
25041+
25042+ /* lookup the next lower positive entry */
25043+ err = -ENOMEM;
25044+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25045+ if (unlikely(!tmp))
25046+ goto out;
25047+
25048+ a->bfound = -1;
25049+ a->bwh = -1;
25050+ dinfo = au_di(a->dentry);
25051+ au_di_cp(tmp, dinfo);
25052+ au_di_swap(tmp, dinfo);
25053+
25054+ /* returns the number of positive dentries */
5afbbe0d
AM
25055+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25056+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25057+ if (!err)
25058+ a->bwh = au_dbwh(a->dentry);
25059+ else if (err > 0)
5afbbe0d 25060+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25061+
25062+ au_di_swap(tmp, dinfo);
25063+ au_rw_write_unlock(&tmp->di_rwsem);
25064+ au_di_free(tmp);
25065+ if (unlikely(err < 0))
392086de 25066+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25067+
25068+ /*
25069+ * here, we have these cases.
25070+ * bfound == -1
25071+ * no positive dentry under bsrc. there are more sub-cases.
25072+ * bwh < 0
25073+ * there no whiteout, we can safely move-down.
25074+ * bwh <= bsrc
25075+ * impossible
25076+ * bsrc < bwh && bwh < bdst
25077+ * there is a whiteout on RO branch. cannot proceed.
25078+ * bwh == bdst
25079+ * there is a whiteout on the RW target branch. it should
25080+ * be removed.
25081+ * bdst < bwh
25082+ * there is a whiteout somewhere unrelated branch.
25083+ * -1 < bfound && bfound <= bsrc
25084+ * impossible.
25085+ * bfound < bdst
25086+ * found, but it is on RO branch between bsrc and bdst. cannot
25087+ * proceed.
25088+ * bfound == bdst
25089+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25090+ * error.
25091+ * bdst < bfound
25092+ * found, after we create the file on bdst, it will be hidden.
25093+ */
25094+
25095+ AuDebugOn(a->bfound == -1
25096+ && a->bwh != -1
25097+ && a->bwh <= a->mvd_bsrc);
25098+ AuDebugOn(-1 < a->bfound
25099+ && a->bfound <= a->mvd_bsrc);
25100+
25101+ err = -EINVAL;
25102+ if (a->bfound == -1
25103+ && a->mvd_bsrc < a->bwh
25104+ && a->bwh != -1
25105+ && a->bwh < a->mvd_bdst) {
392086de
AM
25106+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25107+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25108+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25109+ goto out;
25110+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25111+ a->mvd_errno = EAU_MVDOWN_UPPER;
25112+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25113+ a->mvd_bdst, a->bfound);
25114+ goto out;
25115+ }
25116+
25117+ err = 0; /* success */
25118+
25119+out:
25120+ AuTraceErr(err);
25121+ return err;
25122+}
25123+
392086de 25124+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25125+{
25126+ int err;
25127+
392086de
AM
25128+ err = 0;
25129+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25130+ && a->bfound == a->mvd_bdst)
25131+ err = -EEXIST;
c2b27bf2
AM
25132+ AuTraceErr(err);
25133+ return err;
25134+}
25135+
392086de 25136+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25137+{
25138+ int err;
25139+ struct au_branch *br;
25140+
25141+ err = -EISDIR;
25142+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25143+ goto out;
25144+
25145+ err = -EINVAL;
392086de 25146+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25147+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25148+ else {
25149+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25150+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25151+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25152+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25153+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25154+ || (a->mvd_bsrc < au_ibtop(a->inode)
25155+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25156+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25157+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25158+ AU_MVD_PR(dmsg, "no upper\n");
25159+ goto out;
25160+ }
25161+ }
5afbbe0d 25162+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25163+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25164+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25165+ goto out;
25166+ }
392086de 25167+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25168+ br = au_sbr(a->sb, a->mvd_bsrc);
25169+ err = au_br_rdonly(br);
392086de
AM
25170+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25171+ if (unlikely(err))
25172+ goto out;
25173+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25174+ || IS_APPEND(a->mvd_h_src_inode))) {
25175+ if (err)
25176+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25177+ /* go on */
25178+ } else
c2b27bf2
AM
25179+ goto out;
25180+
25181+ err = -EINVAL;
392086de
AM
25182+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25183+ a->mvd_bdst = find_lower_writable(a);
25184+ if (unlikely(a->mvd_bdst < 0)) {
25185+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25186+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25187+ goto out;
25188+ }
25189+ } else {
25190+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25191+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25192+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25193+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25194+ AU_MVD_PR(dmsg, "no lower brid\n");
25195+ goto out;
25196+ }
c2b27bf2
AM
25197+ }
25198+
392086de 25199+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25200+ if (!err)
392086de 25201+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25202+ if (!err)
392086de 25203+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25204+ if (!err)
392086de 25205+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25206+ if (!err)
25207+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25208+
25209+out:
25210+ AuTraceErr(err);
25211+ return err;
25212+}
25213+
25214+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25215+{
392086de
AM
25216+ int err, e;
25217+ unsigned char dmsg;
25218+ struct au_mvd_args *args;
79b8bda9 25219+ struct inode *inode;
c2b27bf2 25220+
79b8bda9 25221+ inode = d_inode(dentry);
c2b27bf2
AM
25222+ err = -EPERM;
25223+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25224+ goto out;
25225+
392086de
AM
25226+ err = -ENOMEM;
25227+ args = kmalloc(sizeof(*args), GFP_NOFS);
25228+ if (unlikely(!args))
25229+ goto out;
25230+
25231+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25232+ if (!err)
25233+ err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
c2b27bf2
AM
25234+ if (unlikely(err)) {
25235+ err = -EFAULT;
392086de
AM
25236+ AuTraceErr(err);
25237+ goto out_free;
c2b27bf2 25238+ }
392086de
AM
25239+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25240+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25241+ args->mvdown.au_errno = 0;
25242+ args->dentry = dentry;
79b8bda9 25243+ args->inode = inode;
392086de 25244+ args->sb = dentry->d_sb;
c2b27bf2 25245+
392086de
AM
25246+ err = -ENOENT;
25247+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25248+ args->parent = dget_parent(dentry);
5527c038 25249+ args->dir = d_inode(args->parent);
febd17d6 25250+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25251+ dput(args->parent);
25252+ if (unlikely(args->parent != dentry->d_parent)) {
25253+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25254+ goto out_dir;
25255+ }
25256+
febd17d6 25257+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25258+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25259+ if (unlikely(err))
25260+ goto out_inode;
25261+
392086de
AM
25262+ di_write_lock_parent(args->parent);
25263+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25264+ if (unlikely(err))
25265+ goto out_parent;
25266+
392086de 25267+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25268+ if (unlikely(err))
25269+ goto out_parent;
c2b27bf2 25270+
392086de 25271+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25272+ au_cpup_attr_timesizes(inode);
25273+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25274+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25275+ /* au_digen_dec(dentry); */
25276+
25277+out_parent:
392086de 25278+ di_write_unlock(args->parent);
c2b27bf2
AM
25279+ aufs_read_unlock(dentry, AuLock_DW);
25280+out_inode:
febd17d6 25281+ inode_unlock(inode);
c2b27bf2 25282+out_dir:
febd17d6 25283+ inode_unlock(args->dir);
392086de
AM
25284+out_free:
25285+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25286+ if (unlikely(e))
25287+ err = -EFAULT;
1c60b727 25288+ kfree(args);
c2b27bf2
AM
25289+out:
25290+ AuTraceErr(err);
25291+ return err;
25292+}
25293diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25294--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 25295+++ linux/fs/aufs/opts.c 2018-06-04 09:08:09.188079511 +0200
8b6a4947 25296@@ -0,0 +1,1891 @@
1facf9fc 25297+/*
b00004a5 25298+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 25299+ *
25300+ * This program, aufs is free software; you can redistribute it and/or modify
25301+ * it under the terms of the GNU General Public License as published by
25302+ * the Free Software Foundation; either version 2 of the License, or
25303+ * (at your option) any later version.
dece6358
AM
25304+ *
25305+ * This program is distributed in the hope that it will be useful,
25306+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25307+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25308+ * GNU General Public License for more details.
25309+ *
25310+ * You should have received a copy of the GNU General Public License
523b37e3 25311+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25312+ */
25313+
25314+/*
25315+ * mount options/flags
25316+ */
25317+
dece6358 25318+#include <linux/namei.h>
1facf9fc 25319+#include <linux/types.h> /* a distribution requires */
25320+#include <linux/parser.h>
25321+#include "aufs.h"
25322+
25323+/* ---------------------------------------------------------------------- */
25324+
25325+enum {
25326+ Opt_br,
7e9cd9fe
AM
25327+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25328+ Opt_idel, Opt_imod,
25329+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25330+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25331+ Opt_xino, Opt_noxino,
1facf9fc 25332+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25333+ Opt_trunc_xino_path, Opt_itrunc_xino,
25334+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25335+ Opt_shwh, Opt_noshwh,
1facf9fc 25336+ Opt_plink, Opt_noplink, Opt_list_plink,
25337+ Opt_udba,
4a4d8108 25338+ Opt_dio, Opt_nodio,
1facf9fc 25339+ Opt_diropq_a, Opt_diropq_w,
25340+ Opt_warn_perm, Opt_nowarn_perm,
25341+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25342+ Opt_fhsm_sec,
1facf9fc 25343+ Opt_verbose, Opt_noverbose,
25344+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25345+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25346+ Opt_dirren, Opt_nodirren,
c1595e42 25347+ Opt_acl, Opt_noacl,
1facf9fc 25348+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25349+};
25350+
25351+static match_table_t options = {
25352+ {Opt_br, "br=%s"},
25353+ {Opt_br, "br:%s"},
25354+
25355+ {Opt_add, "add=%d:%s"},
25356+ {Opt_add, "add:%d:%s"},
25357+ {Opt_add, "ins=%d:%s"},
25358+ {Opt_add, "ins:%d:%s"},
25359+ {Opt_append, "append=%s"},
25360+ {Opt_append, "append:%s"},
25361+ {Opt_prepend, "prepend=%s"},
25362+ {Opt_prepend, "prepend:%s"},
25363+
25364+ {Opt_del, "del=%s"},
25365+ {Opt_del, "del:%s"},
25366+ /* {Opt_idel, "idel:%d"}, */
25367+ {Opt_mod, "mod=%s"},
25368+ {Opt_mod, "mod:%s"},
25369+ /* {Opt_imod, "imod:%d:%s"}, */
25370+
25371+ {Opt_dirwh, "dirwh=%d"},
25372+
25373+ {Opt_xino, "xino=%s"},
25374+ {Opt_noxino, "noxino"},
25375+ {Opt_trunc_xino, "trunc_xino"},
25376+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25377+ {Opt_notrunc_xino, "notrunc_xino"},
25378+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25379+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25380+ /* {Opt_zxino, "zxino=%s"}, */
25381+ {Opt_trunc_xib, "trunc_xib"},
25382+ {Opt_notrunc_xib, "notrunc_xib"},
25383+
e49829fe 25384+#ifdef CONFIG_PROC_FS
1facf9fc 25385+ {Opt_plink, "plink"},
e49829fe
JR
25386+#else
25387+ {Opt_ignore_silent, "plink"},
25388+#endif
25389+
1facf9fc 25390+ {Opt_noplink, "noplink"},
e49829fe 25391+
1facf9fc 25392+#ifdef CONFIG_AUFS_DEBUG
25393+ {Opt_list_plink, "list_plink"},
25394+#endif
25395+
25396+ {Opt_udba, "udba=%s"},
25397+
4a4d8108
AM
25398+ {Opt_dio, "dio"},
25399+ {Opt_nodio, "nodio"},
25400+
8b6a4947
AM
25401+#ifdef CONFIG_AUFS_DIRREN
25402+ {Opt_dirren, "dirren"},
25403+ {Opt_nodirren, "nodirren"},
25404+#else
25405+ {Opt_ignore, "dirren"},
25406+ {Opt_ignore_silent, "nodirren"},
25407+#endif
25408+
076b876e
AM
25409+#ifdef CONFIG_AUFS_FHSM
25410+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25411+#else
8b6a4947 25412+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25413+#endif
25414+
1facf9fc 25415+ {Opt_diropq_a, "diropq=always"},
25416+ {Opt_diropq_a, "diropq=a"},
25417+ {Opt_diropq_w, "diropq=whiteouted"},
25418+ {Opt_diropq_w, "diropq=w"},
25419+
25420+ {Opt_warn_perm, "warn_perm"},
25421+ {Opt_nowarn_perm, "nowarn_perm"},
25422+
25423+ /* keep them temporary */
1facf9fc 25424+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25425+ {Opt_ignore, "clean_plink"},
1facf9fc 25426+
dece6358
AM
25427+#ifdef CONFIG_AUFS_SHWH
25428+ {Opt_shwh, "shwh"},
25429+#endif
25430+ {Opt_noshwh, "noshwh"},
25431+
076b876e
AM
25432+ {Opt_dirperm1, "dirperm1"},
25433+ {Opt_nodirperm1, "nodirperm1"},
25434+
1facf9fc 25435+ {Opt_verbose, "verbose"},
25436+ {Opt_verbose, "v"},
25437+ {Opt_noverbose, "noverbose"},
25438+ {Opt_noverbose, "quiet"},
25439+ {Opt_noverbose, "q"},
25440+ {Opt_noverbose, "silent"},
25441+
25442+ {Opt_sum, "sum"},
25443+ {Opt_nosum, "nosum"},
25444+ {Opt_wsum, "wsum"},
25445+
25446+ {Opt_rdcache, "rdcache=%d"},
25447+ {Opt_rdblk, "rdblk=%d"},
dece6358 25448+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25449+ {Opt_rdhash, "rdhash=%d"},
dece6358 25450+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25451+
25452+ {Opt_wbr_create, "create=%s"},
25453+ {Opt_wbr_create, "create_policy=%s"},
25454+ {Opt_wbr_copyup, "cpup=%s"},
25455+ {Opt_wbr_copyup, "copyup=%s"},
25456+ {Opt_wbr_copyup, "copyup_policy=%s"},
25457+
c1595e42
JR
25458+ /* generic VFS flag */
25459+#ifdef CONFIG_FS_POSIX_ACL
25460+ {Opt_acl, "acl"},
25461+ {Opt_noacl, "noacl"},
25462+#else
8b6a4947 25463+ {Opt_ignore, "acl"},
c1595e42
JR
25464+ {Opt_ignore_silent, "noacl"},
25465+#endif
25466+
1facf9fc 25467+ /* internal use for the scripts */
25468+ {Opt_ignore_silent, "si=%s"},
25469+
25470+ {Opt_br, "dirs=%s"},
25471+ {Opt_ignore, "debug=%d"},
25472+ {Opt_ignore, "delete=whiteout"},
25473+ {Opt_ignore, "delete=all"},
25474+ {Opt_ignore, "imap=%s"},
25475+
1308ab2a 25476+ /* temporary workaround, due to old mount(8)? */
25477+ {Opt_ignore_silent, "relatime"},
25478+
1facf9fc 25479+ {Opt_err, NULL}
25480+};
25481+
25482+/* ---------------------------------------------------------------------- */
25483+
076b876e 25484+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25485+{
076b876e
AM
25486+ struct match_token *p;
25487+
25488+ p = tbl;
25489+ while (p->pattern) {
25490+ if (p->token == val)
25491+ return p->pattern;
25492+ p++;
1facf9fc 25493+ }
25494+ BUG();
25495+ return "??";
25496+}
25497+
076b876e
AM
25498+static const char *au_optstr(int *val, match_table_t tbl)
25499+{
25500+ struct match_token *p;
25501+ int v;
25502+
25503+ v = *val;
2000de60
JR
25504+ if (!v)
25505+ goto out;
076b876e 25506+ p = tbl;
2000de60
JR
25507+ while (p->pattern) {
25508+ if (p->token
25509+ && (v & p->token) == p->token) {
076b876e
AM
25510+ *val &= ~p->token;
25511+ return p->pattern;
25512+ }
25513+ p++;
25514+ }
2000de60
JR
25515+
25516+out:
076b876e
AM
25517+ return NULL;
25518+}
25519+
1facf9fc 25520+/* ---------------------------------------------------------------------- */
25521+
1e00d052 25522+static match_table_t brperm = {
1facf9fc 25523+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25524+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25525+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
25526+ {0, NULL}
25527+};
1facf9fc 25528+
86dc4139 25529+static match_table_t brattr = {
076b876e
AM
25530+ /* general */
25531+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25532+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 25533+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 25534+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 25535+#ifdef CONFIG_AUFS_FHSM
076b876e 25536+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
25537+#endif
25538+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
25539+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25540+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25541+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25542+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25543+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25544+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 25545+#endif
076b876e
AM
25546+
25547+ /* ro/rr branch */
1e00d052 25548+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
25549+
25550+ /* rw branch */
25551+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 25552+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 25553+
1e00d052 25554+ {0, NULL}
1facf9fc 25555+};
25556+
1e00d052
AM
25557+static int br_attr_val(char *str, match_table_t table, substring_t args[])
25558+{
25559+ int attr, v;
25560+ char *p;
25561+
25562+ attr = 0;
25563+ do {
25564+ p = strchr(str, '+');
25565+ if (p)
25566+ *p = 0;
25567+ v = match_token(str, table, args);
076b876e
AM
25568+ if (v) {
25569+ if (v & AuBrAttr_CMOO_Mask)
25570+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 25571+ attr |= v;
076b876e 25572+ } else {
1e00d052
AM
25573+ if (p)
25574+ *p = '+';
0c3ec466 25575+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
25576+ break;
25577+ }
25578+ if (p)
25579+ str = p + 1;
25580+ } while (p);
25581+
25582+ return attr;
25583+}
25584+
076b876e
AM
25585+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25586+{
25587+ int sz;
25588+ const char *p;
25589+ char *q;
25590+
076b876e
AM
25591+ q = str->a;
25592+ *q = 0;
25593+ p = au_optstr(&perm, brattr);
25594+ if (p) {
25595+ sz = strlen(p);
25596+ memcpy(q, p, sz + 1);
25597+ q += sz;
25598+ } else
25599+ goto out;
25600+
25601+ do {
25602+ p = au_optstr(&perm, brattr);
25603+ if (p) {
25604+ *q++ = '+';
25605+ sz = strlen(p);
25606+ memcpy(q, p, sz + 1);
25607+ q += sz;
25608+ }
25609+ } while (p);
25610+
25611+out:
c1595e42 25612+ return q - str->a;
076b876e
AM
25613+}
25614+
4a4d8108 25615+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 25616+{
076b876e
AM
25617+ int val, bad, sz;
25618+ char *p;
1facf9fc 25619+ substring_t args[MAX_OPT_ARGS];
076b876e 25620+ au_br_perm_str_t attr;
1facf9fc 25621+
1e00d052
AM
25622+ p = strchr(perm, '+');
25623+ if (p)
25624+ *p = 0;
25625+ val = match_token(perm, brperm, args);
25626+ if (!val) {
25627+ if (p)
25628+ *p = '+';
0c3ec466 25629+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
25630+ val = AuBrPerm_RO;
25631+ goto out;
25632+ }
25633+ if (!p)
25634+ goto out;
25635+
076b876e
AM
25636+ val |= br_attr_val(p + 1, brattr, args);
25637+
25638+ bad = 0;
86dc4139 25639+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
25640+ case AuBrPerm_RO:
25641+ case AuBrPerm_RR:
076b876e
AM
25642+ bad = val & AuBrWAttr_Mask;
25643+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
25644+ break;
25645+ case AuBrPerm_RW:
076b876e
AM
25646+ bad = val & AuBrRAttr_Mask;
25647+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
25648+ break;
25649+ }
c1595e42
JR
25650+
25651+ /*
25652+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25653+ * does not treat it as an error, just warning.
25654+ * this is a tiny guard for the user operation.
25655+ */
25656+ if (val & AuBrAttr_UNPIN) {
25657+ bad |= AuBrAttr_UNPIN;
25658+ val &= ~AuBrAttr_UNPIN;
25659+ }
25660+
076b876e
AM
25661+ if (unlikely(bad)) {
25662+ sz = au_do_optstr_br_attr(&attr, bad);
25663+ AuDebugOn(!sz);
25664+ pr_warn("ignored branch attribute %s\n", attr.a);
25665+ }
1e00d052
AM
25666+
25667+out:
1facf9fc 25668+ return val;
25669+}
25670+
076b876e 25671+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 25672+{
076b876e
AM
25673+ au_br_perm_str_t attr;
25674+ const char *p;
25675+ char *q;
1e00d052
AM
25676+ int sz;
25677+
076b876e
AM
25678+ q = str->a;
25679+ p = au_optstr(&perm, brperm);
25680+ AuDebugOn(!p || !*p);
25681+ sz = strlen(p);
25682+ memcpy(q, p, sz + 1);
25683+ q += sz;
1e00d052 25684+
076b876e
AM
25685+ sz = au_do_optstr_br_attr(&attr, perm);
25686+ if (sz) {
25687+ *q++ = '+';
25688+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
25689+ }
25690+
076b876e 25691+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 25692+}
25693+
25694+/* ---------------------------------------------------------------------- */
25695+
25696+static match_table_t udbalevel = {
25697+ {AuOpt_UDBA_REVAL, "reval"},
25698+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
25699+#ifdef CONFIG_AUFS_HNOTIFY
25700+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
25701+#ifdef CONFIG_AUFS_HFSNOTIFY
25702+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 25703+#endif
1facf9fc 25704+#endif
25705+ {-1, NULL}
25706+};
25707+
4a4d8108 25708+static int noinline_for_stack udba_val(char *str)
1facf9fc 25709+{
25710+ substring_t args[MAX_OPT_ARGS];
25711+
7f207e10 25712+ return match_token(str, udbalevel, args);
1facf9fc 25713+}
25714+
25715+const char *au_optstr_udba(int udba)
25716+{
076b876e 25717+ return au_parser_pattern(udba, udbalevel);
1facf9fc 25718+}
25719+
25720+/* ---------------------------------------------------------------------- */
25721+
25722+static match_table_t au_wbr_create_policy = {
25723+ {AuWbrCreate_TDP, "tdp"},
25724+ {AuWbrCreate_TDP, "top-down-parent"},
25725+ {AuWbrCreate_RR, "rr"},
25726+ {AuWbrCreate_RR, "round-robin"},
25727+ {AuWbrCreate_MFS, "mfs"},
25728+ {AuWbrCreate_MFS, "most-free-space"},
25729+ {AuWbrCreate_MFSV, "mfs:%d"},
25730+ {AuWbrCreate_MFSV, "most-free-space:%d"},
25731+
f2c43d5f
AM
25732+ /* top-down regardless the parent, and then mfs */
25733+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
25734+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
25735+
1facf9fc 25736+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
25737+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
25738+ {AuWbrCreate_PMFS, "pmfs"},
25739+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
25740+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
25741+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 25742+
25743+ {-1, NULL}
25744+};
25745+
1facf9fc 25746+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
25747+ struct au_opt_wbr_create *create)
25748+{
25749+ int err;
25750+ unsigned long long ull;
25751+
25752+ err = 0;
a2654f78 25753+ if (!match_u64(arg, &ull))
1facf9fc 25754+ create->mfsrr_watermark = ull;
25755+ else {
4a4d8108 25756+ pr_err("bad integer in %s\n", str);
1facf9fc 25757+ err = -EINVAL;
25758+ }
25759+
25760+ return err;
25761+}
25762+
25763+static int au_wbr_mfs_sec(substring_t *arg, char *str,
25764+ struct au_opt_wbr_create *create)
25765+{
25766+ int n, err;
25767+
25768+ err = 0;
027c5e7a 25769+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 25770+ create->mfs_second = n;
25771+ else {
4a4d8108 25772+ pr_err("bad integer in %s\n", str);
1facf9fc 25773+ err = -EINVAL;
25774+ }
25775+
25776+ return err;
25777+}
25778+
4a4d8108
AM
25779+static int noinline_for_stack
25780+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 25781+{
25782+ int err, e;
25783+ substring_t args[MAX_OPT_ARGS];
25784+
25785+ err = match_token(str, au_wbr_create_policy, args);
25786+ create->wbr_create = err;
25787+ switch (err) {
25788+ case AuWbrCreate_MFSRRV:
f2c43d5f 25789+ case AuWbrCreate_TDMFSV:
392086de 25790+ case AuWbrCreate_PMFSRRV:
1facf9fc 25791+ e = au_wbr_mfs_wmark(&args[0], str, create);
25792+ if (!e)
25793+ e = au_wbr_mfs_sec(&args[1], str, create);
25794+ if (unlikely(e))
25795+ err = e;
25796+ break;
25797+ case AuWbrCreate_MFSRR:
f2c43d5f 25798+ case AuWbrCreate_TDMFS:
392086de 25799+ case AuWbrCreate_PMFSRR:
1facf9fc 25800+ e = au_wbr_mfs_wmark(&args[0], str, create);
25801+ if (unlikely(e)) {
25802+ err = e;
25803+ break;
25804+ }
25805+ /*FALLTHROUGH*/
25806+ case AuWbrCreate_MFS:
25807+ case AuWbrCreate_PMFS:
027c5e7a 25808+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 25809+ break;
25810+ case AuWbrCreate_MFSV:
25811+ case AuWbrCreate_PMFSV:
25812+ e = au_wbr_mfs_sec(&args[0], str, create);
25813+ if (unlikely(e))
25814+ err = e;
25815+ break;
25816+ }
25817+
25818+ return err;
25819+}
25820+
25821+const char *au_optstr_wbr_create(int wbr_create)
25822+{
076b876e 25823+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 25824+}
25825+
25826+static match_table_t au_wbr_copyup_policy = {
25827+ {AuWbrCopyup_TDP, "tdp"},
25828+ {AuWbrCopyup_TDP, "top-down-parent"},
25829+ {AuWbrCopyup_BUP, "bup"},
25830+ {AuWbrCopyup_BUP, "bottom-up-parent"},
25831+ {AuWbrCopyup_BU, "bu"},
25832+ {AuWbrCopyup_BU, "bottom-up"},
25833+ {-1, NULL}
25834+};
25835+
4a4d8108 25836+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 25837+{
25838+ substring_t args[MAX_OPT_ARGS];
25839+
25840+ return match_token(str, au_wbr_copyup_policy, args);
25841+}
25842+
25843+const char *au_optstr_wbr_copyup(int wbr_copyup)
25844+{
076b876e 25845+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 25846+}
25847+
25848+/* ---------------------------------------------------------------------- */
25849+
25850+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
25851+
25852+static void dump_opts(struct au_opts *opts)
25853+{
25854+#ifdef CONFIG_AUFS_DEBUG
25855+ /* reduce stack space */
25856+ union {
25857+ struct au_opt_add *add;
25858+ struct au_opt_del *del;
25859+ struct au_opt_mod *mod;
25860+ struct au_opt_xino *xino;
25861+ struct au_opt_xino_itrunc *xino_itrunc;
25862+ struct au_opt_wbr_create *create;
25863+ } u;
25864+ struct au_opt *opt;
25865+
25866+ opt = opts->opt;
25867+ while (opt->type != Opt_tail) {
25868+ switch (opt->type) {
25869+ case Opt_add:
25870+ u.add = &opt->add;
25871+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
25872+ u.add->bindex, u.add->pathname, u.add->perm,
25873+ u.add->path.dentry);
25874+ break;
25875+ case Opt_del:
25876+ case Opt_idel:
25877+ u.del = &opt->del;
25878+ AuDbg("del {%s, %p}\n",
25879+ u.del->pathname, u.del->h_path.dentry);
25880+ break;
25881+ case Opt_mod:
25882+ case Opt_imod:
25883+ u.mod = &opt->mod;
25884+ AuDbg("mod {%s, 0x%x, %p}\n",
25885+ u.mod->path, u.mod->perm, u.mod->h_root);
25886+ break;
25887+ case Opt_append:
25888+ u.add = &opt->add;
25889+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
25890+ u.add->bindex, u.add->pathname, u.add->perm,
25891+ u.add->path.dentry);
25892+ break;
25893+ case Opt_prepend:
25894+ u.add = &opt->add;
25895+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
25896+ u.add->bindex, u.add->pathname, u.add->perm,
25897+ u.add->path.dentry);
25898+ break;
25899+ case Opt_dirwh:
25900+ AuDbg("dirwh %d\n", opt->dirwh);
25901+ break;
25902+ case Opt_rdcache:
25903+ AuDbg("rdcache %d\n", opt->rdcache);
25904+ break;
25905+ case Opt_rdblk:
25906+ AuDbg("rdblk %u\n", opt->rdblk);
25907+ break;
dece6358
AM
25908+ case Opt_rdblk_def:
25909+ AuDbg("rdblk_def\n");
25910+ break;
1facf9fc 25911+ case Opt_rdhash:
25912+ AuDbg("rdhash %u\n", opt->rdhash);
25913+ break;
dece6358
AM
25914+ case Opt_rdhash_def:
25915+ AuDbg("rdhash_def\n");
25916+ break;
1facf9fc 25917+ case Opt_xino:
25918+ u.xino = &opt->xino;
523b37e3 25919+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 25920+ break;
25921+ case Opt_trunc_xino:
25922+ AuLabel(trunc_xino);
25923+ break;
25924+ case Opt_notrunc_xino:
25925+ AuLabel(notrunc_xino);
25926+ break;
25927+ case Opt_trunc_xino_path:
25928+ case Opt_itrunc_xino:
25929+ u.xino_itrunc = &opt->xino_itrunc;
25930+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
25931+ break;
1facf9fc 25932+ case Opt_noxino:
25933+ AuLabel(noxino);
25934+ break;
25935+ case Opt_trunc_xib:
25936+ AuLabel(trunc_xib);
25937+ break;
25938+ case Opt_notrunc_xib:
25939+ AuLabel(notrunc_xib);
25940+ break;
dece6358
AM
25941+ case Opt_shwh:
25942+ AuLabel(shwh);
25943+ break;
25944+ case Opt_noshwh:
25945+ AuLabel(noshwh);
25946+ break;
076b876e
AM
25947+ case Opt_dirperm1:
25948+ AuLabel(dirperm1);
25949+ break;
25950+ case Opt_nodirperm1:
25951+ AuLabel(nodirperm1);
25952+ break;
1facf9fc 25953+ case Opt_plink:
25954+ AuLabel(plink);
25955+ break;
25956+ case Opt_noplink:
25957+ AuLabel(noplink);
25958+ break;
25959+ case Opt_list_plink:
25960+ AuLabel(list_plink);
25961+ break;
25962+ case Opt_udba:
25963+ AuDbg("udba %d, %s\n",
25964+ opt->udba, au_optstr_udba(opt->udba));
25965+ break;
4a4d8108
AM
25966+ case Opt_dio:
25967+ AuLabel(dio);
25968+ break;
25969+ case Opt_nodio:
25970+ AuLabel(nodio);
25971+ break;
1facf9fc 25972+ case Opt_diropq_a:
25973+ AuLabel(diropq_a);
25974+ break;
25975+ case Opt_diropq_w:
25976+ AuLabel(diropq_w);
25977+ break;
25978+ case Opt_warn_perm:
25979+ AuLabel(warn_perm);
25980+ break;
25981+ case Opt_nowarn_perm:
25982+ AuLabel(nowarn_perm);
25983+ break;
1facf9fc 25984+ case Opt_verbose:
25985+ AuLabel(verbose);
25986+ break;
25987+ case Opt_noverbose:
25988+ AuLabel(noverbose);
25989+ break;
25990+ case Opt_sum:
25991+ AuLabel(sum);
25992+ break;
25993+ case Opt_nosum:
25994+ AuLabel(nosum);
25995+ break;
25996+ case Opt_wsum:
25997+ AuLabel(wsum);
25998+ break;
25999+ case Opt_wbr_create:
26000+ u.create = &opt->wbr_create;
26001+ AuDbg("create %d, %s\n", u.create->wbr_create,
26002+ au_optstr_wbr_create(u.create->wbr_create));
26003+ switch (u.create->wbr_create) {
26004+ case AuWbrCreate_MFSV:
26005+ case AuWbrCreate_PMFSV:
26006+ AuDbg("%d sec\n", u.create->mfs_second);
26007+ break;
26008+ case AuWbrCreate_MFSRR:
f2c43d5f 26009+ case AuWbrCreate_TDMFS:
1facf9fc 26010+ AuDbg("%llu watermark\n",
26011+ u.create->mfsrr_watermark);
26012+ break;
26013+ case AuWbrCreate_MFSRRV:
f2c43d5f 26014+ case AuWbrCreate_TDMFSV:
392086de 26015+ case AuWbrCreate_PMFSRRV:
1facf9fc 26016+ AuDbg("%llu watermark, %d sec\n",
26017+ u.create->mfsrr_watermark,
26018+ u.create->mfs_second);
26019+ break;
26020+ }
26021+ break;
26022+ case Opt_wbr_copyup:
26023+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26024+ au_optstr_wbr_copyup(opt->wbr_copyup));
26025+ break;
076b876e
AM
26026+ case Opt_fhsm_sec:
26027+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26028+ break;
8b6a4947
AM
26029+ case Opt_dirren:
26030+ AuLabel(dirren);
26031+ break;
26032+ case Opt_nodirren:
26033+ AuLabel(nodirren);
26034+ break;
c1595e42
JR
26035+ case Opt_acl:
26036+ AuLabel(acl);
26037+ break;
26038+ case Opt_noacl:
26039+ AuLabel(noacl);
26040+ break;
1facf9fc 26041+ default:
26042+ BUG();
26043+ }
26044+ opt++;
26045+ }
26046+#endif
26047+}
26048+
26049+void au_opts_free(struct au_opts *opts)
26050+{
26051+ struct au_opt *opt;
26052+
26053+ opt = opts->opt;
26054+ while (opt->type != Opt_tail) {
26055+ switch (opt->type) {
26056+ case Opt_add:
26057+ case Opt_append:
26058+ case Opt_prepend:
26059+ path_put(&opt->add.path);
26060+ break;
26061+ case Opt_del:
26062+ case Opt_idel:
26063+ path_put(&opt->del.h_path);
26064+ break;
26065+ case Opt_mod:
26066+ case Opt_imod:
26067+ dput(opt->mod.h_root);
26068+ break;
26069+ case Opt_xino:
26070+ fput(opt->xino.file);
26071+ break;
26072+ }
26073+ opt++;
26074+ }
26075+}
26076+
26077+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26078+ aufs_bindex_t bindex)
26079+{
26080+ int err;
26081+ struct au_opt_add *add = &opt->add;
26082+ char *p;
26083+
26084+ add->bindex = bindex;
1e00d052 26085+ add->perm = AuBrPerm_RO;
1facf9fc 26086+ add->pathname = opt_str;
26087+ p = strchr(opt_str, '=');
26088+ if (p) {
26089+ *p++ = 0;
26090+ if (*p)
26091+ add->perm = br_perm_val(p);
26092+ }
26093+
26094+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26095+ if (!err) {
26096+ if (!p) {
26097+ add->perm = AuBrPerm_RO;
26098+ if (au_test_fs_rr(add->path.dentry->d_sb))
26099+ add->perm = AuBrPerm_RR;
2121bcd9 26100+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 26101+ add->perm = AuBrPerm_RW;
26102+ }
26103+ opt->type = Opt_add;
26104+ goto out;
26105+ }
4a4d8108 26106+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26107+ err = -EINVAL;
26108+
4f0767ce 26109+out:
1facf9fc 26110+ return err;
26111+}
26112+
26113+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26114+{
26115+ int err;
26116+
26117+ del->pathname = args[0].from;
26118+ AuDbg("del path %s\n", del->pathname);
26119+
26120+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26121+ if (unlikely(err))
4a4d8108 26122+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26123+
26124+ return err;
26125+}
26126+
26127+#if 0 /* reserved for future use */
26128+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26129+ struct au_opt_del *del, substring_t args[])
26130+{
26131+ int err;
26132+ struct dentry *root;
26133+
26134+ err = -EINVAL;
26135+ root = sb->s_root;
26136+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26137+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26138+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26139+ goto out;
26140+ }
26141+
26142+ err = 0;
26143+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26144+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26145+
4f0767ce 26146+out:
1facf9fc 26147+ aufs_read_unlock(root, !AuLock_IR);
26148+ return err;
26149+}
26150+#endif
26151+
4a4d8108
AM
26152+static int noinline_for_stack
26153+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26154+{
26155+ int err;
26156+ struct path path;
26157+ char *p;
26158+
26159+ err = -EINVAL;
26160+ mod->path = args[0].from;
26161+ p = strchr(mod->path, '=');
26162+ if (unlikely(!p)) {
4a4d8108 26163+ pr_err("no permssion %s\n", args[0].from);
1facf9fc 26164+ goto out;
26165+ }
26166+
26167+ *p++ = 0;
26168+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26169+ if (unlikely(err)) {
4a4d8108 26170+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26171+ goto out;
26172+ }
26173+
26174+ mod->perm = br_perm_val(p);
26175+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26176+ mod->h_root = dget(path.dentry);
26177+ path_put(&path);
26178+
4f0767ce 26179+out:
1facf9fc 26180+ return err;
26181+}
26182+
26183+#if 0 /* reserved for future use */
26184+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26185+ struct au_opt_mod *mod, substring_t args[])
26186+{
26187+ int err;
26188+ struct dentry *root;
26189+
26190+ err = -EINVAL;
26191+ root = sb->s_root;
26192+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26193+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26194+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26195+ goto out;
26196+ }
26197+
26198+ err = 0;
26199+ mod->perm = br_perm_val(args[1].from);
26200+ AuDbg("mod path %s, perm 0x%x, %s\n",
26201+ mod->path, mod->perm, args[1].from);
26202+ mod->h_root = dget(au_h_dptr(root, bindex));
26203+
4f0767ce 26204+out:
1facf9fc 26205+ aufs_read_unlock(root, !AuLock_IR);
26206+ return err;
26207+}
26208+#endif
26209+
26210+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26211+ substring_t args[])
26212+{
26213+ int err;
26214+ struct file *file;
26215+
26216+ file = au_xino_create(sb, args[0].from, /*silent*/0);
26217+ err = PTR_ERR(file);
26218+ if (IS_ERR(file))
26219+ goto out;
26220+
26221+ err = -EINVAL;
2000de60 26222+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26223+ fput(file);
4a4d8108 26224+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26225+ goto out;
26226+ }
26227+
26228+ err = 0;
26229+ xino->file = file;
26230+ xino->path = args[0].from;
26231+
4f0767ce 26232+out:
1facf9fc 26233+ return err;
26234+}
26235+
4a4d8108
AM
26236+static int noinline_for_stack
26237+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26238+ struct au_opt_xino_itrunc *xino_itrunc,
26239+ substring_t args[])
1facf9fc 26240+{
26241+ int err;
5afbbe0d 26242+ aufs_bindex_t bbot, bindex;
1facf9fc 26243+ struct path path;
26244+ struct dentry *root;
26245+
26246+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26247+ if (unlikely(err)) {
4a4d8108 26248+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26249+ goto out;
26250+ }
26251+
26252+ xino_itrunc->bindex = -1;
26253+ root = sb->s_root;
26254+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26255+ bbot = au_sbbot(sb);
26256+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26257+ if (au_h_dptr(root, bindex) == path.dentry) {
26258+ xino_itrunc->bindex = bindex;
26259+ break;
26260+ }
26261+ }
26262+ aufs_read_unlock(root, !AuLock_IR);
26263+ path_put(&path);
26264+
26265+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26266+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26267+ err = -EINVAL;
26268+ }
26269+
4f0767ce 26270+out:
1facf9fc 26271+ return err;
26272+}
26273+
26274+/* called without aufs lock */
26275+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26276+{
26277+ int err, n, token;
26278+ aufs_bindex_t bindex;
26279+ unsigned char skipped;
26280+ struct dentry *root;
26281+ struct au_opt *opt, *opt_tail;
26282+ char *opt_str;
26283+ /* reduce the stack space */
26284+ union {
26285+ struct au_opt_xino_itrunc *xino_itrunc;
26286+ struct au_opt_wbr_create *create;
26287+ } u;
26288+ struct {
26289+ substring_t args[MAX_OPT_ARGS];
26290+ } *a;
26291+
26292+ err = -ENOMEM;
26293+ a = kmalloc(sizeof(*a), GFP_NOFS);
26294+ if (unlikely(!a))
26295+ goto out;
26296+
26297+ root = sb->s_root;
26298+ err = 0;
26299+ bindex = 0;
26300+ opt = opts->opt;
26301+ opt_tail = opt + opts->max_opt - 1;
26302+ opt->type = Opt_tail;
26303+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26304+ err = -EINVAL;
26305+ skipped = 0;
26306+ token = match_token(opt_str, options, a->args);
26307+ switch (token) {
26308+ case Opt_br:
26309+ err = 0;
26310+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26311+ && *opt_str) {
26312+ err = opt_add(opt, opt_str, opts->sb_flags,
26313+ bindex++);
26314+ if (unlikely(!err && ++opt > opt_tail)) {
26315+ err = -E2BIG;
26316+ break;
26317+ }
26318+ opt->type = Opt_tail;
26319+ skipped = 1;
26320+ }
26321+ break;
26322+ case Opt_add:
26323+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26324+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26325+ break;
26326+ }
26327+ bindex = n;
26328+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26329+ bindex);
26330+ if (!err)
26331+ opt->type = token;
26332+ break;
26333+ case Opt_append:
26334+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26335+ /*dummy bindex*/1);
26336+ if (!err)
26337+ opt->type = token;
26338+ break;
26339+ case Opt_prepend:
26340+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26341+ /*bindex*/0);
26342+ if (!err)
26343+ opt->type = token;
26344+ break;
26345+ case Opt_del:
26346+ err = au_opts_parse_del(&opt->del, a->args);
26347+ if (!err)
26348+ opt->type = token;
26349+ break;
26350+#if 0 /* reserved for future use */
26351+ case Opt_idel:
26352+ del->pathname = "(indexed)";
26353+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26354+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26355+ break;
26356+ }
26357+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26358+ if (!err)
26359+ opt->type = token;
26360+ break;
26361+#endif
26362+ case Opt_mod:
26363+ err = au_opts_parse_mod(&opt->mod, a->args);
26364+ if (!err)
26365+ opt->type = token;
26366+ break;
26367+#ifdef IMOD /* reserved for future use */
26368+ case Opt_imod:
26369+ u.mod->path = "(indexed)";
26370+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26371+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26372+ break;
26373+ }
26374+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26375+ if (!err)
26376+ opt->type = token;
26377+ break;
26378+#endif
26379+ case Opt_xino:
26380+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26381+ if (!err)
26382+ opt->type = token;
26383+ break;
26384+
26385+ case Opt_trunc_xino_path:
26386+ err = au_opts_parse_xino_itrunc_path
26387+ (sb, &opt->xino_itrunc, a->args);
26388+ if (!err)
26389+ opt->type = token;
26390+ break;
26391+
26392+ case Opt_itrunc_xino:
26393+ u.xino_itrunc = &opt->xino_itrunc;
26394+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26395+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26396+ break;
26397+ }
26398+ u.xino_itrunc->bindex = n;
26399+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26400+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26401+ pr_err("out of bounds, %d\n", n);
1facf9fc 26402+ aufs_read_unlock(root, !AuLock_IR);
26403+ break;
26404+ }
26405+ aufs_read_unlock(root, !AuLock_IR);
26406+ err = 0;
26407+ opt->type = token;
26408+ break;
26409+
26410+ case Opt_dirwh:
26411+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26412+ break;
26413+ err = 0;
26414+ opt->type = token;
26415+ break;
26416+
26417+ case Opt_rdcache:
027c5e7a
AM
26418+ if (unlikely(match_int(&a->args[0], &n))) {
26419+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26420+ break;
027c5e7a
AM
26421+ }
26422+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26423+ pr_err("rdcache must be smaller than %d\n",
26424+ AUFS_RDCACHE_MAX);
26425+ break;
26426+ }
26427+ opt->rdcache = n;
1facf9fc 26428+ err = 0;
26429+ opt->type = token;
26430+ break;
26431+ case Opt_rdblk:
26432+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26433+ || n < 0
1facf9fc 26434+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26435+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26436+ break;
26437+ }
1308ab2a 26438+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26439+ pr_err("rdblk must be larger than %d\n",
26440+ NAME_MAX);
1facf9fc 26441+ break;
26442+ }
26443+ opt->rdblk = n;
26444+ err = 0;
26445+ opt->type = token;
26446+ break;
26447+ case Opt_rdhash:
26448+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26449+ || n < 0
1facf9fc 26450+ || n * sizeof(struct hlist_head)
26451+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26452+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26453+ break;
26454+ }
26455+ opt->rdhash = n;
26456+ err = 0;
26457+ opt->type = token;
26458+ break;
26459+
26460+ case Opt_trunc_xino:
26461+ case Opt_notrunc_xino:
26462+ case Opt_noxino:
26463+ case Opt_trunc_xib:
26464+ case Opt_notrunc_xib:
dece6358
AM
26465+ case Opt_shwh:
26466+ case Opt_noshwh:
076b876e
AM
26467+ case Opt_dirperm1:
26468+ case Opt_nodirperm1:
1facf9fc 26469+ case Opt_plink:
26470+ case Opt_noplink:
26471+ case Opt_list_plink:
4a4d8108
AM
26472+ case Opt_dio:
26473+ case Opt_nodio:
1facf9fc 26474+ case Opt_diropq_a:
26475+ case Opt_diropq_w:
26476+ case Opt_warn_perm:
26477+ case Opt_nowarn_perm:
1facf9fc 26478+ case Opt_verbose:
26479+ case Opt_noverbose:
26480+ case Opt_sum:
26481+ case Opt_nosum:
26482+ case Opt_wsum:
dece6358
AM
26483+ case Opt_rdblk_def:
26484+ case Opt_rdhash_def:
8b6a4947
AM
26485+ case Opt_dirren:
26486+ case Opt_nodirren:
c1595e42
JR
26487+ case Opt_acl:
26488+ case Opt_noacl:
1facf9fc 26489+ err = 0;
26490+ opt->type = token;
26491+ break;
26492+
26493+ case Opt_udba:
26494+ opt->udba = udba_val(a->args[0].from);
26495+ if (opt->udba >= 0) {
26496+ err = 0;
26497+ opt->type = token;
26498+ } else
4a4d8108 26499+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26500+ break;
26501+
26502+ case Opt_wbr_create:
26503+ u.create = &opt->wbr_create;
26504+ u.create->wbr_create
26505+ = au_wbr_create_val(a->args[0].from, u.create);
26506+ if (u.create->wbr_create >= 0) {
26507+ err = 0;
26508+ opt->type = token;
26509+ } else
4a4d8108 26510+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26511+ break;
26512+ case Opt_wbr_copyup:
26513+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26514+ if (opt->wbr_copyup >= 0) {
26515+ err = 0;
26516+ opt->type = token;
26517+ } else
4a4d8108 26518+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26519+ break;
26520+
076b876e
AM
26521+ case Opt_fhsm_sec:
26522+ if (unlikely(match_int(&a->args[0], &n)
26523+ || n < 0)) {
26524+ pr_err("bad integer in %s\n", opt_str);
26525+ break;
26526+ }
26527+ if (sysaufs_brs) {
26528+ opt->fhsm_second = n;
26529+ opt->type = token;
26530+ } else
26531+ pr_warn("ignored %s\n", opt_str);
26532+ err = 0;
26533+ break;
26534+
1facf9fc 26535+ case Opt_ignore:
0c3ec466 26536+ pr_warn("ignored %s\n", opt_str);
1facf9fc 26537+ /*FALLTHROUGH*/
26538+ case Opt_ignore_silent:
26539+ skipped = 1;
26540+ err = 0;
26541+ break;
26542+ case Opt_err:
4a4d8108 26543+ pr_err("unknown option %s\n", opt_str);
1facf9fc 26544+ break;
26545+ }
26546+
26547+ if (!err && !skipped) {
26548+ if (unlikely(++opt > opt_tail)) {
26549+ err = -E2BIG;
26550+ opt--;
26551+ opt->type = Opt_tail;
26552+ break;
26553+ }
26554+ opt->type = Opt_tail;
26555+ }
26556+ }
26557+
1c60b727 26558+ kfree(a);
1facf9fc 26559+ dump_opts(opts);
26560+ if (unlikely(err))
26561+ au_opts_free(opts);
26562+
4f0767ce 26563+out:
1facf9fc 26564+ return err;
26565+}
26566+
26567+static int au_opt_wbr_create(struct super_block *sb,
26568+ struct au_opt_wbr_create *create)
26569+{
26570+ int err;
26571+ struct au_sbinfo *sbinfo;
26572+
dece6358
AM
26573+ SiMustWriteLock(sb);
26574+
1facf9fc 26575+ err = 1; /* handled */
26576+ sbinfo = au_sbi(sb);
26577+ if (sbinfo->si_wbr_create_ops->fin) {
26578+ err = sbinfo->si_wbr_create_ops->fin(sb);
26579+ if (!err)
26580+ err = 1;
26581+ }
26582+
26583+ sbinfo->si_wbr_create = create->wbr_create;
26584+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26585+ switch (create->wbr_create) {
26586+ case AuWbrCreate_MFSRRV:
26587+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
26588+ case AuWbrCreate_TDMFS:
26589+ case AuWbrCreate_TDMFSV:
392086de
AM
26590+ case AuWbrCreate_PMFSRR:
26591+ case AuWbrCreate_PMFSRRV:
1facf9fc 26592+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26593+ /*FALLTHROUGH*/
26594+ case AuWbrCreate_MFS:
26595+ case AuWbrCreate_MFSV:
26596+ case AuWbrCreate_PMFS:
26597+ case AuWbrCreate_PMFSV:
e49829fe
JR
26598+ sbinfo->si_wbr_mfs.mfs_expire
26599+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 26600+ break;
26601+ }
26602+
26603+ if (sbinfo->si_wbr_create_ops->init)
26604+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26605+
26606+ return err;
26607+}
26608+
26609+/*
26610+ * returns,
26611+ * plus: processed without an error
26612+ * zero: unprocessed
26613+ */
26614+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26615+ struct au_opts *opts)
26616+{
26617+ int err;
26618+ struct au_sbinfo *sbinfo;
26619+
dece6358
AM
26620+ SiMustWriteLock(sb);
26621+
1facf9fc 26622+ err = 1; /* handled */
26623+ sbinfo = au_sbi(sb);
26624+ switch (opt->type) {
26625+ case Opt_udba:
26626+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26627+ sbinfo->si_mntflags |= opt->udba;
26628+ opts->given_udba |= opt->udba;
26629+ break;
26630+
26631+ case Opt_plink:
26632+ au_opt_set(sbinfo->si_mntflags, PLINK);
26633+ break;
26634+ case Opt_noplink:
26635+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 26636+ au_plink_put(sb, /*verbose*/1);
1facf9fc 26637+ au_opt_clr(sbinfo->si_mntflags, PLINK);
26638+ break;
26639+ case Opt_list_plink:
26640+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26641+ au_plink_list(sb);
26642+ break;
26643+
4a4d8108
AM
26644+ case Opt_dio:
26645+ au_opt_set(sbinfo->si_mntflags, DIO);
26646+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26647+ break;
26648+ case Opt_nodio:
26649+ au_opt_clr(sbinfo->si_mntflags, DIO);
26650+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26651+ break;
26652+
076b876e
AM
26653+ case Opt_fhsm_sec:
26654+ au_fhsm_set(sbinfo, opt->fhsm_second);
26655+ break;
26656+
1facf9fc 26657+ case Opt_diropq_a:
26658+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26659+ break;
26660+ case Opt_diropq_w:
26661+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26662+ break;
26663+
26664+ case Opt_warn_perm:
26665+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26666+ break;
26667+ case Opt_nowarn_perm:
26668+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26669+ break;
26670+
1facf9fc 26671+ case Opt_verbose:
26672+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
26673+ break;
26674+ case Opt_noverbose:
26675+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26676+ break;
26677+
26678+ case Opt_sum:
26679+ au_opt_set(sbinfo->si_mntflags, SUM);
26680+ break;
26681+ case Opt_wsum:
26682+ au_opt_clr(sbinfo->si_mntflags, SUM);
26683+ au_opt_set(sbinfo->si_mntflags, SUM_W);
26684+ case Opt_nosum:
26685+ au_opt_clr(sbinfo->si_mntflags, SUM);
26686+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
26687+ break;
26688+
26689+ case Opt_wbr_create:
26690+ err = au_opt_wbr_create(sb, &opt->wbr_create);
26691+ break;
26692+ case Opt_wbr_copyup:
26693+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
26694+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26695+ break;
26696+
26697+ case Opt_dirwh:
26698+ sbinfo->si_dirwh = opt->dirwh;
26699+ break;
26700+
26701+ case Opt_rdcache:
e49829fe
JR
26702+ sbinfo->si_rdcache
26703+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 26704+ break;
26705+ case Opt_rdblk:
26706+ sbinfo->si_rdblk = opt->rdblk;
26707+ break;
dece6358
AM
26708+ case Opt_rdblk_def:
26709+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26710+ break;
1facf9fc 26711+ case Opt_rdhash:
26712+ sbinfo->si_rdhash = opt->rdhash;
26713+ break;
dece6358
AM
26714+ case Opt_rdhash_def:
26715+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26716+ break;
26717+
26718+ case Opt_shwh:
26719+ au_opt_set(sbinfo->si_mntflags, SHWH);
26720+ break;
26721+ case Opt_noshwh:
26722+ au_opt_clr(sbinfo->si_mntflags, SHWH);
26723+ break;
1facf9fc 26724+
076b876e
AM
26725+ case Opt_dirperm1:
26726+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
26727+ break;
26728+ case Opt_nodirperm1:
26729+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
26730+ break;
26731+
1facf9fc 26732+ case Opt_trunc_xino:
26733+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
26734+ break;
26735+ case Opt_notrunc_xino:
26736+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
26737+ break;
26738+
26739+ case Opt_trunc_xino_path:
26740+ case Opt_itrunc_xino:
26741+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
26742+ if (!err)
26743+ err = 1;
26744+ break;
26745+
26746+ case Opt_trunc_xib:
26747+ au_fset_opts(opts->flags, TRUNC_XIB);
26748+ break;
26749+ case Opt_notrunc_xib:
26750+ au_fclr_opts(opts->flags, TRUNC_XIB);
26751+ break;
26752+
8b6a4947
AM
26753+ case Opt_dirren:
26754+ err = 1;
26755+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26756+ err = au_dr_opt_set(sb);
26757+ if (!err)
26758+ err = 1;
26759+ }
26760+ if (err == 1)
26761+ au_opt_set(sbinfo->si_mntflags, DIRREN);
26762+ break;
26763+ case Opt_nodirren:
26764+ err = 1;
26765+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26766+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
26767+ DR_FLUSHED));
26768+ if (!err)
26769+ err = 1;
26770+ }
26771+ if (err == 1)
26772+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
26773+ break;
26774+
c1595e42 26775+ case Opt_acl:
2121bcd9 26776+ sb->s_flags |= SB_POSIXACL;
c1595e42
JR
26777+ break;
26778+ case Opt_noacl:
2121bcd9 26779+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
26780+ break;
26781+
1facf9fc 26782+ default:
26783+ err = 0;
26784+ break;
26785+ }
26786+
26787+ return err;
26788+}
26789+
26790+/*
26791+ * returns tri-state.
26792+ * plus: processed without an error
26793+ * zero: unprocessed
26794+ * minus: error
26795+ */
26796+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
26797+ struct au_opts *opts)
26798+{
26799+ int err, do_refresh;
26800+
26801+ err = 0;
26802+ switch (opt->type) {
26803+ case Opt_append:
5afbbe0d 26804+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 26805+ if (opt->add.bindex < 0)
26806+ opt->add.bindex = 0;
26807+ goto add;
26808+ case Opt_prepend:
26809+ opt->add.bindex = 0;
f6b6e03d 26810+ add: /* indented label */
1facf9fc 26811+ case Opt_add:
26812+ err = au_br_add(sb, &opt->add,
26813+ au_ftest_opts(opts->flags, REMOUNT));
26814+ if (!err) {
26815+ err = 1;
027c5e7a 26816+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26817+ }
26818+ break;
26819+
26820+ case Opt_del:
26821+ case Opt_idel:
26822+ err = au_br_del(sb, &opt->del,
26823+ au_ftest_opts(opts->flags, REMOUNT));
26824+ if (!err) {
26825+ err = 1;
26826+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 26827+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26828+ }
26829+ break;
26830+
26831+ case Opt_mod:
26832+ case Opt_imod:
26833+ err = au_br_mod(sb, &opt->mod,
26834+ au_ftest_opts(opts->flags, REMOUNT),
26835+ &do_refresh);
26836+ if (!err) {
26837+ err = 1;
027c5e7a
AM
26838+ if (do_refresh)
26839+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26840+ }
26841+ break;
26842+ }
1facf9fc 26843+ return err;
26844+}
26845+
26846+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
26847+ struct au_opt_xino **opt_xino,
26848+ struct au_opts *opts)
26849+{
26850+ int err;
5afbbe0d 26851+ aufs_bindex_t bbot, bindex;
1facf9fc 26852+ struct dentry *root, *parent, *h_root;
26853+
26854+ err = 0;
26855+ switch (opt->type) {
26856+ case Opt_xino:
26857+ err = au_xino_set(sb, &opt->xino,
26858+ !!au_ftest_opts(opts->flags, REMOUNT));
26859+ if (unlikely(err))
26860+ break;
26861+
26862+ *opt_xino = &opt->xino;
26863+ au_xino_brid_set(sb, -1);
26864+
26865+ /* safe d_parent access */
2000de60 26866+ parent = opt->xino.file->f_path.dentry->d_parent;
1facf9fc 26867+ root = sb->s_root;
5afbbe0d
AM
26868+ bbot = au_sbbot(sb);
26869+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26870+ h_root = au_h_dptr(root, bindex);
26871+ if (h_root == parent) {
26872+ au_xino_brid_set(sb, au_sbr_id(sb, bindex));
26873+ break;
26874+ }
26875+ }
26876+ break;
26877+
26878+ case Opt_noxino:
26879+ au_xino_clr(sb);
26880+ au_xino_brid_set(sb, -1);
26881+ *opt_xino = (void *)-1;
26882+ break;
26883+ }
26884+
26885+ return err;
26886+}
26887+
26888+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
26889+ unsigned int pending)
26890+{
076b876e 26891+ int err, fhsm;
5afbbe0d 26892+ aufs_bindex_t bindex, bbot;
79b8bda9 26893+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 26894+ struct au_branch *br;
26895+ struct au_wbr *wbr;
79b8bda9 26896+ struct dentry *root, *dentry;
1facf9fc 26897+ struct inode *dir, *h_dir;
26898+ struct au_sbinfo *sbinfo;
26899+ struct au_hinode *hdir;
26900+
dece6358
AM
26901+ SiMustAnyLock(sb);
26902+
1facf9fc 26903+ sbinfo = au_sbi(sb);
26904+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
26905+
2121bcd9 26906+ if (!(sb_flags & SB_RDONLY)) {
dece6358 26907+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 26908+ pr_warn("first branch should be rw\n");
dece6358 26909+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 26910+ pr_warn_once("shwh should be used with ro\n");
dece6358 26911+ }
1facf9fc 26912+
4a4d8108 26913+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 26914+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 26915+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 26916+
076b876e 26917+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
26918+ pr_warn_once("dirperm1 breaks the protection"
26919+ " by the permission bits on the lower branch\n");
076b876e 26920+
1facf9fc 26921+ err = 0;
076b876e 26922+ fhsm = 0;
1facf9fc 26923+ root = sb->s_root;
5527c038 26924+ dir = d_inode(root);
1facf9fc 26925+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
26926+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
26927+ UDBA_NONE);
5afbbe0d
AM
26928+ bbot = au_sbbot(sb);
26929+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 26930+ skip = 0;
26931+ h_dir = au_h_iptr(dir, bindex);
26932+ br = au_sbr(sb, bindex);
1facf9fc 26933+
c1595e42
JR
26934+ if ((br->br_perm & AuBrAttr_ICEX)
26935+ && !h_dir->i_op->listxattr)
26936+ br->br_perm &= ~AuBrAttr_ICEX;
26937+#if 0
26938+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 26939+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
26940+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
26941+#endif
26942+
26943+ do_free = 0;
1facf9fc 26944+ wbr = br->br_wbr;
26945+ if (wbr)
26946+ wbr_wh_read_lock(wbr);
26947+
1e00d052 26948+ if (!au_br_writable(br->br_perm)) {
1facf9fc 26949+ do_free = !!wbr;
26950+ skip = (!wbr
26951+ || (!wbr->wbr_whbase
26952+ && !wbr->wbr_plink
26953+ && !wbr->wbr_orph));
1e00d052 26954+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 26955+ /* skip = (!br->br_whbase && !br->br_orph); */
26956+ skip = (!wbr || !wbr->wbr_whbase);
26957+ if (skip && wbr) {
26958+ if (do_plink)
26959+ skip = !!wbr->wbr_plink;
26960+ else
26961+ skip = !wbr->wbr_plink;
26962+ }
1e00d052 26963+ } else {
1facf9fc 26964+ /* skip = (br->br_whbase && br->br_ohph); */
26965+ skip = (wbr && wbr->wbr_whbase);
26966+ if (skip) {
26967+ if (do_plink)
26968+ skip = !!wbr->wbr_plink;
26969+ else
26970+ skip = !wbr->wbr_plink;
26971+ }
1facf9fc 26972+ }
26973+ if (wbr)
26974+ wbr_wh_read_unlock(wbr);
26975+
79b8bda9
AM
26976+ if (can_no_dreval) {
26977+ dentry = br->br_path.dentry;
26978+ spin_lock(&dentry->d_lock);
26979+ if (dentry->d_flags &
26980+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
26981+ can_no_dreval = 0;
26982+ spin_unlock(&dentry->d_lock);
26983+ }
26984+
076b876e
AM
26985+ if (au_br_fhsm(br->br_perm)) {
26986+ fhsm++;
26987+ AuDebugOn(!br->br_fhsm);
26988+ }
26989+
1facf9fc 26990+ if (skip)
26991+ continue;
26992+
26993+ hdir = au_hi(dir, bindex);
5afbbe0d 26994+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 26995+ if (wbr)
26996+ wbr_wh_write_lock(wbr);
86dc4139 26997+ err = au_wh_init(br, sb);
1facf9fc 26998+ if (wbr)
26999+ wbr_wh_write_unlock(wbr);
5afbbe0d 27000+ au_hn_inode_unlock(hdir);
1facf9fc 27001+
27002+ if (!err && do_free) {
1c60b727 27003+ kfree(wbr);
1facf9fc 27004+ br->br_wbr = NULL;
27005+ }
27006+ }
27007+
79b8bda9
AM
27008+ if (can_no_dreval)
27009+ au_fset_si(sbinfo, NO_DREVAL);
27010+ else
27011+ au_fclr_si(sbinfo, NO_DREVAL);
27012+
c1595e42 27013+ if (fhsm >= 2) {
076b876e 27014+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27015+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27016+ br = au_sbr(sb, bindex);
27017+ if (au_br_fhsm(br->br_perm)) {
27018+ au_fhsm_set_bottom(sb, bindex);
27019+ break;
27020+ }
27021+ }
27022+ } else {
076b876e 27023+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27024+ au_fhsm_set_bottom(sb, -1);
27025+ }
076b876e 27026+
1facf9fc 27027+ return err;
27028+}
27029+
27030+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27031+{
27032+ int err;
27033+ unsigned int tmp;
5afbbe0d 27034+ aufs_bindex_t bindex, bbot;
1facf9fc 27035+ struct au_opt *opt;
27036+ struct au_opt_xino *opt_xino, xino;
27037+ struct au_sbinfo *sbinfo;
027c5e7a 27038+ struct au_branch *br;
076b876e 27039+ struct inode *dir;
1facf9fc 27040+
dece6358
AM
27041+ SiMustWriteLock(sb);
27042+
1facf9fc 27043+ err = 0;
27044+ opt_xino = NULL;
27045+ opt = opts->opt;
27046+ while (err >= 0 && opt->type != Opt_tail)
27047+ err = au_opt_simple(sb, opt++, opts);
27048+ if (err > 0)
27049+ err = 0;
27050+ else if (unlikely(err < 0))
27051+ goto out;
27052+
27053+ /* disable xino and udba temporary */
27054+ sbinfo = au_sbi(sb);
27055+ tmp = sbinfo->si_mntflags;
27056+ au_opt_clr(sbinfo->si_mntflags, XINO);
27057+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27058+
27059+ opt = opts->opt;
27060+ while (err >= 0 && opt->type != Opt_tail)
27061+ err = au_opt_br(sb, opt++, opts);
27062+ if (err > 0)
27063+ err = 0;
27064+ else if (unlikely(err < 0))
27065+ goto out;
27066+
5afbbe0d
AM
27067+ bbot = au_sbbot(sb);
27068+ if (unlikely(bbot < 0)) {
1facf9fc 27069+ err = -EINVAL;
4a4d8108 27070+ pr_err("no branches\n");
1facf9fc 27071+ goto out;
27072+ }
27073+
27074+ if (au_opt_test(tmp, XINO))
27075+ au_opt_set(sbinfo->si_mntflags, XINO);
27076+ opt = opts->opt;
27077+ while (!err && opt->type != Opt_tail)
27078+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27079+ if (unlikely(err))
27080+ goto out;
27081+
27082+ err = au_opts_verify(sb, sb->s_flags, tmp);
27083+ if (unlikely(err))
27084+ goto out;
27085+
27086+ /* restore xino */
27087+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27088+ xino.file = au_xino_def(sb);
27089+ err = PTR_ERR(xino.file);
27090+ if (IS_ERR(xino.file))
27091+ goto out;
27092+
27093+ err = au_xino_set(sb, &xino, /*remount*/0);
27094+ fput(xino.file);
27095+ if (unlikely(err))
27096+ goto out;
27097+ }
27098+
27099+ /* restore udba */
027c5e7a 27100+ tmp &= AuOptMask_UDBA;
1facf9fc 27101+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27102+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27103+ bbot = au_sbbot(sb);
27104+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27105+ br = au_sbr(sb, bindex);
27106+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27107+ if (unlikely(err))
27108+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27109+ bindex, err);
27110+ /* go on even if err */
27111+ }
4a4d8108 27112+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27113+ dir = d_inode(sb->s_root);
4a4d8108 27114+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27115+ }
27116+
4f0767ce 27117+out:
1facf9fc 27118+ return err;
27119+}
27120+
27121+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27122+{
27123+ int err, rerr;
79b8bda9 27124+ unsigned char no_dreval;
1facf9fc 27125+ struct inode *dir;
27126+ struct au_opt_xino *opt_xino;
27127+ struct au_opt *opt;
27128+ struct au_sbinfo *sbinfo;
27129+
dece6358
AM
27130+ SiMustWriteLock(sb);
27131+
8b6a4947
AM
27132+ err = au_dr_opt_flush(sb);
27133+ if (unlikely(err))
27134+ goto out;
27135+ au_fset_opts(opts->flags, DR_FLUSHED);
27136+
5527c038 27137+ dir = d_inode(sb->s_root);
1facf9fc 27138+ sbinfo = au_sbi(sb);
1facf9fc 27139+ opt_xino = NULL;
27140+ opt = opts->opt;
27141+ while (err >= 0 && opt->type != Opt_tail) {
27142+ err = au_opt_simple(sb, opt, opts);
27143+ if (!err)
27144+ err = au_opt_br(sb, opt, opts);
27145+ if (!err)
27146+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27147+ opt++;
27148+ }
27149+ if (err > 0)
27150+ err = 0;
27151+ AuTraceErr(err);
27152+ /* go on even err */
27153+
79b8bda9 27154+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27155+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27156+ if (unlikely(rerr && !err))
27157+ err = rerr;
27158+
79b8bda9 27159+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27160+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27161+
1facf9fc 27162+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27163+ rerr = au_xib_trunc(sb);
27164+ if (unlikely(rerr && !err))
27165+ err = rerr;
27166+ }
27167+
27168+ /* will be handled by the caller */
027c5e7a 27169+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27170+ && (opts->given_udba
27171+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27172+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27173+ ))
027c5e7a 27174+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27175+
27176+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27177+
27178+out:
1facf9fc 27179+ return err;
27180+}
27181+
27182+/* ---------------------------------------------------------------------- */
27183+
27184+unsigned int au_opt_udba(struct super_block *sb)
27185+{
27186+ return au_mntflags(sb) & AuOptMask_UDBA;
27187+}
7f207e10
AM
27188diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27189--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 27190+++ linux/fs/aufs/opts.h 2018-04-15 08:49:13.401150731 +0200
8b6a4947 27191@@ -0,0 +1,224 @@
1facf9fc 27192+/*
b00004a5 27193+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 27194+ *
27195+ * This program, aufs is free software; you can redistribute it and/or modify
27196+ * it under the terms of the GNU General Public License as published by
27197+ * the Free Software Foundation; either version 2 of the License, or
27198+ * (at your option) any later version.
dece6358
AM
27199+ *
27200+ * This program is distributed in the hope that it will be useful,
27201+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27202+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27203+ * GNU General Public License for more details.
27204+ *
27205+ * You should have received a copy of the GNU General Public License
523b37e3 27206+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27207+ */
27208+
27209+/*
27210+ * mount options/flags
27211+ */
27212+
27213+#ifndef __AUFS_OPTS_H__
27214+#define __AUFS_OPTS_H__
27215+
27216+#ifdef __KERNEL__
27217+
dece6358 27218+#include <linux/path.h>
1facf9fc 27219+
dece6358 27220+struct file;
dece6358 27221+
1facf9fc 27222+/* ---------------------------------------------------------------------- */
27223+
27224+/* mount flags */
27225+#define AuOpt_XINO 1 /* external inode number bitmap
27226+ and translation table */
27227+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27228+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27229+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27230+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27231+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27232+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27233+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27234+ bits */
dece6358
AM
27235+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27236+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27237+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27238+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27239+#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
4a4d8108 27240+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27241+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27242+
4a4d8108
AM
27243+#ifndef CONFIG_AUFS_HNOTIFY
27244+#undef AuOpt_UDBA_HNOTIFY
27245+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27246+#endif
8b6a4947
AM
27247+#ifndef CONFIG_AUFS_DIRREN
27248+#undef AuOpt_DIRREN
27249+#define AuOpt_DIRREN 0
27250+#endif
dece6358
AM
27251+#ifndef CONFIG_AUFS_SHWH
27252+#undef AuOpt_SHWH
27253+#define AuOpt_SHWH 0
27254+#endif
1facf9fc 27255+
27256+#define AuOpt_Def (AuOpt_XINO \
27257+ | AuOpt_UDBA_REVAL \
27258+ | AuOpt_PLINK \
27259+ /* | AuOpt_DIRPERM1 */ \
27260+ | AuOpt_WARN_PERM)
27261+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27262+ | AuOpt_UDBA_REVAL \
4a4d8108 27263+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27264+
27265+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27266+#define au_opt_set(flags, name) do { \
27267+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27268+ ((flags) |= AuOpt_##name); \
27269+} while (0)
27270+#define au_opt_set_udba(flags, name) do { \
27271+ (flags) &= ~AuOptMask_UDBA; \
27272+ ((flags) |= AuOpt_##name); \
27273+} while (0)
7f207e10
AM
27274+#define au_opt_clr(flags, name) do { \
27275+ ((flags) &= ~AuOpt_##name); \
27276+} while (0)
1facf9fc 27277+
e49829fe
JR
27278+static inline unsigned int au_opts_plink(unsigned int mntflags)
27279+{
27280+#ifdef CONFIG_PROC_FS
27281+ return mntflags;
27282+#else
27283+ return mntflags & ~AuOpt_PLINK;
27284+#endif
27285+}
27286+
1facf9fc 27287+/* ---------------------------------------------------------------------- */
27288+
27289+/* policies to select one among multiple writable branches */
27290+enum {
27291+ AuWbrCreate_TDP, /* top down parent */
27292+ AuWbrCreate_RR, /* round robin */
27293+ AuWbrCreate_MFS, /* most free space */
27294+ AuWbrCreate_MFSV, /* mfs with seconds */
27295+ AuWbrCreate_MFSRR, /* mfs then rr */
27296+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27297+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27298+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27299+ AuWbrCreate_PMFS, /* parent and mfs */
27300+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27301+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27302+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27303+
27304+ AuWbrCreate_Def = AuWbrCreate_TDP
27305+};
27306+
27307+enum {
27308+ AuWbrCopyup_TDP, /* top down parent */
27309+ AuWbrCopyup_BUP, /* bottom up parent */
27310+ AuWbrCopyup_BU, /* bottom up */
27311+
27312+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27313+};
27314+
27315+/* ---------------------------------------------------------------------- */
27316+
27317+struct au_opt_add {
27318+ aufs_bindex_t bindex;
27319+ char *pathname;
27320+ int perm;
27321+ struct path path;
27322+};
27323+
27324+struct au_opt_del {
27325+ char *pathname;
27326+ struct path h_path;
27327+};
27328+
27329+struct au_opt_mod {
27330+ char *path;
27331+ int perm;
27332+ struct dentry *h_root;
27333+};
27334+
27335+struct au_opt_xino {
27336+ char *path;
27337+ struct file *file;
27338+};
27339+
27340+struct au_opt_xino_itrunc {
27341+ aufs_bindex_t bindex;
27342+};
27343+
27344+struct au_opt_wbr_create {
27345+ int wbr_create;
27346+ int mfs_second;
27347+ unsigned long long mfsrr_watermark;
27348+};
27349+
27350+struct au_opt {
27351+ int type;
27352+ union {
27353+ struct au_opt_xino xino;
27354+ struct au_opt_xino_itrunc xino_itrunc;
27355+ struct au_opt_add add;
27356+ struct au_opt_del del;
27357+ struct au_opt_mod mod;
27358+ int dirwh;
27359+ int rdcache;
27360+ unsigned int rdblk;
27361+ unsigned int rdhash;
27362+ int udba;
27363+ struct au_opt_wbr_create wbr_create;
27364+ int wbr_copyup;
076b876e 27365+ unsigned int fhsm_second;
1facf9fc 27366+ };
27367+};
27368+
27369+/* opts flags */
27370+#define AuOpts_REMOUNT 1
027c5e7a
AM
27371+#define AuOpts_REFRESH (1 << 1)
27372+#define AuOpts_TRUNC_XIB (1 << 2)
27373+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27374+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27375+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27376+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27377+#define au_fset_opts(flags, name) \
27378+ do { (flags) |= AuOpts_##name; } while (0)
27379+#define au_fclr_opts(flags, name) \
27380+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27381+
8b6a4947
AM
27382+#ifndef CONFIG_AUFS_DIRREN
27383+#undef AuOpts_DR_FLUSHED
27384+#define AuOpts_DR_FLUSHED 0
27385+#endif
27386+
1facf9fc 27387+struct au_opts {
27388+ struct au_opt *opt;
27389+ int max_opt;
27390+
27391+ unsigned int given_udba;
27392+ unsigned int flags;
27393+ unsigned long sb_flags;
27394+};
27395+
27396+/* ---------------------------------------------------------------------- */
27397+
7e9cd9fe 27398+/* opts.c */
076b876e 27399+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27400+const char *au_optstr_udba(int udba);
27401+const char *au_optstr_wbr_copyup(int wbr_copyup);
27402+const char *au_optstr_wbr_create(int wbr_create);
27403+
27404+void au_opts_free(struct au_opts *opts);
3c1bdaff 27405+struct super_block;
1facf9fc 27406+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27407+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27408+ unsigned int pending);
27409+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27410+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27411+
27412+unsigned int au_opt_udba(struct super_block *sb);
27413+
1facf9fc 27414+#endif /* __KERNEL__ */
27415+#endif /* __AUFS_OPTS_H__ */
7f207e10
AM
27416diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27417--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 27418+++ linux/fs/aufs/plink.c 2018-06-04 09:08:09.188079511 +0200
8b6a4947 27419@@ -0,0 +1,515 @@
1facf9fc 27420+/*
b00004a5 27421+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 27422+ *
27423+ * This program, aufs is free software; you can redistribute it and/or modify
27424+ * it under the terms of the GNU General Public License as published by
27425+ * the Free Software Foundation; either version 2 of the License, or
27426+ * (at your option) any later version.
dece6358
AM
27427+ *
27428+ * This program is distributed in the hope that it will be useful,
27429+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27430+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27431+ * GNU General Public License for more details.
27432+ *
27433+ * You should have received a copy of the GNU General Public License
523b37e3 27434+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27435+ */
27436+
27437+/*
27438+ * pseudo-link
27439+ */
27440+
27441+#include "aufs.h"
27442+
27443+/*
e49829fe 27444+ * the pseudo-link maintenance mode.
1facf9fc 27445+ * during a user process maintains the pseudo-links,
27446+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27447+ *
27448+ * Flags
27449+ * NOPLM:
27450+ * For entry functions which will handle plink, and i_mutex is already held
27451+ * in VFS.
27452+ * They cannot wait and should return an error at once.
27453+ * Callers has to check the error.
27454+ * NOPLMW:
27455+ * For entry functions which will handle plink, but i_mutex is not held
27456+ * in VFS.
27457+ * They can wait the plink maintenance mode to finish.
27458+ *
27459+ * They behave like F_SETLK and F_SETLKW.
27460+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27461+ */
e49829fe
JR
27462+
27463+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27464+{
e49829fe
JR
27465+ int err;
27466+ pid_t pid, ppid;
f0c0a007 27467+ struct task_struct *parent, *prev;
e49829fe 27468+ struct au_sbinfo *sbi;
dece6358
AM
27469+
27470+ SiMustAnyLock(sb);
27471+
e49829fe
JR
27472+ err = 0;
27473+ if (!au_opt_test(au_mntflags(sb), PLINK))
27474+ goto out;
27475+
27476+ sbi = au_sbi(sb);
27477+ pid = sbi->si_plink_maint_pid;
27478+ if (!pid || pid == current->pid)
27479+ goto out;
27480+
27481+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27482+ prev = NULL;
27483+ parent = current;
27484+ ppid = 0;
e49829fe 27485+ rcu_read_lock();
f0c0a007
AM
27486+ while (1) {
27487+ parent = rcu_dereference(parent->real_parent);
27488+ if (parent == prev)
27489+ break;
27490+ ppid = task_pid_vnr(parent);
27491+ if (pid == ppid) {
27492+ rcu_read_unlock();
27493+ goto out;
27494+ }
27495+ prev = parent;
27496+ }
e49829fe 27497+ rcu_read_unlock();
e49829fe
JR
27498+
27499+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27500+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27501+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27502+ while (sbi->si_plink_maint_pid) {
27503+ si_read_unlock(sb);
27504+ /* gave up wake_up_bit() */
27505+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27506+
27507+ if (au_ftest_lock(flags, FLUSH))
27508+ au_nwt_flush(&sbi->si_nowait);
27509+ si_noflush_read_lock(sb);
27510+ }
27511+ } else if (au_ftest_lock(flags, NOPLM)) {
27512+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27513+ err = -EAGAIN;
27514+ }
27515+
27516+out:
27517+ return err;
4a4d8108
AM
27518+}
27519+
e49829fe 27520+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 27521+{
4a4d8108 27522+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 27523+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 27524+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 27525+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
27526+}
27527+
e49829fe 27528+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
27529+{
27530+ int err;
4a4d8108
AM
27531+ struct au_sbinfo *sbinfo;
27532+
27533+ err = 0;
4a4d8108
AM
27534+ sbinfo = au_sbi(sb);
27535+ /* make sure i am the only one in this fs */
e49829fe
JR
27536+ si_write_lock(sb, AuLock_FLUSH);
27537+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27538+ spin_lock(&sbinfo->si_plink_maint_lock);
27539+ if (!sbinfo->si_plink_maint_pid)
27540+ sbinfo->si_plink_maint_pid = current->pid;
27541+ else
27542+ err = -EBUSY;
27543+ spin_unlock(&sbinfo->si_plink_maint_lock);
27544+ }
4a4d8108
AM
27545+ si_write_unlock(sb);
27546+
27547+ return err;
1facf9fc 27548+}
27549+
27550+/* ---------------------------------------------------------------------- */
27551+
1facf9fc 27552+#ifdef CONFIG_AUFS_DEBUG
27553+void au_plink_list(struct super_block *sb)
27554+{
86dc4139 27555+ int i;
1facf9fc 27556+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27557+ struct hlist_bl_head *hbl;
27558+ struct hlist_bl_node *pos;
5afbbe0d 27559+ struct au_icntnr *icntnr;
1facf9fc 27560+
dece6358
AM
27561+ SiMustAnyLock(sb);
27562+
1facf9fc 27563+ sbinfo = au_sbi(sb);
27564+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27565+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27566+
86dc4139 27567+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27568+ hbl = sbinfo->si_plink + i;
27569+ hlist_bl_lock(hbl);
27570+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27571+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 27572+ hlist_bl_unlock(hbl);
86dc4139 27573+ }
1facf9fc 27574+}
27575+#endif
27576+
27577+/* is the inode pseudo-linked? */
27578+int au_plink_test(struct inode *inode)
27579+{
86dc4139 27580+ int found, i;
1facf9fc 27581+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27582+ struct hlist_bl_head *hbl;
27583+ struct hlist_bl_node *pos;
5afbbe0d 27584+ struct au_icntnr *icntnr;
1facf9fc 27585+
27586+ sbinfo = au_sbi(inode->i_sb);
dece6358 27587+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 27588+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 27589+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 27590+
27591+ found = 0;
86dc4139 27592+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
27593+ hbl = sbinfo->si_plink + i;
27594+ hlist_bl_lock(hbl);
27595+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27596+ if (&icntnr->vfs_inode == inode) {
1facf9fc 27597+ found = 1;
27598+ break;
27599+ }
8b6a4947 27600+ hlist_bl_unlock(hbl);
1facf9fc 27601+ return found;
27602+}
27603+
27604+/* ---------------------------------------------------------------------- */
27605+
27606+/*
27607+ * generate a name for plink.
27608+ * the file will be stored under AUFS_WH_PLINKDIR.
27609+ */
27610+/* 20 is max digits length of ulong 64 */
27611+#define PLINK_NAME_LEN ((20 + 1) * 2)
27612+
27613+static int plink_name(char *name, int len, struct inode *inode,
27614+ aufs_bindex_t bindex)
27615+{
27616+ int rlen;
27617+ struct inode *h_inode;
27618+
27619+ h_inode = au_h_iptr(inode, bindex);
27620+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27621+ return rlen;
27622+}
27623+
7f207e10
AM
27624+struct au_do_plink_lkup_args {
27625+ struct dentry **errp;
27626+ struct qstr *tgtname;
27627+ struct dentry *h_parent;
27628+ struct au_branch *br;
27629+};
27630+
27631+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27632+ struct dentry *h_parent,
27633+ struct au_branch *br)
27634+{
27635+ struct dentry *h_dentry;
febd17d6 27636+ struct inode *h_inode;
7f207e10 27637+
febd17d6 27638+ h_inode = d_inode(h_parent);
be118d29 27639+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 27640+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 27641+ inode_unlock_shared(h_inode);
7f207e10
AM
27642+ return h_dentry;
27643+}
27644+
27645+static void au_call_do_plink_lkup(void *args)
27646+{
27647+ struct au_do_plink_lkup_args *a = args;
27648+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27649+}
27650+
1facf9fc 27651+/* lookup the plink-ed @inode under the branch at @bindex */
27652+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27653+{
27654+ struct dentry *h_dentry, *h_parent;
27655+ struct au_branch *br;
7f207e10 27656+ int wkq_err;
1facf9fc 27657+ char a[PLINK_NAME_LEN];
0c3ec466 27658+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 27659+
e49829fe
JR
27660+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27661+
1facf9fc 27662+ br = au_sbr(inode->i_sb, bindex);
27663+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 27664+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27665+
2dfbb274 27666+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
27667+ struct au_do_plink_lkup_args args = {
27668+ .errp = &h_dentry,
27669+ .tgtname = &tgtname,
27670+ .h_parent = h_parent,
27671+ .br = br
27672+ };
27673+
27674+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27675+ if (unlikely(wkq_err))
27676+ h_dentry = ERR_PTR(wkq_err);
27677+ } else
27678+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27679+
1facf9fc 27680+ return h_dentry;
27681+}
27682+
27683+/* create a pseudo-link */
27684+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27685+ struct dentry *h_dentry, struct au_branch *br)
27686+{
27687+ int err;
27688+ struct path h_path = {
86dc4139 27689+ .mnt = au_br_mnt(br)
1facf9fc 27690+ };
523b37e3 27691+ struct inode *h_dir, *delegated;
1facf9fc 27692+
5527c038 27693+ h_dir = d_inode(h_parent);
febd17d6 27694+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 27695+again:
b4510431 27696+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 27697+ err = PTR_ERR(h_path.dentry);
27698+ if (IS_ERR(h_path.dentry))
27699+ goto out;
27700+
27701+ err = 0;
27702+ /* wh.plink dir is not monitored */
7f207e10 27703+ /* todo: is it really safe? */
5527c038
JR
27704+ if (d_is_positive(h_path.dentry)
27705+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
27706+ delegated = NULL;
27707+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27708+ if (unlikely(err == -EWOULDBLOCK)) {
27709+ pr_warn("cannot retry for NFSv4 delegation"
27710+ " for an internal unlink\n");
27711+ iput(delegated);
27712+ }
1facf9fc 27713+ dput(h_path.dentry);
27714+ h_path.dentry = NULL;
27715+ if (!err)
27716+ goto again;
27717+ }
5527c038 27718+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
27719+ delegated = NULL;
27720+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
27721+ if (unlikely(err == -EWOULDBLOCK)) {
27722+ pr_warn("cannot retry for NFSv4 delegation"
27723+ " for an internal link\n");
27724+ iput(delegated);
27725+ }
27726+ }
1facf9fc 27727+ dput(h_path.dentry);
27728+
4f0767ce 27729+out:
febd17d6 27730+ inode_unlock(h_dir);
1facf9fc 27731+ return err;
27732+}
27733+
27734+struct do_whplink_args {
27735+ int *errp;
27736+ struct qstr *tgt;
27737+ struct dentry *h_parent;
27738+ struct dentry *h_dentry;
27739+ struct au_branch *br;
27740+};
27741+
27742+static void call_do_whplink(void *args)
27743+{
27744+ struct do_whplink_args *a = args;
27745+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
27746+}
27747+
27748+static int whplink(struct dentry *h_dentry, struct inode *inode,
27749+ aufs_bindex_t bindex, struct au_branch *br)
27750+{
27751+ int err, wkq_err;
27752+ struct au_wbr *wbr;
27753+ struct dentry *h_parent;
1facf9fc 27754+ char a[PLINK_NAME_LEN];
0c3ec466 27755+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 27756+
27757+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
27758+ h_parent = wbr->wbr_plink;
1facf9fc 27759+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27760+
27761+ /* always superio. */
2dfbb274 27762+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 27763+ struct do_whplink_args args = {
27764+ .errp = &err,
27765+ .tgt = &tgtname,
27766+ .h_parent = h_parent,
27767+ .h_dentry = h_dentry,
27768+ .br = br
27769+ };
27770+ wkq_err = au_wkq_wait(call_do_whplink, &args);
27771+ if (unlikely(wkq_err))
27772+ err = wkq_err;
27773+ } else
27774+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 27775+
27776+ return err;
27777+}
27778+
1facf9fc 27779+/*
27780+ * create a new pseudo-link for @h_dentry on @bindex.
27781+ * the linked inode is held in aufs @inode.
27782+ */
27783+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
27784+ struct dentry *h_dentry)
27785+{
27786+ struct super_block *sb;
27787+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27788+ struct hlist_bl_head *hbl;
27789+ struct hlist_bl_node *pos;
5afbbe0d 27790+ struct au_icntnr *icntnr;
86dc4139 27791+ int found, err, cnt, i;
1facf9fc 27792+
27793+ sb = inode->i_sb;
27794+ sbinfo = au_sbi(sb);
27795+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27796+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27797+
86dc4139 27798+ found = au_plink_test(inode);
4a4d8108 27799+ if (found)
1facf9fc 27800+ return;
4a4d8108 27801+
86dc4139 27802+ i = au_plink_hash(inode->i_ino);
8b6a4947 27803+ hbl = sbinfo->si_plink + i;
5afbbe0d 27804+ au_igrab(inode);
1facf9fc 27805+
8b6a4947
AM
27806+ hlist_bl_lock(hbl);
27807+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 27808+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
27809+ found = 1;
27810+ break;
27811+ }
1facf9fc 27812+ }
5afbbe0d
AM
27813+ if (!found) {
27814+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 27815+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 27816+ }
8b6a4947 27817+ hlist_bl_unlock(hbl);
4a4d8108 27818+ if (!found) {
8b6a4947 27819+ cnt = au_hbl_count(hbl);
86dc4139
AM
27820+#define msg "unexpectedly unblanced or too many pseudo-links"
27821+ if (cnt > AUFS_PLINK_WARN)
27822+ AuWarn1(msg ", %d\n", cnt);
27823+#undef msg
1facf9fc 27824+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
27825+ if (unlikely(err)) {
27826+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 27827+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 27828+ iput(&icntnr->vfs_inode);
4a4d8108 27829+ }
5afbbe0d
AM
27830+ } else
27831+ iput(&icntnr->vfs_inode);
1facf9fc 27832+}
27833+
27834+/* free all plinks */
e49829fe 27835+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 27836+{
86dc4139 27837+ int i, warned;
1facf9fc 27838+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27839+ struct hlist_bl_head *hbl;
27840+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 27841+ struct au_icntnr *icntnr;
1facf9fc 27842+
dece6358
AM
27843+ SiMustWriteLock(sb);
27844+
1facf9fc 27845+ sbinfo = au_sbi(sb);
27846+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27847+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27848+
1facf9fc 27849+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
27850+ warned = 0;
27851+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27852+ hbl = sbinfo->si_plink + i;
27853+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
27854+ pr_warn("pseudo-link is not flushed");
27855+ warned = 1;
27856+ }
8b6a4947 27857+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 27858+ iput(&icntnr->vfs_inode);
8b6a4947 27859+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 27860+ }
1facf9fc 27861+}
27862+
e49829fe
JR
27863+void au_plink_clean(struct super_block *sb, int verbose)
27864+{
27865+ struct dentry *root;
27866+
27867+ root = sb->s_root;
27868+ aufs_write_lock(root);
27869+ if (au_opt_test(au_mntflags(sb), PLINK))
27870+ au_plink_put(sb, verbose);
27871+ aufs_write_unlock(root);
27872+}
27873+
86dc4139
AM
27874+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
27875+{
27876+ int do_put;
5afbbe0d 27877+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
27878+
27879+ do_put = 0;
5afbbe0d
AM
27880+ btop = au_ibtop(inode);
27881+ bbot = au_ibbot(inode);
27882+ if (btop >= 0) {
27883+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
27884+ if (!au_h_iptr(inode, bindex)
27885+ || au_ii_br_id(inode, bindex) != br_id)
27886+ continue;
27887+ au_set_h_iptr(inode, bindex, NULL, 0);
27888+ do_put = 1;
27889+ break;
27890+ }
27891+ if (do_put)
5afbbe0d 27892+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
27893+ if (au_h_iptr(inode, bindex)) {
27894+ do_put = 0;
27895+ break;
27896+ }
27897+ } else
27898+ do_put = 1;
27899+
27900+ return do_put;
27901+}
27902+
1facf9fc 27903+/* free the plinks on a branch specified by @br_id */
27904+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
27905+{
27906+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27907+ struct hlist_bl_head *hbl;
27908+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 27909+ struct au_icntnr *icntnr;
1facf9fc 27910+ struct inode *inode;
86dc4139 27911+ int i, do_put;
1facf9fc 27912+
dece6358
AM
27913+ SiMustWriteLock(sb);
27914+
1facf9fc 27915+ sbinfo = au_sbi(sb);
27916+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27917+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27918+
8b6a4947 27919+ /* no bit_lock since sbinfo is write-locked */
86dc4139 27920+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27921+ hbl = sbinfo->si_plink + i;
27922+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 27923+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
27924+ ii_write_lock_child(inode);
27925+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 27926+ if (do_put) {
8b6a4947 27927+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
27928+ iput(inode);
27929+ }
86dc4139
AM
27930+ ii_write_unlock(inode);
27931+ iput(inode);
dece6358 27932+ }
dece6358
AM
27933+ }
27934+}
7f207e10
AM
27935diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
27936--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
b00004a5
AM
27937+++ linux/fs/aufs/poll.c 2018-06-04 09:08:09.188079511 +0200
27938@@ -0,0 +1,53 @@
dece6358 27939+/*
b00004a5 27940+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
27941+ *
27942+ * This program, aufs is free software; you can redistribute it and/or modify
27943+ * it under the terms of the GNU General Public License as published by
27944+ * the Free Software Foundation; either version 2 of the License, or
27945+ * (at your option) any later version.
27946+ *
27947+ * This program is distributed in the hope that it will be useful,
27948+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27949+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27950+ * GNU General Public License for more details.
27951+ *
27952+ * You should have received a copy of the GNU General Public License
523b37e3 27953+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
27954+ */
27955+
1308ab2a 27956+/*
27957+ * poll operation
27958+ * There is only one filesystem which implements ->poll operation, currently.
27959+ */
27960+
27961+#include "aufs.h"
27962+
be118d29 27963+__poll_t aufs_poll(struct file *file, poll_table *wait)
1308ab2a 27964+{
be118d29 27965+ __poll_t mask;
1308ab2a 27966+ int err;
27967+ struct file *h_file;
1308ab2a 27968+ struct super_block *sb;
27969+
27970+ /* We should pretend an error happened. */
be118d29 27971+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 27972+ sb = file->f_path.dentry->d_sb;
e49829fe 27973+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 27974+
521ced18 27975+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
27976+ err = PTR_ERR(h_file);
27977+ if (IS_ERR(h_file))
1308ab2a 27978+ goto out;
27979+
27980+ /* it is not an error if h_file has no operation */
27981+ mask = DEFAULT_POLLMASK;
523b37e3 27982+ if (h_file->f_op->poll)
1308ab2a 27983+ mask = h_file->f_op->poll(h_file, wait);
b912730e 27984+ fput(h_file); /* instead of au_read_post() */
1308ab2a 27985+
4f0767ce 27986+out:
1308ab2a 27987+ si_read_unlock(sb);
b00004a5
AM
27988+ if (mask & POLLERR)
27989+ AuDbg("mask 0x%x\n", mask);
1308ab2a 27990+ return mask;
27991+}
c1595e42
JR
27992diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
27993--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 27994+++ linux/fs/aufs/posix_acl.c 2018-06-04 09:08:09.188079511 +0200
a2654f78 27995@@ -0,0 +1,102 @@
c1595e42 27996+/*
b00004a5 27997+ * Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
27998+ *
27999+ * This program, aufs is free software; you can redistribute it and/or modify
28000+ * it under the terms of the GNU General Public License as published by
28001+ * the Free Software Foundation; either version 2 of the License, or
28002+ * (at your option) any later version.
28003+ *
28004+ * This program is distributed in the hope that it will be useful,
28005+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28006+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28007+ * GNU General Public License for more details.
28008+ *
28009+ * You should have received a copy of the GNU General Public License
28010+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28011+ */
28012+
28013+/*
28014+ * posix acl operations
28015+ */
28016+
28017+#include <linux/fs.h>
c1595e42
JR
28018+#include "aufs.h"
28019+
28020+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28021+{
28022+ struct posix_acl *acl;
28023+ int err;
28024+ aufs_bindex_t bindex;
28025+ struct inode *h_inode;
28026+ struct super_block *sb;
28027+
28028+ acl = NULL;
28029+ sb = inode->i_sb;
28030+ si_read_lock(sb, AuLock_FLUSH);
28031+ ii_read_lock_child(inode);
2121bcd9 28032+ if (!(sb->s_flags & SB_POSIXACL))
c1595e42
JR
28033+ goto out;
28034+
5afbbe0d 28035+ bindex = au_ibtop(inode);
c1595e42
JR
28036+ h_inode = au_h_iptr(inode, bindex);
28037+ if (unlikely(!h_inode
28038+ || ((h_inode->i_mode & S_IFMT)
28039+ != (inode->i_mode & S_IFMT)))) {
28040+ err = au_busy_or_stale();
28041+ acl = ERR_PTR(err);
28042+ goto out;
28043+ }
28044+
28045+ /* always topmost only */
28046+ acl = get_acl(h_inode, type);
a2654f78
AM
28047+ if (!IS_ERR_OR_NULL(acl))
28048+ set_cached_acl(inode, type, acl);
c1595e42
JR
28049+
28050+out:
28051+ ii_read_unlock(inode);
28052+ si_read_unlock(sb);
28053+
28054+ AuTraceErrPtr(acl);
28055+ return acl;
28056+}
28057+
28058+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28059+{
28060+ int err;
28061+ ssize_t ssz;
28062+ struct dentry *dentry;
f2c43d5f 28063+ struct au_sxattr arg = {
c1595e42
JR
28064+ .type = AU_ACL_SET,
28065+ .u.acl_set = {
28066+ .acl = acl,
28067+ .type = type
28068+ },
28069+ };
28070+
5afbbe0d
AM
28071+ IMustLock(inode);
28072+
c1595e42
JR
28073+ if (inode->i_ino == AUFS_ROOT_INO)
28074+ dentry = dget(inode->i_sb->s_root);
28075+ else {
28076+ dentry = d_find_alias(inode);
28077+ if (!dentry)
28078+ dentry = d_find_any_alias(inode);
28079+ if (!dentry) {
28080+ pr_warn("cannot handle this inode, "
28081+ "please report to aufs-users ML\n");
28082+ err = -ENOENT;
28083+ goto out;
28084+ }
28085+ }
28086+
f2c43d5f 28087+ ssz = au_sxattr(dentry, inode, &arg);
c1595e42
JR
28088+ dput(dentry);
28089+ err = ssz;
a2654f78 28090+ if (ssz >= 0) {
c1595e42 28091+ err = 0;
a2654f78
AM
28092+ set_cached_acl(inode, type, acl);
28093+ }
c1595e42
JR
28094+
28095+out:
c1595e42
JR
28096+ return err;
28097+}
7f207e10
AM
28098diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28099--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 28100+++ linux/fs/aufs/procfs.c 2018-04-15 08:49:13.401150731 +0200
8b6a4947 28101@@ -0,0 +1,170 @@
e49829fe 28102+/*
b00004a5 28103+ * Copyright (C) 2010-2018 Junjiro R. Okajima
e49829fe
JR
28104+ *
28105+ * This program, aufs is free software; you can redistribute it and/or modify
28106+ * it under the terms of the GNU General Public License as published by
28107+ * the Free Software Foundation; either version 2 of the License, or
28108+ * (at your option) any later version.
28109+ *
28110+ * This program is distributed in the hope that it will be useful,
28111+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28112+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28113+ * GNU General Public License for more details.
28114+ *
28115+ * You should have received a copy of the GNU General Public License
523b37e3 28116+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28117+ */
28118+
28119+/*
28120+ * procfs interfaces
28121+ */
28122+
28123+#include <linux/proc_fs.h>
28124+#include "aufs.h"
28125+
28126+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28127+{
28128+ struct au_sbinfo *sbinfo;
28129+
28130+ sbinfo = file->private_data;
28131+ if (sbinfo) {
28132+ au_plink_maint_leave(sbinfo);
28133+ kobject_put(&sbinfo->si_kobj);
28134+ }
28135+
28136+ return 0;
28137+}
28138+
28139+static void au_procfs_plm_write_clean(struct file *file)
28140+{
28141+ struct au_sbinfo *sbinfo;
28142+
28143+ sbinfo = file->private_data;
28144+ if (sbinfo)
28145+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28146+}
28147+
28148+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28149+{
28150+ int err;
28151+ struct super_block *sb;
28152+ struct au_sbinfo *sbinfo;
8b6a4947 28153+ struct hlist_bl_node *pos;
e49829fe
JR
28154+
28155+ err = -EBUSY;
28156+ if (unlikely(file->private_data))
28157+ goto out;
28158+
28159+ sb = NULL;
53392da6 28160+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28161+ hlist_bl_lock(&au_sbilist);
28162+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe
JR
28163+ if (id == sysaufs_si_id(sbinfo)) {
28164+ kobject_get(&sbinfo->si_kobj);
28165+ sb = sbinfo->si_sb;
28166+ break;
28167+ }
8b6a4947 28168+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28169+
28170+ err = -EINVAL;
28171+ if (unlikely(!sb))
28172+ goto out;
28173+
28174+ err = au_plink_maint_enter(sb);
28175+ if (!err)
28176+ /* keep kobject_get() */
28177+ file->private_data = sbinfo;
28178+ else
28179+ kobject_put(&sbinfo->si_kobj);
28180+out:
28181+ return err;
28182+}
28183+
28184+/*
28185+ * Accept a valid "si=xxxx" only.
28186+ * Once it is accepted successfully, accept "clean" too.
28187+ */
28188+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28189+ size_t count, loff_t *ppos)
28190+{
28191+ ssize_t err;
28192+ unsigned long id;
28193+ /* last newline is allowed */
28194+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28195+
28196+ err = -EACCES;
28197+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28198+ goto out;
28199+
28200+ err = -EINVAL;
28201+ if (unlikely(count > sizeof(buf)))
28202+ goto out;
28203+
28204+ err = copy_from_user(buf, ubuf, count);
28205+ if (unlikely(err)) {
28206+ err = -EFAULT;
28207+ goto out;
28208+ }
28209+ buf[count] = 0;
28210+
28211+ err = -EINVAL;
28212+ if (!strcmp("clean", buf)) {
28213+ au_procfs_plm_write_clean(file);
28214+ goto out_success;
28215+ } else if (unlikely(strncmp("si=", buf, 3)))
28216+ goto out;
28217+
9dbd164d 28218+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28219+ if (unlikely(err))
28220+ goto out;
28221+
28222+ err = au_procfs_plm_write_si(file, id);
28223+ if (unlikely(err))
28224+ goto out;
28225+
28226+out_success:
28227+ err = count; /* success */
28228+out:
28229+ return err;
28230+}
28231+
28232+static const struct file_operations au_procfs_plm_fop = {
28233+ .write = au_procfs_plm_write,
28234+ .release = au_procfs_plm_release,
28235+ .owner = THIS_MODULE
28236+};
28237+
28238+/* ---------------------------------------------------------------------- */
28239+
28240+static struct proc_dir_entry *au_procfs_dir;
28241+
28242+void au_procfs_fin(void)
28243+{
28244+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28245+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28246+}
28247+
28248+int __init au_procfs_init(void)
28249+{
28250+ int err;
28251+ struct proc_dir_entry *entry;
28252+
28253+ err = -ENOMEM;
28254+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28255+ if (unlikely(!au_procfs_dir))
28256+ goto out;
28257+
28258+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
28259+ au_procfs_dir, &au_procfs_plm_fop);
28260+ if (unlikely(!entry))
28261+ goto out_dir;
28262+
28263+ err = 0;
28264+ goto out; /* success */
28265+
28266+
28267+out_dir:
28268+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28269+out:
28270+ return err;
28271+}
7f207e10
AM
28272diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28273--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 28274+++ linux/fs/aufs/rdu.c 2018-04-15 08:49:13.404484168 +0200
5afbbe0d 28275@@ -0,0 +1,381 @@
1308ab2a 28276+/*
b00004a5 28277+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1308ab2a 28278+ *
28279+ * This program, aufs is free software; you can redistribute it and/or modify
28280+ * it under the terms of the GNU General Public License as published by
28281+ * the Free Software Foundation; either version 2 of the License, or
28282+ * (at your option) any later version.
28283+ *
28284+ * This program is distributed in the hope that it will be useful,
28285+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28286+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28287+ * GNU General Public License for more details.
28288+ *
28289+ * You should have received a copy of the GNU General Public License
523b37e3 28290+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28291+ */
28292+
28293+/*
28294+ * readdir in userspace.
28295+ */
28296+
b752ccd1 28297+#include <linux/compat.h>
4a4d8108 28298+#include <linux/fs_stack.h>
1308ab2a 28299+#include <linux/security.h>
1308ab2a 28300+#include "aufs.h"
28301+
28302+/* bits for struct aufs_rdu.flags */
28303+#define AuRdu_CALLED 1
28304+#define AuRdu_CONT (1 << 1)
28305+#define AuRdu_FULL (1 << 2)
28306+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28307+#define au_fset_rdu(flags, name) \
28308+ do { (flags) |= AuRdu_##name; } while (0)
28309+#define au_fclr_rdu(flags, name) \
28310+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28311+
28312+struct au_rdu_arg {
392086de 28313+ struct dir_context ctx;
1308ab2a 28314+ struct aufs_rdu *rdu;
28315+ union au_rdu_ent_ul ent;
28316+ unsigned long end;
28317+
28318+ struct super_block *sb;
28319+ int err;
28320+};
28321+
392086de 28322+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28323+ loff_t offset, u64 h_ino, unsigned int d_type)
28324+{
28325+ int err, len;
392086de 28326+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28327+ struct aufs_rdu *rdu = arg->rdu;
28328+ struct au_rdu_ent ent;
28329+
28330+ err = 0;
28331+ arg->err = 0;
28332+ au_fset_rdu(rdu->cookie.flags, CALLED);
28333+ len = au_rdu_len(nlen);
28334+ if (arg->ent.ul + len < arg->end) {
28335+ ent.ino = h_ino;
28336+ ent.bindex = rdu->cookie.bindex;
28337+ ent.type = d_type;
28338+ ent.nlen = nlen;
4a4d8108
AM
28339+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28340+ ent.type = DT_UNKNOWN;
1308ab2a 28341+
9dbd164d 28342+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28343+ err = -EFAULT;
28344+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28345+ goto out;
28346+ if (copy_to_user(arg->ent.e->name, name, nlen))
28347+ goto out;
28348+ /* the terminating NULL */
28349+ if (__put_user(0, arg->ent.e->name + nlen))
28350+ goto out;
28351+ err = 0;
28352+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28353+ arg->ent.ul += len;
28354+ rdu->rent++;
28355+ } else {
28356+ err = -EFAULT;
28357+ au_fset_rdu(rdu->cookie.flags, FULL);
28358+ rdu->full = 1;
28359+ rdu->tail = arg->ent;
28360+ }
28361+
4f0767ce 28362+out:
1308ab2a 28363+ /* AuTraceErr(err); */
28364+ return err;
28365+}
28366+
28367+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28368+{
28369+ int err;
28370+ loff_t offset;
28371+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28372+
92d182d2 28373+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28374+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28375+ err = offset;
28376+ if (unlikely(offset != cookie->h_pos))
28377+ goto out;
28378+
28379+ err = 0;
28380+ do {
28381+ arg->err = 0;
28382+ au_fclr_rdu(cookie->flags, CALLED);
28383+ /* smp_mb(); */
392086de 28384+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28385+ if (err >= 0)
28386+ err = arg->err;
28387+ } while (!err
28388+ && au_ftest_rdu(cookie->flags, CALLED)
28389+ && !au_ftest_rdu(cookie->flags, FULL));
28390+ cookie->h_pos = h_file->f_pos;
28391+
4f0767ce 28392+out:
1308ab2a 28393+ AuTraceErr(err);
28394+ return err;
28395+}
28396+
28397+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28398+{
28399+ int err;
5afbbe0d 28400+ aufs_bindex_t bbot;
392086de
AM
28401+ struct au_rdu_arg arg = {
28402+ .ctx = {
2000de60 28403+ .actor = au_rdu_fill
392086de
AM
28404+ }
28405+ };
1308ab2a 28406+ struct dentry *dentry;
28407+ struct inode *inode;
28408+ struct file *h_file;
28409+ struct au_rdu_cookie *cookie = &rdu->cookie;
28410+
28411+ err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
28412+ if (unlikely(err)) {
28413+ err = -EFAULT;
28414+ AuTraceErr(err);
28415+ goto out;
28416+ }
28417+ rdu->rent = 0;
28418+ rdu->tail = rdu->ent;
28419+ rdu->full = 0;
28420+ arg.rdu = rdu;
28421+ arg.ent = rdu->ent;
28422+ arg.end = arg.ent.ul;
28423+ arg.end += rdu->sz;
28424+
28425+ err = -ENOTDIR;
5afbbe0d 28426+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28427+ goto out;
28428+
28429+ err = security_file_permission(file, MAY_READ);
28430+ AuTraceErr(err);
28431+ if (unlikely(err))
28432+ goto out;
28433+
2000de60 28434+ dentry = file->f_path.dentry;
5527c038 28435+ inode = d_inode(dentry);
5afbbe0d 28436+ inode_lock_shared(inode);
1308ab2a 28437+
28438+ arg.sb = inode->i_sb;
e49829fe
JR
28439+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28440+ if (unlikely(err))
28441+ goto out_mtx;
027c5e7a
AM
28442+ err = au_alive_dir(dentry);
28443+ if (unlikely(err))
28444+ goto out_si;
e49829fe 28445+ /* todo: reval? */
1308ab2a 28446+ fi_read_lock(file);
28447+
28448+ err = -EAGAIN;
28449+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28450+ && cookie->generation != au_figen(file)))
28451+ goto out_unlock;
28452+
28453+ err = 0;
28454+ if (!rdu->blk) {
28455+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28456+ if (!rdu->blk)
28457+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28458+ }
5afbbe0d
AM
28459+ bbot = au_fbtop(file);
28460+ if (cookie->bindex < bbot)
28461+ cookie->bindex = bbot;
28462+ bbot = au_fbbot_dir(file);
28463+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28464+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28465+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28466+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28467+ if (!h_file)
28468+ continue;
28469+
28470+ au_fclr_rdu(cookie->flags, FULL);
28471+ err = au_rdu_do(h_file, &arg);
28472+ AuTraceErr(err);
28473+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28474+ break;
28475+ }
28476+ AuDbg("rent %llu\n", rdu->rent);
28477+
28478+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28479+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28480+ au_fset_rdu(cookie->flags, CONT);
28481+ cookie->generation = au_figen(file);
28482+ }
28483+
28484+ ii_read_lock_child(inode);
5afbbe0d 28485+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28486+ ii_read_unlock(inode);
28487+
4f0767ce 28488+out_unlock:
1308ab2a 28489+ fi_read_unlock(file);
027c5e7a 28490+out_si:
1308ab2a 28491+ si_read_unlock(arg.sb);
4f0767ce 28492+out_mtx:
5afbbe0d 28493+ inode_unlock_shared(inode);
4f0767ce 28494+out:
1308ab2a 28495+ AuTraceErr(err);
28496+ return err;
28497+}
28498+
28499+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28500+{
28501+ int err;
28502+ ino_t ino;
28503+ unsigned long long nent;
28504+ union au_rdu_ent_ul *u;
28505+ struct au_rdu_ent ent;
28506+ struct super_block *sb;
28507+
28508+ err = 0;
28509+ nent = rdu->nent;
28510+ u = &rdu->ent;
2000de60 28511+ sb = file->f_path.dentry->d_sb;
1308ab2a 28512+ si_read_lock(sb, AuLock_FLUSH);
28513+ while (nent-- > 0) {
9dbd164d 28514+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28515+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108
AM
28516+ if (!err)
28517+ err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
1308ab2a 28518+ if (unlikely(err)) {
28519+ err = -EFAULT;
28520+ AuTraceErr(err);
28521+ break;
28522+ }
28523+
28524+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28525+ if (!ent.wh)
28526+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28527+ else
28528+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28529+ &ino);
28530+ if (unlikely(err)) {
28531+ AuTraceErr(err);
28532+ break;
28533+ }
28534+
28535+ err = __put_user(ino, &u->e->ino);
28536+ if (unlikely(err)) {
28537+ err = -EFAULT;
28538+ AuTraceErr(err);
28539+ break;
28540+ }
28541+ u->ul += au_rdu_len(ent.nlen);
28542+ }
28543+ si_read_unlock(sb);
28544+
28545+ return err;
28546+}
28547+
28548+/* ---------------------------------------------------------------------- */
28549+
28550+static int au_rdu_verify(struct aufs_rdu *rdu)
28551+{
b752ccd1 28552+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 28553+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 28554+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 28555+ rdu->blk,
28556+ rdu->rent, rdu->shwh, rdu->full,
28557+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28558+ rdu->cookie.generation);
dece6358 28559+
b752ccd1 28560+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 28561+ return 0;
dece6358 28562+
b752ccd1
AM
28563+ AuDbg("%u:%u\n",
28564+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 28565+ return -EINVAL;
28566+}
28567+
28568+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 28569+{
1308ab2a 28570+ long err, e;
28571+ struct aufs_rdu rdu;
28572+ void __user *p = (void __user *)arg;
dece6358 28573+
1308ab2a 28574+ err = copy_from_user(&rdu, p, sizeof(rdu));
28575+ if (unlikely(err)) {
28576+ err = -EFAULT;
28577+ AuTraceErr(err);
28578+ goto out;
28579+ }
28580+ err = au_rdu_verify(&rdu);
dece6358
AM
28581+ if (unlikely(err))
28582+ goto out;
28583+
1308ab2a 28584+ switch (cmd) {
28585+ case AUFS_CTL_RDU:
28586+ err = au_rdu(file, &rdu);
28587+ if (unlikely(err))
28588+ break;
dece6358 28589+
1308ab2a 28590+ e = copy_to_user(p, &rdu, sizeof(rdu));
28591+ if (unlikely(e)) {
28592+ err = -EFAULT;
28593+ AuTraceErr(err);
28594+ }
28595+ break;
28596+ case AUFS_CTL_RDU_INO:
28597+ err = au_rdu_ino(file, &rdu);
28598+ break;
28599+
28600+ default:
4a4d8108 28601+ /* err = -ENOTTY; */
1308ab2a 28602+ err = -EINVAL;
28603+ }
dece6358 28604+
4f0767ce 28605+out:
1308ab2a 28606+ AuTraceErr(err);
28607+ return err;
1facf9fc 28608+}
b752ccd1
AM
28609+
28610+#ifdef CONFIG_COMPAT
28611+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28612+{
28613+ long err, e;
28614+ struct aufs_rdu rdu;
28615+ void __user *p = compat_ptr(arg);
28616+
28617+ /* todo: get_user()? */
28618+ err = copy_from_user(&rdu, p, sizeof(rdu));
28619+ if (unlikely(err)) {
28620+ err = -EFAULT;
28621+ AuTraceErr(err);
28622+ goto out;
28623+ }
28624+ rdu.ent.e = compat_ptr(rdu.ent.ul);
28625+ err = au_rdu_verify(&rdu);
28626+ if (unlikely(err))
28627+ goto out;
28628+
28629+ switch (cmd) {
28630+ case AUFS_CTL_RDU:
28631+ err = au_rdu(file, &rdu);
28632+ if (unlikely(err))
28633+ break;
28634+
28635+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28636+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28637+ e = copy_to_user(p, &rdu, sizeof(rdu));
28638+ if (unlikely(e)) {
28639+ err = -EFAULT;
28640+ AuTraceErr(err);
28641+ }
28642+ break;
28643+ case AUFS_CTL_RDU_INO:
28644+ err = au_rdu_ino(file, &rdu);
28645+ break;
28646+
28647+ default:
28648+ /* err = -ENOTTY; */
28649+ err = -EINVAL;
28650+ }
28651+
4f0767ce 28652+out:
b752ccd1
AM
28653+ AuTraceErr(err);
28654+ return err;
28655+}
28656+#endif
7f207e10
AM
28657diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28658--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 28659+++ linux/fs/aufs/rwsem.h 2018-06-04 09:08:09.188079511 +0200
8b6a4947 28660@@ -0,0 +1,72 @@
1facf9fc 28661+/*
b00004a5 28662+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 28663+ *
28664+ * This program, aufs is free software; you can redistribute it and/or modify
28665+ * it under the terms of the GNU General Public License as published by
28666+ * the Free Software Foundation; either version 2 of the License, or
28667+ * (at your option) any later version.
dece6358
AM
28668+ *
28669+ * This program is distributed in the hope that it will be useful,
28670+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28671+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28672+ * GNU General Public License for more details.
28673+ *
28674+ * You should have received a copy of the GNU General Public License
523b37e3 28675+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28676+ */
28677+
28678+/*
28679+ * simple read-write semaphore wrappers
28680+ */
28681+
28682+#ifndef __AUFS_RWSEM_H__
28683+#define __AUFS_RWSEM_H__
28684+
28685+#ifdef __KERNEL__
28686+
4a4d8108 28687+#include "debug.h"
dece6358 28688+
8b6a4947
AM
28689+/* in the futre, the name 'au_rwsem' will be totally gone */
28690+#define au_rwsem rw_semaphore
dece6358
AM
28691+
28692+/* to debug easier, do not make them inlined functions */
8b6a4947 28693+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
dece6358 28694+/* rwsem_is_locked() is unusable */
8b6a4947
AM
28695+#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
28696+ && debug_locks \
28697+ && !lockdep_is_held_type(rw, 1))
28698+#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
28699+ && debug_locks \
28700+ && !lockdep_is_held_type(rw, 0))
28701+#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
28702+ && debug_locks \
28703+ && !lockdep_is_held(rw))
28704+#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
28705+ && debug_locks \
28706+ && lockdep_is_held(rw))
28707+
28708+#define au_rw_init(rw) init_rwsem(rw)
dece6358 28709+
5afbbe0d
AM
28710+#define au_rw_init_wlock(rw) do { \
28711+ au_rw_init(rw); \
8b6a4947 28712+ down_write(rw); \
5afbbe0d 28713+ } while (0)
dece6358 28714+
8b6a4947
AM
28715+#define au_rw_init_wlock_nested(rw, lsc) do { \
28716+ au_rw_init(rw); \
28717+ down_write_nested(rw, lsc); \
5afbbe0d 28718+ } while (0)
dece6358 28719+
8b6a4947
AM
28720+#define au_rw_read_lock(rw) down_read(rw)
28721+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
28722+#define au_rw_read_unlock(rw) up_read(rw)
28723+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
28724+#define au_rw_write_lock(rw) down_write(rw)
28725+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
28726+#define au_rw_write_unlock(rw) up_write(rw)
28727+/* why is not _nested version defined? */
28728+#define au_rw_read_trylock(rw) down_read_trylock(rw)
28729+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 28730+
28731+#endif /* __KERNEL__ */
28732+#endif /* __AUFS_RWSEM_H__ */
7f207e10
AM
28733diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
28734--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 28735+++ linux/fs/aufs/sbinfo.c 2018-06-04 09:08:09.188079511 +0200
a2654f78 28736@@ -0,0 +1,304 @@
1facf9fc 28737+/*
b00004a5 28738+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 28739+ *
28740+ * This program, aufs is free software; you can redistribute it and/or modify
28741+ * it under the terms of the GNU General Public License as published by
28742+ * the Free Software Foundation; either version 2 of the License, or
28743+ * (at your option) any later version.
dece6358
AM
28744+ *
28745+ * This program is distributed in the hope that it will be useful,
28746+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28747+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28748+ * GNU General Public License for more details.
28749+ *
28750+ * You should have received a copy of the GNU General Public License
523b37e3 28751+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28752+ */
28753+
28754+/*
28755+ * superblock private data
28756+ */
28757+
28758+#include "aufs.h"
28759+
28760+/*
28761+ * they are necessary regardless sysfs is disabled.
28762+ */
28763+void au_si_free(struct kobject *kobj)
28764+{
86dc4139 28765+ int i;
1facf9fc 28766+ struct au_sbinfo *sbinfo;
b752ccd1 28767+ char *locked __maybe_unused; /* debug only */
1facf9fc 28768+
28769+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 28770+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 28771+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 28772+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d
AM
28773+
28774+ AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
28775+ percpu_counter_destroy(&sbinfo->si_ninodes);
28776+ AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
28777+ percpu_counter_destroy(&sbinfo->si_nfiles);
1facf9fc 28778+
e49829fe 28779+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 28780+ au_br_free(sbinfo);
e49829fe 28781+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 28782+
1c60b727 28783+ kfree(sbinfo->si_branch);
1facf9fc 28784+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 28785+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 28786+
1c60b727 28787+ kfree(sbinfo);
1facf9fc 28788+}
28789+
28790+int au_si_alloc(struct super_block *sb)
28791+{
86dc4139 28792+ int err, i;
1facf9fc 28793+ struct au_sbinfo *sbinfo;
28794+
28795+ err = -ENOMEM;
4a4d8108 28796+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 28797+ if (unlikely(!sbinfo))
28798+ goto out;
28799+
28800+ /* will be reallocated separately */
28801+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
28802+ if (unlikely(!sbinfo->si_branch))
febd17d6 28803+ goto out_sbinfo;
1facf9fc 28804+
1facf9fc 28805+ err = sysaufs_si_init(sbinfo);
28806+ if (unlikely(err))
28807+ goto out_br;
28808+
28809+ au_nwt_init(&sbinfo->si_nowait);
dece6358 28810+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 28811+
5afbbe0d
AM
28812+ percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
28813+ percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
7f207e10 28814+
5afbbe0d 28815+ sbinfo->si_bbot = -1;
392086de 28816+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 28817+
28818+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
28819+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
28820+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
28821+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 28822+
076b876e
AM
28823+ au_fhsm_init(sbinfo);
28824+
e49829fe 28825+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 28826+
392086de
AM
28827+ sbinfo->si_xino_jiffy = jiffies;
28828+ sbinfo->si_xino_expire
28829+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 28830+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 28831+ sbinfo->si_xino_brid = -1;
28832+ /* leave si_xib_last_pindex and si_xib_next_bit */
28833+
8b6a4947 28834+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 28835+
e49829fe 28836+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 28837+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
28838+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
28839+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
28840+
86dc4139 28841+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 28842+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 28843+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 28844+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 28845+
8b6a4947 28846+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 28847+
b95c5147
AM
28848+ /* with getattr by default */
28849+ sbinfo->si_iop_array = aufs_iop;
28850+
1facf9fc 28851+ /* leave other members for sysaufs and si_mnt. */
28852+ sbinfo->si_sb = sb;
28853+ sb->s_fs_info = sbinfo;
b752ccd1 28854+ si_pid_set(sb);
1facf9fc 28855+ return 0; /* success */
28856+
4f0767ce 28857+out_br:
1c60b727 28858+ kfree(sbinfo->si_branch);
4f0767ce 28859+out_sbinfo:
1c60b727 28860+ kfree(sbinfo);
4f0767ce 28861+out:
1facf9fc 28862+ return err;
28863+}
28864+
e2f27e51 28865+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 28866+{
28867+ int err, sz;
28868+ struct au_branch **brp;
28869+
dece6358
AM
28870+ AuRwMustWriteLock(&sbinfo->si_rwsem);
28871+
1facf9fc 28872+ err = -ENOMEM;
5afbbe0d 28873+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 28874+ if (unlikely(!sz))
28875+ sz = sizeof(*brp);
e2f27e51
AM
28876+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
28877+ may_shrink);
1facf9fc 28878+ if (brp) {
28879+ sbinfo->si_branch = brp;
28880+ err = 0;
28881+ }
28882+
28883+ return err;
28884+}
28885+
28886+/* ---------------------------------------------------------------------- */
28887+
28888+unsigned int au_sigen_inc(struct super_block *sb)
28889+{
28890+ unsigned int gen;
5527c038 28891+ struct inode *inode;
1facf9fc 28892+
dece6358
AM
28893+ SiMustWriteLock(sb);
28894+
1facf9fc 28895+ gen = ++au_sbi(sb)->si_generation;
28896+ au_update_digen(sb->s_root);
5527c038
JR
28897+ inode = d_inode(sb->s_root);
28898+ au_update_iigen(inode, /*half*/0);
be118d29 28899+ inode_inc_iversion(inode);
1facf9fc 28900+ return gen;
28901+}
28902+
28903+aufs_bindex_t au_new_br_id(struct super_block *sb)
28904+{
28905+ aufs_bindex_t br_id;
28906+ int i;
28907+ struct au_sbinfo *sbinfo;
28908+
dece6358
AM
28909+ SiMustWriteLock(sb);
28910+
1facf9fc 28911+ sbinfo = au_sbi(sb);
28912+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
28913+ br_id = ++sbinfo->si_last_br_id;
7f207e10 28914+ AuDebugOn(br_id < 0);
1facf9fc 28915+ if (br_id && au_br_index(sb, br_id) < 0)
28916+ return br_id;
28917+ }
28918+
28919+ return -1;
28920+}
28921+
28922+/* ---------------------------------------------------------------------- */
28923+
e49829fe
JR
28924+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
28925+int si_read_lock(struct super_block *sb, int flags)
28926+{
28927+ int err;
28928+
28929+ err = 0;
28930+ if (au_ftest_lock(flags, FLUSH))
28931+ au_nwt_flush(&au_sbi(sb)->si_nowait);
28932+
28933+ si_noflush_read_lock(sb);
28934+ err = au_plink_maint(sb, flags);
28935+ if (unlikely(err))
28936+ si_read_unlock(sb);
28937+
28938+ return err;
28939+}
28940+
28941+int si_write_lock(struct super_block *sb, int flags)
28942+{
28943+ int err;
28944+
28945+ if (au_ftest_lock(flags, FLUSH))
28946+ au_nwt_flush(&au_sbi(sb)->si_nowait);
28947+
28948+ si_noflush_write_lock(sb);
28949+ err = au_plink_maint(sb, flags);
28950+ if (unlikely(err))
28951+ si_write_unlock(sb);
28952+
28953+ return err;
28954+}
28955+
1facf9fc 28956+/* dentry and super_block lock. call at entry point */
e49829fe 28957+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 28958+{
e49829fe 28959+ int err;
027c5e7a 28960+ struct super_block *sb;
e49829fe 28961+
027c5e7a
AM
28962+ sb = dentry->d_sb;
28963+ err = si_read_lock(sb, flags);
28964+ if (unlikely(err))
28965+ goto out;
28966+
28967+ if (au_ftest_lock(flags, DW))
28968+ di_write_lock_child(dentry);
28969+ else
28970+ di_read_lock_child(dentry, flags);
28971+
28972+ if (au_ftest_lock(flags, GEN)) {
28973+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
28974+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
28975+ AuDebugOn(!err && au_dbrange_test(dentry));
28976+ else if (!err)
28977+ err = au_dbrange_test(dentry);
027c5e7a
AM
28978+ if (unlikely(err))
28979+ aufs_read_unlock(dentry, flags);
e49829fe
JR
28980+ }
28981+
027c5e7a 28982+out:
e49829fe 28983+ return err;
1facf9fc 28984+}
28985+
28986+void aufs_read_unlock(struct dentry *dentry, int flags)
28987+{
28988+ if (au_ftest_lock(flags, DW))
28989+ di_write_unlock(dentry);
28990+ else
28991+ di_read_unlock(dentry, flags);
28992+ si_read_unlock(dentry->d_sb);
28993+}
28994+
28995+void aufs_write_lock(struct dentry *dentry)
28996+{
e49829fe 28997+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 28998+ di_write_lock_child(dentry);
28999+}
29000+
29001+void aufs_write_unlock(struct dentry *dentry)
29002+{
29003+ di_write_unlock(dentry);
29004+ si_write_unlock(dentry->d_sb);
29005+}
29006+
e49829fe 29007+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29008+{
e49829fe 29009+ int err;
027c5e7a
AM
29010+ unsigned int sigen;
29011+ struct super_block *sb;
e49829fe 29012+
027c5e7a
AM
29013+ sb = d1->d_sb;
29014+ err = si_read_lock(sb, flags);
29015+ if (unlikely(err))
29016+ goto out;
29017+
b95c5147 29018+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29019+
29020+ if (au_ftest_lock(flags, GEN)) {
29021+ sigen = au_sigen(sb);
29022+ err = au_digen_test(d1, sigen);
29023+ AuDebugOn(!err && au_dbrange_test(d1));
29024+ if (!err) {
29025+ err = au_digen_test(d2, sigen);
29026+ AuDebugOn(!err && au_dbrange_test(d2));
29027+ }
29028+ if (unlikely(err))
29029+ aufs_read_and_write_unlock2(d1, d2);
29030+ }
29031+
29032+out:
e49829fe 29033+ return err;
1facf9fc 29034+}
29035+
29036+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29037+{
29038+ di_write_unlock2(d1, d2);
29039+ si_read_unlock(d1->d_sb);
29040+}
7f207e10
AM
29041diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29042--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 29043+++ linux/fs/aufs/super.c 2018-06-04 09:08:09.188079511 +0200
be118d29 29044@@ -0,0 +1,1051 @@
1facf9fc 29045+/*
b00004a5 29046+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 29047+ *
29048+ * This program, aufs is free software; you can redistribute it and/or modify
29049+ * it under the terms of the GNU General Public License as published by
29050+ * the Free Software Foundation; either version 2 of the License, or
29051+ * (at your option) any later version.
dece6358
AM
29052+ *
29053+ * This program is distributed in the hope that it will be useful,
29054+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29055+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29056+ * GNU General Public License for more details.
29057+ *
29058+ * You should have received a copy of the GNU General Public License
523b37e3 29059+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29060+ */
29061+
29062+/*
29063+ * mount and super_block operations
29064+ */
29065+
f6c5ef8b 29066+#include <linux/mm.h>
1facf9fc 29067+#include <linux/seq_file.h>
29068+#include <linux/statfs.h>
7f207e10 29069+#include <linux/vmalloc.h>
1facf9fc 29070+#include "aufs.h"
29071+
29072+/*
29073+ * super_operations
29074+ */
29075+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29076+{
29077+ struct au_icntnr *c;
29078+
29079+ c = au_cache_alloc_icntnr();
29080+ if (c) {
027c5e7a 29081+ au_icntnr_init(c);
be118d29 29082+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29083+ c->iinfo.ii_hinode = NULL;
29084+ return &c->vfs_inode;
29085+ }
29086+ return NULL;
29087+}
29088+
027c5e7a
AM
29089+static void aufs_destroy_inode_cb(struct rcu_head *head)
29090+{
29091+ struct inode *inode = container_of(head, struct inode, i_rcu);
29092+
1c60b727 29093+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
027c5e7a
AM
29094+}
29095+
1facf9fc 29096+static void aufs_destroy_inode(struct inode *inode)
29097+{
5afbbe0d
AM
29098+ if (!au_is_bad_inode(inode))
29099+ au_iinfo_fin(inode);
027c5e7a 29100+ call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
1facf9fc 29101+}
29102+
29103+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29104+{
29105+ struct inode *inode;
29106+ int err;
29107+
29108+ inode = iget_locked(sb, ino);
29109+ if (unlikely(!inode)) {
29110+ inode = ERR_PTR(-ENOMEM);
29111+ goto out;
29112+ }
29113+ if (!(inode->i_state & I_NEW))
29114+ goto out;
29115+
29116+ err = au_xigen_new(inode);
29117+ if (!err)
29118+ err = au_iinfo_init(inode);
29119+ if (!err)
be118d29 29120+ inode_inc_iversion(inode);
1facf9fc 29121+ else {
29122+ iget_failed(inode);
29123+ inode = ERR_PTR(err);
29124+ }
29125+
4f0767ce 29126+out:
1facf9fc 29127+ /* never return NULL */
29128+ AuDebugOn(!inode);
29129+ AuTraceErrPtr(inode);
29130+ return inode;
29131+}
29132+
29133+/* lock free root dinfo */
29134+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29135+{
29136+ int err;
5afbbe0d 29137+ aufs_bindex_t bindex, bbot;
1facf9fc 29138+ struct path path;
4a4d8108 29139+ struct au_hdentry *hdp;
1facf9fc 29140+ struct au_branch *br;
076b876e 29141+ au_br_perm_str_t perm;
1facf9fc 29142+
29143+ err = 0;
5afbbe0d
AM
29144+ bbot = au_sbbot(sb);
29145+ bindex = 0;
29146+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29147+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29148+ br = au_sbr(sb, bindex);
86dc4139 29149+ path.mnt = au_br_mnt(br);
5afbbe0d 29150+ path.dentry = hdp->hd_dentry;
1facf9fc 29151+ err = au_seq_path(seq, &path);
79b8bda9 29152+ if (!err) {
076b876e 29153+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29154+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29155+ if (bindex != bbot)
79b8bda9 29156+ seq_putc(seq, ':');
1e00d052 29157+ }
1facf9fc 29158+ }
79b8bda9
AM
29159+ if (unlikely(err || seq_has_overflowed(seq)))
29160+ err = -E2BIG;
1facf9fc 29161+
29162+ return err;
29163+}
29164+
f2c43d5f
AM
29165+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29166+ const char *append)
29167+{
29168+ char *p;
29169+
29170+ p = fmt;
29171+ while (*pat != ':')
29172+ *p++ = *pat++;
29173+ *p++ = *pat++;
29174+ strcpy(p, append);
29175+ AuDebugOn(strlen(fmt) >= len);
29176+}
29177+
1facf9fc 29178+static void au_show_wbr_create(struct seq_file *m, int v,
29179+ struct au_sbinfo *sbinfo)
29180+{
29181+ const char *pat;
f2c43d5f
AM
29182+ char fmt[32];
29183+ struct au_wbr_mfs *mfs;
1facf9fc 29184+
dece6358
AM
29185+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29186+
c2b27bf2 29187+ seq_puts(m, ",create=");
1facf9fc 29188+ pat = au_optstr_wbr_create(v);
f2c43d5f 29189+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29190+ switch (v) {
29191+ case AuWbrCreate_TDP:
29192+ case AuWbrCreate_RR:
29193+ case AuWbrCreate_MFS:
29194+ case AuWbrCreate_PMFS:
c2b27bf2 29195+ seq_puts(m, pat);
1facf9fc 29196+ break;
f2c43d5f
AM
29197+ case AuWbrCreate_MFSRR:
29198+ case AuWbrCreate_TDMFS:
29199+ case AuWbrCreate_PMFSRR:
29200+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29201+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29202+ break;
f2c43d5f 29203+ case AuWbrCreate_MFSV:
1facf9fc 29204+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29205+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29206+ seq_printf(m, fmt,
29207+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29208+ / MSEC_PER_SEC);
1facf9fc 29209+ break;
1facf9fc 29210+ case AuWbrCreate_MFSRRV:
f2c43d5f 29211+ case AuWbrCreate_TDMFSV:
392086de 29212+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29213+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29214+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29215+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29216+ break;
f2c43d5f
AM
29217+ default:
29218+ BUG();
1facf9fc 29219+ }
29220+}
29221+
7eafdf33 29222+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29223+{
29224+#ifdef CONFIG_SYSFS
29225+ return 0;
29226+#else
29227+ int err;
29228+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29229+ aufs_bindex_t bindex, brid;
1facf9fc 29230+ struct qstr *name;
29231+ struct file *f;
29232+ struct dentry *d, *h_root;
29233+
dece6358
AM
29234+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29235+
1facf9fc 29236+ err = 0;
1facf9fc 29237+ f = au_sbi(sb)->si_xib;
29238+ if (!f)
29239+ goto out;
29240+
29241+ /* stop printing the default xino path on the first writable branch */
29242+ h_root = NULL;
29243+ brid = au_xino_brid(sb);
29244+ if (brid >= 0) {
29245+ bindex = au_br_index(sb, brid);
5afbbe0d 29246+ h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
1facf9fc 29247+ }
2000de60 29248+ d = f->f_path.dentry;
1facf9fc 29249+ name = &d->d_name;
29250+ /* safe ->d_parent because the file is unlinked */
29251+ if (d->d_parent == h_root
29252+ && name->len == len
29253+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29254+ goto out;
29255+
29256+ seq_puts(seq, ",xino=");
29257+ err = au_xino_path(seq, f);
29258+
4f0767ce 29259+out:
1facf9fc 29260+ return err;
29261+#endif
29262+}
29263+
29264+/* seq_file will re-call me in case of too long string */
7eafdf33 29265+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29266+{
027c5e7a 29267+ int err;
1facf9fc 29268+ unsigned int mnt_flags, v;
29269+ struct super_block *sb;
29270+ struct au_sbinfo *sbinfo;
29271+
29272+#define AuBool(name, str) do { \
29273+ v = au_opt_test(mnt_flags, name); \
29274+ if (v != au_opt_test(AuOpt_Def, name)) \
29275+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29276+} while (0)
29277+
29278+#define AuStr(name, str) do { \
29279+ v = mnt_flags & AuOptMask_##name; \
29280+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29281+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29282+} while (0)
29283+
29284+#define AuUInt(name, str, val) do { \
29285+ if (val != AUFS_##name##_DEF) \
29286+ seq_printf(m, "," #str "=%u", val); \
29287+} while (0)
29288+
7eafdf33 29289+ sb = dentry->d_sb;
2121bcd9 29290+ if (sb->s_flags & SB_POSIXACL)
c1595e42 29291+ seq_puts(m, ",acl");
be118d29
JR
29292+#if 0
29293+ if (sb->s_flags & SB_I_VERSION)
29294+ seq_puts(m, ",i_version");
29295+#endif
c1595e42
JR
29296+
29297+ /* lock free root dinfo */
1facf9fc 29298+ si_noflush_read_lock(sb);
29299+ sbinfo = au_sbi(sb);
29300+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29301+
29302+ mnt_flags = au_mntflags(sb);
29303+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29304+ err = au_show_xino(m, sb);
1facf9fc 29305+ if (unlikely(err))
29306+ goto out;
29307+ } else
29308+ seq_puts(m, ",noxino");
29309+
29310+ AuBool(TRUNC_XINO, trunc_xino);
29311+ AuStr(UDBA, udba);
dece6358 29312+ AuBool(SHWH, shwh);
1facf9fc 29313+ AuBool(PLINK, plink);
4a4d8108 29314+ AuBool(DIO, dio);
076b876e 29315+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29316+
29317+ v = sbinfo->si_wbr_create;
29318+ if (v != AuWbrCreate_Def)
29319+ au_show_wbr_create(m, v, sbinfo);
29320+
29321+ v = sbinfo->si_wbr_copyup;
29322+ if (v != AuWbrCopyup_Def)
29323+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29324+
29325+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29326+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29327+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29328+
29329+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29330+
027c5e7a
AM
29331+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29332+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29333+
29334+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29335+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29336+
076b876e
AM
29337+ au_fhsm_show(m, sbinfo);
29338+
8b6a4947 29339+ AuBool(DIRREN, dirren);
1facf9fc 29340+ AuBool(SUM, sum);
29341+ /* AuBool(SUM_W, wsum); */
29342+ AuBool(WARN_PERM, warn_perm);
29343+ AuBool(VERBOSE, verbose);
29344+
4f0767ce 29345+out:
1facf9fc 29346+ /* be sure to print "br:" last */
29347+ if (!sysaufs_brs) {
29348+ seq_puts(m, ",br:");
29349+ au_show_brs(m, sb);
29350+ }
29351+ si_read_unlock(sb);
29352+ return 0;
29353+
1facf9fc 29354+#undef AuBool
29355+#undef AuStr
4a4d8108 29356+#undef AuUInt
1facf9fc 29357+}
29358+
29359+/* ---------------------------------------------------------------------- */
29360+
29361+/* sum mode which returns the summation for statfs(2) */
29362+
29363+static u64 au_add_till_max(u64 a, u64 b)
29364+{
29365+ u64 old;
29366+
29367+ old = a;
29368+ a += b;
92d182d2
AM
29369+ if (old <= a)
29370+ return a;
29371+ return ULLONG_MAX;
29372+}
29373+
29374+static u64 au_mul_till_max(u64 a, long mul)
29375+{
29376+ u64 old;
29377+
29378+ old = a;
29379+ a *= mul;
29380+ if (old <= a)
1facf9fc 29381+ return a;
29382+ return ULLONG_MAX;
29383+}
29384+
29385+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29386+{
29387+ int err;
92d182d2 29388+ long bsize, factor;
1facf9fc 29389+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29390+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29391+ unsigned char shared;
7f207e10 29392+ struct path h_path;
1facf9fc 29393+ struct super_block *h_sb;
29394+
92d182d2
AM
29395+ err = 0;
29396+ bsize = LONG_MAX;
29397+ files = 0;
29398+ ffree = 0;
1facf9fc 29399+ blocks = 0;
29400+ bfree = 0;
29401+ bavail = 0;
5afbbe0d
AM
29402+ bbot = au_sbbot(sb);
29403+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29404+ h_path.mnt = au_sbr_mnt(sb, bindex);
29405+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29406+ shared = 0;
92d182d2 29407+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29408+ shared = (au_sbr_sb(sb, i) == h_sb);
29409+ if (shared)
29410+ continue;
29411+
29412+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29413+ h_path.dentry = h_path.mnt->mnt_root;
29414+ err = vfs_statfs(&h_path, buf);
1facf9fc 29415+ if (unlikely(err))
29416+ goto out;
29417+
92d182d2
AM
29418+ if (bsize > buf->f_bsize) {
29419+ /*
29420+ * we will reduce bsize, so we have to expand blocks
29421+ * etc. to match them again
29422+ */
29423+ factor = (bsize / buf->f_bsize);
29424+ blocks = au_mul_till_max(blocks, factor);
29425+ bfree = au_mul_till_max(bfree, factor);
29426+ bavail = au_mul_till_max(bavail, factor);
29427+ bsize = buf->f_bsize;
29428+ }
29429+
29430+ factor = (buf->f_bsize / bsize);
29431+ blocks = au_add_till_max(blocks,
29432+ au_mul_till_max(buf->f_blocks, factor));
29433+ bfree = au_add_till_max(bfree,
29434+ au_mul_till_max(buf->f_bfree, factor));
29435+ bavail = au_add_till_max(bavail,
29436+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29437+ files = au_add_till_max(files, buf->f_files);
29438+ ffree = au_add_till_max(ffree, buf->f_ffree);
29439+ }
29440+
92d182d2 29441+ buf->f_bsize = bsize;
1facf9fc 29442+ buf->f_blocks = blocks;
29443+ buf->f_bfree = bfree;
29444+ buf->f_bavail = bavail;
29445+ buf->f_files = files;
29446+ buf->f_ffree = ffree;
92d182d2 29447+ buf->f_frsize = 0;
1facf9fc 29448+
4f0767ce 29449+out:
1facf9fc 29450+ return err;
29451+}
29452+
29453+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29454+{
29455+ int err;
7f207e10 29456+ struct path h_path;
1facf9fc 29457+ struct super_block *sb;
29458+
29459+ /* lock free root dinfo */
29460+ sb = dentry->d_sb;
29461+ si_noflush_read_lock(sb);
7f207e10 29462+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29463+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29464+ h_path.mnt = au_sbr_mnt(sb, 0);
29465+ h_path.dentry = h_path.mnt->mnt_root;
29466+ err = vfs_statfs(&h_path, buf);
29467+ } else
1facf9fc 29468+ err = au_statfs_sum(sb, buf);
29469+ si_read_unlock(sb);
29470+
29471+ if (!err) {
29472+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29473+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29474+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29475+ }
29476+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29477+
29478+ return err;
29479+}
29480+
29481+/* ---------------------------------------------------------------------- */
29482+
537831f9
AM
29483+static int aufs_sync_fs(struct super_block *sb, int wait)
29484+{
29485+ int err, e;
5afbbe0d 29486+ aufs_bindex_t bbot, bindex;
537831f9
AM
29487+ struct au_branch *br;
29488+ struct super_block *h_sb;
29489+
29490+ err = 0;
29491+ si_noflush_read_lock(sb);
5afbbe0d
AM
29492+ bbot = au_sbbot(sb);
29493+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29494+ br = au_sbr(sb, bindex);
29495+ if (!au_br_writable(br->br_perm))
29496+ continue;
29497+
29498+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29499+ e = vfsub_sync_filesystem(h_sb, wait);
29500+ if (unlikely(e && !err))
29501+ err = e;
29502+ /* go on even if an error happens */
537831f9
AM
29503+ }
29504+ si_read_unlock(sb);
29505+
29506+ return err;
29507+}
29508+
29509+/* ---------------------------------------------------------------------- */
29510+
1facf9fc 29511+/* final actions when unmounting a file system */
29512+static void aufs_put_super(struct super_block *sb)
29513+{
29514+ struct au_sbinfo *sbinfo;
29515+
29516+ sbinfo = au_sbi(sb);
29517+ if (!sbinfo)
29518+ return;
29519+
1facf9fc 29520+ dbgaufs_si_fin(sbinfo);
29521+ kobject_put(&sbinfo->si_kobj);
29522+}
29523+
29524+/* ---------------------------------------------------------------------- */
29525+
79b8bda9
AM
29526+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29527+ struct super_block *sb, void *arg)
7f207e10
AM
29528+{
29529+ void *array;
076b876e 29530+ unsigned long long n, sz;
7f207e10
AM
29531+
29532+ array = NULL;
29533+ n = 0;
29534+ if (!*hint)
29535+ goto out;
29536+
29537+ if (*hint > ULLONG_MAX / sizeof(array)) {
29538+ array = ERR_PTR(-EMFILE);
29539+ pr_err("hint %llu\n", *hint);
29540+ goto out;
29541+ }
29542+
076b876e
AM
29543+ sz = sizeof(array) * *hint;
29544+ array = kzalloc(sz, GFP_NOFS);
7f207e10 29545+ if (unlikely(!array))
076b876e 29546+ array = vzalloc(sz);
7f207e10
AM
29547+ if (unlikely(!array)) {
29548+ array = ERR_PTR(-ENOMEM);
29549+ goto out;
29550+ }
29551+
79b8bda9 29552+ n = cb(sb, array, *hint, arg);
7f207e10
AM
29553+ AuDebugOn(n > *hint);
29554+
29555+out:
29556+ *hint = n;
29557+ return array;
29558+}
29559+
79b8bda9 29560+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
29561+ unsigned long long max __maybe_unused,
29562+ void *arg)
29563+{
29564+ unsigned long long n;
29565+ struct inode **p, *inode;
29566+ struct list_head *head;
29567+
29568+ n = 0;
29569+ p = a;
29570+ head = arg;
79b8bda9 29571+ spin_lock(&sb->s_inode_list_lock);
7f207e10 29572+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
29573+ if (!au_is_bad_inode(inode)
29574+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
29575+ spin_lock(&inode->i_lock);
29576+ if (atomic_read(&inode->i_count)) {
29577+ au_igrab(inode);
29578+ *p++ = inode;
29579+ n++;
29580+ AuDebugOn(n > max);
29581+ }
29582+ spin_unlock(&inode->i_lock);
7f207e10
AM
29583+ }
29584+ }
79b8bda9 29585+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
29586+
29587+ return n;
29588+}
29589+
29590+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29591+{
5afbbe0d 29592+ *max = au_ninodes(sb);
79b8bda9 29593+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
29594+}
29595+
29596+void au_iarray_free(struct inode **a, unsigned long long max)
29597+{
29598+ unsigned long long ull;
29599+
29600+ for (ull = 0; ull < max; ull++)
29601+ iput(a[ull]);
be52b249 29602+ kvfree(a);
7f207e10
AM
29603+}
29604+
29605+/* ---------------------------------------------------------------------- */
29606+
1facf9fc 29607+/*
29608+ * refresh dentry and inode at remount time.
29609+ */
027c5e7a
AM
29610+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29611+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29612+ struct dentry *parent)
1facf9fc 29613+{
29614+ int err;
1facf9fc 29615+
29616+ di_write_lock_child(dentry);
1facf9fc 29617+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
29618+ err = au_refresh_dentry(dentry, parent);
29619+ if (!err && dir_flags)
5527c038 29620+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 29621+ di_read_unlock(parent, AuLock_IR);
1facf9fc 29622+ di_write_unlock(dentry);
29623+
29624+ return err;
29625+}
29626+
027c5e7a
AM
29627+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29628+ struct au_sbinfo *sbinfo,
b95c5147 29629+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 29630+{
027c5e7a
AM
29631+ int err;
29632+ struct dentry *parent;
027c5e7a
AM
29633+
29634+ err = 0;
29635+ parent = dget_parent(dentry);
29636+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
29637+ if (d_really_is_positive(dentry)) {
29638+ if (!d_is_dir(dentry))
027c5e7a
AM
29639+ err = au_do_refresh(dentry, /*dir_flags*/0,
29640+ parent);
29641+ else {
29642+ err = au_do_refresh(dentry, dir_flags, parent);
29643+ if (unlikely(err))
29644+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29645+ }
29646+ } else
29647+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29648+ AuDbgDentry(dentry);
29649+ }
29650+ dput(parent);
29651+
79b8bda9 29652+ if (!err) {
b95c5147 29653+ if (do_idop)
79b8bda9
AM
29654+ au_refresh_dop(dentry, /*force_reval*/0);
29655+ } else
29656+ au_refresh_dop(dentry, /*force_reval*/1);
29657+
027c5e7a
AM
29658+ AuTraceErr(err);
29659+ return err;
1facf9fc 29660+}
29661+
b95c5147 29662+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 29663+{
29664+ int err, i, j, ndentry, e;
027c5e7a 29665+ unsigned int sigen;
1facf9fc 29666+ struct au_dcsub_pages dpages;
29667+ struct au_dpage *dpage;
027c5e7a
AM
29668+ struct dentry **dentries, *d;
29669+ struct au_sbinfo *sbinfo;
29670+ struct dentry *root = sb->s_root;
5527c038 29671+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 29672+
b95c5147 29673+ if (do_idop)
79b8bda9
AM
29674+ au_refresh_dop(root, /*force_reval*/0);
29675+
027c5e7a
AM
29676+ err = au_dpages_init(&dpages, GFP_NOFS);
29677+ if (unlikely(err))
1facf9fc 29678+ goto out;
027c5e7a
AM
29679+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
29680+ if (unlikely(err))
1facf9fc 29681+ goto out_dpages;
1facf9fc 29682+
027c5e7a
AM
29683+ sigen = au_sigen(sb);
29684+ sbinfo = au_sbi(sb);
29685+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 29686+ dpage = dpages.dpages + i;
29687+ dentries = dpage->dentries;
29688+ ndentry = dpage->ndentry;
027c5e7a 29689+ for (j = 0; j < ndentry; j++) {
1facf9fc 29690+ d = dentries[j];
79b8bda9 29691+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 29692+ do_idop);
027c5e7a
AM
29693+ if (unlikely(e && !err))
29694+ err = e;
29695+ /* go on even err */
1facf9fc 29696+ }
29697+ }
29698+
4f0767ce 29699+out_dpages:
1facf9fc 29700+ au_dpages_free(&dpages);
4f0767ce 29701+out:
1facf9fc 29702+ return err;
29703+}
29704+
b95c5147 29705+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 29706+{
027c5e7a
AM
29707+ int err, e;
29708+ unsigned int sigen;
29709+ unsigned long long max, ull;
29710+ struct inode *inode, **array;
1facf9fc 29711+
027c5e7a
AM
29712+ array = au_iarray_alloc(sb, &max);
29713+ err = PTR_ERR(array);
29714+ if (IS_ERR(array))
29715+ goto out;
1facf9fc 29716+
29717+ err = 0;
027c5e7a
AM
29718+ sigen = au_sigen(sb);
29719+ for (ull = 0; ull < max; ull++) {
29720+ inode = array[ull];
076b876e
AM
29721+ if (unlikely(!inode))
29722+ break;
b95c5147
AM
29723+
29724+ e = 0;
29725+ ii_write_lock_child(inode);
537831f9 29726+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 29727+ e = au_refresh_hinode_self(inode);
1facf9fc 29728+ if (unlikely(e)) {
b95c5147 29729+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 29730+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 29731+ if (!err)
29732+ err = e;
29733+ /* go on even if err */
29734+ }
29735+ }
b95c5147
AM
29736+ if (!e && do_idop)
29737+ au_refresh_iop(inode, /*force_getattr*/0);
29738+ ii_write_unlock(inode);
1facf9fc 29739+ }
29740+
027c5e7a 29741+ au_iarray_free(array, max);
1facf9fc 29742+
4f0767ce 29743+out:
1facf9fc 29744+ return err;
29745+}
29746+
b95c5147 29747+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 29748+{
027c5e7a
AM
29749+ int err, e;
29750+ unsigned int udba;
5afbbe0d 29751+ aufs_bindex_t bindex, bbot;
1facf9fc 29752+ struct dentry *root;
29753+ struct inode *inode;
027c5e7a 29754+ struct au_branch *br;
79b8bda9 29755+ struct au_sbinfo *sbi;
1facf9fc 29756+
29757+ au_sigen_inc(sb);
79b8bda9
AM
29758+ sbi = au_sbi(sb);
29759+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 29760+
29761+ root = sb->s_root;
29762+ DiMustNoWaiters(root);
5527c038 29763+ inode = d_inode(root);
1facf9fc 29764+ IiMustNoWaiters(inode);
1facf9fc 29765+
027c5e7a 29766+ udba = au_opt_udba(sb);
5afbbe0d
AM
29767+ bbot = au_sbbot(sb);
29768+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
29769+ br = au_sbr(sb, bindex);
29770+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 29771+ if (unlikely(err))
027c5e7a
AM
29772+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
29773+ bindex, err);
29774+ /* go on even if err */
1facf9fc 29775+ }
027c5e7a 29776+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 29777+
b95c5147 29778+ if (do_idop) {
79b8bda9
AM
29779+ if (au_ftest_si(sbi, NO_DREVAL)) {
29780+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
29781+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
29782+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
29783+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
29784+ } else {
29785+ AuDebugOn(sb->s_d_op == &aufs_dop);
29786+ sb->s_d_op = &aufs_dop;
b95c5147
AM
29787+ AuDebugOn(sbi->si_iop_array == aufs_iop);
29788+ sbi->si_iop_array = aufs_iop;
79b8bda9 29789+ }
b95c5147
AM
29790+ pr_info("reset to %pf and %pf\n",
29791+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
29792+ }
29793+
027c5e7a 29794+ di_write_unlock(root);
b95c5147
AM
29795+ err = au_refresh_d(sb, do_idop);
29796+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
29797+ if (unlikely(e && !err))
29798+ err = e;
1facf9fc 29799+ /* aufs_write_lock() calls ..._child() */
29800+ di_write_lock_child(root);
027c5e7a
AM
29801+
29802+ au_cpup_attr_all(inode, /*force*/1);
29803+
29804+ if (unlikely(err))
29805+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 29806+}
29807+
29808+/* stop extra interpretation of errno in mount(8), and strange error messages */
29809+static int cvt_err(int err)
29810+{
29811+ AuTraceErr(err);
29812+
29813+ switch (err) {
29814+ case -ENOENT:
29815+ case -ENOTDIR:
29816+ case -EEXIST:
29817+ case -EIO:
29818+ err = -EINVAL;
29819+ }
29820+ return err;
29821+}
29822+
29823+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
29824+{
4a4d8108
AM
29825+ int err, do_dx;
29826+ unsigned int mntflags;
be52b249
AM
29827+ struct au_opts opts = {
29828+ .opt = NULL
29829+ };
1facf9fc 29830+ struct dentry *root;
29831+ struct inode *inode;
29832+ struct au_sbinfo *sbinfo;
29833+
29834+ err = 0;
29835+ root = sb->s_root;
29836+ if (!data || !*data) {
e49829fe
JR
29837+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
29838+ if (!err) {
29839+ di_write_lock_child(root);
29840+ err = au_opts_verify(sb, *flags, /*pending*/0);
29841+ aufs_write_unlock(root);
29842+ }
1facf9fc 29843+ goto out;
29844+ }
29845+
29846+ err = -ENOMEM;
1facf9fc 29847+ opts.opt = (void *)__get_free_page(GFP_NOFS);
29848+ if (unlikely(!opts.opt))
29849+ goto out;
29850+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
29851+ opts.flags = AuOpts_REMOUNT;
29852+ opts.sb_flags = *flags;
29853+
29854+ /* parse it before aufs lock */
29855+ err = au_opts_parse(sb, data, &opts);
29856+ if (unlikely(err))
29857+ goto out_opts;
29858+
29859+ sbinfo = au_sbi(sb);
5527c038 29860+ inode = d_inode(root);
febd17d6 29861+ inode_lock(inode);
e49829fe
JR
29862+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
29863+ if (unlikely(err))
29864+ goto out_mtx;
29865+ di_write_lock_child(root);
1facf9fc 29866+
29867+ /* au_opts_remount() may return an error */
29868+ err = au_opts_remount(sb, &opts);
29869+ au_opts_free(&opts);
29870+
027c5e7a 29871+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 29872+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 29873+
4a4d8108
AM
29874+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
29875+ mntflags = au_mntflags(sb);
29876+ do_dx = !!au_opt_test(mntflags, DIO);
29877+ au_dy_arefresh(do_dx);
29878+ }
29879+
076b876e 29880+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 29881+ aufs_write_unlock(root);
953406b4 29882+
e49829fe 29883+out_mtx:
febd17d6 29884+ inode_unlock(inode);
4f0767ce 29885+out_opts:
1c60b727 29886+ free_page((unsigned long)opts.opt);
4f0767ce 29887+out:
1facf9fc 29888+ err = cvt_err(err);
29889+ AuTraceErr(err);
29890+ return err;
29891+}
29892+
4a4d8108 29893+static const struct super_operations aufs_sop = {
1facf9fc 29894+ .alloc_inode = aufs_alloc_inode,
29895+ .destroy_inode = aufs_destroy_inode,
b752ccd1 29896+ /* always deleting, no clearing */
1facf9fc 29897+ .drop_inode = generic_delete_inode,
29898+ .show_options = aufs_show_options,
29899+ .statfs = aufs_statfs,
29900+ .put_super = aufs_put_super,
537831f9 29901+ .sync_fs = aufs_sync_fs,
1facf9fc 29902+ .remount_fs = aufs_remount_fs
29903+};
29904+
29905+/* ---------------------------------------------------------------------- */
29906+
29907+static int alloc_root(struct super_block *sb)
29908+{
29909+ int err;
29910+ struct inode *inode;
29911+ struct dentry *root;
29912+
29913+ err = -ENOMEM;
29914+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
29915+ err = PTR_ERR(inode);
29916+ if (IS_ERR(inode))
29917+ goto out;
29918+
b95c5147 29919+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 29920+ inode->i_fop = &aufs_dir_fop;
29921+ inode->i_mode = S_IFDIR;
9dbd164d 29922+ set_nlink(inode, 2);
1facf9fc 29923+ unlock_new_inode(inode);
29924+
92d182d2 29925+ root = d_make_root(inode);
1facf9fc 29926+ if (unlikely(!root))
92d182d2 29927+ goto out;
1facf9fc 29928+ err = PTR_ERR(root);
29929+ if (IS_ERR(root))
92d182d2 29930+ goto out;
1facf9fc 29931+
4a4d8108 29932+ err = au_di_init(root);
1facf9fc 29933+ if (!err) {
29934+ sb->s_root = root;
29935+ return 0; /* success */
29936+ }
29937+ dput(root);
1facf9fc 29938+
4f0767ce 29939+out:
1facf9fc 29940+ return err;
1facf9fc 29941+}
29942+
29943+static int aufs_fill_super(struct super_block *sb, void *raw_data,
29944+ int silent __maybe_unused)
29945+{
29946+ int err;
be52b249
AM
29947+ struct au_opts opts = {
29948+ .opt = NULL
29949+ };
79b8bda9 29950+ struct au_sbinfo *sbinfo;
1facf9fc 29951+ struct dentry *root;
29952+ struct inode *inode;
29953+ char *arg = raw_data;
29954+
29955+ if (unlikely(!arg || !*arg)) {
29956+ err = -EINVAL;
4a4d8108 29957+ pr_err("no arg\n");
1facf9fc 29958+ goto out;
29959+ }
29960+
29961+ err = -ENOMEM;
1facf9fc 29962+ opts.opt = (void *)__get_free_page(GFP_NOFS);
29963+ if (unlikely(!opts.opt))
29964+ goto out;
29965+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
29966+ opts.sb_flags = sb->s_flags;
29967+
29968+ err = au_si_alloc(sb);
29969+ if (unlikely(err))
29970+ goto out_opts;
79b8bda9 29971+ sbinfo = au_sbi(sb);
1facf9fc 29972+
29973+ /* all timestamps always follow the ones on the branch */
2121bcd9 29974+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
be118d29 29975+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
1facf9fc 29976+ sb->s_op = &aufs_sop;
027c5e7a 29977+ sb->s_d_op = &aufs_dop;
1facf9fc 29978+ sb->s_magic = AUFS_SUPER_MAGIC;
29979+ sb->s_maxbytes = 0;
c1595e42 29980+ sb->s_stack_depth = 1;
1facf9fc 29981+ au_export_init(sb);
f2c43d5f 29982+ au_xattr_init(sb);
1facf9fc 29983+
29984+ err = alloc_root(sb);
29985+ if (unlikely(err)) {
29986+ si_write_unlock(sb);
29987+ goto out_info;
29988+ }
29989+ root = sb->s_root;
5527c038 29990+ inode = d_inode(root);
1facf9fc 29991+
29992+ /*
29993+ * actually we can parse options regardless aufs lock here.
29994+ * but at remount time, parsing must be done before aufs lock.
29995+ * so we follow the same rule.
29996+ */
29997+ ii_write_lock_parent(inode);
29998+ aufs_write_unlock(root);
29999+ err = au_opts_parse(sb, arg, &opts);
30000+ if (unlikely(err))
30001+ goto out_root;
30002+
30003+ /* lock vfs_inode first, then aufs. */
febd17d6 30004+ inode_lock(inode);
1facf9fc 30005+ aufs_write_lock(root);
30006+ err = au_opts_mount(sb, &opts);
30007+ au_opts_free(&opts);
79b8bda9
AM
30008+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30009+ sb->s_d_op = &aufs_dop_noreval;
30010+ pr_info("%pf\n", sb->s_d_op);
30011+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30012+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30013+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30014+ }
1facf9fc 30015+ aufs_write_unlock(root);
febd17d6 30016+ inode_unlock(inode);
4a4d8108
AM
30017+ if (!err)
30018+ goto out_opts; /* success */
1facf9fc 30019+
4f0767ce 30020+out_root:
1facf9fc 30021+ dput(root);
30022+ sb->s_root = NULL;
4f0767ce 30023+out_info:
79b8bda9
AM
30024+ dbgaufs_si_fin(sbinfo);
30025+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30026+ sb->s_fs_info = NULL;
4f0767ce 30027+out_opts:
1c60b727 30028+ free_page((unsigned long)opts.opt);
4f0767ce 30029+out:
1facf9fc 30030+ AuTraceErr(err);
30031+ err = cvt_err(err);
30032+ AuTraceErr(err);
30033+ return err;
30034+}
30035+
30036+/* ---------------------------------------------------------------------- */
30037+
027c5e7a
AM
30038+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30039+ const char *dev_name __maybe_unused,
30040+ void *raw_data)
1facf9fc 30041+{
027c5e7a 30042+ struct dentry *root;
1facf9fc 30043+ struct super_block *sb;
30044+
30045+ /* all timestamps always follow the ones on the branch */
30046+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30047+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30048+ if (IS_ERR(root))
30049+ goto out;
30050+
30051+ sb = root->d_sb;
30052+ si_write_lock(sb, !AuLock_FLUSH);
30053+ sysaufs_brs_add(sb, 0);
30054+ si_write_unlock(sb);
30055+ au_sbilist_add(sb);
30056+
30057+out:
30058+ return root;
1facf9fc 30059+}
30060+
e49829fe
JR
30061+static void aufs_kill_sb(struct super_block *sb)
30062+{
30063+ struct au_sbinfo *sbinfo;
30064+
30065+ sbinfo = au_sbi(sb);
30066+ if (sbinfo) {
30067+ au_sbilist_del(sb);
30068+ aufs_write_lock(sb->s_root);
076b876e 30069+ au_fhsm_fin(sb);
e49829fe
JR
30070+ if (sbinfo->si_wbr_create_ops->fin)
30071+ sbinfo->si_wbr_create_ops->fin(sb);
30072+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30073+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30074+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30075+ }
30076+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30077+ au_plink_put(sb, /*verbose*/1);
30078+ au_xino_clr(sb);
8b6a4947 30079+ au_dr_opt_flush(sb);
1e00d052 30080+ sbinfo->si_sb = NULL;
e49829fe 30081+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30082+ au_nwt_flush(&sbinfo->si_nowait);
30083+ }
98d9a5b1 30084+ kill_anon_super(sb);
e49829fe
JR
30085+}
30086+
1facf9fc 30087+struct file_system_type aufs_fs_type = {
30088+ .name = AUFS_FSTYPE,
c06a8ce3
AM
30089+ /* a race between rename and others */
30090+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 30091+ .mount = aufs_mount,
e49829fe 30092+ .kill_sb = aufs_kill_sb,
1facf9fc 30093+ /* no need to __module_get() and module_put(). */
30094+ .owner = THIS_MODULE,
30095+};
7f207e10
AM
30096diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30097--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 30098+++ linux/fs/aufs/super.h 2018-06-04 09:08:09.188079511 +0200
8b6a4947 30099@@ -0,0 +1,626 @@
1facf9fc 30100+/*
b00004a5 30101+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30102+ *
30103+ * This program, aufs is free software; you can redistribute it and/or modify
30104+ * it under the terms of the GNU General Public License as published by
30105+ * the Free Software Foundation; either version 2 of the License, or
30106+ * (at your option) any later version.
dece6358
AM
30107+ *
30108+ * This program is distributed in the hope that it will be useful,
30109+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30110+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30111+ * GNU General Public License for more details.
30112+ *
30113+ * You should have received a copy of the GNU General Public License
523b37e3 30114+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30115+ */
30116+
30117+/*
30118+ * super_block operations
30119+ */
30120+
30121+#ifndef __AUFS_SUPER_H__
30122+#define __AUFS_SUPER_H__
30123+
30124+#ifdef __KERNEL__
30125+
30126+#include <linux/fs.h>
5527c038 30127+#include <linux/kobject.h>
8b6a4947 30128+#include "hbl.h"
1facf9fc 30129+#include "rwsem.h"
1facf9fc 30130+#include "wkq.h"
30131+
1facf9fc 30132+/* policies to select one among multiple writable branches */
30133+struct au_wbr_copyup_operations {
30134+ int (*copyup)(struct dentry *dentry);
30135+};
30136+
392086de
AM
30137+#define AuWbr_DIR 1 /* target is a dir */
30138+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30139+
30140+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30141+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30142+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30143+
1facf9fc 30144+struct au_wbr_create_operations {
392086de 30145+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30146+ int (*init)(struct super_block *sb);
30147+ int (*fin)(struct super_block *sb);
30148+};
30149+
30150+struct au_wbr_mfs {
30151+ struct mutex mfs_lock; /* protect this structure */
30152+ unsigned long mfs_jiffy;
30153+ unsigned long mfs_expire;
30154+ aufs_bindex_t mfs_bindex;
30155+
30156+ unsigned long long mfsrr_bytes;
30157+ unsigned long long mfsrr_watermark;
30158+};
30159+
86dc4139
AM
30160+#define AuPlink_NHASH 100
30161+static inline int au_plink_hash(ino_t ino)
30162+{
30163+ return ino % AuPlink_NHASH;
30164+}
30165+
076b876e
AM
30166+/* File-based Hierarchical Storage Management */
30167+struct au_fhsm {
30168+#ifdef CONFIG_AUFS_FHSM
30169+ /* allow only one process who can receive the notification */
30170+ spinlock_t fhsm_spin;
30171+ pid_t fhsm_pid;
30172+ wait_queue_head_t fhsm_wqh;
30173+ atomic_t fhsm_readable;
30174+
c1595e42 30175+ /* these are protected by si_rwsem */
076b876e 30176+ unsigned long fhsm_expire;
c1595e42 30177+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30178+#endif
30179+};
30180+
1facf9fc 30181+struct au_branch;
30182+struct au_sbinfo {
30183+ /* nowait tasks in the system-wide workqueue */
30184+ struct au_nowait_tasks si_nowait;
30185+
b752ccd1
AM
30186+ /*
30187+ * tried sb->s_umount, but failed due to the dependecy between i_mutex.
30188+ * rwsem for au_sbinfo is necessary.
30189+ */
dece6358 30190+ struct au_rwsem si_rwsem;
1facf9fc 30191+
7f207e10 30192+ /*
523b37e3
AM
30193+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30194+ * remount.
7f207e10 30195+ */
5afbbe0d 30196+ struct percpu_counter si_ninodes, si_nfiles;
7f207e10 30197+
1facf9fc 30198+ /* branch management */
30199+ unsigned int si_generation;
30200+
2000de60 30201+ /* see AuSi_ flags */
1facf9fc 30202+ unsigned char au_si_status;
30203+
5afbbe0d 30204+ aufs_bindex_t si_bbot;
7f207e10
AM
30205+
30206+ /* dirty trick to keep br_id plus */
30207+ unsigned int si_last_br_id :
30208+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30209+ struct au_branch **si_branch;
30210+
30211+ /* policy to select a writable branch */
30212+ unsigned char si_wbr_copyup;
30213+ unsigned char si_wbr_create;
30214+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30215+ struct au_wbr_create_operations *si_wbr_create_ops;
30216+
30217+ /* round robin */
30218+ atomic_t si_wbr_rr_next;
30219+
30220+ /* most free space */
30221+ struct au_wbr_mfs si_wbr_mfs;
30222+
076b876e
AM
30223+ /* File-based Hierarchical Storage Management */
30224+ struct au_fhsm si_fhsm;
30225+
1facf9fc 30226+ /* mount flags */
30227+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30228+ unsigned int si_mntflags;
30229+
30230+ /* external inode number (bitmap and translation table) */
5527c038
JR
30231+ vfs_readf_t si_xread;
30232+ vfs_writef_t si_xwrite;
1facf9fc 30233+ struct file *si_xib;
30234+ struct mutex si_xib_mtx; /* protect xib members */
30235+ unsigned long *si_xib_buf;
30236+ unsigned long si_xib_last_pindex;
30237+ int si_xib_next_bit;
30238+ aufs_bindex_t si_xino_brid;
392086de
AM
30239+ unsigned long si_xino_jiffy;
30240+ unsigned long si_xino_expire;
1facf9fc 30241+ /* reserved for future use */
30242+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30243+
30244+#ifdef CONFIG_AUFS_EXPORT
30245+ /* i_generation */
30246+ struct file *si_xigen;
30247+ atomic_t si_xigen_next;
30248+#endif
30249+
b912730e 30250+ /* dirty trick to suppoer atomic_open */
8b6a4947 30251+ struct hlist_bl_head si_aopen;
b912730e 30252+
1facf9fc 30253+ /* vdir parameters */
e49829fe 30254+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30255+ unsigned int si_rdblk; /* deblk size */
30256+ unsigned int si_rdhash; /* hash size */
30257+
30258+ /*
30259+ * If the number of whiteouts are larger than si_dirwh, leave all of
30260+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30261+ * future fsck.aufs or kernel thread will remove them later.
30262+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30263+ */
30264+ unsigned int si_dirwh;
30265+
1facf9fc 30266+ /* pseudo_link list */
8b6a4947 30267+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30268+ wait_queue_head_t si_plink_wq;
4a4d8108 30269+ spinlock_t si_plink_maint_lock;
e49829fe 30270+ pid_t si_plink_maint_pid;
1facf9fc 30271+
523b37e3 30272+ /* file list */
8b6a4947 30273+ struct hlist_bl_head si_files;
523b37e3 30274+
b95c5147
AM
30275+ /* with/without getattr, brother of sb->s_d_op */
30276+ struct inode_operations *si_iop_array;
30277+
1facf9fc 30278+ /*
30279+ * sysfs and lifetime management.
30280+ * this is not a small structure and it may be a waste of memory in case
30281+ * of sysfs is disabled, particulary when many aufs-es are mounted.
30282+ * but using sysfs is majority.
30283+ */
30284+ struct kobject si_kobj;
30285+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30286+ struct dentry *si_dbgaufs;
30287+ struct dentry *si_dbgaufs_plink;
30288+ struct dentry *si_dbgaufs_xib;
1facf9fc 30289+#ifdef CONFIG_AUFS_EXPORT
30290+ struct dentry *si_dbgaufs_xigen;
30291+#endif
30292+#endif
30293+
e49829fe 30294+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30295+ struct hlist_bl_node si_list;
e49829fe
JR
30296+#endif
30297+
1facf9fc 30298+ /* dirty, necessary for unmounting, sysfs and sysrq */
30299+ struct super_block *si_sb;
30300+};
30301+
dece6358
AM
30302+/* sbinfo status flags */
30303+/*
30304+ * set true when refresh_dirs() failed at remount time.
30305+ * then try refreshing dirs at access time again.
30306+ * if it is false, refreshing dirs at access time is unnecesary
30307+ */
027c5e7a 30308+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30309+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30310+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30311+
30312+#ifndef CONFIG_AUFS_FHSM
30313+#undef AuSi_FHSM
30314+#define AuSi_FHSM 0
30315+#endif
30316+
dece6358
AM
30317+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30318+ unsigned int flag)
30319+{
30320+ AuRwMustAnyLock(&sbi->si_rwsem);
30321+ return sbi->au_si_status & flag;
30322+}
30323+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30324+#define au_fset_si(sbinfo, name) do { \
30325+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30326+ (sbinfo)->au_si_status |= AuSi_##name; \
30327+} while (0)
30328+#define au_fclr_si(sbinfo, name) do { \
30329+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30330+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30331+} while (0)
30332+
1facf9fc 30333+/* ---------------------------------------------------------------------- */
30334+
30335+/* policy to select one among writable branches */
4a4d8108
AM
30336+#define AuWbrCopyup(sbinfo, ...) \
30337+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30338+#define AuWbrCreate(sbinfo, ...) \
30339+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30340+
30341+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30342+#define AuLock_DW 1 /* write-lock dentry */
30343+#define AuLock_IR (1 << 1) /* read-lock inode */
30344+#define AuLock_IW (1 << 2) /* write-lock inode */
30345+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30346+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30347+ /* except RENAME_EXCHANGE */
e49829fe
JR
30348+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30349+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30350+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30351+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30352+#define au_fset_lock(flags, name) \
30353+ do { (flags) |= AuLock_##name; } while (0)
30354+#define au_fclr_lock(flags, name) \
30355+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30356+
30357+/* ---------------------------------------------------------------------- */
30358+
30359+/* super.c */
30360+extern struct file_system_type aufs_fs_type;
30361+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30362+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30363+ unsigned long long max, void *arg);
79b8bda9
AM
30364+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30365+ struct super_block *sb, void *arg);
7f207e10
AM
30366+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30367+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30368+
30369+/* sbinfo.c */
30370+void au_si_free(struct kobject *kobj);
30371+int au_si_alloc(struct super_block *sb);
e2f27e51 30372+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30373+
30374+unsigned int au_sigen_inc(struct super_block *sb);
30375+aufs_bindex_t au_new_br_id(struct super_block *sb);
30376+
e49829fe
JR
30377+int si_read_lock(struct super_block *sb, int flags);
30378+int si_write_lock(struct super_block *sb, int flags);
30379+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30380+void aufs_read_unlock(struct dentry *dentry, int flags);
30381+void aufs_write_lock(struct dentry *dentry);
30382+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30383+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30384+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30385+
30386+/* wbr_policy.c */
30387+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30388+extern struct au_wbr_create_operations au_wbr_create_ops[];
30389+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30390+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30391+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30392+
30393+/* mvdown.c */
30394+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30395+
076b876e
AM
30396+#ifdef CONFIG_AUFS_FHSM
30397+/* fhsm.c */
30398+
30399+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30400+{
30401+ pid_t pid;
30402+
30403+ spin_lock(&fhsm->fhsm_spin);
30404+ pid = fhsm->fhsm_pid;
30405+ spin_unlock(&fhsm->fhsm_spin);
30406+
30407+ return pid;
30408+}
30409+
30410+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30411+void au_fhsm_wrote_all(struct super_block *sb, int force);
30412+int au_fhsm_fd(struct super_block *sb, int oflags);
30413+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30414+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30415+void au_fhsm_fin(struct super_block *sb);
30416+void au_fhsm_init(struct au_sbinfo *sbinfo);
30417+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30418+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30419+#else
30420+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30421+ int force)
30422+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30423+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30424+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30425+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30426+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30427+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30428+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30429+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30430+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30431+#endif
30432+
1facf9fc 30433+/* ---------------------------------------------------------------------- */
30434+
30435+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30436+{
30437+ return sb->s_fs_info;
30438+}
30439+
30440+/* ---------------------------------------------------------------------- */
30441+
30442+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30443+int au_test_nfsd(void);
1facf9fc 30444+void au_export_init(struct super_block *sb);
b752ccd1 30445+void au_xigen_inc(struct inode *inode);
1facf9fc 30446+int au_xigen_new(struct inode *inode);
30447+int au_xigen_set(struct super_block *sb, struct file *base);
30448+void au_xigen_clr(struct super_block *sb);
30449+
30450+static inline int au_busy_or_stale(void)
30451+{
b752ccd1 30452+ if (!au_test_nfsd())
1facf9fc 30453+ return -EBUSY;
30454+ return -ESTALE;
30455+}
30456+#else
b752ccd1 30457+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30458+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30459+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108
AM
30460+AuStubInt0(au_xigen_new, struct inode *inode)
30461+AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
30462+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30463+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30464+#endif /* CONFIG_AUFS_EXPORT */
30465+
30466+/* ---------------------------------------------------------------------- */
30467+
e49829fe
JR
30468+#ifdef CONFIG_AUFS_SBILIST
30469+/* module.c */
8b6a4947 30470+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30471+
30472+static inline void au_sbilist_init(void)
30473+{
8b6a4947 30474+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30475+}
30476+
30477+static inline void au_sbilist_add(struct super_block *sb)
30478+{
8b6a4947 30479+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30480+}
30481+
30482+static inline void au_sbilist_del(struct super_block *sb)
30483+{
8b6a4947 30484+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30485+}
53392da6
AM
30486+
30487+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30488+static inline void au_sbilist_lock(void)
30489+{
8b6a4947 30490+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30491+}
30492+
30493+static inline void au_sbilist_unlock(void)
30494+{
8b6a4947 30495+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30496+}
30497+#define AuGFP_SBILIST GFP_ATOMIC
30498+#else
30499+AuStubVoid(au_sbilist_lock, void)
30500+AuStubVoid(au_sbilist_unlock, void)
30501+#define AuGFP_SBILIST GFP_NOFS
30502+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
30503+#else
30504+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
30505+AuStubVoid(au_sbilist_add, struct super_block *sb)
30506+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
30507+AuStubVoid(au_sbilist_lock, void)
30508+AuStubVoid(au_sbilist_unlock, void)
30509+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
30510+#endif
30511+
30512+/* ---------------------------------------------------------------------- */
30513+
1facf9fc 30514+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30515+{
dece6358 30516+ /*
c1595e42 30517+ * This function is a dynamic '__init' function actually,
dece6358
AM
30518+ * so the tiny check for si_rwsem is unnecessary.
30519+ */
30520+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 30521+#ifdef CONFIG_DEBUG_FS
30522+ sbinfo->si_dbgaufs = NULL;
86dc4139 30523+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 30524+ sbinfo->si_dbgaufs_xib = NULL;
30525+#ifdef CONFIG_AUFS_EXPORT
30526+ sbinfo->si_dbgaufs_xigen = NULL;
30527+#endif
30528+#endif
30529+}
30530+
30531+/* ---------------------------------------------------------------------- */
30532+
a2654f78
AM
30533+/* current->atomic_flags */
30534+/* this value should never corrupt the ones defined in linux/sched.h */
30535+#define PFA_AUFS 7
30536+
30537+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30538+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
30539+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
30540+
30541+static inline int si_pid_test(struct super_block *sb)
30542+{
a2654f78 30543+ return !!task_test_aufs(current);
b752ccd1
AM
30544+}
30545+
30546+static inline void si_pid_clr(struct super_block *sb)
30547+{
a2654f78
AM
30548+ AuDebugOn(!task_test_aufs(current));
30549+ task_clear_aufs(current);
b752ccd1
AM
30550+}
30551+
a2654f78
AM
30552+static inline void si_pid_set(struct super_block *sb)
30553+{
30554+ AuDebugOn(task_test_aufs(current));
30555+ task_set_aufs(current);
30556+}
febd17d6 30557+
b752ccd1
AM
30558+/* ---------------------------------------------------------------------- */
30559+
1facf9fc 30560+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
30561+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30562+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30563+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30564+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30565+/*
30566+#define __si_read_trylock_nested(sb) \
30567+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30568+#define __si_write_trylock_nested(sb) \
30569+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30570+*/
30571+
30572+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30573+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30574+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 30575+
dece6358
AM
30576+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30577+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30578+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30579+
b752ccd1
AM
30580+static inline void si_noflush_read_lock(struct super_block *sb)
30581+{
30582+ __si_read_lock(sb);
30583+ si_pid_set(sb);
30584+}
30585+
30586+static inline int si_noflush_read_trylock(struct super_block *sb)
30587+{
076b876e
AM
30588+ int locked;
30589+
30590+ locked = __si_read_trylock(sb);
b752ccd1
AM
30591+ if (locked)
30592+ si_pid_set(sb);
30593+ return locked;
30594+}
30595+
30596+static inline void si_noflush_write_lock(struct super_block *sb)
30597+{
30598+ __si_write_lock(sb);
30599+ si_pid_set(sb);
30600+}
30601+
30602+static inline int si_noflush_write_trylock(struct super_block *sb)
30603+{
076b876e
AM
30604+ int locked;
30605+
30606+ locked = __si_write_trylock(sb);
b752ccd1
AM
30607+ if (locked)
30608+ si_pid_set(sb);
30609+ return locked;
30610+}
30611+
7e9cd9fe 30612+#if 0 /* reserved */
1facf9fc 30613+static inline int si_read_trylock(struct super_block *sb, int flags)
30614+{
30615+ if (au_ftest_lock(flags, FLUSH))
30616+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30617+ return si_noflush_read_trylock(sb);
30618+}
e49829fe 30619+#endif
1facf9fc 30620+
b752ccd1
AM
30621+static inline void si_read_unlock(struct super_block *sb)
30622+{
30623+ si_pid_clr(sb);
30624+ __si_read_unlock(sb);
30625+}
30626+
7e9cd9fe 30627+#if 0 /* reserved */
1facf9fc 30628+static inline int si_write_trylock(struct super_block *sb, int flags)
30629+{
30630+ if (au_ftest_lock(flags, FLUSH))
30631+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30632+ return si_noflush_write_trylock(sb);
30633+}
b752ccd1
AM
30634+#endif
30635+
30636+static inline void si_write_unlock(struct super_block *sb)
30637+{
30638+ si_pid_clr(sb);
30639+ __si_write_unlock(sb);
30640+}
30641+
7e9cd9fe 30642+#if 0 /* reserved */
b752ccd1
AM
30643+static inline void si_downgrade_lock(struct super_block *sb)
30644+{
30645+ __si_downgrade_lock(sb);
30646+}
30647+#endif
1facf9fc 30648+
30649+/* ---------------------------------------------------------------------- */
30650+
5afbbe0d 30651+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 30652+{
dece6358 30653+ SiMustAnyLock(sb);
5afbbe0d 30654+ return au_sbi(sb)->si_bbot;
1facf9fc 30655+}
30656+
30657+static inline unsigned int au_mntflags(struct super_block *sb)
30658+{
dece6358 30659+ SiMustAnyLock(sb);
1facf9fc 30660+ return au_sbi(sb)->si_mntflags;
30661+}
30662+
30663+static inline unsigned int au_sigen(struct super_block *sb)
30664+{
dece6358 30665+ SiMustAnyLock(sb);
1facf9fc 30666+ return au_sbi(sb)->si_generation;
30667+}
30668+
5afbbe0d
AM
30669+static inline unsigned long long au_ninodes(struct super_block *sb)
30670+{
30671+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
30672+
30673+ BUG_ON(n < 0);
30674+ return n;
30675+}
30676+
7f207e10
AM
30677+static inline void au_ninodes_inc(struct super_block *sb)
30678+{
5afbbe0d 30679+ percpu_counter_inc(&au_sbi(sb)->si_ninodes);
7f207e10
AM
30680+}
30681+
30682+static inline void au_ninodes_dec(struct super_block *sb)
30683+{
5afbbe0d
AM
30684+ percpu_counter_dec(&au_sbi(sb)->si_ninodes);
30685+}
30686+
30687+static inline unsigned long long au_nfiles(struct super_block *sb)
30688+{
30689+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
30690+
30691+ BUG_ON(n < 0);
30692+ return n;
7f207e10
AM
30693+}
30694+
30695+static inline void au_nfiles_inc(struct super_block *sb)
30696+{
5afbbe0d 30697+ percpu_counter_inc(&au_sbi(sb)->si_nfiles);
7f207e10
AM
30698+}
30699+
30700+static inline void au_nfiles_dec(struct super_block *sb)
30701+{
5afbbe0d 30702+ percpu_counter_dec(&au_sbi(sb)->si_nfiles);
7f207e10
AM
30703+}
30704+
1facf9fc 30705+static inline struct au_branch *au_sbr(struct super_block *sb,
30706+ aufs_bindex_t bindex)
30707+{
dece6358 30708+ SiMustAnyLock(sb);
1facf9fc 30709+ return au_sbi(sb)->si_branch[0 + bindex];
30710+}
30711+
30712+static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
30713+{
dece6358 30714+ SiMustWriteLock(sb);
1facf9fc 30715+ au_sbi(sb)->si_xino_brid = brid;
30716+}
30717+
30718+static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
30719+{
dece6358 30720+ SiMustAnyLock(sb);
1facf9fc 30721+ return au_sbi(sb)->si_xino_brid;
30722+}
30723+
30724+#endif /* __KERNEL__ */
30725+#endif /* __AUFS_SUPER_H__ */
7f207e10
AM
30726diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
30727--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 30728+++ linux/fs/aufs/sysaufs.c 2018-04-15 08:49:13.404484168 +0200
523b37e3 30729@@ -0,0 +1,104 @@
1facf9fc 30730+/*
b00004a5 30731+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30732+ *
30733+ * This program, aufs is free software; you can redistribute it and/or modify
30734+ * it under the terms of the GNU General Public License as published by
30735+ * the Free Software Foundation; either version 2 of the License, or
30736+ * (at your option) any later version.
dece6358
AM
30737+ *
30738+ * This program is distributed in the hope that it will be useful,
30739+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30740+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30741+ * GNU General Public License for more details.
30742+ *
30743+ * You should have received a copy of the GNU General Public License
523b37e3 30744+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30745+ */
30746+
30747+/*
30748+ * sysfs interface and lifetime management
30749+ * they are necessary regardless sysfs is disabled.
30750+ */
30751+
1facf9fc 30752+#include <linux/random.h>
1facf9fc 30753+#include "aufs.h"
30754+
30755+unsigned long sysaufs_si_mask;
e49829fe 30756+struct kset *sysaufs_kset;
1facf9fc 30757+
30758+#define AuSiAttr(_name) { \
30759+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
30760+ .show = sysaufs_si_##_name, \
30761+}
30762+
30763+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
30764+struct attribute *sysaufs_si_attrs[] = {
30765+ &sysaufs_si_attr_xi_path.attr,
30766+ NULL,
30767+};
30768+
4a4d8108 30769+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 30770+ .show = sysaufs_si_show
30771+};
30772+
30773+static struct kobj_type au_sbi_ktype = {
30774+ .release = au_si_free,
30775+ .sysfs_ops = &au_sbi_ops,
30776+ .default_attrs = sysaufs_si_attrs
30777+};
30778+
30779+/* ---------------------------------------------------------------------- */
30780+
30781+int sysaufs_si_init(struct au_sbinfo *sbinfo)
30782+{
30783+ int err;
30784+
e49829fe 30785+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 30786+ /* cf. sysaufs_name() */
30787+ err = kobject_init_and_add
e49829fe 30788+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 30789+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
30790+
30791+ dbgaufs_si_null(sbinfo);
30792+ if (!err) {
30793+ err = dbgaufs_si_init(sbinfo);
30794+ if (unlikely(err))
30795+ kobject_put(&sbinfo->si_kobj);
30796+ }
30797+ return err;
30798+}
30799+
30800+void sysaufs_fin(void)
30801+{
30802+ dbgaufs_fin();
e49829fe
JR
30803+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
30804+ kset_unregister(sysaufs_kset);
1facf9fc 30805+}
30806+
30807+int __init sysaufs_init(void)
30808+{
30809+ int err;
30810+
30811+ do {
30812+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
30813+ } while (!sysaufs_si_mask);
30814+
4a4d8108 30815+ err = -EINVAL;
e49829fe
JR
30816+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
30817+ if (unlikely(!sysaufs_kset))
4a4d8108 30818+ goto out;
e49829fe
JR
30819+ err = PTR_ERR(sysaufs_kset);
30820+ if (IS_ERR(sysaufs_kset))
1facf9fc 30821+ goto out;
e49829fe 30822+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
1facf9fc 30823+ if (unlikely(err)) {
e49829fe 30824+ kset_unregister(sysaufs_kset);
1facf9fc 30825+ goto out;
30826+ }
30827+
30828+ err = dbgaufs_init();
30829+ if (unlikely(err))
30830+ sysaufs_fin();
4f0767ce 30831+out:
1facf9fc 30832+ return err;
30833+}
7f207e10
AM
30834diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
30835--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 30836+++ linux/fs/aufs/sysaufs.h 2018-04-15 08:49:13.404484168 +0200
c1595e42 30837@@ -0,0 +1,101 @@
1facf9fc 30838+/*
b00004a5 30839+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30840+ *
30841+ * This program, aufs is free software; you can redistribute it and/or modify
30842+ * it under the terms of the GNU General Public License as published by
30843+ * the Free Software Foundation; either version 2 of the License, or
30844+ * (at your option) any later version.
dece6358
AM
30845+ *
30846+ * This program is distributed in the hope that it will be useful,
30847+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30848+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30849+ * GNU General Public License for more details.
30850+ *
30851+ * You should have received a copy of the GNU General Public License
523b37e3 30852+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30853+ */
30854+
30855+/*
30856+ * sysfs interface and mount lifetime management
30857+ */
30858+
30859+#ifndef __SYSAUFS_H__
30860+#define __SYSAUFS_H__
30861+
30862+#ifdef __KERNEL__
30863+
1facf9fc 30864+#include <linux/sysfs.h>
1facf9fc 30865+#include "module.h"
30866+
dece6358
AM
30867+struct super_block;
30868+struct au_sbinfo;
30869+
1facf9fc 30870+struct sysaufs_si_attr {
30871+ struct attribute attr;
30872+ int (*show)(struct seq_file *seq, struct super_block *sb);
30873+};
30874+
30875+/* ---------------------------------------------------------------------- */
30876+
30877+/* sysaufs.c */
30878+extern unsigned long sysaufs_si_mask;
e49829fe 30879+extern struct kset *sysaufs_kset;
1facf9fc 30880+extern struct attribute *sysaufs_si_attrs[];
30881+int sysaufs_si_init(struct au_sbinfo *sbinfo);
30882+int __init sysaufs_init(void);
30883+void sysaufs_fin(void);
30884+
30885+/* ---------------------------------------------------------------------- */
30886+
30887+/* some people doesn't like to show a pointer in kernel */
30888+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
30889+{
30890+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
30891+}
30892+
30893+#define SysaufsSiNamePrefix "si_"
30894+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
30895+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
30896+{
30897+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
30898+ sysaufs_si_id(sbinfo));
30899+}
30900+
30901+struct au_branch;
30902+#ifdef CONFIG_SYSFS
30903+/* sysfs.c */
30904+extern struct attribute_group *sysaufs_attr_group;
30905+
30906+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
30907+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
30908+ char *buf);
076b876e
AM
30909+long au_brinfo_ioctl(struct file *file, unsigned long arg);
30910+#ifdef CONFIG_COMPAT
30911+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
30912+#endif
1facf9fc 30913+
30914+void sysaufs_br_init(struct au_branch *br);
30915+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
30916+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
30917+
30918+#define sysaufs_brs_init() do {} while (0)
30919+
30920+#else
30921+#define sysaufs_attr_group NULL
30922+
4a4d8108 30923+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
30924+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
30925+ struct attribute *attr, char *buf)
4a4d8108
AM
30926+AuStubVoid(sysaufs_br_init, struct au_branch *br)
30927+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
30928+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 30929+
30930+static inline void sysaufs_brs_init(void)
30931+{
30932+ sysaufs_brs = 0;
30933+}
30934+
30935+#endif /* CONFIG_SYSFS */
30936+
30937+#endif /* __KERNEL__ */
30938+#endif /* __SYSAUFS_H__ */
7f207e10
AM
30939diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
30940--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 30941+++ linux/fs/aufs/sysfs.c 2018-04-15 08:49:13.404484168 +0200
79b8bda9 30942@@ -0,0 +1,376 @@
1facf9fc 30943+/*
b00004a5 30944+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30945+ *
30946+ * This program, aufs is free software; you can redistribute it and/or modify
30947+ * it under the terms of the GNU General Public License as published by
30948+ * the Free Software Foundation; either version 2 of the License, or
30949+ * (at your option) any later version.
dece6358
AM
30950+ *
30951+ * This program is distributed in the hope that it will be useful,
30952+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30953+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30954+ * GNU General Public License for more details.
30955+ *
30956+ * You should have received a copy of the GNU General Public License
523b37e3 30957+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30958+ */
30959+
30960+/*
30961+ * sysfs interface
30962+ */
30963+
076b876e 30964+#include <linux/compat.h>
1facf9fc 30965+#include <linux/seq_file.h>
1facf9fc 30966+#include "aufs.h"
30967+
4a4d8108
AM
30968+#ifdef CONFIG_AUFS_FS_MODULE
30969+/* this entry violates the "one line per file" policy of sysfs */
30970+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
30971+ char *buf)
30972+{
30973+ ssize_t err;
30974+ static char *conf =
30975+/* this file is generated at compiling */
30976+#include "conf.str"
30977+ ;
30978+
30979+ err = snprintf(buf, PAGE_SIZE, conf);
30980+ if (unlikely(err >= PAGE_SIZE))
30981+ err = -EFBIG;
30982+ return err;
30983+}
30984+
30985+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
30986+#endif
30987+
1facf9fc 30988+static struct attribute *au_attr[] = {
4a4d8108
AM
30989+#ifdef CONFIG_AUFS_FS_MODULE
30990+ &au_config_attr.attr,
30991+#endif
1facf9fc 30992+ NULL, /* need to NULL terminate the list of attributes */
30993+};
30994+
30995+static struct attribute_group sysaufs_attr_group_body = {
30996+ .attrs = au_attr
30997+};
30998+
30999+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31000+
31001+/* ---------------------------------------------------------------------- */
31002+
31003+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31004+{
31005+ int err;
31006+
dece6358
AM
31007+ SiMustAnyLock(sb);
31008+
1facf9fc 31009+ err = 0;
31010+ if (au_opt_test(au_mntflags(sb), XINO)) {
31011+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31012+ seq_putc(seq, '\n');
31013+ }
31014+ return err;
31015+}
31016+
31017+/*
31018+ * the lifetime of branch is independent from the entry under sysfs.
31019+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31020+ * unlinked.
31021+ */
31022+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31023+ aufs_bindex_t bindex, int idx)
1facf9fc 31024+{
1e00d052 31025+ int err;
1facf9fc 31026+ struct path path;
31027+ struct dentry *root;
31028+ struct au_branch *br;
076b876e 31029+ au_br_perm_str_t perm;
1facf9fc 31030+
31031+ AuDbg("b%d\n", bindex);
31032+
1e00d052 31033+ err = 0;
1facf9fc 31034+ root = sb->s_root;
31035+ di_read_lock_parent(root, !AuLock_IR);
31036+ br = au_sbr(sb, bindex);
392086de
AM
31037+
31038+ switch (idx) {
31039+ case AuBrSysfs_BR:
31040+ path.mnt = au_br_mnt(br);
31041+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31042+ err = au_seq_path(seq, &path);
31043+ if (!err) {
31044+ au_optstr_br_perm(&perm, br->br_perm);
31045+ seq_printf(seq, "=%s\n", perm.a);
31046+ }
392086de
AM
31047+ break;
31048+ case AuBrSysfs_BRID:
79b8bda9 31049+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31050+ break;
31051+ }
076b876e 31052+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31053+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31054+ err = -E2BIG;
392086de 31055+
1e00d052 31056+ return err;
1facf9fc 31057+}
31058+
31059+/* ---------------------------------------------------------------------- */
31060+
31061+static struct seq_file *au_seq(char *p, ssize_t len)
31062+{
31063+ struct seq_file *seq;
31064+
31065+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31066+ if (seq) {
31067+ /* mutex_init(&seq.lock); */
31068+ seq->buf = p;
31069+ seq->size = len;
31070+ return seq; /* success */
31071+ }
31072+
31073+ seq = ERR_PTR(-ENOMEM);
31074+ return seq;
31075+}
31076+
392086de
AM
31077+#define SysaufsBr_PREFIX "br"
31078+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31079+
31080+/* todo: file size may exceed PAGE_SIZE */
31081+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31082+ char *buf)
1facf9fc 31083+{
31084+ ssize_t err;
392086de 31085+ int idx;
1facf9fc 31086+ long l;
5afbbe0d 31087+ aufs_bindex_t bbot;
1facf9fc 31088+ struct au_sbinfo *sbinfo;
31089+ struct super_block *sb;
31090+ struct seq_file *seq;
31091+ char *name;
31092+ struct attribute **cattr;
31093+
31094+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31095+ sb = sbinfo->si_sb;
1308ab2a 31096+
31097+ /*
31098+ * prevent a race condition between sysfs and aufs.
31099+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31100+ * prohibits maintaining the sysfs entries.
31101+ * hew we acquire read lock after sysfs_get_active_two().
31102+ * on the other hand, the remount process may maintain the sysfs/aufs
31103+ * entries after acquiring write lock.
31104+ * it can cause a deadlock.
31105+ * simply we gave up processing read here.
31106+ */
31107+ err = -EBUSY;
31108+ if (unlikely(!si_noflush_read_trylock(sb)))
31109+ goto out;
1facf9fc 31110+
31111+ seq = au_seq(buf, PAGE_SIZE);
31112+ err = PTR_ERR(seq);
31113+ if (IS_ERR(seq))
1308ab2a 31114+ goto out_unlock;
1facf9fc 31115+
31116+ name = (void *)attr->name;
31117+ cattr = sysaufs_si_attrs;
31118+ while (*cattr) {
31119+ if (!strcmp(name, (*cattr)->name)) {
31120+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31121+ ->show(seq, sb);
31122+ goto out_seq;
31123+ }
31124+ cattr++;
31125+ }
31126+
392086de
AM
31127+ if (!strncmp(name, SysaufsBrid_PREFIX,
31128+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31129+ idx = AuBrSysfs_BRID;
31130+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31131+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31132+ sizeof(SysaufsBr_PREFIX) - 1)) {
31133+ idx = AuBrSysfs_BR;
1facf9fc 31134+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31135+ } else
31136+ BUG();
31137+
31138+ err = kstrtol(name, 10, &l);
31139+ if (!err) {
5afbbe0d
AM
31140+ bbot = au_sbbot(sb);
31141+ if (l <= bbot)
392086de
AM
31142+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31143+ else
31144+ err = -ENOENT;
1facf9fc 31145+ }
1facf9fc 31146+
4f0767ce 31147+out_seq:
1facf9fc 31148+ if (!err) {
31149+ err = seq->count;
31150+ /* sysfs limit */
31151+ if (unlikely(err == PAGE_SIZE))
31152+ err = -EFBIG;
31153+ }
1c60b727 31154+ kfree(seq);
4f0767ce 31155+out_unlock:
1facf9fc 31156+ si_read_unlock(sb);
4f0767ce 31157+out:
1facf9fc 31158+ return err;
31159+}
31160+
31161+/* ---------------------------------------------------------------------- */
31162+
076b876e
AM
31163+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31164+{
31165+ int err;
31166+ int16_t brid;
5afbbe0d 31167+ aufs_bindex_t bindex, bbot;
076b876e
AM
31168+ size_t sz;
31169+ char *buf;
31170+ struct seq_file *seq;
31171+ struct au_branch *br;
31172+
31173+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31174+ bbot = au_sbbot(sb);
31175+ err = bbot + 1;
076b876e
AM
31176+ if (!arg)
31177+ goto out;
31178+
31179+ err = -ENOMEM;
31180+ buf = (void *)__get_free_page(GFP_NOFS);
31181+ if (unlikely(!buf))
31182+ goto out;
31183+
31184+ seq = au_seq(buf, PAGE_SIZE);
31185+ err = PTR_ERR(seq);
31186+ if (IS_ERR(seq))
31187+ goto out_buf;
31188+
31189+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31190+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
076b876e
AM
31191+ err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
31192+ if (unlikely(err))
31193+ break;
31194+
31195+ br = au_sbr(sb, bindex);
31196+ brid = br->br_id;
31197+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31198+ err = __put_user(brid, &arg->id);
31199+ if (unlikely(err))
31200+ break;
31201+
31202+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31203+ err = __put_user(br->br_perm, &arg->perm);
31204+ if (unlikely(err))
31205+ break;
31206+
79b8bda9
AM
31207+ err = au_seq_path(seq, &br->br_path);
31208+ if (unlikely(err))
31209+ break;
31210+ seq_putc(seq, '\0');
31211+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31212+ err = copy_to_user(arg->path, seq->buf, seq->count);
31213+ seq->count = 0;
31214+ if (unlikely(err))
31215+ break;
31216+ } else {
31217+ err = -E2BIG;
31218+ goto out_seq;
31219+ }
31220+ }
31221+ if (unlikely(err))
31222+ err = -EFAULT;
31223+
31224+out_seq:
1c60b727 31225+ kfree(seq);
076b876e 31226+out_buf:
1c60b727 31227+ free_page((unsigned long)buf);
076b876e
AM
31228+out:
31229+ si_read_unlock(sb);
31230+ return err;
31231+}
31232+
31233+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31234+{
2000de60 31235+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31236+}
31237+
31238+#ifdef CONFIG_COMPAT
31239+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31240+{
2000de60 31241+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31242+}
31243+#endif
31244+
31245+/* ---------------------------------------------------------------------- */
31246+
1facf9fc 31247+void sysaufs_br_init(struct au_branch *br)
31248+{
392086de
AM
31249+ int i;
31250+ struct au_brsysfs *br_sysfs;
31251+ struct attribute *attr;
4a4d8108 31252+
392086de
AM
31253+ br_sysfs = br->br_sysfs;
31254+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31255+ attr = &br_sysfs->attr;
31256+ sysfs_attr_init(attr);
31257+ attr->name = br_sysfs->name;
31258+ attr->mode = S_IRUGO;
31259+ br_sysfs++;
31260+ }
1facf9fc 31261+}
31262+
31263+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31264+{
31265+ struct au_branch *br;
31266+ struct kobject *kobj;
392086de
AM
31267+ struct au_brsysfs *br_sysfs;
31268+ int i;
5afbbe0d 31269+ aufs_bindex_t bbot;
1facf9fc 31270+
31271+ dbgaufs_brs_del(sb, bindex);
31272+
31273+ if (!sysaufs_brs)
31274+ return;
31275+
31276+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31277+ bbot = au_sbbot(sb);
31278+ for (; bindex <= bbot; bindex++) {
1facf9fc 31279+ br = au_sbr(sb, bindex);
392086de
AM
31280+ br_sysfs = br->br_sysfs;
31281+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31282+ sysfs_remove_file(kobj, &br_sysfs->attr);
31283+ br_sysfs++;
31284+ }
1facf9fc 31285+ }
31286+}
31287+
31288+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31289+{
392086de 31290+ int err, i;
5afbbe0d 31291+ aufs_bindex_t bbot;
1facf9fc 31292+ struct kobject *kobj;
31293+ struct au_branch *br;
392086de 31294+ struct au_brsysfs *br_sysfs;
1facf9fc 31295+
31296+ dbgaufs_brs_add(sb, bindex);
31297+
31298+ if (!sysaufs_brs)
31299+ return;
31300+
31301+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31302+ bbot = au_sbbot(sb);
31303+ for (; bindex <= bbot; bindex++) {
1facf9fc 31304+ br = au_sbr(sb, bindex);
392086de
AM
31305+ br_sysfs = br->br_sysfs;
31306+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31307+ SysaufsBr_PREFIX "%d", bindex);
31308+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31309+ SysaufsBrid_PREFIX "%d", bindex);
31310+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31311+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31312+ if (unlikely(err))
31313+ pr_warn("failed %s under sysfs(%d)\n",
31314+ br_sysfs->name, err);
31315+ br_sysfs++;
31316+ }
1facf9fc 31317+ }
31318+}
7f207e10
AM
31319diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31320--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 31321+++ linux/fs/aufs/sysrq.c 2018-04-15 08:49:13.404484168 +0200
8b6a4947 31322@@ -0,0 +1,159 @@
1facf9fc 31323+/*
b00004a5 31324+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31325+ *
31326+ * This program, aufs is free software; you can redistribute it and/or modify
31327+ * it under the terms of the GNU General Public License as published by
31328+ * the Free Software Foundation; either version 2 of the License, or
31329+ * (at your option) any later version.
dece6358
AM
31330+ *
31331+ * This program is distributed in the hope that it will be useful,
31332+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31333+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31334+ * GNU General Public License for more details.
31335+ *
31336+ * You should have received a copy of the GNU General Public License
523b37e3 31337+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31338+ */
31339+
31340+/*
31341+ * magic sysrq hanlder
31342+ */
31343+
1facf9fc 31344+/* #include <linux/sysrq.h> */
027c5e7a 31345+#include <linux/writeback.h>
1facf9fc 31346+#include "aufs.h"
31347+
31348+/* ---------------------------------------------------------------------- */
31349+
31350+static void sysrq_sb(struct super_block *sb)
31351+{
31352+ char *plevel;
31353+ struct au_sbinfo *sbinfo;
31354+ struct file *file;
8b6a4947
AM
31355+ struct hlist_bl_head *files;
31356+ struct hlist_bl_node *pos;
523b37e3 31357+ struct au_finfo *finfo;
1facf9fc 31358+
31359+ plevel = au_plevel;
31360+ au_plevel = KERN_WARNING;
1facf9fc 31361+
4a4d8108 31362+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31363+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31364+
31365+ sbinfo = au_sbi(sb);
4a4d8108 31366+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31367+ pr("superblock\n");
1facf9fc 31368+ au_dpri_sb(sb);
027c5e7a
AM
31369+
31370+#if 0
c06a8ce3 31371+ pr("root dentry\n");
1facf9fc 31372+ au_dpri_dentry(sb->s_root);
c06a8ce3 31373+ pr("root inode\n");
5527c038 31374+ au_dpri_inode(d_inode(sb->s_root));
027c5e7a
AM
31375+#endif
31376+
1facf9fc 31377+#if 0
027c5e7a
AM
31378+ do {
31379+ int err, i, j, ndentry;
31380+ struct au_dcsub_pages dpages;
31381+ struct au_dpage *dpage;
31382+
31383+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31384+ if (unlikely(err))
31385+ break;
31386+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31387+ if (!err)
31388+ for (i = 0; i < dpages.ndpage; i++) {
31389+ dpage = dpages.dpages + i;
31390+ ndentry = dpage->ndentry;
31391+ for (j = 0; j < ndentry; j++)
31392+ au_dpri_dentry(dpage->dentries[j]);
31393+ }
31394+ au_dpages_free(&dpages);
31395+ } while (0);
31396+#endif
31397+
31398+#if 1
31399+ {
31400+ struct inode *i;
076b876e 31401+
c06a8ce3 31402+ pr("isolated inode\n");
79b8bda9 31403+ spin_lock(&sb->s_inode_list_lock);
2cbb1c4b
JR
31404+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31405+ spin_lock(&i->i_lock);
b4510431 31406+ if (1 || hlist_empty(&i->i_dentry))
027c5e7a 31407+ au_dpri_inode(i);
2cbb1c4b
JR
31408+ spin_unlock(&i->i_lock);
31409+ }
79b8bda9 31410+ spin_unlock(&sb->s_inode_list_lock);
027c5e7a 31411+ }
1facf9fc 31412+#endif
c06a8ce3 31413+ pr("files\n");
523b37e3 31414+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31415+ hlist_bl_lock(files);
31416+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31417+ umode_t mode;
076b876e 31418+
523b37e3 31419+ file = finfo->fi_file;
c06a8ce3 31420+ mode = file_inode(file)->i_mode;
38d290e6 31421+ if (!special_file(mode))
1facf9fc 31422+ au_dpri_file(file);
523b37e3 31423+ }
8b6a4947 31424+ hlist_bl_unlock(files);
c06a8ce3 31425+ pr("done\n");
1facf9fc 31426+
c06a8ce3 31427+#undef pr
1facf9fc 31428+ au_plevel = plevel;
1facf9fc 31429+}
31430+
31431+/* ---------------------------------------------------------------------- */
31432+
31433+/* module parameter */
31434+static char *aufs_sysrq_key = "a";
31435+module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
31436+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31437+
0c5527e5 31438+static void au_sysrq(int key __maybe_unused)
1facf9fc 31439+{
1facf9fc 31440+ struct au_sbinfo *sbinfo;
8b6a4947 31441+ struct hlist_bl_node *pos;
1facf9fc 31442+
027c5e7a 31443+ lockdep_off();
53392da6 31444+ au_sbilist_lock();
8b6a4947 31445+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31446+ sysrq_sb(sbinfo->si_sb);
53392da6 31447+ au_sbilist_unlock();
027c5e7a 31448+ lockdep_on();
1facf9fc 31449+}
31450+
31451+static struct sysrq_key_op au_sysrq_op = {
31452+ .handler = au_sysrq,
31453+ .help_msg = "Aufs",
31454+ .action_msg = "Aufs",
31455+ .enable_mask = SYSRQ_ENABLE_DUMP
31456+};
31457+
31458+/* ---------------------------------------------------------------------- */
31459+
31460+int __init au_sysrq_init(void)
31461+{
31462+ int err;
31463+ char key;
31464+
31465+ err = -1;
31466+ key = *aufs_sysrq_key;
31467+ if ('a' <= key && key <= 'z')
31468+ err = register_sysrq_key(key, &au_sysrq_op);
31469+ if (unlikely(err))
4a4d8108 31470+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31471+ return err;
31472+}
31473+
31474+void au_sysrq_fin(void)
31475+{
31476+ int err;
076b876e 31477+
1facf9fc 31478+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31479+ if (unlikely(err))
4a4d8108 31480+ pr_err("err %d (ignored)\n", err);
1facf9fc 31481+}
7f207e10
AM
31482diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31483--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
b00004a5
AM
31484+++ linux/fs/aufs/vdir.c 2018-06-04 09:08:09.188079511 +0200
31485@@ -0,0 +1,893 @@
1facf9fc 31486+/*
b00004a5 31487+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31488+ *
31489+ * This program, aufs is free software; you can redistribute it and/or modify
31490+ * it under the terms of the GNU General Public License as published by
31491+ * the Free Software Foundation; either version 2 of the License, or
31492+ * (at your option) any later version.
dece6358
AM
31493+ *
31494+ * This program is distributed in the hope that it will be useful,
31495+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31496+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31497+ * GNU General Public License for more details.
31498+ *
31499+ * You should have received a copy of the GNU General Public License
523b37e3 31500+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31501+ */
31502+
31503+/*
31504+ * virtual or vertical directory
31505+ */
31506+
31507+#include "aufs.h"
31508+
dece6358 31509+static unsigned int calc_size(int nlen)
1facf9fc 31510+{
dece6358 31511+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31512+}
31513+
31514+static int set_deblk_end(union au_vdir_deblk_p *p,
31515+ union au_vdir_deblk_p *deblk_end)
31516+{
31517+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31518+ p->de->de_str.len = 0;
31519+ /* smp_mb(); */
31520+ return 0;
31521+ }
31522+ return -1; /* error */
31523+}
31524+
31525+/* returns true or false */
31526+static int is_deblk_end(union au_vdir_deblk_p *p,
31527+ union au_vdir_deblk_p *deblk_end)
31528+{
31529+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31530+ return !p->de->de_str.len;
31531+ return 1;
31532+}
31533+
31534+static unsigned char *last_deblk(struct au_vdir *vdir)
31535+{
31536+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31537+}
31538+
31539+/* ---------------------------------------------------------------------- */
31540+
79b8bda9 31541+/* estimate the appropriate size for name hash table */
1308ab2a 31542+unsigned int au_rdhash_est(loff_t sz)
31543+{
31544+ unsigned int n;
31545+
31546+ n = UINT_MAX;
31547+ sz >>= 10;
31548+ if (sz < n)
31549+ n = sz;
31550+ if (sz < AUFS_RDHASH_DEF)
31551+ n = AUFS_RDHASH_DEF;
4a4d8108 31552+ /* pr_info("n %u\n", n); */
1308ab2a 31553+ return n;
31554+}
31555+
1facf9fc 31556+/*
31557+ * the allocated memory has to be freed by
dece6358 31558+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 31559+ */
dece6358 31560+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 31561+{
1facf9fc 31562+ struct hlist_head *head;
dece6358 31563+ unsigned int u;
076b876e 31564+ size_t sz;
1facf9fc 31565+
076b876e
AM
31566+ sz = sizeof(*nhash->nh_head) * num_hash;
31567+ head = kmalloc(sz, gfp);
dece6358
AM
31568+ if (head) {
31569+ nhash->nh_num = num_hash;
31570+ nhash->nh_head = head;
31571+ for (u = 0; u < num_hash; u++)
1facf9fc 31572+ INIT_HLIST_HEAD(head++);
dece6358 31573+ return 0; /* success */
1facf9fc 31574+ }
1facf9fc 31575+
dece6358 31576+ return -ENOMEM;
1facf9fc 31577+}
31578+
dece6358
AM
31579+static void nhash_count(struct hlist_head *head)
31580+{
31581+#if 0
31582+ unsigned long n;
31583+ struct hlist_node *pos;
31584+
31585+ n = 0;
31586+ hlist_for_each(pos, head)
31587+ n++;
4a4d8108 31588+ pr_info("%lu\n", n);
dece6358
AM
31589+#endif
31590+}
31591+
31592+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 31593+{
c06a8ce3
AM
31594+ struct au_vdir_wh *pos;
31595+ struct hlist_node *node;
1facf9fc 31596+
c06a8ce3 31597+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
1c60b727 31598+ kfree(pos);
1facf9fc 31599+}
31600+
dece6358 31601+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 31602+{
c06a8ce3
AM
31603+ struct au_vdir_dehstr *pos;
31604+ struct hlist_node *node;
1facf9fc 31605+
c06a8ce3 31606+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 31607+ au_cache_free_vdir_dehstr(pos);
1facf9fc 31608+}
31609+
dece6358
AM
31610+static void au_nhash_do_free(struct au_nhash *nhash,
31611+ void (*free)(struct hlist_head *head))
1facf9fc 31612+{
1308ab2a 31613+ unsigned int n;
1facf9fc 31614+ struct hlist_head *head;
1facf9fc 31615+
dece6358 31616+ n = nhash->nh_num;
1308ab2a 31617+ if (!n)
31618+ return;
31619+
dece6358 31620+ head = nhash->nh_head;
1308ab2a 31621+ while (n-- > 0) {
dece6358
AM
31622+ nhash_count(head);
31623+ free(head++);
1facf9fc 31624+ }
1c60b727 31625+ kfree(nhash->nh_head);
1facf9fc 31626+}
31627+
dece6358 31628+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 31629+{
dece6358
AM
31630+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
31631+}
1facf9fc 31632+
dece6358
AM
31633+static void au_nhash_de_free(struct au_nhash *delist)
31634+{
31635+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 31636+}
31637+
31638+/* ---------------------------------------------------------------------- */
31639+
31640+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31641+ int limit)
31642+{
31643+ int num;
31644+ unsigned int u, n;
31645+ struct hlist_head *head;
c06a8ce3 31646+ struct au_vdir_wh *pos;
1facf9fc 31647+
31648+ num = 0;
31649+ n = whlist->nh_num;
31650+ head = whlist->nh_head;
1308ab2a 31651+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
31652+ hlist_for_each_entry(pos, head, wh_hash)
31653+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 31654+ return 1;
1facf9fc 31655+ return 0;
31656+}
31657+
31658+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 31659+ unsigned char *name,
1facf9fc 31660+ unsigned int len)
31661+{
dece6358
AM
31662+ unsigned int v;
31663+ /* const unsigned int magic_bit = 12; */
31664+
1308ab2a 31665+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31666+
dece6358 31667+ v = 0;
f0c0a007
AM
31668+ if (len > 8)
31669+ len = 8;
dece6358
AM
31670+ while (len--)
31671+ v += *name++;
31672+ /* v = hash_long(v, magic_bit); */
31673+ v %= nhash->nh_num;
31674+ return nhash->nh_head + v;
31675+}
31676+
31677+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31678+ int nlen)
31679+{
31680+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 31681+}
31682+
31683+/* returns found or not */
dece6358 31684+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 31685+{
31686+ struct hlist_head *head;
c06a8ce3 31687+ struct au_vdir_wh *pos;
1facf9fc 31688+ struct au_vdir_destr *str;
31689+
dece6358 31690+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
31691+ hlist_for_each_entry(pos, head, wh_hash) {
31692+ str = &pos->wh_str;
1facf9fc 31693+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
31694+ if (au_nhash_test_name(str, name, nlen))
31695+ return 1;
31696+ }
31697+ return 0;
31698+}
31699+
31700+/* returns found(true) or not */
31701+static int test_known(struct au_nhash *delist, char *name, int nlen)
31702+{
31703+ struct hlist_head *head;
c06a8ce3 31704+ struct au_vdir_dehstr *pos;
dece6358
AM
31705+ struct au_vdir_destr *str;
31706+
31707+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
31708+ hlist_for_each_entry(pos, head, hash) {
31709+ str = pos->str;
dece6358
AM
31710+ AuDbg("%.*s\n", str->len, str->name);
31711+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 31712+ return 1;
31713+ }
31714+ return 0;
31715+}
31716+
dece6358
AM
31717+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31718+ unsigned char d_type)
31719+{
31720+#ifdef CONFIG_AUFS_SHWH
31721+ wh->wh_ino = ino;
31722+ wh->wh_type = d_type;
31723+#endif
31724+}
31725+
31726+/* ---------------------------------------------------------------------- */
31727+
31728+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31729+ unsigned int d_type, aufs_bindex_t bindex,
31730+ unsigned char shwh)
1facf9fc 31731+{
31732+ int err;
31733+ struct au_vdir_destr *str;
31734+ struct au_vdir_wh *wh;
31735+
dece6358 31736+ AuDbg("%.*s\n", nlen, name);
1308ab2a 31737+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31738+
1facf9fc 31739+ err = -ENOMEM;
dece6358 31740+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 31741+ if (unlikely(!wh))
31742+ goto out;
31743+
31744+ err = 0;
31745+ wh->wh_bindex = bindex;
dece6358
AM
31746+ if (shwh)
31747+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 31748+ str = &wh->wh_str;
dece6358
AM
31749+ str->len = nlen;
31750+ memcpy(str->name, name, nlen);
31751+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 31752+ /* smp_mb(); */
31753+
4f0767ce 31754+out:
1facf9fc 31755+ return err;
31756+}
31757+
1facf9fc 31758+static int append_deblk(struct au_vdir *vdir)
31759+{
31760+ int err;
dece6358 31761+ unsigned long ul;
1facf9fc 31762+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31763+ union au_vdir_deblk_p p, deblk_end;
31764+ unsigned char **o;
31765+
31766+ err = -ENOMEM;
e2f27e51
AM
31767+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
31768+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 31769+ if (unlikely(!o))
31770+ goto out;
31771+
31772+ vdir->vd_deblk = o;
31773+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
31774+ if (p.deblk) {
31775+ ul = vdir->vd_nblk++;
31776+ vdir->vd_deblk[ul] = p.deblk;
31777+ vdir->vd_last.ul = ul;
31778+ vdir->vd_last.p.deblk = p.deblk;
31779+ deblk_end.deblk = p.deblk + deblk_sz;
31780+ err = set_deblk_end(&p, &deblk_end);
31781+ }
31782+
4f0767ce 31783+out:
1facf9fc 31784+ return err;
31785+}
31786+
dece6358
AM
31787+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
31788+ unsigned int d_type, struct au_nhash *delist)
31789+{
31790+ int err;
31791+ unsigned int sz;
31792+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31793+ union au_vdir_deblk_p p, *room, deblk_end;
31794+ struct au_vdir_dehstr *dehstr;
31795+
31796+ p.deblk = last_deblk(vdir);
31797+ deblk_end.deblk = p.deblk + deblk_sz;
31798+ room = &vdir->vd_last.p;
31799+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
31800+ || !is_deblk_end(room, &deblk_end));
31801+
31802+ sz = calc_size(nlen);
31803+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
31804+ err = append_deblk(vdir);
31805+ if (unlikely(err))
31806+ goto out;
31807+
31808+ p.deblk = last_deblk(vdir);
31809+ deblk_end.deblk = p.deblk + deblk_sz;
31810+ /* smp_mb(); */
31811+ AuDebugOn(room->deblk != p.deblk);
31812+ }
31813+
31814+ err = -ENOMEM;
4a4d8108 31815+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
31816+ if (unlikely(!dehstr))
31817+ goto out;
31818+
31819+ dehstr->str = &room->de->de_str;
31820+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
31821+ room->de->de_ino = ino;
31822+ room->de->de_type = d_type;
31823+ room->de->de_str.len = nlen;
31824+ memcpy(room->de->de_str.name, name, nlen);
31825+
31826+ err = 0;
31827+ room->deblk += sz;
31828+ if (unlikely(set_deblk_end(room, &deblk_end)))
31829+ err = append_deblk(vdir);
31830+ /* smp_mb(); */
31831+
4f0767ce 31832+out:
dece6358
AM
31833+ return err;
31834+}
31835+
31836+/* ---------------------------------------------------------------------- */
31837+
1c60b727 31838+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
31839+{
31840+ unsigned char **deblk;
31841+
31842+ deblk = vdir->vd_deblk;
1c60b727
AM
31843+ while (vdir->vd_nblk--)
31844+ kfree(*deblk++);
31845+ kfree(vdir->vd_deblk);
31846+ au_cache_free_vdir(vdir);
dece6358
AM
31847+}
31848+
1308ab2a 31849+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 31850+{
31851+ struct au_vdir *vdir;
1308ab2a 31852+ struct super_block *sb;
1facf9fc 31853+ int err;
31854+
2000de60 31855+ sb = file->f_path.dentry->d_sb;
dece6358
AM
31856+ SiMustAnyLock(sb);
31857+
1facf9fc 31858+ err = -ENOMEM;
31859+ vdir = au_cache_alloc_vdir();
31860+ if (unlikely(!vdir))
31861+ goto out;
31862+
31863+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
31864+ if (unlikely(!vdir->vd_deblk))
31865+ goto out_free;
31866+
31867+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 31868+ if (!vdir->vd_deblk_sz) {
79b8bda9 31869+ /* estimate the appropriate size for deblk */
1308ab2a 31870+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 31871+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 31872+ }
1facf9fc 31873+ vdir->vd_nblk = 0;
31874+ vdir->vd_version = 0;
31875+ vdir->vd_jiffy = 0;
31876+ err = append_deblk(vdir);
31877+ if (!err)
31878+ return vdir; /* success */
31879+
1c60b727 31880+ kfree(vdir->vd_deblk);
1facf9fc 31881+
4f0767ce 31882+out_free:
1c60b727 31883+ au_cache_free_vdir(vdir);
4f0767ce 31884+out:
1facf9fc 31885+ vdir = ERR_PTR(err);
31886+ return vdir;
31887+}
31888+
31889+static int reinit_vdir(struct au_vdir *vdir)
31890+{
31891+ int err;
31892+ union au_vdir_deblk_p p, deblk_end;
31893+
31894+ while (vdir->vd_nblk > 1) {
1c60b727 31895+ kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 31896+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
31897+ vdir->vd_nblk--;
31898+ }
31899+ p.deblk = vdir->vd_deblk[0];
31900+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
31901+ err = set_deblk_end(&p, &deblk_end);
31902+ /* keep vd_dblk_sz */
31903+ vdir->vd_last.ul = 0;
31904+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
31905+ vdir->vd_version = 0;
31906+ vdir->vd_jiffy = 0;
31907+ /* smp_mb(); */
31908+ return err;
31909+}
31910+
31911+/* ---------------------------------------------------------------------- */
31912+
1facf9fc 31913+#define AuFillVdir_CALLED 1
31914+#define AuFillVdir_WHABLE (1 << 1)
dece6358 31915+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 31916+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
31917+#define au_fset_fillvdir(flags, name) \
31918+ do { (flags) |= AuFillVdir_##name; } while (0)
31919+#define au_fclr_fillvdir(flags, name) \
31920+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 31921+
dece6358
AM
31922+#ifndef CONFIG_AUFS_SHWH
31923+#undef AuFillVdir_SHWH
31924+#define AuFillVdir_SHWH 0
31925+#endif
31926+
1facf9fc 31927+struct fillvdir_arg {
392086de 31928+ struct dir_context ctx;
1facf9fc 31929+ struct file *file;
31930+ struct au_vdir *vdir;
dece6358
AM
31931+ struct au_nhash delist;
31932+ struct au_nhash whlist;
1facf9fc 31933+ aufs_bindex_t bindex;
31934+ unsigned int flags;
31935+ int err;
31936+};
31937+
392086de 31938+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 31939+ loff_t offset __maybe_unused, u64 h_ino,
31940+ unsigned int d_type)
31941+{
392086de 31942+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 31943+ char *name = (void *)__name;
31944+ struct super_block *sb;
1facf9fc 31945+ ino_t ino;
dece6358 31946+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 31947+
1facf9fc 31948+ arg->err = 0;
2000de60 31949+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 31950+ au_fset_fillvdir(arg->flags, CALLED);
31951+ /* smp_mb(); */
dece6358 31952+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 31953+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
31954+ if (test_known(&arg->delist, name, nlen)
31955+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
31956+ goto out; /* already exists or whiteouted */
1facf9fc 31957+
dece6358 31958+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
31959+ if (!arg->err) {
31960+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
31961+ d_type = DT_UNKNOWN;
dece6358
AM
31962+ arg->err = append_de(arg->vdir, name, nlen, ino,
31963+ d_type, &arg->delist);
4a4d8108 31964+ }
1facf9fc 31965+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
31966+ name += AUFS_WH_PFX_LEN;
dece6358
AM
31967+ nlen -= AUFS_WH_PFX_LEN;
31968+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
31969+ goto out; /* already whiteouted */
1facf9fc 31970+
dece6358
AM
31971+ if (shwh)
31972+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
31973+ &ino);
4a4d8108
AM
31974+ if (!arg->err) {
31975+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
31976+ d_type = DT_UNKNOWN;
1facf9fc 31977+ arg->err = au_nhash_append_wh
dece6358
AM
31978+ (&arg->whlist, name, nlen, ino, d_type,
31979+ arg->bindex, shwh);
4a4d8108 31980+ }
1facf9fc 31981+ }
31982+
4f0767ce 31983+out:
1facf9fc 31984+ if (!arg->err)
31985+ arg->vdir->vd_jiffy = jiffies;
31986+ /* smp_mb(); */
31987+ AuTraceErr(arg->err);
31988+ return arg->err;
31989+}
31990+
dece6358
AM
31991+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
31992+ struct au_nhash *whlist, struct au_nhash *delist)
31993+{
31994+#ifdef CONFIG_AUFS_SHWH
31995+ int err;
31996+ unsigned int nh, u;
31997+ struct hlist_head *head;
c06a8ce3
AM
31998+ struct au_vdir_wh *pos;
31999+ struct hlist_node *n;
dece6358
AM
32000+ char *p, *o;
32001+ struct au_vdir_destr *destr;
32002+
32003+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32004+
32005+ err = -ENOMEM;
537831f9 32006+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32007+ if (unlikely(!p))
32008+ goto out;
32009+
32010+ err = 0;
32011+ nh = whlist->nh_num;
32012+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32013+ p += AUFS_WH_PFX_LEN;
32014+ for (u = 0; u < nh; u++) {
32015+ head = whlist->nh_head + u;
c06a8ce3
AM
32016+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32017+ destr = &pos->wh_str;
dece6358
AM
32018+ memcpy(p, destr->name, destr->len);
32019+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32020+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32021+ if (unlikely(err))
32022+ break;
32023+ }
32024+ }
32025+
1c60b727 32026+ free_page((unsigned long)o);
dece6358 32027+
4f0767ce 32028+out:
dece6358
AM
32029+ AuTraceErr(err);
32030+ return err;
32031+#else
32032+ return 0;
32033+#endif
32034+}
32035+
1facf9fc 32036+static int au_do_read_vdir(struct fillvdir_arg *arg)
32037+{
32038+ int err;
dece6358 32039+ unsigned int rdhash;
1facf9fc 32040+ loff_t offset;
5afbbe0d 32041+ aufs_bindex_t bbot, bindex, btop;
dece6358 32042+ unsigned char shwh;
1facf9fc 32043+ struct file *hf, *file;
32044+ struct super_block *sb;
32045+
1facf9fc 32046+ file = arg->file;
2000de60 32047+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32048+ SiMustAnyLock(sb);
32049+
32050+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32051+ if (!rdhash)
32052+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32053+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32054+ if (unlikely(err))
1facf9fc 32055+ goto out;
dece6358
AM
32056+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32057+ if (unlikely(err))
1facf9fc 32058+ goto out_delist;
32059+
32060+ err = 0;
32061+ arg->flags = 0;
dece6358
AM
32062+ shwh = 0;
32063+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32064+ shwh = 1;
32065+ au_fset_fillvdir(arg->flags, SHWH);
32066+ }
5afbbe0d
AM
32067+ btop = au_fbtop(file);
32068+ bbot = au_fbbot_dir(file);
32069+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32070+ hf = au_hf_dir(file, bindex);
1facf9fc 32071+ if (!hf)
32072+ continue;
32073+
32074+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32075+ err = offset;
32076+ if (unlikely(offset))
32077+ break;
32078+
32079+ arg->bindex = bindex;
32080+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32081+ if (shwh
5afbbe0d 32082+ || (bindex != bbot
dece6358 32083+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32084+ au_fset_fillvdir(arg->flags, WHABLE);
32085+ do {
32086+ arg->err = 0;
32087+ au_fclr_fillvdir(arg->flags, CALLED);
32088+ /* smp_mb(); */
392086de 32089+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32090+ if (err >= 0)
32091+ err = arg->err;
32092+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32093+
32094+ /*
32095+ * dir_relax() may be good for concurrency, but aufs should not
32096+ * use it since it will cause a lockdep problem.
32097+ */
1facf9fc 32098+ }
dece6358
AM
32099+
32100+ if (!err && shwh)
32101+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32102+
32103+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32104+
4f0767ce 32105+out_delist:
dece6358 32106+ au_nhash_de_free(&arg->delist);
4f0767ce 32107+out:
1facf9fc 32108+ return err;
32109+}
32110+
32111+static int read_vdir(struct file *file, int may_read)
32112+{
32113+ int err;
32114+ unsigned long expire;
32115+ unsigned char do_read;
392086de
AM
32116+ struct fillvdir_arg arg = {
32117+ .ctx = {
2000de60 32118+ .actor = fillvdir
392086de
AM
32119+ }
32120+ };
1facf9fc 32121+ struct inode *inode;
32122+ struct au_vdir *vdir, *allocated;
32123+
32124+ err = 0;
c06a8ce3 32125+ inode = file_inode(file);
1facf9fc 32126+ IMustLock(inode);
5afbbe0d 32127+ IiMustWriteLock(inode);
dece6358
AM
32128+ SiMustAnyLock(inode->i_sb);
32129+
1facf9fc 32130+ allocated = NULL;
32131+ do_read = 0;
32132+ expire = au_sbi(inode->i_sb)->si_rdcache;
32133+ vdir = au_ivdir(inode);
32134+ if (!vdir) {
32135+ do_read = 1;
1308ab2a 32136+ vdir = alloc_vdir(file);
1facf9fc 32137+ err = PTR_ERR(vdir);
32138+ if (IS_ERR(vdir))
32139+ goto out;
32140+ err = 0;
32141+ allocated = vdir;
32142+ } else if (may_read
be118d29 32143+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32144+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32145+ do_read = 1;
32146+ err = reinit_vdir(vdir);
32147+ if (unlikely(err))
32148+ goto out;
32149+ }
32150+
32151+ if (!do_read)
32152+ return 0; /* success */
32153+
32154+ arg.file = file;
32155+ arg.vdir = vdir;
32156+ err = au_do_read_vdir(&arg);
32157+ if (!err) {
392086de 32158+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32159+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32160+ vdir->vd_last.ul = 0;
32161+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32162+ if (allocated)
32163+ au_set_ivdir(inode, allocated);
32164+ } else if (allocated)
1c60b727 32165+ au_vdir_free(allocated);
1facf9fc 32166+
4f0767ce 32167+out:
1facf9fc 32168+ return err;
32169+}
32170+
32171+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32172+{
32173+ int err, rerr;
32174+ unsigned long ul, n;
32175+ const unsigned int deblk_sz = src->vd_deblk_sz;
32176+
32177+ AuDebugOn(tgt->vd_nblk != 1);
32178+
32179+ err = -ENOMEM;
32180+ if (tgt->vd_nblk < src->vd_nblk) {
32181+ unsigned char **p;
32182+
e2f27e51
AM
32183+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32184+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32185+ if (unlikely(!p))
32186+ goto out;
32187+ tgt->vd_deblk = p;
32188+ }
32189+
1308ab2a 32190+ if (tgt->vd_deblk_sz != deblk_sz) {
32191+ unsigned char *p;
32192+
32193+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32194+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32195+ /*may_shrink*/1);
1308ab2a 32196+ if (unlikely(!p))
32197+ goto out;
32198+ tgt->vd_deblk[0] = p;
32199+ }
1facf9fc 32200+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32201+ tgt->vd_version = src->vd_version;
32202+ tgt->vd_jiffy = src->vd_jiffy;
32203+
32204+ n = src->vd_nblk;
32205+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32206+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32207+ GFP_NOFS);
32208+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32209+ goto out;
1308ab2a 32210+ tgt->vd_nblk++;
1facf9fc 32211+ }
1308ab2a 32212+ tgt->vd_nblk = n;
32213+ tgt->vd_last.ul = tgt->vd_last.ul;
32214+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32215+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32216+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32217+ /* smp_mb(); */
32218+ return 0; /* success */
32219+
4f0767ce 32220+out:
1facf9fc 32221+ rerr = reinit_vdir(tgt);
32222+ BUG_ON(rerr);
32223+ return err;
32224+}
32225+
32226+int au_vdir_init(struct file *file)
32227+{
32228+ int err;
32229+ struct inode *inode;
32230+ struct au_vdir *vdir_cache, *allocated;
32231+
392086de 32232+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32233+ err = read_vdir(file, !file->f_pos);
32234+ if (unlikely(err))
32235+ goto out;
32236+
32237+ allocated = NULL;
32238+ vdir_cache = au_fvdir_cache(file);
32239+ if (!vdir_cache) {
1308ab2a 32240+ vdir_cache = alloc_vdir(file);
1facf9fc 32241+ err = PTR_ERR(vdir_cache);
32242+ if (IS_ERR(vdir_cache))
32243+ goto out;
32244+ allocated = vdir_cache;
32245+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32246+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32247+ err = reinit_vdir(vdir_cache);
32248+ if (unlikely(err))
32249+ goto out;
32250+ } else
32251+ return 0; /* success */
32252+
c06a8ce3 32253+ inode = file_inode(file);
1facf9fc 32254+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32255+ if (!err) {
be118d29 32256+ file->f_version = inode_query_iversion(inode);
1facf9fc 32257+ if (allocated)
32258+ au_set_fvdir_cache(file, allocated);
32259+ } else if (allocated)
1c60b727 32260+ au_vdir_free(allocated);
1facf9fc 32261+
4f0767ce 32262+out:
1facf9fc 32263+ return err;
32264+}
32265+
32266+static loff_t calc_offset(struct au_vdir *vdir)
32267+{
32268+ loff_t offset;
32269+ union au_vdir_deblk_p p;
32270+
32271+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32272+ offset = vdir->vd_last.p.deblk - p.deblk;
32273+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32274+ return offset;
32275+}
32276+
32277+/* returns true or false */
392086de 32278+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32279+{
32280+ int valid;
32281+ unsigned int deblk_sz;
32282+ unsigned long ul, n;
32283+ loff_t offset;
32284+ union au_vdir_deblk_p p, deblk_end;
32285+ struct au_vdir *vdir_cache;
32286+
32287+ valid = 1;
32288+ vdir_cache = au_fvdir_cache(file);
32289+ offset = calc_offset(vdir_cache);
32290+ AuDbg("offset %lld\n", offset);
392086de 32291+ if (ctx->pos == offset)
1facf9fc 32292+ goto out;
32293+
32294+ vdir_cache->vd_last.ul = 0;
32295+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32296+ if (!ctx->pos)
1facf9fc 32297+ goto out;
32298+
32299+ valid = 0;
32300+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32301+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32302+ AuDbg("ul %lu\n", ul);
32303+ if (ul >= vdir_cache->vd_nblk)
32304+ goto out;
32305+
32306+ n = vdir_cache->vd_nblk;
32307+ for (; ul < n; ul++) {
32308+ p.deblk = vdir_cache->vd_deblk[ul];
32309+ deblk_end.deblk = p.deblk + deblk_sz;
32310+ offset = ul;
32311+ offset *= deblk_sz;
392086de 32312+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32313+ unsigned int l;
32314+
32315+ l = calc_size(p.de->de_str.len);
32316+ offset += l;
32317+ p.deblk += l;
32318+ }
32319+ if (!is_deblk_end(&p, &deblk_end)) {
32320+ valid = 1;
32321+ vdir_cache->vd_last.ul = ul;
32322+ vdir_cache->vd_last.p = p;
32323+ break;
32324+ }
32325+ }
32326+
4f0767ce 32327+out:
1facf9fc 32328+ /* smp_mb(); */
b00004a5
AM
32329+ if (!valid)
32330+ AuDbg("valid %d\n", !valid);
1facf9fc 32331+ return valid;
32332+}
32333+
392086de 32334+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32335+{
1facf9fc 32336+ unsigned int l, deblk_sz;
32337+ union au_vdir_deblk_p deblk_end;
32338+ struct au_vdir *vdir_cache;
32339+ struct au_vdir_de *de;
32340+
32341+ vdir_cache = au_fvdir_cache(file);
392086de 32342+ if (!seek_vdir(file, ctx))
1facf9fc 32343+ return 0;
32344+
32345+ deblk_sz = vdir_cache->vd_deblk_sz;
32346+ while (1) {
32347+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32348+ deblk_end.deblk += deblk_sz;
32349+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32350+ de = vdir_cache->vd_last.p.de;
32351+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32352+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32353+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32354+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32355+ de->de_str.len, de->de_ino,
32356+ de->de_type))) {
1facf9fc 32357+ /* todo: ignore the error caused by udba? */
32358+ /* return err; */
32359+ return 0;
32360+ }
32361+
32362+ l = calc_size(de->de_str.len);
32363+ vdir_cache->vd_last.p.deblk += l;
392086de 32364+ ctx->pos += l;
1facf9fc 32365+ }
32366+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32367+ vdir_cache->vd_last.ul++;
32368+ vdir_cache->vd_last.p.deblk
32369+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32370+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32371+ continue;
32372+ }
32373+ break;
32374+ }
32375+
32376+ /* smp_mb(); */
32377+ return 0;
32378+}
7f207e10
AM
32379diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32380--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
b00004a5
AM
32381+++ linux/fs/aufs/vfsub.c 2018-06-04 09:08:11.628152835 +0200
32382@@ -0,0 +1,893 @@
1facf9fc 32383+/*
b00004a5 32384+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 32385+ *
32386+ * This program, aufs is free software; you can redistribute it and/or modify
32387+ * it under the terms of the GNU General Public License as published by
32388+ * the Free Software Foundation; either version 2 of the License, or
32389+ * (at your option) any later version.
dece6358
AM
32390+ *
32391+ * This program is distributed in the hope that it will be useful,
32392+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32393+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32394+ * GNU General Public License for more details.
32395+ *
32396+ * You should have received a copy of the GNU General Public License
523b37e3 32397+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32398+ */
32399+
32400+/*
32401+ * sub-routines for VFS
32402+ */
32403+
8b6a4947 32404+#include <linux/mnt_namespace.h>
dece6358 32405+#include <linux/namei.h>
8cdd5066 32406+#include <linux/nsproxy.h>
dece6358
AM
32407+#include <linux/security.h>
32408+#include <linux/splice.h>
1facf9fc 32409+#include "aufs.h"
32410+
8cdd5066
JR
32411+#ifdef CONFIG_AUFS_BR_FUSE
32412+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32413+{
8cdd5066
JR
32414+ if (!au_test_fuse(h_sb) || !au_userns)
32415+ return 0;
32416+
8b6a4947 32417+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32418+}
32419+#endif
32420+
a2654f78
AM
32421+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32422+{
32423+ int err;
32424+
32425+ lockdep_off();
32426+ down_read(&h_sb->s_umount);
32427+ err = __sync_filesystem(h_sb, wait);
32428+ up_read(&h_sb->s_umount);
32429+ lockdep_on();
32430+
32431+ return err;
32432+}
32433+
8cdd5066
JR
32434+/* ---------------------------------------------------------------------- */
32435+
1facf9fc 32436+int vfsub_update_h_iattr(struct path *h_path, int *did)
32437+{
32438+ int err;
32439+ struct kstat st;
32440+ struct super_block *h_sb;
32441+
32442+ /* for remote fs, leave work for its getattr or d_revalidate */
32443+ /* for bad i_attr fs, handle them in aufs_getattr() */
32444+ /* still some fs may acquire i_mutex. we need to skip them */
32445+ err = 0;
32446+ if (!did)
32447+ did = &err;
32448+ h_sb = h_path->dentry->d_sb;
32449+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32450+ if (*did)
521ced18 32451+ err = vfsub_getattr(h_path, &st);
1facf9fc 32452+
32453+ return err;
32454+}
32455+
32456+/* ---------------------------------------------------------------------- */
32457+
4a4d8108 32458+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32459+{
32460+ struct file *file;
32461+
b4510431 32462+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32463+ current_cred());
2cbb1c4b
JR
32464+ if (!IS_ERR_OR_NULL(file)
32465+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 32466+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 32467+
1308ab2a 32468+ return file;
32469+}
32470+
1facf9fc 32471+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32472+{
32473+ struct file *file;
32474+
2cbb1c4b 32475+ lockdep_off();
7f207e10 32476+ file = filp_open(path,
2cbb1c4b 32477+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32478+ mode);
2cbb1c4b 32479+ lockdep_on();
1facf9fc 32480+ if (IS_ERR(file))
32481+ goto out;
32482+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32483+
4f0767ce 32484+out:
1facf9fc 32485+ return file;
32486+}
32487+
b912730e
AM
32488+/*
32489+ * Ideally this function should call VFS:do_last() in order to keep all its
32490+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32491+ * structure such as nameidata. This is a second (or third) best approach.
32492+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32493+ */
32494+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32495+ struct vfsub_aopen_args *args, struct au_branch *br)
32496+{
32497+ int err;
32498+ struct file *file = args->file;
32499+ /* copied from linux/fs/namei.c:atomic_open() */
32500+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32501+
32502+ IMustLock(dir);
32503+ AuDebugOn(!dir->i_op->atomic_open);
32504+
32505+ err = au_br_test_oflag(args->open_flag, br);
32506+ if (unlikely(err))
32507+ goto out;
32508+
32509+ args->file->f_path.dentry = DENTRY_NOT_SET;
32510+ args->file->f_path.mnt = au_br_mnt(br);
32511+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32512+ args->create_mode, args->opened);
32513+ if (err >= 0) {
32514+ /* some filesystems don't set FILE_CREATED while succeeded? */
32515+ if (*args->opened & FILE_CREATED)
32516+ fsnotify_create(dir, dentry);
32517+ } else
32518+ goto out;
32519+
32520+
32521+ if (!err) {
32522+ /* todo: call VFS:may_open() here */
b912730e
AM
32523+ /* todo: ima_file_check() too? */
32524+ if (!err && (args->open_flag & __FMODE_EXEC))
32525+ err = deny_write_access(file);
32526+ if (unlikely(err))
32527+ /* note that the file is created and still opened */
32528+ goto out;
32529+ }
32530+
5afbbe0d 32531+ au_br_get(br);
b912730e
AM
32532+ fsnotify_open(file);
32533+
32534+out:
32535+ return err;
32536+}
32537+
1facf9fc 32538+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32539+{
32540+ int err;
32541+
1facf9fc 32542+ err = kern_path(name, flags, path);
5527c038 32543+ if (!err && d_is_positive(path->dentry))
1facf9fc 32544+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32545+ return err;
32546+}
32547+
febd17d6
JR
32548+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32549+ struct dentry *parent, int len)
32550+{
32551+ struct path path = {
32552+ .mnt = NULL
32553+ };
32554+
32555+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32556+ if (IS_ERR(path.dentry))
32557+ goto out;
32558+ if (d_is_positive(path.dentry))
32559+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32560+
32561+out:
32562+ AuTraceErrPtr(path.dentry);
32563+ return path.dentry;
32564+}
32565+
1facf9fc 32566+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32567+ int len)
32568+{
32569+ struct path path = {
32570+ .mnt = NULL
32571+ };
32572+
1308ab2a 32573+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 32574+ IMustLock(d_inode(parent));
1facf9fc 32575+
32576+ path.dentry = lookup_one_len(name, parent, len);
32577+ if (IS_ERR(path.dentry))
32578+ goto out;
5527c038 32579+ if (d_is_positive(path.dentry))
1facf9fc 32580+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32581+
4f0767ce 32582+out:
4a4d8108 32583+ AuTraceErrPtr(path.dentry);
1facf9fc 32584+ return path.dentry;
32585+}
32586+
b4510431 32587+void vfsub_call_lkup_one(void *args)
2cbb1c4b 32588+{
b4510431
AM
32589+ struct vfsub_lkup_one_args *a = args;
32590+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
32591+}
32592+
1facf9fc 32593+/* ---------------------------------------------------------------------- */
32594+
32595+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32596+ struct dentry *d2, struct au_hinode *hdir2)
32597+{
32598+ struct dentry *d;
32599+
2cbb1c4b 32600+ lockdep_off();
1facf9fc 32601+ d = lock_rename(d1, d2);
2cbb1c4b 32602+ lockdep_on();
4a4d8108 32603+ au_hn_suspend(hdir1);
1facf9fc 32604+ if (hdir1 != hdir2)
4a4d8108 32605+ au_hn_suspend(hdir2);
1facf9fc 32606+
32607+ return d;
32608+}
32609+
32610+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32611+ struct dentry *d2, struct au_hinode *hdir2)
32612+{
4a4d8108 32613+ au_hn_resume(hdir1);
1facf9fc 32614+ if (hdir1 != hdir2)
4a4d8108 32615+ au_hn_resume(hdir2);
2cbb1c4b 32616+ lockdep_off();
1facf9fc 32617+ unlock_rename(d1, d2);
2cbb1c4b 32618+ lockdep_on();
1facf9fc 32619+}
32620+
32621+/* ---------------------------------------------------------------------- */
32622+
b4510431 32623+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 32624+{
32625+ int err;
32626+ struct dentry *d;
32627+
32628+ IMustLock(dir);
32629+
32630+ d = path->dentry;
32631+ path->dentry = d->d_parent;
b752ccd1 32632+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 32633+ path->dentry = d;
32634+ if (unlikely(err))
32635+ goto out;
32636+
c1595e42 32637+ lockdep_off();
b4510431 32638+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 32639+ lockdep_on();
1facf9fc 32640+ if (!err) {
32641+ struct path tmp = *path;
32642+ int did;
32643+
32644+ vfsub_update_h_iattr(&tmp, &did);
32645+ if (did) {
32646+ tmp.dentry = path->dentry->d_parent;
32647+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32648+ }
32649+ /*ignore*/
32650+ }
32651+
4f0767ce 32652+out:
1facf9fc 32653+ return err;
32654+}
32655+
32656+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32657+{
32658+ int err;
32659+ struct dentry *d;
32660+
32661+ IMustLock(dir);
32662+
32663+ d = path->dentry;
32664+ path->dentry = d->d_parent;
b752ccd1 32665+ err = security_path_symlink(path, d, symname);
1facf9fc 32666+ path->dentry = d;
32667+ if (unlikely(err))
32668+ goto out;
32669+
c1595e42 32670+ lockdep_off();
1facf9fc 32671+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 32672+ lockdep_on();
1facf9fc 32673+ if (!err) {
32674+ struct path tmp = *path;
32675+ int did;
32676+
32677+ vfsub_update_h_iattr(&tmp, &did);
32678+ if (did) {
32679+ tmp.dentry = path->dentry->d_parent;
32680+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32681+ }
32682+ /*ignore*/
32683+ }
32684+
4f0767ce 32685+out:
1facf9fc 32686+ return err;
32687+}
32688+
32689+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32690+{
32691+ int err;
32692+ struct dentry *d;
32693+
32694+ IMustLock(dir);
32695+
32696+ d = path->dentry;
32697+ path->dentry = d->d_parent;
027c5e7a 32698+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 32699+ path->dentry = d;
32700+ if (unlikely(err))
32701+ goto out;
32702+
c1595e42 32703+ lockdep_off();
1facf9fc 32704+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 32705+ lockdep_on();
1facf9fc 32706+ if (!err) {
32707+ struct path tmp = *path;
32708+ int did;
32709+
32710+ vfsub_update_h_iattr(&tmp, &did);
32711+ if (did) {
32712+ tmp.dentry = path->dentry->d_parent;
32713+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32714+ }
32715+ /*ignore*/
32716+ }
32717+
4f0767ce 32718+out:
1facf9fc 32719+ return err;
32720+}
32721+
32722+static int au_test_nlink(struct inode *inode)
32723+{
32724+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32725+
32726+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
32727+ || inode->i_nlink < link_max)
32728+ return 0;
32729+ return -EMLINK;
32730+}
32731+
523b37e3
AM
32732+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32733+ struct inode **delegated_inode)
1facf9fc 32734+{
32735+ int err;
32736+ struct dentry *d;
32737+
32738+ IMustLock(dir);
32739+
5527c038 32740+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 32741+ if (unlikely(err))
32742+ return err;
32743+
b4510431 32744+ /* we don't call may_linkat() */
1facf9fc 32745+ d = path->dentry;
32746+ path->dentry = d->d_parent;
b752ccd1 32747+ err = security_path_link(src_dentry, path, d);
1facf9fc 32748+ path->dentry = d;
32749+ if (unlikely(err))
32750+ goto out;
32751+
2cbb1c4b 32752+ lockdep_off();
523b37e3 32753+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 32754+ lockdep_on();
1facf9fc 32755+ if (!err) {
32756+ struct path tmp = *path;
32757+ int did;
32758+
32759+ /* fuse has different memory inode for the same inumber */
32760+ vfsub_update_h_iattr(&tmp, &did);
32761+ if (did) {
32762+ tmp.dentry = path->dentry->d_parent;
32763+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32764+ tmp.dentry = src_dentry;
32765+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32766+ }
32767+ /*ignore*/
32768+ }
32769+
4f0767ce 32770+out:
1facf9fc 32771+ return err;
32772+}
32773+
32774+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 32775+ struct inode *dir, struct path *path,
f2c43d5f 32776+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 32777+{
32778+ int err;
32779+ struct path tmp = {
32780+ .mnt = path->mnt
32781+ };
32782+ struct dentry *d;
32783+
32784+ IMustLock(dir);
32785+ IMustLock(src_dir);
32786+
32787+ d = path->dentry;
32788+ path->dentry = d->d_parent;
32789+ tmp.dentry = src_dentry->d_parent;
38d290e6 32790+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 32791+ path->dentry = d;
32792+ if (unlikely(err))
32793+ goto out;
32794+
2cbb1c4b 32795+ lockdep_off();
523b37e3 32796+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
f2c43d5f 32797+ delegated_inode, flags);
2cbb1c4b 32798+ lockdep_on();
1facf9fc 32799+ if (!err) {
32800+ int did;
32801+
32802+ tmp.dentry = d->d_parent;
32803+ vfsub_update_h_iattr(&tmp, &did);
32804+ if (did) {
32805+ tmp.dentry = src_dentry;
32806+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32807+ tmp.dentry = src_dentry->d_parent;
32808+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32809+ }
32810+ /*ignore*/
32811+ }
32812+
4f0767ce 32813+out:
1facf9fc 32814+ return err;
32815+}
32816+
32817+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
32818+{
32819+ int err;
32820+ struct dentry *d;
32821+
32822+ IMustLock(dir);
32823+
32824+ d = path->dentry;
32825+ path->dentry = d->d_parent;
b752ccd1 32826+ err = security_path_mkdir(path, d, mode);
1facf9fc 32827+ path->dentry = d;
32828+ if (unlikely(err))
32829+ goto out;
32830+
c1595e42 32831+ lockdep_off();
1facf9fc 32832+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 32833+ lockdep_on();
1facf9fc 32834+ if (!err) {
32835+ struct path tmp = *path;
32836+ int did;
32837+
32838+ vfsub_update_h_iattr(&tmp, &did);
32839+ if (did) {
32840+ tmp.dentry = path->dentry->d_parent;
32841+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32842+ }
32843+ /*ignore*/
32844+ }
32845+
4f0767ce 32846+out:
1facf9fc 32847+ return err;
32848+}
32849+
32850+int vfsub_rmdir(struct inode *dir, struct path *path)
32851+{
32852+ int err;
32853+ struct dentry *d;
32854+
32855+ IMustLock(dir);
32856+
32857+ d = path->dentry;
32858+ path->dentry = d->d_parent;
b752ccd1 32859+ err = security_path_rmdir(path, d);
1facf9fc 32860+ path->dentry = d;
32861+ if (unlikely(err))
32862+ goto out;
32863+
2cbb1c4b 32864+ lockdep_off();
1facf9fc 32865+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 32866+ lockdep_on();
1facf9fc 32867+ if (!err) {
32868+ struct path tmp = {
32869+ .dentry = path->dentry->d_parent,
32870+ .mnt = path->mnt
32871+ };
32872+
32873+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
32874+ }
32875+
4f0767ce 32876+out:
1facf9fc 32877+ return err;
32878+}
32879+
32880+/* ---------------------------------------------------------------------- */
32881+
9dbd164d 32882+/* todo: support mmap_sem? */
1facf9fc 32883+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
32884+ loff_t *ppos)
32885+{
32886+ ssize_t err;
32887+
2cbb1c4b 32888+ lockdep_off();
1facf9fc 32889+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 32890+ lockdep_on();
1facf9fc 32891+ if (err >= 0)
32892+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32893+ return err;
32894+}
32895+
32896+/* todo: kernel_read()? */
32897+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
32898+ loff_t *ppos)
32899+{
32900+ ssize_t err;
32901+ mm_segment_t oldfs;
b752ccd1
AM
32902+ union {
32903+ void *k;
32904+ char __user *u;
32905+ } buf;
1facf9fc 32906+
b752ccd1 32907+ buf.k = kbuf;
1facf9fc 32908+ oldfs = get_fs();
32909+ set_fs(KERNEL_DS);
b752ccd1 32910+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 32911+ set_fs(oldfs);
32912+ return err;
32913+}
32914+
32915+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
32916+ loff_t *ppos)
32917+{
32918+ ssize_t err;
32919+
2cbb1c4b 32920+ lockdep_off();
1facf9fc 32921+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 32922+ lockdep_on();
1facf9fc 32923+ if (err >= 0)
32924+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32925+ return err;
32926+}
32927+
32928+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
32929+{
32930+ ssize_t err;
32931+ mm_segment_t oldfs;
b752ccd1
AM
32932+ union {
32933+ void *k;
32934+ const char __user *u;
32935+ } buf;
1facf9fc 32936+
b752ccd1 32937+ buf.k = kbuf;
1facf9fc 32938+ oldfs = get_fs();
32939+ set_fs(KERNEL_DS);
b752ccd1 32940+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 32941+ set_fs(oldfs);
32942+ return err;
32943+}
32944+
4a4d8108
AM
32945+int vfsub_flush(struct file *file, fl_owner_t id)
32946+{
32947+ int err;
32948+
32949+ err = 0;
523b37e3 32950+ if (file->f_op->flush) {
2000de60 32951+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
32952+ err = file->f_op->flush(file, id);
32953+ else {
32954+ lockdep_off();
32955+ err = file->f_op->flush(file, id);
32956+ lockdep_on();
32957+ }
4a4d8108
AM
32958+ if (!err)
32959+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
32960+ /*ignore*/
32961+ }
32962+ return err;
32963+}
32964+
392086de 32965+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 32966+{
32967+ int err;
32968+
523b37e3 32969+ AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
392086de 32970+
2cbb1c4b 32971+ lockdep_off();
392086de 32972+ err = iterate_dir(file, ctx);
2cbb1c4b 32973+ lockdep_on();
1facf9fc 32974+ if (err >= 0)
32975+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 32976+
1facf9fc 32977+ return err;
32978+}
32979+
32980+long vfsub_splice_to(struct file *in, loff_t *ppos,
32981+ struct pipe_inode_info *pipe, size_t len,
32982+ unsigned int flags)
32983+{
32984+ long err;
32985+
2cbb1c4b 32986+ lockdep_off();
0fc653ad 32987+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 32988+ lockdep_on();
4a4d8108 32989+ file_accessed(in);
1facf9fc 32990+ if (err >= 0)
32991+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
32992+ return err;
32993+}
32994+
32995+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
32996+ loff_t *ppos, size_t len, unsigned int flags)
32997+{
32998+ long err;
32999+
2cbb1c4b 33000+ lockdep_off();
0fc653ad 33001+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33002+ lockdep_on();
1facf9fc 33003+ if (err >= 0)
33004+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33005+ return err;
33006+}
33007+
53392da6
AM
33008+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33009+{
33010+ int err;
33011+
33012+ /* file can be NULL */
33013+ lockdep_off();
33014+ err = vfs_fsync(file, datasync);
33015+ lockdep_on();
33016+ if (!err) {
33017+ if (!path) {
33018+ AuDebugOn(!file);
33019+ path = &file->f_path;
33020+ }
33021+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33022+ }
33023+ return err;
33024+}
33025+
1facf9fc 33026+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33027+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33028+ struct file *h_file)
33029+{
33030+ int err;
33031+ struct inode *h_inode;
c06a8ce3 33032+ struct super_block *h_sb;
1facf9fc 33033+
1facf9fc 33034+ if (!h_file) {
c06a8ce3
AM
33035+ err = vfsub_truncate(h_path, length);
33036+ goto out;
1facf9fc 33037+ }
33038+
5527c038 33039+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33040+ h_sb = h_inode->i_sb;
33041+ lockdep_off();
33042+ sb_start_write(h_sb);
33043+ lockdep_on();
1facf9fc 33044+ err = locks_verify_truncate(h_inode, h_file, length);
33045+ if (!err)
953406b4 33046+ err = security_path_truncate(h_path);
2cbb1c4b
JR
33047+ if (!err) {
33048+ lockdep_off();
1facf9fc 33049+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
33050+ lockdep_on();
33051+ }
c06a8ce3
AM
33052+ lockdep_off();
33053+ sb_end_write(h_sb);
33054+ lockdep_on();
1facf9fc 33055+
4f0767ce 33056+out:
1facf9fc 33057+ return err;
33058+}
33059+
33060+/* ---------------------------------------------------------------------- */
33061+
33062+struct au_vfsub_mkdir_args {
33063+ int *errp;
33064+ struct inode *dir;
33065+ struct path *path;
33066+ int mode;
33067+};
33068+
33069+static void au_call_vfsub_mkdir(void *args)
33070+{
33071+ struct au_vfsub_mkdir_args *a = args;
33072+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33073+}
33074+
33075+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33076+{
33077+ int err, do_sio, wkq_err;
33078+
33079+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33080+ if (!do_sio) {
33081+ lockdep_off();
1facf9fc 33082+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33083+ lockdep_on();
33084+ } else {
1facf9fc 33085+ struct au_vfsub_mkdir_args args = {
33086+ .errp = &err,
33087+ .dir = dir,
33088+ .path = path,
33089+ .mode = mode
33090+ };
33091+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33092+ if (unlikely(wkq_err))
33093+ err = wkq_err;
33094+ }
33095+
33096+ return err;
33097+}
33098+
33099+struct au_vfsub_rmdir_args {
33100+ int *errp;
33101+ struct inode *dir;
33102+ struct path *path;
33103+};
33104+
33105+static void au_call_vfsub_rmdir(void *args)
33106+{
33107+ struct au_vfsub_rmdir_args *a = args;
33108+ *a->errp = vfsub_rmdir(a->dir, a->path);
33109+}
33110+
33111+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33112+{
33113+ int err, do_sio, wkq_err;
33114+
33115+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33116+ if (!do_sio) {
33117+ lockdep_off();
1facf9fc 33118+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33119+ lockdep_on();
33120+ } else {
1facf9fc 33121+ struct au_vfsub_rmdir_args args = {
33122+ .errp = &err,
33123+ .dir = dir,
33124+ .path = path
33125+ };
33126+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33127+ if (unlikely(wkq_err))
33128+ err = wkq_err;
33129+ }
33130+
33131+ return err;
33132+}
33133+
33134+/* ---------------------------------------------------------------------- */
33135+
33136+struct notify_change_args {
33137+ int *errp;
33138+ struct path *path;
33139+ struct iattr *ia;
523b37e3 33140+ struct inode **delegated_inode;
1facf9fc 33141+};
33142+
33143+static void call_notify_change(void *args)
33144+{
33145+ struct notify_change_args *a = args;
33146+ struct inode *h_inode;
33147+
5527c038 33148+ h_inode = d_inode(a->path->dentry);
1facf9fc 33149+ IMustLock(h_inode);
33150+
33151+ *a->errp = -EPERM;
33152+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 33153+ lockdep_off();
523b37e3
AM
33154+ *a->errp = notify_change(a->path->dentry, a->ia,
33155+ a->delegated_inode);
c1595e42 33156+ lockdep_on();
1facf9fc 33157+ if (!*a->errp)
33158+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33159+ }
33160+ AuTraceErr(*a->errp);
33161+}
33162+
523b37e3
AM
33163+int vfsub_notify_change(struct path *path, struct iattr *ia,
33164+ struct inode **delegated_inode)
1facf9fc 33165+{
33166+ int err;
33167+ struct notify_change_args args = {
523b37e3
AM
33168+ .errp = &err,
33169+ .path = path,
33170+ .ia = ia,
33171+ .delegated_inode = delegated_inode
1facf9fc 33172+ };
33173+
33174+ call_notify_change(&args);
33175+
33176+ return err;
33177+}
33178+
523b37e3
AM
33179+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33180+ struct inode **delegated_inode)
1facf9fc 33181+{
33182+ int err, wkq_err;
33183+ struct notify_change_args args = {
523b37e3
AM
33184+ .errp = &err,
33185+ .path = path,
33186+ .ia = ia,
33187+ .delegated_inode = delegated_inode
1facf9fc 33188+ };
33189+
33190+ wkq_err = au_wkq_wait(call_notify_change, &args);
33191+ if (unlikely(wkq_err))
33192+ err = wkq_err;
33193+
33194+ return err;
33195+}
33196+
33197+/* ---------------------------------------------------------------------- */
33198+
33199+struct unlink_args {
33200+ int *errp;
33201+ struct inode *dir;
33202+ struct path *path;
523b37e3 33203+ struct inode **delegated_inode;
1facf9fc 33204+};
33205+
33206+static void call_unlink(void *args)
33207+{
33208+ struct unlink_args *a = args;
33209+ struct dentry *d = a->path->dentry;
33210+ struct inode *h_inode;
33211+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33212+ && au_dcount(d) == 1);
1facf9fc 33213+
33214+ IMustLock(a->dir);
33215+
33216+ a->path->dentry = d->d_parent;
33217+ *a->errp = security_path_unlink(a->path, d);
33218+ a->path->dentry = d;
33219+ if (unlikely(*a->errp))
33220+ return;
33221+
33222+ if (!stop_sillyrename)
33223+ dget(d);
5527c038
JR
33224+ h_inode = NULL;
33225+ if (d_is_positive(d)) {
33226+ h_inode = d_inode(d);
027c5e7a 33227+ ihold(h_inode);
5527c038 33228+ }
1facf9fc 33229+
2cbb1c4b 33230+ lockdep_off();
523b37e3 33231+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 33232+ lockdep_on();
1facf9fc 33233+ if (!*a->errp) {
33234+ struct path tmp = {
33235+ .dentry = d->d_parent,
33236+ .mnt = a->path->mnt
33237+ };
33238+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33239+ }
33240+
33241+ if (!stop_sillyrename)
33242+ dput(d);
33243+ if (h_inode)
33244+ iput(h_inode);
33245+
33246+ AuTraceErr(*a->errp);
33247+}
33248+
33249+/*
33250+ * @dir: must be locked.
33251+ * @dentry: target dentry.
33252+ */
523b37e3
AM
33253+int vfsub_unlink(struct inode *dir, struct path *path,
33254+ struct inode **delegated_inode, int force)
1facf9fc 33255+{
33256+ int err;
33257+ struct unlink_args args = {
523b37e3
AM
33258+ .errp = &err,
33259+ .dir = dir,
33260+ .path = path,
33261+ .delegated_inode = delegated_inode
1facf9fc 33262+ };
33263+
33264+ if (!force)
33265+ call_unlink(&args);
33266+ else {
33267+ int wkq_err;
33268+
33269+ wkq_err = au_wkq_wait(call_unlink, &args);
33270+ if (unlikely(wkq_err))
33271+ err = wkq_err;
33272+ }
33273+
33274+ return err;
33275+}
7f207e10
AM
33276diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33277--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
b00004a5
AM
33278+++ linux/fs/aufs/vfsub.h 2018-06-04 09:08:11.628152835 +0200
33279@@ -0,0 +1,353 @@
1facf9fc 33280+/*
b00004a5 33281+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 33282+ *
33283+ * This program, aufs is free software; you can redistribute it and/or modify
33284+ * it under the terms of the GNU General Public License as published by
33285+ * the Free Software Foundation; either version 2 of the License, or
33286+ * (at your option) any later version.
dece6358
AM
33287+ *
33288+ * This program is distributed in the hope that it will be useful,
33289+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33290+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33291+ * GNU General Public License for more details.
33292+ *
33293+ * You should have received a copy of the GNU General Public License
523b37e3 33294+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33295+ */
33296+
33297+/*
33298+ * sub-routines for VFS
33299+ */
33300+
33301+#ifndef __AUFS_VFSUB_H__
33302+#define __AUFS_VFSUB_H__
33303+
33304+#ifdef __KERNEL__
33305+
33306+#include <linux/fs.h>
be118d29 33307+#include <linux/iversion.h>
b4510431 33308+#include <linux/mount.h>
8cdd5066 33309+#include <linux/posix_acl.h>
c1595e42 33310+#include <linux/xattr.h>
7f207e10 33311+#include "debug.h"
1facf9fc 33312+
7f207e10 33313+/* copied from linux/fs/internal.h */
2cbb1c4b 33314+/* todo: BAD approach!! */
c06a8ce3 33315+extern void __mnt_drop_write(struct vfsmount *);
7f207e10
AM
33316+
33317+/* ---------------------------------------------------------------------- */
1facf9fc 33318+
33319+/* lock subclass for lower inode */
33320+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33321+/* reduce? gave up. */
33322+enum {
c1595e42 33323+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33324+ AuLsc_I_PARENT, /* lower inode, parent first */
33325+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33326+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33327+ AuLsc_I_CHILD,
33328+ AuLsc_I_CHILD2,
33329+ AuLsc_I_End
33330+};
33331+
33332+/* to debug easier, do not make them inlined functions */
33333+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33334+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33335+
33336+/* ---------------------------------------------------------------------- */
33337+
7f207e10
AM
33338+static inline void vfsub_drop_nlink(struct inode *inode)
33339+{
33340+ AuDebugOn(!inode->i_nlink);
33341+ drop_nlink(inode);
33342+}
33343+
027c5e7a
AM
33344+static inline void vfsub_dead_dir(struct inode *inode)
33345+{
33346+ AuDebugOn(!S_ISDIR(inode->i_mode));
33347+ inode->i_flags |= S_DEAD;
33348+ clear_nlink(inode);
33349+}
33350+
392086de
AM
33351+static inline int vfsub_native_ro(struct inode *inode)
33352+{
8b6a4947 33353+ return sb_rdonly(inode->i_sb)
392086de
AM
33354+ || IS_RDONLY(inode)
33355+ /* || IS_APPEND(inode) */
33356+ || IS_IMMUTABLE(inode);
33357+}
33358+
8cdd5066
JR
33359+#ifdef CONFIG_AUFS_BR_FUSE
33360+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33361+#else
33362+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33363+#endif
33364+
a2654f78
AM
33365+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33366+
7f207e10
AM
33367+/* ---------------------------------------------------------------------- */
33368+
33369+int vfsub_update_h_iattr(struct path *h_path, int *did);
33370+struct file *vfsub_dentry_open(struct path *path, int flags);
33371+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
b912730e
AM
33372+struct vfsub_aopen_args {
33373+ struct file *file;
33374+ unsigned int open_flag;
33375+ umode_t create_mode;
33376+ int *opened;
33377+};
33378+struct au_branch;
33379+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33380+ struct vfsub_aopen_args *args, struct au_branch *br);
1facf9fc 33381+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33382+
febd17d6
JR
33383+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33384+ struct dentry *parent, int len);
1facf9fc 33385+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33386+ int len);
b4510431
AM
33387+
33388+struct vfsub_lkup_one_args {
33389+ struct dentry **errp;
33390+ struct qstr *name;
33391+ struct dentry *parent;
33392+};
33393+
33394+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33395+ struct dentry *parent)
33396+{
33397+ return vfsub_lookup_one_len(name->name, parent, name->len);
33398+}
33399+
33400+void vfsub_call_lkup_one(void *args);
33401+
33402+/* ---------------------------------------------------------------------- */
33403+
33404+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33405+{
33406+ int err;
076b876e 33407+
b4510431
AM
33408+ lockdep_off();
33409+ err = mnt_want_write(mnt);
33410+ lockdep_on();
33411+ return err;
33412+}
33413+
33414+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33415+{
33416+ lockdep_off();
33417+ mnt_drop_write(mnt);
33418+ lockdep_on();
33419+}
1facf9fc 33420+
7e9cd9fe 33421+#if 0 /* reserved */
c06a8ce3
AM
33422+static inline void vfsub_mnt_drop_write_file(struct file *file)
33423+{
33424+ lockdep_off();
33425+ mnt_drop_write_file(file);
33426+ lockdep_on();
33427+}
7e9cd9fe 33428+#endif
c06a8ce3 33429+
1facf9fc 33430+/* ---------------------------------------------------------------------- */
33431+
33432+struct au_hinode;
33433+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33434+ struct dentry *d2, struct au_hinode *hdir2);
33435+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33436+ struct dentry *d2, struct au_hinode *hdir2);
33437+
537831f9
AM
33438+int vfsub_create(struct inode *dir, struct path *path, int mode,
33439+ bool want_excl);
1facf9fc 33440+int vfsub_symlink(struct inode *dir, struct path *path,
33441+ const char *symname);
33442+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33443+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33444+ struct path *path, struct inode **delegated_inode);
1facf9fc 33445+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33446+ struct inode *hdir, struct path *path,
f2c43d5f 33447+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33448+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33449+int vfsub_rmdir(struct inode *dir, struct path *path);
33450+
33451+/* ---------------------------------------------------------------------- */
33452+
33453+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33454+ loff_t *ppos);
33455+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33456+ loff_t *ppos);
33457+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33458+ loff_t *ppos);
33459+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33460+ loff_t *ppos);
4a4d8108 33461+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33462+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33463+
c06a8ce3
AM
33464+static inline loff_t vfsub_f_size_read(struct file *file)
33465+{
33466+ return i_size_read(file_inode(file));
33467+}
33468+
4a4d8108
AM
33469+static inline unsigned int vfsub_file_flags(struct file *file)
33470+{
33471+ unsigned int flags;
33472+
33473+ spin_lock(&file->f_lock);
33474+ flags = file->f_flags;
33475+ spin_unlock(&file->f_lock);
33476+
33477+ return flags;
33478+}
1308ab2a 33479+
f0c0a007
AM
33480+static inline int vfsub_file_execed(struct file *file)
33481+{
33482+ /* todo: direct access f_flags */
33483+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33484+}
33485+
7e9cd9fe 33486+#if 0 /* reserved */
1facf9fc 33487+static inline void vfsub_file_accessed(struct file *h_file)
33488+{
33489+ file_accessed(h_file);
33490+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33491+}
7e9cd9fe 33492+#endif
1facf9fc 33493+
79b8bda9 33494+#if 0 /* reserved */
1facf9fc 33495+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33496+ struct dentry *h_dentry)
33497+{
33498+ struct path h_path = {
33499+ .dentry = h_dentry,
33500+ .mnt = h_mnt
33501+ };
92d182d2 33502+ touch_atime(&h_path);
1facf9fc 33503+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33504+}
79b8bda9 33505+#endif
1facf9fc 33506+
0c3ec466
AM
33507+static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
33508+ int flags)
33509+{
5afbbe0d 33510+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33511+ /* no vfsub_update_h_iattr() since we don't have struct path */
33512+}
33513+
8cdd5066
JR
33514+#ifdef CONFIG_FS_POSIX_ACL
33515+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33516+{
33517+ int err;
33518+
33519+ err = posix_acl_chmod(h_inode, h_mode);
33520+ if (err == -EOPNOTSUPP)
33521+ err = 0;
33522+ return err;
33523+}
33524+#else
33525+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33526+#endif
33527+
4a4d8108
AM
33528+long vfsub_splice_to(struct file *in, loff_t *ppos,
33529+ struct pipe_inode_info *pipe, size_t len,
33530+ unsigned int flags);
33531+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33532+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33533+
33534+static inline long vfsub_truncate(struct path *path, loff_t length)
33535+{
33536+ long err;
076b876e 33537+
c06a8ce3
AM
33538+ lockdep_off();
33539+ err = vfs_truncate(path, length);
33540+ lockdep_on();
33541+ return err;
33542+}
33543+
4a4d8108
AM
33544+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33545+ struct file *h_file);
53392da6 33546+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 33547+
521ced18
JR
33548+/*
33549+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33550+ * ioctl.
33551+ */
33552+static inline int vfsub_clone_file_range(struct file *src, struct file *dst,
33553+ u64 len)
33554+{
33555+ int err;
33556+
33557+ lockdep_off();
33558+ err = vfs_clone_file_range(src, 0, dst, 0, len);
33559+ lockdep_on();
33560+
33561+ return err;
33562+}
33563+
33564+/* copy_file_range(2) is a systemcall */
33565+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33566+ struct file *dst, loff_t dst_pos,
33567+ size_t len, unsigned int flags)
33568+{
33569+ ssize_t ssz;
33570+
33571+ lockdep_off();
33572+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33573+ lockdep_on();
33574+
33575+ return ssz;
33576+}
33577+
1facf9fc 33578+/* ---------------------------------------------------------------------- */
33579+
33580+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33581+{
33582+ loff_t err;
33583+
2cbb1c4b 33584+ lockdep_off();
1facf9fc 33585+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 33586+ lockdep_on();
1facf9fc 33587+ return err;
33588+}
33589+
33590+/* ---------------------------------------------------------------------- */
33591+
4a4d8108
AM
33592+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33593+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
33594+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33595+ struct inode **delegated_inode);
33596+int vfsub_notify_change(struct path *path, struct iattr *ia,
33597+ struct inode **delegated_inode);
33598+int vfsub_unlink(struct inode *dir, struct path *path,
33599+ struct inode **delegated_inode, int force);
4a4d8108 33600+
521ced18
JR
33601+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33602+{
33603+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33604+}
33605+
c1595e42
JR
33606+/* ---------------------------------------------------------------------- */
33607+
33608+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33609+ const void *value, size_t size, int flags)
33610+{
33611+ int err;
33612+
33613+ lockdep_off();
33614+ err = vfs_setxattr(dentry, name, value, size, flags);
33615+ lockdep_on();
33616+
33617+ return err;
33618+}
33619+
33620+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33621+{
33622+ int err;
33623+
33624+ lockdep_off();
33625+ err = vfs_removexattr(dentry, name);
33626+ lockdep_on();
33627+
33628+ return err;
33629+}
33630+
1facf9fc 33631+#endif /* __KERNEL__ */
33632+#endif /* __AUFS_VFSUB_H__ */
7f207e10
AM
33633diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33634--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 33635+++ linux/fs/aufs/wbr_policy.c 2018-04-15 08:49:13.404484168 +0200
f2c43d5f 33636@@ -0,0 +1,830 @@
1facf9fc 33637+/*
b00004a5 33638+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 33639+ *
33640+ * This program, aufs is free software; you can redistribute it and/or modify
33641+ * it under the terms of the GNU General Public License as published by
33642+ * the Free Software Foundation; either version 2 of the License, or
33643+ * (at your option) any later version.
dece6358
AM
33644+ *
33645+ * This program is distributed in the hope that it will be useful,
33646+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33647+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33648+ * GNU General Public License for more details.
33649+ *
33650+ * You should have received a copy of the GNU General Public License
523b37e3 33651+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33652+ */
33653+
33654+/*
33655+ * policies for selecting one among multiple writable branches
33656+ */
33657+
33658+#include <linux/statfs.h>
33659+#include "aufs.h"
33660+
33661+/* subset of cpup_attr() */
33662+static noinline_for_stack
33663+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33664+{
33665+ int err, sbits;
33666+ struct iattr ia;
33667+ struct inode *h_isrc;
33668+
5527c038 33669+ h_isrc = d_inode(h_src);
1facf9fc 33670+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33671+ ia.ia_mode = h_isrc->i_mode;
33672+ ia.ia_uid = h_isrc->i_uid;
33673+ ia.ia_gid = h_isrc->i_gid;
33674+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 33675+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
33676+ /* no delegation since it is just created */
33677+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33678+
33679+ /* is this nfs only? */
33680+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33681+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33682+ ia.ia_mode = h_isrc->i_mode;
523b37e3 33683+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33684+ }
33685+
33686+ return err;
33687+}
33688+
33689+#define AuCpdown_PARENT_OPQ 1
33690+#define AuCpdown_WHED (1 << 1)
33691+#define AuCpdown_MADE_DIR (1 << 2)
33692+#define AuCpdown_DIROPQ (1 << 3)
33693+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
33694+#define au_fset_cpdown(flags, name) \
33695+ do { (flags) |= AuCpdown_##name; } while (0)
33696+#define au_fclr_cpdown(flags, name) \
33697+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 33698+
1facf9fc 33699+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 33700+ unsigned int *flags)
1facf9fc 33701+{
33702+ int err;
33703+ struct dentry *opq_dentry;
33704+
33705+ opq_dentry = au_diropq_create(dentry, bdst);
33706+ err = PTR_ERR(opq_dentry);
33707+ if (IS_ERR(opq_dentry))
33708+ goto out;
33709+ dput(opq_dentry);
c2b27bf2 33710+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 33711+
4f0767ce 33712+out:
1facf9fc 33713+ return err;
33714+}
33715+
33716+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33717+ struct inode *dir, aufs_bindex_t bdst)
33718+{
33719+ int err;
33720+ struct path h_path;
33721+ struct au_branch *br;
33722+
33723+ br = au_sbr(dentry->d_sb, bdst);
33724+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
33725+ err = PTR_ERR(h_path.dentry);
33726+ if (IS_ERR(h_path.dentry))
33727+ goto out;
33728+
33729+ err = 0;
5527c038 33730+ if (d_is_positive(h_path.dentry)) {
86dc4139 33731+ h_path.mnt = au_br_mnt(br);
1facf9fc 33732+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
33733+ dentry);
33734+ }
33735+ dput(h_path.dentry);
33736+
4f0767ce 33737+out:
1facf9fc 33738+ return err;
33739+}
33740+
33741+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 33742+ struct au_pin *pin,
1facf9fc 33743+ struct dentry *h_parent, void *arg)
33744+{
33745+ int err, rerr;
5afbbe0d 33746+ aufs_bindex_t bopq, btop;
1facf9fc 33747+ struct path h_path;
33748+ struct dentry *parent;
33749+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 33750+ unsigned int *flags = arg;
1facf9fc 33751+
5afbbe0d 33752+ btop = au_dbtop(dentry);
1facf9fc 33753+ /* dentry is di-locked */
33754+ parent = dget_parent(dentry);
5527c038
JR
33755+ dir = d_inode(parent);
33756+ h_dir = d_inode(h_parent);
1facf9fc 33757+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
33758+ IMustLock(h_dir);
33759+
86dc4139 33760+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 33761+ if (unlikely(err < 0))
33762+ goto out;
33763+ h_path.dentry = au_h_dptr(dentry, bdst);
33764+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
33765+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
33766+ S_IRWXU | S_IRUGO | S_IXUGO);
33767+ if (unlikely(err))
33768+ goto out_put;
c2b27bf2 33769+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 33770+
1facf9fc 33771+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
33772+ au_fclr_cpdown(*flags, WHED);
33773+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 33774+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
33775+ au_fset_cpdown(*flags, WHED);
33776+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
33777+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 33778+ h_inode = d_inode(h_path.dentry);
febd17d6 33779+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
33780+ if (au_ftest_cpdown(*flags, WHED)) {
33781+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 33782+ if (unlikely(err)) {
febd17d6 33783+ inode_unlock(h_inode);
1facf9fc 33784+ goto out_dir;
33785+ }
33786+ }
33787+
5afbbe0d 33788+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 33789+ inode_unlock(h_inode);
1facf9fc 33790+ if (unlikely(err))
33791+ goto out_opq;
33792+
c2b27bf2 33793+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 33794+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
33795+ if (unlikely(err))
33796+ goto out_opq;
33797+ }
33798+
5527c038 33799+ inode = d_inode(dentry);
5afbbe0d
AM
33800+ if (au_ibbot(inode) < bdst)
33801+ au_set_ibbot(inode, bdst);
1facf9fc 33802+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
33803+ au_hi_flags(inode, /*isdir*/1));
076b876e 33804+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 33805+ goto out; /* success */
33806+
33807+ /* revert */
4f0767ce 33808+out_opq:
c2b27bf2 33809+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 33810+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 33811+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 33812+ inode_unlock(h_inode);
1facf9fc 33813+ if (unlikely(rerr)) {
523b37e3
AM
33814+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
33815+ dentry, bdst, rerr);
1facf9fc 33816+ err = -EIO;
33817+ goto out;
33818+ }
33819+ }
4f0767ce 33820+out_dir:
c2b27bf2 33821+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 33822+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
33823+ if (unlikely(rerr)) {
523b37e3
AM
33824+ AuIOErr("failed removing %pd b%d (%d)\n",
33825+ dentry, bdst, rerr);
1facf9fc 33826+ err = -EIO;
33827+ }
33828+ }
4f0767ce 33829+out_put:
1facf9fc 33830+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
33831+ if (au_dbbot(dentry) == bdst)
33832+ au_update_dbbot(dentry);
4f0767ce 33833+out:
1facf9fc 33834+ dput(parent);
33835+ return err;
33836+}
33837+
33838+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
33839+{
33840+ int err;
c2b27bf2 33841+ unsigned int flags;
1facf9fc 33842+
c2b27bf2
AM
33843+ flags = 0;
33844+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 33845+
33846+ return err;
33847+}
33848+
33849+/* ---------------------------------------------------------------------- */
33850+
33851+/* policies for create */
33852+
c2b27bf2 33853+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
33854+{
33855+ int err, i, j, ndentry;
33856+ aufs_bindex_t bopq;
33857+ struct au_dcsub_pages dpages;
33858+ struct au_dpage *dpage;
33859+ struct dentry **dentries, *parent, *d;
33860+
33861+ err = au_dpages_init(&dpages, GFP_NOFS);
33862+ if (unlikely(err))
33863+ goto out;
33864+ parent = dget_parent(dentry);
027c5e7a 33865+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
33866+ if (unlikely(err))
33867+ goto out_free;
33868+
33869+ err = bindex;
33870+ for (i = 0; i < dpages.ndpage; i++) {
33871+ dpage = dpages.dpages + i;
33872+ dentries = dpage->dentries;
33873+ ndentry = dpage->ndentry;
33874+ for (j = 0; j < ndentry; j++) {
33875+ d = dentries[j];
33876+ di_read_lock_parent2(d, !AuLock_IR);
33877+ bopq = au_dbdiropq(d);
33878+ di_read_unlock(d, !AuLock_IR);
33879+ if (bopq >= 0 && bopq < err)
33880+ err = bopq;
33881+ }
33882+ }
33883+
33884+out_free:
33885+ dput(parent);
33886+ au_dpages_free(&dpages);
33887+out:
33888+ return err;
33889+}
33890+
1facf9fc 33891+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
33892+{
33893+ for (; bindex >= 0; bindex--)
33894+ if (!au_br_rdonly(au_sbr(sb, bindex)))
33895+ return bindex;
33896+ return -EROFS;
33897+}
33898+
33899+/* top down parent */
392086de
AM
33900+static int au_wbr_create_tdp(struct dentry *dentry,
33901+ unsigned int flags __maybe_unused)
1facf9fc 33902+{
33903+ int err;
5afbbe0d 33904+ aufs_bindex_t btop, bindex;
1facf9fc 33905+ struct super_block *sb;
33906+ struct dentry *parent, *h_parent;
33907+
33908+ sb = dentry->d_sb;
5afbbe0d
AM
33909+ btop = au_dbtop(dentry);
33910+ err = btop;
33911+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 33912+ goto out;
33913+
33914+ err = -EROFS;
33915+ parent = dget_parent(dentry);
5afbbe0d 33916+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 33917+ h_parent = au_h_dptr(parent, bindex);
5527c038 33918+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 33919+ continue;
33920+
33921+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
33922+ err = bindex;
33923+ break;
33924+ }
33925+ }
33926+ dput(parent);
33927+
33928+ /* bottom up here */
4a4d8108 33929+ if (unlikely(err < 0)) {
5afbbe0d 33930+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
33931+ if (err >= 0)
33932+ err = au_wbr_nonopq(dentry, err);
33933+ }
1facf9fc 33934+
4f0767ce 33935+out:
1facf9fc 33936+ AuDbg("b%d\n", err);
33937+ return err;
33938+}
33939+
33940+/* ---------------------------------------------------------------------- */
33941+
33942+/* an exception for the policy other than tdp */
33943+static int au_wbr_create_exp(struct dentry *dentry)
33944+{
33945+ int err;
33946+ aufs_bindex_t bwh, bdiropq;
33947+ struct dentry *parent;
33948+
33949+ err = -1;
33950+ bwh = au_dbwh(dentry);
33951+ parent = dget_parent(dentry);
33952+ bdiropq = au_dbdiropq(parent);
33953+ if (bwh >= 0) {
33954+ if (bdiropq >= 0)
33955+ err = min(bdiropq, bwh);
33956+ else
33957+ err = bwh;
33958+ AuDbg("%d\n", err);
33959+ } else if (bdiropq >= 0) {
33960+ err = bdiropq;
33961+ AuDbg("%d\n", err);
33962+ }
33963+ dput(parent);
33964+
4a4d8108
AM
33965+ if (err >= 0)
33966+ err = au_wbr_nonopq(dentry, err);
33967+
1facf9fc 33968+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
33969+ err = -1;
33970+
33971+ AuDbg("%d\n", err);
33972+ return err;
33973+}
33974+
33975+/* ---------------------------------------------------------------------- */
33976+
33977+/* round robin */
33978+static int au_wbr_create_init_rr(struct super_block *sb)
33979+{
33980+ int err;
33981+
5afbbe0d 33982+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 33983+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 33984+ /* smp_mb(); */
1facf9fc 33985+
33986+ AuDbg("b%d\n", err);
33987+ return err;
33988+}
33989+
392086de 33990+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 33991+{
33992+ int err, nbr;
33993+ unsigned int u;
5afbbe0d 33994+ aufs_bindex_t bindex, bbot;
1facf9fc 33995+ struct super_block *sb;
33996+ atomic_t *next;
33997+
33998+ err = au_wbr_create_exp(dentry);
33999+ if (err >= 0)
34000+ goto out;
34001+
34002+ sb = dentry->d_sb;
34003+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34004+ bbot = au_sbbot(sb);
34005+ nbr = bbot + 1;
34006+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34007+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34008+ err = atomic_dec_return(next) + 1;
34009+ /* modulo for 0 is meaningless */
34010+ if (unlikely(!err))
34011+ err = atomic_dec_return(next) + 1;
34012+ } else
34013+ err = atomic_read(next);
34014+ AuDbg("%d\n", err);
34015+ u = err;
34016+ err = u % nbr;
34017+ AuDbg("%d\n", err);
34018+ if (!au_br_rdonly(au_sbr(sb, err)))
34019+ break;
34020+ err = -EROFS;
34021+ }
34022+
4a4d8108
AM
34023+ if (err >= 0)
34024+ err = au_wbr_nonopq(dentry, err);
34025+
4f0767ce 34026+out:
1facf9fc 34027+ AuDbg("%d\n", err);
34028+ return err;
34029+}
34030+
34031+/* ---------------------------------------------------------------------- */
34032+
34033+/* most free space */
392086de 34034+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34035+{
34036+ struct super_block *sb;
34037+ struct au_branch *br;
34038+ struct au_wbr_mfs *mfs;
392086de 34039+ struct dentry *h_parent;
5afbbe0d 34040+ aufs_bindex_t bindex, bbot;
1facf9fc 34041+ int err;
34042+ unsigned long long b, bavail;
7f207e10 34043+ struct path h_path;
1facf9fc 34044+ /* reduce the stack usage */
34045+ struct kstatfs *st;
34046+
34047+ st = kmalloc(sizeof(*st), GFP_NOFS);
34048+ if (unlikely(!st)) {
34049+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34050+ return;
34051+ }
34052+
34053+ bavail = 0;
34054+ sb = dentry->d_sb;
34055+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34056+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34057+ mfs->mfs_bindex = -EROFS;
34058+ mfs->mfsrr_bytes = 0;
392086de
AM
34059+ if (!parent) {
34060+ bindex = 0;
5afbbe0d 34061+ bbot = au_sbbot(sb);
392086de 34062+ } else {
5afbbe0d
AM
34063+ bindex = au_dbtop(parent);
34064+ bbot = au_dbtaildir(parent);
392086de
AM
34065+ }
34066+
5afbbe0d 34067+ for (; bindex <= bbot; bindex++) {
392086de
AM
34068+ if (parent) {
34069+ h_parent = au_h_dptr(parent, bindex);
5527c038 34070+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34071+ continue;
34072+ }
1facf9fc 34073+ br = au_sbr(sb, bindex);
34074+ if (au_br_rdonly(br))
34075+ continue;
34076+
34077+ /* sb->s_root for NFS is unreliable */
86dc4139 34078+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34079+ h_path.dentry = h_path.mnt->mnt_root;
34080+ err = vfs_statfs(&h_path, st);
1facf9fc 34081+ if (unlikely(err)) {
34082+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34083+ continue;
34084+ }
34085+
34086+ /* when the available size is equal, select the lower one */
34087+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34088+ || sizeof(b) < sizeof(st->f_bsize));
34089+ b = st->f_bavail * st->f_bsize;
34090+ br->br_wbr->wbr_bytes = b;
34091+ if (b >= bavail) {
34092+ bavail = b;
34093+ mfs->mfs_bindex = bindex;
34094+ mfs->mfs_jiffy = jiffies;
34095+ }
34096+ }
34097+
34098+ mfs->mfsrr_bytes = bavail;
34099+ AuDbg("b%d\n", mfs->mfs_bindex);
1c60b727 34100+ kfree(st);
1facf9fc 34101+}
34102+
392086de 34103+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34104+{
34105+ int err;
392086de 34106+ struct dentry *parent;
1facf9fc 34107+ struct super_block *sb;
34108+ struct au_wbr_mfs *mfs;
34109+
34110+ err = au_wbr_create_exp(dentry);
34111+ if (err >= 0)
34112+ goto out;
34113+
34114+ sb = dentry->d_sb;
392086de
AM
34115+ parent = NULL;
34116+ if (au_ftest_wbr(flags, PARENT))
34117+ parent = dget_parent(dentry);
1facf9fc 34118+ mfs = &au_sbi(sb)->si_wbr_mfs;
34119+ mutex_lock(&mfs->mfs_lock);
34120+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34121+ || mfs->mfs_bindex < 0
34122+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34123+ au_mfs(dentry, parent);
1facf9fc 34124+ mutex_unlock(&mfs->mfs_lock);
34125+ err = mfs->mfs_bindex;
392086de 34126+ dput(parent);
1facf9fc 34127+
4a4d8108
AM
34128+ if (err >= 0)
34129+ err = au_wbr_nonopq(dentry, err);
34130+
4f0767ce 34131+out:
1facf9fc 34132+ AuDbg("b%d\n", err);
34133+ return err;
34134+}
34135+
34136+static int au_wbr_create_init_mfs(struct super_block *sb)
34137+{
34138+ struct au_wbr_mfs *mfs;
34139+
34140+ mfs = &au_sbi(sb)->si_wbr_mfs;
34141+ mutex_init(&mfs->mfs_lock);
34142+ mfs->mfs_jiffy = 0;
34143+ mfs->mfs_bindex = -EROFS;
34144+
34145+ return 0;
34146+}
34147+
34148+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34149+{
34150+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34151+ return 0;
34152+}
34153+
34154+/* ---------------------------------------------------------------------- */
34155+
f2c43d5f
AM
34156+/* top down regardless parent, and then mfs */
34157+static int au_wbr_create_tdmfs(struct dentry *dentry,
34158+ unsigned int flags __maybe_unused)
34159+{
34160+ int err;
34161+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34162+ unsigned long long watermark;
34163+ struct super_block *sb;
34164+ struct au_wbr_mfs *mfs;
34165+ struct au_branch *br;
34166+ struct dentry *parent;
34167+
34168+ sb = dentry->d_sb;
34169+ mfs = &au_sbi(sb)->si_wbr_mfs;
34170+ mutex_lock(&mfs->mfs_lock);
34171+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34172+ || mfs->mfs_bindex < 0)
34173+ au_mfs(dentry, /*parent*/NULL);
34174+ watermark = mfs->mfsrr_watermark;
34175+ bmfs = mfs->mfs_bindex;
34176+ mutex_unlock(&mfs->mfs_lock);
34177+
34178+ /* another style of au_wbr_create_exp() */
34179+ bwh = au_dbwh(dentry);
34180+ parent = dget_parent(dentry);
34181+ btail = au_dbtaildir(parent);
34182+ if (bwh >= 0 && bwh < btail)
34183+ btail = bwh;
34184+
34185+ err = au_wbr_nonopq(dentry, btail);
34186+ if (unlikely(err < 0))
34187+ goto out;
34188+ btail = err;
34189+ bfound = -1;
34190+ for (bindex = 0; bindex <= btail; bindex++) {
34191+ br = au_sbr(sb, bindex);
34192+ if (au_br_rdonly(br))
34193+ continue;
34194+ if (br->br_wbr->wbr_bytes > watermark) {
34195+ bfound = bindex;
34196+ break;
34197+ }
34198+ }
34199+ err = bfound;
34200+ if (err < 0)
34201+ err = bmfs;
34202+
34203+out:
34204+ dput(parent);
34205+ AuDbg("b%d\n", err);
34206+ return err;
34207+}
34208+
34209+/* ---------------------------------------------------------------------- */
34210+
1facf9fc 34211+/* most free space and then round robin */
392086de 34212+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34213+{
34214+ int err;
34215+ struct au_wbr_mfs *mfs;
34216+
392086de 34217+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34218+ if (err >= 0) {
34219+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34220+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34221+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34222+ err = au_wbr_create_rr(dentry, flags);
dece6358 34223+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34224+ }
34225+
34226+ AuDbg("b%d\n", err);
34227+ return err;
34228+}
34229+
34230+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34231+{
34232+ int err;
34233+
34234+ au_wbr_create_init_mfs(sb); /* ignore */
34235+ err = au_wbr_create_init_rr(sb);
34236+
34237+ return err;
34238+}
34239+
34240+/* ---------------------------------------------------------------------- */
34241+
34242+/* top down parent and most free space */
392086de 34243+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34244+{
34245+ int err, e2;
34246+ unsigned long long b;
5afbbe0d 34247+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34248+ struct super_block *sb;
34249+ struct dentry *parent, *h_parent;
34250+ struct au_branch *br;
34251+
392086de 34252+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34253+ if (unlikely(err < 0))
34254+ goto out;
34255+ parent = dget_parent(dentry);
5afbbe0d
AM
34256+ btop = au_dbtop(parent);
34257+ bbot = au_dbtaildir(parent);
34258+ if (btop == bbot)
1facf9fc 34259+ goto out_parent; /* success */
34260+
392086de 34261+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34262+ if (e2 < 0)
34263+ goto out_parent; /* success */
34264+
34265+ /* when the available size is equal, select upper one */
34266+ sb = dentry->d_sb;
34267+ br = au_sbr(sb, err);
34268+ b = br->br_wbr->wbr_bytes;
34269+ AuDbg("b%d, %llu\n", err, b);
34270+
5afbbe0d 34271+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34272+ h_parent = au_h_dptr(parent, bindex);
5527c038 34273+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34274+ continue;
34275+
34276+ br = au_sbr(sb, bindex);
34277+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34278+ b = br->br_wbr->wbr_bytes;
34279+ err = bindex;
34280+ AuDbg("b%d, %llu\n", err, b);
34281+ }
34282+ }
34283+
4a4d8108
AM
34284+ if (err >= 0)
34285+ err = au_wbr_nonopq(dentry, err);
34286+
4f0767ce 34287+out_parent:
1facf9fc 34288+ dput(parent);
4f0767ce 34289+out:
1facf9fc 34290+ AuDbg("b%d\n", err);
34291+ return err;
34292+}
34293+
34294+/* ---------------------------------------------------------------------- */
34295+
392086de
AM
34296+/*
34297+ * - top down parent
34298+ * - most free space with parent
34299+ * - most free space round-robin regardless parent
34300+ */
34301+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34302+{
34303+ int err;
34304+ unsigned long long watermark;
34305+ struct super_block *sb;
34306+ struct au_branch *br;
34307+ struct au_wbr_mfs *mfs;
34308+
34309+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34310+ if (unlikely(err < 0))
34311+ goto out;
34312+
34313+ sb = dentry->d_sb;
34314+ br = au_sbr(sb, err);
34315+ mfs = &au_sbi(sb)->si_wbr_mfs;
34316+ mutex_lock(&mfs->mfs_lock);
34317+ watermark = mfs->mfsrr_watermark;
34318+ mutex_unlock(&mfs->mfs_lock);
34319+ if (br->br_wbr->wbr_bytes < watermark)
34320+ /* regardless the parent dir */
34321+ err = au_wbr_create_mfsrr(dentry, flags);
34322+
34323+out:
34324+ AuDbg("b%d\n", err);
34325+ return err;
34326+}
34327+
34328+/* ---------------------------------------------------------------------- */
34329+
1facf9fc 34330+/* policies for copyup */
34331+
34332+/* top down parent */
34333+static int au_wbr_copyup_tdp(struct dentry *dentry)
34334+{
392086de 34335+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34336+}
34337+
34338+/* bottom up parent */
34339+static int au_wbr_copyup_bup(struct dentry *dentry)
34340+{
34341+ int err;
5afbbe0d 34342+ aufs_bindex_t bindex, btop;
1facf9fc 34343+ struct dentry *parent, *h_parent;
34344+ struct super_block *sb;
34345+
34346+ err = -EROFS;
34347+ sb = dentry->d_sb;
34348+ parent = dget_parent(dentry);
5afbbe0d
AM
34349+ btop = au_dbtop(parent);
34350+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34351+ h_parent = au_h_dptr(parent, bindex);
5527c038 34352+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34353+ continue;
34354+
34355+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34356+ err = bindex;
34357+ break;
34358+ }
34359+ }
34360+ dput(parent);
34361+
34362+ /* bottom up here */
34363+ if (unlikely(err < 0))
5afbbe0d 34364+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34365+
34366+ AuDbg("b%d\n", err);
34367+ return err;
34368+}
34369+
34370+/* bottom up */
5afbbe0d 34371+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34372+{
34373+ int err;
34374+
5afbbe0d 34375+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34376+ AuDbg("b%d\n", err);
5afbbe0d 34377+ if (err > btop)
4a4d8108 34378+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34379+
34380+ AuDbg("b%d\n", err);
34381+ return err;
34382+}
34383+
076b876e
AM
34384+static int au_wbr_copyup_bu(struct dentry *dentry)
34385+{
34386+ int err;
5afbbe0d 34387+ aufs_bindex_t btop;
076b876e 34388+
5afbbe0d
AM
34389+ btop = au_dbtop(dentry);
34390+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34391+ return err;
34392+}
34393+
1facf9fc 34394+/* ---------------------------------------------------------------------- */
34395+
34396+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34397+ [AuWbrCopyup_TDP] = {
34398+ .copyup = au_wbr_copyup_tdp
34399+ },
34400+ [AuWbrCopyup_BUP] = {
34401+ .copyup = au_wbr_copyup_bup
34402+ },
34403+ [AuWbrCopyup_BU] = {
34404+ .copyup = au_wbr_copyup_bu
34405+ }
34406+};
34407+
34408+struct au_wbr_create_operations au_wbr_create_ops[] = {
34409+ [AuWbrCreate_TDP] = {
34410+ .create = au_wbr_create_tdp
34411+ },
34412+ [AuWbrCreate_RR] = {
34413+ .create = au_wbr_create_rr,
34414+ .init = au_wbr_create_init_rr
34415+ },
34416+ [AuWbrCreate_MFS] = {
34417+ .create = au_wbr_create_mfs,
34418+ .init = au_wbr_create_init_mfs,
34419+ .fin = au_wbr_create_fin_mfs
34420+ },
34421+ [AuWbrCreate_MFSV] = {
34422+ .create = au_wbr_create_mfs,
34423+ .init = au_wbr_create_init_mfs,
34424+ .fin = au_wbr_create_fin_mfs
34425+ },
34426+ [AuWbrCreate_MFSRR] = {
34427+ .create = au_wbr_create_mfsrr,
34428+ .init = au_wbr_create_init_mfsrr,
34429+ .fin = au_wbr_create_fin_mfs
34430+ },
34431+ [AuWbrCreate_MFSRRV] = {
34432+ .create = au_wbr_create_mfsrr,
34433+ .init = au_wbr_create_init_mfsrr,
34434+ .fin = au_wbr_create_fin_mfs
34435+ },
f2c43d5f
AM
34436+ [AuWbrCreate_TDMFS] = {
34437+ .create = au_wbr_create_tdmfs,
34438+ .init = au_wbr_create_init_mfs,
34439+ .fin = au_wbr_create_fin_mfs
34440+ },
34441+ [AuWbrCreate_TDMFSV] = {
34442+ .create = au_wbr_create_tdmfs,
34443+ .init = au_wbr_create_init_mfs,
34444+ .fin = au_wbr_create_fin_mfs
34445+ },
1facf9fc 34446+ [AuWbrCreate_PMFS] = {
34447+ .create = au_wbr_create_pmfs,
34448+ .init = au_wbr_create_init_mfs,
34449+ .fin = au_wbr_create_fin_mfs
34450+ },
34451+ [AuWbrCreate_PMFSV] = {
34452+ .create = au_wbr_create_pmfs,
34453+ .init = au_wbr_create_init_mfs,
34454+ .fin = au_wbr_create_fin_mfs
392086de
AM
34455+ },
34456+ [AuWbrCreate_PMFSRR] = {
34457+ .create = au_wbr_create_pmfsrr,
34458+ .init = au_wbr_create_init_mfsrr,
34459+ .fin = au_wbr_create_fin_mfs
34460+ },
34461+ [AuWbrCreate_PMFSRRV] = {
34462+ .create = au_wbr_create_pmfsrr,
34463+ .init = au_wbr_create_init_mfsrr,
34464+ .fin = au_wbr_create_fin_mfs
1facf9fc 34465+ }
34466+};
7f207e10
AM
34467diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34468--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 34469+++ linux/fs/aufs/whout.c 2018-04-15 08:49:13.404484168 +0200
f2c43d5f 34470@@ -0,0 +1,1061 @@
1facf9fc 34471+/*
b00004a5 34472+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 34473+ *
34474+ * This program, aufs is free software; you can redistribute it and/or modify
34475+ * it under the terms of the GNU General Public License as published by
34476+ * the Free Software Foundation; either version 2 of the License, or
34477+ * (at your option) any later version.
dece6358
AM
34478+ *
34479+ * This program is distributed in the hope that it will be useful,
34480+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34481+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34482+ * GNU General Public License for more details.
34483+ *
34484+ * You should have received a copy of the GNU General Public License
523b37e3 34485+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34486+ */
34487+
34488+/*
34489+ * whiteout for logical deletion and opaque directory
34490+ */
34491+
1facf9fc 34492+#include "aufs.h"
34493+
34494+#define WH_MASK S_IRUGO
34495+
34496+/*
34497+ * If a directory contains this file, then it is opaque. We start with the
34498+ * .wh. flag so that it is blocked by lookup.
34499+ */
0c3ec466
AM
34500+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34501+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34502+
34503+/*
34504+ * generate whiteout name, which is NOT terminated by NULL.
34505+ * @name: original d_name.name
34506+ * @len: original d_name.len
34507+ * @wh: whiteout qstr
34508+ * returns zero when succeeds, otherwise error.
34509+ * succeeded value as wh->name should be freed by kfree().
34510+ */
34511+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34512+{
34513+ char *p;
34514+
34515+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34516+ return -ENAMETOOLONG;
34517+
34518+ wh->len = name->len + AUFS_WH_PFX_LEN;
34519+ p = kmalloc(wh->len, GFP_NOFS);
34520+ wh->name = p;
34521+ if (p) {
34522+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34523+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34524+ /* smp_mb(); */
34525+ return 0;
34526+ }
34527+ return -ENOMEM;
34528+}
34529+
34530+/* ---------------------------------------------------------------------- */
34531+
34532+/*
34533+ * test if the @wh_name exists under @h_parent.
34534+ * @try_sio specifies the necessary of super-io.
34535+ */
076b876e 34536+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 34537+{
34538+ int err;
34539+ struct dentry *wh_dentry;
1facf9fc 34540+
1facf9fc 34541+ if (!try_sio)
b4510431 34542+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 34543+ else
076b876e 34544+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 34545+ err = PTR_ERR(wh_dentry);
2000de60
JR
34546+ if (IS_ERR(wh_dentry)) {
34547+ if (err == -ENAMETOOLONG)
34548+ err = 0;
1facf9fc 34549+ goto out;
2000de60 34550+ }
1facf9fc 34551+
34552+ err = 0;
5527c038 34553+ if (d_is_negative(wh_dentry))
1facf9fc 34554+ goto out_wh; /* success */
34555+
34556+ err = 1;
7e9cd9fe 34557+ if (d_is_reg(wh_dentry))
1facf9fc 34558+ goto out_wh; /* success */
34559+
34560+ err = -EIO;
523b37e3 34561+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 34562+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 34563+
4f0767ce 34564+out_wh:
1facf9fc 34565+ dput(wh_dentry);
4f0767ce 34566+out:
1facf9fc 34567+ return err;
34568+}
34569+
34570+/*
34571+ * test if the @h_dentry sets opaque or not.
34572+ */
076b876e 34573+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 34574+{
34575+ int err;
34576+ struct inode *h_dir;
34577+
5527c038 34578+ h_dir = d_inode(h_dentry);
076b876e 34579+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 34580+ au_test_h_perm_sio(h_dir, MAY_EXEC));
34581+ return err;
34582+}
34583+
34584+/*
34585+ * returns a negative dentry whose name is unique and temporary.
34586+ */
34587+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34588+ struct qstr *prefix)
34589+{
1facf9fc 34590+ struct dentry *dentry;
34591+ int i;
027c5e7a 34592+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 34593+ *name, *p;
027c5e7a 34594+ /* strict atomic_t is unnecessary here */
1facf9fc 34595+ static unsigned short cnt;
34596+ struct qstr qs;
34597+
4a4d8108
AM
34598+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34599+
1facf9fc 34600+ name = defname;
027c5e7a
AM
34601+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34602+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 34603+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 34604+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 34605+ goto out;
34606+ dentry = ERR_PTR(-ENOMEM);
34607+ name = kmalloc(qs.len + 1, GFP_NOFS);
34608+ if (unlikely(!name))
34609+ goto out;
34610+ }
34611+
34612+ /* doubly whiteout-ed */
34613+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34614+ p = name + AUFS_WH_PFX_LEN * 2;
34615+ memcpy(p, prefix->name, prefix->len);
34616+ p += prefix->len;
34617+ *p++ = '.';
4a4d8108 34618+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 34619+
34620+ qs.name = name;
34621+ for (i = 0; i < 3; i++) {
b752ccd1 34622+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 34623+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 34624+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 34625+ goto out_name;
34626+ dput(dentry);
34627+ }
0c3ec466 34628+ /* pr_warn("could not get random name\n"); */
1facf9fc 34629+ dentry = ERR_PTR(-EEXIST);
34630+ AuDbg("%.*s\n", AuLNPair(&qs));
34631+ BUG();
34632+
4f0767ce 34633+out_name:
1facf9fc 34634+ if (name != defname)
1c60b727 34635+ kfree(name);
4f0767ce 34636+out:
4a4d8108 34637+ AuTraceErrPtr(dentry);
1facf9fc 34638+ return dentry;
1facf9fc 34639+}
34640+
34641+/*
34642+ * rename the @h_dentry on @br to the whiteouted temporary name.
34643+ */
34644+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34645+{
34646+ int err;
34647+ struct path h_path = {
86dc4139 34648+ .mnt = au_br_mnt(br)
1facf9fc 34649+ };
523b37e3 34650+ struct inode *h_dir, *delegated;
1facf9fc 34651+ struct dentry *h_parent;
34652+
34653+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 34654+ h_dir = d_inode(h_parent);
1facf9fc 34655+ IMustLock(h_dir);
34656+
34657+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34658+ err = PTR_ERR(h_path.dentry);
34659+ if (IS_ERR(h_path.dentry))
34660+ goto out;
34661+
34662+ /* under the same dir, no need to lock_rename() */
523b37e3 34663+ delegated = NULL;
f2c43d5f
AM
34664+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34665+ /*flags*/0);
1facf9fc 34666+ AuTraceErr(err);
523b37e3
AM
34667+ if (unlikely(err == -EWOULDBLOCK)) {
34668+ pr_warn("cannot retry for NFSv4 delegation"
34669+ " for an internal rename\n");
34670+ iput(delegated);
34671+ }
1facf9fc 34672+ dput(h_path.dentry);
34673+
4f0767ce 34674+out:
4a4d8108 34675+ AuTraceErr(err);
1facf9fc 34676+ return err;
34677+}
34678+
34679+/* ---------------------------------------------------------------------- */
34680+/*
34681+ * functions for removing a whiteout
34682+ */
34683+
34684+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34685+{
523b37e3
AM
34686+ int err, force;
34687+ struct inode *delegated;
1facf9fc 34688+
34689+ /*
34690+ * forces superio when the dir has a sticky bit.
34691+ * this may be a violation of unix fs semantics.
34692+ */
34693+ force = (h_dir->i_mode & S_ISVTX)
5527c038 34694+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
34695+ delegated = NULL;
34696+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
34697+ if (unlikely(err == -EWOULDBLOCK)) {
34698+ pr_warn("cannot retry for NFSv4 delegation"
34699+ " for an internal unlink\n");
34700+ iput(delegated);
34701+ }
34702+ return err;
1facf9fc 34703+}
34704+
34705+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34706+ struct dentry *dentry)
34707+{
34708+ int err;
34709+
34710+ err = do_unlink_wh(h_dir, h_path);
34711+ if (!err && dentry)
34712+ au_set_dbwh(dentry, -1);
34713+
34714+ return err;
34715+}
34716+
34717+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
34718+ struct au_branch *br)
34719+{
34720+ int err;
34721+ struct path h_path = {
86dc4139 34722+ .mnt = au_br_mnt(br)
1facf9fc 34723+ };
34724+
34725+ err = 0;
b4510431 34726+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 34727+ if (IS_ERR(h_path.dentry))
34728+ err = PTR_ERR(h_path.dentry);
34729+ else {
5527c038
JR
34730+ if (d_is_reg(h_path.dentry))
34731+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 34732+ dput(h_path.dentry);
34733+ }
34734+
34735+ return err;
34736+}
34737+
34738+/* ---------------------------------------------------------------------- */
34739+/*
34740+ * initialize/clean whiteout for a branch
34741+ */
34742+
34743+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
34744+ const int isdir)
34745+{
34746+ int err;
523b37e3 34747+ struct inode *delegated;
1facf9fc 34748+
5527c038 34749+ if (d_is_negative(whpath->dentry))
1facf9fc 34750+ return;
34751+
86dc4139
AM
34752+ if (isdir)
34753+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
34754+ else {
34755+ delegated = NULL;
34756+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
34757+ if (unlikely(err == -EWOULDBLOCK)) {
34758+ pr_warn("cannot retry for NFSv4 delegation"
34759+ " for an internal unlink\n");
34760+ iput(delegated);
34761+ }
34762+ }
1facf9fc 34763+ if (unlikely(err))
523b37e3
AM
34764+ pr_warn("failed removing %pd (%d), ignored.\n",
34765+ whpath->dentry, err);
1facf9fc 34766+}
34767+
34768+static int test_linkable(struct dentry *h_root)
34769+{
5527c038 34770+ struct inode *h_dir = d_inode(h_root);
1facf9fc 34771+
34772+ if (h_dir->i_op->link)
34773+ return 0;
34774+
523b37e3
AM
34775+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
34776+ h_root, au_sbtype(h_root->d_sb));
1facf9fc 34777+ return -ENOSYS;
34778+}
34779+
34780+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
34781+static int au_whdir(struct inode *h_dir, struct path *path)
34782+{
34783+ int err;
34784+
34785+ err = -EEXIST;
5527c038 34786+ if (d_is_negative(path->dentry)) {
1facf9fc 34787+ int mode = S_IRWXU;
34788+
34789+ if (au_test_nfs(path->dentry->d_sb))
34790+ mode |= S_IXUGO;
86dc4139 34791+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 34792+ } else if (d_is_dir(path->dentry))
1facf9fc 34793+ err = 0;
34794+ else
523b37e3 34795+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 34796+
34797+ return err;
34798+}
34799+
34800+struct au_wh_base {
34801+ const struct qstr *name;
34802+ struct dentry *dentry;
34803+};
34804+
34805+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
34806+ struct path *h_path)
34807+{
34808+ h_path->dentry = base[AuBrWh_BASE].dentry;
34809+ au_wh_clean(h_dir, h_path, /*isdir*/0);
34810+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34811+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34812+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34813+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34814+}
34815+
34816+/*
34817+ * returns tri-state,
c1595e42 34818+ * minus: error, caller should print the message
1facf9fc 34819+ * zero: succuess
c1595e42 34820+ * plus: error, caller should NOT print the message
1facf9fc 34821+ */
34822+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
34823+ int do_plink, struct au_wh_base base[],
34824+ struct path *h_path)
34825+{
34826+ int err;
34827+ struct inode *h_dir;
34828+
5527c038 34829+ h_dir = d_inode(h_root);
1facf9fc 34830+ h_path->dentry = base[AuBrWh_BASE].dentry;
34831+ au_wh_clean(h_dir, h_path, /*isdir*/0);
34832+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34833+ if (do_plink) {
34834+ err = test_linkable(h_root);
34835+ if (unlikely(err)) {
34836+ err = 1;
34837+ goto out;
34838+ }
34839+
34840+ err = au_whdir(h_dir, h_path);
34841+ if (unlikely(err))
34842+ goto out;
34843+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
34844+ } else
34845+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34846+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34847+ err = au_whdir(h_dir, h_path);
34848+ if (unlikely(err))
34849+ goto out;
34850+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
34851+
4f0767ce 34852+out:
1facf9fc 34853+ return err;
34854+}
34855+
34856+/*
34857+ * for the moment, aufs supports the branch filesystem which does not support
34858+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
34859+ * copyup failed. finally, such filesystem will not be used as the writable
34860+ * branch.
34861+ *
34862+ * returns tri-state, see above.
34863+ */
34864+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
34865+ int do_plink, struct au_wh_base base[],
34866+ struct path *h_path)
34867+{
34868+ int err;
34869+ struct inode *h_dir;
34870+
1308ab2a 34871+ WbrWhMustWriteLock(wbr);
34872+
1facf9fc 34873+ err = test_linkable(h_root);
34874+ if (unlikely(err)) {
34875+ err = 1;
34876+ goto out;
34877+ }
34878+
34879+ /*
34880+ * todo: should this create be done in /sbin/mount.aufs helper?
34881+ */
34882+ err = -EEXIST;
5527c038
JR
34883+ h_dir = d_inode(h_root);
34884+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
34885+ h_path->dentry = base[AuBrWh_BASE].dentry;
34886+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 34887+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 34888+ err = 0;
34889+ else
523b37e3 34890+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 34891+ if (unlikely(err))
34892+ goto out;
34893+
34894+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34895+ if (do_plink) {
34896+ err = au_whdir(h_dir, h_path);
34897+ if (unlikely(err))
34898+ goto out;
34899+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
34900+ } else
34901+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34902+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
34903+
34904+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34905+ err = au_whdir(h_dir, h_path);
34906+ if (unlikely(err))
34907+ goto out;
34908+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
34909+
4f0767ce 34910+out:
1facf9fc 34911+ return err;
34912+}
34913+
34914+/*
34915+ * initialize the whiteout base file/dir for @br.
34916+ */
86dc4139 34917+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 34918+{
34919+ int err, i;
34920+ const unsigned char do_plink
34921+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 34922+ struct inode *h_dir;
86dc4139
AM
34923+ struct path path = br->br_path;
34924+ struct dentry *h_root = path.dentry;
1facf9fc 34925+ struct au_wbr *wbr = br->br_wbr;
34926+ static const struct qstr base_name[] = {
0c3ec466
AM
34927+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
34928+ sizeof(AUFS_BASE_NAME) - 1),
34929+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
34930+ sizeof(AUFS_PLINKDIR_NAME) - 1),
34931+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
34932+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 34933+ };
34934+ struct au_wh_base base[] = {
34935+ [AuBrWh_BASE] = {
34936+ .name = base_name + AuBrWh_BASE,
34937+ .dentry = NULL
34938+ },
34939+ [AuBrWh_PLINK] = {
34940+ .name = base_name + AuBrWh_PLINK,
34941+ .dentry = NULL
34942+ },
34943+ [AuBrWh_ORPH] = {
34944+ .name = base_name + AuBrWh_ORPH,
34945+ .dentry = NULL
34946+ }
34947+ };
34948+
1308ab2a 34949+ if (wbr)
34950+ WbrWhMustWriteLock(wbr);
1facf9fc 34951+
1facf9fc 34952+ for (i = 0; i < AuBrWh_Last; i++) {
34953+ /* doubly whiteouted */
34954+ struct dentry *d;
34955+
34956+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
34957+ err = PTR_ERR(d);
34958+ if (IS_ERR(d))
34959+ goto out;
34960+
34961+ base[i].dentry = d;
34962+ AuDebugOn(wbr
34963+ && wbr->wbr_wh[i]
34964+ && wbr->wbr_wh[i] != base[i].dentry);
34965+ }
34966+
34967+ if (wbr)
34968+ for (i = 0; i < AuBrWh_Last; i++) {
34969+ dput(wbr->wbr_wh[i]);
34970+ wbr->wbr_wh[i] = NULL;
34971+ }
34972+
34973+ err = 0;
1e00d052 34974+ if (!au_br_writable(br->br_perm)) {
5527c038 34975+ h_dir = d_inode(h_root);
1facf9fc 34976+ au_wh_init_ro(h_dir, base, &path);
1e00d052 34977+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 34978+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
34979+ if (err > 0)
34980+ goto out;
34981+ else if (err)
34982+ goto out_err;
1e00d052 34983+ } else {
1facf9fc 34984+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
34985+ if (err > 0)
34986+ goto out;
34987+ else if (err)
34988+ goto out_err;
1facf9fc 34989+ }
34990+ goto out; /* success */
34991+
4f0767ce 34992+out_err:
523b37e3
AM
34993+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
34994+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 34995+out:
1facf9fc 34996+ for (i = 0; i < AuBrWh_Last; i++)
34997+ dput(base[i].dentry);
34998+ return err;
34999+}
35000+
35001+/* ---------------------------------------------------------------------- */
35002+/*
35003+ * whiteouts are all hard-linked usually.
35004+ * when its link count reaches a ceiling, we create a new whiteout base
35005+ * asynchronously.
35006+ */
35007+
35008+struct reinit_br_wh {
35009+ struct super_block *sb;
35010+ struct au_branch *br;
35011+};
35012+
35013+static void reinit_br_wh(void *arg)
35014+{
35015+ int err;
35016+ aufs_bindex_t bindex;
35017+ struct path h_path;
35018+ struct reinit_br_wh *a = arg;
35019+ struct au_wbr *wbr;
523b37e3 35020+ struct inode *dir, *delegated;
1facf9fc 35021+ struct dentry *h_root;
35022+ struct au_hinode *hdir;
35023+
35024+ err = 0;
35025+ wbr = a->br->br_wbr;
35026+ /* big aufs lock */
35027+ si_noflush_write_lock(a->sb);
35028+ if (!au_br_writable(a->br->br_perm))
35029+ goto out;
35030+ bindex = au_br_index(a->sb, a->br->br_id);
35031+ if (unlikely(bindex < 0))
35032+ goto out;
35033+
1308ab2a 35034+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35035+ dir = d_inode(a->sb->s_root);
1facf9fc 35036+ hdir = au_hi(dir, bindex);
35037+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35038+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35039+
5afbbe0d 35040+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35041+ wbr_wh_write_lock(wbr);
35042+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35043+ h_root, a->br);
35044+ if (!err) {
86dc4139
AM
35045+ h_path.dentry = wbr->wbr_whbase;
35046+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35047+ delegated = NULL;
35048+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35049+ /*force*/0);
35050+ if (unlikely(err == -EWOULDBLOCK)) {
35051+ pr_warn("cannot retry for NFSv4 delegation"
35052+ " for an internal unlink\n");
35053+ iput(delegated);
35054+ }
1facf9fc 35055+ } else {
523b37e3 35056+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35057+ err = 0;
35058+ }
35059+ dput(wbr->wbr_whbase);
35060+ wbr->wbr_whbase = NULL;
35061+ if (!err)
86dc4139 35062+ err = au_wh_init(a->br, a->sb);
1facf9fc 35063+ wbr_wh_write_unlock(wbr);
5afbbe0d 35064+ au_hn_inode_unlock(hdir);
1308ab2a 35065+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35066+ if (!err)
35067+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35068+
4f0767ce 35069+out:
1facf9fc 35070+ if (wbr)
35071+ atomic_dec(&wbr->wbr_wh_running);
5afbbe0d 35072+ au_br_put(a->br);
1facf9fc 35073+ si_write_unlock(a->sb);
027c5e7a 35074+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
1c60b727 35075+ kfree(arg);
1facf9fc 35076+ if (unlikely(err))
35077+ AuIOErr("err %d\n", err);
35078+}
35079+
35080+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35081+{
35082+ int do_dec, wkq_err;
35083+ struct reinit_br_wh *arg;
35084+
35085+ do_dec = 1;
35086+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35087+ goto out;
35088+
35089+ /* ignore ENOMEM */
35090+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35091+ if (arg) {
35092+ /*
35093+ * dec(wh_running), kfree(arg) and dec(br_count)
35094+ * in reinit function
35095+ */
35096+ arg->sb = sb;
35097+ arg->br = br;
5afbbe0d 35098+ au_br_get(br);
53392da6 35099+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35100+ if (unlikely(wkq_err)) {
35101+ atomic_dec(&br->br_wbr->wbr_wh_running);
5afbbe0d 35102+ au_br_put(br);
1c60b727 35103+ kfree(arg);
1facf9fc 35104+ }
35105+ do_dec = 0;
35106+ }
35107+
4f0767ce 35108+out:
1facf9fc 35109+ if (do_dec)
35110+ atomic_dec(&br->br_wbr->wbr_wh_running);
35111+}
35112+
35113+/* ---------------------------------------------------------------------- */
35114+
35115+/*
35116+ * create the whiteout @wh.
35117+ */
35118+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35119+ struct dentry *wh)
35120+{
35121+ int err;
35122+ struct path h_path = {
35123+ .dentry = wh
35124+ };
35125+ struct au_branch *br;
35126+ struct au_wbr *wbr;
35127+ struct dentry *h_parent;
523b37e3 35128+ struct inode *h_dir, *delegated;
1facf9fc 35129+
35130+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35131+ h_dir = d_inode(h_parent);
1facf9fc 35132+ IMustLock(h_dir);
35133+
35134+ br = au_sbr(sb, bindex);
86dc4139 35135+ h_path.mnt = au_br_mnt(br);
1facf9fc 35136+ wbr = br->br_wbr;
35137+ wbr_wh_read_lock(wbr);
35138+ if (wbr->wbr_whbase) {
523b37e3
AM
35139+ delegated = NULL;
35140+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35141+ if (unlikely(err == -EWOULDBLOCK)) {
35142+ pr_warn("cannot retry for NFSv4 delegation"
35143+ " for an internal link\n");
35144+ iput(delegated);
35145+ }
1facf9fc 35146+ if (!err || err != -EMLINK)
35147+ goto out;
35148+
35149+ /* link count full. re-initialize br_whbase. */
35150+ kick_reinit_br_wh(sb, br);
35151+ }
35152+
35153+ /* return this error in this context */
b4510431 35154+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35155+ if (!err)
35156+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35157+
4f0767ce 35158+out:
1facf9fc 35159+ wbr_wh_read_unlock(wbr);
35160+ return err;
35161+}
35162+
35163+/* ---------------------------------------------------------------------- */
35164+
35165+/*
35166+ * create or remove the diropq.
35167+ */
35168+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35169+ unsigned int flags)
35170+{
35171+ struct dentry *opq_dentry, *h_dentry;
35172+ struct super_block *sb;
35173+ struct au_branch *br;
35174+ int err;
35175+
35176+ sb = dentry->d_sb;
35177+ br = au_sbr(sb, bindex);
35178+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35179+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35180+ if (IS_ERR(opq_dentry))
35181+ goto out;
35182+
35183+ if (au_ftest_diropq(flags, CREATE)) {
35184+ err = link_or_create_wh(sb, bindex, opq_dentry);
35185+ if (!err) {
35186+ au_set_dbdiropq(dentry, bindex);
35187+ goto out; /* success */
35188+ }
35189+ } else {
35190+ struct path tmp = {
35191+ .dentry = opq_dentry,
86dc4139 35192+ .mnt = au_br_mnt(br)
1facf9fc 35193+ };
5527c038 35194+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35195+ if (!err)
35196+ au_set_dbdiropq(dentry, -1);
35197+ }
35198+ dput(opq_dentry);
35199+ opq_dentry = ERR_PTR(err);
35200+
4f0767ce 35201+out:
1facf9fc 35202+ return opq_dentry;
35203+}
35204+
35205+struct do_diropq_args {
35206+ struct dentry **errp;
35207+ struct dentry *dentry;
35208+ aufs_bindex_t bindex;
35209+ unsigned int flags;
35210+};
35211+
35212+static void call_do_diropq(void *args)
35213+{
35214+ struct do_diropq_args *a = args;
35215+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35216+}
35217+
35218+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35219+ unsigned int flags)
35220+{
35221+ struct dentry *diropq, *h_dentry;
35222+
35223+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 35224+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 35225+ diropq = do_diropq(dentry, bindex, flags);
35226+ else {
35227+ int wkq_err;
35228+ struct do_diropq_args args = {
35229+ .errp = &diropq,
35230+ .dentry = dentry,
35231+ .bindex = bindex,
35232+ .flags = flags
35233+ };
35234+
35235+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35236+ if (unlikely(wkq_err))
35237+ diropq = ERR_PTR(wkq_err);
35238+ }
35239+
35240+ return diropq;
35241+}
35242+
35243+/* ---------------------------------------------------------------------- */
35244+
35245+/*
35246+ * lookup whiteout dentry.
35247+ * @h_parent: lower parent dentry which must exist and be locked
35248+ * @base_name: name of dentry which will be whiteouted
35249+ * returns dentry for whiteout.
35250+ */
35251+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35252+ struct au_branch *br)
35253+{
35254+ int err;
35255+ struct qstr wh_name;
35256+ struct dentry *wh_dentry;
35257+
35258+ err = au_wh_name_alloc(&wh_name, base_name);
35259+ wh_dentry = ERR_PTR(err);
35260+ if (!err) {
b4510431 35261+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
1c60b727 35262+ kfree(wh_name.name);
1facf9fc 35263+ }
35264+ return wh_dentry;
35265+}
35266+
35267+/*
35268+ * link/create a whiteout for @dentry on @bindex.
35269+ */
35270+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35271+ struct dentry *h_parent)
35272+{
35273+ struct dentry *wh_dentry;
35274+ struct super_block *sb;
35275+ int err;
35276+
35277+ sb = dentry->d_sb;
35278+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35279+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35280+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35281+ if (!err) {
1facf9fc 35282+ au_set_dbwh(dentry, bindex);
076b876e
AM
35283+ au_fhsm_wrote(sb, bindex, /*force*/0);
35284+ } else {
1facf9fc 35285+ dput(wh_dentry);
35286+ wh_dentry = ERR_PTR(err);
35287+ }
35288+ }
35289+
35290+ return wh_dentry;
35291+}
35292+
35293+/* ---------------------------------------------------------------------- */
35294+
35295+/* Delete all whiteouts in this directory on branch bindex. */
35296+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35297+ aufs_bindex_t bindex, struct au_branch *br)
35298+{
35299+ int err;
35300+ unsigned long ul, n;
35301+ struct qstr wh_name;
35302+ char *p;
35303+ struct hlist_head *head;
c06a8ce3 35304+ struct au_vdir_wh *pos;
1facf9fc 35305+ struct au_vdir_destr *str;
35306+
35307+ err = -ENOMEM;
537831f9 35308+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35309+ wh_name.name = p;
35310+ if (unlikely(!wh_name.name))
35311+ goto out;
35312+
35313+ err = 0;
35314+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35315+ p += AUFS_WH_PFX_LEN;
35316+ n = whlist->nh_num;
35317+ head = whlist->nh_head;
35318+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35319+ hlist_for_each_entry(pos, head, wh_hash) {
35320+ if (pos->wh_bindex != bindex)
1facf9fc 35321+ continue;
35322+
c06a8ce3 35323+ str = &pos->wh_str;
1facf9fc 35324+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35325+ memcpy(p, str->name, str->len);
35326+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35327+ err = unlink_wh_name(h_dentry, &wh_name, br);
35328+ if (!err)
35329+ continue;
35330+ break;
35331+ }
35332+ AuIOErr("whiteout name too long %.*s\n",
35333+ str->len, str->name);
35334+ err = -EIO;
35335+ break;
35336+ }
35337+ }
1c60b727 35338+ free_page((unsigned long)wh_name.name);
1facf9fc 35339+
4f0767ce 35340+out:
1facf9fc 35341+ return err;
35342+}
35343+
35344+struct del_wh_children_args {
35345+ int *errp;
35346+ struct dentry *h_dentry;
1308ab2a 35347+ struct au_nhash *whlist;
1facf9fc 35348+ aufs_bindex_t bindex;
35349+ struct au_branch *br;
35350+};
35351+
35352+static void call_del_wh_children(void *args)
35353+{
35354+ struct del_wh_children_args *a = args;
1308ab2a 35355+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35356+}
35357+
35358+/* ---------------------------------------------------------------------- */
35359+
35360+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35361+{
35362+ struct au_whtmp_rmdir *whtmp;
dece6358 35363+ int err;
1308ab2a 35364+ unsigned int rdhash;
dece6358
AM
35365+
35366+ SiMustAnyLock(sb);
1facf9fc 35367+
be52b249 35368+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35369+ if (unlikely(!whtmp)) {
35370+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35371+ goto out;
dece6358 35372+ }
1facf9fc 35373+
1308ab2a 35374+ /* no estimation for dir size */
35375+ rdhash = au_sbi(sb)->si_rdhash;
35376+ if (!rdhash)
35377+ rdhash = AUFS_RDHASH_DEF;
35378+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35379+ if (unlikely(err)) {
1c60b727 35380+ kfree(whtmp);
1308ab2a 35381+ whtmp = ERR_PTR(err);
35382+ }
dece6358 35383+
4f0767ce 35384+out:
dece6358 35385+ return whtmp;
1facf9fc 35386+}
35387+
35388+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35389+{
027c5e7a 35390+ if (whtmp->br)
5afbbe0d 35391+ au_br_put(whtmp->br);
1facf9fc 35392+ dput(whtmp->wh_dentry);
35393+ iput(whtmp->dir);
dece6358 35394+ au_nhash_wh_free(&whtmp->whlist);
1c60b727 35395+ kfree(whtmp);
1facf9fc 35396+}
35397+
35398+/*
35399+ * rmdir the whiteouted temporary named dir @h_dentry.
35400+ * @whlist: whiteouted children.
35401+ */
35402+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35403+ struct dentry *wh_dentry, struct au_nhash *whlist)
35404+{
35405+ int err;
2000de60 35406+ unsigned int h_nlink;
1facf9fc 35407+ struct path h_tmp;
35408+ struct inode *wh_inode, *h_dir;
35409+ struct au_branch *br;
35410+
5527c038 35411+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35412+ IMustLock(h_dir);
35413+
35414+ br = au_sbr(dir->i_sb, bindex);
5527c038 35415+ wh_inode = d_inode(wh_dentry);
febd17d6 35416+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35417+
35418+ /*
35419+ * someone else might change some whiteouts while we were sleeping.
35420+ * it means this whlist may have an obsoleted entry.
35421+ */
35422+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35423+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35424+ else {
35425+ int wkq_err;
35426+ struct del_wh_children_args args = {
35427+ .errp = &err,
35428+ .h_dentry = wh_dentry,
1308ab2a 35429+ .whlist = whlist,
1facf9fc 35430+ .bindex = bindex,
35431+ .br = br
35432+ };
35433+
35434+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35435+ if (unlikely(wkq_err))
35436+ err = wkq_err;
35437+ }
febd17d6 35438+ inode_unlock(wh_inode);
1facf9fc 35439+
35440+ if (!err) {
35441+ h_tmp.dentry = wh_dentry;
86dc4139 35442+ h_tmp.mnt = au_br_mnt(br);
2000de60 35443+ h_nlink = h_dir->i_nlink;
1facf9fc 35444+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35445+ /* some fs doesn't change the parent nlink in some cases */
35446+ h_nlink -= h_dir->i_nlink;
1facf9fc 35447+ }
35448+
35449+ if (!err) {
5afbbe0d 35450+ if (au_ibtop(dir) == bindex) {
7f207e10 35451+ /* todo: dir->i_mutex is necessary */
1facf9fc 35452+ au_cpup_attr_timesizes(dir);
2000de60
JR
35453+ if (h_nlink)
35454+ vfsub_drop_nlink(dir);
1facf9fc 35455+ }
35456+ return 0; /* success */
35457+ }
35458+
523b37e3 35459+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35460+ return err;
35461+}
35462+
35463+static void call_rmdir_whtmp(void *args)
35464+{
35465+ int err;
e49829fe 35466+ aufs_bindex_t bindex;
1facf9fc 35467+ struct au_whtmp_rmdir *a = args;
35468+ struct super_block *sb;
35469+ struct dentry *h_parent;
35470+ struct inode *h_dir;
1facf9fc 35471+ struct au_hinode *hdir;
35472+
35473+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35474+ /* inode_lock(a->dir); */
e49829fe 35475+ err = -EROFS;
1facf9fc 35476+ sb = a->dir->i_sb;
e49829fe
JR
35477+ si_read_lock(sb, !AuLock_FLUSH);
35478+ if (!au_br_writable(a->br->br_perm))
35479+ goto out;
35480+ bindex = au_br_index(sb, a->br->br_id);
35481+ if (unlikely(bindex < 0))
1facf9fc 35482+ goto out;
35483+
35484+ err = -EIO;
1facf9fc 35485+ ii_write_lock_parent(a->dir);
35486+ h_parent = dget_parent(a->wh_dentry);
5527c038 35487+ h_dir = d_inode(h_parent);
e49829fe 35488+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35489+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35490+ if (unlikely(err))
35491+ goto out_mnt;
5afbbe0d 35492+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35493+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35494+ a->br);
86dc4139
AM
35495+ if (!err)
35496+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35497+ au_hn_inode_unlock(hdir);
86dc4139
AM
35498+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35499+
35500+out_mnt:
1facf9fc 35501+ dput(h_parent);
35502+ ii_write_unlock(a->dir);
4f0767ce 35503+out:
febd17d6 35504+ /* inode_unlock(a->dir); */
1facf9fc 35505+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35506+ si_read_unlock(sb);
35507+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35508+ if (unlikely(err))
35509+ AuIOErr("err %d\n", err);
35510+}
35511+
35512+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35513+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35514+{
35515+ int wkq_err;
e49829fe 35516+ struct super_block *sb;
1facf9fc 35517+
35518+ IMustLock(dir);
35519+
35520+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35521+ sb = dir->i_sb;
1facf9fc 35522+ args->dir = au_igrab(dir);
e49829fe 35523+ args->br = au_sbr(sb, bindex);
5afbbe0d 35524+ au_br_get(args->br);
1facf9fc 35525+ args->wh_dentry = dget(wh_dentry);
53392da6 35526+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35527+ if (unlikely(wkq_err)) {
523b37e3 35528+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35529+ au_whtmp_rmdir_free(args);
35530+ }
35531+}
7f207e10
AM
35532diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35533--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 35534+++ linux/fs/aufs/whout.h 2018-04-15 08:49:13.404484168 +0200
3c1bdaff 35535@@ -0,0 +1,85 @@
1facf9fc 35536+/*
b00004a5 35537+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 35538+ *
35539+ * This program, aufs is free software; you can redistribute it and/or modify
35540+ * it under the terms of the GNU General Public License as published by
35541+ * the Free Software Foundation; either version 2 of the License, or
35542+ * (at your option) any later version.
dece6358
AM
35543+ *
35544+ * This program is distributed in the hope that it will be useful,
35545+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35546+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35547+ * GNU General Public License for more details.
35548+ *
35549+ * You should have received a copy of the GNU General Public License
523b37e3 35550+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35551+ */
35552+
35553+/*
35554+ * whiteout for logical deletion and opaque directory
35555+ */
35556+
35557+#ifndef __AUFS_WHOUT_H__
35558+#define __AUFS_WHOUT_H__
35559+
35560+#ifdef __KERNEL__
35561+
1facf9fc 35562+#include "dir.h"
35563+
35564+/* whout.c */
35565+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
35566+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35567+int au_diropq_test(struct dentry *h_dentry);
3c1bdaff 35568+struct au_branch;
1facf9fc 35569+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35570+ struct qstr *prefix);
35571+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35572+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35573+ struct dentry *dentry);
86dc4139 35574+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 35575+
35576+/* diropq flags */
35577+#define AuDiropq_CREATE 1
35578+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
35579+#define au_fset_diropq(flags, name) \
35580+ do { (flags) |= AuDiropq_##name; } while (0)
35581+#define au_fclr_diropq(flags, name) \
35582+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 35583+
35584+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35585+ unsigned int flags);
35586+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35587+ struct au_branch *br);
35588+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35589+ struct dentry *h_parent);
35590+
35591+/* real rmdir for the whiteout-ed dir */
35592+struct au_whtmp_rmdir {
35593+ struct inode *dir;
e49829fe 35594+ struct au_branch *br;
1facf9fc 35595+ struct dentry *wh_dentry;
dece6358 35596+ struct au_nhash whlist;
1facf9fc 35597+};
35598+
35599+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35600+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35601+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35602+ struct dentry *wh_dentry, struct au_nhash *whlist);
35603+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35604+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35605+
35606+/* ---------------------------------------------------------------------- */
35607+
35608+static inline struct dentry *au_diropq_create(struct dentry *dentry,
35609+ aufs_bindex_t bindex)
35610+{
35611+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35612+}
35613+
35614+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35615+{
35616+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35617+}
35618+
35619+#endif /* __KERNEL__ */
35620+#endif /* __AUFS_WHOUT_H__ */
7f207e10
AM
35621diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35622--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 35623+++ linux/fs/aufs/wkq.c 2018-06-04 09:08:09.188079511 +0200
8b6a4947 35624@@ -0,0 +1,390 @@
1facf9fc 35625+/*
b00004a5 35626+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 35627+ *
35628+ * This program, aufs is free software; you can redistribute it and/or modify
35629+ * it under the terms of the GNU General Public License as published by
35630+ * the Free Software Foundation; either version 2 of the License, or
35631+ * (at your option) any later version.
dece6358
AM
35632+ *
35633+ * This program is distributed in the hope that it will be useful,
35634+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35635+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35636+ * GNU General Public License for more details.
35637+ *
35638+ * You should have received a copy of the GNU General Public License
523b37e3 35639+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35640+ */
35641+
35642+/*
35643+ * workqueue for asynchronous/super-io operations
35644+ * todo: try new dredential scheme
35645+ */
35646+
dece6358 35647+#include <linux/module.h>
1facf9fc 35648+#include "aufs.h"
35649+
9dbd164d 35650+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 35651+
9dbd164d 35652+static struct workqueue_struct *au_wkq;
1facf9fc 35653+
35654+struct au_wkinfo {
35655+ struct work_struct wk;
7f207e10 35656+ struct kobject *kobj;
1facf9fc 35657+
35658+ unsigned int flags; /* see wkq.h */
35659+
35660+ au_wkq_func_t func;
35661+ void *args;
35662+
8b6a4947
AM
35663+#ifdef CONFIG_LOCKDEP
35664+ int dont_check;
35665+ struct held_lock **hlock;
35666+#endif
35667+
1facf9fc 35668+ struct completion *comp;
35669+};
35670+
35671+/* ---------------------------------------------------------------------- */
8b6a4947
AM
35672+/*
35673+ * Aufs passes some operations to the workqueue such as the internal copyup.
35674+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35675+ * job run by workqueue depends upon the locks acquired in the other task.
35676+ * Delegating a small operation to the workqueue, aufs passes its lockdep
35677+ * information too. And the job in the workqueue restores the info in order to
35678+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
35679+ * correctly and expectedly.
35680+ */
35681+
35682+#ifndef CONFIG_LOCKDEP
35683+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35684+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35685+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35686+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35687+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35688+#else
35689+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35690+{
35691+ wkinfo->hlock = NULL;
35692+ wkinfo->dont_check = 0;
35693+}
35694+
35695+/*
35696+ * 1: matched
35697+ * 0: unmatched
35698+ */
35699+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35700+{
35701+ static DEFINE_SPINLOCK(spin);
35702+ static struct {
35703+ char *name;
35704+ struct lock_class_key *key;
35705+ } a[] = {
35706+ { .name = "&sbinfo->si_rwsem" },
35707+ { .name = "&finfo->fi_rwsem" },
35708+ { .name = "&dinfo->di_rwsem" },
35709+ { .name = "&iinfo->ii_rwsem" }
35710+ };
35711+ static int set;
35712+ int i;
35713+
35714+ /* lockless read from 'set.' see below */
35715+ if (set == ARRAY_SIZE(a)) {
35716+ for (i = 0; i < ARRAY_SIZE(a); i++)
35717+ if (a[i].key == key)
35718+ goto match;
35719+ goto unmatch;
35720+ }
35721+
35722+ spin_lock(&spin);
35723+ if (set)
35724+ for (i = 0; i < ARRAY_SIZE(a); i++)
35725+ if (a[i].key == key) {
35726+ spin_unlock(&spin);
35727+ goto match;
35728+ }
35729+ for (i = 0; i < ARRAY_SIZE(a); i++) {
35730+ if (a[i].key) {
35731+ if (unlikely(a[i].key == key)) { /* rare but possible */
35732+ spin_unlock(&spin);
35733+ goto match;
35734+ } else
35735+ continue;
35736+ }
35737+ if (strstr(a[i].name, name)) {
35738+ /*
35739+ * the order of these three lines is important for the
35740+ * lockless read above.
35741+ */
35742+ a[i].key = key;
35743+ spin_unlock(&spin);
35744+ set++;
35745+ /* AuDbg("%d, %s\n", set, name); */
35746+ goto match;
35747+ }
35748+ }
35749+ spin_unlock(&spin);
35750+ goto unmatch;
35751+
35752+match:
35753+ return 1;
35754+unmatch:
35755+ return 0;
35756+}
35757+
35758+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
35759+{
35760+ int err, n;
35761+ struct task_struct *curr;
35762+ struct held_lock **hl, *held_locks, *p;
35763+
35764+ err = 0;
35765+ curr = current;
35766+ wkinfo->dont_check = lockdep_recursing(curr);
35767+ if (wkinfo->dont_check)
35768+ goto out;
35769+ n = curr->lockdep_depth;
35770+ if (!n)
35771+ goto out;
35772+
35773+ err = -ENOMEM;
35774+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
35775+ if (unlikely(!wkinfo->hlock))
35776+ goto out;
35777+
35778+ err = 0;
35779+#if 0
35780+ if (0 && au_debug_test()) /* left for debugging */
35781+ lockdep_print_held_locks(curr);
35782+#endif
35783+ held_locks = curr->held_locks;
35784+ hl = wkinfo->hlock;
35785+ while (n--) {
35786+ p = held_locks++;
35787+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
35788+ *hl++ = p;
35789+ }
35790+ *hl = NULL;
35791+
35792+out:
35793+ return err;
35794+}
35795+
35796+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
35797+{
35798+ kfree(wkinfo->hlock);
35799+}
35800+
35801+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
35802+{
35803+ struct held_lock *p, **hl = wkinfo->hlock;
35804+ int subclass;
35805+
35806+ if (wkinfo->dont_check)
35807+ lockdep_off();
35808+ if (!hl)
35809+ return;
35810+ while ((p = *hl++)) { /* assignment */
35811+ subclass = lockdep_hlock_class(p)->subclass;
35812+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
35813+ if (p->read)
35814+ rwsem_acquire_read(p->instance, subclass, 0,
35815+ /*p->acquire_ip*/_RET_IP_);
35816+ else
35817+ rwsem_acquire(p->instance, subclass, 0,
35818+ /*p->acquire_ip*/_RET_IP_);
35819+ }
35820+}
35821+
35822+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
35823+{
35824+ struct held_lock *p, **hl = wkinfo->hlock;
35825+
35826+ if (wkinfo->dont_check)
35827+ lockdep_on();
35828+ if (!hl)
35829+ return;
35830+ while ((p = *hl++)) /* assignment */
35831+ rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
35832+}
35833+#endif
1facf9fc 35834+
1facf9fc 35835+static void wkq_func(struct work_struct *wk)
35836+{
35837+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
35838+
2dfbb274 35839+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
35840+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
35841+
8b6a4947 35842+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 35843+ wkinfo->func(wkinfo->args);
8b6a4947 35844+ au_wkq_lockdep_post(wkinfo);
1facf9fc 35845+ if (au_ftest_wkq(wkinfo->flags, WAIT))
35846+ complete(wkinfo->comp);
35847+ else {
7f207e10 35848+ kobject_put(wkinfo->kobj);
9dbd164d 35849+ module_put(THIS_MODULE); /* todo: ?? */
1c60b727 35850+ kfree(wkinfo);
1facf9fc 35851+ }
35852+}
35853+
35854+/*
35855+ * Since struct completion is large, try allocating it dynamically.
35856+ */
c2b27bf2 35857+#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
1facf9fc 35858+#define AuWkqCompDeclare(name) struct completion *comp = NULL
35859+
35860+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
35861+{
35862+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
35863+ if (*comp) {
35864+ init_completion(*comp);
35865+ wkinfo->comp = *comp;
35866+ return 0;
35867+ }
35868+ return -ENOMEM;
35869+}
35870+
35871+static void au_wkq_comp_free(struct completion *comp)
35872+{
1c60b727 35873+ kfree(comp);
1facf9fc 35874+}
35875+
35876+#else
35877+
35878+/* no braces */
35879+#define AuWkqCompDeclare(name) \
35880+ DECLARE_COMPLETION_ONSTACK(_ ## name); \
35881+ struct completion *comp = &_ ## name
35882+
35883+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
35884+{
35885+ wkinfo->comp = *comp;
35886+ return 0;
35887+}
35888+
35889+static void au_wkq_comp_free(struct completion *comp __maybe_unused)
35890+{
35891+ /* empty */
35892+}
35893+#endif /* 4KSTACKS */
35894+
53392da6 35895+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 35896+{
53392da6
AM
35897+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
35898+ if (au_wkq_test()) {
38d290e6
JR
35899+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
35900+ " due to a dead dir by UDBA?\n");
53392da6
AM
35901+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
35902+ }
35903+ } else
35904+ au_dbg_verify_kthread();
35905+
35906+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 35907+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 35908+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
35909+ } else {
35910+ INIT_WORK(&wkinfo->wk, wkq_func);
35911+ schedule_work(&wkinfo->wk);
35912+ }
1facf9fc 35913+}
35914+
7f207e10
AM
35915+/*
35916+ * Be careful. It is easy to make deadlock happen.
35917+ * processA: lock, wkq and wait
35918+ * processB: wkq and wait, lock in wkq
35919+ * --> deadlock
35920+ */
b752ccd1 35921+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 35922+{
35923+ int err;
35924+ AuWkqCompDeclare(comp);
35925+ struct au_wkinfo wkinfo = {
b752ccd1 35926+ .flags = flags,
1facf9fc 35927+ .func = func,
35928+ .args = args
35929+ };
35930+
35931+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
35932+ if (unlikely(err))
35933+ goto out;
35934+ err = au_wkq_lockdep_alloc(&wkinfo);
35935+ if (unlikely(err))
35936+ goto out_comp;
1facf9fc 35937+ if (!err) {
53392da6 35938+ au_wkq_run(&wkinfo);
1facf9fc 35939+ /* no timeout, no interrupt */
35940+ wait_for_completion(wkinfo.comp);
1facf9fc 35941+ }
8b6a4947 35942+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 35943+
8b6a4947
AM
35944+out_comp:
35945+ au_wkq_comp_free(comp);
35946+out:
35947+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 35948+ return err;
1facf9fc 35949+}
35950+
027c5e7a
AM
35951+/*
35952+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
35953+ * problem in a concurrent umounting.
35954+ */
53392da6
AM
35955+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
35956+ unsigned int flags)
1facf9fc 35957+{
35958+ int err;
35959+ struct au_wkinfo *wkinfo;
35960+
f0c0a007 35961+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 35962+
35963+ /*
35964+ * wkq_func() must free this wkinfo.
35965+ * it highly depends upon the implementation of workqueue.
35966+ */
35967+ err = 0;
35968+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
35969+ if (wkinfo) {
7f207e10 35970+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 35971+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 35972+ wkinfo->func = func;
35973+ wkinfo->args = args;
35974+ wkinfo->comp = NULL;
8b6a4947 35975+ au_wkq_lockdep_init(wkinfo);
7f207e10 35976+ kobject_get(wkinfo->kobj);
9dbd164d 35977+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 35978+
53392da6 35979+ au_wkq_run(wkinfo);
1facf9fc 35980+ } else {
35981+ err = -ENOMEM;
e49829fe 35982+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35983+ }
35984+
35985+ return err;
35986+}
35987+
35988+/* ---------------------------------------------------------------------- */
35989+
35990+void au_nwt_init(struct au_nowait_tasks *nwt)
35991+{
f0c0a007
AM
35992+ atomic_set(&nwt->nw_len, 0);
35993+ /* smp_mb(); */ /* atomic_set */
1facf9fc 35994+ init_waitqueue_head(&nwt->nw_wq);
35995+}
35996+
35997+void au_wkq_fin(void)
35998+{
9dbd164d 35999+ destroy_workqueue(au_wkq);
1facf9fc 36000+}
36001+
36002+int __init au_wkq_init(void)
36003+{
9dbd164d 36004+ int err;
b752ccd1
AM
36005+
36006+ err = 0;
86dc4139 36007+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36008+ if (IS_ERR(au_wkq))
36009+ err = PTR_ERR(au_wkq);
36010+ else if (!au_wkq)
36011+ err = -ENOMEM;
b752ccd1
AM
36012+
36013+ return err;
1facf9fc 36014+}
7f207e10
AM
36015diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36016--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 36017+++ linux/fs/aufs/wkq.h 2018-04-15 08:49:13.404484168 +0200
f0c0a007 36018@@ -0,0 +1,93 @@
1facf9fc 36019+/*
b00004a5 36020+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36021+ *
36022+ * This program, aufs is free software; you can redistribute it and/or modify
36023+ * it under the terms of the GNU General Public License as published by
36024+ * the Free Software Foundation; either version 2 of the License, or
36025+ * (at your option) any later version.
dece6358
AM
36026+ *
36027+ * This program is distributed in the hope that it will be useful,
36028+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36029+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36030+ * GNU General Public License for more details.
36031+ *
36032+ * You should have received a copy of the GNU General Public License
523b37e3 36033+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36034+ */
36035+
36036+/*
36037+ * workqueue for asynchronous/super-io operations
36038+ * todo: try new credentials management scheme
36039+ */
36040+
36041+#ifndef __AUFS_WKQ_H__
36042+#define __AUFS_WKQ_H__
36043+
36044+#ifdef __KERNEL__
36045+
8b6a4947 36046+#include <linux/wait.h>
5afbbe0d 36047+
dece6358
AM
36048+struct super_block;
36049+
1facf9fc 36050+/* ---------------------------------------------------------------------- */
36051+
36052+/*
36053+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36054+ */
36055+struct au_nowait_tasks {
f0c0a007 36056+ atomic_t nw_len;
1facf9fc 36057+ wait_queue_head_t nw_wq;
36058+};
36059+
36060+/* ---------------------------------------------------------------------- */
36061+
36062+typedef void (*au_wkq_func_t)(void *args);
36063+
36064+/* wkq flags */
36065+#define AuWkq_WAIT 1
9dbd164d 36066+#define AuWkq_NEST (1 << 1)
1facf9fc 36067+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36068+#define au_fset_wkq(flags, name) \
36069+ do { (flags) |= AuWkq_##name; } while (0)
36070+#define au_fclr_wkq(flags, name) \
36071+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36072+
9dbd164d
AM
36073+#ifndef CONFIG_AUFS_HNOTIFY
36074+#undef AuWkq_NEST
36075+#define AuWkq_NEST 0
36076+#endif
36077+
1facf9fc 36078+/* wkq.c */
b752ccd1 36079+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36080+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36081+ unsigned int flags);
1facf9fc 36082+void au_nwt_init(struct au_nowait_tasks *nwt);
36083+int __init au_wkq_init(void);
36084+void au_wkq_fin(void);
36085+
36086+/* ---------------------------------------------------------------------- */
36087+
53392da6
AM
36088+static inline int au_wkq_test(void)
36089+{
36090+ return current->flags & PF_WQ_WORKER;
36091+}
36092+
b752ccd1 36093+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36094+{
b752ccd1 36095+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36096+}
36097+
36098+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36099+{
f0c0a007 36100+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36101+ wake_up_all(&nwt->nw_wq);
36102+}
36103+
36104+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36105+{
f0c0a007 36106+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36107+ return 0;
36108+}
36109+
36110+#endif /* __KERNEL__ */
36111+#endif /* __AUFS_WKQ_H__ */
c1595e42
JR
36112diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36113--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 36114+++ linux/fs/aufs/xattr.c 2018-06-04 09:08:09.188079511 +0200
8b6a4947 36115@@ -0,0 +1,355 @@
c1595e42 36116+/*
b00004a5 36117+ * Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
36118+ *
36119+ * This program, aufs is free software; you can redistribute it and/or modify
36120+ * it under the terms of the GNU General Public License as published by
36121+ * the Free Software Foundation; either version 2 of the License, or
36122+ * (at your option) any later version.
36123+ *
36124+ * This program is distributed in the hope that it will be useful,
36125+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36126+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36127+ * GNU General Public License for more details.
36128+ *
36129+ * You should have received a copy of the GNU General Public License
36130+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36131+ */
36132+
36133+/*
36134+ * handling xattr functions
36135+ */
36136+
a2654f78
AM
36137+#include <linux/fs.h>
36138+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36139+#include <linux/xattr.h>
36140+#include "aufs.h"
36141+
36142+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36143+{
36144+ if (!ignore_flags)
36145+ goto out;
36146+ switch (err) {
36147+ case -ENOMEM:
36148+ case -EDQUOT:
36149+ goto out;
36150+ }
36151+
36152+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36153+ err = 0;
36154+ goto out;
36155+ }
36156+
36157+#define cmp(brattr, prefix) do { \
36158+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36159+ XATTR_##prefix##_PREFIX_LEN)) { \
36160+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36161+ err = 0; \
36162+ goto out; \
36163+ } \
36164+ } while (0)
36165+
36166+ cmp(SEC, SECURITY);
36167+ cmp(SYS, SYSTEM);
36168+ cmp(TR, TRUSTED);
36169+ cmp(USR, USER);
36170+#undef cmp
36171+
36172+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36173+ err = 0;
36174+
36175+out:
36176+ return err;
36177+}
36178+
36179+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36180+
36181+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
36182+ char *name, char **buf, unsigned int ignore_flags,
36183+ unsigned int verbose)
c1595e42
JR
36184+{
36185+ int err;
36186+ ssize_t ssz;
36187+ struct inode *h_idst;
36188+
36189+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36190+ err = ssz;
36191+ if (unlikely(err <= 0)) {
c1595e42
JR
36192+ if (err == -ENODATA
36193+ || (err == -EOPNOTSUPP
b912730e 36194+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 36195+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
36196+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36197+ || !strcmp(name,
36198+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36199+ ))
c1595e42 36200+ err = 0;
b912730e
AM
36201+ if (err && (verbose || au_debug_test()))
36202+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36203+ goto out;
36204+ }
36205+
36206+ /* unlock it temporary */
5527c038 36207+ h_idst = d_inode(h_dst);
febd17d6 36208+ inode_unlock(h_idst);
c1595e42 36209+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 36210+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36211+ if (unlikely(err)) {
7e9cd9fe
AM
36212+ if (verbose || au_debug_test())
36213+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36214+ err = au_xattr_ignore(err, name, ignore_flags);
36215+ }
36216+
36217+out:
36218+ return err;
36219+}
36220+
7e9cd9fe
AM
36221+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36222+ unsigned int verbose)
c1595e42
JR
36223+{
36224+ int err, unlocked, acl_access, acl_default;
36225+ ssize_t ssz;
36226+ struct inode *h_isrc, *h_idst;
36227+ char *value, *p, *o, *e;
36228+
36229+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36230+ /* there should not be the parent-child relationship between them */
5527c038
JR
36231+ h_isrc = d_inode(h_src);
36232+ h_idst = d_inode(h_dst);
febd17d6 36233+ inode_unlock(h_idst);
be118d29 36234+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36235+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36236+ unlocked = 0;
36237+
36238+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36239+ ssz = vfs_listxattr(h_src, NULL, 0);
36240+ err = ssz;
36241+ if (unlikely(err < 0)) {
36242+ AuTraceErr(err);
36243+ if (err == -ENODATA
36244+ || err == -EOPNOTSUPP)
36245+ err = 0; /* ignore */
36246+ goto out;
36247+ }
36248+
36249+ err = 0;
36250+ p = NULL;
36251+ o = NULL;
36252+ if (ssz) {
36253+ err = -ENOMEM;
36254+ p = kmalloc(ssz, GFP_NOFS);
36255+ o = p;
36256+ if (unlikely(!p))
36257+ goto out;
36258+ err = vfs_listxattr(h_src, p, ssz);
36259+ }
3c1bdaff 36260+ inode_unlock_shared(h_isrc);
c1595e42
JR
36261+ unlocked = 1;
36262+ AuDbg("err %d, ssz %zd\n", err, ssz);
36263+ if (unlikely(err < 0))
36264+ goto out_free;
36265+
36266+ err = 0;
36267+ e = p + ssz;
36268+ value = NULL;
36269+ acl_access = 0;
36270+ acl_default = 0;
36271+ while (!err && p < e) {
36272+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36273+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36274+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36275+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36276+ - 1);
7e9cd9fe
AM
36277+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36278+ verbose);
c1595e42
JR
36279+ p += strlen(p) + 1;
36280+ }
36281+ AuTraceErr(err);
36282+ ignore_flags |= au_xattr_out_of_list;
36283+ if (!err && !acl_access) {
36284+ err = au_do_cpup_xattr(h_dst, h_src,
36285+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36286+ ignore_flags, verbose);
c1595e42
JR
36287+ AuTraceErr(err);
36288+ }
36289+ if (!err && !acl_default) {
36290+ err = au_do_cpup_xattr(h_dst, h_src,
36291+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36292+ ignore_flags, verbose);
c1595e42
JR
36293+ AuTraceErr(err);
36294+ }
36295+
8b6a4947 36296+ kfree(value);
c1595e42
JR
36297+
36298+out_free:
8b6a4947 36299+ kfree(o);
c1595e42
JR
36300+out:
36301+ if (!unlocked)
3c1bdaff 36302+ inode_unlock_shared(h_isrc);
c1595e42
JR
36303+ AuTraceErr(err);
36304+ return err;
36305+}
36306+
36307+/* ---------------------------------------------------------------------- */
36308+
a2654f78
AM
36309+static int au_smack_reentering(struct super_block *sb)
36310+{
36311+#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36312+ /*
36313+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36314+ * i_op->getxattr(). ouch.
36315+ */
36316+ return si_pid_test(sb);
36317+#else
36318+ return 0;
36319+#endif
36320+}
36321+
c1595e42
JR
36322+enum {
36323+ AU_XATTR_LIST,
36324+ AU_XATTR_GET
36325+};
36326+
36327+struct au_lgxattr {
36328+ int type;
36329+ union {
36330+ struct {
36331+ char *list;
36332+ size_t size;
36333+ } list;
36334+ struct {
36335+ const char *name;
36336+ void *value;
36337+ size_t size;
36338+ } get;
36339+ } u;
36340+};
36341+
36342+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36343+{
36344+ ssize_t err;
a2654f78 36345+ int reenter;
c1595e42
JR
36346+ struct path h_path;
36347+ struct super_block *sb;
36348+
36349+ sb = dentry->d_sb;
a2654f78
AM
36350+ reenter = au_smack_reentering(sb);
36351+ if (!reenter) {
36352+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36353+ if (unlikely(err))
36354+ goto out;
36355+ }
36356+ err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
c1595e42
JR
36357+ if (unlikely(err))
36358+ goto out_si;
36359+ if (unlikely(!h_path.dentry))
36360+ /* illegally overlapped or something */
36361+ goto out_di; /* pretending success */
36362+
36363+ /* always topmost entry only */
36364+ switch (arg->type) {
36365+ case AU_XATTR_LIST:
36366+ err = vfs_listxattr(h_path.dentry,
36367+ arg->u.list.list, arg->u.list.size);
36368+ break;
36369+ case AU_XATTR_GET:
5afbbe0d 36370+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
36371+ err = vfs_getxattr(h_path.dentry,
36372+ arg->u.get.name, arg->u.get.value,
36373+ arg->u.get.size);
36374+ break;
36375+ }
36376+
36377+out_di:
a2654f78
AM
36378+ if (!reenter)
36379+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36380+out_si:
a2654f78
AM
36381+ if (!reenter)
36382+ si_read_unlock(sb);
c1595e42
JR
36383+out:
36384+ AuTraceErr(err);
36385+ return err;
36386+}
36387+
36388+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36389+{
36390+ struct au_lgxattr arg = {
36391+ .type = AU_XATTR_LIST,
36392+ .u.list = {
36393+ .list = list,
36394+ .size = size
36395+ },
36396+ };
36397+
36398+ return au_lgxattr(dentry, &arg);
36399+}
36400+
f2c43d5f
AM
36401+static ssize_t au_getxattr(struct dentry *dentry,
36402+ struct inode *inode __maybe_unused,
36403+ const char *name, void *value, size_t size)
c1595e42
JR
36404+{
36405+ struct au_lgxattr arg = {
36406+ .type = AU_XATTR_GET,
36407+ .u.get = {
36408+ .name = name,
36409+ .value = value,
36410+ .size = size
36411+ },
36412+ };
36413+
36414+ return au_lgxattr(dentry, &arg);
36415+}
36416+
f2c43d5f
AM
36417+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36418+ const char *name, const void *value, size_t size,
36419+ int flags)
c1595e42 36420+{
f2c43d5f 36421+ struct au_sxattr arg = {
c1595e42
JR
36422+ .type = AU_XATTR_SET,
36423+ .u.set = {
36424+ .name = name,
36425+ .value = value,
36426+ .size = size,
36427+ .flags = flags
36428+ },
36429+ };
36430+
f2c43d5f 36431+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36432+}
36433+
36434+/* ---------------------------------------------------------------------- */
36435+
f2c43d5f
AM
36436+static int au_xattr_get(const struct xattr_handler *handler,
36437+ struct dentry *dentry, struct inode *inode,
36438+ const char *name, void *buffer, size_t size)
c1595e42 36439+{
f2c43d5f 36440+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36441+}
36442+
f2c43d5f
AM
36443+static int au_xattr_set(const struct xattr_handler *handler,
36444+ struct dentry *dentry, struct inode *inode,
36445+ const char *name, const void *value, size_t size,
36446+ int flags)
c1595e42 36447+{
f2c43d5f 36448+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36449+}
36450+
36451+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36452+ .name = "",
36453+ .prefix = "",
c1595e42
JR
36454+ .get = au_xattr_get,
36455+ .set = au_xattr_set
c1595e42
JR
36456+};
36457+
36458+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36459+#ifdef CONFIG_FS_POSIX_ACL
36460+ &posix_acl_access_xattr_handler,
36461+ &posix_acl_default_xattr_handler,
36462+#endif
36463+ &au_xattr_handler, /* must be last */
f2c43d5f 36464+ NULL
c1595e42
JR
36465+};
36466+
36467+void au_xattr_init(struct super_block *sb)
36468+{
f2c43d5f 36469+ sb->s_xattr = au_xattr_handlers;
c1595e42 36470+}
7f207e10
AM
36471diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36472--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
b00004a5 36473+++ linux/fs/aufs/xino.c 2018-06-04 09:08:09.188079511 +0200
be118d29 36474@@ -0,0 +1,1469 @@
1facf9fc 36475+/*
b00004a5 36476+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36477+ *
36478+ * This program, aufs is free software; you can redistribute it and/or modify
36479+ * it under the terms of the GNU General Public License as published by
36480+ * the Free Software Foundation; either version 2 of the License, or
36481+ * (at your option) any later version.
dece6358
AM
36482+ *
36483+ * This program is distributed in the hope that it will be useful,
36484+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36485+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36486+ * GNU General Public License for more details.
36487+ *
36488+ * You should have received a copy of the GNU General Public License
523b37e3 36489+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36490+ */
36491+
36492+/*
36493+ * external inode number translation table and bitmap
36494+ */
36495+
36496+#include <linux/seq_file.h>
392086de 36497+#include <linux/statfs.h>
1facf9fc 36498+#include "aufs.h"
36499+
be118d29
JR
36500+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
36501+ size_t size, loff_t *pos);
36502+
9dbd164d 36503+/* todo: unnecessary to support mmap_sem since kernel-space? */
5527c038 36504+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
1facf9fc 36505+ loff_t *pos)
36506+{
36507+ ssize_t err;
36508+ mm_segment_t oldfs;
b752ccd1
AM
36509+ union {
36510+ void *k;
36511+ char __user *u;
36512+ } buf;
be118d29
JR
36513+ int i;
36514+ const int prevent_endless = 10;
1facf9fc 36515+
be118d29 36516+ i = 0;
b752ccd1 36517+ buf.k = kbuf;
1facf9fc 36518+ oldfs = get_fs();
36519+ set_fs(KERNEL_DS);
36520+ do {
b752ccd1 36521+ err = func(file, buf.u, size, pos);
be118d29
JR
36522+ if (err == -EINTR
36523+ && !au_wkq_test()
36524+ && fatal_signal_pending(current)) {
36525+ set_fs(oldfs);
36526+ err = xino_fread_wkq(func, file, kbuf, size, pos);
36527+ BUG_ON(err == -EINTR);
36528+ oldfs = get_fs();
36529+ set_fs(KERNEL_DS);
36530+ }
36531+ } while (i++ < prevent_endless
36532+ && (err == -EAGAIN || err == -EINTR));
1facf9fc 36533+ set_fs(oldfs);
36534+
36535+#if 0 /* reserved for future use */
36536+ if (err > 0)
2000de60 36537+ fsnotify_access(file->f_path.dentry);
1facf9fc 36538+#endif
36539+
36540+ return err;
36541+}
36542+
be118d29
JR
36543+struct xino_fread_args {
36544+ ssize_t *errp;
36545+ vfs_readf_t func;
36546+ struct file *file;
36547+ void *buf;
36548+ size_t size;
36549+ loff_t *pos;
36550+};
36551+
36552+static void call_xino_fread(void *args)
36553+{
36554+ struct xino_fread_args *a = args;
36555+ *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
36556+}
36557+
36558+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
36559+ size_t size, loff_t *pos)
36560+{
36561+ ssize_t err;
36562+ int wkq_err;
36563+ struct xino_fread_args args = {
36564+ .errp = &err,
36565+ .func = func,
36566+ .file = file,
36567+ .buf = buf,
36568+ .size = size,
36569+ .pos = pos
36570+ };
36571+
36572+ wkq_err = au_wkq_wait(call_xino_fread, &args);
36573+ if (unlikely(wkq_err))
36574+ err = wkq_err;
36575+
36576+ return err;
36577+}
36578+
1facf9fc 36579+/* ---------------------------------------------------------------------- */
36580+
be52b249
AM
36581+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
36582+ size_t size, loff_t *pos);
36583+
5527c038 36584+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
1facf9fc 36585+ size_t size, loff_t *pos)
36586+{
36587+ ssize_t err;
36588+ mm_segment_t oldfs;
b752ccd1
AM
36589+ union {
36590+ void *k;
36591+ const char __user *u;
36592+ } buf;
be52b249
AM
36593+ int i;
36594+ const int prevent_endless = 10;
1facf9fc 36595+
be52b249 36596+ i = 0;
b752ccd1 36597+ buf.k = kbuf;
1facf9fc 36598+ oldfs = get_fs();
36599+ set_fs(KERNEL_DS);
1facf9fc 36600+ do {
b752ccd1 36601+ err = func(file, buf.u, size, pos);
be52b249
AM
36602+ if (err == -EINTR
36603+ && !au_wkq_test()
36604+ && fatal_signal_pending(current)) {
36605+ set_fs(oldfs);
36606+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
36607+ BUG_ON(err == -EINTR);
36608+ oldfs = get_fs();
36609+ set_fs(KERNEL_DS);
36610+ }
36611+ } while (i++ < prevent_endless
36612+ && (err == -EAGAIN || err == -EINTR));
1facf9fc 36613+ set_fs(oldfs);
36614+
36615+#if 0 /* reserved for future use */
36616+ if (err > 0)
2000de60 36617+ fsnotify_modify(file->f_path.dentry);
1facf9fc 36618+#endif
36619+
36620+ return err;
36621+}
36622+
36623+struct do_xino_fwrite_args {
36624+ ssize_t *errp;
5527c038 36625+ vfs_writef_t func;
1facf9fc 36626+ struct file *file;
36627+ void *buf;
36628+ size_t size;
36629+ loff_t *pos;
36630+};
36631+
36632+static void call_do_xino_fwrite(void *args)
36633+{
36634+ struct do_xino_fwrite_args *a = args;
36635+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
36636+}
36637+
be52b249
AM
36638+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
36639+ size_t size, loff_t *pos)
36640+{
36641+ ssize_t err;
36642+ int wkq_err;
36643+ struct do_xino_fwrite_args args = {
36644+ .errp = &err,
36645+ .func = func,
36646+ .file = file,
36647+ .buf = buf,
36648+ .size = size,
36649+ .pos = pos
36650+ };
36651+
36652+ /*
36653+ * it breaks RLIMIT_FSIZE and normal user's limit,
36654+ * users should care about quota and real 'filesystem full.'
36655+ */
36656+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
36657+ if (unlikely(wkq_err))
36658+ err = wkq_err;
36659+
36660+ return err;
36661+}
36662+
5527c038
JR
36663+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
36664+ size_t size, loff_t *pos)
1facf9fc 36665+{
36666+ ssize_t err;
36667+
b752ccd1
AM
36668+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
36669+ lockdep_off();
36670+ err = do_xino_fwrite(func, file, buf, size, pos);
36671+ lockdep_on();
8b6a4947
AM
36672+ } else {
36673+ lockdep_off();
be52b249 36674+ err = xino_fwrite_wkq(func, file, buf, size, pos);
8b6a4947
AM
36675+ lockdep_on();
36676+ }
1facf9fc 36677+
36678+ return err;
36679+}
36680+
36681+/* ---------------------------------------------------------------------- */
36682+
36683+/*
36684+ * create a new xinofile at the same place/path as @base_file.
36685+ */
36686+struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
36687+{
36688+ struct file *file;
4a4d8108 36689+ struct dentry *base, *parent;
523b37e3 36690+ struct inode *dir, *delegated;
1facf9fc 36691+ struct qstr *name;
1308ab2a 36692+ struct path path;
4a4d8108 36693+ int err;
1facf9fc 36694+
2000de60 36695+ base = base_file->f_path.dentry;
1facf9fc 36696+ parent = base->d_parent; /* dir inode is locked */
5527c038 36697+ dir = d_inode(parent);
1facf9fc 36698+ IMustLock(dir);
36699+
36700+ file = ERR_PTR(-EINVAL);
36701+ name = &base->d_name;
4a4d8108
AM
36702+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
36703+ if (IS_ERR(path.dentry)) {
36704+ file = (void *)path.dentry;
523b37e3
AM
36705+ pr_err("%pd lookup err %ld\n",
36706+ base, PTR_ERR(path.dentry));
1facf9fc 36707+ goto out;
36708+ }
36709+
36710+ /* no need to mnt_want_write() since we call dentry_open() later */
4a4d8108 36711+ err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
1facf9fc 36712+ if (unlikely(err)) {
36713+ file = ERR_PTR(err);
523b37e3 36714+ pr_err("%pd create err %d\n", base, err);
1facf9fc 36715+ goto out_dput;
36716+ }
36717+
c06a8ce3 36718+ path.mnt = base_file->f_path.mnt;
4a4d8108 36719+ file = vfsub_dentry_open(&path,
7f207e10 36720+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
2cbb1c4b 36721+ /* | __FMODE_NONOTIFY */);
1facf9fc 36722+ if (IS_ERR(file)) {
523b37e3 36723+ pr_err("%pd open err %ld\n", base, PTR_ERR(file));
1facf9fc 36724+ goto out_dput;
36725+ }
36726+
523b37e3
AM
36727+ delegated = NULL;
36728+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
36729+ if (unlikely(err == -EWOULDBLOCK)) {
36730+ pr_warn("cannot retry for NFSv4 delegation"
36731+ " for an internal unlink\n");
36732+ iput(delegated);
36733+ }
1facf9fc 36734+ if (unlikely(err)) {
523b37e3 36735+ pr_err("%pd unlink err %d\n", base, err);
1facf9fc 36736+ goto out_fput;
36737+ }
36738+
36739+ if (copy_src) {
36740+ /* no one can touch copy_src xino */
c06a8ce3 36741+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
1facf9fc 36742+ if (unlikely(err)) {
523b37e3 36743+ pr_err("%pd copy err %d\n", base, err);
1facf9fc 36744+ goto out_fput;
36745+ }
36746+ }
36747+ goto out_dput; /* success */
36748+
4f0767ce 36749+out_fput:
1facf9fc 36750+ fput(file);
36751+ file = ERR_PTR(err);
4f0767ce 36752+out_dput:
4a4d8108 36753+ dput(path.dentry);
4f0767ce 36754+out:
1facf9fc 36755+ return file;
36756+}
36757+
36758+struct au_xino_lock_dir {
36759+ struct au_hinode *hdir;
36760+ struct dentry *parent;
febd17d6 36761+ struct inode *dir;
1facf9fc 36762+};
36763+
36764+static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
36765+ struct au_xino_lock_dir *ldir)
36766+{
36767+ aufs_bindex_t brid, bindex;
36768+
36769+ ldir->hdir = NULL;
36770+ bindex = -1;
36771+ brid = au_xino_brid(sb);
36772+ if (brid >= 0)
36773+ bindex = au_br_index(sb, brid);
36774+ if (bindex >= 0) {
5527c038 36775+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 36776+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 36777+ } else {
2000de60 36778+ ldir->parent = dget_parent(xino->f_path.dentry);
febd17d6
JR
36779+ ldir->dir = d_inode(ldir->parent);
36780+ inode_lock_nested(ldir->dir, AuLsc_I_PARENT);
1facf9fc 36781+ }
36782+}
36783+
36784+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36785+{
36786+ if (ldir->hdir)
5afbbe0d 36787+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 36788+ else {
febd17d6 36789+ inode_unlock(ldir->dir);
1facf9fc 36790+ dput(ldir->parent);
36791+ }
36792+}
36793+
36794+/* ---------------------------------------------------------------------- */
36795+
36796+/* trucate xino files asynchronously */
36797+
36798+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
36799+{
36800+ int err;
392086de
AM
36801+ unsigned long jiffy;
36802+ blkcnt_t blocks;
5afbbe0d 36803+ aufs_bindex_t bi, bbot;
392086de 36804+ struct kstatfs *st;
1facf9fc 36805+ struct au_branch *br;
36806+ struct file *new_xino, *file;
36807+ struct super_block *h_sb;
36808+ struct au_xino_lock_dir ldir;
36809+
392086de 36810+ err = -ENOMEM;
be52b249 36811+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
36812+ if (unlikely(!st))
36813+ goto out;
36814+
1facf9fc 36815+ err = -EINVAL;
5afbbe0d
AM
36816+ bbot = au_sbbot(sb);
36817+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 36818+ goto out_st;
1facf9fc 36819+ br = au_sbr(sb, bindex);
36820+ file = br->br_xino.xi_file;
36821+ if (!file)
392086de
AM
36822+ goto out_st;
36823+
36824+ err = vfs_statfs(&file->f_path, st);
36825+ if (unlikely(err))
36826+ AuErr1("statfs err %d, ignored\n", err);
36827+ jiffy = jiffies;
36828+ blocks = file_inode(file)->i_blocks;
36829+ pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36830+ bindex, (u64)blocks, st->f_bfree, st->f_blocks);
1facf9fc 36831+
36832+ au_xino_lock_dir(sb, file, &ldir);
36833+ /* mnt_want_write() is unnecessary here */
36834+ new_xino = au_xino_create2(file, file);
36835+ au_xino_unlock_dir(&ldir);
36836+ err = PTR_ERR(new_xino);
392086de
AM
36837+ if (IS_ERR(new_xino)) {
36838+ pr_err("err %d, ignored\n", err);
36839+ goto out_st;
36840+ }
1facf9fc 36841+ err = 0;
36842+ fput(file);
36843+ br->br_xino.xi_file = new_xino;
36844+
86dc4139 36845+ h_sb = au_br_sb(br);
5afbbe0d 36846+ for (bi = 0; bi <= bbot; bi++) {
1facf9fc 36847+ if (unlikely(bi == bindex))
36848+ continue;
36849+ br = au_sbr(sb, bi);
86dc4139 36850+ if (au_br_sb(br) != h_sb)
1facf9fc 36851+ continue;
36852+
36853+ fput(br->br_xino.xi_file);
36854+ br->br_xino.xi_file = new_xino;
36855+ get_file(new_xino);
36856+ }
36857+
392086de
AM
36858+ err = vfs_statfs(&new_xino->f_path, st);
36859+ if (!err) {
36860+ pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36861+ bindex, (u64)file_inode(new_xino)->i_blocks,
36862+ st->f_bfree, st->f_blocks);
36863+ if (file_inode(new_xino)->i_blocks < blocks)
36864+ au_sbi(sb)->si_xino_jiffy = jiffy;
36865+ } else
36866+ AuErr1("statfs err %d, ignored\n", err);
36867+
36868+out_st:
1c60b727 36869+ kfree(st);
4f0767ce 36870+out:
1facf9fc 36871+ return err;
36872+}
36873+
36874+struct xino_do_trunc_args {
36875+ struct super_block *sb;
36876+ struct au_branch *br;
36877+};
36878+
36879+static void xino_do_trunc(void *_args)
36880+{
36881+ struct xino_do_trunc_args *args = _args;
36882+ struct super_block *sb;
36883+ struct au_branch *br;
36884+ struct inode *dir;
36885+ int err;
36886+ aufs_bindex_t bindex;
36887+
36888+ err = 0;
36889+ sb = args->sb;
5527c038 36890+ dir = d_inode(sb->s_root);
1facf9fc 36891+ br = args->br;
36892+
36893+ si_noflush_write_lock(sb);
36894+ ii_read_lock_parent(dir);
36895+ bindex = au_br_index(sb, br->br_id);
36896+ err = au_xino_trunc(sb, bindex);
1facf9fc 36897+ ii_read_unlock(dir);
36898+ if (unlikely(err))
392086de 36899+ pr_warn("err b%d, (%d)\n", bindex, err);
1facf9fc 36900+ atomic_dec(&br->br_xino_running);
5afbbe0d 36901+ au_br_put(br);
1facf9fc 36902+ si_write_unlock(sb);
027c5e7a 36903+ au_nwt_done(&au_sbi(sb)->si_nowait);
1c60b727 36904+ kfree(args);
1facf9fc 36905+}
36906+
392086de
AM
36907+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
36908+{
36909+ int err;
36910+ struct kstatfs st;
36911+ struct au_sbinfo *sbinfo;
36912+
36913+ /* todo: si_xino_expire and the ratio should be customizable */
36914+ sbinfo = au_sbi(sb);
36915+ if (time_before(jiffies,
36916+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
36917+ return 0;
36918+
36919+ /* truncation border */
36920+ err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
36921+ if (unlikely(err)) {
36922+ AuErr1("statfs err %d, ignored\n", err);
36923+ return 0;
36924+ }
36925+ if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
36926+ return 0;
36927+
36928+ return 1;
36929+}
36930+
1facf9fc 36931+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
36932+{
36933+ struct xino_do_trunc_args *args;
36934+ int wkq_err;
36935+
392086de 36936+ if (!xino_trunc_test(sb, br))
1facf9fc 36937+ return;
36938+
36939+ if (atomic_inc_return(&br->br_xino_running) > 1)
36940+ goto out;
36941+
36942+ /* lock and kfree() will be called in trunc_xino() */
36943+ args = kmalloc(sizeof(*args), GFP_NOFS);
36944+ if (unlikely(!args)) {
36945+ AuErr1("no memory\n");
f0c0a007 36946+ goto out;
1facf9fc 36947+ }
36948+
5afbbe0d 36949+ au_br_get(br);
1facf9fc 36950+ args->sb = sb;
36951+ args->br = br;
53392da6 36952+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 36953+ if (!wkq_err)
36954+ return; /* success */
36955+
4a4d8108 36956+ pr_err("wkq %d\n", wkq_err);
5afbbe0d 36957+ au_br_put(br);
1c60b727 36958+ kfree(args);
1facf9fc 36959+
4f0767ce 36960+out:
e49829fe 36961+ atomic_dec(&br->br_xino_running);
1facf9fc 36962+}
36963+
36964+/* ---------------------------------------------------------------------- */
36965+
5527c038 36966+static int au_xino_do_write(vfs_writef_t write, struct file *file,
1facf9fc 36967+ ino_t h_ino, ino_t ino)
36968+{
36969+ loff_t pos;
36970+ ssize_t sz;
36971+
36972+ pos = h_ino;
36973+ if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
36974+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
36975+ return -EFBIG;
36976+ }
36977+ pos *= sizeof(ino);
36978+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
36979+ if (sz == sizeof(ino))
36980+ return 0; /* success */
36981+
36982+ AuIOErr("write failed (%zd)\n", sz);
36983+ return -EIO;
36984+}
36985+
36986+/*
36987+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
36988+ * at the position of @h_ino.
36989+ * even if @ino is zero, it is written to the xinofile and means no entry.
36990+ * if the size of the xino file on a specific filesystem exceeds the watermark,
36991+ * try truncating it.
36992+ */
36993+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
36994+ ino_t ino)
36995+{
36996+ int err;
36997+ unsigned int mnt_flags;
36998+ struct au_branch *br;
36999+
37000+ BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
37001+ || ((loff_t)-1) > 0);
dece6358 37002+ SiMustAnyLock(sb);
1facf9fc 37003+
37004+ mnt_flags = au_mntflags(sb);
37005+ if (!au_opt_test(mnt_flags, XINO))
37006+ return 0;
37007+
37008+ br = au_sbr(sb, bindex);
37009+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
37010+ h_ino, ino);
37011+ if (!err) {
37012+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37013+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37014+ xino_try_trunc(sb, br);
37015+ return 0; /* success */
37016+ }
37017+
37018+ AuIOErr("write failed (%d)\n", err);
37019+ return -EIO;
37020+}
37021+
37022+/* ---------------------------------------------------------------------- */
37023+
37024+/* aufs inode number bitmap */
37025+
37026+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37027+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37028+{
37029+ ino_t ino;
37030+
37031+ AuDebugOn(bit < 0 || page_bits <= bit);
37032+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37033+ return ino;
37034+}
37035+
37036+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37037+{
37038+ AuDebugOn(ino < AUFS_FIRST_INO);
37039+ ino -= AUFS_FIRST_INO;
37040+ *pindex = ino / page_bits;
37041+ *bit = ino % page_bits;
37042+}
37043+
37044+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37045+{
37046+ int err;
37047+ loff_t pos;
37048+ ssize_t sz;
37049+ struct au_sbinfo *sbinfo;
37050+ struct file *xib;
37051+ unsigned long *p;
37052+
37053+ sbinfo = au_sbi(sb);
37054+ MtxMustLock(&sbinfo->si_xib_mtx);
37055+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37056+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37057+
37058+ if (pindex == sbinfo->si_xib_last_pindex)
37059+ return 0;
37060+
37061+ xib = sbinfo->si_xib;
37062+ p = sbinfo->si_xib_buf;
37063+ pos = sbinfo->si_xib_last_pindex;
37064+ pos *= PAGE_SIZE;
37065+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37066+ if (unlikely(sz != PAGE_SIZE))
37067+ goto out;
37068+
37069+ pos = pindex;
37070+ pos *= PAGE_SIZE;
c06a8ce3 37071+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 37072+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37073+ else {
37074+ memset(p, 0, PAGE_SIZE);
37075+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37076+ }
37077+ if (sz == PAGE_SIZE) {
37078+ sbinfo->si_xib_last_pindex = pindex;
37079+ return 0; /* success */
37080+ }
37081+
4f0767ce 37082+out:
b752ccd1
AM
37083+ AuIOErr1("write failed (%zd)\n", sz);
37084+ err = sz;
37085+ if (sz >= 0)
37086+ err = -EIO;
37087+ return err;
37088+}
37089+
37090+/* ---------------------------------------------------------------------- */
37091+
37092+static void au_xib_clear_bit(struct inode *inode)
37093+{
37094+ int err, bit;
37095+ unsigned long pindex;
37096+ struct super_block *sb;
37097+ struct au_sbinfo *sbinfo;
37098+
37099+ AuDebugOn(inode->i_nlink);
37100+
37101+ sb = inode->i_sb;
37102+ xib_calc_bit(inode->i_ino, &pindex, &bit);
37103+ AuDebugOn(page_bits <= bit);
37104+ sbinfo = au_sbi(sb);
37105+ mutex_lock(&sbinfo->si_xib_mtx);
37106+ err = xib_pindex(sb, pindex);
37107+ if (!err) {
37108+ clear_bit(bit, sbinfo->si_xib_buf);
37109+ sbinfo->si_xib_next_bit = bit;
37110+ }
37111+ mutex_unlock(&sbinfo->si_xib_mtx);
37112+}
37113+
37114+/* for s_op->delete_inode() */
37115+void au_xino_delete_inode(struct inode *inode, const int unlinked)
37116+{
37117+ int err;
37118+ unsigned int mnt_flags;
5afbbe0d 37119+ aufs_bindex_t bindex, bbot, bi;
b752ccd1
AM
37120+ unsigned char try_trunc;
37121+ struct au_iinfo *iinfo;
37122+ struct super_block *sb;
37123+ struct au_hinode *hi;
37124+ struct inode *h_inode;
37125+ struct au_branch *br;
5527c038 37126+ vfs_writef_t xwrite;
b752ccd1 37127+
5afbbe0d
AM
37128+ AuDebugOn(au_is_bad_inode(inode));
37129+
b752ccd1
AM
37130+ sb = inode->i_sb;
37131+ mnt_flags = au_mntflags(sb);
37132+ if (!au_opt_test(mnt_flags, XINO)
37133+ || inode->i_ino == AUFS_ROOT_INO)
37134+ return;
37135+
37136+ if (unlinked) {
37137+ au_xigen_inc(inode);
37138+ au_xib_clear_bit(inode);
37139+ }
37140+
37141+ iinfo = au_ii(inode);
5afbbe0d 37142+ bindex = iinfo->ii_btop;
b752ccd1
AM
37143+ if (bindex < 0)
37144+ return;
1facf9fc 37145+
b752ccd1
AM
37146+ xwrite = au_sbi(sb)->si_xwrite;
37147+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
5afbbe0d
AM
37148+ hi = au_hinode(iinfo, bindex);
37149+ bbot = iinfo->ii_bbot;
37150+ for (; bindex <= bbot; bindex++, hi++) {
b752ccd1
AM
37151+ h_inode = hi->hi_inode;
37152+ if (!h_inode
37153+ || (!unlinked && h_inode->i_nlink))
37154+ continue;
1facf9fc 37155+
b752ccd1
AM
37156+ /* inode may not be revalidated */
37157+ bi = au_br_index(sb, hi->hi_id);
37158+ if (bi < 0)
37159+ continue;
1facf9fc 37160+
b752ccd1
AM
37161+ br = au_sbr(sb, bi);
37162+ err = au_xino_do_write(xwrite, br->br_xino.xi_file,
37163+ h_inode->i_ino, /*ino*/0);
37164+ if (!err && try_trunc
86dc4139 37165+ && au_test_fs_trunc_xino(au_br_sb(br)))
b752ccd1 37166+ xino_try_trunc(sb, br);
1facf9fc 37167+ }
1facf9fc 37168+}
37169+
37170+/* get an unused inode number from bitmap */
37171+ino_t au_xino_new_ino(struct super_block *sb)
37172+{
37173+ ino_t ino;
37174+ unsigned long *p, pindex, ul, pend;
37175+ struct au_sbinfo *sbinfo;
37176+ struct file *file;
37177+ int free_bit, err;
37178+
37179+ if (!au_opt_test(au_mntflags(sb), XINO))
37180+ return iunique(sb, AUFS_FIRST_INO);
37181+
37182+ sbinfo = au_sbi(sb);
37183+ mutex_lock(&sbinfo->si_xib_mtx);
37184+ p = sbinfo->si_xib_buf;
37185+ free_bit = sbinfo->si_xib_next_bit;
37186+ if (free_bit < page_bits && !test_bit(free_bit, p))
37187+ goto out; /* success */
37188+ free_bit = find_first_zero_bit(p, page_bits);
37189+ if (free_bit < page_bits)
37190+ goto out; /* success */
37191+
37192+ pindex = sbinfo->si_xib_last_pindex;
37193+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
37194+ err = xib_pindex(sb, ul);
37195+ if (unlikely(err))
37196+ goto out_err;
37197+ free_bit = find_first_zero_bit(p, page_bits);
37198+ if (free_bit < page_bits)
37199+ goto out; /* success */
37200+ }
37201+
37202+ file = sbinfo->si_xib;
c06a8ce3 37203+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
1facf9fc 37204+ for (ul = pindex + 1; ul <= pend; ul++) {
37205+ err = xib_pindex(sb, ul);
37206+ if (unlikely(err))
37207+ goto out_err;
37208+ free_bit = find_first_zero_bit(p, page_bits);
37209+ if (free_bit < page_bits)
37210+ goto out; /* success */
37211+ }
37212+ BUG();
37213+
4f0767ce 37214+out:
1facf9fc 37215+ set_bit(free_bit, p);
7f207e10 37216+ sbinfo->si_xib_next_bit = free_bit + 1;
1facf9fc 37217+ pindex = sbinfo->si_xib_last_pindex;
37218+ mutex_unlock(&sbinfo->si_xib_mtx);
37219+ ino = xib_calc_ino(pindex, free_bit);
37220+ AuDbg("i%lu\n", (unsigned long)ino);
37221+ return ino;
4f0767ce 37222+out_err:
1facf9fc 37223+ mutex_unlock(&sbinfo->si_xib_mtx);
37224+ AuDbg("i0\n");
37225+ return 0;
37226+}
37227+
37228+/*
37229+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37230+ * at the position of @h_ino.
37231+ * if @ino does not exist and @do_new is true, get new one.
37232+ */
37233+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37234+ ino_t *ino)
37235+{
37236+ int err;
37237+ ssize_t sz;
37238+ loff_t pos;
37239+ struct file *file;
37240+ struct au_sbinfo *sbinfo;
37241+
37242+ *ino = 0;
37243+ if (!au_opt_test(au_mntflags(sb), XINO))
37244+ return 0; /* no xino */
37245+
37246+ err = 0;
37247+ sbinfo = au_sbi(sb);
37248+ pos = h_ino;
37249+ if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
37250+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
37251+ return -EFBIG;
37252+ }
37253+ pos *= sizeof(*ino);
37254+
37255+ file = au_sbr(sb, bindex)->br_xino.xi_file;
c06a8ce3 37256+ if (vfsub_f_size_read(file) < pos + sizeof(*ino))
1facf9fc 37257+ return 0; /* no ino */
37258+
37259+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
37260+ if (sz == sizeof(*ino))
37261+ return 0; /* success */
37262+
37263+ err = sz;
37264+ if (unlikely(sz >= 0)) {
37265+ err = -EIO;
37266+ AuIOErr("xino read error (%zd)\n", sz);
37267+ }
37268+
37269+ return err;
37270+}
37271+
37272+/* ---------------------------------------------------------------------- */
37273+
37274+/* create and set a new xino file */
37275+
37276+struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
37277+{
37278+ struct file *file;
37279+ struct dentry *h_parent, *d;
b912730e 37280+ struct inode *h_dir, *inode;
1facf9fc 37281+ int err;
37282+
37283+ /*
37284+ * at mount-time, and the xino file is the default path,
4a4d8108 37285+ * hnotify is disabled so we have no notify events to ignore.
1facf9fc 37286+ * when a user specified the xino, we cannot get au_hdir to be ignored.
37287+ */
7f207e10 37288+ file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
2cbb1c4b 37289+ /* | __FMODE_NONOTIFY */,
1facf9fc 37290+ S_IRUGO | S_IWUGO);
37291+ if (IS_ERR(file)) {
37292+ if (!silent)
4a4d8108 37293+ pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
1facf9fc 37294+ return file;
37295+ }
37296+
37297+ /* keep file count */
b912730e
AM
37298+ err = 0;
37299+ inode = file_inode(file);
2000de60 37300+ h_parent = dget_parent(file->f_path.dentry);
5527c038 37301+ h_dir = d_inode(h_parent);
febd17d6 37302+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
1facf9fc 37303+ /* mnt_want_write() is unnecessary here */
523b37e3 37304+ /* no delegation since it is just created */
b912730e
AM
37305+ if (inode->i_nlink)
37306+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37307+ /*force*/0);
febd17d6 37308+ inode_unlock(h_dir);
1facf9fc 37309+ dput(h_parent);
37310+ if (unlikely(err)) {
37311+ if (!silent)
4a4d8108 37312+ pr_err("unlink %s(%d)\n", fname, err);
1facf9fc 37313+ goto out;
37314+ }
37315+
37316+ err = -EINVAL;
2000de60 37317+ d = file->f_path.dentry;
1facf9fc 37318+ if (unlikely(sb == d->d_sb)) {
37319+ if (!silent)
4a4d8108 37320+ pr_err("%s must be outside\n", fname);
1facf9fc 37321+ goto out;
37322+ }
37323+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37324+ if (!silent)
4a4d8108
AM
37325+ pr_err("xino doesn't support %s(%s)\n",
37326+ fname, au_sbtype(d->d_sb));
1facf9fc 37327+ goto out;
37328+ }
37329+ return file; /* success */
37330+
4f0767ce 37331+out:
1facf9fc 37332+ fput(file);
37333+ file = ERR_PTR(err);
37334+ return file;
37335+}
37336+
37337+/*
37338+ * find another branch who is on the same filesystem of the specified
5afbbe0d 37339+ * branch{@btgt}. search until @bbot.
1facf9fc 37340+ */
37341+static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
5afbbe0d 37342+ aufs_bindex_t bbot)
1facf9fc 37343+{
37344+ aufs_bindex_t bindex;
37345+ struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
37346+
37347+ for (bindex = 0; bindex < btgt; bindex++)
37348+ if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
37349+ return bindex;
5afbbe0d 37350+ for (bindex++; bindex <= bbot; bindex++)
1facf9fc 37351+ if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
37352+ return bindex;
37353+ return -1;
37354+}
37355+
37356+/* ---------------------------------------------------------------------- */
37357+
37358+/*
37359+ * initialize the xinofile for the specified branch @br
37360+ * at the place/path where @base_file indicates.
37361+ * test whether another branch is on the same filesystem or not,
37362+ * if @do_test is true.
37363+ */
37364+int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
37365+ struct file *base_file, int do_test)
37366+{
37367+ int err;
37368+ ino_t ino;
5afbbe0d 37369+ aufs_bindex_t bbot, bindex;
1facf9fc 37370+ struct au_branch *shared_br, *b;
37371+ struct file *file;
37372+ struct super_block *tgt_sb;
37373+
37374+ shared_br = NULL;
5afbbe0d 37375+ bbot = au_sbbot(sb);
1facf9fc 37376+ if (do_test) {
86dc4139 37377+ tgt_sb = au_br_sb(br);
5afbbe0d 37378+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 37379+ b = au_sbr(sb, bindex);
86dc4139 37380+ if (tgt_sb == au_br_sb(b)) {
1facf9fc 37381+ shared_br = b;
37382+ break;
37383+ }
37384+ }
37385+ }
37386+
37387+ if (!shared_br || !shared_br->br_xino.xi_file) {
37388+ struct au_xino_lock_dir ldir;
37389+
37390+ au_xino_lock_dir(sb, base_file, &ldir);
37391+ /* mnt_want_write() is unnecessary here */
37392+ file = au_xino_create2(base_file, NULL);
37393+ au_xino_unlock_dir(&ldir);
37394+ err = PTR_ERR(file);
37395+ if (IS_ERR(file))
37396+ goto out;
37397+ br->br_xino.xi_file = file;
37398+ } else {
37399+ br->br_xino.xi_file = shared_br->br_xino.xi_file;
37400+ get_file(br->br_xino.xi_file);
37401+ }
37402+
37403+ ino = AUFS_ROOT_INO;
37404+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
37405+ h_ino, ino);
b752ccd1
AM
37406+ if (unlikely(err)) {
37407+ fput(br->br_xino.xi_file);
37408+ br->br_xino.xi_file = NULL;
37409+ }
1facf9fc 37410+
4f0767ce 37411+out:
1facf9fc 37412+ return err;
37413+}
37414+
37415+/* ---------------------------------------------------------------------- */
37416+
37417+/* trucate a xino bitmap file */
37418+
37419+/* todo: slow */
37420+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37421+{
37422+ int err, bit;
37423+ ssize_t sz;
37424+ unsigned long pindex;
37425+ loff_t pos, pend;
37426+ struct au_sbinfo *sbinfo;
5527c038 37427+ vfs_readf_t func;
1facf9fc 37428+ ino_t *ino;
37429+ unsigned long *p;
37430+
37431+ err = 0;
37432+ sbinfo = au_sbi(sb);
dece6358 37433+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 37434+ p = sbinfo->si_xib_buf;
37435+ func = sbinfo->si_xread;
c06a8ce3 37436+ pend = vfsub_f_size_read(file);
1facf9fc 37437+ pos = 0;
37438+ while (pos < pend) {
37439+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
37440+ err = sz;
37441+ if (unlikely(sz <= 0))
37442+ goto out;
37443+
37444+ err = 0;
37445+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37446+ if (unlikely(*ino < AUFS_FIRST_INO))
37447+ continue;
37448+
37449+ xib_calc_bit(*ino, &pindex, &bit);
37450+ AuDebugOn(page_bits <= bit);
37451+ err = xib_pindex(sb, pindex);
37452+ if (!err)
37453+ set_bit(bit, p);
37454+ else
37455+ goto out;
37456+ }
37457+ }
37458+
4f0767ce 37459+out:
1facf9fc 37460+ return err;
37461+}
37462+
37463+static int xib_restore(struct super_block *sb)
37464+{
37465+ int err;
5afbbe0d 37466+ aufs_bindex_t bindex, bbot;
1facf9fc 37467+ void *page;
37468+
37469+ err = -ENOMEM;
37470+ page = (void *)__get_free_page(GFP_NOFS);
37471+ if (unlikely(!page))
37472+ goto out;
37473+
37474+ err = 0;
5afbbe0d
AM
37475+ bbot = au_sbbot(sb);
37476+ for (bindex = 0; !err && bindex <= bbot; bindex++)
1facf9fc 37477+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
37478+ err = do_xib_restore
37479+ (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
37480+ else
37481+ AuDbg("b%d\n", bindex);
1c60b727 37482+ free_page((unsigned long)page);
1facf9fc 37483+
4f0767ce 37484+out:
1facf9fc 37485+ return err;
37486+}
37487+
37488+int au_xib_trunc(struct super_block *sb)
37489+{
37490+ int err;
37491+ ssize_t sz;
37492+ loff_t pos;
37493+ struct au_xino_lock_dir ldir;
37494+ struct au_sbinfo *sbinfo;
37495+ unsigned long *p;
37496+ struct file *file;
37497+
dece6358
AM
37498+ SiMustWriteLock(sb);
37499+
1facf9fc 37500+ err = 0;
37501+ sbinfo = au_sbi(sb);
37502+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
37503+ goto out;
37504+
37505+ file = sbinfo->si_xib;
c06a8ce3 37506+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 37507+ goto out;
37508+
37509+ au_xino_lock_dir(sb, file, &ldir);
37510+ /* mnt_want_write() is unnecessary here */
37511+ file = au_xino_create2(sbinfo->si_xib, NULL);
37512+ au_xino_unlock_dir(&ldir);
37513+ err = PTR_ERR(file);
37514+ if (IS_ERR(file))
37515+ goto out;
37516+ fput(sbinfo->si_xib);
37517+ sbinfo->si_xib = file;
37518+
37519+ p = sbinfo->si_xib_buf;
37520+ memset(p, 0, PAGE_SIZE);
37521+ pos = 0;
37522+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
37523+ if (unlikely(sz != PAGE_SIZE)) {
37524+ err = sz;
37525+ AuIOErr("err %d\n", err);
37526+ if (sz >= 0)
37527+ err = -EIO;
37528+ goto out;
37529+ }
37530+
37531+ mutex_lock(&sbinfo->si_xib_mtx);
37532+ /* mnt_want_write() is unnecessary here */
37533+ err = xib_restore(sb);
37534+ mutex_unlock(&sbinfo->si_xib_mtx);
37535+
37536+out:
37537+ return err;
37538+}
37539+
37540+/* ---------------------------------------------------------------------- */
37541+
37542+/*
37543+ * xino mount option handlers
37544+ */
1facf9fc 37545+
37546+/* xino bitmap */
37547+static void xino_clear_xib(struct super_block *sb)
37548+{
37549+ struct au_sbinfo *sbinfo;
37550+
dece6358
AM
37551+ SiMustWriteLock(sb);
37552+
1facf9fc 37553+ sbinfo = au_sbi(sb);
37554+ sbinfo->si_xread = NULL;
37555+ sbinfo->si_xwrite = NULL;
37556+ if (sbinfo->si_xib)
37557+ fput(sbinfo->si_xib);
37558+ sbinfo->si_xib = NULL;
f0c0a007 37559+ if (sbinfo->si_xib_buf)
1c60b727 37560+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 37561+ sbinfo->si_xib_buf = NULL;
37562+}
37563+
37564+static int au_xino_set_xib(struct super_block *sb, struct file *base)
37565+{
37566+ int err;
37567+ loff_t pos;
37568+ struct au_sbinfo *sbinfo;
37569+ struct file *file;
37570+
dece6358
AM
37571+ SiMustWriteLock(sb);
37572+
1facf9fc 37573+ sbinfo = au_sbi(sb);
37574+ file = au_xino_create2(base, sbinfo->si_xib);
37575+ err = PTR_ERR(file);
37576+ if (IS_ERR(file))
37577+ goto out;
37578+ if (sbinfo->si_xib)
37579+ fput(sbinfo->si_xib);
37580+ sbinfo->si_xib = file;
5527c038
JR
37581+ sbinfo->si_xread = vfs_readf(file);
37582+ sbinfo->si_xwrite = vfs_writef(file);
1facf9fc 37583+
37584+ err = -ENOMEM;
37585+ if (!sbinfo->si_xib_buf)
37586+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
37587+ if (unlikely(!sbinfo->si_xib_buf))
37588+ goto out_unset;
37589+
37590+ sbinfo->si_xib_last_pindex = 0;
37591+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 37592+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 37593+ pos = 0;
37594+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
37595+ PAGE_SIZE, &pos);
37596+ if (unlikely(err != PAGE_SIZE))
37597+ goto out_free;
37598+ }
37599+ err = 0;
37600+ goto out; /* success */
37601+
4f0767ce 37602+out_free:
f0c0a007 37603+ if (sbinfo->si_xib_buf)
1c60b727 37604+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
37605+ sbinfo->si_xib_buf = NULL;
37606+ if (err >= 0)
37607+ err = -EIO;
4f0767ce 37608+out_unset:
b752ccd1
AM
37609+ fput(sbinfo->si_xib);
37610+ sbinfo->si_xib = NULL;
37611+ sbinfo->si_xread = NULL;
37612+ sbinfo->si_xwrite = NULL;
4f0767ce 37613+out:
b752ccd1 37614+ return err;
1facf9fc 37615+}
37616+
b752ccd1
AM
37617+/* xino for each branch */
37618+static void xino_clear_br(struct super_block *sb)
37619+{
5afbbe0d 37620+ aufs_bindex_t bindex, bbot;
b752ccd1 37621+ struct au_branch *br;
1facf9fc 37622+
5afbbe0d
AM
37623+ bbot = au_sbbot(sb);
37624+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
37625+ br = au_sbr(sb, bindex);
37626+ if (!br || !br->br_xino.xi_file)
37627+ continue;
37628+
37629+ fput(br->br_xino.xi_file);
37630+ br->br_xino.xi_file = NULL;
37631+ }
37632+}
37633+
37634+static int au_xino_set_br(struct super_block *sb, struct file *base)
1facf9fc 37635+{
37636+ int err;
b752ccd1 37637+ ino_t ino;
5afbbe0d 37638+ aufs_bindex_t bindex, bbot, bshared;
b752ccd1
AM
37639+ struct {
37640+ struct file *old, *new;
37641+ } *fpair, *p;
37642+ struct au_branch *br;
37643+ struct inode *inode;
5527c038 37644+ vfs_writef_t writef;
1facf9fc 37645+
b752ccd1
AM
37646+ SiMustWriteLock(sb);
37647+
37648+ err = -ENOMEM;
5afbbe0d
AM
37649+ bbot = au_sbbot(sb);
37650+ fpair = kcalloc(bbot + 1, sizeof(*fpair), GFP_NOFS);
b752ccd1 37651+ if (unlikely(!fpair))
1facf9fc 37652+ goto out;
37653+
5527c038 37654+ inode = d_inode(sb->s_root);
b752ccd1
AM
37655+ ino = AUFS_ROOT_INO;
37656+ writef = au_sbi(sb)->si_xwrite;
5afbbe0d 37657+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
b752ccd1
AM
37658+ bshared = is_sb_shared(sb, bindex, bindex - 1);
37659+ if (bshared >= 0) {
37660+ /* shared xino */
37661+ *p = fpair[bshared];
37662+ get_file(p->new);
37663+ }
37664+
37665+ if (!p->new) {
37666+ /* new xino */
5afbbe0d 37667+ br = au_sbr(sb, bindex);
b752ccd1
AM
37668+ p->old = br->br_xino.xi_file;
37669+ p->new = au_xino_create2(base, br->br_xino.xi_file);
37670+ err = PTR_ERR(p->new);
37671+ if (IS_ERR(p->new)) {
37672+ p->new = NULL;
37673+ goto out_pair;
37674+ }
37675+ }
37676+
37677+ err = au_xino_do_write(writef, p->new,
37678+ au_h_iptr(inode, bindex)->i_ino, ino);
37679+ if (unlikely(err))
37680+ goto out_pair;
37681+ }
37682+
5afbbe0d 37683+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
b752ccd1
AM
37684+ br = au_sbr(sb, bindex);
37685+ if (br->br_xino.xi_file)
37686+ fput(br->br_xino.xi_file);
37687+ get_file(p->new);
37688+ br->br_xino.xi_file = p->new;
37689+ }
1facf9fc 37690+
4f0767ce 37691+out_pair:
5afbbe0d 37692+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++)
b752ccd1
AM
37693+ if (p->new)
37694+ fput(p->new);
37695+ else
37696+ break;
1c60b727 37697+ kfree(fpair);
4f0767ce 37698+out:
1facf9fc 37699+ return err;
37700+}
b752ccd1
AM
37701+
37702+void au_xino_clr(struct super_block *sb)
37703+{
37704+ struct au_sbinfo *sbinfo;
37705+
37706+ au_xigen_clr(sb);
37707+ xino_clear_xib(sb);
37708+ xino_clear_br(sb);
37709+ sbinfo = au_sbi(sb);
37710+ /* lvalue, do not call au_mntflags() */
37711+ au_opt_clr(sbinfo->si_mntflags, XINO);
37712+}
37713+
37714+int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
37715+{
37716+ int err, skip;
37717+ struct dentry *parent, *cur_parent;
37718+ struct qstr *dname, *cur_name;
37719+ struct file *cur_xino;
37720+ struct inode *dir;
37721+ struct au_sbinfo *sbinfo;
37722+
37723+ SiMustWriteLock(sb);
37724+
37725+ err = 0;
37726+ sbinfo = au_sbi(sb);
2000de60 37727+ parent = dget_parent(xino->file->f_path.dentry);
b752ccd1
AM
37728+ if (remount) {
37729+ skip = 0;
2000de60 37730+ dname = &xino->file->f_path.dentry->d_name;
b752ccd1
AM
37731+ cur_xino = sbinfo->si_xib;
37732+ if (cur_xino) {
2000de60
JR
37733+ cur_parent = dget_parent(cur_xino->f_path.dentry);
37734+ cur_name = &cur_xino->f_path.dentry->d_name;
b752ccd1 37735+ skip = (cur_parent == parent
38d290e6 37736+ && au_qstreq(dname, cur_name));
b752ccd1
AM
37737+ dput(cur_parent);
37738+ }
37739+ if (skip)
37740+ goto out;
37741+ }
37742+
37743+ au_opt_set(sbinfo->si_mntflags, XINO);
5527c038 37744+ dir = d_inode(parent);
febd17d6 37745+ inode_lock_nested(dir, AuLsc_I_PARENT);
b752ccd1
AM
37746+ /* mnt_want_write() is unnecessary here */
37747+ err = au_xino_set_xib(sb, xino->file);
37748+ if (!err)
37749+ err = au_xigen_set(sb, xino->file);
37750+ if (!err)
37751+ err = au_xino_set_br(sb, xino->file);
febd17d6 37752+ inode_unlock(dir);
b752ccd1
AM
37753+ if (!err)
37754+ goto out; /* success */
37755+
37756+ /* reset all */
37757+ AuIOErr("failed creating xino(%d).\n", err);
c1595e42
JR
37758+ au_xigen_clr(sb);
37759+ xino_clear_xib(sb);
b752ccd1 37760+
4f0767ce 37761+out:
b752ccd1
AM
37762+ dput(parent);
37763+ return err;
37764+}
37765+
37766+/* ---------------------------------------------------------------------- */
37767+
37768+/*
37769+ * create a xinofile at the default place/path.
37770+ */
37771+struct file *au_xino_def(struct super_block *sb)
37772+{
37773+ struct file *file;
37774+ char *page, *p;
37775+ struct au_branch *br;
37776+ struct super_block *h_sb;
37777+ struct path path;
5afbbe0d 37778+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
37779+
37780+ br = NULL;
5afbbe0d 37781+ bbot = au_sbbot(sb);
b752ccd1 37782+ bwr = -1;
5afbbe0d 37783+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
37784+ br = au_sbr(sb, bindex);
37785+ if (au_br_writable(br->br_perm)
86dc4139 37786+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
37787+ bwr = bindex;
37788+ break;
37789+ }
37790+ }
37791+
7f207e10
AM
37792+ if (bwr >= 0) {
37793+ file = ERR_PTR(-ENOMEM);
537831f9 37794+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
37795+ if (unlikely(!page))
37796+ goto out;
86dc4139 37797+ path.mnt = au_br_mnt(br);
7f207e10
AM
37798+ path.dentry = au_h_dptr(sb->s_root, bwr);
37799+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
37800+ file = (void *)p;
37801+ if (!IS_ERR(p)) {
37802+ strcat(p, "/" AUFS_XINO_FNAME);
37803+ AuDbg("%s\n", p);
37804+ file = au_xino_create(sb, p, /*silent*/0);
37805+ if (!IS_ERR(file))
37806+ au_xino_brid_set(sb, br->br_id);
37807+ }
1c60b727 37808+ free_page((unsigned long)page);
7f207e10
AM
37809+ } else {
37810+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
37811+ if (IS_ERR(file))
37812+ goto out;
2000de60 37813+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
37814+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
37815+ pr_err("xino doesn't support %s(%s)\n",
37816+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
37817+ fput(file);
37818+ file = ERR_PTR(-EINVAL);
37819+ }
37820+ if (!IS_ERR(file))
37821+ au_xino_brid_set(sb, -1);
37822+ }
0c5527e5 37823+
7f207e10
AM
37824+out:
37825+ return file;
37826+}
37827+
37828+/* ---------------------------------------------------------------------- */
37829+
37830+int au_xino_path(struct seq_file *seq, struct file *file)
37831+{
37832+ int err;
37833+
37834+ err = au_seq_path(seq, &file->f_path);
79b8bda9 37835+ if (unlikely(err))
7f207e10
AM
37836+ goto out;
37837+
7f207e10
AM
37838+#define Deleted "\\040(deleted)"
37839+ seq->count -= sizeof(Deleted) - 1;
37840+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
37841+ sizeof(Deleted) - 1));
37842+#undef Deleted
37843+
37844+out:
37845+ return err;
37846+}
521ced18
JR
37847+
37848+/* ---------------------------------------------------------------------- */
37849+
37850+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
37851+ ino_t h_ino, int idx)
37852+{
37853+ struct au_xino_file *xino;
37854+
37855+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
37856+ xino = &au_sbr(sb, bindex)->br_xino;
37857+ AuDebugOn(idx < 0 || xino->xi_nondir.total <= idx);
37858+
37859+ spin_lock(&xino->xi_nondir.spin);
37860+ AuDebugOn(xino->xi_nondir.array[idx] != h_ino);
37861+ xino->xi_nondir.array[idx] = 0;
37862+ spin_unlock(&xino->xi_nondir.spin);
37863+ wake_up_all(&xino->xi_nondir.wqh);
37864+}
37865+
37866+static int au_xinondir_find(struct au_xino_file *xino, ino_t h_ino)
37867+{
37868+ int found, total, i;
37869+
37870+ found = -1;
37871+ total = xino->xi_nondir.total;
37872+ for (i = 0; i < total; i++) {
37873+ if (xino->xi_nondir.array[i] != h_ino)
37874+ continue;
37875+ found = i;
37876+ break;
37877+ }
37878+
37879+ return found;
37880+}
37881+
37882+static int au_xinondir_expand(struct au_xino_file *xino)
37883+{
37884+ int err, sz;
37885+ ino_t *p;
37886+
37887+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
37888+
37889+ err = -ENOMEM;
37890+ sz = xino->xi_nondir.total * sizeof(ino_t);
37891+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
37892+ goto out;
37893+ p = au_kzrealloc(xino->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
37894+ /*may_shrink*/0);
37895+ if (p) {
37896+ xino->xi_nondir.array = p;
37897+ xino->xi_nondir.total <<= 1;
37898+ AuDbg("xi_nondir.total %d\n", xino->xi_nondir.total);
37899+ err = 0;
37900+ }
37901+
37902+out:
37903+ return err;
37904+}
37905+
37906+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37907+ int *idx)
37908+{
37909+ int err, found, empty;
37910+ struct au_xino_file *xino;
37911+
37912+ err = 0;
37913+ *idx = -1;
37914+ if (!au_opt_test(au_mntflags(sb), XINO))
37915+ goto out; /* no xino */
37916+
37917+ xino = &au_sbr(sb, bindex)->br_xino;
37918+
37919+again:
37920+ spin_lock(&xino->xi_nondir.spin);
37921+ found = au_xinondir_find(xino, h_ino);
37922+ if (found == -1) {
37923+ empty = au_xinondir_find(xino, /*h_ino*/0);
37924+ if (empty == -1) {
37925+ empty = xino->xi_nondir.total;
37926+ err = au_xinondir_expand(xino);
37927+ if (unlikely(err))
37928+ goto out_unlock;
37929+ }
37930+ xino->xi_nondir.array[empty] = h_ino;
37931+ *idx = empty;
37932+ } else {
37933+ spin_unlock(&xino->xi_nondir.spin);
37934+ wait_event(xino->xi_nondir.wqh,
37935+ xino->xi_nondir.array[found] != h_ino);
37936+ goto again;
37937+ }
37938+
37939+out_unlock:
37940+ spin_unlock(&xino->xi_nondir.spin);
37941+out:
37942+ return err;
37943+}
537831f9
AM
37944diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
37945--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
b00004a5 37946+++ linux/include/uapi/linux/aufs_type.h 2018-06-04 09:08:11.628152835 +0200
8b6a4947 37947@@ -0,0 +1,447 @@
7f207e10 37948+/*
b00004a5 37949+ * Copyright (C) 2005-2018 Junjiro R. Okajima
7f207e10
AM
37950+ *
37951+ * This program, aufs is free software; you can redistribute it and/or modify
37952+ * it under the terms of the GNU General Public License as published by
37953+ * the Free Software Foundation; either version 2 of the License, or
37954+ * (at your option) any later version.
37955+ *
37956+ * This program is distributed in the hope that it will be useful,
37957+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
37958+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37959+ * GNU General Public License for more details.
37960+ *
37961+ * You should have received a copy of the GNU General Public License
523b37e3 37962+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
37963+ */
37964+
37965+#ifndef __AUFS_TYPE_H__
37966+#define __AUFS_TYPE_H__
37967+
f6c5ef8b
AM
37968+#define AUFS_NAME "aufs"
37969+
9dbd164d 37970+#ifdef __KERNEL__
f6c5ef8b
AM
37971+/*
37972+ * define it before including all other headers.
37973+ * sched.h may use pr_* macros before defining "current", so define the
37974+ * no-current version first, and re-define later.
37975+ */
37976+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
37977+#include <linux/sched.h>
37978+#undef pr_fmt
a2a7ad62
AM
37979+#define pr_fmt(fmt) \
37980+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
37981+ (int)sizeof(current->comm), current->comm, current->pid
9dbd164d
AM
37982+#else
37983+#include <stdint.h>
37984+#include <sys/types.h>
f6c5ef8b 37985+#endif /* __KERNEL__ */
7f207e10 37986+
f6c5ef8b
AM
37987+#include <linux/limits.h>
37988+
b00004a5 37989+#define AUFS_VERSION "4.x-rcN-20180430"
7f207e10
AM
37990+
37991+/* todo? move this to linux-2.6.19/include/magic.h */
37992+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
37993+
37994+/* ---------------------------------------------------------------------- */
37995+
37996+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 37997+typedef int8_t aufs_bindex_t;
7f207e10
AM
37998+#define AUFS_BRANCH_MAX 127
37999+#else
9dbd164d 38000+typedef int16_t aufs_bindex_t;
7f207e10
AM
38001+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38002+#define AUFS_BRANCH_MAX 511
38003+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38004+#define AUFS_BRANCH_MAX 1023
38005+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38006+#define AUFS_BRANCH_MAX 32767
38007+#endif
38008+#endif
38009+
38010+#ifdef __KERNEL__
38011+#ifndef AUFS_BRANCH_MAX
38012+#error unknown CONFIG_AUFS_BRANCH_MAX value
38013+#endif
38014+#endif /* __KERNEL__ */
38015+
38016+/* ---------------------------------------------------------------------- */
38017+
7f207e10
AM
38018+#define AUFS_FSTYPE AUFS_NAME
38019+
38020+#define AUFS_ROOT_INO 2
38021+#define AUFS_FIRST_INO 11
38022+
38023+#define AUFS_WH_PFX ".wh."
38024+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38025+#define AUFS_WH_TMP_LEN 4
86dc4139 38026+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
38027+#define AUFS_MAX_NAMELEN (NAME_MAX \
38028+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38029+ - 1 /* dot */\
38030+ - AUFS_WH_TMP_LEN) /* hex */
38031+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38032+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
38033+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38034+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
38035+#define AUFS_DIRWH_DEF 3
38036+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 38037+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
38038+#define AUFS_RDBLK_DEF 512 /* bytes */
38039+#define AUFS_RDHASH_DEF 32
38040+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
38041+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38042+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 38043+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 38044+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
38045+
38046+/* pseudo-link maintenace under /proc */
38047+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38048+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38049+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38050+
8b6a4947
AM
38051+/* dirren, renamed dir */
38052+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38053+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38054+/* whiteouted doubly */
38055+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38056+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38057+
7f207e10
AM
38058+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38059+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38060+
38061+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38062+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38063+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38064+
38065+/* doubly whiteouted */
38066+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38067+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38068+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38069+
1e00d052 38070+/* branch permissions and attributes */
7f207e10
AM
38071+#define AUFS_BRPERM_RW "rw"
38072+#define AUFS_BRPERM_RO "ro"
38073+#define AUFS_BRPERM_RR "rr"
076b876e
AM
38074+#define AUFS_BRATTR_COO_REG "coo_reg"
38075+#define AUFS_BRATTR_COO_ALL "coo_all"
38076+#define AUFS_BRATTR_FHSM "fhsm"
38077+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
38078+#define AUFS_BRATTR_ICEX "icex"
38079+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38080+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38081+#define AUFS_BRATTR_ICEX_TR "icextr"
38082+#define AUFS_BRATTR_ICEX_USR "icexusr"
38083+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
38084+#define AUFS_BRRATTR_WH "wh"
38085+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
38086+#define AUFS_BRWATTR_MOO "moo"
38087+
38088+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38089+#define AuBrPerm_RO (1 << 1) /* readonly */
38090+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38091+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38092+
38093+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38094+#define AuBrAttr_COO_ALL (1 << 4)
38095+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38096+
38097+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38098+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
38099+ branch. meaningless since
38100+ linux-3.18-rc1 */
38101+
38102+/* ignore error in copying XATTR */
38103+#define AuBrAttr_ICEX_SEC (1 << 7)
38104+#define AuBrAttr_ICEX_SYS (1 << 8)
38105+#define AuBrAttr_ICEX_TR (1 << 9)
38106+#define AuBrAttr_ICEX_USR (1 << 10)
38107+#define AuBrAttr_ICEX_OTH (1 << 11)
38108+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38109+ | AuBrAttr_ICEX_SYS \
38110+ | AuBrAttr_ICEX_TR \
38111+ | AuBrAttr_ICEX_USR \
38112+ | AuBrAttr_ICEX_OTH)
38113+
38114+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
38115+#define AuBrRAttr_Mask AuBrRAttr_WH
38116+
c1595e42
JR
38117+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
38118+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
38119+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38120+
38121+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38122+
c1595e42 38123+/* #warning test userspace */
076b876e
AM
38124+#ifdef __KERNEL__
38125+#ifndef CONFIG_AUFS_FHSM
38126+#undef AuBrAttr_FHSM
38127+#define AuBrAttr_FHSM 0
38128+#endif
c1595e42
JR
38129+#ifndef CONFIG_AUFS_XATTR
38130+#undef AuBrAttr_ICEX
38131+#define AuBrAttr_ICEX 0
38132+#undef AuBrAttr_ICEX_SEC
38133+#define AuBrAttr_ICEX_SEC 0
38134+#undef AuBrAttr_ICEX_SYS
38135+#define AuBrAttr_ICEX_SYS 0
38136+#undef AuBrAttr_ICEX_TR
38137+#define AuBrAttr_ICEX_TR 0
38138+#undef AuBrAttr_ICEX_USR
38139+#define AuBrAttr_ICEX_USR 0
38140+#undef AuBrAttr_ICEX_OTH
38141+#define AuBrAttr_ICEX_OTH 0
38142+#endif
076b876e
AM
38143+#endif
38144+
38145+/* the longest combination */
c1595e42
JR
38146+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38147+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
38148+ "+" AUFS_BRATTR_COO_REG \
38149+ "+" AUFS_BRATTR_FHSM \
38150+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
38151+ "+" AUFS_BRATTR_ICEX_SEC \
38152+ "+" AUFS_BRATTR_ICEX_SYS \
38153+ "+" AUFS_BRATTR_ICEX_USR \
38154+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
38155+ "+" AUFS_BRWATTR_NLWH)
38156+
38157+typedef struct {
38158+ char a[AuBrPermStrSz];
38159+} au_br_perm_str_t;
38160+
38161+static inline int au_br_writable(int brperm)
38162+{
38163+ return brperm & AuBrPerm_RW;
38164+}
38165+
38166+static inline int au_br_whable(int brperm)
38167+{
38168+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38169+}
38170+
38171+static inline int au_br_wh_linkable(int brperm)
38172+{
38173+ return !(brperm & AuBrWAttr_NoLinkWH);
38174+}
38175+
38176+static inline int au_br_cmoo(int brperm)
38177+{
38178+ return brperm & AuBrAttr_CMOO_Mask;
38179+}
38180+
38181+static inline int au_br_fhsm(int brperm)
38182+{
38183+ return brperm & AuBrAttr_FHSM;
38184+}
7f207e10
AM
38185+
38186+/* ---------------------------------------------------------------------- */
38187+
38188+/* ioctl */
38189+enum {
38190+ /* readdir in userspace */
38191+ AuCtl_RDU,
38192+ AuCtl_RDU_INO,
38193+
076b876e
AM
38194+ AuCtl_WBR_FD, /* pathconf wrapper */
38195+ AuCtl_IBUSY, /* busy inode */
38196+ AuCtl_MVDOWN, /* move-down */
38197+ AuCtl_BR, /* info about branches */
38198+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
38199+};
38200+
38201+/* borrowed from linux/include/linux/kernel.h */
38202+#ifndef ALIGN
38203+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
38204+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
38205+#endif
38206+
38207+/* borrowed from linux/include/linux/compiler-gcc3.h */
38208+#ifndef __aligned
38209+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
38210+#endif
38211+
38212+#ifdef __KERNEL__
38213+#ifndef __packed
7f207e10
AM
38214+#define __packed __attribute__((packed))
38215+#endif
53392da6 38216+#endif
7f207e10
AM
38217+
38218+struct au_rdu_cookie {
9dbd164d
AM
38219+ uint64_t h_pos;
38220+ int16_t bindex;
38221+ uint8_t flags;
38222+ uint8_t pad;
38223+ uint32_t generation;
7f207e10
AM
38224+} __aligned(8);
38225+
38226+struct au_rdu_ent {
9dbd164d
AM
38227+ uint64_t ino;
38228+ int16_t bindex;
38229+ uint8_t type;
38230+ uint8_t nlen;
38231+ uint8_t wh;
7f207e10
AM
38232+ char name[0];
38233+} __aligned(8);
38234+
38235+static inline int au_rdu_len(int nlen)
38236+{
38237+ /* include the terminating NULL */
38238+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 38239+ sizeof(uint64_t));
7f207e10
AM
38240+}
38241+
38242+union au_rdu_ent_ul {
38243+ struct au_rdu_ent __user *e;
9dbd164d 38244+ uint64_t ul;
7f207e10
AM
38245+};
38246+
38247+enum {
38248+ AufsCtlRduV_SZ,
38249+ AufsCtlRduV_End
38250+};
38251+
38252+struct aufs_rdu {
38253+ /* input */
38254+ union {
9dbd164d
AM
38255+ uint64_t sz; /* AuCtl_RDU */
38256+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
38257+ };
38258+ union au_rdu_ent_ul ent;
9dbd164d 38259+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
38260+
38261+ /* input/output */
9dbd164d 38262+ uint32_t blk;
7f207e10
AM
38263+
38264+ /* output */
38265+ union au_rdu_ent_ul tail;
38266+ /* number of entries which were added in a single call */
9dbd164d
AM
38267+ uint64_t rent;
38268+ uint8_t full;
38269+ uint8_t shwh;
7f207e10
AM
38270+
38271+ struct au_rdu_cookie cookie;
38272+} __aligned(8);
38273+
1e00d052
AM
38274+/* ---------------------------------------------------------------------- */
38275+
8b6a4947
AM
38276+/* dirren. the branch is identified by the filename who contains this */
38277+struct au_drinfo {
38278+ uint64_t ino;
38279+ union {
38280+ uint8_t oldnamelen;
38281+ uint64_t _padding;
38282+ };
38283+ uint8_t oldname[0];
38284+} __aligned(8);
38285+
38286+struct au_drinfo_fdata {
38287+ uint32_t magic;
38288+ struct au_drinfo drinfo;
38289+} __aligned(8);
38290+
38291+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
38292+/* future */
38293+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
38294+
38295+/* ---------------------------------------------------------------------- */
38296+
1e00d052 38297+struct aufs_wbr_fd {
9dbd164d
AM
38298+ uint32_t oflags;
38299+ int16_t brid;
1e00d052
AM
38300+} __aligned(8);
38301+
38302+/* ---------------------------------------------------------------------- */
38303+
027c5e7a 38304+struct aufs_ibusy {
9dbd164d
AM
38305+ uint64_t ino, h_ino;
38306+ int16_t bindex;
027c5e7a
AM
38307+} __aligned(8);
38308+
1e00d052
AM
38309+/* ---------------------------------------------------------------------- */
38310+
392086de
AM
38311+/* error code for move-down */
38312+/* the actual message strings are implemented in aufs-util.git */
38313+enum {
38314+ EAU_MVDOWN_OPAQUE = 1,
38315+ EAU_MVDOWN_WHITEOUT,
38316+ EAU_MVDOWN_UPPER,
38317+ EAU_MVDOWN_BOTTOM,
38318+ EAU_MVDOWN_NOUPPER,
38319+ EAU_MVDOWN_NOLOWERBR,
38320+ EAU_Last
38321+};
38322+
c2b27bf2 38323+/* flags for move-down */
392086de
AM
38324+#define AUFS_MVDOWN_DMSG 1
38325+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
38326+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
38327+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
38328+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
38329+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
38330+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
38331+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
38332+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
38333+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
38334+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
38335+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
38336+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 38337+
076b876e 38338+/* index for move-down */
392086de
AM
38339+enum {
38340+ AUFS_MVDOWN_UPPER,
38341+ AUFS_MVDOWN_LOWER,
38342+ AUFS_MVDOWN_NARRAY
38343+};
38344+
076b876e
AM
38345+/*
38346+ * additional info of move-down
38347+ * number of free blocks and inodes.
38348+ * subset of struct kstatfs, but smaller and always 64bit.
38349+ */
38350+struct aufs_stfs {
38351+ uint64_t f_blocks;
38352+ uint64_t f_bavail;
38353+ uint64_t f_files;
38354+ uint64_t f_ffree;
38355+};
38356+
38357+struct aufs_stbr {
38358+ int16_t brid; /* optional input */
38359+ int16_t bindex; /* output */
38360+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
38361+} __aligned(8);
38362+
c2b27bf2 38363+struct aufs_mvdown {
076b876e
AM
38364+ uint32_t flags; /* input/output */
38365+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
38366+ int8_t au_errno; /* output */
38367+} __aligned(8);
38368+
38369+/* ---------------------------------------------------------------------- */
38370+
38371+union aufs_brinfo {
38372+ /* PATH_MAX may differ between kernel-space and user-space */
38373+ char _spacer[4096];
392086de 38374+ struct {
076b876e
AM
38375+ int16_t id;
38376+ int perm;
38377+ char path[0];
38378+ };
c2b27bf2
AM
38379+} __aligned(8);
38380+
38381+/* ---------------------------------------------------------------------- */
38382+
7f207e10
AM
38383+#define AuCtlType 'A'
38384+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
38385+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
38386+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
38387+ struct aufs_wbr_fd)
027c5e7a 38388+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
38389+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
38390+ struct aufs_mvdown)
076b876e
AM
38391+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
38392+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
38393+
38394+#endif /* __AUFS_TYPE_H__ */
2121bcd9 38395SPDX-License-Identifier: GPL-2.0
ffa93bbd 38396aufs4.x-rcN loopback patch
5527c038
JR
38397
38398diff --git a/drivers/block/loop.c b/drivers/block/loop.c
b00004a5 38399index 79902c1..40db6d1 100644
5527c038
JR
38400--- a/drivers/block/loop.c
38401+++ b/drivers/block/loop.c
2121bcd9 38402@@ -600,6 +600,15 @@ static inline void loop_update_dio(struct loop_device *lo)
8b6a4947 38403 lo->use_dio);
5527c038
JR
38404 }
38405
5527c038
JR
38406+static struct file *loop_real_file(struct file *file)
38407+{
38408+ struct file *f = NULL;
38409+
38410+ if (file->f_path.dentry->d_sb->s_op->real_loop)
38411+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
38412+ return f;
8b6a4947
AM
38413+}
38414+
c2c0f25c 38415 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
38416 struct block_device *bdev)
38417 {
2121bcd9 38418@@ -634,6 +643,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
38419 unsigned int arg)
38420 {
38421 struct file *file, *old_file;
38422+ struct file *f, *virt_file = NULL, *old_virt_file;
38423 struct inode *inode;
38424 int error;
38425
2121bcd9 38426@@ -650,9 +660,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
38427 file = fget(arg);
38428 if (!file)
38429 goto out;
38430+ f = loop_real_file(file);
38431+ if (f) {
38432+ virt_file = file;
38433+ file = f;
38434+ get_file(file);
38435+ }
38436
38437 inode = file->f_mapping->host;
38438 old_file = lo->lo_backing_file;
38439+ old_virt_file = lo->lo_backing_virt_file;
38440
38441 error = -EINVAL;
38442
2121bcd9 38443@@ -667,6 +684,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
38444 blk_mq_freeze_queue(lo->lo_queue);
38445 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
38446 lo->lo_backing_file = file;
38447+ lo->lo_backing_virt_file = virt_file;
38448 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
38449 mapping_set_gfp_mask(file->f_mapping,
38450 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
2121bcd9 38451@@ -674,12 +692,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947 38452 blk_mq_unfreeze_queue(lo->lo_queue);
5527c038
JR
38453
38454 fput(old_file);
38455+ if (old_virt_file)
38456+ fput(old_virt_file);
38457 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
c2c0f25c 38458 loop_reread_partitions(lo, bdev);
5527c038
JR
38459 return 0;
38460
38461 out_putf:
38462 fput(file);
38463+ if (virt_file)
38464+ fput(virt_file);
38465 out:
38466 return error;
38467 }
2121bcd9 38468@@ -873,7 +895,7 @@ static int loop_prepare_queue(struct loop_device *lo)
5527c038
JR
38469 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38470 struct block_device *bdev, unsigned int arg)
38471 {
38472- struct file *file, *f;
38473+ struct file *file, *f, *virt_file = NULL;
38474 struct inode *inode;
38475 struct address_space *mapping;
8b6a4947 38476 int lo_flags = 0;
2121bcd9 38477@@ -887,6 +909,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38478 file = fget(arg);
38479 if (!file)
38480 goto out;
38481+ f = loop_real_file(file);
38482+ if (f) {
38483+ virt_file = file;
38484+ file = f;
38485+ get_file(file);
38486+ }
38487
38488 error = -EBUSY;
38489 if (lo->lo_state != Lo_unbound)
2121bcd9 38490@@ -935,6 +963,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38491 lo->lo_device = bdev;
38492 lo->lo_flags = lo_flags;
38493 lo->lo_backing_file = file;
38494+ lo->lo_backing_virt_file = virt_file;
38495 lo->transfer = NULL;
38496 lo->ioctl = NULL;
38497 lo->lo_sizelimit = 0;
2121bcd9 38498@@ -968,6 +997,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38499
38500 out_putf:
38501 fput(file);
38502+ if (virt_file)
38503+ fput(virt_file);
38504 out:
38505 /* This is safe: open() is still holding a reference. */
38506 module_put(THIS_MODULE);
2121bcd9 38507@@ -1014,6 +1045,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
5527c038
JR
38508 static int loop_clr_fd(struct loop_device *lo)
38509 {
38510 struct file *filp = lo->lo_backing_file;
38511+ struct file *virt_filp = lo->lo_backing_virt_file;
38512 gfp_t gfp = lo->old_gfp_mask;
38513 struct block_device *bdev = lo->lo_device;
38514
2121bcd9 38515@@ -1045,6 +1077,7 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
38516 spin_lock_irq(&lo->lo_lock);
38517 lo->lo_state = Lo_rundown;
38518 lo->lo_backing_file = NULL;
38519+ lo->lo_backing_virt_file = NULL;
38520 spin_unlock_irq(&lo->lo_lock);
38521
38522 loop_release_xfer(lo);
2121bcd9 38523@@ -1092,6 +1125,8 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
38524 * bd_mutex which is usually taken before lo_ctl_mutex.
38525 */
38526 fput(filp);
38527+ if (virt_filp)
38528+ fput(virt_filp);
38529 return 0;
38530 }
38531
38532diff --git a/drivers/block/loop.h b/drivers/block/loop.h
2121bcd9 38533index 0f45416..101f193 100644
5527c038
JR
38534--- a/drivers/block/loop.h
38535+++ b/drivers/block/loop.h
38536@@ -46,7 +46,7 @@ struct loop_device {
38537 int (*ioctl)(struct loop_device *, int cmd,
38538 unsigned long arg);
38539
38540- struct file * lo_backing_file;
38541+ struct file * lo_backing_file, *lo_backing_virt_file;
38542 struct block_device *lo_device;
5527c038 38543 void *key_data;
8b6a4947 38544
5527c038 38545diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
b00004a5 38546index ad30f42..5caf353 100644
5527c038
JR
38547--- a/fs/aufs/f_op.c
38548+++ b/fs/aufs/f_op.c
2121bcd9 38549@@ -358,7 +358,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
38550 if (IS_ERR(h_file))
38551 goto out;
38552
38553- if (au_test_loopback_kthread()) {
38554+ if (0 && au_test_loopback_kthread()) {
38555 au_warn_loopback(h_file->f_path.dentry->d_sb);
38556 if (file->f_mapping != h_file->f_mapping) {
38557 file->f_mapping = h_file->f_mapping;
38558diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
b00004a5 38559index 3e9d59a..42811d3 100644
5527c038
JR
38560--- a/fs/aufs/loop.c
38561+++ b/fs/aufs/loop.c
2121bcd9 38562@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 38563 symbol_put(loop_backing_file);
1c60b727 38564 kfree(au_warn_loopback_array);
5527c038
JR
38565 }
38566+
38567+/* ---------------------------------------------------------------------- */
38568+
38569+/* support the loopback block device insude aufs */
38570+
38571+struct file *aufs_real_loop(struct file *file)
38572+{
38573+ struct file *f;
38574+
38575+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
38576+ fi_read_lock(file);
38577+ f = au_hf_top(file);
38578+ fi_read_unlock(file);
38579+ AuDebugOn(!f);
38580+ return f;
38581+}
38582diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
b00004a5 38583index dc3f3be..c33d060 100644
5527c038
JR
38584--- a/fs/aufs/loop.h
38585+++ b/fs/aufs/loop.h
2121bcd9 38586@@ -26,7 +26,11 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
38587
38588 int au_loopback_init(void);
38589 void au_loopback_fin(void);
38590+
38591+struct file *aufs_real_loop(struct file *file);
38592 #else
38593+AuStub(struct file *, loop_backing_file, return NULL)
38594+
38595 AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
38596 struct dentry *h_adding)
38597 AuStubInt0(au_test_loopback_kthread, void)
2121bcd9 38598@@ -34,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
38599
38600 AuStubInt0(au_loopback_init, void)
38601 AuStubVoid(au_loopback_fin, void)
38602+
38603+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
38604 #endif /* BLK_DEV_LOOP */
38605
38606 #endif /* __KERNEL__ */
38607diff --git a/fs/aufs/super.c b/fs/aufs/super.c
b00004a5 38608index 80644ef..19cde48 100644
5527c038
JR
38609--- a/fs/aufs/super.c
38610+++ b/fs/aufs/super.c
be118d29 38611@@ -843,7 +843,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
38612 .statfs = aufs_statfs,
38613 .put_super = aufs_put_super,
38614 .sync_fs = aufs_sync_fs,
38615- .remount_fs = aufs_remount_fs
38616+ .remount_fs = aufs_remount_fs,
38617+#ifdef CONFIG_AUFS_BDEV_LOOP
38618+ .real_loop = aufs_real_loop
38619+#endif
38620 };
38621
38622 /* ---------------------------------------------------------------------- */
38623diff --git a/include/linux/fs.h b/include/linux/fs.h
b00004a5 38624index 09a2542..11a6346 100644
5527c038
JR
38625--- a/include/linux/fs.h
38626+++ b/include/linux/fs.h
b00004a5 38627@@ -1852,6 +1852,10 @@ struct super_operations {
5527c038
JR
38628 struct shrink_control *);
38629 long (*free_cached_objects)(struct super_block *,
38630 struct shrink_control *);
38631+#if defined(CONFIG_BLK_DEV_LOOP) || defined(CONFIG_BLK_DEV_LOOP_MODULE)
38632+ /* and aufs */
38633+ struct file *(*real_loop)(struct file *);
38634+#endif
38635 };
38636
38637 /*
This page took 7.769585 seconds and 4 git commands to generate.