]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs4.patch
- up to 4.17.9
[packages/kernel.git] / kernel-aufs4.patch
CommitLineData
2121bcd9 1SPDX-License-Identifier: GPL-2.0
c4adf169 2aufs4.17 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
c4adf169 26aufs4.17 base patch
7f207e10 27
c1595e42 28diff --git a/MAINTAINERS b/MAINTAINERS
c4adf169 29index 9c125f7..4616bbf 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
c4adf169 32@@ -2519,6 +2519,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
c4adf169 53index 55cf554..bc965e5 100644
392086de
AM
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
c4adf169 56@@ -713,6 +713,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
c4adf169 82index 2acfc69..ff338e2 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
c4adf169 117index 3b55391..e0c5255 100644
5afbbe0d
AM
118--- a/fs/inode.c
119+++ b/fs/inode.c
c4adf169 120@@ -1663,7 +1663,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
c4adf169 358aufs4.17 mmap patch
fb47a38f 359
c1595e42 360diff --git a/fs/proc/base.c b/fs/proc/base.c
c4adf169 361index 1a76d75..77f698e 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
c4adf169 431index 02a616e..01b3bb9 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
c4adf169 523index fc41c05..e376869 100644
fb47a38f
JR
524--- a/mm/mmap.c
525+++ b/mm/mmap.c
c4adf169 526@@ -180,7 +180,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;
c4adf169 535@@ -905,7 +905,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);
c4adf169 544@@ -1820,8 +1820,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);
c4adf169 554@@ -2645,7 +2645,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);
c4adf169 563@@ -2664,7 +2664,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));
c4adf169 572@@ -2826,7 +2826,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);
c4adf169 581@@ -2901,10 +2901,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)
c4adf169 610@@ -3220,7 +3237,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
062440b3 665index 0000000..37e95e1
076b876e
AM
666--- /dev/null
667+++ b/mm/prfile.c
2121bcd9 668@@ -0,0 +1,86 @@
062440b3 669+/* SPDX-License-Identifier: GPL-2.0 */
076b876e 670+/*
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
c4adf169 756aufs4.17 standalone patch
7f207e10 757
c1595e42 758diff --git a/fs/dcache.c b/fs/dcache.c
c4adf169 759index ff338e2..3e2bae8 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;
c4adf169 770@@ -2942,6 +2943,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
c4adf169 839index e0c5255..ff36056 100644
5afbbe0d
AM
840--- a/fs/inode.c
841+++ b/fs/inode.c
c4adf169 842@@ -1672,6 +1672,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
c4adf169 951index d0e955b..527bc1a 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 {
c4adf169
AM
962@@ -723,6 +724,7 @@ SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
963 {
964 return ksys_fchown(fd, user, group);
965 }
966+EXPORT_SYMBOL_GPL(open_check_o_direct);
967
968 int open_check_o_direct(struct file *f)
969 {
5527c038 970diff --git a/fs/read_write.c b/fs/read_write.c
b00004a5 971index ddd6e67..aabf92d 100644
5527c038
JR
972--- a/fs/read_write.c
973+++ b/fs/read_write.c
b00004a5 974@@ -459,6 +459,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
8b6a4947
AM
975
976 return ret;
977 }
978+EXPORT_SYMBOL_GPL(vfs_read);
979
980 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
981 {
b00004a5 982@@ -499,6 +500,7 @@ vfs_readf_t vfs_readf(struct file *file)
5527c038
JR
983 return new_sync_read;
984 return ERR_PTR(-ENOSYS);
985 }
febd17d6 986+EXPORT_SYMBOL_GPL(vfs_readf);
5527c038
JR
987
988 vfs_writef_t vfs_writef(struct file *file)
989 {
b00004a5 990@@ -510,6 +512,7 @@ vfs_writef_t vfs_writef(struct file *file)
5527c038
JR
991 return new_sync_write;
992 return ERR_PTR(-ENOSYS);
993 }
febd17d6 994+EXPORT_SYMBOL_GPL(vfs_writef);
5527c038 995
8b6a4947
AM
996 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
997 {
b00004a5 998@@ -579,6 +582,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
8b6a4947
AM
999
1000 return ret;
1001 }
1002+EXPORT_SYMBOL_GPL(vfs_write);
1003
1004 static inline loff_t file_pos_read(struct file *file)
5527c038 1005 {
7f207e10 1006diff --git a/fs/splice.c b/fs/splice.c
b00004a5 1007index f617ab0..ec0ad02 100644
7f207e10
AM
1008--- a/fs/splice.c
1009+++ b/fs/splice.c
8b6a4947 1010@@ -850,6 +850,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
392086de
AM
1011
1012 return splice_write(pipe, out, ppos, len, flags);
7f207e10 1013 }
febd17d6 1014+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
1015
1016 /*
1017 * Attempt to initiate a splice from a file to a pipe.
8b6a4947 1018@@ -879,6 +880,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
7f207e10
AM
1019
1020 return splice_read(in, ppos, pipe, len, flags);
1021 }
febd17d6 1022+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
1023
1024 /**
1025 * splice_direct_to_actor - splices data directly between two non-pipes
a2654f78 1026diff --git a/fs/sync.c b/fs/sync.c
b00004a5 1027index 2860782..ffd7ea4 100644
a2654f78
AM
1028--- a/fs/sync.c
1029+++ b/fs/sync.c
2121bcd9 1030@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
a2654f78
AM
1031 sb->s_op->sync_fs(sb, wait);
1032 return __sync_blockdev(sb->s_bdev, wait);
1033 }
1034+EXPORT_SYMBOL_GPL(__sync_filesystem);
1035
1036 /*
1037 * Write out and wait upon all dirty data associated with this
c1595e42 1038diff --git a/fs/xattr.c b/fs/xattr.c
8b6a4947 1039index 61cd28b..35570cd 100644
c1595e42
JR
1040--- a/fs/xattr.c
1041+++ b/fs/xattr.c
8b6a4947 1042@@ -297,6 +297,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
c1595e42
JR
1043 *xattr_value = value;
1044 return error;
1045 }
febd17d6 1046+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 1047
febd17d6 1048 ssize_t
f2c43d5f 1049 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947 1050diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
b00004a5 1051index 06e0d7a..6af91bd 100644
8b6a4947
AM
1052--- a/kernel/locking/lockdep.c
1053+++ b/kernel/locking/lockdep.c
be118d29 1054@@ -151,6 +151,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
8b6a4947
AM
1055 }
1056 return lock_classes + hlock->class_idx - 1;
1057 }
1058+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
1059 #define hlock_class(hlock) lockdep_hlock_class(hlock)
1060
1061 #ifdef CONFIG_LOCK_STAT
8cdd5066 1062diff --git a/kernel/task_work.c b/kernel/task_work.c
2121bcd9 1063index 0fef395..83fb1ec 100644
8cdd5066
JR
1064--- a/kernel/task_work.c
1065+++ b/kernel/task_work.c
2121bcd9 1066@@ -116,3 +116,4 @@ void task_work_run(void)
8cdd5066
JR
1067 } while (work);
1068 }
1069 }
febd17d6 1070+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 1071diff --git a/security/commoncap.c b/security/commoncap.c
c4adf169 1072index 1ce701f..a0d106e 100644
7f207e10
AM
1073--- a/security/commoncap.c
1074+++ b/security/commoncap.c
c4adf169 1075@@ -1332,12 +1332,14 @@ int cap_mmap_addr(unsigned long addr)
94337f0d 1076 }
7f207e10
AM
1077 return ret;
1078 }
febd17d6 1079+EXPORT_SYMBOL_GPL(cap_mmap_addr);
0c3ec466
AM
1080
1081 int cap_mmap_file(struct file *file, unsigned long reqprot,
1082 unsigned long prot, unsigned long flags)
1083 {
1084 return 0;
1085 }
febd17d6 1086+EXPORT_SYMBOL_GPL(cap_mmap_file);
c2c0f25c
AM
1087
1088 #ifdef CONFIG_SECURITY
1089
7f207e10 1090diff --git a/security/device_cgroup.c b/security/device_cgroup.c
2121bcd9 1091index c65b39b..e363d22 100644
7f207e10
AM
1092--- a/security/device_cgroup.c
1093+++ b/security/device_cgroup.c
2121bcd9 1094@@ -8,6 +8,7 @@
f6c5ef8b
AM
1095 #include <linux/device_cgroup.h>
1096 #include <linux/cgroup.h>
1097 #include <linux/ctype.h>
1098+#include <linux/export.h>
1099 #include <linux/list.h>
1100 #include <linux/uaccess.h>
1101 #include <linux/seq_file.h>
2121bcd9 1102@@ -824,3 +825,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
7f207e10 1103
2121bcd9
AM
1104 return 0;
1105 }
1106+EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
7f207e10 1107diff --git a/security/security.c b/security/security.c
b00004a5 1108index 7bc2fde..6bd0468 100644
7f207e10
AM
1109--- a/security/security.c
1110+++ b/security/security.c
b00004a5 1111@@ -537,6 +537,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 1112 return 0;
c2c0f25c 1113 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 1114 }
febd17d6 1115+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 1116
5afbbe0d 1117 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 1118 {
b00004a5 1119@@ -553,6 +554,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 1120 return 0;
c2c0f25c 1121 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 1122 }
febd17d6 1123+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 1124
5afbbe0d 1125 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1126 struct dentry *new_dentry)
b00004a5 1127@@ -561,6 +563,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1128 return 0;
c2c0f25c 1129 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 1130 }
febd17d6 1131+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 1132
5afbbe0d
AM
1133 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1134 const struct path *new_dir, struct dentry *new_dentry,
b00004a5 1135@@ -588,6 +591,7 @@ int security_path_truncate(const struct path *path)
7f207e10 1136 return 0;
c2c0f25c 1137 return call_int_hook(path_truncate, 0, path);
7f207e10 1138 }
febd17d6 1139+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 1140
5afbbe0d 1141 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 1142 {
b00004a5 1143@@ -595,6 +599,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 1144 return 0;
c2c0f25c 1145 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 1146 }
febd17d6 1147+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 1148
5afbbe0d 1149 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1150 {
b00004a5 1151@@ -602,6 +607,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1152 return 0;
c2c0f25c 1153 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 1154 }
febd17d6 1155+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 1156
5afbbe0d 1157 int security_path_chroot(const struct path *path)
7f207e10 1158 {
b00004a5 1159@@ -687,6 +693,7 @@ int security_inode_readlink(struct dentry *dentry)
7f207e10 1160 return 0;
c2c0f25c 1161 return call_int_hook(inode_readlink, 0, dentry);
7f207e10 1162 }
febd17d6 1163+EXPORT_SYMBOL_GPL(security_inode_readlink);
7f207e10 1164
c2c0f25c
AM
1165 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1166 bool rcu)
b00004a5 1167@@ -702,6 +709,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 1168 return 0;
c2c0f25c 1169 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 1170 }
febd17d6 1171+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 1172
1e00d052 1173 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
7f207e10 1174 {
b00004a5 1175@@ -873,6 +881,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
1176
1177 return fsnotify_perm(file, mask);
1178 }
febd17d6 1179+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
1180
1181 int security_file_alloc(struct file *file)
1182 {
b00004a5 1183@@ -932,6 +941,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
7f207e10
AM
1184 return ret;
1185 return ima_file_mmap(file, prot);
1186 }
febd17d6 1187+EXPORT_SYMBOL_GPL(security_mmap_file);
7f207e10 1188
0c3ec466
AM
1189 int security_mmap_addr(unsigned long addr)
1190 {
7f207e10
AM
1191diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1192--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
1193+++ linux/Documentation/ABI/testing/debugfs-aufs 2018-07-19 09:46:13.053314374 +0200
1194@@ -0,0 +1,55 @@
7f207e10
AM
1195+What: /debug/aufs/si_<id>/
1196+Date: March 2009
f6b6e03d 1197+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1198+Description:
1199+ Under /debug/aufs, a directory named si_<id> is created
1200+ per aufs mount, where <id> is a unique id generated
1201+ internally.
1facf9fc 1202+
86dc4139
AM
1203+What: /debug/aufs/si_<id>/plink
1204+Date: Apr 2013
f6b6e03d 1205+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
1206+Description:
1207+ It has three lines and shows the information about the
1208+ pseudo-link. The first line is a single number
1209+ representing a number of buckets. The second line is a
1210+ number of pseudo-links per buckets (separated by a
1211+ blank). The last line is a single number representing a
1212+ total number of psedo-links.
1213+ When the aufs mount option 'noplink' is specified, it
1214+ will show "1\n0\n0\n".
1215+
7f207e10
AM
1216+What: /debug/aufs/si_<id>/xib
1217+Date: March 2009
f6b6e03d 1218+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1219+Description:
1220+ It shows the consumed blocks by xib (External Inode Number
1221+ Bitmap), its block size and file size.
1222+ When the aufs mount option 'noxino' is specified, it
1223+ will be empty. About XINO files, see the aufs manual.
1224+
062440b3 1225+What: /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
7f207e10 1226+Date: March 2009
f6b6e03d 1227+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1228+Description:
1229+ It shows the consumed blocks by xino (External Inode Number
1230+ Translation Table), its link count, block size and file
1231+ size.
062440b3
AM
1232+ Due to the file size limit, there may exist multiple
1233+ xino files per branch. In this case, "-N" is added to
1234+ the filename and it corresponds to the index of the
1235+ internal xino array. "-0" is omitted.
1236+ When the aufs mount option 'noxino' is specified, Those
1237+ entries won't exist. About XINO files, see the aufs
1238+ manual.
7f207e10
AM
1239+
1240+What: /debug/aufs/si_<id>/xigen
1241+Date: March 2009
f6b6e03d 1242+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1243+Description:
1244+ It shows the consumed blocks by xigen (External Inode
1245+ Generation Table), its block size and file size.
1246+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1247+ be created.
1248+ When the aufs mount option 'noxino' is specified, it
1249+ will be empty. About XINO files, see the aufs manual.
1250diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1251--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
b00004a5 1252+++ linux/Documentation/ABI/testing/sysfs-aufs 2017-07-29 12:14:25.893041746 +0200
392086de 1253@@ -0,0 +1,31 @@
7f207e10
AM
1254+What: /sys/fs/aufs/si_<id>/
1255+Date: March 2009
f6b6e03d 1256+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1257+Description:
1258+ Under /sys/fs/aufs, a directory named si_<id> is created
1259+ per aufs mount, where <id> is a unique id generated
1260+ internally.
1261+
1262+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1263+Date: March 2009
f6b6e03d 1264+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1265+Description:
1266+ It shows the abolute path of a member directory (which
1267+ is called branch) in aufs, and its permission.
1268+
392086de
AM
1269+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1270+Date: July 2013
f6b6e03d 1271+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1272+Description:
1273+ It shows the id of a member directory (which is called
1274+ branch) in aufs.
1275+
7f207e10
AM
1276+What: /sys/fs/aufs/si_<id>/xi_path
1277+Date: March 2009
f6b6e03d 1278+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1279+Description:
1280+ It shows the abolute path of XINO (External Inode Number
1281+ Bitmap, Translation Table and Generation Table) file
1282+ even if it is the default path.
1283+ When the aufs mount option 'noxino' is specified, it
1284+ will be empty. About XINO files, see the aufs manual.
53392da6
AM
1285diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1286--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1287+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2018-04-15 08:49:13.394483860 +0200
1c60b727 1288@@ -0,0 +1,171 @@
53392da6 1289+
b00004a5 1290+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1291+#
1292+# This program is free software; you can redistribute it and/or modify
1293+# it under the terms of the GNU General Public License as published by
1294+# the Free Software Foundation; either version 2 of the License, or
1295+# (at your option) any later version.
1296+#
1297+# This program is distributed in the hope that it will be useful,
1298+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1299+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1300+# GNU General Public License for more details.
1301+#
1302+# You should have received a copy of the GNU General Public License
523b37e3 1303+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1304+
1305+Introduction
1306+----------------------------------------
1307+
3c1bdaff 1308+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1309+1. abbrev. for "advanced multi-layered unification filesystem".
1310+2. abbrev. for "another unionfs".
1311+3. abbrev. for "auf das" in German which means "on the" in English.
1312+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1313+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1314+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1315+
1316+AUFS is a filesystem with features:
1317+- multi layered stackable unification filesystem, the member directory
1318+ is called as a branch.
1319+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1320+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1321+ combination.
1322+- internal "file copy-on-write".
1323+- logical deletion, whiteout.
1324+- dynamic branch manipulation, adding, deleting and changing permission.
1325+- allow bypassing aufs, user's direct branch access.
1326+- external inode number translation table and bitmap which maintains the
1327+ persistent aufs inode number.
1328+- seekable directory, including NFS readdir.
1329+- file mapping, mmap and sharing pages.
1330+- pseudo-link, hardlink over branches.
1331+- loopback mounted filesystem as a branch.
1332+- several policies to select one among multiple writable branches.
1333+- revert a single systemcall when an error occurs in aufs.
1334+- and more...
1335+
1336+
1337+Multi Layered Stackable Unification Filesystem
1338+----------------------------------------------------------------------
1339+Most people already knows what it is.
1340+It is a filesystem which unifies several directories and provides a
1341+merged single directory. When users access a file, the access will be
1342+passed/re-directed/converted (sorry, I am not sure which English word is
1343+correct) to the real file on the member filesystem. The member
1344+filesystem is called 'lower filesystem' or 'branch' and has a mode
1345+'readonly' and 'readwrite.' And the deletion for a file on the lower
1346+readonly branch is handled by creating 'whiteout' on the upper writable
1347+branch.
1348+
1349+On LKML, there have been discussions about UnionMount (Jan Blunck,
1350+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1351+different approaches to implement the merged-view.
1352+The former tries putting it into VFS, and the latter implements as a
1353+separate filesystem.
1354+(If I misunderstand about these implementations, please let me know and
1355+I shall correct it. Because it is a long time ago when I read their
1356+source files last time).
1357+
1358+UnionMount's approach will be able to small, but may be hard to share
1359+branches between several UnionMount since the whiteout in it is
1360+implemented in the inode on branch filesystem and always
1361+shared. According to Bharata's post, readdir does not seems to be
1362+finished yet.
1363+There are several missing features known in this implementations such as
1364+- for users, the inode number may change silently. eg. copy-up.
1365+- link(2) may break by copy-up.
1366+- read(2) may get an obsoleted filedata (fstat(2) too).
1367+- fcntl(F_SETLK) may be broken by copy-up.
1368+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1369+ open(O_RDWR).
1370+
7e9cd9fe
AM
1371+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1372+merged into mainline. This is another implementation of UnionMount as a
1373+separated filesystem. All the limitations and known problems which
1374+UnionMount are equally inherited to "overlay" filesystem.
1375+
1376+Unionfs has a longer history. When I started implementing a stackable
1377+filesystem (Aug 2005), it already existed. It has virtual super_block,
1378+inode, dentry and file objects and they have an array pointing lower
1379+same kind objects. After contributing many patches for Unionfs, I
1380+re-started my project AUFS (Jun 2006).
53392da6
AM
1381+
1382+In AUFS, the structure of filesystem resembles to Unionfs, but I
1383+implemented my own ideas, approaches and enhancements and it became
1384+totally different one.
1385+
1386+Comparing DM snapshot and fs based implementation
1387+- the number of bytes to be copied between devices is much smaller.
1388+- the type of filesystem must be one and only.
1389+- the fs must be writable, no readonly fs, even for the lower original
1390+ device. so the compression fs will not be usable. but if we use
1391+ loopback mount, we may address this issue.
1392+ for instance,
1393+ mount /cdrom/squashfs.img /sq
1394+ losetup /sq/ext2.img
1395+ losetup /somewhere/cow
1396+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1397+- it will be difficult (or needs more operations) to extract the
1398+ difference between the original device and COW.
1399+- DM snapshot-merge may help a lot when users try merging. in the
1400+ fs-layer union, users will use rsync(1).
1401+
7e9cd9fe
AM
1402+You may want to read my old paper "Filesystems in LiveCD"
1403+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1404+
7e9cd9fe
AM
1405+
1406+Several characters/aspects/persona of aufs
53392da6
AM
1407+----------------------------------------------------------------------
1408+
7e9cd9fe 1409+Aufs has several characters, aspects or persona.
53392da6
AM
1410+1. a filesystem, callee of VFS helper
1411+2. sub-VFS, caller of VFS helper for branches
1412+3. a virtual filesystem which maintains persistent inode number
1413+4. reader/writer of files on branches such like an application
1414+
1415+1. Callee of VFS Helper
1416+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1417+unlink(2) from an application reaches sys_unlink() kernel function and
1418+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1419+calls filesystem specific unlink operation. Actually aufs implements the
1420+unlink operation but it behaves like a redirector.
1421+
1422+2. Caller of VFS Helper for Branches
1423+aufs_unlink() passes the unlink request to the branch filesystem as if
1424+it were called from VFS. So the called unlink operation of the branch
1425+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1426+every necessary pre/post operation for the branch filesystem.
1427+- acquire the lock for the parent dir on a branch
1428+- lookup in a branch
1429+- revalidate dentry on a branch
1430+- mnt_want_write() for a branch
1431+- vfs_unlink() for a branch
1432+- mnt_drop_write() for a branch
1433+- release the lock on a branch
1434+
1435+3. Persistent Inode Number
1436+One of the most important issue for a filesystem is to maintain inode
1437+numbers. This is particularly important to support exporting a
1438+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1439+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1440+keep and maintain the inode numbers. It may be a large space and may not
1441+suit to keep in memory. Aufs rents some space from its first writable
1442+branch filesystem (by default) and creates file(s) on it. These files
1443+are created by aufs internally and removed soon (currently) keeping
1444+opened.
53392da6
AM
1445+Note: Because these files are removed, they are totally gone after
1446+ unmounting aufs. It means the inode numbers are not persistent
1447+ across unmount or reboot. I have a plan to make them really
1448+ persistent which will be important for aufs on NFS server.
1449+
1450+4. Read/Write Files Internally (copy-on-write)
1451+Because a branch can be readonly, when you write a file on it, aufs will
1452+"copy-up" it to the upper writable branch internally. And then write the
1453+originally requested thing to the file. Generally kernel doesn't
1454+open/read/write file actively. In aufs, even a single write may cause a
1455+internal "file copy". This behaviour is very similar to cp(1) command.
1456+
1457+Some people may think it is better to pass such work to user space
1458+helper, instead of doing in kernel space. Actually I am still thinking
1459+about it. But currently I have implemented it in kernel space.
1460diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1461--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1462+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 1463@@ -0,0 +1,258 @@
53392da6 1464+
b00004a5 1465+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1466+#
1467+# This program is free software; you can redistribute it and/or modify
1468+# it under the terms of the GNU General Public License as published by
1469+# the Free Software Foundation; either version 2 of the License, or
1470+# (at your option) any later version.
1471+#
1472+# This program is distributed in the hope that it will be useful,
1473+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1474+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1475+# GNU General Public License for more details.
1476+#
1477+# You should have received a copy of the GNU General Public License
523b37e3 1478+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1479+
1480+Basic Aufs Internal Structure
1481+
1482+Superblock/Inode/Dentry/File Objects
1483+----------------------------------------------------------------------
1484+As like an ordinary filesystem, aufs has its own
1485+superblock/inode/dentry/file objects. All these objects have a
1486+dynamically allocated array and store the same kind of pointers to the
1487+lower filesystem, branch.
1488+For example, when you build a union with one readwrite branch and one
1489+readonly, mounted /au, /rw and /ro respectively.
1490+- /au = /rw + /ro
1491+- /ro/fileA exists but /rw/fileA
1492+
1493+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1494+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1495+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1496+- [1] = /ro/fileA
1497+
1498+This style of an array is essentially same to the aufs
1499+superblock/inode/dentry/file objects.
1500+
1501+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1502+branches dynamically, these objects has its own generation. When
1503+branches are changed, the generation in aufs superblock is
1504+incremented. And a generation in other object are compared when it is
1505+accessed. When a generation in other objects are obsoleted, aufs
1506+refreshes the internal array.
53392da6
AM
1507+
1508+
1509+Superblock
1510+----------------------------------------------------------------------
1511+Additionally aufs superblock has some data for policies to select one
1512+among multiple writable branches, XIB files, pseudo-links and kobject.
1513+See below in detail.
7e9cd9fe
AM
1514+About the policies which supports copy-down a directory, see
1515+wbr_policy.txt too.
53392da6
AM
1516+
1517+
1518+Branch and XINO(External Inode Number Translation Table)
1519+----------------------------------------------------------------------
1520+Every branch has its own xino (external inode number translation table)
1521+file. The xino file is created and unlinked by aufs internally. When two
1522+members of a union exist on the same filesystem, they share the single
1523+xino file.
1524+The struct of a xino file is simple, just a sequence of aufs inode
1525+numbers which is indexed by the lower inode number.
1526+In the above sample, assume the inode number of /ro/fileA is i111 and
1527+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1528+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1529+
1530+When the inode numbers are not contiguous, the xino file will be sparse
1531+which has a hole in it and doesn't consume as much disk space as it
1532+might appear. If your branch filesystem consumes disk space for such
1533+holes, then you should specify 'xino=' option at mounting aufs.
1534+
7e9cd9fe
AM
1535+Aufs has a mount option to free the disk blocks for such holes in XINO
1536+files on tmpfs or ramdisk. But it is not so effective actually. If you
1537+meet a problem of disk shortage due to XINO files, then you should try
1538+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1539+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1540+the holes in XINO files.
1541+
53392da6 1542+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1543+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1544+whiteout-ed doubly, so that users will never see their names in aufs
1545+hierarchy.
7e9cd9fe 1546+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1547+2. a directory to store a pseudo-link.
7e9cd9fe 1548+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1549+
1550+1. Whiteout Base
1551+ When you remove a file on a readonly branch, aufs handles it as a
1552+ logical deletion and creates a whiteout on the upper writable branch
1553+ as a hardlink of this file in order not to consume inode on the
1554+ writable branch.
1555+2. Pseudo-link Dir
1556+ See below, Pseudo-link.
1557+3. Step-Parent Dir
1558+ When "fileC" exists on the lower readonly branch only and it is
1559+ opened and removed with its parent dir, and then user writes
1560+ something into it, then aufs copies-up fileC to this
1561+ directory. Because there is no other dir to store fileC. After
1562+ creating a file under this dir, the file is unlinked.
1563+
1564+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1565+dynamically, a branch has its own id. When the branch order changes,
1566+aufs finds the new index by searching the branch id.
53392da6
AM
1567+
1568+
1569+Pseudo-link
1570+----------------------------------------------------------------------
1571+Assume "fileA" exists on the lower readonly branch only and it is
1572+hardlinked to "fileB" on the branch. When you write something to fileA,
1573+aufs copies-up it to the upper writable branch. Additionally aufs
1574+creates a hardlink under the Pseudo-link Directory of the writable
1575+branch. The inode of a pseudo-link is kept in aufs super_block as a
1576+simple list. If fileB is read after unlinking fileA, aufs returns
1577+filedata from the pseudo-link instead of the lower readonly
1578+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1579+inode number by xino (see above) is essentially necessary.
53392da6
AM
1580+
1581+All the hardlinks under the Pseudo-link Directory of the writable branch
1582+should be restored in a proper location later. Aufs provides a utility
1583+to do this. The userspace helpers executed at remounting and unmounting
1584+aufs by default.
1585+During this utility is running, it puts aufs into the pseudo-link
1586+maintenance mode. In this mode, only the process which began the
1587+maintenance mode (and its child processes) is allowed to operate in
1588+aufs. Some other processes which are not related to the pseudo-link will
1589+be allowed to run too, but the rest have to return an error or wait
1590+until the maintenance mode ends. If a process already acquires an inode
1591+mutex (in VFS), it has to return an error.
1592+
1593+
1594+XIB(external inode number bitmap)
1595+----------------------------------------------------------------------
1596+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1597+bitmap in a superblock object. It is also an internal file such like a
1598+xino file.
53392da6
AM
1599+It is a simple bitmap to mark whether the aufs inode number is in-use or
1600+not.
1601+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1602+
7e9cd9fe 1603+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1604+reduce the number of consumed disk blocks for these files.
1605+
1606+
1607+Virtual or Vertical Dir, and Readdir in Userspace
1608+----------------------------------------------------------------------
1609+In order to support multiple layers (branches), aufs readdir operation
1610+constructs a virtual dir block on memory. For readdir, aufs calls
1611+vfs_readdir() internally for each dir on branches, merges their entries
1612+with eliminating the whiteout-ed ones, and sets it to file (dir)
1613+object. So the file object has its entry list until it is closed. The
1614+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1615+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1616+
1617+The dynamically allocated memory block for the name of entries has a
1618+unit of 512 bytes (by default) and stores the names contiguously (no
1619+padding). Another block for each entry is handled by kmem_cache too.
1620+During building dir blocks, aufs creates hash list and judging whether
1621+the entry is whiteouted by its upper branch or already listed.
1622+The merged result is cached in the corresponding inode object and
1623+maintained by a customizable life-time option.
1624+
1625+Some people may call it can be a security hole or invite DoS attack
1626+since the opened and once readdir-ed dir (file object) holds its entry
1627+list and becomes a pressure for system memory. But I'd say it is similar
1628+to files under /proc or /sys. The virtual files in them also holds a
1629+memory page (generally) while they are opened. When an idea to reduce
1630+memory for them is introduced, it will be applied to aufs too.
1631+For those who really hate this situation, I've developed readdir(3)
1632+library which operates this merging in userspace. You just need to set
1633+LD_PRELOAD environment variable, and aufs will not consume no memory in
1634+kernel space for readdir(3).
1635+
1636+
1637+Workqueue
1638+----------------------------------------------------------------------
1639+Aufs sometimes requires privilege access to a branch. For instance,
1640+in copy-up/down operation. When a user process is going to make changes
1641+to a file which exists in the lower readonly branch only, and the mode
1642+of one of ancestor directories may not be writable by a user
1643+process. Here aufs copy-up the file with its ancestors and they may
1644+require privilege to set its owner/group/mode/etc.
1645+This is a typical case of a application character of aufs (see
1646+Introduction).
1647+
1648+Aufs uses workqueue synchronously for this case. It creates its own
1649+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1650+passes the request to call mkdir or write (for example), and wait for
1651+its completion. This approach solves a problem of a signal handler
1652+simply.
1653+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1654+process, then the process may receive the unexpected SIGXFSZ or other
1655+signals.
53392da6
AM
1656+
1657+Also aufs uses the system global workqueue ("events" kernel thread) too
1658+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1659+whiteout base and etc. This is unrelated to a privilege.
1660+Most of aufs operation tries acquiring a rw_semaphore for aufs
1661+superblock at the beginning, at the same time waits for the completion
1662+of all queued asynchronous tasks.
1663+
1664+
1665+Whiteout
1666+----------------------------------------------------------------------
1667+The whiteout in aufs is very similar to Unionfs's. That is represented
1668+by its filename. UnionMount takes an approach of a file mode, but I am
1669+afraid several utilities (find(1) or something) will have to support it.
1670+
1671+Basically the whiteout represents "logical deletion" which stops aufs to
1672+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1673+further lookup.
53392da6
AM
1674+
1675+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1676+In order to make several functions in a single systemcall to be
1677+revertible, aufs adopts an approach to rename a directory to a temporary
1678+unique whiteouted name.
1679+For example, in rename(2) dir where the target dir already existed, aufs
1680+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1681+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1682+update the attributes, etc). If an error happens in these actions, aufs
1683+simply renames the whiteouted name back and returns an error. If all are
1684+succeeded, aufs registers a function to remove the whiteouted unique
1685+temporary name completely and asynchronously to the system global
1686+workqueue.
1687+
1688+
1689+Copy-up
1690+----------------------------------------------------------------------
1691+It is a well-known feature or concept.
1692+When user modifies a file on a readonly branch, aufs operate "copy-up"
1693+internally and makes change to the new file on the upper writable branch.
1694+When the trigger systemcall does not update the timestamps of the parent
1695+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1696+
1697+
1698+Move-down (aufs3.9 and later)
1699+----------------------------------------------------------------------
1700+"Copy-up" is one of the essential feature in aufs. It copies a file from
1701+the lower readonly branch to the upper writable branch when a user
1702+changes something about the file.
1703+"Move-down" is an opposite action of copy-up. Basically this action is
1704+ran manually instead of automatically and internally.
076b876e
AM
1705+For desgin and implementation, aufs has to consider these issues.
1706+- whiteout for the file may exist on the lower branch.
1707+- ancestor directories may not exist on the lower branch.
1708+- diropq for the ancestor directories may exist on the upper branch.
1709+- free space on the lower branch will reduce.
1710+- another access to the file may happen during moving-down, including
7e9cd9fe 1711+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1712+- the file should not be hard-linked nor pseudo-linked. they should be
1713+ handled by auplink utility later.
c2b27bf2
AM
1714+
1715+Sometimes users want to move-down a file from the upper writable branch
1716+to the lower readonly or writable branch. For instance,
1717+- the free space of the upper writable branch is going to run out.
1718+- create a new intermediate branch between the upper and lower branch.
1719+- etc.
1720+
1721+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e
AM
1722diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1723--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1724+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2018-04-15 08:49:13.394483860 +0200
b912730e
AM
1725@@ -0,0 +1,85 @@
1726+
b00004a5 1727+# Copyright (C) 2015-2018 Junjiro R. Okajima
b912730e
AM
1728+#
1729+# This program is free software; you can redistribute it and/or modify
1730+# it under the terms of the GNU General Public License as published by
1731+# the Free Software Foundation; either version 2 of the License, or
1732+# (at your option) any later version.
1733+#
1734+# This program is distributed in the hope that it will be useful,
1735+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1736+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1737+# GNU General Public License for more details.
1738+#
1739+# You should have received a copy of the GNU General Public License
1740+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1741+
1742+Support for a branch who has its ->atomic_open()
1743+----------------------------------------------------------------------
1744+The filesystems who implement its ->atomic_open() are not majority. For
1745+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1746+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1747+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1748+sure whether all filesystems who have ->atomic_open() behave like this,
1749+but NFSv4 surely returns the error.
1750+
1751+In order to support ->atomic_open() for aufs, there are a few
1752+approaches.
1753+
1754+A. Introduce aufs_atomic_open()
1755+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1756+ branch fs.
1757+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1758+ an aufs user Pip Cet's approach
1759+ - calls aufs_create(), VFS finish_open() and notify_change().
1760+ - pass fake-mode to finish_open(), and then correct the mode by
1761+ notify_change().
1762+C. Extend aufs_open() to call branch fs's ->atomic_open()
1763+ - no aufs_atomic_open().
1764+ - aufs_lookup() registers the TID to an aufs internal object.
1765+ - aufs_create() does nothing when the matching TID is registered, but
1766+ registers the mode.
1767+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1768+ TID is registered.
1769+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1770+ credential
1771+ - no aufs_atomic_open().
1772+ - aufs_create() registers the TID to an internal object. this info
1773+ represents "this process created this file just now."
1774+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1775+ registered TID and re-try open() with superuser's credential.
1776+
1777+Pros and cons for each approach.
1778+
1779+A.
1780+ - straightforward but highly depends upon VFS internal.
1781+ - the atomic behavaiour is kept.
1782+ - some of parameters such as nameidata are hard to reproduce for
1783+ branch fs.
1784+ - large overhead.
1785+B.
1786+ - easy to implement.
1787+ - the atomic behavaiour is lost.
1788+C.
1789+ - the atomic behavaiour is kept.
1790+ - dirty and tricky.
1791+ - VFS checks whether the file is created correctly after calling
1792+ ->create(), which means this approach doesn't work.
1793+D.
1794+ - easy to implement.
1795+ - the atomic behavaiour is lost.
1796+ - to open a file with superuser's credential and give it to a user
1797+ process is a bad idea, since the file object keeps the credential
1798+ in it. It may affect LSM or something. This approach doesn't work
1799+ either.
1800+
1801+The approach A is ideal, but it hard to implement. So here is a
1802+variation of A, which is to be implemented.
1803+
1804+A-1. Introduce aufs_atomic_open()
1805+ - calls branch fs ->atomic_open() if exists. otherwise calls
1806+ vfs_create() and finish_open().
1807+ - the demerit is that the several checks after branch fs
1808+ ->atomic_open() are lost. in the ordinary case, the checks are
1809+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1810+ be implemented in aufs, but not all I am afraid.
53392da6
AM
1811diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1812--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1813+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 1814@@ -0,0 +1,113 @@
53392da6 1815+
b00004a5 1816+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1817+#
1818+# This program is free software; you can redistribute it and/or modify
1819+# it under the terms of the GNU General Public License as published by
1820+# the Free Software Foundation; either version 2 of the License, or
1821+# (at your option) any later version.
1822+#
1823+# This program is distributed in the hope that it will be useful,
1824+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1825+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1826+# GNU General Public License for more details.
1827+#
1828+# You should have received a copy of the GNU General Public License
523b37e3 1829+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1830+
1831+Lookup in a Branch
1832+----------------------------------------------------------------------
1833+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1834+lookup for branches as VFS does. It may be a heavy work. But almost all
1835+lookup operation in aufs is the simplest case, ie. lookup only an entry
1836+directly connected to its parent. Digging down the directory hierarchy
1837+is unnecessary. VFS has a function lookup_one_len() for that use, and
1838+aufs calls it.
1839+
1840+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1841+->d_revalidate(), also aufs forces the hardest revalidate tests for
1842+them.
1843+For d_revalidate, aufs implements three levels of revalidate tests. See
1844+"Revalidate Dentry and UDBA" in detail.
1845+
1846+
076b876e
AM
1847+Test Only the Highest One for the Directory Permission (dirperm1 option)
1848+----------------------------------------------------------------------
1849+Let's try case study.
1850+- aufs has two branches, upper readwrite and lower readonly.
1851+ /au = /rw + /ro
1852+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1853+- user invoked "chmod a+rx /au/dirA"
1854+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1855+ permission bits are set to world readable.
076b876e
AM
1856+- then "/au/dirA" becomes world readable?
1857+
1858+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1859+or it may be a natively readonly filesystem. If aufs respects the lower
1860+branch, it should not respond readdir request from other users. But user
1861+allowed it by chmod. Should really aufs rejects showing the entries
1862+under /ro/dirA?
1863+
7e9cd9fe
AM
1864+To be honest, I don't have a good solution for this case. So aufs
1865+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1866+users.
076b876e
AM
1867+When dirperm1 is specified, aufs checks only the highest one for the
1868+directory permission, and shows the entries. Otherwise, as usual, checks
1869+every dir existing on all branches and rejects the request.
1870+
1871+As a side effect, dirperm1 option improves the performance of aufs
1872+because the number of permission check is reduced when the number of
1873+branch is many.
1874+
1875+
53392da6
AM
1876+Revalidate Dentry and UDBA (User's Direct Branch Access)
1877+----------------------------------------------------------------------
1878+Generally VFS helpers re-validate a dentry as a part of lookup.
1879+0. digging down the directory hierarchy.
1880+1. lock the parent dir by its i_mutex.
1881+2. lookup the final (child) entry.
1882+3. revalidate it.
1883+4. call the actual operation (create, unlink, etc.)
1884+5. unlock the parent dir
1885+
1886+If the filesystem implements its ->d_revalidate() (step 3), then it is
1887+called. Actually aufs implements it and checks the dentry on a branch is
1888+still valid.
1889+But it is not enough. Because aufs has to release the lock for the
1890+parent dir on a branch at the end of ->lookup() (step 2) and
1891+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1892+held by VFS.
1893+If the file on a branch is changed directly, eg. bypassing aufs, after
1894+aufs released the lock, then the subsequent operation may cause
1895+something unpleasant result.
1896+
1897+This situation is a result of VFS architecture, ->lookup() and
1898+->d_revalidate() is separated. But I never say it is wrong. It is a good
1899+design from VFS's point of view. It is just not suitable for sub-VFS
1900+character in aufs.
1901+
1902+Aufs supports such case by three level of revalidation which is
1903+selectable by user.
1904+1. Simple Revalidate
1905+ Addition to the native flow in VFS's, confirm the child-parent
1906+ relationship on the branch just after locking the parent dir on the
1907+ branch in the "actual operation" (step 4). When this validation
1908+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1909+ checks the validation of the dentry on branches.
1910+2. Monitor Changes Internally by Inotify/Fsnotify
1911+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1912+ the dentry on the branch, and returns EBUSY if it finds different
1913+ dentry.
1914+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1915+ during it is in cache. When the event is notified, aufs registers a
1916+ function to kernel 'events' thread by schedule_work(). And the
1917+ function sets some special status to the cached aufs dentry and inode
1918+ private data. If they are not cached, then aufs has nothing to
1919+ do. When the same file is accessed through aufs (step 0-3) later,
1920+ aufs will detect the status and refresh all necessary data.
1921+ In this mode, aufs has to ignore the event which is fired by aufs
1922+ itself.
1923+3. No Extra Validation
1924+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1925+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1926+ aufs performance when system surely hide the aufs branches from user,
1927+ by over-mounting something (or another method).
1928diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1929--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 1930+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 1931@@ -0,0 +1,74 @@
53392da6 1932+
b00004a5 1933+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
1934+#
1935+# This program is free software; you can redistribute it and/or modify
1936+# it under the terms of the GNU General Public License as published by
1937+# the Free Software Foundation; either version 2 of the License, or
1938+# (at your option) any later version.
1939+#
1940+# This program is distributed in the hope that it will be useful,
1941+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1942+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1943+# GNU General Public License for more details.
1944+#
1945+# You should have received a copy of the GNU General Public License
523b37e3 1946+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1947+
1948+Branch Manipulation
1949+
1950+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1951+and changing its permission/attribute, there are a lot of works to do.
1952+
1953+
1954+Add a Branch
1955+----------------------------------------------------------------------
1956+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1957+ mount, and its various attributes.
53392da6
AM
1958+o Initialize the xino file and whiteout bases if necessary.
1959+ See struct.txt.
1960+
1961+o Check the owner/group/mode of the directory
1962+ When the owner/group/mode of the adding directory differs from the
1963+ existing branch, aufs issues a warning because it may impose a
1964+ security risk.
1965+ For example, when a upper writable branch has a world writable empty
1966+ top directory, a malicious user can create any files on the writable
1967+ branch directly, like copy-up and modify manually. If something like
1968+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1969+ writable branch, and the writable branch is world-writable, then a
1970+ malicious guy may create /etc/passwd on the writable branch directly
1971+ and the infected file will be valid in aufs.
7e9cd9fe 1972+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1973+ producing a warning.
1974+
1975+
1976+Delete a Branch
1977+----------------------------------------------------------------------
1978+o Confirm the deleting branch is not busy
1979+ To be general, there is one merit to adopt "remount" interface to
1980+ manipulate branches. It is to discard caches. At deleting a branch,
1981+ aufs checks the still cached (and connected) dentries and inodes. If
1982+ there are any, then they are all in-use. An inode without its
1983+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1984+
1985+ For the cached one, aufs checks whether the same named entry exists on
1986+ other branches.
1987+ If the cached one is a directory, because aufs provides a merged view
1988+ to users, as long as one dir is left on any branch aufs can show the
1989+ dir to users. In this case, the branch can be removed from aufs.
1990+ Otherwise aufs rejects deleting the branch.
1991+
1992+ If any file on the deleting branch is opened by aufs, then aufs
1993+ rejects deleting.
1994+
1995+
1996+Modify the Permission of a Branch
1997+----------------------------------------------------------------------
1998+o Re-initialize or remove the xino file and whiteout bases if necessary.
1999+ See struct.txt.
2000+
2001+o rw --> ro: Confirm the modifying branch is not busy
2002+ Aufs rejects the request if any of these conditions are true.
2003+ - a file on the branch is mmap-ed.
2004+ - a regular file on the branch is opened for write and there is no
2005+ same named entry on the upper branch.
2006diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
2007--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2008+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2018-04-15 08:49:13.394483860 +0200
523b37e3 2009@@ -0,0 +1,64 @@
53392da6 2010+
b00004a5 2011+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
2012+#
2013+# This program is free software; you can redistribute it and/or modify
2014+# it under the terms of the GNU General Public License as published by
2015+# the Free Software Foundation; either version 2 of the License, or
2016+# (at your option) any later version.
2017+#
2018+# This program is distributed in the hope that it will be useful,
2019+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2020+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2021+# GNU General Public License for more details.
2022+#
2023+# You should have received a copy of the GNU General Public License
523b37e3 2024+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2025+
2026+Policies to Select One among Multiple Writable Branches
2027+----------------------------------------------------------------------
2028+When the number of writable branch is more than one, aufs has to decide
2029+the target branch for file creation or copy-up. By default, the highest
2030+writable branch which has the parent (or ancestor) dir of the target
2031+file is chosen (top-down-parent policy).
2032+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
2033+writable branch, for file creation several policies, round-robin,
2034+most-free-space, and other policies. For copy-up, top-down-parent,
2035+bottom-up-parent, bottom-up and others.
53392da6
AM
2036+
2037+As expected, the round-robin policy selects the branch in circular. When
2038+you have two writable branches and creates 10 new files, 5 files will be
2039+created for each branch. mkdir(2) systemcall is an exception. When you
2040+create 10 new directories, all will be created on the same branch.
2041+And the most-free-space policy selects the one which has most free
2042+space among the writable branches. The amount of free space will be
2043+checked by aufs internally, and users can specify its time interval.
2044+
2045+The policies for copy-up is more simple,
2046+top-down-parent is equivalent to the same named on in create policy,
2047+bottom-up-parent selects the writable branch where the parent dir
2048+exists and the nearest upper one from the copyup-source,
2049+bottom-up selects the nearest upper writable branch from the
2050+copyup-source, regardless the existence of the parent dir.
2051+
2052+There are some rules or exceptions to apply these policies.
2053+- If there is a readonly branch above the policy-selected branch and
2054+ the parent dir is marked as opaque (a variation of whiteout), or the
2055+ target (creating) file is whiteout-ed on the upper readonly branch,
2056+ then the result of the policy is ignored and the target file will be
2057+ created on the nearest upper writable branch than the readonly branch.
2058+- If there is a writable branch above the policy-selected branch and
2059+ the parent dir is marked as opaque or the target file is whiteouted
2060+ on the branch, then the result of the policy is ignored and the target
2061+ file will be created on the highest one among the upper writable
2062+ branches who has diropq or whiteout. In case of whiteout, aufs removes
2063+ it as usual.
2064+- link(2) and rename(2) systemcalls are exceptions in every policy.
2065+ They try selecting the branch where the source exists as possible
2066+ since copyup a large file will take long time. If it can't be,
2067+ ie. the branch where the source exists is readonly, then they will
2068+ follow the copyup policy.
2069+- There is an exception for rename(2) when the target exists.
2070+ If the rename target exists, aufs compares the index of the branches
2071+ where the source and the target exists and selects the higher
2072+ one. If the selected branch is readonly, then aufs follows the
2073+ copyup policy.
8b6a4947
AM
2074diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
2075--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
b00004a5 2076+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2018-04-15 08:49:13.394483860 +0200
8b6a4947
AM
2077@@ -0,0 +1,31 @@
2078+
2079+// to view this graph, run dot(1) command in GRAPHVIZ.
2080+
2081+digraph G {
2082+node [shape=box];
2083+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
2084+
2085+node [shape=oval];
2086+
2087+aufs_rename -> whinfo [label="store/remove"];
2088+
2089+node [shape=oval];
2090+inode_list [label="h_inum list in branch\ncache"];
2091+
2092+node [shape=box];
2093+whinode [label="h_inum list file"];
2094+
2095+node [shape=oval];
2096+brmgmt [label="br_add/del/mod/umount"];
2097+
2098+brmgmt -> inode_list [label="create/remove"];
2099+brmgmt -> whinode [label="load/store"];
2100+
2101+inode_list -> whinode [style=dashed,dir=both];
2102+
2103+aufs_rename -> inode_list [label="add/del"];
2104+
2105+aufs_lookup -> inode_list [label="search"];
2106+
2107+aufs_lookup -> whinfo [label="load/remove"];
2108+}
2109diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
2110--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2111+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2018-04-15 08:49:13.394483860 +0200
8b6a4947
AM
2112@@ -0,0 +1,102 @@
2113+
b00004a5 2114+# Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
2115+#
2116+# This program is free software; you can redistribute it and/or modify
2117+# it under the terms of the GNU General Public License as published by
2118+# the Free Software Foundation; either version 2 of the License, or
2119+# (at your option) any later version.
2120+#
2121+# This program is distributed in the hope that it will be useful,
2122+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2123+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2124+# GNU General Public License for more details.
2125+#
2126+# You should have received a copy of the GNU General Public License
2127+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2128+
2129+Special handling for renaming a directory (DIRREN)
2130+----------------------------------------------------------------------
2131+First, let's assume we have a simple usecase.
2132+
2133+- /u = /rw + /ro
2134+- /rw/dirA exists
2135+- /ro/dirA and /ro/dirA/file exist too
2136+- there is no dirB on both branches
2137+- a user issues rename("dirA", "dirB")
2138+
2139+Now, what should aufs behave against this rename(2)?
2140+There are a few possible cases.
2141+
2142+A. returns EROFS.
2143+ since dirA exists on a readonly branch which cannot be renamed.
2144+B. returns EXDEV.
2145+ it is possible to copy-up dirA (only the dir itself), but the child
2146+ entries ("file" in this case) should not be. it must be a bad
2147+ approach to copy-up recursively.
2148+C. returns a success.
2149+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
2150+ is a violation of aufs' policy.
2151+D. construct an extra information which indicates that /ro/dirA should
2152+ be handled as the name of dirB.
2153+ overlayfs has a similar feature called REDIRECT.
2154+
2155+Until now, aufs implements the case B only which returns EXDEV, and
2156+expects the userspace application behaves like mv(1) which tries
2157+issueing rename(2) recursively.
2158+
2159+A new aufs feature called DIRREN is introduced which implements the case
2160+D. There are several "extra information" added.
2161+
2162+1. detailed info per renamed directory
2163+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2164+2. the inode-number list of directories on a branch
2165+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
2166+
2167+The filename of "detailed info per directory" represents the lower
2168+branch, and its format is
2169+- a type of the branch id
2170+ one of these.
2171+ + uuid (not implemented yet)
2172+ + fsid
2173+ + dev
2174+- the inode-number of the branch root dir
2175+
2176+And it contains these info in a single regular file.
2177+- magic number
2178+- branch's inode-number of the logically renamed dir
2179+- the name of the before-renamed dir
2180+
2181+The "detailed info per directory" file is created in aufs rename(2), and
2182+loaded in any lookup.
2183+The info is considered in lookup for the matching case only. Here
2184+"matching" means that the root of branch (in the info filename) is same
2185+to the current looking-up branch. After looking-up the before-renamed
2186+name, the inode-number is compared. And the matched dentry is used.
2187+
2188+The "inode-number list of directories" is a regular file which contains
2189+simply the inode-numbers on the branch. The file is created or updated
2190+in removing the branch, and loaded in adding the branch. Its lifetime is
2191+equal to the branch.
2192+The list is refered in lookup, and when the current target inode is
2193+found in the list, the aufs tries loading the "detailed info per
2194+directory" and get the changed and valid name of the dir.
2195+
2196+Theoretically these "extra informaiton" may be able to be put into XATTR
2197+in the dir inode. But aufs doesn't choose this way because
2198+1. XATTR may not be supported by the branch (or its configuration)
2199+2. XATTR may have its size limit.
2200+3. XATTR may be less easy to convert than a regular file, when the
2201+ format of the info is changed in the future.
2202+At the same time, I agree that the regular file approach is much slower
2203+than XATTR approach. So, in the future, aufs may take the XATTR or other
2204+better approach.
2205+
2206+This DIRREN feature is enabled by aufs configuration, and is activated
2207+by a new mount option.
2208+
2209+For the more complicated case, there is a work with UDBA option, which
2210+is to dected the direct access to the branches (by-passing aufs) and to
2211+maintain the cashes in aufs. Since a single cached aufs dentry may
2212+contains two names, before- and after-rename, the name comparision in
2213+UDBA handler may not work correctly. In this case, the behaviour will be
2214+equivalen to udba=reval case.
076b876e
AM
2215diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2216--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2217+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2018-04-15 08:49:13.394483860 +0200
076b876e
AM
2218@@ -0,0 +1,120 @@
2219+
b00004a5 2220+# Copyright (C) 2011-2018 Junjiro R. Okajima
076b876e
AM
2221+#
2222+# This program is free software; you can redistribute it and/or modify
2223+# it under the terms of the GNU General Public License as published by
2224+# the Free Software Foundation; either version 2 of the License, or
2225+# (at your option) any later version.
2226+#
2227+# This program is distributed in the hope that it will be useful,
2228+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2229+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2230+# GNU General Public License for more details.
2231+#
2232+# You should have received a copy of the GNU General Public License
2233+# along with this program; if not, write to the Free Software
2234+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2235+
2236+
2237+File-based Hierarchical Storage Management (FHSM)
2238+----------------------------------------------------------------------
2239+Hierarchical Storage Management (or HSM) is a well-known feature in the
2240+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 2241+writable branches, based upon the principle of "Colder, the Lower".
076b876e 2242+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 2243+that the position in the order of the stacked branches vertically.
076b876e
AM
2244+These multiple writable branches are prioritized, ie. the topmost one
2245+should be the fastest drive and be used heavily.
2246+
2247+o Characters in aufs FHSM story
2248+- aufs itself and a new branch attribute.
2249+- a new ioctl interface to move-down and to establish a connection with
2250+ the daemon ("move-down" is a converse of "copy-up").
2251+- userspace tool and daemon.
2252+
2253+The userspace daemon establishes a connection with aufs and waits for
2254+the notification. The notified information is very similar to struct
2255+statfs containing the number of consumed blocks and inodes.
2256+When the consumed blocks/inodes of a branch exceeds the user-specified
2257+upper watermark, the daemon activates its move-down process until the
2258+consumed blocks/inodes reaches the user-specified lower watermark.
2259+
2260+The actual move-down is done by aufs based upon the request from
2261+user-space since we need to maintain the inode number and the internal
2262+pointer arrays in aufs.
2263+
2264+Currently aufs FHSM handles the regular files only. Additionally they
2265+must not be hard-linked nor pseudo-linked.
2266+
2267+
2268+o Cowork of aufs and the user-space daemon
2269+ During the userspace daemon established the connection, aufs sends a
2270+ small notification to it whenever aufs writes something into the
2271+ writable branch. But it may cost high since aufs issues statfs(2)
2272+ internally. So user can specify a new option to cache the
2273+ info. Actually the notification is controlled by these factors.
2274+ + the specified cache time.
2275+ + classified as "force" by aufs internally.
2276+ Until the specified time expires, aufs doesn't send the info
2277+ except the forced cases. When aufs decide forcing, the info is always
2278+ notified to userspace.
2279+ For example, the number of free inodes is generally large enough and
2280+ the shortage of it happens rarely. So aufs doesn't force the
2281+ notification when creating a new file, directory and others. This is
2282+ the typical case which aufs doesn't force.
2283+ When aufs writes the actual filedata and the files consumes any of new
2284+ blocks, the aufs forces notifying.
2285+
2286+
2287+o Interfaces in aufs
2288+- New branch attribute.
2289+ + fhsm
2290+ Specifies that the branch is managed by FHSM feature. In other word,
2291+ participant in the FHSM.
2292+ When nofhsm is set to the branch, it will not be the source/target
2293+ branch of the move-down operation. This attribute is set
2294+ independently from coo and moo attributes, and if you want full
2295+ FHSM, you should specify them as well.
2296+- New mount option.
2297+ + fhsm_sec
2298+ Specifies a second to suppress many less important info to be
2299+ notified.
2300+- New ioctl.
2301+ + AUFS_CTL_FHSM_FD
2302+ create a new file descriptor which userspace can read the notification
2303+ (a subset of struct statfs) from aufs.
2304+- Module parameter 'brs'
2305+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2306+ be set.
2307+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2308+ When there are two or more branches with fhsm attributes,
2309+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2310+ terminates it. As a result of remounting and branch-manipulation, the
2311+ number of branches with fhsm attribute can be one. In this case,
2312+ /sbin/mount.aufs will terminate the user-space daemon.
2313+
2314+
2315+Finally the operation is done as these steps in kernel-space.
2316+- make sure that,
2317+ + no one else is using the file.
2318+ + the file is not hard-linked.
2319+ + the file is not pseudo-linked.
2320+ + the file is a regular file.
2321+ + the parent dir is not opaqued.
2322+- find the target writable branch.
2323+- make sure the file is not whiteout-ed by the upper (than the target)
2324+ branch.
2325+- make the parent dir on the target branch.
2326+- mutex lock the inode on the branch.
2327+- unlink the whiteout on the target branch (if exists).
2328+- lookup and create the whiteout-ed temporary name on the target branch.
2329+- copy the file as the whiteout-ed temporary name on the target branch.
2330+- rename the whiteout-ed temporary name to the original name.
2331+- unlink the file on the source branch.
2332+- maintain the internal pointer array and the external inode number
2333+ table (XINO).
2334+- maintain the timestamps and other attributes of the parent dir and the
2335+ file.
2336+
2337+And of course, in every step, an error may happen. So the operation
2338+should restore the original file state after an error happens.
53392da6
AM
2339diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2340--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2341+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2018-04-15 08:49:13.394483860 +0200
b912730e 2342@@ -0,0 +1,72 @@
53392da6 2343+
b00004a5 2344+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
2345+#
2346+# This program is free software; you can redistribute it and/or modify
2347+# it under the terms of the GNU General Public License as published by
2348+# the Free Software Foundation; either version 2 of the License, or
2349+# (at your option) any later version.
2350+#
2351+# This program is distributed in the hope that it will be useful,
2352+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2353+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2354+# GNU General Public License for more details.
2355+#
2356+# You should have received a copy of the GNU General Public License
523b37e3 2357+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2358+
2359+mmap(2) -- File Memory Mapping
2360+----------------------------------------------------------------------
2361+In aufs, the file-mapped pages are handled by a branch fs directly, no
2362+interaction with aufs. It means aufs_mmap() calls the branch fs's
2363+->mmap().
2364+This approach is simple and good, but there is one problem.
7e9cd9fe 2365+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2366+device and inode number), and the printed path will be the path on the
2367+branch fs's instead of virtual aufs's.
2368+This is not a problem in most cases, but some utilities lsof(1) (and its
2369+user) may expect the path on aufs.
2370+
2371+To address this issue, aufs adds a new member called vm_prfile in struct
2372+vm_area_struct (and struct vm_region). The original vm_file points to
2373+the file on the branch fs in order to handle everything correctly as
2374+usual. The new vm_prfile points to a virtual file in aufs, and the
2375+show-functions in procfs refers to vm_prfile if it is set.
2376+Also we need to maintain several other places where touching vm_file
2377+such like
2378+- fork()/clone() copies vma and the reference count of vm_file is
2379+ incremented.
2380+- merging vma maintains the ref count too.
2381+
7e9cd9fe 2382+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2383+leaves all behaviour around f_mapping unchanged. This is surely an
2384+advantage.
2385+Actually aufs had adopted another complicated approach which calls
2386+generic_file_mmap() and handles struct vm_operations_struct. In this
2387+approach, aufs met a hard problem and I could not solve it without
2388+switching the approach.
b912730e
AM
2389+
2390+There may be one more another approach which is
2391+- bind-mount the branch-root onto the aufs-root internally
2392+- grab the new vfsmount (ie. struct mount)
2393+- lazy-umount the branch-root internally
2394+- in open(2) the aufs-file, open the branch-file with the hidden
2395+ vfsmount (instead of the original branch's vfsmount)
2396+- ideally this "bind-mount and lazy-umount" should be done atomically,
2397+ but it may be possible from userspace by the mount helper.
2398+
2399+Adding the internal hidden vfsmount and using it in opening a file, the
2400+file path under /proc will be printed correctly. This approach looks
2401+smarter, but is not possible I am afraid.
2402+- aufs-root may be bind-mount later. when it happens, another hidden
2403+ vfsmount will be required.
2404+- it is hard to get the chance to bind-mount and lazy-umount
2405+ + in kernel-space, FS can have vfsmount in open(2) via
2406+ file->f_path, and aufs can know its vfsmount. But several locks are
2407+ already acquired, and if aufs tries to bind-mount and lazy-umount
2408+ here, then it may cause a deadlock.
2409+ + in user-space, bind-mount doesn't invoke the mount helper.
2410+- since /proc shows dev and ino, aufs has to give vma these info. it
2411+ means a new member vm_prinode will be necessary. this is essentially
2412+ equivalent to vm_prfile described above.
2413+
2414+I have to give up this "looks-smater" approach.
c1595e42
JR
2415diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2416--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2417+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2018-04-15 08:49:13.394483860 +0200
c1595e42
JR
2418@@ -0,0 +1,96 @@
2419+
b00004a5 2420+# Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
2421+#
2422+# This program is free software; you can redistribute it and/or modify
2423+# it under the terms of the GNU General Public License as published by
2424+# the Free Software Foundation; either version 2 of the License, or
2425+# (at your option) any later version.
2426+#
2427+# This program is distributed in the hope that it will be useful,
2428+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2429+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2430+# GNU General Public License for more details.
2431+#
2432+# You should have received a copy of the GNU General Public License
2433+# along with this program; if not, write to the Free Software
2434+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2435+
2436+
2437+Listing XATTR/EA and getting the value
2438+----------------------------------------------------------------------
2439+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2440+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2441+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2442+information. But for the directories, aufs considers all the same named
2443+entries on the lower branches. Which means, if one of the lower entry
2444+rejects readdir call, then aufs returns an error even if the topmost
2445+entry allows it. This behaviour is necessary to respect the branch fs's
2446+security, but can make users confused since the user-visible standard
2447+attributes don't match the behaviour.
2448+To address this issue, aufs has a mount option called dirperm1 which
2449+checks the permission for the topmost entry only, and ignores the lower
2450+entry's permission.
2451+
2452+A similar issue can happen around XATTR.
2453+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2454+always set. Otherwise these very unpleasant situation would happen.
2455+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2456+- users may not be able to remove or reset the XATTR forever,
2457+
2458+
2459+XATTR/EA support in the internal (copy,move)-(up,down)
2460+----------------------------------------------------------------------
7e9cd9fe 2461+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2462+- "security" for LSM and capability.
2463+- "system" for posix ACL, 'acl' mount option is required for the branch
2464+ fs generally.
2465+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2466+- "user" for userspace, 'user_xattr' mount option is required for the
2467+ branch fs generally.
2468+
2469+Moreover there are some other categories. Aufs handles these rather
2470+unpopular categories as the ordinary ones, ie. there is no special
2471+condition nor exception.
2472+
2473+In copy-up, the support for XATTR on the dst branch may differ from the
2474+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2475+the original user operation which triggered the copy-up will fail. It
2476+can happen that even all copy-up will fail.
c1595e42
JR
2477+When both of src and dst branches support XATTR and if an error occurs
2478+during copying XATTR, then the copy-up should fail obviously. That is a
2479+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2480+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2481+For example, the src branch supports ACL but the dst branch doesn't
2482+because the dst branch may natively un-support it or temporary
2483+un-support it due to "noacl" mount option. Of course, the dst branch fs
2484+may NOT return an error even if the XATTR is not supported. It is
2485+totally up to the branch fs.
2486+
2487+Anyway when the aufs internal copy-up gets an error from the dst branch
2488+fs, then aufs tries removing the just copied entry and returns the error
2489+to the userspace. The worst case of this situation will be all copy-up
2490+will fail.
2491+
2492+For the copy-up operation, there two basic approaches.
2493+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2494+ error unconditionally if it happens.
c1595e42
JR
2495+- copy all XATTR, and ignore the error on the specified category only.
2496+
2497+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2498+chooses the latter approach and introduces some new branch attributes,
2499+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2500+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2501+convenient, "icex" is also provided which means all "icex*" attributes
2502+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2503+
2504+The meaning of these attributes is to ignore the error from setting
2505+XATTR on that branch.
2506+Note that aufs tries copying all XATTR unconditionally, and ignores the
2507+error from the dst branch according to the specified attributes.
2508+
2509+Some XATTR may have its default value. The default value may come from
2510+the parent dir or the environment. If the default value is set at the
2511+file creating-time, it will be overwritten by copy-up.
2512+Some contradiction may happen I am afraid.
2513+Do we need another attribute to stop copying XATTR? I am unsure. For
2514+now, aufs implements the branch attributes to ignore the error.
53392da6
AM
2515diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2516--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2517+++ linux/Documentation/filesystems/aufs/design/07export.txt 2018-04-15 08:49:13.394483860 +0200
523b37e3 2518@@ -0,0 +1,58 @@
53392da6 2519+
b00004a5 2520+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
2521+#
2522+# This program is free software; you can redistribute it and/or modify
2523+# it under the terms of the GNU General Public License as published by
2524+# the Free Software Foundation; either version 2 of the License, or
2525+# (at your option) any later version.
2526+#
2527+# This program is distributed in the hope that it will be useful,
2528+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2529+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2530+# GNU General Public License for more details.
2531+#
2532+# You should have received a copy of the GNU General Public License
523b37e3 2533+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2534+
2535+Export Aufs via NFS
2536+----------------------------------------------------------------------
2537+Here is an approach.
2538+- like xino/xib, add a new file 'xigen' which stores aufs inode
2539+ generation.
2540+- iget_locked(): initialize aufs inode generation for a new inode, and
2541+ store it in xigen file.
2542+- destroy_inode(): increment aufs inode generation and store it in xigen
2543+ file. it is necessary even if it is not unlinked, because any data of
2544+ inode may be changed by UDBA.
2545+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2546+ build file handle by
2547+ + branch id (4 bytes)
2548+ + superblock generation (4 bytes)
2549+ + inode number (4 or 8 bytes)
2550+ + parent dir inode number (4 or 8 bytes)
2551+ + inode generation (4 bytes))
2552+ + return value of exportfs_encode_fh() for the parent on a branch (4
2553+ bytes)
2554+ + file handle for a branch (by exportfs_encode_fh())
2555+- fh_to_dentry():
2556+ + find the index of a branch from its id in handle, and check it is
2557+ still exist in aufs.
2558+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2559+ + 2nd level: if not found in cache, get the parent inode number from
2560+ the handle and search it in cache. and then open the found parent
2561+ dir, find the matching inode number by vfs_readdir() and get its
2562+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2563+ + 3rd level: if the parent dir is not cached, call
2564+ exportfs_decode_fh() for a branch and get the parent on a branch,
2565+ build a pathname of it, convert it a pathname in aufs, call
2566+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2567+ the 2nd level.
2568+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2569+ for every branch, but not itself. to get this, (currently) aufs
2570+ searches in current->nsproxy->mnt_ns list. it may not be a good
2571+ idea, but I didn't get other approach.
2572+ + test the generation of the gotten inode.
2573+- every inode operation: they may get EBUSY due to UDBA. in this case,
2574+ convert it into ESTALE for NFSD.
2575+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2576+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2577diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2578--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2579+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2018-04-15 08:49:13.394483860 +0200
523b37e3 2580@@ -0,0 +1,52 @@
53392da6 2581+
b00004a5 2582+# Copyright (C) 2005-2018 Junjiro R. Okajima
53392da6
AM
2583+#
2584+# This program is free software; you can redistribute it and/or modify
2585+# it under the terms of the GNU General Public License as published by
2586+# the Free Software Foundation; either version 2 of the License, or
2587+# (at your option) any later version.
2588+#
2589+# This program is distributed in the hope that it will be useful,
2590+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2591+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2592+# GNU General Public License for more details.
2593+#
2594+# You should have received a copy of the GNU General Public License
523b37e3 2595+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2596+
2597+Show Whiteout Mode (shwh)
2598+----------------------------------------------------------------------
2599+Generally aufs hides the name of whiteouts. But in some cases, to show
2600+them is very useful for users. For instance, creating a new middle layer
2601+(branch) by merging existing layers.
2602+
2603+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2604+When you have three branches,
2605+- Bottom: 'system', squashfs (underlying base system), read-only
2606+- Middle: 'mods', squashfs, read-only
2607+- Top: 'overlay', ram (tmpfs), read-write
2608+
2609+The top layer is loaded at boot time and saved at shutdown, to preserve
2610+the changes made to the system during the session.
2611+When larger changes have been made, or smaller changes have accumulated,
2612+the size of the saved top layer data grows. At this point, it would be
2613+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2614+and rewrite the 'mods' squashfs, clearing the top layer and thus
2615+restoring save and load speed.
2616+
2617+This merging is simplified by the use of another aufs mount, of just the
2618+two overlay branches using the 'shwh' option.
2619+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2620+ aufs /livesys/merge_union
2621+
2622+A merged view of these two branches is then available at
2623+/livesys/merge_union, and the new feature is that the whiteouts are
2624+visible!
2625+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2626+writing to all branches. Also the default mode for all branches is 'ro'.
2627+It is now possible to save the combined contents of the two overlay
2628+branches to a new squashfs, e.g.:
2629+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2630+
2631+This new squashfs archive can be stored on the boot device and the
2632+initramfs will use it to replace the old one at the next boot.
2633diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2634--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
b00004a5 2635+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2018-04-15 08:49:13.394483860 +0200
7e9cd9fe 2636@@ -0,0 +1,47 @@
53392da6 2637+
b00004a5 2638+# Copyright (C) 2010-2018 Junjiro R. Okajima
53392da6
AM
2639+#
2640+# This program is free software; you can redistribute it and/or modify
2641+# it under the terms of the GNU General Public License as published by
2642+# the Free Software Foundation; either version 2 of the License, or
2643+# (at your option) any later version.
2644+#
2645+# This program is distributed in the hope that it will be useful,
2646+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2647+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2648+# GNU General Public License for more details.
2649+#
2650+# You should have received a copy of the GNU General Public License
523b37e3 2651+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2652+
2653+Dynamically customizable FS operations
2654+----------------------------------------------------------------------
2655+Generally FS operations (struct inode_operations, struct
2656+address_space_operations, struct file_operations, etc.) are defined as
2657+"static const", but it never means that FS have only one set of
2658+operation. Some FS have multiple sets of them. For instance, ext2 has
2659+three sets, one for XIP, for NOBH, and for normal.
2660+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2661+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2662+VFS acts differently if a function (member in the struct) is set or
2663+not. It means aufs should have several sets of operations and select one
2664+among them according to the branch FS definition.
2665+
7e9cd9fe 2666+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2667+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2668+dummy direct_IO function for struct address_space_operations, but it may
2669+not be set to the address_space_operations actually. When the branch FS
2670+doesn't have it, aufs doesn't set it to its address_space_operations
2671+while the function definition itself is still alive. So the behaviour
2672+itself will not change, and it will return an error when direct_IO is
2673+not set.
53392da6
AM
2674+
2675+The lifetime of these dynamically generated operation object is
2676+maintained by aufs branch object. When the branch is removed from aufs,
2677+the reference counter of the object is decremented. When it reaches
2678+zero, the dynamically generated operation object will be freed.
2679+
7e9cd9fe
AM
2680+This approach is designed to support AIO (io_submit), Direct I/O and
2681+XIP (DAX) mainly.
2682+Currently this approach is applied to address_space_operations for
2683+regular files only.
53392da6
AM
2684diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2685--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
c4adf169 2686+++ linux/Documentation/filesystems/aufs/README 2018-06-15 11:15:15.400449109 +0200
f2c43d5f 2687@@ -0,0 +1,393 @@
53392da6 2688+
5527c038 2689+Aufs4 -- advanced multi layered unification filesystem version 4.x
53392da6
AM
2690+http://aufs.sf.net
2691+Junjiro R. Okajima
2692+
2693+
2694+0. Introduction
2695+----------------------------------------
2696+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2697+Unionfs Version 1.x series. Adding many original ideas, approaches,
53392da6
AM
2698+improvements and implementations, it becomes totally different from
2699+Unionfs while keeping the basic features.
2700+Recently, Unionfs Version 2.x series begin taking some of the same
2701+approaches to aufs1's.
2702+Unionfs is being developed by Professor Erez Zadok at Stony Brook
2703+University and his team.
2704+
5527c038 2705+Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
53392da6
AM
2706+If you want older kernel version support, try aufs2-2.6.git or
2707+aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2708+
2709+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2710+ According to Christoph Hellwig, linux rejects all union-type
2711+ filesystems but UnionMount.
53392da6
AM
2712+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2713+
38d290e6
JR
2714+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2715+ UnionMount, and he pointed out an issue around a directory mutex
2716+ lock and aufs addressed it. But it is still unsure whether aufs will
2717+ be merged (or any other union solution).
076b876e 2718+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2719+
53392da6
AM
2720+
2721+1. Features
2722+----------------------------------------
2723+- unite several directories into a single virtual filesystem. The member
2724+ directory is called as a branch.
2725+- you can specify the permission flags to the branch, which are 'readonly',
2726+ 'readwrite' and 'whiteout-able.'
2727+- by upper writable branch, internal copyup and whiteout, files/dirs on
2728+ readonly branch are modifiable logically.
2729+- dynamic branch manipulation, add, del.
2730+- etc...
2731+
7e9cd9fe
AM
2732+Also there are many enhancements in aufs, such as:
2733+- test only the highest one for the directory permission (dirperm1)
2734+- copyup on open (coo=)
2735+- 'move' policy for copy-up between two writable branches, after
2736+ checking free space.
2737+- xattr, acl
53392da6
AM
2738+- readdir(3) in userspace.
2739+- keep inode number by external inode number table
2740+- keep the timestamps of file/dir in internal copyup operation
2741+- seekable directory, supporting NFS readdir.
2742+- whiteout is hardlinked in order to reduce the consumption of inodes
2743+ on branch
2744+- do not copyup, nor create a whiteout when it is unnecessary
2745+- revert a single systemcall when an error occurs in aufs
2746+- remount interface instead of ioctl
2747+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2748+- loopback mounted filesystem as a branch
2749+- kernel thread for removing the dir who has a plenty of whiteouts
2750+- support copyup sparse file (a file which has a 'hole' in it)
2751+- default permission flags for branches
2752+- selectable permission flags for ro branch, whether whiteout can
2753+ exist or not
2754+- export via NFS.
2755+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2756+- support multiple writable branches, some policies to select one
2757+ among multiple writable branches.
2758+- a new semantics for link(2) and rename(2) to support multiple
2759+ writable branches.
2760+- no glibc changes are required.
2761+- pseudo hardlink (hardlink over branches)
2762+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2763+ including NFS or remote filesystem branch.
2764+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2765+- and more...
2766+
5527c038 2767+Currently these features are dropped temporary from aufs4.
53392da6 2768+See design/08plan.txt in detail.
53392da6
AM
2769+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2770+ (robr)
2771+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2772+
2773+Features or just an idea in the future (see also design/*.txt),
2774+- reorder the branch index without del/re-add.
2775+- permanent xino files for NFSD
2776+- an option for refreshing the opened files after add/del branches
53392da6
AM
2777+- light version, without branch manipulation. (unnecessary?)
2778+- copyup in userspace
2779+- inotify in userspace
2780+- readv/writev
53392da6
AM
2781+
2782+
2783+2. Download
2784+----------------------------------------
5527c038
JR
2785+There are three GIT trees for aufs4, aufs4-linux.git,
2786+aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
1e00d052 2787+"aufs-util.git."
5527c038
JR
2788+While the aufs-util is always necessary, you need either of aufs4-linux
2789+or aufs4-standalone.
1e00d052 2790+
5527c038 2791+The aufs4-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2792+git://git.kernel.org/.../torvalds/linux.git.
2793+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
5527c038 2794+build aufs4 as an external kernel module.
2000de60 2795+Several extra patches are not included in this tree. Only
be52b249 2796+aufs4-standalone tree contains them. They are described in the later
2000de60 2797+section "Configuration and Compilation."
1e00d052 2798+
5527c038 2799+On the other hand, the aufs4-standalone tree has only aufs source files
53392da6 2800+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2801+But you need to apply all aufs patches manually.
53392da6 2802+
5527c038
JR
2803+You will find GIT branches whose name is in form of "aufs4.x" where "x"
2804+represents the linux kernel version, "linux-4.x". For instance,
2805+"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2806+"aufs4.x-rcN" branch.
1e00d052 2807+
5527c038 2808+o aufs4-linux tree
1e00d052 2809+$ git clone --reference /your/linux/git/tree \
5527c038 2810+ git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
1e00d052 2811+- if you don't have linux GIT tree, then remove "--reference ..."
5527c038
JR
2812+$ cd aufs4-linux.git
2813+$ git checkout origin/aufs4.0
53392da6 2814+
2000de60
JR
2815+Or You may want to directly git-pull aufs into your linux GIT tree, and
2816+leave the patch-work to GIT.
2817+$ cd /your/linux/git/tree
5527c038
JR
2818+$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2819+$ git fetch aufs4
2820+$ git checkout -b my4.0 v4.0
2821+$ (add your local change...)
2822+$ git pull aufs4 aufs4.0
2823+- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2000de60 2824+- you may need to solve some conflicts between your_changes and
5527c038
JR
2825+ aufs4.0. in this case, git-rerere is recommended so that you can
2826+ solve the similar conflicts automatically when you upgrade to 4.1 or
2000de60
JR
2827+ later in the future.
2828+
5527c038
JR
2829+o aufs4-standalone tree
2830+$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2831+$ cd aufs4-standalone.git
2832+$ git checkout origin/aufs4.0
53392da6
AM
2833+
2834+o aufs-util tree
5527c038
JR
2835+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2836+- note that the public aufs-util.git is on SourceForge instead of
2837+ GitHUB.
53392da6 2838+$ cd aufs-util.git
5527c038 2839+$ git checkout origin/aufs4.0
53392da6 2840+
5527c038
JR
2841+Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2842+The minor version number, 'x' in '4.x', of aufs may not always
9dbd164d
AM
2843+follow the minor version number of the kernel.
2844+Because changes in the kernel that cause the use of a new
2845+minor version number do not always require changes to aufs-util.
2846+
2847+Since aufs-util has its own minor version number, you may not be
2848+able to find a GIT branch in aufs-util for your kernel's
2849+exact minor version number.
2850+In this case, you should git-checkout the branch for the
53392da6 2851+nearest lower number.
9dbd164d
AM
2852+
2853+For (an unreleased) example:
5527c038
JR
2854+If you are using "linux-4.10" and the "aufs4.10" branch
2855+does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
9dbd164d
AM
2856+or something numerically smaller is the branch for your kernel.
2857+
53392da6
AM
2858+Also you can view all branches by
2859+ $ git branch -a
2860+
2861+
2862+3. Configuration and Compilation
2863+----------------------------------------
2864+Make sure you have git-checkout'ed the correct branch.
2865+
5527c038 2866+For aufs4-linux tree,
c06a8ce3 2867+- enable CONFIG_AUFS_FS.
1e00d052
AM
2868+- set other aufs configurations if necessary.
2869+
5527c038 2870+For aufs4-standalone tree,
53392da6
AM
2871+There are several ways to build.
2872+
2873+1.
5527c038
JR
2874+- apply ./aufs4-kbuild.patch to your kernel source files.
2875+- apply ./aufs4-base.patch too.
2876+- apply ./aufs4-mmap.patch too.
2877+- apply ./aufs4-standalone.patch too, if you have a plan to set
2878+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
537831f9
AM
2879+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2880+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2881+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2882+ =m or =y.
2883+- and build your kernel as usual.
2884+- install the built kernel.
c06a8ce3
AM
2885+ Note: Since linux-3.9, every filesystem module requires an alias
2886+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2887+ modules.aliases file if you set CONFIG_AUFS_FS=m.
7eafdf33
AM
2888+- install the header files too by "make headers_install" to the
2889+ directory where you specify. By default, it is $PWD/usr.
b4510431 2890+ "make help" shows a brief note for headers_install.
53392da6
AM
2891+- and reboot your system.
2892+
2893+2.
2894+- module only (CONFIG_AUFS_FS=m).
5527c038
JR
2895+- apply ./aufs4-base.patch to your kernel source files.
2896+- apply ./aufs4-mmap.patch too.
2897+- apply ./aufs4-standalone.patch too.
53392da6
AM
2898+- build your kernel, don't forget "make headers_install", and reboot.
2899+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2900+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2901+ every aufs configurations.
2902+- build the module by simple "make".
c06a8ce3
AM
2903+ Note: Since linux-3.9, every filesystem module requires an alias
2904+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2905+ modules.aliases file.
53392da6
AM
2906+- you can specify ${KDIR} make variable which points to your kernel
2907+ source tree.
2908+- install the files
2909+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2910+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2911+ + run "make install_headers" (instead of headers_install) to install
2912+ the modified aufs header file (you can specify DESTDIR which is
2913+ available in aufs standalone version's Makefile only), or copy
2914+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2915+ you like manually. By default, the target directory is $PWD/usr.
5527c038 2916+- no need to apply aufs4-kbuild.patch, nor copying source files to your
53392da6
AM
2917+ kernel source tree.
2918+
b4510431 2919+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2920+ as well as "make headers_install" in the kernel source tree.
2921+ headers_install is subject to be forgotten, but it is essentially
2922+ necessary, not only for building aufs-util.
2923+ You may not meet problems without headers_install in some older
2924+ version though.
2925+
2926+And then,
2927+- read README in aufs-util, build and install it
9dbd164d
AM
2928+- note that your distribution may contain an obsoleted version of
2929+ aufs_type.h in /usr/include/linux or something. When you build aufs
2930+ utilities, make sure that your compiler refers the correct aufs header
2931+ file which is built by "make headers_install."
53392da6
AM
2932+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2933+ then run "make install_ulib" too. And refer to the aufs manual in
2934+ detail.
2935+
5527c038 2936+There several other patches in aufs4-standalone.git. They are all
38d290e6 2937+optional. When you meet some problems, they will help you.
5527c038 2938+- aufs4-loopback.patch
38d290e6
JR
2939+ Supports a nested loopback mount in a branch-fs. This patch is
2940+ unnecessary until aufs produces a message like "you may want to try
2941+ another patch for loopback file".
2942+- vfs-ino.patch
2943+ Modifies a system global kernel internal function get_next_ino() in
2944+ order to stop assigning 0 for an inode-number. Not directly related to
2945+ aufs, but recommended generally.
2946+- tmpfs-idr.patch
2947+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2948+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2949+ duplication of inode number, which is important for backup tools and
2950+ other utilities. When you find aufs XINO files for tmpfs branch
2951+ growing too much, try this patch.
be52b249
AM
2952+- lockdep-debug.patch
2953+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2954+ also a caller of VFS functions for branch filesystems, subclassing of
2955+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2956+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2957+ need to apply this debug patch to expand several constant values.
2958+ If don't know what LOCKDEP, then you don't have apply this patch.
38d290e6 2959+
53392da6
AM
2960+
2961+4. Usage
2962+----------------------------------------
2963+At first, make sure aufs-util are installed, and please read the aufs
2964+manual, aufs.5 in aufs-util.git tree.
2965+$ man -l aufs.5
2966+
2967+And then,
2968+$ mkdir /tmp/rw /tmp/aufs
2969+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2970+
2971+Here is another example. The result is equivalent.
2972+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2973+ Or
2974+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2975+# mount -o remount,append:${HOME} /tmp/aufs
2976+
2977+Then, you can see whole tree of your home dir through /tmp/aufs. If
2978+you modify a file under /tmp/aufs, the one on your home directory is
2979+not affected, instead the same named file will be newly created under
2980+/tmp/rw. And all of your modification to a file will be applied to
2981+the one under /tmp/rw. This is called the file based Copy on Write
2982+(COW) method.
2983+Aufs mount options are described in aufs.5.
2984+If you run chroot or something and make your aufs as a root directory,
2985+then you need to customize the shutdown script. See the aufs manual in
2986+detail.
2987+
2988+Additionally, there are some sample usages of aufs which are a
2989+diskless system with network booting, and LiveCD over NFS.
2990+See sample dir in CVS tree on SourceForge.
2991+
2992+
2993+5. Contact
2994+----------------------------------------
2995+When you have any problems or strange behaviour in aufs, please let me
2996+know with:
2997+- /proc/mounts (instead of the output of mount(8))
2998+- /sys/module/aufs/*
2999+- /sys/fs/aufs/* (if you have them)
3000+- /debug/aufs/* (if you have them)
3001+- linux kernel version
3002+ if your kernel is not plain, for example modified by distributor,
3003+ the url where i can download its source is necessary too.
3004+- aufs version which was printed at loading the module or booting the
3005+ system, instead of the date you downloaded.
3006+- configuration (define/undefine CONFIG_AUFS_xxx)
3007+- kernel configuration or /proc/config.gz (if you have it)
3008+- behaviour which you think to be incorrect
3009+- actual operation, reproducible one is better
3010+- mailto: aufs-users at lists.sourceforge.net
3011+
3012+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
3013+and Feature Requests) on SourceForge. Please join and write to
3014+aufs-users ML.
3015+
3016+
3017+6. Acknowledgements
3018+----------------------------------------
3019+Thanks to everyone who have tried and are using aufs, whoever
3020+have reported a bug or any feedback.
3021+
3022+Especially donators:
3023+Tomas Matejicek(slax.org) made a donation (much more than once).
3024+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
3025+ scripts) is making "doubling" donations.
3026+ Unfortunately I cannot list all of the donators, but I really
b4510431 3027+ appreciate.
53392da6
AM
3028+ It ends Aug 2010, but the ordinary donation URL is still available.
3029+ <http://sourceforge.net/donate/index.php?group_id=167503>
3030+Dai Itasaka made a donation (2007/8).
3031+Chuck Smith made a donation (2008/4, 10 and 12).
3032+Henk Schoneveld made a donation (2008/9).
3033+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
3034+Francois Dupoux made a donation (2008/11).
3035+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
3036+ aufs2 GIT tree (2009/2).
3037+William Grant made a donation (2009/3).
3038+Patrick Lane made a donation (2009/4).
3039+The Mail Archive (mail-archive.com) made donations (2009/5).
3040+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
3041+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
3042+Pavel Pronskiy made a donation (2011/2).
3043+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
3044+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
3045+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
3046+11).
1e00d052 3047+Sam Liddicott made a donation (2011/9).
86dc4139
AM
3048+Era Scarecrow made a donation (2013/4).
3049+Bor Ratajc made a donation (2013/4).
3050+Alessandro Gorreta made a donation (2013/4).
3051+POIRETTE Marc made a donation (2013/4).
3052+Alessandro Gorreta made a donation (2013/4).
3053+lauri kasvandik made a donation (2013/5).
392086de 3054+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
3055+The Parted Magic Project made a donation (2013/9 and 11).
3056+Pavel Barta made a donation (2013/10).
38d290e6 3057+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 3058+James B made a donation (2014/7 and 2015/7).
076b876e 3059+Stefano Di Biase made a donation (2014/8).
2000de60 3060+Daniel Epellei made a donation (2015/1).
c4adf169 3061+OmegaPhil made a donation (2016/1, 2018/4).
5afbbe0d 3062+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 3063+James Burry made a donation (2016/12).
53392da6
AM
3064+
3065+Thank you very much.
3066+Donations are always, including future donations, very important and
3067+helpful for me to keep on developing aufs.
3068+
3069+
3070+7.
3071+----------------------------------------
3072+If you are an experienced user, no explanation is needed. Aufs is
3073+just a linux filesystem.
3074+
3075+
3076+Enjoy!
3077+
3078+# Local variables: ;
3079+# mode: text;
3080+# End: ;
7f207e10
AM
3081diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
3082--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
3083+++ linux/fs/aufs/aufs.h 2018-07-19 09:46:13.053314374 +0200
3084@@ -0,0 +1,61 @@
3085+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 3086+/*
b00004a5 3087+ * Copyright (C) 2005-2018 Junjiro R. Okajima
7f207e10
AM
3088+ *
3089+ * This program, aufs is free software; you can redistribute it and/or modify
3090+ * it under the terms of the GNU General Public License as published by
3091+ * the Free Software Foundation; either version 2 of the License, or
3092+ * (at your option) any later version.
3093+ *
3094+ * This program is distributed in the hope that it will be useful,
3095+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3096+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3097+ * GNU General Public License for more details.
3098+ *
3099+ * You should have received a copy of the GNU General Public License
523b37e3 3100+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3101+ */
3102+
3103+/*
3104+ * all header files
3105+ */
3106+
3107+#ifndef __AUFS_H__
3108+#define __AUFS_H__
3109+
3110+#ifdef __KERNEL__
3111+
3112+#define AuStub(type, name, body, ...) \
3113+ static inline type name(__VA_ARGS__) { body; }
3114+
3115+#define AuStubVoid(name, ...) \
3116+ AuStub(void, name, , __VA_ARGS__)
3117+#define AuStubInt0(name, ...) \
3118+ AuStub(int, name, return 0, __VA_ARGS__)
3119+
3120+#include "debug.h"
3121+
3122+#include "branch.h"
3123+#include "cpup.h"
3124+#include "dcsub.h"
3125+#include "dbgaufs.h"
3126+#include "dentry.h"
3127+#include "dir.h"
8b6a4947 3128+#include "dirren.h"
7f207e10
AM
3129+#include "dynop.h"
3130+#include "file.h"
3131+#include "fstype.h"
8b6a4947 3132+#include "hbl.h"
7f207e10
AM
3133+#include "inode.h"
3134+#include "loop.h"
3135+#include "module.h"
7f207e10
AM
3136+#include "opts.h"
3137+#include "rwsem.h"
7f207e10
AM
3138+#include "super.h"
3139+#include "sysaufs.h"
3140+#include "vfsub.h"
3141+#include "whout.h"
3142+#include "wkq.h"
3143+
3144+#endif /* __KERNEL__ */
3145+#endif /* __AUFS_H__ */
3146diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3147--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
3148+++ linux/fs/aufs/branch.c 2018-07-19 09:46:13.053314374 +0200
3149@@ -0,0 +1,1422 @@
3150+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 3151+/*
b00004a5 3152+ * Copyright (C) 2005-2018 Junjiro R. Okajima
7f207e10
AM
3153+ *
3154+ * This program, aufs is free software; you can redistribute it and/or modify
3155+ * it under the terms of the GNU General Public License as published by
3156+ * the Free Software Foundation; either version 2 of the License, or
3157+ * (at your option) any later version.
3158+ *
3159+ * This program is distributed in the hope that it will be useful,
3160+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3161+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3162+ * GNU General Public License for more details.
3163+ *
3164+ * You should have received a copy of the GNU General Public License
523b37e3 3165+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3166+ */
3167+
3168+/*
3169+ * branch management
3170+ */
3171+
027c5e7a 3172+#include <linux/compat.h>
7f207e10
AM
3173+#include <linux/statfs.h>
3174+#include "aufs.h"
3175+
3176+/*
3177+ * free a single branch
1facf9fc 3178+ */
3179+static void au_br_do_free(struct au_branch *br)
3180+{
3181+ int i;
3182+ struct au_wbr *wbr;
4a4d8108 3183+ struct au_dykey **key;
1facf9fc 3184+
027c5e7a 3185+ au_hnotify_fin_br(br);
8b6a4947
AM
3186+ /* always, regardless the mount option */
3187+ au_dr_hino_free(&br->br_dirren);
062440b3 3188+ au_xino_put(br);
1facf9fc 3189+
5afbbe0d
AM
3190+ AuDebugOn(au_br_count(br));
3191+ au_br_count_fin(br);
1facf9fc 3192+
3193+ wbr = br->br_wbr;
3194+ if (wbr) {
3195+ for (i = 0; i < AuBrWh_Last; i++)
3196+ dput(wbr->wbr_wh[i]);
3197+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 3198+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 3199+ }
3200+
076b876e
AM
3201+ if (br->br_fhsm) {
3202+ au_br_fhsm_fin(br->br_fhsm);
1c60b727 3203+ kfree(br->br_fhsm);
076b876e
AM
3204+ }
3205+
4a4d8108
AM
3206+ key = br->br_dykey;
3207+ for (i = 0; i < AuBrDynOp; i++, key++)
3208+ if (*key)
3209+ au_dy_put(*key);
3210+ else
3211+ break;
3212+
537831f9
AM
3213+ /* recursive lock, s_umount of branch's */
3214+ lockdep_off();
86dc4139 3215+ path_put(&br->br_path);
537831f9 3216+ lockdep_on();
1c60b727
AM
3217+ kfree(wbr);
3218+ kfree(br);
1facf9fc 3219+}
3220+
3221+/*
3222+ * frees all branches
3223+ */
3224+void au_br_free(struct au_sbinfo *sbinfo)
3225+{
3226+ aufs_bindex_t bmax;
3227+ struct au_branch **br;
3228+
dece6358
AM
3229+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3230+
5afbbe0d 3231+ bmax = sbinfo->si_bbot + 1;
1facf9fc 3232+ br = sbinfo->si_branch;
3233+ while (bmax--)
3234+ au_br_do_free(*br++);
3235+}
3236+
3237+/*
3238+ * find the index of a branch which is specified by @br_id.
3239+ */
3240+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3241+{
5afbbe0d 3242+ aufs_bindex_t bindex, bbot;
1facf9fc 3243+
5afbbe0d
AM
3244+ bbot = au_sbbot(sb);
3245+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3246+ if (au_sbr_id(sb, bindex) == br_id)
3247+ return bindex;
3248+ return -1;
3249+}
3250+
3251+/* ---------------------------------------------------------------------- */
3252+
3253+/*
3254+ * add a branch
3255+ */
3256+
b752ccd1
AM
3257+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3258+ struct dentry *h_root)
1facf9fc 3259+{
b752ccd1
AM
3260+ if (unlikely(h_adding == h_root
3261+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3262+ return 1;
b752ccd1
AM
3263+ if (h_adding->d_sb != h_root->d_sb)
3264+ return 0;
3265+ return au_test_subdir(h_adding, h_root)
3266+ || au_test_subdir(h_root, h_adding);
1facf9fc 3267+}
3268+
3269+/*
3270+ * returns a newly allocated branch. @new_nbranch is a number of branches
3271+ * after adding a branch.
3272+ */
3273+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3274+ int perm)
3275+{
3276+ struct au_branch *add_branch;
3277+ struct dentry *root;
5527c038 3278+ struct inode *inode;
4a4d8108 3279+ int err;
1facf9fc 3280+
4a4d8108 3281+ err = -ENOMEM;
be52b249 3282+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3283+ if (unlikely(!add_branch))
3284+ goto out;
062440b3
AM
3285+ add_branch->br_xino = au_xino_alloc();
3286+ if (unlikely(!add_branch->br_xino))
521ced18 3287+ goto out_br;
027c5e7a
AM
3288+ err = au_hnotify_init_br(add_branch, perm);
3289+ if (unlikely(err))
062440b3 3290+ goto out_xino;
027c5e7a 3291+
1facf9fc 3292+ if (au_br_writable(perm)) {
3293+ /* may be freed separately at changing the branch permission */
be52b249 3294+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3295+ GFP_NOFS);
3296+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3297+ goto out_hnotify;
1facf9fc 3298+ }
3299+
076b876e
AM
3300+ if (au_br_fhsm(perm)) {
3301+ err = au_fhsm_br_alloc(add_branch);
3302+ if (unlikely(err))
3303+ goto out_wbr;
3304+ }
3305+
521ced18 3306+ root = sb->s_root;
e2f27e51 3307+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3308+ if (!err)
e2f27e51 3309+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3310+ if (!err) {
3311+ inode = d_inode(root);
1c60b727
AM
3312+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3313+ /*may_shrink*/0);
5527c038 3314+ }
4a4d8108
AM
3315+ if (!err)
3316+ return add_branch; /* success */
1facf9fc 3317+
076b876e 3318+out_wbr:
1c60b727 3319+ kfree(add_branch->br_wbr);
027c5e7a
AM
3320+out_hnotify:
3321+ au_hnotify_fin_br(add_branch);
062440b3
AM
3322+out_xino:
3323+ au_xino_put(add_branch);
4f0767ce 3324+out_br:
1c60b727 3325+ kfree(add_branch);
4f0767ce 3326+out:
4a4d8108 3327+ return ERR_PTR(err);
1facf9fc 3328+}
3329+
3330+/*
3331+ * test if the branch permission is legal or not.
3332+ */
3333+static int test_br(struct inode *inode, int brperm, char *path)
3334+{
3335+ int err;
3336+
4a4d8108
AM
3337+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3338+ if (!err)
3339+ goto out;
1facf9fc 3340+
4a4d8108
AM
3341+ err = -EINVAL;
3342+ pr_err("write permission for readonly mount or inode, %s\n", path);
3343+
4f0767ce 3344+out:
1facf9fc 3345+ return err;
3346+}
3347+
3348+/*
3349+ * returns:
3350+ * 0: success, the caller will add it
3351+ * plus: success, it is already unified, the caller should ignore it
3352+ * minus: error
3353+ */
3354+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3355+{
3356+ int err;
5afbbe0d 3357+ aufs_bindex_t bbot, bindex;
5527c038 3358+ struct dentry *root, *h_dentry;
1facf9fc 3359+ struct inode *inode, *h_inode;
3360+
3361+ root = sb->s_root;
5afbbe0d
AM
3362+ bbot = au_sbbot(sb);
3363+ if (unlikely(bbot >= 0
1facf9fc 3364+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3365+ err = 1;
3366+ if (!remount) {
3367+ err = -EINVAL;
4a4d8108 3368+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3369+ }
3370+ goto out;
3371+ }
3372+
3373+ err = -ENOSPC; /* -E2BIG; */
3374+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3375+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3376+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3377+ goto out;
3378+ }
3379+
3380+ err = -EDOM;
5afbbe0d 3381+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3382+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3383+ goto out;
3384+ }
3385+
5527c038 3386+ inode = d_inode(add->path.dentry);
1facf9fc 3387+ err = -ENOENT;
3388+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3389+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3390+ goto out;
3391+ }
3392+
3393+ err = -EINVAL;
3394+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3395+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3396+ goto out;
3397+ }
3398+
3399+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3400+ pr_err("unsupported filesystem, %s (%s)\n",
3401+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3402+ goto out;
3403+ }
3404+
c1595e42
JR
3405+ if (unlikely(inode->i_sb->s_stack_depth)) {
3406+ pr_err("already stacked, %s (%s)\n",
3407+ add->pathname, au_sbtype(inode->i_sb));
3408+ goto out;
3409+ }
3410+
5527c038 3411+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3412+ if (unlikely(err))
3413+ goto out;
3414+
5afbbe0d 3415+ if (bbot < 0)
1facf9fc 3416+ return 0; /* success */
3417+
3418+ err = -EINVAL;
5afbbe0d 3419+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3420+ if (unlikely(test_overlap(sb, add->path.dentry,
3421+ au_h_dptr(root, bindex)))) {
4a4d8108 3422+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3423+ goto out;
3424+ }
3425+
3426+ err = 0;
3427+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3428+ h_dentry = au_h_dptr(root, 0);
3429+ h_inode = d_inode(h_dentry);
1facf9fc 3430+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3431+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3432+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3433+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3434+ add->pathname,
3435+ i_uid_read(inode), i_gid_read(inode),
3436+ (inode->i_mode & S_IALLUGO),
3437+ i_uid_read(h_inode), i_gid_read(h_inode),
3438+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3439+ }
3440+
4f0767ce 3441+out:
1facf9fc 3442+ return err;
3443+}
3444+
3445+/*
3446+ * initialize or clean the whiteouts for an adding branch
3447+ */
3448+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3449+ int new_perm)
1facf9fc 3450+{
3451+ int err, old_perm;
3452+ aufs_bindex_t bindex;
febd17d6 3453+ struct inode *h_inode;
1facf9fc 3454+ struct au_wbr *wbr;
3455+ struct au_hinode *hdir;
5527c038 3456+ struct dentry *h_dentry;
1facf9fc 3457+
86dc4139
AM
3458+ err = vfsub_mnt_want_write(au_br_mnt(br));
3459+ if (unlikely(err))
3460+ goto out;
3461+
1facf9fc 3462+ wbr = br->br_wbr;
3463+ old_perm = br->br_perm;
3464+ br->br_perm = new_perm;
3465+ hdir = NULL;
febd17d6 3466+ h_inode = NULL;
1facf9fc 3467+ bindex = au_br_index(sb, br->br_id);
3468+ if (0 <= bindex) {
5527c038 3469+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3470+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3471+ } else {
5527c038 3472+ h_dentry = au_br_dentry(br);
febd17d6
JR
3473+ h_inode = d_inode(h_dentry);
3474+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3475+ }
3476+ if (!wbr)
86dc4139 3477+ err = au_wh_init(br, sb);
1facf9fc 3478+ else {
3479+ wbr_wh_write_lock(wbr);
86dc4139 3480+ err = au_wh_init(br, sb);
1facf9fc 3481+ wbr_wh_write_unlock(wbr);
3482+ }
3483+ if (hdir)
5afbbe0d 3484+ au_hn_inode_unlock(hdir);
1facf9fc 3485+ else
febd17d6 3486+ inode_unlock(h_inode);
86dc4139 3487+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3488+ br->br_perm = old_perm;
3489+
3490+ if (!err && wbr && !au_br_writable(new_perm)) {
1c60b727 3491+ kfree(wbr);
1facf9fc 3492+ br->br_wbr = NULL;
3493+ }
3494+
86dc4139 3495+out:
1facf9fc 3496+ return err;
3497+}
3498+
3499+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3500+ int perm)
1facf9fc 3501+{
3502+ int err;
4a4d8108 3503+ struct kstatfs kst;
1facf9fc 3504+ struct au_wbr *wbr;
3505+
3506+ wbr = br->br_wbr;
dece6358 3507+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3508+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3509+
4a4d8108
AM
3510+ /*
3511+ * a limit for rmdir/rename a dir
523b37e3 3512+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3513+ */
86dc4139 3514+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3515+ if (unlikely(err))
3516+ goto out;
3517+ err = -EINVAL;
3518+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3519+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3520+ else
523b37e3
AM
3521+ pr_err("%pd(%s), unsupported namelen %ld\n",
3522+ au_br_dentry(br),
86dc4139 3523+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3524+
4f0767ce 3525+out:
1facf9fc 3526+ return err;
3527+}
3528+
c1595e42 3529+/* initialize a new branch */
1facf9fc 3530+static int au_br_init(struct au_branch *br, struct super_block *sb,
3531+ struct au_opt_add *add)
3532+{
3533+ int err;
062440b3
AM
3534+ struct au_branch *brbase;
3535+ struct file *xf;
5527c038 3536+ struct inode *h_inode;
1facf9fc 3537+
3538+ err = 0;
1facf9fc 3539+ br->br_perm = add->perm;
86dc4139 3540+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3541+ spin_lock_init(&br->br_dykey_lock);
5afbbe0d 3542+ au_br_count_init(br);
1facf9fc 3543+ br->br_id = au_new_br_id(sb);
7f207e10 3544+ AuDebugOn(br->br_id < 0);
1facf9fc 3545+
8b6a4947
AM
3546+ /* always, regardless the given option */
3547+ err = au_dr_br_init(sb, br, &add->path);
3548+ if (unlikely(err))
3549+ goto out_err;
3550+
1facf9fc 3551+ if (au_br_writable(add->perm)) {
86dc4139 3552+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3553+ if (unlikely(err))
b752ccd1 3554+ goto out_err;
1facf9fc 3555+ }
3556+
3557+ if (au_opt_test(au_mntflags(sb), XINO)) {
062440b3
AM
3558+ brbase = au_sbr(sb, 0);
3559+ xf = au_xino_file(brbase);
3560+ AuDebugOn(!xf);
5527c038 3561+ h_inode = d_inode(add->path.dentry);
062440b3 3562+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
1facf9fc 3563+ if (unlikely(err)) {
062440b3 3564+ AuDebugOn(au_xino_file(br));
b752ccd1 3565+ goto out_err;
1facf9fc 3566+ }
3567+ }
3568+
3569+ sysaufs_br_init(br);
86dc4139 3570+ path_get(&br->br_path);
b752ccd1 3571+ goto out; /* success */
1facf9fc 3572+
4f0767ce 3573+out_err:
86dc4139 3574+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3575+out:
1facf9fc 3576+ return err;
3577+}
3578+
3579+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3580+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3581+ aufs_bindex_t amount)
3582+{
3583+ struct au_branch **brp;
3584+
dece6358
AM
3585+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3586+
1facf9fc 3587+ brp = sbinfo->si_branch + bindex;
3588+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3589+ *brp = br;
5afbbe0d
AM
3590+ sbinfo->si_bbot++;
3591+ if (unlikely(bbot < 0))
3592+ sbinfo->si_bbot = 0;
1facf9fc 3593+}
3594+
3595+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3596+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3597+{
3598+ struct au_hdentry *hdp;
3599+
1308ab2a 3600+ AuRwMustWriteLock(&dinfo->di_rwsem);
3601+
5afbbe0d 3602+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3603+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3604+ au_h_dentry_init(hdp);
5afbbe0d
AM
3605+ dinfo->di_bbot++;
3606+ if (unlikely(bbot < 0))
3607+ dinfo->di_btop = 0;
1facf9fc 3608+}
3609+
3610+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3611+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3612+{
3613+ struct au_hinode *hip;
3614+
1308ab2a 3615+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3616+
5afbbe0d 3617+ hip = au_hinode(iinfo, bindex);
1facf9fc 3618+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3619+ au_hinode_init(hip);
3620+ iinfo->ii_bbot++;
3621+ if (unlikely(bbot < 0))
3622+ iinfo->ii_btop = 0;
1facf9fc 3623+}
3624+
86dc4139
AM
3625+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3626+ aufs_bindex_t bindex)
1facf9fc 3627+{
86dc4139 3628+ struct dentry *root, *h_dentry;
5527c038 3629+ struct inode *root_inode, *h_inode;
5afbbe0d 3630+ aufs_bindex_t bbot, amount;
1facf9fc 3631+
3632+ root = sb->s_root;
5527c038 3633+ root_inode = d_inode(root);
5afbbe0d
AM
3634+ bbot = au_sbbot(sb);
3635+ amount = bbot + 1 - bindex;
86dc4139 3636+ h_dentry = au_br_dentry(br);
53392da6 3637+ au_sbilist_lock();
5afbbe0d
AM
3638+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3639+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3640+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3641+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3642+ h_inode = d_inode(h_dentry);
3643+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3644+ au_sbilist_unlock();
1facf9fc 3645+}
3646+
3647+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3648+{
3649+ int err;
5afbbe0d 3650+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3651+ struct dentry *root, *h_dentry;
3652+ struct inode *root_inode;
3653+ struct au_branch *add_branch;
062440b3 3654+ struct file *xf;
1facf9fc 3655+
3656+ root = sb->s_root;
5527c038 3657+ root_inode = d_inode(root);
1facf9fc 3658+ IMustLock(root_inode);
5afbbe0d 3659+ IiMustWriteLock(root_inode);
1facf9fc 3660+ err = test_add(sb, add, remount);
3661+ if (unlikely(err < 0))
3662+ goto out;
3663+ if (err) {
3664+ err = 0;
3665+ goto out; /* success */
3666+ }
3667+
5afbbe0d
AM
3668+ bbot = au_sbbot(sb);
3669+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3670+ err = PTR_ERR(add_branch);
3671+ if (IS_ERR(add_branch))
3672+ goto out;
3673+
3674+ err = au_br_init(add_branch, sb, add);
3675+ if (unlikely(err)) {
3676+ au_br_do_free(add_branch);
3677+ goto out;
3678+ }
3679+
3680+ add_bindex = add->bindex;
062440b3
AM
3681+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3682+ au_br_do_add(sb, add_branch, add_bindex);
3683+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3684+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
1facf9fc 3685+
86dc4139 3686+ h_dentry = add->path.dentry;
1308ab2a 3687+ if (!add_bindex) {
1facf9fc 3688+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3689+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3690+ } else
5527c038 3691+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3692+
3693+ /*
062440b3 3694+ * this test/set prevents aufs from handling unnecessary notify events
027c5e7a 3695+ * of xino files, in case of re-adding a writable branch which was
1facf9fc 3696+ * once detached from aufs.
3697+ */
3698+ if (au_xino_brid(sb) < 0
3699+ && au_br_writable(add_branch->br_perm)
062440b3
AM
3700+ && !au_test_fs_bad_xino(h_dentry->d_sb)) {
3701+ xf = au_xino_file(add_branch);
3702+ if (xf && xf->f_path.dentry->d_parent == h_dentry)
3703+ au_xino_brid_set(sb, add_branch->br_id);
3704+ }
1facf9fc 3705+
4f0767ce 3706+out:
1facf9fc 3707+ return err;
3708+}
3709+
3710+/* ---------------------------------------------------------------------- */
3711+
79b8bda9 3712+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3713+ unsigned long long max __maybe_unused,
3714+ void *arg)
3715+{
3716+ unsigned long long n;
3717+ struct file **p, *f;
8b6a4947
AM
3718+ struct hlist_bl_head *files;
3719+ struct hlist_bl_node *pos;
076b876e 3720+ struct au_finfo *finfo;
076b876e
AM
3721+
3722+ n = 0;
3723+ p = a;
3724+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3725+ hlist_bl_lock(files);
3726+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3727+ f = finfo->fi_file;
3728+ if (file_count(f)
3729+ && !special_file(file_inode(f)->i_mode)) {
3730+ get_file(f);
3731+ *p++ = f;
3732+ n++;
3733+ AuDebugOn(n > max);
3734+ }
3735+ }
8b6a4947 3736+ hlist_bl_unlock(files);
076b876e
AM
3737+
3738+ return n;
3739+}
3740+
3741+static struct file **au_farray_alloc(struct super_block *sb,
3742+ unsigned long long *max)
3743+{
5afbbe0d 3744+ *max = au_nfiles(sb);
79b8bda9 3745+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3746+}
3747+
3748+static void au_farray_free(struct file **a, unsigned long long max)
3749+{
3750+ unsigned long long ull;
3751+
3752+ for (ull = 0; ull < max; ull++)
3753+ if (a[ull])
3754+ fput(a[ull]);
be52b249 3755+ kvfree(a);
076b876e
AM
3756+}
3757+
3758+/* ---------------------------------------------------------------------- */
3759+
1facf9fc 3760+/*
3761+ * delete a branch
3762+ */
3763+
3764+/* to show the line number, do not make it inlined function */
4a4d8108 3765+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3766+ if (do_info) \
4a4d8108 3767+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3768+} while (0)
3769+
5afbbe0d
AM
3770+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3771+ aufs_bindex_t bbot)
027c5e7a 3772+{
5afbbe0d 3773+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3774+}
3775+
5afbbe0d
AM
3776+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3777+ aufs_bindex_t bbot)
027c5e7a 3778+{
5afbbe0d 3779+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3780+}
3781+
1facf9fc 3782+/*
3783+ * test if the branch is deletable or not.
3784+ */
3785+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3786+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3787+{
3788+ int err, i, j, ndentry;
5afbbe0d 3789+ aufs_bindex_t btop, bbot;
1facf9fc 3790+ struct au_dcsub_pages dpages;
3791+ struct au_dpage *dpage;
3792+ struct dentry *d;
1facf9fc 3793+
3794+ err = au_dpages_init(&dpages, GFP_NOFS);
3795+ if (unlikely(err))
3796+ goto out;
3797+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3798+ if (unlikely(err))
3799+ goto out_dpages;
3800+
1facf9fc 3801+ for (i = 0; !err && i < dpages.ndpage; i++) {
3802+ dpage = dpages.dpages + i;
3803+ ndentry = dpage->ndentry;
3804+ for (j = 0; !err && j < ndentry; j++) {
3805+ d = dpage->dentries[j];
c1595e42 3806+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3807+ if (!au_digen_test(d, sigen)) {
1facf9fc 3808+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3809+ if (unlikely(au_dbrange_test(d))) {
3810+ di_read_unlock(d, AuLock_IR);
3811+ continue;
3812+ }
3813+ } else {
1facf9fc 3814+ di_write_lock_child(d);
027c5e7a
AM
3815+ if (unlikely(au_dbrange_test(d))) {
3816+ di_write_unlock(d);
3817+ continue;
3818+ }
1facf9fc 3819+ err = au_reval_dpath(d, sigen);
3820+ if (!err)
3821+ di_downgrade_lock(d, AuLock_IR);
3822+ else {
3823+ di_write_unlock(d);
3824+ break;
3825+ }
3826+ }
3827+
027c5e7a 3828+ /* AuDbgDentry(d); */
5afbbe0d
AM
3829+ btop = au_dbtop(d);
3830+ bbot = au_dbbot(d);
3831+ if (btop <= bindex
3832+ && bindex <= bbot
1facf9fc 3833+ && au_h_dptr(d, bindex)
5afbbe0d 3834+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3835+ err = -EBUSY;
523b37e3 3836+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3837+ AuDbgDentry(d);
1facf9fc 3838+ }
3839+ di_read_unlock(d, AuLock_IR);
3840+ }
3841+ }
3842+
4f0767ce 3843+out_dpages:
1facf9fc 3844+ au_dpages_free(&dpages);
4f0767ce 3845+out:
1facf9fc 3846+ return err;
3847+}
3848+
3849+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3850+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3851+{
3852+ int err;
7f207e10
AM
3853+ unsigned long long max, ull;
3854+ struct inode *i, **array;
5afbbe0d 3855+ aufs_bindex_t btop, bbot;
1facf9fc 3856+
7f207e10
AM
3857+ array = au_iarray_alloc(sb, &max);
3858+ err = PTR_ERR(array);
3859+ if (IS_ERR(array))
3860+ goto out;
3861+
1facf9fc 3862+ err = 0;
7f207e10
AM
3863+ AuDbg("b%d\n", bindex);
3864+ for (ull = 0; !err && ull < max; ull++) {
3865+ i = array[ull];
076b876e
AM
3866+ if (unlikely(!i))
3867+ break;
7f207e10 3868+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3869+ continue;
3870+
7f207e10 3871+ /* AuDbgInode(i); */
537831f9 3872+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3873+ ii_read_lock_child(i);
3874+ else {
3875+ ii_write_lock_child(i);
027c5e7a
AM
3876+ err = au_refresh_hinode_self(i);
3877+ au_iigen_dec(i);
1facf9fc 3878+ if (!err)
3879+ ii_downgrade_lock(i);
3880+ else {
3881+ ii_write_unlock(i);
3882+ break;
3883+ }
3884+ }
3885+
5afbbe0d
AM
3886+ btop = au_ibtop(i);
3887+ bbot = au_ibbot(i);
3888+ if (btop <= bindex
3889+ && bindex <= bbot
1facf9fc 3890+ && au_h_iptr(i, bindex)
5afbbe0d 3891+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3892+ err = -EBUSY;
3893+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3894+ AuDbgInode(i);
1facf9fc 3895+ }
3896+ ii_read_unlock(i);
3897+ }
7f207e10 3898+ au_iarray_free(array, max);
1facf9fc 3899+
7f207e10 3900+out:
1facf9fc 3901+ return err;
3902+}
3903+
b752ccd1
AM
3904+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3905+ const unsigned int verbose)
1facf9fc 3906+{
3907+ int err;
3908+ unsigned int sigen;
3909+
3910+ sigen = au_sigen(root->d_sb);
3911+ DiMustNoWaiters(root);
5527c038 3912+ IiMustNoWaiters(d_inode(root));
1facf9fc 3913+ di_write_unlock(root);
b752ccd1 3914+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3915+ if (!err)
b752ccd1 3916+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3917+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3918+
3919+ return err;
3920+}
3921+
076b876e
AM
3922+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3923+ struct file **to_free, int *idx)
3924+{
3925+ int err;
c1595e42 3926+ unsigned char matched, root;
5afbbe0d 3927+ aufs_bindex_t bindex, bbot;
076b876e
AM
3928+ struct au_fidir *fidir;
3929+ struct au_hfile *hfile;
3930+
3931+ err = 0;
2000de60 3932+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3933+ if (root) {
3934+ get_file(file);
3935+ to_free[*idx] = file;
3936+ (*idx)++;
3937+ goto out;
3938+ }
3939+
076b876e 3940+ matched = 0;
076b876e
AM
3941+ fidir = au_fi(file)->fi_hdir;
3942+ AuDebugOn(!fidir);
5afbbe0d
AM
3943+ bbot = au_fbbot_dir(file);
3944+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3945+ hfile = fidir->fd_hfile + bindex;
3946+ if (!hfile->hf_file)
3947+ continue;
3948+
c1595e42 3949+ if (hfile->hf_br->br_id == br_id) {
076b876e 3950+ matched = 1;
076b876e 3951+ break;
c1595e42 3952+ }
076b876e 3953+ }
c1595e42 3954+ if (matched)
076b876e
AM
3955+ err = -EBUSY;
3956+
3957+out:
3958+ return err;
3959+}
3960+
3961+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3962+ struct file **to_free, int opened)
3963+{
3964+ int err, idx;
3965+ unsigned long long ull, max;
5afbbe0d 3966+ aufs_bindex_t btop;
076b876e 3967+ struct file *file, **array;
076b876e
AM
3968+ struct dentry *root;
3969+ struct au_hfile *hfile;
3970+
3971+ array = au_farray_alloc(sb, &max);
3972+ err = PTR_ERR(array);
3973+ if (IS_ERR(array))
3974+ goto out;
3975+
3976+ err = 0;
3977+ idx = 0;
3978+ root = sb->s_root;
3979+ di_write_unlock(root);
3980+ for (ull = 0; ull < max; ull++) {
3981+ file = array[ull];
3982+ if (unlikely(!file))
3983+ break;
3984+
3985+ /* AuDbg("%pD\n", file); */
3986+ fi_read_lock(file);
5afbbe0d 3987+ btop = au_fbtop(file);
2000de60 3988+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3989+ hfile = &au_fi(file)->fi_htop;
3990+ if (hfile->hf_br->br_id == br_id)
3991+ err = -EBUSY;
3992+ } else
3993+ err = test_dir_busy(file, br_id, to_free, &idx);
3994+ fi_read_unlock(file);
3995+ if (unlikely(err))
3996+ break;
3997+ }
3998+ di_write_lock_child(root);
3999+ au_farray_free(array, max);
4000+ AuDebugOn(idx > opened);
4001+
4002+out:
4003+ return err;
4004+}
4005+
4006+static void br_del_file(struct file **to_free, unsigned long long opened,
062440b3 4007+ aufs_bindex_t br_id)
076b876e
AM
4008+{
4009+ unsigned long long ull;
5afbbe0d 4010+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
4011+ struct file *file;
4012+ struct au_fidir *fidir;
4013+ struct au_hfile *hfile;
4014+
4015+ for (ull = 0; ull < opened; ull++) {
4016+ file = to_free[ull];
4017+ if (unlikely(!file))
4018+ break;
4019+
4020+ /* AuDbg("%pD\n", file); */
2000de60 4021+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
4022+ bfound = -1;
4023+ fidir = au_fi(file)->fi_hdir;
4024+ AuDebugOn(!fidir);
4025+ fi_write_lock(file);
5afbbe0d
AM
4026+ btop = au_fbtop(file);
4027+ bbot = au_fbbot_dir(file);
4028+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
4029+ hfile = fidir->fd_hfile + bindex;
4030+ if (!hfile->hf_file)
4031+ continue;
4032+
4033+ if (hfile->hf_br->br_id == br_id) {
4034+ bfound = bindex;
4035+ break;
4036+ }
4037+ }
4038+ AuDebugOn(bfound < 0);
4039+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
4040+ if (bfound == btop) {
4041+ for (btop++; btop <= bbot; btop++)
4042+ if (au_hf_dir(file, btop)) {
4043+ au_set_fbtop(file, btop);
076b876e
AM
4044+ break;
4045+ }
4046+ }
4047+ fi_write_unlock(file);
4048+ }
4049+}
4050+
1facf9fc 4051+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
4052+ const aufs_bindex_t bindex,
5afbbe0d 4053+ const aufs_bindex_t bbot)
1facf9fc 4054+{
4055+ struct au_branch **brp, **p;
4056+
dece6358
AM
4057+ AuRwMustWriteLock(&sbinfo->si_rwsem);
4058+
1facf9fc 4059+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
4060+ if (bindex < bbot)
4061+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
4062+ sbinfo->si_branch[0 + bbot] = NULL;
4063+ sbinfo->si_bbot--;
1facf9fc 4064+
e2f27e51
AM
4065+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
4066+ /*may_shrink*/1);
1facf9fc 4067+ if (p)
4068+ sbinfo->si_branch = p;
4a4d8108 4069+ /* harmless error */
1facf9fc 4070+}
4071+
4072+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 4073+ const aufs_bindex_t bbot)
1facf9fc 4074+{
4075+ struct au_hdentry *hdp, *p;
4076+
1308ab2a 4077+ AuRwMustWriteLock(&dinfo->di_rwsem);
4078+
5afbbe0d
AM
4079+ hdp = au_hdentry(dinfo, bindex);
4080+ if (bindex < bbot)
4081+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
4082+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
4083+ dinfo->di_bbot--;
1facf9fc 4084+
e2f27e51
AM
4085+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
4086+ /*may_shrink*/1);
1facf9fc 4087+ if (p)
4088+ dinfo->di_hdentry = p;
4a4d8108 4089+ /* harmless error */
1facf9fc 4090+}
4091+
4092+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 4093+ const aufs_bindex_t bbot)
1facf9fc 4094+{
4095+ struct au_hinode *hip, *p;
4096+
1308ab2a 4097+ AuRwMustWriteLock(&iinfo->ii_rwsem);
4098+
5afbbe0d
AM
4099+ hip = au_hinode(iinfo, bindex);
4100+ if (bindex < bbot)
4101+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
4102+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
4103+ iinfo->ii_bbot--;
1facf9fc 4104+
e2f27e51
AM
4105+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
4106+ /*may_shrink*/1);
1facf9fc 4107+ if (p)
4108+ iinfo->ii_hinode = p;
4a4d8108 4109+ /* harmless error */
1facf9fc 4110+}
4111+
4112+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
4113+ struct au_branch *br)
4114+{
5afbbe0d 4115+ aufs_bindex_t bbot;
1facf9fc 4116+ struct au_sbinfo *sbinfo;
53392da6
AM
4117+ struct dentry *root, *h_root;
4118+ struct inode *inode, *h_inode;
4119+ struct au_hinode *hinode;
1facf9fc 4120+
dece6358
AM
4121+ SiMustWriteLock(sb);
4122+
1facf9fc 4123+ root = sb->s_root;
5527c038 4124+ inode = d_inode(root);
1facf9fc 4125+ sbinfo = au_sbi(sb);
5afbbe0d 4126+ bbot = sbinfo->si_bbot;
1facf9fc 4127+
53392da6
AM
4128+ h_root = au_h_dptr(root, bindex);
4129+ hinode = au_hi(inode, bindex);
4130+ h_inode = au_igrab(hinode->hi_inode);
4131+ au_hiput(hinode);
1facf9fc 4132+
53392da6 4133+ au_sbilist_lock();
5afbbe0d
AM
4134+ au_br_do_del_brp(sbinfo, bindex, bbot);
4135+ au_br_do_del_hdp(au_di(root), bindex, bbot);
4136+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
4137+ au_sbilist_unlock();
4138+
8b6a4947
AM
4139+ /* ignore an error */
4140+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
4141+
53392da6
AM
4142+ dput(h_root);
4143+ iput(h_inode);
4144+ au_br_do_free(br);
1facf9fc 4145+}
4146+
79b8bda9
AM
4147+static unsigned long long empty_cb(struct super_block *sb, void *array,
4148+ unsigned long long max, void *arg)
076b876e
AM
4149+{
4150+ return max;
4151+}
4152+
1facf9fc 4153+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4154+{
4155+ int err, rerr, i;
076b876e 4156+ unsigned long long opened;
1facf9fc 4157+ unsigned int mnt_flags;
5afbbe0d 4158+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 4159+ unsigned char do_wh, verbose;
4160+ struct au_branch *br;
4161+ struct au_wbr *wbr;
076b876e
AM
4162+ struct dentry *root;
4163+ struct file **to_free;
1facf9fc 4164+
4165+ err = 0;
076b876e
AM
4166+ opened = 0;
4167+ to_free = NULL;
4168+ root = sb->s_root;
4169+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 4170+ if (bindex < 0) {
4171+ if (remount)
4172+ goto out; /* success */
4173+ err = -ENOENT;
4a4d8108 4174+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 4175+ goto out;
4176+ }
4177+ AuDbg("bindex b%d\n", bindex);
4178+
4179+ err = -EBUSY;
4180+ mnt_flags = au_mntflags(sb);
4181+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
4182+ bbot = au_sbbot(sb);
4183+ if (unlikely(!bbot)) {
1facf9fc 4184+ AuVerbose(verbose, "no more branches left\n");
4185+ goto out;
4186+ }
4187+ br = au_sbr(sb, bindex);
86dc4139 4188+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
076b876e
AM
4189+
4190+ br_id = br->br_id;
5afbbe0d 4191+ opened = au_br_count(br);
076b876e 4192+ if (unlikely(opened)) {
79b8bda9 4193+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
4194+ err = PTR_ERR(to_free);
4195+ if (IS_ERR(to_free))
4196+ goto out;
4197+
4198+ err = test_file_busy(sb, br_id, to_free, opened);
4199+ if (unlikely(err)) {
4200+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
4201+ goto out;
4202+ }
1facf9fc 4203+ }
4204+
4205+ wbr = br->br_wbr;
4206+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4207+ if (do_wh) {
1308ab2a 4208+ /* instead of WbrWhMustWriteLock(wbr) */
4209+ SiMustWriteLock(sb);
1facf9fc 4210+ for (i = 0; i < AuBrWh_Last; i++) {
4211+ dput(wbr->wbr_wh[i]);
4212+ wbr->wbr_wh[i] = NULL;
4213+ }
4214+ }
4215+
076b876e 4216+ err = test_children_busy(root, bindex, verbose);
1facf9fc 4217+ if (unlikely(err)) {
4218+ if (do_wh)
4219+ goto out_wh;
4220+ goto out;
4221+ }
4222+
4223+ err = 0;
076b876e
AM
4224+ if (to_free) {
4225+ /*
4226+ * now we confirmed the branch is deletable.
4227+ * let's free the remaining opened dirs on the branch.
4228+ */
4229+ di_write_unlock(root);
4230+ br_del_file(to_free, opened, br_id);
4231+ di_write_lock_child(root);
4232+ }
4233+
062440b3
AM
4234+ sysaufs_brs_del(sb, bindex); /* remove successors */
4235+ dbgaufs_xino_del(br); /* remove one */
4236+ au_br_do_del(sb, bindex, br);
4237+ sysaufs_brs_add(sb, bindex); /* append successors */
4238+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
1facf9fc 4239+
1308ab2a 4240+ if (!bindex) {
5527c038 4241+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4242+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4243+ } else
5527c038 4244+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4245+ if (au_opt_test(mnt_flags, PLINK))
4246+ au_plink_half_refresh(sb, br_id);
4247+
b752ccd1 4248+ if (au_xino_brid(sb) == br_id)
1facf9fc 4249+ au_xino_brid_set(sb, -1);
4250+ goto out; /* success */
4251+
4f0767ce 4252+out_wh:
1facf9fc 4253+ /* revert */
86dc4139 4254+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4255+ if (rerr)
0c3ec466
AM
4256+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4257+ del->pathname, rerr);
4f0767ce 4258+out:
076b876e
AM
4259+ if (to_free)
4260+ au_farray_free(to_free, opened);
1facf9fc 4261+ return err;
4262+}
4263+
4264+/* ---------------------------------------------------------------------- */
4265+
027c5e7a
AM
4266+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4267+{
4268+ int err;
5afbbe0d 4269+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4270+ struct aufs_ibusy ibusy;
4271+ struct inode *inode, *h_inode;
4272+
4273+ err = -EPERM;
4274+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4275+ goto out;
4276+
4277+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4278+ if (!err)
4279+ err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4280+ if (unlikely(err)) {
4281+ err = -EFAULT;
4282+ AuTraceErr(err);
4283+ goto out;
4284+ }
4285+
4286+ err = -EINVAL;
4287+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4288+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4289+ goto out_unlock;
4290+
4291+ err = 0;
4292+ ibusy.h_ino = 0; /* invalid */
4293+ inode = ilookup(sb, ibusy.ino);
4294+ if (!inode
4295+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4296+ || au_is_bad_inode(inode))
027c5e7a
AM
4297+ goto out_unlock;
4298+
4299+ ii_read_lock_child(inode);
5afbbe0d
AM
4300+ btop = au_ibtop(inode);
4301+ bbot = au_ibbot(inode);
4302+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4303+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4304+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4305+ ibusy.h_ino = h_inode->i_ino;
4306+ }
4307+ ii_read_unlock(inode);
4308+ iput(inode);
4309+
4310+out_unlock:
4311+ si_read_unlock(sb);
4312+ if (!err) {
4313+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4314+ if (unlikely(err)) {
4315+ err = -EFAULT;
4316+ AuTraceErr(err);
4317+ }
4318+ }
4319+out:
4320+ return err;
4321+}
4322+
4323+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4324+{
2000de60 4325+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4326+}
4327+
4328+#ifdef CONFIG_COMPAT
4329+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4330+{
2000de60 4331+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4332+}
4333+#endif
4334+
4335+/* ---------------------------------------------------------------------- */
4336+
1facf9fc 4337+/*
4338+ * change a branch permission
4339+ */
4340+
dece6358
AM
4341+static void au_warn_ima(void)
4342+{
4343+#ifdef CONFIG_IMA
1308ab2a 4344+ /* since it doesn't support mark_files_ro() */
027c5e7a 4345+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4346+#endif
4347+}
4348+
1facf9fc 4349+static int do_need_sigen_inc(int a, int b)
4350+{
4351+ return au_br_whable(a) && !au_br_whable(b);
4352+}
4353+
4354+static int need_sigen_inc(int old, int new)
4355+{
4356+ return do_need_sigen_inc(old, new)
4357+ || do_need_sigen_inc(new, old);
4358+}
4359+
4360+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4361+{
7f207e10 4362+ int err, do_warn;
027c5e7a 4363+ unsigned int mnt_flags;
7f207e10 4364+ unsigned long long ull, max;
e49829fe 4365+ aufs_bindex_t br_id;
38d290e6 4366+ unsigned char verbose, writer;
7f207e10 4367+ struct file *file, *hf, **array;
e49829fe 4368+ struct au_hfile *hfile;
1facf9fc 4369+
027c5e7a
AM
4370+ mnt_flags = au_mntflags(sb);
4371+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4372+
7f207e10
AM
4373+ array = au_farray_alloc(sb, &max);
4374+ err = PTR_ERR(array);
4375+ if (IS_ERR(array))
1facf9fc 4376+ goto out;
4377+
7f207e10 4378+ do_warn = 0;
e49829fe 4379+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4380+ for (ull = 0; ull < max; ull++) {
4381+ file = array[ull];
076b876e
AM
4382+ if (unlikely(!file))
4383+ break;
1facf9fc 4384+
523b37e3 4385+ /* AuDbg("%pD\n", file); */
1facf9fc 4386+ fi_read_lock(file);
4387+ if (unlikely(au_test_mmapped(file))) {
4388+ err = -EBUSY;
523b37e3 4389+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4390+ AuDbgFile(file);
1facf9fc 4391+ FiMustNoWaiters(file);
4392+ fi_read_unlock(file);
7f207e10 4393+ goto out_array;
1facf9fc 4394+ }
4395+
e49829fe
JR
4396+ hfile = &au_fi(file)->fi_htop;
4397+ hf = hfile->hf_file;
7e9cd9fe 4398+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4399+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4400+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4401+ || !(hf->f_mode & FMODE_WRITE))
4402+ array[ull] = NULL;
4403+ else {
4404+ do_warn = 1;
4405+ get_file(file);
1facf9fc 4406+ }
4407+
1facf9fc 4408+ FiMustNoWaiters(file);
4409+ fi_read_unlock(file);
7f207e10
AM
4410+ fput(file);
4411+ }
1facf9fc 4412+
4413+ err = 0;
7f207e10 4414+ if (do_warn)
dece6358 4415+ au_warn_ima();
7f207e10
AM
4416+
4417+ for (ull = 0; ull < max; ull++) {
4418+ file = array[ull];
4419+ if (!file)
4420+ continue;
4421+
1facf9fc 4422+ /* todo: already flushed? */
523b37e3
AM
4423+ /*
4424+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4425+ * approach which resets f_mode and calls mnt_drop_write() and
4426+ * file_release_write() for each file, because the branch
4427+ * attribute in aufs world is totally different from the native
4428+ * fs rw/ro mode.
4429+ */
7f207e10
AM
4430+ /* fi_read_lock(file); */
4431+ hfile = &au_fi(file)->fi_htop;
4432+ hf = hfile->hf_file;
4433+ /* fi_read_unlock(file); */
027c5e7a 4434+ spin_lock(&hf->f_lock);
38d290e6
JR
4435+ writer = !!(hf->f_mode & FMODE_WRITER);
4436+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4437+ spin_unlock(&hf->f_lock);
38d290e6
JR
4438+ if (writer) {
4439+ put_write_access(file_inode(hf));
c06a8ce3 4440+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4441+ }
4442+ }
4443+
7f207e10
AM
4444+out_array:
4445+ au_farray_free(array, max);
4f0767ce 4446+out:
7f207e10 4447+ AuTraceErr(err);
1facf9fc 4448+ return err;
4449+}
4450+
4451+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4452+ int *do_refresh)
1facf9fc 4453+{
4454+ int err, rerr;
4455+ aufs_bindex_t bindex;
4456+ struct dentry *root;
4457+ struct au_branch *br;
076b876e 4458+ struct au_br_fhsm *bf;
1facf9fc 4459+
4460+ root = sb->s_root;
1facf9fc 4461+ bindex = au_find_dbindex(root, mod->h_root);
4462+ if (bindex < 0) {
4463+ if (remount)
4464+ return 0; /* success */
4465+ err = -ENOENT;
4a4d8108 4466+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4467+ goto out;
4468+ }
4469+ AuDbg("bindex b%d\n", bindex);
4470+
5527c038 4471+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4472+ if (unlikely(err))
4473+ goto out;
4474+
4475+ br = au_sbr(sb, bindex);
86dc4139 4476+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4477+ if (br->br_perm == mod->perm)
4478+ return 0; /* success */
4479+
076b876e
AM
4480+ /* pre-allocate for non-fhsm --> fhsm */
4481+ bf = NULL;
4482+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4483+ err = au_fhsm_br_alloc(br);
4484+ if (unlikely(err))
4485+ goto out;
4486+ bf = br->br_fhsm;
4487+ br->br_fhsm = NULL;
4488+ }
4489+
1facf9fc 4490+ if (au_br_writable(br->br_perm)) {
4491+ /* remove whiteout base */
86dc4139 4492+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4493+ if (unlikely(err))
076b876e 4494+ goto out_bf;
1facf9fc 4495+
4496+ if (!au_br_writable(mod->perm)) {
4497+ /* rw --> ro, file might be mmapped */
4498+ DiMustNoWaiters(root);
5527c038 4499+ IiMustNoWaiters(d_inode(root));
1facf9fc 4500+ di_write_unlock(root);
4501+ err = au_br_mod_files_ro(sb, bindex);
4502+ /* aufs_write_lock() calls ..._child() */
4503+ di_write_lock_child(root);
4504+
4505+ if (unlikely(err)) {
4506+ rerr = -ENOMEM;
be52b249 4507+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4508+ GFP_NOFS);
86dc4139
AM
4509+ if (br->br_wbr)
4510+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4511+ if (unlikely(rerr)) {
4512+ AuIOErr("nested error %d (%d)\n",
4513+ rerr, err);
4514+ br->br_perm = mod->perm;
4515+ }
4516+ }
4517+ }
4518+ } else if (au_br_writable(mod->perm)) {
4519+ /* ro --> rw */
4520+ err = -ENOMEM;
be52b249 4521+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4522+ if (br->br_wbr) {
86dc4139 4523+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4524+ if (unlikely(err)) {
1c60b727 4525+ kfree(br->br_wbr);
1facf9fc 4526+ br->br_wbr = NULL;
4527+ }
4528+ }
4529+ }
076b876e
AM
4530+ if (unlikely(err))
4531+ goto out_bf;
4532+
4533+ if (au_br_fhsm(br->br_perm)) {
4534+ if (!au_br_fhsm(mod->perm)) {
4535+ /* fhsm --> non-fhsm */
4536+ au_br_fhsm_fin(br->br_fhsm);
1c60b727 4537+ kfree(br->br_fhsm);
076b876e
AM
4538+ br->br_fhsm = NULL;
4539+ }
4540+ } else if (au_br_fhsm(mod->perm))
4541+ /* non-fhsm --> fhsm */
4542+ br->br_fhsm = bf;
4543+
076b876e
AM
4544+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4545+ br->br_perm = mod->perm;
4546+ goto out; /* success */
1facf9fc 4547+
076b876e 4548+out_bf:
8b6a4947 4549+ kfree(bf);
076b876e
AM
4550+out:
4551+ AuTraceErr(err);
4552+ return err;
4553+}
4554+
4555+/* ---------------------------------------------------------------------- */
4556+
4557+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4558+{
4559+ int err;
4560+ struct kstatfs kstfs;
4561+
4562+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4563+ if (!err) {
076b876e
AM
4564+ stfs->f_blocks = kstfs.f_blocks;
4565+ stfs->f_bavail = kstfs.f_bavail;
4566+ stfs->f_files = kstfs.f_files;
4567+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4568+ }
4569+
1facf9fc 4570+ return err;
4571+}
7f207e10
AM
4572diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4573--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
4574+++ linux/fs/aufs/branch.h 2018-07-19 09:46:13.053314374 +0200
4575@@ -0,0 +1,374 @@
4576+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 4577+/*
b00004a5 4578+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 4579+ *
4580+ * This program, aufs is free software; you can redistribute it and/or modify
4581+ * it under the terms of the GNU General Public License as published by
4582+ * the Free Software Foundation; either version 2 of the License, or
4583+ * (at your option) any later version.
dece6358
AM
4584+ *
4585+ * This program is distributed in the hope that it will be useful,
4586+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4587+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4588+ * GNU General Public License for more details.
4589+ *
4590+ * You should have received a copy of the GNU General Public License
523b37e3 4591+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4592+ */
4593+
4594+/*
4595+ * branch filesystems and xino for them
4596+ */
4597+
4598+#ifndef __AUFS_BRANCH_H__
4599+#define __AUFS_BRANCH_H__
4600+
4601+#ifdef __KERNEL__
4602+
1facf9fc 4603+#include <linux/mount.h>
8b6a4947 4604+#include "dirren.h"
4a4d8108 4605+#include "dynop.h"
1facf9fc 4606+#include "rwsem.h"
4607+#include "super.h"
4608+
4609+/* ---------------------------------------------------------------------- */
4610+
4611+/* a xino file */
062440b3 4612+struct au_xino {
1facf9fc 4613+ struct file *xi_file;
521ced18
JR
4614+ struct {
4615+ spinlock_t spin;
4616+ ino_t *array;
4617+ int total;
4618+ /* reserved for future use */
4619+ /* unsigned long *bitmap; */
4620+ wait_queue_head_t wqh;
4621+ } xi_nondir;
1facf9fc 4622+
062440b3 4623+ atomic_t xi_truncating;
1facf9fc 4624+
062440b3
AM
4625+ struct kref xi_kref;
4626+
4627+ /* todo: make xino files an array to support huge inode number */
1facf9fc 4628+};
4629+
076b876e
AM
4630+/* File-based Hierarchical Storage Management */
4631+struct au_br_fhsm {
4632+#ifdef CONFIG_AUFS_FHSM
4633+ struct mutex bf_lock;
4634+ unsigned long bf_jiffy;
4635+ struct aufs_stfs bf_stfs;
4636+ int bf_readable;
4637+#endif
4638+};
4639+
1facf9fc 4640+/* members for writable branch only */
4641+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4642+struct au_wbr {
dece6358 4643+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4644+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4645+ atomic_t wbr_wh_running;
1facf9fc 4646+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4647+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4648+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4649+
4650+ /* mfs mode */
4651+ unsigned long long wbr_bytes;
4652+};
4653+
4a4d8108
AM
4654+/* ext2 has 3 types of operations at least, ext3 has 4 */
4655+#define AuBrDynOp (AuDyLast * 4)
4656+
1716fcea
AM
4657+#ifdef CONFIG_AUFS_HFSNOTIFY
4658+/* support for asynchronous destruction */
4659+struct au_br_hfsnotify {
4660+ struct fsnotify_group *hfsn_group;
4661+};
4662+#endif
4663+
392086de
AM
4664+/* sysfs entries */
4665+struct au_brsysfs {
4666+ char name[16];
4667+ struct attribute attr;
4668+};
4669+
4670+enum {
4671+ AuBrSysfs_BR,
4672+ AuBrSysfs_BRID,
4673+ AuBrSysfs_Last
4674+};
4675+
1facf9fc 4676+/* protected by superblock rwsem */
4677+struct au_branch {
062440b3 4678+ struct au_xino *br_xino;
1facf9fc 4679+
4680+ aufs_bindex_t br_id;
4681+
4682+ int br_perm;
86dc4139 4683+ struct path br_path;
4a4d8108
AM
4684+ spinlock_t br_dykey_lock;
4685+ struct au_dykey *br_dykey[AuBrDynOp];
5afbbe0d 4686+ struct percpu_counter br_count;
1facf9fc 4687+
4688+ struct au_wbr *br_wbr;
076b876e 4689+ struct au_br_fhsm *br_fhsm;
1facf9fc 4690+
027c5e7a 4691+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4692+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4693+#endif
4694+
1facf9fc 4695+#ifdef CONFIG_SYSFS
392086de
AM
4696+ /* entries under sysfs per mount-point */
4697+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4698+#endif
8b6a4947 4699+
062440b3
AM
4700+#ifdef CONFIG_DEBUG_FS
4701+ struct dentry *br_dbgaufs; /* xino */
4702+#endif
4703+
8b6a4947 4704+ struct au_dr_br br_dirren;
1facf9fc 4705+};
4706+
4707+/* ---------------------------------------------------------------------- */
4708+
86dc4139
AM
4709+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4710+{
4711+ return br->br_path.mnt;
4712+}
4713+
4714+static inline struct dentry *au_br_dentry(struct au_branch *br)
4715+{
4716+ return br->br_path.dentry;
4717+}
4718+
4719+static inline struct super_block *au_br_sb(struct au_branch *br)
4720+{
4721+ return au_br_mnt(br)->mnt_sb;
4722+}
4723+
5afbbe0d
AM
4724+static inline void au_br_get(struct au_branch *br)
4725+{
4726+ percpu_counter_inc(&br->br_count);
4727+}
4728+
4729+static inline void au_br_put(struct au_branch *br)
4730+{
4731+ percpu_counter_dec(&br->br_count);
4732+}
4733+
4734+static inline s64 au_br_count(struct au_branch *br)
4735+{
4736+ return percpu_counter_sum(&br->br_count);
4737+}
4738+
4739+static inline void au_br_count_init(struct au_branch *br)
4740+{
4741+ percpu_counter_init(&br->br_count, 0, GFP_NOFS);
4742+}
4743+
4744+static inline void au_br_count_fin(struct au_branch *br)
4745+{
4746+ percpu_counter_destroy(&br->br_count);
4747+}
4748+
1facf9fc 4749+static inline int au_br_rdonly(struct au_branch *br)
4750+{
8b6a4947 4751+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4752+ || !au_br_writable(br->br_perm))
4753+ ? -EROFS : 0;
4754+}
4755+
4a4d8108 4756+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4757+{
4a4d8108 4758+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4759+ return !(brperm & AuBrPerm_RR);
1facf9fc 4760+#else
4761+ return 0;
4762+#endif
4763+}
4764+
b912730e
AM
4765+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4766+{
4767+ int err, exec_flag;
4768+
4769+ err = 0;
4770+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4771+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4772+ err = -EACCES;
4773+
4774+ return err;
4775+}
4776+
062440b3
AM
4777+static inline void au_xino_get(struct au_branch *br)
4778+{
4779+ struct au_xino *xi;
4780+
4781+ xi = br->br_xino;
4782+ if (xi)
4783+ kref_get(&xi->xi_kref);
4784+}
4785+
4786+static inline int au_xino_count(struct au_branch *br)
4787+{
4788+ int v;
4789+ struct au_xino *xi;
4790+
4791+ v = 0;
4792+ xi = br->br_xino;
4793+ if (xi)
4794+ v = kref_read(&xi->xi_kref);
4795+
4796+ return v;
4797+}
4798+
4799+static inline struct file *au_xino_file(struct au_branch *br)
4800+{
4801+ struct au_xino *xi;
4802+
4803+ xi = br->br_xino;
4804+ return xi ? xi->xi_file : NULL;
4805+}
4806+
1facf9fc 4807+/* ---------------------------------------------------------------------- */
4808+
4809+/* branch.c */
4810+struct au_sbinfo;
4811+void au_br_free(struct au_sbinfo *sinfo);
4812+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4813+struct au_opt_add;
4814+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4815+struct au_opt_del;
4816+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4817+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4818+#ifdef CONFIG_COMPAT
4819+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4820+#endif
1facf9fc 4821+struct au_opt_mod;
4822+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4823+ int *do_refresh);
076b876e
AM
4824+struct aufs_stfs;
4825+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4826+
4827+/* xino.c */
4828+static const loff_t au_loff_max = LLONG_MAX;
4829+
062440b3
AM
4830+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent);
4831+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4832+ struct file *copy_src);
4833+
4834+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4835+ ino_t *ino);
4836+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4837+ ino_t ino);
5527c038 4838+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4839+ loff_t *pos);
5527c038
JR
4840+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4841+ size_t size, loff_t *pos);
062440b3
AM
4842+
4843+int au_xib_trunc(struct super_block *sb);
1facf9fc 4844+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4845+
062440b3
AM
4846+struct au_xino *au_xino_alloc(void);
4847+int au_xino_put(struct au_branch *br);
4848+void au_xino_file_set(struct au_branch *br, struct file *file);
4849+
1facf9fc 4850+struct au_opt_xino;
1facf9fc 4851+void au_xino_clr(struct super_block *sb);
062440b3 4852+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
1facf9fc 4853+struct file *au_xino_def(struct super_block *sb);
062440b3
AM
4854+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4855+ struct path *base);
4856+
4857+ino_t au_xino_new_ino(struct super_block *sb);
4858+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4859+
521ced18
JR
4860+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4861+ ino_t h_ino, int idx);
4862+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4863+ int *idx);
4864+
062440b3
AM
4865+int au_xino_path(struct seq_file *seq, struct file *file);
4866+
1facf9fc 4867+/* ---------------------------------------------------------------------- */
4868+
4869+/* Superblock to branch */
4870+static inline
4871+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4872+{
4873+ return au_sbr(sb, bindex)->br_id;
4874+}
4875+
4876+static inline
4877+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4878+{
86dc4139 4879+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4880+}
4881+
4882+static inline
4883+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4884+{
86dc4139 4885+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4886+}
4887+
5afbbe0d
AM
4888+static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
4889+{
4890+ au_br_get(au_sbr(sb, bindex));
4891+}
4892+
1facf9fc 4893+static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4894+{
5afbbe0d 4895+ au_br_put(au_sbr(sb, bindex));
1facf9fc 4896+}
4897+
4898+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4899+{
4900+ return au_sbr(sb, bindex)->br_perm;
4901+}
4902+
4903+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4904+{
4905+ return au_br_whable(au_sbr_perm(sb, bindex));
4906+}
4907+
4908+/* ---------------------------------------------------------------------- */
4909+
8b6a4947
AM
4910+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4911+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4912+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4913+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4914+/*
8b6a4947
AM
4915+#define wbr_wh_read_trylock_nested(wbr) \
4916+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4917+#define wbr_wh_write_trylock_nested(wbr) \
4918+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4919+*/
1facf9fc 4920+
8b6a4947
AM
4921+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4922+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4923+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4924+
4925+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4926+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4927+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4928+
076b876e
AM
4929+/* ---------------------------------------------------------------------- */
4930+
4931+#ifdef CONFIG_AUFS_FHSM
4932+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4933+{
4934+ mutex_init(&brfhsm->bf_lock);
4935+ brfhsm->bf_jiffy = 0;
4936+ brfhsm->bf_readable = 0;
4937+}
4938+
4939+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4940+{
4941+ mutex_destroy(&brfhsm->bf_lock);
4942+}
4943+#else
4944+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4945+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4946+#endif
4947+
1facf9fc 4948+#endif /* __KERNEL__ */
4949+#endif /* __AUFS_BRANCH_H__ */
7f207e10
AM
4950diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4951--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
b00004a5 4952+++ linux/fs/aufs/conf.mk 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
4953@@ -0,0 +1,40 @@
4954+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4955+
4956+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4957+
4958+define AuConf
4959+ifdef ${1}
4960+AuConfStr += ${1}=${${1}}
4961+endif
4962+endef
4963+
b752ccd1 4964+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4965+ SBILIST \
7f207e10 4966+ HNOTIFY HFSNOTIFY \
4a4d8108 4967+ EXPORT INO_T_64 \
c1595e42 4968+ XATTR \
076b876e 4969+ FHSM \
4a4d8108 4970+ RDU \
8b6a4947 4971+ DIRREN \
4a4d8108
AM
4972+ SHWH \
4973+ BR_RAMFS \
4974+ BR_FUSE POLL \
4975+ BR_HFSPLUS \
4976+ BDEV_LOOP \
b752ccd1
AM
4977+ DEBUG MAGIC_SYSRQ
4978+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4979+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4980+
4981+AuConfName = ${obj}/conf.str
4982+${AuConfName}.tmp: FORCE
4983+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4984+${AuConfName}: ${AuConfName}.tmp
4985+ @diff -q $< $@ > /dev/null 2>&1 || { \
4986+ echo ' GEN ' $@; \
4987+ cp -p $< $@; \
4988+ }
4989+FORCE:
4990+clean-files += ${AuConfName} ${AuConfName}.tmp
4991+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4992+
4993+-include ${srctree}/${src}/conf_priv.mk
7f207e10
AM
4994diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4995--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
4996+++ linux/fs/aufs/cpup.c 2018-07-19 09:46:13.056647808 +0200
4997@@ -0,0 +1,1442 @@
4998+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 4999+/*
b00004a5 5000+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 5001+ *
5002+ * This program, aufs is free software; you can redistribute it and/or modify
5003+ * it under the terms of the GNU General Public License as published by
5004+ * the Free Software Foundation; either version 2 of the License, or
5005+ * (at your option) any later version.
dece6358
AM
5006+ *
5007+ * This program is distributed in the hope that it will be useful,
5008+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
5009+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5010+ * GNU General Public License for more details.
5011+ *
5012+ * You should have received a copy of the GNU General Public License
523b37e3 5013+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 5014+ */
5015+
5016+/*
5017+ * copy-up functions, see wbr_policy.c for copy-down
5018+ */
5019+
5020+#include <linux/fs_stack.h>
dece6358 5021+#include <linux/mm.h>
8cdd5066 5022+#include <linux/task_work.h>
1facf9fc 5023+#include "aufs.h"
5024+
86dc4139 5025+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 5026+{
5027+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 5028+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 5029+
86dc4139
AM
5030+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
5031+
5032+ dst->i_flags |= iflags & ~mask;
1facf9fc 5033+ if (au_test_fs_notime(dst->i_sb))
5034+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
5035+}
5036+
5037+void au_cpup_attr_timesizes(struct inode *inode)
5038+{
5039+ struct inode *h_inode;
5040+
5afbbe0d 5041+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5042+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 5043+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 5044+}
5045+
5046+void au_cpup_attr_nlink(struct inode *inode, int force)
5047+{
5048+ struct inode *h_inode;
5049+ struct super_block *sb;
5afbbe0d 5050+ aufs_bindex_t bindex, bbot;
1facf9fc 5051+
5052+ sb = inode->i_sb;
5afbbe0d 5053+ bindex = au_ibtop(inode);
1facf9fc 5054+ h_inode = au_h_iptr(inode, bindex);
5055+ if (!force
5056+ && !S_ISDIR(h_inode->i_mode)
5057+ && au_opt_test(au_mntflags(sb), PLINK)
5058+ && au_plink_test(inode))
5059+ return;
5060+
7eafdf33
AM
5061+ /*
5062+ * 0 can happen in revalidating.
38d290e6
JR
5063+ * h_inode->i_mutex may not be held here, but it is harmless since once
5064+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
5065+ * case.
5066+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
5067+ * the incorrect link count.
7eafdf33 5068+ */
92d182d2 5069+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 5070+
5071+ /*
5072+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
5073+ * it may includes whplink directory.
5074+ */
5075+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
5076+ bbot = au_ibbot(inode);
5077+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 5078+ h_inode = au_h_iptr(inode, bindex);
5079+ if (h_inode)
5080+ au_add_nlink(inode, h_inode);
5081+ }
5082+ }
5083+}
5084+
5085+void au_cpup_attr_changeable(struct inode *inode)
5086+{
5087+ struct inode *h_inode;
5088+
5afbbe0d 5089+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5090+ inode->i_mode = h_inode->i_mode;
5091+ inode->i_uid = h_inode->i_uid;
5092+ inode->i_gid = h_inode->i_gid;
5093+ au_cpup_attr_timesizes(inode);
86dc4139 5094+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 5095+}
5096+
5097+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
5098+{
5099+ struct au_iinfo *iinfo = au_ii(inode);
5100+
1308ab2a 5101+ IiMustWriteLock(inode);
5102+
1facf9fc 5103+ iinfo->ii_higen = h_inode->i_generation;
5104+ iinfo->ii_hsb1 = h_inode->i_sb;
5105+}
5106+
5107+void au_cpup_attr_all(struct inode *inode, int force)
5108+{
5109+ struct inode *h_inode;
5110+
5afbbe0d 5111+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5112+ au_cpup_attr_changeable(inode);
5113+ if (inode->i_nlink > 0)
5114+ au_cpup_attr_nlink(inode, force);
5115+ inode->i_rdev = h_inode->i_rdev;
5116+ inode->i_blkbits = h_inode->i_blkbits;
5117+ au_cpup_igen(inode, h_inode);
5118+}
5119+
5120+/* ---------------------------------------------------------------------- */
5121+
5122+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
5123+
5124+/* keep the timestamps of the parent dir when cpup */
5125+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5126+ struct path *h_path)
5127+{
5128+ struct inode *h_inode;
5129+
5130+ dt->dt_dentry = dentry;
5131+ dt->dt_h_path = *h_path;
5527c038 5132+ h_inode = d_inode(h_path->dentry);
1facf9fc 5133+ dt->dt_atime = h_inode->i_atime;
5134+ dt->dt_mtime = h_inode->i_mtime;
5135+ /* smp_mb(); */
5136+}
5137+
5138+void au_dtime_revert(struct au_dtime *dt)
5139+{
5140+ struct iattr attr;
5141+ int err;
5142+
5143+ attr.ia_atime = dt->dt_atime;
5144+ attr.ia_mtime = dt->dt_mtime;
5145+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5146+ | ATTR_ATIME | ATTR_ATIME_SET;
5147+
523b37e3
AM
5148+ /* no delegation since this is a directory */
5149+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 5150+ if (unlikely(err))
0c3ec466 5151+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 5152+}
5153+
5154+/* ---------------------------------------------------------------------- */
5155+
86dc4139
AM
5156+/* internal use only */
5157+struct au_cpup_reg_attr {
5158+ int valid;
5159+ struct kstat st;
5160+ unsigned int iflags; /* inode->i_flags */
5161+};
5162+
1facf9fc 5163+static noinline_for_stack
86dc4139
AM
5164+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5165+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5166+{
c1595e42 5167+ int err, sbits, icex;
7e9cd9fe
AM
5168+ unsigned int mnt_flags;
5169+ unsigned char verbose;
1facf9fc 5170+ struct iattr ia;
5171+ struct path h_path;
1308ab2a 5172+ struct inode *h_isrc, *h_idst;
86dc4139 5173+ struct kstat *h_st;
c1595e42 5174+ struct au_branch *br;
1facf9fc 5175+
5176+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 5177+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
5178+ br = au_sbr(dst->d_sb, bindex);
5179+ h_path.mnt = au_br_mnt(br);
5527c038 5180+ h_isrc = d_inode(h_src);
1308ab2a 5181+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 5182+ | ATTR_ATIME | ATTR_MTIME
5183+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
5184+ if (h_src_attr && h_src_attr->valid) {
5185+ h_st = &h_src_attr->st;
5186+ ia.ia_uid = h_st->uid;
5187+ ia.ia_gid = h_st->gid;
5188+ ia.ia_atime = h_st->atime;
5189+ ia.ia_mtime = h_st->mtime;
5190+ if (h_idst->i_mode != h_st->mode
5191+ && !S_ISLNK(h_idst->i_mode)) {
5192+ ia.ia_valid |= ATTR_MODE;
5193+ ia.ia_mode = h_st->mode;
5194+ }
5195+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5196+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5197+ } else {
5198+ ia.ia_uid = h_isrc->i_uid;
5199+ ia.ia_gid = h_isrc->i_gid;
5200+ ia.ia_atime = h_isrc->i_atime;
5201+ ia.ia_mtime = h_isrc->i_mtime;
5202+ if (h_idst->i_mode != h_isrc->i_mode
5203+ && !S_ISLNK(h_idst->i_mode)) {
5204+ ia.ia_valid |= ATTR_MODE;
5205+ ia.ia_mode = h_isrc->i_mode;
5206+ }
5207+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5208+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 5209+ }
523b37e3
AM
5210+ /* no delegation since it is just created */
5211+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5212+
5213+ /* is this nfs only? */
5214+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5215+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5216+ ia.ia_mode = h_isrc->i_mode;
523b37e3 5217+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5218+ }
5219+
c1595e42 5220+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
5221+ if (!err) {
5222+ mnt_flags = au_mntflags(dst->d_sb);
5223+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5224+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5225+ }
c1595e42 5226+
1facf9fc 5227+ return err;
5228+}
5229+
5230+/* ---------------------------------------------------------------------- */
5231+
5232+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5233+ char *buf, unsigned long blksize)
5234+{
5235+ int err;
5236+ size_t sz, rbytes, wbytes;
5237+ unsigned char all_zero;
5238+ char *p, *zp;
febd17d6 5239+ struct inode *h_inode;
1facf9fc 5240+ /* reduce stack usage */
5241+ struct iattr *ia;
5242+
5243+ zp = page_address(ZERO_PAGE(0));
5244+ if (unlikely(!zp))
5245+ return -ENOMEM; /* possible? */
5246+
5247+ err = 0;
5248+ all_zero = 0;
5249+ while (len) {
5250+ AuDbg("len %lld\n", len);
5251+ sz = blksize;
5252+ if (len < blksize)
5253+ sz = len;
5254+
5255+ rbytes = 0;
5256+ /* todo: signal_pending? */
5257+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5258+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5259+ err = rbytes;
5260+ }
5261+ if (unlikely(err < 0))
5262+ break;
5263+
5264+ all_zero = 0;
5265+ if (len >= rbytes && rbytes == blksize)
5266+ all_zero = !memcmp(buf, zp, rbytes);
5267+ if (!all_zero) {
5268+ wbytes = rbytes;
5269+ p = buf;
5270+ while (wbytes) {
5271+ size_t b;
5272+
5273+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5274+ err = b;
5275+ /* todo: signal_pending? */
5276+ if (unlikely(err == -EAGAIN || err == -EINTR))
5277+ continue;
5278+ if (unlikely(err < 0))
5279+ break;
5280+ wbytes -= b;
5281+ p += b;
5282+ }
392086de
AM
5283+ if (unlikely(err < 0))
5284+ break;
1facf9fc 5285+ } else {
5286+ loff_t res;
5287+
5288+ AuLabel(hole);
5289+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5290+ err = res;
5291+ if (unlikely(res < 0))
5292+ break;
5293+ }
5294+ len -= rbytes;
5295+ err = 0;
5296+ }
5297+
5298+ /* the last block may be a hole */
5299+ if (!err && all_zero) {
5300+ AuLabel(last hole);
5301+
5302+ err = 1;
2000de60 5303+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5304+ /* nfs requires this step to make last hole */
5305+ /* is this only nfs? */
5306+ do {
5307+ /* todo: signal_pending? */
5308+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5309+ } while (err == -EAGAIN || err == -EINTR);
5310+ if (err == 1)
5311+ dst->f_pos--;
5312+ }
5313+
5314+ if (err == 1) {
5315+ ia = (void *)buf;
5316+ ia->ia_size = dst->f_pos;
5317+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5318+ ia->ia_file = dst;
febd17d6
JR
5319+ h_inode = file_inode(dst);
5320+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5321+ /* no delegation since it is just created */
5322+ err = vfsub_notify_change(&dst->f_path, ia,
5323+ /*delegated*/NULL);
febd17d6 5324+ inode_unlock(h_inode);
1facf9fc 5325+ }
5326+ }
5327+
5328+ return err;
5329+}
5330+
5331+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5332+{
5333+ int err;
5334+ unsigned long blksize;
5335+ unsigned char do_kfree;
5336+ char *buf;
5337+
5338+ err = -ENOMEM;
2000de60 5339+ blksize = dst->f_path.dentry->d_sb->s_blocksize;
1facf9fc 5340+ if (!blksize || PAGE_SIZE < blksize)
5341+ blksize = PAGE_SIZE;
5342+ AuDbg("blksize %lu\n", blksize);
5343+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5344+ if (do_kfree)
5345+ buf = kmalloc(blksize, GFP_NOFS);
5346+ else
5347+ buf = (void *)__get_free_page(GFP_NOFS);
5348+ if (unlikely(!buf))
5349+ goto out;
5350+
5351+ if (len > (1 << 22))
5352+ AuDbg("copying a large file %lld\n", (long long)len);
5353+
5354+ src->f_pos = 0;
5355+ dst->f_pos = 0;
5356+ err = au_do_copy_file(dst, src, len, buf, blksize);
5357+ if (do_kfree)
1c60b727 5358+ kfree(buf);
1facf9fc 5359+ else
1c60b727 5360+ free_page((unsigned long)buf);
1facf9fc 5361+
4f0767ce 5362+out:
1facf9fc 5363+ return err;
5364+}
5365+
1c60b727
AM
5366+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5367+{
5368+ int err;
5369+ struct super_block *h_src_sb;
5370+ struct inode *h_src_inode;
5371+
5372+ h_src_inode = file_inode(src);
5373+ h_src_sb = h_src_inode->i_sb;
5374+
5375+ /* XFS acquires inode_lock */
5376+ if (!au_test_xfs(h_src_sb))
5377+ err = au_copy_file(dst, src, len);
5378+ else {
3c1bdaff 5379+ inode_unlock_shared(h_src_inode);
1c60b727 5380+ err = au_copy_file(dst, src, len);
be118d29 5381+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5382+ }
5383+
5384+ return err;
5385+}
5386+
5387+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5388+{
5389+ int err;
5390+ struct super_block *h_src_sb;
5391+ struct inode *h_src_inode;
5392+
5393+ h_src_inode = file_inode(src);
5394+ h_src_sb = h_src_inode->i_sb;
5395+ if (h_src_sb != file_inode(dst)->i_sb
5396+ || !dst->f_op->clone_file_range) {
5397+ err = au_do_copy(dst, src, len);
5398+ goto out;
5399+ }
5400+
5401+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5402+ inode_unlock_shared(h_src_inode);
1c60b727 5403+ err = vfsub_clone_file_range(src, dst, len);
be118d29 5404+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5405+ } else
5406+ err = vfsub_clone_file_range(src, dst, len);
5407+ /* older XFS has a condition in cloning */
5408+ if (unlikely(err != -EOPNOTSUPP))
5409+ goto out;
5410+
5411+ /* the backend fs on NFS may not support cloning */
5412+ err = au_do_copy(dst, src, len);
5413+
5414+out:
5415+ AuTraceErr(err);
5416+ return err;
5417+}
5418+
1facf9fc 5419+/*
5420+ * to support a sparse file which is opened with O_APPEND,
5421+ * we need to close the file.
5422+ */
c2b27bf2 5423+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5424+{
5425+ int err, i;
5426+ enum { SRC, DST };
5427+ struct {
5428+ aufs_bindex_t bindex;
5429+ unsigned int flags;
5430+ struct dentry *dentry;
392086de 5431+ int force_wr;
1facf9fc 5432+ struct file *file;
523b37e3 5433+ void *label;
1facf9fc 5434+ } *f, file[] = {
5435+ {
c2b27bf2 5436+ .bindex = cpg->bsrc,
1facf9fc 5437+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
523b37e3 5438+ .label = &&out
1facf9fc 5439+ },
5440+ {
c2b27bf2 5441+ .bindex = cpg->bdst,
1facf9fc 5442+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5443+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
523b37e3 5444+ .label = &&out_src
1facf9fc 5445+ }
5446+ };
521ced18 5447+ struct super_block *sb, *h_src_sb;
e2f27e51 5448+ struct inode *h_src_inode;
8cdd5066 5449+ struct task_struct *tsk = current;
1facf9fc 5450+
5451+ /* bsrc branch can be ro/rw. */
c2b27bf2 5452+ sb = cpg->dentry->d_sb;
1facf9fc 5453+ f = file;
5454+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5455+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5456+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5457+ /*file*/NULL, f->force_wr);
1facf9fc 5458+ err = PTR_ERR(f->file);
5459+ if (IS_ERR(f->file))
5460+ goto *f->label;
1facf9fc 5461+ }
5462+
5463+ /* try stopping to update while we copyup */
e2f27e51 5464+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5465+ h_src_sb = h_src_inode->i_sb;
5466+ if (!au_test_nfs(h_src_sb))
e2f27e51 5467+ IMustLock(h_src_inode);
1c60b727 5468+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5469+
8cdd5066
JR
5470+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5471+ if (tsk->flags & PF_KTHREAD)
5472+ __fput_sync(file[DST].file);
5473+ else {
062440b3 5474+ /* it happened actually */
8cdd5066
JR
5475+ fput(file[DST].file);
5476+ /*
5477+ * too bad.
5478+ * we have to call both since we don't know which place the file
5479+ * was added to.
5480+ */
5481+ task_work_run();
5482+ flush_delayed_fput();
5483+ }
1facf9fc 5484+ au_sbr_put(sb, file[DST].bindex);
523b37e3 5485+
4f0767ce 5486+out_src:
1facf9fc 5487+ fput(file[SRC].file);
5488+ au_sbr_put(sb, file[SRC].bindex);
4f0767ce 5489+out:
1facf9fc 5490+ return err;
5491+}
5492+
c2b27bf2 5493+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5494+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5495+{
5496+ int err, rerr;
5497+ loff_t l;
86dc4139 5498+ struct path h_path;
38d290e6 5499+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5500+
5501+ err = 0;
5527c038 5502+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5503+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5504+ if (cpg->len == -1 || l < cpg->len)
5505+ cpg->len = l;
5506+ if (cpg->len) {
86dc4139 5507+ /* try stopping to update while we are referencing */
be118d29 5508+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5509+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5510+
c2b27bf2
AM
5511+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5512+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5513+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5514+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5515+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5516+ else {
3c1bdaff 5517+ inode_unlock_shared(h_src_inode);
521ced18 5518+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5519+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5520+ }
86dc4139 5521+ if (unlikely(err)) {
3c1bdaff 5522+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5523+ goto out;
5524+ }
5525+ h_src_attr->valid = 1;
e2f27e51
AM
5526+ if (!au_test_nfs(h_src_inode->i_sb)) {
5527+ err = au_cp_regular(cpg);
3c1bdaff 5528+ inode_unlock_shared(h_src_inode);
e2f27e51 5529+ } else {
3c1bdaff 5530+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5531+ err = au_cp_regular(cpg);
5532+ }
c2b27bf2 5533+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5534+ if (!err && rerr)
5535+ err = rerr;
1facf9fc 5536+ }
38d290e6
JR
5537+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5538+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5539+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5540+ spin_lock(&h_dst_inode->i_lock);
5541+ h_dst_inode->i_state |= I_LINKABLE;
5542+ spin_unlock(&h_dst_inode->i_lock);
5543+ }
1facf9fc 5544+
4f0767ce 5545+out:
1facf9fc 5546+ return err;
5547+}
5548+
5549+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5550+ struct inode *h_dir)
5551+{
5552+ int err, symlen;
5553+ mm_segment_t old_fs;
b752ccd1
AM
5554+ union {
5555+ char *k;
5556+ char __user *u;
5557+ } sym;
1facf9fc 5558+
5559+ err = -ENOMEM;
537831f9 5560+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5561+ if (unlikely(!sym.k))
1facf9fc 5562+ goto out;
5563+
9dbd164d 5564+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5565+ old_fs = get_fs();
5566+ set_fs(KERNEL_DS);
a2654f78 5567+ symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5568+ err = symlen;
5569+ set_fs(old_fs);
5570+
5571+ if (symlen > 0) {
b752ccd1
AM
5572+ sym.k[symlen] = 0;
5573+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5574+ }
1c60b727 5575+ free_page((unsigned long)sym.k);
1facf9fc 5576+
4f0767ce 5577+out:
1facf9fc 5578+ return err;
5579+}
5580+
8cdd5066
JR
5581+/*
5582+ * regardless 'acl' option, reset all ACL.
5583+ * All ACL will be copied up later from the original entry on the lower branch.
5584+ */
5585+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5586+{
5587+ int err;
5588+ struct dentry *h_dentry;
5589+ struct inode *h_inode;
5590+
5591+ h_dentry = h_path->dentry;
5592+ h_inode = d_inode(h_dentry);
5593+ /* forget_all_cached_acls(h_inode)); */
5594+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5595+ AuTraceErr(err);
5596+ if (err == -EOPNOTSUPP)
5597+ err = 0;
5598+ if (!err)
5599+ err = vfsub_acl_chmod(h_inode, mode);
5600+
5601+ AuTraceErr(err);
5602+ return err;
5603+}
5604+
5605+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5606+ struct inode *h_dir, struct path *h_path)
5607+{
5608+ int err;
5609+ struct inode *dir, *inode;
5610+
5611+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5612+ AuTraceErr(err);
5613+ if (err == -EOPNOTSUPP)
5614+ err = 0;
5615+ if (unlikely(err))
5616+ goto out;
5617+
5618+ /*
5619+ * strange behaviour from the users view,
5620+ * particularry setattr case
5621+ */
5622+ dir = d_inode(dst_parent);
5afbbe0d 5623+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5624+ au_cpup_attr_nlink(dir, /*force*/1);
5625+ inode = d_inode(cpg->dentry);
5626+ au_cpup_attr_nlink(inode, /*force*/1);
5627+
5628+out:
5629+ return err;
5630+}
5631+
1facf9fc 5632+static noinline_for_stack
c2b27bf2 5633+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5634+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5635+{
5636+ int err;
5637+ umode_t mode;
5638+ unsigned int mnt_flags;
076b876e 5639+ unsigned char isdir, isreg, force;
c2b27bf2 5640+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5641+ struct au_dtime dt;
5642+ struct path h_path;
5643+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5644+ struct inode *h_inode, *h_dir;
1facf9fc 5645+ struct super_block *sb;
5646+
5647+ /* bsrc branch can be ro/rw. */
c2b27bf2 5648+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5649+ h_inode = d_inode(h_src);
5650+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5651+
5652+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5653+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5654+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5655+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5656+ AUFS_WH_PFX_LEN));
1facf9fc 5657+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5658+ h_dir = d_inode(h_parent);
1facf9fc 5659+ IMustLock(h_dir);
5660+ AuDebugOn(h_parent != h_dst->d_parent);
5661+
c2b27bf2
AM
5662+ sb = cpg->dentry->d_sb;
5663+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5664+ if (do_dt) {
5665+ h_path.dentry = h_parent;
5666+ au_dtime_store(&dt, dst_parent, &h_path);
5667+ }
5668+ h_path.dentry = h_dst;
5669+
076b876e 5670+ isreg = 0;
1facf9fc 5671+ isdir = 0;
5672+ mode = h_inode->i_mode;
5673+ switch (mode & S_IFMT) {
5674+ case S_IFREG:
076b876e 5675+ isreg = 1;
8cdd5066 5676+ err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
b4510431 5677+ /*want_excl*/true);
1facf9fc 5678+ if (!err)
c2b27bf2 5679+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5680+ break;
5681+ case S_IFDIR:
5682+ isdir = 1;
5683+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5684+ if (!err)
5685+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5686+ break;
5687+ case S_IFLNK:
5688+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5689+ break;
5690+ case S_IFCHR:
5691+ case S_IFBLK:
5692+ AuDebugOn(!capable(CAP_MKNOD));
5693+ /*FALLTHROUGH*/
5694+ case S_IFIFO:
5695+ case S_IFSOCK:
5696+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5697+ break;
5698+ default:
5699+ AuIOErr("Unknown inode type 0%o\n", mode);
5700+ err = -EIO;
5701+ }
8cdd5066
JR
5702+ if (!err)
5703+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5704+
5705+ mnt_flags = au_mntflags(sb);
5706+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5707+ && !isdir
5708+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5709+ && (h_inode->i_nlink == 1
5710+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5711+ /* todo: unnecessary? */
5527c038 5712+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5713+ && cpg->bdst < cpg->bsrc
5714+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5715+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5716+ /* ignore this error */
5717+
076b876e
AM
5718+ if (!err) {
5719+ force = 0;
5720+ if (isreg) {
5721+ force = !!cpg->len;
5722+ if (cpg->len == -1)
5723+ force = !!i_size_read(h_inode);
5724+ }
5725+ au_fhsm_wrote(sb, cpg->bdst, force);
5726+ }
5727+
1facf9fc 5728+ if (do_dt)
5729+ au_dtime_revert(&dt);
5730+ return err;
5731+}
5732+
392086de 5733+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5734+{
5735+ int err;
392086de 5736+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5737+ struct inode *h_dir;
392086de 5738+ aufs_bindex_t bdst;
86dc4139 5739+
392086de
AM
5740+ dentry = cpg->dentry;
5741+ bdst = cpg->bdst;
5742+ h_dentry = au_h_dptr(dentry, bdst);
5743+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5744+ dget(h_dentry);
5745+ au_set_h_dptr(dentry, bdst, NULL);
5746+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5747+ if (!err)
5748+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5749+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5750+ } else {
5751+ err = 0;
5752+ parent = dget_parent(dentry);
5753+ h_parent = au_h_dptr(parent, bdst);
5754+ dput(parent);
5755+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5756+ if (IS_ERR(h_path->dentry))
5757+ err = PTR_ERR(h_path->dentry);
86dc4139 5758+ }
392086de
AM
5759+ if (unlikely(err))
5760+ goto out;
86dc4139 5761+
86dc4139 5762+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5763+ h_dir = d_inode(h_parent);
86dc4139 5764+ IMustLock(h_dir);
523b37e3
AM
5765+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5766+ /* no delegation since it is just created */
f2c43d5f
AM
5767+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5768+ /*flags*/0);
86dc4139
AM
5769+ dput(h_path->dentry);
5770+
5771+out:
5772+ return err;
5773+}
5774+
1facf9fc 5775+/*
5776+ * copyup the @dentry from @bsrc to @bdst.
5777+ * the caller must set the both of lower dentries.
5778+ * @len is for truncating when it is -1 copyup the entire file.
5779+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5780+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f
AM
5781+ * aufs doesn't touch the credential so
5782+ * security_inode_copy_up{,_xattr}() are unnecrssary.
1facf9fc 5783+ */
c2b27bf2 5784+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5785+{
5786+ int err, rerr;
5afbbe0d 5787+ aufs_bindex_t old_ibtop;
1facf9fc 5788+ unsigned char isdir, plink;
1facf9fc 5789+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5790+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5791+ struct super_block *sb;
86dc4139 5792+ struct au_branch *br;
c2b27bf2
AM
5793+ /* to reuduce stack size */
5794+ struct {
5795+ struct au_dtime dt;
5796+ struct path h_path;
5797+ struct au_cpup_reg_attr h_src_attr;
5798+ } *a;
1facf9fc 5799+
c2b27bf2
AM
5800+ err = -ENOMEM;
5801+ a = kmalloc(sizeof(*a), GFP_NOFS);
5802+ if (unlikely(!a))
5803+ goto out;
5804+ a->h_src_attr.valid = 0;
1facf9fc 5805+
c2b27bf2
AM
5806+ sb = cpg->dentry->d_sb;
5807+ br = au_sbr(sb, cpg->bdst);
5808+ a->h_path.mnt = au_br_mnt(br);
5809+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5810+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5811+ h_dir = d_inode(h_parent);
1facf9fc 5812+ IMustLock(h_dir);
5813+
c2b27bf2 5814+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5815+ inode = d_inode(cpg->dentry);
1facf9fc 5816+
5817+ if (!dst_parent)
c2b27bf2 5818+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5819+ else
5820+ dget(dst_parent);
5821+
5822+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5823+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5824+ if (dst_inode) {
5825+ if (unlikely(!plink)) {
5826+ err = -EIO;
027c5e7a
AM
5827+ AuIOErr("hi%lu(i%lu) exists on b%d "
5828+ "but plink is disabled\n",
c2b27bf2
AM
5829+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5830+ goto out_parent;
1facf9fc 5831+ }
5832+
5833+ if (dst_inode->i_nlink) {
c2b27bf2 5834+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5835+
c2b27bf2 5836+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5837+ err = PTR_ERR(h_src);
5838+ if (IS_ERR(h_src))
c2b27bf2 5839+ goto out_parent;
5527c038 5840+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5841+ err = -EIO;
79b8bda9 5842+ AuIOErr("i%lu exists on b%d "
027c5e7a 5843+ "but not pseudo-linked\n",
79b8bda9 5844+ inode->i_ino, cpg->bdst);
1facf9fc 5845+ dput(h_src);
c2b27bf2 5846+ goto out_parent;
1facf9fc 5847+ }
5848+
5849+ if (do_dt) {
c2b27bf2
AM
5850+ a->h_path.dentry = h_parent;
5851+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5852+ }
86dc4139 5853+
c2b27bf2 5854+ a->h_path.dentry = h_dst;
523b37e3
AM
5855+ delegated = NULL;
5856+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5857+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5858+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5859+ if (do_dt)
c2b27bf2 5860+ au_dtime_revert(&a->dt);
523b37e3
AM
5861+ if (unlikely(err == -EWOULDBLOCK)) {
5862+ pr_warn("cannot retry for NFSv4 delegation"
5863+ " for an internal link\n");
5864+ iput(delegated);
5865+ }
1facf9fc 5866+ dput(h_src);
c2b27bf2 5867+ goto out_parent;
1facf9fc 5868+ } else
5869+ /* todo: cpup_wh_file? */
5870+ /* udba work */
4a4d8108 5871+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5872+ }
5873+
86dc4139 5874+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5875+ old_ibtop = au_ibtop(inode);
c2b27bf2 5876+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5877+ if (unlikely(err))
86dc4139 5878+ goto out_rev;
5527c038 5879+ dst_inode = d_inode(h_dst);
febd17d6 5880+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5881+ /* todo: necessary? */
c2b27bf2 5882+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5883+
c2b27bf2 5884+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5885+ if (unlikely(err)) {
5886+ /* todo: necessary? */
c2b27bf2 5887+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5888+ inode_unlock(dst_inode);
86dc4139
AM
5889+ goto out_rev;
5890+ }
5891+
5afbbe0d 5892+ if (cpg->bdst < old_ibtop) {
86dc4139 5893+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5894+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5895+ if (unlikely(err)) {
c2b27bf2
AM
5896+ /* ignore an error */
5897+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5898+ inode_unlock(dst_inode);
86dc4139 5899+ goto out_rev;
4a4d8108 5900+ }
4a4d8108 5901+ }
5afbbe0d 5902+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5903+ } else
5afbbe0d 5904+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5905+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5906+ au_hi_flags(inode, isdir));
5907+
5908+ /* todo: necessary? */
c2b27bf2 5909+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5910+ inode_unlock(dst_inode);
86dc4139
AM
5911+ if (unlikely(err))
5912+ goto out_rev;
5913+
5527c038 5914+ src_inode = d_inode(h_src);
86dc4139 5915+ if (!isdir
5527c038
JR
5916+ && (src_inode->i_nlink > 1
5917+ || src_inode->i_state & I_LINKABLE)
86dc4139 5918+ && plink)
c2b27bf2 5919+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5920+
c2b27bf2
AM
5921+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5922+ a->h_path.dentry = h_dst;
392086de 5923+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5924+ }
5925+ if (!err)
c2b27bf2 5926+ goto out_parent; /* success */
1facf9fc 5927+
5928+ /* revert */
4a4d8108 5929+out_rev:
c2b27bf2
AM
5930+ a->h_path.dentry = h_parent;
5931+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5932+ a->h_path.dentry = h_dst;
86dc4139 5933+ rerr = 0;
5527c038 5934+ if (d_is_positive(h_dst)) {
523b37e3
AM
5935+ if (!isdir) {
5936+ /* no delegation since it is just created */
5937+ rerr = vfsub_unlink(h_dir, &a->h_path,
5938+ /*delegated*/NULL, /*force*/0);
5939+ } else
c2b27bf2 5940+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5941+ }
c2b27bf2 5942+ au_dtime_revert(&a->dt);
1facf9fc 5943+ if (rerr) {
5944+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5945+ err = -EIO;
5946+ }
c2b27bf2 5947+out_parent:
1facf9fc 5948+ dput(dst_parent);
1c60b727 5949+ kfree(a);
c2b27bf2 5950+out:
1facf9fc 5951+ return err;
5952+}
5953+
7e9cd9fe 5954+#if 0 /* reserved */
1facf9fc 5955+struct au_cpup_single_args {
5956+ int *errp;
c2b27bf2 5957+ struct au_cp_generic *cpg;
1facf9fc 5958+ struct dentry *dst_parent;
5959+};
5960+
5961+static void au_call_cpup_single(void *args)
5962+{
5963+ struct au_cpup_single_args *a = args;
86dc4139 5964+
c2b27bf2
AM
5965+ au_pin_hdir_acquire_nest(a->cpg->pin);
5966+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5967+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5968+}
c2b27bf2 5969+#endif
1facf9fc 5970+
53392da6
AM
5971+/*
5972+ * prevent SIGXFSZ in copy-up.
5973+ * testing CAP_MKNOD is for generic fs,
5974+ * but CAP_FSETID is for xfs only, currently.
5975+ */
86dc4139 5976+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5977+{
5978+ int do_sio;
86dc4139
AM
5979+ struct super_block *sb;
5980+ struct inode *h_dir;
53392da6
AM
5981+
5982+ do_sio = 0;
86dc4139 5983+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5984+ if (!au_wkq_test()
5985+ && (!au_sbi(sb)->si_plink_maint_pid
5986+ || au_plink_maint(sb, AuLock_NOPLM))) {
5987+ switch (mode & S_IFMT) {
5988+ case S_IFREG:
5989+ /* no condition about RLIMIT_FSIZE and the file size */
5990+ do_sio = 1;
5991+ break;
5992+ case S_IFCHR:
5993+ case S_IFBLK:
5994+ do_sio = !capable(CAP_MKNOD);
5995+ break;
5996+ }
5997+ if (!do_sio)
5998+ do_sio = ((mode & (S_ISUID | S_ISGID))
5999+ && !capable(CAP_FSETID));
86dc4139
AM
6000+ /* this workaround may be removed in the future */
6001+ if (!do_sio) {
6002+ h_dir = au_pinned_h_dir(pin);
6003+ do_sio = h_dir->i_mode & S_ISVTX;
6004+ }
53392da6
AM
6005+ }
6006+
6007+ return do_sio;
6008+}
6009+
7e9cd9fe 6010+#if 0 /* reserved */
c2b27bf2 6011+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 6012+{
6013+ int err, wkq_err;
1facf9fc 6014+ struct dentry *h_dentry;
6015+
c2b27bf2 6016+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 6017+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 6018+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 6019+ else {
6020+ struct au_cpup_single_args args = {
6021+ .errp = &err,
c2b27bf2
AM
6022+ .cpg = cpg,
6023+ .dst_parent = dst_parent
1facf9fc 6024+ };
6025+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
6026+ if (unlikely(wkq_err))
6027+ err = wkq_err;
6028+ }
6029+
6030+ return err;
6031+}
c2b27bf2 6032+#endif
1facf9fc 6033+
6034+/*
6035+ * copyup the @dentry from the first active lower branch to @bdst,
6036+ * using au_cpup_single().
6037+ */
c2b27bf2 6038+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6039+{
6040+ int err;
c2b27bf2
AM
6041+ unsigned int flags_orig;
6042+ struct dentry *dentry;
6043+
6044+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 6045+
c2b27bf2 6046+ dentry = cpg->dentry;
86dc4139 6047+ DiMustWriteLock(dentry);
1facf9fc 6048+
c2b27bf2 6049+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 6050+ if (!err) {
c2b27bf2
AM
6051+ flags_orig = cpg->flags;
6052+ au_fset_cpup(cpg->flags, RENAME);
6053+ err = au_cpup_single(cpg, NULL);
6054+ cpg->flags = flags_orig;
1facf9fc 6055+ if (!err)
6056+ return 0; /* success */
6057+
6058+ /* revert */
c2b27bf2 6059+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 6060+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 6061+ }
6062+
6063+ return err;
6064+}
6065+
6066+struct au_cpup_simple_args {
6067+ int *errp;
c2b27bf2 6068+ struct au_cp_generic *cpg;
1facf9fc 6069+};
6070+
6071+static void au_call_cpup_simple(void *args)
6072+{
6073+ struct au_cpup_simple_args *a = args;
86dc4139 6074+
c2b27bf2
AM
6075+ au_pin_hdir_acquire_nest(a->cpg->pin);
6076+ *a->errp = au_cpup_simple(a->cpg);
6077+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6078+}
6079+
c2b27bf2 6080+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6081+{
6082+ int err, wkq_err;
c2b27bf2
AM
6083+ struct dentry *dentry, *parent;
6084+ struct file *h_file;
1facf9fc 6085+ struct inode *h_dir;
6086+
c2b27bf2
AM
6087+ dentry = cpg->dentry;
6088+ h_file = NULL;
6089+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
6090+ AuDebugOn(cpg->bsrc < 0);
392086de 6091+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
6092+ err = PTR_ERR(h_file);
6093+ if (IS_ERR(h_file))
6094+ goto out;
6095+ }
6096+
1facf9fc 6097+ parent = dget_parent(dentry);
5527c038 6098+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 6099+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 6100+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6101+ err = au_cpup_simple(cpg);
1facf9fc 6102+ else {
6103+ struct au_cpup_simple_args args = {
6104+ .errp = &err,
c2b27bf2 6105+ .cpg = cpg
1facf9fc 6106+ };
6107+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6108+ if (unlikely(wkq_err))
6109+ err = wkq_err;
6110+ }
6111+
6112+ dput(parent);
c2b27bf2
AM
6113+ if (h_file)
6114+ au_h_open_post(dentry, cpg->bsrc, h_file);
6115+
6116+out:
1facf9fc 6117+ return err;
6118+}
6119+
c2b27bf2 6120+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 6121+{
5afbbe0d 6122+ aufs_bindex_t bsrc, bbot;
c2b27bf2 6123+ struct dentry *dentry, *h_dentry;
367653fa 6124+
c2b27bf2
AM
6125+ if (cpg->bsrc < 0) {
6126+ dentry = cpg->dentry;
5afbbe0d
AM
6127+ bbot = au_dbbot(dentry);
6128+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
6129+ h_dentry = au_h_dptr(dentry, bsrc);
6130+ if (h_dentry) {
5527c038 6131+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
6132+ break;
6133+ }
6134+ }
5afbbe0d 6135+ AuDebugOn(bsrc > bbot);
c2b27bf2 6136+ cpg->bsrc = bsrc;
367653fa 6137+ }
c2b27bf2
AM
6138+ AuDebugOn(cpg->bsrc <= cpg->bdst);
6139+ return au_do_sio_cpup_simple(cpg);
6140+}
367653fa 6141+
c2b27bf2
AM
6142+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6143+{
6144+ AuDebugOn(cpg->bdst <= cpg->bsrc);
6145+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
6146+}
6147+
1facf9fc 6148+/* ---------------------------------------------------------------------- */
6149+
6150+/*
6151+ * copyup the deleted file for writing.
6152+ */
c2b27bf2
AM
6153+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6154+ struct file *file)
1facf9fc 6155+{
6156+ int err;
c2b27bf2
AM
6157+ unsigned int flags_orig;
6158+ aufs_bindex_t bsrc_orig;
c2b27bf2 6159+ struct au_dinfo *dinfo;
5afbbe0d
AM
6160+ struct {
6161+ struct au_hdentry *hd;
6162+ struct dentry *h_dentry;
6163+ } hdst, hsrc;
1facf9fc 6164+
c2b27bf2 6165+ dinfo = au_di(cpg->dentry);
1308ab2a 6166+ AuRwMustWriteLock(&dinfo->di_rwsem);
6167+
c2b27bf2 6168+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
6169+ cpg->bsrc = dinfo->di_btop;
6170+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
6171+ hdst.h_dentry = hdst.hd->hd_dentry;
6172+ hdst.hd->hd_dentry = wh_dentry;
6173+ dinfo->di_btop = cpg->bdst;
6174+
6175+ hsrc.h_dentry = NULL;
027c5e7a 6176+ if (file) {
5afbbe0d
AM
6177+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6178+ hsrc.h_dentry = hsrc.hd->hd_dentry;
6179+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 6180+ }
c2b27bf2
AM
6181+ flags_orig = cpg->flags;
6182+ cpg->flags = !AuCpup_DTIME;
6183+ err = au_cpup_single(cpg, /*h_parent*/NULL);
6184+ cpg->flags = flags_orig;
027c5e7a
AM
6185+ if (file) {
6186+ if (!err)
6187+ err = au_reopen_nondir(file);
5afbbe0d 6188+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 6189+ }
5afbbe0d
AM
6190+ hdst.hd->hd_dentry = hdst.h_dentry;
6191+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 6192+ cpg->bsrc = bsrc_orig;
1facf9fc 6193+
6194+ return err;
6195+}
6196+
c2b27bf2 6197+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6198+{
6199+ int err;
c2b27bf2 6200+ aufs_bindex_t bdst;
1facf9fc 6201+ struct au_dtime dt;
c2b27bf2 6202+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6203+ struct au_branch *br;
6204+ struct path h_path;
6205+
c2b27bf2
AM
6206+ dentry = cpg->dentry;
6207+ bdst = cpg->bdst;
1facf9fc 6208+ br = au_sbr(dentry->d_sb, bdst);
6209+ parent = dget_parent(dentry);
6210+ h_parent = au_h_dptr(parent, bdst);
6211+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6212+ err = PTR_ERR(wh_dentry);
6213+ if (IS_ERR(wh_dentry))
6214+ goto out;
6215+
6216+ h_path.dentry = h_parent;
86dc4139 6217+ h_path.mnt = au_br_mnt(br);
1facf9fc 6218+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6219+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6220+ if (unlikely(err))
6221+ goto out_wh;
6222+
6223+ dget(wh_dentry);
6224+ h_path.dentry = wh_dentry;
2000de60 6225+ if (!d_is_dir(wh_dentry)) {
523b37e3 6226+ /* no delegation since it is just created */
5527c038 6227+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6228+ /*delegated*/NULL, /*force*/0);
6229+ } else
5527c038 6230+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6231+ if (unlikely(err)) {
523b37e3
AM
6232+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6233+ wh_dentry, err);
1facf9fc 6234+ err = -EIO;
6235+ }
6236+ au_dtime_revert(&dt);
5527c038 6237+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6238+
4f0767ce 6239+out_wh:
1facf9fc 6240+ dput(wh_dentry);
4f0767ce 6241+out:
1facf9fc 6242+ dput(parent);
6243+ return err;
6244+}
6245+
6246+struct au_cpup_wh_args {
6247+ int *errp;
c2b27bf2 6248+ struct au_cp_generic *cpg;
1facf9fc 6249+ struct file *file;
6250+};
6251+
6252+static void au_call_cpup_wh(void *args)
6253+{
6254+ struct au_cpup_wh_args *a = args;
86dc4139 6255+
c2b27bf2
AM
6256+ au_pin_hdir_acquire_nest(a->cpg->pin);
6257+ *a->errp = au_cpup_wh(a->cpg, a->file);
6258+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6259+}
6260+
c2b27bf2 6261+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6262+{
6263+ int err, wkq_err;
c2b27bf2 6264+ aufs_bindex_t bdst;
c1595e42 6265+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6266+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6267+ struct au_wbr *wbr;
c2b27bf2 6268+ struct au_pin wh_pin, *pin_orig;
1facf9fc 6269+
c2b27bf2
AM
6270+ dentry = cpg->dentry;
6271+ bdst = cpg->bdst;
1facf9fc 6272+ parent = dget_parent(dentry);
5527c038 6273+ dir = d_inode(parent);
1facf9fc 6274+ h_orph = NULL;
6275+ h_parent = NULL;
6276+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6277+ h_tmpdir = h_dir;
c2b27bf2 6278+ pin_orig = NULL;
1facf9fc 6279+ if (!h_dir->i_nlink) {
6280+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6281+ h_orph = wbr->wbr_orph;
6282+
6283+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6284+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6285+ h_tmpdir = d_inode(h_orph);
1facf9fc 6286+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6287+
febd17d6 6288+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6289+ /* todo: au_h_open_pre()? */
86dc4139 6290+
c2b27bf2 6291+ pin_orig = cpg->pin;
86dc4139 6292+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6293+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6294+ cpg->pin = &wh_pin;
1facf9fc 6295+ }
6296+
53392da6 6297+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6298+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6299+ err = au_cpup_wh(cpg, file);
1facf9fc 6300+ else {
6301+ struct au_cpup_wh_args args = {
6302+ .errp = &err,
c2b27bf2
AM
6303+ .cpg = cpg,
6304+ .file = file
1facf9fc 6305+ };
6306+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6307+ if (unlikely(wkq_err))
6308+ err = wkq_err;
6309+ }
6310+
6311+ if (h_orph) {
febd17d6 6312+ inode_unlock(h_tmpdir);
4a4d8108 6313+ /* todo: au_h_open_post()? */
1facf9fc 6314+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6315+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6316+ AuDebugOn(!pin_orig);
6317+ cpg->pin = pin_orig;
1facf9fc 6318+ }
6319+ iput(h_dir);
6320+ dput(parent);
6321+
6322+ return err;
6323+}
6324+
6325+/* ---------------------------------------------------------------------- */
6326+
6327+/*
6328+ * generic routine for both of copy-up and copy-down.
6329+ */
6330+/* cf. revalidate function in file.c */
6331+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6332+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6333+ struct au_pin *pin,
1facf9fc 6334+ struct dentry *h_parent, void *arg),
6335+ void *arg)
6336+{
6337+ int err;
6338+ struct au_pin pin;
5527c038 6339+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6340+
6341+ err = 0;
6342+ parent = dget_parent(dentry);
6343+ if (IS_ROOT(parent))
6344+ goto out;
6345+
6346+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6347+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6348+
6349+ /* do not use au_dpage */
6350+ real_parent = parent;
6351+ while (1) {
6352+ dput(parent);
6353+ parent = dget_parent(dentry);
6354+ h_parent = au_h_dptr(parent, bdst);
6355+ if (h_parent)
6356+ goto out; /* success */
6357+
6358+ /* find top dir which is necessary to cpup */
6359+ do {
6360+ d = parent;
6361+ dput(parent);
6362+ parent = dget_parent(d);
6363+ di_read_lock_parent3(parent, !AuLock_IR);
6364+ h_parent = au_h_dptr(parent, bdst);
6365+ di_read_unlock(parent, !AuLock_IR);
6366+ } while (!h_parent);
6367+
6368+ if (d != real_parent)
6369+ di_write_lock_child3(d);
6370+
6371+ /* somebody else might create while we were sleeping */
5527c038
JR
6372+ h_dentry = au_h_dptr(d, bdst);
6373+ if (!h_dentry || d_is_negative(h_dentry)) {
6374+ if (h_dentry)
5afbbe0d 6375+ au_update_dbtop(d);
1facf9fc 6376+
6377+ au_pin_set_dentry(&pin, d);
6378+ err = au_do_pin(&pin);
6379+ if (!err) {
86dc4139 6380+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6381+ au_unpin(&pin);
6382+ }
6383+ }
6384+
6385+ if (d != real_parent)
6386+ di_write_unlock(d);
6387+ if (unlikely(err))
6388+ break;
6389+ }
6390+
4f0767ce 6391+out:
1facf9fc 6392+ dput(parent);
6393+ return err;
6394+}
6395+
6396+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6397+ struct au_pin *pin,
2000de60 6398+ struct dentry *h_parent __maybe_unused,
1facf9fc 6399+ void *arg __maybe_unused)
6400+{
c2b27bf2
AM
6401+ struct au_cp_generic cpg = {
6402+ .dentry = dentry,
6403+ .bdst = bdst,
6404+ .bsrc = -1,
6405+ .len = 0,
6406+ .pin = pin,
6407+ .flags = AuCpup_DTIME
6408+ };
6409+ return au_sio_cpup_simple(&cpg);
1facf9fc 6410+}
6411+
6412+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6413+{
6414+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6415+}
6416+
6417+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6418+{
6419+ int err;
6420+ struct dentry *parent;
6421+ struct inode *dir;
6422+
6423+ parent = dget_parent(dentry);
5527c038 6424+ dir = d_inode(parent);
1facf9fc 6425+ err = 0;
6426+ if (au_h_iptr(dir, bdst))
6427+ goto out;
6428+
6429+ di_read_unlock(parent, AuLock_IR);
6430+ di_write_lock_parent(parent);
6431+ /* someone else might change our inode while we were sleeping */
6432+ if (!au_h_iptr(dir, bdst))
6433+ err = au_cpup_dirs(dentry, bdst);
6434+ di_downgrade_lock(parent, AuLock_IR);
6435+
4f0767ce 6436+out:
1facf9fc 6437+ dput(parent);
6438+ return err;
6439+}
7f207e10
AM
6440diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6441--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
6442+++ linux/fs/aufs/cpup.h 2018-07-19 09:46:13.056647808 +0200
6443@@ -0,0 +1,100 @@
6444+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6445+/*
b00004a5 6446+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6447+ *
6448+ * This program, aufs is free software; you can redistribute it and/or modify
6449+ * it under the terms of the GNU General Public License as published by
6450+ * the Free Software Foundation; either version 2 of the License, or
6451+ * (at your option) any later version.
dece6358
AM
6452+ *
6453+ * This program is distributed in the hope that it will be useful,
6454+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6455+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6456+ * GNU General Public License for more details.
6457+ *
6458+ * You should have received a copy of the GNU General Public License
523b37e3 6459+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6460+ */
6461+
6462+/*
6463+ * copy-up/down functions
6464+ */
6465+
6466+#ifndef __AUFS_CPUP_H__
6467+#define __AUFS_CPUP_H__
6468+
6469+#ifdef __KERNEL__
6470+
dece6358 6471+#include <linux/path.h>
1facf9fc 6472+
dece6358
AM
6473+struct inode;
6474+struct file;
86dc4139 6475+struct au_pin;
dece6358 6476+
86dc4139 6477+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6478+void au_cpup_attr_timesizes(struct inode *inode);
6479+void au_cpup_attr_nlink(struct inode *inode, int force);
6480+void au_cpup_attr_changeable(struct inode *inode);
6481+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6482+void au_cpup_attr_all(struct inode *inode, int force);
6483+
6484+/* ---------------------------------------------------------------------- */
6485+
c2b27bf2
AM
6486+struct au_cp_generic {
6487+ struct dentry *dentry;
6488+ aufs_bindex_t bdst, bsrc;
6489+ loff_t len;
6490+ struct au_pin *pin;
6491+ unsigned int flags;
6492+};
6493+
1facf9fc 6494+/* cpup flags */
392086de
AM
6495+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6496+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6497+ for link(2) */
6498+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6499+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6500+ cpup */
6501+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6502+ existing entry */
6503+#define AuCpup_RWDST (1 << 5) /* force write target even if
6504+ the branch is marked as RO */
c2b27bf2 6505+
8b6a4947
AM
6506+#ifndef CONFIG_AUFS_BR_HFSPLUS
6507+#undef AuCpup_HOPEN
6508+#define AuCpup_HOPEN 0
6509+#endif
6510+
1facf9fc 6511+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6512+#define au_fset_cpup(flags, name) \
6513+ do { (flags) |= AuCpup_##name; } while (0)
6514+#define au_fclr_cpup(flags, name) \
6515+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6516+
6517+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6518+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6519+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6520+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6521+
6522+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6523+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6524+ struct au_pin *pin,
1facf9fc 6525+ struct dentry *h_parent, void *arg),
6526+ void *arg);
6527+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6528+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6529+
6530+/* ---------------------------------------------------------------------- */
6531+
6532+/* keep timestamps when copyup */
6533+struct au_dtime {
6534+ struct dentry *dt_dentry;
6535+ struct path dt_h_path;
6536+ struct timespec dt_atime, dt_mtime;
6537+};
6538+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6539+ struct path *h_path);
6540+void au_dtime_revert(struct au_dtime *dt);
6541+
6542+#endif /* __KERNEL__ */
6543+#endif /* __AUFS_CPUP_H__ */
7f207e10
AM
6544diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6545--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
6546+++ linux/fs/aufs/dbgaufs.c 2018-07-19 09:46:13.056647808 +0200
6547@@ -0,0 +1,478 @@
6548+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6549+/*
b00004a5 6550+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 6551+ *
6552+ * This program, aufs is free software; you can redistribute it and/or modify
6553+ * it under the terms of the GNU General Public License as published by
6554+ * the Free Software Foundation; either version 2 of the License, or
6555+ * (at your option) any later version.
dece6358
AM
6556+ *
6557+ * This program is distributed in the hope that it will be useful,
6558+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6559+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6560+ * GNU General Public License for more details.
6561+ *
6562+ * You should have received a copy of the GNU General Public License
523b37e3 6563+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6564+ */
6565+
6566+/*
6567+ * debugfs interface
6568+ */
6569+
6570+#include <linux/debugfs.h>
6571+#include "aufs.h"
6572+
6573+#ifndef CONFIG_SYSFS
6574+#error DEBUG_FS depends upon SYSFS
6575+#endif
6576+
6577+static struct dentry *dbgaufs;
6578+static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
6579+
6580+/* 20 is max digits length of ulong 64 */
6581+struct dbgaufs_arg {
6582+ int n;
6583+ char a[20 * 4];
6584+};
6585+
6586+/*
6587+ * common function for all XINO files
6588+ */
6589+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6590+ struct file *file)
6591+{
1c60b727 6592+ kfree(file->private_data);
1facf9fc 6593+ return 0;
6594+}
6595+
062440b3
AM
6596+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6597+ int cnt)
1facf9fc 6598+{
6599+ int err;
6600+ struct kstat st;
6601+ struct dbgaufs_arg *p;
6602+
6603+ err = -ENOMEM;
6604+ p = kmalloc(sizeof(*p), GFP_NOFS);
6605+ if (unlikely(!p))
6606+ goto out;
6607+
6608+ err = 0;
6609+ p->n = 0;
6610+ file->private_data = p;
6611+ if (!xf)
6612+ goto out;
6613+
521ced18 6614+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6615+ if (!err) {
6616+ if (do_fcnt)
6617+ p->n = snprintf
062440b3
AM
6618+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6619+ cnt, st.blocks, st.blksize,
1facf9fc 6620+ (long long)st.size);
6621+ else
521ced18 6622+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6623+ st.blocks, st.blksize,
6624+ (long long)st.size);
6625+ AuDebugOn(p->n >= sizeof(p->a));
6626+ } else {
6627+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6628+ err = 0;
6629+ }
6630+
4f0767ce 6631+out:
1facf9fc 6632+ return err;
6633+
6634+}
6635+
6636+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6637+ size_t count, loff_t *ppos)
6638+{
6639+ struct dbgaufs_arg *p;
6640+
6641+ p = file->private_data;
6642+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6643+}
6644+
6645+/* ---------------------------------------------------------------------- */
6646+
86dc4139
AM
6647+struct dbgaufs_plink_arg {
6648+ int n;
6649+ char a[];
6650+};
6651+
6652+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6653+ struct file *file)
6654+{
1c60b727 6655+ free_page((unsigned long)file->private_data);
86dc4139
AM
6656+ return 0;
6657+}
6658+
6659+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6660+{
6661+ int err, i, limit;
6662+ unsigned long n, sum;
6663+ struct dbgaufs_plink_arg *p;
6664+ struct au_sbinfo *sbinfo;
6665+ struct super_block *sb;
8b6a4947 6666+ struct hlist_bl_head *hbl;
86dc4139
AM
6667+
6668+ err = -ENOMEM;
6669+ p = (void *)get_zeroed_page(GFP_NOFS);
6670+ if (unlikely(!p))
6671+ goto out;
6672+
6673+ err = -EFBIG;
6674+ sbinfo = inode->i_private;
6675+ sb = sbinfo->si_sb;
6676+ si_noflush_read_lock(sb);
6677+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6678+ limit = PAGE_SIZE - sizeof(p->n);
6679+
6680+ /* the number of buckets */
6681+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6682+ p->n += n;
6683+ limit -= n;
6684+
6685+ sum = 0;
8b6a4947
AM
6686+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6687+ i++, hbl++) {
6688+ n = au_hbl_count(hbl);
86dc4139
AM
6689+ sum += n;
6690+
6691+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6692+ p->n += n;
6693+ limit -= n;
6694+ if (unlikely(limit <= 0))
6695+ goto out_free;
6696+ }
6697+ p->a[p->n - 1] = '\n';
6698+
6699+ /* the sum of plinks */
6700+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6701+ p->n += n;
6702+ limit -= n;
6703+ if (unlikely(limit <= 0))
6704+ goto out_free;
6705+ } else {
6706+#define str "1\n0\n0\n"
6707+ p->n = sizeof(str) - 1;
6708+ strcpy(p->a, str);
6709+#undef str
6710+ }
6711+ si_read_unlock(sb);
6712+
6713+ err = 0;
6714+ file->private_data = p;
6715+ goto out; /* success */
6716+
6717+out_free:
1c60b727 6718+ free_page((unsigned long)p);
86dc4139
AM
6719+out:
6720+ return err;
6721+}
6722+
6723+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6724+ size_t count, loff_t *ppos)
6725+{
6726+ struct dbgaufs_plink_arg *p;
6727+
6728+ p = file->private_data;
6729+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6730+}
6731+
6732+static const struct file_operations dbgaufs_plink_fop = {
6733+ .owner = THIS_MODULE,
6734+ .open = dbgaufs_plink_open,
6735+ .release = dbgaufs_plink_release,
6736+ .read = dbgaufs_plink_read
6737+};
6738+
6739+/* ---------------------------------------------------------------------- */
6740+
1facf9fc 6741+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6742+{
6743+ int err;
6744+ struct au_sbinfo *sbinfo;
6745+ struct super_block *sb;
6746+
6747+ sbinfo = inode->i_private;
6748+ sb = sbinfo->si_sb;
6749+ si_noflush_read_lock(sb);
062440b3 6750+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6751+ si_read_unlock(sb);
6752+ return err;
6753+}
6754+
6755+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6756+ .owner = THIS_MODULE,
1facf9fc 6757+ .open = dbgaufs_xib_open,
6758+ .release = dbgaufs_xi_release,
6759+ .read = dbgaufs_xi_read
6760+};
6761+
6762+/* ---------------------------------------------------------------------- */
6763+
6764+#define DbgaufsXi_PREFIX "xi"
6765+
6766+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6767+{
6768+ int err;
6769+ long l;
6770+ struct au_sbinfo *sbinfo;
6771+ struct super_block *sb;
6772+ struct file *xf;
6773+ struct qstr *name;
062440b3 6774+ struct au_branch *br;
1facf9fc 6775+
6776+ err = -ENOENT;
6777+ xf = NULL;
2000de60 6778+ name = &file->f_path.dentry->d_name;
1facf9fc 6779+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6780+ || memcmp(name->name, DbgaufsXi_PREFIX,
6781+ sizeof(DbgaufsXi_PREFIX) - 1)))
6782+ goto out;
9dbd164d 6783+ err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6784+ if (unlikely(err))
6785+ goto out;
6786+
6787+ sbinfo = inode->i_private;
6788+ sb = sbinfo->si_sb;
6789+ si_noflush_read_lock(sb);
5afbbe0d 6790+ if (l <= au_sbbot(sb)) {
062440b3
AM
6791+ br = au_sbr(sb, (aufs_bindex_t)l);
6792+ xf = au_xino_file(br);
6793+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6794+ au_xino_count(br));
1facf9fc 6795+ } else
6796+ err = -ENOENT;
6797+ si_read_unlock(sb);
6798+
4f0767ce 6799+out:
1facf9fc 6800+ return err;
6801+}
6802+
6803+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6804+ .owner = THIS_MODULE,
1facf9fc 6805+ .open = dbgaufs_xino_open,
6806+ .release = dbgaufs_xi_release,
6807+ .read = dbgaufs_xi_read
6808+};
6809+
062440b3
AM
6810+void dbgaufs_xino_del(struct au_branch *br)
6811+{
6812+ struct dentry *dbgaufs;
6813+
6814+ dbgaufs = br->br_dbgaufs;
6815+ if (!dbgaufs)
6816+ return;
6817+
6818+ br->br_dbgaufs = NULL;
6819+ /* debugfs acquires the parent i_mutex */
6820+ lockdep_off();
6821+ debugfs_remove(dbgaufs);
6822+ lockdep_on();
6823+}
6824+
1facf9fc 6825+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6826+{
5afbbe0d 6827+ aufs_bindex_t bbot;
1facf9fc 6828+ struct au_branch *br;
1facf9fc 6829+
6830+ if (!au_sbi(sb)->si_dbgaufs)
6831+ return;
6832+
5afbbe0d
AM
6833+ bbot = au_sbbot(sb);
6834+ for (; bindex <= bbot; bindex++) {
1facf9fc 6835+ br = au_sbr(sb, bindex);
062440b3
AM
6836+ dbgaufs_xino_del(br);
6837+ }
6838+}
6839+
6840+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6841+ struct dentry *parent, struct au_sbinfo *sbinfo)
6842+{
6843+ struct au_branch *br;
6844+ struct dentry *d;
6845+ char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6846+
6847+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6848+ br = au_sbr(sb, bindex);
6849+ if (br->br_dbgaufs) {
6850+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6851+
6852+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6853+ /* debugfs acquires the parent i_mutex */
6854+ lockdep_off();
6855+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6856+ name);
6857+ lockdep_on();
6858+ if (unlikely(!d))
6859+ pr_warn("failed renaming %pd/%s, ignored.\n",
6860+ parent, name);
6861+ }
6862+ } else {
e2f27e51 6863+ lockdep_off();
062440b3
AM
6864+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6865+ sbinfo, &dbgaufs_xino_fop);
e2f27e51 6866+ lockdep_on();
062440b3
AM
6867+ if (unlikely(!br->br_dbgaufs))
6868+ pr_warn("failed creaiting %pd/%s, ignored.\n",
6869+ parent, name);
1facf9fc 6870+ }
6871+}
6872+
062440b3 6873+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
1facf9fc 6874+{
6875+ struct au_sbinfo *sbinfo;
6876+ struct dentry *parent;
5afbbe0d 6877+ aufs_bindex_t bbot;
062440b3
AM
6878+
6879+ if (!au_opt_test(au_mntflags(sb), XINO))
6880+ return;
1facf9fc 6881+
6882+ sbinfo = au_sbi(sb);
6883+ parent = sbinfo->si_dbgaufs;
6884+ if (!parent)
6885+ return;
6886+
5afbbe0d 6887+ bbot = au_sbbot(sb);
062440b3
AM
6888+ if (topdown)
6889+ for (; bindex <= bbot; bindex++)
6890+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6891+ else
6892+ for (; bbot >= bindex; bbot--)
6893+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
1facf9fc 6894+}
6895+
6896+/* ---------------------------------------------------------------------- */
6897+
6898+#ifdef CONFIG_AUFS_EXPORT
6899+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6900+{
6901+ int err;
6902+ struct au_sbinfo *sbinfo;
6903+ struct super_block *sb;
6904+
6905+ sbinfo = inode->i_private;
6906+ sb = sbinfo->si_sb;
6907+ si_noflush_read_lock(sb);
062440b3 6908+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6909+ si_read_unlock(sb);
6910+ return err;
6911+}
6912+
6913+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6914+ .owner = THIS_MODULE,
1facf9fc 6915+ .open = dbgaufs_xigen_open,
6916+ .release = dbgaufs_xi_release,
6917+ .read = dbgaufs_xi_read
6918+};
6919+
6920+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6921+{
6922+ int err;
6923+
dece6358 6924+ /*
c1595e42 6925+ * This function is a dynamic '__init' function actually,
dece6358
AM
6926+ * so the tiny check for si_rwsem is unnecessary.
6927+ */
6928+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6929+
1facf9fc 6930+ err = -EIO;
6931+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6932+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6933+ &dbgaufs_xigen_fop);
6934+ if (sbinfo->si_dbgaufs_xigen)
6935+ err = 0;
6936+
6937+ return err;
6938+}
6939+#else
6940+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6941+{
6942+ return 0;
6943+}
6944+#endif /* CONFIG_AUFS_EXPORT */
6945+
6946+/* ---------------------------------------------------------------------- */
6947+
6948+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6949+{
dece6358 6950+ /*
7e9cd9fe 6951+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6952+ * so the tiny check for si_rwsem is unnecessary.
6953+ */
6954+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6955+
1facf9fc 6956+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6957+ sbinfo->si_dbgaufs = NULL;
1facf9fc 6958+}
6959+
6960+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6961+{
6962+ int err;
6963+ char name[SysaufsSiNameLen];
6964+
dece6358 6965+ /*
c1595e42 6966+ * This function is a dynamic '__init' function actually,
dece6358
AM
6967+ * so the tiny check for si_rwsem is unnecessary.
6968+ */
6969+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6970+
1facf9fc 6971+ err = -ENOENT;
6972+ if (!dbgaufs) {
6973+ AuErr1("/debug/aufs is uninitialized\n");
6974+ goto out;
6975+ }
6976+
6977+ err = -EIO;
6978+ sysaufs_name(sbinfo, name);
6979+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6980+ if (unlikely(!sbinfo->si_dbgaufs))
6981+ goto out;
1facf9fc 6982+
062440b3 6983+ /* regardless plink/noplink option */
86dc4139
AM
6984+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6985+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6986+ &dbgaufs_plink_fop);
6987+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6988+ goto out_dir;
6989+
062440b3
AM
6990+ /* regardless xino/noxino option */
6991+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6992+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6993+ &dbgaufs_xib_fop);
6994+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6995+ goto out_dir;
6996+
1facf9fc 6997+ err = dbgaufs_xigen_init(sbinfo);
6998+ if (!err)
6999+ goto out; /* success */
7000+
4f0767ce 7001+out_dir:
1facf9fc 7002+ dbgaufs_si_fin(sbinfo);
4f0767ce 7003+out:
062440b3
AM
7004+ if (unlikely(err))
7005+ pr_err("debugfs/aufs failed\n");
1facf9fc 7006+ return err;
7007+}
7008+
7009+/* ---------------------------------------------------------------------- */
7010+
7011+void dbgaufs_fin(void)
7012+{
7013+ debugfs_remove(dbgaufs);
7014+}
7015+
7016+int __init dbgaufs_init(void)
7017+{
7018+ int err;
7019+
7020+ err = -EIO;
7021+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
7022+ if (dbgaufs)
7023+ err = 0;
7024+ return err;
7025+}
7f207e10
AM
7026diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
7027--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
7028+++ linux/fs/aufs/dbgaufs.h 2018-07-19 09:46:13.056647808 +0200
7029@@ -0,0 +1,53 @@
7030+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7031+/*
b00004a5 7032+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7033+ *
7034+ * This program, aufs is free software; you can redistribute it and/or modify
7035+ * it under the terms of the GNU General Public License as published by
7036+ * the Free Software Foundation; either version 2 of the License, or
7037+ * (at your option) any later version.
dece6358
AM
7038+ *
7039+ * This program is distributed in the hope that it will be useful,
7040+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7041+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7042+ * GNU General Public License for more details.
7043+ *
7044+ * You should have received a copy of the GNU General Public License
523b37e3 7045+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7046+ */
7047+
7048+/*
7049+ * debugfs interface
7050+ */
7051+
7052+#ifndef __DBGAUFS_H__
7053+#define __DBGAUFS_H__
7054+
7055+#ifdef __KERNEL__
7056+
dece6358 7057+struct super_block;
1facf9fc 7058+struct au_sbinfo;
062440b3 7059+struct au_branch;
dece6358 7060+
1facf9fc 7061+#ifdef CONFIG_DEBUG_FS
7062+/* dbgaufs.c */
062440b3 7063+void dbgaufs_xino_del(struct au_branch *br);
1facf9fc 7064+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
062440b3 7065+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
1facf9fc 7066+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
7067+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
7068+void dbgaufs_fin(void);
7069+int __init dbgaufs_init(void);
1facf9fc 7070+#else
062440b3 7071+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
4a4d8108 7072+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
062440b3
AM
7073+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
7074+ int topdown)
4a4d8108
AM
7075+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7076+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7077+AuStubVoid(dbgaufs_fin, void)
7078+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 7079+#endif /* CONFIG_DEBUG_FS */
7080+
7081+#endif /* __KERNEL__ */
7082+#endif /* __DBGAUFS_H__ */
7f207e10
AM
7083diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7084--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
7085+++ linux/fs/aufs/dcsub.c 2018-07-19 09:46:13.056647808 +0200
7086@@ -0,0 +1,226 @@
7087+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7088+/*
b00004a5 7089+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7090+ *
7091+ * This program, aufs is free software; you can redistribute it and/or modify
7092+ * it under the terms of the GNU General Public License as published by
7093+ * the Free Software Foundation; either version 2 of the License, or
7094+ * (at your option) any later version.
dece6358
AM
7095+ *
7096+ * This program is distributed in the hope that it will be useful,
7097+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7098+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7099+ * GNU General Public License for more details.
7100+ *
7101+ * You should have received a copy of the GNU General Public License
523b37e3 7102+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7103+ */
7104+
7105+/*
7106+ * sub-routines for dentry cache
7107+ */
7108+
7109+#include "aufs.h"
7110+
7111+static void au_dpage_free(struct au_dpage *dpage)
7112+{
7113+ int i;
7114+ struct dentry **p;
7115+
7116+ p = dpage->dentries;
7117+ for (i = 0; i < dpage->ndentry; i++)
7118+ dput(*p++);
1c60b727 7119+ free_page((unsigned long)dpage->dentries);
1facf9fc 7120+}
7121+
7122+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7123+{
7124+ int err;
7125+ void *p;
7126+
7127+ err = -ENOMEM;
7128+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7129+ if (unlikely(!dpages->dpages))
7130+ goto out;
7131+
7132+ p = (void *)__get_free_page(gfp);
7133+ if (unlikely(!p))
7134+ goto out_dpages;
7135+
7136+ dpages->dpages[0].ndentry = 0;
7137+ dpages->dpages[0].dentries = p;
7138+ dpages->ndpage = 1;
7139+ return 0; /* success */
7140+
4f0767ce 7141+out_dpages:
1c60b727 7142+ kfree(dpages->dpages);
4f0767ce 7143+out:
1facf9fc 7144+ return err;
7145+}
7146+
7147+void au_dpages_free(struct au_dcsub_pages *dpages)
7148+{
7149+ int i;
7150+ struct au_dpage *p;
7151+
7152+ p = dpages->dpages;
7153+ for (i = 0; i < dpages->ndpage; i++)
7154+ au_dpage_free(p++);
1c60b727 7155+ kfree(dpages->dpages);
1facf9fc 7156+}
7157+
7158+static int au_dpages_append(struct au_dcsub_pages *dpages,
7159+ struct dentry *dentry, gfp_t gfp)
7160+{
7161+ int err, sz;
7162+ struct au_dpage *dpage;
7163+ void *p;
7164+
7165+ dpage = dpages->dpages + dpages->ndpage - 1;
7166+ sz = PAGE_SIZE / sizeof(dentry);
7167+ if (unlikely(dpage->ndentry >= sz)) {
7168+ AuLabel(new dpage);
7169+ err = -ENOMEM;
7170+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7171+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7172+ sz + sizeof(*dpages->dpages), gfp,
7173+ /*may_shrink*/0);
1facf9fc 7174+ if (unlikely(!p))
7175+ goto out;
7176+
7177+ dpages->dpages = p;
7178+ dpage = dpages->dpages + dpages->ndpage;
7179+ p = (void *)__get_free_page(gfp);
7180+ if (unlikely(!p))
7181+ goto out;
7182+
7183+ dpage->ndentry = 0;
7184+ dpage->dentries = p;
7185+ dpages->ndpage++;
7186+ }
7187+
c1595e42 7188+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7189+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7190+ return 0; /* success */
7191+
4f0767ce 7192+out:
1facf9fc 7193+ return err;
7194+}
7195+
c1595e42
JR
7196+/* todo: BAD approach */
7197+/* copied from linux/fs/dcache.c */
7198+enum d_walk_ret {
7199+ D_WALK_CONTINUE,
7200+ D_WALK_QUIT,
7201+ D_WALK_NORETRY,
7202+ D_WALK_SKIP,
7203+};
7204+
7205+extern void d_walk(struct dentry *parent, void *data,
7206+ enum d_walk_ret (*enter)(void *, struct dentry *),
7207+ void (*finish)(void *));
7208+
7209+struct ac_dpages_arg {
1facf9fc 7210+ int err;
c1595e42
JR
7211+ struct au_dcsub_pages *dpages;
7212+ struct super_block *sb;
7213+ au_dpages_test test;
7214+ void *arg;
7215+};
1facf9fc 7216+
c1595e42
JR
7217+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7218+{
7219+ enum d_walk_ret ret;
7220+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7221+
c1595e42
JR
7222+ ret = D_WALK_CONTINUE;
7223+ if (dentry->d_sb == arg->sb
7224+ && !IS_ROOT(dentry)
7225+ && au_dcount(dentry) > 0
7226+ && au_di(dentry)
7227+ && (!arg->test || arg->test(dentry, arg->arg))) {
7228+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7229+ if (unlikely(arg->err))
7230+ ret = D_WALK_QUIT;
1facf9fc 7231+ }
7232+
c1595e42
JR
7233+ return ret;
7234+}
027c5e7a 7235+
c1595e42
JR
7236+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7237+ au_dpages_test test, void *arg)
7238+{
7239+ struct ac_dpages_arg args = {
7240+ .err = 0,
7241+ .dpages = dpages,
7242+ .sb = root->d_sb,
7243+ .test = test,
7244+ .arg = arg
7245+ };
027c5e7a 7246+
c1595e42
JR
7247+ d_walk(root, &args, au_call_dpages_append, NULL);
7248+
7249+ return args.err;
1facf9fc 7250+}
7251+
7252+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7253+ int do_include, au_dpages_test test, void *arg)
7254+{
7255+ int err;
7256+
7257+ err = 0;
027c5e7a
AM
7258+ write_seqlock(&rename_lock);
7259+ spin_lock(&dentry->d_lock);
7260+ if (do_include
c1595e42 7261+ && au_dcount(dentry) > 0
027c5e7a 7262+ && (!test || test(dentry, arg)))
1facf9fc 7263+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7264+ spin_unlock(&dentry->d_lock);
7265+ if (unlikely(err))
7266+ goto out;
7267+
7268+ /*
523b37e3 7269+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7270+ * mount
7271+ */
1facf9fc 7272+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7273+ dentry = dentry->d_parent; /* rename_lock is locked */
7274+ spin_lock(&dentry->d_lock);
c1595e42 7275+ if (au_dcount(dentry) > 0
027c5e7a 7276+ && (!test || test(dentry, arg)))
1facf9fc 7277+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7278+ spin_unlock(&dentry->d_lock);
7279+ if (unlikely(err))
7280+ break;
1facf9fc 7281+ }
7282+
4f0767ce 7283+out:
027c5e7a 7284+ write_sequnlock(&rename_lock);
1facf9fc 7285+ return err;
7286+}
7287+
027c5e7a
AM
7288+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7289+{
7290+ return au_di(dentry) && dentry->d_sb == arg;
7291+}
7292+
7293+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7294+ struct dentry *dentry, int do_include)
7295+{
7296+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7297+ au_dcsub_dpages_aufs, dentry->d_sb);
7298+}
7299+
4a4d8108 7300+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7301+{
4a4d8108
AM
7302+ struct path path[2] = {
7303+ {
7304+ .dentry = d1
7305+ },
7306+ {
7307+ .dentry = d2
7308+ }
7309+ };
1facf9fc 7310+
4a4d8108 7311+ return path_is_under(path + 0, path + 1);
1facf9fc 7312+}
7f207e10
AM
7313diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7314--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
7315+++ linux/fs/aufs/dcsub.h 2018-07-19 09:46:13.056647808 +0200
7316@@ -0,0 +1,137 @@
7317+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7318+/*
b00004a5 7319+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7320+ *
7321+ * This program, aufs is free software; you can redistribute it and/or modify
7322+ * it under the terms of the GNU General Public License as published by
7323+ * the Free Software Foundation; either version 2 of the License, or
7324+ * (at your option) any later version.
dece6358
AM
7325+ *
7326+ * This program is distributed in the hope that it will be useful,
7327+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7328+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7329+ * GNU General Public License for more details.
7330+ *
7331+ * You should have received a copy of the GNU General Public License
523b37e3 7332+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7333+ */
7334+
7335+/*
7336+ * sub-routines for dentry cache
7337+ */
7338+
7339+#ifndef __AUFS_DCSUB_H__
7340+#define __AUFS_DCSUB_H__
7341+
7342+#ifdef __KERNEL__
7343+
7f207e10 7344+#include <linux/dcache.h>
027c5e7a 7345+#include <linux/fs.h>
dece6358 7346+
1facf9fc 7347+struct au_dpage {
7348+ int ndentry;
7349+ struct dentry **dentries;
7350+};
7351+
7352+struct au_dcsub_pages {
7353+ int ndpage;
7354+ struct au_dpage *dpages;
7355+};
7356+
7357+/* ---------------------------------------------------------------------- */
7358+
7f207e10 7359+/* dcsub.c */
1facf9fc 7360+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7361+void au_dpages_free(struct au_dcsub_pages *dpages);
7362+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7363+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7364+ au_dpages_test test, void *arg);
7365+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7366+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7367+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7368+ struct dentry *dentry, int do_include);
4a4d8108 7369+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7370+
7f207e10
AM
7371+/* ---------------------------------------------------------------------- */
7372+
523b37e3
AM
7373+/*
7374+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7375+ * include/linux/dcache.h. Try them (in the future).
7376+ */
7377+
027c5e7a
AM
7378+static inline int au_d_hashed_positive(struct dentry *d)
7379+{
7380+ int err;
5527c038 7381+ struct inode *inode = d_inode(d);
076b876e 7382+
027c5e7a 7383+ err = 0;
5527c038
JR
7384+ if (unlikely(d_unhashed(d)
7385+ || d_is_negative(d)
7386+ || !inode->i_nlink))
027c5e7a
AM
7387+ err = -ENOENT;
7388+ return err;
7389+}
7390+
38d290e6
JR
7391+static inline int au_d_linkable(struct dentry *d)
7392+{
7393+ int err;
5527c038 7394+ struct inode *inode = d_inode(d);
076b876e 7395+
38d290e6
JR
7396+ err = au_d_hashed_positive(d);
7397+ if (err
5527c038 7398+ && d_is_positive(d)
38d290e6
JR
7399+ && (inode->i_state & I_LINKABLE))
7400+ err = 0;
7401+ return err;
7402+}
7403+
027c5e7a
AM
7404+static inline int au_d_alive(struct dentry *d)
7405+{
7406+ int err;
7407+ struct inode *inode;
076b876e 7408+
027c5e7a
AM
7409+ err = 0;
7410+ if (!IS_ROOT(d))
7411+ err = au_d_hashed_positive(d);
7412+ else {
5527c038
JR
7413+ inode = d_inode(d);
7414+ if (unlikely(d_unlinked(d)
7415+ || d_is_negative(d)
7416+ || !inode->i_nlink))
027c5e7a
AM
7417+ err = -ENOENT;
7418+ }
7419+ return err;
7420+}
7421+
7422+static inline int au_alive_dir(struct dentry *d)
7f207e10 7423+{
027c5e7a 7424+ int err;
076b876e 7425+
027c5e7a 7426+ err = au_d_alive(d);
5527c038 7427+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7428+ err = -ENOENT;
7429+ return err;
7f207e10
AM
7430+}
7431+
38d290e6
JR
7432+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7433+{
7434+ return a->len == b->len
7435+ && !memcmp(a->name, b->name, a->len);
7436+}
7437+
7e9cd9fe
AM
7438+/*
7439+ * by the commit
7440+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7441+ * taking d_lock
7442+ * the type of d_lockref.count became int, but the inlined function d_count()
7443+ * still returns unsigned int.
7444+ * I don't know why. Maybe it is for every d_count() users?
7445+ * Anyway au_dcount() lives on.
7446+ */
c1595e42
JR
7447+static inline int au_dcount(struct dentry *d)
7448+{
7449+ return (int)d_count(d);
7450+}
7451+
1facf9fc 7452+#endif /* __KERNEL__ */
7453+#endif /* __AUFS_DCSUB_H__ */
7f207e10
AM
7454diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7455--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
7456+++ linux/fs/aufs/debug.c 2018-07-19 09:46:13.056647808 +0200
7457@@ -0,0 +1,441 @@
7458+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7459+/*
b00004a5 7460+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7461+ *
7462+ * This program, aufs is free software; you can redistribute it and/or modify
7463+ * it under the terms of the GNU General Public License as published by
7464+ * the Free Software Foundation; either version 2 of the License, or
7465+ * (at your option) any later version.
dece6358
AM
7466+ *
7467+ * This program is distributed in the hope that it will be useful,
7468+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7469+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7470+ * GNU General Public License for more details.
7471+ *
7472+ * You should have received a copy of the GNU General Public License
523b37e3 7473+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7474+ */
7475+
7476+/*
7477+ * debug print functions
7478+ */
7479+
7480+#include "aufs.h"
7481+
392086de
AM
7482+/* Returns 0, or -errno. arg is in kp->arg. */
7483+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7484+{
7485+ int err, n;
7486+
7487+ err = kstrtoint(val, 0, &n);
7488+ if (!err) {
7489+ if (n > 0)
7490+ au_debug_on();
7491+ else
7492+ au_debug_off();
7493+ }
7494+ return err;
7495+}
7496+
7497+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7498+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7499+{
7500+ atomic_t *a;
7501+
7502+ a = kp->arg;
7503+ return sprintf(buffer, "%d", atomic_read(a));
7504+}
7505+
7506+static struct kernel_param_ops param_ops_atomic_t = {
7507+ .set = param_atomic_t_set,
7508+ .get = param_atomic_t_get
7509+ /* void (*free)(void *arg) */
7510+};
7511+
7512+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7513+MODULE_PARM_DESC(debug, "debug print");
392086de 7514+module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
1facf9fc 7515+
c1595e42 7516+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7517+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7518+#define dpri(fmt, ...) do { \
7519+ if ((au_plevel \
7520+ && strcmp(au_plevel, KERN_DEBUG)) \
7521+ || au_debug_test()) \
7522+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7523+} while (0)
7524+
7525+/* ---------------------------------------------------------------------- */
7526+
7527+void au_dpri_whlist(struct au_nhash *whlist)
7528+{
7529+ unsigned long ul, n;
7530+ struct hlist_head *head;
c06a8ce3 7531+ struct au_vdir_wh *pos;
1facf9fc 7532+
7533+ n = whlist->nh_num;
7534+ head = whlist->nh_head;
7535+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7536+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7537+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7538+ pos->wh_bindex,
7539+ pos->wh_str.len, pos->wh_str.name,
7540+ pos->wh_str.len);
1facf9fc 7541+ head++;
7542+ }
7543+}
7544+
7545+void au_dpri_vdir(struct au_vdir *vdir)
7546+{
7547+ unsigned long ul;
7548+ union au_vdir_deblk_p p;
7549+ unsigned char *o;
7550+
7551+ if (!vdir || IS_ERR(vdir)) {
7552+ dpri("err %ld\n", PTR_ERR(vdir));
7553+ return;
7554+ }
7555+
be118d29 7556+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7557+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7558+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7559+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7560+ p.deblk = vdir->vd_deblk[ul];
7561+ o = p.deblk;
7562+ dpri("[%lu]: %p\n", ul, o);
7563+ }
7564+}
7565+
53392da6 7566+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7567+ struct dentry *wh)
7568+{
7569+ char *n = NULL;
7570+ int l = 0;
7571+
7572+ if (!inode || IS_ERR(inode)) {
7573+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7574+ return -1;
7575+ }
7576+
c2b27bf2 7577+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7578+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7579+ && sizeof(inode->i_blocks) != sizeof(u64));
7580+ if (wh) {
7581+ n = (void *)wh->d_name.name;
7582+ l = wh->d_name.len;
7583+ }
7584+
53392da6
AM
7585+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7586+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7587+ bindex, inode,
1facf9fc 7588+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7589+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7590+ i_size_read(inode), (unsigned long long)inode->i_blocks,
53392da6 7591+ hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7592+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7593+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7594+ inode->i_generation,
1facf9fc 7595+ l ? ", wh " : "", l, n);
7596+ return 0;
7597+}
7598+
7599+void au_dpri_inode(struct inode *inode)
7600+{
7601+ struct au_iinfo *iinfo;
5afbbe0d 7602+ struct au_hinode *hi;
1facf9fc 7603+ aufs_bindex_t bindex;
53392da6 7604+ int err, hn;
1facf9fc 7605+
53392da6 7606+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7607+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7608+ return;
7609+
7610+ iinfo = au_ii(inode);
5afbbe0d
AM
7611+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7612+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7613+ if (iinfo->ii_btop < 0)
1facf9fc 7614+ return;
53392da6 7615+ hn = 0;
5afbbe0d
AM
7616+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7617+ hi = au_hinode(iinfo, bindex);
7618+ hn = !!au_hn(hi);
7619+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7620+ }
1facf9fc 7621+}
7622+
2cbb1c4b
JR
7623+void au_dpri_dalias(struct inode *inode)
7624+{
7625+ struct dentry *d;
7626+
7627+ spin_lock(&inode->i_lock);
c1595e42 7628+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7629+ au_dpri_dentry(d);
7630+ spin_unlock(&inode->i_lock);
7631+}
7632+
1facf9fc 7633+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7634+{
7635+ struct dentry *wh = NULL;
53392da6 7636+ int hn;
5afbbe0d 7637+ struct inode *inode;
076b876e 7638+ struct au_iinfo *iinfo;
5afbbe0d 7639+ struct au_hinode *hi;
1facf9fc 7640+
7641+ if (!dentry || IS_ERR(dentry)) {
7642+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7643+ return -1;
7644+ }
7645+ /* do not call dget_parent() here */
027c5e7a 7646+ /* note: access d_xxx without d_lock */
523b37e3
AM
7647+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7648+ bindex, dentry, dentry,
1facf9fc 7649+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7650+ au_dcount(dentry), dentry->d_flags,
523b37e3 7651+ d_unhashed(dentry) ? "un" : "");
53392da6 7652+ hn = -1;
5afbbe0d
AM
7653+ inode = NULL;
7654+ if (d_is_positive(dentry))
7655+ inode = d_inode(dentry);
7656+ if (inode
7657+ && au_test_aufs(dentry->d_sb)
7658+ && bindex >= 0
7659+ && !au_is_bad_inode(inode)) {
7660+ iinfo = au_ii(inode);
7661+ hi = au_hinode(iinfo, bindex);
7662+ hn = !!au_hn(hi);
7663+ wh = hi->hi_whdentry;
7664+ }
7665+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7666+ return 0;
7667+}
7668+
7669+void au_dpri_dentry(struct dentry *dentry)
7670+{
7671+ struct au_dinfo *dinfo;
7672+ aufs_bindex_t bindex;
7673+ int err;
7674+
7675+ err = do_pri_dentry(-1, dentry);
7676+ if (err || !au_test_aufs(dentry->d_sb))
7677+ return;
7678+
7679+ dinfo = au_di(dentry);
7680+ if (!dinfo)
7681+ return;
5afbbe0d
AM
7682+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7683+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7684+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7685+ dinfo->di_tmpfile);
5afbbe0d 7686+ if (dinfo->di_btop < 0)
1facf9fc 7687+ return;
5afbbe0d
AM
7688+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7689+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7690+}
7691+
7692+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7693+{
7694+ char a[32];
7695+
7696+ if (!file || IS_ERR(file)) {
7697+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7698+ return -1;
7699+ }
7700+ a[0] = 0;
7701+ if (bindex < 0
b912730e 7702+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7703+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7704+ && au_fi(file))
e49829fe 7705+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7706+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7707+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7708+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7709+ file->f_version, file->f_pos, a);
b912730e 7710+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7711+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7712+ return 0;
7713+}
7714+
7715+void au_dpri_file(struct file *file)
7716+{
7717+ struct au_finfo *finfo;
4a4d8108
AM
7718+ struct au_fidir *fidir;
7719+ struct au_hfile *hfile;
1facf9fc 7720+ aufs_bindex_t bindex;
7721+ int err;
7722+
7723+ err = do_pri_file(-1, file);
2000de60 7724+ if (err
b912730e 7725+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7726+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7727+ return;
7728+
7729+ finfo = au_fi(file);
7730+ if (!finfo)
7731+ return;
4a4d8108 7732+ if (finfo->fi_btop < 0)
1facf9fc 7733+ return;
4a4d8108
AM
7734+ fidir = finfo->fi_hdir;
7735+ if (!fidir)
7736+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7737+ else
e49829fe
JR
7738+ for (bindex = finfo->fi_btop;
7739+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7740+ bindex++) {
7741+ hfile = fidir->fd_hfile + bindex;
7742+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7743+ }
1facf9fc 7744+}
7745+
7746+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7747+{
7748+ struct vfsmount *mnt;
7749+ struct super_block *sb;
7750+
7751+ if (!br || IS_ERR(br))
7752+ goto out;
86dc4139 7753+ mnt = au_br_mnt(br);
1facf9fc 7754+ if (!mnt || IS_ERR(mnt))
7755+ goto out;
7756+ sb = mnt->mnt_sb;
7757+ if (!sb || IS_ERR(sb))
7758+ goto out;
7759+
5afbbe0d 7760+ dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
b752ccd1 7761+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7762+ "xino %d\n",
5afbbe0d 7763+ bindex, br->br_perm, br->br_id, au_br_count(br),
1e00d052 7764+ br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7765+ sb->s_flags, sb->s_count,
062440b3 7766+ atomic_read(&sb->s_active), !!au_xino_file(br));
1facf9fc 7767+ return 0;
7768+
4f0767ce 7769+out:
1facf9fc 7770+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7771+ return -1;
7772+}
7773+
7774+void au_dpri_sb(struct super_block *sb)
7775+{
7776+ struct au_sbinfo *sbinfo;
7777+ aufs_bindex_t bindex;
7778+ int err;
7779+ /* to reuduce stack size */
7780+ struct {
7781+ struct vfsmount mnt;
7782+ struct au_branch fake;
7783+ } *a;
7784+
7785+ /* this function can be called from magic sysrq */
7786+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7787+ if (unlikely(!a)) {
7788+ dpri("no memory\n");
7789+ return;
7790+ }
7791+
7792+ a->mnt.mnt_sb = sb;
86dc4139 7793+ a->fake.br_path.mnt = &a->mnt;
5afbbe0d 7794+ au_br_count_init(&a->fake);
1facf9fc 7795+ err = do_pri_br(-1, &a->fake);
5afbbe0d 7796+ au_br_count_fin(&a->fake);
1c60b727 7797+ kfree(a);
1facf9fc 7798+ dpri("dev 0x%x\n", sb->s_dev);
7799+ if (err || !au_test_aufs(sb))
7800+ return;
7801+
7802+ sbinfo = au_sbi(sb);
7803+ if (!sbinfo)
7804+ return;
f0c0a007
AM
7805+ dpri("nw %d, gen %u, kobj %d\n",
7806+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7807+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7808+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7809+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7810+}
7811+
7812+/* ---------------------------------------------------------------------- */
7813+
027c5e7a
AM
7814+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7815+{
5527c038 7816+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7817+ struct dentry *h_dentry;
5afbbe0d 7818+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7819+
7820+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7821+ return;
7822+
5afbbe0d
AM
7823+ bbot = au_dbbot(dentry);
7824+ bi = au_ibbot(inode);
7825+ if (bi < bbot)
7826+ bbot = bi;
7827+ bindex = au_dbtop(dentry);
7828+ bi = au_ibtop(inode);
027c5e7a
AM
7829+ if (bi > bindex)
7830+ bindex = bi;
7831+
5afbbe0d 7832+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7833+ h_dentry = au_h_dptr(dentry, bindex);
7834+ if (!h_dentry)
7835+ continue;
7836+ h_inode = au_h_iptr(inode, bindex);
5527c038 7837+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7838+ au_debug_on();
027c5e7a
AM
7839+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7840+ AuDbgDentry(dentry);
7841+ AuDbgInode(inode);
392086de 7842+ au_debug_off();
027c5e7a
AM
7843+ BUG();
7844+ }
7845+ }
7846+}
7847+
1facf9fc 7848+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7849+{
7850+ int err, i, j;
7851+ struct au_dcsub_pages dpages;
7852+ struct au_dpage *dpage;
7853+ struct dentry **dentries;
7854+
7855+ err = au_dpages_init(&dpages, GFP_NOFS);
7856+ AuDebugOn(err);
027c5e7a 7857+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7858+ AuDebugOn(err);
7859+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7860+ dpage = dpages.dpages + i;
7861+ dentries = dpage->dentries;
7862+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7863+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7864+ }
7865+ au_dpages_free(&dpages);
7866+}
7867+
1facf9fc 7868+void au_dbg_verify_kthread(void)
7869+{
53392da6 7870+ if (au_wkq_test()) {
1facf9fc 7871+ au_dbg_blocked();
1e00d052
AM
7872+ /*
7873+ * It may be recursive, but udba=notify between two aufs mounts,
7874+ * where a single ro branch is shared, is not a problem.
7875+ */
7876+ /* WARN_ON(1); */
1facf9fc 7877+ }
7878+}
7879+
7880+/* ---------------------------------------------------------------------- */
7881+
1facf9fc 7882+int __init au_debug_init(void)
7883+{
7884+ aufs_bindex_t bindex;
7885+ struct au_vdir_destr destr;
7886+
7887+ bindex = -1;
7888+ AuDebugOn(bindex >= 0);
7889+
7890+ destr.len = -1;
7891+ AuDebugOn(destr.len < NAME_MAX);
7892+
7893+#ifdef CONFIG_4KSTACKS
0c3ec466 7894+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7895+#endif
7896+
1facf9fc 7897+ return 0;
7898+}
7f207e10
AM
7899diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7900--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
7901+++ linux/fs/aufs/debug.h 2018-07-19 09:46:13.056647808 +0200
7902@@ -0,0 +1,226 @@
7903+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7904+/*
b00004a5 7905+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 7906+ *
7907+ * This program, aufs is free software; you can redistribute it and/or modify
7908+ * it under the terms of the GNU General Public License as published by
7909+ * the Free Software Foundation; either version 2 of the License, or
7910+ * (at your option) any later version.
dece6358
AM
7911+ *
7912+ * This program is distributed in the hope that it will be useful,
7913+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7914+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7915+ * GNU General Public License for more details.
7916+ *
7917+ * You should have received a copy of the GNU General Public License
523b37e3 7918+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7919+ */
7920+
7921+/*
7922+ * debug print functions
7923+ */
7924+
7925+#ifndef __AUFS_DEBUG_H__
7926+#define __AUFS_DEBUG_H__
7927+
7928+#ifdef __KERNEL__
7929+
392086de 7930+#include <linux/atomic.h>
4a4d8108
AM
7931+#include <linux/module.h>
7932+#include <linux/kallsyms.h>
1facf9fc 7933+#include <linux/sysrq.h>
4a4d8108 7934+
1facf9fc 7935+#ifdef CONFIG_AUFS_DEBUG
7936+#define AuDebugOn(a) BUG_ON(a)
7937+
7938+/* module parameter */
392086de
AM
7939+extern atomic_t aufs_debug;
7940+static inline void au_debug_on(void)
1facf9fc 7941+{
392086de
AM
7942+ atomic_inc(&aufs_debug);
7943+}
7944+static inline void au_debug_off(void)
7945+{
7946+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7947+}
7948+
7949+static inline int au_debug_test(void)
7950+{
392086de 7951+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7952+}
7953+#else
7954+#define AuDebugOn(a) do {} while (0)
392086de
AM
7955+AuStubVoid(au_debug_on, void)
7956+AuStubVoid(au_debug_off, void)
4a4d8108 7957+AuStubInt0(au_debug_test, void)
1facf9fc 7958+#endif /* CONFIG_AUFS_DEBUG */
7959+
392086de
AM
7960+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7961+
1facf9fc 7962+/* ---------------------------------------------------------------------- */
7963+
7964+/* debug print */
7965+
4a4d8108 7966+#define AuDbg(fmt, ...) do { \
1facf9fc 7967+ if (au_debug_test()) \
4a4d8108 7968+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7969+} while (0)
4a4d8108
AM
7970+#define AuLabel(l) AuDbg(#l "\n")
7971+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7972+#define AuWarn1(fmt, ...) do { \
1facf9fc 7973+ static unsigned char _c; \
7974+ if (!_c++) \
0c3ec466 7975+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7976+} while (0)
7977+
4a4d8108 7978+#define AuErr1(fmt, ...) do { \
1facf9fc 7979+ static unsigned char _c; \
7980+ if (!_c++) \
4a4d8108 7981+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7982+} while (0)
7983+
4a4d8108 7984+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7985+ static unsigned char _c; \
7986+ if (!_c++) \
4a4d8108 7987+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7988+} while (0)
7989+
7990+#define AuUnsupportMsg "This operation is not supported." \
7991+ " Please report this application to aufs-users ML."
4a4d8108
AM
7992+#define AuUnsupport(fmt, ...) do { \
7993+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7994+ dump_stack(); \
7995+} while (0)
7996+
7997+#define AuTraceErr(e) do { \
7998+ if (unlikely((e) < 0)) \
7999+ AuDbg("err %d\n", (int)(e)); \
8000+} while (0)
8001+
8002+#define AuTraceErrPtr(p) do { \
8003+ if (IS_ERR(p)) \
8004+ AuDbg("err %ld\n", PTR_ERR(p)); \
8005+} while (0)
8006+
8007+/* dirty macros for debug print, use with "%.*s" and caution */
8008+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 8009+
8010+/* ---------------------------------------------------------------------- */
8011+
dece6358 8012+struct dentry;
1facf9fc 8013+#ifdef CONFIG_AUFS_DEBUG
c1595e42 8014+extern struct mutex au_dbg_mtx;
1facf9fc 8015+extern char *au_plevel;
8016+struct au_nhash;
8017+void au_dpri_whlist(struct au_nhash *whlist);
8018+struct au_vdir;
8019+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 8020+struct inode;
1facf9fc 8021+void au_dpri_inode(struct inode *inode);
2cbb1c4b 8022+void au_dpri_dalias(struct inode *inode);
1facf9fc 8023+void au_dpri_dentry(struct dentry *dentry);
dece6358 8024+struct file;
1facf9fc 8025+void au_dpri_file(struct file *filp);
dece6358 8026+struct super_block;
1facf9fc 8027+void au_dpri_sb(struct super_block *sb);
8028+
027c5e7a
AM
8029+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
8030+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 8031+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 8032+void au_dbg_verify_kthread(void);
8033+
8034+int __init au_debug_init(void);
7e9cd9fe 8035+
1facf9fc 8036+#define AuDbgWhlist(w) do { \
c1595e42 8037+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8038+ AuDbg(#w "\n"); \
8039+ au_dpri_whlist(w); \
c1595e42 8040+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8041+} while (0)
8042+
8043+#define AuDbgVdir(v) do { \
c1595e42 8044+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8045+ AuDbg(#v "\n"); \
8046+ au_dpri_vdir(v); \
c1595e42 8047+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8048+} while (0)
8049+
8050+#define AuDbgInode(i) do { \
c1595e42 8051+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8052+ AuDbg(#i "\n"); \
8053+ au_dpri_inode(i); \
c1595e42 8054+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8055+} while (0)
8056+
2cbb1c4b 8057+#define AuDbgDAlias(i) do { \
c1595e42 8058+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
8059+ AuDbg(#i "\n"); \
8060+ au_dpri_dalias(i); \
c1595e42 8061+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
8062+} while (0)
8063+
1facf9fc 8064+#define AuDbgDentry(d) do { \
c1595e42 8065+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8066+ AuDbg(#d "\n"); \
8067+ au_dpri_dentry(d); \
c1595e42 8068+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8069+} while (0)
8070+
8071+#define AuDbgFile(f) do { \
c1595e42 8072+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8073+ AuDbg(#f "\n"); \
8074+ au_dpri_file(f); \
c1595e42 8075+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8076+} while (0)
8077+
8078+#define AuDbgSb(sb) do { \
c1595e42 8079+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8080+ AuDbg(#sb "\n"); \
8081+ au_dpri_sb(sb); \
c1595e42 8082+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8083+} while (0)
8084+
4a4d8108
AM
8085+#define AuDbgSym(addr) do { \
8086+ char sym[KSYM_SYMBOL_LEN]; \
8087+ sprint_symbol(sym, (unsigned long)addr); \
8088+ AuDbg("%s\n", sym); \
8089+} while (0)
1facf9fc 8090+#else
027c5e7a 8091+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
8092+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8093+AuStubVoid(au_dbg_verify_kthread, void)
8094+AuStubInt0(__init au_debug_init, void)
1facf9fc 8095+
1facf9fc 8096+#define AuDbgWhlist(w) do {} while (0)
8097+#define AuDbgVdir(v) do {} while (0)
8098+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 8099+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 8100+#define AuDbgDentry(d) do {} while (0)
8101+#define AuDbgFile(f) do {} while (0)
8102+#define AuDbgSb(sb) do {} while (0)
4a4d8108 8103+#define AuDbgSym(addr) do {} while (0)
1facf9fc 8104+#endif /* CONFIG_AUFS_DEBUG */
8105+
8106+/* ---------------------------------------------------------------------- */
8107+
8108+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8109+int __init au_sysrq_init(void);
8110+void au_sysrq_fin(void);
8111+
8112+#ifdef CONFIG_HW_CONSOLE
8113+#define au_dbg_blocked() do { \
8114+ WARN_ON(1); \
0c5527e5 8115+ handle_sysrq('w'); \
1facf9fc 8116+} while (0)
8117+#else
4a4d8108 8118+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8119+#endif
8120+
8121+#else
4a4d8108
AM
8122+AuStubInt0(__init au_sysrq_init, void)
8123+AuStubVoid(au_sysrq_fin, void)
8124+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8125+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8126+
8127+#endif /* __KERNEL__ */
8128+#endif /* __AUFS_DEBUG_H__ */
7f207e10
AM
8129diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8130--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
8131+++ linux/fs/aufs/dentry.c 2018-07-19 09:46:13.056647808 +0200
8132@@ -0,0 +1,1153 @@
8133+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 8134+/*
b00004a5 8135+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 8136+ *
8137+ * This program, aufs is free software; you can redistribute it and/or modify
8138+ * it under the terms of the GNU General Public License as published by
8139+ * the Free Software Foundation; either version 2 of the License, or
8140+ * (at your option) any later version.
dece6358
AM
8141+ *
8142+ * This program is distributed in the hope that it will be useful,
8143+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8144+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8145+ * GNU General Public License for more details.
8146+ *
8147+ * You should have received a copy of the GNU General Public License
523b37e3 8148+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8149+ */
8150+
8151+/*
8152+ * lookup and dentry operations
8153+ */
8154+
dece6358 8155+#include <linux/namei.h>
1facf9fc 8156+#include "aufs.h"
8157+
1facf9fc 8158+/*
8159+ * returns positive/negative dentry, NULL or an error.
8160+ * NULL means whiteout-ed or not-found.
8161+ */
8162+static struct dentry*
8163+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8164+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8165+{
8166+ struct dentry *h_dentry;
2000de60 8167+ struct inode *h_inode;
1facf9fc 8168+ struct au_branch *br;
8169+ int wh_found, opq;
8170+ unsigned char wh_able;
8171+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8172+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8173+ IGNORE_PERM);
1facf9fc 8174+
1facf9fc 8175+ wh_found = 0;
8176+ br = au_sbr(dentry->d_sb, bindex);
8177+ wh_able = !!au_br_whable(br->br_perm);
8178+ if (wh_able)
8b6a4947 8179+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
1facf9fc 8180+ h_dentry = ERR_PTR(wh_found);
8181+ if (!wh_found)
8182+ goto real_lookup;
8183+ if (unlikely(wh_found < 0))
8184+ goto out;
8185+
8186+ /* We found a whiteout */
5afbbe0d 8187+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8188+ au_set_dbwh(dentry, bindex);
8189+ if (!allow_neg)
8190+ return NULL; /* success */
8191+
4f0767ce 8192+real_lookup:
076b876e 8193+ if (!ignore_perm)
8b6a4947 8194+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8195+ else
8b6a4947 8196+ h_dentry = au_sio_lkup_one(args->name, h_parent);
2000de60
JR
8197+ if (IS_ERR(h_dentry)) {
8198+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8199+ && !allow_neg)
8200+ h_dentry = NULL;
1facf9fc 8201+ goto out;
2000de60 8202+ }
1facf9fc 8203+
5527c038
JR
8204+ h_inode = d_inode(h_dentry);
8205+ if (d_is_negative(h_dentry)) {
1facf9fc 8206+ if (!allow_neg)
8207+ goto out_neg;
8208+ } else if (wh_found
8209+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8210+ goto out_neg;
8b6a4947
AM
8211+ else if (au_ftest_lkup(args->flags, DIRREN)
8212+ /* && h_inode */
8213+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8214+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8215+ (unsigned long long)h_inode->i_ino);
8216+ goto out_neg;
8217+ }
1facf9fc 8218+
5afbbe0d
AM
8219+ if (au_dbbot(dentry) <= bindex)
8220+ au_set_dbbot(dentry, bindex);
8221+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8222+ au_set_dbtop(dentry, bindex);
1facf9fc 8223+ au_set_h_dptr(dentry, bindex, h_dentry);
8224+
2000de60
JR
8225+ if (!d_is_dir(h_dentry)
8226+ || !wh_able
5527c038 8227+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8228+ goto out; /* success */
8229+
be118d29 8230+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
076b876e 8231+ opq = au_diropq_test(h_dentry);
3c1bdaff 8232+ inode_unlock_shared(h_inode);
1facf9fc 8233+ if (opq > 0)
8234+ au_set_dbdiropq(dentry, bindex);
8235+ else if (unlikely(opq < 0)) {
8236+ au_set_h_dptr(dentry, bindex, NULL);
8237+ h_dentry = ERR_PTR(opq);
8238+ }
8239+ goto out;
8240+
4f0767ce 8241+out_neg:
1facf9fc 8242+ dput(h_dentry);
8243+ h_dentry = NULL;
4f0767ce 8244+out:
1facf9fc 8245+ return h_dentry;
8246+}
8247+
dece6358
AM
8248+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8249+{
8250+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8251+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8252+ return -EPERM;
8253+ return 0;
8254+}
8255+
1facf9fc 8256+/*
8257+ * returns the number of lower positive dentries,
8258+ * otherwise an error.
8259+ * can be called at unlinking with @type is zero.
8260+ */
5afbbe0d
AM
8261+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8262+ unsigned int flags)
1facf9fc 8263+{
8264+ int npositive, err;
8265+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8266+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8267+ struct au_do_lookup_args args = {
8b6a4947
AM
8268+ .flags = flags,
8269+ .name = &dentry->d_name
1facf9fc 8270+ };
1facf9fc 8271+ struct dentry *parent;
076b876e 8272+ struct super_block *sb;
1facf9fc 8273+
076b876e 8274+ sb = dentry->d_sb;
8b6a4947 8275+ err = au_test_shwh(sb, args.name);
dece6358 8276+ if (unlikely(err))
1facf9fc 8277+ goto out;
8278+
8b6a4947 8279+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8280+ if (unlikely(err))
8281+ goto out;
8282+
2000de60 8283+ isdir = !!d_is_dir(dentry);
076b876e 8284+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8285+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8286+ if (dirren)
8287+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8288+
8289+ npositive = 0;
4a4d8108 8290+ parent = dget_parent(dentry);
1facf9fc 8291+ btail = au_dbtaildir(parent);
5afbbe0d 8292+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8293+ struct dentry *h_parent, *h_dentry;
8294+ struct inode *h_inode, *h_dir;
8b6a4947 8295+ struct au_branch *br;
1facf9fc 8296+
8297+ h_dentry = au_h_dptr(dentry, bindex);
8298+ if (h_dentry) {
5527c038 8299+ if (d_is_positive(h_dentry))
1facf9fc 8300+ npositive++;
5afbbe0d 8301+ break;
1facf9fc 8302+ }
8303+ h_parent = au_h_dptr(parent, bindex);
2000de60 8304+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8305+ continue;
8306+
8b6a4947
AM
8307+ if (dirren) {
8308+ /* if the inum matches, then use the prepared name */
8309+ err = au_dr_lkup_name(&args, bindex);
8310+ if (unlikely(err))
8311+ goto out_parent;
8312+ }
8313+
5527c038 8314+ h_dir = d_inode(h_parent);
be118d29 8315+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8316+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8317+ inode_unlock_shared(h_dir);
1facf9fc 8318+ err = PTR_ERR(h_dentry);
8319+ if (IS_ERR(h_dentry))
4a4d8108 8320+ goto out_parent;
2000de60
JR
8321+ if (h_dentry)
8322+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8323+ if (dirperm1)
8324+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8325+
79b8bda9 8326+ if (au_dbwh(dentry) == bindex)
1facf9fc 8327+ break;
8328+ if (!h_dentry)
8329+ continue;
5527c038 8330+ if (d_is_negative(h_dentry))
1facf9fc 8331+ continue;
5527c038 8332+ h_inode = d_inode(h_dentry);
1facf9fc 8333+ npositive++;
8334+ if (!args.type)
8335+ args.type = h_inode->i_mode & S_IFMT;
8336+ if (args.type != S_IFDIR)
8337+ break;
8338+ else if (isdir) {
8339+ /* the type of lower may be different */
8340+ bdiropq = au_dbdiropq(dentry);
8341+ if (bdiropq >= 0 && bdiropq <= bindex)
8342+ break;
8343+ }
8b6a4947
AM
8344+ br = au_sbr(sb, bindex);
8345+ if (dirren
8346+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8347+ /*add_ent*/NULL)) {
8348+ /* prepare next name to lookup */
8349+ err = au_dr_lkup(&args, dentry, bindex);
8350+ if (unlikely(err))
8351+ goto out_parent;
8352+ }
1facf9fc 8353+ }
8354+
8355+ if (npositive) {
8356+ AuLabel(positive);
5afbbe0d 8357+ au_update_dbtop(dentry);
1facf9fc 8358+ }
8359+ err = npositive;
076b876e 8360+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8361+ && au_dbtop(dentry) < 0)) {
1facf9fc 8362+ err = -EIO;
523b37e3
AM
8363+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8364+ dentry, err);
027c5e7a 8365+ }
1facf9fc 8366+
4f0767ce 8367+out_parent:
4a4d8108 8368+ dput(parent);
8b6a4947
AM
8369+ kfree(args.whname.name);
8370+ if (dirren)
8371+ au_dr_lkup_fin(&args);
4f0767ce 8372+out:
1facf9fc 8373+ return err;
8374+}
8375+
076b876e 8376+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 8377+{
8378+ struct dentry *dentry;
8379+ int wkq_err;
8380+
5527c038 8381+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 8382+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8383+ else {
b4510431
AM
8384+ struct vfsub_lkup_one_args args = {
8385+ .errp = &dentry,
8386+ .name = name,
8387+ .parent = parent
1facf9fc 8388+ };
8389+
b4510431 8390+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8391+ if (unlikely(wkq_err))
8392+ dentry = ERR_PTR(wkq_err);
8393+ }
8394+
8395+ return dentry;
8396+}
8397+
8398+/*
8399+ * lookup @dentry on @bindex which should be negative.
8400+ */
86dc4139 8401+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8402+{
8403+ int err;
8404+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8405+ struct au_branch *br;
1facf9fc 8406+
1facf9fc 8407+ parent = dget_parent(dentry);
8408+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
8409+ br = au_sbr(dentry->d_sb, bindex);
8410+ if (wh)
8411+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8412+ else
076b876e 8413+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 8414+ err = PTR_ERR(h_dentry);
8415+ if (IS_ERR(h_dentry))
8416+ goto out;
5527c038 8417+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8418+ err = -EIO;
523b37e3 8419+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8420+ dput(h_dentry);
8421+ goto out;
8422+ }
8423+
4a4d8108 8424+ err = 0;
5afbbe0d
AM
8425+ if (bindex < au_dbtop(dentry))
8426+ au_set_dbtop(dentry, bindex);
8427+ if (au_dbbot(dentry) < bindex)
8428+ au_set_dbbot(dentry, bindex);
1facf9fc 8429+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8430+
4f0767ce 8431+out:
1facf9fc 8432+ dput(parent);
8433+ return err;
8434+}
8435+
8436+/* ---------------------------------------------------------------------- */
8437+
8438+/* subset of struct inode */
8439+struct au_iattr {
8440+ unsigned long i_ino;
8441+ /* unsigned int i_nlink; */
0c3ec466
AM
8442+ kuid_t i_uid;
8443+ kgid_t i_gid;
1facf9fc 8444+ u64 i_version;
8445+/*
8446+ loff_t i_size;
8447+ blkcnt_t i_blocks;
8448+*/
8449+ umode_t i_mode;
8450+};
8451+
8452+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8453+{
8454+ ia->i_ino = h_inode->i_ino;
8455+ /* ia->i_nlink = h_inode->i_nlink; */
8456+ ia->i_uid = h_inode->i_uid;
8457+ ia->i_gid = h_inode->i_gid;
be118d29 8458+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8459+/*
8460+ ia->i_size = h_inode->i_size;
8461+ ia->i_blocks = h_inode->i_blocks;
8462+*/
8463+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8464+}
8465+
8466+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8467+{
8468+ return ia->i_ino != h_inode->i_ino
8469+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8470+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8471+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8472+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8473+/*
8474+ || ia->i_size != h_inode->i_size
8475+ || ia->i_blocks != h_inode->i_blocks
8476+*/
8477+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8478+}
8479+
8480+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8481+ struct au_branch *br)
8482+{
8483+ int err;
8484+ struct au_iattr ia;
8485+ struct inode *h_inode;
8486+ struct dentry *h_d;
8487+ struct super_block *h_sb;
8488+
8489+ err = 0;
8490+ memset(&ia, -1, sizeof(ia));
8491+ h_sb = h_dentry->d_sb;
5527c038
JR
8492+ h_inode = NULL;
8493+ if (d_is_positive(h_dentry)) {
8494+ h_inode = d_inode(h_dentry);
1facf9fc 8495+ au_iattr_save(&ia, h_inode);
5527c038 8496+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8497+ /* nfs d_revalidate may return 0 for negative dentry */
8498+ /* fuse d_revalidate always return 0 for negative dentry */
8499+ goto out;
8500+
8501+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8502+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8503+ err = PTR_ERR(h_d);
8504+ if (IS_ERR(h_d))
8505+ goto out;
8506+
8507+ err = 0;
8508+ if (unlikely(h_d != h_dentry
5527c038 8509+ || d_inode(h_d) != h_inode
1facf9fc 8510+ || (h_inode && au_iattr_test(&ia, h_inode))))
8511+ err = au_busy_or_stale();
8512+ dput(h_d);
8513+
4f0767ce 8514+out:
1facf9fc 8515+ AuTraceErr(err);
8516+ return err;
8517+}
8518+
8519+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8520+ struct dentry *h_parent, struct au_branch *br)
8521+{
8522+ int err;
8523+
8524+ err = 0;
027c5e7a
AM
8525+ if (udba == AuOpt_UDBA_REVAL
8526+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8527+ IMustLock(h_dir);
5527c038 8528+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8529+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8530+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8531+
8532+ return err;
8533+}
8534+
8535+/* ---------------------------------------------------------------------- */
8536+
027c5e7a 8537+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8538+{
027c5e7a 8539+ int err;
5afbbe0d 8540+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8541+ struct au_hdentry tmp, *p, *q;
8542+ struct au_dinfo *dinfo;
8543+ struct super_block *sb;
1facf9fc 8544+
027c5e7a 8545+ DiMustWriteLock(dentry);
1308ab2a 8546+
027c5e7a
AM
8547+ sb = dentry->d_sb;
8548+ dinfo = au_di(dentry);
5afbbe0d 8549+ bbot = dinfo->di_bbot;
1facf9fc 8550+ bwh = dinfo->di_bwh;
8551+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8552+ bindex = dinfo->di_btop;
8553+ p = au_hdentry(dinfo, bindex);
8554+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8555+ if (!p->hd_dentry)
1facf9fc 8556+ continue;
8557+
027c5e7a
AM
8558+ new_bindex = au_br_index(sb, p->hd_id);
8559+ if (new_bindex == bindex)
1facf9fc 8560+ continue;
1facf9fc 8561+
1facf9fc 8562+ if (dinfo->di_bwh == bindex)
8563+ bwh = new_bindex;
8564+ if (dinfo->di_bdiropq == bindex)
8565+ bdiropq = new_bindex;
8566+ if (new_bindex < 0) {
8567+ au_hdput(p);
8568+ p->hd_dentry = NULL;
8569+ continue;
8570+ }
8571+
8572+ /* swap two lower dentries, and loop again */
5afbbe0d 8573+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8574+ tmp = *q;
8575+ *q = *p;
8576+ *p = tmp;
8577+ if (tmp.hd_dentry) {
8578+ bindex--;
8579+ p--;
8580+ }
8581+ }
8582+
1facf9fc 8583+ dinfo->di_bwh = -1;
5afbbe0d 8584+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8585+ dinfo->di_bwh = bwh;
8586+
8587+ dinfo->di_bdiropq = -1;
8588+ if (bdiropq >= 0
5afbbe0d 8589+ && bdiropq <= au_sbbot(sb)
1facf9fc 8590+ && au_sbr_whable(sb, bdiropq))
8591+ dinfo->di_bdiropq = bdiropq;
8592+
027c5e7a 8593+ err = -EIO;
5afbbe0d
AM
8594+ dinfo->di_btop = -1;
8595+ dinfo->di_bbot = -1;
8596+ bbot = au_dbbot(parent);
8597+ bindex = 0;
8598+ p = au_hdentry(dinfo, bindex);
8599+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8600+ if (p->hd_dentry) {
5afbbe0d 8601+ dinfo->di_btop = bindex;
1facf9fc 8602+ break;
8603+ }
8604+
5afbbe0d
AM
8605+ if (dinfo->di_btop >= 0) {
8606+ bindex = bbot;
8607+ p = au_hdentry(dinfo, bindex);
8608+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8609+ if (p->hd_dentry) {
5afbbe0d 8610+ dinfo->di_bbot = bindex;
027c5e7a
AM
8611+ err = 0;
8612+ break;
8613+ }
8614+ }
8615+
8616+ return err;
1facf9fc 8617+}
8618+
027c5e7a 8619+static void au_do_hide(struct dentry *dentry)
1facf9fc 8620+{
027c5e7a 8621+ struct inode *inode;
1facf9fc 8622+
5527c038
JR
8623+ if (d_really_is_positive(dentry)) {
8624+ inode = d_inode(dentry);
8625+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8626+ if (inode->i_nlink && !d_unhashed(dentry))
8627+ drop_nlink(inode);
8628+ } else {
8629+ clear_nlink(inode);
8630+ /* stop next lookup */
8631+ inode->i_flags |= S_DEAD;
8632+ }
8633+ smp_mb(); /* necessary? */
8634+ }
8635+ d_drop(dentry);
8636+}
1308ab2a 8637+
027c5e7a
AM
8638+static int au_hide_children(struct dentry *parent)
8639+{
8640+ int err, i, j, ndentry;
8641+ struct au_dcsub_pages dpages;
8642+ struct au_dpage *dpage;
8643+ struct dentry *dentry;
1facf9fc 8644+
027c5e7a 8645+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8646+ if (unlikely(err))
8647+ goto out;
027c5e7a
AM
8648+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8649+ if (unlikely(err))
8650+ goto out_dpages;
1facf9fc 8651+
027c5e7a
AM
8652+ /* in reverse order */
8653+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8654+ dpage = dpages.dpages + i;
8655+ ndentry = dpage->ndentry;
8656+ for (j = ndentry - 1; j >= 0; j--) {
8657+ dentry = dpage->dentries[j];
8658+ if (dentry != parent)
8659+ au_do_hide(dentry);
8660+ }
8661+ }
1facf9fc 8662+
027c5e7a
AM
8663+out_dpages:
8664+ au_dpages_free(&dpages);
4f0767ce 8665+out:
027c5e7a 8666+ return err;
1facf9fc 8667+}
8668+
027c5e7a 8669+static void au_hide(struct dentry *dentry)
1facf9fc 8670+{
027c5e7a 8671+ int err;
1facf9fc 8672+
027c5e7a 8673+ AuDbgDentry(dentry);
2000de60 8674+ if (d_is_dir(dentry)) {
027c5e7a
AM
8675+ /* shrink_dcache_parent(dentry); */
8676+ err = au_hide_children(dentry);
8677+ if (unlikely(err))
523b37e3
AM
8678+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8679+ dentry, err);
027c5e7a
AM
8680+ }
8681+ au_do_hide(dentry);
8682+}
1facf9fc 8683+
027c5e7a
AM
8684+/*
8685+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8686+ *
8687+ * a dirty branch is added
8688+ * - on the top of layers
8689+ * - in the middle of layers
8690+ * - to the bottom of layers
8691+ *
8692+ * on the added branch there exists
8693+ * - a whiteout
8694+ * - a diropq
8695+ * - a same named entry
8696+ * + exist
8697+ * * negative --> positive
8698+ * * positive --> positive
8699+ * - type is unchanged
8700+ * - type is changed
8701+ * + doesn't exist
8702+ * * negative --> negative
8703+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8704+ * - none
8705+ */
8706+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8707+ struct au_dinfo *tmp)
8708+{
8709+ int err;
5afbbe0d 8710+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8711+ struct {
8712+ struct dentry *dentry;
8713+ struct inode *inode;
8714+ mode_t mode;
be52b249
AM
8715+ } orig_h, tmp_h = {
8716+ .dentry = NULL
8717+ };
027c5e7a
AM
8718+ struct au_hdentry *hd;
8719+ struct inode *inode, *h_inode;
8720+ struct dentry *h_dentry;
8721+
8722+ err = 0;
5afbbe0d 8723+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8724+ orig_h.mode = 0;
5afbbe0d 8725+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8726+ orig_h.inode = NULL;
8727+ if (d_is_positive(orig_h.dentry)) {
8728+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8729+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8730+ }
5afbbe0d
AM
8731+ if (tmp->di_btop >= 0) {
8732+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8733+ if (d_is_positive(tmp_h.dentry)) {
8734+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8735+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8736+ }
027c5e7a
AM
8737+ }
8738+
5527c038
JR
8739+ inode = NULL;
8740+ if (d_really_is_positive(dentry))
8741+ inode = d_inode(dentry);
027c5e7a
AM
8742+ if (!orig_h.inode) {
8743+ AuDbg("nagative originally\n");
8744+ if (inode) {
8745+ au_hide(dentry);
8746+ goto out;
8747+ }
8748+ AuDebugOn(inode);
5afbbe0d 8749+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8750+ AuDebugOn(dinfo->di_bdiropq != -1);
8751+
8752+ if (!tmp_h.inode) {
8753+ AuDbg("negative --> negative\n");
8754+ /* should have only one negative lower */
5afbbe0d
AM
8755+ if (tmp->di_btop >= 0
8756+ && tmp->di_btop < dinfo->di_btop) {
8757+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8758+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8759+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8760+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8761+ hd = au_hdentry(tmp, tmp->di_btop);
8762+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8763+ dget(hd->hd_dentry));
8764+ }
8765+ au_dbg_verify_dinode(dentry);
8766+ } else {
8767+ AuDbg("negative --> positive\n");
8768+ /*
8769+ * similar to the behaviour of creating with bypassing
8770+ * aufs.
8771+ * unhash it in order to force an error in the
8772+ * succeeding create operation.
8773+ * we should not set S_DEAD here.
8774+ */
8775+ d_drop(dentry);
8776+ /* au_di_swap(tmp, dinfo); */
8777+ au_dbg_verify_dinode(dentry);
8778+ }
8779+ } else {
8780+ AuDbg("positive originally\n");
8781+ /* inode may be NULL */
8782+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8783+ if (!tmp_h.inode) {
8784+ AuDbg("positive --> negative\n");
8785+ /* or bypassing aufs */
8786+ au_hide(dentry);
5afbbe0d 8787+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8788+ dinfo->di_bwh = tmp->di_bwh;
8789+ if (inode)
8790+ err = au_refresh_hinode_self(inode);
8791+ au_dbg_verify_dinode(dentry);
8792+ } else if (orig_h.mode == tmp_h.mode) {
8793+ AuDbg("positive --> positive, same type\n");
8794+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8795+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8796+ /*
8797+ * similar to the behaviour of removing and
8798+ * creating.
8799+ */
8800+ au_hide(dentry);
8801+ if (inode)
8802+ err = au_refresh_hinode_self(inode);
8803+ au_dbg_verify_dinode(dentry);
8804+ } else {
8805+ /* fill empty slots */
5afbbe0d
AM
8806+ if (dinfo->di_btop > tmp->di_btop)
8807+ dinfo->di_btop = tmp->di_btop;
8808+ if (dinfo->di_bbot < tmp->di_bbot)
8809+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8810+ dinfo->di_bwh = tmp->di_bwh;
8811+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8812+ bbot = dinfo->di_bbot;
8813+ bindex = tmp->di_btop;
8814+ hd = au_hdentry(tmp, bindex);
8815+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8816+ if (au_h_dptr(dentry, bindex))
8817+ continue;
5afbbe0d 8818+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8819+ if (!h_dentry)
8820+ continue;
5527c038
JR
8821+ AuDebugOn(d_is_negative(h_dentry));
8822+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8823+ AuDebugOn(orig_h.mode
8824+ != (h_inode->i_mode
8825+ & S_IFMT));
8826+ au_set_h_dptr(dentry, bindex,
8827+ dget(h_dentry));
8828+ }
5afbbe0d
AM
8829+ if (inode)
8830+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8831+ au_dbg_verify_dinode(dentry);
8832+ }
8833+ } else {
8834+ AuDbg("positive --> positive, different type\n");
8835+ /* similar to the behaviour of removing and creating */
8836+ au_hide(dentry);
8837+ if (inode)
8838+ err = au_refresh_hinode_self(inode);
8839+ au_dbg_verify_dinode(dentry);
8840+ }
8841+ }
8842+
8843+out:
8844+ return err;
8845+}
8846+
79b8bda9
AM
8847+void au_refresh_dop(struct dentry *dentry, int force_reval)
8848+{
8849+ const struct dentry_operations *dop
8850+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8851+ static const unsigned int mask
8852+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8853+
8854+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8855+
8856+ if (dentry->d_op == dop)
8857+ return;
8858+
8859+ AuDbg("%pd\n", dentry);
8860+ spin_lock(&dentry->d_lock);
8861+ if (dop == &aufs_dop)
8862+ dentry->d_flags |= mask;
8863+ else
8864+ dentry->d_flags &= ~mask;
8865+ dentry->d_op = dop;
8866+ spin_unlock(&dentry->d_lock);
8867+}
8868+
027c5e7a
AM
8869+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8870+{
e2f27e51 8871+ int err, ebrange, nbr;
027c5e7a
AM
8872+ unsigned int sigen;
8873+ struct au_dinfo *dinfo, *tmp;
8874+ struct super_block *sb;
8875+ struct inode *inode;
8876+
8877+ DiMustWriteLock(dentry);
8878+ AuDebugOn(IS_ROOT(dentry));
5527c038 8879+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8880+
8881+ sb = dentry->d_sb;
027c5e7a
AM
8882+ sigen = au_sigen(sb);
8883+ err = au_digen_test(parent, sigen);
8884+ if (unlikely(err))
8885+ goto out;
8886+
e2f27e51 8887+ nbr = au_sbbot(sb) + 1;
027c5e7a 8888+ dinfo = au_di(dentry);
e2f27e51 8889+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8890+ if (unlikely(err))
8891+ goto out;
8892+ ebrange = au_dbrange_test(dentry);
8893+ if (!ebrange)
8894+ ebrange = au_do_refresh_hdentry(dentry, parent);
8895+
38d290e6 8896+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8897+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8898+ if (d_really_is_positive(dentry)) {
8899+ inode = d_inode(dentry);
027c5e7a 8900+ err = au_refresh_hinode_self(inode);
5527c038 8901+ }
027c5e7a
AM
8902+ au_dbg_verify_dinode(dentry);
8903+ if (!err)
8904+ goto out_dgen; /* success */
8905+ goto out;
8906+ }
8907+
8908+ /* temporary dinfo */
8909+ AuDbgDentry(dentry);
8910+ err = -ENOMEM;
8911+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8912+ if (unlikely(!tmp))
8913+ goto out;
8914+ au_di_swap(tmp, dinfo);
8915+ /* returns the number of positive dentries */
8916+ /*
8917+ * if current working dir is removed, it returns an error.
8918+ * but the dentry is legal.
8919+ */
5afbbe0d 8920+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8921+ AuDbgDentry(dentry);
8922+ au_di_swap(tmp, dinfo);
8923+ if (err == -ENOENT)
8924+ err = 0;
8925+ if (err >= 0) {
8926+ /* compare/refresh by dinfo */
8927+ AuDbgDentry(dentry);
8928+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8929+ au_dbg_verify_dinode(dentry);
8930+ AuTraceErr(err);
8931+ }
e2f27e51 8932+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8933+ au_rw_write_unlock(&tmp->di_rwsem);
8934+ au_di_free(tmp);
8935+ if (unlikely(err))
8936+ goto out;
8937+
8938+out_dgen:
8939+ au_update_digen(dentry);
8940+out:
8941+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8942+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8943+ AuDbgDentry(dentry);
8944+ }
8945+ AuTraceErr(err);
8946+ return err;
8947+}
8948+
b4510431
AM
8949+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8950+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8951+{
8952+ int err, valid;
027c5e7a
AM
8953+
8954+ err = 0;
8955+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8956+ goto out;
027c5e7a
AM
8957+
8958+ AuDbg("b%d\n", bindex);
b4510431
AM
8959+ /*
8960+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8961+ * due to whiteout and branch permission.
8962+ */
8963+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8964+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8965+ /* it may return tri-state */
8966+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8967+
8968+ if (unlikely(valid < 0))
8969+ err = valid;
8970+ else if (!valid)
8971+ err = -EINVAL;
8972+
4f0767ce 8973+out:
1facf9fc 8974+ AuTraceErr(err);
8975+ return err;
8976+}
8977+
8978+/* todo: remove this */
8979+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8980+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8981+{
8982+ int err;
8983+ umode_t mode, h_mode;
5afbbe0d 8984+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8985+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8986+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8987+ struct dentry *h_dentry;
8988+ struct qstr *name, *h_name;
8989+
8990+ err = 0;
8991+ plus = 0;
8992+ mode = 0;
1facf9fc 8993+ ibs = -1;
8994+ ibe = -1;
8995+ unhashed = !!d_unhashed(dentry);
8996+ is_root = !!IS_ROOT(dentry);
8997+ name = &dentry->d_name;
38d290e6 8998+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8999+
9000+ /*
7f207e10
AM
9001+ * Theoretically, REVAL test should be unnecessary in case of
9002+ * {FS,I}NOTIFY.
9003+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 9004+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 9005+ * Let's do REVAL test too.
9006+ */
9007+ if (do_udba && inode) {
9008+ mode = (inode->i_mode & S_IFMT);
9009+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
9010+ ibs = au_ibtop(inode);
9011+ ibe = au_ibbot(inode);
1facf9fc 9012+ }
9013+
5afbbe0d
AM
9014+ btop = au_dbtop(dentry);
9015+ btail = btop;
1facf9fc 9016+ if (inode && S_ISDIR(inode->i_mode))
9017+ btail = au_dbtaildir(dentry);
5afbbe0d 9018+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 9019+ h_dentry = au_h_dptr(dentry, bindex);
9020+ if (!h_dentry)
9021+ continue;
9022+
523b37e3
AM
9023+ AuDbg("b%d, %pd\n", bindex, h_dentry);
9024+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 9025+ spin_lock(&h_dentry->d_lock);
1facf9fc 9026+ h_name = &h_dentry->d_name;
9027+ if (unlikely(do_udba
9028+ && !is_root
523b37e3
AM
9029+ && ((!h_nfs
9030+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 9031+ || (!tmpfile && !dirren
38d290e6
JR
9032+ && !au_qstreq(name, h_name))
9033+ ))
523b37e3
AM
9034+ || (h_nfs
9035+ && !(flags & LOOKUP_OPEN)
9036+ && (h_dentry->d_flags
9037+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 9038+ )) {
38d290e6
JR
9039+ int h_unhashed;
9040+
9041+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 9042+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
9043+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
9044+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 9045+ goto err;
9046+ }
027c5e7a 9047+ spin_unlock(&h_dentry->d_lock);
1facf9fc 9048+
b4510431 9049+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 9050+ if (unlikely(err))
9051+ /* do not goto err, to keep the errno */
9052+ break;
9053+
9054+ /* todo: plink too? */
9055+ if (!do_udba)
9056+ continue;
9057+
9058+ /* UDBA tests */
5527c038 9059+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 9060+ goto err;
9061+
5527c038
JR
9062+ h_inode = NULL;
9063+ if (d_is_positive(h_dentry))
9064+ h_inode = d_inode(h_dentry);
1facf9fc 9065+ h_plus = plus;
9066+ h_mode = mode;
9067+ h_cached_inode = h_inode;
9068+ if (h_inode) {
9069+ h_mode = (h_inode->i_mode & S_IFMT);
9070+ h_plus = (h_inode->i_nlink > 0);
9071+ }
9072+ if (inode && ibs <= bindex && bindex <= ibe)
9073+ h_cached_inode = au_h_iptr(inode, bindex);
9074+
523b37e3 9075+ if (!h_nfs) {
38d290e6 9076+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
9077+ goto err;
9078+ } else {
9079+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9080+ && !is_root
9081+ && !IS_ROOT(h_dentry)
9082+ && unhashed != d_unhashed(h_dentry)))
9083+ goto err;
9084+ }
9085+ if (unlikely(mode != h_mode
1facf9fc 9086+ || h_cached_inode != h_inode))
9087+ goto err;
9088+ continue;
9089+
f6b6e03d 9090+err:
1facf9fc 9091+ err = -EINVAL;
9092+ break;
9093+ }
9094+
523b37e3 9095+ AuTraceErr(err);
1facf9fc 9096+ return err;
9097+}
9098+
027c5e7a 9099+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 9100+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9101+{
9102+ int err;
9103+ struct dentry *parent;
1facf9fc 9104+
027c5e7a 9105+ if (!au_digen_test(dentry, sigen))
1facf9fc 9106+ return 0;
9107+
9108+ parent = dget_parent(dentry);
9109+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9110+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 9111+ au_dbg_verify_gen(parent, sigen);
027c5e7a 9112+ err = au_refresh_dentry(dentry, parent);
1facf9fc 9113+ di_read_unlock(parent, AuLock_IR);
9114+ dput(parent);
027c5e7a 9115+ AuTraceErr(err);
1facf9fc 9116+ return err;
9117+}
9118+
9119+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9120+{
9121+ int err;
9122+ struct dentry *d, *parent;
1facf9fc 9123+
027c5e7a 9124+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 9125+ return simple_reval_dpath(dentry, sigen);
9126+
9127+ /* slow loop, keep it simple and stupid */
9128+ /* cf: au_cpup_dirs() */
9129+ err = 0;
9130+ parent = NULL;
027c5e7a 9131+ while (au_digen_test(dentry, sigen)) {
1facf9fc 9132+ d = dentry;
9133+ while (1) {
9134+ dput(parent);
9135+ parent = dget_parent(d);
027c5e7a 9136+ if (!au_digen_test(parent, sigen))
1facf9fc 9137+ break;
9138+ d = parent;
9139+ }
9140+
1facf9fc 9141+ if (d != dentry)
027c5e7a 9142+ di_write_lock_child2(d);
1facf9fc 9143+
9144+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9145+ if (au_digen_test(d, sigen)) {
9146+ /*
9147+ * todo: consolidate with simple_reval_dpath(),
9148+ * do_refresh() and au_reval_for_attr().
9149+ */
1facf9fc 9150+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9151+ err = au_refresh_dentry(d, parent);
1facf9fc 9152+ di_read_unlock(parent, AuLock_IR);
9153+ }
9154+
9155+ if (d != dentry)
9156+ di_write_unlock(d);
9157+ dput(parent);
9158+ if (unlikely(err))
9159+ break;
9160+ }
9161+
9162+ return err;
9163+}
9164+
9165+/*
9166+ * if valid returns 1, otherwise 0.
9167+ */
b4510431 9168+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9169+{
9170+ int valid, err;
9171+ unsigned int sigen;
8b6a4947 9172+ unsigned char do_udba, dirren;
1facf9fc 9173+ struct super_block *sb;
9174+ struct inode *inode;
9175+
027c5e7a 9176+ /* todo: support rcu-walk? */
b4510431 9177+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9178+ return -ECHILD;
9179+
9180+ valid = 0;
9181+ if (unlikely(!au_di(dentry)))
9182+ goto out;
9183+
e49829fe 9184+ valid = 1;
1facf9fc 9185+ sb = dentry->d_sb;
e49829fe
JR
9186+ /*
9187+ * todo: very ugly
9188+ * i_mutex of parent dir may be held,
9189+ * but we should not return 'invalid' due to busy.
9190+ */
9191+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9192+ if (unlikely(err)) {
9193+ valid = err;
027c5e7a 9194+ AuTraceErr(err);
e49829fe
JR
9195+ goto out;
9196+ }
5527c038
JR
9197+ inode = NULL;
9198+ if (d_really_is_positive(dentry))
9199+ inode = d_inode(dentry);
5afbbe0d 9200+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9201+ err = -EINVAL;
9202+ AuTraceErr(err);
9203+ goto out_dgrade;
9204+ }
027c5e7a
AM
9205+ if (unlikely(au_dbrange_test(dentry))) {
9206+ err = -EINVAL;
9207+ AuTraceErr(err);
9208+ goto out_dgrade;
1facf9fc 9209+ }
027c5e7a
AM
9210+
9211+ sigen = au_sigen(sb);
9212+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9213+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9214+ err = au_reval_dpath(dentry, sigen);
9215+ if (unlikely(err)) {
9216+ AuTraceErr(err);
1facf9fc 9217+ goto out_dgrade;
027c5e7a 9218+ }
1facf9fc 9219+ }
9220+ di_downgrade_lock(dentry, AuLock_IR);
9221+
1facf9fc 9222+ err = -EINVAL;
c1595e42 9223+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9224+ && inode
38d290e6 9225+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9226+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9227+ AuTraceErr(err);
027c5e7a 9228+ goto out_inval;
79b8bda9 9229+ }
027c5e7a 9230+
1facf9fc 9231+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9232+ if (do_udba && inode) {
5afbbe0d 9233+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9234+ struct inode *h_inode;
1facf9fc 9235+
5afbbe0d
AM
9236+ if (btop >= 0) {
9237+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9238+ if (h_inode && au_test_higen(inode, h_inode)) {
9239+ AuTraceErr(err);
027c5e7a 9240+ goto out_inval;
79b8bda9 9241+ }
027c5e7a 9242+ }
1facf9fc 9243+ }
9244+
8b6a4947
AM
9245+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9246+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9247+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9248+ err = -EIO;
523b37e3
AM
9249+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9250+ dentry, err);
027c5e7a 9251+ }
e49829fe 9252+ goto out_inval;
1facf9fc 9253+
4f0767ce 9254+out_dgrade:
1facf9fc 9255+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9256+out_inval:
1facf9fc 9257+ aufs_read_unlock(dentry, AuLock_IR);
9258+ AuTraceErr(err);
9259+ valid = !err;
e49829fe 9260+out:
027c5e7a 9261+ if (!valid) {
523b37e3 9262+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9263+ d_drop(dentry);
9264+ }
1facf9fc 9265+ return valid;
9266+}
9267+
9268+static void aufs_d_release(struct dentry *dentry)
9269+{
027c5e7a 9270+ if (au_di(dentry)) {
4a4d8108
AM
9271+ au_di_fin(dentry);
9272+ au_hn_di_reinit(dentry);
1facf9fc 9273+ }
1facf9fc 9274+}
9275+
4a4d8108 9276+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9277+ .d_revalidate = aufs_d_revalidate,
9278+ .d_weak_revalidate = aufs_d_revalidate,
9279+ .d_release = aufs_d_release
1facf9fc 9280+};
79b8bda9
AM
9281+
9282+/* aufs_dop without d_revalidate */
9283+const struct dentry_operations aufs_dop_noreval = {
9284+ .d_release = aufs_d_release
9285+};
7f207e10
AM
9286diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9287--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
9288+++ linux/fs/aufs/dentry.h 2018-07-19 09:46:13.056647808 +0200
9289@@ -0,0 +1,267 @@
9290+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9291+/*
b00004a5 9292+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 9293+ *
9294+ * This program, aufs is free software; you can redistribute it and/or modify
9295+ * it under the terms of the GNU General Public License as published by
9296+ * the Free Software Foundation; either version 2 of the License, or
9297+ * (at your option) any later version.
dece6358
AM
9298+ *
9299+ * This program is distributed in the hope that it will be useful,
9300+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9301+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9302+ * GNU General Public License for more details.
9303+ *
9304+ * You should have received a copy of the GNU General Public License
523b37e3 9305+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9306+ */
9307+
9308+/*
9309+ * lookup and dentry operations
9310+ */
9311+
9312+#ifndef __AUFS_DENTRY_H__
9313+#define __AUFS_DENTRY_H__
9314+
9315+#ifdef __KERNEL__
9316+
dece6358 9317+#include <linux/dcache.h>
8b6a4947 9318+#include "dirren.h"
1facf9fc 9319+#include "rwsem.h"
9320+
1facf9fc 9321+struct au_hdentry {
9322+ struct dentry *hd_dentry;
027c5e7a 9323+ aufs_bindex_t hd_id;
1facf9fc 9324+};
9325+
9326+struct au_dinfo {
9327+ atomic_t di_generation;
9328+
dece6358 9329+ struct au_rwsem di_rwsem;
5afbbe0d 9330+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9331+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9332+ struct au_hdentry *di_hdentry;
4a4d8108 9333+} ____cacheline_aligned_in_smp;
1facf9fc 9334+
9335+/* ---------------------------------------------------------------------- */
9336+
5afbbe0d
AM
9337+/* flags for au_lkup_dentry() */
9338+#define AuLkup_ALLOW_NEG 1
9339+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9340+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9341+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9342+#define au_fset_lkup(flags, name) \
9343+ do { (flags) |= AuLkup_##name; } while (0)
9344+#define au_fclr_lkup(flags, name) \
9345+ do { (flags) &= ~AuLkup_##name; } while (0)
9346+
8b6a4947
AM
9347+#ifndef CONFIG_AUFS_DIRREN
9348+#undef AuLkup_DIRREN
9349+#define AuLkup_DIRREN 0
9350+#endif
9351+
9352+struct au_do_lookup_args {
9353+ unsigned int flags;
9354+ mode_t type;
9355+ struct qstr whname, *name;
9356+ struct au_dr_lookup dirren;
9357+};
9358+
5afbbe0d
AM
9359+/* ---------------------------------------------------------------------- */
9360+
1facf9fc 9361+/* dentry.c */
79b8bda9 9362+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9363+struct au_branch;
076b876e 9364+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 9365+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9366+ struct dentry *h_parent, struct au_branch *br);
9367+
5afbbe0d
AM
9368+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9369+ unsigned int flags);
86dc4139 9370+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9371+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9372+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9373+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9374+
9375+/* dinfo.c */
4a4d8108 9376+void au_di_init_once(void *_di);
027c5e7a
AM
9377+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9378+void au_di_free(struct au_dinfo *dinfo);
9379+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9380+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9381+int au_di_init(struct dentry *dentry);
9382+void au_di_fin(struct dentry *dentry);
e2f27e51 9383+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9384+
9385+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9386+void di_read_unlock(struct dentry *d, int flags);
9387+void di_downgrade_lock(struct dentry *d, int flags);
9388+void di_write_lock(struct dentry *d, unsigned int lsc);
9389+void di_write_unlock(struct dentry *d);
9390+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9391+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9392+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9393+
9394+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9395+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9396+aufs_bindex_t au_dbtail(struct dentry *dentry);
9397+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9398+
9399+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9400+ struct dentry *h_dentry);
027c5e7a
AM
9401+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9402+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9403+void au_update_digen(struct dentry *dentry);
9404+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9405+void au_update_dbtop(struct dentry *dentry);
9406+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9407+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9408+
9409+/* ---------------------------------------------------------------------- */
9410+
9411+static inline struct au_dinfo *au_di(struct dentry *dentry)
9412+{
9413+ return dentry->d_fsdata;
9414+}
9415+
9416+/* ---------------------------------------------------------------------- */
9417+
9418+/* lock subclass for dinfo */
9419+enum {
9420+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9421+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9422+ AuLsc_DI_CHILD3, /* copyup dirs */
9423+ AuLsc_DI_PARENT,
9424+ AuLsc_DI_PARENT2,
027c5e7a
AM
9425+ AuLsc_DI_PARENT3,
9426+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9427+};
9428+
9429+/*
9430+ * di_read_lock_child, di_write_lock_child,
9431+ * di_read_lock_child2, di_write_lock_child2,
9432+ * di_read_lock_child3, di_write_lock_child3,
9433+ * di_read_lock_parent, di_write_lock_parent,
9434+ * di_read_lock_parent2, di_write_lock_parent2,
9435+ * di_read_lock_parent3, di_write_lock_parent3,
9436+ */
9437+#define AuReadLockFunc(name, lsc) \
9438+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9439+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9440+
9441+#define AuWriteLockFunc(name, lsc) \
9442+static inline void di_write_lock_##name(struct dentry *d) \
9443+{ di_write_lock(d, AuLsc_DI_##lsc); }
9444+
9445+#define AuRWLockFuncs(name, lsc) \
9446+ AuReadLockFunc(name, lsc) \
9447+ AuWriteLockFunc(name, lsc)
9448+
9449+AuRWLockFuncs(child, CHILD);
9450+AuRWLockFuncs(child2, CHILD2);
9451+AuRWLockFuncs(child3, CHILD3);
9452+AuRWLockFuncs(parent, PARENT);
9453+AuRWLockFuncs(parent2, PARENT2);
9454+AuRWLockFuncs(parent3, PARENT3);
9455+
9456+#undef AuReadLockFunc
9457+#undef AuWriteLockFunc
9458+#undef AuRWLockFuncs
9459+
9460+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9461+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9462+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9463+
9464+/* ---------------------------------------------------------------------- */
9465+
9466+/* todo: memory barrier? */
9467+static inline unsigned int au_digen(struct dentry *d)
9468+{
9469+ return atomic_read(&au_di(d)->di_generation);
9470+}
9471+
9472+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9473+{
9474+ hdentry->hd_dentry = NULL;
9475+}
9476+
5afbbe0d
AM
9477+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9478+ aufs_bindex_t bindex)
9479+{
9480+ return di->di_hdentry + bindex;
9481+}
9482+
1facf9fc 9483+static inline void au_hdput(struct au_hdentry *hd)
9484+{
4a4d8108
AM
9485+ if (hd)
9486+ dput(hd->hd_dentry);
1facf9fc 9487+}
9488+
5afbbe0d 9489+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9490+{
1308ab2a 9491+ DiMustAnyLock(dentry);
5afbbe0d 9492+ return au_di(dentry)->di_btop;
1facf9fc 9493+}
9494+
5afbbe0d 9495+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9496+{
1308ab2a 9497+ DiMustAnyLock(dentry);
5afbbe0d 9498+ return au_di(dentry)->di_bbot;
1facf9fc 9499+}
9500+
9501+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9502+{
1308ab2a 9503+ DiMustAnyLock(dentry);
1facf9fc 9504+ return au_di(dentry)->di_bwh;
9505+}
9506+
9507+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9508+{
1308ab2a 9509+ DiMustAnyLock(dentry);
1facf9fc 9510+ return au_di(dentry)->di_bdiropq;
9511+}
9512+
9513+/* todo: hard/soft set? */
5afbbe0d 9514+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9515+{
1308ab2a 9516+ DiMustWriteLock(dentry);
5afbbe0d 9517+ au_di(dentry)->di_btop = bindex;
1facf9fc 9518+}
9519+
5afbbe0d 9520+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9521+{
1308ab2a 9522+ DiMustWriteLock(dentry);
5afbbe0d 9523+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9524+}
9525+
9526+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9527+{
1308ab2a 9528+ DiMustWriteLock(dentry);
5afbbe0d 9529+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9530+ au_di(dentry)->di_bwh = bindex;
9531+}
9532+
9533+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9534+{
1308ab2a 9535+ DiMustWriteLock(dentry);
1facf9fc 9536+ au_di(dentry)->di_bdiropq = bindex;
9537+}
9538+
9539+/* ---------------------------------------------------------------------- */
9540+
4a4d8108 9541+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9542+static inline void au_digen_dec(struct dentry *d)
9543+{
e49829fe 9544+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9545+}
9546+
4a4d8108 9547+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9548+{
9549+ dentry->d_fsdata = NULL;
9550+}
9551+#else
4a4d8108
AM
9552+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9553+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9554+
9555+#endif /* __KERNEL__ */
9556+#endif /* __AUFS_DENTRY_H__ */
7f207e10
AM
9557diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9558--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
9559+++ linux/fs/aufs/dinfo.c 2018-07-19 09:46:13.056647808 +0200
9560@@ -0,0 +1,554 @@
9561+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9562+/*
b00004a5 9563+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 9564+ *
9565+ * This program, aufs is free software; you can redistribute it and/or modify
9566+ * it under the terms of the GNU General Public License as published by
9567+ * the Free Software Foundation; either version 2 of the License, or
9568+ * (at your option) any later version.
dece6358
AM
9569+ *
9570+ * This program is distributed in the hope that it will be useful,
9571+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9572+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9573+ * GNU General Public License for more details.
9574+ *
9575+ * You should have received a copy of the GNU General Public License
523b37e3 9576+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9577+ */
9578+
9579+/*
9580+ * dentry private data
9581+ */
9582+
9583+#include "aufs.h"
9584+
e49829fe 9585+void au_di_init_once(void *_dinfo)
4a4d8108 9586+{
e49829fe 9587+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9588+
e49829fe 9589+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9590+}
9591+
027c5e7a 9592+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9593+{
9594+ struct au_dinfo *dinfo;
027c5e7a 9595+ int nbr, i;
1facf9fc 9596+
9597+ dinfo = au_cache_alloc_dinfo();
9598+ if (unlikely(!dinfo))
9599+ goto out;
9600+
5afbbe0d 9601+ nbr = au_sbbot(sb) + 1;
1facf9fc 9602+ if (nbr <= 0)
9603+ nbr = 1;
9604+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9605+ if (dinfo->di_hdentry) {
9606+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9607+ dinfo->di_btop = -1;
9608+ dinfo->di_bbot = -1;
027c5e7a
AM
9609+ dinfo->di_bwh = -1;
9610+ dinfo->di_bdiropq = -1;
38d290e6 9611+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9612+ for (i = 0; i < nbr; i++)
9613+ dinfo->di_hdentry[i].hd_id = -1;
9614+ goto out;
9615+ }
1facf9fc 9616+
1c60b727 9617+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9618+ dinfo = NULL;
9619+
4f0767ce 9620+out:
027c5e7a 9621+ return dinfo;
1facf9fc 9622+}
9623+
027c5e7a 9624+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9625+{
4a4d8108 9626+ struct au_hdentry *p;
5afbbe0d 9627+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9628+
9629+ /* dentry may not be revalidated */
5afbbe0d 9630+ bindex = dinfo->di_btop;
4a4d8108 9631+ if (bindex >= 0) {
5afbbe0d
AM
9632+ bbot = dinfo->di_bbot;
9633+ p = au_hdentry(dinfo, bindex);
9634+ while (bindex++ <= bbot)
4a4d8108
AM
9635+ au_hdput(p++);
9636+ }
1c60b727
AM
9637+ kfree(dinfo->di_hdentry);
9638+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9639+}
9640+
9641+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9642+{
9643+ struct au_hdentry *p;
9644+ aufs_bindex_t bi;
9645+
9646+ AuRwMustWriteLock(&a->di_rwsem);
9647+ AuRwMustWriteLock(&b->di_rwsem);
9648+
9649+#define DiSwap(v, name) \
9650+ do { \
9651+ v = a->di_##name; \
9652+ a->di_##name = b->di_##name; \
9653+ b->di_##name = v; \
9654+ } while (0)
9655+
9656+ DiSwap(p, hdentry);
5afbbe0d
AM
9657+ DiSwap(bi, btop);
9658+ DiSwap(bi, bbot);
027c5e7a
AM
9659+ DiSwap(bi, bwh);
9660+ DiSwap(bi, bdiropq);
9661+ /* smp_mb(); */
9662+
9663+#undef DiSwap
9664+}
9665+
9666+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9667+{
9668+ AuRwMustWriteLock(&dst->di_rwsem);
9669+ AuRwMustWriteLock(&src->di_rwsem);
9670+
5afbbe0d
AM
9671+ dst->di_btop = src->di_btop;
9672+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9673+ dst->di_bwh = src->di_bwh;
9674+ dst->di_bdiropq = src->di_bdiropq;
9675+ /* smp_mb(); */
9676+}
9677+
9678+int au_di_init(struct dentry *dentry)
9679+{
9680+ int err;
9681+ struct super_block *sb;
9682+ struct au_dinfo *dinfo;
9683+
9684+ err = 0;
9685+ sb = dentry->d_sb;
9686+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9687+ if (dinfo) {
9688+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9689+ /* smp_mb(); */ /* atomic_set */
9690+ dentry->d_fsdata = dinfo;
9691+ } else
9692+ err = -ENOMEM;
9693+
9694+ return err;
9695+}
9696+
9697+void au_di_fin(struct dentry *dentry)
9698+{
9699+ struct au_dinfo *dinfo;
9700+
9701+ dinfo = au_di(dentry);
9702+ AuRwDestroy(&dinfo->di_rwsem);
9703+ au_di_free(dinfo);
4a4d8108
AM
9704+}
9705+
e2f27e51 9706+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9707+{
9708+ int err, sz;
9709+ struct au_hdentry *hdp;
9710+
1308ab2a 9711+ AuRwMustWriteLock(&dinfo->di_rwsem);
9712+
1facf9fc 9713+ err = -ENOMEM;
5afbbe0d 9714+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9715+ if (!sz)
9716+ sz = sizeof(*hdp);
e2f27e51
AM
9717+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9718+ may_shrink);
1facf9fc 9719+ if (hdp) {
9720+ dinfo->di_hdentry = hdp;
9721+ err = 0;
9722+ }
9723+
9724+ return err;
9725+}
9726+
9727+/* ---------------------------------------------------------------------- */
9728+
9729+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9730+{
9731+ switch (lsc) {
9732+ case AuLsc_DI_CHILD:
9733+ ii_write_lock_child(inode);
9734+ break;
9735+ case AuLsc_DI_CHILD2:
9736+ ii_write_lock_child2(inode);
9737+ break;
9738+ case AuLsc_DI_CHILD3:
9739+ ii_write_lock_child3(inode);
9740+ break;
9741+ case AuLsc_DI_PARENT:
9742+ ii_write_lock_parent(inode);
9743+ break;
9744+ case AuLsc_DI_PARENT2:
9745+ ii_write_lock_parent2(inode);
9746+ break;
9747+ case AuLsc_DI_PARENT3:
9748+ ii_write_lock_parent3(inode);
9749+ break;
9750+ default:
9751+ BUG();
9752+ }
9753+}
9754+
9755+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9756+{
9757+ switch (lsc) {
9758+ case AuLsc_DI_CHILD:
9759+ ii_read_lock_child(inode);
9760+ break;
9761+ case AuLsc_DI_CHILD2:
9762+ ii_read_lock_child2(inode);
9763+ break;
9764+ case AuLsc_DI_CHILD3:
9765+ ii_read_lock_child3(inode);
9766+ break;
9767+ case AuLsc_DI_PARENT:
9768+ ii_read_lock_parent(inode);
9769+ break;
9770+ case AuLsc_DI_PARENT2:
9771+ ii_read_lock_parent2(inode);
9772+ break;
9773+ case AuLsc_DI_PARENT3:
9774+ ii_read_lock_parent3(inode);
9775+ break;
9776+ default:
9777+ BUG();
9778+ }
9779+}
9780+
9781+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9782+{
5527c038
JR
9783+ struct inode *inode;
9784+
dece6358 9785+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9786+ if (d_really_is_positive(d)) {
9787+ inode = d_inode(d);
1facf9fc 9788+ if (au_ftest_lock(flags, IW))
5527c038 9789+ do_ii_write_lock(inode, lsc);
1facf9fc 9790+ else if (au_ftest_lock(flags, IR))
5527c038 9791+ do_ii_read_lock(inode, lsc);
1facf9fc 9792+ }
9793+}
9794+
9795+void di_read_unlock(struct dentry *d, int flags)
9796+{
5527c038
JR
9797+ struct inode *inode;
9798+
9799+ if (d_really_is_positive(d)) {
9800+ inode = d_inode(d);
027c5e7a
AM
9801+ if (au_ftest_lock(flags, IW)) {
9802+ au_dbg_verify_dinode(d);
5527c038 9803+ ii_write_unlock(inode);
027c5e7a
AM
9804+ } else if (au_ftest_lock(flags, IR)) {
9805+ au_dbg_verify_dinode(d);
5527c038 9806+ ii_read_unlock(inode);
027c5e7a 9807+ }
1facf9fc 9808+ }
dece6358 9809+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9810+}
9811+
9812+void di_downgrade_lock(struct dentry *d, int flags)
9813+{
5527c038
JR
9814+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9815+ ii_downgrade_lock(d_inode(d));
dece6358 9816+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9817+}
9818+
9819+void di_write_lock(struct dentry *d, unsigned int lsc)
9820+{
dece6358 9821+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9822+ if (d_really_is_positive(d))
9823+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9824+}
9825+
9826+void di_write_unlock(struct dentry *d)
9827+{
027c5e7a 9828+ au_dbg_verify_dinode(d);
5527c038
JR
9829+ if (d_really_is_positive(d))
9830+ ii_write_unlock(d_inode(d));
dece6358 9831+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9832+}
9833+
9834+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9835+{
9836+ AuDebugOn(d1 == d2
5527c038 9837+ || d_inode(d1) == d_inode(d2)
1facf9fc 9838+ || d1->d_sb != d2->d_sb);
9839+
521ced18
JR
9840+ if ((isdir && au_test_subdir(d1, d2))
9841+ || d1 < d2) {
1facf9fc 9842+ di_write_lock_child(d1);
9843+ di_write_lock_child2(d2);
9844+ } else {
1facf9fc 9845+ di_write_lock_child(d2);
9846+ di_write_lock_child2(d1);
9847+ }
9848+}
9849+
9850+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9851+{
9852+ AuDebugOn(d1 == d2
5527c038 9853+ || d_inode(d1) == d_inode(d2)
1facf9fc 9854+ || d1->d_sb != d2->d_sb);
9855+
521ced18
JR
9856+ if ((isdir && au_test_subdir(d1, d2))
9857+ || d1 < d2) {
1facf9fc 9858+ di_write_lock_parent(d1);
9859+ di_write_lock_parent2(d2);
9860+ } else {
1facf9fc 9861+ di_write_lock_parent(d2);
9862+ di_write_lock_parent2(d1);
9863+ }
9864+}
9865+
9866+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9867+{
9868+ di_write_unlock(d1);
5527c038 9869+ if (d_inode(d1) == d_inode(d2))
dece6358 9870+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9871+ else
9872+ di_write_unlock(d2);
9873+}
9874+
9875+/* ---------------------------------------------------------------------- */
9876+
9877+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9878+{
9879+ struct dentry *d;
9880+
1308ab2a 9881+ DiMustAnyLock(dentry);
9882+
5afbbe0d 9883+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9884+ return NULL;
9885+ AuDebugOn(bindex < 0);
5afbbe0d 9886+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9887+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9888+ return d;
9889+}
9890+
2cbb1c4b
JR
9891+/*
9892+ * extended version of au_h_dptr().
38d290e6
JR
9893+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9894+ * error.
2cbb1c4b
JR
9895+ */
9896+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9897+{
9898+ struct dentry *h_dentry;
9899+ struct inode *inode, *h_inode;
9900+
5527c038 9901+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9902+
9903+ h_dentry = NULL;
5afbbe0d
AM
9904+ if (au_dbtop(dentry) <= bindex
9905+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9906+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9907+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9908+ dget(h_dentry);
9909+ goto out; /* success */
9910+ }
9911+
5527c038 9912+ inode = d_inode(dentry);
5afbbe0d
AM
9913+ AuDebugOn(bindex < au_ibtop(inode));
9914+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9915+ h_inode = au_h_iptr(inode, bindex);
9916+ h_dentry = d_find_alias(h_inode);
9917+ if (h_dentry) {
9918+ if (!IS_ERR(h_dentry)) {
38d290e6 9919+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9920+ goto out; /* success */
9921+ dput(h_dentry);
9922+ } else
9923+ goto out;
9924+ }
9925+
9926+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9927+ h_dentry = au_plink_lkup(inode, bindex);
9928+ AuDebugOn(!h_dentry);
9929+ if (!IS_ERR(h_dentry)) {
9930+ if (!au_d_hashed_positive(h_dentry))
9931+ goto out; /* success */
9932+ dput(h_dentry);
9933+ h_dentry = NULL;
9934+ }
9935+ }
9936+
9937+out:
9938+ AuDbgDentry(h_dentry);
9939+ return h_dentry;
9940+}
9941+
1facf9fc 9942+aufs_bindex_t au_dbtail(struct dentry *dentry)
9943+{
5afbbe0d 9944+ aufs_bindex_t bbot, bwh;
1facf9fc 9945+
5afbbe0d
AM
9946+ bbot = au_dbbot(dentry);
9947+ if (0 <= bbot) {
1facf9fc 9948+ bwh = au_dbwh(dentry);
9949+ if (!bwh)
9950+ return bwh;
5afbbe0d 9951+ if (0 < bwh && bwh < bbot)
1facf9fc 9952+ return bwh - 1;
9953+ }
5afbbe0d 9954+ return bbot;
1facf9fc 9955+}
9956+
9957+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9958+{
5afbbe0d 9959+ aufs_bindex_t bbot, bopq;
1facf9fc 9960+
5afbbe0d
AM
9961+ bbot = au_dbtail(dentry);
9962+ if (0 <= bbot) {
1facf9fc 9963+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9964+ if (0 <= bopq && bopq < bbot)
9965+ bbot = bopq;
1facf9fc 9966+ }
5afbbe0d 9967+ return bbot;
1facf9fc 9968+}
9969+
9970+/* ---------------------------------------------------------------------- */
9971+
9972+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9973+ struct dentry *h_dentry)
9974+{
5afbbe0d
AM
9975+ struct au_dinfo *dinfo;
9976+ struct au_hdentry *hd;
027c5e7a 9977+ struct au_branch *br;
1facf9fc 9978+
1308ab2a 9979+ DiMustWriteLock(dentry);
9980+
5afbbe0d
AM
9981+ dinfo = au_di(dentry);
9982+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9983+ au_hdput(hd);
1facf9fc 9984+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9985+ if (h_dentry) {
9986+ br = au_sbr(dentry->d_sb, bindex);
9987+ hd->hd_id = br->br_id;
9988+ }
9989+}
9990+
9991+int au_dbrange_test(struct dentry *dentry)
9992+{
9993+ int err;
5afbbe0d 9994+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9995+
9996+ err = 0;
5afbbe0d
AM
9997+ btop = au_dbtop(dentry);
9998+ bbot = au_dbbot(dentry);
9999+ if (btop >= 0)
10000+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
10001+ else {
10002+ err = -EIO;
5afbbe0d 10003+ AuDebugOn(bbot >= 0);
027c5e7a
AM
10004+ }
10005+
10006+ return err;
10007+}
10008+
10009+int au_digen_test(struct dentry *dentry, unsigned int sigen)
10010+{
10011+ int err;
10012+
10013+ err = 0;
10014+ if (unlikely(au_digen(dentry) != sigen
5527c038 10015+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
10016+ err = -EIO;
10017+
10018+ return err;
1facf9fc 10019+}
10020+
10021+void au_update_digen(struct dentry *dentry)
10022+{
10023+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
10024+ /* smp_mb(); */ /* atomic_set */
10025+}
10026+
10027+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
10028+{
10029+ struct au_dinfo *dinfo;
10030+ struct dentry *h_d;
4a4d8108 10031+ struct au_hdentry *hdp;
5afbbe0d 10032+ aufs_bindex_t bindex, bbot;
1facf9fc 10033+
1308ab2a 10034+ DiMustWriteLock(dentry);
10035+
1facf9fc 10036+ dinfo = au_di(dentry);
5afbbe0d 10037+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 10038+ return;
10039+
10040+ if (do_put_zero) {
5afbbe0d
AM
10041+ bbot = dinfo->di_bbot;
10042+ bindex = dinfo->di_btop;
10043+ hdp = au_hdentry(dinfo, bindex);
10044+ for (; bindex <= bbot; bindex++, hdp++) {
10045+ h_d = hdp->hd_dentry;
5527c038 10046+ if (h_d && d_is_negative(h_d))
1facf9fc 10047+ au_set_h_dptr(dentry, bindex, NULL);
10048+ }
10049+ }
10050+
5afbbe0d
AM
10051+ dinfo->di_btop = 0;
10052+ hdp = au_hdentry(dinfo, dinfo->di_btop);
10053+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
10054+ if (hdp->hd_dentry)
1facf9fc 10055+ break;
5afbbe0d
AM
10056+ if (dinfo->di_btop > dinfo->di_bbot) {
10057+ dinfo->di_btop = -1;
10058+ dinfo->di_bbot = -1;
1facf9fc 10059+ return;
10060+ }
10061+
5afbbe0d
AM
10062+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
10063+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
10064+ if (hdp->hd_dentry)
1facf9fc 10065+ break;
5afbbe0d 10066+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 10067+}
10068+
5afbbe0d 10069+void au_update_dbtop(struct dentry *dentry)
1facf9fc 10070+{
5afbbe0d 10071+ aufs_bindex_t bindex, bbot;
1facf9fc 10072+ struct dentry *h_dentry;
10073+
5afbbe0d
AM
10074+ bbot = au_dbbot(dentry);
10075+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 10076+ h_dentry = au_h_dptr(dentry, bindex);
10077+ if (!h_dentry)
10078+ continue;
5527c038 10079+ if (d_is_positive(h_dentry)) {
5afbbe0d 10080+ au_set_dbtop(dentry, bindex);
1facf9fc 10081+ return;
10082+ }
10083+ au_set_h_dptr(dentry, bindex, NULL);
10084+ }
10085+}
10086+
5afbbe0d 10087+void au_update_dbbot(struct dentry *dentry)
1facf9fc 10088+{
5afbbe0d 10089+ aufs_bindex_t bindex, btop;
1facf9fc 10090+ struct dentry *h_dentry;
10091+
5afbbe0d
AM
10092+ btop = au_dbtop(dentry);
10093+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 10094+ h_dentry = au_h_dptr(dentry, bindex);
10095+ if (!h_dentry)
10096+ continue;
5527c038 10097+ if (d_is_positive(h_dentry)) {
5afbbe0d 10098+ au_set_dbbot(dentry, bindex);
1facf9fc 10099+ return;
10100+ }
10101+ au_set_h_dptr(dentry, bindex, NULL);
10102+ }
10103+}
10104+
10105+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10106+{
5afbbe0d 10107+ aufs_bindex_t bindex, bbot;
1facf9fc 10108+
5afbbe0d
AM
10109+ bbot = au_dbbot(dentry);
10110+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 10111+ if (au_h_dptr(dentry, bindex) == h_dentry)
10112+ return bindex;
10113+ return -1;
10114+}
7f207e10
AM
10115diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10116--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
10117+++ linux/fs/aufs/dir.c 2018-07-19 09:46:13.056647808 +0200
10118@@ -0,0 +1,760 @@
10119+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10120+/*
b00004a5 10121+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 10122+ *
10123+ * This program, aufs is free software; you can redistribute it and/or modify
10124+ * it under the terms of the GNU General Public License as published by
10125+ * the Free Software Foundation; either version 2 of the License, or
10126+ * (at your option) any later version.
dece6358
AM
10127+ *
10128+ * This program is distributed in the hope that it will be useful,
10129+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10130+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10131+ * GNU General Public License for more details.
10132+ *
10133+ * You should have received a copy of the GNU General Public License
523b37e3 10134+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10135+ */
10136+
10137+/*
10138+ * directory operations
10139+ */
10140+
10141+#include <linux/fs_stack.h>
10142+#include "aufs.h"
10143+
10144+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10145+{
9dbd164d
AM
10146+ unsigned int nlink;
10147+
1facf9fc 10148+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10149+
9dbd164d
AM
10150+ nlink = dir->i_nlink;
10151+ nlink += h_dir->i_nlink - 2;
1facf9fc 10152+ if (h_dir->i_nlink < 2)
9dbd164d 10153+ nlink += 2;
f6b6e03d 10154+ smp_mb(); /* for i_nlink */
7eafdf33 10155+ /* 0 can happen in revaliding */
92d182d2 10156+ set_nlink(dir, nlink);
1facf9fc 10157+}
10158+
10159+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10160+{
9dbd164d
AM
10161+ unsigned int nlink;
10162+
1facf9fc 10163+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10164+
9dbd164d
AM
10165+ nlink = dir->i_nlink;
10166+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10167+ if (h_dir->i_nlink < 2)
9dbd164d 10168+ nlink -= 2;
f6b6e03d 10169+ smp_mb(); /* for i_nlink */
92d182d2 10170+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10171+ set_nlink(dir, nlink);
1facf9fc 10172+}
10173+
1308ab2a 10174+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10175+{
10176+ loff_t sz;
5afbbe0d 10177+ aufs_bindex_t bindex, bbot;
1308ab2a 10178+ struct file *h_file;
10179+ struct dentry *h_dentry;
10180+
10181+ sz = 0;
10182+ if (file) {
2000de60 10183+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10184+
5afbbe0d
AM
10185+ bbot = au_fbbot_dir(file);
10186+ for (bindex = au_fbtop(file);
10187+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10188+ bindex++) {
4a4d8108 10189+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10190+ if (h_file && file_inode(h_file))
10191+ sz += vfsub_f_size_read(h_file);
1308ab2a 10192+ }
10193+ } else {
10194+ AuDebugOn(!dentry);
2000de60 10195+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10196+
5afbbe0d
AM
10197+ bbot = au_dbtaildir(dentry);
10198+ for (bindex = au_dbtop(dentry);
10199+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10200+ bindex++) {
10201+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10202+ if (h_dentry && d_is_positive(h_dentry))
10203+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10204+ }
10205+ }
10206+ if (sz < KMALLOC_MAX_SIZE)
10207+ sz = roundup_pow_of_two(sz);
10208+ if (sz > KMALLOC_MAX_SIZE)
10209+ sz = KMALLOC_MAX_SIZE;
10210+ else if (sz < NAME_MAX) {
10211+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10212+ sz = AUFS_RDBLK_DEF;
10213+ }
10214+ return sz;
10215+}
10216+
b912730e
AM
10217+struct au_dir_ts_arg {
10218+ struct dentry *dentry;
10219+ aufs_bindex_t brid;
10220+};
10221+
10222+static void au_do_dir_ts(void *arg)
10223+{
10224+ struct au_dir_ts_arg *a = arg;
10225+ struct au_dtime dt;
10226+ struct path h_path;
10227+ struct inode *dir, *h_dir;
10228+ struct super_block *sb;
10229+ struct au_branch *br;
10230+ struct au_hinode *hdir;
10231+ int err;
5afbbe0d 10232+ aufs_bindex_t btop, bindex;
b912730e
AM
10233+
10234+ sb = a->dentry->d_sb;
5527c038 10235+ if (d_really_is_negative(a->dentry))
b912730e 10236+ goto out;
5527c038 10237+ /* no dir->i_mutex lock */
b95c5147
AM
10238+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10239+
5527c038 10240+ dir = d_inode(a->dentry);
5afbbe0d 10241+ btop = au_ibtop(dir);
b912730e 10242+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10243+ if (bindex < btop)
b912730e
AM
10244+ goto out_unlock;
10245+
10246+ br = au_sbr(sb, bindex);
10247+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10248+ if (!h_path.dentry)
10249+ goto out_unlock;
10250+ h_path.mnt = au_br_mnt(br);
10251+ au_dtime_store(&dt, a->dentry, &h_path);
10252+
5afbbe0d 10253+ br = au_sbr(sb, btop);
b912730e
AM
10254+ if (!au_br_writable(br->br_perm))
10255+ goto out_unlock;
5afbbe0d 10256+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10257+ h_path.mnt = au_br_mnt(br);
10258+ err = vfsub_mnt_want_write(h_path.mnt);
10259+ if (err)
10260+ goto out_unlock;
5afbbe0d
AM
10261+ hdir = au_hi(dir, btop);
10262+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10263+ h_dir = au_h_iptr(dir, btop);
b912730e
AM
10264+ if (h_dir->i_nlink
10265+ && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10266+ dt.dt_h_path = h_path;
10267+ au_dtime_revert(&dt);
10268+ }
5afbbe0d 10269+ au_hn_inode_unlock(hdir);
b912730e
AM
10270+ vfsub_mnt_drop_write(h_path.mnt);
10271+ au_cpup_attr_timesizes(dir);
10272+
10273+out_unlock:
10274+ aufs_read_unlock(a->dentry, AuLock_DW);
10275+out:
10276+ dput(a->dentry);
10277+ au_nwt_done(&au_sbi(sb)->si_nowait);
1c60b727 10278+ kfree(arg);
b912730e
AM
10279+}
10280+
10281+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10282+{
10283+ int perm, wkq_err;
5afbbe0d 10284+ aufs_bindex_t btop;
b912730e
AM
10285+ struct au_dir_ts_arg *arg;
10286+ struct dentry *dentry;
10287+ struct super_block *sb;
10288+
10289+ IMustLock(dir);
10290+
10291+ dentry = d_find_any_alias(dir);
10292+ AuDebugOn(!dentry);
10293+ sb = dentry->d_sb;
5afbbe0d
AM
10294+ btop = au_ibtop(dir);
10295+ if (btop == bindex) {
b912730e
AM
10296+ au_cpup_attr_timesizes(dir);
10297+ goto out;
10298+ }
10299+
5afbbe0d 10300+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10301+ if (!au_br_writable(perm))
10302+ goto out;
10303+
10304+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10305+ if (!arg)
10306+ goto out;
10307+
10308+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10309+ arg->brid = au_sbr_id(sb, bindex);
10310+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10311+ if (unlikely(wkq_err)) {
10312+ pr_err("wkq %d\n", wkq_err);
10313+ dput(dentry);
1c60b727 10314+ kfree(arg);
b912730e
AM
10315+ }
10316+
10317+out:
10318+ dput(dentry);
10319+}
10320+
1facf9fc 10321+/* ---------------------------------------------------------------------- */
10322+
10323+static int reopen_dir(struct file *file)
10324+{
10325+ int err;
10326+ unsigned int flags;
5afbbe0d 10327+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10328+ struct dentry *dentry, *h_dentry;
10329+ struct file *h_file;
10330+
10331+ /* open all lower dirs */
2000de60 10332+ dentry = file->f_path.dentry;
5afbbe0d
AM
10333+ btop = au_dbtop(dentry);
10334+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10335+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10336+ au_set_fbtop(file, btop);
1facf9fc 10337+
10338+ btail = au_dbtaildir(dentry);
5afbbe0d 10339+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10340+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10341+ au_set_fbbot_dir(file, btail);
1facf9fc 10342+
4a4d8108 10343+ flags = vfsub_file_flags(file);
5afbbe0d 10344+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10345+ h_dentry = au_h_dptr(dentry, bindex);
10346+ if (!h_dentry)
10347+ continue;
4a4d8108 10348+ h_file = au_hf_dir(file, bindex);
1facf9fc 10349+ if (h_file)
10350+ continue;
10351+
392086de 10352+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10353+ err = PTR_ERR(h_file);
10354+ if (IS_ERR(h_file))
10355+ goto out; /* close all? */
10356+ au_set_h_fptr(file, bindex, h_file);
10357+ }
10358+ au_update_figen(file);
10359+ /* todo: necessary? */
10360+ /* file->f_ra = h_file->f_ra; */
10361+ err = 0;
10362+
4f0767ce 10363+out:
1facf9fc 10364+ return err;
10365+}
10366+
b912730e 10367+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10368+{
10369+ int err;
10370+ aufs_bindex_t bindex, btail;
10371+ struct dentry *dentry, *h_dentry;
8cdd5066 10372+ struct vfsmount *mnt;
1facf9fc 10373+
1308ab2a 10374+ FiMustWriteLock(file);
b912730e 10375+ AuDebugOn(h_file);
1308ab2a 10376+
523b37e3 10377+ err = 0;
8cdd5066 10378+ mnt = file->f_path.mnt;
2000de60 10379+ dentry = file->f_path.dentry;
be118d29 10380+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10381+ bindex = au_dbtop(dentry);
10382+ au_set_fbtop(file, bindex);
1facf9fc 10383+ btail = au_dbtaildir(dentry);
5afbbe0d 10384+ au_set_fbbot_dir(file, btail);
1facf9fc 10385+ for (; !err && bindex <= btail; bindex++) {
10386+ h_dentry = au_h_dptr(dentry, bindex);
10387+ if (!h_dentry)
10388+ continue;
10389+
8cdd5066
JR
10390+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10391+ if (unlikely(err))
10392+ break;
392086de 10393+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10394+ if (IS_ERR(h_file)) {
10395+ err = PTR_ERR(h_file);
10396+ break;
10397+ }
10398+ au_set_h_fptr(file, bindex, h_file);
10399+ }
10400+ au_update_figen(file);
10401+ /* todo: necessary? */
10402+ /* file->f_ra = h_file->f_ra; */
10403+ if (!err)
10404+ return 0; /* success */
10405+
10406+ /* close all */
5afbbe0d 10407+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10408+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10409+ au_set_fbtop(file, -1);
10410+ au_set_fbbot_dir(file, -1);
4a4d8108 10411+
1facf9fc 10412+ return err;
10413+}
10414+
10415+static int aufs_open_dir(struct inode *inode __maybe_unused,
10416+ struct file *file)
10417+{
4a4d8108
AM
10418+ int err;
10419+ struct super_block *sb;
10420+ struct au_fidir *fidir;
10421+
10422+ err = -ENOMEM;
2000de60 10423+ sb = file->f_path.dentry->d_sb;
4a4d8108 10424+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10425+ fidir = au_fidir_alloc(sb);
4a4d8108 10426+ if (fidir) {
b912730e
AM
10427+ struct au_do_open_args args = {
10428+ .open = do_open_dir,
10429+ .fidir = fidir
10430+ };
10431+ err = au_do_open(file, &args);
4a4d8108 10432+ if (unlikely(err))
1c60b727 10433+ kfree(fidir);
4a4d8108
AM
10434+ }
10435+ si_read_unlock(sb);
10436+ return err;
1facf9fc 10437+}
10438+
10439+static int aufs_release_dir(struct inode *inode __maybe_unused,
10440+ struct file *file)
10441+{
10442+ struct au_vdir *vdir_cache;
4a4d8108
AM
10443+ struct au_finfo *finfo;
10444+ struct au_fidir *fidir;
f0c0a007 10445+ struct au_hfile *hf;
5afbbe0d 10446+ aufs_bindex_t bindex, bbot;
1facf9fc 10447+
4a4d8108
AM
10448+ finfo = au_fi(file);
10449+ fidir = finfo->fi_hdir;
10450+ if (fidir) {
8b6a4947
AM
10451+ au_hbl_del(&finfo->fi_hlist,
10452+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10453+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10454+ if (vdir_cache)
1c60b727 10455+ au_vdir_free(vdir_cache);
4a4d8108
AM
10456+
10457+ bindex = finfo->fi_btop;
10458+ if (bindex >= 0) {
f0c0a007 10459+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10460+ /*
10461+ * calls fput() instead of filp_close(),
10462+ * since no dnotify or lock for the lower file.
10463+ */
5afbbe0d 10464+ bbot = fidir->fd_bbot;
f0c0a007
AM
10465+ for (; bindex <= bbot; bindex++, hf++)
10466+ if (hf->hf_file)
1c60b727 10467+ au_hfput(hf, /*execed*/0);
4a4d8108 10468+ }
1c60b727 10469+ kfree(fidir);
4a4d8108 10470+ finfo->fi_hdir = NULL;
1facf9fc 10471+ }
1c60b727 10472+ au_finfo_fin(file);
1facf9fc 10473+ return 0;
10474+}
10475+
10476+/* ---------------------------------------------------------------------- */
10477+
4a4d8108
AM
10478+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10479+{
10480+ int err;
5afbbe0d 10481+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10482+ struct file *h_file;
10483+
10484+ err = 0;
5afbbe0d
AM
10485+ bbot = au_fbbot_dir(file);
10486+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10487+ h_file = au_hf_dir(file, bindex);
10488+ if (h_file)
10489+ err = vfsub_flush(h_file, id);
10490+ }
10491+ return err;
10492+}
10493+
10494+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10495+{
10496+ return au_do_flush(file, id, au_do_flush_dir);
10497+}
10498+
10499+/* ---------------------------------------------------------------------- */
10500+
1facf9fc 10501+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10502+{
10503+ int err;
5afbbe0d 10504+ aufs_bindex_t bbot, bindex;
1facf9fc 10505+ struct inode *inode;
10506+ struct super_block *sb;
10507+
10508+ err = 0;
10509+ sb = dentry->d_sb;
5527c038 10510+ inode = d_inode(dentry);
1facf9fc 10511+ IMustLock(inode);
5afbbe0d
AM
10512+ bbot = au_dbbot(dentry);
10513+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10514+ struct path h_path;
1facf9fc 10515+
10516+ if (au_test_ro(sb, bindex, inode))
10517+ continue;
10518+ h_path.dentry = au_h_dptr(dentry, bindex);
10519+ if (!h_path.dentry)
10520+ continue;
1facf9fc 10521+
1facf9fc 10522+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10523+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10524+ }
10525+
10526+ return err;
10527+}
10528+
10529+static int au_do_fsync_dir(struct file *file, int datasync)
10530+{
10531+ int err;
5afbbe0d 10532+ aufs_bindex_t bbot, bindex;
1facf9fc 10533+ struct file *h_file;
10534+ struct super_block *sb;
10535+ struct inode *inode;
1facf9fc 10536+
521ced18 10537+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10538+ if (unlikely(err))
10539+ goto out;
10540+
c06a8ce3 10541+ inode = file_inode(file);
b912730e 10542+ sb = inode->i_sb;
5afbbe0d
AM
10543+ bbot = au_fbbot_dir(file);
10544+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10545+ h_file = au_hf_dir(file, bindex);
1facf9fc 10546+ if (!h_file || au_test_ro(sb, bindex, inode))
10547+ continue;
10548+
53392da6 10549+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10550+ }
10551+
4f0767ce 10552+out:
1facf9fc 10553+ return err;
10554+}
10555+
10556+/*
10557+ * @file may be NULL
10558+ */
1e00d052
AM
10559+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10560+ int datasync)
1facf9fc 10561+{
10562+ int err;
b752ccd1 10563+ struct dentry *dentry;
5527c038 10564+ struct inode *inode;
1facf9fc 10565+ struct super_block *sb;
1facf9fc 10566+
10567+ err = 0;
2000de60 10568+ dentry = file->f_path.dentry;
5527c038 10569+ inode = d_inode(dentry);
febd17d6 10570+ inode_lock(inode);
1facf9fc 10571+ sb = dentry->d_sb;
10572+ si_noflush_read_lock(sb);
10573+ if (file)
10574+ err = au_do_fsync_dir(file, datasync);
10575+ else {
10576+ di_write_lock_child(dentry);
10577+ err = au_do_fsync_dir_no_file(dentry, datasync);
10578+ }
5527c038 10579+ au_cpup_attr_timesizes(inode);
1facf9fc 10580+ di_write_unlock(dentry);
10581+ if (file)
10582+ fi_write_unlock(file);
10583+
10584+ si_read_unlock(sb);
febd17d6 10585+ inode_unlock(inode);
1facf9fc 10586+ return err;
10587+}
10588+
10589+/* ---------------------------------------------------------------------- */
10590+
5afbbe0d 10591+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10592+{
10593+ int err;
10594+ struct dentry *dentry;
9dbd164d 10595+ struct inode *inode, *h_inode;
1facf9fc 10596+ struct super_block *sb;
10597+
062440b3 10598+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10599+
2000de60 10600+ dentry = file->f_path.dentry;
5527c038 10601+ inode = d_inode(dentry);
1facf9fc 10602+ IMustLock(inode);
10603+
10604+ sb = dentry->d_sb;
10605+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10606+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10607+ if (unlikely(err))
10608+ goto out;
027c5e7a
AM
10609+ err = au_alive_dir(dentry);
10610+ if (!err)
10611+ err = au_vdir_init(file);
1facf9fc 10612+ di_downgrade_lock(dentry, AuLock_IR);
10613+ if (unlikely(err))
10614+ goto out_unlock;
10615+
5afbbe0d 10616+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10617+ if (!au_test_nfsd()) {
392086de 10618+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10619+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10620+ } else {
10621+ /*
10622+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10623+ * encode_fh() and others.
10624+ */
9dbd164d 10625+ atomic_inc(&h_inode->i_count);
1facf9fc 10626+ di_read_unlock(dentry, AuLock_IR);
10627+ si_read_unlock(sb);
392086de 10628+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10629+ fsstack_copy_attr_atime(inode, h_inode);
10630+ fi_write_unlock(file);
9dbd164d 10631+ iput(h_inode);
1facf9fc 10632+
10633+ AuTraceErr(err);
10634+ return err;
10635+ }
10636+
4f0767ce 10637+out_unlock:
1facf9fc 10638+ di_read_unlock(dentry, AuLock_IR);
10639+ fi_write_unlock(file);
4f0767ce 10640+out:
1facf9fc 10641+ si_read_unlock(sb);
10642+ return err;
10643+}
10644+
10645+/* ---------------------------------------------------------------------- */
10646+
10647+#define AuTestEmpty_WHONLY 1
dece6358
AM
10648+#define AuTestEmpty_CALLED (1 << 1)
10649+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10650+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10651+#define au_fset_testempty(flags, name) \
10652+ do { (flags) |= AuTestEmpty_##name; } while (0)
10653+#define au_fclr_testempty(flags, name) \
10654+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10655+
dece6358
AM
10656+#ifndef CONFIG_AUFS_SHWH
10657+#undef AuTestEmpty_SHWH
10658+#define AuTestEmpty_SHWH 0
10659+#endif
10660+
1facf9fc 10661+struct test_empty_arg {
392086de 10662+ struct dir_context ctx;
1308ab2a 10663+ struct au_nhash *whlist;
1facf9fc 10664+ unsigned int flags;
10665+ int err;
10666+ aufs_bindex_t bindex;
10667+};
10668+
392086de
AM
10669+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10670+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10671+ unsigned int d_type)
1facf9fc 10672+{
392086de
AM
10673+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10674+ ctx);
1facf9fc 10675+ char *name = (void *)__name;
10676+
10677+ arg->err = 0;
10678+ au_fset_testempty(arg->flags, CALLED);
10679+ /* smp_mb(); */
10680+ if (name[0] == '.'
10681+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10682+ goto out; /* success */
10683+
10684+ if (namelen <= AUFS_WH_PFX_LEN
10685+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10686+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10687+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10688+ arg->err = -ENOTEMPTY;
10689+ goto out;
10690+ }
10691+
10692+ name += AUFS_WH_PFX_LEN;
10693+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10694+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10695+ arg->err = au_nhash_append_wh
1308ab2a 10696+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10697+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10698+
4f0767ce 10699+out:
1facf9fc 10700+ /* smp_mb(); */
10701+ AuTraceErr(arg->err);
10702+ return arg->err;
10703+}
10704+
10705+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10706+{
10707+ int err;
10708+ struct file *h_file;
10709+
10710+ h_file = au_h_open(dentry, arg->bindex,
10711+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10712+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10713+ err = PTR_ERR(h_file);
10714+ if (IS_ERR(h_file))
10715+ goto out;
10716+
10717+ err = 0;
10718+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10719+ && !file_inode(h_file)->i_nlink)
1facf9fc 10720+ goto out_put;
10721+
10722+ do {
10723+ arg->err = 0;
10724+ au_fclr_testempty(arg->flags, CALLED);
10725+ /* smp_mb(); */
392086de 10726+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10727+ if (err >= 0)
10728+ err = arg->err;
10729+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10730+
4f0767ce 10731+out_put:
1facf9fc 10732+ fput(h_file);
10733+ au_sbr_put(dentry->d_sb, arg->bindex);
4f0767ce 10734+out:
1facf9fc 10735+ return err;
10736+}
10737+
10738+struct do_test_empty_args {
10739+ int *errp;
10740+ struct dentry *dentry;
10741+ struct test_empty_arg *arg;
10742+};
10743+
10744+static void call_do_test_empty(void *args)
10745+{
10746+ struct do_test_empty_args *a = args;
10747+ *a->errp = do_test_empty(a->dentry, a->arg);
10748+}
10749+
10750+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10751+{
10752+ int err, wkq_err;
10753+ struct dentry *h_dentry;
10754+ struct inode *h_inode;
10755+
10756+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10757+ h_inode = d_inode(h_dentry);
53392da6 10758+ /* todo: i_mode changes anytime? */
be118d29 10759+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10760+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10761+ inode_unlock_shared(h_inode);
1facf9fc 10762+ if (!err)
10763+ err = do_test_empty(dentry, arg);
10764+ else {
10765+ struct do_test_empty_args args = {
10766+ .errp = &err,
10767+ .dentry = dentry,
10768+ .arg = arg
10769+ };
10770+ unsigned int flags = arg->flags;
10771+
10772+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10773+ if (unlikely(wkq_err))
10774+ err = wkq_err;
10775+ arg->flags = flags;
10776+ }
10777+
10778+ return err;
10779+}
10780+
10781+int au_test_empty_lower(struct dentry *dentry)
10782+{
10783+ int err;
1308ab2a 10784+ unsigned int rdhash;
5afbbe0d 10785+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10786+ struct au_nhash whlist;
392086de
AM
10787+ struct test_empty_arg arg = {
10788+ .ctx = {
2000de60 10789+ .actor = test_empty_cb
392086de
AM
10790+ }
10791+ };
076b876e 10792+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10793+
dece6358
AM
10794+ SiMustAnyLock(dentry->d_sb);
10795+
1308ab2a 10796+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10797+ if (!rdhash)
10798+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10799+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10800+ if (unlikely(err))
1facf9fc 10801+ goto out;
10802+
1facf9fc 10803+ arg.flags = 0;
1308ab2a 10804+ arg.whlist = &whlist;
5afbbe0d 10805+ btop = au_dbtop(dentry);
dece6358
AM
10806+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10807+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10808+ test_empty = do_test_empty;
10809+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10810+ test_empty = sio_test_empty;
5afbbe0d 10811+ arg.bindex = btop;
076b876e 10812+ err = test_empty(dentry, &arg);
1facf9fc 10813+ if (unlikely(err))
10814+ goto out_whlist;
10815+
10816+ au_fset_testempty(arg.flags, WHONLY);
10817+ btail = au_dbtaildir(dentry);
5afbbe0d 10818+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10819+ struct dentry *h_dentry;
10820+
10821+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10822+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10823+ arg.bindex = bindex;
076b876e 10824+ err = test_empty(dentry, &arg);
1facf9fc 10825+ }
10826+ }
10827+
4f0767ce 10828+out_whlist:
1308ab2a 10829+ au_nhash_wh_free(&whlist);
4f0767ce 10830+out:
1facf9fc 10831+ return err;
10832+}
10833+
10834+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10835+{
10836+ int err;
392086de
AM
10837+ struct test_empty_arg arg = {
10838+ .ctx = {
2000de60 10839+ .actor = test_empty_cb
392086de
AM
10840+ }
10841+ };
1facf9fc 10842+ aufs_bindex_t bindex, btail;
10843+
10844+ err = 0;
1308ab2a 10845+ arg.whlist = whlist;
1facf9fc 10846+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10847+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10848+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10849+ btail = au_dbtaildir(dentry);
5afbbe0d 10850+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10851+ struct dentry *h_dentry;
10852+
10853+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10854+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10855+ arg.bindex = bindex;
10856+ err = sio_test_empty(dentry, &arg);
10857+ }
10858+ }
10859+
10860+ return err;
10861+}
10862+
10863+/* ---------------------------------------------------------------------- */
10864+
10865+const struct file_operations aufs_dir_fop = {
4a4d8108 10866+ .owner = THIS_MODULE,
027c5e7a 10867+ .llseek = default_llseek,
1facf9fc 10868+ .read = generic_read_dir,
5afbbe0d 10869+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10870+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10871+#ifdef CONFIG_COMPAT
10872+ .compat_ioctl = aufs_compat_ioctl_dir,
10873+#endif
1facf9fc 10874+ .open = aufs_open_dir,
10875+ .release = aufs_release_dir,
4a4d8108 10876+ .flush = aufs_flush_dir,
1facf9fc 10877+ .fsync = aufs_fsync_dir
10878+};
7f207e10
AM
10879diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10880--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
10881+++ linux/fs/aufs/dir.h 2018-07-19 09:46:13.056647808 +0200
10882@@ -0,0 +1,132 @@
10883+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10884+/*
b00004a5 10885+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 10886+ *
10887+ * This program, aufs is free software; you can redistribute it and/or modify
10888+ * it under the terms of the GNU General Public License as published by
10889+ * the Free Software Foundation; either version 2 of the License, or
10890+ * (at your option) any later version.
dece6358
AM
10891+ *
10892+ * This program is distributed in the hope that it will be useful,
10893+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10894+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10895+ * GNU General Public License for more details.
10896+ *
10897+ * You should have received a copy of the GNU General Public License
523b37e3 10898+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10899+ */
10900+
10901+/*
10902+ * directory operations
10903+ */
10904+
10905+#ifndef __AUFS_DIR_H__
10906+#define __AUFS_DIR_H__
10907+
10908+#ifdef __KERNEL__
10909+
10910+#include <linux/fs.h>
1facf9fc 10911+
10912+/* ---------------------------------------------------------------------- */
10913+
10914+/* need to be faster and smaller */
10915+
10916+struct au_nhash {
dece6358
AM
10917+ unsigned int nh_num;
10918+ struct hlist_head *nh_head;
1facf9fc 10919+};
10920+
10921+struct au_vdir_destr {
10922+ unsigned char len;
10923+ unsigned char name[0];
10924+} __packed;
10925+
10926+struct au_vdir_dehstr {
10927+ struct hlist_node hash;
1c60b727 10928+ struct au_vdir_destr *str;
4a4d8108 10929+} ____cacheline_aligned_in_smp;
1facf9fc 10930+
10931+struct au_vdir_de {
10932+ ino_t de_ino;
10933+ unsigned char de_type;
10934+ /* caution: packed */
10935+ struct au_vdir_destr de_str;
10936+} __packed;
10937+
10938+struct au_vdir_wh {
10939+ struct hlist_node wh_hash;
dece6358
AM
10940+#ifdef CONFIG_AUFS_SHWH
10941+ ino_t wh_ino;
1facf9fc 10942+ aufs_bindex_t wh_bindex;
dece6358
AM
10943+ unsigned char wh_type;
10944+#else
10945+ aufs_bindex_t wh_bindex;
10946+#endif
10947+ /* caution: packed */
1facf9fc 10948+ struct au_vdir_destr wh_str;
10949+} __packed;
10950+
10951+union au_vdir_deblk_p {
10952+ unsigned char *deblk;
10953+ struct au_vdir_de *de;
10954+};
10955+
10956+struct au_vdir {
10957+ unsigned char **vd_deblk;
10958+ unsigned long vd_nblk;
1facf9fc 10959+ struct {
10960+ unsigned long ul;
10961+ union au_vdir_deblk_p p;
10962+ } vd_last;
10963+
be118d29 10964+ u64 vd_version;
dece6358 10965+ unsigned int vd_deblk_sz;
1c60b727 10966+ unsigned long vd_jiffy;
4a4d8108 10967+} ____cacheline_aligned_in_smp;
1facf9fc 10968+
10969+/* ---------------------------------------------------------------------- */
10970+
10971+/* dir.c */
10972+extern const struct file_operations aufs_dir_fop;
10973+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10974+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10975+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10976+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10977+int au_test_empty_lower(struct dentry *dentry);
10978+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10979+
10980+/* vdir.c */
1308ab2a 10981+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10982+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10983+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10984+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10985+ int limit);
dece6358
AM
10986+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10987+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10988+ unsigned int d_type, aufs_bindex_t bindex,
10989+ unsigned char shwh);
1c60b727 10990+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 10991+int au_vdir_init(struct file *file);
392086de 10992+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10993+
10994+/* ioctl.c */
10995+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10996+
1308ab2a 10997+#ifdef CONFIG_AUFS_RDU
10998+/* rdu.c */
10999+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
11000+#ifdef CONFIG_COMPAT
11001+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
11002+ unsigned long arg);
11003+#endif
1308ab2a 11004+#else
c1595e42
JR
11005+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
11006+ unsigned int cmd, unsigned long arg)
b752ccd1 11007+#ifdef CONFIG_COMPAT
c1595e42
JR
11008+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
11009+ unsigned int cmd, unsigned long arg)
b752ccd1 11010+#endif
1308ab2a 11011+#endif
11012+
1facf9fc 11013+#endif /* __KERNEL__ */
11014+#endif /* __AUFS_DIR_H__ */
8b6a4947
AM
11015diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
11016--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
11017+++ linux/fs/aufs/dirren.c 2018-07-19 09:46:13.056647808 +0200
11018@@ -0,0 +1,1316 @@
11019+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 11020+/*
b00004a5 11021+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
11022+ *
11023+ * This program, aufs is free software; you can redistribute it and/or modify
11024+ * it under the terms of the GNU General Public License as published by
11025+ * the Free Software Foundation; either version 2 of the License, or
11026+ * (at your option) any later version.
11027+ *
11028+ * This program is distributed in the hope that it will be useful,
11029+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11030+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11031+ * GNU General Public License for more details.
11032+ *
11033+ * You should have received a copy of the GNU General Public License
11034+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
11035+ */
11036+
11037+/*
11038+ * special handling in renaming a directoy
11039+ * in order to support looking-up the before-renamed name on the lower readonly
11040+ * branches
11041+ */
11042+
11043+#include <linux/byteorder/generic.h>
11044+#include "aufs.h"
11045+
11046+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
11047+{
11048+ int idx;
11049+
11050+ idx = au_dr_ihash(ent->dr_h_ino);
11051+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
11052+}
11053+
11054+static int au_dr_hino_test_empty(struct au_dr_br *dr)
11055+{
11056+ int ret, i;
11057+ struct hlist_bl_head *hbl;
11058+
11059+ ret = 1;
11060+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
11061+ hbl = dr->dr_h_ino + i;
11062+ hlist_bl_lock(hbl);
11063+ ret &= hlist_bl_empty(hbl);
11064+ hlist_bl_unlock(hbl);
11065+ }
11066+
11067+ return ret;
11068+}
11069+
11070+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11071+{
11072+ struct au_dr_hino *found, *ent;
11073+ struct hlist_bl_head *hbl;
11074+ struct hlist_bl_node *pos;
11075+ int idx;
11076+
11077+ found = NULL;
11078+ idx = au_dr_ihash(ino);
11079+ hbl = dr->dr_h_ino + idx;
11080+ hlist_bl_lock(hbl);
11081+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11082+ if (ent->dr_h_ino == ino) {
11083+ found = ent;
11084+ break;
11085+ }
11086+ hlist_bl_unlock(hbl);
11087+
11088+ return found;
11089+}
11090+
11091+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11092+ struct au_dr_hino *add_ent)
11093+{
11094+ int found, idx;
11095+ struct hlist_bl_head *hbl;
11096+ struct hlist_bl_node *pos;
11097+ struct au_dr_hino *ent;
11098+
11099+ found = 0;
11100+ idx = au_dr_ihash(ino);
11101+ hbl = dr->dr_h_ino + idx;
11102+#if 0
11103+ {
11104+ struct hlist_bl_node *tmp;
11105+
11106+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11107+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11108+ }
11109+#endif
11110+ hlist_bl_lock(hbl);
11111+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11112+ if (ent->dr_h_ino == ino) {
11113+ found = 1;
11114+ break;
11115+ }
11116+ if (!found && add_ent)
11117+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11118+ hlist_bl_unlock(hbl);
11119+
11120+ if (!found && add_ent)
11121+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11122+
11123+ return found;
11124+}
11125+
11126+void au_dr_hino_free(struct au_dr_br *dr)
11127+{
11128+ int i;
11129+ struct hlist_bl_head *hbl;
11130+ struct hlist_bl_node *pos, *tmp;
11131+ struct au_dr_hino *ent;
11132+
11133+ /* SiMustWriteLock(sb); */
11134+
11135+ for (i = 0; i < AuDirren_NHASH; i++) {
11136+ hbl = dr->dr_h_ino + i;
11137+ /* no spinlock since sbinfo must be write-locked */
11138+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11139+ kfree(ent);
11140+ INIT_HLIST_BL_HEAD(hbl);
11141+ }
11142+}
11143+
11144+/* returns the number of inodes or an error */
11145+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11146+ struct file *hinofile)
11147+{
11148+ int err, i;
11149+ ssize_t ssz;
11150+ loff_t pos, oldsize;
11151+ __be64 u64;
11152+ struct inode *hinoinode;
11153+ struct hlist_bl_head *hbl;
11154+ struct hlist_bl_node *n1, *n2;
11155+ struct au_dr_hino *ent;
11156+
11157+ SiMustWriteLock(sb);
11158+ AuDebugOn(!au_br_writable(br->br_perm));
11159+
11160+ hinoinode = file_inode(hinofile);
11161+ oldsize = i_size_read(hinoinode);
11162+
11163+ err = 0;
11164+ pos = 0;
11165+ hbl = br->br_dirren.dr_h_ino;
11166+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11167+ /* no bit-lock since sbinfo must be write-locked */
11168+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11169+ AuDbg("hi%llu, %pD2\n",
11170+ (unsigned long long)ent->dr_h_ino, hinofile);
11171+ u64 = cpu_to_be64(ent->dr_h_ino);
11172+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11173+ if (ssz == sizeof(u64))
11174+ continue;
11175+
11176+ /* write error */
11177+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11178+ err = -ENOSPC;
11179+ if (ssz < 0)
11180+ err = ssz;
11181+ break;
11182+ }
11183+ }
11184+ /* regardless the error */
11185+ if (pos < oldsize) {
11186+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11187+ AuTraceErr(err);
11188+ }
11189+
11190+ AuTraceErr(err);
11191+ return err;
11192+}
11193+
11194+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11195+{
11196+ int err, hidx;
11197+ ssize_t ssz;
11198+ size_t sz, n;
11199+ loff_t pos;
11200+ uint64_t u64;
11201+ struct au_dr_hino *ent;
11202+ struct inode *hinoinode;
11203+ struct hlist_bl_head *hbl;
11204+
11205+ err = 0;
11206+ pos = 0;
11207+ hbl = dr->dr_h_ino;
11208+ hinoinode = file_inode(hinofile);
11209+ sz = i_size_read(hinoinode);
11210+ AuDebugOn(sz % sizeof(u64));
11211+ n = sz / sizeof(u64);
11212+ while (n--) {
11213+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11214+ if (unlikely(ssz != sizeof(u64))) {
11215+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11216+ err = -EINVAL;
11217+ if (ssz < 0)
11218+ err = ssz;
11219+ goto out_free;
11220+ }
11221+
11222+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11223+ if (!ent) {
11224+ err = -ENOMEM;
11225+ AuTraceErr(err);
11226+ goto out_free;
11227+ }
11228+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11229+ AuDbg("hi%llu, %pD2\n",
11230+ (unsigned long long)ent->dr_h_ino, hinofile);
11231+ hidx = au_dr_ihash(ent->dr_h_ino);
11232+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11233+ }
11234+ goto out; /* success */
11235+
11236+out_free:
11237+ au_dr_hino_free(dr);
11238+out:
11239+ AuTraceErr(err);
11240+ return err;
11241+}
11242+
11243+/*
11244+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11245+ * @path is a switch to distinguish load and store.
11246+ */
11247+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11248+ struct au_branch *br, const struct path *path)
11249+{
11250+ int err, flags;
11251+ unsigned char load, suspend;
11252+ struct file *hinofile;
11253+ struct au_hinode *hdir;
11254+ struct inode *dir, *delegated;
11255+ struct path hinopath;
11256+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11257+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11258+
11259+ AuDebugOn(bindex < 0 && !br);
11260+ AuDebugOn(bindex >= 0 && br);
11261+
11262+ err = -EINVAL;
11263+ suspend = !br;
11264+ if (suspend)
11265+ br = au_sbr(sb, bindex);
11266+ load = !!path;
11267+ if (!load) {
11268+ path = &br->br_path;
11269+ AuDebugOn(!au_br_writable(br->br_perm));
11270+ if (unlikely(!au_br_writable(br->br_perm)))
11271+ goto out;
11272+ }
11273+
11274+ hdir = NULL;
11275+ if (suspend) {
11276+ dir = d_inode(sb->s_root);
11277+ hdir = au_hinode(au_ii(dir), bindex);
11278+ dir = hdir->hi_inode;
11279+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11280+ } else {
11281+ dir = d_inode(path->dentry);
11282+ inode_lock_nested(dir, AuLsc_I_CHILD);
11283+ }
11284+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11285+ err = PTR_ERR(hinopath.dentry);
11286+ if (IS_ERR(hinopath.dentry))
11287+ goto out_unlock;
11288+
11289+ err = 0;
11290+ flags = O_RDONLY;
11291+ if (load) {
11292+ if (d_is_negative(hinopath.dentry))
11293+ goto out_dput; /* success */
11294+ } else {
11295+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11296+ if (d_is_positive(hinopath.dentry)) {
11297+ delegated = NULL;
11298+ err = vfsub_unlink(dir, &hinopath, &delegated,
11299+ /*force*/0);
11300+ AuTraceErr(err);
11301+ if (unlikely(err))
11302+ pr_err("ignored err %d, %pd2\n",
11303+ err, hinopath.dentry);
11304+ if (unlikely(err == -EWOULDBLOCK))
11305+ iput(delegated);
11306+ err = 0;
11307+ }
11308+ goto out_dput;
11309+ } else if (!d_is_positive(hinopath.dentry)) {
11310+ err = vfsub_create(dir, &hinopath, 0600,
11311+ /*want_excl*/false);
11312+ AuTraceErr(err);
11313+ if (unlikely(err))
11314+ goto out_dput;
11315+ }
11316+ flags = O_WRONLY;
11317+ }
11318+ hinopath.mnt = path->mnt;
11319+ hinofile = vfsub_dentry_open(&hinopath, flags);
11320+ if (suspend)
11321+ au_hn_inode_unlock(hdir);
11322+ else
11323+ inode_unlock(dir);
11324+ dput(hinopath.dentry);
11325+ AuTraceErrPtr(hinofile);
11326+ if (IS_ERR(hinofile)) {
11327+ err = PTR_ERR(hinofile);
11328+ goto out;
11329+ }
11330+
11331+ if (load)
11332+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11333+ else
11334+ err = au_dr_hino_store(sb, br, hinofile);
11335+ fput(hinofile);
11336+ goto out;
11337+
11338+out_dput:
11339+ dput(hinopath.dentry);
11340+out_unlock:
11341+ if (suspend)
11342+ au_hn_inode_unlock(hdir);
11343+ else
11344+ inode_unlock(dir);
11345+out:
11346+ AuTraceErr(err);
11347+ return err;
11348+}
11349+
11350+/* ---------------------------------------------------------------------- */
11351+
11352+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11353+{
11354+ int err;
11355+ struct kstatfs kstfs;
11356+ dev_t dev;
11357+ struct dentry *dentry;
11358+ struct super_block *sb;
11359+
11360+ err = vfs_statfs((void *)path, &kstfs);
11361+ AuTraceErr(err);
11362+ if (unlikely(err))
11363+ goto out;
11364+
11365+ /* todo: support for UUID */
11366+
11367+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11368+ brid->type = AuBrid_FSID;
11369+ brid->fsid = kstfs.f_fsid;
11370+ } else {
11371+ dentry = path->dentry;
11372+ sb = dentry->d_sb;
11373+ dev = sb->s_dev;
11374+ if (dev) {
11375+ brid->type = AuBrid_DEV;
11376+ brid->dev = dev;
11377+ }
11378+ }
11379+
11380+out:
11381+ return err;
11382+}
11383+
11384+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11385+ const struct path *path)
11386+{
11387+ int err, i;
11388+ struct au_dr_br *dr;
11389+ struct hlist_bl_head *hbl;
11390+
11391+ dr = &br->br_dirren;
11392+ hbl = dr->dr_h_ino;
11393+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11394+ INIT_HLIST_BL_HEAD(hbl);
11395+
11396+ err = au_dr_brid_init(&dr->dr_brid, path);
11397+ if (unlikely(err))
11398+ goto out;
11399+
11400+ if (au_opt_test(au_mntflags(sb), DIRREN))
11401+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11402+
11403+out:
11404+ AuTraceErr(err);
11405+ return err;
11406+}
11407+
11408+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11409+{
11410+ int err;
11411+
11412+ err = 0;
11413+ if (au_br_writable(br->br_perm))
11414+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11415+ if (!err)
11416+ au_dr_hino_free(&br->br_dirren);
11417+
11418+ return err;
11419+}
11420+
11421+/* ---------------------------------------------------------------------- */
11422+
11423+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11424+ char *buf, size_t sz)
11425+{
11426+ int err;
11427+ unsigned int major, minor;
11428+ char *p;
11429+
11430+ p = buf;
11431+ err = snprintf(p, sz, "%d_", brid->type);
11432+ AuDebugOn(err > sz);
11433+ p += err;
11434+ sz -= err;
11435+ switch (brid->type) {
11436+ case AuBrid_Unset:
11437+ return -EINVAL;
11438+ case AuBrid_UUID:
11439+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11440+ break;
11441+ case AuBrid_FSID:
11442+ err = snprintf(p, sz, "%08x-%08x",
11443+ brid->fsid.val[0], brid->fsid.val[1]);
11444+ break;
11445+ case AuBrid_DEV:
11446+ major = MAJOR(brid->dev);
11447+ minor = MINOR(brid->dev);
11448+ if (major <= 0xff && minor <= 0xff)
11449+ err = snprintf(p, sz, "%02x%02x", major, minor);
11450+ else
11451+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11452+ break;
11453+ }
11454+ AuDebugOn(err > sz);
11455+ p += err;
11456+ sz -= err;
11457+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11458+ AuDebugOn(err > sz);
11459+ p += err;
11460+ sz -= err;
11461+
11462+ return p - buf;
11463+}
11464+
11465+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11466+{
11467+ int rlen;
11468+ struct dentry *br_dentry;
11469+ struct inode *br_inode;
11470+
11471+ br_dentry = au_br_dentry(br);
11472+ br_inode = d_inode(br_dentry);
11473+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11474+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11475+ AuDebugOn(rlen > len);
11476+
11477+ return rlen;
11478+}
11479+
11480+/* ---------------------------------------------------------------------- */
11481+
11482+/*
11483+ * from the given @h_dentry, construct drinfo at @*fdata.
11484+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11485+ * @allocated.
11486+ */
11487+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11488+ struct dentry *h_dentry,
11489+ unsigned char *allocated)
11490+{
11491+ int err, v;
11492+ struct au_drinfo_fdata *f, *p;
11493+ struct au_drinfo *drinfo;
11494+ struct inode *h_inode;
11495+ struct qstr *qname;
11496+
11497+ err = 0;
11498+ f = *fdata;
11499+ h_inode = d_inode(h_dentry);
11500+ qname = &h_dentry->d_name;
11501+ drinfo = &f->drinfo;
11502+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11503+ drinfo->oldnamelen = qname->len;
11504+ if (*allocated < sizeof(*f) + qname->len) {
11505+ v = roundup_pow_of_two(*allocated + qname->len);
11506+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11507+ if (unlikely(!p)) {
11508+ err = -ENOMEM;
11509+ AuTraceErr(err);
11510+ goto out;
11511+ }
11512+ f = p;
11513+ *fdata = f;
11514+ *allocated = v;
11515+ drinfo = &f->drinfo;
11516+ }
11517+ memcpy(drinfo->oldname, qname->name, qname->len);
11518+ AuDbg("i%llu, %.*s\n",
11519+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11520+ drinfo->oldname);
11521+
11522+out:
11523+ AuTraceErr(err);
11524+ return err;
11525+}
11526+
11527+/* callers have to free the return value */
11528+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11529+{
11530+ struct au_drinfo *ret, *drinfo;
11531+ struct au_drinfo_fdata fdata;
11532+ int len;
11533+ loff_t pos;
11534+ ssize_t ssz;
11535+
11536+ ret = ERR_PTR(-EIO);
11537+ pos = 0;
11538+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11539+ if (unlikely(ssz != sizeof(fdata))) {
11540+ AuIOErr("ssz %zd, %u, %pD2\n",
11541+ ssz, (unsigned int)sizeof(fdata), file);
11542+ goto out;
11543+ }
11544+
11545+ fdata.magic = ntohl((__force __be32)fdata.magic);
11546+ switch (fdata.magic) {
11547+ case AUFS_DRINFO_MAGIC_V1:
11548+ break;
11549+ default:
11550+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11551+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11552+ goto out;
11553+ }
11554+
11555+ drinfo = &fdata.drinfo;
11556+ len = drinfo->oldnamelen;
11557+ if (!len) {
11558+ AuIOErr("broken drinfo %pD2\n", file);
11559+ goto out;
11560+ }
11561+
11562+ ret = NULL;
11563+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11564+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11565+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11566+ (unsigned long long)drinfo->ino,
11567+ (unsigned long long)h_ino, file);
11568+ goto out; /* success */
11569+ }
11570+
11571+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11572+ if (unlikely(!ret)) {
11573+ ret = ERR_PTR(-ENOMEM);
11574+ AuTraceErrPtr(ret);
11575+ goto out;
11576+ }
11577+
11578+ *ret = *drinfo;
11579+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11580+ if (unlikely(ssz != len)) {
11581+ kfree(ret);
11582+ ret = ERR_PTR(-EIO);
11583+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11584+ goto out;
11585+ }
11586+
11587+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11588+
11589+out:
11590+ return ret;
11591+}
11592+
11593+/* ---------------------------------------------------------------------- */
11594+
11595+/* in order to be revertible */
11596+struct au_drinfo_rev_elm {
11597+ int created;
11598+ struct dentry *info_dentry;
11599+ struct au_drinfo *info_last;
11600+};
11601+
11602+struct au_drinfo_rev {
11603+ unsigned char already;
11604+ aufs_bindex_t nelm;
11605+ struct au_drinfo_rev_elm elm[0];
11606+};
11607+
11608+/* todo: isn't it too large? */
11609+struct au_drinfo_store {
11610+ struct path h_ppath;
11611+ struct dentry *h_dentry;
11612+ struct au_drinfo_fdata *fdata;
11613+ char *infoname; /* inside of whname, just after PFX */
11614+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11615+ aufs_bindex_t btgt, btail;
11616+ unsigned char no_sio,
11617+ allocated, /* current size of *fdata */
11618+ infonamelen, /* room size for p */
11619+ whnamelen, /* length of the genarated name */
11620+ renameback; /* renamed back */
11621+};
11622+
11623+/* on rename(2) error, the caller should revert it using @elm */
11624+static int au_drinfo_do_store(struct au_drinfo_store *w,
11625+ struct au_drinfo_rev_elm *elm)
11626+{
11627+ int err, len;
11628+ ssize_t ssz;
11629+ loff_t pos;
11630+ struct path infopath = {
11631+ .mnt = w->h_ppath.mnt
11632+ };
11633+ struct inode *h_dir, *h_inode, *delegated;
11634+ struct file *infofile;
11635+ struct qstr *qname;
11636+
11637+ AuDebugOn(elm
11638+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11639+
11640+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11641+ w->whnamelen);
11642+ AuTraceErrPtr(infopath.dentry);
11643+ if (IS_ERR(infopath.dentry)) {
11644+ err = PTR_ERR(infopath.dentry);
11645+ goto out;
11646+ }
11647+
11648+ err = 0;
11649+ h_dir = d_inode(w->h_ppath.dentry);
11650+ if (elm && d_is_negative(infopath.dentry)) {
11651+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11652+ AuTraceErr(err);
11653+ if (unlikely(err))
11654+ goto out_dput;
11655+ elm->created = 1;
11656+ elm->info_dentry = dget(infopath.dentry);
11657+ }
11658+
11659+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11660+ AuTraceErrPtr(infofile);
11661+ if (IS_ERR(infofile)) {
11662+ err = PTR_ERR(infofile);
11663+ goto out_dput;
11664+ }
11665+
11666+ h_inode = d_inode(infopath.dentry);
11667+ if (elm && i_size_read(h_inode)) {
11668+ h_inode = d_inode(w->h_dentry);
11669+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11670+ AuTraceErrPtr(elm->info_last);
11671+ if (IS_ERR(elm->info_last)) {
11672+ err = PTR_ERR(elm->info_last);
11673+ elm->info_last = NULL;
11674+ AuDebugOn(elm->info_dentry);
11675+ goto out_fput;
11676+ }
11677+ }
11678+
11679+ if (elm && w->renameback) {
11680+ delegated = NULL;
11681+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11682+ AuTraceErr(err);
11683+ if (unlikely(err == -EWOULDBLOCK))
11684+ iput(delegated);
11685+ goto out_fput;
11686+ }
11687+
11688+ pos = 0;
11689+ qname = &w->h_dentry->d_name;
11690+ len = sizeof(*w->fdata) + qname->len;
11691+ if (!elm)
11692+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11693+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11694+ if (ssz == len) {
11695+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11696+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11697+ goto out_fput; /* success */
11698+ } else {
11699+ err = -EIO;
11700+ if (ssz < 0)
11701+ err = ssz;
11702+ /* the caller should revert it using @elm */
11703+ }
11704+
11705+out_fput:
11706+ fput(infofile);
11707+out_dput:
11708+ dput(infopath.dentry);
11709+out:
11710+ AuTraceErr(err);
11711+ return err;
11712+}
11713+
11714+struct au_call_drinfo_do_store_args {
11715+ int *errp;
11716+ struct au_drinfo_store *w;
11717+ struct au_drinfo_rev_elm *elm;
11718+};
11719+
11720+static void au_call_drinfo_do_store(void *args)
11721+{
11722+ struct au_call_drinfo_do_store_args *a = args;
11723+
11724+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11725+}
11726+
11727+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11728+ struct au_drinfo_rev_elm *elm)
11729+{
11730+ int err, wkq_err;
11731+
11732+ if (w->no_sio)
11733+ err = au_drinfo_do_store(w, elm);
11734+ else {
11735+ struct au_call_drinfo_do_store_args a = {
11736+ .errp = &err,
11737+ .w = w,
11738+ .elm = elm
11739+ };
11740+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11741+ if (unlikely(wkq_err))
11742+ err = wkq_err;
11743+ }
11744+ AuTraceErr(err);
11745+
11746+ return err;
11747+}
11748+
11749+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11750+ aufs_bindex_t btgt)
11751+{
11752+ int err;
11753+
11754+ memset(w, 0, sizeof(*w));
11755+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11756+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11757+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11758+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11759+ w->btgt = btgt;
11760+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11761+
11762+ err = -ENOMEM;
11763+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11764+ if (unlikely(!w->fdata)) {
11765+ AuTraceErr(err);
11766+ goto out;
11767+ }
11768+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11769+ err = 0;
11770+
11771+out:
11772+ return err;
11773+}
11774+
11775+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11776+{
11777+ kfree(w->fdata);
11778+}
11779+
11780+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11781+ struct au_drinfo_store *w)
11782+{
11783+ struct au_drinfo_rev_elm *elm;
11784+ struct inode *h_dir, *delegated;
11785+ int err, nelm;
11786+ struct path infopath = {
11787+ .mnt = w->h_ppath.mnt
11788+ };
11789+
11790+ h_dir = d_inode(w->h_ppath.dentry);
11791+ IMustLock(h_dir);
11792+
11793+ err = 0;
11794+ elm = rev->elm;
11795+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11796+ AuDebugOn(elm->created && elm->info_last);
11797+ if (elm->created) {
11798+ AuDbg("here\n");
11799+ delegated = NULL;
11800+ infopath.dentry = elm->info_dentry;
11801+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11802+ !w->no_sio);
11803+ AuTraceErr(err);
11804+ if (unlikely(err == -EWOULDBLOCK))
11805+ iput(delegated);
11806+ dput(elm->info_dentry);
11807+ } else if (elm->info_last) {
11808+ AuDbg("here\n");
11809+ w->fdata->drinfo = *elm->info_last;
11810+ memcpy(w->fdata->drinfo.oldname,
11811+ elm->info_last->oldname,
11812+ elm->info_last->oldnamelen);
11813+ err = au_drinfo_store_sio(w, /*elm*/NULL);
11814+ kfree(elm->info_last);
11815+ }
11816+ if (unlikely(err))
11817+ AuIOErr("%d, %s\n", err, w->whname);
11818+ /* go on even if err */
11819+ }
11820+}
11821+
11822+/* caller has to call au_dr_rename_fin() later */
11823+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11824+ struct qstr *dst_name, void *_rev)
11825+{
11826+ int err, sz, nelm;
11827+ aufs_bindex_t bindex, btail;
11828+ struct au_drinfo_store work;
11829+ struct au_drinfo_rev *rev, **p;
11830+ struct au_drinfo_rev_elm *elm;
11831+ struct super_block *sb;
11832+ struct au_branch *br;
11833+ struct au_hinode *hdir;
11834+
11835+ err = au_drinfo_store_work_init(&work, btgt);
11836+ AuTraceErr(err);
11837+ if (unlikely(err))
11838+ goto out;
11839+
11840+ err = -ENOMEM;
11841+ btail = au_dbtaildir(dentry);
11842+ nelm = btail - btgt;
11843+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11844+ rev = kcalloc(1, sz, GFP_NOFS);
11845+ if (unlikely(!rev)) {
11846+ AuTraceErr(err);
11847+ goto out_args;
11848+ }
11849+ rev->nelm = nelm;
11850+ elm = rev->elm;
11851+ p = _rev;
11852+ *p = rev;
11853+
11854+ err = 0;
11855+ sb = dentry->d_sb;
11856+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11857+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11858+ hdir = au_hi(d_inode(dentry), btgt);
11859+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11860+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11861+ work.h_dentry = au_h_dptr(dentry, bindex);
11862+ if (!work.h_dentry)
11863+ continue;
11864+
11865+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11866+ &work.allocated);
11867+ AuTraceErr(err);
11868+ if (unlikely(err))
11869+ break;
11870+
11871+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11872+ br = au_sbr(sb, bindex);
11873+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11874+ work.whnamelen += au_drinfo_name(br, work.infoname,
11875+ work.infonamelen);
11876+ AuDbg("whname %.*s, i%llu, %.*s\n",
11877+ work.whnamelen, work.whname,
11878+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11879+ work.fdata->drinfo.oldnamelen,
11880+ work.fdata->drinfo.oldname);
11881+
11882+ err = au_drinfo_store_sio(&work, elm);
11883+ AuTraceErr(err);
11884+ if (unlikely(err))
11885+ break;
11886+ }
11887+ if (unlikely(err)) {
11888+ /* revert all drinfo */
11889+ au_drinfo_store_rev(rev, &work);
11890+ kfree(rev);
11891+ *p = NULL;
11892+ }
11893+ au_hn_inode_unlock(hdir);
11894+
11895+out_args:
11896+ au_drinfo_store_work_fin(&work);
11897+out:
11898+ return err;
11899+}
11900+
11901+/* ---------------------------------------------------------------------- */
11902+
11903+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11904+ struct qstr *dst_name, void *_rev)
11905+{
11906+ int err, already;
11907+ ino_t ino;
11908+ struct super_block *sb;
11909+ struct au_branch *br;
11910+ struct au_dr_br *dr;
11911+ struct dentry *h_dentry;
11912+ struct inode *h_inode;
11913+ struct au_dr_hino *ent;
11914+ struct au_drinfo_rev *rev, **p;
11915+
11916+ AuDbg("bindex %d\n", bindex);
11917+
11918+ err = -ENOMEM;
11919+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11920+ if (unlikely(!ent))
11921+ goto out;
11922+
11923+ sb = src->d_sb;
11924+ br = au_sbr(sb, bindex);
11925+ dr = &br->br_dirren;
11926+ h_dentry = au_h_dptr(src, bindex);
11927+ h_inode = d_inode(h_dentry);
11928+ ino = h_inode->i_ino;
11929+ ent->dr_h_ino = ino;
11930+ already = au_dr_hino_test_add(dr, ino, ent);
11931+ AuDbg("b%d, hi%llu, already %d\n",
11932+ bindex, (unsigned long long)ino, already);
11933+
11934+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11935+ AuTraceErr(err);
11936+ if (!err) {
11937+ p = _rev;
11938+ rev = *p;
11939+ rev->already = already;
11940+ goto out; /* success */
11941+ }
11942+
11943+ /* revert */
11944+ if (!already)
11945+ au_dr_hino_del(dr, ent);
11946+ kfree(ent);
11947+
11948+out:
11949+ AuTraceErr(err);
11950+ return err;
11951+}
11952+
11953+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11954+{
11955+ struct au_drinfo_rev *rev;
11956+ struct au_drinfo_rev_elm *elm;
11957+ int nelm;
11958+
11959+ rev = _rev;
11960+ elm = rev->elm;
11961+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11962+ dput(elm->info_dentry);
11963+ kfree(elm->info_last);
11964+ }
11965+ kfree(rev);
11966+}
11967+
11968+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11969+{
11970+ int err;
11971+ struct au_drinfo_store work;
11972+ struct au_drinfo_rev *rev = _rev;
11973+ struct super_block *sb;
11974+ struct au_branch *br;
11975+ struct inode *h_inode;
11976+ struct au_dr_br *dr;
11977+ struct au_dr_hino *ent;
11978+
11979+ err = au_drinfo_store_work_init(&work, btgt);
11980+ if (unlikely(err))
11981+ goto out;
11982+
11983+ sb = src->d_sb;
11984+ br = au_sbr(sb, btgt);
11985+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11986+ work.h_ppath.mnt = au_br_mnt(br);
11987+ au_drinfo_store_rev(rev, &work);
11988+ au_drinfo_store_work_fin(&work);
11989+ if (rev->already)
11990+ goto out;
11991+
11992+ dr = &br->br_dirren;
11993+ h_inode = d_inode(work.h_ppath.dentry);
11994+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11995+ BUG_ON(!ent);
11996+ au_dr_hino_del(dr, ent);
11997+ kfree(ent);
11998+
11999+out:
12000+ kfree(rev);
12001+ if (unlikely(err))
12002+ pr_err("failed to remove dirren info\n");
12003+}
12004+
12005+/* ---------------------------------------------------------------------- */
12006+
12007+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
12008+ char *whname, int whnamelen,
12009+ struct dentry **info_dentry)
12010+{
12011+ struct au_drinfo *drinfo;
12012+ struct file *f;
12013+ struct inode *h_dir;
12014+ struct path infopath;
12015+ int unlocked;
12016+
12017+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
12018+
12019+ *info_dentry = NULL;
12020+ drinfo = NULL;
12021+ unlocked = 0;
12022+ h_dir = d_inode(h_ppath->dentry);
be118d29 12023+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947
AM
12024+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
12025+ whnamelen);
12026+ if (IS_ERR(infopath.dentry)) {
12027+ drinfo = (void *)infopath.dentry;
12028+ goto out;
12029+ }
12030+
12031+ if (d_is_negative(infopath.dentry))
12032+ goto out_dput; /* success */
12033+
12034+ infopath.mnt = h_ppath->mnt;
12035+ f = vfsub_dentry_open(&infopath, O_RDONLY);
12036+ inode_unlock_shared(h_dir);
12037+ unlocked = 1;
12038+ if (IS_ERR(f)) {
12039+ drinfo = (void *)f;
12040+ goto out_dput;
12041+ }
12042+
12043+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
12044+ if (IS_ERR_OR_NULL(drinfo))
12045+ goto out_fput;
12046+
12047+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
12048+ *info_dentry = dget(infopath.dentry); /* keep it alive */
12049+
12050+out_fput:
12051+ fput(f);
12052+out_dput:
12053+ dput(infopath.dentry);
12054+out:
12055+ if (!unlocked)
12056+ inode_unlock_shared(h_dir);
12057+ AuTraceErrPtr(drinfo);
12058+ return drinfo;
12059+}
12060+
12061+struct au_drinfo_do_load_args {
12062+ struct au_drinfo **drinfop;
12063+ struct path *h_ppath;
12064+ char *whname;
12065+ int whnamelen;
12066+ struct dentry **info_dentry;
12067+};
12068+
12069+static void au_call_drinfo_do_load(void *args)
12070+{
12071+ struct au_drinfo_do_load_args *a = args;
12072+
12073+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12074+ a->info_dentry);
12075+}
12076+
12077+struct au_drinfo_load {
12078+ struct path h_ppath;
12079+ struct qstr *qname;
12080+ unsigned char no_sio;
12081+
12082+ aufs_bindex_t ninfo;
12083+ struct au_drinfo **drinfo;
12084+};
12085+
12086+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12087+ struct au_branch *br)
12088+{
12089+ int err, wkq_err, whnamelen, e;
12090+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12091+ = AUFS_WH_DR_INFO_PFX;
12092+ struct au_drinfo *drinfo;
12093+ struct qstr oldname;
12094+ struct inode *h_dir, *delegated;
12095+ struct dentry *info_dentry;
12096+ struct path infopath;
12097+
12098+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12099+ whnamelen += au_drinfo_name(br, whname + whnamelen,
12100+ sizeof(whname) - whnamelen);
12101+ if (w->no_sio)
12102+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12103+ &info_dentry);
12104+ else {
12105+ struct au_drinfo_do_load_args args = {
12106+ .drinfop = &drinfo,
12107+ .h_ppath = &w->h_ppath,
12108+ .whname = whname,
12109+ .whnamelen = whnamelen,
12110+ .info_dentry = &info_dentry
12111+ };
12112+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12113+ if (unlikely(wkq_err))
12114+ drinfo = ERR_PTR(wkq_err);
12115+ }
12116+ err = PTR_ERR(drinfo);
12117+ if (IS_ERR_OR_NULL(drinfo))
12118+ goto out;
12119+
12120+ err = 0;
12121+ oldname.len = drinfo->oldnamelen;
12122+ oldname.name = drinfo->oldname;
12123+ if (au_qstreq(w->qname, &oldname)) {
12124+ /* the name is renamed back */
12125+ kfree(drinfo);
12126+ drinfo = NULL;
12127+
12128+ infopath.dentry = info_dentry;
12129+ infopath.mnt = w->h_ppath.mnt;
12130+ h_dir = d_inode(w->h_ppath.dentry);
12131+ delegated = NULL;
12132+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12133+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12134+ inode_unlock(h_dir);
12135+ if (unlikely(e))
12136+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12137+ if (unlikely(e == -EWOULDBLOCK))
12138+ iput(delegated);
12139+ }
12140+ kfree(w->drinfo[bindex]);
12141+ w->drinfo[bindex] = drinfo;
12142+ dput(info_dentry);
12143+
12144+out:
12145+ AuTraceErr(err);
12146+ return err;
12147+}
12148+
12149+/* ---------------------------------------------------------------------- */
12150+
12151+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12152+{
12153+ struct au_drinfo **p = drinfo;
12154+
12155+ while (n-- > 0)
12156+ kfree(*drinfo++);
12157+ kfree(p);
12158+}
12159+
12160+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12161+ aufs_bindex_t btgt)
12162+{
12163+ int err, ninfo;
12164+ struct au_drinfo_load w;
12165+ aufs_bindex_t bindex, bbot;
12166+ struct au_branch *br;
12167+ struct inode *h_dir;
12168+ struct au_dr_hino *ent;
12169+ struct super_block *sb;
12170+
12171+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12172+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12173+ AuLNPair(&lkup->whname), btgt);
12174+
12175+ sb = dentry->d_sb;
12176+ bbot = au_sbbot(sb);
12177+ w.ninfo = bbot + 1;
12178+ if (!lkup->dirren.drinfo) {
12179+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12180+ sizeof(*lkup->dirren.drinfo),
12181+ GFP_NOFS);
12182+ if (unlikely(!lkup->dirren.drinfo)) {
12183+ err = -ENOMEM;
12184+ goto out;
12185+ }
12186+ lkup->dirren.ninfo = w.ninfo;
12187+ }
12188+ w.drinfo = lkup->dirren.drinfo;
12189+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12190+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12191+ AuDebugOn(!w.h_ppath.dentry);
12192+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12193+ w.qname = &dentry->d_name;
12194+
12195+ ninfo = 0;
12196+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12197+ br = au_sbr(sb, bindex);
12198+ err = au_drinfo_load(&w, bindex, br);
12199+ if (unlikely(err))
12200+ goto out_free;
12201+ if (w.drinfo[bindex])
12202+ ninfo++;
12203+ }
12204+ if (!ninfo) {
12205+ br = au_sbr(sb, btgt);
12206+ h_dir = d_inode(w.h_ppath.dentry);
12207+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12208+ AuDebugOn(!ent);
12209+ au_dr_hino_del(&br->br_dirren, ent);
12210+ kfree(ent);
12211+ }
12212+ goto out; /* success */
12213+
12214+out_free:
12215+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12216+ lkup->dirren.ninfo = 0;
12217+ lkup->dirren.drinfo = NULL;
12218+out:
12219+ AuTraceErr(err);
12220+ return err;
12221+}
12222+
12223+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12224+{
12225+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12226+}
12227+
12228+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12229+{
12230+ int err;
12231+ struct au_drinfo *drinfo;
12232+
12233+ err = 0;
12234+ if (!lkup->dirren.drinfo)
12235+ goto out;
12236+ AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12237+ drinfo = lkup->dirren.drinfo[btgt + 1];
12238+ if (!drinfo)
12239+ goto out;
12240+
12241+ kfree(lkup->whname.name);
12242+ lkup->whname.name = NULL;
12243+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12244+ lkup->dirren.dr_name.name = drinfo->oldname;
12245+ lkup->name = &lkup->dirren.dr_name;
12246+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12247+ if (!err)
12248+ AuDbg("name %.*s, whname %.*s, b%d\n",
12249+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12250+ btgt);
12251+
12252+out:
12253+ AuTraceErr(err);
12254+ return err;
12255+}
12256+
12257+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12258+ ino_t h_ino)
12259+{
12260+ int match;
12261+ struct au_drinfo *drinfo;
12262+
12263+ match = 1;
12264+ if (!lkup->dirren.drinfo)
12265+ goto out;
12266+ AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12267+ drinfo = lkup->dirren.drinfo[bindex + 1];
12268+ if (!drinfo)
12269+ goto out;
12270+
12271+ match = (drinfo->ino == h_ino);
12272+ AuDbg("match %d\n", match);
12273+
12274+out:
12275+ return match;
12276+}
12277+
12278+/* ---------------------------------------------------------------------- */
12279+
12280+int au_dr_opt_set(struct super_block *sb)
12281+{
12282+ int err;
12283+ aufs_bindex_t bindex, bbot;
12284+ struct au_branch *br;
12285+
12286+ err = 0;
12287+ bbot = au_sbbot(sb);
12288+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12289+ br = au_sbr(sb, bindex);
12290+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12291+ }
12292+
12293+ return err;
12294+}
12295+
12296+int au_dr_opt_flush(struct super_block *sb)
12297+{
12298+ int err;
12299+ aufs_bindex_t bindex, bbot;
12300+ struct au_branch *br;
12301+
12302+ err = 0;
12303+ bbot = au_sbbot(sb);
12304+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12305+ br = au_sbr(sb, bindex);
12306+ if (au_br_writable(br->br_perm))
12307+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12308+ }
12309+
12310+ return err;
12311+}
12312+
12313+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12314+{
12315+ int err;
12316+ aufs_bindex_t bindex, bbot;
12317+ struct au_branch *br;
12318+
12319+ err = 0;
12320+ if (!no_flush) {
12321+ err = au_dr_opt_flush(sb);
12322+ if (unlikely(err))
12323+ goto out;
12324+ }
12325+
12326+ bbot = au_sbbot(sb);
12327+ for (bindex = 0; bindex <= bbot; bindex++) {
12328+ br = au_sbr(sb, bindex);
12329+ au_dr_hino_free(&br->br_dirren);
12330+ }
12331+
12332+out:
12333+ return err;
12334+}
12335diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12336--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
12337+++ linux/fs/aufs/dirren.h 2018-07-19 09:46:13.056647808 +0200
12338@@ -0,0 +1,140 @@
12339+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 12340+/*
b00004a5 12341+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
12342+ *
12343+ * This program, aufs is free software; you can redistribute it and/or modify
12344+ * it under the terms of the GNU General Public License as published by
12345+ * the Free Software Foundation; either version 2 of the License, or
12346+ * (at your option) any later version.
12347+ *
12348+ * This program is distributed in the hope that it will be useful,
12349+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12350+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12351+ * GNU General Public License for more details.
12352+ *
12353+ * You should have received a copy of the GNU General Public License
12354+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12355+ */
12356+
12357+/*
12358+ * renamed dir info
12359+ */
12360+
12361+#ifndef __AUFS_DIRREN_H__
12362+#define __AUFS_DIRREN_H__
12363+
12364+#ifdef __KERNEL__
12365+
12366+#include <linux/dcache.h>
12367+#include <linux/statfs.h>
12368+#include <linux/uuid.h>
12369+#include "hbl.h"
12370+
12371+#define AuDirren_NHASH 100
12372+
12373+#ifdef CONFIG_AUFS_DIRREN
12374+enum au_brid_type {
12375+ AuBrid_Unset,
12376+ AuBrid_UUID,
12377+ AuBrid_FSID,
12378+ AuBrid_DEV
12379+};
12380+
12381+struct au_dr_brid {
12382+ enum au_brid_type type;
12383+ union {
12384+ uuid_t uuid; /* unimplemented yet */
12385+ fsid_t fsid;
12386+ dev_t dev;
12387+ };
12388+};
12389+
12390+/* 20 is the max digits length of ulong 64 */
12391+/* brid-type "_" uuid "_" inum */
12392+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12393+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12394+
12395+struct au_dr_hino {
12396+ struct hlist_bl_node dr_hnode;
12397+ ino_t dr_h_ino;
12398+};
12399+
12400+struct au_dr_br {
12401+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12402+ struct au_dr_brid dr_brid;
12403+};
12404+
12405+struct au_dr_lookup {
12406+ /* dr_name is pointed by struct au_do_lookup_args.name */
12407+ struct qstr dr_name; /* subset of dr_info */
12408+ aufs_bindex_t ninfo;
12409+ struct au_drinfo **drinfo;
12410+};
12411+#else
12412+struct au_dr_hino;
12413+/* empty */
12414+struct au_dr_br { };
12415+struct au_dr_lookup { };
12416+#endif
12417+
12418+/* ---------------------------------------------------------------------- */
12419+
12420+struct au_branch;
12421+struct au_do_lookup_args;
12422+struct au_hinode;
12423+#ifdef CONFIG_AUFS_DIRREN
12424+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12425+ struct au_dr_hino *add_ent);
12426+void au_dr_hino_free(struct au_dr_br *dr);
12427+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12428+ const struct path *path);
12429+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12430+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12431+ struct qstr *dst_name, void *_rev);
12432+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12433+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12434+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12435+ aufs_bindex_t bindex);
12436+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12437+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12438+ ino_t h_ino);
12439+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12440+int au_dr_opt_set(struct super_block *sb);
12441+int au_dr_opt_flush(struct super_block *sb);
12442+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12443+#else
12444+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12445+ struct au_dr_hino *add_ent);
12446+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12447+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12448+ const struct path *path);
12449+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12450+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12451+ struct qstr *dst_name, void *_rev);
12452+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12453+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12454+ void *rev);
12455+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12456+ aufs_bindex_t bindex);
12457+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12458+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12459+ aufs_bindex_t bindex, ino_t h_ino);
12460+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12461+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12462+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12463+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12464+#endif
12465+
12466+/* ---------------------------------------------------------------------- */
12467+
12468+#ifdef CONFIG_AUFS_DIRREN
12469+static inline int au_dr_ihash(ino_t h_ino)
12470+{
12471+ return h_ino % AuDirren_NHASH;
12472+}
12473+#else
12474+AuStubInt0(au_dr_ihash, ino_t h_ino);
12475+#endif
12476+
12477+#endif /* __KERNEL__ */
12478+#endif /* __AUFS_DIRREN_H__ */
7f207e10
AM
12479diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12480--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
12481+++ linux/fs/aufs/dynop.c 2018-07-19 09:46:13.056647808 +0200
12482@@ -0,0 +1,370 @@
12483+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 12484+/*
b00004a5 12485+ * Copyright (C) 2010-2018 Junjiro R. Okajima
1facf9fc 12486+ *
12487+ * This program, aufs is free software; you can redistribute it and/or modify
12488+ * it under the terms of the GNU General Public License as published by
12489+ * the Free Software Foundation; either version 2 of the License, or
12490+ * (at your option) any later version.
dece6358
AM
12491+ *
12492+ * This program is distributed in the hope that it will be useful,
12493+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12494+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12495+ * GNU General Public License for more details.
12496+ *
12497+ * You should have received a copy of the GNU General Public License
523b37e3 12498+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12499+ */
12500+
12501+/*
4a4d8108 12502+ * dynamically customizable operations for regular files
1facf9fc 12503+ */
12504+
1facf9fc 12505+#include "aufs.h"
12506+
4a4d8108 12507+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12508+
4a4d8108
AM
12509+/*
12510+ * How large will these lists be?
12511+ * Usually just a few elements, 20-30 at most for each, I guess.
12512+ */
8b6a4947 12513+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12514+
8b6a4947
AM
12515+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12516+ const void *h_op)
1facf9fc 12517+{
4a4d8108 12518+ struct au_dykey *key, *tmp;
8b6a4947 12519+ struct hlist_bl_node *pos;
1facf9fc 12520+
4a4d8108 12521+ key = NULL;
8b6a4947
AM
12522+ hlist_bl_lock(hbl);
12523+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12524+ if (tmp->dk_op.dy_hop == h_op) {
12525+ key = tmp;
12526+ kref_get(&key->dk_kref);
12527+ break;
12528+ }
8b6a4947 12529+ hlist_bl_unlock(hbl);
4a4d8108
AM
12530+
12531+ return key;
1facf9fc 12532+}
12533+
4a4d8108 12534+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12535+{
4a4d8108
AM
12536+ struct au_dykey **k, *found;
12537+ const void *h_op = key->dk_op.dy_hop;
12538+ int i;
1facf9fc 12539+
4a4d8108
AM
12540+ found = NULL;
12541+ k = br->br_dykey;
12542+ for (i = 0; i < AuBrDynOp; i++)
12543+ if (k[i]) {
12544+ if (k[i]->dk_op.dy_hop == h_op) {
12545+ found = k[i];
12546+ break;
12547+ }
12548+ } else
12549+ break;
12550+ if (!found) {
12551+ spin_lock(&br->br_dykey_lock);
12552+ for (; i < AuBrDynOp; i++)
12553+ if (k[i]) {
12554+ if (k[i]->dk_op.dy_hop == h_op) {
12555+ found = k[i];
12556+ break;
12557+ }
12558+ } else {
12559+ k[i] = key;
12560+ break;
12561+ }
12562+ spin_unlock(&br->br_dykey_lock);
12563+ BUG_ON(i == AuBrDynOp); /* expand the array */
12564+ }
12565+
12566+ return found;
1facf9fc 12567+}
12568+
4a4d8108 12569+/* kref_get() if @key is already added */
8b6a4947 12570+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12571+{
12572+ struct au_dykey *tmp, *found;
8b6a4947 12573+ struct hlist_bl_node *pos;
4a4d8108 12574+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12575+
4a4d8108 12576+ found = NULL;
8b6a4947
AM
12577+ hlist_bl_lock(hbl);
12578+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108
AM
12579+ if (tmp->dk_op.dy_hop == h_op) {
12580+ kref_get(&tmp->dk_kref);
12581+ found = tmp;
12582+ break;
12583+ }
12584+ if (!found)
8b6a4947
AM
12585+ hlist_bl_add_head(&key->dk_hnode, hbl);
12586+ hlist_bl_unlock(hbl);
1facf9fc 12587+
4a4d8108
AM
12588+ if (!found)
12589+ DyPrSym(key);
12590+ return found;
12591+}
12592+
12593+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12594+{
4a4d8108
AM
12595+ struct au_dykey *key;
12596+
12597+ key = container_of(rcu, struct au_dykey, dk_rcu);
12598+ DyPrSym(key);
1c60b727 12599+ kfree(key);
1facf9fc 12600+}
12601+
4a4d8108
AM
12602+static void dy_free(struct kref *kref)
12603+{
12604+ struct au_dykey *key;
8b6a4947 12605+ struct hlist_bl_head *hbl;
1facf9fc 12606+
4a4d8108 12607+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12608+ hbl = dynop + key->dk_op.dy_type;
12609+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12610+ call_rcu(&key->dk_rcu, dy_free_rcu);
12611+}
12612+
12613+void au_dy_put(struct au_dykey *key)
1facf9fc 12614+{
4a4d8108
AM
12615+ kref_put(&key->dk_kref, dy_free);
12616+}
1facf9fc 12617+
4a4d8108
AM
12618+/* ---------------------------------------------------------------------- */
12619+
12620+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12621+
12622+#ifdef CONFIG_AUFS_DEBUG
12623+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12624+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12625+#else
12626+#define DyDbgDeclare(cnt) do {} while (0)
12627+#define DyDbgInc(cnt) do {} while (0)
12628+#endif
12629+
12630+#define DySet(func, dst, src, h_op, h_sb) do { \
12631+ DyDbgInc(cnt); \
12632+ if (h_op->func) { \
12633+ if (src.func) \
12634+ dst.func = src.func; \
12635+ else \
12636+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12637+ } \
12638+} while (0)
12639+
12640+#define DySetForce(func, dst, src) do { \
12641+ AuDebugOn(!src.func); \
12642+ DyDbgInc(cnt); \
12643+ dst.func = src.func; \
12644+} while (0)
12645+
12646+#define DySetAop(func) \
12647+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12648+#define DySetAopForce(func) \
12649+ DySetForce(func, dyaop->da_op, aufs_aop)
12650+
12651+static void dy_aop(struct au_dykey *key, const void *h_op,
12652+ struct super_block *h_sb __maybe_unused)
12653+{
12654+ struct au_dyaop *dyaop = (void *)key;
12655+ const struct address_space_operations *h_aop = h_op;
12656+ DyDbgDeclare(cnt);
12657+
12658+ AuDbg("%s\n", au_sbtype(h_sb));
12659+
12660+ DySetAop(writepage);
12661+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12662+ DySetAop(writepages);
12663+ DySetAop(set_page_dirty);
12664+ DySetAop(readpages);
12665+ DySetAop(write_begin);
12666+ DySetAop(write_end);
12667+ DySetAop(bmap);
12668+ DySetAop(invalidatepage);
12669+ DySetAop(releasepage);
027c5e7a 12670+ DySetAop(freepage);
7e9cd9fe 12671+ /* this one will be changed according to an aufs mount option */
4a4d8108 12672+ DySetAop(direct_IO);
4a4d8108 12673+ DySetAop(migratepage);
e2f27e51
AM
12674+ DySetAop(isolate_page);
12675+ DySetAop(putback_page);
4a4d8108
AM
12676+ DySetAop(launder_page);
12677+ DySetAop(is_partially_uptodate);
392086de 12678+ DySetAop(is_dirty_writeback);
4a4d8108 12679+ DySetAop(error_remove_page);
b4510431
AM
12680+ DySetAop(swap_activate);
12681+ DySetAop(swap_deactivate);
4a4d8108
AM
12682+
12683+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12684+}
12685+
4a4d8108
AM
12686+/* ---------------------------------------------------------------------- */
12687+
12688+static void dy_bug(struct kref *kref)
12689+{
12690+ BUG();
12691+}
12692+
12693+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12694+{
12695+ struct au_dykey *key, *old;
8b6a4947 12696+ struct hlist_bl_head *hbl;
b752ccd1 12697+ struct op {
4a4d8108 12698+ unsigned int sz;
b752ccd1
AM
12699+ void (*set)(struct au_dykey *key, const void *h_op,
12700+ struct super_block *h_sb __maybe_unused);
12701+ };
12702+ static const struct op a[] = {
4a4d8108
AM
12703+ [AuDy_AOP] = {
12704+ .sz = sizeof(struct au_dyaop),
b752ccd1 12705+ .set = dy_aop
4a4d8108 12706+ }
b752ccd1
AM
12707+ };
12708+ const struct op *p;
4a4d8108 12709+
8b6a4947
AM
12710+ hbl = dynop + op->dy_type;
12711+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12712+ if (key)
12713+ goto out_add; /* success */
12714+
12715+ p = a + op->dy_type;
12716+ key = kzalloc(p->sz, GFP_NOFS);
12717+ if (unlikely(!key)) {
12718+ key = ERR_PTR(-ENOMEM);
12719+ goto out;
12720+ }
12721+
12722+ key->dk_op.dy_hop = op->dy_hop;
12723+ kref_init(&key->dk_kref);
86dc4139 12724+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12725+ old = dy_gadd(hbl, key);
4a4d8108 12726+ if (old) {
1c60b727 12727+ kfree(key);
4a4d8108
AM
12728+ key = old;
12729+ }
12730+
12731+out_add:
12732+ old = dy_bradd(br, key);
12733+ if (old)
12734+ /* its ref-count should never be zero here */
12735+ kref_put(&key->dk_kref, dy_bug);
12736+out:
12737+ return key;
12738+}
12739+
12740+/* ---------------------------------------------------------------------- */
12741+/*
12742+ * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
c1595e42 12743+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12744+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12745+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12746+ * See the aufs manual in detail.
4a4d8108
AM
12747+ */
12748+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12749+{
7e9cd9fe 12750+ if (!do_dx)
4a4d8108 12751+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12752+ else
4a4d8108 12753+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12754+}
12755+
12756+static struct au_dyaop *dy_aget(struct au_branch *br,
12757+ const struct address_space_operations *h_aop,
12758+ int do_dx)
12759+{
12760+ struct au_dyaop *dyaop;
12761+ struct au_dynop op;
12762+
12763+ op.dy_type = AuDy_AOP;
12764+ op.dy_haop = h_aop;
12765+ dyaop = (void *)dy_get(&op, br);
12766+ if (IS_ERR(dyaop))
12767+ goto out;
12768+ dy_adx(dyaop, do_dx);
12769+
12770+out:
12771+ return dyaop;
12772+}
12773+
12774+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12775+ struct inode *h_inode)
12776+{
12777+ int err, do_dx;
12778+ struct super_block *sb;
12779+ struct au_branch *br;
12780+ struct au_dyaop *dyaop;
12781+
12782+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12783+ IiMustWriteLock(inode);
12784+
12785+ sb = inode->i_sb;
12786+ br = au_sbr(sb, bindex);
12787+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12788+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12789+ err = PTR_ERR(dyaop);
12790+ if (IS_ERR(dyaop))
12791+ /* unnecessary to call dy_fput() */
12792+ goto out;
12793+
12794+ err = 0;
12795+ inode->i_mapping->a_ops = &dyaop->da_op;
12796+
12797+out:
12798+ return err;
12799+}
12800+
b752ccd1
AM
12801+/*
12802+ * Is it safe to replace a_ops during the inode/file is in operation?
12803+ * Yes, I hope so.
12804+ */
12805+int au_dy_irefresh(struct inode *inode)
12806+{
12807+ int err;
5afbbe0d 12808+ aufs_bindex_t btop;
b752ccd1
AM
12809+ struct inode *h_inode;
12810+
12811+ err = 0;
12812+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12813+ btop = au_ibtop(inode);
12814+ h_inode = au_h_iptr(inode, btop);
12815+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12816+ }
12817+ return err;
12818+}
12819+
4a4d8108
AM
12820+void au_dy_arefresh(int do_dx)
12821+{
8b6a4947
AM
12822+ struct hlist_bl_head *hbl;
12823+ struct hlist_bl_node *pos;
4a4d8108
AM
12824+ struct au_dykey *key;
12825+
8b6a4947
AM
12826+ hbl = dynop + AuDy_AOP;
12827+ hlist_bl_lock(hbl);
12828+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12829+ dy_adx((void *)key, do_dx);
8b6a4947 12830+ hlist_bl_unlock(hbl);
4a4d8108
AM
12831+}
12832+
4a4d8108
AM
12833+/* ---------------------------------------------------------------------- */
12834+
12835+void __init au_dy_init(void)
12836+{
12837+ int i;
12838+
12839+ /* make sure that 'struct au_dykey *' can be any type */
12840+ BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
4a4d8108
AM
12841+
12842+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12843+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12844+}
12845+
12846+void au_dy_fin(void)
12847+{
12848+ int i;
12849+
12850+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12851+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12852+}
7f207e10
AM
12853diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12854--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
12855+++ linux/fs/aufs/dynop.h 2018-07-19 09:46:13.056647808 +0200
12856@@ -0,0 +1,75 @@
12857+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12858+/*
b00004a5 12859+ * Copyright (C) 2010-2018 Junjiro R. Okajima
4a4d8108
AM
12860+ *
12861+ * This program, aufs is free software; you can redistribute it and/or modify
12862+ * it under the terms of the GNU General Public License as published by
12863+ * the Free Software Foundation; either version 2 of the License, or
12864+ * (at your option) any later version.
12865+ *
12866+ * This program is distributed in the hope that it will be useful,
12867+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12868+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12869+ * GNU General Public License for more details.
12870+ *
12871+ * You should have received a copy of the GNU General Public License
523b37e3 12872+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12873+ */
12874+
12875+/*
12876+ * dynamically customizable operations (for regular files only)
12877+ */
12878+
12879+#ifndef __AUFS_DYNOP_H__
12880+#define __AUFS_DYNOP_H__
12881+
12882+#ifdef __KERNEL__
12883+
7e9cd9fe
AM
12884+#include <linux/fs.h>
12885+#include <linux/kref.h>
4a4d8108 12886+
2cbb1c4b 12887+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12888+
12889+struct au_dynop {
12890+ int dy_type;
12891+ union {
12892+ const void *dy_hop;
12893+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12894+ };
12895+};
12896+
12897+struct au_dykey {
12898+ union {
8b6a4947 12899+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12900+ struct rcu_head dk_rcu;
12901+ };
12902+ struct au_dynop dk_op;
12903+
12904+ /*
12905+ * during I am in the branch local array, kref is gotten. when the
12906+ * branch is removed, kref is put.
12907+ */
12908+ struct kref dk_kref;
12909+};
12910+
12911+/* stop unioning since their sizes are very different from each other */
12912+struct au_dyaop {
12913+ struct au_dykey da_key;
12914+ struct address_space_operations da_op; /* not const */
4a4d8108
AM
12915+};
12916+
4a4d8108
AM
12917+/* ---------------------------------------------------------------------- */
12918+
12919+/* dynop.c */
12920+struct au_branch;
12921+void au_dy_put(struct au_dykey *key);
12922+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12923+ struct inode *h_inode);
b752ccd1 12924+int au_dy_irefresh(struct inode *inode);
4a4d8108 12925+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12926+
12927+void __init au_dy_init(void);
12928+void au_dy_fin(void);
12929+
4a4d8108
AM
12930+#endif /* __KERNEL__ */
12931+#endif /* __AUFS_DYNOP_H__ */
7f207e10
AM
12932diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12933--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
12934+++ linux/fs/aufs/export.c 2018-07-19 09:46:13.056647808 +0200
12935@@ -0,0 +1,838 @@
12936+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12937+/*
b00004a5 12938+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
12939+ *
12940+ * This program, aufs is free software; you can redistribute it and/or modify
12941+ * it under the terms of the GNU General Public License as published by
12942+ * the Free Software Foundation; either version 2 of the License, or
12943+ * (at your option) any later version.
12944+ *
12945+ * This program is distributed in the hope that it will be useful,
12946+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12947+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12948+ * GNU General Public License for more details.
12949+ *
12950+ * You should have received a copy of the GNU General Public License
523b37e3 12951+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12952+ */
12953+
12954+/*
12955+ * export via nfs
12956+ */
12957+
12958+#include <linux/exportfs.h>
7eafdf33 12959+#include <linux/fs_struct.h>
4a4d8108
AM
12960+#include <linux/namei.h>
12961+#include <linux/nsproxy.h>
12962+#include <linux/random.h>
12963+#include <linux/writeback.h>
12964+#include "aufs.h"
12965+
12966+union conv {
12967+#ifdef CONFIG_AUFS_INO_T_64
12968+ __u32 a[2];
12969+#else
12970+ __u32 a[1];
12971+#endif
12972+ ino_t ino;
12973+};
12974+
12975+static ino_t decode_ino(__u32 *a)
12976+{
12977+ union conv u;
12978+
12979+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12980+ u.a[0] = a[0];
12981+#ifdef CONFIG_AUFS_INO_T_64
12982+ u.a[1] = a[1];
12983+#endif
12984+ return u.ino;
12985+}
12986+
12987+static void encode_ino(__u32 *a, ino_t ino)
12988+{
12989+ union conv u;
12990+
12991+ u.ino = ino;
12992+ a[0] = u.a[0];
12993+#ifdef CONFIG_AUFS_INO_T_64
12994+ a[1] = u.a[1];
12995+#endif
12996+}
12997+
12998+/* NFS file handle */
12999+enum {
13000+ Fh_br_id,
13001+ Fh_sigen,
13002+#ifdef CONFIG_AUFS_INO_T_64
13003+ /* support 64bit inode number */
13004+ Fh_ino1,
13005+ Fh_ino2,
13006+ Fh_dir_ino1,
13007+ Fh_dir_ino2,
13008+#else
13009+ Fh_ino1,
13010+ Fh_dir_ino1,
13011+#endif
13012+ Fh_igen,
13013+ Fh_h_type,
13014+ Fh_tail,
13015+
13016+ Fh_ino = Fh_ino1,
13017+ Fh_dir_ino = Fh_dir_ino1
13018+};
13019+
13020+static int au_test_anon(struct dentry *dentry)
13021+{
027c5e7a 13022+ /* note: read d_flags without d_lock */
4a4d8108
AM
13023+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
13024+}
13025+
a2a7ad62
AM
13026+int au_test_nfsd(void)
13027+{
13028+ int ret;
13029+ struct task_struct *tsk = current;
13030+ char comm[sizeof(tsk->comm)];
13031+
13032+ ret = 0;
13033+ if (tsk->flags & PF_KTHREAD) {
13034+ get_task_comm(comm, tsk);
13035+ ret = !strcmp(comm, "nfsd");
13036+ }
13037+
13038+ return ret;
13039+}
13040+
4a4d8108
AM
13041+/* ---------------------------------------------------------------------- */
13042+/* inode generation external table */
13043+
b752ccd1 13044+void au_xigen_inc(struct inode *inode)
4a4d8108 13045+{
4a4d8108
AM
13046+ loff_t pos;
13047+ ssize_t sz;
13048+ __u32 igen;
13049+ struct super_block *sb;
13050+ struct au_sbinfo *sbinfo;
13051+
4a4d8108 13052+ sb = inode->i_sb;
b752ccd1 13053+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 13054+
b752ccd1 13055+ sbinfo = au_sbi(sb);
1facf9fc 13056+ pos = inode->i_ino;
13057+ pos *= sizeof(igen);
13058+ igen = inode->i_generation + 1;
1facf9fc 13059+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
13060+ sizeof(igen), &pos);
13061+ if (sz == sizeof(igen))
b752ccd1 13062+ return; /* success */
1facf9fc 13063+
b752ccd1 13064+ if (unlikely(sz >= 0))
1facf9fc 13065+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 13066+}
13067+
13068+int au_xigen_new(struct inode *inode)
13069+{
13070+ int err;
13071+ loff_t pos;
13072+ ssize_t sz;
13073+ struct super_block *sb;
13074+ struct au_sbinfo *sbinfo;
13075+ struct file *file;
13076+
13077+ err = 0;
13078+ /* todo: dirty, at mount time */
13079+ if (inode->i_ino == AUFS_ROOT_INO)
13080+ goto out;
13081+ sb = inode->i_sb;
dece6358 13082+ SiMustAnyLock(sb);
1facf9fc 13083+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13084+ goto out;
13085+
13086+ err = -EFBIG;
13087+ pos = inode->i_ino;
13088+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13089+ AuIOErr1("too large i%lld\n", pos);
13090+ goto out;
13091+ }
13092+ pos *= sizeof(inode->i_generation);
13093+
13094+ err = 0;
13095+ sbinfo = au_sbi(sb);
13096+ file = sbinfo->si_xigen;
13097+ BUG_ON(!file);
13098+
c06a8ce3 13099+ if (vfsub_f_size_read(file)
1facf9fc 13100+ < pos + sizeof(inode->i_generation)) {
13101+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13102+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13103+ sizeof(inode->i_generation), &pos);
13104+ } else
13105+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13106+ sizeof(inode->i_generation), &pos);
13107+ if (sz == sizeof(inode->i_generation))
13108+ goto out; /* success */
13109+
13110+ err = sz;
13111+ if (unlikely(sz >= 0)) {
13112+ err = -EIO;
13113+ AuIOErr("xigen error (%zd)\n", sz);
13114+ }
13115+
4f0767ce 13116+out:
1facf9fc 13117+ return err;
13118+}
13119+
062440b3 13120+int au_xigen_set(struct super_block *sb, struct path *path)
1facf9fc 13121+{
13122+ int err;
13123+ struct au_sbinfo *sbinfo;
13124+ struct file *file;
13125+
dece6358
AM
13126+ SiMustWriteLock(sb);
13127+
1facf9fc 13128+ sbinfo = au_sbi(sb);
062440b3 13129+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
1facf9fc 13130+ err = PTR_ERR(file);
13131+ if (IS_ERR(file))
13132+ goto out;
13133+ err = 0;
13134+ if (sbinfo->si_xigen)
13135+ fput(sbinfo->si_xigen);
13136+ sbinfo->si_xigen = file;
13137+
4f0767ce 13138+out:
062440b3 13139+ AuTraceErr(err);
1facf9fc 13140+ return err;
13141+}
13142+
13143+void au_xigen_clr(struct super_block *sb)
13144+{
13145+ struct au_sbinfo *sbinfo;
13146+
dece6358
AM
13147+ SiMustWriteLock(sb);
13148+
1facf9fc 13149+ sbinfo = au_sbi(sb);
13150+ if (sbinfo->si_xigen) {
13151+ fput(sbinfo->si_xigen);
13152+ sbinfo->si_xigen = NULL;
13153+ }
13154+}
13155+
13156+/* ---------------------------------------------------------------------- */
13157+
13158+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13159+ ino_t dir_ino)
13160+{
13161+ struct dentry *dentry, *d;
13162+ struct inode *inode;
13163+ unsigned int sigen;
13164+
13165+ dentry = NULL;
13166+ inode = ilookup(sb, ino);
13167+ if (!inode)
13168+ goto out;
13169+
13170+ dentry = ERR_PTR(-ESTALE);
13171+ sigen = au_sigen(sb);
5afbbe0d 13172+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13173+ || IS_DEADDIR(inode)
537831f9 13174+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13175+ goto out_iput;
13176+
13177+ dentry = NULL;
13178+ if (!dir_ino || S_ISDIR(inode->i_mode))
13179+ dentry = d_find_alias(inode);
13180+ else {
027c5e7a 13181+ spin_lock(&inode->i_lock);
c1595e42 13182+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13183+ spin_lock(&d->d_lock);
1facf9fc 13184+ if (!au_test_anon(d)
5527c038 13185+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13186+ dentry = dget_dlock(d);
13187+ spin_unlock(&d->d_lock);
1facf9fc 13188+ break;
13189+ }
027c5e7a
AM
13190+ spin_unlock(&d->d_lock);
13191+ }
13192+ spin_unlock(&inode->i_lock);
1facf9fc 13193+ }
027c5e7a 13194+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13195+ /* need to refresh */
1facf9fc 13196+ dput(dentry);
2cbb1c4b 13197+ dentry = NULL;
1facf9fc 13198+ }
13199+
4f0767ce 13200+out_iput:
1facf9fc 13201+ iput(inode);
4f0767ce 13202+out:
2cbb1c4b 13203+ AuTraceErrPtr(dentry);
1facf9fc 13204+ return dentry;
13205+}
13206+
13207+/* ---------------------------------------------------------------------- */
13208+
13209+/* todo: dirty? */
13210+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13211+
13212+struct au_compare_mnt_args {
13213+ /* input */
13214+ struct super_block *sb;
13215+
13216+ /* output */
13217+ struct vfsmount *mnt;
13218+};
13219+
13220+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13221+{
13222+ struct au_compare_mnt_args *a = arg;
13223+
13224+ if (mnt->mnt_sb != a->sb)
13225+ return 0;
13226+ a->mnt = mntget(mnt);
13227+ return 1;
13228+}
13229+
1facf9fc 13230+static struct vfsmount *au_mnt_get(struct super_block *sb)
13231+{
4a4d8108 13232+ int err;
7eafdf33 13233+ struct path root;
4a4d8108
AM
13234+ struct au_compare_mnt_args args = {
13235+ .sb = sb
13236+ };
1facf9fc 13237+
7eafdf33 13238+ get_fs_root(current->fs, &root);
523b37e3 13239+ rcu_read_lock();
7eafdf33 13240+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13241+ rcu_read_unlock();
7eafdf33 13242+ path_put(&root);
4a4d8108
AM
13243+ AuDebugOn(!err);
13244+ AuDebugOn(!args.mnt);
13245+ return args.mnt;
1facf9fc 13246+}
13247+
13248+struct au_nfsd_si_lock {
4a4d8108 13249+ unsigned int sigen;
027c5e7a 13250+ aufs_bindex_t bindex, br_id;
1facf9fc 13251+ unsigned char force_lock;
13252+};
13253+
027c5e7a
AM
13254+static int si_nfsd_read_lock(struct super_block *sb,
13255+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13256+{
027c5e7a 13257+ int err;
1facf9fc 13258+ aufs_bindex_t bindex;
13259+
13260+ si_read_lock(sb, AuLock_FLUSH);
13261+
13262+ /* branch id may be wrapped around */
027c5e7a 13263+ err = 0;
1facf9fc 13264+ bindex = au_br_index(sb, nsi_lock->br_id);
13265+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13266+ goto out; /* success */
13267+
027c5e7a
AM
13268+ err = -ESTALE;
13269+ bindex = -1;
1facf9fc 13270+ if (!nsi_lock->force_lock)
13271+ si_read_unlock(sb);
1facf9fc 13272+
4f0767ce 13273+out:
027c5e7a
AM
13274+ nsi_lock->bindex = bindex;
13275+ return err;
1facf9fc 13276+}
13277+
13278+struct find_name_by_ino {
392086de 13279+ struct dir_context ctx;
1facf9fc 13280+ int called, found;
13281+ ino_t ino;
13282+ char *name;
13283+ int namelen;
13284+};
13285+
13286+static int
392086de
AM
13287+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13288+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13289+{
392086de
AM
13290+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13291+ ctx);
1facf9fc 13292+
13293+ a->called++;
13294+ if (a->ino != ino)
13295+ return 0;
13296+
13297+ memcpy(a->name, name, namelen);
13298+ a->namelen = namelen;
13299+ a->found = 1;
13300+ return 1;
13301+}
13302+
13303+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13304+ struct au_nfsd_si_lock *nsi_lock)
13305+{
13306+ struct dentry *dentry, *parent;
13307+ struct file *file;
13308+ struct inode *dir;
392086de
AM
13309+ struct find_name_by_ino arg = {
13310+ .ctx = {
2000de60 13311+ .actor = find_name_by_ino
392086de
AM
13312+ }
13313+ };
1facf9fc 13314+ int err;
13315+
13316+ parent = path->dentry;
13317+ if (nsi_lock)
13318+ si_read_unlock(parent->d_sb);
4a4d8108 13319+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13320+ dentry = (void *)file;
13321+ if (IS_ERR(file))
13322+ goto out;
13323+
13324+ dentry = ERR_PTR(-ENOMEM);
537831f9 13325+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13326+ if (unlikely(!arg.name))
13327+ goto out_file;
13328+ arg.ino = ino;
13329+ arg.found = 0;
13330+ do {
13331+ arg.called = 0;
13332+ /* smp_mb(); */
392086de 13333+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13334+ } while (!err && !arg.found && arg.called);
13335+ dentry = ERR_PTR(err);
13336+ if (unlikely(err))
13337+ goto out_name;
1716fcea
AM
13338+ /* instead of ENOENT */
13339+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13340+ if (!arg.found)
13341+ goto out_name;
13342+
b4510431 13343+ /* do not call vfsub_lkup_one() */
5527c038 13344+ dir = d_inode(parent);
febd17d6 13345+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13346+ AuTraceErrPtr(dentry);
13347+ if (IS_ERR(dentry))
13348+ goto out_name;
13349+ AuDebugOn(au_test_anon(dentry));
5527c038 13350+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13351+ dput(dentry);
13352+ dentry = ERR_PTR(-ENOENT);
13353+ }
13354+
4f0767ce 13355+out_name:
1c60b727 13356+ free_page((unsigned long)arg.name);
4f0767ce 13357+out_file:
1facf9fc 13358+ fput(file);
4f0767ce 13359+out:
1facf9fc 13360+ if (unlikely(nsi_lock
13361+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13362+ if (!IS_ERR(dentry)) {
13363+ dput(dentry);
13364+ dentry = ERR_PTR(-ESTALE);
13365+ }
13366+ AuTraceErrPtr(dentry);
13367+ return dentry;
13368+}
13369+
13370+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13371+ ino_t dir_ino,
13372+ struct au_nfsd_si_lock *nsi_lock)
13373+{
13374+ struct dentry *dentry;
13375+ struct path path;
13376+
13377+ if (dir_ino != AUFS_ROOT_INO) {
13378+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13379+ dentry = path.dentry;
13380+ if (!path.dentry || IS_ERR(path.dentry))
13381+ goto out;
13382+ AuDebugOn(au_test_anon(path.dentry));
13383+ } else
13384+ path.dentry = dget(sb->s_root);
13385+
13386+ path.mnt = au_mnt_get(sb);
13387+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13388+ path_put(&path);
13389+
4f0767ce 13390+out:
1facf9fc 13391+ AuTraceErrPtr(dentry);
13392+ return dentry;
13393+}
13394+
13395+/* ---------------------------------------------------------------------- */
13396+
13397+static int h_acceptable(void *expv, struct dentry *dentry)
13398+{
13399+ return 1;
13400+}
13401+
13402+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13403+ char *buf, int len, struct super_block *sb)
13404+{
13405+ char *p;
13406+ int n;
13407+ struct path path;
13408+
13409+ p = d_path(h_rootpath, buf, len);
13410+ if (IS_ERR(p))
13411+ goto out;
13412+ n = strlen(p);
13413+
13414+ path.mnt = h_rootpath->mnt;
13415+ path.dentry = h_parent;
13416+ p = d_path(&path, buf, len);
13417+ if (IS_ERR(p))
13418+ goto out;
13419+ if (n != 1)
13420+ p += n;
13421+
13422+ path.mnt = au_mnt_get(sb);
13423+ path.dentry = sb->s_root;
13424+ p = d_path(&path, buf, len - strlen(p));
13425+ mntput(path.mnt);
13426+ if (IS_ERR(p))
13427+ goto out;
13428+ if (n != 1)
13429+ p[strlen(p)] = '/';
13430+
4f0767ce 13431+out:
1facf9fc 13432+ AuTraceErrPtr(p);
13433+ return p;
13434+}
13435+
13436+static
027c5e7a
AM
13437+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13438+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13439+{
13440+ struct dentry *dentry, *h_parent, *root;
13441+ struct super_block *h_sb;
13442+ char *pathname, *p;
13443+ struct vfsmount *h_mnt;
13444+ struct au_branch *br;
13445+ int err;
13446+ struct path path;
13447+
027c5e7a 13448+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13449+ h_mnt = au_br_mnt(br);
1facf9fc 13450+ h_sb = h_mnt->mnt_sb;
13451+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13452+ lockdep_off();
1facf9fc 13453+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13454+ fh_len - Fh_tail, fh[Fh_h_type],
13455+ h_acceptable, /*context*/NULL);
5afbbe0d 13456+ lockdep_on();
1facf9fc 13457+ dentry = h_parent;
13458+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13459+ AuWarn1("%s decode_fh failed, %ld\n",
13460+ au_sbtype(h_sb), PTR_ERR(h_parent));
13461+ goto out;
13462+ }
13463+ dentry = NULL;
13464+ if (unlikely(au_test_anon(h_parent))) {
13465+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13466+ au_sbtype(h_sb));
13467+ goto out_h_parent;
13468+ }
13469+
13470+ dentry = ERR_PTR(-ENOMEM);
13471+ pathname = (void *)__get_free_page(GFP_NOFS);
13472+ if (unlikely(!pathname))
13473+ goto out_h_parent;
13474+
13475+ root = sb->s_root;
13476+ path.mnt = h_mnt;
13477+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13478+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13479+ di_read_unlock(root, !AuLock_IR);
13480+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13481+ dentry = (void *)p;
13482+ if (IS_ERR(p))
13483+ goto out_pathname;
13484+
13485+ si_read_unlock(sb);
13486+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13487+ dentry = ERR_PTR(err);
13488+ if (unlikely(err))
13489+ goto out_relock;
13490+
13491+ dentry = ERR_PTR(-ENOENT);
13492+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13493+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13494+ goto out_path;
13495+
5527c038 13496+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13497+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13498+ else
13499+ dentry = dget(path.dentry);
13500+
4f0767ce 13501+out_path:
1facf9fc 13502+ path_put(&path);
4f0767ce 13503+out_relock:
1facf9fc 13504+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13505+ if (!IS_ERR(dentry)) {
13506+ dput(dentry);
13507+ dentry = ERR_PTR(-ESTALE);
13508+ }
4f0767ce 13509+out_pathname:
1c60b727 13510+ free_page((unsigned long)pathname);
4f0767ce 13511+out_h_parent:
1facf9fc 13512+ dput(h_parent);
4f0767ce 13513+out:
1facf9fc 13514+ AuTraceErrPtr(dentry);
13515+ return dentry;
13516+}
13517+
13518+/* ---------------------------------------------------------------------- */
13519+
13520+static struct dentry *
13521+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13522+ int fh_type)
13523+{
13524+ struct dentry *dentry;
13525+ __u32 *fh = fid->raw;
027c5e7a 13526+ struct au_branch *br;
1facf9fc 13527+ ino_t ino, dir_ino;
1facf9fc 13528+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13529+ .force_lock = 0
13530+ };
13531+
1facf9fc 13532+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13533+ /* it should never happen, but the file handle is unreliable */
13534+ if (unlikely(fh_len < Fh_tail))
13535+ goto out;
13536+ nsi_lock.sigen = fh[Fh_sigen];
13537+ nsi_lock.br_id = fh[Fh_br_id];
13538+
1facf9fc 13539+ /* branch id may be wrapped around */
027c5e7a
AM
13540+ br = NULL;
13541+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13542+ goto out;
13543+ nsi_lock.force_lock = 1;
13544+
13545+ /* is this inode still cached? */
13546+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13547+ /* it should never happen */
13548+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13549+ goto out_unlock;
4a4d8108 13550+
1facf9fc 13551+ dir_ino = decode_ino(fh + Fh_dir_ino);
13552+ dentry = decode_by_ino(sb, ino, dir_ino);
13553+ if (IS_ERR(dentry))
13554+ goto out_unlock;
13555+ if (dentry)
13556+ goto accept;
13557+
13558+ /* is the parent dir cached? */
027c5e7a 13559+ br = au_sbr(sb, nsi_lock.bindex);
5afbbe0d 13560+ au_br_get(br);
1facf9fc 13561+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13562+ if (IS_ERR(dentry))
13563+ goto out_unlock;
13564+ if (dentry)
13565+ goto accept;
13566+
13567+ /* lookup path */
027c5e7a 13568+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13569+ if (IS_ERR(dentry))
13570+ goto out_unlock;
13571+ if (unlikely(!dentry))
13572+ /* todo?: make it ESTALE */
13573+ goto out_unlock;
13574+
4f0767ce 13575+accept:
027c5e7a 13576+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13577+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13578+ goto out_unlock; /* success */
13579+
13580+ dput(dentry);
13581+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13582+out_unlock:
027c5e7a 13583+ if (br)
5afbbe0d 13584+ au_br_put(br);
1facf9fc 13585+ si_read_unlock(sb);
4f0767ce 13586+out:
1facf9fc 13587+ AuTraceErrPtr(dentry);
13588+ return dentry;
13589+}
13590+
13591+#if 0 /* reserved for future use */
13592+/* support subtreecheck option */
13593+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13594+ int fh_len, int fh_type)
13595+{
13596+ struct dentry *parent;
13597+ __u32 *fh = fid->raw;
13598+ ino_t dir_ino;
13599+
13600+ dir_ino = decode_ino(fh + Fh_dir_ino);
13601+ parent = decode_by_ino(sb, dir_ino, 0);
13602+ if (IS_ERR(parent))
13603+ goto out;
13604+ if (!parent)
13605+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13606+ dir_ino, fh, fh_len);
13607+
4f0767ce 13608+out:
1facf9fc 13609+ AuTraceErrPtr(parent);
13610+ return parent;
13611+}
13612+#endif
13613+
13614+/* ---------------------------------------------------------------------- */
13615+
0c3ec466
AM
13616+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13617+ struct inode *dir)
1facf9fc 13618+{
13619+ int err;
0c3ec466 13620+ aufs_bindex_t bindex;
1facf9fc 13621+ struct super_block *sb, *h_sb;
0c3ec466
AM
13622+ struct dentry *dentry, *parent, *h_parent;
13623+ struct inode *h_dir;
1facf9fc 13624+ struct au_branch *br;
13625+
1facf9fc 13626+ err = -ENOSPC;
13627+ if (unlikely(*max_len <= Fh_tail)) {
13628+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13629+ goto out;
13630+ }
13631+
13632+ err = FILEID_ROOT;
0c3ec466
AM
13633+ if (inode->i_ino == AUFS_ROOT_INO) {
13634+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13635+ goto out;
13636+ }
13637+
1facf9fc 13638+ h_parent = NULL;
0c3ec466
AM
13639+ sb = inode->i_sb;
13640+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13641+ if (unlikely(err))
13642+ goto out;
13643+
1facf9fc 13644+#ifdef CONFIG_AUFS_DEBUG
13645+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13646+ AuWarn1("NFS-exporting requires xino\n");
13647+#endif
027c5e7a 13648+ err = -EIO;
0c3ec466
AM
13649+ parent = NULL;
13650+ ii_read_lock_child(inode);
5afbbe0d 13651+ bindex = au_ibtop(inode);
0c3ec466 13652+ if (!dir) {
c1595e42 13653+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13654+ if (unlikely(!dentry))
13655+ goto out_unlock;
13656+ AuDebugOn(au_test_anon(dentry));
13657+ parent = dget_parent(dentry);
13658+ dput(dentry);
13659+ if (unlikely(!parent))
13660+ goto out_unlock;
5527c038
JR
13661+ if (d_really_is_positive(parent))
13662+ dir = d_inode(parent);
1facf9fc 13663+ }
0c3ec466
AM
13664+
13665+ ii_read_lock_parent(dir);
13666+ h_dir = au_h_iptr(dir, bindex);
13667+ ii_read_unlock(dir);
13668+ if (unlikely(!h_dir))
13669+ goto out_parent;
c1595e42 13670+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13671+ if (unlikely(!h_parent))
0c3ec466 13672+ goto out_hparent;
1facf9fc 13673+
13674+ err = -EPERM;
13675+ br = au_sbr(sb, bindex);
86dc4139 13676+ h_sb = au_br_sb(br);
1facf9fc 13677+ if (unlikely(!h_sb->s_export_op)) {
13678+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13679+ goto out_hparent;
1facf9fc 13680+ }
13681+
13682+ fh[Fh_br_id] = br->br_id;
13683+ fh[Fh_sigen] = au_sigen(sb);
13684+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13685+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13686+ fh[Fh_igen] = inode->i_generation;
13687+
13688+ *max_len -= Fh_tail;
13689+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13690+ max_len,
13691+ /*connectable or subtreecheck*/0);
13692+ err = fh[Fh_h_type];
13693+ *max_len += Fh_tail;
13694+ /* todo: macros? */
1716fcea 13695+ if (err != FILEID_INVALID)
1facf9fc 13696+ err = 99;
13697+ else
13698+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13699+
0c3ec466 13700+out_hparent:
1facf9fc 13701+ dput(h_parent);
0c3ec466 13702+out_parent:
1facf9fc 13703+ dput(parent);
0c3ec466
AM
13704+out_unlock:
13705+ ii_read_unlock(inode);
13706+ si_read_unlock(sb);
4f0767ce 13707+out:
1facf9fc 13708+ if (unlikely(err < 0))
1716fcea 13709+ err = FILEID_INVALID;
1facf9fc 13710+ return err;
13711+}
13712+
13713+/* ---------------------------------------------------------------------- */
13714+
4a4d8108
AM
13715+static int aufs_commit_metadata(struct inode *inode)
13716+{
13717+ int err;
13718+ aufs_bindex_t bindex;
13719+ struct super_block *sb;
13720+ struct inode *h_inode;
13721+ int (*f)(struct inode *inode);
13722+
13723+ sb = inode->i_sb;
e49829fe 13724+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13725+ ii_write_lock_child(inode);
5afbbe0d 13726+ bindex = au_ibtop(inode);
4a4d8108
AM
13727+ AuDebugOn(bindex < 0);
13728+ h_inode = au_h_iptr(inode, bindex);
13729+
13730+ f = h_inode->i_sb->s_export_op->commit_metadata;
13731+ if (f)
13732+ err = f(h_inode);
13733+ else {
13734+ struct writeback_control wbc = {
13735+ .sync_mode = WB_SYNC_ALL,
13736+ .nr_to_write = 0 /* metadata only */
13737+ };
13738+
13739+ err = sync_inode(h_inode, &wbc);
13740+ }
13741+
13742+ au_cpup_attr_timesizes(inode);
13743+ ii_write_unlock(inode);
13744+ si_read_unlock(sb);
13745+ return err;
13746+}
13747+
13748+/* ---------------------------------------------------------------------- */
13749+
1facf9fc 13750+static struct export_operations aufs_export_op = {
4a4d8108 13751+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13752+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13753+ .encode_fh = aufs_encode_fh,
13754+ .commit_metadata = aufs_commit_metadata
1facf9fc 13755+};
13756+
13757+void au_export_init(struct super_block *sb)
13758+{
13759+ struct au_sbinfo *sbinfo;
13760+ __u32 u;
13761+
5afbbe0d
AM
13762+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13763+ && IS_MODULE(CONFIG_EXPORTFS),
13764+ AUFS_NAME ": unsupported configuration "
13765+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13766+
1facf9fc 13767+ sb->s_export_op = &aufs_export_op;
13768+ sbinfo = au_sbi(sb);
13769+ sbinfo->si_xigen = NULL;
13770+ get_random_bytes(&u, sizeof(u));
13771+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13772+ atomic_set(&sbinfo->si_xigen_next, u);
13773+}
076b876e
AM
13774diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13775--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
13776+++ linux/fs/aufs/fhsm.c 2018-07-19 09:46:13.056647808 +0200
13777@@ -0,0 +1,427 @@
13778+/* SPDX-License-Identifier: GPL-2.0 */
076b876e 13779+/*
b00004a5 13780+ * Copyright (C) 2011-2018 Junjiro R. Okajima
076b876e
AM
13781+ *
13782+ * This program, aufs is free software; you can redistribute it and/or modify
13783+ * it under the terms of the GNU General Public License as published by
13784+ * the Free Software Foundation; either version 2 of the License, or
13785+ * (at your option) any later version.
13786+ *
13787+ * This program is distributed in the hope that it will be useful,
13788+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13789+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13790+ * GNU General Public License for more details.
13791+ *
13792+ * You should have received a copy of the GNU General Public License
13793+ * along with this program; if not, write to the Free Software
13794+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13795+ */
13796+
13797+/*
13798+ * File-based Hierarchy Storage Management
13799+ */
13800+
13801+#include <linux/anon_inodes.h>
13802+#include <linux/poll.h>
13803+#include <linux/seq_file.h>
13804+#include <linux/statfs.h>
13805+#include "aufs.h"
13806+
c1595e42
JR
13807+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13808+{
13809+ struct au_sbinfo *sbinfo;
13810+ struct au_fhsm *fhsm;
13811+
13812+ SiMustAnyLock(sb);
13813+
13814+ sbinfo = au_sbi(sb);
13815+ fhsm = &sbinfo->si_fhsm;
13816+ AuDebugOn(!fhsm);
13817+ return fhsm->fhsm_bottom;
13818+}
13819+
13820+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13821+{
13822+ struct au_sbinfo *sbinfo;
13823+ struct au_fhsm *fhsm;
13824+
13825+ SiMustWriteLock(sb);
13826+
13827+ sbinfo = au_sbi(sb);
13828+ fhsm = &sbinfo->si_fhsm;
13829+ AuDebugOn(!fhsm);
13830+ fhsm->fhsm_bottom = bindex;
13831+}
13832+
13833+/* ---------------------------------------------------------------------- */
13834+
076b876e
AM
13835+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13836+{
13837+ struct au_br_fhsm *bf;
13838+
13839+ bf = br->br_fhsm;
13840+ MtxMustLock(&bf->bf_lock);
13841+
13842+ return !bf->bf_readable
13843+ || time_after(jiffies,
13844+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13845+}
13846+
13847+/* ---------------------------------------------------------------------- */
13848+
13849+static void au_fhsm_notify(struct super_block *sb, int val)
13850+{
13851+ struct au_sbinfo *sbinfo;
13852+ struct au_fhsm *fhsm;
13853+
13854+ SiMustAnyLock(sb);
13855+
13856+ sbinfo = au_sbi(sb);
13857+ fhsm = &sbinfo->si_fhsm;
13858+ if (au_fhsm_pid(fhsm)
13859+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13860+ atomic_set(&fhsm->fhsm_readable, val);
13861+ if (val)
13862+ wake_up(&fhsm->fhsm_wqh);
13863+ }
13864+}
13865+
13866+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13867+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13868+{
13869+ int err;
13870+ struct au_branch *br;
13871+ struct au_br_fhsm *bf;
13872+
13873+ br = au_sbr(sb, bindex);
13874+ AuDebugOn(au_br_rdonly(br));
13875+ bf = br->br_fhsm;
13876+ AuDebugOn(!bf);
13877+
13878+ if (do_lock)
13879+ mutex_lock(&bf->bf_lock);
13880+ else
13881+ MtxMustLock(&bf->bf_lock);
13882+
13883+ /* sb->s_root for NFS is unreliable */
13884+ err = au_br_stfs(br, &bf->bf_stfs);
13885+ if (unlikely(err)) {
13886+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13887+ goto out;
13888+ }
13889+
13890+ bf->bf_jiffy = jiffies;
13891+ bf->bf_readable = 1;
13892+ if (do_notify)
13893+ au_fhsm_notify(sb, /*val*/1);
13894+ if (rstfs)
13895+ *rstfs = bf->bf_stfs;
13896+
13897+out:
13898+ if (do_lock)
13899+ mutex_unlock(&bf->bf_lock);
13900+ au_fhsm_notify(sb, /*val*/1);
13901+
13902+ return err;
13903+}
13904+
13905+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13906+{
13907+ int err;
076b876e
AM
13908+ struct au_sbinfo *sbinfo;
13909+ struct au_fhsm *fhsm;
13910+ struct au_branch *br;
13911+ struct au_br_fhsm *bf;
13912+
13913+ AuDbg("b%d, force %d\n", bindex, force);
13914+ SiMustAnyLock(sb);
13915+
13916+ sbinfo = au_sbi(sb);
13917+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13918+ if (!au_ftest_si(sbinfo, FHSM)
13919+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13920+ return;
13921+
13922+ br = au_sbr(sb, bindex);
13923+ bf = br->br_fhsm;
13924+ AuDebugOn(!bf);
13925+ mutex_lock(&bf->bf_lock);
13926+ if (force
13927+ || au_fhsm_pid(fhsm)
13928+ || au_fhsm_test_jiffy(sbinfo, br))
13929+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13930+ /*do_notify*/1);
13931+ mutex_unlock(&bf->bf_lock);
13932+}
13933+
13934+void au_fhsm_wrote_all(struct super_block *sb, int force)
13935+{
5afbbe0d 13936+ aufs_bindex_t bindex, bbot;
076b876e
AM
13937+ struct au_branch *br;
13938+
13939+ /* exclude the bottom */
5afbbe0d
AM
13940+ bbot = au_fhsm_bottom(sb);
13941+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13942+ br = au_sbr(sb, bindex);
13943+ if (au_br_fhsm(br->br_perm))
13944+ au_fhsm_wrote(sb, bindex, force);
13945+ }
13946+}
13947+
13948+/* ---------------------------------------------------------------------- */
13949+
be118d29 13950+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13951+{
be118d29 13952+ __poll_t mask;
076b876e
AM
13953+ struct au_sbinfo *sbinfo;
13954+ struct au_fhsm *fhsm;
13955+
13956+ mask = 0;
13957+ sbinfo = file->private_data;
13958+ fhsm = &sbinfo->si_fhsm;
13959+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13960+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13961+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13962+
b00004a5
AM
13963+ if (!mask)
13964+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13965+ return mask;
13966+}
13967+
13968+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13969+ struct aufs_stfs *stfs, __s16 brid)
13970+{
13971+ int err;
13972+
13973+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13974+ if (!err)
13975+ err = __put_user(brid, &stbr->brid);
13976+ if (unlikely(err))
13977+ err = -EFAULT;
13978+
13979+ return err;
13980+}
13981+
13982+static ssize_t au_fhsm_do_read(struct super_block *sb,
13983+ struct aufs_stbr __user *stbr, size_t count)
13984+{
13985+ ssize_t err;
13986+ int nstbr;
5afbbe0d 13987+ aufs_bindex_t bindex, bbot;
076b876e
AM
13988+ struct au_branch *br;
13989+ struct au_br_fhsm *bf;
13990+
13991+ /* except the bottom branch */
13992+ err = 0;
13993+ nstbr = 0;
5afbbe0d
AM
13994+ bbot = au_fhsm_bottom(sb);
13995+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
13996+ br = au_sbr(sb, bindex);
13997+ if (!au_br_fhsm(br->br_perm))
13998+ continue;
13999+
14000+ bf = br->br_fhsm;
14001+ mutex_lock(&bf->bf_lock);
14002+ if (bf->bf_readable) {
14003+ err = -EFAULT;
14004+ if (count >= sizeof(*stbr))
14005+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
14006+ br->br_id);
14007+ if (!err) {
14008+ bf->bf_readable = 0;
14009+ count -= sizeof(*stbr);
14010+ nstbr++;
14011+ }
14012+ }
14013+ mutex_unlock(&bf->bf_lock);
14014+ }
14015+ if (!err)
14016+ err = sizeof(*stbr) * nstbr;
14017+
14018+ return err;
14019+}
14020+
14021+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
14022+ loff_t *pos)
14023+{
14024+ ssize_t err;
14025+ int readable;
5afbbe0d 14026+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
14027+ struct au_sbinfo *sbinfo;
14028+ struct au_fhsm *fhsm;
14029+ struct au_branch *br;
14030+ struct super_block *sb;
14031+
14032+ err = 0;
14033+ sbinfo = file->private_data;
14034+ fhsm = &sbinfo->si_fhsm;
14035+need_data:
14036+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
14037+ if (!atomic_read(&fhsm->fhsm_readable)) {
14038+ if (vfsub_file_flags(file) & O_NONBLOCK)
14039+ err = -EAGAIN;
14040+ else
14041+ err = wait_event_interruptible_locked_irq
14042+ (fhsm->fhsm_wqh,
14043+ atomic_read(&fhsm->fhsm_readable));
14044+ }
14045+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
14046+ if (unlikely(err))
14047+ goto out;
14048+
14049+ /* sb may already be dead */
14050+ au_rw_read_lock(&sbinfo->si_rwsem);
14051+ readable = atomic_read(&fhsm->fhsm_readable);
14052+ if (readable > 0) {
14053+ sb = sbinfo->si_sb;
14054+ AuDebugOn(!sb);
14055+ /* exclude the bottom branch */
14056+ nfhsm = 0;
5afbbe0d
AM
14057+ bbot = au_fhsm_bottom(sb);
14058+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
14059+ br = au_sbr(sb, bindex);
14060+ if (au_br_fhsm(br->br_perm))
14061+ nfhsm++;
14062+ }
14063+ err = -EMSGSIZE;
14064+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
14065+ atomic_set(&fhsm->fhsm_readable, 0);
14066+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14067+ count);
14068+ }
14069+ }
14070+ au_rw_read_unlock(&sbinfo->si_rwsem);
14071+ if (!readable)
14072+ goto need_data;
14073+
14074+out:
14075+ return err;
14076+}
14077+
14078+static int au_fhsm_release(struct inode *inode, struct file *file)
14079+{
14080+ struct au_sbinfo *sbinfo;
14081+ struct au_fhsm *fhsm;
14082+
14083+ /* sb may already be dead */
14084+ sbinfo = file->private_data;
14085+ fhsm = &sbinfo->si_fhsm;
14086+ spin_lock(&fhsm->fhsm_spin);
14087+ fhsm->fhsm_pid = 0;
14088+ spin_unlock(&fhsm->fhsm_spin);
14089+ kobject_put(&sbinfo->si_kobj);
14090+
14091+ return 0;
14092+}
14093+
14094+static const struct file_operations au_fhsm_fops = {
14095+ .owner = THIS_MODULE,
14096+ .llseek = noop_llseek,
14097+ .read = au_fhsm_read,
14098+ .poll = au_fhsm_poll,
14099+ .release = au_fhsm_release
14100+};
14101+
14102+int au_fhsm_fd(struct super_block *sb, int oflags)
14103+{
14104+ int err, fd;
14105+ struct au_sbinfo *sbinfo;
14106+ struct au_fhsm *fhsm;
14107+
14108+ err = -EPERM;
14109+ if (unlikely(!capable(CAP_SYS_ADMIN)))
14110+ goto out;
14111+
14112+ err = -EINVAL;
14113+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14114+ goto out;
14115+
14116+ err = 0;
14117+ sbinfo = au_sbi(sb);
14118+ fhsm = &sbinfo->si_fhsm;
14119+ spin_lock(&fhsm->fhsm_spin);
14120+ if (!fhsm->fhsm_pid)
14121+ fhsm->fhsm_pid = current->pid;
14122+ else
14123+ err = -EBUSY;
14124+ spin_unlock(&fhsm->fhsm_spin);
14125+ if (unlikely(err))
14126+ goto out;
14127+
14128+ oflags |= O_RDONLY;
14129+ /* oflags |= FMODE_NONOTIFY; */
14130+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14131+ err = fd;
14132+ if (unlikely(fd < 0))
14133+ goto out_pid;
14134+
14135+ /* succeed reglardless 'fhsm' status */
14136+ kobject_get(&sbinfo->si_kobj);
14137+ si_noflush_read_lock(sb);
14138+ if (au_ftest_si(sbinfo, FHSM))
14139+ au_fhsm_wrote_all(sb, /*force*/0);
14140+ si_read_unlock(sb);
14141+ goto out; /* success */
14142+
14143+out_pid:
14144+ spin_lock(&fhsm->fhsm_spin);
14145+ fhsm->fhsm_pid = 0;
14146+ spin_unlock(&fhsm->fhsm_spin);
14147+out:
14148+ AuTraceErr(err);
14149+ return err;
14150+}
14151+
14152+/* ---------------------------------------------------------------------- */
14153+
14154+int au_fhsm_br_alloc(struct au_branch *br)
14155+{
14156+ int err;
14157+
14158+ err = 0;
14159+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14160+ if (br->br_fhsm)
14161+ au_br_fhsm_init(br->br_fhsm);
14162+ else
14163+ err = -ENOMEM;
14164+
14165+ return err;
14166+}
14167+
14168+/* ---------------------------------------------------------------------- */
14169+
14170+void au_fhsm_fin(struct super_block *sb)
14171+{
14172+ au_fhsm_notify(sb, /*val*/-1);
14173+}
14174+
14175+void au_fhsm_init(struct au_sbinfo *sbinfo)
14176+{
14177+ struct au_fhsm *fhsm;
14178+
14179+ fhsm = &sbinfo->si_fhsm;
14180+ spin_lock_init(&fhsm->fhsm_spin);
14181+ init_waitqueue_head(&fhsm->fhsm_wqh);
14182+ atomic_set(&fhsm->fhsm_readable, 0);
14183+ fhsm->fhsm_expire
14184+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14185+ fhsm->fhsm_bottom = -1;
076b876e
AM
14186+}
14187+
14188+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14189+{
14190+ sbinfo->si_fhsm.fhsm_expire
14191+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14192+}
14193+
14194+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14195+{
14196+ unsigned int u;
14197+
14198+ if (!au_ftest_si(sbinfo, FHSM))
14199+ return;
14200+
14201+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14202+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14203+ seq_printf(seq, ",fhsm_sec=%u", u);
14204+}
7f207e10
AM
14205diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14206--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
14207+++ linux/fs/aufs/file.c 2018-07-19 09:46:13.056647808 +0200
14208@@ -0,0 +1,857 @@
14209+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 14210+/*
b00004a5 14211+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 14212+ *
14213+ * This program, aufs is free software; you can redistribute it and/or modify
14214+ * it under the terms of the GNU General Public License as published by
14215+ * the Free Software Foundation; either version 2 of the License, or
14216+ * (at your option) any later version.
dece6358
AM
14217+ *
14218+ * This program is distributed in the hope that it will be useful,
14219+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14220+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14221+ * GNU General Public License for more details.
14222+ *
14223+ * You should have received a copy of the GNU General Public License
523b37e3 14224+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14225+ */
14226+
14227+/*
4a4d8108 14228+ * handling file/dir, and address_space operation
1facf9fc 14229+ */
14230+
7eafdf33
AM
14231+#ifdef CONFIG_AUFS_DEBUG
14232+#include <linux/migrate.h>
14233+#endif
4a4d8108 14234+#include <linux/pagemap.h>
1facf9fc 14235+#include "aufs.h"
14236+
4a4d8108
AM
14237+/* drop flags for writing */
14238+unsigned int au_file_roflags(unsigned int flags)
14239+{
14240+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14241+ flags |= O_RDONLY | O_NOATIME;
14242+ return flags;
14243+}
14244+
14245+/* common functions to regular file and dir */
14246+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14247+ struct file *file, int force_wr)
1facf9fc 14248+{
1308ab2a 14249+ struct file *h_file;
4a4d8108
AM
14250+ struct dentry *h_dentry;
14251+ struct inode *h_inode;
14252+ struct super_block *sb;
14253+ struct au_branch *br;
14254+ struct path h_path;
b912730e 14255+ int err;
1facf9fc 14256+
4a4d8108
AM
14257+ /* a race condition can happen between open and unlink/rmdir */
14258+ h_file = ERR_PTR(-ENOENT);
14259+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14260+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14261+ goto out;
5527c038 14262+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14263+ spin_lock(&h_dentry->d_lock);
14264+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14265+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14266+ ;
14267+ spin_unlock(&h_dentry->d_lock);
14268+ if (unlikely(err))
4a4d8108 14269+ goto out;
1facf9fc 14270+
4a4d8108
AM
14271+ sb = dentry->d_sb;
14272+ br = au_sbr(sb, bindex);
b912730e
AM
14273+ err = au_br_test_oflag(flags, br);
14274+ h_file = ERR_PTR(err);
14275+ if (unlikely(err))
027c5e7a 14276+ goto out;
1facf9fc 14277+
4a4d8108 14278+ /* drop flags for writing */
5527c038 14279+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14280+ if (force_wr && !(flags & O_WRONLY))
14281+ force_wr = 0;
4a4d8108 14282+ flags = au_file_roflags(flags);
392086de
AM
14283+ if (force_wr) {
14284+ h_file = ERR_PTR(-EROFS);
14285+ flags = au_file_roflags(flags);
14286+ if (unlikely(vfsub_native_ro(h_inode)
14287+ || IS_APPEND(h_inode)))
14288+ goto out;
14289+ flags &= ~O_ACCMODE;
14290+ flags |= O_WRONLY;
14291+ }
14292+ }
4a4d8108 14293+ flags &= ~O_CREAT;
5afbbe0d 14294+ au_br_get(br);
4a4d8108 14295+ h_path.dentry = h_dentry;
86dc4139 14296+ h_path.mnt = au_br_mnt(br);
38d290e6 14297+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14298+ if (IS_ERR(h_file))
14299+ goto out_br;
dece6358 14300+
b912730e 14301+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14302+ err = deny_write_access(h_file);
14303+ if (unlikely(err)) {
14304+ fput(h_file);
14305+ h_file = ERR_PTR(err);
14306+ goto out_br;
14307+ }
14308+ }
953406b4 14309+ fsnotify_open(h_file);
4a4d8108 14310+ goto out; /* success */
1facf9fc 14311+
4f0767ce 14312+out_br:
5afbbe0d 14313+ au_br_put(br);
4f0767ce 14314+out:
4a4d8108
AM
14315+ return h_file;
14316+}
1308ab2a 14317+
076b876e
AM
14318+static int au_cmoo(struct dentry *dentry)
14319+{
8b6a4947 14320+ int err, cmoo, matched;
076b876e
AM
14321+ unsigned int udba;
14322+ struct path h_path;
14323+ struct au_pin pin;
14324+ struct au_cp_generic cpg = {
14325+ .dentry = dentry,
14326+ .bdst = -1,
14327+ .bsrc = -1,
14328+ .len = -1,
14329+ .pin = &pin,
14330+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14331+ };
7e9cd9fe 14332+ struct inode *delegated;
076b876e
AM
14333+ struct super_block *sb;
14334+ struct au_sbinfo *sbinfo;
14335+ struct au_fhsm *fhsm;
14336+ pid_t pid;
14337+ struct au_branch *br;
14338+ struct dentry *parent;
14339+ struct au_hinode *hdir;
14340+
14341+ DiMustWriteLock(dentry);
5527c038 14342+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14343+
14344+ err = 0;
14345+ if (IS_ROOT(dentry))
14346+ goto out;
5afbbe0d 14347+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14348+ if (!cpg.bsrc)
14349+ goto out;
14350+
14351+ sb = dentry->d_sb;
14352+ sbinfo = au_sbi(sb);
14353+ fhsm = &sbinfo->si_fhsm;
14354+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14355+ rcu_read_lock();
14356+ matched = (pid
14357+ && (current->pid == pid
14358+ || rcu_dereference(current->real_parent)->pid == pid));
14359+ rcu_read_unlock();
14360+ if (matched)
076b876e
AM
14361+ goto out;
14362+
14363+ br = au_sbr(sb, cpg.bsrc);
14364+ cmoo = au_br_cmoo(br->br_perm);
14365+ if (!cmoo)
14366+ goto out;
7e9cd9fe 14367+ if (!d_is_reg(dentry))
076b876e
AM
14368+ cmoo &= AuBrAttr_COO_ALL;
14369+ if (!cmoo)
14370+ goto out;
14371+
14372+ parent = dget_parent(dentry);
14373+ di_write_lock_parent(parent);
14374+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14375+ cpg.bdst = err;
14376+ if (unlikely(err < 0)) {
14377+ err = 0; /* there is no upper writable branch */
14378+ goto out_dgrade;
14379+ }
14380+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14381+
14382+ /* do not respect the coo attrib for the target branch */
14383+ err = au_cpup_dirs(dentry, cpg.bdst);
14384+ if (unlikely(err))
14385+ goto out_dgrade;
14386+
14387+ di_downgrade_lock(parent, AuLock_IR);
14388+ udba = au_opt_udba(sb);
14389+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14390+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14391+ if (unlikely(err))
14392+ goto out_parent;
14393+
14394+ err = au_sio_cpup_simple(&cpg);
14395+ au_unpin(&pin);
14396+ if (unlikely(err))
14397+ goto out_parent;
14398+ if (!(cmoo & AuBrWAttr_MOO))
14399+ goto out_parent; /* success */
14400+
14401+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14402+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14403+ if (unlikely(err))
14404+ goto out_parent;
14405+
14406+ h_path.mnt = au_br_mnt(br);
14407+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14408+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14409+ delegated = NULL;
14410+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14411+ au_unpin(&pin);
14412+ /* todo: keep h_dentry or not? */
14413+ if (unlikely(err == -EWOULDBLOCK)) {
14414+ pr_warn("cannot retry for NFSv4 delegation"
14415+ " for an internal unlink\n");
14416+ iput(delegated);
14417+ }
14418+ if (unlikely(err)) {
14419+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14420+ dentry, err);
14421+ err = 0;
14422+ }
14423+ goto out_parent; /* success */
14424+
14425+out_dgrade:
14426+ di_downgrade_lock(parent, AuLock_IR);
14427+out_parent:
14428+ di_read_unlock(parent, AuLock_IR);
14429+ dput(parent);
14430+out:
14431+ AuTraceErr(err);
14432+ return err;
14433+}
14434+
b912730e 14435+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14436+{
8b6a4947 14437+ int err, aopen = args->aopen;
1facf9fc 14438+ struct dentry *dentry;
076b876e 14439+ struct au_finfo *finfo;
1308ab2a 14440+
8b6a4947 14441+ if (!aopen)
b912730e
AM
14442+ err = au_finfo_init(file, args->fidir);
14443+ else {
14444+ lockdep_off();
14445+ err = au_finfo_init(file, args->fidir);
14446+ lockdep_on();
14447+ }
4a4d8108
AM
14448+ if (unlikely(err))
14449+ goto out;
1facf9fc 14450+
2000de60 14451+ dentry = file->f_path.dentry;
b912730e 14452+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14453+ di_write_lock_child(dentry);
14454+ err = au_cmoo(dentry);
14455+ di_downgrade_lock(dentry, AuLock_IR);
14456+ if (!err) {
14457+ if (!aopen)
b912730e 14458+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14459+ else {
14460+ lockdep_off();
14461+ err = args->open(file, vfsub_file_flags(file), NULL);
14462+ lockdep_on();
14463+ }
b912730e 14464+ }
8b6a4947 14465+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14466+
076b876e
AM
14467+ finfo = au_fi(file);
14468+ if (!err) {
14469+ finfo->fi_file = file;
8b6a4947
AM
14470+ au_hbl_add(&finfo->fi_hlist,
14471+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14472+ }
8b6a4947 14473+ if (!aopen)
b912730e
AM
14474+ fi_write_unlock(file);
14475+ else {
14476+ lockdep_off();
14477+ fi_write_unlock(file);
14478+ lockdep_on();
14479+ }
4a4d8108 14480+ if (unlikely(err)) {
076b876e 14481+ finfo->fi_hdir = NULL;
1c60b727 14482+ au_finfo_fin(file);
1308ab2a 14483+ }
4a4d8108 14484+
4f0767ce 14485+out:
8b6a4947 14486+ AuTraceErr(err);
1308ab2a 14487+ return err;
14488+}
dece6358 14489+
4a4d8108 14490+int au_reopen_nondir(struct file *file)
1308ab2a 14491+{
4a4d8108 14492+ int err;
5afbbe0d 14493+ aufs_bindex_t btop;
4a4d8108
AM
14494+ struct dentry *dentry;
14495+ struct file *h_file, *h_file_tmp;
1308ab2a 14496+
2000de60 14497+ dentry = file->f_path.dentry;
5afbbe0d 14498+ btop = au_dbtop(dentry);
4a4d8108 14499+ h_file_tmp = NULL;
5afbbe0d 14500+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14501+ h_file = au_hf_top(file);
14502+ if (file->f_mode == h_file->f_mode)
14503+ return 0; /* success */
14504+ h_file_tmp = h_file;
14505+ get_file(h_file_tmp);
5afbbe0d 14506+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14507+ }
14508+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14509+ /*
14510+ * it can happen
14511+ * file exists on both of rw and ro
5afbbe0d 14512+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14513+ * prepend a branch as rw, "rw" become ro
14514+ * remove rw/file
14515+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14516+ * --> dbtop is 1, fbtop is still 0
14517+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14518+ */
5afbbe0d 14519+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14520+
5afbbe0d 14521+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14522+ file, /*force_wr*/0);
4a4d8108 14523+ err = PTR_ERR(h_file);
86dc4139
AM
14524+ if (IS_ERR(h_file)) {
14525+ if (h_file_tmp) {
5afbbe0d
AM
14526+ au_sbr_get(dentry->d_sb, btop);
14527+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14528+ h_file_tmp = NULL;
14529+ }
4a4d8108 14530+ goto out; /* todo: close all? */
86dc4139 14531+ }
4a4d8108
AM
14532+
14533+ err = 0;
5afbbe0d
AM
14534+ au_set_fbtop(file, btop);
14535+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14536+ au_update_figen(file);
14537+ /* todo: necessary? */
14538+ /* file->f_ra = h_file->f_ra; */
14539+
4f0767ce 14540+out:
4a4d8108
AM
14541+ if (h_file_tmp)
14542+ fput(h_file_tmp);
14543+ return err;
1facf9fc 14544+}
14545+
1308ab2a 14546+/* ---------------------------------------------------------------------- */
14547+
4a4d8108
AM
14548+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14549+ struct dentry *hi_wh)
1facf9fc 14550+{
4a4d8108 14551+ int err;
5afbbe0d 14552+ aufs_bindex_t btop;
4a4d8108
AM
14553+ struct au_dinfo *dinfo;
14554+ struct dentry *h_dentry;
14555+ struct au_hdentry *hdp;
1facf9fc 14556+
2000de60 14557+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14558+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14559+
5afbbe0d
AM
14560+ btop = dinfo->di_btop;
14561+ dinfo->di_btop = btgt;
14562+ hdp = au_hdentry(dinfo, btgt);
14563+ h_dentry = hdp->hd_dentry;
14564+ hdp->hd_dentry = hi_wh;
4a4d8108 14565+ err = au_reopen_nondir(file);
5afbbe0d
AM
14566+ hdp->hd_dentry = h_dentry;
14567+ dinfo->di_btop = btop;
1facf9fc 14568+
1facf9fc 14569+ return err;
14570+}
14571+
4a4d8108 14572+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14573+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14574+{
4a4d8108 14575+ int err;
027c5e7a 14576+ struct inode *inode, *h_inode;
c2b27bf2
AM
14577+ struct dentry *h_dentry, *hi_wh;
14578+ struct au_cp_generic cpg = {
2000de60 14579+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14580+ .bdst = bcpup,
14581+ .bsrc = -1,
14582+ .len = len,
14583+ .pin = pin
14584+ };
1facf9fc 14585+
5afbbe0d 14586+ au_update_dbtop(cpg.dentry);
5527c038 14587+ inode = d_inode(cpg.dentry);
027c5e7a 14588+ h_inode = NULL;
5afbbe0d
AM
14589+ if (au_dbtop(cpg.dentry) <= bcpup
14590+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14591+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14592+ if (h_dentry && d_is_positive(h_dentry))
14593+ h_inode = d_inode(h_dentry);
027c5e7a 14594+ }
4a4d8108 14595+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14596+ if (!hi_wh && !h_inode)
c2b27bf2 14597+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14598+ else
14599+ /* already copied-up after unlink */
14600+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14601+
4a4d8108 14602+ if (!err
38d290e6
JR
14603+ && (inode->i_nlink > 1
14604+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14605+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14606+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14607+
dece6358 14608+ return err;
1facf9fc 14609+}
14610+
4a4d8108
AM
14611+/*
14612+ * prepare the @file for writing.
14613+ */
14614+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14615+{
4a4d8108 14616+ int err;
5afbbe0d 14617+ aufs_bindex_t dbtop;
c1595e42 14618+ struct dentry *parent;
86dc4139 14619+ struct inode *inode;
1facf9fc 14620+ struct super_block *sb;
4a4d8108 14621+ struct file *h_file;
c2b27bf2 14622+ struct au_cp_generic cpg = {
2000de60 14623+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14624+ .bdst = -1,
14625+ .bsrc = -1,
14626+ .len = len,
14627+ .pin = pin,
14628+ .flags = AuCpup_DTIME
14629+ };
1facf9fc 14630+
c2b27bf2 14631+ sb = cpg.dentry->d_sb;
5527c038 14632+ inode = d_inode(cpg.dentry);
5afbbe0d 14633+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14634+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14635+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14636+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14637+ /*flags*/0);
1facf9fc 14638+ goto out;
4a4d8108 14639+ }
1facf9fc 14640+
027c5e7a 14641+ /* need to cpup or reopen */
c2b27bf2 14642+ parent = dget_parent(cpg.dentry);
4a4d8108 14643+ di_write_lock_parent(parent);
c2b27bf2
AM
14644+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14645+ cpg.bdst = err;
4a4d8108
AM
14646+ if (unlikely(err < 0))
14647+ goto out_dgrade;
14648+ err = 0;
14649+
c2b27bf2
AM
14650+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14651+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14652+ if (unlikely(err))
4a4d8108
AM
14653+ goto out_dgrade;
14654+ }
14655+
c2b27bf2 14656+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14657+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14658+ if (unlikely(err))
14659+ goto out_dgrade;
14660+
5afbbe0d
AM
14661+ dbtop = au_dbtop(cpg.dentry);
14662+ if (dbtop <= cpg.bdst)
c2b27bf2 14663+ cpg.bsrc = cpg.bdst;
027c5e7a 14664+
5afbbe0d 14665+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14666+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14667+ ) {
392086de 14668+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14669+ if (IS_ERR(h_file))
027c5e7a 14670+ err = PTR_ERR(h_file);
86dc4139 14671+ else {
027c5e7a 14672+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14673+ if (dbtop > cpg.bdst)
c2b27bf2 14674+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14675+ if (!err)
14676+ err = au_reopen_nondir(file);
c2b27bf2 14677+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14678+ }
027c5e7a
AM
14679+ } else { /* copyup as wh and reopen */
14680+ /*
14681+ * since writable hfsplus branch is not supported,
14682+ * h_open_pre/post() are unnecessary.
14683+ */
c2b27bf2 14684+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14685+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14686+ }
4a4d8108
AM
14687+
14688+ if (!err) {
14689+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14690+ goto out_dput; /* success */
14691+ }
14692+ au_unpin(pin);
14693+ goto out_unlock;
1facf9fc 14694+
4f0767ce 14695+out_dgrade:
4a4d8108 14696+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14697+out_unlock:
4a4d8108 14698+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14699+out_dput:
4a4d8108 14700+ dput(parent);
4f0767ce 14701+out:
1facf9fc 14702+ return err;
14703+}
14704+
4a4d8108
AM
14705+/* ---------------------------------------------------------------------- */
14706+
14707+int au_do_flush(struct file *file, fl_owner_t id,
14708+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14709+{
4a4d8108 14710+ int err;
1facf9fc 14711+ struct super_block *sb;
4a4d8108 14712+ struct inode *inode;
1facf9fc 14713+
c06a8ce3
AM
14714+ inode = file_inode(file);
14715+ sb = inode->i_sb;
4a4d8108
AM
14716+ si_noflush_read_lock(sb);
14717+ fi_read_lock(file);
b752ccd1 14718+ ii_read_lock_child(inode);
1facf9fc 14719+
4a4d8108
AM
14720+ err = flush(file, id);
14721+ au_cpup_attr_timesizes(inode);
1facf9fc 14722+
b752ccd1 14723+ ii_read_unlock(inode);
4a4d8108 14724+ fi_read_unlock(file);
1308ab2a 14725+ si_read_unlock(sb);
dece6358 14726+ return err;
1facf9fc 14727+}
14728+
4a4d8108
AM
14729+/* ---------------------------------------------------------------------- */
14730+
14731+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14732+{
4a4d8108 14733+ int err;
4a4d8108
AM
14734+ struct au_pin pin;
14735+ struct au_finfo *finfo;
c2b27bf2 14736+ struct dentry *parent, *hi_wh;
4a4d8108 14737+ struct inode *inode;
1facf9fc 14738+ struct super_block *sb;
c2b27bf2 14739+ struct au_cp_generic cpg = {
2000de60 14740+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14741+ .bdst = -1,
14742+ .bsrc = -1,
14743+ .len = -1,
14744+ .pin = &pin,
14745+ .flags = AuCpup_DTIME
14746+ };
1facf9fc 14747+
4a4d8108
AM
14748+ FiMustWriteLock(file);
14749+
14750+ err = 0;
14751+ finfo = au_fi(file);
c2b27bf2 14752+ sb = cpg.dentry->d_sb;
5527c038 14753+ inode = d_inode(cpg.dentry);
5afbbe0d 14754+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14755+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14756+ goto out;
dece6358 14757+
c2b27bf2
AM
14758+ parent = dget_parent(cpg.dentry);
14759+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14760+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14761+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14762+ cpg.bdst = err;
4a4d8108
AM
14763+ di_read_unlock(parent, !AuLock_IR);
14764+ if (unlikely(err < 0))
14765+ goto out_parent;
14766+ err = 0;
1facf9fc 14767+ }
1facf9fc 14768+
4a4d8108 14769+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14770+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14771+ if (!S_ISDIR(inode->i_mode)
14772+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14773+ && au_plink_test(inode)
c2b27bf2 14774+ && !d_unhashed(cpg.dentry)
5afbbe0d 14775+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14776+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14777+ if (unlikely(err))
14778+ goto out_unlock;
14779+
14780+ /* always superio. */
c2b27bf2 14781+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14782+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14783+ if (!err) {
c2b27bf2 14784+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14785+ au_unpin(&pin);
14786+ }
4a4d8108
AM
14787+ } else if (hi_wh) {
14788+ /* already copied-up after unlink */
c2b27bf2 14789+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14790+ *need_reopen = 0;
14791+ }
1facf9fc 14792+
4f0767ce 14793+out_unlock:
4a4d8108 14794+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14795+out_parent:
4a4d8108 14796+ dput(parent);
4f0767ce 14797+out:
1308ab2a 14798+ return err;
dece6358 14799+}
1facf9fc 14800+
4a4d8108 14801+static void au_do_refresh_dir(struct file *file)
dece6358 14802+{
5afbbe0d 14803+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14804+ struct au_hfile *p, tmp, *q;
14805+ struct au_finfo *finfo;
1308ab2a 14806+ struct super_block *sb;
4a4d8108 14807+ struct au_fidir *fidir;
1facf9fc 14808+
4a4d8108 14809+ FiMustWriteLock(file);
1facf9fc 14810+
2000de60 14811+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14812+ finfo = au_fi(file);
14813+ fidir = finfo->fi_hdir;
14814+ AuDebugOn(!fidir);
14815+ p = fidir->fd_hfile + finfo->fi_btop;
14816+ brid = p->hf_br->br_id;
5afbbe0d
AM
14817+ bbot = fidir->fd_bbot;
14818+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14819+ if (!p->hf_file)
14820+ continue;
1308ab2a 14821+
4a4d8108
AM
14822+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14823+ if (new_bindex == bindex)
14824+ continue;
14825+ if (new_bindex < 0) {
14826+ au_set_h_fptr(file, bindex, NULL);
14827+ continue;
14828+ }
1308ab2a 14829+
4a4d8108
AM
14830+ /* swap two lower inode, and loop again */
14831+ q = fidir->fd_hfile + new_bindex;
14832+ tmp = *q;
14833+ *q = *p;
14834+ *p = tmp;
14835+ if (tmp.hf_file) {
14836+ bindex--;
14837+ p--;
14838+ }
14839+ }
1308ab2a 14840+
4a4d8108 14841+ p = fidir->fd_hfile;
2000de60 14842+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14843+ bbot = au_sbbot(sb);
14844+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14845+ finfo->fi_btop++, p++)
14846+ if (p->hf_file) {
c06a8ce3 14847+ if (file_inode(p->hf_file))
4a4d8108 14848+ break;
1c60b727 14849+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14850+ }
14851+ } else {
5afbbe0d
AM
14852+ bbot = au_br_index(sb, brid);
14853+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14854+ finfo->fi_btop++, p++)
14855+ if (p->hf_file)
1c60b727 14856+ au_hfput(p, /*execed*/0);
5afbbe0d 14857+ bbot = au_sbbot(sb);
4a4d8108 14858+ }
1308ab2a 14859+
5afbbe0d
AM
14860+ p = fidir->fd_hfile + bbot;
14861+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14862+ fidir->fd_bbot--, p--)
14863+ if (p->hf_file) {
c06a8ce3 14864+ if (file_inode(p->hf_file))
4a4d8108 14865+ break;
1c60b727 14866+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14867+ }
14868+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14869+}
14870+
4a4d8108
AM
14871+/*
14872+ * after branch manipulating, refresh the file.
14873+ */
14874+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14875+{
e2f27e51 14876+ int err, need_reopen, nbr;
5afbbe0d 14877+ aufs_bindex_t bbot, bindex;
4a4d8108 14878+ struct dentry *dentry;
e2f27e51 14879+ struct super_block *sb;
1308ab2a 14880+ struct au_finfo *finfo;
4a4d8108 14881+ struct au_hfile *hfile;
1facf9fc 14882+
2000de60 14883+ dentry = file->f_path.dentry;
e2f27e51
AM
14884+ sb = dentry->d_sb;
14885+ nbr = au_sbbot(sb) + 1;
1308ab2a 14886+ finfo = au_fi(file);
4a4d8108
AM
14887+ if (!finfo->fi_hdir) {
14888+ hfile = &finfo->fi_htop;
14889+ AuDebugOn(!hfile->hf_file);
e2f27e51 14890+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14891+ AuDebugOn(bindex < 0);
14892+ if (bindex != finfo->fi_btop)
5afbbe0d 14893+ au_set_fbtop(file, bindex);
4a4d8108 14894+ } else {
e2f27e51 14895+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14896+ if (unlikely(err))
14897+ goto out;
14898+ au_do_refresh_dir(file);
14899+ }
1facf9fc 14900+
4a4d8108
AM
14901+ err = 0;
14902+ need_reopen = 1;
14903+ if (!au_test_mmapped(file))
14904+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14905+ if (finfo->fi_hdir)
14906+ /* harmless if err */
14907+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14908+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14909+ err = reopen(file);
14910+ if (!err) {
14911+ au_update_figen(file);
14912+ goto out; /* success */
14913+ }
14914+
14915+ /* error, close all lower files */
14916+ if (finfo->fi_hdir) {
5afbbe0d
AM
14917+ bbot = au_fbbot_dir(file);
14918+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14919+ au_set_h_fptr(file, bindex, NULL);
14920+ }
1facf9fc 14921+
4f0767ce 14922+out:
1facf9fc 14923+ return err;
14924+}
14925+
4a4d8108
AM
14926+/* common function to regular file and dir */
14927+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14928+ int wlock, unsigned int fi_lsc)
dece6358 14929+{
1308ab2a 14930+ int err;
4a4d8108 14931+ unsigned int sigen, figen;
5afbbe0d 14932+ aufs_bindex_t btop;
4a4d8108
AM
14933+ unsigned char pseudo_link;
14934+ struct dentry *dentry;
14935+ struct inode *inode;
1facf9fc 14936+
4a4d8108 14937+ err = 0;
2000de60 14938+ dentry = file->f_path.dentry;
5527c038 14939+ inode = d_inode(dentry);
4a4d8108 14940+ sigen = au_sigen(dentry->d_sb);
521ced18 14941+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14942+ figen = au_figen(file);
521ced18
JR
14943+ if (!fi_lsc)
14944+ di_write_lock_child(dentry);
14945+ else
14946+ di_write_lock_child2(dentry);
5afbbe0d
AM
14947+ btop = au_dbtop(dentry);
14948+ pseudo_link = (btop != au_ibtop(inode));
14949+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14950+ if (!wlock) {
14951+ di_downgrade_lock(dentry, AuLock_IR);
14952+ fi_downgrade_lock(file);
14953+ }
14954+ goto out; /* success */
14955+ }
dece6358 14956+
4a4d8108 14957+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14958+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14959+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14960+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14961+ }
dece6358 14962+
027c5e7a
AM
14963+ if (!err)
14964+ err = refresh_file(file, reopen);
4a4d8108
AM
14965+ if (!err) {
14966+ if (!wlock) {
14967+ di_downgrade_lock(dentry, AuLock_IR);
14968+ fi_downgrade_lock(file);
14969+ }
14970+ } else {
14971+ di_write_unlock(dentry);
14972+ fi_write_unlock(file);
14973+ }
1facf9fc 14974+
4f0767ce 14975+out:
1308ab2a 14976+ return err;
14977+}
1facf9fc 14978+
4a4d8108
AM
14979+/* ---------------------------------------------------------------------- */
14980+
14981+/* cf. aufs_nopage() */
14982+/* for madvise(2) */
14983+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 14984+{
4a4d8108
AM
14985+ unlock_page(page);
14986+ return 0;
14987+}
1facf9fc 14988+
4a4d8108 14989+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 14990+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 14991+{ BUG(); return 0; }
1facf9fc 14992+
4a4d8108
AM
14993+/* they will never be called. */
14994+#ifdef CONFIG_AUFS_DEBUG
14995+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14996+ loff_t pos, unsigned len, unsigned flags,
14997+ struct page **pagep, void **fsdata)
14998+{ AuUnsupport(); return 0; }
14999+static int aufs_write_end(struct file *file, struct address_space *mapping,
15000+ loff_t pos, unsigned len, unsigned copied,
15001+ struct page *page, void *fsdata)
15002+{ AuUnsupport(); return 0; }
15003+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
15004+{ AuUnsupport(); return 0; }
1308ab2a 15005+
4a4d8108
AM
15006+static int aufs_set_page_dirty(struct page *page)
15007+{ AuUnsupport(); return 0; }
392086de
AM
15008+static void aufs_invalidatepage(struct page *page, unsigned int offset,
15009+ unsigned int length)
4a4d8108
AM
15010+{ AuUnsupport(); }
15011+static int aufs_releasepage(struct page *page, gfp_t gfp)
15012+{ AuUnsupport(); return 0; }
79b8bda9 15013+#if 0 /* called by memory compaction regardless file */
4a4d8108 15014+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 15015+ struct page *page, enum migrate_mode mode)
4a4d8108 15016+{ AuUnsupport(); return 0; }
79b8bda9 15017+#endif
e2f27e51
AM
15018+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
15019+{ AuUnsupport(); return true; }
15020+static void aufs_putback_page(struct page *page)
15021+{ AuUnsupport(); }
4a4d8108
AM
15022+static int aufs_launder_page(struct page *page)
15023+{ AuUnsupport(); return 0; }
15024+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
15025+ unsigned long from,
15026+ unsigned long count)
4a4d8108 15027+{ AuUnsupport(); return 0; }
392086de
AM
15028+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
15029+ bool *writeback)
15030+{ AuUnsupport(); }
4a4d8108
AM
15031+static int aufs_error_remove_page(struct address_space *mapping,
15032+ struct page *page)
15033+{ AuUnsupport(); return 0; }
b4510431
AM
15034+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
15035+ sector_t *span)
15036+{ AuUnsupport(); return 0; }
15037+static void aufs_swap_deactivate(struct file *file)
15038+{ AuUnsupport(); }
4a4d8108
AM
15039+#endif /* CONFIG_AUFS_DEBUG */
15040+
15041+const struct address_space_operations aufs_aop = {
15042+ .readpage = aufs_readpage,
15043+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
15044+#ifdef CONFIG_AUFS_DEBUG
15045+ .writepage = aufs_writepage,
4a4d8108
AM
15046+ /* no writepages, because of writepage */
15047+ .set_page_dirty = aufs_set_page_dirty,
15048+ /* no readpages, because of readpage */
15049+ .write_begin = aufs_write_begin,
15050+ .write_end = aufs_write_end,
15051+ /* no bmap, no block device */
15052+ .invalidatepage = aufs_invalidatepage,
15053+ .releasepage = aufs_releasepage,
79b8bda9
AM
15054+ /* is fallback_migrate_page ok? */
15055+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
15056+ .isolate_page = aufs_isolate_page,
15057+ .putback_page = aufs_putback_page,
4a4d8108
AM
15058+ .launder_page = aufs_launder_page,
15059+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 15060+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
15061+ .error_remove_page = aufs_error_remove_page,
15062+ .swap_activate = aufs_swap_activate,
15063+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 15064+#endif /* CONFIG_AUFS_DEBUG */
dece6358 15065+};
7f207e10
AM
15066diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15067--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
15068+++ linux/fs/aufs/file.h 2018-07-19 09:46:13.059981241 +0200
15069@@ -0,0 +1,341 @@
15070+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 15071+/*
b00004a5 15072+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
15073+ *
15074+ * This program, aufs is free software; you can redistribute it and/or modify
15075+ * it under the terms of the GNU General Public License as published by
15076+ * the Free Software Foundation; either version 2 of the License, or
15077+ * (at your option) any later version.
15078+ *
15079+ * This program is distributed in the hope that it will be useful,
15080+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15081+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15082+ * GNU General Public License for more details.
15083+ *
15084+ * You should have received a copy of the GNU General Public License
523b37e3 15085+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15086+ */
1facf9fc 15087+
4a4d8108
AM
15088+/*
15089+ * file operations
15090+ */
1facf9fc 15091+
4a4d8108
AM
15092+#ifndef __AUFS_FILE_H__
15093+#define __AUFS_FILE_H__
1facf9fc 15094+
4a4d8108 15095+#ifdef __KERNEL__
1facf9fc 15096+
2cbb1c4b 15097+#include <linux/file.h>
4a4d8108 15098+#include <linux/fs.h>
3c1bdaff 15099+#include <linux/mm_types.h>
4a4d8108 15100+#include <linux/poll.h>
4a4d8108 15101+#include "rwsem.h"
1facf9fc 15102+
4a4d8108
AM
15103+struct au_branch;
15104+struct au_hfile {
15105+ struct file *hf_file;
15106+ struct au_branch *hf_br;
15107+};
1facf9fc 15108+
4a4d8108
AM
15109+struct au_vdir;
15110+struct au_fidir {
15111+ aufs_bindex_t fd_bbot;
15112+ aufs_bindex_t fd_nent;
15113+ struct au_vdir *fd_vdir_cache;
15114+ struct au_hfile fd_hfile[];
15115+};
1facf9fc 15116+
4a4d8108 15117+static inline int au_fidir_sz(int nent)
dece6358 15118+{
4f0767ce
JR
15119+ AuDebugOn(nent < 0);
15120+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 15121+}
1facf9fc 15122+
4a4d8108
AM
15123+struct au_finfo {
15124+ atomic_t fi_generation;
dece6358 15125+
4a4d8108
AM
15126+ struct au_rwsem fi_rwsem;
15127+ aufs_bindex_t fi_btop;
15128+
15129+ /* do not union them */
15130+ struct { /* for non-dir */
15131+ struct au_hfile fi_htop;
2cbb1c4b 15132+ atomic_t fi_mmapped;
4a4d8108
AM
15133+ };
15134+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15135+
8b6a4947 15136+ struct hlist_bl_node fi_hlist;
1c60b727 15137+ struct file *fi_file; /* very ugly */
4a4d8108 15138+} ____cacheline_aligned_in_smp;
1facf9fc 15139+
4a4d8108 15140+/* ---------------------------------------------------------------------- */
1facf9fc 15141+
4a4d8108
AM
15142+/* file.c */
15143+extern const struct address_space_operations aufs_aop;
15144+unsigned int au_file_roflags(unsigned int flags);
15145+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15146+ struct file *file, int force_wr);
b912730e 15147+struct au_do_open_args {
8b6a4947 15148+ int aopen;
b912730e
AM
15149+ int (*open)(struct file *file, int flags,
15150+ struct file *h_file);
15151+ struct au_fidir *fidir;
15152+ struct file *h_file;
15153+};
15154+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15155+int au_reopen_nondir(struct file *file);
15156+struct au_pin;
15157+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15158+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15159+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15160+int au_do_flush(struct file *file, fl_owner_t id,
15161+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15162+
4a4d8108
AM
15163+/* poll.c */
15164+#ifdef CONFIG_AUFS_POLL
be118d29 15165+__poll_t aufs_poll(struct file *file, poll_table *wait);
4a4d8108 15166+#endif
1facf9fc 15167+
4a4d8108
AM
15168+#ifdef CONFIG_AUFS_BR_HFSPLUS
15169+/* hfsplus.c */
392086de
AM
15170+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15171+ int force_wr);
4a4d8108
AM
15172+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15173+ struct file *h_file);
15174+#else
c1595e42
JR
15175+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15176+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15177+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15178+ struct file *h_file);
15179+#endif
1facf9fc 15180+
4a4d8108
AM
15181+/* f_op.c */
15182+extern const struct file_operations aufs_file_fop;
b912730e 15183+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15184+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15185+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15186+
4a4d8108 15187+/* finfo.c */
f0c0a007 15188+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15189+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15190+ struct file *h_file);
1facf9fc 15191+
4a4d8108 15192+void au_update_figen(struct file *file);
4a4d8108 15193+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15194+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15195+
4a4d8108 15196+void au_fi_init_once(void *_fi);
1c60b727 15197+void au_finfo_fin(struct file *file);
4a4d8108 15198+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15199+
4a4d8108
AM
15200+/* ioctl.c */
15201+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15202+#ifdef CONFIG_COMPAT
15203+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15204+ unsigned long arg);
c2b27bf2
AM
15205+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15206+ unsigned long arg);
b752ccd1 15207+#endif
1facf9fc 15208+
4a4d8108 15209+/* ---------------------------------------------------------------------- */
1facf9fc 15210+
4a4d8108
AM
15211+static inline struct au_finfo *au_fi(struct file *file)
15212+{
38d290e6 15213+ return file->private_data;
4a4d8108 15214+}
1facf9fc 15215+
4a4d8108 15216+/* ---------------------------------------------------------------------- */
1facf9fc 15217+
8b6a4947
AM
15218+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15219+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15220+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15221+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15222+/*
8b6a4947
AM
15223+#define fi_read_trylock_nested(f) \
15224+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15225+#define fi_write_trylock_nested(f) \
15226+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15227+*/
15228+
15229+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15230+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15231+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15232+
521ced18
JR
15233+/* lock subclass for finfo */
15234+enum {
15235+ AuLsc_FI_1,
15236+ AuLsc_FI_2
15237+};
15238+
15239+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15240+{
15241+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15242+}
15243+
15244+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15245+{
15246+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15247+}
15248+
15249+/*
15250+ * fi_read_lock_1, fi_write_lock_1,
15251+ * fi_read_lock_2, fi_write_lock_2
15252+ */
15253+#define AuReadLockFunc(name) \
15254+static inline void fi_read_lock_##name(struct file *f) \
15255+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15256+
15257+#define AuWriteLockFunc(name) \
15258+static inline void fi_write_lock_##name(struct file *f) \
15259+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15260+
15261+#define AuRWLockFuncs(name) \
15262+ AuReadLockFunc(name) \
15263+ AuWriteLockFunc(name)
15264+
15265+AuRWLockFuncs(1);
15266+AuRWLockFuncs(2);
15267+
15268+#undef AuReadLockFunc
15269+#undef AuWriteLockFunc
15270+#undef AuRWLockFuncs
15271+
4a4d8108
AM
15272+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15273+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15274+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15275+
1308ab2a 15276+/* ---------------------------------------------------------------------- */
15277+
4a4d8108 15278+/* todo: hard/soft set? */
5afbbe0d 15279+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15280+{
4a4d8108
AM
15281+ FiMustAnyLock(file);
15282+ return au_fi(file)->fi_btop;
15283+}
dece6358 15284+
5afbbe0d 15285+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15286+{
15287+ FiMustAnyLock(file);
15288+ AuDebugOn(!au_fi(file)->fi_hdir);
15289+ return au_fi(file)->fi_hdir->fd_bbot;
15290+}
1facf9fc 15291+
4a4d8108
AM
15292+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15293+{
15294+ FiMustAnyLock(file);
15295+ AuDebugOn(!au_fi(file)->fi_hdir);
15296+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15297+}
1facf9fc 15298+
5afbbe0d 15299+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15300+{
15301+ FiMustWriteLock(file);
15302+ au_fi(file)->fi_btop = bindex;
15303+}
1facf9fc 15304+
5afbbe0d 15305+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15306+{
15307+ FiMustWriteLock(file);
15308+ AuDebugOn(!au_fi(file)->fi_hdir);
15309+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15310+}
1308ab2a 15311+
4a4d8108
AM
15312+static inline void au_set_fvdir_cache(struct file *file,
15313+ struct au_vdir *vdir_cache)
15314+{
15315+ FiMustWriteLock(file);
15316+ AuDebugOn(!au_fi(file)->fi_hdir);
15317+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15318+}
dece6358 15319+
4a4d8108
AM
15320+static inline struct file *au_hf_top(struct file *file)
15321+{
15322+ FiMustAnyLock(file);
15323+ AuDebugOn(au_fi(file)->fi_hdir);
15324+ return au_fi(file)->fi_htop.hf_file;
15325+}
1facf9fc 15326+
4a4d8108
AM
15327+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15328+{
15329+ FiMustAnyLock(file);
15330+ AuDebugOn(!au_fi(file)->fi_hdir);
15331+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15332+}
15333+
4a4d8108
AM
15334+/* todo: memory barrier? */
15335+static inline unsigned int au_figen(struct file *f)
dece6358 15336+{
4a4d8108
AM
15337+ return atomic_read(&au_fi(f)->fi_generation);
15338+}
dece6358 15339+
2cbb1c4b
JR
15340+static inline void au_set_mmapped(struct file *f)
15341+{
15342+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15343+ return;
0c3ec466 15344+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15345+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15346+ ;
15347+}
15348+
15349+static inline void au_unset_mmapped(struct file *f)
15350+{
15351+ atomic_dec(&au_fi(f)->fi_mmapped);
15352+}
15353+
4a4d8108
AM
15354+static inline int au_test_mmapped(struct file *f)
15355+{
2cbb1c4b
JR
15356+ return atomic_read(&au_fi(f)->fi_mmapped);
15357+}
15358+
15359+/* customize vma->vm_file */
15360+
15361+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15362+ struct file *file)
15363+{
53392da6
AM
15364+ struct file *f;
15365+
15366+ f = vma->vm_file;
2cbb1c4b
JR
15367+ get_file(file);
15368+ vma->vm_file = file;
53392da6 15369+ fput(f);
2cbb1c4b
JR
15370+}
15371+
15372+#ifdef CONFIG_MMU
15373+#define AuDbgVmRegion(file, vma) do {} while (0)
15374+
15375+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15376+ struct file *file)
15377+{
15378+ au_do_vm_file_reset(vma, file);
15379+}
15380+#else
15381+#define AuDbgVmRegion(file, vma) \
15382+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15383+
15384+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15385+ struct file *file)
15386+{
53392da6
AM
15387+ struct file *f;
15388+
2cbb1c4b 15389+ au_do_vm_file_reset(vma, file);
53392da6 15390+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15391+ get_file(file);
15392+ vma->vm_region->vm_file = file;
53392da6 15393+ fput(f);
2cbb1c4b
JR
15394+}
15395+#endif /* CONFIG_MMU */
15396+
15397+/* handle vma->vm_prfile */
fb47a38f 15398+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15399+ struct file *file)
15400+{
2cbb1c4b
JR
15401+ get_file(file);
15402+ vma->vm_prfile = file;
15403+#ifndef CONFIG_MMU
15404+ get_file(file);
15405+ vma->vm_region->vm_prfile = file;
15406+#endif
fb47a38f 15407+}
1308ab2a 15408+
4a4d8108
AM
15409+#endif /* __KERNEL__ */
15410+#endif /* __AUFS_FILE_H__ */
7f207e10
AM
15411diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15412--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
15413+++ linux/fs/aufs/finfo.c 2018-07-19 09:46:13.059981241 +0200
15414@@ -0,0 +1,149 @@
15415+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 15416+/*
b00004a5 15417+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
15418+ *
15419+ * This program, aufs is free software; you can redistribute it and/or modify
15420+ * it under the terms of the GNU General Public License as published by
15421+ * the Free Software Foundation; either version 2 of the License, or
15422+ * (at your option) any later version.
15423+ *
15424+ * This program is distributed in the hope that it will be useful,
15425+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15426+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15427+ * GNU General Public License for more details.
15428+ *
15429+ * You should have received a copy of the GNU General Public License
523b37e3 15430+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15431+ */
1308ab2a 15432+
4a4d8108
AM
15433+/*
15434+ * file private data
15435+ */
1facf9fc 15436+
4a4d8108 15437+#include "aufs.h"
1facf9fc 15438+
f0c0a007 15439+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15440+{
f0c0a007 15441+ if (execed)
4a4d8108
AM
15442+ allow_write_access(hf->hf_file);
15443+ fput(hf->hf_file);
15444+ hf->hf_file = NULL;
5afbbe0d 15445+ au_br_put(hf->hf_br);
4a4d8108
AM
15446+ hf->hf_br = NULL;
15447+}
1facf9fc 15448+
4a4d8108
AM
15449+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15450+{
15451+ struct au_finfo *finfo = au_fi(file);
15452+ struct au_hfile *hf;
15453+ struct au_fidir *fidir;
15454+
15455+ fidir = finfo->fi_hdir;
15456+ if (!fidir) {
15457+ AuDebugOn(finfo->fi_btop != bindex);
15458+ hf = &finfo->fi_htop;
15459+ } else
15460+ hf = fidir->fd_hfile + bindex;
15461+
15462+ if (hf && hf->hf_file)
f0c0a007 15463+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15464+ if (val) {
15465+ FiMustWriteLock(file);
b912730e 15466+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15467+ hf->hf_file = val;
2000de60 15468+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15469+ }
4a4d8108 15470+}
1facf9fc 15471+
4a4d8108
AM
15472+void au_update_figen(struct file *file)
15473+{
2000de60 15474+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15475+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15476+}
15477+
4a4d8108
AM
15478+/* ---------------------------------------------------------------------- */
15479+
4a4d8108
AM
15480+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15481+{
15482+ struct au_fidir *fidir;
15483+ int nbr;
15484+
5afbbe0d 15485+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15486+ if (nbr < 2)
15487+ nbr = 2; /* initial allocate for 2 branches */
15488+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15489+ if (fidir) {
15490+ fidir->fd_bbot = -1;
15491+ fidir->fd_nent = nbr;
4a4d8108
AM
15492+ }
15493+
15494+ return fidir;
15495+}
15496+
e2f27e51 15497+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15498+{
15499+ int err;
15500+ struct au_fidir *fidir, *p;
15501+
15502+ AuRwMustWriteLock(&finfo->fi_rwsem);
15503+ fidir = finfo->fi_hdir;
15504+ AuDebugOn(!fidir);
15505+
15506+ err = -ENOMEM;
15507+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15508+ GFP_NOFS, may_shrink);
4a4d8108
AM
15509+ if (p) {
15510+ p->fd_nent = nbr;
15511+ finfo->fi_hdir = p;
15512+ err = 0;
15513+ }
1facf9fc 15514+
dece6358 15515+ return err;
1facf9fc 15516+}
1308ab2a 15517+
15518+/* ---------------------------------------------------------------------- */
15519+
1c60b727 15520+void au_finfo_fin(struct file *file)
1308ab2a 15521+{
4a4d8108
AM
15522+ struct au_finfo *finfo;
15523+
2000de60 15524+ au_nfiles_dec(file->f_path.dentry->d_sb);
7f207e10 15525+
4a4d8108
AM
15526+ finfo = au_fi(file);
15527+ AuDebugOn(finfo->fi_hdir);
15528+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15529+ au_cache_free_finfo(finfo);
1308ab2a 15530+}
1308ab2a 15531+
e49829fe 15532+void au_fi_init_once(void *_finfo)
4a4d8108 15533+{
e49829fe 15534+ struct au_finfo *finfo = _finfo;
1308ab2a 15535+
e49829fe 15536+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15537+}
1308ab2a 15538+
4a4d8108
AM
15539+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15540+{
1716fcea 15541+ int err;
4a4d8108
AM
15542+ struct au_finfo *finfo;
15543+ struct dentry *dentry;
15544+
15545+ err = -ENOMEM;
2000de60 15546+ dentry = file->f_path.dentry;
4a4d8108
AM
15547+ finfo = au_cache_alloc_finfo();
15548+ if (unlikely(!finfo))
15549+ goto out;
15550+
15551+ err = 0;
7f207e10 15552+ au_nfiles_inc(dentry->d_sb);
4a4d8108
AM
15553+ au_rw_write_lock(&finfo->fi_rwsem);
15554+ finfo->fi_btop = -1;
15555+ finfo->fi_hdir = fidir;
15556+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15557+ /* smp_mb(); */ /* atomic_set */
15558+
15559+ file->private_data = finfo;
15560+
15561+out:
15562+ return err;
15563+}
7f207e10
AM
15564diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15565--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
15566+++ linux/fs/aufs/f_op.c 2018-07-19 09:46:13.056647808 +0200
15567@@ -0,0 +1,818 @@
15568+/* SPDX-License-Identifier: GPL-2.0 */
dece6358 15569+/*
b00004a5 15570+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
15571+ *
15572+ * This program, aufs is free software; you can redistribute it and/or modify
15573+ * it under the terms of the GNU General Public License as published by
15574+ * the Free Software Foundation; either version 2 of the License, or
15575+ * (at your option) any later version.
15576+ *
15577+ * This program is distributed in the hope that it will be useful,
15578+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15579+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15580+ * GNU General Public License for more details.
15581+ *
15582+ * You should have received a copy of the GNU General Public License
523b37e3 15583+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15584+ */
1facf9fc 15585+
15586+/*
4a4d8108 15587+ * file and vm operations
1facf9fc 15588+ */
dece6358 15589+
86dc4139 15590+#include <linux/aio.h>
4a4d8108
AM
15591+#include <linux/fs_stack.h>
15592+#include <linux/mman.h>
4a4d8108 15593+#include <linux/security.h>
dece6358
AM
15594+#include "aufs.h"
15595+
b912730e 15596+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15597+{
4a4d8108
AM
15598+ int err;
15599+ aufs_bindex_t bindex;
8cdd5066 15600+ struct dentry *dentry, *h_dentry;
4a4d8108 15601+ struct au_finfo *finfo;
38d290e6 15602+ struct inode *h_inode;
4a4d8108
AM
15603+
15604+ FiMustWriteLock(file);
15605+
523b37e3 15606+ err = 0;
2000de60 15607+ dentry = file->f_path.dentry;
b912730e 15608+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15609+ finfo = au_fi(file);
15610+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15611+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15612+ bindex = au_dbtop(dentry);
8cdd5066
JR
15613+ if (!h_file) {
15614+ h_dentry = au_h_dptr(dentry, bindex);
15615+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15616+ if (unlikely(err))
15617+ goto out;
b912730e 15618+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
8cdd5066
JR
15619+ } else {
15620+ h_dentry = h_file->f_path.dentry;
15621+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15622+ if (unlikely(err))
15623+ goto out;
b912730e 15624+ get_file(h_file);
8cdd5066 15625+ }
4a4d8108
AM
15626+ if (IS_ERR(h_file))
15627+ err = PTR_ERR(h_file);
15628+ else {
38d290e6
JR
15629+ if ((flags & __O_TMPFILE)
15630+ && !(flags & O_EXCL)) {
15631+ h_inode = file_inode(h_file);
15632+ spin_lock(&h_inode->i_lock);
15633+ h_inode->i_state |= I_LINKABLE;
15634+ spin_unlock(&h_inode->i_lock);
15635+ }
5afbbe0d 15636+ au_set_fbtop(file, bindex);
4a4d8108
AM
15637+ au_set_h_fptr(file, bindex, h_file);
15638+ au_update_figen(file);
15639+ /* todo: necessary? */
15640+ /* file->f_ra = h_file->f_ra; */
15641+ }
027c5e7a 15642+
8cdd5066 15643+out:
4a4d8108 15644+ return err;
1facf9fc 15645+}
15646+
4a4d8108
AM
15647+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15648+ struct file *file)
1facf9fc 15649+{
4a4d8108 15650+ int err;
1308ab2a 15651+ struct super_block *sb;
b912730e
AM
15652+ struct au_do_open_args args = {
15653+ .open = au_do_open_nondir
15654+ };
1facf9fc 15655+
523b37e3
AM
15656+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15657+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15658+
2000de60 15659+ sb = file->f_path.dentry->d_sb;
4a4d8108 15660+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15661+ err = au_do_open(file, &args);
4a4d8108
AM
15662+ si_read_unlock(sb);
15663+ return err;
15664+}
1facf9fc 15665+
4a4d8108
AM
15666+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15667+{
15668+ struct au_finfo *finfo;
15669+ aufs_bindex_t bindex;
1facf9fc 15670+
4a4d8108 15671+ finfo = au_fi(file);
8b6a4947
AM
15672+ au_hbl_del(&finfo->fi_hlist,
15673+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15674+ bindex = finfo->fi_btop;
b4510431 15675+ if (bindex >= 0)
4a4d8108 15676+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15677+
1c60b727 15678+ au_finfo_fin(file);
4a4d8108 15679+ return 0;
1facf9fc 15680+}
15681+
4a4d8108
AM
15682+/* ---------------------------------------------------------------------- */
15683+
15684+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15685+{
1308ab2a 15686+ int err;
4a4d8108
AM
15687+ struct file *h_file;
15688+
15689+ err = 0;
15690+ h_file = au_hf_top(file);
15691+ if (h_file)
15692+ err = vfsub_flush(h_file, id);
15693+ return err;
15694+}
15695+
15696+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15697+{
15698+ return au_do_flush(file, id, au_do_flush_nondir);
15699+}
15700+
15701+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15702+/*
15703+ * read and write functions acquire [fdi]_rwsem once, but release before
15704+ * mmap_sem. This is because to stop a race condition between mmap(2).
15705+ * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
15706+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15707+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15708+ */
4a4d8108 15709+
b912730e 15710+/* Callers should call au_read_post() or fput() in the end */
521ced18 15711+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15712+{
4a4d8108 15713+ struct file *h_file;
b912730e 15714+ int err;
1facf9fc 15715+
521ced18 15716+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15717+ if (!err) {
15718+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15719+ h_file = au_hf_top(file);
15720+ get_file(h_file);
15721+ if (!keep_fi)
15722+ fi_read_unlock(file);
15723+ } else
15724+ h_file = ERR_PTR(err);
15725+
15726+ return h_file;
15727+}
15728+
15729+static void au_read_post(struct inode *inode, struct file *h_file)
15730+{
15731+ /* update without lock, I don't think it a problem */
15732+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15733+ fput(h_file);
15734+}
15735+
15736+struct au_write_pre {
521ced18
JR
15737+ /* input */
15738+ unsigned int lsc;
15739+
15740+ /* output */
b912730e 15741+ blkcnt_t blks;
5afbbe0d 15742+ aufs_bindex_t btop;
b912730e
AM
15743+};
15744+
15745+/*
15746+ * return with iinfo is write-locked
15747+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15748+ * end
15749+ */
15750+static struct file *au_write_pre(struct file *file, int do_ready,
15751+ struct au_write_pre *wpre)
15752+{
15753+ struct file *h_file;
15754+ struct dentry *dentry;
15755+ int err;
521ced18 15756+ unsigned int lsc;
b912730e
AM
15757+ struct au_pin pin;
15758+
521ced18
JR
15759+ lsc = 0;
15760+ if (wpre)
15761+ lsc = wpre->lsc;
15762+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15763+ h_file = ERR_PTR(err);
dece6358
AM
15764+ if (unlikely(err))
15765+ goto out;
1facf9fc 15766+
b912730e
AM
15767+ dentry = file->f_path.dentry;
15768+ if (do_ready) {
15769+ err = au_ready_to_write(file, -1, &pin);
15770+ if (unlikely(err)) {
15771+ h_file = ERR_PTR(err);
15772+ di_write_unlock(dentry);
15773+ goto out_fi;
15774+ }
15775+ }
15776+
15777+ di_downgrade_lock(dentry, /*flags*/0);
15778+ if (wpre)
5afbbe0d 15779+ wpre->btop = au_fbtop(file);
4a4d8108 15780+ h_file = au_hf_top(file);
9dbd164d 15781+ get_file(h_file);
b912730e
AM
15782+ if (wpre)
15783+ wpre->blks = file_inode(h_file)->i_blocks;
15784+ if (do_ready)
15785+ au_unpin(&pin);
15786+ di_read_unlock(dentry, /*flags*/0);
15787+
15788+out_fi:
15789+ fi_write_unlock(file);
15790+out:
15791+ return h_file;
15792+}
15793+
15794+static void au_write_post(struct inode *inode, struct file *h_file,
15795+ struct au_write_pre *wpre, ssize_t written)
15796+{
15797+ struct inode *h_inode;
15798+
15799+ au_cpup_attr_timesizes(inode);
5afbbe0d 15800+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15801+ h_inode = file_inode(h_file);
15802+ inode->i_mode = h_inode->i_mode;
15803+ ii_write_unlock(inode);
b912730e
AM
15804+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15805+ if (written > 0)
5afbbe0d 15806+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15807+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15808+ fput(h_file);
b912730e
AM
15809+}
15810+
15811+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15812+ loff_t *ppos)
15813+{
15814+ ssize_t err;
15815+ struct inode *inode;
15816+ struct file *h_file;
15817+ struct super_block *sb;
15818+
15819+ inode = file_inode(file);
15820+ sb = inode->i_sb;
15821+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15822+
521ced18 15823+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15824+ err = PTR_ERR(h_file);
15825+ if (IS_ERR(h_file))
15826+ goto out;
9dbd164d
AM
15827+
15828+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
15829+ err = vfsub_read_u(h_file, buf, count, ppos);
15830+ /* todo: necessary? */
15831+ /* file->f_ra = h_file->f_ra; */
b912730e 15832+ au_read_post(inode, h_file);
1308ab2a 15833+
4f0767ce 15834+out:
dece6358
AM
15835+ si_read_unlock(sb);
15836+ return err;
15837+}
1facf9fc 15838+
e49829fe
JR
15839+/*
15840+ * todo: very ugly
15841+ * it locks both of i_mutex and si_rwsem for read in safe.
15842+ * if the plink maintenance mode continues forever (that is the problem),
15843+ * may loop forever.
15844+ */
15845+static void au_mtx_and_read_lock(struct inode *inode)
15846+{
15847+ int err;
15848+ struct super_block *sb = inode->i_sb;
15849+
15850+ while (1) {
febd17d6 15851+ inode_lock(inode);
e49829fe
JR
15852+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15853+ if (!err)
15854+ break;
febd17d6 15855+ inode_unlock(inode);
e49829fe
JR
15856+ si_read_lock(sb, AuLock_NOPLMW);
15857+ si_read_unlock(sb);
15858+ }
15859+}
15860+
4a4d8108
AM
15861+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15862+ size_t count, loff_t *ppos)
dece6358 15863+{
4a4d8108 15864+ ssize_t err;
b912730e
AM
15865+ struct au_write_pre wpre;
15866+ struct inode *inode;
4a4d8108
AM
15867+ struct file *h_file;
15868+ char __user *buf = (char __user *)ubuf;
1facf9fc 15869+
b912730e 15870+ inode = file_inode(file);
e49829fe 15871+ au_mtx_and_read_lock(inode);
1facf9fc 15872+
521ced18 15873+ wpre.lsc = 0;
b912730e
AM
15874+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15875+ err = PTR_ERR(h_file);
15876+ if (IS_ERR(h_file))
9dbd164d 15877+ goto out;
9dbd164d 15878+
4a4d8108 15879+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 15880+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15881+
4f0767ce 15882+out:
b912730e 15883+ si_read_unlock(inode->i_sb);
febd17d6 15884+ inode_unlock(inode);
dece6358
AM
15885+ return err;
15886+}
1facf9fc 15887+
076b876e
AM
15888+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15889+ struct iov_iter *iov_iter)
dece6358 15890+{
4a4d8108
AM
15891+ ssize_t err;
15892+ struct file *file;
076b876e 15893+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15894+
4a4d8108
AM
15895+ err = security_file_permission(h_file, rw);
15896+ if (unlikely(err))
15897+ goto out;
1facf9fc 15898+
4a4d8108 15899+ err = -ENOSYS;
076b876e 15900+ iter = NULL;
5527c038 15901+ if (rw == MAY_READ)
076b876e 15902+ iter = h_file->f_op->read_iter;
5527c038 15903+ else if (rw == MAY_WRITE)
076b876e 15904+ iter = h_file->f_op->write_iter;
076b876e
AM
15905+
15906+ file = kio->ki_filp;
15907+ kio->ki_filp = h_file;
15908+ if (iter) {
2cbb1c4b 15909+ lockdep_off();
076b876e
AM
15910+ err = iter(kio, iov_iter);
15911+ lockdep_on();
4a4d8108
AM
15912+ } else
15913+ /* currently there is no such fs */
15914+ WARN_ON_ONCE(1);
076b876e 15915+ kio->ki_filp = file;
1facf9fc 15916+
4f0767ce 15917+out:
dece6358
AM
15918+ return err;
15919+}
1facf9fc 15920+
076b876e 15921+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15922+{
4a4d8108
AM
15923+ ssize_t err;
15924+ struct file *file, *h_file;
b912730e 15925+ struct inode *inode;
dece6358 15926+ struct super_block *sb;
1facf9fc 15927+
4a4d8108 15928+ file = kio->ki_filp;
b912730e
AM
15929+ inode = file_inode(file);
15930+ sb = inode->i_sb;
e49829fe 15931+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15932+
521ced18 15933+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15934+ err = PTR_ERR(h_file);
15935+ if (IS_ERR(h_file))
15936+ goto out;
9dbd164d 15937+
5afbbe0d
AM
15938+ if (au_test_loopback_kthread()) {
15939+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15940+ if (file->f_mapping != h_file->f_mapping) {
15941+ file->f_mapping = h_file->f_mapping;
15942+ smp_mb(); /* unnecessary? */
15943+ }
15944+ }
15945+ fi_read_unlock(file);
15946+
076b876e 15947+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15948+ /* todo: necessary? */
15949+ /* file->f_ra = h_file->f_ra; */
b912730e 15950+ au_read_post(inode, h_file);
1facf9fc 15951+
4f0767ce 15952+out:
4a4d8108 15953+ si_read_unlock(sb);
1308ab2a 15954+ return err;
15955+}
1facf9fc 15956+
076b876e 15957+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15958+{
4a4d8108 15959+ ssize_t err;
b912730e
AM
15960+ struct au_write_pre wpre;
15961+ struct inode *inode;
4a4d8108 15962+ struct file *file, *h_file;
1308ab2a 15963+
4a4d8108 15964+ file = kio->ki_filp;
b912730e 15965+ inode = file_inode(file);
e49829fe
JR
15966+ au_mtx_and_read_lock(inode);
15967+
521ced18 15968+ wpre.lsc = 0;
b912730e
AM
15969+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15970+ err = PTR_ERR(h_file);
15971+ if (IS_ERR(h_file))
9dbd164d 15972+ goto out;
9dbd164d 15973+
076b876e 15974+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 15975+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15976+
4f0767ce 15977+out:
b912730e 15978+ si_read_unlock(inode->i_sb);
febd17d6 15979+ inode_unlock(inode);
dece6358 15980+ return err;
1facf9fc 15981+}
15982+
4a4d8108
AM
15983+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15984+ struct pipe_inode_info *pipe, size_t len,
15985+ unsigned int flags)
1facf9fc 15986+{
4a4d8108
AM
15987+ ssize_t err;
15988+ struct file *h_file;
b912730e 15989+ struct inode *inode;
dece6358 15990+ struct super_block *sb;
1facf9fc 15991+
b912730e
AM
15992+ inode = file_inode(file);
15993+ sb = inode->i_sb;
e49829fe 15994+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 15995+
521ced18 15996+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15997+ err = PTR_ERR(h_file);
15998+ if (IS_ERR(h_file))
dece6358 15999+ goto out;
1facf9fc 16000+
4a4d8108
AM
16001+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
16002+ /* todo: necessasry? */
16003+ /* file->f_ra = h_file->f_ra; */
b912730e 16004+ au_read_post(inode, h_file);
1facf9fc 16005+
4f0767ce 16006+out:
4a4d8108 16007+ si_read_unlock(sb);
dece6358 16008+ return err;
1facf9fc 16009+}
16010+
4a4d8108
AM
16011+static ssize_t
16012+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
16013+ size_t len, unsigned int flags)
1facf9fc 16014+{
4a4d8108 16015+ ssize_t err;
b912730e
AM
16016+ struct au_write_pre wpre;
16017+ struct inode *inode;
076b876e 16018+ struct file *h_file;
1facf9fc 16019+
b912730e 16020+ inode = file_inode(file);
e49829fe 16021+ au_mtx_and_read_lock(inode);
9dbd164d 16022+
521ced18 16023+ wpre.lsc = 0;
b912730e
AM
16024+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16025+ err = PTR_ERR(h_file);
16026+ if (IS_ERR(h_file))
9dbd164d 16027+ goto out;
9dbd164d 16028+
4a4d8108 16029+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 16030+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 16031+
4f0767ce 16032+out:
b912730e 16033+ si_read_unlock(inode->i_sb);
febd17d6 16034+ inode_unlock(inode);
4a4d8108
AM
16035+ return err;
16036+}
1facf9fc 16037+
38d290e6
JR
16038+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
16039+ loff_t len)
16040+{
16041+ long err;
b912730e 16042+ struct au_write_pre wpre;
38d290e6
JR
16043+ struct inode *inode;
16044+ struct file *h_file;
16045+
b912730e 16046+ inode = file_inode(file);
38d290e6
JR
16047+ au_mtx_and_read_lock(inode);
16048+
521ced18 16049+ wpre.lsc = 0;
b912730e
AM
16050+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16051+ err = PTR_ERR(h_file);
16052+ if (IS_ERR(h_file))
38d290e6 16053+ goto out;
38d290e6
JR
16054+
16055+ lockdep_off();
03673fb0 16056+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 16057+ lockdep_on();
b912730e 16058+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
16059+
16060+out:
b912730e 16061+ si_read_unlock(inode->i_sb);
febd17d6 16062+ inode_unlock(inode);
38d290e6
JR
16063+ return err;
16064+}
16065+
521ced18
JR
16066+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16067+ struct file *dst, loff_t dst_pos,
16068+ size_t len, unsigned int flags)
16069+{
16070+ ssize_t err;
16071+ struct au_write_pre wpre;
16072+ enum { SRC, DST };
16073+ struct {
16074+ struct inode *inode;
16075+ struct file *h_file;
16076+ struct super_block *h_sb;
16077+ } a[2];
16078+#define a_src a[SRC]
16079+#define a_dst a[DST]
16080+
16081+ err = -EINVAL;
16082+ a_src.inode = file_inode(src);
16083+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16084+ goto out;
16085+ a_dst.inode = file_inode(dst);
16086+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16087+ goto out;
16088+
16089+ au_mtx_and_read_lock(a_dst.inode);
16090+ /*
16091+ * in order to match the order in di_write_lock2_{child,parent}(),
16092+ * use f_path.dentry for this comparision.
16093+ */
16094+ if (src->f_path.dentry < dst->f_path.dentry) {
16095+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16096+ err = PTR_ERR(a_src.h_file);
16097+ if (IS_ERR(a_src.h_file))
16098+ goto out_si;
16099+
16100+ wpre.lsc = AuLsc_FI_2;
16101+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16102+ err = PTR_ERR(a_dst.h_file);
16103+ if (IS_ERR(a_dst.h_file)) {
16104+ au_read_post(a_src.inode, a_src.h_file);
16105+ goto out_si;
16106+ }
16107+ } else {
16108+ wpre.lsc = AuLsc_FI_1;
16109+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16110+ err = PTR_ERR(a_dst.h_file);
16111+ if (IS_ERR(a_dst.h_file))
16112+ goto out_si;
16113+
16114+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16115+ err = PTR_ERR(a_src.h_file);
16116+ if (IS_ERR(a_src.h_file)) {
16117+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16118+ /*written*/0);
16119+ goto out_si;
16120+ }
16121+ }
16122+
16123+ err = -EXDEV;
16124+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16125+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16126+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16127+ AuDbgFile(src);
16128+ AuDbgFile(dst);
16129+ goto out_file;
16130+ }
16131+
16132+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16133+ dst_pos, len, flags);
16134+
16135+out_file:
16136+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16137+ fi_read_unlock(src);
16138+ au_read_post(a_src.inode, a_src.h_file);
16139+out_si:
16140+ si_read_unlock(a_dst.inode->i_sb);
16141+ inode_unlock(a_dst.inode);
16142+out:
16143+ return err;
16144+#undef a_src
16145+#undef a_dst
16146+}
16147+
4a4d8108
AM
16148+/* ---------------------------------------------------------------------- */
16149+
9dbd164d
AM
16150+/*
16151+ * The locking order around current->mmap_sem.
16152+ * - in most and regular cases
16153+ * file I/O syscall -- aufs_read() or something
16154+ * -- si_rwsem for read -- mmap_sem
16155+ * (Note that [fdi]i_rwsem are released before mmap_sem).
16156+ * - in mmap case
16157+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16158+ * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
16159+ * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
16160+ * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16161+ * It means that when aufs acquires si_rwsem for write, the process should never
16162+ * acquire mmap_sem.
16163+ *
392086de 16164+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
16165+ * problem either since any directory is not able to be mmap-ed.
16166+ * The similar scenario is applied to aufs_readlink() too.
16167+ */
16168+
38d290e6 16169+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16170+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16171+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16172+
16173+static unsigned long au_arch_prot_conv(unsigned long flags)
16174+{
16175+ /* currently ppc64 only */
16176+#ifdef CONFIG_PPC64
16177+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16178+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16179+ return AuConv_VM_PROT(flags, SAO);
16180+#else
16181+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16182+ return 0;
16183+#endif
16184+}
16185+
16186+static unsigned long au_prot_conv(unsigned long flags)
16187+{
16188+ return AuConv_VM_PROT(flags, READ)
16189+ | AuConv_VM_PROT(flags, WRITE)
16190+ | AuConv_VM_PROT(flags, EXEC)
16191+ | au_arch_prot_conv(flags);
16192+}
16193+
16194+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16195+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16196+
16197+static unsigned long au_flag_conv(unsigned long flags)
16198+{
16199+ return AuConv_VM_MAP(flags, GROWSDOWN)
16200+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16201+ | AuConv_VM_MAP(flags, LOCKED);
16202+}
38d290e6 16203+#endif
2dfbb274 16204+
9dbd164d 16205+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16206+{
4a4d8108 16207+ int err;
4a4d8108 16208+ const unsigned char wlock
9dbd164d 16209+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16210+ struct super_block *sb;
9dbd164d 16211+ struct file *h_file;
b912730e 16212+ struct inode *inode;
9dbd164d
AM
16213+
16214+ AuDbgVmRegion(file, vma);
1308ab2a 16215+
b912730e
AM
16216+ inode = file_inode(file);
16217+ sb = inode->i_sb;
9dbd164d 16218+ lockdep_off();
e49829fe 16219+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16220+
b912730e 16221+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16222+ lockdep_on();
b912730e
AM
16223+ err = PTR_ERR(h_file);
16224+ if (IS_ERR(h_file))
16225+ goto out;
1308ab2a 16226+
b912730e
AM
16227+ err = 0;
16228+ au_set_mmapped(file);
9dbd164d 16229+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16230+ /*
16231+ * we cannot call security_mmap_file() here since it may acquire
16232+ * mmap_sem or i_mutex.
16233+ *
16234+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16235+ * au_flag_conv(vma->vm_flags));
16236+ */
9dbd164d 16237+ if (!err)
521ced18 16238+ err = call_mmap(h_file, vma);
b912730e
AM
16239+ if (!err) {
16240+ au_vm_prfile_set(vma, file);
16241+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16242+ goto out_fput; /* success */
16243+ }
2cbb1c4b
JR
16244+ au_unset_mmapped(file);
16245+ au_vm_file_reset(vma, file);
b912730e 16246+
2cbb1c4b 16247+out_fput:
9dbd164d 16248+ lockdep_off();
b912730e
AM
16249+ ii_write_unlock(inode);
16250+ lockdep_on();
16251+ fput(h_file);
4f0767ce 16252+out:
b912730e 16253+ lockdep_off();
9dbd164d
AM
16254+ si_read_unlock(sb);
16255+ lockdep_on();
16256+ AuTraceErr(err);
4a4d8108
AM
16257+ return err;
16258+}
16259+
16260+/* ---------------------------------------------------------------------- */
16261+
1e00d052
AM
16262+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16263+ int datasync)
4a4d8108
AM
16264+{
16265+ int err;
b912730e 16266+ struct au_write_pre wpre;
4a4d8108
AM
16267+ struct inode *inode;
16268+ struct file *h_file;
4a4d8108
AM
16269+
16270+ err = 0; /* -EBADF; */ /* posix? */
16271+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16272+ goto out;
4a4d8108 16273+
b912730e
AM
16274+ inode = file_inode(file);
16275+ au_mtx_and_read_lock(inode);
16276+
521ced18 16277+ wpre.lsc = 0;
b912730e
AM
16278+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16279+ err = PTR_ERR(h_file);
16280+ if (IS_ERR(h_file))
4a4d8108 16281+ goto out_unlock;
4a4d8108 16282+
53392da6 16283+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16284+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16285+
4f0767ce 16286+out_unlock:
b912730e 16287+ si_read_unlock(inode->i_sb);
febd17d6 16288+ inode_unlock(inode);
b912730e 16289+out:
4a4d8108 16290+ return err;
dece6358
AM
16291+}
16292+
4a4d8108 16293+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16294+{
4a4d8108
AM
16295+ int err;
16296+ struct file *h_file;
4a4d8108 16297+ struct super_block *sb;
1308ab2a 16298+
b912730e 16299+ sb = file->f_path.dentry->d_sb;
e49829fe 16300+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16301+
521ced18 16302+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16303+ err = PTR_ERR(h_file);
16304+ if (IS_ERR(h_file))
4a4d8108
AM
16305+ goto out;
16306+
523b37e3 16307+ if (h_file->f_op->fasync)
4a4d8108 16308+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16309+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16310+
4f0767ce 16311+out:
4a4d8108 16312+ si_read_unlock(sb);
1308ab2a 16313+ return err;
dece6358 16314+}
4a4d8108 16315+
febd17d6
JR
16316+static int aufs_setfl(struct file *file, unsigned long arg)
16317+{
16318+ int err;
16319+ struct file *h_file;
16320+ struct super_block *sb;
16321+
16322+ sb = file->f_path.dentry->d_sb;
16323+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16324+
521ced18 16325+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16326+ err = PTR_ERR(h_file);
16327+ if (IS_ERR(h_file))
16328+ goto out;
16329+
1c60b727
AM
16330+ /* stop calling h_file->fasync */
16331+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16332+ err = setfl(/*unused fd*/-1, h_file, arg);
16333+ fput(h_file); /* instead of au_read_post() */
16334+
16335+out:
16336+ si_read_unlock(sb);
16337+ return err;
16338+}
16339+
4a4d8108
AM
16340+/* ---------------------------------------------------------------------- */
16341+
16342+/* no one supports this operation, currently */
16343+#if 0
16344+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16345+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16346+{
16347+}
16348+#endif
16349+
16350+/* ---------------------------------------------------------------------- */
16351+
16352+const struct file_operations aufs_file_fop = {
16353+ .owner = THIS_MODULE,
2cbb1c4b 16354+
027c5e7a 16355+ .llseek = default_llseek,
4a4d8108
AM
16356+
16357+ .read = aufs_read,
16358+ .write = aufs_write,
076b876e
AM
16359+ .read_iter = aufs_read_iter,
16360+ .write_iter = aufs_write_iter,
16361+
4a4d8108
AM
16362+#ifdef CONFIG_AUFS_POLL
16363+ .poll = aufs_poll,
16364+#endif
16365+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16366+#ifdef CONFIG_COMPAT
c2b27bf2 16367+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16368+#endif
4a4d8108
AM
16369+ .mmap = aufs_mmap,
16370+ .open = aufs_open_nondir,
16371+ .flush = aufs_flush_nondir,
16372+ .release = aufs_release_nondir,
16373+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16374+ .fasync = aufs_fasync,
16375+ /* .sendpage = aufs_sendpage, */
febd17d6 16376+ .setfl = aufs_setfl,
4a4d8108
AM
16377+ .splice_write = aufs_splice_write,
16378+ .splice_read = aufs_splice_read,
16379+#if 0
16380+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16381+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16382+#endif
521ced18
JR
16383+ .fallocate = aufs_fallocate,
16384+ .copy_file_range = aufs_copy_file_range
4a4d8108 16385+};
7f207e10
AM
16386diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16387--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
16388+++ linux/fs/aufs/fstype.h 2018-07-19 09:46:13.059981241 +0200
16389@@ -0,0 +1,401 @@
16390+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 16391+/*
b00004a5 16392+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
16393+ *
16394+ * This program, aufs is free software; you can redistribute it and/or modify
16395+ * it under the terms of the GNU General Public License as published by
16396+ * the Free Software Foundation; either version 2 of the License, or
16397+ * (at your option) any later version.
16398+ *
16399+ * This program is distributed in the hope that it will be useful,
16400+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16401+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16402+ * GNU General Public License for more details.
16403+ *
16404+ * You should have received a copy of the GNU General Public License
523b37e3 16405+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16406+ */
16407+
16408+/*
16409+ * judging filesystem type
16410+ */
16411+
16412+#ifndef __AUFS_FSTYPE_H__
16413+#define __AUFS_FSTYPE_H__
16414+
16415+#ifdef __KERNEL__
16416+
16417+#include <linux/fs.h>
16418+#include <linux/magic.h>
b912730e 16419+#include <linux/nfs_fs.h>
b95c5147 16420+#include <linux/romfs_fs.h>
4a4d8108
AM
16421+
16422+static inline int au_test_aufs(struct super_block *sb)
16423+{
16424+ return sb->s_magic == AUFS_SUPER_MAGIC;
16425+}
16426+
16427+static inline const char *au_sbtype(struct super_block *sb)
16428+{
16429+ return sb->s_type->name;
16430+}
1308ab2a 16431+
16432+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16433+{
f0c0a007 16434+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16435+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16436+#else
16437+ return 0;
16438+#endif
16439+}
16440+
1308ab2a 16441+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16442+{
f0c0a007 16443+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16444+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16445+#else
16446+ return 0;
16447+#endif
16448+}
16449+
1308ab2a 16450+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16451+{
f0c0a007 16452+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16453+ return sb->s_magic == CRAMFS_MAGIC;
16454+#endif
16455+ return 0;
16456+}
16457+
16458+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16459+{
f0c0a007 16460+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16461+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16462+#else
16463+ return 0;
16464+#endif
16465+}
16466+
1308ab2a 16467+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16468+{
f0c0a007 16469+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16470+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16471+#else
16472+ return 0;
16473+#endif
16474+}
16475+
1308ab2a 16476+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16477+{
f0c0a007 16478+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16479+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16480+#else
16481+ return 0;
16482+#endif
16483+}
16484+
1308ab2a 16485+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16486+{
1308ab2a 16487+#ifdef CONFIG_TMPFS
16488+ return sb->s_magic == TMPFS_MAGIC;
16489+#else
16490+ return 0;
dece6358 16491+#endif
dece6358
AM
16492+}
16493+
1308ab2a 16494+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16495+{
f0c0a007 16496+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16497+ return !strcmp(au_sbtype(sb), "ecryptfs");
16498+#else
16499+ return 0;
16500+#endif
1facf9fc 16501+}
16502+
1308ab2a 16503+static inline int au_test_ramfs(struct super_block *sb)
16504+{
16505+ return sb->s_magic == RAMFS_MAGIC;
16506+}
16507+
16508+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16509+{
f0c0a007 16510+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16511+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16512+#else
16513+ return 0;
16514+#endif
16515+}
16516+
16517+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16518+{
16519+#ifdef CONFIG_PROC_FS
16520+ return sb->s_magic == PROC_SUPER_MAGIC;
16521+#else
16522+ return 0;
16523+#endif
16524+}
16525+
16526+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16527+{
16528+#ifdef CONFIG_SYSFS
16529+ return sb->s_magic == SYSFS_MAGIC;
16530+#else
16531+ return 0;
16532+#endif
16533+}
16534+
16535+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16536+{
f0c0a007 16537+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16538+ return sb->s_magic == CONFIGFS_MAGIC;
16539+#else
16540+ return 0;
16541+#endif
16542+}
16543+
16544+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16545+{
f0c0a007 16546+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16547+ return sb->s_magic == MINIX3_SUPER_MAGIC
16548+ || sb->s_magic == MINIX2_SUPER_MAGIC
16549+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16550+ || sb->s_magic == MINIX_SUPER_MAGIC
16551+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16552+#else
16553+ return 0;
16554+#endif
16555+}
16556+
1308ab2a 16557+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16558+{
f0c0a007 16559+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16560+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16561+#else
16562+ return 0;
16563+#endif
16564+}
16565+
16566+static inline int au_test_msdos(struct super_block *sb)
16567+{
16568+ return au_test_fat(sb);
16569+}
16570+
16571+static inline int au_test_vfat(struct super_block *sb)
16572+{
16573+ return au_test_fat(sb);
16574+}
16575+
16576+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16577+{
16578+#ifdef CONFIG_SECURITYFS
16579+ return sb->s_magic == SECURITYFS_MAGIC;
16580+#else
16581+ return 0;
16582+#endif
16583+}
16584+
16585+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16586+{
f0c0a007 16587+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16588+ return sb->s_magic == SQUASHFS_MAGIC;
16589+#else
16590+ return 0;
16591+#endif
16592+}
16593+
16594+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16595+{
f0c0a007 16596+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16597+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16598+#else
16599+ return 0;
16600+#endif
16601+}
16602+
16603+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16604+{
f0c0a007 16605+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16606+ return sb->s_magic == XENFS_SUPER_MAGIC;
16607+#else
16608+ return 0;
16609+#endif
16610+}
16611+
16612+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16613+{
16614+#ifdef CONFIG_DEBUG_FS
16615+ return sb->s_magic == DEBUGFS_MAGIC;
16616+#else
16617+ return 0;
16618+#endif
16619+}
16620+
16621+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16622+{
f0c0a007 16623+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16624+ return sb->s_magic == NILFS_SUPER_MAGIC;
16625+#else
16626+ return 0;
16627+#endif
16628+}
16629+
4a4d8108
AM
16630+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16631+{
f0c0a007 16632+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16633+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16634+#else
16635+ return 0;
16636+#endif
16637+}
16638+
1308ab2a 16639+/* ---------------------------------------------------------------------- */
16640+/*
16641+ * they can't be an aufs branch.
16642+ */
16643+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16644+{
16645+ return
16646+#ifndef CONFIG_AUFS_BR_RAMFS
16647+ au_test_ramfs(sb) ||
16648+#endif
16649+ au_test_procfs(sb)
16650+ || au_test_sysfs(sb)
16651+ || au_test_configfs(sb)
16652+ || au_test_debugfs(sb)
16653+ || au_test_securityfs(sb)
16654+ || au_test_xenfs(sb)
16655+ || au_test_ecryptfs(sb)
16656+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16657+ || au_test_aufs(sb); /* will be supported in next version */
16658+}
16659+
1308ab2a 16660+static inline int au_test_fs_remote(struct super_block *sb)
16661+{
16662+ return !au_test_tmpfs(sb)
16663+#ifdef CONFIG_AUFS_BR_RAMFS
16664+ && !au_test_ramfs(sb)
16665+#endif
16666+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16667+}
16668+
16669+/* ---------------------------------------------------------------------- */
16670+
16671+/*
16672+ * Note: these functions (below) are created after reading ->getattr() in all
16673+ * filesystems under linux/fs. it means we have to do so in every update...
16674+ */
16675+
16676+/*
16677+ * some filesystems require getattr to refresh the inode attributes before
16678+ * referencing.
16679+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16680+ * and leave the work for d_revalidate()
16681+ */
16682+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16683+{
16684+ return au_test_nfs(sb)
16685+ || au_test_fuse(sb)
1308ab2a 16686+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16687+ ;
16688+}
16689+
16690+/*
16691+ * filesystems which don't maintain i_size or i_blocks.
16692+ */
16693+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16694+{
16695+ return au_test_xfs(sb)
4a4d8108
AM
16696+ || au_test_btrfs(sb)
16697+ || au_test_ubifs(sb)
16698+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16699+ /* || au_test_minix(sb) */ /* untested */
16700+ ;
16701+}
16702+
16703+/*
16704+ * filesystems which don't store the correct value in some of their inode
16705+ * attributes.
16706+ */
16707+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16708+{
16709+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16710+ || au_test_fat(sb)
16711+ || au_test_msdos(sb)
16712+ || au_test_vfat(sb);
1facf9fc 16713+}
16714+
16715+/* they don't check i_nlink in link(2) */
16716+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16717+{
16718+ return au_test_tmpfs(sb)
16719+#ifdef CONFIG_AUFS_BR_RAMFS
16720+ || au_test_ramfs(sb)
16721+#endif
4a4d8108 16722+ || au_test_ubifs(sb)
4a4d8108 16723+ || au_test_hfsplus(sb);
1facf9fc 16724+}
16725+
16726+/*
16727+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16728+ */
16729+static inline int au_test_fs_notime(struct super_block *sb)
16730+{
16731+ return au_test_nfs(sb)
16732+ || au_test_fuse(sb)
dece6358 16733+ || au_test_ubifs(sb)
1facf9fc 16734+ ;
16735+}
16736+
1facf9fc 16737+/* temporary support for i#1 in cramfs */
16738+static inline int au_test_fs_unique_ino(struct inode *inode)
16739+{
16740+ if (au_test_cramfs(inode->i_sb))
16741+ return inode->i_ino != 1;
16742+ return 1;
16743+}
16744+
16745+/* ---------------------------------------------------------------------- */
16746+
16747+/*
16748+ * the filesystem where the xino files placed must support i/o after unlink and
16749+ * maintain i_size and i_blocks.
16750+ */
16751+static inline int au_test_fs_bad_xino(struct super_block *sb)
16752+{
16753+ return au_test_fs_remote(sb)
16754+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16755+ /* don't want unnecessary work for xino */
16756+ || au_test_aufs(sb)
1308ab2a 16757+ || au_test_ecryptfs(sb)
16758+ || au_test_nilfs(sb);
1facf9fc 16759+}
16760+
16761+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16762+{
16763+ return au_test_tmpfs(sb)
16764+ || au_test_ramfs(sb);
16765+}
16766+
16767+/*
16768+ * test if the @sb is real-readonly.
16769+ */
16770+static inline int au_test_fs_rr(struct super_block *sb)
16771+{
16772+ return au_test_squashfs(sb)
16773+ || au_test_iso9660(sb)
16774+ || au_test_cramfs(sb)
16775+ || au_test_romfs(sb);
16776+}
16777+
b912730e
AM
16778+/*
16779+ * test if the @inode is nfs with 'noacl' option
2121bcd9 16780+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
16781+ */
16782+static inline int au_test_nfs_noacl(struct inode *inode)
16783+{
16784+ return au_test_nfs(inode->i_sb)
16785+ /* && IS_POSIXACL(inode) */
16786+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16787+}
16788+
1facf9fc 16789+#endif /* __KERNEL__ */
16790+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947
AM
16791diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16792--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
16793+++ linux/fs/aufs/hbl.h 2018-07-19 09:46:13.059981241 +0200
16794@@ -0,0 +1,65 @@
16795+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 16796+/*
b00004a5 16797+ * Copyright (C) 2017-2018 Junjiro R. Okajima
8b6a4947
AM
16798+ *
16799+ * This program, aufs is free software; you can redistribute it and/or modify
16800+ * it under the terms of the GNU General Public License as published by
16801+ * the Free Software Foundation; either version 2 of the License, or
16802+ * (at your option) any later version.
16803+ *
16804+ * This program is distributed in the hope that it will be useful,
16805+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16806+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16807+ * GNU General Public License for more details.
16808+ *
16809+ * You should have received a copy of the GNU General Public License
16810+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16811+ */
16812+
16813+/*
16814+ * helpers for hlist_bl.h
16815+ */
16816+
16817+#ifndef __AUFS_HBL_H__
16818+#define __AUFS_HBL_H__
16819+
16820+#ifdef __KERNEL__
16821+
16822+#include <linux/list_bl.h>
16823+
16824+static inline void au_hbl_add(struct hlist_bl_node *node,
16825+ struct hlist_bl_head *hbl)
16826+{
16827+ hlist_bl_lock(hbl);
16828+ hlist_bl_add_head(node, hbl);
16829+ hlist_bl_unlock(hbl);
16830+}
16831+
16832+static inline void au_hbl_del(struct hlist_bl_node *node,
16833+ struct hlist_bl_head *hbl)
16834+{
16835+ hlist_bl_lock(hbl);
16836+ hlist_bl_del(node);
16837+ hlist_bl_unlock(hbl);
16838+}
16839+
16840+#define au_hbl_for_each(pos, head) \
16841+ for (pos = hlist_bl_first(head); \
16842+ pos; \
16843+ pos = pos->next)
16844+
16845+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16846+{
16847+ unsigned long cnt;
16848+ struct hlist_bl_node *pos;
16849+
16850+ cnt = 0;
16851+ hlist_bl_lock(hbl);
16852+ au_hbl_for_each(pos, hbl)
16853+ cnt++;
16854+ hlist_bl_unlock(hbl);
16855+ return cnt;
16856+}
16857+
16858+#endif /* __KERNEL__ */
16859+#endif /* __AUFS_HBL_H__ */
7f207e10
AM
16860diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16861--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
16862+++ linux/fs/aufs/hfsnotify.c 2018-07-19 09:46:13.059981241 +0200
16863@@ -0,0 +1,290 @@
16864+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 16865+/*
b00004a5 16866+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 16867+ *
16868+ * This program, aufs is free software; you can redistribute it and/or modify
16869+ * it under the terms of the GNU General Public License as published by
16870+ * the Free Software Foundation; either version 2 of the License, or
16871+ * (at your option) any later version.
dece6358
AM
16872+ *
16873+ * This program is distributed in the hope that it will be useful,
16874+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16875+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16876+ * GNU General Public License for more details.
16877+ *
16878+ * You should have received a copy of the GNU General Public License
523b37e3 16879+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16880+ */
16881+
16882+/*
4a4d8108 16883+ * fsnotify for the lower directories
1facf9fc 16884+ */
16885+
16886+#include "aufs.h"
16887+
4a4d8108
AM
16888+/* FS_IN_IGNORED is unnecessary */
16889+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16890+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16891+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16892+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16893+
0c5527e5 16894+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16895+{
0c5527e5
AM
16896+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16897+ hn_mark);
5afbbe0d 16898+ /* AuDbg("here\n"); */
1c60b727 16899+ au_cache_free_hnotify(hn);
8b6a4947 16900+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16901+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16902+ wake_up(&au_hfsn_wq);
4a4d8108 16903+}
1facf9fc 16904+
027c5e7a 16905+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16906+{
1716fcea 16907+ int err;
027c5e7a
AM
16908+ struct au_hnotify *hn;
16909+ struct super_block *sb;
16910+ struct au_branch *br;
0c5527e5 16911+ struct fsnotify_mark *mark;
027c5e7a 16912+ aufs_bindex_t bindex;
1facf9fc 16913+
027c5e7a
AM
16914+ hn = hinode->hi_notify;
16915+ sb = hn->hn_aufs_inode->i_sb;
16916+ bindex = au_br_index(sb, hinode->hi_id);
16917+ br = au_sbr(sb, bindex);
1716fcea
AM
16918+ AuDebugOn(!br->br_hfsn);
16919+
0c5527e5 16920+ mark = &hn->hn_mark;
ffa93bbd 16921+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16922+ mark->mask = AuHfsnMask;
7f207e10
AM
16923+ /*
16924+ * by udba rename or rmdir, aufs assign a new inode to the known
16925+ * h_inode, so specify 1 to allow dups.
16926+ */
c1595e42 16927+ lockdep_off();
ffa93bbd
AM
16928+ err = fsnotify_add_mark(mark, hinode->hi_inode, /*mnt*/NULL,
16929+ /*allow_dups*/1);
c1595e42 16930+ lockdep_on();
1716fcea
AM
16931+
16932+ return err;
1facf9fc 16933+}
16934+
7eafdf33 16935+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16936+{
0c5527e5 16937+ struct fsnotify_mark *mark;
7eafdf33 16938+ unsigned long long ull;
1716fcea 16939+ struct fsnotify_group *group;
7eafdf33
AM
16940+
16941+ ull = atomic64_inc_return(&au_hfsn_ifree);
16942+ BUG_ON(!ull);
953406b4 16943+
0c5527e5 16944+ mark = &hn->hn_mark;
1716fcea
AM
16945+ spin_lock(&mark->lock);
16946+ group = mark->group;
16947+ fsnotify_get_group(group);
16948+ spin_unlock(&mark->lock);
c1595e42 16949+ lockdep_off();
1716fcea 16950+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16951+ fsnotify_put_mark(mark);
1716fcea 16952+ fsnotify_put_group(group);
c1595e42 16953+ lockdep_on();
7f207e10 16954+
7eafdf33
AM
16955+ /* free hn by myself */
16956+ return 0;
1facf9fc 16957+}
16958+
16959+/* ---------------------------------------------------------------------- */
16960+
4a4d8108 16961+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16962+{
0c5527e5 16963+ struct fsnotify_mark *mark;
1facf9fc 16964+
0c5527e5
AM
16965+ mark = &hinode->hi_notify->hn_mark;
16966+ spin_lock(&mark->lock);
1facf9fc 16967+ if (do_set) {
0c5527e5
AM
16968+ AuDebugOn(mark->mask & AuHfsnMask);
16969+ mark->mask |= AuHfsnMask;
1facf9fc 16970+ } else {
0c5527e5
AM
16971+ AuDebugOn(!(mark->mask & AuHfsnMask));
16972+ mark->mask &= ~AuHfsnMask;
1facf9fc 16973+ }
0c5527e5 16974+ spin_unlock(&mark->lock);
4a4d8108 16975+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 16976+}
16977+
4a4d8108 16978+/* ---------------------------------------------------------------------- */
1facf9fc 16979+
4a4d8108
AM
16980+/* #define AuDbgHnotify */
16981+#ifdef AuDbgHnotify
16982+static char *au_hfsn_name(u32 mask)
16983+{
16984+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
16985+#define test_ret(flag) \
16986+ do { \
16987+ if (mask & flag) \
16988+ return #flag; \
16989+ } while (0)
4a4d8108
AM
16990+ test_ret(FS_ACCESS);
16991+ test_ret(FS_MODIFY);
16992+ test_ret(FS_ATTRIB);
16993+ test_ret(FS_CLOSE_WRITE);
16994+ test_ret(FS_CLOSE_NOWRITE);
16995+ test_ret(FS_OPEN);
16996+ test_ret(FS_MOVED_FROM);
16997+ test_ret(FS_MOVED_TO);
16998+ test_ret(FS_CREATE);
16999+ test_ret(FS_DELETE);
17000+ test_ret(FS_DELETE_SELF);
17001+ test_ret(FS_MOVE_SELF);
17002+ test_ret(FS_UNMOUNT);
17003+ test_ret(FS_Q_OVERFLOW);
17004+ test_ret(FS_IN_IGNORED);
b912730e 17005+ test_ret(FS_ISDIR);
4a4d8108
AM
17006+ test_ret(FS_IN_ONESHOT);
17007+ test_ret(FS_EVENT_ON_CHILD);
17008+ return "";
17009+#undef test_ret
17010+#else
17011+ return "??";
17012+#endif
1facf9fc 17013+}
4a4d8108 17014+#endif
1facf9fc 17015+
17016+/* ---------------------------------------------------------------------- */
17017+
1716fcea
AM
17018+static void au_hfsn_free_group(struct fsnotify_group *group)
17019+{
17020+ struct au_br_hfsnotify *hfsn = group->private;
17021+
5afbbe0d 17022+ /* AuDbg("here\n"); */
1c60b727 17023+ kfree(hfsn);
1716fcea
AM
17024+}
17025+
4a4d8108 17026+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 17027+ struct inode *inode,
0c5527e5
AM
17028+ struct fsnotify_mark *inode_mark,
17029+ struct fsnotify_mark *vfsmount_mark,
a2654f78 17030+ u32 mask, const void *data, int data_type,
ffa93bbd
AM
17031+ const unsigned char *file_name, u32 cookie,
17032+ struct fsnotify_iter_info *iter_info)
1facf9fc 17033+{
17034+ int err;
4a4d8108
AM
17035+ struct au_hnotify *hnotify;
17036+ struct inode *h_dir, *h_inode;
fb47a38f 17037+ struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
4a4d8108 17038+
fb47a38f 17039+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 17040+
17041+ err = 0;
0c5527e5 17042+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 17043+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 17044+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 17045+ goto out;
1facf9fc 17046+
fb47a38f
JR
17047+ h_dir = inode;
17048+ h_inode = NULL;
4a4d8108 17049+#ifdef AuDbgHnotify
392086de 17050+ au_debug_on();
4a4d8108
AM
17051+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
17052+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
17053+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
17054+ h_dir->i_ino, mask, au_hfsn_name(mask),
17055+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
17056+ /* WARN_ON(1); */
1facf9fc 17057+ }
392086de 17058+ au_debug_off();
1facf9fc 17059+#endif
4a4d8108 17060+
0c5527e5
AM
17061+ AuDebugOn(!inode_mark);
17062+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
17063+ err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
1facf9fc 17064+
4a4d8108
AM
17065+out:
17066+ return err;
17067+}
1facf9fc 17068+
4a4d8108 17069+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 17070+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
17071+ .free_group_priv = au_hfsn_free_group,
17072+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
17073+};
17074+
17075+/* ---------------------------------------------------------------------- */
17076+
027c5e7a
AM
17077+static void au_hfsn_fin_br(struct au_branch *br)
17078+{
1716fcea 17079+ struct au_br_hfsnotify *hfsn;
027c5e7a 17080+
1716fcea 17081+ hfsn = br->br_hfsn;
c1595e42
JR
17082+ if (hfsn) {
17083+ lockdep_off();
1716fcea 17084+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
17085+ lockdep_on();
17086+ }
027c5e7a
AM
17087+}
17088+
1716fcea 17089+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
17090+{
17091+ int err;
1716fcea
AM
17092+ struct fsnotify_group *group;
17093+ struct au_br_hfsnotify *hfsn;
1facf9fc 17094+
4a4d8108 17095+ err = 0;
1716fcea
AM
17096+ br->br_hfsn = NULL;
17097+ if (!au_br_hnotifyable(perm))
027c5e7a 17098+ goto out;
027c5e7a 17099+
1716fcea
AM
17100+ err = -ENOMEM;
17101+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17102+ if (unlikely(!hfsn))
027c5e7a
AM
17103+ goto out;
17104+
1716fcea
AM
17105+ err = 0;
17106+ group = fsnotify_alloc_group(&au_hfsn_ops);
17107+ if (IS_ERR(group)) {
17108+ err = PTR_ERR(group);
0c5527e5 17109+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 17110+ goto out_hfsn;
4a4d8108 17111+ }
1facf9fc 17112+
1716fcea
AM
17113+ group->private = hfsn;
17114+ hfsn->hfsn_group = group;
17115+ br->br_hfsn = hfsn;
17116+ goto out; /* success */
17117+
17118+out_hfsn:
1c60b727 17119+ kfree(hfsn);
027c5e7a 17120+out:
1716fcea
AM
17121+ return err;
17122+}
17123+
17124+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17125+{
17126+ int err;
17127+
17128+ err = 0;
17129+ if (!br->br_hfsn)
17130+ err = au_hfsn_init_br(br, perm);
17131+
1facf9fc 17132+ return err;
17133+}
17134+
7eafdf33
AM
17135+/* ---------------------------------------------------------------------- */
17136+
17137+static void au_hfsn_fin(void)
17138+{
17139+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17140+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17141+}
17142+
4a4d8108
AM
17143+const struct au_hnotify_op au_hnotify_op = {
17144+ .ctl = au_hfsn_ctl,
17145+ .alloc = au_hfsn_alloc,
17146+ .free = au_hfsn_free,
1facf9fc 17147+
7eafdf33
AM
17148+ .fin = au_hfsn_fin,
17149+
027c5e7a
AM
17150+ .reset_br = au_hfsn_reset_br,
17151+ .fin_br = au_hfsn_fin_br,
17152+ .init_br = au_hfsn_init_br
4a4d8108 17153+};
7f207e10
AM
17154diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17155--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
17156+++ linux/fs/aufs/hfsplus.c 2018-07-19 09:46:13.059981241 +0200
17157@@ -0,0 +1,57 @@
17158+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 17159+/*
b00004a5 17160+ * Copyright (C) 2010-2018 Junjiro R. Okajima
4a4d8108
AM
17161+ *
17162+ * This program, aufs is free software; you can redistribute it and/or modify
17163+ * it under the terms of the GNU General Public License as published by
17164+ * the Free Software Foundation; either version 2 of the License, or
17165+ * (at your option) any later version.
17166+ *
17167+ * This program is distributed in the hope that it will be useful,
17168+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17169+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17170+ * GNU General Public License for more details.
17171+ *
17172+ * You should have received a copy of the GNU General Public License
523b37e3 17173+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17174+ */
1facf9fc 17175+
4a4d8108
AM
17176+/*
17177+ * special support for filesystems which aqucires an inode mutex
17178+ * at final closing a file, eg, hfsplus.
17179+ *
17180+ * This trick is very simple and stupid, just to open the file before really
17181+ * neceeary open to tell hfsplus that this is not the final closing.
17182+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17183+ * and au_h_open_post() after releasing it.
17184+ */
1facf9fc 17185+
4a4d8108 17186+#include "aufs.h"
1facf9fc 17187+
392086de
AM
17188+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17189+ int force_wr)
4a4d8108
AM
17190+{
17191+ struct file *h_file;
17192+ struct dentry *h_dentry;
1facf9fc 17193+
4a4d8108
AM
17194+ h_dentry = au_h_dptr(dentry, bindex);
17195+ AuDebugOn(!h_dentry);
5527c038 17196+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17197+
17198+ h_file = NULL;
17199+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17200+ && d_is_reg(h_dentry))
4a4d8108
AM
17201+ h_file = au_h_open(dentry, bindex,
17202+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17203+ /*file*/NULL, force_wr);
4a4d8108 17204+ return h_file;
1facf9fc 17205+}
17206+
4a4d8108
AM
17207+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17208+ struct file *h_file)
17209+{
17210+ if (h_file) {
17211+ fput(h_file);
17212+ au_sbr_put(dentry->d_sb, bindex);
17213+ }
17214+}
7f207e10
AM
17215diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17216--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
17217+++ linux/fs/aufs/hnotify.c 2018-07-19 09:46:13.059981241 +0200
17218@@ -0,0 +1,720 @@
17219+/* SPDX-License-Identifier: GPL-2.0 */
e49829fe 17220+/*
b00004a5 17221+ * Copyright (C) 2005-2018 Junjiro R. Okajima
e49829fe
JR
17222+ *
17223+ * This program, aufs is free software; you can redistribute it and/or modify
17224+ * it under the terms of the GNU General Public License as published by
17225+ * the Free Software Foundation; either version 2 of the License, or
17226+ * (at your option) any later version.
17227+ *
17228+ * This program is distributed in the hope that it will be useful,
17229+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17230+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17231+ * GNU General Public License for more details.
17232+ *
17233+ * You should have received a copy of the GNU General Public License
523b37e3 17234+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17235+ */
17236+
17237+/*
7f207e10 17238+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17239+ */
17240+
17241+#include "aufs.h"
17242+
027c5e7a 17243+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17244+{
17245+ int err;
7f207e10 17246+ struct au_hnotify *hn;
1facf9fc 17247+
4a4d8108
AM
17248+ err = -ENOMEM;
17249+ hn = au_cache_alloc_hnotify();
17250+ if (hn) {
17251+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17252+ hinode->hi_notify = hn;
17253+ err = au_hnotify_op.alloc(hinode);
17254+ AuTraceErr(err);
17255+ if (unlikely(err)) {
17256+ hinode->hi_notify = NULL;
1c60b727 17257+ au_cache_free_hnotify(hn);
4a4d8108
AM
17258+ /*
17259+ * The upper dir was removed by udba, but the same named
17260+ * dir left. In this case, aufs assignes a new inode
17261+ * number and set the monitor again.
17262+ * For the lower dir, the old monitnor is still left.
17263+ */
17264+ if (err == -EEXIST)
17265+ err = 0;
17266+ }
1308ab2a 17267+ }
1308ab2a 17268+
027c5e7a 17269+ AuTraceErr(err);
1308ab2a 17270+ return err;
dece6358 17271+}
1facf9fc 17272+
4a4d8108 17273+void au_hn_free(struct au_hinode *hinode)
dece6358 17274+{
4a4d8108 17275+ struct au_hnotify *hn;
1facf9fc 17276+
4a4d8108
AM
17277+ hn = hinode->hi_notify;
17278+ if (hn) {
4a4d8108 17279+ hinode->hi_notify = NULL;
7eafdf33 17280+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17281+ au_cache_free_hnotify(hn);
4a4d8108
AM
17282+ }
17283+}
dece6358 17284+
4a4d8108 17285+/* ---------------------------------------------------------------------- */
dece6358 17286+
4a4d8108
AM
17287+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17288+{
17289+ if (hinode->hi_notify)
17290+ au_hnotify_op.ctl(hinode, do_set);
17291+}
17292+
17293+void au_hn_reset(struct inode *inode, unsigned int flags)
17294+{
5afbbe0d 17295+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17296+ struct inode *hi;
17297+ struct dentry *iwhdentry;
1facf9fc 17298+
5afbbe0d
AM
17299+ bbot = au_ibbot(inode);
17300+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17301+ hi = au_h_iptr(inode, bindex);
17302+ if (!hi)
17303+ continue;
1308ab2a 17304+
febd17d6 17305+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17306+ iwhdentry = au_hi_wh(inode, bindex);
17307+ if (iwhdentry)
17308+ dget(iwhdentry);
17309+ au_igrab(hi);
17310+ au_set_h_iptr(inode, bindex, NULL, 0);
17311+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17312+ flags & ~AuHi_XINO);
17313+ iput(hi);
17314+ dput(iwhdentry);
febd17d6 17315+ /* inode_unlock(hi); */
1facf9fc 17316+ }
1facf9fc 17317+}
17318+
1308ab2a 17319+/* ---------------------------------------------------------------------- */
1facf9fc 17320+
4a4d8108 17321+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17322+{
4a4d8108 17323+ int err;
5afbbe0d 17324+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17325+ struct inode *h_i;
1facf9fc 17326+
4a4d8108
AM
17327+ err = 0;
17328+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17329+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17330+ goto out;
17331+ }
1facf9fc 17332+
4a4d8108 17333+ bfound = -1;
5afbbe0d
AM
17334+ bbot = au_ibbot(inode);
17335+ btop = au_ibtop(inode);
4a4d8108 17336+#if 0 /* reserved for future use */
5afbbe0d 17337+ if (bindex == bbot) {
4a4d8108
AM
17338+ /* keep this ino in rename case */
17339+ goto out;
17340+ }
17341+#endif
5afbbe0d 17342+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17343+ if (au_h_iptr(inode, bindex) == h_inode) {
17344+ bfound = bindex;
17345+ break;
17346+ }
17347+ if (bfound < 0)
1308ab2a 17348+ goto out;
1facf9fc 17349+
5afbbe0d 17350+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17351+ h_i = au_h_iptr(inode, bindex);
17352+ if (!h_i)
17353+ continue;
1facf9fc 17354+
4a4d8108
AM
17355+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17356+ /* ignore this error */
17357+ /* bad action? */
1facf9fc 17358+ }
1facf9fc 17359+
4a4d8108 17360+ /* children inode number will be broken */
1facf9fc 17361+
4f0767ce 17362+out:
4a4d8108
AM
17363+ AuTraceErr(err);
17364+ return err;
1facf9fc 17365+}
17366+
4a4d8108 17367+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17368+{
4a4d8108
AM
17369+ int err, i, j, ndentry;
17370+ struct au_dcsub_pages dpages;
17371+ struct au_dpage *dpage;
17372+ struct dentry **dentries;
1facf9fc 17373+
4a4d8108
AM
17374+ err = au_dpages_init(&dpages, GFP_NOFS);
17375+ if (unlikely(err))
17376+ goto out;
17377+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17378+ if (unlikely(err))
17379+ goto out_dpages;
1facf9fc 17380+
4a4d8108
AM
17381+ for (i = 0; i < dpages.ndpage; i++) {
17382+ dpage = dpages.dpages + i;
17383+ dentries = dpage->dentries;
17384+ ndentry = dpage->ndentry;
17385+ for (j = 0; j < ndentry; j++) {
17386+ struct dentry *d;
17387+
17388+ d = dentries[j];
17389+ if (IS_ROOT(d))
17390+ continue;
17391+
4a4d8108 17392+ au_digen_dec(d);
5527c038 17393+ if (d_really_is_positive(d))
4a4d8108
AM
17394+ /* todo: reset children xino?
17395+ cached children only? */
5527c038 17396+ au_iigen_dec(d_inode(d));
1308ab2a 17397+ }
dece6358 17398+ }
1facf9fc 17399+
4f0767ce 17400+out_dpages:
4a4d8108 17401+ au_dpages_free(&dpages);
dece6358 17402+
027c5e7a 17403+#if 0
4a4d8108
AM
17404+ /* discard children */
17405+ dentry_unhash(dentry);
17406+ dput(dentry);
027c5e7a 17407+#endif
4f0767ce 17408+out:
dece6358
AM
17409+ return err;
17410+}
17411+
1308ab2a 17412+/*
4a4d8108 17413+ * return 0 if processed.
1308ab2a 17414+ */
4a4d8108
AM
17415+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17416+ const unsigned int isdir)
dece6358 17417+{
1308ab2a 17418+ int err;
4a4d8108
AM
17419+ struct dentry *d;
17420+ struct qstr *dname;
1facf9fc 17421+
4a4d8108
AM
17422+ err = 1;
17423+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17424+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17425+ err = 0;
17426+ goto out;
17427+ }
dece6358 17428+
4a4d8108
AM
17429+ if (!isdir) {
17430+ AuDebugOn(!name);
17431+ au_iigen_dec(inode);
027c5e7a 17432+ spin_lock(&inode->i_lock);
c1595e42 17433+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17434+ spin_lock(&d->d_lock);
4a4d8108
AM
17435+ dname = &d->d_name;
17436+ if (dname->len != nlen
027c5e7a
AM
17437+ && memcmp(dname->name, name, nlen)) {
17438+ spin_unlock(&d->d_lock);
4a4d8108 17439+ continue;
027c5e7a 17440+ }
4a4d8108 17441+ err = 0;
4a4d8108
AM
17442+ au_digen_dec(d);
17443+ spin_unlock(&d->d_lock);
17444+ break;
1facf9fc 17445+ }
027c5e7a 17446+ spin_unlock(&inode->i_lock);
1308ab2a 17447+ } else {
027c5e7a 17448+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17449+ d = d_find_any_alias(inode);
4a4d8108
AM
17450+ if (!d) {
17451+ au_iigen_dec(inode);
17452+ goto out;
17453+ }
1facf9fc 17454+
027c5e7a 17455+ spin_lock(&d->d_lock);
4a4d8108 17456+ dname = &d->d_name;
027c5e7a
AM
17457+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17458+ spin_unlock(&d->d_lock);
4a4d8108 17459+ err = hn_gen_tree(d);
027c5e7a
AM
17460+ spin_lock(&d->d_lock);
17461+ }
17462+ spin_unlock(&d->d_lock);
4a4d8108
AM
17463+ dput(d);
17464+ }
1facf9fc 17465+
4f0767ce 17466+out:
4a4d8108 17467+ AuTraceErr(err);
1308ab2a 17468+ return err;
17469+}
dece6358 17470+
4a4d8108 17471+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17472+{
4a4d8108 17473+ int err;
1facf9fc 17474+
5527c038 17475+ if (IS_ROOT(dentry)) {
0c3ec466 17476+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17477+ return 0;
17478+ }
1308ab2a 17479+
4a4d8108
AM
17480+ err = 0;
17481+ if (!isdir) {
4a4d8108 17482+ au_digen_dec(dentry);
5527c038
JR
17483+ if (d_really_is_positive(dentry))
17484+ au_iigen_dec(d_inode(dentry));
4a4d8108 17485+ } else {
027c5e7a 17486+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17487+ if (d_really_is_positive(dentry))
4a4d8108
AM
17488+ err = hn_gen_tree(dentry);
17489+ }
17490+
17491+ AuTraceErr(err);
17492+ return err;
1facf9fc 17493+}
17494+
4a4d8108 17495+/* ---------------------------------------------------------------------- */
1facf9fc 17496+
4a4d8108
AM
17497+/* hnotify job flags */
17498+#define AuHnJob_XINO0 1
17499+#define AuHnJob_GEN (1 << 1)
17500+#define AuHnJob_DIRENT (1 << 2)
17501+#define AuHnJob_ISDIR (1 << 3)
17502+#define AuHnJob_TRYXINO0 (1 << 4)
17503+#define AuHnJob_MNTPNT (1 << 5)
17504+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17505+#define au_fset_hnjob(flags, name) \
17506+ do { (flags) |= AuHnJob_##name; } while (0)
17507+#define au_fclr_hnjob(flags, name) \
17508+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17509+
4a4d8108
AM
17510+enum {
17511+ AuHn_CHILD,
17512+ AuHn_PARENT,
17513+ AuHnLast
17514+};
1facf9fc 17515+
4a4d8108
AM
17516+struct au_hnotify_args {
17517+ struct inode *h_dir, *dir, *h_child_inode;
17518+ u32 mask;
17519+ unsigned int flags[AuHnLast];
17520+ unsigned int h_child_nlen;
17521+ char h_child_name[];
17522+};
1facf9fc 17523+
4a4d8108
AM
17524+struct hn_job_args {
17525+ unsigned int flags;
17526+ struct inode *inode, *h_inode, *dir, *h_dir;
17527+ struct dentry *dentry;
17528+ char *h_name;
17529+ int h_nlen;
17530+};
1308ab2a 17531+
4a4d8108
AM
17532+static int hn_job(struct hn_job_args *a)
17533+{
17534+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17535+ int e;
1308ab2a 17536+
4a4d8108
AM
17537+ /* reset xino */
17538+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17539+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17540+
4a4d8108
AM
17541+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17542+ && a->inode
17543+ && a->h_inode) {
be118d29 17544+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17545+ if (!a->h_inode->i_nlink
17546+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17547+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17548+ inode_unlock_shared(a->h_inode);
1308ab2a 17549+ }
1facf9fc 17550+
4a4d8108
AM
17551+ /* make the generation obsolete */
17552+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17553+ e = -1;
4a4d8108 17554+ if (a->inode)
076b876e 17555+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17556+ isdir);
076b876e 17557+ if (e && a->dentry)
4a4d8108
AM
17558+ hn_gen_by_name(a->dentry, isdir);
17559+ /* ignore this error */
1facf9fc 17560+ }
1facf9fc 17561+
4a4d8108
AM
17562+ /* make dir entries obsolete */
17563+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17564+ struct au_vdir *vdir;
1facf9fc 17565+
4a4d8108
AM
17566+ vdir = au_ivdir(a->inode);
17567+ if (vdir)
17568+ vdir->vd_jiffy = 0;
17569+ /* IMustLock(a->inode); */
be118d29 17570+ /* inode_inc_iversion(a->inode); */
4a4d8108 17571+ }
1facf9fc 17572+
4a4d8108
AM
17573+ /* can do nothing but warn */
17574+ if (au_ftest_hnjob(a->flags, MNTPNT)
17575+ && a->dentry
17576+ && d_mountpoint(a->dentry))
523b37e3 17577+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17578+
4a4d8108 17579+ return 0;
1308ab2a 17580+}
1facf9fc 17581+
1308ab2a 17582+/* ---------------------------------------------------------------------- */
1facf9fc 17583+
4a4d8108
AM
17584+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17585+ struct inode *dir)
1308ab2a 17586+{
4a4d8108
AM
17587+ struct dentry *dentry, *d, *parent;
17588+ struct qstr *dname;
1308ab2a 17589+
c1595e42 17590+ parent = d_find_any_alias(dir);
4a4d8108
AM
17591+ if (!parent)
17592+ return NULL;
1308ab2a 17593+
4a4d8108 17594+ dentry = NULL;
027c5e7a 17595+ spin_lock(&parent->d_lock);
c1595e42 17596+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17597+ /* AuDbg("%pd\n", d); */
027c5e7a 17598+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17599+ dname = &d->d_name;
17600+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17601+ goto cont_unlock;
17602+ if (au_di(d))
17603+ au_digen_dec(d);
17604+ else
17605+ goto cont_unlock;
c1595e42 17606+ if (au_dcount(d) > 0) {
027c5e7a 17607+ dentry = dget_dlock(d);
4a4d8108 17608+ spin_unlock(&d->d_lock);
027c5e7a 17609+ break;
dece6358 17610+ }
1facf9fc 17611+
f6b6e03d 17612+cont_unlock:
027c5e7a 17613+ spin_unlock(&d->d_lock);
1308ab2a 17614+ }
027c5e7a 17615+ spin_unlock(&parent->d_lock);
4a4d8108 17616+ dput(parent);
1facf9fc 17617+
4a4d8108
AM
17618+ if (dentry)
17619+ di_write_lock_child(dentry);
1308ab2a 17620+
4a4d8108
AM
17621+ return dentry;
17622+}
dece6358 17623+
4a4d8108
AM
17624+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17625+ aufs_bindex_t bindex, ino_t h_ino)
17626+{
17627+ struct inode *inode;
17628+ ino_t ino;
17629+ int err;
17630+
17631+ inode = NULL;
17632+ err = au_xino_read(sb, bindex, h_ino, &ino);
17633+ if (!err && ino)
17634+ inode = ilookup(sb, ino);
17635+ if (!inode)
17636+ goto out;
17637+
17638+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17639+ pr_warn("wrong root branch\n");
4a4d8108
AM
17640+ iput(inode);
17641+ inode = NULL;
17642+ goto out;
1308ab2a 17643+ }
17644+
4a4d8108 17645+ ii_write_lock_child(inode);
1308ab2a 17646+
4f0767ce 17647+out:
4a4d8108 17648+ return inode;
dece6358
AM
17649+}
17650+
4a4d8108 17651+static void au_hn_bh(void *_args)
1facf9fc 17652+{
4a4d8108
AM
17653+ struct au_hnotify_args *a = _args;
17654+ struct super_block *sb;
5afbbe0d 17655+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17656+ unsigned char xino, try_iput;
1facf9fc 17657+ int err;
1308ab2a 17658+ struct inode *inode;
4a4d8108
AM
17659+ ino_t h_ino;
17660+ struct hn_job_args args;
17661+ struct dentry *dentry;
17662+ struct au_sbinfo *sbinfo;
1facf9fc 17663+
4a4d8108
AM
17664+ AuDebugOn(!_args);
17665+ AuDebugOn(!a->h_dir);
17666+ AuDebugOn(!a->dir);
17667+ AuDebugOn(!a->mask);
17668+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17669+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17670+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17671+
4a4d8108
AM
17672+ inode = NULL;
17673+ dentry = NULL;
17674+ /*
17675+ * do not lock a->dir->i_mutex here
17676+ * because of d_revalidate() may cause a deadlock.
17677+ */
17678+ sb = a->dir->i_sb;
17679+ AuDebugOn(!sb);
17680+ sbinfo = au_sbi(sb);
17681+ AuDebugOn(!sbinfo);
7f207e10 17682+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17683+
8b6a4947
AM
17684+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17685+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17686+ case FS_MOVED_FROM:
17687+ case FS_MOVED_TO:
17688+ AuWarn1("DIRREN with UDBA may not work correctly "
17689+ "for the direct rename(2)\n");
17690+ }
17691+
4a4d8108
AM
17692+ ii_read_lock_parent(a->dir);
17693+ bfound = -1;
5afbbe0d
AM
17694+ bbot = au_ibbot(a->dir);
17695+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17696+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17697+ bfound = bindex;
17698+ break;
17699+ }
17700+ ii_read_unlock(a->dir);
17701+ if (unlikely(bfound < 0))
17702+ goto out;
1facf9fc 17703+
4a4d8108
AM
17704+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17705+ h_ino = 0;
17706+ if (a->h_child_inode)
17707+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17708+
4a4d8108
AM
17709+ if (a->h_child_nlen
17710+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17711+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17712+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17713+ a->dir);
17714+ try_iput = 0;
5527c038
JR
17715+ if (dentry && d_really_is_positive(dentry))
17716+ inode = d_inode(dentry);
4a4d8108
AM
17717+ if (xino && !inode && h_ino
17718+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17719+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17720+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17721+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17722+ try_iput = 1;
f0c0a007 17723+ }
1facf9fc 17724+
4a4d8108
AM
17725+ args.flags = a->flags[AuHn_CHILD];
17726+ args.dentry = dentry;
17727+ args.inode = inode;
17728+ args.h_inode = a->h_child_inode;
17729+ args.dir = a->dir;
17730+ args.h_dir = a->h_dir;
17731+ args.h_name = a->h_child_name;
17732+ args.h_nlen = a->h_child_nlen;
17733+ err = hn_job(&args);
17734+ if (dentry) {
027c5e7a 17735+ if (au_di(dentry))
4a4d8108
AM
17736+ di_write_unlock(dentry);
17737+ dput(dentry);
17738+ }
17739+ if (inode && try_iput) {
17740+ ii_write_unlock(inode);
17741+ iput(inode);
17742+ }
1facf9fc 17743+
4a4d8108
AM
17744+ ii_write_lock_parent(a->dir);
17745+ args.flags = a->flags[AuHn_PARENT];
17746+ args.dentry = NULL;
17747+ args.inode = a->dir;
17748+ args.h_inode = a->h_dir;
17749+ args.dir = NULL;
17750+ args.h_dir = NULL;
17751+ args.h_name = NULL;
17752+ args.h_nlen = 0;
17753+ err = hn_job(&args);
17754+ ii_write_unlock(a->dir);
1facf9fc 17755+
4f0767ce 17756+out:
4a4d8108
AM
17757+ iput(a->h_child_inode);
17758+ iput(a->h_dir);
17759+ iput(a->dir);
027c5e7a
AM
17760+ si_write_unlock(sb);
17761+ au_nwt_done(&sbinfo->si_nowait);
1c60b727 17762+ kfree(a);
dece6358 17763+}
1facf9fc 17764+
4a4d8108
AM
17765+/* ---------------------------------------------------------------------- */
17766+
17767+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17768+ struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17769+{
4a4d8108 17770+ int err, len;
53392da6 17771+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17772+ unsigned char isdir, isroot, wh;
17773+ struct inode *dir;
17774+ struct au_hnotify_args *args;
17775+ char *p, *h_child_name;
dece6358 17776+
1308ab2a 17777+ err = 0;
4a4d8108
AM
17778+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17779+ dir = igrab(hnotify->hn_aufs_inode);
17780+ if (!dir)
17781+ goto out;
1facf9fc 17782+
4a4d8108
AM
17783+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17784+ wh = 0;
17785+ h_child_name = (void *)h_child_qstr->name;
17786+ len = h_child_qstr->len;
17787+ if (h_child_name) {
17788+ if (len > AUFS_WH_PFX_LEN
17789+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17790+ h_child_name += AUFS_WH_PFX_LEN;
17791+ len -= AUFS_WH_PFX_LEN;
17792+ wh = 1;
17793+ }
1facf9fc 17794+ }
dece6358 17795+
4a4d8108
AM
17796+ isdir = 0;
17797+ if (h_child_inode)
17798+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17799+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17800+ flags[AuHn_CHILD] = 0;
17801+ if (isdir)
17802+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17803+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17804+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17805+ switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17806+ case FS_MOVED_FROM:
17807+ case FS_MOVED_TO:
17808+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17809+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17810+ /*FALLTHROUGH*/
17811+ case FS_CREATE:
fb47a38f 17812+ AuDebugOn(!h_child_name);
4a4d8108 17813+ break;
1facf9fc 17814+
4a4d8108
AM
17815+ case FS_DELETE:
17816+ /*
17817+ * aufs never be able to get this child inode.
17818+ * revalidation should be in d_revalidate()
17819+ * by checking i_nlink, i_generation or d_unhashed().
17820+ */
17821+ AuDebugOn(!h_child_name);
17822+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17823+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17824+ break;
dece6358 17825+
4a4d8108
AM
17826+ default:
17827+ AuDebugOn(1);
17828+ }
1308ab2a 17829+
4a4d8108
AM
17830+ if (wh)
17831+ h_child_inode = NULL;
1308ab2a 17832+
4a4d8108
AM
17833+ err = -ENOMEM;
17834+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17835+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17836+ if (unlikely(!args)) {
17837+ AuErr1("no memory\n");
17838+ iput(dir);
17839+ goto out;
17840+ }
17841+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17842+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17843+ args->mask = mask;
17844+ args->dir = dir;
17845+ args->h_dir = igrab(h_dir);
17846+ if (h_child_inode)
17847+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17848+ args->h_child_inode = h_child_inode;
17849+ args->h_child_nlen = len;
17850+ if (len) {
17851+ p = (void *)args;
17852+ p += sizeof(*args);
17853+ memcpy(p, h_child_name, len);
17854+ p[len] = 0;
1308ab2a 17855+ }
1308ab2a 17856+
38d290e6 17857+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17858+ f = 0;
38d290e6
JR
17859+ if (!dir->i_nlink
17860+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17861+ f = AuWkq_NEST;
17862+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17863+ if (unlikely(err)) {
17864+ pr_err("wkq %d\n", err);
17865+ iput(args->h_child_inode);
17866+ iput(args->h_dir);
17867+ iput(args->dir);
1c60b727 17868+ kfree(args);
1facf9fc 17869+ }
1facf9fc 17870+
4a4d8108 17871+out:
1facf9fc 17872+ return err;
17873+}
17874+
027c5e7a
AM
17875+/* ---------------------------------------------------------------------- */
17876+
17877+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17878+{
17879+ int err;
17880+
17881+ AuDebugOn(!(udba & AuOptMask_UDBA));
17882+
17883+ err = 0;
17884+ if (au_hnotify_op.reset_br)
17885+ err = au_hnotify_op.reset_br(udba, br, perm);
17886+
17887+ return err;
17888+}
17889+
17890+int au_hnotify_init_br(struct au_branch *br, int perm)
17891+{
17892+ int err;
17893+
17894+ err = 0;
17895+ if (au_hnotify_op.init_br)
17896+ err = au_hnotify_op.init_br(br, perm);
17897+
17898+ return err;
17899+}
17900+
17901+void au_hnotify_fin_br(struct au_branch *br)
17902+{
17903+ if (au_hnotify_op.fin_br)
17904+ au_hnotify_op.fin_br(br);
17905+}
17906+
4a4d8108
AM
17907+static void au_hn_destroy_cache(void)
17908+{
1c60b727
AM
17909+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17910+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17911+}
1308ab2a 17912+
4a4d8108 17913+int __init au_hnotify_init(void)
1facf9fc 17914+{
1308ab2a 17915+ int err;
1308ab2a 17916+
4a4d8108 17917+ err = -ENOMEM;
1c60b727
AM
17918+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17919+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17920+ err = 0;
17921+ if (au_hnotify_op.init)
17922+ err = au_hnotify_op.init();
4a4d8108
AM
17923+ if (unlikely(err))
17924+ au_hn_destroy_cache();
1308ab2a 17925+ }
1308ab2a 17926+ AuTraceErr(err);
4a4d8108 17927+ return err;
1308ab2a 17928+}
17929+
4a4d8108 17930+void au_hnotify_fin(void)
1308ab2a 17931+{
027c5e7a
AM
17932+ if (au_hnotify_op.fin)
17933+ au_hnotify_op.fin();
f0c0a007 17934+
4a4d8108 17935+ /* cf. au_cache_fin() */
1c60b727 17936+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17937+ au_hn_destroy_cache();
dece6358 17938+}
7f207e10
AM
17939diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17940--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
17941+++ linux/fs/aufs/iinfo.c 2018-07-19 09:46:13.059981241 +0200
17942@@ -0,0 +1,286 @@
17943+/* SPDX-License-Identifier: GPL-2.0 */
dece6358 17944+/*
b00004a5 17945+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
17946+ *
17947+ * This program, aufs is free software; you can redistribute it and/or modify
17948+ * it under the terms of the GNU General Public License as published by
17949+ * the Free Software Foundation; either version 2 of the License, or
17950+ * (at your option) any later version.
17951+ *
17952+ * This program is distributed in the hope that it will be useful,
17953+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17954+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17955+ * GNU General Public License for more details.
17956+ *
17957+ * You should have received a copy of the GNU General Public License
523b37e3 17958+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17959+ */
1facf9fc 17960+
dece6358 17961+/*
4a4d8108 17962+ * inode private data
dece6358 17963+ */
1facf9fc 17964+
1308ab2a 17965+#include "aufs.h"
1facf9fc 17966+
4a4d8108 17967+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17968+{
4a4d8108 17969+ struct inode *h_inode;
5afbbe0d 17970+ struct au_hinode *hinode;
1facf9fc 17971+
4a4d8108 17972+ IiMustAnyLock(inode);
1facf9fc 17973+
5afbbe0d
AM
17974+ hinode = au_hinode(au_ii(inode), bindex);
17975+ h_inode = hinode->hi_inode;
4a4d8108
AM
17976+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17977+ return h_inode;
17978+}
1facf9fc 17979+
4a4d8108
AM
17980+/* todo: hard/soft set? */
17981+void au_hiput(struct au_hinode *hinode)
17982+{
17983+ au_hn_free(hinode);
17984+ dput(hinode->hi_whdentry);
17985+ iput(hinode->hi_inode);
17986+}
1facf9fc 17987+
4a4d8108
AM
17988+unsigned int au_hi_flags(struct inode *inode, int isdir)
17989+{
17990+ unsigned int flags;
17991+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 17992+
4a4d8108
AM
17993+ flags = 0;
17994+ if (au_opt_test(mnt_flags, XINO))
17995+ au_fset_hi(flags, XINO);
17996+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17997+ au_fset_hi(flags, HNOTIFY);
17998+ return flags;
1facf9fc 17999+}
18000+
4a4d8108
AM
18001+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18002+ struct inode *h_inode, unsigned int flags)
1308ab2a 18003+{
4a4d8108
AM
18004+ struct au_hinode *hinode;
18005+ struct inode *hi;
18006+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 18007+
4a4d8108 18008+ IiMustWriteLock(inode);
dece6358 18009+
5afbbe0d 18010+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
18011+ hi = hinode->hi_inode;
18012+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18013+
18014+ if (hi)
18015+ au_hiput(hinode);
18016+ hinode->hi_inode = h_inode;
18017+ if (h_inode) {
18018+ int err;
18019+ struct super_block *sb = inode->i_sb;
18020+ struct au_branch *br;
18021+
027c5e7a
AM
18022+ AuDebugOn(inode->i_mode
18023+ && (h_inode->i_mode & S_IFMT)
18024+ != (inode->i_mode & S_IFMT));
5afbbe0d 18025+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
18026+ au_cpup_igen(inode, h_inode);
18027+ br = au_sbr(sb, bindex);
18028+ hinode->hi_id = br->br_id;
18029+ if (au_ftest_hi(flags, XINO)) {
18030+ err = au_xino_write(sb, bindex, h_inode->i_ino,
18031+ inode->i_ino);
18032+ if (unlikely(err))
18033+ AuIOErr1("failed au_xino_write() %d\n", err);
18034+ }
18035+
18036+ if (au_ftest_hi(flags, HNOTIFY)
18037+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 18038+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
18039+ if (unlikely(err))
18040+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 18041+ }
18042+ }
4a4d8108 18043+}
dece6358 18044+
4a4d8108
AM
18045+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18046+ struct dentry *h_wh)
18047+{
18048+ struct au_hinode *hinode;
dece6358 18049+
4a4d8108
AM
18050+ IiMustWriteLock(inode);
18051+
5afbbe0d 18052+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
18053+ AuDebugOn(hinode->hi_whdentry);
18054+ hinode->hi_whdentry = h_wh;
1facf9fc 18055+}
18056+
537831f9 18057+void au_update_iigen(struct inode *inode, int half)
1308ab2a 18058+{
537831f9
AM
18059+ struct au_iinfo *iinfo;
18060+ struct au_iigen *iigen;
18061+ unsigned int sigen;
18062+
18063+ sigen = au_sigen(inode->i_sb);
18064+ iinfo = au_ii(inode);
18065+ iigen = &iinfo->ii_generation;
be52b249 18066+ spin_lock(&iigen->ig_spin);
537831f9
AM
18067+ iigen->ig_generation = sigen;
18068+ if (half)
18069+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18070+ else
18071+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 18072+ spin_unlock(&iigen->ig_spin);
4a4d8108 18073+}
1facf9fc 18074+
4a4d8108
AM
18075+/* it may be called at remount time, too */
18076+void au_update_ibrange(struct inode *inode, int do_put_zero)
18077+{
18078+ struct au_iinfo *iinfo;
5afbbe0d 18079+ aufs_bindex_t bindex, bbot;
1facf9fc 18080+
5afbbe0d 18081+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18082+ IiMustWriteLock(inode);
1facf9fc 18083+
5afbbe0d
AM
18084+ iinfo = au_ii(inode);
18085+ if (do_put_zero && iinfo->ii_btop >= 0) {
18086+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18087+ bindex++) {
18088+ struct inode *h_i;
1facf9fc 18089+
5afbbe0d 18090+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
18091+ if (h_i
18092+ && !h_i->i_nlink
18093+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
18094+ au_set_h_iptr(inode, bindex, NULL, 0);
18095+ }
4a4d8108
AM
18096+ }
18097+
5afbbe0d
AM
18098+ iinfo->ii_btop = -1;
18099+ iinfo->ii_bbot = -1;
18100+ bbot = au_sbbot(inode->i_sb);
18101+ for (bindex = 0; bindex <= bbot; bindex++)
18102+ if (au_hinode(iinfo, bindex)->hi_inode) {
18103+ iinfo->ii_btop = bindex;
4a4d8108 18104+ break;
027c5e7a 18105+ }
5afbbe0d
AM
18106+ if (iinfo->ii_btop >= 0)
18107+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18108+ if (au_hinode(iinfo, bindex)->hi_inode) {
18109+ iinfo->ii_bbot = bindex;
027c5e7a
AM
18110+ break;
18111+ }
5afbbe0d 18112+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 18113+}
1facf9fc 18114+
dece6358 18115+/* ---------------------------------------------------------------------- */
1facf9fc 18116+
4a4d8108 18117+void au_icntnr_init_once(void *_c)
dece6358 18118+{
4a4d8108
AM
18119+ struct au_icntnr *c = _c;
18120+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 18121+
be52b249 18122+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
18123+ au_rw_init(&iinfo->ii_rwsem);
18124+ inode_init_once(&c->vfs_inode);
18125+}
1facf9fc 18126+
5afbbe0d
AM
18127+void au_hinode_init(struct au_hinode *hinode)
18128+{
18129+ hinode->hi_inode = NULL;
18130+ hinode->hi_id = -1;
18131+ au_hn_init(hinode);
18132+ hinode->hi_whdentry = NULL;
18133+}
18134+
4a4d8108
AM
18135+int au_iinfo_init(struct inode *inode)
18136+{
18137+ struct au_iinfo *iinfo;
18138+ struct super_block *sb;
5afbbe0d 18139+ struct au_hinode *hi;
4a4d8108 18140+ int nbr, i;
1facf9fc 18141+
4a4d8108
AM
18142+ sb = inode->i_sb;
18143+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 18144+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
18145+ if (unlikely(nbr <= 0))
18146+ nbr = 1;
5afbbe0d
AM
18147+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18148+ if (hi) {
7f207e10 18149+ au_ninodes_inc(sb);
5afbbe0d
AM
18150+
18151+ iinfo->ii_hinode = hi;
18152+ for (i = 0; i < nbr; i++, hi++)
18153+ au_hinode_init(hi);
1facf9fc 18154+
537831f9 18155+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
18156+ iinfo->ii_btop = -1;
18157+ iinfo->ii_bbot = -1;
4a4d8108
AM
18158+ iinfo->ii_vdir = NULL;
18159+ return 0;
1308ab2a 18160+ }
4a4d8108
AM
18161+ return -ENOMEM;
18162+}
1facf9fc 18163+
e2f27e51 18164+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 18165+{
5afbbe0d 18166+ int err, i;
4a4d8108 18167+ struct au_hinode *hip;
1facf9fc 18168+
4a4d8108
AM
18169+ AuRwMustWriteLock(&iinfo->ii_rwsem);
18170+
18171+ err = -ENOMEM;
e2f27e51
AM
18172+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18173+ may_shrink);
4a4d8108
AM
18174+ if (hip) {
18175+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18176+ i = iinfo->ii_bbot + 1;
18177+ hip += i;
18178+ for (; i < nbr; i++, hip++)
18179+ au_hinode_init(hip);
4a4d8108 18180+ err = 0;
1308ab2a 18181+ }
4a4d8108 18182+
1308ab2a 18183+ return err;
1facf9fc 18184+}
18185+
4a4d8108 18186+void au_iinfo_fin(struct inode *inode)
1facf9fc 18187+{
4a4d8108
AM
18188+ struct au_iinfo *iinfo;
18189+ struct au_hinode *hi;
18190+ struct super_block *sb;
5afbbe0d 18191+ aufs_bindex_t bindex, bbot;
b752ccd1 18192+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18193+
5afbbe0d 18194+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18195+
b752ccd1 18196+ sb = inode->i_sb;
7f207e10 18197+ au_ninodes_dec(sb);
b752ccd1
AM
18198+ if (si_pid_test(sb))
18199+ au_xino_delete_inode(inode, unlinked);
18200+ else {
18201+ /*
18202+ * it is safe to hide the dependency between sbinfo and
18203+ * sb->s_umount.
18204+ */
18205+ lockdep_off();
18206+ si_noflush_read_lock(sb);
18207+ au_xino_delete_inode(inode, unlinked);
18208+ si_read_unlock(sb);
18209+ lockdep_on();
18210+ }
18211+
5afbbe0d 18212+ iinfo = au_ii(inode);
4a4d8108 18213+ if (iinfo->ii_vdir)
1c60b727 18214+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18215+
5afbbe0d 18216+ bindex = iinfo->ii_btop;
b752ccd1 18217+ if (bindex >= 0) {
5afbbe0d
AM
18218+ hi = au_hinode(iinfo, bindex);
18219+ bbot = iinfo->ii_bbot;
18220+ while (bindex++ <= bbot) {
b752ccd1 18221+ if (hi->hi_inode)
4a4d8108 18222+ au_hiput(hi);
4a4d8108
AM
18223+ hi++;
18224+ }
18225+ }
1c60b727 18226+ kfree(iinfo->ii_hinode);
4a4d8108 18227+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18228+}
7f207e10
AM
18229diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18230--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
18231+++ linux/fs/aufs/inode.c 2018-07-19 09:46:13.059981241 +0200
18232@@ -0,0 +1,528 @@
18233+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 18234+/*
b00004a5 18235+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
18236+ *
18237+ * This program, aufs is free software; you can redistribute it and/or modify
18238+ * it under the terms of the GNU General Public License as published by
18239+ * the Free Software Foundation; either version 2 of the License, or
18240+ * (at your option) any later version.
18241+ *
18242+ * This program is distributed in the hope that it will be useful,
18243+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18244+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18245+ * GNU General Public License for more details.
18246+ *
18247+ * You should have received a copy of the GNU General Public License
523b37e3 18248+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18249+ */
1facf9fc 18250+
4a4d8108
AM
18251+/*
18252+ * inode functions
18253+ */
1facf9fc 18254+
4a4d8108 18255+#include "aufs.h"
1308ab2a 18256+
4a4d8108
AM
18257+struct inode *au_igrab(struct inode *inode)
18258+{
18259+ if (inode) {
18260+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18261+ ihold(inode);
1facf9fc 18262+ }
4a4d8108
AM
18263+ return inode;
18264+}
1facf9fc 18265+
4a4d8108
AM
18266+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18267+{
18268+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18269+ au_update_iigen(inode, /*half*/1);
4a4d8108 18270+ if (do_version)
be118d29 18271+ inode_inc_iversion(inode);
dece6358 18272+}
1facf9fc 18273+
027c5e7a 18274+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18275+{
e2f27e51 18276+ int err, e, nbr;
027c5e7a 18277+ umode_t type;
4a4d8108 18278+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18279+ struct super_block *sb;
4a4d8108 18280+ struct au_iinfo *iinfo;
027c5e7a 18281+ struct au_hinode *p, *q, tmp;
1facf9fc 18282+
5afbbe0d 18283+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18284+ IiMustWriteLock(inode);
1facf9fc 18285+
027c5e7a 18286+ *update = 0;
4a4d8108 18287+ sb = inode->i_sb;
e2f27e51 18288+ nbr = au_sbbot(sb) + 1;
027c5e7a 18289+ type = inode->i_mode & S_IFMT;
4a4d8108 18290+ iinfo = au_ii(inode);
e2f27e51 18291+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18292+ if (unlikely(err))
1308ab2a 18293+ goto out;
1facf9fc 18294+
5afbbe0d
AM
18295+ AuDebugOn(iinfo->ii_btop < 0);
18296+ p = au_hinode(iinfo, iinfo->ii_btop);
18297+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18298+ bindex++, p++) {
18299+ if (!p->hi_inode)
18300+ continue;
1facf9fc 18301+
027c5e7a 18302+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18303+ new_bindex = au_br_index(sb, p->hi_id);
18304+ if (new_bindex == bindex)
18305+ continue;
1facf9fc 18306+
4a4d8108 18307+ if (new_bindex < 0) {
027c5e7a 18308+ *update = 1;
4a4d8108
AM
18309+ au_hiput(p);
18310+ p->hi_inode = NULL;
18311+ continue;
1308ab2a 18312+ }
4a4d8108 18313+
5afbbe0d
AM
18314+ if (new_bindex < iinfo->ii_btop)
18315+ iinfo->ii_btop = new_bindex;
18316+ if (iinfo->ii_bbot < new_bindex)
18317+ iinfo->ii_bbot = new_bindex;
4a4d8108 18318+ /* swap two lower inode, and loop again */
5afbbe0d 18319+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18320+ tmp = *q;
18321+ *q = *p;
18322+ *p = tmp;
18323+ if (tmp.hi_inode) {
18324+ bindex--;
18325+ p--;
1308ab2a 18326+ }
18327+ }
4a4d8108 18328+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18329+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18330+ e = au_dy_irefresh(inode);
18331+ if (unlikely(e && !err))
18332+ err = e;
1facf9fc 18333+
4f0767ce 18334+out:
027c5e7a
AM
18335+ AuTraceErr(err);
18336+ return err;
18337+}
18338+
b95c5147
AM
18339+void au_refresh_iop(struct inode *inode, int force_getattr)
18340+{
18341+ int type;
18342+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18343+ const struct inode_operations *iop
18344+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18345+
18346+ if (inode->i_op == iop)
18347+ return;
18348+
18349+ switch (inode->i_mode & S_IFMT) {
18350+ case S_IFDIR:
18351+ type = AuIop_DIR;
18352+ break;
18353+ case S_IFLNK:
18354+ type = AuIop_SYMLINK;
18355+ break;
18356+ default:
18357+ type = AuIop_OTHER;
18358+ break;
18359+ }
18360+
18361+ inode->i_op = iop + type;
18362+ /* unnecessary smp_wmb() */
18363+}
18364+
027c5e7a
AM
18365+int au_refresh_hinode_self(struct inode *inode)
18366+{
18367+ int err, update;
18368+
18369+ err = au_ii_refresh(inode, &update);
18370+ if (!err)
18371+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18372+
18373+ AuTraceErr(err);
4a4d8108
AM
18374+ return err;
18375+}
1facf9fc 18376+
4a4d8108
AM
18377+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18378+{
027c5e7a 18379+ int err, e, update;
4a4d8108 18380+ unsigned int flags;
027c5e7a 18381+ umode_t mode;
5afbbe0d 18382+ aufs_bindex_t bindex, bbot;
027c5e7a 18383+ unsigned char isdir;
4a4d8108
AM
18384+ struct au_hinode *p;
18385+ struct au_iinfo *iinfo;
1facf9fc 18386+
027c5e7a 18387+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18388+ if (unlikely(err))
18389+ goto out;
18390+
18391+ update = 0;
18392+ iinfo = au_ii(inode);
5afbbe0d 18393+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18394+ mode = (inode->i_mode & S_IFMT);
18395+ isdir = S_ISDIR(mode);
4a4d8108 18396+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18397+ bbot = au_dbbot(dentry);
18398+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18399+ struct inode *h_i, *h_inode;
4a4d8108
AM
18400+ struct dentry *h_d;
18401+
18402+ h_d = au_h_dptr(dentry, bindex);
5527c038 18403+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18404+ continue;
18405+
5527c038
JR
18406+ h_inode = d_inode(h_d);
18407+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18408+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18409+ h_i = au_h_iptr(inode, bindex);
18410+ if (h_i) {
5527c038 18411+ if (h_i == h_inode)
4a4d8108
AM
18412+ continue;
18413+ err = -EIO;
18414+ break;
18415+ }
18416+ }
5afbbe0d
AM
18417+ if (bindex < iinfo->ii_btop)
18418+ iinfo->ii_btop = bindex;
18419+ if (iinfo->ii_bbot < bindex)
18420+ iinfo->ii_bbot = bindex;
5527c038 18421+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18422+ update = 1;
1308ab2a 18423+ }
4a4d8108
AM
18424+ au_update_ibrange(inode, /*do_put_zero*/0);
18425+ e = au_dy_irefresh(inode);
18426+ if (unlikely(e && !err))
18427+ err = e;
027c5e7a
AM
18428+ if (!err)
18429+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18430+
4f0767ce 18431+out:
4a4d8108 18432+ AuTraceErr(err);
1308ab2a 18433+ return err;
dece6358
AM
18434+}
18435+
4a4d8108 18436+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18437+{
4a4d8108
AM
18438+ int err;
18439+ unsigned int flags;
18440+ umode_t mode;
5afbbe0d 18441+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18442+ unsigned char isdir;
18443+ struct dentry *h_dentry;
18444+ struct inode *h_inode;
18445+ struct au_iinfo *iinfo;
b95c5147 18446+ struct inode_operations *iop;
dece6358 18447+
4a4d8108 18448+ IiMustWriteLock(inode);
dece6358 18449+
4a4d8108
AM
18450+ err = 0;
18451+ isdir = 0;
b95c5147 18452+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18453+ btop = au_dbtop(dentry);
18454+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18455+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18456+ mode = h_inode->i_mode;
18457+ switch (mode & S_IFMT) {
18458+ case S_IFREG:
18459+ btail = au_dbtail(dentry);
b95c5147 18460+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18461+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18462+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18463+ if (unlikely(err))
18464+ goto out;
18465+ break;
18466+ case S_IFDIR:
18467+ isdir = 1;
18468+ btail = au_dbtaildir(dentry);
b95c5147 18469+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18470+ inode->i_fop = &aufs_dir_fop;
18471+ break;
18472+ case S_IFLNK:
18473+ btail = au_dbtail(dentry);
b95c5147 18474+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18475+ break;
18476+ case S_IFBLK:
18477+ case S_IFCHR:
18478+ case S_IFIFO:
18479+ case S_IFSOCK:
18480+ btail = au_dbtail(dentry);
b95c5147 18481+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18482+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18483+ break;
18484+ default:
18485+ AuIOErr("Unknown file type 0%o\n", mode);
18486+ err = -EIO;
1308ab2a 18487+ goto out;
4a4d8108 18488+ }
dece6358 18489+
4a4d8108
AM
18490+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18491+ flags = au_hi_flags(inode, isdir);
18492+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18493+ && au_ftest_hi(flags, HNOTIFY)
18494+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18495+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18496+ au_fclr_hi(flags, HNOTIFY);
18497+ iinfo = au_ii(inode);
5afbbe0d
AM
18498+ iinfo->ii_btop = btop;
18499+ iinfo->ii_bbot = btail;
18500+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18501+ h_dentry = au_h_dptr(dentry, bindex);
18502+ if (h_dentry)
18503+ au_set_h_iptr(inode, bindex,
5527c038 18504+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18505+ }
18506+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18507+ /*
18508+ * to force calling aufs_get_acl() every time,
18509+ * do not call cache_no_acl() for aufs inode.
18510+ */
dece6358 18511+
4f0767ce 18512+out:
4a4d8108
AM
18513+ return err;
18514+}
dece6358 18515+
027c5e7a
AM
18516+/*
18517+ * successful returns with iinfo write_locked
18518+ * minus: errno
18519+ * zero: success, matched
18520+ * plus: no error, but unmatched
18521+ */
18522+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18523+{
18524+ int err;
cfc41e69 18525+ unsigned int gen, igflags;
5afbbe0d 18526+ aufs_bindex_t bindex, bbot;
4a4d8108 18527+ struct inode *h_inode, *h_dinode;
5527c038 18528+ struct dentry *h_dentry;
dece6358 18529+
4a4d8108
AM
18530+ /*
18531+ * before this function, if aufs got any iinfo lock, it must be only
18532+ * one, the parent dir.
18533+ * it can happen by UDBA and the obsoleted inode number.
18534+ */
18535+ err = -EIO;
18536+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18537+ goto out;
18538+
027c5e7a 18539+ err = 1;
4a4d8108 18540+ ii_write_lock_new_child(inode);
5afbbe0d 18541+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18542+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18543+ bbot = au_ibbot(inode);
18544+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18545+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18546+ if (!h_inode || h_inode != h_dinode)
18547+ continue;
18548+
18549+ err = 0;
cfc41e69 18550+ gen = au_iigen(inode, &igflags);
537831f9 18551+ if (gen == au_digen(dentry)
cfc41e69 18552+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18553+ break;
537831f9
AM
18554+
18555+ /* fully refresh inode using dentry */
18556+ err = au_refresh_hinode(inode, dentry);
18557+ if (!err)
18558+ au_update_iigen(inode, /*half*/0);
18559+ break;
1facf9fc 18560+ }
dece6358 18561+
4a4d8108
AM
18562+ if (unlikely(err))
18563+ ii_write_unlock(inode);
4f0767ce 18564+out:
1facf9fc 18565+ return err;
18566+}
1facf9fc 18567+
4a4d8108
AM
18568+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18569+ unsigned int d_type, ino_t *ino)
1facf9fc 18570+{
521ced18
JR
18571+ int err, idx;
18572+ const int isnondir = d_type != DT_DIR;
1facf9fc 18573+
b752ccd1 18574+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18575+ if (isnondir) {
18576+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18577+ if (unlikely(err))
18578+ goto out;
4a4d8108 18579+ }
521ced18 18580+
4a4d8108
AM
18581+ err = au_xino_read(sb, bindex, h_ino, ino);
18582+ if (unlikely(err))
521ced18 18583+ goto out_xinondir;
1308ab2a 18584+
4a4d8108
AM
18585+ if (!*ino) {
18586+ err = -EIO;
18587+ *ino = au_xino_new_ino(sb);
18588+ if (unlikely(!*ino))
521ced18 18589+ goto out_xinondir;
4a4d8108
AM
18590+ err = au_xino_write(sb, bindex, h_ino, *ino);
18591+ if (unlikely(err))
521ced18 18592+ goto out_xinondir;
1308ab2a 18593+ }
1facf9fc 18594+
521ced18
JR
18595+out_xinondir:
18596+ if (isnondir && idx >= 0)
18597+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18598+out:
1facf9fc 18599+ return err;
18600+}
18601+
4a4d8108
AM
18602+/* successful returns with iinfo write_locked */
18603+/* todo: return with unlocked? */
18604+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18605+{
5527c038 18606+ struct inode *inode, *h_inode;
4a4d8108
AM
18607+ struct dentry *h_dentry;
18608+ struct super_block *sb;
18609+ ino_t h_ino, ino;
521ced18 18610+ int err, idx, hlinked;
5afbbe0d 18611+ aufs_bindex_t btop;
1facf9fc 18612+
4a4d8108 18613+ sb = dentry->d_sb;
5afbbe0d
AM
18614+ btop = au_dbtop(dentry);
18615+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18616+ h_inode = d_inode(h_dentry);
18617+ h_ino = h_inode->i_ino;
521ced18 18618+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18619+
521ced18 18620+new_ino:
b752ccd1
AM
18621+ /*
18622+ * stop 'race'-ing between hardlinks under different
18623+ * parents.
18624+ */
521ced18
JR
18625+ if (hlinked) {
18626+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18627+ inode = ERR_PTR(err);
18628+ if (unlikely(err))
18629+ goto out;
18630+ }
b752ccd1 18631+
5afbbe0d 18632+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18633+ inode = ERR_PTR(err);
18634+ if (unlikely(err))
521ced18 18635+ goto out_xinondir;
b752ccd1 18636+
4a4d8108
AM
18637+ if (!ino) {
18638+ ino = au_xino_new_ino(sb);
18639+ if (unlikely(!ino)) {
18640+ inode = ERR_PTR(-EIO);
521ced18 18641+ goto out_xinondir;
dece6358
AM
18642+ }
18643+ }
1facf9fc 18644+
4a4d8108
AM
18645+ AuDbg("i%lu\n", (unsigned long)ino);
18646+ inode = au_iget_locked(sb, ino);
18647+ err = PTR_ERR(inode);
18648+ if (IS_ERR(inode))
521ced18 18649+ goto out_xinondir;
1facf9fc 18650+
4a4d8108
AM
18651+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18652+ if (inode->i_state & I_NEW) {
18653+ ii_write_lock_new_child(inode);
18654+ err = set_inode(inode, dentry);
18655+ if (!err) {
18656+ unlock_new_inode(inode);
521ced18 18657+ goto out_xinondir; /* success */
4a4d8108 18658+ }
1308ab2a 18659+
027c5e7a
AM
18660+ /*
18661+ * iget_failed() calls iput(), but we need to call
18662+ * ii_write_unlock() after iget_failed(). so dirty hack for
18663+ * i_count.
18664+ */
18665+ atomic_inc(&inode->i_count);
4a4d8108 18666+ iget_failed(inode);
027c5e7a 18667+ ii_write_unlock(inode);
5afbbe0d 18668+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18669+ /* ignore this error */
18670+ goto out_iput;
18671+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18672+ /*
18673+ * horrible race condition between lookup, readdir and copyup
18674+ * (or something).
18675+ */
521ced18
JR
18676+ if (hlinked && idx >= 0)
18677+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18678+ err = reval_inode(inode, dentry);
18679+ if (unlikely(err < 0)) {
521ced18 18680+ hlinked = 0;
027c5e7a
AM
18681+ goto out_iput;
18682+ }
521ced18 18683+ if (!err)
4a4d8108 18684+ goto out; /* success */
521ced18
JR
18685+ else if (hlinked && idx >= 0) {
18686+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18687+ if (unlikely(err)) {
18688+ iput(inode);
18689+ inode = ERR_PTR(err);
18690+ goto out;
18691+ }
18692+ }
4a4d8108
AM
18693+ }
18694+
5527c038 18695+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18696+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18697+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18698+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18699+ (unsigned long)h_ino, (unsigned long)ino);
18700+ ino = 0;
5afbbe0d 18701+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18702+ if (!err) {
18703+ iput(inode);
521ced18
JR
18704+ if (hlinked && idx >= 0)
18705+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18706+ goto new_ino;
18707+ }
1308ab2a 18708+
4f0767ce 18709+out_iput:
4a4d8108 18710+ iput(inode);
4a4d8108 18711+ inode = ERR_PTR(err);
521ced18
JR
18712+out_xinondir:
18713+ if (hlinked && idx >= 0)
18714+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18715+out:
4a4d8108 18716+ return inode;
1facf9fc 18717+}
18718+
4a4d8108 18719+/* ---------------------------------------------------------------------- */
1facf9fc 18720+
4a4d8108
AM
18721+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18722+ struct inode *inode)
18723+{
18724+ int err;
076b876e 18725+ struct inode *hi;
1facf9fc 18726+
4a4d8108 18727+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18728+
4a4d8108
AM
18729+ /* pseudo-link after flushed may happen out of bounds */
18730+ if (!err
18731+ && inode
5afbbe0d
AM
18732+ && au_ibtop(inode) <= bindex
18733+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18734+ /*
18735+ * permission check is unnecessary since vfsub routine
18736+ * will be called later
18737+ */
076b876e 18738+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18739+ if (hi)
18740+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18741+ }
18742+
4a4d8108
AM
18743+ return err;
18744+}
dece6358 18745+
4a4d8108
AM
18746+int au_test_h_perm(struct inode *h_inode, int mask)
18747+{
2dfbb274 18748+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
18749+ return 0;
18750+ return inode_permission(h_inode, mask);
18751+}
1facf9fc 18752+
4a4d8108
AM
18753+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18754+{
18755+ if (au_test_nfs(h_inode->i_sb)
18756+ && (mask & MAY_WRITE)
18757+ && S_ISDIR(h_inode->i_mode))
18758+ mask |= MAY_READ; /* force permission check */
18759+ return au_test_h_perm(h_inode, mask);
1facf9fc 18760+}
7f207e10
AM
18761diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18762--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
18763+++ linux/fs/aufs/inode.h 2018-07-19 09:46:13.059981241 +0200
18764@@ -0,0 +1,696 @@
18765+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 18766+/*
b00004a5 18767+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
18768+ *
18769+ * This program, aufs is free software; you can redistribute it and/or modify
18770+ * it under the terms of the GNU General Public License as published by
18771+ * the Free Software Foundation; either version 2 of the License, or
18772+ * (at your option) any later version.
18773+ *
18774+ * This program is distributed in the hope that it will be useful,
18775+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18776+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18777+ * GNU General Public License for more details.
18778+ *
18779+ * You should have received a copy of the GNU General Public License
523b37e3 18780+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18781+ */
1facf9fc 18782+
1308ab2a 18783+/*
4a4d8108 18784+ * inode operations
1308ab2a 18785+ */
dece6358 18786+
4a4d8108
AM
18787+#ifndef __AUFS_INODE_H__
18788+#define __AUFS_INODE_H__
dece6358 18789+
4a4d8108 18790+#ifdef __KERNEL__
1308ab2a 18791+
4a4d8108 18792+#include <linux/fsnotify.h>
4a4d8108 18793+#include "rwsem.h"
1308ab2a 18794+
4a4d8108 18795+struct vfsmount;
1facf9fc 18796+
4a4d8108
AM
18797+struct au_hnotify {
18798+#ifdef CONFIG_AUFS_HNOTIFY
18799+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18800+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18801+ struct fsnotify_mark hn_mark;
4a4d8108 18802+#endif
1c60b727 18803+ struct inode *hn_aufs_inode; /* no get/put */
4a4d8108
AM
18804+#endif
18805+} ____cacheline_aligned_in_smp;
1facf9fc 18806+
4a4d8108
AM
18807+struct au_hinode {
18808+ struct inode *hi_inode;
18809+ aufs_bindex_t hi_id;
18810+#ifdef CONFIG_AUFS_HNOTIFY
18811+ struct au_hnotify *hi_notify;
18812+#endif
dece6358 18813+
4a4d8108
AM
18814+ /* reference to the copied-up whiteout with get/put */
18815+ struct dentry *hi_whdentry;
18816+};
dece6358 18817+
537831f9
AM
18818+/* ig_flags */
18819+#define AuIG_HALF_REFRESHED 1
18820+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18821+#define au_ig_fset(flags, name) \
18822+ do { (flags) |= AuIG_##name; } while (0)
18823+#define au_ig_fclr(flags, name) \
18824+ do { (flags) &= ~AuIG_##name; } while (0)
18825+
18826+struct au_iigen {
be52b249 18827+ spinlock_t ig_spin;
537831f9
AM
18828+ __u32 ig_generation, ig_flags;
18829+};
18830+
4a4d8108
AM
18831+struct au_vdir;
18832+struct au_iinfo {
7a9e40b8 18833+ struct au_iigen ii_generation;
4a4d8108 18834+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18835+
4a4d8108 18836+ struct au_rwsem ii_rwsem;
5afbbe0d 18837+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18838+ __u32 ii_higen;
18839+ struct au_hinode *ii_hinode;
18840+ struct au_vdir *ii_vdir;
18841+};
1facf9fc 18842+
4a4d8108
AM
18843+struct au_icntnr {
18844+ struct au_iinfo iinfo;
18845+ struct inode vfs_inode;
8b6a4947 18846+ struct hlist_bl_node plink;
4a4d8108 18847+} ____cacheline_aligned_in_smp;
1308ab2a 18848+
4a4d8108
AM
18849+/* au_pin flags */
18850+#define AuPin_DI_LOCKED 1
18851+#define AuPin_MNT_WRITE (1 << 1)
18852+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18853+#define au_fset_pin(flags, name) \
18854+ do { (flags) |= AuPin_##name; } while (0)
18855+#define au_fclr_pin(flags, name) \
18856+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18857+
18858+struct au_pin {
18859+ /* input */
18860+ struct dentry *dentry;
18861+ unsigned int udba;
18862+ unsigned char lsc_di, lsc_hi, flags;
18863+ aufs_bindex_t bindex;
18864+
18865+ /* output */
18866+ struct dentry *parent;
18867+ struct au_hinode *hdir;
18868+ struct vfsmount *h_mnt;
86dc4139
AM
18869+
18870+ /* temporary unlock/relock for copyup */
18871+ struct dentry *h_dentry, *h_parent;
18872+ struct au_branch *br;
18873+ struct task_struct *task;
4a4d8108 18874+};
1facf9fc 18875+
86dc4139 18876+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18877+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18878+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18879+void au_pin_hdir_acquire_nest(struct au_pin *p);
18880+void au_pin_hdir_release(struct au_pin *p);
18881+
1308ab2a 18882+/* ---------------------------------------------------------------------- */
18883+
4a4d8108 18884+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18885+{
5afbbe0d
AM
18886+ BUG_ON(is_bad_inode(inode));
18887+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18888+}
1facf9fc 18889+
4a4d8108 18890+/* ---------------------------------------------------------------------- */
1facf9fc 18891+
4a4d8108
AM
18892+/* inode.c */
18893+struct inode *au_igrab(struct inode *inode);
b95c5147 18894+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18895+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18896+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18897+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18898+ unsigned int d_type, ino_t *ino);
18899+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18900+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18901+ struct inode *inode);
18902+int au_test_h_perm(struct inode *h_inode, int mask);
18903+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 18904+
4a4d8108
AM
18905+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18906+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18907+{
18908+#ifdef CONFIG_AUFS_SHWH
18909+ return au_ino(sb, bindex, h_ino, d_type, ino);
18910+#else
18911+ return 0;
18912+#endif
18913+}
1facf9fc 18914+
4a4d8108 18915+/* i_op.c */
b95c5147
AM
18916+enum {
18917+ AuIop_SYMLINK,
18918+ AuIop_DIR,
18919+ AuIop_OTHER,
18920+ AuIop_Last
18921+};
18922+extern struct inode_operations aufs_iop[AuIop_Last],
18923+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18924+
4a4d8108
AM
18925+/* au_wr_dir flags */
18926+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18927+#define AuWrDir_ISDIR (1 << 1)
18928+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18929+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18930+#define au_fset_wrdir(flags, name) \
18931+ do { (flags) |= AuWrDir_##name; } while (0)
18932+#define au_fclr_wrdir(flags, name) \
18933+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18934+
4a4d8108
AM
18935+struct au_wr_dir_args {
18936+ aufs_bindex_t force_btgt;
18937+ unsigned char flags;
18938+};
18939+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18940+ struct au_wr_dir_args *args);
dece6358 18941+
4a4d8108
AM
18942+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18943+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18944+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18945+ unsigned int udba, unsigned char flags);
18946+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18947+ unsigned int udba, unsigned char flags) __must_check;
18948+int au_do_pin(struct au_pin *pin) __must_check;
18949+void au_unpin(struct au_pin *pin);
c1595e42
JR
18950+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18951+
18952+#define AuIcpup_DID_CPUP 1
18953+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18954+#define au_fset_icpup(flags, name) \
18955+ do { (flags) |= AuIcpup_##name; } while (0)
18956+#define au_fclr_icpup(flags, name) \
18957+ do { (flags) &= ~AuIcpup_##name; } while (0)
18958+
18959+struct au_icpup_args {
18960+ unsigned char flags;
18961+ unsigned char pin_flags;
18962+ aufs_bindex_t btgt;
18963+ unsigned int udba;
18964+ struct au_pin pin;
18965+ struct path h_path;
18966+ struct inode *h_inode;
18967+};
18968+
18969+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18970+ struct au_icpup_args *a);
18971+
a2654f78
AM
18972+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
18973+ int locked);
1facf9fc 18974+
4a4d8108
AM
18975+/* i_op_add.c */
18976+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18977+ struct dentry *h_parent, int isdir);
7eafdf33
AM
18978+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18979+ dev_t dev);
4a4d8108 18980+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 18981+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 18982+ bool want_excl);
b912730e
AM
18983+struct vfsub_aopen_args;
18984+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18985+ struct vfsub_aopen_args *args);
38d290e6 18986+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
18987+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18988+ struct dentry *dentry);
7eafdf33 18989+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 18990+
4a4d8108
AM
18991+/* i_op_del.c */
18992+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18993+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18994+ struct dentry *h_parent, int isdir);
18995+int aufs_unlink(struct inode *dir, struct dentry *dentry);
18996+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 18997+
4a4d8108
AM
18998+/* i_op_ren.c */
18999+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
19000+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
f2c43d5f
AM
19001+ struct inode *dir, struct dentry *dentry,
19002+ unsigned int flags);
1facf9fc 19003+
4a4d8108
AM
19004+/* iinfo.c */
19005+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
19006+void au_hiput(struct au_hinode *hinode);
19007+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19008+ struct dentry *h_wh);
19009+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 19010+
4a4d8108
AM
19011+/* hinode flags */
19012+#define AuHi_XINO 1
19013+#define AuHi_HNOTIFY (1 << 1)
19014+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
19015+#define au_fset_hi(flags, name) \
19016+ do { (flags) |= AuHi_##name; } while (0)
19017+#define au_fclr_hi(flags, name) \
19018+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 19019+
4a4d8108
AM
19020+#ifndef CONFIG_AUFS_HNOTIFY
19021+#undef AuHi_HNOTIFY
19022+#define AuHi_HNOTIFY 0
19023+#endif
1facf9fc 19024+
4a4d8108
AM
19025+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19026+ struct inode *h_inode, unsigned int flags);
1facf9fc 19027+
537831f9 19028+void au_update_iigen(struct inode *inode, int half);
4a4d8108 19029+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 19030+
4a4d8108 19031+void au_icntnr_init_once(void *_c);
5afbbe0d 19032+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
19033+int au_iinfo_init(struct inode *inode);
19034+void au_iinfo_fin(struct inode *inode);
e2f27e51 19035+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 19036+
e49829fe 19037+#ifdef CONFIG_PROC_FS
4a4d8108 19038+/* plink.c */
e49829fe 19039+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 19040+struct au_sbinfo;
e49829fe
JR
19041+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
19042+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
19043+#ifdef CONFIG_AUFS_DEBUG
19044+void au_plink_list(struct super_block *sb);
19045+#else
19046+AuStubVoid(au_plink_list, struct super_block *sb)
19047+#endif
19048+int au_plink_test(struct inode *inode);
19049+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
19050+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
19051+ struct dentry *h_dentry);
e49829fe
JR
19052+void au_plink_put(struct super_block *sb, int verbose);
19053+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 19054+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
19055+#else
19056+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
19057+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
19058+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
19059+AuStubVoid(au_plink_list, struct super_block *sb);
19060+AuStubInt0(au_plink_test, struct inode *inode);
19061+AuStub(struct dentry *, au_plink_lkup, return NULL,
19062+ struct inode *inode, aufs_bindex_t bindex);
19063+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19064+ struct dentry *h_dentry);
19065+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19066+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19067+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19068+#endif /* CONFIG_PROC_FS */
1facf9fc 19069+
c1595e42
JR
19070+#ifdef CONFIG_AUFS_XATTR
19071+/* xattr.c */
7e9cd9fe
AM
19072+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19073+ unsigned int verbose);
c1595e42 19074+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 19075+void au_xattr_init(struct super_block *sb);
c1595e42
JR
19076+#else
19077+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 19078+ int ignore_flags, unsigned int verbose);
f2c43d5f 19079+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
19080+#endif
19081+
19082+#ifdef CONFIG_FS_POSIX_ACL
19083+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19084+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19085+#endif
19086+
19087+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19088+enum {
19089+ AU_XATTR_SET,
c1595e42
JR
19090+ AU_ACL_SET
19091+};
19092+
f2c43d5f 19093+struct au_sxattr {
c1595e42
JR
19094+ int type;
19095+ union {
19096+ struct {
19097+ const char *name;
19098+ const void *value;
19099+ size_t size;
19100+ int flags;
19101+ } set;
19102+ struct {
c1595e42
JR
19103+ struct posix_acl *acl;
19104+ int type;
19105+ } acl_set;
19106+ } u;
19107+};
f2c43d5f
AM
19108+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19109+ struct au_sxattr *arg);
c1595e42
JR
19110+#endif
19111+
4a4d8108 19112+/* ---------------------------------------------------------------------- */
1308ab2a 19113+
4a4d8108
AM
19114+/* lock subclass for iinfo */
19115+enum {
19116+ AuLsc_II_CHILD, /* child first */
19117+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
19118+ AuLsc_II_CHILD3, /* copyup dirs */
19119+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
19120+ AuLsc_II_PARENT2,
19121+ AuLsc_II_PARENT3, /* copyup dirs */
19122+ AuLsc_II_NEW_CHILD
19123+};
1308ab2a 19124+
1facf9fc 19125+/*
4a4d8108
AM
19126+ * ii_read_lock_child, ii_write_lock_child,
19127+ * ii_read_lock_child2, ii_write_lock_child2,
19128+ * ii_read_lock_child3, ii_write_lock_child3,
19129+ * ii_read_lock_parent, ii_write_lock_parent,
19130+ * ii_read_lock_parent2, ii_write_lock_parent2,
19131+ * ii_read_lock_parent3, ii_write_lock_parent3,
19132+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 19133+ */
4a4d8108
AM
19134+#define AuReadLockFunc(name, lsc) \
19135+static inline void ii_read_lock_##name(struct inode *i) \
19136+{ \
19137+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19138+}
19139+
19140+#define AuWriteLockFunc(name, lsc) \
19141+static inline void ii_write_lock_##name(struct inode *i) \
19142+{ \
19143+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19144+}
19145+
19146+#define AuRWLockFuncs(name, lsc) \
19147+ AuReadLockFunc(name, lsc) \
19148+ AuWriteLockFunc(name, lsc)
19149+
19150+AuRWLockFuncs(child, CHILD);
19151+AuRWLockFuncs(child2, CHILD2);
19152+AuRWLockFuncs(child3, CHILD3);
19153+AuRWLockFuncs(parent, PARENT);
19154+AuRWLockFuncs(parent2, PARENT2);
19155+AuRWLockFuncs(parent3, PARENT3);
19156+AuRWLockFuncs(new_child, NEW_CHILD);
19157+
19158+#undef AuReadLockFunc
19159+#undef AuWriteLockFunc
19160+#undef AuRWLockFuncs
1facf9fc 19161+
8b6a4947
AM
19162+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19163+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19164+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19165+
4a4d8108
AM
19166+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19167+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19168+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19169+
4a4d8108 19170+/* ---------------------------------------------------------------------- */
1308ab2a 19171+
027c5e7a
AM
19172+static inline void au_icntnr_init(struct au_icntnr *c)
19173+{
19174+#ifdef CONFIG_AUFS_DEBUG
19175+ c->vfs_inode.i_mode = 0;
19176+#endif
19177+}
19178+
cfc41e69 19179+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19180+{
537831f9
AM
19181+ unsigned int gen;
19182+ struct au_iinfo *iinfo;
be52b249 19183+ struct au_iigen *iigen;
537831f9
AM
19184+
19185+ iinfo = au_ii(inode);
be52b249
AM
19186+ iigen = &iinfo->ii_generation;
19187+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19188+ if (igflags)
19189+ *igflags = iigen->ig_flags;
be52b249
AM
19190+ gen = iigen->ig_generation;
19191+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19192+
19193+ return gen;
4a4d8108 19194+}
1308ab2a 19195+
4a4d8108
AM
19196+/* tiny test for inode number */
19197+/* tmpfs generation is too rough */
19198+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19199+{
19200+ struct au_iinfo *iinfo;
1308ab2a 19201+
4a4d8108
AM
19202+ iinfo = au_ii(inode);
19203+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19204+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19205+ && iinfo->ii_higen == h_inode->i_generation);
19206+}
1308ab2a 19207+
4a4d8108
AM
19208+static inline void au_iigen_dec(struct inode *inode)
19209+{
537831f9 19210+ struct au_iinfo *iinfo;
be52b249 19211+ struct au_iigen *iigen;
537831f9
AM
19212+
19213+ iinfo = au_ii(inode);
be52b249
AM
19214+ iigen = &iinfo->ii_generation;
19215+ spin_lock(&iigen->ig_spin);
19216+ iigen->ig_generation--;
19217+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19218+}
19219+
19220+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19221+{
19222+ int err;
19223+
19224+ err = 0;
537831f9 19225+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19226+ err = -EIO;
19227+
19228+ return err;
4a4d8108 19229+}
1308ab2a 19230+
4a4d8108 19231+/* ---------------------------------------------------------------------- */
1308ab2a 19232+
5afbbe0d
AM
19233+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19234+ aufs_bindex_t bindex)
19235+{
19236+ return iinfo->ii_hinode + bindex;
19237+}
19238+
19239+static inline int au_is_bad_inode(struct inode *inode)
19240+{
19241+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19242+}
19243+
4a4d8108
AM
19244+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19245+ aufs_bindex_t bindex)
19246+{
19247+ IiMustAnyLock(inode);
5afbbe0d 19248+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19249+}
1308ab2a 19250+
5afbbe0d 19251+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19252+{
19253+ IiMustAnyLock(inode);
5afbbe0d 19254+ return au_ii(inode)->ii_btop;
4a4d8108 19255+}
1308ab2a 19256+
5afbbe0d 19257+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19258+{
19259+ IiMustAnyLock(inode);
5afbbe0d 19260+ return au_ii(inode)->ii_bbot;
4a4d8108 19261+}
1308ab2a 19262+
4a4d8108
AM
19263+static inline struct au_vdir *au_ivdir(struct inode *inode)
19264+{
19265+ IiMustAnyLock(inode);
19266+ return au_ii(inode)->ii_vdir;
19267+}
1308ab2a 19268+
4a4d8108
AM
19269+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19270+{
19271+ IiMustAnyLock(inode);
5afbbe0d 19272+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19273+}
1308ab2a 19274+
5afbbe0d 19275+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19276+{
4a4d8108 19277+ IiMustWriteLock(inode);
5afbbe0d 19278+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19279+}
1308ab2a 19280+
5afbbe0d 19281+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19282+{
19283+ IiMustWriteLock(inode);
5afbbe0d 19284+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19285+}
19286+
4a4d8108
AM
19287+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19288+{
19289+ IiMustWriteLock(inode);
19290+ au_ii(inode)->ii_vdir = vdir;
19291+}
1facf9fc 19292+
4a4d8108 19293+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19294+{
4a4d8108 19295+ IiMustAnyLock(inode);
5afbbe0d 19296+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19297+}
dece6358 19298+
4a4d8108 19299+/* ---------------------------------------------------------------------- */
1facf9fc 19300+
4a4d8108
AM
19301+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19302+{
19303+ if (pin)
19304+ return pin->parent;
19305+ return NULL;
1facf9fc 19306+}
19307+
4a4d8108 19308+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19309+{
4a4d8108
AM
19310+ if (pin && pin->hdir)
19311+ return pin->hdir->hi_inode;
19312+ return NULL;
1308ab2a 19313+}
1facf9fc 19314+
4a4d8108
AM
19315+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19316+{
19317+ if (pin)
19318+ return pin->hdir;
19319+ return NULL;
19320+}
1facf9fc 19321+
4a4d8108 19322+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19323+{
4a4d8108
AM
19324+ if (pin)
19325+ pin->dentry = dentry;
19326+}
1308ab2a 19327+
4a4d8108
AM
19328+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19329+ unsigned char lflag)
19330+{
19331+ if (pin) {
7f207e10 19332+ if (lflag)
4a4d8108 19333+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19334+ else
4a4d8108 19335+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19336+ }
4a4d8108
AM
19337+}
19338+
7e9cd9fe 19339+#if 0 /* reserved */
4a4d8108
AM
19340+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19341+{
19342+ if (pin) {
19343+ dput(pin->parent);
19344+ pin->parent = dget(parent);
1facf9fc 19345+ }
4a4d8108 19346+}
7e9cd9fe 19347+#endif
1facf9fc 19348+
4a4d8108
AM
19349+/* ---------------------------------------------------------------------- */
19350+
027c5e7a 19351+struct au_branch;
4a4d8108
AM
19352+#ifdef CONFIG_AUFS_HNOTIFY
19353+struct au_hnotify_op {
19354+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19355+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19356+
19357+ /*
19358+ * if it returns true, the the caller should free hinode->hi_notify,
19359+ * otherwise ->free() frees it.
19360+ */
19361+ int (*free)(struct au_hinode *hinode,
19362+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19363+
19364+ void (*fin)(void);
19365+ int (*init)(void);
027c5e7a
AM
19366+
19367+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19368+ void (*fin_br)(struct au_branch *br);
19369+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19370+};
19371+
19372+/* hnotify.c */
027c5e7a 19373+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19374+void au_hn_free(struct au_hinode *hinode);
19375+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19376+void au_hn_reset(struct inode *inode, unsigned int flags);
19377+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19378+ struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19379+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19380+int au_hnotify_init_br(struct au_branch *br, int perm);
19381+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19382+int __init au_hnotify_init(void);
19383+void au_hnotify_fin(void);
19384+
7f207e10 19385+/* hfsnotify.c */
4a4d8108
AM
19386+extern const struct au_hnotify_op au_hnotify_op;
19387+
19388+static inline
19389+void au_hn_init(struct au_hinode *hinode)
19390+{
19391+ hinode->hi_notify = NULL;
1308ab2a 19392+}
19393+
53392da6
AM
19394+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19395+{
19396+ return hinode->hi_notify;
19397+}
19398+
4a4d8108 19399+#else
c1595e42
JR
19400+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19401+ struct au_hinode *hinode __maybe_unused,
19402+ struct inode *inode __maybe_unused)
19403+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19404+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19405+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19406+ int do_set __maybe_unused)
19407+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19408+ unsigned int flags __maybe_unused)
027c5e7a
AM
19409+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19410+ struct au_branch *br __maybe_unused,
19411+ int perm __maybe_unused)
19412+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19413+ int perm __maybe_unused)
19414+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19415+AuStubInt0(__init au_hnotify_init, void)
19416+AuStubVoid(au_hnotify_fin, void)
19417+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19418+#endif /* CONFIG_AUFS_HNOTIFY */
19419+
19420+static inline void au_hn_suspend(struct au_hinode *hdir)
19421+{
19422+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19423+}
19424+
4a4d8108 19425+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19426+{
4a4d8108
AM
19427+ au_hn_ctl(hdir, /*do_set*/1);
19428+}
1308ab2a 19429+
5afbbe0d 19430+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19431+{
febd17d6 19432+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19433+ au_hn_suspend(hdir);
19434+}
dece6358 19435+
5afbbe0d 19436+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19437+ unsigned int sc __maybe_unused)
19438+{
febd17d6 19439+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19440+ au_hn_suspend(hdir);
1facf9fc 19441+}
1facf9fc 19442+
8b6a4947
AM
19443+#if 0 /* unused */
19444+#include "vfsub.h"
3c1bdaff
AM
19445+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19446+ unsigned int sc)
19447+{
be118d29 19448+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
19449+ au_hn_suspend(hdir);
19450+}
8b6a4947 19451+#endif
3c1bdaff 19452+
5afbbe0d 19453+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19454+{
19455+ au_hn_resume(hdir);
febd17d6 19456+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19457+}
19458+
19459+#endif /* __KERNEL__ */
19460+#endif /* __AUFS_INODE_H__ */
7f207e10
AM
19461diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19462--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
19463+++ linux/fs/aufs/ioctl.c 2018-07-19 09:46:13.059981241 +0200
19464@@ -0,0 +1,220 @@
19465+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 19466+/*
b00004a5 19467+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
19468+ *
19469+ * This program, aufs is free software; you can redistribute it and/or modify
19470+ * it under the terms of the GNU General Public License as published by
19471+ * the Free Software Foundation; either version 2 of the License, or
19472+ * (at your option) any later version.
19473+ *
19474+ * This program is distributed in the hope that it will be useful,
19475+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19476+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19477+ * GNU General Public License for more details.
19478+ *
19479+ * You should have received a copy of the GNU General Public License
523b37e3 19480+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19481+ */
19482+
19483+/*
19484+ * ioctl
19485+ * plink-management and readdir in userspace.
19486+ * assist the pathconf(3) wrapper library.
c2b27bf2 19487+ * move-down
076b876e 19488+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19489+ */
19490+
c2b27bf2
AM
19491+#include <linux/compat.h>
19492+#include <linux/file.h>
4a4d8108
AM
19493+#include "aufs.h"
19494+
1e00d052 19495+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19496+{
19497+ int err, fd;
5afbbe0d 19498+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19499+ struct file *h_file;
19500+ struct super_block *sb;
19501+ struct dentry *root;
1e00d052
AM
19502+ struct au_branch *br;
19503+ struct aufs_wbr_fd wbrfd = {
19504+ .oflags = au_dir_roflags,
19505+ .brid = -1
19506+ };
19507+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19508+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19509+
1e00d052
AM
19510+ AuDebugOn(wbrfd.oflags & ~valid);
19511+
19512+ if (arg) {
19513+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19514+ if (unlikely(err)) {
19515+ err = -EFAULT;
19516+ goto out;
19517+ }
19518+
19519+ err = -EINVAL;
19520+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19521+ wbrfd.oflags |= au_dir_roflags;
19522+ AuDbg("0%o\n", wbrfd.oflags);
19523+ if (unlikely(wbrfd.oflags & ~valid))
19524+ goto out;
19525+ }
19526+
2000de60 19527+ fd = get_unused_fd_flags(0);
1e00d052
AM
19528+ err = fd;
19529+ if (unlikely(fd < 0))
4a4d8108 19530+ goto out;
4a4d8108 19531+
1e00d052 19532+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19533+ wbi = 0;
1e00d052 19534+ br = NULL;
4a4d8108
AM
19535+ sb = path->dentry->d_sb;
19536+ root = sb->s_root;
19537+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19538+ bbot = au_sbbot(sb);
1e00d052
AM
19539+ if (wbrfd.brid >= 0) {
19540+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19541+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19542+ goto out_unlock;
19543+ }
19544+
19545+ h_file = ERR_PTR(-ENOENT);
19546+ br = au_sbr(sb, wbi);
19547+ if (!au_br_writable(br->br_perm)) {
19548+ if (arg)
19549+ goto out_unlock;
19550+
19551+ bindex = wbi + 1;
19552+ wbi = -1;
5afbbe0d 19553+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19554+ br = au_sbr(sb, bindex);
19555+ if (au_br_writable(br->br_perm)) {
4a4d8108 19556+ wbi = bindex;
1e00d052 19557+ br = au_sbr(sb, wbi);
4a4d8108
AM
19558+ break;
19559+ }
19560+ }
4a4d8108
AM
19561+ }
19562+ AuDbg("wbi %d\n", wbi);
1e00d052 19563+ if (wbi >= 0)
392086de
AM
19564+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19565+ /*force_wr*/0);
1e00d052
AM
19566+
19567+out_unlock:
4a4d8108
AM
19568+ aufs_read_unlock(root, AuLock_IR);
19569+ err = PTR_ERR(h_file);
19570+ if (IS_ERR(h_file))
19571+ goto out_fd;
19572+
5afbbe0d 19573+ au_br_put(br); /* cf. au_h_open() */
4a4d8108
AM
19574+ fd_install(fd, h_file);
19575+ err = fd;
19576+ goto out; /* success */
19577+
4f0767ce 19578+out_fd:
4a4d8108 19579+ put_unused_fd(fd);
4f0767ce 19580+out:
1e00d052 19581+ AuTraceErr(err);
4a4d8108
AM
19582+ return err;
19583+}
19584+
19585+/* ---------------------------------------------------------------------- */
19586+
19587+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19588+{
19589+ long err;
c1595e42 19590+ struct dentry *dentry;
4a4d8108
AM
19591+
19592+ switch (cmd) {
4a4d8108
AM
19593+ case AUFS_CTL_RDU:
19594+ case AUFS_CTL_RDU_INO:
19595+ err = au_rdu_ioctl(file, cmd, arg);
19596+ break;
19597+
19598+ case AUFS_CTL_WBR_FD:
1e00d052 19599+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19600+ break;
19601+
027c5e7a
AM
19602+ case AUFS_CTL_IBUSY:
19603+ err = au_ibusy_ioctl(file, arg);
19604+ break;
19605+
076b876e
AM
19606+ case AUFS_CTL_BRINFO:
19607+ err = au_brinfo_ioctl(file, arg);
19608+ break;
19609+
19610+ case AUFS_CTL_FHSM_FD:
2000de60 19611+ dentry = file->f_path.dentry;
c1595e42
JR
19612+ if (IS_ROOT(dentry))
19613+ err = au_fhsm_fd(dentry->d_sb, arg);
19614+ else
19615+ err = -ENOTTY;
076b876e
AM
19616+ break;
19617+
4a4d8108
AM
19618+ default:
19619+ /* do not call the lower */
19620+ AuDbg("0x%x\n", cmd);
19621+ err = -ENOTTY;
19622+ }
19623+
19624+ AuTraceErr(err);
19625+ return err;
19626+}
19627+
19628+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19629+{
19630+ long err;
19631+
19632+ switch (cmd) {
c2b27bf2 19633+ case AUFS_CTL_MVDOWN:
2000de60 19634+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19635+ break;
19636+
4a4d8108 19637+ case AUFS_CTL_WBR_FD:
1e00d052 19638+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19639+ break;
19640+
19641+ default:
19642+ /* do not call the lower */
19643+ AuDbg("0x%x\n", cmd);
19644+ err = -ENOTTY;
19645+ }
19646+
19647+ AuTraceErr(err);
19648+ return err;
19649+}
b752ccd1
AM
19650+
19651+#ifdef CONFIG_COMPAT
19652+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19653+ unsigned long arg)
19654+{
19655+ long err;
19656+
19657+ switch (cmd) {
19658+ case AUFS_CTL_RDU:
19659+ case AUFS_CTL_RDU_INO:
19660+ err = au_rdu_compat_ioctl(file, cmd, arg);
19661+ break;
19662+
027c5e7a
AM
19663+ case AUFS_CTL_IBUSY:
19664+ err = au_ibusy_compat_ioctl(file, arg);
19665+ break;
19666+
076b876e
AM
19667+ case AUFS_CTL_BRINFO:
19668+ err = au_brinfo_compat_ioctl(file, arg);
19669+ break;
19670+
b752ccd1
AM
19671+ default:
19672+ err = aufs_ioctl_dir(file, cmd, arg);
19673+ }
19674+
19675+ AuTraceErr(err);
19676+ return err;
19677+}
19678+
b752ccd1
AM
19679+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19680+ unsigned long arg)
19681+{
19682+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19683+}
19684+#endif
7f207e10
AM
19685diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19686--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
19687+++ linux/fs/aufs/i_op_add.c 2018-07-19 09:46:13.059981241 +0200
19688@@ -0,0 +1,921 @@
19689+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 19690+/*
b00004a5 19691+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
19692+ *
19693+ * This program, aufs is free software; you can redistribute it and/or modify
19694+ * it under the terms of the GNU General Public License as published by
19695+ * the Free Software Foundation; either version 2 of the License, or
19696+ * (at your option) any later version.
19697+ *
19698+ * This program is distributed in the hope that it will be useful,
19699+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19700+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19701+ * GNU General Public License for more details.
19702+ *
19703+ * You should have received a copy of the GNU General Public License
523b37e3 19704+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19705+ */
19706+
19707+/*
19708+ * inode operations (add entry)
19709+ */
19710+
19711+#include "aufs.h"
19712+
19713+/*
19714+ * final procedure of adding a new entry, except link(2).
19715+ * remove whiteout, instantiate, copyup the parent dir's times and size
19716+ * and update version.
19717+ * if it failed, re-create the removed whiteout.
19718+ */
19719+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19720+ struct dentry *wh_dentry, struct dentry *dentry)
19721+{
19722+ int err, rerr;
19723+ aufs_bindex_t bwh;
19724+ struct path h_path;
076b876e 19725+ struct super_block *sb;
4a4d8108
AM
19726+ struct inode *inode, *h_dir;
19727+ struct dentry *wh;
19728+
19729+ bwh = -1;
076b876e 19730+ sb = dir->i_sb;
4a4d8108 19731+ if (wh_dentry) {
5527c038 19732+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19733+ IMustLock(h_dir);
19734+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19735+ bwh = au_dbwh(dentry);
19736+ h_path.dentry = wh_dentry;
076b876e 19737+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19738+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19739+ dentry);
19740+ if (unlikely(err))
19741+ goto out;
19742+ }
19743+
19744+ inode = au_new_inode(dentry, /*must_new*/1);
19745+ if (!IS_ERR(inode)) {
19746+ d_instantiate(dentry, inode);
5527c038 19747+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19748+ IMustLock(dir);
b912730e 19749+ au_dir_ts(dir, bindex);
be118d29 19750+ inode_inc_iversion(dir);
076b876e 19751+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19752+ return 0; /* success */
19753+ }
19754+
19755+ err = PTR_ERR(inode);
19756+ if (!wh_dentry)
19757+ goto out;
19758+
19759+ /* revert */
19760+ /* dir inode is locked */
19761+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19762+ rerr = PTR_ERR(wh);
19763+ if (IS_ERR(wh)) {
523b37e3
AM
19764+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19765+ dentry, err, rerr);
4a4d8108
AM
19766+ err = -EIO;
19767+ } else
19768+ dput(wh);
19769+
4f0767ce 19770+out:
4a4d8108
AM
19771+ return err;
19772+}
19773+
027c5e7a
AM
19774+static int au_d_may_add(struct dentry *dentry)
19775+{
19776+ int err;
19777+
19778+ err = 0;
19779+ if (unlikely(d_unhashed(dentry)))
19780+ err = -ENOENT;
5527c038 19781+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19782+ err = -EEXIST;
19783+ return err;
19784+}
19785+
4a4d8108
AM
19786+/*
19787+ * simple tests for the adding inode operations.
19788+ * following the checks in vfs, plus the parent-child relationship.
19789+ */
19790+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19791+ struct dentry *h_parent, int isdir)
19792+{
19793+ int err;
19794+ umode_t h_mode;
19795+ struct dentry *h_dentry;
19796+ struct inode *h_inode;
19797+
19798+ err = -ENAMETOOLONG;
19799+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19800+ goto out;
19801+
19802+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19803+ if (d_really_is_negative(dentry)) {
4a4d8108 19804+ err = -EEXIST;
5527c038 19805+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19806+ goto out;
19807+ } else {
19808+ /* rename(2) case */
19809+ err = -EIO;
5527c038
JR
19810+ if (unlikely(d_is_negative(h_dentry)))
19811+ goto out;
19812+ h_inode = d_inode(h_dentry);
19813+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19814+ goto out;
19815+
19816+ h_mode = h_inode->i_mode;
19817+ if (!isdir) {
19818+ err = -EISDIR;
19819+ if (unlikely(S_ISDIR(h_mode)))
19820+ goto out;
19821+ } else if (unlikely(!S_ISDIR(h_mode))) {
19822+ err = -ENOTDIR;
19823+ goto out;
19824+ }
19825+ }
19826+
19827+ err = 0;
19828+ /* expected parent dir is locked */
19829+ if (unlikely(h_parent != h_dentry->d_parent))
19830+ err = -EIO;
19831+
4f0767ce 19832+out:
4a4d8108
AM
19833+ AuTraceErr(err);
19834+ return err;
19835+}
19836+
19837+/*
19838+ * initial procedure of adding a new entry.
19839+ * prepare writable branch and the parent dir, lock it,
19840+ * and lookup whiteout for the new entry.
19841+ */
19842+static struct dentry*
19843+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19844+ struct dentry *src_dentry, struct au_pin *pin,
19845+ struct au_wr_dir_args *wr_dir_args)
19846+{
19847+ struct dentry *wh_dentry, *h_parent;
19848+ struct super_block *sb;
19849+ struct au_branch *br;
19850+ int err;
19851+ unsigned int udba;
19852+ aufs_bindex_t bcpup;
19853+
523b37e3 19854+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19855+
19856+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19857+ bcpup = err;
19858+ wh_dentry = ERR_PTR(err);
19859+ if (unlikely(err < 0))
19860+ goto out;
19861+
19862+ sb = dentry->d_sb;
19863+ udba = au_opt_udba(sb);
19864+ err = au_pin(pin, dentry, bcpup, udba,
19865+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19866+ wh_dentry = ERR_PTR(err);
19867+ if (unlikely(err))
19868+ goto out;
19869+
19870+ h_parent = au_pinned_h_parent(pin);
19871+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19872+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19873+ err = au_may_add(dentry, bcpup, h_parent,
19874+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19875+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19876+ err = -ENAMETOOLONG;
19877+ wh_dentry = ERR_PTR(err);
19878+ if (unlikely(err))
19879+ goto out_unpin;
19880+
19881+ br = au_sbr(sb, bcpup);
19882+ if (dt) {
19883+ struct path tmp = {
19884+ .dentry = h_parent,
86dc4139 19885+ .mnt = au_br_mnt(br)
4a4d8108
AM
19886+ };
19887+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19888+ }
19889+
19890+ wh_dentry = NULL;
19891+ if (bcpup != au_dbwh(dentry))
19892+ goto out; /* success */
19893+
2000de60
JR
19894+ /*
19895+ * ENAMETOOLONG here means that if we allowed create such name, then it
19896+ * would not be able to removed in the future. So we don't allow such
19897+ * name here and we don't handle ENAMETOOLONG differently here.
19898+ */
4a4d8108
AM
19899+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19900+
4f0767ce 19901+out_unpin:
4a4d8108
AM
19902+ if (IS_ERR(wh_dentry))
19903+ au_unpin(pin);
4f0767ce 19904+out:
4a4d8108
AM
19905+ return wh_dentry;
19906+}
19907+
19908+/* ---------------------------------------------------------------------- */
19909+
19910+enum { Mknod, Symlink, Creat };
19911+struct simple_arg {
19912+ int type;
19913+ union {
19914+ struct {
b912730e
AM
19915+ umode_t mode;
19916+ bool want_excl;
19917+ bool try_aopen;
19918+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19919+ } c;
19920+ struct {
19921+ const char *symname;
19922+ } s;
19923+ struct {
7eafdf33 19924+ umode_t mode;
4a4d8108
AM
19925+ dev_t dev;
19926+ } m;
19927+ } u;
19928+};
19929+
19930+static int add_simple(struct inode *dir, struct dentry *dentry,
19931+ struct simple_arg *arg)
19932+{
076b876e 19933+ int err, rerr;
5afbbe0d 19934+ aufs_bindex_t btop;
4a4d8108 19935+ unsigned char created;
b912730e
AM
19936+ const unsigned char try_aopen
19937+ = (arg->type == Creat && arg->u.c.try_aopen);
4a4d8108
AM
19938+ struct dentry *wh_dentry, *parent;
19939+ struct inode *h_dir;
b912730e
AM
19940+ struct super_block *sb;
19941+ struct au_branch *br;
c2b27bf2
AM
19942+ /* to reuduce stack size */
19943+ struct {
19944+ struct au_dtime dt;
19945+ struct au_pin pin;
19946+ struct path h_path;
19947+ struct au_wr_dir_args wr_dir_args;
19948+ } *a;
4a4d8108 19949+
523b37e3 19950+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19951+ IMustLock(dir);
19952+
c2b27bf2
AM
19953+ err = -ENOMEM;
19954+ a = kmalloc(sizeof(*a), GFP_NOFS);
19955+ if (unlikely(!a))
19956+ goto out;
19957+ a->wr_dir_args.force_btgt = -1;
19958+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19959+
4a4d8108 19960+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
19961+ if (!try_aopen) {
19962+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19963+ if (unlikely(err))
19964+ goto out_free;
19965+ }
027c5e7a
AM
19966+ err = au_d_may_add(dentry);
19967+ if (unlikely(err))
19968+ goto out_unlock;
b912730e
AM
19969+ if (!try_aopen)
19970+ di_write_lock_parent(parent);
c2b27bf2
AM
19971+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19972+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
19973+ err = PTR_ERR(wh_dentry);
19974+ if (IS_ERR(wh_dentry))
027c5e7a 19975+ goto out_parent;
4a4d8108 19976+
5afbbe0d 19977+ btop = au_dbtop(dentry);
b912730e 19978+ sb = dentry->d_sb;
5afbbe0d
AM
19979+ br = au_sbr(sb, btop);
19980+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 19981+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 19982+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
19983+ switch (arg->type) {
19984+ case Creat:
b912730e
AM
19985+ err = 0;
19986+ if (!try_aopen || !h_dir->i_op->atomic_open)
19987+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19988+ arg->u.c.want_excl);
19989+ else
19990+ err = vfsub_atomic_open(h_dir, a->h_path.dentry,
19991+ arg->u.c.aopen, br);
4a4d8108
AM
19992+ break;
19993+ case Symlink:
c2b27bf2 19994+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
4a4d8108
AM
19995+ break;
19996+ case Mknod:
c2b27bf2
AM
19997+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19998+ arg->u.m.dev);
4a4d8108
AM
19999+ break;
20000+ default:
20001+ BUG();
20002+ }
20003+ created = !err;
20004+ if (!err)
5afbbe0d 20005+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108
AM
20006+
20007+ /* revert */
5527c038 20008+ if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
523b37e3
AM
20009+ /* no delegation since it is just created */
20010+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
20011+ /*force*/0);
4a4d8108 20012+ if (rerr) {
523b37e3
AM
20013+ AuIOErr("%pd revert failure(%d, %d)\n",
20014+ dentry, err, rerr);
4a4d8108
AM
20015+ err = -EIO;
20016+ }
c2b27bf2 20017+ au_dtime_revert(&a->dt);
4a4d8108
AM
20018+ }
20019+
b912730e
AM
20020+ if (!err && try_aopen && !h_dir->i_op->atomic_open)
20021+ *arg->u.c.aopen->opened |= FILE_CREATED;
20022+
c2b27bf2 20023+ au_unpin(&a->pin);
4a4d8108
AM
20024+ dput(wh_dentry);
20025+
027c5e7a 20026+out_parent:
b912730e
AM
20027+ if (!try_aopen)
20028+ di_write_unlock(parent);
027c5e7a 20029+out_unlock:
4a4d8108 20030+ if (unlikely(err)) {
5afbbe0d 20031+ au_update_dbtop(dentry);
4a4d8108
AM
20032+ d_drop(dentry);
20033+ }
b912730e
AM
20034+ if (!try_aopen)
20035+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 20036+out_free:
1c60b727 20037+ kfree(a);
027c5e7a 20038+out:
4a4d8108
AM
20039+ return err;
20040+}
20041+
7eafdf33
AM
20042+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
20043+ dev_t dev)
4a4d8108
AM
20044+{
20045+ struct simple_arg arg = {
20046+ .type = Mknod,
20047+ .u.m = {
20048+ .mode = mode,
20049+ .dev = dev
20050+ }
20051+ };
20052+ return add_simple(dir, dentry, &arg);
20053+}
20054+
20055+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20056+{
20057+ struct simple_arg arg = {
20058+ .type = Symlink,
20059+ .u.s.symname = symname
20060+ };
20061+ return add_simple(dir, dentry, &arg);
20062+}
20063+
7eafdf33 20064+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 20065+ bool want_excl)
4a4d8108
AM
20066+{
20067+ struct simple_arg arg = {
20068+ .type = Creat,
20069+ .u.c = {
b4510431
AM
20070+ .mode = mode,
20071+ .want_excl = want_excl
4a4d8108
AM
20072+ }
20073+ };
20074+ return add_simple(dir, dentry, &arg);
20075+}
20076+
b912730e
AM
20077+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20078+ struct vfsub_aopen_args *aopen_args)
20079+{
20080+ struct simple_arg arg = {
20081+ .type = Creat,
20082+ .u.c = {
20083+ .mode = aopen_args->create_mode,
20084+ .want_excl = aopen_args->open_flag & O_EXCL,
20085+ .try_aopen = true,
20086+ .aopen = aopen_args
20087+ }
20088+ };
20089+ return add_simple(dir, dentry, &arg);
20090+}
20091+
38d290e6
JR
20092+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20093+{
20094+ int err;
20095+ aufs_bindex_t bindex;
20096+ struct super_block *sb;
20097+ struct dentry *parent, *h_parent, *h_dentry;
20098+ struct inode *h_dir, *inode;
20099+ struct vfsmount *h_mnt;
20100+ struct au_wr_dir_args wr_dir_args = {
20101+ .force_btgt = -1,
20102+ .flags = AuWrDir_TMPFILE
20103+ };
20104+
20105+ /* copy-up may happen */
febd17d6 20106+ inode_lock(dir);
38d290e6
JR
20107+
20108+ sb = dir->i_sb;
20109+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20110+ if (unlikely(err))
20111+ goto out;
20112+
20113+ err = au_di_init(dentry);
20114+ if (unlikely(err))
20115+ goto out_si;
20116+
20117+ err = -EBUSY;
20118+ parent = d_find_any_alias(dir);
20119+ AuDebugOn(!parent);
20120+ di_write_lock_parent(parent);
5527c038 20121+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
20122+ goto out_parent;
20123+
20124+ err = au_digen_test(parent, au_sigen(sb));
20125+ if (unlikely(err))
20126+ goto out_parent;
20127+
5afbbe0d
AM
20128+ bindex = au_dbtop(parent);
20129+ au_set_dbtop(dentry, bindex);
20130+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20131+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20132+ bindex = err;
20133+ if (unlikely(err < 0))
20134+ goto out_parent;
20135+
20136+ err = -EOPNOTSUPP;
20137+ h_dir = au_h_iptr(dir, bindex);
20138+ if (unlikely(!h_dir->i_op->tmpfile))
20139+ goto out_parent;
20140+
20141+ h_mnt = au_sbr_mnt(sb, bindex);
20142+ err = vfsub_mnt_want_write(h_mnt);
20143+ if (unlikely(err))
20144+ goto out_parent;
20145+
20146+ h_parent = au_h_dptr(parent, bindex);
521ced18
JR
20147+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20148+ if (IS_ERR(h_dentry)) {
20149+ err = PTR_ERR(h_dentry);
38d290e6 20150+ goto out_mnt;
521ced18 20151+ }
38d290e6 20152+
5afbbe0d
AM
20153+ au_set_dbtop(dentry, bindex);
20154+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20155+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20156+ inode = au_new_inode(dentry, /*must_new*/1);
20157+ if (IS_ERR(inode)) {
20158+ err = PTR_ERR(inode);
20159+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20160+ au_set_dbtop(dentry, -1);
20161+ au_set_dbbot(dentry, -1);
38d290e6
JR
20162+ } else {
20163+ if (!inode->i_nlink)
20164+ set_nlink(inode, 1);
20165+ d_tmpfile(dentry, inode);
20166+ au_di(dentry)->di_tmpfile = 1;
20167+
20168+ /* update without i_mutex */
5afbbe0d 20169+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20170+ au_cpup_attr_timesizes(dir);
20171+ }
38d290e6 20172+ dput(h_dentry);
521ced18 20173+
38d290e6
JR
20174+out_mnt:
20175+ vfsub_mnt_drop_write(h_mnt);
20176+out_parent:
20177+ di_write_unlock(parent);
20178+ dput(parent);
20179+ di_write_unlock(dentry);
5afbbe0d 20180+ if (unlikely(err)) {
38d290e6
JR
20181+ au_di_fin(dentry);
20182+ dentry->d_fsdata = NULL;
20183+ }
20184+out_si:
20185+ si_read_unlock(sb);
20186+out:
febd17d6 20187+ inode_unlock(dir);
38d290e6
JR
20188+ return err;
20189+}
20190+
4a4d8108
AM
20191+/* ---------------------------------------------------------------------- */
20192+
20193+struct au_link_args {
20194+ aufs_bindex_t bdst, bsrc;
20195+ struct au_pin pin;
20196+ struct path h_path;
20197+ struct dentry *src_parent, *parent;
20198+};
20199+
20200+static int au_cpup_before_link(struct dentry *src_dentry,
20201+ struct au_link_args *a)
20202+{
20203+ int err;
20204+ struct dentry *h_src_dentry;
c2b27bf2
AM
20205+ struct au_cp_generic cpg = {
20206+ .dentry = src_dentry,
20207+ .bdst = a->bdst,
20208+ .bsrc = a->bsrc,
20209+ .len = -1,
20210+ .pin = &a->pin,
20211+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20212+ };
4a4d8108
AM
20213+
20214+ di_read_lock_parent(a->src_parent, AuLock_IR);
20215+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20216+ if (unlikely(err))
20217+ goto out;
20218+
20219+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20220+ err = au_pin(&a->pin, src_dentry, a->bdst,
20221+ au_opt_udba(src_dentry->d_sb),
20222+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20223+ if (unlikely(err))
20224+ goto out;
367653fa 20225+
c2b27bf2 20226+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20227+ au_unpin(&a->pin);
20228+
4f0767ce 20229+out:
4a4d8108
AM
20230+ di_read_unlock(a->src_parent, AuLock_IR);
20231+ return err;
20232+}
20233+
86dc4139
AM
20234+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20235+ struct au_link_args *a)
4a4d8108
AM
20236+{
20237+ int err;
20238+ unsigned char plink;
5afbbe0d 20239+ aufs_bindex_t bbot;
4a4d8108 20240+ struct dentry *h_src_dentry;
523b37e3 20241+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20242+ struct super_block *sb;
20243+ struct file *h_file;
20244+
20245+ plink = 0;
20246+ h_inode = NULL;
20247+ sb = src_dentry->d_sb;
5527c038 20248+ inode = d_inode(src_dentry);
5afbbe0d 20249+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20250+ h_inode = au_h_iptr(inode, a->bdst);
20251+ if (!h_inode || !h_inode->i_nlink) {
20252+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20253+ bbot = au_dbbot(dentry);
20254+ if (bbot < a->bsrc)
20255+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20256+ au_set_h_dptr(dentry, a->bsrc,
20257+ dget(au_h_dptr(src_dentry, a->bsrc)));
20258+ dget(a->h_path.dentry);
20259+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20260+ AuDbg("temporary d_inode...\n");
20261+ spin_lock(&dentry->d_lock);
5527c038 20262+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20263+ spin_unlock(&dentry->d_lock);
392086de 20264+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20265+ if (IS_ERR(h_file))
4a4d8108 20266+ err = PTR_ERR(h_file);
86dc4139 20267+ else {
c2b27bf2
AM
20268+ struct au_cp_generic cpg = {
20269+ .dentry = dentry,
20270+ .bdst = a->bdst,
20271+ .bsrc = -1,
20272+ .len = -1,
20273+ .pin = &a->pin,
20274+ .flags = AuCpup_KEEPLINO
20275+ };
20276+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20277+ au_h_open_post(dentry, a->bsrc, h_file);
20278+ if (!err) {
20279+ dput(a->h_path.dentry);
20280+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20281+ } else
20282+ au_set_h_dptr(dentry, a->bdst,
20283+ a->h_path.dentry);
20284+ }
c1595e42 20285+ spin_lock(&dentry->d_lock);
86dc4139 20286+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20287+ spin_unlock(&dentry->d_lock);
20288+ AuDbg("temporary d_inode...done\n");
86dc4139 20289+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20290+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20291+ } else {
20292+ /* the inode of src_dentry already exists on a.bdst branch */
20293+ h_src_dentry = d_find_alias(h_inode);
20294+ if (!h_src_dentry && au_plink_test(inode)) {
20295+ plink = 1;
20296+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20297+ err = PTR_ERR(h_src_dentry);
20298+ if (IS_ERR(h_src_dentry))
20299+ goto out;
20300+
5527c038 20301+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20302+ dput(h_src_dentry);
20303+ h_src_dentry = NULL;
20304+ }
20305+
20306+ }
20307+ if (h_src_dentry) {
523b37e3 20308+ delegated = NULL;
4a4d8108 20309+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20310+ &a->h_path, &delegated);
20311+ if (unlikely(err == -EWOULDBLOCK)) {
20312+ pr_warn("cannot retry for NFSv4 delegation"
20313+ " for an internal link\n");
20314+ iput(delegated);
20315+ }
4a4d8108
AM
20316+ dput(h_src_dentry);
20317+ } else {
20318+ AuIOErr("no dentry found for hi%lu on b%d\n",
20319+ h_inode->i_ino, a->bdst);
20320+ err = -EIO;
20321+ }
20322+ }
20323+
20324+ if (!err && !plink)
20325+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20326+
20327+out:
2cbb1c4b 20328+ AuTraceErr(err);
4a4d8108
AM
20329+ return err;
20330+}
20331+
20332+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20333+ struct dentry *dentry)
20334+{
20335+ int err, rerr;
20336+ struct au_dtime dt;
20337+ struct au_link_args *a;
20338+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20339+ struct inode *inode, *delegated;
4a4d8108
AM
20340+ struct super_block *sb;
20341+ struct au_wr_dir_args wr_dir_args = {
20342+ /* .force_btgt = -1, */
20343+ .flags = AuWrDir_ADD_ENTRY
20344+ };
20345+
20346+ IMustLock(dir);
5527c038 20347+ inode = d_inode(src_dentry);
4a4d8108
AM
20348+ IMustLock(inode);
20349+
4a4d8108
AM
20350+ err = -ENOMEM;
20351+ a = kzalloc(sizeof(*a), GFP_NOFS);
20352+ if (unlikely(!a))
20353+ goto out;
20354+
20355+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20356+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20357+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20358+ if (unlikely(err))
20359+ goto out_kfree;
38d290e6 20360+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20361+ if (unlikely(err))
20362+ goto out_unlock;
20363+ err = au_d_may_add(dentry);
20364+ if (unlikely(err))
20365+ goto out_unlock;
e49829fe 20366+
4a4d8108 20367+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20368+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20369+
20370+ di_write_lock_parent(a->parent);
20371+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20372+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20373+ &wr_dir_args);
20374+ err = PTR_ERR(wh_dentry);
20375+ if (IS_ERR(wh_dentry))
027c5e7a 20376+ goto out_parent;
4a4d8108
AM
20377+
20378+ err = 0;
20379+ sb = dentry->d_sb;
5afbbe0d 20380+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20381+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20382+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20383+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20384+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20385+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20386+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20387+ if (!h_src_dentry) {
5afbbe0d 20388+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20389+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20390+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20391+ } else if (IS_ERR(h_src_dentry)) {
20392+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20393+ goto out_parent;
38d290e6 20394+ }
2cbb1c4b 20395+
f2c43d5f
AM
20396+ /*
20397+ * aufs doesn't touch the credential so
20398+ * security_dentry_create_files_as() is unnecrssary.
20399+ */
4a4d8108
AM
20400+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20401+ if (a->bdst < a->bsrc
20402+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20403+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20404+ else {
20405+ delegated = NULL;
4a4d8108 20406+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20407+ &a->h_path, &delegated);
20408+ if (unlikely(err == -EWOULDBLOCK)) {
20409+ pr_warn("cannot retry for NFSv4 delegation"
20410+ " for an internal link\n");
20411+ iput(delegated);
20412+ }
20413+ }
2cbb1c4b 20414+ dput(h_src_dentry);
4a4d8108
AM
20415+ } else {
20416+ /*
20417+ * copyup src_dentry to the branch we process,
20418+ * and then link(2) to it.
20419+ */
2cbb1c4b 20420+ dput(h_src_dentry);
4a4d8108
AM
20421+ if (a->bdst < a->bsrc
20422+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20423+ au_unpin(&a->pin);
20424+ di_write_unlock(a->parent);
20425+ err = au_cpup_before_link(src_dentry, a);
20426+ di_write_lock_parent(a->parent);
20427+ if (!err)
20428+ err = au_pin(&a->pin, dentry, a->bdst,
20429+ au_opt_udba(sb),
20430+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20431+ if (unlikely(err))
20432+ goto out_wh;
20433+ }
20434+ if (!err) {
20435+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20436+ err = -ENOENT;
5527c038 20437+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20438+ delegated = NULL;
4a4d8108
AM
20439+ err = vfsub_link(h_src_dentry,
20440+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20441+ &a->h_path, &delegated);
20442+ if (unlikely(err == -EWOULDBLOCK)) {
20443+ pr_warn("cannot retry"
20444+ " for NFSv4 delegation"
20445+ " for an internal link\n");
20446+ iput(delegated);
20447+ }
20448+ }
4a4d8108
AM
20449+ }
20450+ }
20451+ if (unlikely(err))
20452+ goto out_unpin;
20453+
20454+ if (wh_dentry) {
20455+ a->h_path.dentry = wh_dentry;
20456+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20457+ dentry);
20458+ if (unlikely(err))
20459+ goto out_revert;
20460+ }
20461+
b912730e 20462+ au_dir_ts(dir, a->bdst);
be118d29 20463+ inode_inc_iversion(dir);
4a4d8108
AM
20464+ inc_nlink(inode);
20465+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20466+ d_instantiate(dentry, au_igrab(inode));
20467+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20468+ /* some filesystem calls d_drop() */
20469+ d_drop(dentry);
076b876e
AM
20470+ /* some filesystems consume an inode even hardlink */
20471+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20472+ goto out_unpin; /* success */
20473+
4f0767ce 20474+out_revert:
523b37e3
AM
20475+ /* no delegation since it is just created */
20476+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20477+ /*delegated*/NULL, /*force*/0);
027c5e7a 20478+ if (unlikely(rerr)) {
523b37e3 20479+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20480+ err = -EIO;
20481+ }
4a4d8108 20482+ au_dtime_revert(&dt);
4f0767ce 20483+out_unpin:
4a4d8108 20484+ au_unpin(&a->pin);
4f0767ce 20485+out_wh:
4a4d8108 20486+ dput(wh_dentry);
027c5e7a
AM
20487+out_parent:
20488+ di_write_unlock(a->parent);
20489+ dput(a->src_parent);
4f0767ce 20490+out_unlock:
4a4d8108 20491+ if (unlikely(err)) {
5afbbe0d 20492+ au_update_dbtop(dentry);
4a4d8108
AM
20493+ d_drop(dentry);
20494+ }
4a4d8108 20495+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20496+out_kfree:
1c60b727 20497+ kfree(a);
4f0767ce 20498+out:
86dc4139 20499+ AuTraceErr(err);
4a4d8108
AM
20500+ return err;
20501+}
20502+
7eafdf33 20503+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
20504+{
20505+ int err, rerr;
20506+ aufs_bindex_t bindex;
20507+ unsigned char diropq;
20508+ struct path h_path;
20509+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20510+ struct inode *h_inode;
4a4d8108
AM
20511+ struct super_block *sb;
20512+ struct {
20513+ struct au_pin pin;
20514+ struct au_dtime dt;
20515+ } *a; /* reduce the stack usage */
20516+ struct au_wr_dir_args wr_dir_args = {
20517+ .force_btgt = -1,
20518+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20519+ };
20520+
20521+ IMustLock(dir);
20522+
20523+ err = -ENOMEM;
20524+ a = kmalloc(sizeof(*a), GFP_NOFS);
20525+ if (unlikely(!a))
20526+ goto out;
20527+
027c5e7a
AM
20528+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20529+ if (unlikely(err))
20530+ goto out_free;
20531+ err = au_d_may_add(dentry);
20532+ if (unlikely(err))
20533+ goto out_unlock;
20534+
4a4d8108
AM
20535+ parent = dentry->d_parent; /* dir inode is locked */
20536+ di_write_lock_parent(parent);
20537+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20538+ &a->pin, &wr_dir_args);
20539+ err = PTR_ERR(wh_dentry);
20540+ if (IS_ERR(wh_dentry))
027c5e7a 20541+ goto out_parent;
4a4d8108
AM
20542+
20543+ sb = dentry->d_sb;
5afbbe0d 20544+ bindex = au_dbtop(dentry);
4a4d8108
AM
20545+ h_path.dentry = au_h_dptr(dentry, bindex);
20546+ h_path.mnt = au_sbr_mnt(sb, bindex);
20547+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20548+ if (unlikely(err))
027c5e7a 20549+ goto out_unpin;
4a4d8108
AM
20550+
20551+ /* make the dir opaque */
20552+ diropq = 0;
febd17d6 20553+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20554+ if (wh_dentry
20555+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20556+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20557+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20558+ inode_unlock(h_inode);
4a4d8108
AM
20559+ err = PTR_ERR(opq_dentry);
20560+ if (IS_ERR(opq_dentry))
20561+ goto out_dir;
20562+ dput(opq_dentry);
20563+ diropq = 1;
20564+ }
20565+
20566+ err = epilog(dir, bindex, wh_dentry, dentry);
20567+ if (!err) {
20568+ inc_nlink(dir);
027c5e7a 20569+ goto out_unpin; /* success */
4a4d8108
AM
20570+ }
20571+
20572+ /* revert */
20573+ if (diropq) {
20574+ AuLabel(revert opq);
febd17d6 20575+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20576+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20577+ inode_unlock(h_inode);
4a4d8108 20578+ if (rerr) {
523b37e3
AM
20579+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20580+ dentry, err, rerr);
4a4d8108
AM
20581+ err = -EIO;
20582+ }
20583+ }
20584+
4f0767ce 20585+out_dir:
4a4d8108
AM
20586+ AuLabel(revert dir);
20587+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20588+ if (rerr) {
523b37e3
AM
20589+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20590+ dentry, err, rerr);
4a4d8108
AM
20591+ err = -EIO;
20592+ }
4a4d8108 20593+ au_dtime_revert(&a->dt);
027c5e7a 20594+out_unpin:
4a4d8108
AM
20595+ au_unpin(&a->pin);
20596+ dput(wh_dentry);
027c5e7a
AM
20597+out_parent:
20598+ di_write_unlock(parent);
20599+out_unlock:
4a4d8108 20600+ if (unlikely(err)) {
5afbbe0d 20601+ au_update_dbtop(dentry);
4a4d8108
AM
20602+ d_drop(dentry);
20603+ }
4a4d8108 20604+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20605+out_free:
1c60b727 20606+ kfree(a);
4f0767ce 20607+out:
4a4d8108
AM
20608+ return err;
20609+}
7f207e10
AM
20610diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20611--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
20612+++ linux/fs/aufs/i_op.c 2018-07-19 09:46:13.059981241 +0200
20613@@ -0,0 +1,1460 @@
20614+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 20615+/*
b00004a5 20616+ * Copyright (C) 2005-2018 Junjiro R. Okajima
4a4d8108
AM
20617+ *
20618+ * This program, aufs is free software; you can redistribute it and/or modify
20619+ * it under the terms of the GNU General Public License as published by
20620+ * the Free Software Foundation; either version 2 of the License, or
20621+ * (at your option) any later version.
20622+ *
20623+ * This program is distributed in the hope that it will be useful,
20624+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20625+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20626+ * GNU General Public License for more details.
20627+ *
20628+ * You should have received a copy of the GNU General Public License
523b37e3 20629+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20630+ */
1facf9fc 20631+
1308ab2a 20632+/*
4a4d8108 20633+ * inode operations (except add/del/rename)
1308ab2a 20634+ */
4a4d8108
AM
20635+
20636+#include <linux/device_cgroup.h>
20637+#include <linux/fs_stack.h>
4a4d8108
AM
20638+#include <linux/namei.h>
20639+#include <linux/security.h>
4a4d8108
AM
20640+#include "aufs.h"
20641+
1e00d052 20642+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20643+ struct path *h_path, int brperm)
1facf9fc 20644+{
1308ab2a 20645+ int err;
4a4d8108 20646+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 20647+
e2f27e51
AM
20648+ err = -EPERM;
20649+ if (write_mask && IS_IMMUTABLE(h_inode))
20650+ goto out;
20651+
4a4d8108 20652+ err = -EACCES;
e2f27e51
AM
20653+ if (((mask & MAY_EXEC)
20654+ && S_ISREG(h_inode->i_mode)
20655+ && (path_noexec(h_path)
20656+ || !(h_inode->i_mode & S_IXUGO))))
4a4d8108
AM
20657+ goto out;
20658+
20659+ /*
20660+ * - skip the lower fs test in the case of write to ro branch.
20661+ * - nfs dir permission write check is optimized, but a policy for
20662+ * link/rename requires a real check.
2121bcd9 20663+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 20664+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
20665+ */
20666+ if ((write_mask && !au_br_writable(brperm))
20667+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20668+ && write_mask && !(mask & MAY_READ))
20669+ || !h_inode->i_op->permission) {
20670+ /* AuLabel(generic_permission); */
062440b3 20671+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
1e00d052 20672+ err = generic_permission(h_inode, mask);
b912730e
AM
20673+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20674+ err = h_inode->i_op->permission(h_inode, mask);
20675+ AuTraceErr(err);
1308ab2a 20676+ } else {
4a4d8108 20677+ /* AuLabel(h_inode->permission); */
1e00d052 20678+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
20679+ AuTraceErr(err);
20680+ }
1facf9fc 20681+
4a4d8108
AM
20682+ if (!err)
20683+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20684+ if (!err)
4a4d8108 20685+ err = security_inode_permission(h_inode, mask);
4a4d8108
AM
20686+
20687+#if 0
20688+ if (!err) {
20689+ /* todo: do we need to call ima_path_check()? */
20690+ struct path h_path = {
20691+ .dentry =
20692+ .mnt = h_mnt
20693+ };
20694+ err = ima_path_check(&h_path,
20695+ mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20696+ IMA_COUNT_LEAVE);
1308ab2a 20697+ }
4a4d8108 20698+#endif
dece6358 20699+
4f0767ce 20700+out:
1308ab2a 20701+ return err;
20702+}
dece6358 20703+
1e00d052 20704+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 20705+{
20706+ int err;
5afbbe0d 20707+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20708+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20709+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20710+ struct inode *h_inode;
20711+ struct super_block *sb;
20712+ struct au_branch *br;
1facf9fc 20713+
027c5e7a 20714+ /* todo: support rcu-walk? */
1e00d052 20715+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20716+ return -ECHILD;
20717+
4a4d8108
AM
20718+ sb = inode->i_sb;
20719+ si_read_lock(sb, AuLock_FLUSH);
20720+ ii_read_lock_child(inode);
027c5e7a
AM
20721+#if 0
20722+ err = au_iigen_test(inode, au_sigen(sb));
20723+ if (unlikely(err))
20724+ goto out;
20725+#endif
dece6358 20726+
076b876e
AM
20727+ if (!isdir
20728+ || write_mask
20729+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20730+ err = au_busy_or_stale();
5afbbe0d 20731+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20732+ if (unlikely(!h_inode
20733+ || (h_inode->i_mode & S_IFMT)
20734+ != (inode->i_mode & S_IFMT)))
20735+ goto out;
1facf9fc 20736+
4a4d8108 20737+ err = 0;
5afbbe0d 20738+ bindex = au_ibtop(inode);
4a4d8108 20739+ br = au_sbr(sb, bindex);
79b8bda9 20740+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20741+ if (write_mask
20742+ && !err
20743+ && !special_file(h_inode->i_mode)) {
20744+ /* test whether the upper writable branch exists */
20745+ err = -EROFS;
20746+ for (; bindex >= 0; bindex--)
20747+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20748+ err = 0;
20749+ break;
20750+ }
20751+ }
20752+ goto out;
20753+ }
dece6358 20754+
4a4d8108 20755+ /* non-write to dir */
1308ab2a 20756+ err = 0;
5afbbe0d
AM
20757+ bbot = au_ibbot(inode);
20758+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20759+ h_inode = au_h_iptr(inode, bindex);
20760+ if (h_inode) {
20761+ err = au_busy_or_stale();
20762+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20763+ break;
20764+
20765+ br = au_sbr(sb, bindex);
79b8bda9 20766+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20767+ br->br_perm);
20768+ }
20769+ }
1308ab2a 20770+
4f0767ce 20771+out:
4a4d8108
AM
20772+ ii_read_unlock(inode);
20773+ si_read_unlock(sb);
1308ab2a 20774+ return err;
20775+}
20776+
4a4d8108 20777+/* ---------------------------------------------------------------------- */
1facf9fc 20778+
4a4d8108 20779+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20780+ unsigned int flags)
4a4d8108
AM
20781+{
20782+ struct dentry *ret, *parent;
b752ccd1 20783+ struct inode *inode;
4a4d8108 20784+ struct super_block *sb;
1716fcea 20785+ int err, npositive;
dece6358 20786+
4a4d8108 20787+ IMustLock(dir);
1308ab2a 20788+
537831f9
AM
20789+ /* todo: support rcu-walk? */
20790+ ret = ERR_PTR(-ECHILD);
20791+ if (flags & LOOKUP_RCU)
20792+ goto out;
20793+
20794+ ret = ERR_PTR(-ENAMETOOLONG);
20795+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20796+ goto out;
20797+
4a4d8108 20798+ sb = dir->i_sb;
7f207e10
AM
20799+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20800+ ret = ERR_PTR(err);
20801+ if (unlikely(err))
20802+ goto out;
20803+
4a4d8108
AM
20804+ err = au_di_init(dentry);
20805+ ret = ERR_PTR(err);
20806+ if (unlikely(err))
7f207e10 20807+ goto out_si;
1308ab2a 20808+
9dbd164d 20809+ inode = NULL;
027c5e7a 20810+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20811+ parent = dentry->d_parent; /* dir inode is locked */
20812+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20813+ err = au_alive_dir(parent);
20814+ if (!err)
20815+ err = au_digen_test(parent, au_sigen(sb));
20816+ if (!err) {
5afbbe0d
AM
20817+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20818+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20819+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20820+ err = npositive;
20821+ }
4a4d8108 20822+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20823+ ret = ERR_PTR(err);
20824+ if (unlikely(err < 0))
20825+ goto out_unlock;
1308ab2a 20826+
4a4d8108 20827+ if (npositive) {
b752ccd1 20828+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20829+ if (IS_ERR(inode)) {
20830+ ret = (void *)inode;
20831+ inode = NULL;
20832+ goto out_unlock;
20833+ }
9dbd164d 20834+ }
4a4d8108 20835+
c1595e42
JR
20836+ if (inode)
20837+ atomic_inc(&inode->i_count);
4a4d8108 20838+ ret = d_splice_alias(inode, dentry);
537831f9
AM
20839+#if 0
20840+ if (unlikely(d_need_lookup(dentry))) {
20841+ spin_lock(&dentry->d_lock);
20842+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20843+ spin_unlock(&dentry->d_lock);
20844+ } else
20845+#endif
c1595e42 20846+ if (inode) {
2000de60 20847+ if (!IS_ERR(ret)) {
c1595e42 20848+ iput(inode);
2000de60
JR
20849+ if (ret && ret != dentry)
20850+ ii_write_unlock(inode);
20851+ } else {
c1595e42
JR
20852+ ii_write_unlock(inode);
20853+ iput(inode);
20854+ inode = NULL;
20855+ }
7f207e10 20856+ }
1facf9fc 20857+
4f0767ce 20858+out_unlock:
4a4d8108 20859+ di_write_unlock(dentry);
7f207e10 20860+out_si:
4a4d8108 20861+ si_read_unlock(sb);
7f207e10 20862+out:
4a4d8108
AM
20863+ return ret;
20864+}
1facf9fc 20865+
4a4d8108 20866+/* ---------------------------------------------------------------------- */
1facf9fc 20867+
b912730e 20868+struct aopen_node {
8b6a4947 20869+ struct hlist_bl_node hblist;
b912730e
AM
20870+ struct file *file, *h_file;
20871+};
20872+
20873+static int au_do_aopen(struct inode *inode, struct file *file)
20874+{
8b6a4947
AM
20875+ struct hlist_bl_head *aopen;
20876+ struct hlist_bl_node *pos;
b912730e
AM
20877+ struct aopen_node *node;
20878+ struct au_do_open_args args = {
8b6a4947
AM
20879+ .aopen = 1,
20880+ .open = au_do_open_nondir
b912730e
AM
20881+ };
20882+
20883+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20884+ hlist_bl_lock(aopen);
20885+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20886+ if (node->file == file) {
20887+ args.h_file = node->h_file;
20888+ break;
20889+ }
8b6a4947 20890+ hlist_bl_unlock(aopen);
b912730e
AM
20891+ /* AuDebugOn(!args.h_file); */
20892+
20893+ return au_do_open(file, &args);
20894+}
20895+
20896+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20897+ struct file *file, unsigned int open_flag,
20898+ umode_t create_mode, int *opened)
20899+{
8b6a4947 20900+ int err, unlocked, h_opened = *opened;
5afbbe0d 20901+ unsigned int lkup_flags;
f0c0a007 20902+ struct dentry *parent, *d;
8b6a4947 20903+ struct hlist_bl_head *aopen;
b912730e
AM
20904+ struct vfsub_aopen_args args = {
20905+ .open_flag = open_flag,
20906+ .create_mode = create_mode,
20907+ .opened = &h_opened
20908+ };
20909+ struct aopen_node aopen_node = {
20910+ .file = file
20911+ };
20912+
20913+ IMustLock(dir);
5afbbe0d 20914+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
20915+ AuDbgDentry(dentry);
20916+
20917+ err = 0;
20918+ if (!au_di(dentry)) {
5afbbe0d
AM
20919+ lkup_flags = LOOKUP_OPEN;
20920+ if (open_flag & O_CREAT)
20921+ lkup_flags |= LOOKUP_CREATE;
20922+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
20923+ if (IS_ERR(d)) {
20924+ err = PTR_ERR(d);
5afbbe0d 20925+ AuTraceErr(err);
b912730e
AM
20926+ goto out;
20927+ } else if (d) {
20928+ /*
20929+ * obsoleted dentry found.
20930+ * another error will be returned later.
20931+ */
20932+ d_drop(d);
b912730e 20933+ AuDbgDentry(d);
5afbbe0d 20934+ dput(d);
b912730e
AM
20935+ }
20936+ AuDbgDentry(dentry);
20937+ }
20938+
20939+ if (d_is_positive(dentry)
20940+ || d_unhashed(dentry)
20941+ || d_unlinked(dentry)
20942+ || !(open_flag & O_CREAT))
20943+ goto out_no_open;
20944+
8b6a4947 20945+ unlocked = 0;
b912730e
AM
20946+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20947+ if (unlikely(err))
20948+ goto out;
20949+
20950+ parent = dentry->d_parent; /* dir is locked */
20951+ di_write_lock_parent(parent);
5afbbe0d 20952+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
b912730e
AM
20953+ if (unlikely(err))
20954+ goto out_unlock;
20955+
20956+ AuDbgDentry(dentry);
20957+ if (d_is_positive(dentry))
20958+ goto out_unlock;
20959+
20960+ args.file = get_empty_filp();
20961+ err = PTR_ERR(args.file);
20962+ if (IS_ERR(args.file))
20963+ goto out_unlock;
20964+
20965+ args.file->f_flags = file->f_flags;
20966+ err = au_aopen_or_create(dir, dentry, &args);
20967+ AuTraceErr(err);
20968+ AuDbgFile(args.file);
20969+ if (unlikely(err < 0)) {
20970+ if (h_opened & FILE_OPENED)
20971+ fput(args.file);
20972+ else
20973+ put_filp(args.file);
20974+ goto out_unlock;
20975+ }
8b6a4947
AM
20976+ di_write_unlock(parent);
20977+ di_write_unlock(dentry);
20978+ unlocked = 1;
b912730e
AM
20979+
20980+ /* some filesystems don't set FILE_CREATED while succeeded? */
20981+ *opened |= FILE_CREATED;
20982+ if (h_opened & FILE_OPENED)
20983+ aopen_node.h_file = args.file;
20984+ else {
20985+ put_filp(args.file);
20986+ args.file = NULL;
20987+ }
20988+ aopen = &au_sbi(dir->i_sb)->si_aopen;
8b6a4947 20989+ au_hbl_add(&aopen_node.hblist, aopen);
b912730e 20990+ err = finish_open(file, dentry, au_do_aopen, opened);
8b6a4947 20991+ au_hbl_del(&aopen_node.hblist, aopen);
b912730e
AM
20992+ AuTraceErr(err);
20993+ AuDbgFile(file);
20994+ if (aopen_node.h_file)
20995+ fput(aopen_node.h_file);
20996+
20997+out_unlock:
8b6a4947
AM
20998+ if (unlocked)
20999+ si_read_unlock(dentry->d_sb);
21000+ else {
21001+ di_write_unlock(parent);
21002+ aufs_read_unlock(dentry, AuLock_DW);
21003+ }
b912730e 21004+ AuDbgDentry(dentry);
f0c0a007 21005+ if (unlikely(err < 0))
b912730e
AM
21006+ goto out;
21007+out_no_open:
f0c0a007 21008+ if (err >= 0 && !(*opened & FILE_CREATED)) {
b912730e
AM
21009+ AuLabel(out_no_open);
21010+ dget(dentry);
21011+ err = finish_no_open(file, dentry);
21012+ }
21013+out:
21014+ AuDbg("%pd%s%s\n", dentry,
21015+ (*opened & FILE_CREATED) ? " created" : "",
21016+ (*opened & FILE_OPENED) ? " opened" : "");
21017+ AuTraceErr(err);
21018+ return err;
21019+}
21020+
21021+
21022+/* ---------------------------------------------------------------------- */
21023+
4a4d8108
AM
21024+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21025+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 21026+ aufs_bindex_t btop)
4a4d8108
AM
21027+{
21028+ int err;
21029+ struct dentry *h_parent;
21030+ struct inode *h_dir;
1facf9fc 21031+
027c5e7a 21032+ if (add_entry)
5527c038 21033+ IMustLock(d_inode(parent));
027c5e7a 21034+ else
4a4d8108
AM
21035+ di_write_lock_parent(parent);
21036+
21037+ err = 0;
21038+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 21039+ if (btop > bcpup)
c2b27bf2 21040+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 21041+ else if (btop < bcpup)
4a4d8108
AM
21042+ err = au_cpdown_dirs(dentry, bcpup);
21043+ else
c2b27bf2 21044+ BUG();
4a4d8108 21045+ }
38d290e6 21046+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 21047+ h_parent = au_h_dptr(parent, bcpup);
5527c038 21048+ h_dir = d_inode(h_parent);
be118d29 21049+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 21050+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 21051+ /* todo: no unlock here */
3c1bdaff 21052+ inode_unlock_shared(h_dir);
027c5e7a
AM
21053+
21054+ AuDbg("bcpup %d\n", bcpup);
21055+ if (!err) {
5527c038 21056+ if (d_really_is_negative(dentry))
5afbbe0d 21057+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
21058+ au_update_dbrange(dentry, /*do_put_zero*/0);
21059+ }
1308ab2a 21060+ }
1facf9fc 21061+
4a4d8108
AM
21062+ if (!add_entry)
21063+ di_write_unlock(parent);
21064+ if (!err)
21065+ err = bcpup; /* success */
1308ab2a 21066+
027c5e7a 21067+ AuTraceErr(err);
4a4d8108
AM
21068+ return err;
21069+}
1facf9fc 21070+
4a4d8108
AM
21071+/*
21072+ * decide the branch and the parent dir where we will create a new entry.
21073+ * returns new bindex or an error.
21074+ * copyup the parent dir if needed.
21075+ */
21076+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21077+ struct au_wr_dir_args *args)
21078+{
21079+ int err;
392086de 21080+ unsigned int flags;
5afbbe0d 21081+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
21082+ const unsigned char add_entry
21083+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 21084+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
21085+ struct super_block *sb;
21086+ struct dentry *parent;
21087+ struct au_sbinfo *sbinfo;
1facf9fc 21088+
4a4d8108
AM
21089+ sb = dentry->d_sb;
21090+ sbinfo = au_sbi(sb);
21091+ parent = dget_parent(dentry);
5afbbe0d
AM
21092+ btop = au_dbtop(dentry);
21093+ bcpup = btop;
4a4d8108
AM
21094+ if (args->force_btgt < 0) {
21095+ if (src_dentry) {
5afbbe0d
AM
21096+ src_btop = au_dbtop(src_dentry);
21097+ if (src_btop < btop)
21098+ bcpup = src_btop;
4a4d8108 21099+ } else if (add_entry) {
392086de
AM
21100+ flags = 0;
21101+ if (au_ftest_wrdir(args->flags, ISDIR))
21102+ au_fset_wbr(flags, DIR);
21103+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
21104+ bcpup = err;
21105+ }
1facf9fc 21106+
5527c038 21107+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
21108+ if (add_entry)
21109+ err = AuWbrCopyup(sbinfo, dentry);
21110+ else {
21111+ if (!IS_ROOT(dentry)) {
21112+ di_read_lock_parent(parent, !AuLock_IR);
21113+ err = AuWbrCopyup(sbinfo, dentry);
21114+ di_read_unlock(parent, !AuLock_IR);
21115+ } else
21116+ err = AuWbrCopyup(sbinfo, dentry);
21117+ }
21118+ bcpup = err;
21119+ if (unlikely(err < 0))
21120+ goto out;
21121+ }
21122+ } else {
21123+ bcpup = args->force_btgt;
5527c038 21124+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21125+ }
027c5e7a 21126+
5afbbe0d 21127+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21128+ err = bcpup;
5afbbe0d 21129+ if (bcpup == btop)
4a4d8108 21130+ goto out; /* success */
4a4d8108
AM
21131+
21132+ /* copyup the new parent into the branch we process */
5afbbe0d 21133+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21134+ if (err >= 0) {
5527c038 21135+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21136+ au_set_h_dptr(dentry, btop, NULL);
21137+ au_set_dbtop(dentry, bcpup);
21138+ au_set_dbbot(dentry, bcpup);
027c5e7a 21139+ }
38d290e6
JR
21140+ AuDebugOn(add_entry
21141+ && !au_ftest_wrdir(args->flags, TMPFILE)
21142+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21143+ }
86dc4139
AM
21144+
21145+out:
21146+ dput(parent);
21147+ return err;
21148+}
21149+
21150+/* ---------------------------------------------------------------------- */
21151+
21152+void au_pin_hdir_unlock(struct au_pin *p)
21153+{
21154+ if (p->hdir)
5afbbe0d 21155+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21156+}
21157+
c1595e42 21158+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21159+{
21160+ int err;
21161+
21162+ err = 0;
21163+ if (!p->hdir)
21164+ goto out;
21165+
21166+ /* even if an error happens later, keep this lock */
5afbbe0d 21167+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21168+
21169+ err = -EBUSY;
5527c038 21170+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21171+ goto out;
21172+
21173+ err = 0;
21174+ if (p->h_dentry)
21175+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21176+ p->h_parent, p->br);
21177+
21178+out:
21179+ return err;
21180+}
21181+
21182+int au_pin_hdir_relock(struct au_pin *p)
21183+{
21184+ int err, i;
21185+ struct inode *h_i;
21186+ struct dentry *h_d[] = {
21187+ p->h_dentry,
21188+ p->h_parent
21189+ };
21190+
21191+ err = au_pin_hdir_lock(p);
21192+ if (unlikely(err))
21193+ goto out;
21194+
21195+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21196+ if (!h_d[i])
21197+ continue;
5527c038
JR
21198+ if (d_is_positive(h_d[i])) {
21199+ h_i = d_inode(h_d[i]);
86dc4139 21200+ err = !h_i->i_nlink;
5527c038 21201+ }
86dc4139
AM
21202+ }
21203+
21204+out:
21205+ return err;
21206+}
21207+
5afbbe0d 21208+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21209+{
5afbbe0d
AM
21210+#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
21211+ p->hdir->hi_inode->i_rwsem.owner = task;
86dc4139
AM
21212+#endif
21213+}
21214+
21215+void au_pin_hdir_acquire_nest(struct au_pin *p)
21216+{
21217+ if (p->hdir) {
5afbbe0d 21218+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21219+ p->lsc_hi, 0, NULL, _RET_IP_);
21220+ au_pin_hdir_set_owner(p, current);
21221+ }
dece6358 21222+}
1facf9fc 21223+
86dc4139
AM
21224+void au_pin_hdir_release(struct au_pin *p)
21225+{
21226+ if (p->hdir) {
21227+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 21228+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
21229+ }
21230+}
1308ab2a 21231+
4a4d8108 21232+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21233+{
4a4d8108
AM
21234+ if (pin && pin->parent)
21235+ return au_h_dptr(pin->parent, pin->bindex);
21236+ return NULL;
dece6358 21237+}
1facf9fc 21238+
4a4d8108 21239+void au_unpin(struct au_pin *p)
dece6358 21240+{
86dc4139
AM
21241+ if (p->hdir)
21242+ au_pin_hdir_unlock(p);
e49829fe 21243+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21244+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21245+ if (!p->hdir)
21246+ return;
1facf9fc 21247+
4a4d8108
AM
21248+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21249+ di_read_unlock(p->parent, AuLock_IR);
21250+ iput(p->hdir->hi_inode);
21251+ dput(p->parent);
21252+ p->parent = NULL;
21253+ p->hdir = NULL;
21254+ p->h_mnt = NULL;
86dc4139 21255+ /* do not clear p->task */
4a4d8108 21256+}
1308ab2a 21257+
4a4d8108
AM
21258+int au_do_pin(struct au_pin *p)
21259+{
21260+ int err;
21261+ struct super_block *sb;
4a4d8108
AM
21262+ struct inode *h_dir;
21263+
21264+ err = 0;
21265+ sb = p->dentry->d_sb;
86dc4139 21266+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21267+ if (IS_ROOT(p->dentry)) {
21268+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21269+ p->h_mnt = au_br_mnt(p->br);
b4510431 21270+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21271+ if (unlikely(err)) {
21272+ au_fclr_pin(p->flags, MNT_WRITE);
21273+ goto out_err;
21274+ }
21275+ }
dece6358 21276+ goto out;
1facf9fc 21277+ }
21278+
86dc4139 21279+ p->h_dentry = NULL;
5afbbe0d 21280+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21281+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21282+
4a4d8108
AM
21283+ p->parent = dget_parent(p->dentry);
21284+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21285+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21286+
4a4d8108 21287+ h_dir = NULL;
86dc4139 21288+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21289+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21290+ if (p->hdir)
21291+ h_dir = p->hdir->hi_inode;
dece6358 21292+
b752ccd1
AM
21293+ /*
21294+ * udba case, or
21295+ * if DI_LOCKED is not set, then p->parent may be different
21296+ * and h_parent can be NULL.
21297+ */
86dc4139 21298+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21299+ err = -EBUSY;
4a4d8108
AM
21300+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21301+ di_read_unlock(p->parent, AuLock_IR);
21302+ dput(p->parent);
21303+ p->parent = NULL;
21304+ goto out_err;
21305+ }
1308ab2a 21306+
4a4d8108 21307+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21308+ p->h_mnt = au_br_mnt(p->br);
b4510431 21309+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21310+ if (unlikely(err)) {
4a4d8108 21311+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
21312+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21313+ di_read_unlock(p->parent, AuLock_IR);
21314+ dput(p->parent);
21315+ p->parent = NULL;
21316+ goto out_err;
dece6358
AM
21317+ }
21318+ }
4a4d8108 21319+
86dc4139
AM
21320+ au_igrab(h_dir);
21321+ err = au_pin_hdir_lock(p);
21322+ if (!err)
21323+ goto out; /* success */
21324+
076b876e
AM
21325+ au_unpin(p);
21326+
4f0767ce 21327+out_err:
4a4d8108
AM
21328+ pr_err("err %d\n", err);
21329+ err = au_busy_or_stale();
4f0767ce 21330+out:
1facf9fc 21331+ return err;
21332+}
21333+
4a4d8108
AM
21334+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21335+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21336+ unsigned int udba, unsigned char flags)
21337+{
21338+ p->dentry = dentry;
21339+ p->udba = udba;
21340+ p->lsc_di = lsc_di;
21341+ p->lsc_hi = lsc_hi;
21342+ p->flags = flags;
21343+ p->bindex = bindex;
21344+
21345+ p->parent = NULL;
21346+ p->hdir = NULL;
21347+ p->h_mnt = NULL;
86dc4139
AM
21348+
21349+ p->h_dentry = NULL;
21350+ p->h_parent = NULL;
21351+ p->br = NULL;
21352+ p->task = current;
4a4d8108
AM
21353+}
21354+
21355+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21356+ unsigned int udba, unsigned char flags)
21357+{
21358+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21359+ udba, flags);
21360+ return au_do_pin(pin);
21361+}
21362+
dece6358
AM
21363+/* ---------------------------------------------------------------------- */
21364+
1308ab2a 21365+/*
4a4d8108
AM
21366+ * ->setattr() and ->getattr() are called in various cases.
21367+ * chmod, stat: dentry is revalidated.
21368+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21369+ * unhashed.
21370+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21371+ */
027c5e7a 21372+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21373+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21374+{
4a4d8108 21375+ int err;
4a4d8108 21376+ struct dentry *parent;
1facf9fc 21377+
1308ab2a 21378+ err = 0;
027c5e7a 21379+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21380+ parent = dget_parent(dentry);
21381+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21382+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21383+ di_read_unlock(parent, AuLock_IR);
21384+ dput(parent);
dece6358 21385+ }
1facf9fc 21386+
4a4d8108 21387+ AuTraceErr(err);
1308ab2a 21388+ return err;
21389+}
dece6358 21390+
c1595e42
JR
21391+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21392+ struct au_icpup_args *a)
1308ab2a 21393+{
21394+ int err;
4a4d8108 21395+ loff_t sz;
5afbbe0d 21396+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21397+ struct dentry *hi_wh, *parent;
21398+ struct inode *inode;
4a4d8108
AM
21399+ struct au_wr_dir_args wr_dir_args = {
21400+ .force_btgt = -1,
21401+ .flags = 0
21402+ };
21403+
2000de60 21404+ if (d_is_dir(dentry))
4a4d8108
AM
21405+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21406+ /* plink or hi_wh() case */
5afbbe0d 21407+ btop = au_dbtop(dentry);
5527c038 21408+ inode = d_inode(dentry);
5afbbe0d
AM
21409+ ibtop = au_ibtop(inode);
21410+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21411+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21412+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21413+ if (unlikely(err < 0))
21414+ goto out;
21415+ a->btgt = err;
5afbbe0d 21416+ if (err != btop)
4a4d8108
AM
21417+ au_fset_icpup(a->flags, DID_CPUP);
21418+
21419+ err = 0;
21420+ a->pin_flags = AuPin_MNT_WRITE;
21421+ parent = NULL;
21422+ if (!IS_ROOT(dentry)) {
21423+ au_fset_pin(a->pin_flags, DI_LOCKED);
21424+ parent = dget_parent(dentry);
21425+ di_write_lock_parent(parent);
21426+ }
21427+
21428+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21429+ if (unlikely(err))
21430+ goto out_parent;
21431+
4a4d8108 21432+ sz = -1;
5afbbe0d 21433+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21434+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21435+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 21436+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21437+ if (ia->ia_size < i_size_read(a->h_inode))
21438+ sz = ia->ia_size;
3c1bdaff 21439+ inode_unlock_shared(a->h_inode);
c1595e42 21440+ }
4a4d8108 21441+
4a4d8108 21442+ hi_wh = NULL;
027c5e7a 21443+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21444+ hi_wh = au_hi_wh(inode, a->btgt);
21445+ if (!hi_wh) {
c2b27bf2
AM
21446+ struct au_cp_generic cpg = {
21447+ .dentry = dentry,
21448+ .bdst = a->btgt,
21449+ .bsrc = -1,
21450+ .len = sz,
21451+ .pin = &a->pin
21452+ };
21453+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21454+ if (unlikely(err))
21455+ goto out_unlock;
21456+ hi_wh = au_hi_wh(inode, a->btgt);
21457+ /* todo: revalidate hi_wh? */
21458+ }
21459+ }
21460+
21461+ if (parent) {
21462+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21463+ di_downgrade_lock(parent, AuLock_IR);
21464+ dput(parent);
21465+ parent = NULL;
21466+ }
21467+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21468+ goto out; /* success */
21469+
21470+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21471+ struct au_cp_generic cpg = {
21472+ .dentry = dentry,
21473+ .bdst = a->btgt,
5afbbe0d 21474+ .bsrc = btop,
c2b27bf2
AM
21475+ .len = sz,
21476+ .pin = &a->pin,
21477+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21478+ };
21479+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21480+ if (!err)
21481+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21482+ } else if (!hi_wh)
21483+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21484+ else
21485+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21486+
4f0767ce 21487+out_unlock:
5527c038 21488+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21489+ if (!err)
dece6358 21490+ goto out; /* success */
4a4d8108 21491+ au_unpin(&a->pin);
4f0767ce 21492+out_parent:
4a4d8108
AM
21493+ if (parent) {
21494+ di_write_unlock(parent);
21495+ dput(parent);
21496+ }
4f0767ce 21497+out:
86dc4139 21498+ if (!err)
febd17d6 21499+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21500+ return err;
21501+}
21502+
4a4d8108 21503+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 21504+{
4a4d8108 21505+ int err;
523b37e3 21506+ struct inode *inode, *delegated;
4a4d8108
AM
21507+ struct super_block *sb;
21508+ struct file *file;
21509+ struct au_icpup_args *a;
1facf9fc 21510+
5527c038 21511+ inode = d_inode(dentry);
4a4d8108 21512+ IMustLock(inode);
dece6358 21513+
f2c43d5f
AM
21514+ err = setattr_prepare(dentry, ia);
21515+ if (unlikely(err))
21516+ goto out;
21517+
4a4d8108
AM
21518+ err = -ENOMEM;
21519+ a = kzalloc(sizeof(*a), GFP_NOFS);
21520+ if (unlikely(!a))
21521+ goto out;
1facf9fc 21522+
4a4d8108
AM
21523+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21524+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21525+
4a4d8108
AM
21526+ file = NULL;
21527+ sb = dentry->d_sb;
e49829fe
JR
21528+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21529+ if (unlikely(err))
21530+ goto out_kfree;
21531+
4a4d8108
AM
21532+ if (ia->ia_valid & ATTR_FILE) {
21533+ /* currently ftruncate(2) only */
7e9cd9fe 21534+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21535+ file = ia->ia_file;
521ced18
JR
21536+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21537+ /*fi_lsc*/0);
4a4d8108
AM
21538+ if (unlikely(err))
21539+ goto out_si;
21540+ ia->ia_file = au_hf_top(file);
21541+ a->udba = AuOpt_UDBA_NONE;
21542+ } else {
21543+ /* fchmod() doesn't pass ia_file */
21544+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21545+ di_write_lock_child(dentry);
21546+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21547+ if (d_unhashed(dentry))
21548+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21549+ if (a->udba != AuOpt_UDBA_NONE) {
21550+ AuDebugOn(IS_ROOT(dentry));
21551+ err = au_reval_for_attr(dentry, au_sigen(sb));
21552+ if (unlikely(err))
21553+ goto out_dentry;
21554+ }
dece6358 21555+ }
dece6358 21556+
4a4d8108
AM
21557+ err = au_pin_and_icpup(dentry, ia, a);
21558+ if (unlikely(err < 0))
21559+ goto out_dentry;
21560+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21561+ ia->ia_file = NULL;
21562+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21563+ }
dece6358 21564+
4a4d8108
AM
21565+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21566+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21567+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21568+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21569+ if (unlikely(err))
21570+ goto out_unlock;
21571+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21572+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21573+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21574+ if (unlikely(err))
21575+ goto out_unlock;
21576+ }
dece6358 21577+
4a4d8108
AM
21578+ if (ia->ia_valid & ATTR_SIZE) {
21579+ struct file *f;
1308ab2a 21580+
953406b4 21581+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21582+ /* unmap only */
953406b4 21583+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21584+
4a4d8108
AM
21585+ f = NULL;
21586+ if (ia->ia_valid & ATTR_FILE)
21587+ f = ia->ia_file;
febd17d6 21588+ inode_unlock(a->h_inode);
4a4d8108 21589+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21590+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21591+ } else {
21592+ delegated = NULL;
21593+ while (1) {
21594+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21595+ if (delegated) {
21596+ err = break_deleg_wait(&delegated);
21597+ if (!err)
21598+ continue;
21599+ }
21600+ break;
21601+ }
21602+ }
8cdd5066
JR
21603+ /*
21604+ * regardless aufs 'acl' option setting.
21605+ * why don't all acl-aware fs call this func from their ->setattr()?
21606+ */
21607+ if (!err && (ia->ia_valid & ATTR_MODE))
21608+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
21609+ if (!err)
21610+ au_cpup_attr_changeable(inode);
1308ab2a 21611+
4f0767ce 21612+out_unlock:
febd17d6 21613+ inode_unlock(a->h_inode);
4a4d8108 21614+ au_unpin(&a->pin);
027c5e7a 21615+ if (unlikely(err))
5afbbe0d 21616+ au_update_dbtop(dentry);
4f0767ce 21617+out_dentry:
4a4d8108
AM
21618+ di_write_unlock(dentry);
21619+ if (file) {
21620+ fi_write_unlock(file);
21621+ ia->ia_file = file;
21622+ ia->ia_valid |= ATTR_FILE;
21623+ }
4f0767ce 21624+out_si:
4a4d8108 21625+ si_read_unlock(sb);
e49829fe 21626+out_kfree:
1c60b727 21627+ kfree(a);
4f0767ce 21628+out:
4a4d8108
AM
21629+ AuTraceErr(err);
21630+ return err;
1facf9fc 21631+}
21632+
c1595e42
JR
21633+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21634+static int au_h_path_to_set_attr(struct dentry *dentry,
21635+ struct au_icpup_args *a, struct path *h_path)
21636+{
21637+ int err;
21638+ struct super_block *sb;
21639+
21640+ sb = dentry->d_sb;
21641+ a->udba = au_opt_udba(sb);
21642+ /* no d_unlinked(), to set UDBA_NONE for root */
21643+ if (d_unhashed(dentry))
21644+ a->udba = AuOpt_UDBA_NONE;
21645+ if (a->udba != AuOpt_UDBA_NONE) {
21646+ AuDebugOn(IS_ROOT(dentry));
21647+ err = au_reval_for_attr(dentry, au_sigen(sb));
21648+ if (unlikely(err))
21649+ goto out;
21650+ }
21651+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21652+ if (unlikely(err < 0))
21653+ goto out;
21654+
21655+ h_path->dentry = a->h_path.dentry;
21656+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21657+
21658+out:
21659+ return err;
21660+}
21661+
f2c43d5f
AM
21662+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21663+ struct au_sxattr *arg)
c1595e42
JR
21664+{
21665+ int err;
21666+ struct path h_path;
21667+ struct super_block *sb;
21668+ struct au_icpup_args *a;
5afbbe0d 21669+ struct inode *h_inode;
c1595e42 21670+
c1595e42
JR
21671+ IMustLock(inode);
21672+
21673+ err = -ENOMEM;
21674+ a = kzalloc(sizeof(*a), GFP_NOFS);
21675+ if (unlikely(!a))
21676+ goto out;
21677+
21678+ sb = dentry->d_sb;
21679+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21680+ if (unlikely(err))
21681+ goto out_kfree;
21682+
21683+ h_path.dentry = NULL; /* silence gcc */
21684+ di_write_lock_child(dentry);
21685+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21686+ if (unlikely(err))
21687+ goto out_di;
21688+
febd17d6 21689+ inode_unlock(a->h_inode);
c1595e42
JR
21690+ switch (arg->type) {
21691+ case AU_XATTR_SET:
5afbbe0d 21692+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
21693+ err = vfsub_setxattr(h_path.dentry,
21694+ arg->u.set.name, arg->u.set.value,
21695+ arg->u.set.size, arg->u.set.flags);
21696+ break;
c1595e42
JR
21697+ case AU_ACL_SET:
21698+ err = -EOPNOTSUPP;
5527c038 21699+ h_inode = d_inode(h_path.dentry);
c1595e42 21700+ if (h_inode->i_op->set_acl)
f2c43d5f 21701+ /* this will call posix_acl_update_mode */
c1595e42
JR
21702+ err = h_inode->i_op->set_acl(h_inode,
21703+ arg->u.acl_set.acl,
21704+ arg->u.acl_set.type);
21705+ break;
21706+ }
21707+ if (!err)
21708+ au_cpup_attr_timesizes(inode);
21709+
21710+ au_unpin(&a->pin);
21711+ if (unlikely(err))
5afbbe0d 21712+ au_update_dbtop(dentry);
c1595e42
JR
21713+
21714+out_di:
21715+ di_write_unlock(dentry);
21716+ si_read_unlock(sb);
21717+out_kfree:
1c60b727 21718+ kfree(a);
c1595e42
JR
21719+out:
21720+ AuTraceErr(err);
21721+ return err;
21722+}
21723+#endif
21724+
4a4d8108
AM
21725+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21726+ unsigned int nlink)
1facf9fc 21727+{
9dbd164d
AM
21728+ unsigned int n;
21729+
4a4d8108 21730+ inode->i_mode = st->mode;
86dc4139
AM
21731+ /* don't i_[ug]id_write() here */
21732+ inode->i_uid = st->uid;
21733+ inode->i_gid = st->gid;
4a4d8108
AM
21734+ inode->i_atime = st->atime;
21735+ inode->i_mtime = st->mtime;
21736+ inode->i_ctime = st->ctime;
1facf9fc 21737+
4a4d8108
AM
21738+ au_cpup_attr_nlink(inode, /*force*/0);
21739+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21740+ n = inode->i_nlink;
21741+ n -= nlink;
21742+ n += st->nlink;
f6b6e03d 21743+ smp_mb(); /* for i_nlink */
7eafdf33 21744+ /* 0 can happen */
92d182d2 21745+ set_nlink(inode, n);
4a4d8108 21746+ }
1facf9fc 21747+
4a4d8108
AM
21748+ spin_lock(&inode->i_lock);
21749+ inode->i_blocks = st->blocks;
21750+ i_size_write(inode, st->size);
21751+ spin_unlock(&inode->i_lock);
1facf9fc 21752+}
21753+
c1595e42 21754+/*
f2c43d5f 21755+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21756+ * returns zero or negative (an error).
21757+ * @dentry will be read-locked in success.
21758+ */
a2654f78
AM
21759+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21760+ int locked)
1facf9fc 21761+{
4a4d8108 21762+ int err;
076b876e 21763+ unsigned int mnt_flags, sigen;
c1595e42 21764+ unsigned char udba_none;
4a4d8108 21765+ aufs_bindex_t bindex;
4a4d8108
AM
21766+ struct super_block *sb, *h_sb;
21767+ struct inode *inode;
1facf9fc 21768+
c1595e42
JR
21769+ h_path->mnt = NULL;
21770+ h_path->dentry = NULL;
21771+
21772+ err = 0;
4a4d8108 21773+ sb = dentry->d_sb;
4a4d8108
AM
21774+ mnt_flags = au_mntflags(sb);
21775+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21776+
a2654f78
AM
21777+ if (unlikely(locked))
21778+ goto body; /* skip locking dinfo */
21779+
4a4d8108 21780+ /* support fstat(2) */
027c5e7a 21781+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21782+ sigen = au_sigen(sb);
027c5e7a
AM
21783+ err = au_digen_test(dentry, sigen);
21784+ if (!err) {
4a4d8108 21785+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21786+ err = au_dbrange_test(dentry);
c1595e42
JR
21787+ if (unlikely(err)) {
21788+ di_read_unlock(dentry, AuLock_IR);
21789+ goto out;
21790+ }
027c5e7a 21791+ } else {
4a4d8108
AM
21792+ AuDebugOn(IS_ROOT(dentry));
21793+ di_write_lock_child(dentry);
027c5e7a
AM
21794+ err = au_dbrange_test(dentry);
21795+ if (!err)
21796+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21797+ if (!err)
21798+ di_downgrade_lock(dentry, AuLock_IR);
21799+ else {
21800+ di_write_unlock(dentry);
21801+ goto out;
21802+ }
4a4d8108
AM
21803+ }
21804+ } else
21805+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21806+
a2654f78 21807+body:
5527c038 21808+ inode = d_inode(dentry);
5afbbe0d 21809+ bindex = au_ibtop(inode);
c1595e42
JR
21810+ h_path->mnt = au_sbr_mnt(sb, bindex);
21811+ h_sb = h_path->mnt->mnt_sb;
21812+ if (!force
21813+ && !au_test_fs_bad_iattr(h_sb)
21814+ && udba_none)
21815+ goto out; /* success */
1facf9fc 21816+
5afbbe0d 21817+ if (au_dbtop(dentry) == bindex)
c1595e42 21818+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21819+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21820+ h_path->dentry = au_plink_lkup(inode, bindex);
21821+ if (IS_ERR(h_path->dentry))
21822+ /* pretending success */
21823+ h_path->dentry = NULL;
21824+ else
21825+ dput(h_path->dentry);
4a4d8108 21826+ }
c1595e42
JR
21827+
21828+out:
21829+ return err;
21830+}
21831+
521ced18
JR
21832+static int aufs_getattr(const struct path *path, struct kstat *st,
21833+ u32 request, unsigned int query)
c1595e42
JR
21834+{
21835+ int err;
21836+ unsigned char positive;
21837+ struct path h_path;
521ced18 21838+ struct dentry *dentry;
c1595e42
JR
21839+ struct inode *inode;
21840+ struct super_block *sb;
21841+
521ced18 21842+ dentry = path->dentry;
5527c038 21843+ inode = d_inode(dentry);
c1595e42
JR
21844+ sb = dentry->d_sb;
21845+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21846+ if (unlikely(err))
21847+ goto out;
a2654f78 21848+ err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
c1595e42
JR
21849+ if (unlikely(err))
21850+ goto out_si;
c06a8ce3 21851+ if (unlikely(!h_path.dentry))
c1595e42 21852+ /* illegally overlapped or something */
4a4d8108
AM
21853+ goto out_fill; /* pretending success */
21854+
5527c038 21855+ positive = d_is_positive(h_path.dentry);
4a4d8108 21856+ if (positive)
521ced18
JR
21857+ /* no vfsub version */
21858+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21859+ if (!err) {
21860+ if (positive)
c06a8ce3 21861+ au_refresh_iattr(inode, st,
5527c038 21862+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21863+ goto out_fill; /* success */
1facf9fc 21864+ }
7f207e10 21865+ AuTraceErr(err);
c1595e42 21866+ goto out_di;
4a4d8108 21867+
4f0767ce 21868+out_fill:
4a4d8108 21869+ generic_fillattr(inode, st);
c1595e42 21870+out_di:
4a4d8108 21871+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21872+out_si:
4a4d8108 21873+ si_read_unlock(sb);
7f207e10
AM
21874+out:
21875+ AuTraceErr(err);
4a4d8108 21876+ return err;
1facf9fc 21877+}
21878+
21879+/* ---------------------------------------------------------------------- */
21880+
febd17d6
JR
21881+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21882+ struct delayed_call *done)
4a4d8108 21883+{
c2c0f25c 21884+ const char *ret;
c2c0f25c 21885+ struct dentry *h_dentry;
febd17d6 21886+ struct inode *h_inode;
4a4d8108 21887+ int err;
c2c0f25c 21888+ aufs_bindex_t bindex;
1facf9fc 21889+
79b8bda9 21890+ ret = NULL; /* suppress a warning */
febd17d6
JR
21891+ err = -ECHILD;
21892+ if (!dentry)
21893+ goto out;
21894+
027c5e7a
AM
21895+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21896+ if (unlikely(err))
c2c0f25c 21897+ goto out;
027c5e7a
AM
21898+
21899+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
21900+ if (unlikely(err))
21901+ goto out_unlock;
21902+
21903+ err = -EINVAL;
21904+ inode = d_inode(dentry);
5afbbe0d 21905+ bindex = au_ibtop(inode);
c2c0f25c 21906+ h_inode = au_h_iptr(inode, bindex);
febd17d6 21907+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
21908+ goto out_unlock;
21909+
21910+ err = -EBUSY;
21911+ h_dentry = NULL;
5afbbe0d 21912+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
21913+ h_dentry = au_h_dptr(dentry, bindex);
21914+ if (h_dentry)
21915+ dget(h_dentry);
027c5e7a 21916+ }
c2c0f25c
AM
21917+ if (!h_dentry) {
21918+ h_dentry = d_find_any_alias(h_inode);
21919+ if (IS_ERR(h_dentry)) {
21920+ err = PTR_ERR(h_dentry);
febd17d6 21921+ goto out_unlock;
c2c0f25c
AM
21922+ }
21923+ }
21924+ if (unlikely(!h_dentry))
febd17d6 21925+ goto out_unlock;
1facf9fc 21926+
c2c0f25c 21927+ err = 0;
062440b3 21928+ AuDbg("%ps\n", h_inode->i_op->get_link);
c2c0f25c 21929+ AuDbgDentry(h_dentry);
f2c43d5f 21930+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 21931+ dput(h_dentry);
febd17d6
JR
21932+ if (IS_ERR(ret))
21933+ err = PTR_ERR(ret);
c2c0f25c 21934+
c2c0f25c
AM
21935+out_unlock:
21936+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 21937+out:
c2c0f25c
AM
21938+ if (unlikely(err))
21939+ ret = ERR_PTR(err);
21940+ AuTraceErrPtr(ret);
21941+ return ret;
4a4d8108 21942+}
1facf9fc 21943+
4a4d8108 21944+/* ---------------------------------------------------------------------- */
1facf9fc 21945+
e2f27e51
AM
21946+static int au_is_special(struct inode *inode)
21947+{
21948+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21949+}
21950+
0c3ec466 21951+static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
4a4d8108 21952+{
0c3ec466 21953+ int err;
e2f27e51 21954+ aufs_bindex_t bindex;
0c3ec466
AM
21955+ struct super_block *sb;
21956+ struct inode *h_inode;
e2f27e51 21957+ struct vfsmount *h_mnt;
0c3ec466
AM
21958+
21959+ sb = inode->i_sb;
e2f27e51
AM
21960+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21961+ "unexpected s_flags 0x%lx", sb->s_flags);
21962+
0c3ec466
AM
21963+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
21964+ lockdep_off();
21965+ si_read_lock(sb, AuLock_FLUSH);
21966+ ii_write_lock_child(inode);
e2f27e51
AM
21967+
21968+ err = 0;
21969+ bindex = au_ibtop(inode);
21970+ h_inode = au_h_iptr(inode, bindex);
21971+ if (!au_test_ro(sb, bindex, inode)) {
21972+ h_mnt = au_sbr_mnt(sb, bindex);
21973+ err = vfsub_mnt_want_write(h_mnt);
21974+ if (!err) {
21975+ err = vfsub_update_time(h_inode, ts, flags);
21976+ vfsub_mnt_drop_write(h_mnt);
21977+ }
21978+ } else if (au_is_special(h_inode)) {
21979+ /*
21980+ * Never copy-up here.
21981+ * These special files may already be opened and used for
21982+ * communicating. If we copied it up, then the communication
21983+ * would be corrupted.
21984+ */
21985+ AuWarn1("timestamps for i%lu are ignored "
21986+ "since it is on readonly branch (hi%lu).\n",
21987+ inode->i_ino, h_inode->i_ino);
21988+ } else if (flags & ~S_ATIME) {
21989+ err = -EIO;
21990+ AuIOErr1("unexpected flags 0x%x\n", flags);
21991+ AuDebugOn(1);
21992+ }
21993+
38d290e6
JR
21994+ if (!err)
21995+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
21996+ ii_write_unlock(inode);
21997+ si_read_unlock(sb);
21998+ lockdep_on();
38d290e6
JR
21999+
22000+ if (!err && (flags & S_VERSION))
22001+ inode_inc_iversion(inode);
22002+
0c3ec466 22003+ return err;
4a4d8108 22004+}
1facf9fc 22005+
4a4d8108 22006+/* ---------------------------------------------------------------------- */
1308ab2a 22007+
b95c5147
AM
22008+/* no getattr version will be set by module.c:aufs_init() */
22009+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22010+ aufs_iop[] = {
22011+ [AuIop_SYMLINK] = {
22012+ .permission = aufs_permission,
c1595e42 22013+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22014+ .get_acl = aufs_get_acl,
22015+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
22016+#endif
22017+
b95c5147
AM
22018+ .setattr = aufs_setattr,
22019+ .getattr = aufs_getattr,
0c3ec466 22020+
c1595e42 22021+#ifdef CONFIG_AUFS_XATTR
b95c5147 22022+ .listxattr = aufs_listxattr,
c1595e42
JR
22023+#endif
22024+
febd17d6 22025+ .get_link = aufs_get_link,
0c3ec466 22026+
b95c5147
AM
22027+ /* .update_time = aufs_update_time */
22028+ },
22029+ [AuIop_DIR] = {
22030+ .create = aufs_create,
22031+ .lookup = aufs_lookup,
22032+ .link = aufs_link,
22033+ .unlink = aufs_unlink,
22034+ .symlink = aufs_symlink,
22035+ .mkdir = aufs_mkdir,
22036+ .rmdir = aufs_rmdir,
22037+ .mknod = aufs_mknod,
22038+ .rename = aufs_rename,
22039+
22040+ .permission = aufs_permission,
c1595e42 22041+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22042+ .get_acl = aufs_get_acl,
22043+ .set_acl = aufs_set_acl,
c1595e42
JR
22044+#endif
22045+
b95c5147
AM
22046+ .setattr = aufs_setattr,
22047+ .getattr = aufs_getattr,
0c3ec466 22048+
c1595e42 22049+#ifdef CONFIG_AUFS_XATTR
b95c5147 22050+ .listxattr = aufs_listxattr,
c1595e42
JR
22051+#endif
22052+
b95c5147
AM
22053+ .update_time = aufs_update_time,
22054+ .atomic_open = aufs_atomic_open,
22055+ .tmpfile = aufs_tmpfile
22056+ },
22057+ [AuIop_OTHER] = {
22058+ .permission = aufs_permission,
c1595e42 22059+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22060+ .get_acl = aufs_get_acl,
22061+ .set_acl = aufs_set_acl,
c1595e42
JR
22062+#endif
22063+
b95c5147
AM
22064+ .setattr = aufs_setattr,
22065+ .getattr = aufs_getattr,
0c3ec466 22066+
c1595e42 22067+#ifdef CONFIG_AUFS_XATTR
b95c5147 22068+ .listxattr = aufs_listxattr,
c1595e42
JR
22069+#endif
22070+
b95c5147
AM
22071+ .update_time = aufs_update_time
22072+ }
4a4d8108 22073+};
7f207e10
AM
22074diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22075--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
22076+++ linux/fs/aufs/i_op_del.c 2018-07-19 09:46:13.059981241 +0200
22077@@ -0,0 +1,512 @@
22078+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 22079+/*
b00004a5 22080+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 22081+ *
22082+ * This program, aufs is free software; you can redistribute it and/or modify
22083+ * it under the terms of the GNU General Public License as published by
22084+ * the Free Software Foundation; either version 2 of the License, or
22085+ * (at your option) any later version.
dece6358
AM
22086+ *
22087+ * This program is distributed in the hope that it will be useful,
22088+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22089+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22090+ * GNU General Public License for more details.
22091+ *
22092+ * You should have received a copy of the GNU General Public License
523b37e3 22093+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22094+ */
22095+
22096+/*
4a4d8108 22097+ * inode operations (del entry)
1308ab2a 22098+ */
dece6358 22099+
1308ab2a 22100+#include "aufs.h"
dece6358 22101+
4a4d8108
AM
22102+/*
22103+ * decide if a new whiteout for @dentry is necessary or not.
22104+ * when it is necessary, prepare the parent dir for the upper branch whose
22105+ * branch index is @bcpup for creation. the actual creation of the whiteout will
22106+ * be done by caller.
22107+ * return value:
22108+ * 0: wh is unnecessary
22109+ * plus: wh is necessary
22110+ * minus: error
22111+ */
22112+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 22113+{
4a4d8108 22114+ int need_wh, err;
5afbbe0d 22115+ aufs_bindex_t btop;
4a4d8108 22116+ struct super_block *sb;
dece6358 22117+
4a4d8108 22118+ sb = dentry->d_sb;
5afbbe0d 22119+ btop = au_dbtop(dentry);
4a4d8108 22120+ if (*bcpup < 0) {
5afbbe0d
AM
22121+ *bcpup = btop;
22122+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
22123+ err = AuWbrCopyup(au_sbi(sb), dentry);
22124+ *bcpup = err;
22125+ if (unlikely(err < 0))
22126+ goto out;
22127+ }
22128+ } else
5afbbe0d 22129+ AuDebugOn(btop < *bcpup
5527c038 22130+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22131+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22132+
5afbbe0d 22133+ if (*bcpup != btop) {
4a4d8108
AM
22134+ err = au_cpup_dirs(dentry, *bcpup);
22135+ if (unlikely(err))
22136+ goto out;
22137+ need_wh = 1;
22138+ } else {
027c5e7a 22139+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22140+
027c5e7a
AM
22141+ need_wh = -ENOMEM;
22142+ dinfo = au_di(dentry);
22143+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22144+ if (tmp) {
22145+ au_di_cp(tmp, dinfo);
22146+ au_di_swap(tmp, dinfo);
22147+ /* returns the number of positive dentries */
5afbbe0d
AM
22148+ need_wh = au_lkup_dentry(dentry, btop + 1,
22149+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22150+ au_di_swap(tmp, dinfo);
22151+ au_rw_write_unlock(&tmp->di_rwsem);
22152+ au_di_free(tmp);
4a4d8108
AM
22153+ }
22154+ }
22155+ AuDbg("need_wh %d\n", need_wh);
22156+ err = need_wh;
22157+
4f0767ce 22158+out:
4a4d8108 22159+ return err;
1facf9fc 22160+}
22161+
4a4d8108
AM
22162+/*
22163+ * simple tests for the del-entry operations.
22164+ * following the checks in vfs, plus the parent-child relationship.
22165+ */
22166+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22167+ struct dentry *h_parent, int isdir)
1facf9fc 22168+{
4a4d8108
AM
22169+ int err;
22170+ umode_t h_mode;
22171+ struct dentry *h_dentry, *h_latest;
1308ab2a 22172+ struct inode *h_inode;
1facf9fc 22173+
4a4d8108 22174+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22175+ if (d_really_is_positive(dentry)) {
4a4d8108 22176+ err = -ENOENT;
5527c038
JR
22177+ if (unlikely(d_is_negative(h_dentry)))
22178+ goto out;
22179+ h_inode = d_inode(h_dentry);
22180+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22181+ goto out;
1facf9fc 22182+
4a4d8108
AM
22183+ h_mode = h_inode->i_mode;
22184+ if (!isdir) {
22185+ err = -EISDIR;
22186+ if (unlikely(S_ISDIR(h_mode)))
22187+ goto out;
22188+ } else if (unlikely(!S_ISDIR(h_mode))) {
22189+ err = -ENOTDIR;
22190+ goto out;
22191+ }
22192+ } else {
22193+ /* rename(2) case */
22194+ err = -EIO;
5527c038 22195+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22196+ goto out;
22197+ }
1facf9fc 22198+
4a4d8108
AM
22199+ err = -ENOENT;
22200+ /* expected parent dir is locked */
22201+ if (unlikely(h_parent != h_dentry->d_parent))
22202+ goto out;
22203+ err = 0;
22204+
22205+ /*
22206+ * rmdir a dir may break the consistency on some filesystem.
22207+ * let's try heavy test.
22208+ */
22209+ err = -EACCES;
076b876e 22210+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 22211+ && au_test_h_perm(d_inode(h_parent),
076b876e 22212+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22213+ goto out;
22214+
076b876e 22215+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
22216+ err = -EIO;
22217+ if (IS_ERR(h_latest))
22218+ goto out;
22219+ if (h_latest == h_dentry)
22220+ err = 0;
22221+ dput(h_latest);
22222+
4f0767ce 22223+out:
4a4d8108 22224+ return err;
1308ab2a 22225+}
1facf9fc 22226+
4a4d8108
AM
22227+/*
22228+ * decide the branch where we operate for @dentry. the branch index will be set
22229+ * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
22230+ * dir for reverting.
22231+ * when a new whiteout is necessary, create it.
22232+ */
22233+static struct dentry*
22234+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22235+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22236+{
4a4d8108
AM
22237+ struct dentry *wh_dentry;
22238+ struct super_block *sb;
22239+ struct path h_path;
22240+ int err, need_wh;
22241+ unsigned int udba;
22242+ aufs_bindex_t bcpup;
dece6358 22243+
4a4d8108
AM
22244+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22245+ wh_dentry = ERR_PTR(need_wh);
22246+ if (unlikely(need_wh < 0))
22247+ goto out;
22248+
22249+ sb = dentry->d_sb;
22250+ udba = au_opt_udba(sb);
22251+ bcpup = *rbcpup;
22252+ err = au_pin(pin, dentry, bcpup, udba,
22253+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22254+ wh_dentry = ERR_PTR(err);
22255+ if (unlikely(err))
22256+ goto out;
22257+
22258+ h_path.dentry = au_pinned_h_parent(pin);
22259+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22260+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22261+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22262+ wh_dentry = ERR_PTR(err);
22263+ if (unlikely(err))
22264+ goto out_unpin;
22265+ }
22266+
22267+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22268+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22269+ wh_dentry = NULL;
22270+ if (!need_wh)
22271+ goto out; /* success, no need to create whiteout */
22272+
22273+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22274+ if (IS_ERR(wh_dentry))
22275+ goto out_unpin;
22276+
22277+ /* returns with the parent is locked and wh_dentry is dget-ed */
22278+ goto out; /* success */
22279+
4f0767ce 22280+out_unpin:
4a4d8108 22281+ au_unpin(pin);
4f0767ce 22282+out:
4a4d8108 22283+ return wh_dentry;
1facf9fc 22284+}
22285+
4a4d8108
AM
22286+/*
22287+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22288+ * in order to be revertible and save time for removing many child whiteouts
22289+ * under the dir.
22290+ * returns 1 when there are too many child whiteout and caller should remove
22291+ * them asynchronously. returns 0 when the number of children is enough small to
22292+ * remove now or the branch fs is a remote fs.
22293+ * otherwise return an error.
22294+ */
22295+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22296+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22297+{
4a4d8108
AM
22298+ int rmdir_later, err, dirwh;
22299+ struct dentry *h_dentry;
22300+ struct super_block *sb;
5527c038 22301+ struct inode *inode;
4a4d8108
AM
22302+
22303+ sb = dentry->d_sb;
22304+ SiMustAnyLock(sb);
22305+ h_dentry = au_h_dptr(dentry, bindex);
22306+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22307+ if (unlikely(err))
22308+ goto out;
22309+
22310+ /* stop monitoring */
5527c038
JR
22311+ inode = d_inode(dentry);
22312+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22313+
22314+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22315+ dirwh = au_sbi(sb)->si_dirwh;
22316+ rmdir_later = (dirwh <= 1);
22317+ if (!rmdir_later)
22318+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22319+ dirwh);
22320+ if (rmdir_later)
22321+ return rmdir_later;
22322+ }
1facf9fc 22323+
4a4d8108
AM
22324+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22325+ if (unlikely(err)) {
523b37e3
AM
22326+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22327+ h_dentry, bindex, err);
4a4d8108
AM
22328+ err = 0;
22329+ }
dece6358 22330+
4f0767ce 22331+out:
4a4d8108
AM
22332+ AuTraceErr(err);
22333+ return err;
22334+}
1308ab2a 22335+
4a4d8108
AM
22336+/*
22337+ * final procedure for deleting a entry.
22338+ * maintain dentry and iattr.
22339+ */
22340+static void epilog(struct inode *dir, struct dentry *dentry,
22341+ aufs_bindex_t bindex)
22342+{
22343+ struct inode *inode;
1308ab2a 22344+
5527c038 22345+ inode = d_inode(dentry);
4a4d8108
AM
22346+ d_drop(dentry);
22347+ inode->i_ctime = dir->i_ctime;
1308ab2a 22348+
b912730e 22349+ au_dir_ts(dir, bindex);
be118d29 22350+ inode_inc_iversion(dir);
1facf9fc 22351+}
22352+
4a4d8108
AM
22353+/*
22354+ * when an error happened, remove the created whiteout and revert everything.
22355+ */
7f207e10
AM
22356+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22357+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22358+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22359+{
4a4d8108
AM
22360+ int rerr;
22361+ struct path h_path = {
22362+ .dentry = wh_dentry,
7f207e10 22363+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22364+ };
dece6358 22365+
7f207e10 22366+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22367+ if (!rerr) {
22368+ au_set_dbwh(dentry, bwh);
22369+ au_dtime_revert(dt);
22370+ return 0;
22371+ }
dece6358 22372+
523b37e3 22373+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22374+ return -EIO;
1facf9fc 22375+}
22376+
4a4d8108 22377+/* ---------------------------------------------------------------------- */
1facf9fc 22378+
4a4d8108 22379+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22380+{
4a4d8108 22381+ int err;
5afbbe0d 22382+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22383+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22384+ struct dentry *parent, *wh_dentry;
c2b27bf2
AM
22385+ /* to reuduce stack size */
22386+ struct {
22387+ struct au_dtime dt;
22388+ struct au_pin pin;
22389+ struct path h_path;
22390+ } *a;
1facf9fc 22391+
4a4d8108 22392+ IMustLock(dir);
027c5e7a 22393+
c2b27bf2
AM
22394+ err = -ENOMEM;
22395+ a = kmalloc(sizeof(*a), GFP_NOFS);
22396+ if (unlikely(!a))
22397+ goto out;
22398+
027c5e7a
AM
22399+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22400+ if (unlikely(err))
c2b27bf2 22401+ goto out_free;
027c5e7a
AM
22402+ err = au_d_hashed_positive(dentry);
22403+ if (unlikely(err))
22404+ goto out_unlock;
5527c038 22405+ inode = d_inode(dentry);
4a4d8108 22406+ IMustLock(inode);
027c5e7a 22407+ err = -EISDIR;
2000de60 22408+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22409+ goto out_unlock; /* possible? */
1facf9fc 22410+
5afbbe0d 22411+ btop = au_dbtop(dentry);
4a4d8108
AM
22412+ bwh = au_dbwh(dentry);
22413+ bindex = -1;
027c5e7a
AM
22414+ parent = dentry->d_parent; /* dir inode is locked */
22415+ di_write_lock_parent(parent);
c2b27bf2
AM
22416+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22417+ &a->pin);
4a4d8108
AM
22418+ err = PTR_ERR(wh_dentry);
22419+ if (IS_ERR(wh_dentry))
027c5e7a 22420+ goto out_parent;
1facf9fc 22421+
5afbbe0d
AM
22422+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22423+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22424+ dget(a->h_path.dentry);
5afbbe0d 22425+ if (bindex == btop) {
c2b27bf2 22426+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22427+ delegated = NULL;
22428+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22429+ if (unlikely(err == -EWOULDBLOCK)) {
22430+ pr_warn("cannot retry for NFSv4 delegation"
22431+ " for an internal unlink\n");
22432+ iput(delegated);
22433+ }
4a4d8108
AM
22434+ } else {
22435+ /* dir inode is locked */
5527c038 22436+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22437+ IMustLock(h_dir);
22438+ err = 0;
22439+ }
dece6358 22440+
4a4d8108 22441+ if (!err) {
7f207e10 22442+ vfsub_drop_nlink(inode);
4a4d8108
AM
22443+ epilog(dir, dentry, bindex);
22444+
22445+ /* update target timestamps */
5afbbe0d 22446+ if (bindex == btop) {
c2b27bf2
AM
22447+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22448+ /*ignore*/
5527c038 22449+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22450+ } else
22451+ /* todo: this timestamp may be reverted later */
22452+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22453+ goto out_unpin; /* success */
1facf9fc 22454+ }
22455+
4a4d8108
AM
22456+ /* revert */
22457+ if (wh_dentry) {
22458+ int rerr;
22459+
c2b27bf2
AM
22460+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22461+ &a->dt);
4a4d8108
AM
22462+ if (rerr)
22463+ err = rerr;
dece6358 22464+ }
1facf9fc 22465+
027c5e7a 22466+out_unpin:
c2b27bf2 22467+ au_unpin(&a->pin);
4a4d8108 22468+ dput(wh_dentry);
c2b27bf2 22469+ dput(a->h_path.dentry);
027c5e7a 22470+out_parent:
4a4d8108 22471+ di_write_unlock(parent);
027c5e7a 22472+out_unlock:
4a4d8108 22473+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22474+out_free:
1c60b727 22475+ kfree(a);
027c5e7a 22476+out:
4a4d8108 22477+ return err;
dece6358
AM
22478+}
22479+
4a4d8108 22480+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22481+{
4a4d8108 22482+ int err, rmdir_later;
5afbbe0d 22483+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22484+ struct inode *inode;
22485+ struct dentry *parent, *wh_dentry, *h_dentry;
22486+ struct au_whtmp_rmdir *args;
c2b27bf2
AM
22487+ /* to reuduce stack size */
22488+ struct {
22489+ struct au_dtime dt;
22490+ struct au_pin pin;
22491+ } *a;
1facf9fc 22492+
4a4d8108 22493+ IMustLock(dir);
027c5e7a 22494+
c2b27bf2
AM
22495+ err = -ENOMEM;
22496+ a = kmalloc(sizeof(*a), GFP_NOFS);
22497+ if (unlikely(!a))
22498+ goto out;
22499+
027c5e7a
AM
22500+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22501+ if (unlikely(err))
c2b27bf2 22502+ goto out_free;
53392da6
AM
22503+ err = au_alive_dir(dentry);
22504+ if (unlikely(err))
027c5e7a 22505+ goto out_unlock;
5527c038 22506+ inode = d_inode(dentry);
4a4d8108 22507+ IMustLock(inode);
027c5e7a 22508+ err = -ENOTDIR;
2000de60 22509+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22510+ goto out_unlock; /* possible? */
dece6358 22511+
4a4d8108
AM
22512+ err = -ENOMEM;
22513+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22514+ if (unlikely(!args))
22515+ goto out_unlock;
dece6358 22516+
4a4d8108
AM
22517+ parent = dentry->d_parent; /* dir inode is locked */
22518+ di_write_lock_parent(parent);
22519+ err = au_test_empty(dentry, &args->whlist);
22520+ if (unlikely(err))
027c5e7a 22521+ goto out_parent;
1facf9fc 22522+
5afbbe0d 22523+ btop = au_dbtop(dentry);
4a4d8108
AM
22524+ bwh = au_dbwh(dentry);
22525+ bindex = -1;
c2b27bf2
AM
22526+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22527+ &a->pin);
4a4d8108
AM
22528+ err = PTR_ERR(wh_dentry);
22529+ if (IS_ERR(wh_dentry))
027c5e7a 22530+ goto out_parent;
1facf9fc 22531+
5afbbe0d 22532+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22533+ dget(h_dentry);
22534+ rmdir_later = 0;
5afbbe0d
AM
22535+ if (bindex == btop) {
22536+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22537+ if (err > 0) {
22538+ rmdir_later = err;
22539+ err = 0;
22540+ }
22541+ } else {
22542+ /* stop monitoring */
5afbbe0d 22543+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22544+
22545+ /* dir inode is locked */
5527c038 22546+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22547+ err = 0;
22548+ }
22549+
4a4d8108 22550+ if (!err) {
027c5e7a 22551+ vfsub_dead_dir(inode);
4a4d8108
AM
22552+ au_set_dbdiropq(dentry, -1);
22553+ epilog(dir, dentry, bindex);
1308ab2a 22554+
4a4d8108 22555+ if (rmdir_later) {
5afbbe0d 22556+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22557+ args = NULL;
22558+ }
1308ab2a 22559+
4a4d8108 22560+ goto out_unpin; /* success */
1facf9fc 22561+ }
22562+
4a4d8108
AM
22563+ /* revert */
22564+ AuLabel(revert);
22565+ if (wh_dentry) {
22566+ int rerr;
1308ab2a 22567+
c2b27bf2
AM
22568+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22569+ &a->dt);
4a4d8108
AM
22570+ if (rerr)
22571+ err = rerr;
1facf9fc 22572+ }
22573+
4f0767ce 22574+out_unpin:
c2b27bf2 22575+ au_unpin(&a->pin);
4a4d8108
AM
22576+ dput(wh_dentry);
22577+ dput(h_dentry);
027c5e7a 22578+out_parent:
4a4d8108
AM
22579+ di_write_unlock(parent);
22580+ if (args)
22581+ au_whtmp_rmdir_free(args);
4f0767ce 22582+out_unlock:
4a4d8108 22583+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22584+out_free:
1c60b727 22585+ kfree(a);
4f0767ce 22586+out:
4a4d8108
AM
22587+ AuTraceErr(err);
22588+ return err;
dece6358 22589+}
7f207e10
AM
22590diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22591--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
22592+++ linux/fs/aufs/i_op_ren.c 2018-07-19 09:46:13.059981241 +0200
22593@@ -0,0 +1,1247 @@
22594+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 22595+/*
b00004a5 22596+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 22597+ *
22598+ * This program, aufs is free software; you can redistribute it and/or modify
22599+ * it under the terms of the GNU General Public License as published by
22600+ * the Free Software Foundation; either version 2 of the License, or
22601+ * (at your option) any later version.
dece6358
AM
22602+ *
22603+ * This program is distributed in the hope that it will be useful,
22604+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22605+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22606+ * GNU General Public License for more details.
22607+ *
22608+ * You should have received a copy of the GNU General Public License
523b37e3 22609+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22610+ */
22611+
22612+/*
4a4d8108
AM
22613+ * inode operation (rename entry)
22614+ * todo: this is crazy monster
1facf9fc 22615+ */
22616+
22617+#include "aufs.h"
22618+
4a4d8108
AM
22619+enum { AuSRC, AuDST, AuSrcDst };
22620+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22621+
f2c43d5f
AM
22622+#define AuRen_ISDIR_SRC 1
22623+#define AuRen_ISDIR_DST (1 << 1)
22624+#define AuRen_ISSAMEDIR (1 << 2)
22625+#define AuRen_WHSRC (1 << 3)
22626+#define AuRen_WHDST (1 << 4)
22627+#define AuRen_MNT_WRITE (1 << 5)
22628+#define AuRen_DT_DSTDIR (1 << 6)
22629+#define AuRen_DIROPQ_SRC (1 << 7)
22630+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22631+#define AuRen_DIRREN (1 << 9)
22632+#define AuRen_DROPPED_SRC (1 << 10)
22633+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22634+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22635+#define au_fset_ren(flags, name) \
22636+ do { (flags) |= AuRen_##name; } while (0)
22637+#define au_fclr_ren(flags, name) \
22638+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22639+
8b6a4947
AM
22640+#ifndef CONFIG_AUFS_DIRREN
22641+#undef AuRen_DIRREN
22642+#define AuRen_DIRREN 0
22643+#endif
22644+
4a4d8108
AM
22645+struct au_ren_args {
22646+ struct {
22647+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22648+ *wh_dentry;
22649+ struct inode *dir, *inode;
f2c43d5f 22650+ struct au_hinode *hdir, *hinode;
4a4d8108 22651+ struct au_dtime dt[AuParentChild];
f2c43d5f 22652+ aufs_bindex_t btop, bdiropq;
4a4d8108 22653+ } sd[AuSrcDst];
1facf9fc 22654+
4a4d8108
AM
22655+#define src_dentry sd[AuSRC].dentry
22656+#define src_dir sd[AuSRC].dir
22657+#define src_inode sd[AuSRC].inode
22658+#define src_h_dentry sd[AuSRC].h_dentry
22659+#define src_parent sd[AuSRC].parent
22660+#define src_h_parent sd[AuSRC].h_parent
22661+#define src_wh_dentry sd[AuSRC].wh_dentry
22662+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22663+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22664+#define src_h_dir sd[AuSRC].hdir->hi_inode
22665+#define src_dt sd[AuSRC].dt
5afbbe0d 22666+#define src_btop sd[AuSRC].btop
f2c43d5f 22667+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22668+
4a4d8108
AM
22669+#define dst_dentry sd[AuDST].dentry
22670+#define dst_dir sd[AuDST].dir
22671+#define dst_inode sd[AuDST].inode
22672+#define dst_h_dentry sd[AuDST].h_dentry
22673+#define dst_parent sd[AuDST].parent
22674+#define dst_h_parent sd[AuDST].h_parent
22675+#define dst_wh_dentry sd[AuDST].wh_dentry
22676+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22677+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22678+#define dst_h_dir sd[AuDST].hdir->hi_inode
22679+#define dst_dt sd[AuDST].dt
5afbbe0d 22680+#define dst_btop sd[AuDST].btop
f2c43d5f 22681+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22682+
22683+ struct dentry *h_trap;
22684+ struct au_branch *br;
4a4d8108
AM
22685+ struct path h_path;
22686+ struct au_nhash whlist;
f2c43d5f 22687+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22688+
f2c43d5f
AM
22689+ struct {
22690+ unsigned short auren_flags;
22691+ unsigned char flags; /* syscall parameter */
22692+ unsigned char exchange;
22693+ } __packed;
1facf9fc 22694+
4a4d8108
AM
22695+ struct au_whtmp_rmdir *thargs;
22696+ struct dentry *h_dst;
8b6a4947 22697+ struct au_hinode *h_root;
4a4d8108 22698+};
1308ab2a 22699+
4a4d8108 22700+/* ---------------------------------------------------------------------- */
1308ab2a 22701+
4a4d8108
AM
22702+/*
22703+ * functions for reverting.
22704+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22705+ * everything as if nothing happened.
4a4d8108
AM
22706+ * we don't need to revert the copied-up/down the parent dir since they are
22707+ * harmless.
22708+ */
1facf9fc 22709+
4a4d8108
AM
22710+#define RevertFailure(fmt, ...) do { \
22711+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22712+ ##__VA_ARGS__, err, rerr); \
22713+ err = -EIO; \
22714+} while (0)
1facf9fc 22715+
f2c43d5f 22716+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22717+{
4a4d8108 22718+ int rerr;
f2c43d5f
AM
22719+ struct dentry *d;
22720+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22721+
f2c43d5f
AM
22722+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22723+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22724+ rerr = au_diropq_remove(d, a->btgt);
22725+ au_hn_inode_unlock(src_or_dst(hinode));
22726+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22727+ if (rerr)
f2c43d5f
AM
22728+ RevertFailure("remove diropq %pd", d);
22729+
22730+#undef src_or_dst_
22731+}
22732+
22733+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22734+{
22735+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22736+ au_ren_do_rev_diropq(err, a, AuSRC);
22737+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22738+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22739+}
1facf9fc 22740+
4a4d8108
AM
22741+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22742+{
22743+ int rerr;
523b37e3 22744+ struct inode *delegated;
1facf9fc 22745+
b4510431
AM
22746+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22747+ a->src_h_parent);
4a4d8108
AM
22748+ rerr = PTR_ERR(a->h_path.dentry);
22749+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22750+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22751+ return;
1facf9fc 22752+ }
22753+
523b37e3 22754+ delegated = NULL;
4a4d8108
AM
22755+ rerr = vfsub_rename(a->dst_h_dir,
22756+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22757+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22758+ if (unlikely(rerr == -EWOULDBLOCK)) {
22759+ pr_warn("cannot retry for NFSv4 delegation"
22760+ " for an internal rename\n");
22761+ iput(delegated);
22762+ }
4a4d8108
AM
22763+ d_drop(a->h_path.dentry);
22764+ dput(a->h_path.dentry);
22765+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22766+ if (rerr)
523b37e3 22767+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22768+}
22769+
4a4d8108 22770+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22771+{
4a4d8108 22772+ int rerr;
523b37e3 22773+ struct inode *delegated;
dece6358 22774+
b4510431
AM
22775+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22776+ a->dst_h_parent);
4a4d8108
AM
22777+ rerr = PTR_ERR(a->h_path.dentry);
22778+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22779+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22780+ return;
22781+ }
5527c038 22782+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22783+ d_drop(a->h_path.dentry);
22784+ dput(a->h_path.dentry);
22785+ return;
dece6358
AM
22786+ }
22787+
523b37e3
AM
22788+ delegated = NULL;
22789+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22790+ &delegated, a->flags);
523b37e3
AM
22791+ if (unlikely(rerr == -EWOULDBLOCK)) {
22792+ pr_warn("cannot retry for NFSv4 delegation"
22793+ " for an internal rename\n");
22794+ iput(delegated);
22795+ }
4a4d8108
AM
22796+ d_drop(a->h_path.dentry);
22797+ dput(a->h_path.dentry);
22798+ if (!rerr)
22799+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22800+ else
523b37e3 22801+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22802+}
1308ab2a 22803+
4a4d8108
AM
22804+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22805+{
22806+ int rerr;
1308ab2a 22807+
4a4d8108
AM
22808+ a->h_path.dentry = a->src_wh_dentry;
22809+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22810+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22811+ if (rerr)
523b37e3 22812+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22813+}
4a4d8108 22814+#undef RevertFailure
1facf9fc 22815+
1308ab2a 22816+/* ---------------------------------------------------------------------- */
22817+
4a4d8108
AM
22818+/*
22819+ * when we have to copyup the renaming entry, do it with the rename-target name
22820+ * in order to minimize the cost (the later actual rename is unnecessary).
22821+ * otherwise rename it on the target branch.
22822+ */
22823+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22824+{
dece6358 22825+ int err;
4a4d8108 22826+ struct dentry *d;
523b37e3 22827+ struct inode *delegated;
1facf9fc 22828+
4a4d8108 22829+ d = a->src_dentry;
5afbbe0d 22830+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22831+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22832+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22833+ delegated = NULL;
4a4d8108 22834+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22835+ a->dst_h_dir, &a->h_path, &delegated,
22836+ a->flags);
523b37e3
AM
22837+ if (unlikely(err == -EWOULDBLOCK)) {
22838+ pr_warn("cannot retry for NFSv4 delegation"
22839+ " for an internal rename\n");
22840+ iput(delegated);
22841+ }
c2b27bf2 22842+ } else
86dc4139 22843+ BUG();
1308ab2a 22844+
027c5e7a
AM
22845+ if (!err && a->h_dst)
22846+ /* it will be set to dinfo later */
22847+ dget(a->h_dst);
1facf9fc 22848+
dece6358
AM
22849+ return err;
22850+}
1facf9fc 22851+
4a4d8108
AM
22852+/* cf. aufs_rmdir() */
22853+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22854+{
4a4d8108
AM
22855+ int err;
22856+ struct inode *dir;
1facf9fc 22857+
4a4d8108
AM
22858+ dir = a->dst_dir;
22859+ SiMustAnyLock(dir->i_sb);
22860+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22861+ au_sbi(dir->i_sb)->si_dirwh)
22862+ || au_test_fs_remote(a->h_dst->d_sb)) {
22863+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22864+ if (unlikely(err))
523b37e3
AM
22865+ pr_warn("failed removing whtmp dir %pd (%d), "
22866+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22867+ } else {
22868+ au_nhash_wh_free(&a->thargs->whlist);
22869+ a->thargs->whlist = a->whlist;
22870+ a->whlist.nh_num = 0;
22871+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22872+ dput(a->h_dst);
22873+ a->thargs = NULL;
22874+ }
22875+
22876+ return 0;
1308ab2a 22877+}
1facf9fc 22878+
4a4d8108 22879+/* make it 'opaque' dir. */
f2c43d5f 22880+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22881+{
22882+ int err;
f2c43d5f
AM
22883+ struct dentry *d, *diropq;
22884+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22885+
4a4d8108 22886+ err = 0;
f2c43d5f
AM
22887+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22888+ src_or_dst(bdiropq) = au_dbdiropq(d);
22889+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22890+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22891+ diropq = au_diropq_create(d, a->btgt);
22892+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22893+ if (IS_ERR(diropq))
22894+ err = PTR_ERR(diropq);
076b876e
AM
22895+ else
22896+ dput(diropq);
1facf9fc 22897+
f2c43d5f 22898+#undef src_or_dst_
4a4d8108
AM
22899+ return err;
22900+}
1facf9fc 22901+
f2c43d5f 22902+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
22903+{
22904+ int err;
f2c43d5f
AM
22905+ unsigned char always;
22906+ struct dentry *d;
1facf9fc 22907+
f2c43d5f
AM
22908+ err = 0;
22909+ d = a->dst_dentry; /* already renamed on the branch */
22910+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22911+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 22912+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
22913+ && a->btgt != au_dbdiropq(a->src_dentry)
22914+ && (a->dst_wh_dentry
22915+ || a->btgt <= au_dbdiropq(d)
22916+ /* hide the lower to keep xino */
22917+ /* the lowers may not be a dir, but we hide them anyway */
22918+ || a->btgt < au_dbbot(d)
22919+ || always)) {
22920+ AuDbg("here\n");
22921+ err = au_ren_do_diropq(a, AuSRC);
22922+ if (unlikely(err))
4a4d8108 22923+ goto out;
f2c43d5f 22924+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 22925+ }
f2c43d5f
AM
22926+ if (!a->exchange)
22927+ goto out; /* success */
1facf9fc 22928+
f2c43d5f
AM
22929+ d = a->src_dentry; /* already renamed on the branch */
22930+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22931+ && a->btgt != au_dbdiropq(a->dst_dentry)
22932+ && (a->btgt < au_dbdiropq(d)
22933+ || a->btgt < au_dbbot(d)
22934+ || always)) {
22935+ AuDbgDentry(a->src_dentry);
22936+ AuDbgDentry(a->dst_dentry);
22937+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 22938+ if (unlikely(err))
f2c43d5f
AM
22939+ goto out_rev_src;
22940+ au_fset_ren(a->auren_flags, DIROPQ_DST);
22941+ }
22942+ goto out; /* success */
dece6358 22943+
f2c43d5f
AM
22944+out_rev_src:
22945+ AuDbg("err %d, reverting src\n", err);
22946+ au_ren_rev_diropq(err, a);
22947+out:
22948+ return err;
22949+}
22950+
22951+static int do_rename(struct au_ren_args *a)
22952+{
22953+ int err;
22954+ struct dentry *d, *h_d;
22955+
22956+ if (!a->exchange) {
22957+ /* prepare workqueue args for asynchronous rmdir */
22958+ h_d = a->dst_h_dentry;
22959+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 22960+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
22961+ && d_is_positive(h_d)) {
22962+ err = -ENOMEM;
22963+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22964+ GFP_NOFS);
22965+ if (unlikely(!a->thargs))
22966+ goto out;
22967+ a->h_dst = dget(h_d);
22968+ }
22969+
22970+ /* create whiteout for src_dentry */
22971+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
22972+ a->src_bwh = au_dbwh(a->src_dentry);
22973+ AuDebugOn(a->src_bwh >= 0);
22974+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22975+ a->src_h_parent);
22976+ err = PTR_ERR(a->src_wh_dentry);
22977+ if (IS_ERR(a->src_wh_dentry))
22978+ goto out_thargs;
22979+ }
22980+
22981+ /* lookup whiteout for dentry */
22982+ if (au_ftest_ren(a->auren_flags, WHDST)) {
22983+ h_d = au_wh_lkup(a->dst_h_parent,
22984+ &a->dst_dentry->d_name, a->br);
22985+ err = PTR_ERR(h_d);
22986+ if (IS_ERR(h_d))
22987+ goto out_whsrc;
22988+ if (d_is_negative(h_d))
22989+ dput(h_d);
22990+ else
22991+ a->dst_wh_dentry = h_d;
22992+ }
22993+
22994+ /* rename dentry to tmpwh */
22995+ if (a->thargs) {
22996+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
22997+ if (unlikely(err))
22998+ goto out_whdst;
22999+
23000+ d = a->dst_dentry;
23001+ au_set_h_dptr(d, a->btgt, NULL);
23002+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
23003+ if (unlikely(err))
23004+ goto out_whtmp;
23005+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
23006+ }
4a4d8108 23007+ }
1facf9fc 23008+
5afbbe0d 23009+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
8b6a4947
AM
23010+#if 0
23011+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23012+ && d_is_positive(a->dst_h_dentry)
23013+ && a->src_btop != a->btgt);
23014+#endif
1facf9fc 23015+
4a4d8108 23016+ /* rename by vfs_rename or cpup */
4a4d8108
AM
23017+ err = au_ren_or_cpup(a);
23018+ if (unlikely(err))
23019+ /* leave the copied-up one */
23020+ goto out_whtmp;
1308ab2a 23021+
4a4d8108 23022+ /* make dir opaque */
f2c43d5f
AM
23023+ err = au_ren_diropq(a);
23024+ if (unlikely(err))
23025+ goto out_rename;
1308ab2a 23026+
4a4d8108 23027+ /* update target timestamps */
f2c43d5f
AM
23028+ if (a->exchange) {
23029+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23030+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23031+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23032+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23033+ }
5afbbe0d 23034+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
23035+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23036+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 23037+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 23038+
f2c43d5f
AM
23039+ if (!a->exchange) {
23040+ /* remove whiteout for dentry */
23041+ if (a->dst_wh_dentry) {
23042+ a->h_path.dentry = a->dst_wh_dentry;
23043+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23044+ a->dst_dentry);
23045+ if (unlikely(err))
23046+ goto out_diropq;
23047+ }
1facf9fc 23048+
f2c43d5f
AM
23049+ /* remove whtmp */
23050+ if (a->thargs)
23051+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 23052+
f2c43d5f
AM
23053+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23054+ }
4a4d8108
AM
23055+ err = 0;
23056+ goto out_success;
23057+
4f0767ce 23058+out_diropq:
f2c43d5f 23059+ au_ren_rev_diropq(err, a);
4f0767ce 23060+out_rename:
7e9cd9fe 23061+ au_ren_rev_rename(err, a);
027c5e7a 23062+ dput(a->h_dst);
4f0767ce 23063+out_whtmp:
4a4d8108
AM
23064+ if (a->thargs)
23065+ au_ren_rev_whtmp(err, a);
4f0767ce 23066+out_whdst:
4a4d8108
AM
23067+ dput(a->dst_wh_dentry);
23068+ a->dst_wh_dentry = NULL;
4f0767ce 23069+out_whsrc:
4a4d8108
AM
23070+ if (a->src_wh_dentry)
23071+ au_ren_rev_whsrc(err, a);
4f0767ce 23072+out_success:
4a4d8108
AM
23073+ dput(a->src_wh_dentry);
23074+ dput(a->dst_wh_dentry);
4f0767ce 23075+out_thargs:
4a4d8108
AM
23076+ if (a->thargs) {
23077+ dput(a->h_dst);
23078+ au_whtmp_rmdir_free(a->thargs);
23079+ a->thargs = NULL;
23080+ }
4f0767ce 23081+out:
4a4d8108 23082+ return err;
dece6358 23083+}
1facf9fc 23084+
1308ab2a 23085+/* ---------------------------------------------------------------------- */
1facf9fc 23086+
4a4d8108
AM
23087+/*
23088+ * test if @dentry dir can be rename destination or not.
23089+ * success means, it is a logically empty dir.
23090+ */
23091+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 23092+{
4a4d8108 23093+ return au_test_empty(dentry, whlist);
1308ab2a 23094+}
1facf9fc 23095+
4a4d8108 23096+/*
8b6a4947
AM
23097+ * test if @a->src_dentry dir can be rename source or not.
23098+ * if it can, return 0.
4a4d8108
AM
23099+ * success means,
23100+ * - it is a logically empty dir.
23101+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 23102+ * on the lower branch unless DIRREN is on.
4a4d8108 23103+ */
8b6a4947 23104+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
23105+{
23106+ int err;
23107+ unsigned int rdhash;
8b6a4947
AM
23108+ aufs_bindex_t btop, btgt;
23109+ struct dentry *dentry;
23110+ struct super_block *sb;
23111+ struct au_sbinfo *sbinfo;
1facf9fc 23112+
8b6a4947
AM
23113+ dentry = a->src_dentry;
23114+ sb = dentry->d_sb;
23115+ sbinfo = au_sbi(sb);
23116+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23117+ au_fset_ren(a->auren_flags, DIRREN);
23118+
23119+ btgt = a->btgt;
5afbbe0d
AM
23120+ btop = au_dbtop(dentry);
23121+ if (btop != btgt) {
4a4d8108 23122+ struct au_nhash whlist;
dece6358 23123+
8b6a4947
AM
23124+ SiMustAnyLock(sb);
23125+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23126+ if (!rdhash)
23127+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23128+ dentry));
23129+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23130+ if (unlikely(err))
23131+ goto out;
23132+ err = au_test_empty(dentry, &whlist);
23133+ au_nhash_wh_free(&whlist);
23134+ goto out;
23135+ }
dece6358 23136+
5afbbe0d 23137+ if (btop == au_dbtaildir(dentry))
4a4d8108 23138+ return 0; /* success */
dece6358 23139+
4a4d8108 23140+ err = au_test_empty_lower(dentry);
1facf9fc 23141+
4f0767ce 23142+out:
4a4d8108 23143+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23144+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23145+ err = 0;
23146+ } else {
23147+ AuWarn1("renaming dir who has child(ren) on multiple "
23148+ "branches, is not supported\n");
23149+ err = -EXDEV;
23150+ }
4a4d8108
AM
23151+ }
23152+ return err;
23153+}
1308ab2a 23154+
4a4d8108
AM
23155+/* side effect: sets whlist and h_dentry */
23156+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23157+{
4a4d8108
AM
23158+ int err;
23159+ unsigned int rdhash;
23160+ struct dentry *d;
1facf9fc 23161+
4a4d8108
AM
23162+ d = a->dst_dentry;
23163+ SiMustAnyLock(d->d_sb);
1facf9fc 23164+
4a4d8108 23165+ err = 0;
f2c43d5f 23166+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23167+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23168+ if (!rdhash)
23169+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23170+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23171+ if (unlikely(err))
23172+ goto out;
1308ab2a 23173+
f2c43d5f
AM
23174+ if (!a->exchange) {
23175+ au_set_dbtop(d, a->dst_btop);
23176+ err = may_rename_dstdir(d, &a->whlist);
23177+ au_set_dbtop(d, a->btgt);
23178+ } else
8b6a4947 23179+ err = may_rename_srcdir(a);
4a4d8108 23180+ }
5afbbe0d 23181+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23182+ if (unlikely(err))
23183+ goto out;
23184+
23185+ d = a->src_dentry;
5afbbe0d 23186+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23187+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23188+ err = may_rename_srcdir(a);
4a4d8108
AM
23189+ if (unlikely(err)) {
23190+ au_nhash_wh_free(&a->whlist);
23191+ a->whlist.nh_num = 0;
23192+ }
23193+ }
4f0767ce 23194+out:
4a4d8108 23195+ return err;
1facf9fc 23196+}
23197+
4a4d8108 23198+/* ---------------------------------------------------------------------- */
1facf9fc 23199+
4a4d8108
AM
23200+/*
23201+ * simple tests for rename.
23202+ * following the checks in vfs, plus the parent-child relationship.
23203+ */
23204+static int au_may_ren(struct au_ren_args *a)
23205+{
23206+ int err, isdir;
23207+ struct inode *h_inode;
1facf9fc 23208+
5afbbe0d 23209+ if (a->src_btop == a->btgt) {
4a4d8108 23210+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23211+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23212+ if (unlikely(err))
23213+ goto out;
23214+ err = -EINVAL;
23215+ if (unlikely(a->src_h_dentry == a->h_trap))
23216+ goto out;
23217+ }
1facf9fc 23218+
4a4d8108 23219+ err = 0;
5afbbe0d 23220+ if (a->dst_btop != a->btgt)
4a4d8108 23221+ goto out;
1facf9fc 23222+
027c5e7a
AM
23223+ err = -ENOTEMPTY;
23224+ if (unlikely(a->dst_h_dentry == a->h_trap))
23225+ goto out;
23226+
4a4d8108 23227+ err = -EIO;
f2c43d5f 23228+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23229+ if (d_really_is_negative(a->dst_dentry)) {
23230+ if (d_is_negative(a->dst_h_dentry))
23231+ err = au_may_add(a->dst_dentry, a->btgt,
23232+ a->dst_h_parent, isdir);
4a4d8108 23233+ } else {
5527c038 23234+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23235+ goto out;
5527c038
JR
23236+ h_inode = d_inode(a->dst_h_dentry);
23237+ if (h_inode->i_nlink)
23238+ err = au_may_del(a->dst_dentry, a->btgt,
23239+ a->dst_h_parent, isdir);
4a4d8108 23240+ }
1facf9fc 23241+
4f0767ce 23242+out:
4a4d8108
AM
23243+ if (unlikely(err == -ENOENT || err == -EEXIST))
23244+ err = -EIO;
23245+ AuTraceErr(err);
23246+ return err;
23247+}
1facf9fc 23248+
1308ab2a 23249+/* ---------------------------------------------------------------------- */
1facf9fc 23250+
4a4d8108
AM
23251+/*
23252+ * locking order
23253+ * (VFS)
23254+ * - src_dir and dir by lock_rename()
23255+ * - inode if exitsts
23256+ * (aufs)
23257+ * - lock all
23258+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23259+ * + si_read_lock
23260+ * + di_write_lock2_child()
23261+ * + di_write_lock_child()
23262+ * + ii_write_lock_child()
23263+ * + di_write_lock_child2()
23264+ * + ii_write_lock_child2()
23265+ * + src_parent and parent
23266+ * + di_write_lock_parent()
23267+ * + ii_write_lock_parent()
23268+ * + di_write_lock_parent2()
23269+ * + ii_write_lock_parent2()
23270+ * + lower src_dir and dir by vfsub_lock_rename()
23271+ * + verify the every relationships between child and parent. if any
23272+ * of them failed, unlock all and return -EBUSY.
23273+ */
23274+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23275+{
4a4d8108
AM
23276+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23277+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23278+ if (au_ftest_ren(a->auren_flags, DIRREN)
23279+ && a->h_root)
23280+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23281+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23282+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23283+}
23284+
4a4d8108 23285+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23286+{
4a4d8108
AM
23287+ int err;
23288+ unsigned int udba;
1308ab2a 23289+
4a4d8108
AM
23290+ err = 0;
23291+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23292+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23293+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23294+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23295+
23296+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23297+ if (unlikely(err))
23298+ goto out;
f2c43d5f 23299+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23300+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23301+ struct dentry *root;
23302+ struct inode *dir;
23303+
23304+ /*
23305+ * sbinfo is already locked, so this ii_read_lock is
23306+ * unnecessary. but our debugging feature checks it.
23307+ */
23308+ root = a->src_inode->i_sb->s_root;
23309+ if (root != a->src_parent && root != a->dst_parent) {
23310+ dir = d_inode(root);
23311+ ii_read_lock_parent3(dir);
23312+ a->h_root = au_hi(dir, a->btgt);
23313+ ii_read_unlock(dir);
23314+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23315+ }
23316+ }
4a4d8108
AM
23317+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23318+ a->dst_h_parent, a->dst_hdir);
23319+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23320+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23321+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23322+ err = au_busy_or_stale();
5afbbe0d 23323+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23324+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23325+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23326+ a->br);
5afbbe0d 23327+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23328+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23329+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23330+ a->br);
86dc4139 23331+ if (!err)
4a4d8108 23332+ goto out; /* success */
4a4d8108
AM
23333+
23334+ err = au_busy_or_stale();
4a4d8108 23335+ au_ren_unlock(a);
86dc4139 23336+
4f0767ce 23337+out:
4a4d8108 23338+ return err;
1facf9fc 23339+}
23340+
23341+/* ---------------------------------------------------------------------- */
23342+
4a4d8108 23343+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23344+{
4a4d8108 23345+ struct inode *dir;
dece6358 23346+
4a4d8108 23347+ dir = a->dst_dir;
be118d29 23348+ inode_inc_iversion(dir);
f2c43d5f 23349+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23350+ /* is this updating defined in POSIX? */
23351+ au_cpup_attr_timesizes(a->src_inode);
23352+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23353+ }
b912730e 23354+ au_dir_ts(dir, a->btgt);
dece6358 23355+
f2c43d5f
AM
23356+ if (a->exchange) {
23357+ dir = a->src_dir;
be118d29 23358+ inode_inc_iversion(dir);
f2c43d5f
AM
23359+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23360+ /* is this updating defined in POSIX? */
23361+ au_cpup_attr_timesizes(a->dst_inode);
23362+ au_cpup_attr_nlink(dir, /*force*/1);
23363+ }
23364+ au_dir_ts(dir, a->btgt);
23365+ }
23366+
23367+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23368+ return;
dece6358 23369+
4a4d8108 23370+ dir = a->src_dir;
be118d29 23371+ inode_inc_iversion(dir);
f2c43d5f 23372+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23373+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23374+ au_dir_ts(dir, a->btgt);
1facf9fc 23375+}
23376+
4a4d8108 23377+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23378+{
5afbbe0d 23379+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23380+ struct dentry *d, *h_d;
23381+ struct inode *i, *h_i;
23382+ struct super_block *sb;
dece6358 23383+
027c5e7a
AM
23384+ d = a->dst_dentry;
23385+ d_drop(d);
23386+ if (a->h_dst)
23387+ /* already dget-ed by au_ren_or_cpup() */
23388+ au_set_h_dptr(d, a->btgt, a->h_dst);
23389+
23390+ i = a->dst_inode;
23391+ if (i) {
f2c43d5f
AM
23392+ if (!a->exchange) {
23393+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23394+ vfsub_drop_nlink(i);
23395+ else {
23396+ vfsub_dead_dir(i);
23397+ au_cpup_attr_timesizes(i);
23398+ }
23399+ au_update_dbrange(d, /*do_put_zero*/1);
23400+ } else
23401+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23402+ } else {
5afbbe0d
AM
23403+ bbot = a->btgt;
23404+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23405+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23406+ bbot = au_dbbot(d);
23407+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23408+ au_set_h_dptr(d, bindex, NULL);
23409+ au_update_dbrange(d, /*do_put_zero*/0);
23410+ }
23411+
8b6a4947
AM
23412+ if (a->exchange
23413+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23414+ d_drop(a->src_dentry);
23415+ if (au_ftest_ren(a->auren_flags, DIRREN))
23416+ au_set_dbwh(a->src_dentry, -1);
23417+ return;
23418+ }
23419+
4a4d8108 23420+ d = a->src_dentry;
8b6a4947
AM
23421+ au_set_dbwh(d, -1);
23422+ bbot = au_dbbot(d);
23423+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23424+ h_d = au_h_dptr(d, bindex);
23425+ if (h_d)
23426+ au_set_h_dptr(d, bindex, NULL);
23427+ }
23428+ au_set_dbbot(d, a->btgt);
4a4d8108 23429+
8b6a4947
AM
23430+ sb = d->d_sb;
23431+ i = a->src_inode;
23432+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23433+ return; /* success */
4a4d8108 23434+
8b6a4947
AM
23435+ bbot = au_ibbot(i);
23436+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23437+ h_i = au_h_iptr(i, bindex);
23438+ if (h_i) {
23439+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23440+ /* ignore this error */
23441+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23442+ }
23443+ }
8b6a4947 23444+ au_set_ibbot(i, a->btgt);
1308ab2a 23445+}
dece6358 23446+
4a4d8108
AM
23447+/* ---------------------------------------------------------------------- */
23448+
23449+/* mainly for link(2) and rename(2) */
23450+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23451+{
4a4d8108
AM
23452+ aufs_bindex_t bdiropq, bwh;
23453+ struct dentry *parent;
23454+ struct au_branch *br;
23455+
23456+ parent = dentry->d_parent;
5527c038 23457+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23458+
23459+ bdiropq = au_dbdiropq(parent);
23460+ bwh = au_dbwh(dentry);
23461+ br = au_sbr(dentry->d_sb, btgt);
23462+ if (au_br_rdonly(br)
23463+ || (0 <= bdiropq && bdiropq < btgt)
23464+ || (0 <= bwh && bwh < btgt))
23465+ btgt = -1;
23466+
23467+ AuDbg("btgt %d\n", btgt);
23468+ return btgt;
1facf9fc 23469+}
23470+
5afbbe0d 23471+/* sets src_btop, dst_btop and btgt */
4a4d8108 23472+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23473+{
4a4d8108
AM
23474+ int err;
23475+ struct au_wr_dir_args wr_dir_args = {
23476+ /* .force_btgt = -1, */
23477+ .flags = AuWrDir_ADD_ENTRY
23478+ };
dece6358 23479+
5afbbe0d
AM
23480+ a->src_btop = au_dbtop(a->src_dentry);
23481+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23482+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23483+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23484+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23485+ wr_dir_args.force_btgt = a->src_btop;
23486+ if (a->dst_inode && a->dst_btop < a->src_btop)
23487+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23488+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23489+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23490+ a->btgt = err;
f2c43d5f
AM
23491+ if (a->exchange)
23492+ au_update_dbtop(a->dst_dentry);
dece6358 23493+
4a4d8108 23494+ return err;
1facf9fc 23495+}
23496+
4a4d8108 23497+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23498+{
4a4d8108
AM
23499+ a->h_path.dentry = a->src_h_parent;
23500+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23501+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23502+ a->h_path.dentry = a->dst_h_parent;
23503+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23504+ }
1facf9fc 23505+
f2c43d5f
AM
23506+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23507+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23508+ && !a->exchange)
4a4d8108 23509+ return;
dece6358 23510+
4a4d8108
AM
23511+ a->h_path.dentry = a->src_h_dentry;
23512+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23513+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23514+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23515+ a->h_path.dentry = a->dst_h_dentry;
23516+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23517+ }
1308ab2a 23518+}
dece6358 23519+
4a4d8108 23520+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23521+{
4a4d8108 23522+ struct dentry *h_d;
febd17d6 23523+ struct inode *h_inode;
4a4d8108
AM
23524+
23525+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23526+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23527+ au_dtime_revert(a->dst_dt + AuPARENT);
23528+
f2c43d5f 23529+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23530+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23531+ h_inode = d_inode(h_d);
23532+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23533+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23534+ inode_unlock(h_inode);
4a4d8108 23535+
f2c43d5f 23536+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23537+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23538+ h_inode = d_inode(h_d);
23539+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23540+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 23541+ inode_unlock(h_inode);
1facf9fc 23542+ }
23543+ }
23544+}
23545+
4a4d8108
AM
23546+/* ---------------------------------------------------------------------- */
23547+
23548+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23549+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23550+ unsigned int _flags)
1facf9fc 23551+{
f2c43d5f 23552+ int err, lock_flags;
8b6a4947 23553+ void *rev;
4a4d8108
AM
23554+ /* reduce stack space */
23555+ struct au_ren_args *a;
f2c43d5f 23556+ struct au_pin pin;
4a4d8108 23557+
f2c43d5f 23558+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23559+ IMustLock(_src_dir);
23560+ IMustLock(_dst_dir);
23561+
f2c43d5f
AM
23562+ err = -EINVAL;
23563+ if (unlikely(_flags & RENAME_WHITEOUT))
23564+ goto out;
23565+
4a4d8108
AM
23566+ err = -ENOMEM;
23567+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23568+ a = kzalloc(sizeof(*a), GFP_NOFS);
23569+ if (unlikely(!a))
23570+ goto out;
23571+
f2c43d5f
AM
23572+ a->flags = _flags;
23573+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23574+ a->src_dir = _src_dir;
23575+ a->src_dentry = _src_dentry;
5527c038
JR
23576+ a->src_inode = NULL;
23577+ if (d_really_is_positive(a->src_dentry))
23578+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23579+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23580+ a->dst_dir = _dst_dir;
23581+ a->dst_dentry = _dst_dentry;
5527c038
JR
23582+ a->dst_inode = NULL;
23583+ if (d_really_is_positive(a->dst_dentry))
23584+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23585+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23586+ if (a->dst_inode) {
f2c43d5f
AM
23587+ /*
23588+ * if EXCHANGE && src is non-dir && dst is dir,
23589+ * dst is not locked.
23590+ */
23591+ /* IMustLock(a->dst_inode); */
4a4d8108 23592+ au_igrab(a->dst_inode);
1facf9fc 23593+ }
1facf9fc 23594+
4a4d8108 23595+ err = -ENOTDIR;
f2c43d5f 23596+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23597+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23598+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23599+ if (unlikely(!a->exchange
23600+ && d_really_is_positive(a->dst_dentry)
2000de60 23601+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23602+ goto out_free;
f2c43d5f
AM
23603+ lock_flags |= AuLock_DIRS;
23604+ }
23605+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23606+ au_fset_ren(a->auren_flags, ISDIR_DST);
23607+ if (unlikely(!a->exchange
23608+ && d_really_is_positive(a->src_dentry)
23609+ && !d_is_dir(a->src_dentry)))
23610+ goto out_free;
23611+ lock_flags |= AuLock_DIRS;
b95c5147 23612+ }
8b6a4947
AM
23613+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23614+ lock_flags);
e49829fe
JR
23615+ if (unlikely(err))
23616+ goto out_free;
1facf9fc 23617+
027c5e7a
AM
23618+ err = au_d_hashed_positive(a->src_dentry);
23619+ if (unlikely(err))
23620+ goto out_unlock;
23621+ err = -ENOENT;
23622+ if (a->dst_inode) {
23623+ /*
f2c43d5f 23624+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23625+ * function. It means we cannot rely upon d_unhashed().
23626+ */
23627+ if (unlikely(!a->dst_inode->i_nlink))
23628+ goto out_unlock;
f2c43d5f 23629+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23630+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23631+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23632+ goto out_unlock;
23633+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23634+ goto out_unlock;
23635+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23636+ goto out_unlock;
23637+
7eafdf33
AM
23638+ /*
23639+ * is it possible?
79b8bda9 23640+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23641+ * there may exist a problem somewhere else.
23642+ */
23643+ err = -EINVAL;
5527c038 23644+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23645+ goto out_unlock;
23646+
f2c43d5f 23647+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23648+ di_write_lock_parent(a->dst_parent);
1facf9fc 23649+
4a4d8108
AM
23650+ /* which branch we process */
23651+ err = au_ren_wbr(a);
23652+ if (unlikely(err < 0))
027c5e7a 23653+ goto out_parent;
4a4d8108 23654+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23655+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23656+
4a4d8108
AM
23657+ /* are they available to be renamed */
23658+ err = au_ren_may_dir(a);
23659+ if (unlikely(err))
23660+ goto out_children;
1facf9fc 23661+
4a4d8108 23662+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23663+ if (a->dst_btop == a->btgt) {
f2c43d5f 23664+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23665+ } else {
23666+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23667+ if (unlikely(err))
23668+ goto out_children;
23669+ }
1facf9fc 23670+
f2c43d5f
AM
23671+ err = 0;
23672+ if (!a->exchange) {
23673+ if (a->src_dir != a->dst_dir) {
23674+ /*
23675+ * this temporary unlock is safe,
23676+ * because both dir->i_mutex are locked.
23677+ */
23678+ di_write_unlock(a->dst_parent);
23679+ di_write_lock_parent(a->src_parent);
23680+ err = au_wr_dir_need_wh(a->src_dentry,
23681+ au_ftest_ren(a->auren_flags,
23682+ ISDIR_SRC),
23683+ &a->btgt);
23684+ di_write_unlock(a->src_parent);
23685+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23686+ /*isdir*/1);
23687+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23688+ } else
23689+ err = au_wr_dir_need_wh(a->src_dentry,
23690+ au_ftest_ren(a->auren_flags,
23691+ ISDIR_SRC),
23692+ &a->btgt);
23693+ }
4a4d8108
AM
23694+ if (unlikely(err < 0))
23695+ goto out_children;
23696+ if (err)
f2c43d5f 23697+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23698+
86dc4139 23699+ /* cpup src */
5afbbe0d 23700+ if (a->src_btop != a->btgt) {
86dc4139
AM
23701+ err = au_pin(&pin, a->src_dentry, a->btgt,
23702+ au_opt_udba(a->src_dentry->d_sb),
23703+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23704+ if (!err) {
c2b27bf2
AM
23705+ struct au_cp_generic cpg = {
23706+ .dentry = a->src_dentry,
23707+ .bdst = a->btgt,
5afbbe0d 23708+ .bsrc = a->src_btop,
c2b27bf2
AM
23709+ .len = -1,
23710+ .pin = &pin,
23711+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23712+ };
5afbbe0d 23713+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23714+ err = au_sio_cpup_simple(&cpg);
367653fa 23715+ au_unpin(&pin);
86dc4139 23716+ }
86dc4139
AM
23717+ if (unlikely(err))
23718+ goto out_children;
5afbbe0d 23719+ a->src_btop = a->btgt;
86dc4139 23720+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23721+ if (!a->exchange)
23722+ au_fset_ren(a->auren_flags, WHSRC);
23723+ }
23724+
23725+ /* cpup dst */
23726+ if (a->exchange && a->dst_inode
23727+ && a->dst_btop != a->btgt) {
23728+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23729+ au_opt_udba(a->dst_dentry->d_sb),
23730+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23731+ if (!err) {
23732+ struct au_cp_generic cpg = {
23733+ .dentry = a->dst_dentry,
23734+ .bdst = a->btgt,
23735+ .bsrc = a->dst_btop,
23736+ .len = -1,
23737+ .pin = &pin,
23738+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23739+ };
23740+ err = au_sio_cpup_simple(&cpg);
23741+ au_unpin(&pin);
23742+ }
23743+ if (unlikely(err))
23744+ goto out_children;
23745+ a->dst_btop = a->btgt;
23746+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23747+ }
23748+
4a4d8108
AM
23749+ /* lock them all */
23750+ err = au_ren_lock(a);
23751+ if (unlikely(err))
86dc4139 23752+ /* leave the copied-up one */
4a4d8108 23753+ goto out_children;
1facf9fc 23754+
f2c43d5f
AM
23755+ if (!a->exchange) {
23756+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23757+ err = au_may_ren(a);
23758+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23759+ err = -ENAMETOOLONG;
23760+ if (unlikely(err))
23761+ goto out_hdir;
23762+ }
1facf9fc 23763+
4a4d8108
AM
23764+ /* store timestamps to be revertible */
23765+ au_ren_dt(a);
1facf9fc 23766+
8b6a4947
AM
23767+ /* store dirren info */
23768+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23769+ err = au_dr_rename(a->src_dentry, a->btgt,
23770+ &a->dst_dentry->d_name, &rev);
23771+ AuTraceErr(err);
23772+ if (unlikely(err))
23773+ goto out_dt;
23774+ }
23775+
4a4d8108
AM
23776+ /* here we go */
23777+ err = do_rename(a);
23778+ if (unlikely(err))
8b6a4947
AM
23779+ goto out_dirren;
23780+
23781+ if (au_ftest_ren(a->auren_flags, DIRREN))
23782+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23783+
23784+ /* update dir attributes */
23785+ au_ren_refresh_dir(a);
23786+
23787+ /* dput/iput all lower dentries */
23788+ au_ren_refresh(a);
23789+
23790+ goto out_hdir; /* success */
23791+
8b6a4947
AM
23792+out_dirren:
23793+ if (au_ftest_ren(a->auren_flags, DIRREN))
23794+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23795+out_dt:
4a4d8108 23796+ au_ren_rev_dt(err, a);
4f0767ce 23797+out_hdir:
4a4d8108 23798+ au_ren_unlock(a);
4f0767ce 23799+out_children:
4a4d8108 23800+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23801+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23802+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23803+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23804+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23805+ }
027c5e7a 23806+out_parent:
f2c43d5f 23807+ if (!err) {
8b6a4947
AM
23808+ if (d_unhashed(a->src_dentry))
23809+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23810+ if (d_unhashed(a->dst_dentry))
23811+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23812+ if (!a->exchange)
23813+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23814+ else {
f2c43d5f 23815+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23816+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23817+ d_drop(a->dst_dentry);
23818+ }
23819+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23820+ d_drop(a->src_dentry);
f2c43d5f 23821+ } else {
5afbbe0d 23822+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23823+ if (!a->dst_inode)
23824+ d_drop(a->dst_dentry);
23825+ }
f2c43d5f 23826+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23827+ di_write_unlock(a->dst_parent);
23828+ else
23829+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23830+out_unlock:
4a4d8108 23831+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23832+out_free:
4a4d8108
AM
23833+ iput(a->dst_inode);
23834+ if (a->thargs)
23835+ au_whtmp_rmdir_free(a->thargs);
1c60b727 23836+ kfree(a);
4f0767ce 23837+out:
4a4d8108
AM
23838+ AuTraceErr(err);
23839+ return err;
1308ab2a 23840+}
7f207e10
AM
23841diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23842--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
b00004a5 23843+++ linux/fs/aufs/Kconfig 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
23844@@ -0,0 +1,199 @@
23845+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
23846+config AUFS_FS
23847+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23848+ help
23849+ Aufs is a stackable unification filesystem such as Unionfs,
23850+ which unifies several directories and provides a merged single
23851+ directory.
23852+ In the early days, aufs was entirely re-designed and
23853+ re-implemented Unionfs Version 1.x series. Introducing many
23854+ original ideas, approaches and improvements, it becomes totally
23855+ different from Unionfs while keeping the basic features.
1facf9fc 23856+
4a4d8108
AM
23857+if AUFS_FS
23858+choice
23859+ prompt "Maximum number of branches"
23860+ default AUFS_BRANCH_MAX_127
23861+ help
23862+ Specifies the maximum number of branches (or member directories)
23863+ in a single aufs. The larger value consumes more system
23864+ resources and has a minor impact to performance.
23865+config AUFS_BRANCH_MAX_127
23866+ bool "127"
23867+ help
23868+ Specifies the maximum number of branches (or member directories)
23869+ in a single aufs. The larger value consumes more system
23870+ resources and has a minor impact to performance.
23871+config AUFS_BRANCH_MAX_511
23872+ bool "511"
23873+ help
23874+ Specifies the maximum number of branches (or member directories)
23875+ in a single aufs. The larger value consumes more system
23876+ resources and has a minor impact to performance.
23877+config AUFS_BRANCH_MAX_1023
23878+ bool "1023"
23879+ help
23880+ Specifies the maximum number of branches (or member directories)
23881+ in a single aufs. The larger value consumes more system
23882+ resources and has a minor impact to performance.
23883+config AUFS_BRANCH_MAX_32767
23884+ bool "32767"
23885+ help
23886+ Specifies the maximum number of branches (or member directories)
23887+ in a single aufs. The larger value consumes more system
23888+ resources and has a minor impact to performance.
23889+endchoice
1facf9fc 23890+
e49829fe
JR
23891+config AUFS_SBILIST
23892+ bool
23893+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23894+ default y
23895+ help
23896+ Automatic configuration for internal use.
23897+ When aufs supports Magic SysRq or /proc, enabled automatically.
23898+
4a4d8108
AM
23899+config AUFS_HNOTIFY
23900+ bool "Detect direct branch access (bypassing aufs)"
23901+ help
23902+ If you want to modify files on branches directly, eg. bypassing aufs,
23903+ and want aufs to detect the changes of them fully, then enable this
23904+ option and use 'udba=notify' mount option.
7f207e10 23905+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
23906+ It will have a negative impact to the performance.
23907+ See detail in aufs.5.
dece6358 23908+
4a4d8108
AM
23909+choice
23910+ prompt "method" if AUFS_HNOTIFY
23911+ default AUFS_HFSNOTIFY
23912+config AUFS_HFSNOTIFY
23913+ bool "fsnotify"
23914+ select FSNOTIFY
4a4d8108 23915+endchoice
1facf9fc 23916+
4a4d8108
AM
23917+config AUFS_EXPORT
23918+ bool "NFS-exportable aufs"
2cbb1c4b 23919+ depends on EXPORTFS
4a4d8108
AM
23920+ help
23921+ If you want to export your mounted aufs via NFS, then enable this
23922+ option. There are several requirements for this configuration.
23923+ See detail in aufs.5.
1facf9fc 23924+
4a4d8108
AM
23925+config AUFS_INO_T_64
23926+ bool
23927+ depends on AUFS_EXPORT
23928+ depends on 64BIT && !(ALPHA || S390)
23929+ default y
23930+ help
23931+ Automatic configuration for internal use.
23932+ /* typedef unsigned long/int __kernel_ino_t */
23933+ /* alpha and s390x are int */
1facf9fc 23934+
c1595e42
JR
23935+config AUFS_XATTR
23936+ bool "support for XATTR/EA (including Security Labels)"
23937+ help
23938+ If your branch fs supports XATTR/EA and you want to make them
23939+ available in aufs too, then enable this opsion and specify the
23940+ branch attributes for EA.
23941+ See detail in aufs.5.
23942+
076b876e
AM
23943+config AUFS_FHSM
23944+ bool "File-based Hierarchical Storage Management"
23945+ help
23946+ Hierarchical Storage Management (or HSM) is a well-known feature
23947+ in the storage world. Aufs provides this feature as file-based.
23948+ with multiple branches.
23949+ These multiple branches are prioritized, ie. the topmost one
23950+ should be the fastest drive and be used heavily.
23951+
4a4d8108
AM
23952+config AUFS_RDU
23953+ bool "Readdir in userspace"
23954+ help
23955+ Aufs has two methods to provide a merged view for a directory,
23956+ by a user-space library and by kernel-space natively. The latter
23957+ is always enabled but sometimes large and slow.
23958+ If you enable this option, install the library in aufs2-util
23959+ package, and set some environment variables for your readdir(3),
23960+ then the work will be handled in user-space which generally
23961+ shows better performance in most cases.
23962+ See detail in aufs.5.
1facf9fc 23963+
8b6a4947
AM
23964+config AUFS_DIRREN
23965+ bool "Workaround for rename(2)-ing a directory"
23966+ help
23967+ By default, aufs returns EXDEV error in renameing a dir who has
23968+ his child on the lower branch, since it is a bad idea to issue
23969+ rename(2) internally for every lower branch. But user may not
23970+ accept this behaviour. So here is a workaround to allow such
23971+ rename(2) and store some extra infromation on the writable
23972+ branch. Obviously this costs high (and I don't like it).
23973+ To use this feature, you need to enable this configuration AND
23974+ to specify the mount option `dirren.'
23975+ See details in aufs.5 and the design documents.
23976+
4a4d8108
AM
23977+config AUFS_SHWH
23978+ bool "Show whiteouts"
23979+ help
23980+ If you want to make the whiteouts in aufs visible, then enable
23981+ this option and specify 'shwh' mount option. Although it may
23982+ sounds like philosophy or something, but in technically it
23983+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 23984+
4a4d8108
AM
23985+config AUFS_BR_RAMFS
23986+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
23987+ help
23988+ If you want to use ramfs as an aufs branch fs, then enable this
23989+ option. Generally tmpfs is recommended.
23990+ Aufs prohibited them to be a branch fs by default, because
23991+ initramfs becomes unusable after switch_root or something
23992+ generally. If you sets initramfs as an aufs branch and boot your
23993+ system by switch_root, you will meet a problem easily since the
23994+ files in initramfs may be inaccessible.
23995+ Unless you are going to use ramfs as an aufs branch fs without
23996+ switch_root or something, leave it N.
1facf9fc 23997+
4a4d8108
AM
23998+config AUFS_BR_FUSE
23999+ bool "Fuse fs as an aufs branch"
24000+ depends on FUSE_FS
24001+ select AUFS_POLL
24002+ help
24003+ If you want to use fuse-based userspace filesystem as an aufs
24004+ branch fs, then enable this option.
24005+ It implements the internal poll(2) operation which is
24006+ implemented by fuse only (curretnly).
1facf9fc 24007+
4a4d8108
AM
24008+config AUFS_POLL
24009+ bool
24010+ help
24011+ Automatic configuration for internal use.
1facf9fc 24012+
4a4d8108
AM
24013+config AUFS_BR_HFSPLUS
24014+ bool "Hfsplus as an aufs branch"
24015+ depends on HFSPLUS_FS
24016+ default y
24017+ help
24018+ If you want to use hfsplus fs as an aufs branch fs, then enable
24019+ this option. This option introduces a small overhead at
24020+ copying-up a file on hfsplus.
1facf9fc 24021+
4a4d8108
AM
24022+config AUFS_BDEV_LOOP
24023+ bool
24024+ depends on BLK_DEV_LOOP
24025+ default y
24026+ help
24027+ Automatic configuration for internal use.
24028+ Convert =[ym] into =y.
1308ab2a 24029+
4a4d8108
AM
24030+config AUFS_DEBUG
24031+ bool "Debug aufs"
24032+ help
24033+ Enable this to compile aufs internal debug code.
24034+ It will have a negative impact to the performance.
24035+
24036+config AUFS_MAGIC_SYSRQ
24037+ bool
24038+ depends on AUFS_DEBUG && MAGIC_SYSRQ
24039+ default y
24040+ help
24041+ Automatic configuration for internal use.
24042+ When aufs supports Magic SysRq, enabled automatically.
24043+endif
7f207e10
AM
24044diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24045--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
24046+++ linux/fs/aufs/loop.c 2018-07-19 09:46:13.059981241 +0200
24047@@ -0,0 +1,148 @@
24048+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24049+/*
b00004a5 24050+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24051+ *
24052+ * This program, aufs is free software; you can redistribute it and/or modify
24053+ * it under the terms of the GNU General Public License as published by
24054+ * the Free Software Foundation; either version 2 of the License, or
24055+ * (at your option) any later version.
dece6358
AM
24056+ *
24057+ * This program is distributed in the hope that it will be useful,
24058+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24059+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24060+ * GNU General Public License for more details.
24061+ *
24062+ * You should have received a copy of the GNU General Public License
523b37e3 24063+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24064+ */
24065+
24066+/*
24067+ * support for loopback block device as a branch
24068+ */
24069+
1facf9fc 24070+#include "aufs.h"
24071+
392086de
AM
24072+/* added into drivers/block/loop.c */
24073+static struct file *(*backing_file_func)(struct super_block *sb);
24074+
1facf9fc 24075+/*
24076+ * test if two lower dentries have overlapping branches.
24077+ */
b752ccd1 24078+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 24079+{
b752ccd1 24080+ struct super_block *h_sb;
392086de
AM
24081+ struct file *backing_file;
24082+
24083+ if (unlikely(!backing_file_func)) {
24084+ /* don't load "loop" module here */
24085+ backing_file_func = symbol_get(loop_backing_file);
24086+ if (unlikely(!backing_file_func))
24087+ /* "loop" module is not loaded */
24088+ return 0;
24089+ }
1facf9fc 24090+
b752ccd1 24091+ h_sb = h_adding->d_sb;
392086de
AM
24092+ backing_file = backing_file_func(h_sb);
24093+ if (!backing_file)
1facf9fc 24094+ return 0;
24095+
2000de60 24096+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
24097+ /*
24098+ * h_adding can be local NFS.
24099+ * in this case aufs cannot detect the loop.
24100+ */
24101+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 24102+ return 1;
b752ccd1 24103+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 24104+}
24105+
24106+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24107+int au_test_loopback_kthread(void)
24108+{
b752ccd1
AM
24109+ int ret;
24110+ struct task_struct *tsk = current;
a2a7ad62 24111+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
24112+
24113+ ret = 0;
24114+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
24115+ get_task_comm(comm, tsk);
24116+ c = comm[4];
b752ccd1 24117+ ret = ('0' <= c && c <= '9'
a2a7ad62 24118+ && !strncmp(comm, "loop", 4));
b752ccd1 24119+ }
1facf9fc 24120+
b752ccd1 24121+ return ret;
1facf9fc 24122+}
87a755f4
AM
24123+
24124+/* ---------------------------------------------------------------------- */
24125+
24126+#define au_warn_loopback_step 16
24127+static int au_warn_loopback_nelem = au_warn_loopback_step;
24128+static unsigned long *au_warn_loopback_array;
24129+
24130+void au_warn_loopback(struct super_block *h_sb)
24131+{
24132+ int i, new_nelem;
24133+ unsigned long *a, magic;
24134+ static DEFINE_SPINLOCK(spin);
24135+
24136+ magic = h_sb->s_magic;
24137+ spin_lock(&spin);
24138+ a = au_warn_loopback_array;
24139+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24140+ if (a[i] == magic) {
24141+ spin_unlock(&spin);
24142+ return;
24143+ }
24144+
24145+ /* h_sb is new to us, print it */
24146+ if (i < au_warn_loopback_nelem) {
24147+ a[i] = magic;
24148+ goto pr;
24149+ }
24150+
24151+ /* expand the array */
24152+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24153+ a = au_kzrealloc(au_warn_loopback_array,
24154+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24155+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24156+ /*may_shrink*/0);
87a755f4
AM
24157+ if (a) {
24158+ au_warn_loopback_nelem = new_nelem;
24159+ au_warn_loopback_array = a;
24160+ a[i] = magic;
24161+ goto pr;
24162+ }
24163+
24164+ spin_unlock(&spin);
24165+ AuWarn1("realloc failed, ignored\n");
24166+ return;
24167+
24168+pr:
24169+ spin_unlock(&spin);
0c3ec466
AM
24170+ pr_warn("you may want to try another patch for loopback file "
24171+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24172+}
24173+
24174+int au_loopback_init(void)
24175+{
24176+ int err;
24177+ struct super_block *sb __maybe_unused;
24178+
79b8bda9 24179+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
87a755f4
AM
24180+
24181+ err = 0;
24182+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24183+ sizeof(unsigned long), GFP_NOFS);
24184+ if (unlikely(!au_warn_loopback_array))
24185+ err = -ENOMEM;
24186+
24187+ return err;
24188+}
24189+
24190+void au_loopback_fin(void)
24191+{
79b8bda9
AM
24192+ if (backing_file_func)
24193+ symbol_put(loop_backing_file);
1c60b727 24194+ kfree(au_warn_loopback_array);
87a755f4 24195+}
7f207e10
AM
24196diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24197--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
24198+++ linux/fs/aufs/loop.h 2018-07-19 09:46:13.059981241 +0200
24199@@ -0,0 +1,53 @@
24200+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24201+/*
b00004a5 24202+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24203+ *
24204+ * This program, aufs is free software; you can redistribute it and/or modify
24205+ * it under the terms of the GNU General Public License as published by
24206+ * the Free Software Foundation; either version 2 of the License, or
24207+ * (at your option) any later version.
dece6358
AM
24208+ *
24209+ * This program is distributed in the hope that it will be useful,
24210+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24211+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24212+ * GNU General Public License for more details.
24213+ *
24214+ * You should have received a copy of the GNU General Public License
523b37e3 24215+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24216+ */
24217+
24218+/*
24219+ * support for loopback mount as a branch
24220+ */
24221+
24222+#ifndef __AUFS_LOOP_H__
24223+#define __AUFS_LOOP_H__
24224+
24225+#ifdef __KERNEL__
24226+
dece6358
AM
24227+struct dentry;
24228+struct super_block;
1facf9fc 24229+
24230+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24231+/* drivers/block/loop.c */
24232+struct file *loop_backing_file(struct super_block *sb);
24233+
1facf9fc 24234+/* loop.c */
b752ccd1 24235+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24236+int au_test_loopback_kthread(void);
87a755f4
AM
24237+void au_warn_loopback(struct super_block *h_sb);
24238+
24239+int au_loopback_init(void);
24240+void au_loopback_fin(void);
1facf9fc 24241+#else
4a4d8108 24242+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24243+ struct dentry *h_adding)
4a4d8108 24244+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24245+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24246+
24247+AuStubInt0(au_loopback_init, void)
24248+AuStubVoid(au_loopback_fin, void)
1facf9fc 24249+#endif /* BLK_DEV_LOOP */
24250+
24251+#endif /* __KERNEL__ */
24252+#endif /* __AUFS_LOOP_H__ */
7f207e10
AM
24253diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24254--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
b00004a5 24255+++ linux/fs/aufs/magic.mk 2018-06-04 09:08:09.188079511 +0200
2121bcd9
AM
24256@@ -0,0 +1,31 @@
24257+# SPDX-License-Identifier: GPL-2.0
1facf9fc 24258+
24259+# defined in ${srctree}/fs/fuse/inode.c
24260+# tristate
24261+ifdef CONFIG_FUSE_FS
24262+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24263+endif
24264+
1facf9fc 24265+# defined in ${srctree}/fs/xfs/xfs_sb.h
24266+# tristate
24267+ifdef CONFIG_XFS_FS
24268+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24269+endif
24270+
24271+# defined in ${srctree}/fs/configfs/mount.c
24272+# tristate
24273+ifdef CONFIG_CONFIGFS_FS
24274+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24275+endif
24276+
1facf9fc 24277+# defined in ${srctree}/fs/ubifs/ubifs.h
24278+# tristate
24279+ifdef CONFIG_UBIFS_FS
24280+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24281+endif
4a4d8108
AM
24282+
24283+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24284+# tristate
24285+ifdef CONFIG_HFSPLUS_FS
24286+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24287+endif
7f207e10
AM
24288diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24289--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
b00004a5 24290+++ linux/fs/aufs/Makefile 2018-06-04 09:08:09.181412645 +0200
2121bcd9
AM
24291@@ -0,0 +1,46 @@
24292+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
24293+
24294+include ${src}/magic.mk
24295+ifeq (${CONFIG_AUFS_FS},m)
24296+include ${src}/conf.mk
24297+endif
24298+-include ${src}/priv_def.mk
24299+
24300+# cf. include/linux/kernel.h
24301+# enable pr_debug
24302+ccflags-y += -DDEBUG
f6c5ef8b
AM
24303+# sparse requires the full pathname
24304+ifdef M
523b37e3 24305+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24306+else
523b37e3 24307+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24308+endif
4a4d8108
AM
24309+
24310+obj-$(CONFIG_AUFS_FS) += aufs.o
24311+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24312+ wkq.o vfsub.o dcsub.o \
e49829fe 24313+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24314+ dinfo.o dentry.o \
24315+ dynop.o \
24316+ finfo.o file.o f_op.o \
24317+ dir.o vdir.o \
24318+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24319+ mvdown.o ioctl.o
4a4d8108
AM
24320+
24321+# all are boolean
e49829fe 24322+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24323+aufs-$(CONFIG_SYSFS) += sysfs.o
24324+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24325+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24326+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24327+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24328+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24329+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24330+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24331+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24332+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24333+aufs-$(CONFIG_AUFS_POLL) += poll.o
24334+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24335+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24336+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24337+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10
AM
24338diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24339--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
24340+++ linux/fs/aufs/module.c 2018-07-19 09:46:13.059981241 +0200
24341@@ -0,0 +1,273 @@
24342+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24343+/*
b00004a5 24344+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24345+ *
24346+ * This program, aufs is free software; you can redistribute it and/or modify
24347+ * it under the terms of the GNU General Public License as published by
24348+ * the Free Software Foundation; either version 2 of the License, or
24349+ * (at your option) any later version.
dece6358
AM
24350+ *
24351+ * This program is distributed in the hope that it will be useful,
24352+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24353+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24354+ * GNU General Public License for more details.
24355+ *
24356+ * You should have received a copy of the GNU General Public License
523b37e3 24357+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24358+ */
24359+
24360+/*
24361+ * module global variables and operations
24362+ */
24363+
24364+#include <linux/module.h>
24365+#include <linux/seq_file.h>
24366+#include "aufs.h"
24367+
e2f27e51
AM
24368+/* shrinkable realloc */
24369+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24370+{
e2f27e51
AM
24371+ size_t sz;
24372+ int diff;
1facf9fc 24373+
e2f27e51
AM
24374+ sz = 0;
24375+ diff = -1;
24376+ if (p) {
24377+#if 0 /* unused */
24378+ if (!new_sz) {
1c60b727 24379+ kfree(p);
e2f27e51
AM
24380+ p = NULL;
24381+ goto out;
24382+ }
24383+#else
24384+ AuDebugOn(!new_sz);
24385+#endif
24386+ sz = ksize(p);
24387+ diff = au_kmidx_sub(sz, new_sz);
24388+ }
24389+ if (sz && !diff)
24390+ goto out;
24391+
24392+ if (sz < new_sz)
24393+ /* expand or SLOB */
24394+ p = krealloc(p, new_sz, gfp);
24395+ else if (new_sz < sz && may_shrink) {
24396+ /* shrink */
24397+ void *q;
24398+
24399+ q = kmalloc(new_sz, gfp);
24400+ if (q) {
24401+ if (p) {
24402+ memcpy(q, p, new_sz);
1c60b727 24403+ kfree(p);
e2f27e51
AM
24404+ }
24405+ p = q;
24406+ } else
24407+ p = NULL;
24408+ }
24409+
24410+out:
24411+ return p;
24412+}
24413+
24414+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24415+ int may_shrink)
24416+{
24417+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24418+ if (p && new_sz > nused)
1facf9fc 24419+ memset(p + nused, 0, new_sz - nused);
24420+ return p;
24421+}
24422+
24423+/* ---------------------------------------------------------------------- */
1facf9fc 24424+/*
24425+ * aufs caches
24426+ */
1c60b727 24427+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24428+
24429+static void au_cache_fin(void)
24430+{
24431+ int i;
24432+
24433+ /*
24434+ * Make sure all delayed rcu free inodes are flushed before we
24435+ * destroy cache.
24436+ */
24437+ rcu_barrier();
24438+
24439+ /* excluding AuCache_HNOTIFY */
24440+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24441+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24442+ kmem_cache_destroy(au_cache[i]);
24443+ au_cache[i] = NULL;
5afbbe0d
AM
24444+ }
24445+}
24446+
1facf9fc 24447+static int __init au_cache_init(void)
24448+{
1c60b727
AM
24449+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24450+ if (au_cache[AuCache_DINFO])
027c5e7a 24451+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24452+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24453+ au_icntnr_init_once);
1c60b727
AM
24454+ if (au_cache[AuCache_ICNTNR])
24455+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24456+ au_fi_init_once);
1c60b727
AM
24457+ if (au_cache[AuCache_FINFO])
24458+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24459+ if (au_cache[AuCache_VDIR])
24460+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24461+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24462+ return 0;
24463+
5afbbe0d 24464+ au_cache_fin();
1facf9fc 24465+ return -ENOMEM;
24466+}
24467+
1facf9fc 24468+/* ---------------------------------------------------------------------- */
24469+
24470+int au_dir_roflags;
24471+
e49829fe 24472+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24473+/*
24474+ * iterate_supers_type() doesn't protect us from
24475+ * remounting (branch management)
24476+ */
8b6a4947 24477+struct hlist_bl_head au_sbilist;
e49829fe
JR
24478+#endif
24479+
1facf9fc 24480+/*
24481+ * functions for module interface.
24482+ */
24483+MODULE_LICENSE("GPL");
24484+/* MODULE_LICENSE("GPL v2"); */
dece6358 24485+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24486+MODULE_DESCRIPTION(AUFS_NAME
24487+ " -- Advanced multi layered unification filesystem");
24488+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24489+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24490+
1facf9fc 24491+/* this module parameter has no meaning when SYSFS is disabled */
24492+int sysaufs_brs = 1;
24493+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24494+module_param_named(brs, sysaufs_brs, int, S_IRUGO);
24495+
076b876e 24496+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24497+bool au_userns;
076b876e
AM
24498+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24499+module_param_named(allow_userns, au_userns, bool, S_IRUGO);
24500+
1facf9fc 24501+/* ---------------------------------------------------------------------- */
24502+
24503+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24504+
24505+int au_seq_path(struct seq_file *seq, struct path *path)
24506+{
79b8bda9
AM
24507+ int err;
24508+
24509+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24510+ if (err >= 0)
79b8bda9 24511+ err = 0;
1c60b727 24512+ else
79b8bda9
AM
24513+ err = -ENOMEM;
24514+
24515+ return err;
1facf9fc 24516+}
24517+
24518+/* ---------------------------------------------------------------------- */
24519+
24520+static int __init aufs_init(void)
24521+{
24522+ int err, i;
24523+ char *p;
24524+
24525+ p = au_esc_chars;
24526+ for (i = 1; i <= ' '; i++)
24527+ *p++ = i;
24528+ *p++ = '\\';
24529+ *p++ = '\x7f';
24530+ *p = 0;
24531+
24532+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24533+
b95c5147
AM
24534+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24535+ for (i = 0; i < AuIop_Last; i++)
24536+ aufs_iop_nogetattr[i].getattr = NULL;
24537+
1c60b727 24538+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24539+
e49829fe 24540+ au_sbilist_init();
1facf9fc 24541+ sysaufs_brs_init();
24542+ au_debug_init();
4a4d8108 24543+ au_dy_init();
1facf9fc 24544+ err = sysaufs_init();
24545+ if (unlikely(err))
24546+ goto out;
062440b3 24547+ err = dbgaufs_init();
4f0767ce 24548+ if (unlikely(err))
953406b4 24549+ goto out_sysaufs;
062440b3
AM
24550+ err = au_procfs_init();
24551+ if (unlikely(err))
24552+ goto out_dbgaufs;
e49829fe
JR
24553+ err = au_wkq_init();
24554+ if (unlikely(err))
24555+ goto out_procfs;
87a755f4 24556+ err = au_loopback_init();
1facf9fc 24557+ if (unlikely(err))
24558+ goto out_wkq;
87a755f4
AM
24559+ err = au_hnotify_init();
24560+ if (unlikely(err))
24561+ goto out_loopback;
1facf9fc 24562+ err = au_sysrq_init();
24563+ if (unlikely(err))
24564+ goto out_hin;
24565+ err = au_cache_init();
24566+ if (unlikely(err))
24567+ goto out_sysrq;
076b876e
AM
24568+
24569+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24570+ err = register_filesystem(&aufs_fs_type);
24571+ if (unlikely(err))
24572+ goto out_cache;
076b876e 24573+
4a4d8108
AM
24574+ /* since we define pr_fmt, call printk directly */
24575+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24576+ goto out; /* success */
24577+
4f0767ce 24578+out_cache:
1facf9fc 24579+ au_cache_fin();
4f0767ce 24580+out_sysrq:
1facf9fc 24581+ au_sysrq_fin();
4f0767ce 24582+out_hin:
4a4d8108 24583+ au_hnotify_fin();
87a755f4
AM
24584+out_loopback:
24585+ au_loopback_fin();
4f0767ce 24586+out_wkq:
1facf9fc 24587+ au_wkq_fin();
e49829fe
JR
24588+out_procfs:
24589+ au_procfs_fin();
062440b3
AM
24590+out_dbgaufs:
24591+ dbgaufs_fin();
4f0767ce 24592+out_sysaufs:
1facf9fc 24593+ sysaufs_fin();
4a4d8108 24594+ au_dy_fin();
4f0767ce 24595+out:
1facf9fc 24596+ return err;
24597+}
24598+
24599+static void __exit aufs_exit(void)
24600+{
24601+ unregister_filesystem(&aufs_fs_type);
24602+ au_cache_fin();
24603+ au_sysrq_fin();
4a4d8108 24604+ au_hnotify_fin();
87a755f4 24605+ au_loopback_fin();
1facf9fc 24606+ au_wkq_fin();
e49829fe 24607+ au_procfs_fin();
062440b3 24608+ dbgaufs_fin();
1facf9fc 24609+ sysaufs_fin();
4a4d8108 24610+ au_dy_fin();
1facf9fc 24611+}
24612+
24613+module_init(aufs_init);
24614+module_exit(aufs_exit);
7f207e10
AM
24615diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24616--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
24617+++ linux/fs/aufs/module.h 2018-07-19 09:46:13.059981241 +0200
24618@@ -0,0 +1,102 @@
24619+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24620+/*
b00004a5 24621+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 24622+ *
24623+ * This program, aufs is free software; you can redistribute it and/or modify
24624+ * it under the terms of the GNU General Public License as published by
24625+ * the Free Software Foundation; either version 2 of the License, or
24626+ * (at your option) any later version.
dece6358
AM
24627+ *
24628+ * This program is distributed in the hope that it will be useful,
24629+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24630+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24631+ * GNU General Public License for more details.
24632+ *
24633+ * You should have received a copy of the GNU General Public License
523b37e3 24634+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24635+ */
24636+
24637+/*
24638+ * module initialization and module-global
24639+ */
24640+
24641+#ifndef __AUFS_MODULE_H__
24642+#define __AUFS_MODULE_H__
24643+
24644+#ifdef __KERNEL__
24645+
24646+#include <linux/slab.h>
24647+
dece6358
AM
24648+struct path;
24649+struct seq_file;
24650+
1facf9fc 24651+/* module parameters */
1facf9fc 24652+extern int sysaufs_brs;
8cdd5066 24653+extern bool au_userns;
1facf9fc 24654+
24655+/* ---------------------------------------------------------------------- */
24656+
24657+extern int au_dir_roflags;
24658+
e2f27e51
AM
24659+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24660+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24661+ int may_shrink);
24662+
24663+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24664+{
24665+#ifndef CONFIG_SLOB
24666+ return kmalloc_index(sz) - kmalloc_index(new_sz);
24667+#else
24668+ return -1; /* SLOB is untested */
24669+#endif
24670+}
24671+
1facf9fc 24672+int au_seq_path(struct seq_file *seq, struct path *path);
24673+
e49829fe
JR
24674+#ifdef CONFIG_PROC_FS
24675+/* procfs.c */
24676+int __init au_procfs_init(void);
24677+void au_procfs_fin(void);
24678+#else
24679+AuStubInt0(au_procfs_init, void);
24680+AuStubVoid(au_procfs_fin, void);
24681+#endif
24682+
4f0767ce
JR
24683+/* ---------------------------------------------------------------------- */
24684+
1c60b727 24685+/* kmem cache */
1facf9fc 24686+enum {
24687+ AuCache_DINFO,
24688+ AuCache_ICNTNR,
24689+ AuCache_FINFO,
24690+ AuCache_VDIR,
24691+ AuCache_DEHSTR,
7eafdf33 24692+ AuCache_HNOTIFY, /* must be last */
1facf9fc 24693+ AuCache_Last
24694+};
24695+
1c60b727 24696+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 24697+
4a4d8108
AM
24698+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24699+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
24700+#define AuCacheCtor(type, ctor) \
24701+ kmem_cache_create(#type, sizeof(struct type), \
24702+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 24703+
1facf9fc 24704+#define AuCacheFuncs(name, index) \
4a4d8108 24705+static inline struct au_##name *au_cache_alloc_##name(void) \
1c60b727 24706+{ return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
4a4d8108 24707+static inline void au_cache_free_##name(struct au_##name *p) \
1c60b727 24708+{ kmem_cache_free(au_cache[AuCache_##index], p); }
1facf9fc 24709+
24710+AuCacheFuncs(dinfo, DINFO);
24711+AuCacheFuncs(icntnr, ICNTNR);
24712+AuCacheFuncs(finfo, FINFO);
24713+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
24714+AuCacheFuncs(vdir_dehstr, DEHSTR);
24715+#ifdef CONFIG_AUFS_HNOTIFY
24716+AuCacheFuncs(hnotify, HNOTIFY);
24717+#endif
1facf9fc 24718+
4a4d8108
AM
24719+#endif /* __KERNEL__ */
24720+#endif /* __AUFS_MODULE_H__ */
c2b27bf2
AM
24721diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24722--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
24723+++ linux/fs/aufs/mvdown.c 2018-07-19 09:46:13.059981241 +0200
24724@@ -0,0 +1,705 @@
24725+/* SPDX-License-Identifier: GPL-2.0 */
c2b27bf2 24726+/*
b00004a5 24727+ * Copyright (C) 2011-2018 Junjiro R. Okajima
c2b27bf2
AM
24728+ *
24729+ * This program, aufs is free software; you can redistribute it and/or modify
24730+ * it under the terms of the GNU General Public License as published by
24731+ * the Free Software Foundation; either version 2 of the License, or
24732+ * (at your option) any later version.
24733+ *
24734+ * This program is distributed in the hope that it will be useful,
24735+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24736+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24737+ * GNU General Public License for more details.
24738+ *
24739+ * You should have received a copy of the GNU General Public License
523b37e3
AM
24740+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24741+ */
24742+
24743+/*
24744+ * move-down, opposite of copy-up
c2b27bf2
AM
24745+ */
24746+
24747+#include "aufs.h"
24748+
c2b27bf2
AM
24749+struct au_mvd_args {
24750+ struct {
c2b27bf2
AM
24751+ struct super_block *h_sb;
24752+ struct dentry *h_parent;
24753+ struct au_hinode *hdir;
392086de 24754+ struct inode *h_dir, *h_inode;
c1595e42 24755+ struct au_pin pin;
c2b27bf2
AM
24756+ } info[AUFS_MVDOWN_NARRAY];
24757+
24758+ struct aufs_mvdown mvdown;
24759+ struct dentry *dentry, *parent;
24760+ struct inode *inode, *dir;
24761+ struct super_block *sb;
24762+ aufs_bindex_t bopq, bwh, bfound;
24763+ unsigned char rename_lock;
c2b27bf2
AM
24764+};
24765+
392086de 24766+#define mvd_errno mvdown.au_errno
076b876e
AM
24767+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24768+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24769+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24770+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 24771+
392086de
AM
24772+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
24773+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
24774+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
24775+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
24776+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 24777+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
24778+
24779+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
24780+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
24781+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
24782+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
24783+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 24784+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
24785+
24786+#define AU_MVD_PR(flag, ...) do { \
24787+ if (flag) \
24788+ pr_err(__VA_ARGS__); \
24789+ } while (0)
24790+
076b876e
AM
24791+static int find_lower_writable(struct au_mvd_args *a)
24792+{
24793+ struct super_block *sb;
5afbbe0d 24794+ aufs_bindex_t bindex, bbot;
076b876e
AM
24795+ struct au_branch *br;
24796+
24797+ sb = a->sb;
24798+ bindex = a->mvd_bsrc;
5afbbe0d 24799+ bbot = au_sbbot(sb);
076b876e 24800+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 24801+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24802+ br = au_sbr(sb, bindex);
24803+ if (au_br_fhsm(br->br_perm)
8b6a4947 24804+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
24805+ return bindex;
24806+ }
24807+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 24808+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24809+ br = au_sbr(sb, bindex);
24810+ if (!au_br_rdonly(br))
24811+ return bindex;
24812+ }
24813+ else
5afbbe0d 24814+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 24815+ br = au_sbr(sb, bindex);
8b6a4947 24816+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
24817+ if (au_br_rdonly(br))
24818+ a->mvdown.flags
24819+ |= AUFS_MVDOWN_ROLOWER_R;
24820+ return bindex;
24821+ }
24822+ }
24823+
24824+ return -1;
24825+}
24826+
c2b27bf2 24827+/* make the parent dir on bdst */
392086de 24828+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24829+{
24830+ int err;
24831+
24832+ err = 0;
24833+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24834+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24835+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24836+ a->mvd_h_dst_parent = NULL;
5afbbe0d 24837+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
24838+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24839+ if (!a->mvd_h_dst_parent) {
24840+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
24841+ if (unlikely(err)) {
392086de 24842+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
24843+ goto out;
24844+ }
24845+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24846+ }
24847+
24848+out:
24849+ AuTraceErr(err);
24850+ return err;
24851+}
24852+
24853+/* lock them all */
392086de 24854+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24855+{
24856+ int err;
24857+ struct dentry *h_trap;
24858+
24859+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
24860+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
24861+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
24862+ au_opt_udba(a->sb),
24863+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24864+ AuTraceErr(err);
24865+ if (unlikely(err)) {
24866+ AU_MVD_PR(dmsg, "pin_dst failed\n");
24867+ goto out;
24868+ }
24869+
c2b27bf2
AM
24870+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
24871+ a->rename_lock = 0;
c1595e42
JR
24872+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24873+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
24874+ au_opt_udba(a->sb),
24875+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24876+ err = au_do_pin(&a->mvd_pin_src);
24877+ AuTraceErr(err);
5527c038 24878+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
24879+ if (unlikely(err)) {
24880+ AU_MVD_PR(dmsg, "pin_src failed\n");
24881+ goto out_dst;
24882+ }
24883+ goto out; /* success */
c2b27bf2
AM
24884+ }
24885+
c2b27bf2 24886+ a->rename_lock = 1;
c1595e42
JR
24887+ au_pin_hdir_unlock(&a->mvd_pin_dst);
24888+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24889+ au_opt_udba(a->sb),
24890+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24891+ AuTraceErr(err);
5527c038 24892+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
24893+ if (unlikely(err)) {
24894+ AU_MVD_PR(dmsg, "pin_src failed\n");
24895+ au_pin_hdir_lock(&a->mvd_pin_dst);
24896+ goto out_dst;
24897+ }
24898+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
24899+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24900+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
24901+ if (h_trap) {
24902+ err = (h_trap != a->mvd_h_src_parent);
24903+ if (err)
24904+ err = (h_trap != a->mvd_h_dst_parent);
24905+ }
24906+ BUG_ON(err); /* it should never happen */
c1595e42
JR
24907+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
24908+ err = -EBUSY;
24909+ AuTraceErr(err);
24910+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24911+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
24912+ au_pin_hdir_lock(&a->mvd_pin_src);
24913+ au_unpin(&a->mvd_pin_src);
24914+ au_pin_hdir_lock(&a->mvd_pin_dst);
24915+ goto out_dst;
24916+ }
24917+ goto out; /* success */
c2b27bf2 24918+
c1595e42
JR
24919+out_dst:
24920+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
24921+out:
24922+ AuTraceErr(err);
24923+ return err;
24924+}
24925+
392086de 24926+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 24927+{
c1595e42
JR
24928+ if (!a->rename_lock)
24929+ au_unpin(&a->mvd_pin_src);
24930+ else {
c2b27bf2
AM
24931+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24932+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
24933+ au_pin_hdir_lock(&a->mvd_pin_src);
24934+ au_unpin(&a->mvd_pin_src);
24935+ au_pin_hdir_lock(&a->mvd_pin_dst);
24936+ }
24937+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
24938+}
24939+
24940+/* copy-down the file */
392086de 24941+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24942+{
24943+ int err;
24944+ struct au_cp_generic cpg = {
24945+ .dentry = a->dentry,
24946+ .bdst = a->mvd_bdst,
24947+ .bsrc = a->mvd_bsrc,
24948+ .len = -1,
c1595e42 24949+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
24950+ .flags = AuCpup_DTIME | AuCpup_HOPEN
24951+ };
24952+
24953+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
24954+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
24955+ au_fset_cpup(cpg.flags, OVERWRITE);
24956+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
24957+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
24958+ err = au_sio_cpdown_simple(&cpg);
24959+ if (unlikely(err))
392086de 24960+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
24961+
24962+ AuTraceErr(err);
24963+ return err;
24964+}
24965+
24966+/*
24967+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
24968+ * were sleeping
24969+ */
392086de 24970+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
24971+{
24972+ int err;
24973+ struct path h_path;
24974+ struct au_branch *br;
523b37e3 24975+ struct inode *delegated;
c2b27bf2
AM
24976+
24977+ br = au_sbr(a->sb, a->mvd_bdst);
24978+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
24979+ err = PTR_ERR(h_path.dentry);
24980+ if (IS_ERR(h_path.dentry)) {
392086de 24981+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
24982+ goto out;
24983+ }
24984+
24985+ err = 0;
5527c038 24986+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 24987+ h_path.mnt = au_br_mnt(br);
523b37e3 24988+ delegated = NULL;
5527c038 24989+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
24990+ &delegated, /*force*/0);
24991+ if (unlikely(err == -EWOULDBLOCK)) {
24992+ pr_warn("cannot retry for NFSv4 delegation"
24993+ " for an internal unlink\n");
24994+ iput(delegated);
24995+ }
c2b27bf2 24996+ if (unlikely(err))
392086de 24997+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
24998+ }
24999+ dput(h_path.dentry);
25000+
25001+out:
25002+ AuTraceErr(err);
25003+ return err;
25004+}
25005+
25006+/*
25007+ * unlink the topmost h_dentry
c2b27bf2 25008+ */
392086de 25009+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25010+{
25011+ int err;
25012+ struct path h_path;
523b37e3 25013+ struct inode *delegated;
c2b27bf2
AM
25014+
25015+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25016+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
25017+ delegated = NULL;
25018+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25019+ if (unlikely(err == -EWOULDBLOCK)) {
25020+ pr_warn("cannot retry for NFSv4 delegation"
25021+ " for an internal unlink\n");
25022+ iput(delegated);
25023+ }
c2b27bf2 25024+ if (unlikely(err))
392086de 25025+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
25026+
25027+ AuTraceErr(err);
25028+ return err;
25029+}
25030+
076b876e
AM
25031+/* Since mvdown succeeded, we ignore an error of this function */
25032+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25033+{
25034+ int err;
25035+ struct au_branch *br;
25036+
25037+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25038+ br = au_sbr(a->sb, a->mvd_bsrc);
25039+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25040+ if (!err) {
25041+ br = au_sbr(a->sb, a->mvd_bdst);
25042+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25043+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25044+ }
25045+ if (!err)
25046+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25047+ else
25048+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25049+}
25050+
c2b27bf2
AM
25051+/*
25052+ * copy-down the file and unlink the bsrc file.
25053+ * - unlink the bdst whout if exist
25054+ * - copy-down the file (with whtmp name and rename)
25055+ * - unlink the bsrc file
25056+ */
392086de 25057+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25058+{
25059+ int err;
25060+
392086de 25061+ err = au_do_mkdir(dmsg, a);
c2b27bf2 25062+ if (!err)
392086de 25063+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
25064+ if (unlikely(err))
25065+ goto out;
25066+
25067+ /*
25068+ * do not revert the activities we made on bdst since they should be
25069+ * harmless in aufs.
25070+ */
25071+
392086de 25072+ err = au_do_cpdown(dmsg, a);
c2b27bf2 25073+ if (!err)
392086de
AM
25074+ err = au_do_unlink_wh(dmsg, a);
25075+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25076+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
25077+ if (unlikely(err))
25078+ goto out_unlock;
25079+
c1595e42
JR
25080+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25081+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
25082+ if (find_lower_writable(a) < 0)
25083+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25084+
25085+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25086+ au_do_stfs(dmsg, a);
25087+
c2b27bf2 25088+ /* maintain internal array */
392086de
AM
25089+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25090+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 25091+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 25092+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 25093+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
25094+ } else {
25095+ /* hide the lower */
25096+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 25097+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 25098+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 25099+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 25100+ }
5afbbe0d
AM
25101+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25102+ au_set_dbbot(a->dentry, a->mvd_bdst);
25103+ if (au_ibbot(a->inode) < a->mvd_bdst)
25104+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
25105+
25106+out_unlock:
392086de 25107+ au_do_unlock(dmsg, a);
c2b27bf2
AM
25108+out:
25109+ AuTraceErr(err);
25110+ return err;
25111+}
25112+
25113+/* ---------------------------------------------------------------------- */
25114+
c2b27bf2 25115+/* make sure the file is idle */
392086de 25116+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25117+{
25118+ int err, plinked;
c2b27bf2
AM
25119+
25120+ err = 0;
c2b27bf2 25121+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 25122+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 25123+ && au_dcount(a->dentry) == 1
c2b27bf2 25124+ && atomic_read(&a->inode->i_count) == 1
392086de 25125+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
25126+ && (!plinked || !au_plink_test(a->inode))
25127+ && a->inode->i_nlink == 1)
25128+ goto out;
25129+
25130+ err = -EBUSY;
392086de 25131+ AU_MVD_PR(dmsg,
c1595e42 25132+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 25133+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 25134+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 25135+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
25136+ plinked, plinked ? au_plink_test(a->inode) : 0);
25137+
25138+out:
25139+ AuTraceErr(err);
25140+ return err;
25141+}
25142+
25143+/* make sure the parent dir is fine */
392086de 25144+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25145+ struct au_mvd_args *a)
25146+{
25147+ int err;
25148+ aufs_bindex_t bindex;
25149+
25150+ err = 0;
25151+ if (unlikely(au_alive_dir(a->parent))) {
25152+ err = -ENOENT;
392086de 25153+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25154+ goto out;
25155+ }
25156+
25157+ a->bopq = au_dbdiropq(a->parent);
25158+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25159+ AuDbg("b%d\n", bindex);
25160+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25161+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25162+ err = -EINVAL;
392086de
AM
25163+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25164+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25165+ a->bopq, a->mvd_bdst);
25166+ }
25167+
25168+out:
25169+ AuTraceErr(err);
25170+ return err;
25171+}
25172+
392086de 25173+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25174+ struct au_mvd_args *a)
25175+{
25176+ int err;
25177+ struct au_dinfo *dinfo, *tmp;
25178+
25179+ /* lookup the next lower positive entry */
25180+ err = -ENOMEM;
25181+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25182+ if (unlikely(!tmp))
25183+ goto out;
25184+
25185+ a->bfound = -1;
25186+ a->bwh = -1;
25187+ dinfo = au_di(a->dentry);
25188+ au_di_cp(tmp, dinfo);
25189+ au_di_swap(tmp, dinfo);
25190+
25191+ /* returns the number of positive dentries */
5afbbe0d
AM
25192+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25193+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25194+ if (!err)
25195+ a->bwh = au_dbwh(a->dentry);
25196+ else if (err > 0)
5afbbe0d 25197+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25198+
25199+ au_di_swap(tmp, dinfo);
25200+ au_rw_write_unlock(&tmp->di_rwsem);
25201+ au_di_free(tmp);
25202+ if (unlikely(err < 0))
392086de 25203+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25204+
25205+ /*
25206+ * here, we have these cases.
25207+ * bfound == -1
25208+ * no positive dentry under bsrc. there are more sub-cases.
25209+ * bwh < 0
25210+ * there no whiteout, we can safely move-down.
25211+ * bwh <= bsrc
25212+ * impossible
25213+ * bsrc < bwh && bwh < bdst
25214+ * there is a whiteout on RO branch. cannot proceed.
25215+ * bwh == bdst
25216+ * there is a whiteout on the RW target branch. it should
25217+ * be removed.
25218+ * bdst < bwh
25219+ * there is a whiteout somewhere unrelated branch.
25220+ * -1 < bfound && bfound <= bsrc
25221+ * impossible.
25222+ * bfound < bdst
25223+ * found, but it is on RO branch between bsrc and bdst. cannot
25224+ * proceed.
25225+ * bfound == bdst
25226+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25227+ * error.
25228+ * bdst < bfound
25229+ * found, after we create the file on bdst, it will be hidden.
25230+ */
25231+
25232+ AuDebugOn(a->bfound == -1
25233+ && a->bwh != -1
25234+ && a->bwh <= a->mvd_bsrc);
25235+ AuDebugOn(-1 < a->bfound
25236+ && a->bfound <= a->mvd_bsrc);
25237+
25238+ err = -EINVAL;
25239+ if (a->bfound == -1
25240+ && a->mvd_bsrc < a->bwh
25241+ && a->bwh != -1
25242+ && a->bwh < a->mvd_bdst) {
392086de
AM
25243+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25244+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25245+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25246+ goto out;
25247+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25248+ a->mvd_errno = EAU_MVDOWN_UPPER;
25249+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25250+ a->mvd_bdst, a->bfound);
25251+ goto out;
25252+ }
25253+
25254+ err = 0; /* success */
25255+
25256+out:
25257+ AuTraceErr(err);
25258+ return err;
25259+}
25260+
392086de 25261+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25262+{
25263+ int err;
25264+
392086de
AM
25265+ err = 0;
25266+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25267+ && a->bfound == a->mvd_bdst)
25268+ err = -EEXIST;
c2b27bf2
AM
25269+ AuTraceErr(err);
25270+ return err;
25271+}
25272+
392086de 25273+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25274+{
25275+ int err;
25276+ struct au_branch *br;
25277+
25278+ err = -EISDIR;
25279+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25280+ goto out;
25281+
25282+ err = -EINVAL;
392086de 25283+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25284+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25285+ else {
25286+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25287+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25288+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25289+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25290+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25291+ || (a->mvd_bsrc < au_ibtop(a->inode)
25292+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25293+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25294+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25295+ AU_MVD_PR(dmsg, "no upper\n");
25296+ goto out;
25297+ }
25298+ }
5afbbe0d 25299+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25300+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25301+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25302+ goto out;
25303+ }
392086de 25304+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25305+ br = au_sbr(a->sb, a->mvd_bsrc);
25306+ err = au_br_rdonly(br);
392086de
AM
25307+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25308+ if (unlikely(err))
25309+ goto out;
25310+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25311+ || IS_APPEND(a->mvd_h_src_inode))) {
25312+ if (err)
25313+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25314+ /* go on */
25315+ } else
c2b27bf2
AM
25316+ goto out;
25317+
25318+ err = -EINVAL;
392086de
AM
25319+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25320+ a->mvd_bdst = find_lower_writable(a);
25321+ if (unlikely(a->mvd_bdst < 0)) {
25322+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25323+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25324+ goto out;
25325+ }
25326+ } else {
25327+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25328+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25329+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25330+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25331+ AU_MVD_PR(dmsg, "no lower brid\n");
25332+ goto out;
25333+ }
c2b27bf2
AM
25334+ }
25335+
392086de 25336+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25337+ if (!err)
392086de 25338+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25339+ if (!err)
392086de 25340+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25341+ if (!err)
392086de 25342+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25343+ if (!err)
25344+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25345+
25346+out:
25347+ AuTraceErr(err);
25348+ return err;
25349+}
25350+
25351+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25352+{
392086de
AM
25353+ int err, e;
25354+ unsigned char dmsg;
25355+ struct au_mvd_args *args;
79b8bda9 25356+ struct inode *inode;
c2b27bf2 25357+
79b8bda9 25358+ inode = d_inode(dentry);
c2b27bf2
AM
25359+ err = -EPERM;
25360+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25361+ goto out;
25362+
392086de
AM
25363+ err = -ENOMEM;
25364+ args = kmalloc(sizeof(*args), GFP_NOFS);
25365+ if (unlikely(!args))
25366+ goto out;
25367+
25368+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25369+ if (!err)
25370+ err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
c2b27bf2
AM
25371+ if (unlikely(err)) {
25372+ err = -EFAULT;
392086de
AM
25373+ AuTraceErr(err);
25374+ goto out_free;
c2b27bf2 25375+ }
392086de
AM
25376+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25377+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25378+ args->mvdown.au_errno = 0;
25379+ args->dentry = dentry;
79b8bda9 25380+ args->inode = inode;
392086de 25381+ args->sb = dentry->d_sb;
c2b27bf2 25382+
392086de
AM
25383+ err = -ENOENT;
25384+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25385+ args->parent = dget_parent(dentry);
5527c038 25386+ args->dir = d_inode(args->parent);
febd17d6 25387+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25388+ dput(args->parent);
25389+ if (unlikely(args->parent != dentry->d_parent)) {
25390+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25391+ goto out_dir;
25392+ }
25393+
febd17d6 25394+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25395+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25396+ if (unlikely(err))
25397+ goto out_inode;
25398+
392086de
AM
25399+ di_write_lock_parent(args->parent);
25400+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25401+ if (unlikely(err))
25402+ goto out_parent;
25403+
392086de 25404+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25405+ if (unlikely(err))
25406+ goto out_parent;
c2b27bf2 25407+
392086de 25408+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25409+ au_cpup_attr_timesizes(inode);
25410+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25411+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25412+ /* au_digen_dec(dentry); */
25413+
25414+out_parent:
392086de 25415+ di_write_unlock(args->parent);
c2b27bf2
AM
25416+ aufs_read_unlock(dentry, AuLock_DW);
25417+out_inode:
febd17d6 25418+ inode_unlock(inode);
c2b27bf2 25419+out_dir:
febd17d6 25420+ inode_unlock(args->dir);
392086de
AM
25421+out_free:
25422+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25423+ if (unlikely(e))
25424+ err = -EFAULT;
1c60b727 25425+ kfree(args);
c2b27bf2
AM
25426+out:
25427+ AuTraceErr(err);
25428+ return err;
25429+}
25430diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25431--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
062440b3 25432+++ linux/fs/aufs/opts.c 2018-07-19 09:46:13.059981241 +0200
8b6a4947 25433@@ -0,0 +1,1891 @@
062440b3 25434+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 25435+/*
b00004a5 25436+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 25437+ *
25438+ * This program, aufs is free software; you can redistribute it and/or modify
25439+ * it under the terms of the GNU General Public License as published by
25440+ * the Free Software Foundation; either version 2 of the License, or
25441+ * (at your option) any later version.
dece6358
AM
25442+ *
25443+ * This program is distributed in the hope that it will be useful,
25444+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25445+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25446+ * GNU General Public License for more details.
25447+ *
25448+ * You should have received a copy of the GNU General Public License
523b37e3 25449+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25450+ */
25451+
25452+/*
25453+ * mount options/flags
25454+ */
25455+
dece6358 25456+#include <linux/namei.h>
1facf9fc 25457+#include <linux/types.h> /* a distribution requires */
25458+#include <linux/parser.h>
25459+#include "aufs.h"
25460+
25461+/* ---------------------------------------------------------------------- */
25462+
25463+enum {
25464+ Opt_br,
7e9cd9fe
AM
25465+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25466+ Opt_idel, Opt_imod,
25467+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25468+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25469+ Opt_xino, Opt_noxino,
1facf9fc 25470+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25471+ Opt_trunc_xino_path, Opt_itrunc_xino,
25472+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25473+ Opt_shwh, Opt_noshwh,
1facf9fc 25474+ Opt_plink, Opt_noplink, Opt_list_plink,
25475+ Opt_udba,
4a4d8108 25476+ Opt_dio, Opt_nodio,
1facf9fc 25477+ Opt_diropq_a, Opt_diropq_w,
25478+ Opt_warn_perm, Opt_nowarn_perm,
25479+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25480+ Opt_fhsm_sec,
1facf9fc 25481+ Opt_verbose, Opt_noverbose,
25482+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25483+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25484+ Opt_dirren, Opt_nodirren,
c1595e42 25485+ Opt_acl, Opt_noacl,
1facf9fc 25486+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25487+};
25488+
25489+static match_table_t options = {
25490+ {Opt_br, "br=%s"},
25491+ {Opt_br, "br:%s"},
25492+
25493+ {Opt_add, "add=%d:%s"},
25494+ {Opt_add, "add:%d:%s"},
25495+ {Opt_add, "ins=%d:%s"},
25496+ {Opt_add, "ins:%d:%s"},
25497+ {Opt_append, "append=%s"},
25498+ {Opt_append, "append:%s"},
25499+ {Opt_prepend, "prepend=%s"},
25500+ {Opt_prepend, "prepend:%s"},
25501+
25502+ {Opt_del, "del=%s"},
25503+ {Opt_del, "del:%s"},
25504+ /* {Opt_idel, "idel:%d"}, */
25505+ {Opt_mod, "mod=%s"},
25506+ {Opt_mod, "mod:%s"},
25507+ /* {Opt_imod, "imod:%d:%s"}, */
25508+
25509+ {Opt_dirwh, "dirwh=%d"},
25510+
25511+ {Opt_xino, "xino=%s"},
25512+ {Opt_noxino, "noxino"},
25513+ {Opt_trunc_xino, "trunc_xino"},
25514+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25515+ {Opt_notrunc_xino, "notrunc_xino"},
25516+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25517+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25518+ /* {Opt_zxino, "zxino=%s"}, */
25519+ {Opt_trunc_xib, "trunc_xib"},
25520+ {Opt_notrunc_xib, "notrunc_xib"},
25521+
e49829fe 25522+#ifdef CONFIG_PROC_FS
1facf9fc 25523+ {Opt_plink, "plink"},
e49829fe
JR
25524+#else
25525+ {Opt_ignore_silent, "plink"},
25526+#endif
25527+
1facf9fc 25528+ {Opt_noplink, "noplink"},
e49829fe 25529+
1facf9fc 25530+#ifdef CONFIG_AUFS_DEBUG
25531+ {Opt_list_plink, "list_plink"},
25532+#endif
25533+
25534+ {Opt_udba, "udba=%s"},
25535+
4a4d8108
AM
25536+ {Opt_dio, "dio"},
25537+ {Opt_nodio, "nodio"},
25538+
8b6a4947
AM
25539+#ifdef CONFIG_AUFS_DIRREN
25540+ {Opt_dirren, "dirren"},
25541+ {Opt_nodirren, "nodirren"},
25542+#else
25543+ {Opt_ignore, "dirren"},
25544+ {Opt_ignore_silent, "nodirren"},
25545+#endif
25546+
076b876e
AM
25547+#ifdef CONFIG_AUFS_FHSM
25548+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25549+#else
8b6a4947 25550+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25551+#endif
25552+
1facf9fc 25553+ {Opt_diropq_a, "diropq=always"},
25554+ {Opt_diropq_a, "diropq=a"},
25555+ {Opt_diropq_w, "diropq=whiteouted"},
25556+ {Opt_diropq_w, "diropq=w"},
25557+
25558+ {Opt_warn_perm, "warn_perm"},
25559+ {Opt_nowarn_perm, "nowarn_perm"},
25560+
25561+ /* keep them temporary */
1facf9fc 25562+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25563+ {Opt_ignore, "clean_plink"},
1facf9fc 25564+
dece6358
AM
25565+#ifdef CONFIG_AUFS_SHWH
25566+ {Opt_shwh, "shwh"},
25567+#endif
25568+ {Opt_noshwh, "noshwh"},
25569+
076b876e
AM
25570+ {Opt_dirperm1, "dirperm1"},
25571+ {Opt_nodirperm1, "nodirperm1"},
25572+
1facf9fc 25573+ {Opt_verbose, "verbose"},
25574+ {Opt_verbose, "v"},
25575+ {Opt_noverbose, "noverbose"},
25576+ {Opt_noverbose, "quiet"},
25577+ {Opt_noverbose, "q"},
25578+ {Opt_noverbose, "silent"},
25579+
25580+ {Opt_sum, "sum"},
25581+ {Opt_nosum, "nosum"},
25582+ {Opt_wsum, "wsum"},
25583+
25584+ {Opt_rdcache, "rdcache=%d"},
25585+ {Opt_rdblk, "rdblk=%d"},
dece6358 25586+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25587+ {Opt_rdhash, "rdhash=%d"},
dece6358 25588+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25589+
25590+ {Opt_wbr_create, "create=%s"},
25591+ {Opt_wbr_create, "create_policy=%s"},
25592+ {Opt_wbr_copyup, "cpup=%s"},
25593+ {Opt_wbr_copyup, "copyup=%s"},
25594+ {Opt_wbr_copyup, "copyup_policy=%s"},
25595+
c1595e42
JR
25596+ /* generic VFS flag */
25597+#ifdef CONFIG_FS_POSIX_ACL
25598+ {Opt_acl, "acl"},
25599+ {Opt_noacl, "noacl"},
25600+#else
8b6a4947 25601+ {Opt_ignore, "acl"},
c1595e42
JR
25602+ {Opt_ignore_silent, "noacl"},
25603+#endif
25604+
1facf9fc 25605+ /* internal use for the scripts */
25606+ {Opt_ignore_silent, "si=%s"},
25607+
25608+ {Opt_br, "dirs=%s"},
25609+ {Opt_ignore, "debug=%d"},
25610+ {Opt_ignore, "delete=whiteout"},
25611+ {Opt_ignore, "delete=all"},
25612+ {Opt_ignore, "imap=%s"},
25613+
1308ab2a 25614+ /* temporary workaround, due to old mount(8)? */
25615+ {Opt_ignore_silent, "relatime"},
25616+
1facf9fc 25617+ {Opt_err, NULL}
25618+};
25619+
25620+/* ---------------------------------------------------------------------- */
25621+
076b876e 25622+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25623+{
076b876e
AM
25624+ struct match_token *p;
25625+
25626+ p = tbl;
25627+ while (p->pattern) {
25628+ if (p->token == val)
25629+ return p->pattern;
25630+ p++;
1facf9fc 25631+ }
25632+ BUG();
25633+ return "??";
25634+}
25635+
076b876e
AM
25636+static const char *au_optstr(int *val, match_table_t tbl)
25637+{
25638+ struct match_token *p;
25639+ int v;
25640+
25641+ v = *val;
2000de60
JR
25642+ if (!v)
25643+ goto out;
076b876e 25644+ p = tbl;
2000de60
JR
25645+ while (p->pattern) {
25646+ if (p->token
25647+ && (v & p->token) == p->token) {
076b876e
AM
25648+ *val &= ~p->token;
25649+ return p->pattern;
25650+ }
25651+ p++;
25652+ }
2000de60
JR
25653+
25654+out:
076b876e
AM
25655+ return NULL;
25656+}
25657+
1facf9fc 25658+/* ---------------------------------------------------------------------- */
25659+
1e00d052 25660+static match_table_t brperm = {
1facf9fc 25661+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25662+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25663+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
25664+ {0, NULL}
25665+};
1facf9fc 25666+
86dc4139 25667+static match_table_t brattr = {
076b876e
AM
25668+ /* general */
25669+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25670+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 25671+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 25672+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 25673+#ifdef CONFIG_AUFS_FHSM
076b876e 25674+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
25675+#endif
25676+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
25677+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25678+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25679+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25680+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25681+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25682+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 25683+#endif
076b876e
AM
25684+
25685+ /* ro/rr branch */
1e00d052 25686+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
25687+
25688+ /* rw branch */
25689+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 25690+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 25691+
1e00d052 25692+ {0, NULL}
1facf9fc 25693+};
25694+
1e00d052
AM
25695+static int br_attr_val(char *str, match_table_t table, substring_t args[])
25696+{
25697+ int attr, v;
25698+ char *p;
25699+
25700+ attr = 0;
25701+ do {
25702+ p = strchr(str, '+');
25703+ if (p)
25704+ *p = 0;
25705+ v = match_token(str, table, args);
076b876e
AM
25706+ if (v) {
25707+ if (v & AuBrAttr_CMOO_Mask)
25708+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 25709+ attr |= v;
076b876e 25710+ } else {
1e00d052
AM
25711+ if (p)
25712+ *p = '+';
0c3ec466 25713+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
25714+ break;
25715+ }
25716+ if (p)
25717+ str = p + 1;
25718+ } while (p);
25719+
25720+ return attr;
25721+}
25722+
076b876e
AM
25723+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25724+{
25725+ int sz;
25726+ const char *p;
25727+ char *q;
25728+
076b876e
AM
25729+ q = str->a;
25730+ *q = 0;
25731+ p = au_optstr(&perm, brattr);
25732+ if (p) {
25733+ sz = strlen(p);
25734+ memcpy(q, p, sz + 1);
25735+ q += sz;
25736+ } else
25737+ goto out;
25738+
25739+ do {
25740+ p = au_optstr(&perm, brattr);
25741+ if (p) {
25742+ *q++ = '+';
25743+ sz = strlen(p);
25744+ memcpy(q, p, sz + 1);
25745+ q += sz;
25746+ }
25747+ } while (p);
25748+
25749+out:
c1595e42 25750+ return q - str->a;
076b876e
AM
25751+}
25752+
4a4d8108 25753+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 25754+{
076b876e
AM
25755+ int val, bad, sz;
25756+ char *p;
1facf9fc 25757+ substring_t args[MAX_OPT_ARGS];
076b876e 25758+ au_br_perm_str_t attr;
1facf9fc 25759+
1e00d052
AM
25760+ p = strchr(perm, '+');
25761+ if (p)
25762+ *p = 0;
25763+ val = match_token(perm, brperm, args);
25764+ if (!val) {
25765+ if (p)
25766+ *p = '+';
0c3ec466 25767+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
25768+ val = AuBrPerm_RO;
25769+ goto out;
25770+ }
25771+ if (!p)
25772+ goto out;
25773+
076b876e
AM
25774+ val |= br_attr_val(p + 1, brattr, args);
25775+
25776+ bad = 0;
86dc4139 25777+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
25778+ case AuBrPerm_RO:
25779+ case AuBrPerm_RR:
076b876e
AM
25780+ bad = val & AuBrWAttr_Mask;
25781+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
25782+ break;
25783+ case AuBrPerm_RW:
076b876e
AM
25784+ bad = val & AuBrRAttr_Mask;
25785+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
25786+ break;
25787+ }
c1595e42
JR
25788+
25789+ /*
25790+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25791+ * does not treat it as an error, just warning.
25792+ * this is a tiny guard for the user operation.
25793+ */
25794+ if (val & AuBrAttr_UNPIN) {
25795+ bad |= AuBrAttr_UNPIN;
25796+ val &= ~AuBrAttr_UNPIN;
25797+ }
25798+
076b876e
AM
25799+ if (unlikely(bad)) {
25800+ sz = au_do_optstr_br_attr(&attr, bad);
25801+ AuDebugOn(!sz);
25802+ pr_warn("ignored branch attribute %s\n", attr.a);
25803+ }
1e00d052
AM
25804+
25805+out:
1facf9fc 25806+ return val;
25807+}
25808+
076b876e 25809+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 25810+{
076b876e
AM
25811+ au_br_perm_str_t attr;
25812+ const char *p;
25813+ char *q;
1e00d052
AM
25814+ int sz;
25815+
076b876e
AM
25816+ q = str->a;
25817+ p = au_optstr(&perm, brperm);
25818+ AuDebugOn(!p || !*p);
25819+ sz = strlen(p);
25820+ memcpy(q, p, sz + 1);
25821+ q += sz;
1e00d052 25822+
076b876e
AM
25823+ sz = au_do_optstr_br_attr(&attr, perm);
25824+ if (sz) {
25825+ *q++ = '+';
25826+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
25827+ }
25828+
076b876e 25829+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 25830+}
25831+
25832+/* ---------------------------------------------------------------------- */
25833+
25834+static match_table_t udbalevel = {
25835+ {AuOpt_UDBA_REVAL, "reval"},
25836+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
25837+#ifdef CONFIG_AUFS_HNOTIFY
25838+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
25839+#ifdef CONFIG_AUFS_HFSNOTIFY
25840+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 25841+#endif
1facf9fc 25842+#endif
25843+ {-1, NULL}
25844+};
25845+
4a4d8108 25846+static int noinline_for_stack udba_val(char *str)
1facf9fc 25847+{
25848+ substring_t args[MAX_OPT_ARGS];
25849+
7f207e10 25850+ return match_token(str, udbalevel, args);
1facf9fc 25851+}
25852+
25853+const char *au_optstr_udba(int udba)
25854+{
076b876e 25855+ return au_parser_pattern(udba, udbalevel);
1facf9fc 25856+}
25857+
25858+/* ---------------------------------------------------------------------- */
25859+
25860+static match_table_t au_wbr_create_policy = {
25861+ {AuWbrCreate_TDP, "tdp"},
25862+ {AuWbrCreate_TDP, "top-down-parent"},
25863+ {AuWbrCreate_RR, "rr"},
25864+ {AuWbrCreate_RR, "round-robin"},
25865+ {AuWbrCreate_MFS, "mfs"},
25866+ {AuWbrCreate_MFS, "most-free-space"},
25867+ {AuWbrCreate_MFSV, "mfs:%d"},
25868+ {AuWbrCreate_MFSV, "most-free-space:%d"},
25869+
f2c43d5f
AM
25870+ /* top-down regardless the parent, and then mfs */
25871+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
25872+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
25873+
1facf9fc 25874+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
25875+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
25876+ {AuWbrCreate_PMFS, "pmfs"},
25877+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
25878+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
25879+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 25880+
25881+ {-1, NULL}
25882+};
25883+
1facf9fc 25884+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
25885+ struct au_opt_wbr_create *create)
25886+{
25887+ int err;
25888+ unsigned long long ull;
25889+
25890+ err = 0;
a2654f78 25891+ if (!match_u64(arg, &ull))
1facf9fc 25892+ create->mfsrr_watermark = ull;
25893+ else {
4a4d8108 25894+ pr_err("bad integer in %s\n", str);
1facf9fc 25895+ err = -EINVAL;
25896+ }
25897+
25898+ return err;
25899+}
25900+
25901+static int au_wbr_mfs_sec(substring_t *arg, char *str,
25902+ struct au_opt_wbr_create *create)
25903+{
25904+ int n, err;
25905+
25906+ err = 0;
027c5e7a 25907+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 25908+ create->mfs_second = n;
25909+ else {
4a4d8108 25910+ pr_err("bad integer in %s\n", str);
1facf9fc 25911+ err = -EINVAL;
25912+ }
25913+
25914+ return err;
25915+}
25916+
4a4d8108
AM
25917+static int noinline_for_stack
25918+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 25919+{
25920+ int err, e;
25921+ substring_t args[MAX_OPT_ARGS];
25922+
25923+ err = match_token(str, au_wbr_create_policy, args);
25924+ create->wbr_create = err;
25925+ switch (err) {
25926+ case AuWbrCreate_MFSRRV:
f2c43d5f 25927+ case AuWbrCreate_TDMFSV:
392086de 25928+ case AuWbrCreate_PMFSRRV:
1facf9fc 25929+ e = au_wbr_mfs_wmark(&args[0], str, create);
25930+ if (!e)
25931+ e = au_wbr_mfs_sec(&args[1], str, create);
25932+ if (unlikely(e))
25933+ err = e;
25934+ break;
25935+ case AuWbrCreate_MFSRR:
f2c43d5f 25936+ case AuWbrCreate_TDMFS:
392086de 25937+ case AuWbrCreate_PMFSRR:
1facf9fc 25938+ e = au_wbr_mfs_wmark(&args[0], str, create);
25939+ if (unlikely(e)) {
25940+ err = e;
25941+ break;
25942+ }
25943+ /*FALLTHROUGH*/
25944+ case AuWbrCreate_MFS:
25945+ case AuWbrCreate_PMFS:
027c5e7a 25946+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 25947+ break;
25948+ case AuWbrCreate_MFSV:
25949+ case AuWbrCreate_PMFSV:
25950+ e = au_wbr_mfs_sec(&args[0], str, create);
25951+ if (unlikely(e))
25952+ err = e;
25953+ break;
25954+ }
25955+
25956+ return err;
25957+}
25958+
25959+const char *au_optstr_wbr_create(int wbr_create)
25960+{
076b876e 25961+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 25962+}
25963+
25964+static match_table_t au_wbr_copyup_policy = {
25965+ {AuWbrCopyup_TDP, "tdp"},
25966+ {AuWbrCopyup_TDP, "top-down-parent"},
25967+ {AuWbrCopyup_BUP, "bup"},
25968+ {AuWbrCopyup_BUP, "bottom-up-parent"},
25969+ {AuWbrCopyup_BU, "bu"},
25970+ {AuWbrCopyup_BU, "bottom-up"},
25971+ {-1, NULL}
25972+};
25973+
4a4d8108 25974+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 25975+{
25976+ substring_t args[MAX_OPT_ARGS];
25977+
25978+ return match_token(str, au_wbr_copyup_policy, args);
25979+}
25980+
25981+const char *au_optstr_wbr_copyup(int wbr_copyup)
25982+{
076b876e 25983+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 25984+}
25985+
25986+/* ---------------------------------------------------------------------- */
25987+
25988+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
25989+
25990+static void dump_opts(struct au_opts *opts)
25991+{
25992+#ifdef CONFIG_AUFS_DEBUG
25993+ /* reduce stack space */
25994+ union {
25995+ struct au_opt_add *add;
25996+ struct au_opt_del *del;
25997+ struct au_opt_mod *mod;
25998+ struct au_opt_xino *xino;
25999+ struct au_opt_xino_itrunc *xino_itrunc;
26000+ struct au_opt_wbr_create *create;
26001+ } u;
26002+ struct au_opt *opt;
26003+
26004+ opt = opts->opt;
26005+ while (opt->type != Opt_tail) {
26006+ switch (opt->type) {
26007+ case Opt_add:
26008+ u.add = &opt->add;
26009+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26010+ u.add->bindex, u.add->pathname, u.add->perm,
26011+ u.add->path.dentry);
26012+ break;
26013+ case Opt_del:
26014+ case Opt_idel:
26015+ u.del = &opt->del;
26016+ AuDbg("del {%s, %p}\n",
26017+ u.del->pathname, u.del->h_path.dentry);
26018+ break;
26019+ case Opt_mod:
26020+ case Opt_imod:
26021+ u.mod = &opt->mod;
26022+ AuDbg("mod {%s, 0x%x, %p}\n",
26023+ u.mod->path, u.mod->perm, u.mod->h_root);
26024+ break;
26025+ case Opt_append:
26026+ u.add = &opt->add;
26027+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26028+ u.add->bindex, u.add->pathname, u.add->perm,
26029+ u.add->path.dentry);
26030+ break;
26031+ case Opt_prepend:
26032+ u.add = &opt->add;
26033+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26034+ u.add->bindex, u.add->pathname, u.add->perm,
26035+ u.add->path.dentry);
26036+ break;
26037+ case Opt_dirwh:
26038+ AuDbg("dirwh %d\n", opt->dirwh);
26039+ break;
26040+ case Opt_rdcache:
26041+ AuDbg("rdcache %d\n", opt->rdcache);
26042+ break;
26043+ case Opt_rdblk:
26044+ AuDbg("rdblk %u\n", opt->rdblk);
26045+ break;
dece6358
AM
26046+ case Opt_rdblk_def:
26047+ AuDbg("rdblk_def\n");
26048+ break;
1facf9fc 26049+ case Opt_rdhash:
26050+ AuDbg("rdhash %u\n", opt->rdhash);
26051+ break;
dece6358
AM
26052+ case Opt_rdhash_def:
26053+ AuDbg("rdhash_def\n");
26054+ break;
1facf9fc 26055+ case Opt_xino:
26056+ u.xino = &opt->xino;
523b37e3 26057+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 26058+ break;
26059+ case Opt_trunc_xino:
26060+ AuLabel(trunc_xino);
26061+ break;
26062+ case Opt_notrunc_xino:
26063+ AuLabel(notrunc_xino);
26064+ break;
26065+ case Opt_trunc_xino_path:
26066+ case Opt_itrunc_xino:
26067+ u.xino_itrunc = &opt->xino_itrunc;
26068+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26069+ break;
1facf9fc 26070+ case Opt_noxino:
26071+ AuLabel(noxino);
26072+ break;
26073+ case Opt_trunc_xib:
26074+ AuLabel(trunc_xib);
26075+ break;
26076+ case Opt_notrunc_xib:
26077+ AuLabel(notrunc_xib);
26078+ break;
dece6358
AM
26079+ case Opt_shwh:
26080+ AuLabel(shwh);
26081+ break;
26082+ case Opt_noshwh:
26083+ AuLabel(noshwh);
26084+ break;
076b876e
AM
26085+ case Opt_dirperm1:
26086+ AuLabel(dirperm1);
26087+ break;
26088+ case Opt_nodirperm1:
26089+ AuLabel(nodirperm1);
26090+ break;
1facf9fc 26091+ case Opt_plink:
26092+ AuLabel(plink);
26093+ break;
26094+ case Opt_noplink:
26095+ AuLabel(noplink);
26096+ break;
26097+ case Opt_list_plink:
26098+ AuLabel(list_plink);
26099+ break;
26100+ case Opt_udba:
26101+ AuDbg("udba %d, %s\n",
26102+ opt->udba, au_optstr_udba(opt->udba));
26103+ break;
4a4d8108
AM
26104+ case Opt_dio:
26105+ AuLabel(dio);
26106+ break;
26107+ case Opt_nodio:
26108+ AuLabel(nodio);
26109+ break;
1facf9fc 26110+ case Opt_diropq_a:
26111+ AuLabel(diropq_a);
26112+ break;
26113+ case Opt_diropq_w:
26114+ AuLabel(diropq_w);
26115+ break;
26116+ case Opt_warn_perm:
26117+ AuLabel(warn_perm);
26118+ break;
26119+ case Opt_nowarn_perm:
26120+ AuLabel(nowarn_perm);
26121+ break;
1facf9fc 26122+ case Opt_verbose:
26123+ AuLabel(verbose);
26124+ break;
26125+ case Opt_noverbose:
26126+ AuLabel(noverbose);
26127+ break;
26128+ case Opt_sum:
26129+ AuLabel(sum);
26130+ break;
26131+ case Opt_nosum:
26132+ AuLabel(nosum);
26133+ break;
26134+ case Opt_wsum:
26135+ AuLabel(wsum);
26136+ break;
26137+ case Opt_wbr_create:
26138+ u.create = &opt->wbr_create;
26139+ AuDbg("create %d, %s\n", u.create->wbr_create,
26140+ au_optstr_wbr_create(u.create->wbr_create));
26141+ switch (u.create->wbr_create) {
26142+ case AuWbrCreate_MFSV:
26143+ case AuWbrCreate_PMFSV:
26144+ AuDbg("%d sec\n", u.create->mfs_second);
26145+ break;
26146+ case AuWbrCreate_MFSRR:
f2c43d5f 26147+ case AuWbrCreate_TDMFS:
1facf9fc 26148+ AuDbg("%llu watermark\n",
26149+ u.create->mfsrr_watermark);
26150+ break;
26151+ case AuWbrCreate_MFSRRV:
f2c43d5f 26152+ case AuWbrCreate_TDMFSV:
392086de 26153+ case AuWbrCreate_PMFSRRV:
1facf9fc 26154+ AuDbg("%llu watermark, %d sec\n",
26155+ u.create->mfsrr_watermark,
26156+ u.create->mfs_second);
26157+ break;
26158+ }
26159+ break;
26160+ case Opt_wbr_copyup:
26161+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26162+ au_optstr_wbr_copyup(opt->wbr_copyup));
26163+ break;
076b876e
AM
26164+ case Opt_fhsm_sec:
26165+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26166+ break;
8b6a4947
AM
26167+ case Opt_dirren:
26168+ AuLabel(dirren);
26169+ break;
26170+ case Opt_nodirren:
26171+ AuLabel(nodirren);
26172+ break;
c1595e42
JR
26173+ case Opt_acl:
26174+ AuLabel(acl);
26175+ break;
26176+ case Opt_noacl:
26177+ AuLabel(noacl);
26178+ break;
1facf9fc 26179+ default:
26180+ BUG();
26181+ }
26182+ opt++;
26183+ }
26184+#endif
26185+}
26186+
26187+void au_opts_free(struct au_opts *opts)
26188+{
26189+ struct au_opt *opt;
26190+
26191+ opt = opts->opt;
26192+ while (opt->type != Opt_tail) {
26193+ switch (opt->type) {
26194+ case Opt_add:
26195+ case Opt_append:
26196+ case Opt_prepend:
26197+ path_put(&opt->add.path);
26198+ break;
26199+ case Opt_del:
26200+ case Opt_idel:
26201+ path_put(&opt->del.h_path);
26202+ break;
26203+ case Opt_mod:
26204+ case Opt_imod:
26205+ dput(opt->mod.h_root);
26206+ break;
26207+ case Opt_xino:
26208+ fput(opt->xino.file);
26209+ break;
26210+ }
26211+ opt++;
26212+ }
26213+}
26214+
26215+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26216+ aufs_bindex_t bindex)
26217+{
26218+ int err;
26219+ struct au_opt_add *add = &opt->add;
26220+ char *p;
26221+
26222+ add->bindex = bindex;
1e00d052 26223+ add->perm = AuBrPerm_RO;
1facf9fc 26224+ add->pathname = opt_str;
26225+ p = strchr(opt_str, '=');
26226+ if (p) {
26227+ *p++ = 0;
26228+ if (*p)
26229+ add->perm = br_perm_val(p);
26230+ }
26231+
26232+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26233+ if (!err) {
26234+ if (!p) {
26235+ add->perm = AuBrPerm_RO;
26236+ if (au_test_fs_rr(add->path.dentry->d_sb))
26237+ add->perm = AuBrPerm_RR;
2121bcd9 26238+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 26239+ add->perm = AuBrPerm_RW;
26240+ }
26241+ opt->type = Opt_add;
26242+ goto out;
26243+ }
4a4d8108 26244+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26245+ err = -EINVAL;
26246+
4f0767ce 26247+out:
1facf9fc 26248+ return err;
26249+}
26250+
26251+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26252+{
26253+ int err;
26254+
26255+ del->pathname = args[0].from;
26256+ AuDbg("del path %s\n", del->pathname);
26257+
26258+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26259+ if (unlikely(err))
4a4d8108 26260+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26261+
26262+ return err;
26263+}
26264+
26265+#if 0 /* reserved for future use */
26266+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26267+ struct au_opt_del *del, substring_t args[])
26268+{
26269+ int err;
26270+ struct dentry *root;
26271+
26272+ err = -EINVAL;
26273+ root = sb->s_root;
26274+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26275+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26276+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26277+ goto out;
26278+ }
26279+
26280+ err = 0;
26281+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26282+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26283+
4f0767ce 26284+out:
1facf9fc 26285+ aufs_read_unlock(root, !AuLock_IR);
26286+ return err;
26287+}
26288+#endif
26289+
4a4d8108
AM
26290+static int noinline_for_stack
26291+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26292+{
26293+ int err;
26294+ struct path path;
26295+ char *p;
26296+
26297+ err = -EINVAL;
26298+ mod->path = args[0].from;
26299+ p = strchr(mod->path, '=');
26300+ if (unlikely(!p)) {
4a4d8108 26301+ pr_err("no permssion %s\n", args[0].from);
1facf9fc 26302+ goto out;
26303+ }
26304+
26305+ *p++ = 0;
26306+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26307+ if (unlikely(err)) {
4a4d8108 26308+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26309+ goto out;
26310+ }
26311+
26312+ mod->perm = br_perm_val(p);
26313+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26314+ mod->h_root = dget(path.dentry);
26315+ path_put(&path);
26316+
4f0767ce 26317+out:
1facf9fc 26318+ return err;
26319+}
26320+
26321+#if 0 /* reserved for future use */
26322+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26323+ struct au_opt_mod *mod, substring_t args[])
26324+{
26325+ int err;
26326+ struct dentry *root;
26327+
26328+ err = -EINVAL;
26329+ root = sb->s_root;
26330+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26331+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26332+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26333+ goto out;
26334+ }
26335+
26336+ err = 0;
26337+ mod->perm = br_perm_val(args[1].from);
26338+ AuDbg("mod path %s, perm 0x%x, %s\n",
26339+ mod->path, mod->perm, args[1].from);
26340+ mod->h_root = dget(au_h_dptr(root, bindex));
26341+
4f0767ce 26342+out:
1facf9fc 26343+ aufs_read_unlock(root, !AuLock_IR);
26344+ return err;
26345+}
26346+#endif
26347+
26348+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26349+ substring_t args[])
26350+{
26351+ int err;
26352+ struct file *file;
26353+
26354+ file = au_xino_create(sb, args[0].from, /*silent*/0);
26355+ err = PTR_ERR(file);
26356+ if (IS_ERR(file))
26357+ goto out;
26358+
26359+ err = -EINVAL;
2000de60 26360+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26361+ fput(file);
4a4d8108 26362+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26363+ goto out;
26364+ }
26365+
26366+ err = 0;
26367+ xino->file = file;
26368+ xino->path = args[0].from;
26369+
4f0767ce 26370+out:
1facf9fc 26371+ return err;
26372+}
26373+
4a4d8108
AM
26374+static int noinline_for_stack
26375+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26376+ struct au_opt_xino_itrunc *xino_itrunc,
26377+ substring_t args[])
1facf9fc 26378+{
26379+ int err;
5afbbe0d 26380+ aufs_bindex_t bbot, bindex;
1facf9fc 26381+ struct path path;
26382+ struct dentry *root;
26383+
26384+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26385+ if (unlikely(err)) {
4a4d8108 26386+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26387+ goto out;
26388+ }
26389+
26390+ xino_itrunc->bindex = -1;
26391+ root = sb->s_root;
26392+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26393+ bbot = au_sbbot(sb);
26394+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26395+ if (au_h_dptr(root, bindex) == path.dentry) {
26396+ xino_itrunc->bindex = bindex;
26397+ break;
26398+ }
26399+ }
26400+ aufs_read_unlock(root, !AuLock_IR);
26401+ path_put(&path);
26402+
26403+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26404+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26405+ err = -EINVAL;
26406+ }
26407+
4f0767ce 26408+out:
1facf9fc 26409+ return err;
26410+}
26411+
26412+/* called without aufs lock */
26413+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26414+{
26415+ int err, n, token;
26416+ aufs_bindex_t bindex;
26417+ unsigned char skipped;
26418+ struct dentry *root;
26419+ struct au_opt *opt, *opt_tail;
26420+ char *opt_str;
26421+ /* reduce the stack space */
26422+ union {
26423+ struct au_opt_xino_itrunc *xino_itrunc;
26424+ struct au_opt_wbr_create *create;
26425+ } u;
26426+ struct {
26427+ substring_t args[MAX_OPT_ARGS];
26428+ } *a;
26429+
26430+ err = -ENOMEM;
26431+ a = kmalloc(sizeof(*a), GFP_NOFS);
26432+ if (unlikely(!a))
26433+ goto out;
26434+
26435+ root = sb->s_root;
26436+ err = 0;
26437+ bindex = 0;
26438+ opt = opts->opt;
26439+ opt_tail = opt + opts->max_opt - 1;
26440+ opt->type = Opt_tail;
26441+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26442+ err = -EINVAL;
26443+ skipped = 0;
26444+ token = match_token(opt_str, options, a->args);
26445+ switch (token) {
26446+ case Opt_br:
26447+ err = 0;
26448+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26449+ && *opt_str) {
26450+ err = opt_add(opt, opt_str, opts->sb_flags,
26451+ bindex++);
26452+ if (unlikely(!err && ++opt > opt_tail)) {
26453+ err = -E2BIG;
26454+ break;
26455+ }
26456+ opt->type = Opt_tail;
26457+ skipped = 1;
26458+ }
26459+ break;
26460+ case Opt_add:
26461+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26462+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26463+ break;
26464+ }
26465+ bindex = n;
26466+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26467+ bindex);
26468+ if (!err)
26469+ opt->type = token;
26470+ break;
26471+ case Opt_append:
26472+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26473+ /*dummy bindex*/1);
26474+ if (!err)
26475+ opt->type = token;
26476+ break;
26477+ case Opt_prepend:
26478+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26479+ /*bindex*/0);
26480+ if (!err)
26481+ opt->type = token;
26482+ break;
26483+ case Opt_del:
26484+ err = au_opts_parse_del(&opt->del, a->args);
26485+ if (!err)
26486+ opt->type = token;
26487+ break;
26488+#if 0 /* reserved for future use */
26489+ case Opt_idel:
26490+ del->pathname = "(indexed)";
26491+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26492+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26493+ break;
26494+ }
26495+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26496+ if (!err)
26497+ opt->type = token;
26498+ break;
26499+#endif
26500+ case Opt_mod:
26501+ err = au_opts_parse_mod(&opt->mod, a->args);
26502+ if (!err)
26503+ opt->type = token;
26504+ break;
26505+#ifdef IMOD /* reserved for future use */
26506+ case Opt_imod:
26507+ u.mod->path = "(indexed)";
26508+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26509+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26510+ break;
26511+ }
26512+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26513+ if (!err)
26514+ opt->type = token;
26515+ break;
26516+#endif
26517+ case Opt_xino:
26518+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26519+ if (!err)
26520+ opt->type = token;
26521+ break;
26522+
26523+ case Opt_trunc_xino_path:
26524+ err = au_opts_parse_xino_itrunc_path
26525+ (sb, &opt->xino_itrunc, a->args);
26526+ if (!err)
26527+ opt->type = token;
26528+ break;
26529+
26530+ case Opt_itrunc_xino:
26531+ u.xino_itrunc = &opt->xino_itrunc;
26532+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26533+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26534+ break;
26535+ }
26536+ u.xino_itrunc->bindex = n;
26537+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26538+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26539+ pr_err("out of bounds, %d\n", n);
1facf9fc 26540+ aufs_read_unlock(root, !AuLock_IR);
26541+ break;
26542+ }
26543+ aufs_read_unlock(root, !AuLock_IR);
26544+ err = 0;
26545+ opt->type = token;
26546+ break;
26547+
26548+ case Opt_dirwh:
26549+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26550+ break;
26551+ err = 0;
26552+ opt->type = token;
26553+ break;
26554+
26555+ case Opt_rdcache:
027c5e7a
AM
26556+ if (unlikely(match_int(&a->args[0], &n))) {
26557+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26558+ break;
027c5e7a
AM
26559+ }
26560+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26561+ pr_err("rdcache must be smaller than %d\n",
26562+ AUFS_RDCACHE_MAX);
26563+ break;
26564+ }
26565+ opt->rdcache = n;
1facf9fc 26566+ err = 0;
26567+ opt->type = token;
26568+ break;
26569+ case Opt_rdblk:
26570+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26571+ || n < 0
1facf9fc 26572+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26573+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26574+ break;
26575+ }
1308ab2a 26576+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26577+ pr_err("rdblk must be larger than %d\n",
26578+ NAME_MAX);
1facf9fc 26579+ break;
26580+ }
26581+ opt->rdblk = n;
26582+ err = 0;
26583+ opt->type = token;
26584+ break;
26585+ case Opt_rdhash:
26586+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26587+ || n < 0
1facf9fc 26588+ || n * sizeof(struct hlist_head)
26589+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26590+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26591+ break;
26592+ }
26593+ opt->rdhash = n;
26594+ err = 0;
26595+ opt->type = token;
26596+ break;
26597+
26598+ case Opt_trunc_xino:
26599+ case Opt_notrunc_xino:
26600+ case Opt_noxino:
26601+ case Opt_trunc_xib:
26602+ case Opt_notrunc_xib:
dece6358
AM
26603+ case Opt_shwh:
26604+ case Opt_noshwh:
076b876e
AM
26605+ case Opt_dirperm1:
26606+ case Opt_nodirperm1:
1facf9fc 26607+ case Opt_plink:
26608+ case Opt_noplink:
26609+ case Opt_list_plink:
4a4d8108
AM
26610+ case Opt_dio:
26611+ case Opt_nodio:
1facf9fc 26612+ case Opt_diropq_a:
26613+ case Opt_diropq_w:
26614+ case Opt_warn_perm:
26615+ case Opt_nowarn_perm:
1facf9fc 26616+ case Opt_verbose:
26617+ case Opt_noverbose:
26618+ case Opt_sum:
26619+ case Opt_nosum:
26620+ case Opt_wsum:
dece6358
AM
26621+ case Opt_rdblk_def:
26622+ case Opt_rdhash_def:
8b6a4947
AM
26623+ case Opt_dirren:
26624+ case Opt_nodirren:
c1595e42
JR
26625+ case Opt_acl:
26626+ case Opt_noacl:
1facf9fc 26627+ err = 0;
26628+ opt->type = token;
26629+ break;
26630+
26631+ case Opt_udba:
26632+ opt->udba = udba_val(a->args[0].from);
26633+ if (opt->udba >= 0) {
26634+ err = 0;
26635+ opt->type = token;
26636+ } else
4a4d8108 26637+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26638+ break;
26639+
26640+ case Opt_wbr_create:
26641+ u.create = &opt->wbr_create;
26642+ u.create->wbr_create
26643+ = au_wbr_create_val(a->args[0].from, u.create);
26644+ if (u.create->wbr_create >= 0) {
26645+ err = 0;
26646+ opt->type = token;
26647+ } else
4a4d8108 26648+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26649+ break;
26650+ case Opt_wbr_copyup:
26651+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26652+ if (opt->wbr_copyup >= 0) {
26653+ err = 0;
26654+ opt->type = token;
26655+ } else
4a4d8108 26656+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26657+ break;
26658+
076b876e
AM
26659+ case Opt_fhsm_sec:
26660+ if (unlikely(match_int(&a->args[0], &n)
26661+ || n < 0)) {
26662+ pr_err("bad integer in %s\n", opt_str);
26663+ break;
26664+ }
26665+ if (sysaufs_brs) {
26666+ opt->fhsm_second = n;
26667+ opt->type = token;
26668+ } else
26669+ pr_warn("ignored %s\n", opt_str);
26670+ err = 0;
26671+ break;
26672+
1facf9fc 26673+ case Opt_ignore:
0c3ec466 26674+ pr_warn("ignored %s\n", opt_str);
1facf9fc 26675+ /*FALLTHROUGH*/
26676+ case Opt_ignore_silent:
26677+ skipped = 1;
26678+ err = 0;
26679+ break;
26680+ case Opt_err:
4a4d8108 26681+ pr_err("unknown option %s\n", opt_str);
1facf9fc 26682+ break;
26683+ }
26684+
26685+ if (!err && !skipped) {
26686+ if (unlikely(++opt > opt_tail)) {
26687+ err = -E2BIG;
26688+ opt--;
26689+ opt->type = Opt_tail;
26690+ break;
26691+ }
26692+ opt->type = Opt_tail;
26693+ }
26694+ }
26695+
1c60b727 26696+ kfree(a);
1facf9fc 26697+ dump_opts(opts);
26698+ if (unlikely(err))
26699+ au_opts_free(opts);
26700+
4f0767ce 26701+out:
1facf9fc 26702+ return err;
26703+}
26704+
26705+static int au_opt_wbr_create(struct super_block *sb,
26706+ struct au_opt_wbr_create *create)
26707+{
26708+ int err;
26709+ struct au_sbinfo *sbinfo;
26710+
dece6358
AM
26711+ SiMustWriteLock(sb);
26712+
1facf9fc 26713+ err = 1; /* handled */
26714+ sbinfo = au_sbi(sb);
26715+ if (sbinfo->si_wbr_create_ops->fin) {
26716+ err = sbinfo->si_wbr_create_ops->fin(sb);
26717+ if (!err)
26718+ err = 1;
26719+ }
26720+
26721+ sbinfo->si_wbr_create = create->wbr_create;
26722+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26723+ switch (create->wbr_create) {
26724+ case AuWbrCreate_MFSRRV:
26725+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
26726+ case AuWbrCreate_TDMFS:
26727+ case AuWbrCreate_TDMFSV:
392086de
AM
26728+ case AuWbrCreate_PMFSRR:
26729+ case AuWbrCreate_PMFSRRV:
1facf9fc 26730+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26731+ /*FALLTHROUGH*/
26732+ case AuWbrCreate_MFS:
26733+ case AuWbrCreate_MFSV:
26734+ case AuWbrCreate_PMFS:
26735+ case AuWbrCreate_PMFSV:
e49829fe
JR
26736+ sbinfo->si_wbr_mfs.mfs_expire
26737+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 26738+ break;
26739+ }
26740+
26741+ if (sbinfo->si_wbr_create_ops->init)
26742+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26743+
26744+ return err;
26745+}
26746+
26747+/*
26748+ * returns,
26749+ * plus: processed without an error
26750+ * zero: unprocessed
26751+ */
26752+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26753+ struct au_opts *opts)
26754+{
26755+ int err;
26756+ struct au_sbinfo *sbinfo;
26757+
dece6358
AM
26758+ SiMustWriteLock(sb);
26759+
1facf9fc 26760+ err = 1; /* handled */
26761+ sbinfo = au_sbi(sb);
26762+ switch (opt->type) {
26763+ case Opt_udba:
26764+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26765+ sbinfo->si_mntflags |= opt->udba;
26766+ opts->given_udba |= opt->udba;
26767+ break;
26768+
26769+ case Opt_plink:
26770+ au_opt_set(sbinfo->si_mntflags, PLINK);
26771+ break;
26772+ case Opt_noplink:
26773+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 26774+ au_plink_put(sb, /*verbose*/1);
1facf9fc 26775+ au_opt_clr(sbinfo->si_mntflags, PLINK);
26776+ break;
26777+ case Opt_list_plink:
26778+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26779+ au_plink_list(sb);
26780+ break;
26781+
4a4d8108
AM
26782+ case Opt_dio:
26783+ au_opt_set(sbinfo->si_mntflags, DIO);
26784+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26785+ break;
26786+ case Opt_nodio:
26787+ au_opt_clr(sbinfo->si_mntflags, DIO);
26788+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26789+ break;
26790+
076b876e
AM
26791+ case Opt_fhsm_sec:
26792+ au_fhsm_set(sbinfo, opt->fhsm_second);
26793+ break;
26794+
1facf9fc 26795+ case Opt_diropq_a:
26796+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26797+ break;
26798+ case Opt_diropq_w:
26799+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26800+ break;
26801+
26802+ case Opt_warn_perm:
26803+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26804+ break;
26805+ case Opt_nowarn_perm:
26806+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26807+ break;
26808+
1facf9fc 26809+ case Opt_verbose:
26810+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
26811+ break;
26812+ case Opt_noverbose:
26813+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26814+ break;
26815+
26816+ case Opt_sum:
26817+ au_opt_set(sbinfo->si_mntflags, SUM);
26818+ break;
26819+ case Opt_wsum:
26820+ au_opt_clr(sbinfo->si_mntflags, SUM);
26821+ au_opt_set(sbinfo->si_mntflags, SUM_W);
26822+ case Opt_nosum:
26823+ au_opt_clr(sbinfo->si_mntflags, SUM);
26824+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
26825+ break;
26826+
26827+ case Opt_wbr_create:
26828+ err = au_opt_wbr_create(sb, &opt->wbr_create);
26829+ break;
26830+ case Opt_wbr_copyup:
26831+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
26832+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26833+ break;
26834+
26835+ case Opt_dirwh:
26836+ sbinfo->si_dirwh = opt->dirwh;
26837+ break;
26838+
26839+ case Opt_rdcache:
e49829fe
JR
26840+ sbinfo->si_rdcache
26841+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 26842+ break;
26843+ case Opt_rdblk:
26844+ sbinfo->si_rdblk = opt->rdblk;
26845+ break;
dece6358
AM
26846+ case Opt_rdblk_def:
26847+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26848+ break;
1facf9fc 26849+ case Opt_rdhash:
26850+ sbinfo->si_rdhash = opt->rdhash;
26851+ break;
dece6358
AM
26852+ case Opt_rdhash_def:
26853+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26854+ break;
26855+
26856+ case Opt_shwh:
26857+ au_opt_set(sbinfo->si_mntflags, SHWH);
26858+ break;
26859+ case Opt_noshwh:
26860+ au_opt_clr(sbinfo->si_mntflags, SHWH);
26861+ break;
1facf9fc 26862+
076b876e
AM
26863+ case Opt_dirperm1:
26864+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
26865+ break;
26866+ case Opt_nodirperm1:
26867+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
26868+ break;
26869+
1facf9fc 26870+ case Opt_trunc_xino:
26871+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
26872+ break;
26873+ case Opt_notrunc_xino:
26874+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
26875+ break;
26876+
26877+ case Opt_trunc_xino_path:
26878+ case Opt_itrunc_xino:
26879+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
26880+ if (!err)
26881+ err = 1;
26882+ break;
26883+
26884+ case Opt_trunc_xib:
26885+ au_fset_opts(opts->flags, TRUNC_XIB);
26886+ break;
26887+ case Opt_notrunc_xib:
26888+ au_fclr_opts(opts->flags, TRUNC_XIB);
26889+ break;
26890+
8b6a4947
AM
26891+ case Opt_dirren:
26892+ err = 1;
26893+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26894+ err = au_dr_opt_set(sb);
26895+ if (!err)
26896+ err = 1;
26897+ }
26898+ if (err == 1)
26899+ au_opt_set(sbinfo->si_mntflags, DIRREN);
26900+ break;
26901+ case Opt_nodirren:
26902+ err = 1;
26903+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26904+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
26905+ DR_FLUSHED));
26906+ if (!err)
26907+ err = 1;
26908+ }
26909+ if (err == 1)
26910+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
26911+ break;
26912+
c1595e42 26913+ case Opt_acl:
2121bcd9 26914+ sb->s_flags |= SB_POSIXACL;
c1595e42
JR
26915+ break;
26916+ case Opt_noacl:
2121bcd9 26917+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
26918+ break;
26919+
1facf9fc 26920+ default:
26921+ err = 0;
26922+ break;
26923+ }
26924+
26925+ return err;
26926+}
26927+
26928+/*
26929+ * returns tri-state.
26930+ * plus: processed without an error
26931+ * zero: unprocessed
26932+ * minus: error
26933+ */
26934+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
26935+ struct au_opts *opts)
26936+{
26937+ int err, do_refresh;
26938+
26939+ err = 0;
26940+ switch (opt->type) {
26941+ case Opt_append:
5afbbe0d 26942+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 26943+ if (opt->add.bindex < 0)
26944+ opt->add.bindex = 0;
26945+ goto add;
26946+ case Opt_prepend:
26947+ opt->add.bindex = 0;
f6b6e03d 26948+ add: /* indented label */
1facf9fc 26949+ case Opt_add:
26950+ err = au_br_add(sb, &opt->add,
26951+ au_ftest_opts(opts->flags, REMOUNT));
26952+ if (!err) {
26953+ err = 1;
027c5e7a 26954+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26955+ }
26956+ break;
26957+
26958+ case Opt_del:
26959+ case Opt_idel:
26960+ err = au_br_del(sb, &opt->del,
26961+ au_ftest_opts(opts->flags, REMOUNT));
26962+ if (!err) {
26963+ err = 1;
26964+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 26965+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26966+ }
26967+ break;
26968+
26969+ case Opt_mod:
26970+ case Opt_imod:
26971+ err = au_br_mod(sb, &opt->mod,
26972+ au_ftest_opts(opts->flags, REMOUNT),
26973+ &do_refresh);
26974+ if (!err) {
26975+ err = 1;
027c5e7a
AM
26976+ if (do_refresh)
26977+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 26978+ }
26979+ break;
26980+ }
1facf9fc 26981+ return err;
26982+}
26983+
26984+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
26985+ struct au_opt_xino **opt_xino,
26986+ struct au_opts *opts)
26987+{
26988+ int err;
5afbbe0d 26989+ aufs_bindex_t bbot, bindex;
1facf9fc 26990+ struct dentry *root, *parent, *h_root;
26991+
26992+ err = 0;
26993+ switch (opt->type) {
26994+ case Opt_xino:
1facf9fc 26995+ au_xino_brid_set(sb, -1);
1facf9fc 26996+ /* safe d_parent access */
2000de60 26997+ parent = opt->xino.file->f_path.dentry->d_parent;
1facf9fc 26998+ root = sb->s_root;
5afbbe0d
AM
26999+ bbot = au_sbbot(sb);
27000+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 27001+ h_root = au_h_dptr(root, bindex);
27002+ if (h_root == parent) {
27003+ au_xino_brid_set(sb, au_sbr_id(sb, bindex));
27004+ break;
27005+ }
27006+ }
062440b3
AM
27007+
27008+ err = au_xino_set(sb, &opt->xino,
27009+ !!au_ftest_opts(opts->flags, REMOUNT));
27010+ if (unlikely(err))
27011+ break;
27012+
27013+ *opt_xino = &opt->xino;
1facf9fc 27014+ break;
27015+
27016+ case Opt_noxino:
27017+ au_xino_clr(sb);
1facf9fc 27018+ *opt_xino = (void *)-1;
27019+ break;
27020+ }
27021+
27022+ return err;
27023+}
27024+
27025+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27026+ unsigned int pending)
27027+{
076b876e 27028+ int err, fhsm;
5afbbe0d 27029+ aufs_bindex_t bindex, bbot;
79b8bda9 27030+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 27031+ struct au_branch *br;
27032+ struct au_wbr *wbr;
79b8bda9 27033+ struct dentry *root, *dentry;
1facf9fc 27034+ struct inode *dir, *h_dir;
27035+ struct au_sbinfo *sbinfo;
27036+ struct au_hinode *hdir;
27037+
dece6358
AM
27038+ SiMustAnyLock(sb);
27039+
1facf9fc 27040+ sbinfo = au_sbi(sb);
27041+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27042+
2121bcd9 27043+ if (!(sb_flags & SB_RDONLY)) {
dece6358 27044+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 27045+ pr_warn("first branch should be rw\n");
dece6358 27046+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 27047+ pr_warn_once("shwh should be used with ro\n");
dece6358 27048+ }
1facf9fc 27049+
4a4d8108 27050+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 27051+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 27052+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 27053+
076b876e 27054+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
27055+ pr_warn_once("dirperm1 breaks the protection"
27056+ " by the permission bits on the lower branch\n");
076b876e 27057+
1facf9fc 27058+ err = 0;
076b876e 27059+ fhsm = 0;
1facf9fc 27060+ root = sb->s_root;
5527c038 27061+ dir = d_inode(root);
1facf9fc 27062+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
27063+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27064+ UDBA_NONE);
5afbbe0d
AM
27065+ bbot = au_sbbot(sb);
27066+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 27067+ skip = 0;
27068+ h_dir = au_h_iptr(dir, bindex);
27069+ br = au_sbr(sb, bindex);
1facf9fc 27070+
c1595e42
JR
27071+ if ((br->br_perm & AuBrAttr_ICEX)
27072+ && !h_dir->i_op->listxattr)
27073+ br->br_perm &= ~AuBrAttr_ICEX;
27074+#if 0
27075+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 27076+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
27077+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27078+#endif
27079+
27080+ do_free = 0;
1facf9fc 27081+ wbr = br->br_wbr;
27082+ if (wbr)
27083+ wbr_wh_read_lock(wbr);
27084+
1e00d052 27085+ if (!au_br_writable(br->br_perm)) {
1facf9fc 27086+ do_free = !!wbr;
27087+ skip = (!wbr
27088+ || (!wbr->wbr_whbase
27089+ && !wbr->wbr_plink
27090+ && !wbr->wbr_orph));
1e00d052 27091+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 27092+ /* skip = (!br->br_whbase && !br->br_orph); */
27093+ skip = (!wbr || !wbr->wbr_whbase);
27094+ if (skip && wbr) {
27095+ if (do_plink)
27096+ skip = !!wbr->wbr_plink;
27097+ else
27098+ skip = !wbr->wbr_plink;
27099+ }
1e00d052 27100+ } else {
1facf9fc 27101+ /* skip = (br->br_whbase && br->br_ohph); */
27102+ skip = (wbr && wbr->wbr_whbase);
27103+ if (skip) {
27104+ if (do_plink)
27105+ skip = !!wbr->wbr_plink;
27106+ else
27107+ skip = !wbr->wbr_plink;
27108+ }
1facf9fc 27109+ }
27110+ if (wbr)
27111+ wbr_wh_read_unlock(wbr);
27112+
79b8bda9
AM
27113+ if (can_no_dreval) {
27114+ dentry = br->br_path.dentry;
27115+ spin_lock(&dentry->d_lock);
27116+ if (dentry->d_flags &
27117+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27118+ can_no_dreval = 0;
27119+ spin_unlock(&dentry->d_lock);
27120+ }
27121+
076b876e
AM
27122+ if (au_br_fhsm(br->br_perm)) {
27123+ fhsm++;
27124+ AuDebugOn(!br->br_fhsm);
27125+ }
27126+
1facf9fc 27127+ if (skip)
27128+ continue;
27129+
27130+ hdir = au_hi(dir, bindex);
5afbbe0d 27131+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27132+ if (wbr)
27133+ wbr_wh_write_lock(wbr);
86dc4139 27134+ err = au_wh_init(br, sb);
1facf9fc 27135+ if (wbr)
27136+ wbr_wh_write_unlock(wbr);
5afbbe0d 27137+ au_hn_inode_unlock(hdir);
1facf9fc 27138+
27139+ if (!err && do_free) {
1c60b727 27140+ kfree(wbr);
1facf9fc 27141+ br->br_wbr = NULL;
27142+ }
27143+ }
27144+
79b8bda9
AM
27145+ if (can_no_dreval)
27146+ au_fset_si(sbinfo, NO_DREVAL);
27147+ else
27148+ au_fclr_si(sbinfo, NO_DREVAL);
27149+
c1595e42 27150+ if (fhsm >= 2) {
076b876e 27151+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27152+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27153+ br = au_sbr(sb, bindex);
27154+ if (au_br_fhsm(br->br_perm)) {
27155+ au_fhsm_set_bottom(sb, bindex);
27156+ break;
27157+ }
27158+ }
27159+ } else {
076b876e 27160+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27161+ au_fhsm_set_bottom(sb, -1);
27162+ }
076b876e 27163+
1facf9fc 27164+ return err;
27165+}
27166+
27167+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27168+{
27169+ int err;
27170+ unsigned int tmp;
5afbbe0d 27171+ aufs_bindex_t bindex, bbot;
1facf9fc 27172+ struct au_opt *opt;
27173+ struct au_opt_xino *opt_xino, xino;
27174+ struct au_sbinfo *sbinfo;
027c5e7a 27175+ struct au_branch *br;
076b876e 27176+ struct inode *dir;
1facf9fc 27177+
dece6358
AM
27178+ SiMustWriteLock(sb);
27179+
1facf9fc 27180+ err = 0;
27181+ opt_xino = NULL;
27182+ opt = opts->opt;
27183+ while (err >= 0 && opt->type != Opt_tail)
27184+ err = au_opt_simple(sb, opt++, opts);
27185+ if (err > 0)
27186+ err = 0;
27187+ else if (unlikely(err < 0))
27188+ goto out;
27189+
27190+ /* disable xino and udba temporary */
27191+ sbinfo = au_sbi(sb);
27192+ tmp = sbinfo->si_mntflags;
27193+ au_opt_clr(sbinfo->si_mntflags, XINO);
27194+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27195+
27196+ opt = opts->opt;
27197+ while (err >= 0 && opt->type != Opt_tail)
27198+ err = au_opt_br(sb, opt++, opts);
27199+ if (err > 0)
27200+ err = 0;
27201+ else if (unlikely(err < 0))
27202+ goto out;
27203+
5afbbe0d
AM
27204+ bbot = au_sbbot(sb);
27205+ if (unlikely(bbot < 0)) {
1facf9fc 27206+ err = -EINVAL;
4a4d8108 27207+ pr_err("no branches\n");
1facf9fc 27208+ goto out;
27209+ }
27210+
27211+ if (au_opt_test(tmp, XINO))
27212+ au_opt_set(sbinfo->si_mntflags, XINO);
27213+ opt = opts->opt;
27214+ while (!err && opt->type != Opt_tail)
27215+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27216+ if (unlikely(err))
27217+ goto out;
27218+
27219+ err = au_opts_verify(sb, sb->s_flags, tmp);
27220+ if (unlikely(err))
27221+ goto out;
27222+
27223+ /* restore xino */
27224+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27225+ xino.file = au_xino_def(sb);
27226+ err = PTR_ERR(xino.file);
27227+ if (IS_ERR(xino.file))
27228+ goto out;
27229+
27230+ err = au_xino_set(sb, &xino, /*remount*/0);
27231+ fput(xino.file);
27232+ if (unlikely(err))
27233+ goto out;
27234+ }
27235+
27236+ /* restore udba */
027c5e7a 27237+ tmp &= AuOptMask_UDBA;
1facf9fc 27238+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27239+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27240+ bbot = au_sbbot(sb);
27241+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27242+ br = au_sbr(sb, bindex);
27243+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27244+ if (unlikely(err))
27245+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27246+ bindex, err);
27247+ /* go on even if err */
27248+ }
4a4d8108 27249+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27250+ dir = d_inode(sb->s_root);
4a4d8108 27251+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27252+ }
27253+
4f0767ce 27254+out:
1facf9fc 27255+ return err;
27256+}
27257+
27258+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27259+{
27260+ int err, rerr;
79b8bda9 27261+ unsigned char no_dreval;
1facf9fc 27262+ struct inode *dir;
27263+ struct au_opt_xino *opt_xino;
27264+ struct au_opt *opt;
27265+ struct au_sbinfo *sbinfo;
27266+
dece6358
AM
27267+ SiMustWriteLock(sb);
27268+
8b6a4947
AM
27269+ err = au_dr_opt_flush(sb);
27270+ if (unlikely(err))
27271+ goto out;
27272+ au_fset_opts(opts->flags, DR_FLUSHED);
27273+
5527c038 27274+ dir = d_inode(sb->s_root);
1facf9fc 27275+ sbinfo = au_sbi(sb);
1facf9fc 27276+ opt_xino = NULL;
27277+ opt = opts->opt;
27278+ while (err >= 0 && opt->type != Opt_tail) {
27279+ err = au_opt_simple(sb, opt, opts);
27280+ if (!err)
27281+ err = au_opt_br(sb, opt, opts);
27282+ if (!err)
27283+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27284+ opt++;
27285+ }
27286+ if (err > 0)
27287+ err = 0;
27288+ AuTraceErr(err);
27289+ /* go on even err */
27290+
79b8bda9 27291+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27292+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27293+ if (unlikely(rerr && !err))
27294+ err = rerr;
27295+
79b8bda9 27296+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27297+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27298+
1facf9fc 27299+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27300+ rerr = au_xib_trunc(sb);
27301+ if (unlikely(rerr && !err))
27302+ err = rerr;
27303+ }
27304+
27305+ /* will be handled by the caller */
027c5e7a 27306+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27307+ && (opts->given_udba
27308+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27309+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27310+ ))
027c5e7a 27311+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27312+
27313+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27314+
27315+out:
1facf9fc 27316+ return err;
27317+}
27318+
27319+/* ---------------------------------------------------------------------- */
27320+
27321+unsigned int au_opt_udba(struct super_block *sb)
27322+{
27323+ return au_mntflags(sb) & AuOptMask_UDBA;
27324+}
7f207e10
AM
27325diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27326--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
27327+++ linux/fs/aufs/opts.h 2018-07-19 09:46:13.063314675 +0200
27328@@ -0,0 +1,225 @@
27329+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27330+/*
b00004a5 27331+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 27332+ *
27333+ * This program, aufs is free software; you can redistribute it and/or modify
27334+ * it under the terms of the GNU General Public License as published by
27335+ * the Free Software Foundation; either version 2 of the License, or
27336+ * (at your option) any later version.
dece6358
AM
27337+ *
27338+ * This program is distributed in the hope that it will be useful,
27339+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27340+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27341+ * GNU General Public License for more details.
27342+ *
27343+ * You should have received a copy of the GNU General Public License
523b37e3 27344+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27345+ */
27346+
27347+/*
27348+ * mount options/flags
27349+ */
27350+
27351+#ifndef __AUFS_OPTS_H__
27352+#define __AUFS_OPTS_H__
27353+
27354+#ifdef __KERNEL__
27355+
dece6358 27356+#include <linux/path.h>
1facf9fc 27357+
dece6358 27358+struct file;
dece6358 27359+
1facf9fc 27360+/* ---------------------------------------------------------------------- */
27361+
27362+/* mount flags */
27363+#define AuOpt_XINO 1 /* external inode number bitmap
27364+ and translation table */
27365+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27366+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27367+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27368+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27369+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27370+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27371+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27372+ bits */
dece6358
AM
27373+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27374+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27375+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27376+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27377+#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
4a4d8108 27378+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27379+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27380+
4a4d8108
AM
27381+#ifndef CONFIG_AUFS_HNOTIFY
27382+#undef AuOpt_UDBA_HNOTIFY
27383+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27384+#endif
8b6a4947
AM
27385+#ifndef CONFIG_AUFS_DIRREN
27386+#undef AuOpt_DIRREN
27387+#define AuOpt_DIRREN 0
27388+#endif
dece6358
AM
27389+#ifndef CONFIG_AUFS_SHWH
27390+#undef AuOpt_SHWH
27391+#define AuOpt_SHWH 0
27392+#endif
1facf9fc 27393+
27394+#define AuOpt_Def (AuOpt_XINO \
27395+ | AuOpt_UDBA_REVAL \
27396+ | AuOpt_PLINK \
27397+ /* | AuOpt_DIRPERM1 */ \
27398+ | AuOpt_WARN_PERM)
27399+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27400+ | AuOpt_UDBA_REVAL \
4a4d8108 27401+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27402+
27403+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27404+#define au_opt_set(flags, name) do { \
27405+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27406+ ((flags) |= AuOpt_##name); \
27407+} while (0)
27408+#define au_opt_set_udba(flags, name) do { \
27409+ (flags) &= ~AuOptMask_UDBA; \
27410+ ((flags) |= AuOpt_##name); \
27411+} while (0)
7f207e10
AM
27412+#define au_opt_clr(flags, name) do { \
27413+ ((flags) &= ~AuOpt_##name); \
27414+} while (0)
1facf9fc 27415+
e49829fe
JR
27416+static inline unsigned int au_opts_plink(unsigned int mntflags)
27417+{
27418+#ifdef CONFIG_PROC_FS
27419+ return mntflags;
27420+#else
27421+ return mntflags & ~AuOpt_PLINK;
27422+#endif
27423+}
27424+
1facf9fc 27425+/* ---------------------------------------------------------------------- */
27426+
27427+/* policies to select one among multiple writable branches */
27428+enum {
27429+ AuWbrCreate_TDP, /* top down parent */
27430+ AuWbrCreate_RR, /* round robin */
27431+ AuWbrCreate_MFS, /* most free space */
27432+ AuWbrCreate_MFSV, /* mfs with seconds */
27433+ AuWbrCreate_MFSRR, /* mfs then rr */
27434+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27435+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27436+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27437+ AuWbrCreate_PMFS, /* parent and mfs */
27438+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27439+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27440+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27441+
27442+ AuWbrCreate_Def = AuWbrCreate_TDP
27443+};
27444+
27445+enum {
27446+ AuWbrCopyup_TDP, /* top down parent */
27447+ AuWbrCopyup_BUP, /* bottom up parent */
27448+ AuWbrCopyup_BU, /* bottom up */
27449+
27450+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27451+};
27452+
27453+/* ---------------------------------------------------------------------- */
27454+
27455+struct au_opt_add {
27456+ aufs_bindex_t bindex;
27457+ char *pathname;
27458+ int perm;
27459+ struct path path;
27460+};
27461+
27462+struct au_opt_del {
27463+ char *pathname;
27464+ struct path h_path;
27465+};
27466+
27467+struct au_opt_mod {
27468+ char *path;
27469+ int perm;
27470+ struct dentry *h_root;
27471+};
27472+
27473+struct au_opt_xino {
27474+ char *path;
27475+ struct file *file;
27476+};
27477+
27478+struct au_opt_xino_itrunc {
27479+ aufs_bindex_t bindex;
27480+};
27481+
27482+struct au_opt_wbr_create {
27483+ int wbr_create;
27484+ int mfs_second;
27485+ unsigned long long mfsrr_watermark;
27486+};
27487+
27488+struct au_opt {
27489+ int type;
27490+ union {
27491+ struct au_opt_xino xino;
27492+ struct au_opt_xino_itrunc xino_itrunc;
27493+ struct au_opt_add add;
27494+ struct au_opt_del del;
27495+ struct au_opt_mod mod;
27496+ int dirwh;
27497+ int rdcache;
27498+ unsigned int rdblk;
27499+ unsigned int rdhash;
27500+ int udba;
27501+ struct au_opt_wbr_create wbr_create;
27502+ int wbr_copyup;
076b876e 27503+ unsigned int fhsm_second;
1facf9fc 27504+ };
27505+};
27506+
27507+/* opts flags */
27508+#define AuOpts_REMOUNT 1
027c5e7a
AM
27509+#define AuOpts_REFRESH (1 << 1)
27510+#define AuOpts_TRUNC_XIB (1 << 2)
27511+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27512+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27513+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27514+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27515+#define au_fset_opts(flags, name) \
27516+ do { (flags) |= AuOpts_##name; } while (0)
27517+#define au_fclr_opts(flags, name) \
27518+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27519+
8b6a4947
AM
27520+#ifndef CONFIG_AUFS_DIRREN
27521+#undef AuOpts_DR_FLUSHED
27522+#define AuOpts_DR_FLUSHED 0
27523+#endif
27524+
1facf9fc 27525+struct au_opts {
27526+ struct au_opt *opt;
27527+ int max_opt;
27528+
27529+ unsigned int given_udba;
27530+ unsigned int flags;
27531+ unsigned long sb_flags;
27532+};
27533+
27534+/* ---------------------------------------------------------------------- */
27535+
7e9cd9fe 27536+/* opts.c */
076b876e 27537+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27538+const char *au_optstr_udba(int udba);
27539+const char *au_optstr_wbr_copyup(int wbr_copyup);
27540+const char *au_optstr_wbr_create(int wbr_create);
27541+
27542+void au_opts_free(struct au_opts *opts);
3c1bdaff 27543+struct super_block;
1facf9fc 27544+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27545+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27546+ unsigned int pending);
27547+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27548+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27549+
27550+unsigned int au_opt_udba(struct super_block *sb);
27551+
1facf9fc 27552+#endif /* __KERNEL__ */
27553+#endif /* __AUFS_OPTS_H__ */
7f207e10
AM
27554diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27555--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
27556+++ linux/fs/aufs/plink.c 2018-07-19 09:46:13.063314675 +0200
27557@@ -0,0 +1,516 @@
27558+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27559+/*
b00004a5 27560+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 27561+ *
27562+ * This program, aufs is free software; you can redistribute it and/or modify
27563+ * it under the terms of the GNU General Public License as published by
27564+ * the Free Software Foundation; either version 2 of the License, or
27565+ * (at your option) any later version.
dece6358
AM
27566+ *
27567+ * This program is distributed in the hope that it will be useful,
27568+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27569+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27570+ * GNU General Public License for more details.
27571+ *
27572+ * You should have received a copy of the GNU General Public License
523b37e3 27573+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27574+ */
27575+
27576+/*
27577+ * pseudo-link
27578+ */
27579+
27580+#include "aufs.h"
27581+
27582+/*
e49829fe 27583+ * the pseudo-link maintenance mode.
1facf9fc 27584+ * during a user process maintains the pseudo-links,
27585+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27586+ *
27587+ * Flags
27588+ * NOPLM:
27589+ * For entry functions which will handle plink, and i_mutex is already held
27590+ * in VFS.
27591+ * They cannot wait and should return an error at once.
27592+ * Callers has to check the error.
27593+ * NOPLMW:
27594+ * For entry functions which will handle plink, but i_mutex is not held
27595+ * in VFS.
27596+ * They can wait the plink maintenance mode to finish.
27597+ *
27598+ * They behave like F_SETLK and F_SETLKW.
27599+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27600+ */
e49829fe
JR
27601+
27602+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27603+{
e49829fe
JR
27604+ int err;
27605+ pid_t pid, ppid;
f0c0a007 27606+ struct task_struct *parent, *prev;
e49829fe 27607+ struct au_sbinfo *sbi;
dece6358
AM
27608+
27609+ SiMustAnyLock(sb);
27610+
e49829fe
JR
27611+ err = 0;
27612+ if (!au_opt_test(au_mntflags(sb), PLINK))
27613+ goto out;
27614+
27615+ sbi = au_sbi(sb);
27616+ pid = sbi->si_plink_maint_pid;
27617+ if (!pid || pid == current->pid)
27618+ goto out;
27619+
27620+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27621+ prev = NULL;
27622+ parent = current;
27623+ ppid = 0;
e49829fe 27624+ rcu_read_lock();
f0c0a007
AM
27625+ while (1) {
27626+ parent = rcu_dereference(parent->real_parent);
27627+ if (parent == prev)
27628+ break;
27629+ ppid = task_pid_vnr(parent);
27630+ if (pid == ppid) {
27631+ rcu_read_unlock();
27632+ goto out;
27633+ }
27634+ prev = parent;
27635+ }
e49829fe 27636+ rcu_read_unlock();
e49829fe
JR
27637+
27638+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27639+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27640+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27641+ while (sbi->si_plink_maint_pid) {
27642+ si_read_unlock(sb);
27643+ /* gave up wake_up_bit() */
27644+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27645+
27646+ if (au_ftest_lock(flags, FLUSH))
27647+ au_nwt_flush(&sbi->si_nowait);
27648+ si_noflush_read_lock(sb);
27649+ }
27650+ } else if (au_ftest_lock(flags, NOPLM)) {
27651+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27652+ err = -EAGAIN;
27653+ }
27654+
27655+out:
27656+ return err;
4a4d8108
AM
27657+}
27658+
e49829fe 27659+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 27660+{
4a4d8108 27661+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 27662+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 27663+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 27664+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
27665+}
27666+
e49829fe 27667+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
27668+{
27669+ int err;
4a4d8108
AM
27670+ struct au_sbinfo *sbinfo;
27671+
27672+ err = 0;
4a4d8108
AM
27673+ sbinfo = au_sbi(sb);
27674+ /* make sure i am the only one in this fs */
e49829fe
JR
27675+ si_write_lock(sb, AuLock_FLUSH);
27676+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27677+ spin_lock(&sbinfo->si_plink_maint_lock);
27678+ if (!sbinfo->si_plink_maint_pid)
27679+ sbinfo->si_plink_maint_pid = current->pid;
27680+ else
27681+ err = -EBUSY;
27682+ spin_unlock(&sbinfo->si_plink_maint_lock);
27683+ }
4a4d8108
AM
27684+ si_write_unlock(sb);
27685+
27686+ return err;
1facf9fc 27687+}
27688+
27689+/* ---------------------------------------------------------------------- */
27690+
1facf9fc 27691+#ifdef CONFIG_AUFS_DEBUG
27692+void au_plink_list(struct super_block *sb)
27693+{
86dc4139 27694+ int i;
1facf9fc 27695+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27696+ struct hlist_bl_head *hbl;
27697+ struct hlist_bl_node *pos;
5afbbe0d 27698+ struct au_icntnr *icntnr;
1facf9fc 27699+
dece6358
AM
27700+ SiMustAnyLock(sb);
27701+
1facf9fc 27702+ sbinfo = au_sbi(sb);
27703+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27704+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27705+
86dc4139 27706+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27707+ hbl = sbinfo->si_plink + i;
27708+ hlist_bl_lock(hbl);
27709+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27710+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 27711+ hlist_bl_unlock(hbl);
86dc4139 27712+ }
1facf9fc 27713+}
27714+#endif
27715+
27716+/* is the inode pseudo-linked? */
27717+int au_plink_test(struct inode *inode)
27718+{
86dc4139 27719+ int found, i;
1facf9fc 27720+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27721+ struct hlist_bl_head *hbl;
27722+ struct hlist_bl_node *pos;
5afbbe0d 27723+ struct au_icntnr *icntnr;
1facf9fc 27724+
27725+ sbinfo = au_sbi(inode->i_sb);
dece6358 27726+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 27727+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 27728+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 27729+
27730+ found = 0;
86dc4139 27731+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
27732+ hbl = sbinfo->si_plink + i;
27733+ hlist_bl_lock(hbl);
27734+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27735+ if (&icntnr->vfs_inode == inode) {
1facf9fc 27736+ found = 1;
27737+ break;
27738+ }
8b6a4947 27739+ hlist_bl_unlock(hbl);
1facf9fc 27740+ return found;
27741+}
27742+
27743+/* ---------------------------------------------------------------------- */
27744+
27745+/*
27746+ * generate a name for plink.
27747+ * the file will be stored under AUFS_WH_PLINKDIR.
27748+ */
27749+/* 20 is max digits length of ulong 64 */
27750+#define PLINK_NAME_LEN ((20 + 1) * 2)
27751+
27752+static int plink_name(char *name, int len, struct inode *inode,
27753+ aufs_bindex_t bindex)
27754+{
27755+ int rlen;
27756+ struct inode *h_inode;
27757+
27758+ h_inode = au_h_iptr(inode, bindex);
27759+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27760+ return rlen;
27761+}
27762+
7f207e10
AM
27763+struct au_do_plink_lkup_args {
27764+ struct dentry **errp;
27765+ struct qstr *tgtname;
27766+ struct dentry *h_parent;
27767+ struct au_branch *br;
27768+};
27769+
27770+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27771+ struct dentry *h_parent,
27772+ struct au_branch *br)
27773+{
27774+ struct dentry *h_dentry;
febd17d6 27775+ struct inode *h_inode;
7f207e10 27776+
febd17d6 27777+ h_inode = d_inode(h_parent);
be118d29 27778+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 27779+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 27780+ inode_unlock_shared(h_inode);
7f207e10
AM
27781+ return h_dentry;
27782+}
27783+
27784+static void au_call_do_plink_lkup(void *args)
27785+{
27786+ struct au_do_plink_lkup_args *a = args;
27787+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27788+}
27789+
1facf9fc 27790+/* lookup the plink-ed @inode under the branch at @bindex */
27791+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27792+{
27793+ struct dentry *h_dentry, *h_parent;
27794+ struct au_branch *br;
7f207e10 27795+ int wkq_err;
1facf9fc 27796+ char a[PLINK_NAME_LEN];
0c3ec466 27797+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 27798+
e49829fe
JR
27799+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27800+
1facf9fc 27801+ br = au_sbr(inode->i_sb, bindex);
27802+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 27803+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27804+
2dfbb274 27805+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
27806+ struct au_do_plink_lkup_args args = {
27807+ .errp = &h_dentry,
27808+ .tgtname = &tgtname,
27809+ .h_parent = h_parent,
27810+ .br = br
27811+ };
27812+
27813+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27814+ if (unlikely(wkq_err))
27815+ h_dentry = ERR_PTR(wkq_err);
27816+ } else
27817+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27818+
1facf9fc 27819+ return h_dentry;
27820+}
27821+
27822+/* create a pseudo-link */
27823+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27824+ struct dentry *h_dentry, struct au_branch *br)
27825+{
27826+ int err;
27827+ struct path h_path = {
86dc4139 27828+ .mnt = au_br_mnt(br)
1facf9fc 27829+ };
523b37e3 27830+ struct inode *h_dir, *delegated;
1facf9fc 27831+
5527c038 27832+ h_dir = d_inode(h_parent);
febd17d6 27833+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 27834+again:
b4510431 27835+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 27836+ err = PTR_ERR(h_path.dentry);
27837+ if (IS_ERR(h_path.dentry))
27838+ goto out;
27839+
27840+ err = 0;
27841+ /* wh.plink dir is not monitored */
7f207e10 27842+ /* todo: is it really safe? */
5527c038
JR
27843+ if (d_is_positive(h_path.dentry)
27844+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
27845+ delegated = NULL;
27846+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27847+ if (unlikely(err == -EWOULDBLOCK)) {
27848+ pr_warn("cannot retry for NFSv4 delegation"
27849+ " for an internal unlink\n");
27850+ iput(delegated);
27851+ }
1facf9fc 27852+ dput(h_path.dentry);
27853+ h_path.dentry = NULL;
27854+ if (!err)
27855+ goto again;
27856+ }
5527c038 27857+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
27858+ delegated = NULL;
27859+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
27860+ if (unlikely(err == -EWOULDBLOCK)) {
27861+ pr_warn("cannot retry for NFSv4 delegation"
27862+ " for an internal link\n");
27863+ iput(delegated);
27864+ }
27865+ }
1facf9fc 27866+ dput(h_path.dentry);
27867+
4f0767ce 27868+out:
febd17d6 27869+ inode_unlock(h_dir);
1facf9fc 27870+ return err;
27871+}
27872+
27873+struct do_whplink_args {
27874+ int *errp;
27875+ struct qstr *tgt;
27876+ struct dentry *h_parent;
27877+ struct dentry *h_dentry;
27878+ struct au_branch *br;
27879+};
27880+
27881+static void call_do_whplink(void *args)
27882+{
27883+ struct do_whplink_args *a = args;
27884+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
27885+}
27886+
27887+static int whplink(struct dentry *h_dentry, struct inode *inode,
27888+ aufs_bindex_t bindex, struct au_branch *br)
27889+{
27890+ int err, wkq_err;
27891+ struct au_wbr *wbr;
27892+ struct dentry *h_parent;
1facf9fc 27893+ char a[PLINK_NAME_LEN];
0c3ec466 27894+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 27895+
27896+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
27897+ h_parent = wbr->wbr_plink;
1facf9fc 27898+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27899+
27900+ /* always superio. */
2dfbb274 27901+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 27902+ struct do_whplink_args args = {
27903+ .errp = &err,
27904+ .tgt = &tgtname,
27905+ .h_parent = h_parent,
27906+ .h_dentry = h_dentry,
27907+ .br = br
27908+ };
27909+ wkq_err = au_wkq_wait(call_do_whplink, &args);
27910+ if (unlikely(wkq_err))
27911+ err = wkq_err;
27912+ } else
27913+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 27914+
27915+ return err;
27916+}
27917+
1facf9fc 27918+/*
27919+ * create a new pseudo-link for @h_dentry on @bindex.
27920+ * the linked inode is held in aufs @inode.
27921+ */
27922+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
27923+ struct dentry *h_dentry)
27924+{
27925+ struct super_block *sb;
27926+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27927+ struct hlist_bl_head *hbl;
27928+ struct hlist_bl_node *pos;
5afbbe0d 27929+ struct au_icntnr *icntnr;
86dc4139 27930+ int found, err, cnt, i;
1facf9fc 27931+
27932+ sb = inode->i_sb;
27933+ sbinfo = au_sbi(sb);
27934+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27935+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27936+
86dc4139 27937+ found = au_plink_test(inode);
4a4d8108 27938+ if (found)
1facf9fc 27939+ return;
4a4d8108 27940+
86dc4139 27941+ i = au_plink_hash(inode->i_ino);
8b6a4947 27942+ hbl = sbinfo->si_plink + i;
5afbbe0d 27943+ au_igrab(inode);
1facf9fc 27944+
8b6a4947
AM
27945+ hlist_bl_lock(hbl);
27946+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 27947+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
27948+ found = 1;
27949+ break;
27950+ }
1facf9fc 27951+ }
5afbbe0d
AM
27952+ if (!found) {
27953+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 27954+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 27955+ }
8b6a4947 27956+ hlist_bl_unlock(hbl);
4a4d8108 27957+ if (!found) {
8b6a4947 27958+ cnt = au_hbl_count(hbl);
86dc4139
AM
27959+#define msg "unexpectedly unblanced or too many pseudo-links"
27960+ if (cnt > AUFS_PLINK_WARN)
27961+ AuWarn1(msg ", %d\n", cnt);
27962+#undef msg
1facf9fc 27963+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
27964+ if (unlikely(err)) {
27965+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 27966+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 27967+ iput(&icntnr->vfs_inode);
4a4d8108 27968+ }
5afbbe0d
AM
27969+ } else
27970+ iput(&icntnr->vfs_inode);
1facf9fc 27971+}
27972+
27973+/* free all plinks */
e49829fe 27974+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 27975+{
86dc4139 27976+ int i, warned;
1facf9fc 27977+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27978+ struct hlist_bl_head *hbl;
27979+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 27980+ struct au_icntnr *icntnr;
1facf9fc 27981+
dece6358
AM
27982+ SiMustWriteLock(sb);
27983+
1facf9fc 27984+ sbinfo = au_sbi(sb);
27985+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27986+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27987+
1facf9fc 27988+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
27989+ warned = 0;
27990+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27991+ hbl = sbinfo->si_plink + i;
27992+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
27993+ pr_warn("pseudo-link is not flushed");
27994+ warned = 1;
27995+ }
8b6a4947 27996+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 27997+ iput(&icntnr->vfs_inode);
8b6a4947 27998+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 27999+ }
1facf9fc 28000+}
28001+
e49829fe
JR
28002+void au_plink_clean(struct super_block *sb, int verbose)
28003+{
28004+ struct dentry *root;
28005+
28006+ root = sb->s_root;
28007+ aufs_write_lock(root);
28008+ if (au_opt_test(au_mntflags(sb), PLINK))
28009+ au_plink_put(sb, verbose);
28010+ aufs_write_unlock(root);
28011+}
28012+
86dc4139
AM
28013+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28014+{
28015+ int do_put;
5afbbe0d 28016+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
28017+
28018+ do_put = 0;
5afbbe0d
AM
28019+ btop = au_ibtop(inode);
28020+ bbot = au_ibbot(inode);
28021+ if (btop >= 0) {
28022+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
28023+ if (!au_h_iptr(inode, bindex)
28024+ || au_ii_br_id(inode, bindex) != br_id)
28025+ continue;
28026+ au_set_h_iptr(inode, bindex, NULL, 0);
28027+ do_put = 1;
28028+ break;
28029+ }
28030+ if (do_put)
5afbbe0d 28031+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
28032+ if (au_h_iptr(inode, bindex)) {
28033+ do_put = 0;
28034+ break;
28035+ }
28036+ } else
28037+ do_put = 1;
28038+
28039+ return do_put;
28040+}
28041+
1facf9fc 28042+/* free the plinks on a branch specified by @br_id */
28043+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28044+{
28045+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28046+ struct hlist_bl_head *hbl;
28047+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28048+ struct au_icntnr *icntnr;
1facf9fc 28049+ struct inode *inode;
86dc4139 28050+ int i, do_put;
1facf9fc 28051+
dece6358
AM
28052+ SiMustWriteLock(sb);
28053+
1facf9fc 28054+ sbinfo = au_sbi(sb);
28055+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28056+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28057+
8b6a4947 28058+ /* no bit_lock since sbinfo is write-locked */
86dc4139 28059+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28060+ hbl = sbinfo->si_plink + i;
28061+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 28062+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
28063+ ii_write_lock_child(inode);
28064+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 28065+ if (do_put) {
8b6a4947 28066+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
28067+ iput(inode);
28068+ }
86dc4139
AM
28069+ ii_write_unlock(inode);
28070+ iput(inode);
dece6358 28071+ }
dece6358
AM
28072+ }
28073+}
7f207e10
AM
28074diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28075--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
28076+++ linux/fs/aufs/poll.c 2018-07-19 09:46:13.063314675 +0200
28077@@ -0,0 +1,54 @@
28078+/* SPDX-License-Identifier: GPL-2.0 */
dece6358 28079+/*
b00004a5 28080+ * Copyright (C) 2005-2018 Junjiro R. Okajima
dece6358
AM
28081+ *
28082+ * This program, aufs is free software; you can redistribute it and/or modify
28083+ * it under the terms of the GNU General Public License as published by
28084+ * the Free Software Foundation; either version 2 of the License, or
28085+ * (at your option) any later version.
28086+ *
28087+ * This program is distributed in the hope that it will be useful,
28088+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28089+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28090+ * GNU General Public License for more details.
28091+ *
28092+ * You should have received a copy of the GNU General Public License
523b37e3 28093+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
28094+ */
28095+
1308ab2a 28096+/*
28097+ * poll operation
28098+ * There is only one filesystem which implements ->poll operation, currently.
28099+ */
28100+
28101+#include "aufs.h"
28102+
be118d29 28103+__poll_t aufs_poll(struct file *file, poll_table *wait)
1308ab2a 28104+{
be118d29 28105+ __poll_t mask;
1308ab2a 28106+ struct file *h_file;
1308ab2a 28107+ struct super_block *sb;
28108+
28109+ /* We should pretend an error happened. */
be118d29 28110+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 28111+ sb = file->f_path.dentry->d_sb;
e49829fe 28112+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 28113+
521ced18 28114+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
062440b3
AM
28115+ if (IS_ERR(h_file)) {
28116+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
1308ab2a 28117+ goto out;
062440b3 28118+ }
1308ab2a 28119+
28120+ /* it is not an error if h_file has no operation */
28121+ mask = DEFAULT_POLLMASK;
523b37e3 28122+ if (h_file->f_op->poll)
1308ab2a 28123+ mask = h_file->f_op->poll(h_file, wait);
b912730e 28124+ fput(h_file); /* instead of au_read_post() */
1308ab2a 28125+
4f0767ce 28126+out:
1308ab2a 28127+ si_read_unlock(sb);
062440b3 28128+ if (mask & EPOLLERR)
b00004a5 28129+ AuDbg("mask 0x%x\n", mask);
1308ab2a 28130+ return mask;
28131+}
c1595e42
JR
28132diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28133--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
28134+++ linux/fs/aufs/posix_acl.c 2018-07-19 09:46:13.063314675 +0200
28135@@ -0,0 +1,103 @@
28136+/* SPDX-License-Identifier: GPL-2.0 */
c1595e42 28137+/*
b00004a5 28138+ * Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
28139+ *
28140+ * This program, aufs is free software; you can redistribute it and/or modify
28141+ * it under the terms of the GNU General Public License as published by
28142+ * the Free Software Foundation; either version 2 of the License, or
28143+ * (at your option) any later version.
28144+ *
28145+ * This program is distributed in the hope that it will be useful,
28146+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28147+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28148+ * GNU General Public License for more details.
28149+ *
28150+ * You should have received a copy of the GNU General Public License
28151+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28152+ */
28153+
28154+/*
28155+ * posix acl operations
28156+ */
28157+
28158+#include <linux/fs.h>
c1595e42
JR
28159+#include "aufs.h"
28160+
28161+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28162+{
28163+ struct posix_acl *acl;
28164+ int err;
28165+ aufs_bindex_t bindex;
28166+ struct inode *h_inode;
28167+ struct super_block *sb;
28168+
28169+ acl = NULL;
28170+ sb = inode->i_sb;
28171+ si_read_lock(sb, AuLock_FLUSH);
28172+ ii_read_lock_child(inode);
2121bcd9 28173+ if (!(sb->s_flags & SB_POSIXACL))
c1595e42
JR
28174+ goto out;
28175+
5afbbe0d 28176+ bindex = au_ibtop(inode);
c1595e42
JR
28177+ h_inode = au_h_iptr(inode, bindex);
28178+ if (unlikely(!h_inode
28179+ || ((h_inode->i_mode & S_IFMT)
28180+ != (inode->i_mode & S_IFMT)))) {
28181+ err = au_busy_or_stale();
28182+ acl = ERR_PTR(err);
28183+ goto out;
28184+ }
28185+
28186+ /* always topmost only */
28187+ acl = get_acl(h_inode, type);
a2654f78
AM
28188+ if (!IS_ERR_OR_NULL(acl))
28189+ set_cached_acl(inode, type, acl);
c1595e42
JR
28190+
28191+out:
28192+ ii_read_unlock(inode);
28193+ si_read_unlock(sb);
28194+
28195+ AuTraceErrPtr(acl);
28196+ return acl;
28197+}
28198+
28199+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28200+{
28201+ int err;
28202+ ssize_t ssz;
28203+ struct dentry *dentry;
f2c43d5f 28204+ struct au_sxattr arg = {
c1595e42
JR
28205+ .type = AU_ACL_SET,
28206+ .u.acl_set = {
28207+ .acl = acl,
28208+ .type = type
28209+ },
28210+ };
28211+
5afbbe0d
AM
28212+ IMustLock(inode);
28213+
c1595e42
JR
28214+ if (inode->i_ino == AUFS_ROOT_INO)
28215+ dentry = dget(inode->i_sb->s_root);
28216+ else {
28217+ dentry = d_find_alias(inode);
28218+ if (!dentry)
28219+ dentry = d_find_any_alias(inode);
28220+ if (!dentry) {
28221+ pr_warn("cannot handle this inode, "
28222+ "please report to aufs-users ML\n");
28223+ err = -ENOENT;
28224+ goto out;
28225+ }
28226+ }
28227+
f2c43d5f 28228+ ssz = au_sxattr(dentry, inode, &arg);
c1595e42
JR
28229+ dput(dentry);
28230+ err = ssz;
a2654f78 28231+ if (ssz >= 0) {
c1595e42 28232+ err = 0;
a2654f78
AM
28233+ set_cached_acl(inode, type, acl);
28234+ }
c1595e42
JR
28235+
28236+out:
c1595e42
JR
28237+ return err;
28238+}
7f207e10
AM
28239diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28240--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
28241+++ linux/fs/aufs/procfs.c 2018-07-19 09:46:13.063314675 +0200
28242@@ -0,0 +1,171 @@
28243+/* SPDX-License-Identifier: GPL-2.0 */
e49829fe 28244+/*
b00004a5 28245+ * Copyright (C) 2010-2018 Junjiro R. Okajima
e49829fe
JR
28246+ *
28247+ * This program, aufs is free software; you can redistribute it and/or modify
28248+ * it under the terms of the GNU General Public License as published by
28249+ * the Free Software Foundation; either version 2 of the License, or
28250+ * (at your option) any later version.
28251+ *
28252+ * This program is distributed in the hope that it will be useful,
28253+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28254+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28255+ * GNU General Public License for more details.
28256+ *
28257+ * You should have received a copy of the GNU General Public License
523b37e3 28258+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28259+ */
28260+
28261+/*
28262+ * procfs interfaces
28263+ */
28264+
28265+#include <linux/proc_fs.h>
28266+#include "aufs.h"
28267+
28268+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28269+{
28270+ struct au_sbinfo *sbinfo;
28271+
28272+ sbinfo = file->private_data;
28273+ if (sbinfo) {
28274+ au_plink_maint_leave(sbinfo);
28275+ kobject_put(&sbinfo->si_kobj);
28276+ }
28277+
28278+ return 0;
28279+}
28280+
28281+static void au_procfs_plm_write_clean(struct file *file)
28282+{
28283+ struct au_sbinfo *sbinfo;
28284+
28285+ sbinfo = file->private_data;
28286+ if (sbinfo)
28287+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28288+}
28289+
28290+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28291+{
28292+ int err;
28293+ struct super_block *sb;
28294+ struct au_sbinfo *sbinfo;
8b6a4947 28295+ struct hlist_bl_node *pos;
e49829fe
JR
28296+
28297+ err = -EBUSY;
28298+ if (unlikely(file->private_data))
28299+ goto out;
28300+
28301+ sb = NULL;
53392da6 28302+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28303+ hlist_bl_lock(&au_sbilist);
28304+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe
JR
28305+ if (id == sysaufs_si_id(sbinfo)) {
28306+ kobject_get(&sbinfo->si_kobj);
28307+ sb = sbinfo->si_sb;
28308+ break;
28309+ }
8b6a4947 28310+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28311+
28312+ err = -EINVAL;
28313+ if (unlikely(!sb))
28314+ goto out;
28315+
28316+ err = au_plink_maint_enter(sb);
28317+ if (!err)
28318+ /* keep kobject_get() */
28319+ file->private_data = sbinfo;
28320+ else
28321+ kobject_put(&sbinfo->si_kobj);
28322+out:
28323+ return err;
28324+}
28325+
28326+/*
28327+ * Accept a valid "si=xxxx" only.
28328+ * Once it is accepted successfully, accept "clean" too.
28329+ */
28330+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28331+ size_t count, loff_t *ppos)
28332+{
28333+ ssize_t err;
28334+ unsigned long id;
28335+ /* last newline is allowed */
28336+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28337+
28338+ err = -EACCES;
28339+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28340+ goto out;
28341+
28342+ err = -EINVAL;
28343+ if (unlikely(count > sizeof(buf)))
28344+ goto out;
28345+
28346+ err = copy_from_user(buf, ubuf, count);
28347+ if (unlikely(err)) {
28348+ err = -EFAULT;
28349+ goto out;
28350+ }
28351+ buf[count] = 0;
28352+
28353+ err = -EINVAL;
28354+ if (!strcmp("clean", buf)) {
28355+ au_procfs_plm_write_clean(file);
28356+ goto out_success;
28357+ } else if (unlikely(strncmp("si=", buf, 3)))
28358+ goto out;
28359+
9dbd164d 28360+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28361+ if (unlikely(err))
28362+ goto out;
28363+
28364+ err = au_procfs_plm_write_si(file, id);
28365+ if (unlikely(err))
28366+ goto out;
28367+
28368+out_success:
28369+ err = count; /* success */
28370+out:
28371+ return err;
28372+}
28373+
28374+static const struct file_operations au_procfs_plm_fop = {
28375+ .write = au_procfs_plm_write,
28376+ .release = au_procfs_plm_release,
28377+ .owner = THIS_MODULE
28378+};
28379+
28380+/* ---------------------------------------------------------------------- */
28381+
28382+static struct proc_dir_entry *au_procfs_dir;
28383+
28384+void au_procfs_fin(void)
28385+{
28386+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28387+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28388+}
28389+
28390+int __init au_procfs_init(void)
28391+{
28392+ int err;
28393+ struct proc_dir_entry *entry;
28394+
28395+ err = -ENOMEM;
28396+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28397+ if (unlikely(!au_procfs_dir))
28398+ goto out;
28399+
28400+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
28401+ au_procfs_dir, &au_procfs_plm_fop);
28402+ if (unlikely(!entry))
28403+ goto out_dir;
28404+
28405+ err = 0;
28406+ goto out; /* success */
28407+
28408+
28409+out_dir:
28410+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28411+out:
28412+ return err;
28413+}
7f207e10
AM
28414diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28415--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
28416+++ linux/fs/aufs/rdu.c 2018-07-19 09:46:13.063314675 +0200
28417@@ -0,0 +1,382 @@
28418+/* SPDX-License-Identifier: GPL-2.0 */
1308ab2a 28419+/*
b00004a5 28420+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1308ab2a 28421+ *
28422+ * This program, aufs is free software; you can redistribute it and/or modify
28423+ * it under the terms of the GNU General Public License as published by
28424+ * the Free Software Foundation; either version 2 of the License, or
28425+ * (at your option) any later version.
28426+ *
28427+ * This program is distributed in the hope that it will be useful,
28428+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28429+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28430+ * GNU General Public License for more details.
28431+ *
28432+ * You should have received a copy of the GNU General Public License
523b37e3 28433+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28434+ */
28435+
28436+/*
28437+ * readdir in userspace.
28438+ */
28439+
b752ccd1 28440+#include <linux/compat.h>
4a4d8108 28441+#include <linux/fs_stack.h>
1308ab2a 28442+#include <linux/security.h>
1308ab2a 28443+#include "aufs.h"
28444+
28445+/* bits for struct aufs_rdu.flags */
28446+#define AuRdu_CALLED 1
28447+#define AuRdu_CONT (1 << 1)
28448+#define AuRdu_FULL (1 << 2)
28449+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28450+#define au_fset_rdu(flags, name) \
28451+ do { (flags) |= AuRdu_##name; } while (0)
28452+#define au_fclr_rdu(flags, name) \
28453+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28454+
28455+struct au_rdu_arg {
392086de 28456+ struct dir_context ctx;
1308ab2a 28457+ struct aufs_rdu *rdu;
28458+ union au_rdu_ent_ul ent;
28459+ unsigned long end;
28460+
28461+ struct super_block *sb;
28462+ int err;
28463+};
28464+
392086de 28465+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28466+ loff_t offset, u64 h_ino, unsigned int d_type)
28467+{
28468+ int err, len;
392086de 28469+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28470+ struct aufs_rdu *rdu = arg->rdu;
28471+ struct au_rdu_ent ent;
28472+
28473+ err = 0;
28474+ arg->err = 0;
28475+ au_fset_rdu(rdu->cookie.flags, CALLED);
28476+ len = au_rdu_len(nlen);
28477+ if (arg->ent.ul + len < arg->end) {
28478+ ent.ino = h_ino;
28479+ ent.bindex = rdu->cookie.bindex;
28480+ ent.type = d_type;
28481+ ent.nlen = nlen;
4a4d8108
AM
28482+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28483+ ent.type = DT_UNKNOWN;
1308ab2a 28484+
9dbd164d 28485+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28486+ err = -EFAULT;
28487+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28488+ goto out;
28489+ if (copy_to_user(arg->ent.e->name, name, nlen))
28490+ goto out;
28491+ /* the terminating NULL */
28492+ if (__put_user(0, arg->ent.e->name + nlen))
28493+ goto out;
28494+ err = 0;
28495+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28496+ arg->ent.ul += len;
28497+ rdu->rent++;
28498+ } else {
28499+ err = -EFAULT;
28500+ au_fset_rdu(rdu->cookie.flags, FULL);
28501+ rdu->full = 1;
28502+ rdu->tail = arg->ent;
28503+ }
28504+
4f0767ce 28505+out:
1308ab2a 28506+ /* AuTraceErr(err); */
28507+ return err;
28508+}
28509+
28510+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28511+{
28512+ int err;
28513+ loff_t offset;
28514+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28515+
92d182d2 28516+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28517+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28518+ err = offset;
28519+ if (unlikely(offset != cookie->h_pos))
28520+ goto out;
28521+
28522+ err = 0;
28523+ do {
28524+ arg->err = 0;
28525+ au_fclr_rdu(cookie->flags, CALLED);
28526+ /* smp_mb(); */
392086de 28527+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28528+ if (err >= 0)
28529+ err = arg->err;
28530+ } while (!err
28531+ && au_ftest_rdu(cookie->flags, CALLED)
28532+ && !au_ftest_rdu(cookie->flags, FULL));
28533+ cookie->h_pos = h_file->f_pos;
28534+
4f0767ce 28535+out:
1308ab2a 28536+ AuTraceErr(err);
28537+ return err;
28538+}
28539+
28540+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28541+{
28542+ int err;
5afbbe0d 28543+ aufs_bindex_t bbot;
392086de
AM
28544+ struct au_rdu_arg arg = {
28545+ .ctx = {
2000de60 28546+ .actor = au_rdu_fill
392086de
AM
28547+ }
28548+ };
1308ab2a 28549+ struct dentry *dentry;
28550+ struct inode *inode;
28551+ struct file *h_file;
28552+ struct au_rdu_cookie *cookie = &rdu->cookie;
28553+
28554+ err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
28555+ if (unlikely(err)) {
28556+ err = -EFAULT;
28557+ AuTraceErr(err);
28558+ goto out;
28559+ }
28560+ rdu->rent = 0;
28561+ rdu->tail = rdu->ent;
28562+ rdu->full = 0;
28563+ arg.rdu = rdu;
28564+ arg.ent = rdu->ent;
28565+ arg.end = arg.ent.ul;
28566+ arg.end += rdu->sz;
28567+
28568+ err = -ENOTDIR;
5afbbe0d 28569+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28570+ goto out;
28571+
28572+ err = security_file_permission(file, MAY_READ);
28573+ AuTraceErr(err);
28574+ if (unlikely(err))
28575+ goto out;
28576+
2000de60 28577+ dentry = file->f_path.dentry;
5527c038 28578+ inode = d_inode(dentry);
5afbbe0d 28579+ inode_lock_shared(inode);
1308ab2a 28580+
28581+ arg.sb = inode->i_sb;
e49829fe
JR
28582+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28583+ if (unlikely(err))
28584+ goto out_mtx;
027c5e7a
AM
28585+ err = au_alive_dir(dentry);
28586+ if (unlikely(err))
28587+ goto out_si;
e49829fe 28588+ /* todo: reval? */
1308ab2a 28589+ fi_read_lock(file);
28590+
28591+ err = -EAGAIN;
28592+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28593+ && cookie->generation != au_figen(file)))
28594+ goto out_unlock;
28595+
28596+ err = 0;
28597+ if (!rdu->blk) {
28598+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28599+ if (!rdu->blk)
28600+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28601+ }
5afbbe0d
AM
28602+ bbot = au_fbtop(file);
28603+ if (cookie->bindex < bbot)
28604+ cookie->bindex = bbot;
28605+ bbot = au_fbbot_dir(file);
28606+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28607+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28608+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28609+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28610+ if (!h_file)
28611+ continue;
28612+
28613+ au_fclr_rdu(cookie->flags, FULL);
28614+ err = au_rdu_do(h_file, &arg);
28615+ AuTraceErr(err);
28616+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28617+ break;
28618+ }
28619+ AuDbg("rent %llu\n", rdu->rent);
28620+
28621+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28622+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28623+ au_fset_rdu(cookie->flags, CONT);
28624+ cookie->generation = au_figen(file);
28625+ }
28626+
28627+ ii_read_lock_child(inode);
5afbbe0d 28628+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28629+ ii_read_unlock(inode);
28630+
4f0767ce 28631+out_unlock:
1308ab2a 28632+ fi_read_unlock(file);
027c5e7a 28633+out_si:
1308ab2a 28634+ si_read_unlock(arg.sb);
4f0767ce 28635+out_mtx:
5afbbe0d 28636+ inode_unlock_shared(inode);
4f0767ce 28637+out:
1308ab2a 28638+ AuTraceErr(err);
28639+ return err;
28640+}
28641+
28642+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28643+{
28644+ int err;
28645+ ino_t ino;
28646+ unsigned long long nent;
28647+ union au_rdu_ent_ul *u;
28648+ struct au_rdu_ent ent;
28649+ struct super_block *sb;
28650+
28651+ err = 0;
28652+ nent = rdu->nent;
28653+ u = &rdu->ent;
2000de60 28654+ sb = file->f_path.dentry->d_sb;
1308ab2a 28655+ si_read_lock(sb, AuLock_FLUSH);
28656+ while (nent-- > 0) {
9dbd164d 28657+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28658+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108
AM
28659+ if (!err)
28660+ err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
1308ab2a 28661+ if (unlikely(err)) {
28662+ err = -EFAULT;
28663+ AuTraceErr(err);
28664+ break;
28665+ }
28666+
28667+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28668+ if (!ent.wh)
28669+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28670+ else
28671+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28672+ &ino);
28673+ if (unlikely(err)) {
28674+ AuTraceErr(err);
28675+ break;
28676+ }
28677+
28678+ err = __put_user(ino, &u->e->ino);
28679+ if (unlikely(err)) {
28680+ err = -EFAULT;
28681+ AuTraceErr(err);
28682+ break;
28683+ }
28684+ u->ul += au_rdu_len(ent.nlen);
28685+ }
28686+ si_read_unlock(sb);
28687+
28688+ return err;
28689+}
28690+
28691+/* ---------------------------------------------------------------------- */
28692+
28693+static int au_rdu_verify(struct aufs_rdu *rdu)
28694+{
b752ccd1 28695+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 28696+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 28697+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 28698+ rdu->blk,
28699+ rdu->rent, rdu->shwh, rdu->full,
28700+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28701+ rdu->cookie.generation);
dece6358 28702+
b752ccd1 28703+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 28704+ return 0;
dece6358 28705+
b752ccd1
AM
28706+ AuDbg("%u:%u\n",
28707+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 28708+ return -EINVAL;
28709+}
28710+
28711+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 28712+{
1308ab2a 28713+ long err, e;
28714+ struct aufs_rdu rdu;
28715+ void __user *p = (void __user *)arg;
dece6358 28716+
1308ab2a 28717+ err = copy_from_user(&rdu, p, sizeof(rdu));
28718+ if (unlikely(err)) {
28719+ err = -EFAULT;
28720+ AuTraceErr(err);
28721+ goto out;
28722+ }
28723+ err = au_rdu_verify(&rdu);
dece6358
AM
28724+ if (unlikely(err))
28725+ goto out;
28726+
1308ab2a 28727+ switch (cmd) {
28728+ case AUFS_CTL_RDU:
28729+ err = au_rdu(file, &rdu);
28730+ if (unlikely(err))
28731+ break;
dece6358 28732+
1308ab2a 28733+ e = copy_to_user(p, &rdu, sizeof(rdu));
28734+ if (unlikely(e)) {
28735+ err = -EFAULT;
28736+ AuTraceErr(err);
28737+ }
28738+ break;
28739+ case AUFS_CTL_RDU_INO:
28740+ err = au_rdu_ino(file, &rdu);
28741+ break;
28742+
28743+ default:
4a4d8108 28744+ /* err = -ENOTTY; */
1308ab2a 28745+ err = -EINVAL;
28746+ }
dece6358 28747+
4f0767ce 28748+out:
1308ab2a 28749+ AuTraceErr(err);
28750+ return err;
1facf9fc 28751+}
b752ccd1
AM
28752+
28753+#ifdef CONFIG_COMPAT
28754+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28755+{
28756+ long err, e;
28757+ struct aufs_rdu rdu;
28758+ void __user *p = compat_ptr(arg);
28759+
28760+ /* todo: get_user()? */
28761+ err = copy_from_user(&rdu, p, sizeof(rdu));
28762+ if (unlikely(err)) {
28763+ err = -EFAULT;
28764+ AuTraceErr(err);
28765+ goto out;
28766+ }
28767+ rdu.ent.e = compat_ptr(rdu.ent.ul);
28768+ err = au_rdu_verify(&rdu);
28769+ if (unlikely(err))
28770+ goto out;
28771+
28772+ switch (cmd) {
28773+ case AUFS_CTL_RDU:
28774+ err = au_rdu(file, &rdu);
28775+ if (unlikely(err))
28776+ break;
28777+
28778+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28779+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28780+ e = copy_to_user(p, &rdu, sizeof(rdu));
28781+ if (unlikely(e)) {
28782+ err = -EFAULT;
28783+ AuTraceErr(err);
28784+ }
28785+ break;
28786+ case AUFS_CTL_RDU_INO:
28787+ err = au_rdu_ino(file, &rdu);
28788+ break;
28789+
28790+ default:
28791+ /* err = -ENOTTY; */
28792+ err = -EINVAL;
28793+ }
28794+
4f0767ce 28795+out:
b752ccd1
AM
28796+ AuTraceErr(err);
28797+ return err;
28798+}
28799+#endif
7f207e10
AM
28800diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28801--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
28802+++ linux/fs/aufs/rwsem.h 2018-07-19 09:46:13.063314675 +0200
28803@@ -0,0 +1,73 @@
28804+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 28805+/*
b00004a5 28806+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 28807+ *
28808+ * This program, aufs is free software; you can redistribute it and/or modify
28809+ * it under the terms of the GNU General Public License as published by
28810+ * the Free Software Foundation; either version 2 of the License, or
28811+ * (at your option) any later version.
dece6358
AM
28812+ *
28813+ * This program is distributed in the hope that it will be useful,
28814+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28815+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28816+ * GNU General Public License for more details.
28817+ *
28818+ * You should have received a copy of the GNU General Public License
523b37e3 28819+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28820+ */
28821+
28822+/*
28823+ * simple read-write semaphore wrappers
28824+ */
28825+
28826+#ifndef __AUFS_RWSEM_H__
28827+#define __AUFS_RWSEM_H__
28828+
28829+#ifdef __KERNEL__
28830+
4a4d8108 28831+#include "debug.h"
dece6358 28832+
8b6a4947
AM
28833+/* in the futre, the name 'au_rwsem' will be totally gone */
28834+#define au_rwsem rw_semaphore
dece6358
AM
28835+
28836+/* to debug easier, do not make them inlined functions */
8b6a4947 28837+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
dece6358 28838+/* rwsem_is_locked() is unusable */
8b6a4947
AM
28839+#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
28840+ && debug_locks \
28841+ && !lockdep_is_held_type(rw, 1))
28842+#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
28843+ && debug_locks \
28844+ && !lockdep_is_held_type(rw, 0))
28845+#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
28846+ && debug_locks \
28847+ && !lockdep_is_held(rw))
28848+#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
28849+ && debug_locks \
28850+ && lockdep_is_held(rw))
28851+
28852+#define au_rw_init(rw) init_rwsem(rw)
dece6358 28853+
5afbbe0d
AM
28854+#define au_rw_init_wlock(rw) do { \
28855+ au_rw_init(rw); \
8b6a4947 28856+ down_write(rw); \
5afbbe0d 28857+ } while (0)
dece6358 28858+
8b6a4947
AM
28859+#define au_rw_init_wlock_nested(rw, lsc) do { \
28860+ au_rw_init(rw); \
28861+ down_write_nested(rw, lsc); \
5afbbe0d 28862+ } while (0)
dece6358 28863+
8b6a4947
AM
28864+#define au_rw_read_lock(rw) down_read(rw)
28865+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
28866+#define au_rw_read_unlock(rw) up_read(rw)
28867+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
28868+#define au_rw_write_lock(rw) down_write(rw)
28869+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
28870+#define au_rw_write_unlock(rw) up_write(rw)
28871+/* why is not _nested version defined? */
28872+#define au_rw_read_trylock(rw) down_read_trylock(rw)
28873+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 28874+
28875+#endif /* __KERNEL__ */
28876+#endif /* __AUFS_RWSEM_H__ */
7f207e10
AM
28877diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
28878--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
28879+++ linux/fs/aufs/sbinfo.c 2018-07-19 09:46:13.063314675 +0200
28880@@ -0,0 +1,312 @@
28881+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 28882+/*
b00004a5 28883+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 28884+ *
28885+ * This program, aufs is free software; you can redistribute it and/or modify
28886+ * it under the terms of the GNU General Public License as published by
28887+ * the Free Software Foundation; either version 2 of the License, or
28888+ * (at your option) any later version.
dece6358
AM
28889+ *
28890+ * This program is distributed in the hope that it will be useful,
28891+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28892+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28893+ * GNU General Public License for more details.
28894+ *
28895+ * You should have received a copy of the GNU General Public License
523b37e3 28896+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28897+ */
28898+
28899+/*
28900+ * superblock private data
28901+ */
28902+
28903+#include "aufs.h"
28904+
28905+/*
28906+ * they are necessary regardless sysfs is disabled.
28907+ */
28908+void au_si_free(struct kobject *kobj)
28909+{
86dc4139 28910+ int i;
1facf9fc 28911+ struct au_sbinfo *sbinfo;
b752ccd1 28912+ char *locked __maybe_unused; /* debug only */
1facf9fc 28913+
28914+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 28915+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 28916+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 28917+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d
AM
28918+
28919+ AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
28920+ percpu_counter_destroy(&sbinfo->si_ninodes);
28921+ AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
28922+ percpu_counter_destroy(&sbinfo->si_nfiles);
1facf9fc 28923+
062440b3 28924+ dbgaufs_si_fin(sbinfo);
e49829fe 28925+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 28926+ au_br_free(sbinfo);
e49829fe 28927+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 28928+
1c60b727 28929+ kfree(sbinfo->si_branch);
1facf9fc 28930+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 28931+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 28932+
1c60b727 28933+ kfree(sbinfo);
1facf9fc 28934+}
28935+
28936+int au_si_alloc(struct super_block *sb)
28937+{
86dc4139 28938+ int err, i;
1facf9fc 28939+ struct au_sbinfo *sbinfo;
28940+
28941+ err = -ENOMEM;
4a4d8108 28942+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 28943+ if (unlikely(!sbinfo))
28944+ goto out;
28945+
28946+ /* will be reallocated separately */
28947+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
28948+ if (unlikely(!sbinfo->si_branch))
febd17d6 28949+ goto out_sbinfo;
1facf9fc 28950+
1facf9fc 28951+ err = sysaufs_si_init(sbinfo);
062440b3
AM
28952+ if (!err) {
28953+ dbgaufs_si_null(sbinfo);
28954+ err = dbgaufs_si_init(sbinfo);
28955+ if (unlikely(err))
28956+ kobject_put(&sbinfo->si_kobj);
28957+ }
1facf9fc 28958+ if (unlikely(err))
28959+ goto out_br;
28960+
28961+ au_nwt_init(&sbinfo->si_nowait);
dece6358 28962+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 28963+
5afbbe0d
AM
28964+ percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
28965+ percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
7f207e10 28966+
5afbbe0d 28967+ sbinfo->si_bbot = -1;
392086de 28968+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 28969+
28970+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
28971+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
28972+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
28973+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 28974+
076b876e
AM
28975+ au_fhsm_init(sbinfo);
28976+
e49829fe 28977+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 28978+
392086de
AM
28979+ sbinfo->si_xino_jiffy = jiffies;
28980+ sbinfo->si_xino_expire
28981+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 28982+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 28983+ sbinfo->si_xino_brid = -1;
28984+ /* leave si_xib_last_pindex and si_xib_next_bit */
28985+
8b6a4947 28986+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 28987+
e49829fe 28988+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 28989+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
28990+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
28991+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
28992+
86dc4139 28993+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 28994+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 28995+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 28996+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 28997+
8b6a4947 28998+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 28999+
b95c5147
AM
29000+ /* with getattr by default */
29001+ sbinfo->si_iop_array = aufs_iop;
29002+
1facf9fc 29003+ /* leave other members for sysaufs and si_mnt. */
29004+ sbinfo->si_sb = sb;
29005+ sb->s_fs_info = sbinfo;
b752ccd1 29006+ si_pid_set(sb);
1facf9fc 29007+ return 0; /* success */
29008+
4f0767ce 29009+out_br:
1c60b727 29010+ kfree(sbinfo->si_branch);
4f0767ce 29011+out_sbinfo:
1c60b727 29012+ kfree(sbinfo);
4f0767ce 29013+out:
1facf9fc 29014+ return err;
29015+}
29016+
e2f27e51 29017+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 29018+{
29019+ int err, sz;
29020+ struct au_branch **brp;
29021+
dece6358
AM
29022+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29023+
1facf9fc 29024+ err = -ENOMEM;
5afbbe0d 29025+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 29026+ if (unlikely(!sz))
29027+ sz = sizeof(*brp);
e2f27e51
AM
29028+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29029+ may_shrink);
1facf9fc 29030+ if (brp) {
29031+ sbinfo->si_branch = brp;
29032+ err = 0;
29033+ }
29034+
29035+ return err;
29036+}
29037+
29038+/* ---------------------------------------------------------------------- */
29039+
29040+unsigned int au_sigen_inc(struct super_block *sb)
29041+{
29042+ unsigned int gen;
5527c038 29043+ struct inode *inode;
1facf9fc 29044+
dece6358
AM
29045+ SiMustWriteLock(sb);
29046+
1facf9fc 29047+ gen = ++au_sbi(sb)->si_generation;
29048+ au_update_digen(sb->s_root);
5527c038
JR
29049+ inode = d_inode(sb->s_root);
29050+ au_update_iigen(inode, /*half*/0);
be118d29 29051+ inode_inc_iversion(inode);
1facf9fc 29052+ return gen;
29053+}
29054+
29055+aufs_bindex_t au_new_br_id(struct super_block *sb)
29056+{
29057+ aufs_bindex_t br_id;
29058+ int i;
29059+ struct au_sbinfo *sbinfo;
29060+
dece6358
AM
29061+ SiMustWriteLock(sb);
29062+
1facf9fc 29063+ sbinfo = au_sbi(sb);
29064+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29065+ br_id = ++sbinfo->si_last_br_id;
7f207e10 29066+ AuDebugOn(br_id < 0);
1facf9fc 29067+ if (br_id && au_br_index(sb, br_id) < 0)
29068+ return br_id;
29069+ }
29070+
29071+ return -1;
29072+}
29073+
29074+/* ---------------------------------------------------------------------- */
29075+
e49829fe
JR
29076+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29077+int si_read_lock(struct super_block *sb, int flags)
29078+{
29079+ int err;
29080+
29081+ err = 0;
29082+ if (au_ftest_lock(flags, FLUSH))
29083+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29084+
29085+ si_noflush_read_lock(sb);
29086+ err = au_plink_maint(sb, flags);
29087+ if (unlikely(err))
29088+ si_read_unlock(sb);
29089+
29090+ return err;
29091+}
29092+
29093+int si_write_lock(struct super_block *sb, int flags)
29094+{
29095+ int err;
29096+
29097+ if (au_ftest_lock(flags, FLUSH))
29098+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29099+
29100+ si_noflush_write_lock(sb);
29101+ err = au_plink_maint(sb, flags);
29102+ if (unlikely(err))
29103+ si_write_unlock(sb);
29104+
29105+ return err;
29106+}
29107+
1facf9fc 29108+/* dentry and super_block lock. call at entry point */
e49829fe 29109+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 29110+{
e49829fe 29111+ int err;
027c5e7a 29112+ struct super_block *sb;
e49829fe 29113+
027c5e7a
AM
29114+ sb = dentry->d_sb;
29115+ err = si_read_lock(sb, flags);
29116+ if (unlikely(err))
29117+ goto out;
29118+
29119+ if (au_ftest_lock(flags, DW))
29120+ di_write_lock_child(dentry);
29121+ else
29122+ di_read_lock_child(dentry, flags);
29123+
29124+ if (au_ftest_lock(flags, GEN)) {
29125+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
29126+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29127+ AuDebugOn(!err && au_dbrange_test(dentry));
29128+ else if (!err)
29129+ err = au_dbrange_test(dentry);
027c5e7a
AM
29130+ if (unlikely(err))
29131+ aufs_read_unlock(dentry, flags);
e49829fe
JR
29132+ }
29133+
027c5e7a 29134+out:
e49829fe 29135+ return err;
1facf9fc 29136+}
29137+
29138+void aufs_read_unlock(struct dentry *dentry, int flags)
29139+{
29140+ if (au_ftest_lock(flags, DW))
29141+ di_write_unlock(dentry);
29142+ else
29143+ di_read_unlock(dentry, flags);
29144+ si_read_unlock(dentry->d_sb);
29145+}
29146+
29147+void aufs_write_lock(struct dentry *dentry)
29148+{
e49829fe 29149+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29150+ di_write_lock_child(dentry);
29151+}
29152+
29153+void aufs_write_unlock(struct dentry *dentry)
29154+{
29155+ di_write_unlock(dentry);
29156+ si_write_unlock(dentry->d_sb);
29157+}
29158+
e49829fe 29159+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29160+{
e49829fe 29161+ int err;
027c5e7a
AM
29162+ unsigned int sigen;
29163+ struct super_block *sb;
e49829fe 29164+
027c5e7a
AM
29165+ sb = d1->d_sb;
29166+ err = si_read_lock(sb, flags);
29167+ if (unlikely(err))
29168+ goto out;
29169+
b95c5147 29170+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29171+
29172+ if (au_ftest_lock(flags, GEN)) {
29173+ sigen = au_sigen(sb);
29174+ err = au_digen_test(d1, sigen);
29175+ AuDebugOn(!err && au_dbrange_test(d1));
29176+ if (!err) {
29177+ err = au_digen_test(d2, sigen);
29178+ AuDebugOn(!err && au_dbrange_test(d2));
29179+ }
29180+ if (unlikely(err))
29181+ aufs_read_and_write_unlock2(d1, d2);
29182+ }
29183+
29184+out:
e49829fe 29185+ return err;
1facf9fc 29186+}
29187+
29188+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29189+{
29190+ di_write_unlock2(d1, d2);
29191+ si_read_unlock(d1->d_sb);
29192+}
7f207e10
AM
29193diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29194--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
29195+++ linux/fs/aufs/super.c 2018-07-19 09:46:13.063314675 +0200
29196@@ -0,0 +1,1043 @@
29197+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 29198+/*
b00004a5 29199+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 29200+ *
29201+ * This program, aufs is free software; you can redistribute it and/or modify
29202+ * it under the terms of the GNU General Public License as published by
29203+ * the Free Software Foundation; either version 2 of the License, or
29204+ * (at your option) any later version.
dece6358
AM
29205+ *
29206+ * This program is distributed in the hope that it will be useful,
29207+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29208+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29209+ * GNU General Public License for more details.
29210+ *
29211+ * You should have received a copy of the GNU General Public License
523b37e3 29212+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29213+ */
29214+
29215+/*
29216+ * mount and super_block operations
29217+ */
29218+
f6c5ef8b 29219+#include <linux/mm.h>
1facf9fc 29220+#include <linux/seq_file.h>
29221+#include <linux/statfs.h>
7f207e10 29222+#include <linux/vmalloc.h>
1facf9fc 29223+#include "aufs.h"
29224+
29225+/*
29226+ * super_operations
29227+ */
29228+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29229+{
29230+ struct au_icntnr *c;
29231+
29232+ c = au_cache_alloc_icntnr();
29233+ if (c) {
027c5e7a 29234+ au_icntnr_init(c);
be118d29 29235+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29236+ c->iinfo.ii_hinode = NULL;
29237+ return &c->vfs_inode;
29238+ }
29239+ return NULL;
29240+}
29241+
027c5e7a
AM
29242+static void aufs_destroy_inode_cb(struct rcu_head *head)
29243+{
29244+ struct inode *inode = container_of(head, struct inode, i_rcu);
29245+
1c60b727 29246+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
027c5e7a
AM
29247+}
29248+
1facf9fc 29249+static void aufs_destroy_inode(struct inode *inode)
29250+{
5afbbe0d
AM
29251+ if (!au_is_bad_inode(inode))
29252+ au_iinfo_fin(inode);
027c5e7a 29253+ call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
1facf9fc 29254+}
29255+
29256+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29257+{
29258+ struct inode *inode;
29259+ int err;
29260+
29261+ inode = iget_locked(sb, ino);
29262+ if (unlikely(!inode)) {
29263+ inode = ERR_PTR(-ENOMEM);
29264+ goto out;
29265+ }
29266+ if (!(inode->i_state & I_NEW))
29267+ goto out;
29268+
29269+ err = au_xigen_new(inode);
29270+ if (!err)
29271+ err = au_iinfo_init(inode);
29272+ if (!err)
be118d29 29273+ inode_inc_iversion(inode);
1facf9fc 29274+ else {
29275+ iget_failed(inode);
29276+ inode = ERR_PTR(err);
29277+ }
29278+
4f0767ce 29279+out:
1facf9fc 29280+ /* never return NULL */
29281+ AuDebugOn(!inode);
29282+ AuTraceErrPtr(inode);
29283+ return inode;
29284+}
29285+
29286+/* lock free root dinfo */
29287+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29288+{
29289+ int err;
5afbbe0d 29290+ aufs_bindex_t bindex, bbot;
1facf9fc 29291+ struct path path;
4a4d8108 29292+ struct au_hdentry *hdp;
1facf9fc 29293+ struct au_branch *br;
076b876e 29294+ au_br_perm_str_t perm;
1facf9fc 29295+
29296+ err = 0;
5afbbe0d
AM
29297+ bbot = au_sbbot(sb);
29298+ bindex = 0;
29299+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29300+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29301+ br = au_sbr(sb, bindex);
86dc4139 29302+ path.mnt = au_br_mnt(br);
5afbbe0d 29303+ path.dentry = hdp->hd_dentry;
1facf9fc 29304+ err = au_seq_path(seq, &path);
79b8bda9 29305+ if (!err) {
076b876e 29306+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29307+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29308+ if (bindex != bbot)
79b8bda9 29309+ seq_putc(seq, ':');
1e00d052 29310+ }
1facf9fc 29311+ }
79b8bda9
AM
29312+ if (unlikely(err || seq_has_overflowed(seq)))
29313+ err = -E2BIG;
1facf9fc 29314+
29315+ return err;
29316+}
29317+
f2c43d5f
AM
29318+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29319+ const char *append)
29320+{
29321+ char *p;
29322+
29323+ p = fmt;
29324+ while (*pat != ':')
29325+ *p++ = *pat++;
29326+ *p++ = *pat++;
29327+ strcpy(p, append);
29328+ AuDebugOn(strlen(fmt) >= len);
29329+}
29330+
1facf9fc 29331+static void au_show_wbr_create(struct seq_file *m, int v,
29332+ struct au_sbinfo *sbinfo)
29333+{
29334+ const char *pat;
f2c43d5f
AM
29335+ char fmt[32];
29336+ struct au_wbr_mfs *mfs;
1facf9fc 29337+
dece6358
AM
29338+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29339+
c2b27bf2 29340+ seq_puts(m, ",create=");
1facf9fc 29341+ pat = au_optstr_wbr_create(v);
f2c43d5f 29342+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29343+ switch (v) {
29344+ case AuWbrCreate_TDP:
29345+ case AuWbrCreate_RR:
29346+ case AuWbrCreate_MFS:
29347+ case AuWbrCreate_PMFS:
c2b27bf2 29348+ seq_puts(m, pat);
1facf9fc 29349+ break;
f2c43d5f
AM
29350+ case AuWbrCreate_MFSRR:
29351+ case AuWbrCreate_TDMFS:
29352+ case AuWbrCreate_PMFSRR:
29353+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29354+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29355+ break;
f2c43d5f 29356+ case AuWbrCreate_MFSV:
1facf9fc 29357+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29358+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29359+ seq_printf(m, fmt,
29360+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29361+ / MSEC_PER_SEC);
1facf9fc 29362+ break;
1facf9fc 29363+ case AuWbrCreate_MFSRRV:
f2c43d5f 29364+ case AuWbrCreate_TDMFSV:
392086de 29365+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29366+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29367+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29368+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29369+ break;
f2c43d5f
AM
29370+ default:
29371+ BUG();
1facf9fc 29372+ }
29373+}
29374+
7eafdf33 29375+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29376+{
29377+#ifdef CONFIG_SYSFS
29378+ return 0;
29379+#else
29380+ int err;
29381+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29382+ aufs_bindex_t bindex, brid;
1facf9fc 29383+ struct qstr *name;
29384+ struct file *f;
29385+ struct dentry *d, *h_root;
29386+
dece6358
AM
29387+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29388+
1facf9fc 29389+ err = 0;
1facf9fc 29390+ f = au_sbi(sb)->si_xib;
29391+ if (!f)
29392+ goto out;
29393+
29394+ /* stop printing the default xino path on the first writable branch */
29395+ h_root = NULL;
29396+ brid = au_xino_brid(sb);
29397+ if (brid >= 0) {
29398+ bindex = au_br_index(sb, brid);
5afbbe0d 29399+ h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
1facf9fc 29400+ }
2000de60 29401+ d = f->f_path.dentry;
1facf9fc 29402+ name = &d->d_name;
29403+ /* safe ->d_parent because the file is unlinked */
29404+ if (d->d_parent == h_root
29405+ && name->len == len
29406+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29407+ goto out;
29408+
29409+ seq_puts(seq, ",xino=");
29410+ err = au_xino_path(seq, f);
29411+
4f0767ce 29412+out:
1facf9fc 29413+ return err;
29414+#endif
29415+}
29416+
29417+/* seq_file will re-call me in case of too long string */
7eafdf33 29418+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29419+{
027c5e7a 29420+ int err;
1facf9fc 29421+ unsigned int mnt_flags, v;
29422+ struct super_block *sb;
29423+ struct au_sbinfo *sbinfo;
29424+
29425+#define AuBool(name, str) do { \
29426+ v = au_opt_test(mnt_flags, name); \
29427+ if (v != au_opt_test(AuOpt_Def, name)) \
29428+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29429+} while (0)
29430+
29431+#define AuStr(name, str) do { \
29432+ v = mnt_flags & AuOptMask_##name; \
29433+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29434+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29435+} while (0)
29436+
29437+#define AuUInt(name, str, val) do { \
29438+ if (val != AUFS_##name##_DEF) \
29439+ seq_printf(m, "," #str "=%u", val); \
29440+} while (0)
29441+
7eafdf33 29442+ sb = dentry->d_sb;
2121bcd9 29443+ if (sb->s_flags & SB_POSIXACL)
c1595e42 29444+ seq_puts(m, ",acl");
be118d29
JR
29445+#if 0
29446+ if (sb->s_flags & SB_I_VERSION)
29447+ seq_puts(m, ",i_version");
29448+#endif
c1595e42
JR
29449+
29450+ /* lock free root dinfo */
1facf9fc 29451+ si_noflush_read_lock(sb);
29452+ sbinfo = au_sbi(sb);
29453+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29454+
29455+ mnt_flags = au_mntflags(sb);
29456+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29457+ err = au_show_xino(m, sb);
1facf9fc 29458+ if (unlikely(err))
29459+ goto out;
29460+ } else
29461+ seq_puts(m, ",noxino");
29462+
29463+ AuBool(TRUNC_XINO, trunc_xino);
29464+ AuStr(UDBA, udba);
dece6358 29465+ AuBool(SHWH, shwh);
1facf9fc 29466+ AuBool(PLINK, plink);
4a4d8108 29467+ AuBool(DIO, dio);
076b876e 29468+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29469+
29470+ v = sbinfo->si_wbr_create;
29471+ if (v != AuWbrCreate_Def)
29472+ au_show_wbr_create(m, v, sbinfo);
29473+
29474+ v = sbinfo->si_wbr_copyup;
29475+ if (v != AuWbrCopyup_Def)
29476+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29477+
29478+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29479+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29480+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29481+
29482+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29483+
027c5e7a
AM
29484+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29485+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29486+
29487+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29488+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29489+
076b876e
AM
29490+ au_fhsm_show(m, sbinfo);
29491+
8b6a4947 29492+ AuBool(DIRREN, dirren);
1facf9fc 29493+ AuBool(SUM, sum);
29494+ /* AuBool(SUM_W, wsum); */
29495+ AuBool(WARN_PERM, warn_perm);
29496+ AuBool(VERBOSE, verbose);
29497+
4f0767ce 29498+out:
1facf9fc 29499+ /* be sure to print "br:" last */
29500+ if (!sysaufs_brs) {
29501+ seq_puts(m, ",br:");
29502+ au_show_brs(m, sb);
29503+ }
29504+ si_read_unlock(sb);
29505+ return 0;
29506+
1facf9fc 29507+#undef AuBool
29508+#undef AuStr
4a4d8108 29509+#undef AuUInt
1facf9fc 29510+}
29511+
29512+/* ---------------------------------------------------------------------- */
29513+
29514+/* sum mode which returns the summation for statfs(2) */
29515+
29516+static u64 au_add_till_max(u64 a, u64 b)
29517+{
29518+ u64 old;
29519+
29520+ old = a;
29521+ a += b;
92d182d2
AM
29522+ if (old <= a)
29523+ return a;
29524+ return ULLONG_MAX;
29525+}
29526+
29527+static u64 au_mul_till_max(u64 a, long mul)
29528+{
29529+ u64 old;
29530+
29531+ old = a;
29532+ a *= mul;
29533+ if (old <= a)
1facf9fc 29534+ return a;
29535+ return ULLONG_MAX;
29536+}
29537+
29538+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29539+{
29540+ int err;
92d182d2 29541+ long bsize, factor;
1facf9fc 29542+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29543+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29544+ unsigned char shared;
7f207e10 29545+ struct path h_path;
1facf9fc 29546+ struct super_block *h_sb;
29547+
92d182d2
AM
29548+ err = 0;
29549+ bsize = LONG_MAX;
29550+ files = 0;
29551+ ffree = 0;
1facf9fc 29552+ blocks = 0;
29553+ bfree = 0;
29554+ bavail = 0;
5afbbe0d
AM
29555+ bbot = au_sbbot(sb);
29556+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29557+ h_path.mnt = au_sbr_mnt(sb, bindex);
29558+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29559+ shared = 0;
92d182d2 29560+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29561+ shared = (au_sbr_sb(sb, i) == h_sb);
29562+ if (shared)
29563+ continue;
29564+
29565+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29566+ h_path.dentry = h_path.mnt->mnt_root;
29567+ err = vfs_statfs(&h_path, buf);
1facf9fc 29568+ if (unlikely(err))
29569+ goto out;
29570+
92d182d2
AM
29571+ if (bsize > buf->f_bsize) {
29572+ /*
29573+ * we will reduce bsize, so we have to expand blocks
29574+ * etc. to match them again
29575+ */
29576+ factor = (bsize / buf->f_bsize);
29577+ blocks = au_mul_till_max(blocks, factor);
29578+ bfree = au_mul_till_max(bfree, factor);
29579+ bavail = au_mul_till_max(bavail, factor);
29580+ bsize = buf->f_bsize;
29581+ }
29582+
29583+ factor = (buf->f_bsize / bsize);
29584+ blocks = au_add_till_max(blocks,
29585+ au_mul_till_max(buf->f_blocks, factor));
29586+ bfree = au_add_till_max(bfree,
29587+ au_mul_till_max(buf->f_bfree, factor));
29588+ bavail = au_add_till_max(bavail,
29589+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29590+ files = au_add_till_max(files, buf->f_files);
29591+ ffree = au_add_till_max(ffree, buf->f_ffree);
29592+ }
29593+
92d182d2 29594+ buf->f_bsize = bsize;
1facf9fc 29595+ buf->f_blocks = blocks;
29596+ buf->f_bfree = bfree;
29597+ buf->f_bavail = bavail;
29598+ buf->f_files = files;
29599+ buf->f_ffree = ffree;
92d182d2 29600+ buf->f_frsize = 0;
1facf9fc 29601+
4f0767ce 29602+out:
1facf9fc 29603+ return err;
29604+}
29605+
29606+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29607+{
29608+ int err;
7f207e10 29609+ struct path h_path;
1facf9fc 29610+ struct super_block *sb;
29611+
29612+ /* lock free root dinfo */
29613+ sb = dentry->d_sb;
29614+ si_noflush_read_lock(sb);
7f207e10 29615+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29616+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29617+ h_path.mnt = au_sbr_mnt(sb, 0);
29618+ h_path.dentry = h_path.mnt->mnt_root;
29619+ err = vfs_statfs(&h_path, buf);
29620+ } else
1facf9fc 29621+ err = au_statfs_sum(sb, buf);
29622+ si_read_unlock(sb);
29623+
29624+ if (!err) {
29625+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29626+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29627+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29628+ }
29629+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29630+
29631+ return err;
29632+}
29633+
29634+/* ---------------------------------------------------------------------- */
29635+
537831f9
AM
29636+static int aufs_sync_fs(struct super_block *sb, int wait)
29637+{
29638+ int err, e;
5afbbe0d 29639+ aufs_bindex_t bbot, bindex;
537831f9
AM
29640+ struct au_branch *br;
29641+ struct super_block *h_sb;
29642+
29643+ err = 0;
29644+ si_noflush_read_lock(sb);
5afbbe0d
AM
29645+ bbot = au_sbbot(sb);
29646+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29647+ br = au_sbr(sb, bindex);
29648+ if (!au_br_writable(br->br_perm))
29649+ continue;
29650+
29651+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29652+ e = vfsub_sync_filesystem(h_sb, wait);
29653+ if (unlikely(e && !err))
29654+ err = e;
29655+ /* go on even if an error happens */
537831f9
AM
29656+ }
29657+ si_read_unlock(sb);
29658+
29659+ return err;
29660+}
29661+
29662+/* ---------------------------------------------------------------------- */
29663+
1facf9fc 29664+/* final actions when unmounting a file system */
29665+static void aufs_put_super(struct super_block *sb)
29666+{
29667+ struct au_sbinfo *sbinfo;
29668+
29669+ sbinfo = au_sbi(sb);
062440b3
AM
29670+ if (sbinfo)
29671+ kobject_put(&sbinfo->si_kobj);
1facf9fc 29672+}
29673+
29674+/* ---------------------------------------------------------------------- */
29675+
79b8bda9
AM
29676+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29677+ struct super_block *sb, void *arg)
7f207e10
AM
29678+{
29679+ void *array;
076b876e 29680+ unsigned long long n, sz;
7f207e10
AM
29681+
29682+ array = NULL;
29683+ n = 0;
29684+ if (!*hint)
29685+ goto out;
29686+
29687+ if (*hint > ULLONG_MAX / sizeof(array)) {
29688+ array = ERR_PTR(-EMFILE);
29689+ pr_err("hint %llu\n", *hint);
29690+ goto out;
29691+ }
29692+
076b876e
AM
29693+ sz = sizeof(array) * *hint;
29694+ array = kzalloc(sz, GFP_NOFS);
7f207e10 29695+ if (unlikely(!array))
076b876e 29696+ array = vzalloc(sz);
7f207e10
AM
29697+ if (unlikely(!array)) {
29698+ array = ERR_PTR(-ENOMEM);
29699+ goto out;
29700+ }
29701+
79b8bda9 29702+ n = cb(sb, array, *hint, arg);
7f207e10
AM
29703+ AuDebugOn(n > *hint);
29704+
29705+out:
29706+ *hint = n;
29707+ return array;
29708+}
29709+
79b8bda9 29710+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
29711+ unsigned long long max __maybe_unused,
29712+ void *arg)
29713+{
29714+ unsigned long long n;
29715+ struct inode **p, *inode;
29716+ struct list_head *head;
29717+
29718+ n = 0;
29719+ p = a;
29720+ head = arg;
79b8bda9 29721+ spin_lock(&sb->s_inode_list_lock);
7f207e10 29722+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
29723+ if (!au_is_bad_inode(inode)
29724+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
29725+ spin_lock(&inode->i_lock);
29726+ if (atomic_read(&inode->i_count)) {
29727+ au_igrab(inode);
29728+ *p++ = inode;
29729+ n++;
29730+ AuDebugOn(n > max);
29731+ }
29732+ spin_unlock(&inode->i_lock);
7f207e10
AM
29733+ }
29734+ }
79b8bda9 29735+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
29736+
29737+ return n;
29738+}
29739+
29740+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29741+{
5afbbe0d 29742+ *max = au_ninodes(sb);
79b8bda9 29743+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
29744+}
29745+
29746+void au_iarray_free(struct inode **a, unsigned long long max)
29747+{
29748+ unsigned long long ull;
29749+
29750+ for (ull = 0; ull < max; ull++)
29751+ iput(a[ull]);
be52b249 29752+ kvfree(a);
7f207e10
AM
29753+}
29754+
29755+/* ---------------------------------------------------------------------- */
29756+
1facf9fc 29757+/*
29758+ * refresh dentry and inode at remount time.
29759+ */
027c5e7a
AM
29760+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29761+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29762+ struct dentry *parent)
1facf9fc 29763+{
29764+ int err;
1facf9fc 29765+
29766+ di_write_lock_child(dentry);
1facf9fc 29767+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
29768+ err = au_refresh_dentry(dentry, parent);
29769+ if (!err && dir_flags)
5527c038 29770+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 29771+ di_read_unlock(parent, AuLock_IR);
1facf9fc 29772+ di_write_unlock(dentry);
29773+
29774+ return err;
29775+}
29776+
027c5e7a
AM
29777+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29778+ struct au_sbinfo *sbinfo,
b95c5147 29779+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 29780+{
027c5e7a
AM
29781+ int err;
29782+ struct dentry *parent;
027c5e7a
AM
29783+
29784+ err = 0;
29785+ parent = dget_parent(dentry);
29786+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
29787+ if (d_really_is_positive(dentry)) {
29788+ if (!d_is_dir(dentry))
027c5e7a
AM
29789+ err = au_do_refresh(dentry, /*dir_flags*/0,
29790+ parent);
29791+ else {
29792+ err = au_do_refresh(dentry, dir_flags, parent);
29793+ if (unlikely(err))
29794+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29795+ }
29796+ } else
29797+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29798+ AuDbgDentry(dentry);
29799+ }
29800+ dput(parent);
29801+
79b8bda9 29802+ if (!err) {
b95c5147 29803+ if (do_idop)
79b8bda9
AM
29804+ au_refresh_dop(dentry, /*force_reval*/0);
29805+ } else
29806+ au_refresh_dop(dentry, /*force_reval*/1);
29807+
027c5e7a
AM
29808+ AuTraceErr(err);
29809+ return err;
1facf9fc 29810+}
29811+
b95c5147 29812+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 29813+{
29814+ int err, i, j, ndentry, e;
027c5e7a 29815+ unsigned int sigen;
1facf9fc 29816+ struct au_dcsub_pages dpages;
29817+ struct au_dpage *dpage;
027c5e7a
AM
29818+ struct dentry **dentries, *d;
29819+ struct au_sbinfo *sbinfo;
29820+ struct dentry *root = sb->s_root;
5527c038 29821+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 29822+
b95c5147 29823+ if (do_idop)
79b8bda9
AM
29824+ au_refresh_dop(root, /*force_reval*/0);
29825+
027c5e7a
AM
29826+ err = au_dpages_init(&dpages, GFP_NOFS);
29827+ if (unlikely(err))
1facf9fc 29828+ goto out;
027c5e7a
AM
29829+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
29830+ if (unlikely(err))
1facf9fc 29831+ goto out_dpages;
1facf9fc 29832+
027c5e7a
AM
29833+ sigen = au_sigen(sb);
29834+ sbinfo = au_sbi(sb);
29835+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 29836+ dpage = dpages.dpages + i;
29837+ dentries = dpage->dentries;
29838+ ndentry = dpage->ndentry;
027c5e7a 29839+ for (j = 0; j < ndentry; j++) {
1facf9fc 29840+ d = dentries[j];
79b8bda9 29841+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 29842+ do_idop);
027c5e7a
AM
29843+ if (unlikely(e && !err))
29844+ err = e;
29845+ /* go on even err */
1facf9fc 29846+ }
29847+ }
29848+
4f0767ce 29849+out_dpages:
1facf9fc 29850+ au_dpages_free(&dpages);
4f0767ce 29851+out:
1facf9fc 29852+ return err;
29853+}
29854+
b95c5147 29855+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 29856+{
027c5e7a
AM
29857+ int err, e;
29858+ unsigned int sigen;
29859+ unsigned long long max, ull;
29860+ struct inode *inode, **array;
1facf9fc 29861+
027c5e7a
AM
29862+ array = au_iarray_alloc(sb, &max);
29863+ err = PTR_ERR(array);
29864+ if (IS_ERR(array))
29865+ goto out;
1facf9fc 29866+
29867+ err = 0;
027c5e7a
AM
29868+ sigen = au_sigen(sb);
29869+ for (ull = 0; ull < max; ull++) {
29870+ inode = array[ull];
076b876e
AM
29871+ if (unlikely(!inode))
29872+ break;
b95c5147
AM
29873+
29874+ e = 0;
29875+ ii_write_lock_child(inode);
537831f9 29876+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 29877+ e = au_refresh_hinode_self(inode);
1facf9fc 29878+ if (unlikely(e)) {
b95c5147 29879+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 29880+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 29881+ if (!err)
29882+ err = e;
29883+ /* go on even if err */
29884+ }
29885+ }
b95c5147
AM
29886+ if (!e && do_idop)
29887+ au_refresh_iop(inode, /*force_getattr*/0);
29888+ ii_write_unlock(inode);
1facf9fc 29889+ }
29890+
027c5e7a 29891+ au_iarray_free(array, max);
1facf9fc 29892+
4f0767ce 29893+out:
1facf9fc 29894+ return err;
29895+}
29896+
b95c5147 29897+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 29898+{
027c5e7a
AM
29899+ int err, e;
29900+ unsigned int udba;
5afbbe0d 29901+ aufs_bindex_t bindex, bbot;
1facf9fc 29902+ struct dentry *root;
29903+ struct inode *inode;
027c5e7a 29904+ struct au_branch *br;
79b8bda9 29905+ struct au_sbinfo *sbi;
1facf9fc 29906+
29907+ au_sigen_inc(sb);
79b8bda9
AM
29908+ sbi = au_sbi(sb);
29909+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 29910+
29911+ root = sb->s_root;
29912+ DiMustNoWaiters(root);
5527c038 29913+ inode = d_inode(root);
1facf9fc 29914+ IiMustNoWaiters(inode);
1facf9fc 29915+
027c5e7a 29916+ udba = au_opt_udba(sb);
5afbbe0d
AM
29917+ bbot = au_sbbot(sb);
29918+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
29919+ br = au_sbr(sb, bindex);
29920+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 29921+ if (unlikely(err))
027c5e7a
AM
29922+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
29923+ bindex, err);
29924+ /* go on even if err */
1facf9fc 29925+ }
027c5e7a 29926+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 29927+
b95c5147 29928+ if (do_idop) {
79b8bda9
AM
29929+ if (au_ftest_si(sbi, NO_DREVAL)) {
29930+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
29931+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
29932+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
29933+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
29934+ } else {
29935+ AuDebugOn(sb->s_d_op == &aufs_dop);
29936+ sb->s_d_op = &aufs_dop;
b95c5147
AM
29937+ AuDebugOn(sbi->si_iop_array == aufs_iop);
29938+ sbi->si_iop_array = aufs_iop;
79b8bda9 29939+ }
062440b3 29940+ pr_info("reset to %ps and %ps\n",
b95c5147 29941+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
29942+ }
29943+
027c5e7a 29944+ di_write_unlock(root);
b95c5147
AM
29945+ err = au_refresh_d(sb, do_idop);
29946+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
29947+ if (unlikely(e && !err))
29948+ err = e;
1facf9fc 29949+ /* aufs_write_lock() calls ..._child() */
29950+ di_write_lock_child(root);
027c5e7a
AM
29951+
29952+ au_cpup_attr_all(inode, /*force*/1);
29953+
29954+ if (unlikely(err))
29955+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 29956+}
29957+
29958+/* stop extra interpretation of errno in mount(8), and strange error messages */
29959+static int cvt_err(int err)
29960+{
29961+ AuTraceErr(err);
29962+
29963+ switch (err) {
29964+ case -ENOENT:
29965+ case -ENOTDIR:
29966+ case -EEXIST:
29967+ case -EIO:
29968+ err = -EINVAL;
29969+ }
29970+ return err;
29971+}
29972+
29973+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
29974+{
4a4d8108
AM
29975+ int err, do_dx;
29976+ unsigned int mntflags;
be52b249
AM
29977+ struct au_opts opts = {
29978+ .opt = NULL
29979+ };
1facf9fc 29980+ struct dentry *root;
29981+ struct inode *inode;
29982+ struct au_sbinfo *sbinfo;
29983+
29984+ err = 0;
29985+ root = sb->s_root;
29986+ if (!data || !*data) {
e49829fe
JR
29987+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
29988+ if (!err) {
29989+ di_write_lock_child(root);
29990+ err = au_opts_verify(sb, *flags, /*pending*/0);
29991+ aufs_write_unlock(root);
29992+ }
1facf9fc 29993+ goto out;
29994+ }
29995+
29996+ err = -ENOMEM;
1facf9fc 29997+ opts.opt = (void *)__get_free_page(GFP_NOFS);
29998+ if (unlikely(!opts.opt))
29999+ goto out;
30000+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30001+ opts.flags = AuOpts_REMOUNT;
30002+ opts.sb_flags = *flags;
30003+
30004+ /* parse it before aufs lock */
30005+ err = au_opts_parse(sb, data, &opts);
30006+ if (unlikely(err))
30007+ goto out_opts;
30008+
30009+ sbinfo = au_sbi(sb);
5527c038 30010+ inode = d_inode(root);
febd17d6 30011+ inode_lock(inode);
e49829fe
JR
30012+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30013+ if (unlikely(err))
30014+ goto out_mtx;
30015+ di_write_lock_child(root);
1facf9fc 30016+
30017+ /* au_opts_remount() may return an error */
30018+ err = au_opts_remount(sb, &opts);
30019+ au_opts_free(&opts);
30020+
027c5e7a 30021+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 30022+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 30023+
4a4d8108
AM
30024+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30025+ mntflags = au_mntflags(sb);
30026+ do_dx = !!au_opt_test(mntflags, DIO);
30027+ au_dy_arefresh(do_dx);
30028+ }
30029+
076b876e 30030+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 30031+ aufs_write_unlock(root);
953406b4 30032+
e49829fe 30033+out_mtx:
febd17d6 30034+ inode_unlock(inode);
4f0767ce 30035+out_opts:
1c60b727 30036+ free_page((unsigned long)opts.opt);
4f0767ce 30037+out:
1facf9fc 30038+ err = cvt_err(err);
30039+ AuTraceErr(err);
30040+ return err;
30041+}
30042+
4a4d8108 30043+static const struct super_operations aufs_sop = {
1facf9fc 30044+ .alloc_inode = aufs_alloc_inode,
30045+ .destroy_inode = aufs_destroy_inode,
b752ccd1 30046+ /* always deleting, no clearing */
1facf9fc 30047+ .drop_inode = generic_delete_inode,
30048+ .show_options = aufs_show_options,
30049+ .statfs = aufs_statfs,
30050+ .put_super = aufs_put_super,
537831f9 30051+ .sync_fs = aufs_sync_fs,
1facf9fc 30052+ .remount_fs = aufs_remount_fs
30053+};
30054+
30055+/* ---------------------------------------------------------------------- */
30056+
30057+static int alloc_root(struct super_block *sb)
30058+{
30059+ int err;
30060+ struct inode *inode;
30061+ struct dentry *root;
30062+
30063+ err = -ENOMEM;
30064+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30065+ err = PTR_ERR(inode);
30066+ if (IS_ERR(inode))
30067+ goto out;
30068+
b95c5147 30069+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 30070+ inode->i_fop = &aufs_dir_fop;
30071+ inode->i_mode = S_IFDIR;
9dbd164d 30072+ set_nlink(inode, 2);
1facf9fc 30073+ unlock_new_inode(inode);
30074+
92d182d2 30075+ root = d_make_root(inode);
1facf9fc 30076+ if (unlikely(!root))
92d182d2 30077+ goto out;
1facf9fc 30078+ err = PTR_ERR(root);
30079+ if (IS_ERR(root))
92d182d2 30080+ goto out;
1facf9fc 30081+
4a4d8108 30082+ err = au_di_init(root);
1facf9fc 30083+ if (!err) {
30084+ sb->s_root = root;
30085+ return 0; /* success */
30086+ }
30087+ dput(root);
1facf9fc 30088+
4f0767ce 30089+out:
1facf9fc 30090+ return err;
1facf9fc 30091+}
30092+
30093+static int aufs_fill_super(struct super_block *sb, void *raw_data,
30094+ int silent __maybe_unused)
30095+{
30096+ int err;
be52b249
AM
30097+ struct au_opts opts = {
30098+ .opt = NULL
30099+ };
79b8bda9 30100+ struct au_sbinfo *sbinfo;
1facf9fc 30101+ struct dentry *root;
30102+ struct inode *inode;
30103+ char *arg = raw_data;
30104+
30105+ if (unlikely(!arg || !*arg)) {
30106+ err = -EINVAL;
4a4d8108 30107+ pr_err("no arg\n");
1facf9fc 30108+ goto out;
30109+ }
30110+
30111+ err = -ENOMEM;
1facf9fc 30112+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30113+ if (unlikely(!opts.opt))
30114+ goto out;
30115+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30116+ opts.sb_flags = sb->s_flags;
30117+
30118+ err = au_si_alloc(sb);
30119+ if (unlikely(err))
30120+ goto out_opts;
79b8bda9 30121+ sbinfo = au_sbi(sb);
1facf9fc 30122+
30123+ /* all timestamps always follow the ones on the branch */
2121bcd9 30124+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
be118d29 30125+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
1facf9fc 30126+ sb->s_op = &aufs_sop;
027c5e7a 30127+ sb->s_d_op = &aufs_dop;
1facf9fc 30128+ sb->s_magic = AUFS_SUPER_MAGIC;
30129+ sb->s_maxbytes = 0;
c1595e42 30130+ sb->s_stack_depth = 1;
1facf9fc 30131+ au_export_init(sb);
f2c43d5f 30132+ au_xattr_init(sb);
1facf9fc 30133+
30134+ err = alloc_root(sb);
30135+ if (unlikely(err)) {
30136+ si_write_unlock(sb);
30137+ goto out_info;
30138+ }
30139+ root = sb->s_root;
5527c038 30140+ inode = d_inode(root);
1facf9fc 30141+
30142+ /*
30143+ * actually we can parse options regardless aufs lock here.
30144+ * but at remount time, parsing must be done before aufs lock.
30145+ * so we follow the same rule.
30146+ */
30147+ ii_write_lock_parent(inode);
30148+ aufs_write_unlock(root);
30149+ err = au_opts_parse(sb, arg, &opts);
30150+ if (unlikely(err))
30151+ goto out_root;
30152+
30153+ /* lock vfs_inode first, then aufs. */
febd17d6 30154+ inode_lock(inode);
1facf9fc 30155+ aufs_write_lock(root);
30156+ err = au_opts_mount(sb, &opts);
30157+ au_opts_free(&opts);
79b8bda9
AM
30158+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30159+ sb->s_d_op = &aufs_dop_noreval;
062440b3 30160+ pr_info("%ps\n", sb->s_d_op);
79b8bda9 30161+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30162+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30163+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30164+ }
1facf9fc 30165+ aufs_write_unlock(root);
febd17d6 30166+ inode_unlock(inode);
4a4d8108
AM
30167+ if (!err)
30168+ goto out_opts; /* success */
1facf9fc 30169+
4f0767ce 30170+out_root:
1facf9fc 30171+ dput(root);
30172+ sb->s_root = NULL;
4f0767ce 30173+out_info:
79b8bda9 30174+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30175+ sb->s_fs_info = NULL;
4f0767ce 30176+out_opts:
1c60b727 30177+ free_page((unsigned long)opts.opt);
4f0767ce 30178+out:
1facf9fc 30179+ AuTraceErr(err);
30180+ err = cvt_err(err);
30181+ AuTraceErr(err);
30182+ return err;
30183+}
30184+
30185+/* ---------------------------------------------------------------------- */
30186+
027c5e7a
AM
30187+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30188+ const char *dev_name __maybe_unused,
30189+ void *raw_data)
1facf9fc 30190+{
027c5e7a 30191+ struct dentry *root;
1facf9fc 30192+
30193+ /* all timestamps always follow the ones on the branch */
30194+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30195+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30196+ if (IS_ERR(root))
30197+ goto out;
30198+
062440b3 30199+ au_sbilist_add(root->d_sb);
027c5e7a
AM
30200+
30201+out:
30202+ return root;
1facf9fc 30203+}
30204+
e49829fe
JR
30205+static void aufs_kill_sb(struct super_block *sb)
30206+{
30207+ struct au_sbinfo *sbinfo;
30208+
30209+ sbinfo = au_sbi(sb);
30210+ if (sbinfo) {
30211+ au_sbilist_del(sb);
30212+ aufs_write_lock(sb->s_root);
076b876e 30213+ au_fhsm_fin(sb);
e49829fe
JR
30214+ if (sbinfo->si_wbr_create_ops->fin)
30215+ sbinfo->si_wbr_create_ops->fin(sb);
30216+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30217+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30218+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30219+ }
30220+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30221+ au_plink_put(sb, /*verbose*/1);
30222+ au_xino_clr(sb);
8b6a4947 30223+ au_dr_opt_flush(sb);
1e00d052 30224+ sbinfo->si_sb = NULL;
e49829fe 30225+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30226+ au_nwt_flush(&sbinfo->si_nowait);
30227+ }
98d9a5b1 30228+ kill_anon_super(sb);
e49829fe
JR
30229+}
30230+
1facf9fc 30231+struct file_system_type aufs_fs_type = {
30232+ .name = AUFS_FSTYPE,
c06a8ce3
AM
30233+ /* a race between rename and others */
30234+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 30235+ .mount = aufs_mount,
e49829fe 30236+ .kill_sb = aufs_kill_sb,
1facf9fc 30237+ /* no need to __module_get() and module_put(). */
30238+ .owner = THIS_MODULE,
30239+};
7f207e10
AM
30240diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30241--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
30242+++ linux/fs/aufs/super.h 2018-07-19 09:46:13.063314675 +0200
30243@@ -0,0 +1,627 @@
30244+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30245+/*
b00004a5 30246+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30247+ *
30248+ * This program, aufs is free software; you can redistribute it and/or modify
30249+ * it under the terms of the GNU General Public License as published by
30250+ * the Free Software Foundation; either version 2 of the License, or
30251+ * (at your option) any later version.
dece6358
AM
30252+ *
30253+ * This program is distributed in the hope that it will be useful,
30254+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30255+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30256+ * GNU General Public License for more details.
30257+ *
30258+ * You should have received a copy of the GNU General Public License
523b37e3 30259+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30260+ */
30261+
30262+/*
30263+ * super_block operations
30264+ */
30265+
30266+#ifndef __AUFS_SUPER_H__
30267+#define __AUFS_SUPER_H__
30268+
30269+#ifdef __KERNEL__
30270+
30271+#include <linux/fs.h>
5527c038 30272+#include <linux/kobject.h>
8b6a4947 30273+#include "hbl.h"
1facf9fc 30274+#include "rwsem.h"
1facf9fc 30275+#include "wkq.h"
30276+
1facf9fc 30277+/* policies to select one among multiple writable branches */
30278+struct au_wbr_copyup_operations {
30279+ int (*copyup)(struct dentry *dentry);
30280+};
30281+
392086de
AM
30282+#define AuWbr_DIR 1 /* target is a dir */
30283+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30284+
30285+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30286+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30287+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30288+
1facf9fc 30289+struct au_wbr_create_operations {
392086de 30290+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30291+ int (*init)(struct super_block *sb);
30292+ int (*fin)(struct super_block *sb);
30293+};
30294+
30295+struct au_wbr_mfs {
30296+ struct mutex mfs_lock; /* protect this structure */
30297+ unsigned long mfs_jiffy;
30298+ unsigned long mfs_expire;
30299+ aufs_bindex_t mfs_bindex;
30300+
30301+ unsigned long long mfsrr_bytes;
30302+ unsigned long long mfsrr_watermark;
30303+};
30304+
86dc4139
AM
30305+#define AuPlink_NHASH 100
30306+static inline int au_plink_hash(ino_t ino)
30307+{
30308+ return ino % AuPlink_NHASH;
30309+}
30310+
076b876e
AM
30311+/* File-based Hierarchical Storage Management */
30312+struct au_fhsm {
30313+#ifdef CONFIG_AUFS_FHSM
30314+ /* allow only one process who can receive the notification */
30315+ spinlock_t fhsm_spin;
30316+ pid_t fhsm_pid;
30317+ wait_queue_head_t fhsm_wqh;
30318+ atomic_t fhsm_readable;
30319+
c1595e42 30320+ /* these are protected by si_rwsem */
076b876e 30321+ unsigned long fhsm_expire;
c1595e42 30322+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30323+#endif
30324+};
30325+
1facf9fc 30326+struct au_branch;
30327+struct au_sbinfo {
30328+ /* nowait tasks in the system-wide workqueue */
30329+ struct au_nowait_tasks si_nowait;
30330+
b752ccd1
AM
30331+ /*
30332+ * tried sb->s_umount, but failed due to the dependecy between i_mutex.
30333+ * rwsem for au_sbinfo is necessary.
30334+ */
dece6358 30335+ struct au_rwsem si_rwsem;
1facf9fc 30336+
7f207e10 30337+ /*
523b37e3
AM
30338+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30339+ * remount.
7f207e10 30340+ */
5afbbe0d 30341+ struct percpu_counter si_ninodes, si_nfiles;
7f207e10 30342+
1facf9fc 30343+ /* branch management */
30344+ unsigned int si_generation;
30345+
2000de60 30346+ /* see AuSi_ flags */
1facf9fc 30347+ unsigned char au_si_status;
30348+
5afbbe0d 30349+ aufs_bindex_t si_bbot;
7f207e10
AM
30350+
30351+ /* dirty trick to keep br_id plus */
30352+ unsigned int si_last_br_id :
30353+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30354+ struct au_branch **si_branch;
30355+
30356+ /* policy to select a writable branch */
30357+ unsigned char si_wbr_copyup;
30358+ unsigned char si_wbr_create;
30359+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30360+ struct au_wbr_create_operations *si_wbr_create_ops;
30361+
30362+ /* round robin */
30363+ atomic_t si_wbr_rr_next;
30364+
30365+ /* most free space */
30366+ struct au_wbr_mfs si_wbr_mfs;
30367+
076b876e
AM
30368+ /* File-based Hierarchical Storage Management */
30369+ struct au_fhsm si_fhsm;
30370+
1facf9fc 30371+ /* mount flags */
30372+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30373+ unsigned int si_mntflags;
30374+
30375+ /* external inode number (bitmap and translation table) */
5527c038
JR
30376+ vfs_readf_t si_xread;
30377+ vfs_writef_t si_xwrite;
1facf9fc 30378+ struct file *si_xib;
30379+ struct mutex si_xib_mtx; /* protect xib members */
30380+ unsigned long *si_xib_buf;
30381+ unsigned long si_xib_last_pindex;
30382+ int si_xib_next_bit;
30383+ aufs_bindex_t si_xino_brid;
392086de
AM
30384+ unsigned long si_xino_jiffy;
30385+ unsigned long si_xino_expire;
1facf9fc 30386+ /* reserved for future use */
30387+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30388+
30389+#ifdef CONFIG_AUFS_EXPORT
30390+ /* i_generation */
30391+ struct file *si_xigen;
30392+ atomic_t si_xigen_next;
30393+#endif
30394+
b912730e 30395+ /* dirty trick to suppoer atomic_open */
8b6a4947 30396+ struct hlist_bl_head si_aopen;
b912730e 30397+
1facf9fc 30398+ /* vdir parameters */
e49829fe 30399+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30400+ unsigned int si_rdblk; /* deblk size */
30401+ unsigned int si_rdhash; /* hash size */
30402+
30403+ /*
30404+ * If the number of whiteouts are larger than si_dirwh, leave all of
30405+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30406+ * future fsck.aufs or kernel thread will remove them later.
30407+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30408+ */
30409+ unsigned int si_dirwh;
30410+
1facf9fc 30411+ /* pseudo_link list */
8b6a4947 30412+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30413+ wait_queue_head_t si_plink_wq;
4a4d8108 30414+ spinlock_t si_plink_maint_lock;
e49829fe 30415+ pid_t si_plink_maint_pid;
1facf9fc 30416+
523b37e3 30417+ /* file list */
8b6a4947 30418+ struct hlist_bl_head si_files;
523b37e3 30419+
b95c5147
AM
30420+ /* with/without getattr, brother of sb->s_d_op */
30421+ struct inode_operations *si_iop_array;
30422+
1facf9fc 30423+ /*
30424+ * sysfs and lifetime management.
30425+ * this is not a small structure and it may be a waste of memory in case
30426+ * of sysfs is disabled, particulary when many aufs-es are mounted.
30427+ * but using sysfs is majority.
30428+ */
30429+ struct kobject si_kobj;
30430+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30431+ struct dentry *si_dbgaufs;
30432+ struct dentry *si_dbgaufs_plink;
30433+ struct dentry *si_dbgaufs_xib;
1facf9fc 30434+#ifdef CONFIG_AUFS_EXPORT
30435+ struct dentry *si_dbgaufs_xigen;
30436+#endif
30437+#endif
30438+
e49829fe 30439+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30440+ struct hlist_bl_node si_list;
e49829fe
JR
30441+#endif
30442+
1facf9fc 30443+ /* dirty, necessary for unmounting, sysfs and sysrq */
30444+ struct super_block *si_sb;
30445+};
30446+
dece6358
AM
30447+/* sbinfo status flags */
30448+/*
30449+ * set true when refresh_dirs() failed at remount time.
30450+ * then try refreshing dirs at access time again.
062440b3 30451+ * if it is false, refreshing dirs at access time is unnecessary
dece6358 30452+ */
027c5e7a 30453+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30454+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30455+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30456+
30457+#ifndef CONFIG_AUFS_FHSM
30458+#undef AuSi_FHSM
30459+#define AuSi_FHSM 0
30460+#endif
30461+
dece6358
AM
30462+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30463+ unsigned int flag)
30464+{
30465+ AuRwMustAnyLock(&sbi->si_rwsem);
30466+ return sbi->au_si_status & flag;
30467+}
30468+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30469+#define au_fset_si(sbinfo, name) do { \
30470+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30471+ (sbinfo)->au_si_status |= AuSi_##name; \
30472+} while (0)
30473+#define au_fclr_si(sbinfo, name) do { \
30474+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30475+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30476+} while (0)
30477+
1facf9fc 30478+/* ---------------------------------------------------------------------- */
30479+
30480+/* policy to select one among writable branches */
4a4d8108
AM
30481+#define AuWbrCopyup(sbinfo, ...) \
30482+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30483+#define AuWbrCreate(sbinfo, ...) \
30484+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30485+
30486+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30487+#define AuLock_DW 1 /* write-lock dentry */
30488+#define AuLock_IR (1 << 1) /* read-lock inode */
30489+#define AuLock_IW (1 << 2) /* write-lock inode */
30490+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30491+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30492+ /* except RENAME_EXCHANGE */
e49829fe
JR
30493+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30494+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30495+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30496+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30497+#define au_fset_lock(flags, name) \
30498+ do { (flags) |= AuLock_##name; } while (0)
30499+#define au_fclr_lock(flags, name) \
30500+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30501+
30502+/* ---------------------------------------------------------------------- */
30503+
30504+/* super.c */
30505+extern struct file_system_type aufs_fs_type;
30506+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30507+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30508+ unsigned long long max, void *arg);
79b8bda9
AM
30509+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30510+ struct super_block *sb, void *arg);
7f207e10
AM
30511+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30512+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30513+
30514+/* sbinfo.c */
30515+void au_si_free(struct kobject *kobj);
30516+int au_si_alloc(struct super_block *sb);
e2f27e51 30517+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30518+
30519+unsigned int au_sigen_inc(struct super_block *sb);
30520+aufs_bindex_t au_new_br_id(struct super_block *sb);
30521+
e49829fe
JR
30522+int si_read_lock(struct super_block *sb, int flags);
30523+int si_write_lock(struct super_block *sb, int flags);
30524+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30525+void aufs_read_unlock(struct dentry *dentry, int flags);
30526+void aufs_write_lock(struct dentry *dentry);
30527+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30528+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30529+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30530+
30531+/* wbr_policy.c */
30532+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30533+extern struct au_wbr_create_operations au_wbr_create_ops[];
30534+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30535+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30536+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30537+
30538+/* mvdown.c */
30539+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30540+
076b876e
AM
30541+#ifdef CONFIG_AUFS_FHSM
30542+/* fhsm.c */
30543+
30544+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30545+{
30546+ pid_t pid;
30547+
30548+ spin_lock(&fhsm->fhsm_spin);
30549+ pid = fhsm->fhsm_pid;
30550+ spin_unlock(&fhsm->fhsm_spin);
30551+
30552+ return pid;
30553+}
30554+
30555+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30556+void au_fhsm_wrote_all(struct super_block *sb, int force);
30557+int au_fhsm_fd(struct super_block *sb, int oflags);
30558+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30559+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30560+void au_fhsm_fin(struct super_block *sb);
30561+void au_fhsm_init(struct au_sbinfo *sbinfo);
30562+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30563+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30564+#else
30565+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30566+ int force)
30567+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30568+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30569+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30570+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30571+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30572+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30573+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30574+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30575+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30576+#endif
30577+
1facf9fc 30578+/* ---------------------------------------------------------------------- */
30579+
30580+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30581+{
30582+ return sb->s_fs_info;
30583+}
30584+
30585+/* ---------------------------------------------------------------------- */
30586+
30587+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30588+int au_test_nfsd(void);
1facf9fc 30589+void au_export_init(struct super_block *sb);
b752ccd1 30590+void au_xigen_inc(struct inode *inode);
1facf9fc 30591+int au_xigen_new(struct inode *inode);
062440b3 30592+int au_xigen_set(struct super_block *sb, struct path *path);
1facf9fc 30593+void au_xigen_clr(struct super_block *sb);
30594+
30595+static inline int au_busy_or_stale(void)
30596+{
b752ccd1 30597+ if (!au_test_nfsd())
1facf9fc 30598+ return -EBUSY;
30599+ return -ESTALE;
30600+}
30601+#else
b752ccd1 30602+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30603+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30604+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108 30605+AuStubInt0(au_xigen_new, struct inode *inode)
062440b3 30606+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
4a4d8108 30607+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30608+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30609+#endif /* CONFIG_AUFS_EXPORT */
30610+
30611+/* ---------------------------------------------------------------------- */
30612+
e49829fe
JR
30613+#ifdef CONFIG_AUFS_SBILIST
30614+/* module.c */
8b6a4947 30615+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30616+
30617+static inline void au_sbilist_init(void)
30618+{
8b6a4947 30619+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30620+}
30621+
30622+static inline void au_sbilist_add(struct super_block *sb)
30623+{
8b6a4947 30624+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30625+}
30626+
30627+static inline void au_sbilist_del(struct super_block *sb)
30628+{
8b6a4947 30629+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30630+}
53392da6
AM
30631+
30632+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30633+static inline void au_sbilist_lock(void)
30634+{
8b6a4947 30635+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30636+}
30637+
30638+static inline void au_sbilist_unlock(void)
30639+{
8b6a4947 30640+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30641+}
30642+#define AuGFP_SBILIST GFP_ATOMIC
30643+#else
30644+AuStubVoid(au_sbilist_lock, void)
30645+AuStubVoid(au_sbilist_unlock, void)
30646+#define AuGFP_SBILIST GFP_NOFS
30647+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
30648+#else
30649+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
30650+AuStubVoid(au_sbilist_add, struct super_block *sb)
30651+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
30652+AuStubVoid(au_sbilist_lock, void)
30653+AuStubVoid(au_sbilist_unlock, void)
30654+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
30655+#endif
30656+
30657+/* ---------------------------------------------------------------------- */
30658+
1facf9fc 30659+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30660+{
dece6358 30661+ /*
c1595e42 30662+ * This function is a dynamic '__init' function actually,
dece6358
AM
30663+ * so the tiny check for si_rwsem is unnecessary.
30664+ */
30665+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 30666+#ifdef CONFIG_DEBUG_FS
30667+ sbinfo->si_dbgaufs = NULL;
86dc4139 30668+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 30669+ sbinfo->si_dbgaufs_xib = NULL;
30670+#ifdef CONFIG_AUFS_EXPORT
30671+ sbinfo->si_dbgaufs_xigen = NULL;
30672+#endif
30673+#endif
30674+}
30675+
30676+/* ---------------------------------------------------------------------- */
30677+
a2654f78
AM
30678+/* current->atomic_flags */
30679+/* this value should never corrupt the ones defined in linux/sched.h */
30680+#define PFA_AUFS 7
30681+
30682+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30683+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
30684+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
30685+
30686+static inline int si_pid_test(struct super_block *sb)
30687+{
a2654f78 30688+ return !!task_test_aufs(current);
b752ccd1
AM
30689+}
30690+
30691+static inline void si_pid_clr(struct super_block *sb)
30692+{
a2654f78
AM
30693+ AuDebugOn(!task_test_aufs(current));
30694+ task_clear_aufs(current);
b752ccd1
AM
30695+}
30696+
a2654f78
AM
30697+static inline void si_pid_set(struct super_block *sb)
30698+{
30699+ AuDebugOn(task_test_aufs(current));
30700+ task_set_aufs(current);
30701+}
febd17d6 30702+
b752ccd1
AM
30703+/* ---------------------------------------------------------------------- */
30704+
1facf9fc 30705+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
30706+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30707+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30708+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30709+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30710+/*
30711+#define __si_read_trylock_nested(sb) \
30712+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30713+#define __si_write_trylock_nested(sb) \
30714+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30715+*/
30716+
30717+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30718+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30719+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 30720+
dece6358
AM
30721+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30722+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30723+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30724+
b752ccd1
AM
30725+static inline void si_noflush_read_lock(struct super_block *sb)
30726+{
30727+ __si_read_lock(sb);
30728+ si_pid_set(sb);
30729+}
30730+
30731+static inline int si_noflush_read_trylock(struct super_block *sb)
30732+{
076b876e
AM
30733+ int locked;
30734+
30735+ locked = __si_read_trylock(sb);
b752ccd1
AM
30736+ if (locked)
30737+ si_pid_set(sb);
30738+ return locked;
30739+}
30740+
30741+static inline void si_noflush_write_lock(struct super_block *sb)
30742+{
30743+ __si_write_lock(sb);
30744+ si_pid_set(sb);
30745+}
30746+
30747+static inline int si_noflush_write_trylock(struct super_block *sb)
30748+{
076b876e
AM
30749+ int locked;
30750+
30751+ locked = __si_write_trylock(sb);
b752ccd1
AM
30752+ if (locked)
30753+ si_pid_set(sb);
30754+ return locked;
30755+}
30756+
7e9cd9fe 30757+#if 0 /* reserved */
1facf9fc 30758+static inline int si_read_trylock(struct super_block *sb, int flags)
30759+{
30760+ if (au_ftest_lock(flags, FLUSH))
30761+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30762+ return si_noflush_read_trylock(sb);
30763+}
e49829fe 30764+#endif
1facf9fc 30765+
b752ccd1
AM
30766+static inline void si_read_unlock(struct super_block *sb)
30767+{
30768+ si_pid_clr(sb);
30769+ __si_read_unlock(sb);
30770+}
30771+
7e9cd9fe 30772+#if 0 /* reserved */
1facf9fc 30773+static inline int si_write_trylock(struct super_block *sb, int flags)
30774+{
30775+ if (au_ftest_lock(flags, FLUSH))
30776+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30777+ return si_noflush_write_trylock(sb);
30778+}
b752ccd1
AM
30779+#endif
30780+
30781+static inline void si_write_unlock(struct super_block *sb)
30782+{
30783+ si_pid_clr(sb);
30784+ __si_write_unlock(sb);
30785+}
30786+
7e9cd9fe 30787+#if 0 /* reserved */
b752ccd1
AM
30788+static inline void si_downgrade_lock(struct super_block *sb)
30789+{
30790+ __si_downgrade_lock(sb);
30791+}
30792+#endif
1facf9fc 30793+
30794+/* ---------------------------------------------------------------------- */
30795+
5afbbe0d 30796+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 30797+{
dece6358 30798+ SiMustAnyLock(sb);
5afbbe0d 30799+ return au_sbi(sb)->si_bbot;
1facf9fc 30800+}
30801+
30802+static inline unsigned int au_mntflags(struct super_block *sb)
30803+{
dece6358 30804+ SiMustAnyLock(sb);
1facf9fc 30805+ return au_sbi(sb)->si_mntflags;
30806+}
30807+
30808+static inline unsigned int au_sigen(struct super_block *sb)
30809+{
dece6358 30810+ SiMustAnyLock(sb);
1facf9fc 30811+ return au_sbi(sb)->si_generation;
30812+}
30813+
5afbbe0d
AM
30814+static inline unsigned long long au_ninodes(struct super_block *sb)
30815+{
30816+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
30817+
30818+ BUG_ON(n < 0);
30819+ return n;
30820+}
30821+
7f207e10
AM
30822+static inline void au_ninodes_inc(struct super_block *sb)
30823+{
5afbbe0d 30824+ percpu_counter_inc(&au_sbi(sb)->si_ninodes);
7f207e10
AM
30825+}
30826+
30827+static inline void au_ninodes_dec(struct super_block *sb)
30828+{
5afbbe0d
AM
30829+ percpu_counter_dec(&au_sbi(sb)->si_ninodes);
30830+}
30831+
30832+static inline unsigned long long au_nfiles(struct super_block *sb)
30833+{
30834+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
30835+
30836+ BUG_ON(n < 0);
30837+ return n;
7f207e10
AM
30838+}
30839+
30840+static inline void au_nfiles_inc(struct super_block *sb)
30841+{
5afbbe0d 30842+ percpu_counter_inc(&au_sbi(sb)->si_nfiles);
7f207e10
AM
30843+}
30844+
30845+static inline void au_nfiles_dec(struct super_block *sb)
30846+{
5afbbe0d 30847+ percpu_counter_dec(&au_sbi(sb)->si_nfiles);
7f207e10
AM
30848+}
30849+
1facf9fc 30850+static inline struct au_branch *au_sbr(struct super_block *sb,
30851+ aufs_bindex_t bindex)
30852+{
dece6358 30853+ SiMustAnyLock(sb);
1facf9fc 30854+ return au_sbi(sb)->si_branch[0 + bindex];
30855+}
30856+
30857+static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
30858+{
dece6358 30859+ SiMustWriteLock(sb);
1facf9fc 30860+ au_sbi(sb)->si_xino_brid = brid;
30861+}
30862+
30863+static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
30864+{
dece6358 30865+ SiMustAnyLock(sb);
1facf9fc 30866+ return au_sbi(sb)->si_xino_brid;
30867+}
30868+
30869+#endif /* __KERNEL__ */
30870+#endif /* __AUFS_SUPER_H__ */
7f207e10
AM
30871diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
30872--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
30873+++ linux/fs/aufs/sysaufs.c 2018-07-19 09:46:13.063314675 +0200
30874@@ -0,0 +1,93 @@
30875+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30876+/*
b00004a5 30877+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30878+ *
30879+ * This program, aufs is free software; you can redistribute it and/or modify
30880+ * it under the terms of the GNU General Public License as published by
30881+ * the Free Software Foundation; either version 2 of the License, or
30882+ * (at your option) any later version.
dece6358
AM
30883+ *
30884+ * This program is distributed in the hope that it will be useful,
30885+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30886+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30887+ * GNU General Public License for more details.
30888+ *
30889+ * You should have received a copy of the GNU General Public License
523b37e3 30890+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30891+ */
30892+
30893+/*
30894+ * sysfs interface and lifetime management
30895+ * they are necessary regardless sysfs is disabled.
30896+ */
30897+
1facf9fc 30898+#include <linux/random.h>
1facf9fc 30899+#include "aufs.h"
30900+
30901+unsigned long sysaufs_si_mask;
e49829fe 30902+struct kset *sysaufs_kset;
1facf9fc 30903+
30904+#define AuSiAttr(_name) { \
30905+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
30906+ .show = sysaufs_si_##_name, \
30907+}
30908+
30909+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
30910+struct attribute *sysaufs_si_attrs[] = {
30911+ &sysaufs_si_attr_xi_path.attr,
30912+ NULL,
30913+};
30914+
4a4d8108 30915+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 30916+ .show = sysaufs_si_show
30917+};
30918+
30919+static struct kobj_type au_sbi_ktype = {
30920+ .release = au_si_free,
30921+ .sysfs_ops = &au_sbi_ops,
30922+ .default_attrs = sysaufs_si_attrs
30923+};
30924+
30925+/* ---------------------------------------------------------------------- */
30926+
30927+int sysaufs_si_init(struct au_sbinfo *sbinfo)
30928+{
30929+ int err;
30930+
e49829fe 30931+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 30932+ /* cf. sysaufs_name() */
30933+ err = kobject_init_and_add
e49829fe 30934+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 30935+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
30936+
1facf9fc 30937+ return err;
30938+}
30939+
30940+void sysaufs_fin(void)
30941+{
e49829fe
JR
30942+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
30943+ kset_unregister(sysaufs_kset);
1facf9fc 30944+}
30945+
30946+int __init sysaufs_init(void)
30947+{
30948+ int err;
30949+
30950+ do {
30951+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
30952+ } while (!sysaufs_si_mask);
30953+
4a4d8108 30954+ err = -EINVAL;
e49829fe
JR
30955+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
30956+ if (unlikely(!sysaufs_kset))
4a4d8108 30957+ goto out;
e49829fe
JR
30958+ err = PTR_ERR(sysaufs_kset);
30959+ if (IS_ERR(sysaufs_kset))
1facf9fc 30960+ goto out;
e49829fe 30961+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
062440b3 30962+ if (unlikely(err))
e49829fe 30963+ kset_unregister(sysaufs_kset);
1facf9fc 30964+
4f0767ce 30965+out:
1facf9fc 30966+ return err;
30967+}
7f207e10
AM
30968diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
30969--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
30970+++ linux/fs/aufs/sysaufs.h 2018-07-19 09:46:13.063314675 +0200
30971@@ -0,0 +1,102 @@
30972+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30973+/*
b00004a5 30974+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 30975+ *
30976+ * This program, aufs is free software; you can redistribute it and/or modify
30977+ * it under the terms of the GNU General Public License as published by
30978+ * the Free Software Foundation; either version 2 of the License, or
30979+ * (at your option) any later version.
dece6358
AM
30980+ *
30981+ * This program is distributed in the hope that it will be useful,
30982+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30983+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30984+ * GNU General Public License for more details.
30985+ *
30986+ * You should have received a copy of the GNU General Public License
523b37e3 30987+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30988+ */
30989+
30990+/*
30991+ * sysfs interface and mount lifetime management
30992+ */
30993+
30994+#ifndef __SYSAUFS_H__
30995+#define __SYSAUFS_H__
30996+
30997+#ifdef __KERNEL__
30998+
1facf9fc 30999+#include <linux/sysfs.h>
1facf9fc 31000+#include "module.h"
31001+
dece6358
AM
31002+struct super_block;
31003+struct au_sbinfo;
31004+
1facf9fc 31005+struct sysaufs_si_attr {
31006+ struct attribute attr;
31007+ int (*show)(struct seq_file *seq, struct super_block *sb);
31008+};
31009+
31010+/* ---------------------------------------------------------------------- */
31011+
31012+/* sysaufs.c */
31013+extern unsigned long sysaufs_si_mask;
e49829fe 31014+extern struct kset *sysaufs_kset;
1facf9fc 31015+extern struct attribute *sysaufs_si_attrs[];
31016+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31017+int __init sysaufs_init(void);
31018+void sysaufs_fin(void);
31019+
31020+/* ---------------------------------------------------------------------- */
31021+
31022+/* some people doesn't like to show a pointer in kernel */
31023+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31024+{
31025+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31026+}
31027+
31028+#define SysaufsSiNamePrefix "si_"
31029+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31030+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31031+{
31032+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31033+ sysaufs_si_id(sbinfo));
31034+}
31035+
31036+struct au_branch;
31037+#ifdef CONFIG_SYSFS
31038+/* sysfs.c */
31039+extern struct attribute_group *sysaufs_attr_group;
31040+
31041+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31042+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31043+ char *buf);
076b876e
AM
31044+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31045+#ifdef CONFIG_COMPAT
31046+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31047+#endif
1facf9fc 31048+
31049+void sysaufs_br_init(struct au_branch *br);
31050+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31051+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31052+
31053+#define sysaufs_brs_init() do {} while (0)
31054+
31055+#else
31056+#define sysaufs_attr_group NULL
31057+
4a4d8108 31058+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
31059+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31060+ struct attribute *attr, char *buf)
4a4d8108
AM
31061+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31062+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31063+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 31064+
31065+static inline void sysaufs_brs_init(void)
31066+{
31067+ sysaufs_brs = 0;
31068+}
31069+
31070+#endif /* CONFIG_SYSFS */
31071+
31072+#endif /* __KERNEL__ */
31073+#endif /* __SYSAUFS_H__ */
7f207e10
AM
31074diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31075--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
31076+++ linux/fs/aufs/sysfs.c 2018-07-19 09:46:13.063314675 +0200
31077@@ -0,0 +1,373 @@
31078+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31079+/*
b00004a5 31080+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31081+ *
31082+ * This program, aufs is free software; you can redistribute it and/or modify
31083+ * it under the terms of the GNU General Public License as published by
31084+ * the Free Software Foundation; either version 2 of the License, or
31085+ * (at your option) any later version.
dece6358
AM
31086+ *
31087+ * This program is distributed in the hope that it will be useful,
31088+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31089+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31090+ * GNU General Public License for more details.
31091+ *
31092+ * You should have received a copy of the GNU General Public License
523b37e3 31093+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31094+ */
31095+
31096+/*
31097+ * sysfs interface
31098+ */
31099+
076b876e 31100+#include <linux/compat.h>
1facf9fc 31101+#include <linux/seq_file.h>
1facf9fc 31102+#include "aufs.h"
31103+
4a4d8108
AM
31104+#ifdef CONFIG_AUFS_FS_MODULE
31105+/* this entry violates the "one line per file" policy of sysfs */
31106+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31107+ char *buf)
31108+{
31109+ ssize_t err;
31110+ static char *conf =
31111+/* this file is generated at compiling */
31112+#include "conf.str"
31113+ ;
31114+
31115+ err = snprintf(buf, PAGE_SIZE, conf);
31116+ if (unlikely(err >= PAGE_SIZE))
31117+ err = -EFBIG;
31118+ return err;
31119+}
31120+
31121+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31122+#endif
31123+
1facf9fc 31124+static struct attribute *au_attr[] = {
4a4d8108
AM
31125+#ifdef CONFIG_AUFS_FS_MODULE
31126+ &au_config_attr.attr,
31127+#endif
1facf9fc 31128+ NULL, /* need to NULL terminate the list of attributes */
31129+};
31130+
31131+static struct attribute_group sysaufs_attr_group_body = {
31132+ .attrs = au_attr
31133+};
31134+
31135+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31136+
31137+/* ---------------------------------------------------------------------- */
31138+
31139+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31140+{
31141+ int err;
31142+
dece6358
AM
31143+ SiMustAnyLock(sb);
31144+
1facf9fc 31145+ err = 0;
31146+ if (au_opt_test(au_mntflags(sb), XINO)) {
31147+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31148+ seq_putc(seq, '\n');
31149+ }
31150+ return err;
31151+}
31152+
31153+/*
31154+ * the lifetime of branch is independent from the entry under sysfs.
31155+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31156+ * unlinked.
31157+ */
31158+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31159+ aufs_bindex_t bindex, int idx)
1facf9fc 31160+{
1e00d052 31161+ int err;
1facf9fc 31162+ struct path path;
31163+ struct dentry *root;
31164+ struct au_branch *br;
076b876e 31165+ au_br_perm_str_t perm;
1facf9fc 31166+
31167+ AuDbg("b%d\n", bindex);
31168+
1e00d052 31169+ err = 0;
1facf9fc 31170+ root = sb->s_root;
31171+ di_read_lock_parent(root, !AuLock_IR);
31172+ br = au_sbr(sb, bindex);
392086de
AM
31173+
31174+ switch (idx) {
31175+ case AuBrSysfs_BR:
31176+ path.mnt = au_br_mnt(br);
31177+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31178+ err = au_seq_path(seq, &path);
31179+ if (!err) {
31180+ au_optstr_br_perm(&perm, br->br_perm);
31181+ seq_printf(seq, "=%s\n", perm.a);
31182+ }
392086de
AM
31183+ break;
31184+ case AuBrSysfs_BRID:
79b8bda9 31185+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31186+ break;
31187+ }
076b876e 31188+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31189+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31190+ err = -E2BIG;
392086de 31191+
1e00d052 31192+ return err;
1facf9fc 31193+}
31194+
31195+/* ---------------------------------------------------------------------- */
31196+
31197+static struct seq_file *au_seq(char *p, ssize_t len)
31198+{
31199+ struct seq_file *seq;
31200+
31201+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31202+ if (seq) {
31203+ /* mutex_init(&seq.lock); */
31204+ seq->buf = p;
31205+ seq->size = len;
31206+ return seq; /* success */
31207+ }
31208+
31209+ seq = ERR_PTR(-ENOMEM);
31210+ return seq;
31211+}
31212+
392086de
AM
31213+#define SysaufsBr_PREFIX "br"
31214+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31215+
31216+/* todo: file size may exceed PAGE_SIZE */
31217+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31218+ char *buf)
1facf9fc 31219+{
31220+ ssize_t err;
392086de 31221+ int idx;
1facf9fc 31222+ long l;
5afbbe0d 31223+ aufs_bindex_t bbot;
1facf9fc 31224+ struct au_sbinfo *sbinfo;
31225+ struct super_block *sb;
31226+ struct seq_file *seq;
31227+ char *name;
31228+ struct attribute **cattr;
31229+
31230+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31231+ sb = sbinfo->si_sb;
1308ab2a 31232+
31233+ /*
31234+ * prevent a race condition between sysfs and aufs.
31235+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31236+ * prohibits maintaining the sysfs entries.
31237+ * hew we acquire read lock after sysfs_get_active_two().
31238+ * on the other hand, the remount process may maintain the sysfs/aufs
31239+ * entries after acquiring write lock.
31240+ * it can cause a deadlock.
31241+ * simply we gave up processing read here.
31242+ */
31243+ err = -EBUSY;
31244+ if (unlikely(!si_noflush_read_trylock(sb)))
31245+ goto out;
1facf9fc 31246+
31247+ seq = au_seq(buf, PAGE_SIZE);
31248+ err = PTR_ERR(seq);
31249+ if (IS_ERR(seq))
1308ab2a 31250+ goto out_unlock;
1facf9fc 31251+
31252+ name = (void *)attr->name;
31253+ cattr = sysaufs_si_attrs;
31254+ while (*cattr) {
31255+ if (!strcmp(name, (*cattr)->name)) {
31256+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31257+ ->show(seq, sb);
31258+ goto out_seq;
31259+ }
31260+ cattr++;
31261+ }
31262+
392086de
AM
31263+ if (!strncmp(name, SysaufsBrid_PREFIX,
31264+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31265+ idx = AuBrSysfs_BRID;
31266+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31267+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31268+ sizeof(SysaufsBr_PREFIX) - 1)) {
31269+ idx = AuBrSysfs_BR;
1facf9fc 31270+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31271+ } else
31272+ BUG();
31273+
31274+ err = kstrtol(name, 10, &l);
31275+ if (!err) {
5afbbe0d
AM
31276+ bbot = au_sbbot(sb);
31277+ if (l <= bbot)
392086de
AM
31278+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31279+ else
31280+ err = -ENOENT;
1facf9fc 31281+ }
1facf9fc 31282+
4f0767ce 31283+out_seq:
1facf9fc 31284+ if (!err) {
31285+ err = seq->count;
31286+ /* sysfs limit */
31287+ if (unlikely(err == PAGE_SIZE))
31288+ err = -EFBIG;
31289+ }
1c60b727 31290+ kfree(seq);
4f0767ce 31291+out_unlock:
1facf9fc 31292+ si_read_unlock(sb);
4f0767ce 31293+out:
1facf9fc 31294+ return err;
31295+}
31296+
31297+/* ---------------------------------------------------------------------- */
31298+
076b876e
AM
31299+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31300+{
31301+ int err;
31302+ int16_t brid;
5afbbe0d 31303+ aufs_bindex_t bindex, bbot;
076b876e
AM
31304+ size_t sz;
31305+ char *buf;
31306+ struct seq_file *seq;
31307+ struct au_branch *br;
31308+
31309+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31310+ bbot = au_sbbot(sb);
31311+ err = bbot + 1;
076b876e
AM
31312+ if (!arg)
31313+ goto out;
31314+
31315+ err = -ENOMEM;
31316+ buf = (void *)__get_free_page(GFP_NOFS);
31317+ if (unlikely(!buf))
31318+ goto out;
31319+
31320+ seq = au_seq(buf, PAGE_SIZE);
31321+ err = PTR_ERR(seq);
31322+ if (IS_ERR(seq))
31323+ goto out_buf;
31324+
31325+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31326+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
076b876e
AM
31327+ err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
31328+ if (unlikely(err))
31329+ break;
31330+
31331+ br = au_sbr(sb, bindex);
31332+ brid = br->br_id;
31333+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31334+ err = __put_user(brid, &arg->id);
31335+ if (unlikely(err))
31336+ break;
31337+
31338+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31339+ err = __put_user(br->br_perm, &arg->perm);
31340+ if (unlikely(err))
31341+ break;
31342+
79b8bda9
AM
31343+ err = au_seq_path(seq, &br->br_path);
31344+ if (unlikely(err))
31345+ break;
31346+ seq_putc(seq, '\0');
31347+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31348+ err = copy_to_user(arg->path, seq->buf, seq->count);
31349+ seq->count = 0;
31350+ if (unlikely(err))
31351+ break;
31352+ } else {
31353+ err = -E2BIG;
31354+ goto out_seq;
31355+ }
31356+ }
31357+ if (unlikely(err))
31358+ err = -EFAULT;
31359+
31360+out_seq:
1c60b727 31361+ kfree(seq);
076b876e 31362+out_buf:
1c60b727 31363+ free_page((unsigned long)buf);
076b876e
AM
31364+out:
31365+ si_read_unlock(sb);
31366+ return err;
31367+}
31368+
31369+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31370+{
2000de60 31371+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31372+}
31373+
31374+#ifdef CONFIG_COMPAT
31375+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31376+{
2000de60 31377+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31378+}
31379+#endif
31380+
31381+/* ---------------------------------------------------------------------- */
31382+
1facf9fc 31383+void sysaufs_br_init(struct au_branch *br)
31384+{
392086de
AM
31385+ int i;
31386+ struct au_brsysfs *br_sysfs;
31387+ struct attribute *attr;
4a4d8108 31388+
392086de
AM
31389+ br_sysfs = br->br_sysfs;
31390+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31391+ attr = &br_sysfs->attr;
31392+ sysfs_attr_init(attr);
31393+ attr->name = br_sysfs->name;
31394+ attr->mode = S_IRUGO;
31395+ br_sysfs++;
31396+ }
1facf9fc 31397+}
31398+
31399+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31400+{
31401+ struct au_branch *br;
31402+ struct kobject *kobj;
392086de
AM
31403+ struct au_brsysfs *br_sysfs;
31404+ int i;
5afbbe0d 31405+ aufs_bindex_t bbot;
1facf9fc 31406+
1facf9fc 31407+ if (!sysaufs_brs)
31408+ return;
31409+
31410+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31411+ bbot = au_sbbot(sb);
31412+ for (; bindex <= bbot; bindex++) {
1facf9fc 31413+ br = au_sbr(sb, bindex);
392086de
AM
31414+ br_sysfs = br->br_sysfs;
31415+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31416+ sysfs_remove_file(kobj, &br_sysfs->attr);
31417+ br_sysfs++;
31418+ }
1facf9fc 31419+ }
31420+}
31421+
31422+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31423+{
392086de 31424+ int err, i;
5afbbe0d 31425+ aufs_bindex_t bbot;
1facf9fc 31426+ struct kobject *kobj;
31427+ struct au_branch *br;
392086de 31428+ struct au_brsysfs *br_sysfs;
1facf9fc 31429+
1facf9fc 31430+ if (!sysaufs_brs)
31431+ return;
31432+
31433+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31434+ bbot = au_sbbot(sb);
31435+ for (; bindex <= bbot; bindex++) {
1facf9fc 31436+ br = au_sbr(sb, bindex);
392086de
AM
31437+ br_sysfs = br->br_sysfs;
31438+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31439+ SysaufsBr_PREFIX "%d", bindex);
31440+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31441+ SysaufsBrid_PREFIX "%d", bindex);
31442+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31443+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31444+ if (unlikely(err))
31445+ pr_warn("failed %s under sysfs(%d)\n",
31446+ br_sysfs->name, err);
31447+ br_sysfs++;
31448+ }
1facf9fc 31449+ }
31450+}
7f207e10
AM
31451diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31452--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
31453+++ linux/fs/aufs/sysrq.c 2018-07-19 09:46:13.063314675 +0200
31454@@ -0,0 +1,160 @@
31455+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31456+/*
b00004a5 31457+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31458+ *
31459+ * This program, aufs is free software; you can redistribute it and/or modify
31460+ * it under the terms of the GNU General Public License as published by
31461+ * the Free Software Foundation; either version 2 of the License, or
31462+ * (at your option) any later version.
dece6358
AM
31463+ *
31464+ * This program is distributed in the hope that it will be useful,
31465+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31466+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31467+ * GNU General Public License for more details.
31468+ *
31469+ * You should have received a copy of the GNU General Public License
523b37e3 31470+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31471+ */
31472+
31473+/*
31474+ * magic sysrq hanlder
31475+ */
31476+
1facf9fc 31477+/* #include <linux/sysrq.h> */
027c5e7a 31478+#include <linux/writeback.h>
1facf9fc 31479+#include "aufs.h"
31480+
31481+/* ---------------------------------------------------------------------- */
31482+
31483+static void sysrq_sb(struct super_block *sb)
31484+{
31485+ char *plevel;
31486+ struct au_sbinfo *sbinfo;
31487+ struct file *file;
8b6a4947
AM
31488+ struct hlist_bl_head *files;
31489+ struct hlist_bl_node *pos;
523b37e3 31490+ struct au_finfo *finfo;
1facf9fc 31491+
31492+ plevel = au_plevel;
31493+ au_plevel = KERN_WARNING;
1facf9fc 31494+
4a4d8108 31495+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31496+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31497+
31498+ sbinfo = au_sbi(sb);
4a4d8108 31499+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31500+ pr("superblock\n");
1facf9fc 31501+ au_dpri_sb(sb);
027c5e7a
AM
31502+
31503+#if 0
c06a8ce3 31504+ pr("root dentry\n");
1facf9fc 31505+ au_dpri_dentry(sb->s_root);
c06a8ce3 31506+ pr("root inode\n");
5527c038 31507+ au_dpri_inode(d_inode(sb->s_root));
027c5e7a
AM
31508+#endif
31509+
1facf9fc 31510+#if 0
027c5e7a
AM
31511+ do {
31512+ int err, i, j, ndentry;
31513+ struct au_dcsub_pages dpages;
31514+ struct au_dpage *dpage;
31515+
31516+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31517+ if (unlikely(err))
31518+ break;
31519+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31520+ if (!err)
31521+ for (i = 0; i < dpages.ndpage; i++) {
31522+ dpage = dpages.dpages + i;
31523+ ndentry = dpage->ndentry;
31524+ for (j = 0; j < ndentry; j++)
31525+ au_dpri_dentry(dpage->dentries[j]);
31526+ }
31527+ au_dpages_free(&dpages);
31528+ } while (0);
31529+#endif
31530+
31531+#if 1
31532+ {
31533+ struct inode *i;
076b876e 31534+
c06a8ce3 31535+ pr("isolated inode\n");
79b8bda9 31536+ spin_lock(&sb->s_inode_list_lock);
2cbb1c4b
JR
31537+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31538+ spin_lock(&i->i_lock);
b4510431 31539+ if (1 || hlist_empty(&i->i_dentry))
027c5e7a 31540+ au_dpri_inode(i);
2cbb1c4b
JR
31541+ spin_unlock(&i->i_lock);
31542+ }
79b8bda9 31543+ spin_unlock(&sb->s_inode_list_lock);
027c5e7a 31544+ }
1facf9fc 31545+#endif
c06a8ce3 31546+ pr("files\n");
523b37e3 31547+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31548+ hlist_bl_lock(files);
31549+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31550+ umode_t mode;
076b876e 31551+
523b37e3 31552+ file = finfo->fi_file;
c06a8ce3 31553+ mode = file_inode(file)->i_mode;
38d290e6 31554+ if (!special_file(mode))
1facf9fc 31555+ au_dpri_file(file);
523b37e3 31556+ }
8b6a4947 31557+ hlist_bl_unlock(files);
c06a8ce3 31558+ pr("done\n");
1facf9fc 31559+
c06a8ce3 31560+#undef pr
1facf9fc 31561+ au_plevel = plevel;
1facf9fc 31562+}
31563+
31564+/* ---------------------------------------------------------------------- */
31565+
31566+/* module parameter */
31567+static char *aufs_sysrq_key = "a";
31568+module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
31569+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31570+
0c5527e5 31571+static void au_sysrq(int key __maybe_unused)
1facf9fc 31572+{
1facf9fc 31573+ struct au_sbinfo *sbinfo;
8b6a4947 31574+ struct hlist_bl_node *pos;
1facf9fc 31575+
027c5e7a 31576+ lockdep_off();
53392da6 31577+ au_sbilist_lock();
8b6a4947 31578+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31579+ sysrq_sb(sbinfo->si_sb);
53392da6 31580+ au_sbilist_unlock();
027c5e7a 31581+ lockdep_on();
1facf9fc 31582+}
31583+
31584+static struct sysrq_key_op au_sysrq_op = {
31585+ .handler = au_sysrq,
31586+ .help_msg = "Aufs",
31587+ .action_msg = "Aufs",
31588+ .enable_mask = SYSRQ_ENABLE_DUMP
31589+};
31590+
31591+/* ---------------------------------------------------------------------- */
31592+
31593+int __init au_sysrq_init(void)
31594+{
31595+ int err;
31596+ char key;
31597+
31598+ err = -1;
31599+ key = *aufs_sysrq_key;
31600+ if ('a' <= key && key <= 'z')
31601+ err = register_sysrq_key(key, &au_sysrq_op);
31602+ if (unlikely(err))
4a4d8108 31603+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31604+ return err;
31605+}
31606+
31607+void au_sysrq_fin(void)
31608+{
31609+ int err;
076b876e 31610+
1facf9fc 31611+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31612+ if (unlikely(err))
4a4d8108 31613+ pr_err("err %d (ignored)\n", err);
1facf9fc 31614+}
7f207e10
AM
31615diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31616--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
31617+++ linux/fs/aufs/vdir.c 2018-07-19 09:46:13.063314675 +0200
31618@@ -0,0 +1,894 @@
31619+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31620+/*
b00004a5 31621+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 31622+ *
31623+ * This program, aufs is free software; you can redistribute it and/or modify
31624+ * it under the terms of the GNU General Public License as published by
31625+ * the Free Software Foundation; either version 2 of the License, or
31626+ * (at your option) any later version.
dece6358
AM
31627+ *
31628+ * This program is distributed in the hope that it will be useful,
31629+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31630+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31631+ * GNU General Public License for more details.
31632+ *
31633+ * You should have received a copy of the GNU General Public License
523b37e3 31634+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31635+ */
31636+
31637+/*
31638+ * virtual or vertical directory
31639+ */
31640+
31641+#include "aufs.h"
31642+
dece6358 31643+static unsigned int calc_size(int nlen)
1facf9fc 31644+{
dece6358 31645+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31646+}
31647+
31648+static int set_deblk_end(union au_vdir_deblk_p *p,
31649+ union au_vdir_deblk_p *deblk_end)
31650+{
31651+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31652+ p->de->de_str.len = 0;
31653+ /* smp_mb(); */
31654+ return 0;
31655+ }
31656+ return -1; /* error */
31657+}
31658+
31659+/* returns true or false */
31660+static int is_deblk_end(union au_vdir_deblk_p *p,
31661+ union au_vdir_deblk_p *deblk_end)
31662+{
31663+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31664+ return !p->de->de_str.len;
31665+ return 1;
31666+}
31667+
31668+static unsigned char *last_deblk(struct au_vdir *vdir)
31669+{
31670+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31671+}
31672+
31673+/* ---------------------------------------------------------------------- */
31674+
79b8bda9 31675+/* estimate the appropriate size for name hash table */
1308ab2a 31676+unsigned int au_rdhash_est(loff_t sz)
31677+{
31678+ unsigned int n;
31679+
31680+ n = UINT_MAX;
31681+ sz >>= 10;
31682+ if (sz < n)
31683+ n = sz;
31684+ if (sz < AUFS_RDHASH_DEF)
31685+ n = AUFS_RDHASH_DEF;
4a4d8108 31686+ /* pr_info("n %u\n", n); */
1308ab2a 31687+ return n;
31688+}
31689+
1facf9fc 31690+/*
31691+ * the allocated memory has to be freed by
dece6358 31692+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 31693+ */
dece6358 31694+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 31695+{
1facf9fc 31696+ struct hlist_head *head;
dece6358 31697+ unsigned int u;
076b876e 31698+ size_t sz;
1facf9fc 31699+
076b876e
AM
31700+ sz = sizeof(*nhash->nh_head) * num_hash;
31701+ head = kmalloc(sz, gfp);
dece6358
AM
31702+ if (head) {
31703+ nhash->nh_num = num_hash;
31704+ nhash->nh_head = head;
31705+ for (u = 0; u < num_hash; u++)
1facf9fc 31706+ INIT_HLIST_HEAD(head++);
dece6358 31707+ return 0; /* success */
1facf9fc 31708+ }
1facf9fc 31709+
dece6358 31710+ return -ENOMEM;
1facf9fc 31711+}
31712+
dece6358
AM
31713+static void nhash_count(struct hlist_head *head)
31714+{
31715+#if 0
31716+ unsigned long n;
31717+ struct hlist_node *pos;
31718+
31719+ n = 0;
31720+ hlist_for_each(pos, head)
31721+ n++;
4a4d8108 31722+ pr_info("%lu\n", n);
dece6358
AM
31723+#endif
31724+}
31725+
31726+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 31727+{
c06a8ce3
AM
31728+ struct au_vdir_wh *pos;
31729+ struct hlist_node *node;
1facf9fc 31730+
c06a8ce3 31731+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
1c60b727 31732+ kfree(pos);
1facf9fc 31733+}
31734+
dece6358 31735+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 31736+{
c06a8ce3
AM
31737+ struct au_vdir_dehstr *pos;
31738+ struct hlist_node *node;
1facf9fc 31739+
c06a8ce3 31740+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 31741+ au_cache_free_vdir_dehstr(pos);
1facf9fc 31742+}
31743+
dece6358
AM
31744+static void au_nhash_do_free(struct au_nhash *nhash,
31745+ void (*free)(struct hlist_head *head))
1facf9fc 31746+{
1308ab2a 31747+ unsigned int n;
1facf9fc 31748+ struct hlist_head *head;
1facf9fc 31749+
dece6358 31750+ n = nhash->nh_num;
1308ab2a 31751+ if (!n)
31752+ return;
31753+
dece6358 31754+ head = nhash->nh_head;
1308ab2a 31755+ while (n-- > 0) {
dece6358
AM
31756+ nhash_count(head);
31757+ free(head++);
1facf9fc 31758+ }
1c60b727 31759+ kfree(nhash->nh_head);
1facf9fc 31760+}
31761+
dece6358 31762+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 31763+{
dece6358
AM
31764+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
31765+}
1facf9fc 31766+
dece6358
AM
31767+static void au_nhash_de_free(struct au_nhash *delist)
31768+{
31769+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 31770+}
31771+
31772+/* ---------------------------------------------------------------------- */
31773+
31774+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31775+ int limit)
31776+{
31777+ int num;
31778+ unsigned int u, n;
31779+ struct hlist_head *head;
c06a8ce3 31780+ struct au_vdir_wh *pos;
1facf9fc 31781+
31782+ num = 0;
31783+ n = whlist->nh_num;
31784+ head = whlist->nh_head;
1308ab2a 31785+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
31786+ hlist_for_each_entry(pos, head, wh_hash)
31787+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 31788+ return 1;
1facf9fc 31789+ return 0;
31790+}
31791+
31792+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 31793+ unsigned char *name,
1facf9fc 31794+ unsigned int len)
31795+{
dece6358
AM
31796+ unsigned int v;
31797+ /* const unsigned int magic_bit = 12; */
31798+
1308ab2a 31799+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31800+
dece6358 31801+ v = 0;
f0c0a007
AM
31802+ if (len > 8)
31803+ len = 8;
dece6358
AM
31804+ while (len--)
31805+ v += *name++;
31806+ /* v = hash_long(v, magic_bit); */
31807+ v %= nhash->nh_num;
31808+ return nhash->nh_head + v;
31809+}
31810+
31811+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31812+ int nlen)
31813+{
31814+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 31815+}
31816+
31817+/* returns found or not */
dece6358 31818+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 31819+{
31820+ struct hlist_head *head;
c06a8ce3 31821+ struct au_vdir_wh *pos;
1facf9fc 31822+ struct au_vdir_destr *str;
31823+
dece6358 31824+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
31825+ hlist_for_each_entry(pos, head, wh_hash) {
31826+ str = &pos->wh_str;
1facf9fc 31827+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
31828+ if (au_nhash_test_name(str, name, nlen))
31829+ return 1;
31830+ }
31831+ return 0;
31832+}
31833+
31834+/* returns found(true) or not */
31835+static int test_known(struct au_nhash *delist, char *name, int nlen)
31836+{
31837+ struct hlist_head *head;
c06a8ce3 31838+ struct au_vdir_dehstr *pos;
dece6358
AM
31839+ struct au_vdir_destr *str;
31840+
31841+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
31842+ hlist_for_each_entry(pos, head, hash) {
31843+ str = pos->str;
dece6358
AM
31844+ AuDbg("%.*s\n", str->len, str->name);
31845+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 31846+ return 1;
31847+ }
31848+ return 0;
31849+}
31850+
dece6358
AM
31851+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31852+ unsigned char d_type)
31853+{
31854+#ifdef CONFIG_AUFS_SHWH
31855+ wh->wh_ino = ino;
31856+ wh->wh_type = d_type;
31857+#endif
31858+}
31859+
31860+/* ---------------------------------------------------------------------- */
31861+
31862+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31863+ unsigned int d_type, aufs_bindex_t bindex,
31864+ unsigned char shwh)
1facf9fc 31865+{
31866+ int err;
31867+ struct au_vdir_destr *str;
31868+ struct au_vdir_wh *wh;
31869+
dece6358 31870+ AuDbg("%.*s\n", nlen, name);
1308ab2a 31871+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31872+
1facf9fc 31873+ err = -ENOMEM;
dece6358 31874+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 31875+ if (unlikely(!wh))
31876+ goto out;
31877+
31878+ err = 0;
31879+ wh->wh_bindex = bindex;
dece6358
AM
31880+ if (shwh)
31881+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 31882+ str = &wh->wh_str;
dece6358
AM
31883+ str->len = nlen;
31884+ memcpy(str->name, name, nlen);
31885+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 31886+ /* smp_mb(); */
31887+
4f0767ce 31888+out:
1facf9fc 31889+ return err;
31890+}
31891+
1facf9fc 31892+static int append_deblk(struct au_vdir *vdir)
31893+{
31894+ int err;
dece6358 31895+ unsigned long ul;
1facf9fc 31896+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31897+ union au_vdir_deblk_p p, deblk_end;
31898+ unsigned char **o;
31899+
31900+ err = -ENOMEM;
e2f27e51
AM
31901+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
31902+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 31903+ if (unlikely(!o))
31904+ goto out;
31905+
31906+ vdir->vd_deblk = o;
31907+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
31908+ if (p.deblk) {
31909+ ul = vdir->vd_nblk++;
31910+ vdir->vd_deblk[ul] = p.deblk;
31911+ vdir->vd_last.ul = ul;
31912+ vdir->vd_last.p.deblk = p.deblk;
31913+ deblk_end.deblk = p.deblk + deblk_sz;
31914+ err = set_deblk_end(&p, &deblk_end);
31915+ }
31916+
4f0767ce 31917+out:
1facf9fc 31918+ return err;
31919+}
31920+
dece6358
AM
31921+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
31922+ unsigned int d_type, struct au_nhash *delist)
31923+{
31924+ int err;
31925+ unsigned int sz;
31926+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
31927+ union au_vdir_deblk_p p, *room, deblk_end;
31928+ struct au_vdir_dehstr *dehstr;
31929+
31930+ p.deblk = last_deblk(vdir);
31931+ deblk_end.deblk = p.deblk + deblk_sz;
31932+ room = &vdir->vd_last.p;
31933+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
31934+ || !is_deblk_end(room, &deblk_end));
31935+
31936+ sz = calc_size(nlen);
31937+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
31938+ err = append_deblk(vdir);
31939+ if (unlikely(err))
31940+ goto out;
31941+
31942+ p.deblk = last_deblk(vdir);
31943+ deblk_end.deblk = p.deblk + deblk_sz;
31944+ /* smp_mb(); */
31945+ AuDebugOn(room->deblk != p.deblk);
31946+ }
31947+
31948+ err = -ENOMEM;
4a4d8108 31949+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
31950+ if (unlikely(!dehstr))
31951+ goto out;
31952+
31953+ dehstr->str = &room->de->de_str;
31954+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
31955+ room->de->de_ino = ino;
31956+ room->de->de_type = d_type;
31957+ room->de->de_str.len = nlen;
31958+ memcpy(room->de->de_str.name, name, nlen);
31959+
31960+ err = 0;
31961+ room->deblk += sz;
31962+ if (unlikely(set_deblk_end(room, &deblk_end)))
31963+ err = append_deblk(vdir);
31964+ /* smp_mb(); */
31965+
4f0767ce 31966+out:
dece6358
AM
31967+ return err;
31968+}
31969+
31970+/* ---------------------------------------------------------------------- */
31971+
1c60b727 31972+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
31973+{
31974+ unsigned char **deblk;
31975+
31976+ deblk = vdir->vd_deblk;
1c60b727
AM
31977+ while (vdir->vd_nblk--)
31978+ kfree(*deblk++);
31979+ kfree(vdir->vd_deblk);
31980+ au_cache_free_vdir(vdir);
dece6358
AM
31981+}
31982+
1308ab2a 31983+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 31984+{
31985+ struct au_vdir *vdir;
1308ab2a 31986+ struct super_block *sb;
1facf9fc 31987+ int err;
31988+
2000de60 31989+ sb = file->f_path.dentry->d_sb;
dece6358
AM
31990+ SiMustAnyLock(sb);
31991+
1facf9fc 31992+ err = -ENOMEM;
31993+ vdir = au_cache_alloc_vdir();
31994+ if (unlikely(!vdir))
31995+ goto out;
31996+
31997+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
31998+ if (unlikely(!vdir->vd_deblk))
31999+ goto out_free;
32000+
32001+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 32002+ if (!vdir->vd_deblk_sz) {
79b8bda9 32003+ /* estimate the appropriate size for deblk */
1308ab2a 32004+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 32005+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 32006+ }
1facf9fc 32007+ vdir->vd_nblk = 0;
32008+ vdir->vd_version = 0;
32009+ vdir->vd_jiffy = 0;
32010+ err = append_deblk(vdir);
32011+ if (!err)
32012+ return vdir; /* success */
32013+
1c60b727 32014+ kfree(vdir->vd_deblk);
1facf9fc 32015+
4f0767ce 32016+out_free:
1c60b727 32017+ au_cache_free_vdir(vdir);
4f0767ce 32018+out:
1facf9fc 32019+ vdir = ERR_PTR(err);
32020+ return vdir;
32021+}
32022+
32023+static int reinit_vdir(struct au_vdir *vdir)
32024+{
32025+ int err;
32026+ union au_vdir_deblk_p p, deblk_end;
32027+
32028+ while (vdir->vd_nblk > 1) {
1c60b727 32029+ kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 32030+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32031+ vdir->vd_nblk--;
32032+ }
32033+ p.deblk = vdir->vd_deblk[0];
32034+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32035+ err = set_deblk_end(&p, &deblk_end);
32036+ /* keep vd_dblk_sz */
32037+ vdir->vd_last.ul = 0;
32038+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32039+ vdir->vd_version = 0;
32040+ vdir->vd_jiffy = 0;
32041+ /* smp_mb(); */
32042+ return err;
32043+}
32044+
32045+/* ---------------------------------------------------------------------- */
32046+
1facf9fc 32047+#define AuFillVdir_CALLED 1
32048+#define AuFillVdir_WHABLE (1 << 1)
dece6358 32049+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 32050+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
32051+#define au_fset_fillvdir(flags, name) \
32052+ do { (flags) |= AuFillVdir_##name; } while (0)
32053+#define au_fclr_fillvdir(flags, name) \
32054+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 32055+
dece6358
AM
32056+#ifndef CONFIG_AUFS_SHWH
32057+#undef AuFillVdir_SHWH
32058+#define AuFillVdir_SHWH 0
32059+#endif
32060+
1facf9fc 32061+struct fillvdir_arg {
392086de 32062+ struct dir_context ctx;
1facf9fc 32063+ struct file *file;
32064+ struct au_vdir *vdir;
dece6358
AM
32065+ struct au_nhash delist;
32066+ struct au_nhash whlist;
1facf9fc 32067+ aufs_bindex_t bindex;
32068+ unsigned int flags;
32069+ int err;
32070+};
32071+
392086de 32072+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 32073+ loff_t offset __maybe_unused, u64 h_ino,
32074+ unsigned int d_type)
32075+{
392086de 32076+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 32077+ char *name = (void *)__name;
32078+ struct super_block *sb;
1facf9fc 32079+ ino_t ino;
dece6358 32080+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 32081+
1facf9fc 32082+ arg->err = 0;
2000de60 32083+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 32084+ au_fset_fillvdir(arg->flags, CALLED);
32085+ /* smp_mb(); */
dece6358 32086+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 32087+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
32088+ if (test_known(&arg->delist, name, nlen)
32089+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32090+ goto out; /* already exists or whiteouted */
1facf9fc 32091+
dece6358 32092+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
32093+ if (!arg->err) {
32094+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32095+ d_type = DT_UNKNOWN;
dece6358
AM
32096+ arg->err = append_de(arg->vdir, name, nlen, ino,
32097+ d_type, &arg->delist);
4a4d8108 32098+ }
1facf9fc 32099+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32100+ name += AUFS_WH_PFX_LEN;
dece6358
AM
32101+ nlen -= AUFS_WH_PFX_LEN;
32102+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32103+ goto out; /* already whiteouted */
1facf9fc 32104+
dece6358
AM
32105+ if (shwh)
32106+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32107+ &ino);
4a4d8108
AM
32108+ if (!arg->err) {
32109+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32110+ d_type = DT_UNKNOWN;
1facf9fc 32111+ arg->err = au_nhash_append_wh
dece6358
AM
32112+ (&arg->whlist, name, nlen, ino, d_type,
32113+ arg->bindex, shwh);
4a4d8108 32114+ }
1facf9fc 32115+ }
32116+
4f0767ce 32117+out:
1facf9fc 32118+ if (!arg->err)
32119+ arg->vdir->vd_jiffy = jiffies;
32120+ /* smp_mb(); */
32121+ AuTraceErr(arg->err);
32122+ return arg->err;
32123+}
32124+
dece6358
AM
32125+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32126+ struct au_nhash *whlist, struct au_nhash *delist)
32127+{
32128+#ifdef CONFIG_AUFS_SHWH
32129+ int err;
32130+ unsigned int nh, u;
32131+ struct hlist_head *head;
c06a8ce3
AM
32132+ struct au_vdir_wh *pos;
32133+ struct hlist_node *n;
dece6358
AM
32134+ char *p, *o;
32135+ struct au_vdir_destr *destr;
32136+
32137+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32138+
32139+ err = -ENOMEM;
537831f9 32140+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32141+ if (unlikely(!p))
32142+ goto out;
32143+
32144+ err = 0;
32145+ nh = whlist->nh_num;
32146+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32147+ p += AUFS_WH_PFX_LEN;
32148+ for (u = 0; u < nh; u++) {
32149+ head = whlist->nh_head + u;
c06a8ce3
AM
32150+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32151+ destr = &pos->wh_str;
dece6358
AM
32152+ memcpy(p, destr->name, destr->len);
32153+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32154+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32155+ if (unlikely(err))
32156+ break;
32157+ }
32158+ }
32159+
1c60b727 32160+ free_page((unsigned long)o);
dece6358 32161+
4f0767ce 32162+out:
dece6358
AM
32163+ AuTraceErr(err);
32164+ return err;
32165+#else
32166+ return 0;
32167+#endif
32168+}
32169+
1facf9fc 32170+static int au_do_read_vdir(struct fillvdir_arg *arg)
32171+{
32172+ int err;
dece6358 32173+ unsigned int rdhash;
1facf9fc 32174+ loff_t offset;
5afbbe0d 32175+ aufs_bindex_t bbot, bindex, btop;
dece6358 32176+ unsigned char shwh;
1facf9fc 32177+ struct file *hf, *file;
32178+ struct super_block *sb;
32179+
1facf9fc 32180+ file = arg->file;
2000de60 32181+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32182+ SiMustAnyLock(sb);
32183+
32184+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32185+ if (!rdhash)
32186+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32187+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32188+ if (unlikely(err))
1facf9fc 32189+ goto out;
dece6358
AM
32190+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32191+ if (unlikely(err))
1facf9fc 32192+ goto out_delist;
32193+
32194+ err = 0;
32195+ arg->flags = 0;
dece6358
AM
32196+ shwh = 0;
32197+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32198+ shwh = 1;
32199+ au_fset_fillvdir(arg->flags, SHWH);
32200+ }
5afbbe0d
AM
32201+ btop = au_fbtop(file);
32202+ bbot = au_fbbot_dir(file);
32203+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32204+ hf = au_hf_dir(file, bindex);
1facf9fc 32205+ if (!hf)
32206+ continue;
32207+
32208+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32209+ err = offset;
32210+ if (unlikely(offset))
32211+ break;
32212+
32213+ arg->bindex = bindex;
32214+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32215+ if (shwh
5afbbe0d 32216+ || (bindex != bbot
dece6358 32217+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32218+ au_fset_fillvdir(arg->flags, WHABLE);
32219+ do {
32220+ arg->err = 0;
32221+ au_fclr_fillvdir(arg->flags, CALLED);
32222+ /* smp_mb(); */
392086de 32223+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32224+ if (err >= 0)
32225+ err = arg->err;
32226+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32227+
32228+ /*
32229+ * dir_relax() may be good for concurrency, but aufs should not
32230+ * use it since it will cause a lockdep problem.
32231+ */
1facf9fc 32232+ }
dece6358
AM
32233+
32234+ if (!err && shwh)
32235+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32236+
32237+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32238+
4f0767ce 32239+out_delist:
dece6358 32240+ au_nhash_de_free(&arg->delist);
4f0767ce 32241+out:
1facf9fc 32242+ return err;
32243+}
32244+
32245+static int read_vdir(struct file *file, int may_read)
32246+{
32247+ int err;
32248+ unsigned long expire;
32249+ unsigned char do_read;
392086de
AM
32250+ struct fillvdir_arg arg = {
32251+ .ctx = {
2000de60 32252+ .actor = fillvdir
392086de
AM
32253+ }
32254+ };
1facf9fc 32255+ struct inode *inode;
32256+ struct au_vdir *vdir, *allocated;
32257+
32258+ err = 0;
c06a8ce3 32259+ inode = file_inode(file);
1facf9fc 32260+ IMustLock(inode);
5afbbe0d 32261+ IiMustWriteLock(inode);
dece6358
AM
32262+ SiMustAnyLock(inode->i_sb);
32263+
1facf9fc 32264+ allocated = NULL;
32265+ do_read = 0;
32266+ expire = au_sbi(inode->i_sb)->si_rdcache;
32267+ vdir = au_ivdir(inode);
32268+ if (!vdir) {
32269+ do_read = 1;
1308ab2a 32270+ vdir = alloc_vdir(file);
1facf9fc 32271+ err = PTR_ERR(vdir);
32272+ if (IS_ERR(vdir))
32273+ goto out;
32274+ err = 0;
32275+ allocated = vdir;
32276+ } else if (may_read
be118d29 32277+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32278+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32279+ do_read = 1;
32280+ err = reinit_vdir(vdir);
32281+ if (unlikely(err))
32282+ goto out;
32283+ }
32284+
32285+ if (!do_read)
32286+ return 0; /* success */
32287+
32288+ arg.file = file;
32289+ arg.vdir = vdir;
32290+ err = au_do_read_vdir(&arg);
32291+ if (!err) {
392086de 32292+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32293+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32294+ vdir->vd_last.ul = 0;
32295+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32296+ if (allocated)
32297+ au_set_ivdir(inode, allocated);
32298+ } else if (allocated)
1c60b727 32299+ au_vdir_free(allocated);
1facf9fc 32300+
4f0767ce 32301+out:
1facf9fc 32302+ return err;
32303+}
32304+
32305+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32306+{
32307+ int err, rerr;
32308+ unsigned long ul, n;
32309+ const unsigned int deblk_sz = src->vd_deblk_sz;
32310+
32311+ AuDebugOn(tgt->vd_nblk != 1);
32312+
32313+ err = -ENOMEM;
32314+ if (tgt->vd_nblk < src->vd_nblk) {
32315+ unsigned char **p;
32316+
e2f27e51
AM
32317+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32318+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32319+ if (unlikely(!p))
32320+ goto out;
32321+ tgt->vd_deblk = p;
32322+ }
32323+
1308ab2a 32324+ if (tgt->vd_deblk_sz != deblk_sz) {
32325+ unsigned char *p;
32326+
32327+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32328+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32329+ /*may_shrink*/1);
1308ab2a 32330+ if (unlikely(!p))
32331+ goto out;
32332+ tgt->vd_deblk[0] = p;
32333+ }
1facf9fc 32334+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32335+ tgt->vd_version = src->vd_version;
32336+ tgt->vd_jiffy = src->vd_jiffy;
32337+
32338+ n = src->vd_nblk;
32339+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32340+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32341+ GFP_NOFS);
32342+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32343+ goto out;
1308ab2a 32344+ tgt->vd_nblk++;
1facf9fc 32345+ }
1308ab2a 32346+ tgt->vd_nblk = n;
32347+ tgt->vd_last.ul = tgt->vd_last.ul;
32348+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32349+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32350+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32351+ /* smp_mb(); */
32352+ return 0; /* success */
32353+
4f0767ce 32354+out:
1facf9fc 32355+ rerr = reinit_vdir(tgt);
32356+ BUG_ON(rerr);
32357+ return err;
32358+}
32359+
32360+int au_vdir_init(struct file *file)
32361+{
32362+ int err;
32363+ struct inode *inode;
32364+ struct au_vdir *vdir_cache, *allocated;
32365+
392086de 32366+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32367+ err = read_vdir(file, !file->f_pos);
32368+ if (unlikely(err))
32369+ goto out;
32370+
32371+ allocated = NULL;
32372+ vdir_cache = au_fvdir_cache(file);
32373+ if (!vdir_cache) {
1308ab2a 32374+ vdir_cache = alloc_vdir(file);
1facf9fc 32375+ err = PTR_ERR(vdir_cache);
32376+ if (IS_ERR(vdir_cache))
32377+ goto out;
32378+ allocated = vdir_cache;
32379+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32380+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32381+ err = reinit_vdir(vdir_cache);
32382+ if (unlikely(err))
32383+ goto out;
32384+ } else
32385+ return 0; /* success */
32386+
c06a8ce3 32387+ inode = file_inode(file);
1facf9fc 32388+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32389+ if (!err) {
be118d29 32390+ file->f_version = inode_query_iversion(inode);
1facf9fc 32391+ if (allocated)
32392+ au_set_fvdir_cache(file, allocated);
32393+ } else if (allocated)
1c60b727 32394+ au_vdir_free(allocated);
1facf9fc 32395+
4f0767ce 32396+out:
1facf9fc 32397+ return err;
32398+}
32399+
32400+static loff_t calc_offset(struct au_vdir *vdir)
32401+{
32402+ loff_t offset;
32403+ union au_vdir_deblk_p p;
32404+
32405+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32406+ offset = vdir->vd_last.p.deblk - p.deblk;
32407+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32408+ return offset;
32409+}
32410+
32411+/* returns true or false */
392086de 32412+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32413+{
32414+ int valid;
32415+ unsigned int deblk_sz;
32416+ unsigned long ul, n;
32417+ loff_t offset;
32418+ union au_vdir_deblk_p p, deblk_end;
32419+ struct au_vdir *vdir_cache;
32420+
32421+ valid = 1;
32422+ vdir_cache = au_fvdir_cache(file);
32423+ offset = calc_offset(vdir_cache);
32424+ AuDbg("offset %lld\n", offset);
392086de 32425+ if (ctx->pos == offset)
1facf9fc 32426+ goto out;
32427+
32428+ vdir_cache->vd_last.ul = 0;
32429+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32430+ if (!ctx->pos)
1facf9fc 32431+ goto out;
32432+
32433+ valid = 0;
32434+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32435+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32436+ AuDbg("ul %lu\n", ul);
32437+ if (ul >= vdir_cache->vd_nblk)
32438+ goto out;
32439+
32440+ n = vdir_cache->vd_nblk;
32441+ for (; ul < n; ul++) {
32442+ p.deblk = vdir_cache->vd_deblk[ul];
32443+ deblk_end.deblk = p.deblk + deblk_sz;
32444+ offset = ul;
32445+ offset *= deblk_sz;
392086de 32446+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32447+ unsigned int l;
32448+
32449+ l = calc_size(p.de->de_str.len);
32450+ offset += l;
32451+ p.deblk += l;
32452+ }
32453+ if (!is_deblk_end(&p, &deblk_end)) {
32454+ valid = 1;
32455+ vdir_cache->vd_last.ul = ul;
32456+ vdir_cache->vd_last.p = p;
32457+ break;
32458+ }
32459+ }
32460+
4f0767ce 32461+out:
1facf9fc 32462+ /* smp_mb(); */
b00004a5
AM
32463+ if (!valid)
32464+ AuDbg("valid %d\n", !valid);
1facf9fc 32465+ return valid;
32466+}
32467+
392086de 32468+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32469+{
1facf9fc 32470+ unsigned int l, deblk_sz;
32471+ union au_vdir_deblk_p deblk_end;
32472+ struct au_vdir *vdir_cache;
32473+ struct au_vdir_de *de;
32474+
32475+ vdir_cache = au_fvdir_cache(file);
392086de 32476+ if (!seek_vdir(file, ctx))
1facf9fc 32477+ return 0;
32478+
32479+ deblk_sz = vdir_cache->vd_deblk_sz;
32480+ while (1) {
32481+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32482+ deblk_end.deblk += deblk_sz;
32483+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32484+ de = vdir_cache->vd_last.p.de;
32485+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32486+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32487+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32488+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32489+ de->de_str.len, de->de_ino,
32490+ de->de_type))) {
1facf9fc 32491+ /* todo: ignore the error caused by udba? */
32492+ /* return err; */
32493+ return 0;
32494+ }
32495+
32496+ l = calc_size(de->de_str.len);
32497+ vdir_cache->vd_last.p.deblk += l;
392086de 32498+ ctx->pos += l;
1facf9fc 32499+ }
32500+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32501+ vdir_cache->vd_last.ul++;
32502+ vdir_cache->vd_last.p.deblk
32503+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32504+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32505+ continue;
32506+ }
32507+ break;
32508+ }
32509+
32510+ /* smp_mb(); */
32511+ return 0;
32512+}
7f207e10
AM
32513diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32514--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
32515+++ linux/fs/aufs/vfsub.c 2018-07-19 09:46:13.063314675 +0200
32516@@ -0,0 +1,895 @@
32517+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 32518+/*
b00004a5 32519+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 32520+ *
32521+ * This program, aufs is free software; you can redistribute it and/or modify
32522+ * it under the terms of the GNU General Public License as published by
32523+ * the Free Software Foundation; either version 2 of the License, or
32524+ * (at your option) any later version.
dece6358
AM
32525+ *
32526+ * This program is distributed in the hope that it will be useful,
32527+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32528+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32529+ * GNU General Public License for more details.
32530+ *
32531+ * You should have received a copy of the GNU General Public License
523b37e3 32532+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32533+ */
32534+
32535+/*
32536+ * sub-routines for VFS
32537+ */
32538+
8b6a4947 32539+#include <linux/mnt_namespace.h>
dece6358 32540+#include <linux/namei.h>
8cdd5066 32541+#include <linux/nsproxy.h>
dece6358
AM
32542+#include <linux/security.h>
32543+#include <linux/splice.h>
1facf9fc 32544+#include "aufs.h"
32545+
8cdd5066
JR
32546+#ifdef CONFIG_AUFS_BR_FUSE
32547+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32548+{
8cdd5066
JR
32549+ if (!au_test_fuse(h_sb) || !au_userns)
32550+ return 0;
32551+
8b6a4947 32552+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32553+}
32554+#endif
32555+
a2654f78
AM
32556+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32557+{
32558+ int err;
32559+
32560+ lockdep_off();
32561+ down_read(&h_sb->s_umount);
32562+ err = __sync_filesystem(h_sb, wait);
32563+ up_read(&h_sb->s_umount);
32564+ lockdep_on();
32565+
32566+ return err;
32567+}
32568+
8cdd5066
JR
32569+/* ---------------------------------------------------------------------- */
32570+
1facf9fc 32571+int vfsub_update_h_iattr(struct path *h_path, int *did)
32572+{
32573+ int err;
32574+ struct kstat st;
32575+ struct super_block *h_sb;
32576+
32577+ /* for remote fs, leave work for its getattr or d_revalidate */
32578+ /* for bad i_attr fs, handle them in aufs_getattr() */
32579+ /* still some fs may acquire i_mutex. we need to skip them */
32580+ err = 0;
32581+ if (!did)
32582+ did = &err;
32583+ h_sb = h_path->dentry->d_sb;
32584+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32585+ if (*did)
521ced18 32586+ err = vfsub_getattr(h_path, &st);
1facf9fc 32587+
32588+ return err;
32589+}
32590+
32591+/* ---------------------------------------------------------------------- */
32592+
4a4d8108 32593+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32594+{
32595+ struct file *file;
32596+
b4510431 32597+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32598+ current_cred());
2cbb1c4b
JR
32599+ if (!IS_ERR_OR_NULL(file)
32600+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 32601+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 32602+
1308ab2a 32603+ return file;
32604+}
32605+
1facf9fc 32606+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32607+{
32608+ struct file *file;
32609+
2cbb1c4b 32610+ lockdep_off();
7f207e10 32611+ file = filp_open(path,
2cbb1c4b 32612+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32613+ mode);
2cbb1c4b 32614+ lockdep_on();
1facf9fc 32615+ if (IS_ERR(file))
32616+ goto out;
32617+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32618+
4f0767ce 32619+out:
1facf9fc 32620+ return file;
32621+}
32622+
b912730e
AM
32623+/*
32624+ * Ideally this function should call VFS:do_last() in order to keep all its
32625+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32626+ * structure such as nameidata. This is a second (or third) best approach.
32627+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32628+ */
32629+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32630+ struct vfsub_aopen_args *args, struct au_branch *br)
32631+{
32632+ int err;
32633+ struct file *file = args->file;
32634+ /* copied from linux/fs/namei.c:atomic_open() */
32635+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32636+
32637+ IMustLock(dir);
32638+ AuDebugOn(!dir->i_op->atomic_open);
32639+
32640+ err = au_br_test_oflag(args->open_flag, br);
32641+ if (unlikely(err))
32642+ goto out;
32643+
32644+ args->file->f_path.dentry = DENTRY_NOT_SET;
32645+ args->file->f_path.mnt = au_br_mnt(br);
32646+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32647+ args->create_mode, args->opened);
32648+ if (err >= 0) {
32649+ /* some filesystems don't set FILE_CREATED while succeeded? */
32650+ if (*args->opened & FILE_CREATED)
32651+ fsnotify_create(dir, dentry);
32652+ } else
32653+ goto out;
32654+
32655+
32656+ if (!err) {
32657+ /* todo: call VFS:may_open() here */
c4adf169 32658+ err = open_check_o_direct(file);
b912730e
AM
32659+ /* todo: ima_file_check() too? */
32660+ if (!err && (args->open_flag & __FMODE_EXEC))
32661+ err = deny_write_access(file);
32662+ if (unlikely(err))
32663+ /* note that the file is created and still opened */
32664+ goto out;
32665+ }
32666+
5afbbe0d 32667+ au_br_get(br);
b912730e
AM
32668+ fsnotify_open(file);
32669+
32670+out:
32671+ return err;
32672+}
32673+
1facf9fc 32674+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32675+{
32676+ int err;
32677+
1facf9fc 32678+ err = kern_path(name, flags, path);
5527c038 32679+ if (!err && d_is_positive(path->dentry))
1facf9fc 32680+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32681+ return err;
32682+}
32683+
febd17d6
JR
32684+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32685+ struct dentry *parent, int len)
32686+{
32687+ struct path path = {
32688+ .mnt = NULL
32689+ };
32690+
32691+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32692+ if (IS_ERR(path.dentry))
32693+ goto out;
32694+ if (d_is_positive(path.dentry))
32695+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32696+
32697+out:
32698+ AuTraceErrPtr(path.dentry);
32699+ return path.dentry;
32700+}
32701+
1facf9fc 32702+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32703+ int len)
32704+{
32705+ struct path path = {
32706+ .mnt = NULL
32707+ };
32708+
1308ab2a 32709+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 32710+ IMustLock(d_inode(parent));
1facf9fc 32711+
32712+ path.dentry = lookup_one_len(name, parent, len);
32713+ if (IS_ERR(path.dentry))
32714+ goto out;
5527c038 32715+ if (d_is_positive(path.dentry))
1facf9fc 32716+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32717+
4f0767ce 32718+out:
4a4d8108 32719+ AuTraceErrPtr(path.dentry);
1facf9fc 32720+ return path.dentry;
32721+}
32722+
b4510431 32723+void vfsub_call_lkup_one(void *args)
2cbb1c4b 32724+{
b4510431
AM
32725+ struct vfsub_lkup_one_args *a = args;
32726+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
32727+}
32728+
1facf9fc 32729+/* ---------------------------------------------------------------------- */
32730+
32731+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32732+ struct dentry *d2, struct au_hinode *hdir2)
32733+{
32734+ struct dentry *d;
32735+
2cbb1c4b 32736+ lockdep_off();
1facf9fc 32737+ d = lock_rename(d1, d2);
2cbb1c4b 32738+ lockdep_on();
4a4d8108 32739+ au_hn_suspend(hdir1);
1facf9fc 32740+ if (hdir1 != hdir2)
4a4d8108 32741+ au_hn_suspend(hdir2);
1facf9fc 32742+
32743+ return d;
32744+}
32745+
32746+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32747+ struct dentry *d2, struct au_hinode *hdir2)
32748+{
4a4d8108 32749+ au_hn_resume(hdir1);
1facf9fc 32750+ if (hdir1 != hdir2)
4a4d8108 32751+ au_hn_resume(hdir2);
2cbb1c4b 32752+ lockdep_off();
1facf9fc 32753+ unlock_rename(d1, d2);
2cbb1c4b 32754+ lockdep_on();
1facf9fc 32755+}
32756+
32757+/* ---------------------------------------------------------------------- */
32758+
b4510431 32759+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 32760+{
32761+ int err;
32762+ struct dentry *d;
32763+
32764+ IMustLock(dir);
32765+
32766+ d = path->dentry;
32767+ path->dentry = d->d_parent;
b752ccd1 32768+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 32769+ path->dentry = d;
32770+ if (unlikely(err))
32771+ goto out;
32772+
c1595e42 32773+ lockdep_off();
b4510431 32774+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 32775+ lockdep_on();
1facf9fc 32776+ if (!err) {
32777+ struct path tmp = *path;
32778+ int did;
32779+
32780+ vfsub_update_h_iattr(&tmp, &did);
32781+ if (did) {
32782+ tmp.dentry = path->dentry->d_parent;
32783+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32784+ }
32785+ /*ignore*/
32786+ }
32787+
4f0767ce 32788+out:
1facf9fc 32789+ return err;
32790+}
32791+
32792+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32793+{
32794+ int err;
32795+ struct dentry *d;
32796+
32797+ IMustLock(dir);
32798+
32799+ d = path->dentry;
32800+ path->dentry = d->d_parent;
b752ccd1 32801+ err = security_path_symlink(path, d, symname);
1facf9fc 32802+ path->dentry = d;
32803+ if (unlikely(err))
32804+ goto out;
32805+
c1595e42 32806+ lockdep_off();
1facf9fc 32807+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 32808+ lockdep_on();
1facf9fc 32809+ if (!err) {
32810+ struct path tmp = *path;
32811+ int did;
32812+
32813+ vfsub_update_h_iattr(&tmp, &did);
32814+ if (did) {
32815+ tmp.dentry = path->dentry->d_parent;
32816+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32817+ }
32818+ /*ignore*/
32819+ }
32820+
4f0767ce 32821+out:
1facf9fc 32822+ return err;
32823+}
32824+
32825+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32826+{
32827+ int err;
32828+ struct dentry *d;
32829+
32830+ IMustLock(dir);
32831+
32832+ d = path->dentry;
32833+ path->dentry = d->d_parent;
027c5e7a 32834+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 32835+ path->dentry = d;
32836+ if (unlikely(err))
32837+ goto out;
32838+
c1595e42 32839+ lockdep_off();
1facf9fc 32840+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 32841+ lockdep_on();
1facf9fc 32842+ if (!err) {
32843+ struct path tmp = *path;
32844+ int did;
32845+
32846+ vfsub_update_h_iattr(&tmp, &did);
32847+ if (did) {
32848+ tmp.dentry = path->dentry->d_parent;
32849+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32850+ }
32851+ /*ignore*/
32852+ }
32853+
4f0767ce 32854+out:
1facf9fc 32855+ return err;
32856+}
32857+
32858+static int au_test_nlink(struct inode *inode)
32859+{
32860+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32861+
32862+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
32863+ || inode->i_nlink < link_max)
32864+ return 0;
32865+ return -EMLINK;
32866+}
32867+
523b37e3
AM
32868+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32869+ struct inode **delegated_inode)
1facf9fc 32870+{
32871+ int err;
32872+ struct dentry *d;
32873+
32874+ IMustLock(dir);
32875+
5527c038 32876+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 32877+ if (unlikely(err))
32878+ return err;
32879+
b4510431 32880+ /* we don't call may_linkat() */
1facf9fc 32881+ d = path->dentry;
32882+ path->dentry = d->d_parent;
b752ccd1 32883+ err = security_path_link(src_dentry, path, d);
1facf9fc 32884+ path->dentry = d;
32885+ if (unlikely(err))
32886+ goto out;
32887+
2cbb1c4b 32888+ lockdep_off();
523b37e3 32889+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 32890+ lockdep_on();
1facf9fc 32891+ if (!err) {
32892+ struct path tmp = *path;
32893+ int did;
32894+
32895+ /* fuse has different memory inode for the same inumber */
32896+ vfsub_update_h_iattr(&tmp, &did);
32897+ if (did) {
32898+ tmp.dentry = path->dentry->d_parent;
32899+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32900+ tmp.dentry = src_dentry;
32901+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32902+ }
32903+ /*ignore*/
32904+ }
32905+
4f0767ce 32906+out:
1facf9fc 32907+ return err;
32908+}
32909+
32910+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 32911+ struct inode *dir, struct path *path,
f2c43d5f 32912+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 32913+{
32914+ int err;
32915+ struct path tmp = {
32916+ .mnt = path->mnt
32917+ };
32918+ struct dentry *d;
32919+
32920+ IMustLock(dir);
32921+ IMustLock(src_dir);
32922+
32923+ d = path->dentry;
32924+ path->dentry = d->d_parent;
32925+ tmp.dentry = src_dentry->d_parent;
38d290e6 32926+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 32927+ path->dentry = d;
32928+ if (unlikely(err))
32929+ goto out;
32930+
2cbb1c4b 32931+ lockdep_off();
523b37e3 32932+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
f2c43d5f 32933+ delegated_inode, flags);
2cbb1c4b 32934+ lockdep_on();
1facf9fc 32935+ if (!err) {
32936+ int did;
32937+
32938+ tmp.dentry = d->d_parent;
32939+ vfsub_update_h_iattr(&tmp, &did);
32940+ if (did) {
32941+ tmp.dentry = src_dentry;
32942+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32943+ tmp.dentry = src_dentry->d_parent;
32944+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32945+ }
32946+ /*ignore*/
32947+ }
32948+
4f0767ce 32949+out:
1facf9fc 32950+ return err;
32951+}
32952+
32953+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
32954+{
32955+ int err;
32956+ struct dentry *d;
32957+
32958+ IMustLock(dir);
32959+
32960+ d = path->dentry;
32961+ path->dentry = d->d_parent;
b752ccd1 32962+ err = security_path_mkdir(path, d, mode);
1facf9fc 32963+ path->dentry = d;
32964+ if (unlikely(err))
32965+ goto out;
32966+
c1595e42 32967+ lockdep_off();
1facf9fc 32968+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 32969+ lockdep_on();
1facf9fc 32970+ if (!err) {
32971+ struct path tmp = *path;
32972+ int did;
32973+
32974+ vfsub_update_h_iattr(&tmp, &did);
32975+ if (did) {
32976+ tmp.dentry = path->dentry->d_parent;
32977+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32978+ }
32979+ /*ignore*/
32980+ }
32981+
4f0767ce 32982+out:
1facf9fc 32983+ return err;
32984+}
32985+
32986+int vfsub_rmdir(struct inode *dir, struct path *path)
32987+{
32988+ int err;
32989+ struct dentry *d;
32990+
32991+ IMustLock(dir);
32992+
32993+ d = path->dentry;
32994+ path->dentry = d->d_parent;
b752ccd1 32995+ err = security_path_rmdir(path, d);
1facf9fc 32996+ path->dentry = d;
32997+ if (unlikely(err))
32998+ goto out;
32999+
2cbb1c4b 33000+ lockdep_off();
1facf9fc 33001+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 33002+ lockdep_on();
1facf9fc 33003+ if (!err) {
33004+ struct path tmp = {
33005+ .dentry = path->dentry->d_parent,
33006+ .mnt = path->mnt
33007+ };
33008+
33009+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33010+ }
33011+
4f0767ce 33012+out:
1facf9fc 33013+ return err;
33014+}
33015+
33016+/* ---------------------------------------------------------------------- */
33017+
9dbd164d 33018+/* todo: support mmap_sem? */
1facf9fc 33019+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33020+ loff_t *ppos)
33021+{
33022+ ssize_t err;
33023+
2cbb1c4b 33024+ lockdep_off();
1facf9fc 33025+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 33026+ lockdep_on();
1facf9fc 33027+ if (err >= 0)
33028+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33029+ return err;
33030+}
33031+
33032+/* todo: kernel_read()? */
33033+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33034+ loff_t *ppos)
33035+{
33036+ ssize_t err;
33037+ mm_segment_t oldfs;
b752ccd1
AM
33038+ union {
33039+ void *k;
33040+ char __user *u;
33041+ } buf;
1facf9fc 33042+
b752ccd1 33043+ buf.k = kbuf;
1facf9fc 33044+ oldfs = get_fs();
33045+ set_fs(KERNEL_DS);
b752ccd1 33046+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 33047+ set_fs(oldfs);
33048+ return err;
33049+}
33050+
33051+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33052+ loff_t *ppos)
33053+{
33054+ ssize_t err;
33055+
2cbb1c4b 33056+ lockdep_off();
1facf9fc 33057+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 33058+ lockdep_on();
1facf9fc 33059+ if (err >= 0)
33060+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33061+ return err;
33062+}
33063+
33064+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33065+{
33066+ ssize_t err;
33067+ mm_segment_t oldfs;
b752ccd1
AM
33068+ union {
33069+ void *k;
33070+ const char __user *u;
33071+ } buf;
1facf9fc 33072+
b752ccd1 33073+ buf.k = kbuf;
1facf9fc 33074+ oldfs = get_fs();
33075+ set_fs(KERNEL_DS);
b752ccd1 33076+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 33077+ set_fs(oldfs);
33078+ return err;
33079+}
33080+
4a4d8108
AM
33081+int vfsub_flush(struct file *file, fl_owner_t id)
33082+{
33083+ int err;
33084+
33085+ err = 0;
523b37e3 33086+ if (file->f_op->flush) {
2000de60 33087+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
33088+ err = file->f_op->flush(file, id);
33089+ else {
33090+ lockdep_off();
33091+ err = file->f_op->flush(file, id);
33092+ lockdep_on();
33093+ }
4a4d8108
AM
33094+ if (!err)
33095+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33096+ /*ignore*/
33097+ }
33098+ return err;
33099+}
33100+
392086de 33101+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 33102+{
33103+ int err;
33104+
062440b3 33105+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 33106+
2cbb1c4b 33107+ lockdep_off();
392086de 33108+ err = iterate_dir(file, ctx);
2cbb1c4b 33109+ lockdep_on();
1facf9fc 33110+ if (err >= 0)
33111+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 33112+
1facf9fc 33113+ return err;
33114+}
33115+
33116+long vfsub_splice_to(struct file *in, loff_t *ppos,
33117+ struct pipe_inode_info *pipe, size_t len,
33118+ unsigned int flags)
33119+{
33120+ long err;
33121+
2cbb1c4b 33122+ lockdep_off();
0fc653ad 33123+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 33124+ lockdep_on();
4a4d8108 33125+ file_accessed(in);
1facf9fc 33126+ if (err >= 0)
33127+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33128+ return err;
33129+}
33130+
33131+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33132+ loff_t *ppos, size_t len, unsigned int flags)
33133+{
33134+ long err;
33135+
2cbb1c4b 33136+ lockdep_off();
0fc653ad 33137+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33138+ lockdep_on();
1facf9fc 33139+ if (err >= 0)
33140+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33141+ return err;
33142+}
33143+
53392da6
AM
33144+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33145+{
33146+ int err;
33147+
33148+ /* file can be NULL */
33149+ lockdep_off();
33150+ err = vfs_fsync(file, datasync);
33151+ lockdep_on();
33152+ if (!err) {
33153+ if (!path) {
33154+ AuDebugOn(!file);
33155+ path = &file->f_path;
33156+ }
33157+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33158+ }
33159+ return err;
33160+}
33161+
1facf9fc 33162+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33163+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33164+ struct file *h_file)
33165+{
33166+ int err;
33167+ struct inode *h_inode;
c06a8ce3 33168+ struct super_block *h_sb;
1facf9fc 33169+
1facf9fc 33170+ if (!h_file) {
c06a8ce3
AM
33171+ err = vfsub_truncate(h_path, length);
33172+ goto out;
1facf9fc 33173+ }
33174+
5527c038 33175+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33176+ h_sb = h_inode->i_sb;
33177+ lockdep_off();
33178+ sb_start_write(h_sb);
33179+ lockdep_on();
1facf9fc 33180+ err = locks_verify_truncate(h_inode, h_file, length);
33181+ if (!err)
953406b4 33182+ err = security_path_truncate(h_path);
2cbb1c4b
JR
33183+ if (!err) {
33184+ lockdep_off();
1facf9fc 33185+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
33186+ lockdep_on();
33187+ }
c06a8ce3
AM
33188+ lockdep_off();
33189+ sb_end_write(h_sb);
33190+ lockdep_on();
1facf9fc 33191+
4f0767ce 33192+out:
1facf9fc 33193+ return err;
33194+}
33195+
33196+/* ---------------------------------------------------------------------- */
33197+
33198+struct au_vfsub_mkdir_args {
33199+ int *errp;
33200+ struct inode *dir;
33201+ struct path *path;
33202+ int mode;
33203+};
33204+
33205+static void au_call_vfsub_mkdir(void *args)
33206+{
33207+ struct au_vfsub_mkdir_args *a = args;
33208+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33209+}
33210+
33211+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33212+{
33213+ int err, do_sio, wkq_err;
33214+
33215+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33216+ if (!do_sio) {
33217+ lockdep_off();
1facf9fc 33218+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33219+ lockdep_on();
33220+ } else {
1facf9fc 33221+ struct au_vfsub_mkdir_args args = {
33222+ .errp = &err,
33223+ .dir = dir,
33224+ .path = path,
33225+ .mode = mode
33226+ };
33227+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33228+ if (unlikely(wkq_err))
33229+ err = wkq_err;
33230+ }
33231+
33232+ return err;
33233+}
33234+
33235+struct au_vfsub_rmdir_args {
33236+ int *errp;
33237+ struct inode *dir;
33238+ struct path *path;
33239+};
33240+
33241+static void au_call_vfsub_rmdir(void *args)
33242+{
33243+ struct au_vfsub_rmdir_args *a = args;
33244+ *a->errp = vfsub_rmdir(a->dir, a->path);
33245+}
33246+
33247+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33248+{
33249+ int err, do_sio, wkq_err;
33250+
33251+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33252+ if (!do_sio) {
33253+ lockdep_off();
1facf9fc 33254+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33255+ lockdep_on();
33256+ } else {
1facf9fc 33257+ struct au_vfsub_rmdir_args args = {
33258+ .errp = &err,
33259+ .dir = dir,
33260+ .path = path
33261+ };
33262+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33263+ if (unlikely(wkq_err))
33264+ err = wkq_err;
33265+ }
33266+
33267+ return err;
33268+}
33269+
33270+/* ---------------------------------------------------------------------- */
33271+
33272+struct notify_change_args {
33273+ int *errp;
33274+ struct path *path;
33275+ struct iattr *ia;
523b37e3 33276+ struct inode **delegated_inode;
1facf9fc 33277+};
33278+
33279+static void call_notify_change(void *args)
33280+{
33281+ struct notify_change_args *a = args;
33282+ struct inode *h_inode;
33283+
5527c038 33284+ h_inode = d_inode(a->path->dentry);
1facf9fc 33285+ IMustLock(h_inode);
33286+
33287+ *a->errp = -EPERM;
33288+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 33289+ lockdep_off();
523b37e3
AM
33290+ *a->errp = notify_change(a->path->dentry, a->ia,
33291+ a->delegated_inode);
c1595e42 33292+ lockdep_on();
1facf9fc 33293+ if (!*a->errp)
33294+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33295+ }
33296+ AuTraceErr(*a->errp);
33297+}
33298+
523b37e3
AM
33299+int vfsub_notify_change(struct path *path, struct iattr *ia,
33300+ struct inode **delegated_inode)
1facf9fc 33301+{
33302+ int err;
33303+ struct notify_change_args args = {
523b37e3
AM
33304+ .errp = &err,
33305+ .path = path,
33306+ .ia = ia,
33307+ .delegated_inode = delegated_inode
1facf9fc 33308+ };
33309+
33310+ call_notify_change(&args);
33311+
33312+ return err;
33313+}
33314+
523b37e3
AM
33315+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33316+ struct inode **delegated_inode)
1facf9fc 33317+{
33318+ int err, wkq_err;
33319+ struct notify_change_args args = {
523b37e3
AM
33320+ .errp = &err,
33321+ .path = path,
33322+ .ia = ia,
33323+ .delegated_inode = delegated_inode
1facf9fc 33324+ };
33325+
33326+ wkq_err = au_wkq_wait(call_notify_change, &args);
33327+ if (unlikely(wkq_err))
33328+ err = wkq_err;
33329+
33330+ return err;
33331+}
33332+
33333+/* ---------------------------------------------------------------------- */
33334+
33335+struct unlink_args {
33336+ int *errp;
33337+ struct inode *dir;
33338+ struct path *path;
523b37e3 33339+ struct inode **delegated_inode;
1facf9fc 33340+};
33341+
33342+static void call_unlink(void *args)
33343+{
33344+ struct unlink_args *a = args;
33345+ struct dentry *d = a->path->dentry;
33346+ struct inode *h_inode;
33347+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33348+ && au_dcount(d) == 1);
1facf9fc 33349+
33350+ IMustLock(a->dir);
33351+
33352+ a->path->dentry = d->d_parent;
33353+ *a->errp = security_path_unlink(a->path, d);
33354+ a->path->dentry = d;
33355+ if (unlikely(*a->errp))
33356+ return;
33357+
33358+ if (!stop_sillyrename)
33359+ dget(d);
5527c038
JR
33360+ h_inode = NULL;
33361+ if (d_is_positive(d)) {
33362+ h_inode = d_inode(d);
027c5e7a 33363+ ihold(h_inode);
5527c038 33364+ }
1facf9fc 33365+
2cbb1c4b 33366+ lockdep_off();
523b37e3 33367+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 33368+ lockdep_on();
1facf9fc 33369+ if (!*a->errp) {
33370+ struct path tmp = {
33371+ .dentry = d->d_parent,
33372+ .mnt = a->path->mnt
33373+ };
33374+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33375+ }
33376+
33377+ if (!stop_sillyrename)
33378+ dput(d);
33379+ if (h_inode)
33380+ iput(h_inode);
33381+
33382+ AuTraceErr(*a->errp);
33383+}
33384+
33385+/*
33386+ * @dir: must be locked.
33387+ * @dentry: target dentry.
33388+ */
523b37e3
AM
33389+int vfsub_unlink(struct inode *dir, struct path *path,
33390+ struct inode **delegated_inode, int force)
1facf9fc 33391+{
33392+ int err;
33393+ struct unlink_args args = {
523b37e3
AM
33394+ .errp = &err,
33395+ .dir = dir,
33396+ .path = path,
33397+ .delegated_inode = delegated_inode
1facf9fc 33398+ };
33399+
33400+ if (!force)
33401+ call_unlink(&args);
33402+ else {
33403+ int wkq_err;
33404+
33405+ wkq_err = au_wkq_wait(call_unlink, &args);
33406+ if (unlikely(wkq_err))
33407+ err = wkq_err;
33408+ }
33409+
33410+ return err;
33411+}
7f207e10
AM
33412diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33413--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
33414+++ linux/fs/aufs/vfsub.h 2018-07-19 09:46:13.063314675 +0200
33415@@ -0,0 +1,355 @@
33416+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33417+/*
b00004a5 33418+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 33419+ *
33420+ * This program, aufs is free software; you can redistribute it and/or modify
33421+ * it under the terms of the GNU General Public License as published by
33422+ * the Free Software Foundation; either version 2 of the License, or
33423+ * (at your option) any later version.
dece6358
AM
33424+ *
33425+ * This program is distributed in the hope that it will be useful,
33426+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33427+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33428+ * GNU General Public License for more details.
33429+ *
33430+ * You should have received a copy of the GNU General Public License
523b37e3 33431+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33432+ */
33433+
33434+/*
33435+ * sub-routines for VFS
33436+ */
33437+
33438+#ifndef __AUFS_VFSUB_H__
33439+#define __AUFS_VFSUB_H__
33440+
33441+#ifdef __KERNEL__
33442+
33443+#include <linux/fs.h>
be118d29 33444+#include <linux/iversion.h>
b4510431 33445+#include <linux/mount.h>
8cdd5066 33446+#include <linux/posix_acl.h>
c1595e42 33447+#include <linux/xattr.h>
7f207e10 33448+#include "debug.h"
1facf9fc 33449+
7f207e10 33450+/* copied from linux/fs/internal.h */
2cbb1c4b 33451+/* todo: BAD approach!! */
c06a8ce3 33452+extern void __mnt_drop_write(struct vfsmount *);
c4adf169 33453+extern int open_check_o_direct(struct file *f);
7f207e10
AM
33454+
33455+/* ---------------------------------------------------------------------- */
1facf9fc 33456+
33457+/* lock subclass for lower inode */
33458+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33459+/* reduce? gave up. */
33460+enum {
c1595e42 33461+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33462+ AuLsc_I_PARENT, /* lower inode, parent first */
33463+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33464+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33465+ AuLsc_I_CHILD,
33466+ AuLsc_I_CHILD2,
33467+ AuLsc_I_End
33468+};
33469+
33470+/* to debug easier, do not make them inlined functions */
33471+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33472+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33473+
33474+/* ---------------------------------------------------------------------- */
33475+
7f207e10
AM
33476+static inline void vfsub_drop_nlink(struct inode *inode)
33477+{
33478+ AuDebugOn(!inode->i_nlink);
33479+ drop_nlink(inode);
33480+}
33481+
027c5e7a
AM
33482+static inline void vfsub_dead_dir(struct inode *inode)
33483+{
33484+ AuDebugOn(!S_ISDIR(inode->i_mode));
33485+ inode->i_flags |= S_DEAD;
33486+ clear_nlink(inode);
33487+}
33488+
392086de
AM
33489+static inline int vfsub_native_ro(struct inode *inode)
33490+{
8b6a4947 33491+ return sb_rdonly(inode->i_sb)
392086de
AM
33492+ || IS_RDONLY(inode)
33493+ /* || IS_APPEND(inode) */
33494+ || IS_IMMUTABLE(inode);
33495+}
33496+
8cdd5066
JR
33497+#ifdef CONFIG_AUFS_BR_FUSE
33498+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33499+#else
33500+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33501+#endif
33502+
a2654f78
AM
33503+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33504+
7f207e10
AM
33505+/* ---------------------------------------------------------------------- */
33506+
33507+int vfsub_update_h_iattr(struct path *h_path, int *did);
33508+struct file *vfsub_dentry_open(struct path *path, int flags);
33509+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
b912730e
AM
33510+struct vfsub_aopen_args {
33511+ struct file *file;
33512+ unsigned int open_flag;
33513+ umode_t create_mode;
33514+ int *opened;
33515+};
33516+struct au_branch;
33517+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33518+ struct vfsub_aopen_args *args, struct au_branch *br);
1facf9fc 33519+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33520+
febd17d6
JR
33521+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33522+ struct dentry *parent, int len);
1facf9fc 33523+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33524+ int len);
b4510431
AM
33525+
33526+struct vfsub_lkup_one_args {
33527+ struct dentry **errp;
33528+ struct qstr *name;
33529+ struct dentry *parent;
33530+};
33531+
33532+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33533+ struct dentry *parent)
33534+{
33535+ return vfsub_lookup_one_len(name->name, parent, name->len);
33536+}
33537+
33538+void vfsub_call_lkup_one(void *args);
33539+
33540+/* ---------------------------------------------------------------------- */
33541+
33542+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33543+{
33544+ int err;
076b876e 33545+
b4510431
AM
33546+ lockdep_off();
33547+ err = mnt_want_write(mnt);
33548+ lockdep_on();
33549+ return err;
33550+}
33551+
33552+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33553+{
33554+ lockdep_off();
33555+ mnt_drop_write(mnt);
33556+ lockdep_on();
33557+}
1facf9fc 33558+
7e9cd9fe 33559+#if 0 /* reserved */
c06a8ce3
AM
33560+static inline void vfsub_mnt_drop_write_file(struct file *file)
33561+{
33562+ lockdep_off();
33563+ mnt_drop_write_file(file);
33564+ lockdep_on();
33565+}
7e9cd9fe 33566+#endif
c06a8ce3 33567+
1facf9fc 33568+/* ---------------------------------------------------------------------- */
33569+
33570+struct au_hinode;
33571+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33572+ struct dentry *d2, struct au_hinode *hdir2);
33573+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33574+ struct dentry *d2, struct au_hinode *hdir2);
33575+
537831f9
AM
33576+int vfsub_create(struct inode *dir, struct path *path, int mode,
33577+ bool want_excl);
1facf9fc 33578+int vfsub_symlink(struct inode *dir, struct path *path,
33579+ const char *symname);
33580+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33581+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33582+ struct path *path, struct inode **delegated_inode);
1facf9fc 33583+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33584+ struct inode *hdir, struct path *path,
f2c43d5f 33585+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33586+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33587+int vfsub_rmdir(struct inode *dir, struct path *path);
33588+
33589+/* ---------------------------------------------------------------------- */
33590+
33591+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33592+ loff_t *ppos);
33593+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33594+ loff_t *ppos);
33595+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33596+ loff_t *ppos);
33597+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33598+ loff_t *ppos);
4a4d8108 33599+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33600+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33601+
c06a8ce3
AM
33602+static inline loff_t vfsub_f_size_read(struct file *file)
33603+{
33604+ return i_size_read(file_inode(file));
33605+}
33606+
4a4d8108
AM
33607+static inline unsigned int vfsub_file_flags(struct file *file)
33608+{
33609+ unsigned int flags;
33610+
33611+ spin_lock(&file->f_lock);
33612+ flags = file->f_flags;
33613+ spin_unlock(&file->f_lock);
33614+
33615+ return flags;
33616+}
1308ab2a 33617+
f0c0a007
AM
33618+static inline int vfsub_file_execed(struct file *file)
33619+{
33620+ /* todo: direct access f_flags */
33621+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33622+}
33623+
7e9cd9fe 33624+#if 0 /* reserved */
1facf9fc 33625+static inline void vfsub_file_accessed(struct file *h_file)
33626+{
33627+ file_accessed(h_file);
33628+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33629+}
7e9cd9fe 33630+#endif
1facf9fc 33631+
79b8bda9 33632+#if 0 /* reserved */
1facf9fc 33633+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33634+ struct dentry *h_dentry)
33635+{
33636+ struct path h_path = {
33637+ .dentry = h_dentry,
33638+ .mnt = h_mnt
33639+ };
92d182d2 33640+ touch_atime(&h_path);
1facf9fc 33641+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33642+}
79b8bda9 33643+#endif
1facf9fc 33644+
0c3ec466
AM
33645+static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
33646+ int flags)
33647+{
5afbbe0d 33648+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33649+ /* no vfsub_update_h_iattr() since we don't have struct path */
33650+}
33651+
8cdd5066
JR
33652+#ifdef CONFIG_FS_POSIX_ACL
33653+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33654+{
33655+ int err;
33656+
33657+ err = posix_acl_chmod(h_inode, h_mode);
33658+ if (err == -EOPNOTSUPP)
33659+ err = 0;
33660+ return err;
33661+}
33662+#else
33663+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33664+#endif
33665+
4a4d8108
AM
33666+long vfsub_splice_to(struct file *in, loff_t *ppos,
33667+ struct pipe_inode_info *pipe, size_t len,
33668+ unsigned int flags);
33669+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33670+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33671+
33672+static inline long vfsub_truncate(struct path *path, loff_t length)
33673+{
33674+ long err;
076b876e 33675+
c06a8ce3
AM
33676+ lockdep_off();
33677+ err = vfs_truncate(path, length);
33678+ lockdep_on();
33679+ return err;
33680+}
33681+
4a4d8108
AM
33682+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33683+ struct file *h_file);
53392da6 33684+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 33685+
521ced18
JR
33686+/*
33687+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33688+ * ioctl.
33689+ */
33690+static inline int vfsub_clone_file_range(struct file *src, struct file *dst,
33691+ u64 len)
33692+{
33693+ int err;
33694+
33695+ lockdep_off();
33696+ err = vfs_clone_file_range(src, 0, dst, 0, len);
33697+ lockdep_on();
33698+
33699+ return err;
33700+}
33701+
33702+/* copy_file_range(2) is a systemcall */
33703+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33704+ struct file *dst, loff_t dst_pos,
33705+ size_t len, unsigned int flags)
33706+{
33707+ ssize_t ssz;
33708+
33709+ lockdep_off();
33710+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33711+ lockdep_on();
33712+
33713+ return ssz;
33714+}
33715+
1facf9fc 33716+/* ---------------------------------------------------------------------- */
33717+
33718+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33719+{
33720+ loff_t err;
33721+
2cbb1c4b 33722+ lockdep_off();
1facf9fc 33723+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 33724+ lockdep_on();
1facf9fc 33725+ return err;
33726+}
33727+
33728+/* ---------------------------------------------------------------------- */
33729+
4a4d8108
AM
33730+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33731+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
33732+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33733+ struct inode **delegated_inode);
33734+int vfsub_notify_change(struct path *path, struct iattr *ia,
33735+ struct inode **delegated_inode);
33736+int vfsub_unlink(struct inode *dir, struct path *path,
33737+ struct inode **delegated_inode, int force);
4a4d8108 33738+
521ced18
JR
33739+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33740+{
33741+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33742+}
33743+
c1595e42
JR
33744+/* ---------------------------------------------------------------------- */
33745+
33746+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33747+ const void *value, size_t size, int flags)
33748+{
33749+ int err;
33750+
33751+ lockdep_off();
33752+ err = vfs_setxattr(dentry, name, value, size, flags);
33753+ lockdep_on();
33754+
33755+ return err;
33756+}
33757+
33758+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33759+{
33760+ int err;
33761+
33762+ lockdep_off();
33763+ err = vfs_removexattr(dentry, name);
33764+ lockdep_on();
33765+
33766+ return err;
33767+}
33768+
1facf9fc 33769+#endif /* __KERNEL__ */
33770+#endif /* __AUFS_VFSUB_H__ */
7f207e10
AM
33771diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33772--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
33773+++ linux/fs/aufs/wbr_policy.c 2018-07-19 09:46:13.063314675 +0200
33774@@ -0,0 +1,831 @@
33775+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33776+/*
b00004a5 33777+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 33778+ *
33779+ * This program, aufs is free software; you can redistribute it and/or modify
33780+ * it under the terms of the GNU General Public License as published by
33781+ * the Free Software Foundation; either version 2 of the License, or
33782+ * (at your option) any later version.
dece6358
AM
33783+ *
33784+ * This program is distributed in the hope that it will be useful,
33785+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33786+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33787+ * GNU General Public License for more details.
33788+ *
33789+ * You should have received a copy of the GNU General Public License
523b37e3 33790+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33791+ */
33792+
33793+/*
33794+ * policies for selecting one among multiple writable branches
33795+ */
33796+
33797+#include <linux/statfs.h>
33798+#include "aufs.h"
33799+
33800+/* subset of cpup_attr() */
33801+static noinline_for_stack
33802+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33803+{
33804+ int err, sbits;
33805+ struct iattr ia;
33806+ struct inode *h_isrc;
33807+
5527c038 33808+ h_isrc = d_inode(h_src);
1facf9fc 33809+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33810+ ia.ia_mode = h_isrc->i_mode;
33811+ ia.ia_uid = h_isrc->i_uid;
33812+ ia.ia_gid = h_isrc->i_gid;
33813+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 33814+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
33815+ /* no delegation since it is just created */
33816+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33817+
33818+ /* is this nfs only? */
33819+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33820+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33821+ ia.ia_mode = h_isrc->i_mode;
523b37e3 33822+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33823+ }
33824+
33825+ return err;
33826+}
33827+
33828+#define AuCpdown_PARENT_OPQ 1
33829+#define AuCpdown_WHED (1 << 1)
33830+#define AuCpdown_MADE_DIR (1 << 2)
33831+#define AuCpdown_DIROPQ (1 << 3)
33832+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
33833+#define au_fset_cpdown(flags, name) \
33834+ do { (flags) |= AuCpdown_##name; } while (0)
33835+#define au_fclr_cpdown(flags, name) \
33836+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 33837+
1facf9fc 33838+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 33839+ unsigned int *flags)
1facf9fc 33840+{
33841+ int err;
33842+ struct dentry *opq_dentry;
33843+
33844+ opq_dentry = au_diropq_create(dentry, bdst);
33845+ err = PTR_ERR(opq_dentry);
33846+ if (IS_ERR(opq_dentry))
33847+ goto out;
33848+ dput(opq_dentry);
c2b27bf2 33849+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 33850+
4f0767ce 33851+out:
1facf9fc 33852+ return err;
33853+}
33854+
33855+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33856+ struct inode *dir, aufs_bindex_t bdst)
33857+{
33858+ int err;
33859+ struct path h_path;
33860+ struct au_branch *br;
33861+
33862+ br = au_sbr(dentry->d_sb, bdst);
33863+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
33864+ err = PTR_ERR(h_path.dentry);
33865+ if (IS_ERR(h_path.dentry))
33866+ goto out;
33867+
33868+ err = 0;
5527c038 33869+ if (d_is_positive(h_path.dentry)) {
86dc4139 33870+ h_path.mnt = au_br_mnt(br);
1facf9fc 33871+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
33872+ dentry);
33873+ }
33874+ dput(h_path.dentry);
33875+
4f0767ce 33876+out:
1facf9fc 33877+ return err;
33878+}
33879+
33880+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 33881+ struct au_pin *pin,
1facf9fc 33882+ struct dentry *h_parent, void *arg)
33883+{
33884+ int err, rerr;
5afbbe0d 33885+ aufs_bindex_t bopq, btop;
1facf9fc 33886+ struct path h_path;
33887+ struct dentry *parent;
33888+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 33889+ unsigned int *flags = arg;
1facf9fc 33890+
5afbbe0d 33891+ btop = au_dbtop(dentry);
1facf9fc 33892+ /* dentry is di-locked */
33893+ parent = dget_parent(dentry);
5527c038
JR
33894+ dir = d_inode(parent);
33895+ h_dir = d_inode(h_parent);
1facf9fc 33896+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
33897+ IMustLock(h_dir);
33898+
86dc4139 33899+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 33900+ if (unlikely(err < 0))
33901+ goto out;
33902+ h_path.dentry = au_h_dptr(dentry, bdst);
33903+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
33904+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
33905+ S_IRWXU | S_IRUGO | S_IXUGO);
33906+ if (unlikely(err))
33907+ goto out_put;
c2b27bf2 33908+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 33909+
1facf9fc 33910+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
33911+ au_fclr_cpdown(*flags, WHED);
33912+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 33913+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
33914+ au_fset_cpdown(*flags, WHED);
33915+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
33916+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 33917+ h_inode = d_inode(h_path.dentry);
febd17d6 33918+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
33919+ if (au_ftest_cpdown(*flags, WHED)) {
33920+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 33921+ if (unlikely(err)) {
febd17d6 33922+ inode_unlock(h_inode);
1facf9fc 33923+ goto out_dir;
33924+ }
33925+ }
33926+
5afbbe0d 33927+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 33928+ inode_unlock(h_inode);
1facf9fc 33929+ if (unlikely(err))
33930+ goto out_opq;
33931+
c2b27bf2 33932+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 33933+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
33934+ if (unlikely(err))
33935+ goto out_opq;
33936+ }
33937+
5527c038 33938+ inode = d_inode(dentry);
5afbbe0d
AM
33939+ if (au_ibbot(inode) < bdst)
33940+ au_set_ibbot(inode, bdst);
1facf9fc 33941+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
33942+ au_hi_flags(inode, /*isdir*/1));
076b876e 33943+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 33944+ goto out; /* success */
33945+
33946+ /* revert */
4f0767ce 33947+out_opq:
c2b27bf2 33948+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 33949+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 33950+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 33951+ inode_unlock(h_inode);
1facf9fc 33952+ if (unlikely(rerr)) {
523b37e3
AM
33953+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
33954+ dentry, bdst, rerr);
1facf9fc 33955+ err = -EIO;
33956+ goto out;
33957+ }
33958+ }
4f0767ce 33959+out_dir:
c2b27bf2 33960+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 33961+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
33962+ if (unlikely(rerr)) {
523b37e3
AM
33963+ AuIOErr("failed removing %pd b%d (%d)\n",
33964+ dentry, bdst, rerr);
1facf9fc 33965+ err = -EIO;
33966+ }
33967+ }
4f0767ce 33968+out_put:
1facf9fc 33969+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
33970+ if (au_dbbot(dentry) == bdst)
33971+ au_update_dbbot(dentry);
4f0767ce 33972+out:
1facf9fc 33973+ dput(parent);
33974+ return err;
33975+}
33976+
33977+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
33978+{
33979+ int err;
c2b27bf2 33980+ unsigned int flags;
1facf9fc 33981+
c2b27bf2
AM
33982+ flags = 0;
33983+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 33984+
33985+ return err;
33986+}
33987+
33988+/* ---------------------------------------------------------------------- */
33989+
33990+/* policies for create */
33991+
c2b27bf2 33992+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
33993+{
33994+ int err, i, j, ndentry;
33995+ aufs_bindex_t bopq;
33996+ struct au_dcsub_pages dpages;
33997+ struct au_dpage *dpage;
33998+ struct dentry **dentries, *parent, *d;
33999+
34000+ err = au_dpages_init(&dpages, GFP_NOFS);
34001+ if (unlikely(err))
34002+ goto out;
34003+ parent = dget_parent(dentry);
027c5e7a 34004+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
34005+ if (unlikely(err))
34006+ goto out_free;
34007+
34008+ err = bindex;
34009+ for (i = 0; i < dpages.ndpage; i++) {
34010+ dpage = dpages.dpages + i;
34011+ dentries = dpage->dentries;
34012+ ndentry = dpage->ndentry;
34013+ for (j = 0; j < ndentry; j++) {
34014+ d = dentries[j];
34015+ di_read_lock_parent2(d, !AuLock_IR);
34016+ bopq = au_dbdiropq(d);
34017+ di_read_unlock(d, !AuLock_IR);
34018+ if (bopq >= 0 && bopq < err)
34019+ err = bopq;
34020+ }
34021+ }
34022+
34023+out_free:
34024+ dput(parent);
34025+ au_dpages_free(&dpages);
34026+out:
34027+ return err;
34028+}
34029+
1facf9fc 34030+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34031+{
34032+ for (; bindex >= 0; bindex--)
34033+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34034+ return bindex;
34035+ return -EROFS;
34036+}
34037+
34038+/* top down parent */
392086de
AM
34039+static int au_wbr_create_tdp(struct dentry *dentry,
34040+ unsigned int flags __maybe_unused)
1facf9fc 34041+{
34042+ int err;
5afbbe0d 34043+ aufs_bindex_t btop, bindex;
1facf9fc 34044+ struct super_block *sb;
34045+ struct dentry *parent, *h_parent;
34046+
34047+ sb = dentry->d_sb;
5afbbe0d
AM
34048+ btop = au_dbtop(dentry);
34049+ err = btop;
34050+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 34051+ goto out;
34052+
34053+ err = -EROFS;
34054+ parent = dget_parent(dentry);
5afbbe0d 34055+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 34056+ h_parent = au_h_dptr(parent, bindex);
5527c038 34057+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34058+ continue;
34059+
34060+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34061+ err = bindex;
34062+ break;
34063+ }
34064+ }
34065+ dput(parent);
34066+
34067+ /* bottom up here */
4a4d8108 34068+ if (unlikely(err < 0)) {
5afbbe0d 34069+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
34070+ if (err >= 0)
34071+ err = au_wbr_nonopq(dentry, err);
34072+ }
1facf9fc 34073+
4f0767ce 34074+out:
1facf9fc 34075+ AuDbg("b%d\n", err);
34076+ return err;
34077+}
34078+
34079+/* ---------------------------------------------------------------------- */
34080+
34081+/* an exception for the policy other than tdp */
34082+static int au_wbr_create_exp(struct dentry *dentry)
34083+{
34084+ int err;
34085+ aufs_bindex_t bwh, bdiropq;
34086+ struct dentry *parent;
34087+
34088+ err = -1;
34089+ bwh = au_dbwh(dentry);
34090+ parent = dget_parent(dentry);
34091+ bdiropq = au_dbdiropq(parent);
34092+ if (bwh >= 0) {
34093+ if (bdiropq >= 0)
34094+ err = min(bdiropq, bwh);
34095+ else
34096+ err = bwh;
34097+ AuDbg("%d\n", err);
34098+ } else if (bdiropq >= 0) {
34099+ err = bdiropq;
34100+ AuDbg("%d\n", err);
34101+ }
34102+ dput(parent);
34103+
4a4d8108
AM
34104+ if (err >= 0)
34105+ err = au_wbr_nonopq(dentry, err);
34106+
1facf9fc 34107+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34108+ err = -1;
34109+
34110+ AuDbg("%d\n", err);
34111+ return err;
34112+}
34113+
34114+/* ---------------------------------------------------------------------- */
34115+
34116+/* round robin */
34117+static int au_wbr_create_init_rr(struct super_block *sb)
34118+{
34119+ int err;
34120+
5afbbe0d 34121+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 34122+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 34123+ /* smp_mb(); */
1facf9fc 34124+
34125+ AuDbg("b%d\n", err);
34126+ return err;
34127+}
34128+
392086de 34129+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 34130+{
34131+ int err, nbr;
34132+ unsigned int u;
5afbbe0d 34133+ aufs_bindex_t bindex, bbot;
1facf9fc 34134+ struct super_block *sb;
34135+ atomic_t *next;
34136+
34137+ err = au_wbr_create_exp(dentry);
34138+ if (err >= 0)
34139+ goto out;
34140+
34141+ sb = dentry->d_sb;
34142+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34143+ bbot = au_sbbot(sb);
34144+ nbr = bbot + 1;
34145+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34146+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34147+ err = atomic_dec_return(next) + 1;
34148+ /* modulo for 0 is meaningless */
34149+ if (unlikely(!err))
34150+ err = atomic_dec_return(next) + 1;
34151+ } else
34152+ err = atomic_read(next);
34153+ AuDbg("%d\n", err);
34154+ u = err;
34155+ err = u % nbr;
34156+ AuDbg("%d\n", err);
34157+ if (!au_br_rdonly(au_sbr(sb, err)))
34158+ break;
34159+ err = -EROFS;
34160+ }
34161+
4a4d8108
AM
34162+ if (err >= 0)
34163+ err = au_wbr_nonopq(dentry, err);
34164+
4f0767ce 34165+out:
1facf9fc 34166+ AuDbg("%d\n", err);
34167+ return err;
34168+}
34169+
34170+/* ---------------------------------------------------------------------- */
34171+
34172+/* most free space */
392086de 34173+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34174+{
34175+ struct super_block *sb;
34176+ struct au_branch *br;
34177+ struct au_wbr_mfs *mfs;
392086de 34178+ struct dentry *h_parent;
5afbbe0d 34179+ aufs_bindex_t bindex, bbot;
1facf9fc 34180+ int err;
34181+ unsigned long long b, bavail;
7f207e10 34182+ struct path h_path;
1facf9fc 34183+ /* reduce the stack usage */
34184+ struct kstatfs *st;
34185+
34186+ st = kmalloc(sizeof(*st), GFP_NOFS);
34187+ if (unlikely(!st)) {
34188+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34189+ return;
34190+ }
34191+
34192+ bavail = 0;
34193+ sb = dentry->d_sb;
34194+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34195+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34196+ mfs->mfs_bindex = -EROFS;
34197+ mfs->mfsrr_bytes = 0;
392086de
AM
34198+ if (!parent) {
34199+ bindex = 0;
5afbbe0d 34200+ bbot = au_sbbot(sb);
392086de 34201+ } else {
5afbbe0d
AM
34202+ bindex = au_dbtop(parent);
34203+ bbot = au_dbtaildir(parent);
392086de
AM
34204+ }
34205+
5afbbe0d 34206+ for (; bindex <= bbot; bindex++) {
392086de
AM
34207+ if (parent) {
34208+ h_parent = au_h_dptr(parent, bindex);
5527c038 34209+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34210+ continue;
34211+ }
1facf9fc 34212+ br = au_sbr(sb, bindex);
34213+ if (au_br_rdonly(br))
34214+ continue;
34215+
34216+ /* sb->s_root for NFS is unreliable */
86dc4139 34217+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34218+ h_path.dentry = h_path.mnt->mnt_root;
34219+ err = vfs_statfs(&h_path, st);
1facf9fc 34220+ if (unlikely(err)) {
34221+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34222+ continue;
34223+ }
34224+
34225+ /* when the available size is equal, select the lower one */
34226+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34227+ || sizeof(b) < sizeof(st->f_bsize));
34228+ b = st->f_bavail * st->f_bsize;
34229+ br->br_wbr->wbr_bytes = b;
34230+ if (b >= bavail) {
34231+ bavail = b;
34232+ mfs->mfs_bindex = bindex;
34233+ mfs->mfs_jiffy = jiffies;
34234+ }
34235+ }
34236+
34237+ mfs->mfsrr_bytes = bavail;
34238+ AuDbg("b%d\n", mfs->mfs_bindex);
1c60b727 34239+ kfree(st);
1facf9fc 34240+}
34241+
392086de 34242+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34243+{
34244+ int err;
392086de 34245+ struct dentry *parent;
1facf9fc 34246+ struct super_block *sb;
34247+ struct au_wbr_mfs *mfs;
34248+
34249+ err = au_wbr_create_exp(dentry);
34250+ if (err >= 0)
34251+ goto out;
34252+
34253+ sb = dentry->d_sb;
392086de
AM
34254+ parent = NULL;
34255+ if (au_ftest_wbr(flags, PARENT))
34256+ parent = dget_parent(dentry);
1facf9fc 34257+ mfs = &au_sbi(sb)->si_wbr_mfs;
34258+ mutex_lock(&mfs->mfs_lock);
34259+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34260+ || mfs->mfs_bindex < 0
34261+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34262+ au_mfs(dentry, parent);
1facf9fc 34263+ mutex_unlock(&mfs->mfs_lock);
34264+ err = mfs->mfs_bindex;
392086de 34265+ dput(parent);
1facf9fc 34266+
4a4d8108
AM
34267+ if (err >= 0)
34268+ err = au_wbr_nonopq(dentry, err);
34269+
4f0767ce 34270+out:
1facf9fc 34271+ AuDbg("b%d\n", err);
34272+ return err;
34273+}
34274+
34275+static int au_wbr_create_init_mfs(struct super_block *sb)
34276+{
34277+ struct au_wbr_mfs *mfs;
34278+
34279+ mfs = &au_sbi(sb)->si_wbr_mfs;
34280+ mutex_init(&mfs->mfs_lock);
34281+ mfs->mfs_jiffy = 0;
34282+ mfs->mfs_bindex = -EROFS;
34283+
34284+ return 0;
34285+}
34286+
34287+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34288+{
34289+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34290+ return 0;
34291+}
34292+
34293+/* ---------------------------------------------------------------------- */
34294+
f2c43d5f
AM
34295+/* top down regardless parent, and then mfs */
34296+static int au_wbr_create_tdmfs(struct dentry *dentry,
34297+ unsigned int flags __maybe_unused)
34298+{
34299+ int err;
34300+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34301+ unsigned long long watermark;
34302+ struct super_block *sb;
34303+ struct au_wbr_mfs *mfs;
34304+ struct au_branch *br;
34305+ struct dentry *parent;
34306+
34307+ sb = dentry->d_sb;
34308+ mfs = &au_sbi(sb)->si_wbr_mfs;
34309+ mutex_lock(&mfs->mfs_lock);
34310+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34311+ || mfs->mfs_bindex < 0)
34312+ au_mfs(dentry, /*parent*/NULL);
34313+ watermark = mfs->mfsrr_watermark;
34314+ bmfs = mfs->mfs_bindex;
34315+ mutex_unlock(&mfs->mfs_lock);
34316+
34317+ /* another style of au_wbr_create_exp() */
34318+ bwh = au_dbwh(dentry);
34319+ parent = dget_parent(dentry);
34320+ btail = au_dbtaildir(parent);
34321+ if (bwh >= 0 && bwh < btail)
34322+ btail = bwh;
34323+
34324+ err = au_wbr_nonopq(dentry, btail);
34325+ if (unlikely(err < 0))
34326+ goto out;
34327+ btail = err;
34328+ bfound = -1;
34329+ for (bindex = 0; bindex <= btail; bindex++) {
34330+ br = au_sbr(sb, bindex);
34331+ if (au_br_rdonly(br))
34332+ continue;
34333+ if (br->br_wbr->wbr_bytes > watermark) {
34334+ bfound = bindex;
34335+ break;
34336+ }
34337+ }
34338+ err = bfound;
34339+ if (err < 0)
34340+ err = bmfs;
34341+
34342+out:
34343+ dput(parent);
34344+ AuDbg("b%d\n", err);
34345+ return err;
34346+}
34347+
34348+/* ---------------------------------------------------------------------- */
34349+
1facf9fc 34350+/* most free space and then round robin */
392086de 34351+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34352+{
34353+ int err;
34354+ struct au_wbr_mfs *mfs;
34355+
392086de 34356+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34357+ if (err >= 0) {
34358+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34359+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34360+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34361+ err = au_wbr_create_rr(dentry, flags);
dece6358 34362+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34363+ }
34364+
34365+ AuDbg("b%d\n", err);
34366+ return err;
34367+}
34368+
34369+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34370+{
34371+ int err;
34372+
34373+ au_wbr_create_init_mfs(sb); /* ignore */
34374+ err = au_wbr_create_init_rr(sb);
34375+
34376+ return err;
34377+}
34378+
34379+/* ---------------------------------------------------------------------- */
34380+
34381+/* top down parent and most free space */
392086de 34382+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34383+{
34384+ int err, e2;
34385+ unsigned long long b;
5afbbe0d 34386+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34387+ struct super_block *sb;
34388+ struct dentry *parent, *h_parent;
34389+ struct au_branch *br;
34390+
392086de 34391+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34392+ if (unlikely(err < 0))
34393+ goto out;
34394+ parent = dget_parent(dentry);
5afbbe0d
AM
34395+ btop = au_dbtop(parent);
34396+ bbot = au_dbtaildir(parent);
34397+ if (btop == bbot)
1facf9fc 34398+ goto out_parent; /* success */
34399+
392086de 34400+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34401+ if (e2 < 0)
34402+ goto out_parent; /* success */
34403+
34404+ /* when the available size is equal, select upper one */
34405+ sb = dentry->d_sb;
34406+ br = au_sbr(sb, err);
34407+ b = br->br_wbr->wbr_bytes;
34408+ AuDbg("b%d, %llu\n", err, b);
34409+
5afbbe0d 34410+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34411+ h_parent = au_h_dptr(parent, bindex);
5527c038 34412+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34413+ continue;
34414+
34415+ br = au_sbr(sb, bindex);
34416+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34417+ b = br->br_wbr->wbr_bytes;
34418+ err = bindex;
34419+ AuDbg("b%d, %llu\n", err, b);
34420+ }
34421+ }
34422+
4a4d8108
AM
34423+ if (err >= 0)
34424+ err = au_wbr_nonopq(dentry, err);
34425+
4f0767ce 34426+out_parent:
1facf9fc 34427+ dput(parent);
4f0767ce 34428+out:
1facf9fc 34429+ AuDbg("b%d\n", err);
34430+ return err;
34431+}
34432+
34433+/* ---------------------------------------------------------------------- */
34434+
392086de
AM
34435+/*
34436+ * - top down parent
34437+ * - most free space with parent
34438+ * - most free space round-robin regardless parent
34439+ */
34440+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34441+{
34442+ int err;
34443+ unsigned long long watermark;
34444+ struct super_block *sb;
34445+ struct au_branch *br;
34446+ struct au_wbr_mfs *mfs;
34447+
34448+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34449+ if (unlikely(err < 0))
34450+ goto out;
34451+
34452+ sb = dentry->d_sb;
34453+ br = au_sbr(sb, err);
34454+ mfs = &au_sbi(sb)->si_wbr_mfs;
34455+ mutex_lock(&mfs->mfs_lock);
34456+ watermark = mfs->mfsrr_watermark;
34457+ mutex_unlock(&mfs->mfs_lock);
34458+ if (br->br_wbr->wbr_bytes < watermark)
34459+ /* regardless the parent dir */
34460+ err = au_wbr_create_mfsrr(dentry, flags);
34461+
34462+out:
34463+ AuDbg("b%d\n", err);
34464+ return err;
34465+}
34466+
34467+/* ---------------------------------------------------------------------- */
34468+
1facf9fc 34469+/* policies for copyup */
34470+
34471+/* top down parent */
34472+static int au_wbr_copyup_tdp(struct dentry *dentry)
34473+{
392086de 34474+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34475+}
34476+
34477+/* bottom up parent */
34478+static int au_wbr_copyup_bup(struct dentry *dentry)
34479+{
34480+ int err;
5afbbe0d 34481+ aufs_bindex_t bindex, btop;
1facf9fc 34482+ struct dentry *parent, *h_parent;
34483+ struct super_block *sb;
34484+
34485+ err = -EROFS;
34486+ sb = dentry->d_sb;
34487+ parent = dget_parent(dentry);
5afbbe0d
AM
34488+ btop = au_dbtop(parent);
34489+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34490+ h_parent = au_h_dptr(parent, bindex);
5527c038 34491+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34492+ continue;
34493+
34494+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34495+ err = bindex;
34496+ break;
34497+ }
34498+ }
34499+ dput(parent);
34500+
34501+ /* bottom up here */
34502+ if (unlikely(err < 0))
5afbbe0d 34503+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34504+
34505+ AuDbg("b%d\n", err);
34506+ return err;
34507+}
34508+
34509+/* bottom up */
5afbbe0d 34510+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34511+{
34512+ int err;
34513+
5afbbe0d 34514+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34515+ AuDbg("b%d\n", err);
5afbbe0d 34516+ if (err > btop)
4a4d8108 34517+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34518+
34519+ AuDbg("b%d\n", err);
34520+ return err;
34521+}
34522+
076b876e
AM
34523+static int au_wbr_copyup_bu(struct dentry *dentry)
34524+{
34525+ int err;
5afbbe0d 34526+ aufs_bindex_t btop;
076b876e 34527+
5afbbe0d
AM
34528+ btop = au_dbtop(dentry);
34529+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34530+ return err;
34531+}
34532+
1facf9fc 34533+/* ---------------------------------------------------------------------- */
34534+
34535+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34536+ [AuWbrCopyup_TDP] = {
34537+ .copyup = au_wbr_copyup_tdp
34538+ },
34539+ [AuWbrCopyup_BUP] = {
34540+ .copyup = au_wbr_copyup_bup
34541+ },
34542+ [AuWbrCopyup_BU] = {
34543+ .copyup = au_wbr_copyup_bu
34544+ }
34545+};
34546+
34547+struct au_wbr_create_operations au_wbr_create_ops[] = {
34548+ [AuWbrCreate_TDP] = {
34549+ .create = au_wbr_create_tdp
34550+ },
34551+ [AuWbrCreate_RR] = {
34552+ .create = au_wbr_create_rr,
34553+ .init = au_wbr_create_init_rr
34554+ },
34555+ [AuWbrCreate_MFS] = {
34556+ .create = au_wbr_create_mfs,
34557+ .init = au_wbr_create_init_mfs,
34558+ .fin = au_wbr_create_fin_mfs
34559+ },
34560+ [AuWbrCreate_MFSV] = {
34561+ .create = au_wbr_create_mfs,
34562+ .init = au_wbr_create_init_mfs,
34563+ .fin = au_wbr_create_fin_mfs
34564+ },
34565+ [AuWbrCreate_MFSRR] = {
34566+ .create = au_wbr_create_mfsrr,
34567+ .init = au_wbr_create_init_mfsrr,
34568+ .fin = au_wbr_create_fin_mfs
34569+ },
34570+ [AuWbrCreate_MFSRRV] = {
34571+ .create = au_wbr_create_mfsrr,
34572+ .init = au_wbr_create_init_mfsrr,
34573+ .fin = au_wbr_create_fin_mfs
34574+ },
f2c43d5f
AM
34575+ [AuWbrCreate_TDMFS] = {
34576+ .create = au_wbr_create_tdmfs,
34577+ .init = au_wbr_create_init_mfs,
34578+ .fin = au_wbr_create_fin_mfs
34579+ },
34580+ [AuWbrCreate_TDMFSV] = {
34581+ .create = au_wbr_create_tdmfs,
34582+ .init = au_wbr_create_init_mfs,
34583+ .fin = au_wbr_create_fin_mfs
34584+ },
1facf9fc 34585+ [AuWbrCreate_PMFS] = {
34586+ .create = au_wbr_create_pmfs,
34587+ .init = au_wbr_create_init_mfs,
34588+ .fin = au_wbr_create_fin_mfs
34589+ },
34590+ [AuWbrCreate_PMFSV] = {
34591+ .create = au_wbr_create_pmfs,
34592+ .init = au_wbr_create_init_mfs,
34593+ .fin = au_wbr_create_fin_mfs
392086de
AM
34594+ },
34595+ [AuWbrCreate_PMFSRR] = {
34596+ .create = au_wbr_create_pmfsrr,
34597+ .init = au_wbr_create_init_mfsrr,
34598+ .fin = au_wbr_create_fin_mfs
34599+ },
34600+ [AuWbrCreate_PMFSRRV] = {
34601+ .create = au_wbr_create_pmfsrr,
34602+ .init = au_wbr_create_init_mfsrr,
34603+ .fin = au_wbr_create_fin_mfs
1facf9fc 34604+ }
34605+};
7f207e10
AM
34606diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34607--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
34608+++ linux/fs/aufs/whout.c 2018-07-19 09:46:13.063314675 +0200
34609@@ -0,0 +1,1062 @@
34610+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 34611+/*
b00004a5 34612+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 34613+ *
34614+ * This program, aufs is free software; you can redistribute it and/or modify
34615+ * it under the terms of the GNU General Public License as published by
34616+ * the Free Software Foundation; either version 2 of the License, or
34617+ * (at your option) any later version.
dece6358
AM
34618+ *
34619+ * This program is distributed in the hope that it will be useful,
34620+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34621+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34622+ * GNU General Public License for more details.
34623+ *
34624+ * You should have received a copy of the GNU General Public License
523b37e3 34625+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34626+ */
34627+
34628+/*
34629+ * whiteout for logical deletion and opaque directory
34630+ */
34631+
1facf9fc 34632+#include "aufs.h"
34633+
34634+#define WH_MASK S_IRUGO
34635+
34636+/*
34637+ * If a directory contains this file, then it is opaque. We start with the
34638+ * .wh. flag so that it is blocked by lookup.
34639+ */
0c3ec466
AM
34640+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34641+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34642+
34643+/*
34644+ * generate whiteout name, which is NOT terminated by NULL.
34645+ * @name: original d_name.name
34646+ * @len: original d_name.len
34647+ * @wh: whiteout qstr
34648+ * returns zero when succeeds, otherwise error.
34649+ * succeeded value as wh->name should be freed by kfree().
34650+ */
34651+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34652+{
34653+ char *p;
34654+
34655+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34656+ return -ENAMETOOLONG;
34657+
34658+ wh->len = name->len + AUFS_WH_PFX_LEN;
34659+ p = kmalloc(wh->len, GFP_NOFS);
34660+ wh->name = p;
34661+ if (p) {
34662+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34663+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34664+ /* smp_mb(); */
34665+ return 0;
34666+ }
34667+ return -ENOMEM;
34668+}
34669+
34670+/* ---------------------------------------------------------------------- */
34671+
34672+/*
34673+ * test if the @wh_name exists under @h_parent.
34674+ * @try_sio specifies the necessary of super-io.
34675+ */
076b876e 34676+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 34677+{
34678+ int err;
34679+ struct dentry *wh_dentry;
1facf9fc 34680+
1facf9fc 34681+ if (!try_sio)
b4510431 34682+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 34683+ else
076b876e 34684+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 34685+ err = PTR_ERR(wh_dentry);
2000de60
JR
34686+ if (IS_ERR(wh_dentry)) {
34687+ if (err == -ENAMETOOLONG)
34688+ err = 0;
1facf9fc 34689+ goto out;
2000de60 34690+ }
1facf9fc 34691+
34692+ err = 0;
5527c038 34693+ if (d_is_negative(wh_dentry))
1facf9fc 34694+ goto out_wh; /* success */
34695+
34696+ err = 1;
7e9cd9fe 34697+ if (d_is_reg(wh_dentry))
1facf9fc 34698+ goto out_wh; /* success */
34699+
34700+ err = -EIO;
523b37e3 34701+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 34702+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 34703+
4f0767ce 34704+out_wh:
1facf9fc 34705+ dput(wh_dentry);
4f0767ce 34706+out:
1facf9fc 34707+ return err;
34708+}
34709+
34710+/*
34711+ * test if the @h_dentry sets opaque or not.
34712+ */
076b876e 34713+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 34714+{
34715+ int err;
34716+ struct inode *h_dir;
34717+
5527c038 34718+ h_dir = d_inode(h_dentry);
076b876e 34719+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 34720+ au_test_h_perm_sio(h_dir, MAY_EXEC));
34721+ return err;
34722+}
34723+
34724+/*
34725+ * returns a negative dentry whose name is unique and temporary.
34726+ */
34727+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34728+ struct qstr *prefix)
34729+{
1facf9fc 34730+ struct dentry *dentry;
34731+ int i;
027c5e7a 34732+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 34733+ *name, *p;
027c5e7a 34734+ /* strict atomic_t is unnecessary here */
1facf9fc 34735+ static unsigned short cnt;
34736+ struct qstr qs;
34737+
4a4d8108
AM
34738+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34739+
1facf9fc 34740+ name = defname;
027c5e7a
AM
34741+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34742+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 34743+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 34744+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 34745+ goto out;
34746+ dentry = ERR_PTR(-ENOMEM);
34747+ name = kmalloc(qs.len + 1, GFP_NOFS);
34748+ if (unlikely(!name))
34749+ goto out;
34750+ }
34751+
34752+ /* doubly whiteout-ed */
34753+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34754+ p = name + AUFS_WH_PFX_LEN * 2;
34755+ memcpy(p, prefix->name, prefix->len);
34756+ p += prefix->len;
34757+ *p++ = '.';
4a4d8108 34758+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 34759+
34760+ qs.name = name;
34761+ for (i = 0; i < 3; i++) {
b752ccd1 34762+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 34763+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 34764+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 34765+ goto out_name;
34766+ dput(dentry);
34767+ }
0c3ec466 34768+ /* pr_warn("could not get random name\n"); */
1facf9fc 34769+ dentry = ERR_PTR(-EEXIST);
34770+ AuDbg("%.*s\n", AuLNPair(&qs));
34771+ BUG();
34772+
4f0767ce 34773+out_name:
1facf9fc 34774+ if (name != defname)
1c60b727 34775+ kfree(name);
4f0767ce 34776+out:
4a4d8108 34777+ AuTraceErrPtr(dentry);
1facf9fc 34778+ return dentry;
1facf9fc 34779+}
34780+
34781+/*
34782+ * rename the @h_dentry on @br to the whiteouted temporary name.
34783+ */
34784+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34785+{
34786+ int err;
34787+ struct path h_path = {
86dc4139 34788+ .mnt = au_br_mnt(br)
1facf9fc 34789+ };
523b37e3 34790+ struct inode *h_dir, *delegated;
1facf9fc 34791+ struct dentry *h_parent;
34792+
34793+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 34794+ h_dir = d_inode(h_parent);
1facf9fc 34795+ IMustLock(h_dir);
34796+
34797+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34798+ err = PTR_ERR(h_path.dentry);
34799+ if (IS_ERR(h_path.dentry))
34800+ goto out;
34801+
34802+ /* under the same dir, no need to lock_rename() */
523b37e3 34803+ delegated = NULL;
f2c43d5f
AM
34804+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34805+ /*flags*/0);
1facf9fc 34806+ AuTraceErr(err);
523b37e3
AM
34807+ if (unlikely(err == -EWOULDBLOCK)) {
34808+ pr_warn("cannot retry for NFSv4 delegation"
34809+ " for an internal rename\n");
34810+ iput(delegated);
34811+ }
1facf9fc 34812+ dput(h_path.dentry);
34813+
4f0767ce 34814+out:
4a4d8108 34815+ AuTraceErr(err);
1facf9fc 34816+ return err;
34817+}
34818+
34819+/* ---------------------------------------------------------------------- */
34820+/*
34821+ * functions for removing a whiteout
34822+ */
34823+
34824+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34825+{
523b37e3
AM
34826+ int err, force;
34827+ struct inode *delegated;
1facf9fc 34828+
34829+ /*
34830+ * forces superio when the dir has a sticky bit.
34831+ * this may be a violation of unix fs semantics.
34832+ */
34833+ force = (h_dir->i_mode & S_ISVTX)
5527c038 34834+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
34835+ delegated = NULL;
34836+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
34837+ if (unlikely(err == -EWOULDBLOCK)) {
34838+ pr_warn("cannot retry for NFSv4 delegation"
34839+ " for an internal unlink\n");
34840+ iput(delegated);
34841+ }
34842+ return err;
1facf9fc 34843+}
34844+
34845+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34846+ struct dentry *dentry)
34847+{
34848+ int err;
34849+
34850+ err = do_unlink_wh(h_dir, h_path);
34851+ if (!err && dentry)
34852+ au_set_dbwh(dentry, -1);
34853+
34854+ return err;
34855+}
34856+
34857+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
34858+ struct au_branch *br)
34859+{
34860+ int err;
34861+ struct path h_path = {
86dc4139 34862+ .mnt = au_br_mnt(br)
1facf9fc 34863+ };
34864+
34865+ err = 0;
b4510431 34866+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 34867+ if (IS_ERR(h_path.dentry))
34868+ err = PTR_ERR(h_path.dentry);
34869+ else {
5527c038
JR
34870+ if (d_is_reg(h_path.dentry))
34871+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 34872+ dput(h_path.dentry);
34873+ }
34874+
34875+ return err;
34876+}
34877+
34878+/* ---------------------------------------------------------------------- */
34879+/*
34880+ * initialize/clean whiteout for a branch
34881+ */
34882+
34883+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
34884+ const int isdir)
34885+{
34886+ int err;
523b37e3 34887+ struct inode *delegated;
1facf9fc 34888+
5527c038 34889+ if (d_is_negative(whpath->dentry))
1facf9fc 34890+ return;
34891+
86dc4139
AM
34892+ if (isdir)
34893+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
34894+ else {
34895+ delegated = NULL;
34896+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
34897+ if (unlikely(err == -EWOULDBLOCK)) {
34898+ pr_warn("cannot retry for NFSv4 delegation"
34899+ " for an internal unlink\n");
34900+ iput(delegated);
34901+ }
34902+ }
1facf9fc 34903+ if (unlikely(err))
523b37e3
AM
34904+ pr_warn("failed removing %pd (%d), ignored.\n",
34905+ whpath->dentry, err);
1facf9fc 34906+}
34907+
34908+static int test_linkable(struct dentry *h_root)
34909+{
5527c038 34910+ struct inode *h_dir = d_inode(h_root);
1facf9fc 34911+
34912+ if (h_dir->i_op->link)
34913+ return 0;
34914+
523b37e3
AM
34915+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
34916+ h_root, au_sbtype(h_root->d_sb));
1facf9fc 34917+ return -ENOSYS;
34918+}
34919+
34920+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
34921+static int au_whdir(struct inode *h_dir, struct path *path)
34922+{
34923+ int err;
34924+
34925+ err = -EEXIST;
5527c038 34926+ if (d_is_negative(path->dentry)) {
1facf9fc 34927+ int mode = S_IRWXU;
34928+
34929+ if (au_test_nfs(path->dentry->d_sb))
34930+ mode |= S_IXUGO;
86dc4139 34931+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 34932+ } else if (d_is_dir(path->dentry))
1facf9fc 34933+ err = 0;
34934+ else
523b37e3 34935+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 34936+
34937+ return err;
34938+}
34939+
34940+struct au_wh_base {
34941+ const struct qstr *name;
34942+ struct dentry *dentry;
34943+};
34944+
34945+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
34946+ struct path *h_path)
34947+{
34948+ h_path->dentry = base[AuBrWh_BASE].dentry;
34949+ au_wh_clean(h_dir, h_path, /*isdir*/0);
34950+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34951+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34952+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34953+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34954+}
34955+
34956+/*
34957+ * returns tri-state,
c1595e42 34958+ * minus: error, caller should print the message
1facf9fc 34959+ * zero: succuess
c1595e42 34960+ * plus: error, caller should NOT print the message
1facf9fc 34961+ */
34962+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
34963+ int do_plink, struct au_wh_base base[],
34964+ struct path *h_path)
34965+{
34966+ int err;
34967+ struct inode *h_dir;
34968+
5527c038 34969+ h_dir = d_inode(h_root);
1facf9fc 34970+ h_path->dentry = base[AuBrWh_BASE].dentry;
34971+ au_wh_clean(h_dir, h_path, /*isdir*/0);
34972+ h_path->dentry = base[AuBrWh_PLINK].dentry;
34973+ if (do_plink) {
34974+ err = test_linkable(h_root);
34975+ if (unlikely(err)) {
34976+ err = 1;
34977+ goto out;
34978+ }
34979+
34980+ err = au_whdir(h_dir, h_path);
34981+ if (unlikely(err))
34982+ goto out;
34983+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
34984+ } else
34985+ au_wh_clean(h_dir, h_path, /*isdir*/1);
34986+ h_path->dentry = base[AuBrWh_ORPH].dentry;
34987+ err = au_whdir(h_dir, h_path);
34988+ if (unlikely(err))
34989+ goto out;
34990+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
34991+
4f0767ce 34992+out:
1facf9fc 34993+ return err;
34994+}
34995+
34996+/*
34997+ * for the moment, aufs supports the branch filesystem which does not support
34998+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
34999+ * copyup failed. finally, such filesystem will not be used as the writable
35000+ * branch.
35001+ *
35002+ * returns tri-state, see above.
35003+ */
35004+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35005+ int do_plink, struct au_wh_base base[],
35006+ struct path *h_path)
35007+{
35008+ int err;
35009+ struct inode *h_dir;
35010+
1308ab2a 35011+ WbrWhMustWriteLock(wbr);
35012+
1facf9fc 35013+ err = test_linkable(h_root);
35014+ if (unlikely(err)) {
35015+ err = 1;
35016+ goto out;
35017+ }
35018+
35019+ /*
35020+ * todo: should this create be done in /sbin/mount.aufs helper?
35021+ */
35022+ err = -EEXIST;
5527c038
JR
35023+ h_dir = d_inode(h_root);
35024+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
35025+ h_path->dentry = base[AuBrWh_BASE].dentry;
35026+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 35027+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 35028+ err = 0;
35029+ else
523b37e3 35030+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 35031+ if (unlikely(err))
35032+ goto out;
35033+
35034+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35035+ if (do_plink) {
35036+ err = au_whdir(h_dir, h_path);
35037+ if (unlikely(err))
35038+ goto out;
35039+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35040+ } else
35041+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35042+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35043+
35044+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35045+ err = au_whdir(h_dir, h_path);
35046+ if (unlikely(err))
35047+ goto out;
35048+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35049+
4f0767ce 35050+out:
1facf9fc 35051+ return err;
35052+}
35053+
35054+/*
35055+ * initialize the whiteout base file/dir for @br.
35056+ */
86dc4139 35057+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 35058+{
35059+ int err, i;
35060+ const unsigned char do_plink
35061+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 35062+ struct inode *h_dir;
86dc4139
AM
35063+ struct path path = br->br_path;
35064+ struct dentry *h_root = path.dentry;
1facf9fc 35065+ struct au_wbr *wbr = br->br_wbr;
35066+ static const struct qstr base_name[] = {
0c3ec466
AM
35067+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35068+ sizeof(AUFS_BASE_NAME) - 1),
35069+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35070+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35071+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35072+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 35073+ };
35074+ struct au_wh_base base[] = {
35075+ [AuBrWh_BASE] = {
35076+ .name = base_name + AuBrWh_BASE,
35077+ .dentry = NULL
35078+ },
35079+ [AuBrWh_PLINK] = {
35080+ .name = base_name + AuBrWh_PLINK,
35081+ .dentry = NULL
35082+ },
35083+ [AuBrWh_ORPH] = {
35084+ .name = base_name + AuBrWh_ORPH,
35085+ .dentry = NULL
35086+ }
35087+ };
35088+
1308ab2a 35089+ if (wbr)
35090+ WbrWhMustWriteLock(wbr);
1facf9fc 35091+
1facf9fc 35092+ for (i = 0; i < AuBrWh_Last; i++) {
35093+ /* doubly whiteouted */
35094+ struct dentry *d;
35095+
35096+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35097+ err = PTR_ERR(d);
35098+ if (IS_ERR(d))
35099+ goto out;
35100+
35101+ base[i].dentry = d;
35102+ AuDebugOn(wbr
35103+ && wbr->wbr_wh[i]
35104+ && wbr->wbr_wh[i] != base[i].dentry);
35105+ }
35106+
35107+ if (wbr)
35108+ for (i = 0; i < AuBrWh_Last; i++) {
35109+ dput(wbr->wbr_wh[i]);
35110+ wbr->wbr_wh[i] = NULL;
35111+ }
35112+
35113+ err = 0;
1e00d052 35114+ if (!au_br_writable(br->br_perm)) {
5527c038 35115+ h_dir = d_inode(h_root);
1facf9fc 35116+ au_wh_init_ro(h_dir, base, &path);
1e00d052 35117+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 35118+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35119+ if (err > 0)
35120+ goto out;
35121+ else if (err)
35122+ goto out_err;
1e00d052 35123+ } else {
1facf9fc 35124+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35125+ if (err > 0)
35126+ goto out;
35127+ else if (err)
35128+ goto out_err;
1facf9fc 35129+ }
35130+ goto out; /* success */
35131+
4f0767ce 35132+out_err:
523b37e3
AM
35133+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35134+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35135+out:
1facf9fc 35136+ for (i = 0; i < AuBrWh_Last; i++)
35137+ dput(base[i].dentry);
35138+ return err;
35139+}
35140+
35141+/* ---------------------------------------------------------------------- */
35142+/*
35143+ * whiteouts are all hard-linked usually.
35144+ * when its link count reaches a ceiling, we create a new whiteout base
35145+ * asynchronously.
35146+ */
35147+
35148+struct reinit_br_wh {
35149+ struct super_block *sb;
35150+ struct au_branch *br;
35151+};
35152+
35153+static void reinit_br_wh(void *arg)
35154+{
35155+ int err;
35156+ aufs_bindex_t bindex;
35157+ struct path h_path;
35158+ struct reinit_br_wh *a = arg;
35159+ struct au_wbr *wbr;
523b37e3 35160+ struct inode *dir, *delegated;
1facf9fc 35161+ struct dentry *h_root;
35162+ struct au_hinode *hdir;
35163+
35164+ err = 0;
35165+ wbr = a->br->br_wbr;
35166+ /* big aufs lock */
35167+ si_noflush_write_lock(a->sb);
35168+ if (!au_br_writable(a->br->br_perm))
35169+ goto out;
35170+ bindex = au_br_index(a->sb, a->br->br_id);
35171+ if (unlikely(bindex < 0))
35172+ goto out;
35173+
1308ab2a 35174+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35175+ dir = d_inode(a->sb->s_root);
1facf9fc 35176+ hdir = au_hi(dir, bindex);
35177+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35178+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35179+
5afbbe0d 35180+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35181+ wbr_wh_write_lock(wbr);
35182+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35183+ h_root, a->br);
35184+ if (!err) {
86dc4139
AM
35185+ h_path.dentry = wbr->wbr_whbase;
35186+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35187+ delegated = NULL;
35188+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35189+ /*force*/0);
35190+ if (unlikely(err == -EWOULDBLOCK)) {
35191+ pr_warn("cannot retry for NFSv4 delegation"
35192+ " for an internal unlink\n");
35193+ iput(delegated);
35194+ }
1facf9fc 35195+ } else {
523b37e3 35196+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35197+ err = 0;
35198+ }
35199+ dput(wbr->wbr_whbase);
35200+ wbr->wbr_whbase = NULL;
35201+ if (!err)
86dc4139 35202+ err = au_wh_init(a->br, a->sb);
1facf9fc 35203+ wbr_wh_write_unlock(wbr);
5afbbe0d 35204+ au_hn_inode_unlock(hdir);
1308ab2a 35205+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35206+ if (!err)
35207+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35208+
4f0767ce 35209+out:
1facf9fc 35210+ if (wbr)
35211+ atomic_dec(&wbr->wbr_wh_running);
5afbbe0d 35212+ au_br_put(a->br);
1facf9fc 35213+ si_write_unlock(a->sb);
027c5e7a 35214+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
1c60b727 35215+ kfree(arg);
1facf9fc 35216+ if (unlikely(err))
35217+ AuIOErr("err %d\n", err);
35218+}
35219+
35220+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35221+{
35222+ int do_dec, wkq_err;
35223+ struct reinit_br_wh *arg;
35224+
35225+ do_dec = 1;
35226+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35227+ goto out;
35228+
35229+ /* ignore ENOMEM */
35230+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35231+ if (arg) {
35232+ /*
35233+ * dec(wh_running), kfree(arg) and dec(br_count)
35234+ * in reinit function
35235+ */
35236+ arg->sb = sb;
35237+ arg->br = br;
5afbbe0d 35238+ au_br_get(br);
53392da6 35239+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35240+ if (unlikely(wkq_err)) {
35241+ atomic_dec(&br->br_wbr->wbr_wh_running);
5afbbe0d 35242+ au_br_put(br);
1c60b727 35243+ kfree(arg);
1facf9fc 35244+ }
35245+ do_dec = 0;
35246+ }
35247+
4f0767ce 35248+out:
1facf9fc 35249+ if (do_dec)
35250+ atomic_dec(&br->br_wbr->wbr_wh_running);
35251+}
35252+
35253+/* ---------------------------------------------------------------------- */
35254+
35255+/*
35256+ * create the whiteout @wh.
35257+ */
35258+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35259+ struct dentry *wh)
35260+{
35261+ int err;
35262+ struct path h_path = {
35263+ .dentry = wh
35264+ };
35265+ struct au_branch *br;
35266+ struct au_wbr *wbr;
35267+ struct dentry *h_parent;
523b37e3 35268+ struct inode *h_dir, *delegated;
1facf9fc 35269+
35270+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35271+ h_dir = d_inode(h_parent);
1facf9fc 35272+ IMustLock(h_dir);
35273+
35274+ br = au_sbr(sb, bindex);
86dc4139 35275+ h_path.mnt = au_br_mnt(br);
1facf9fc 35276+ wbr = br->br_wbr;
35277+ wbr_wh_read_lock(wbr);
35278+ if (wbr->wbr_whbase) {
523b37e3
AM
35279+ delegated = NULL;
35280+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35281+ if (unlikely(err == -EWOULDBLOCK)) {
35282+ pr_warn("cannot retry for NFSv4 delegation"
35283+ " for an internal link\n");
35284+ iput(delegated);
35285+ }
1facf9fc 35286+ if (!err || err != -EMLINK)
35287+ goto out;
35288+
35289+ /* link count full. re-initialize br_whbase. */
35290+ kick_reinit_br_wh(sb, br);
35291+ }
35292+
35293+ /* return this error in this context */
b4510431 35294+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35295+ if (!err)
35296+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35297+
4f0767ce 35298+out:
1facf9fc 35299+ wbr_wh_read_unlock(wbr);
35300+ return err;
35301+}
35302+
35303+/* ---------------------------------------------------------------------- */
35304+
35305+/*
35306+ * create or remove the diropq.
35307+ */
35308+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35309+ unsigned int flags)
35310+{
35311+ struct dentry *opq_dentry, *h_dentry;
35312+ struct super_block *sb;
35313+ struct au_branch *br;
35314+ int err;
35315+
35316+ sb = dentry->d_sb;
35317+ br = au_sbr(sb, bindex);
35318+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35319+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35320+ if (IS_ERR(opq_dentry))
35321+ goto out;
35322+
35323+ if (au_ftest_diropq(flags, CREATE)) {
35324+ err = link_or_create_wh(sb, bindex, opq_dentry);
35325+ if (!err) {
35326+ au_set_dbdiropq(dentry, bindex);
35327+ goto out; /* success */
35328+ }
35329+ } else {
35330+ struct path tmp = {
35331+ .dentry = opq_dentry,
86dc4139 35332+ .mnt = au_br_mnt(br)
1facf9fc 35333+ };
5527c038 35334+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35335+ if (!err)
35336+ au_set_dbdiropq(dentry, -1);
35337+ }
35338+ dput(opq_dentry);
35339+ opq_dentry = ERR_PTR(err);
35340+
4f0767ce 35341+out:
1facf9fc 35342+ return opq_dentry;
35343+}
35344+
35345+struct do_diropq_args {
35346+ struct dentry **errp;
35347+ struct dentry *dentry;
35348+ aufs_bindex_t bindex;
35349+ unsigned int flags;
35350+};
35351+
35352+static void call_do_diropq(void *args)
35353+{
35354+ struct do_diropq_args *a = args;
35355+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35356+}
35357+
35358+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35359+ unsigned int flags)
35360+{
35361+ struct dentry *diropq, *h_dentry;
35362+
35363+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 35364+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 35365+ diropq = do_diropq(dentry, bindex, flags);
35366+ else {
35367+ int wkq_err;
35368+ struct do_diropq_args args = {
35369+ .errp = &diropq,
35370+ .dentry = dentry,
35371+ .bindex = bindex,
35372+ .flags = flags
35373+ };
35374+
35375+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35376+ if (unlikely(wkq_err))
35377+ diropq = ERR_PTR(wkq_err);
35378+ }
35379+
35380+ return diropq;
35381+}
35382+
35383+/* ---------------------------------------------------------------------- */
35384+
35385+/*
35386+ * lookup whiteout dentry.
35387+ * @h_parent: lower parent dentry which must exist and be locked
35388+ * @base_name: name of dentry which will be whiteouted
35389+ * returns dentry for whiteout.
35390+ */
35391+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35392+ struct au_branch *br)
35393+{
35394+ int err;
35395+ struct qstr wh_name;
35396+ struct dentry *wh_dentry;
35397+
35398+ err = au_wh_name_alloc(&wh_name, base_name);
35399+ wh_dentry = ERR_PTR(err);
35400+ if (!err) {
b4510431 35401+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
1c60b727 35402+ kfree(wh_name.name);
1facf9fc 35403+ }
35404+ return wh_dentry;
35405+}
35406+
35407+/*
35408+ * link/create a whiteout for @dentry on @bindex.
35409+ */
35410+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35411+ struct dentry *h_parent)
35412+{
35413+ struct dentry *wh_dentry;
35414+ struct super_block *sb;
35415+ int err;
35416+
35417+ sb = dentry->d_sb;
35418+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35419+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35420+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35421+ if (!err) {
1facf9fc 35422+ au_set_dbwh(dentry, bindex);
076b876e
AM
35423+ au_fhsm_wrote(sb, bindex, /*force*/0);
35424+ } else {
1facf9fc 35425+ dput(wh_dentry);
35426+ wh_dentry = ERR_PTR(err);
35427+ }
35428+ }
35429+
35430+ return wh_dentry;
35431+}
35432+
35433+/* ---------------------------------------------------------------------- */
35434+
35435+/* Delete all whiteouts in this directory on branch bindex. */
35436+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35437+ aufs_bindex_t bindex, struct au_branch *br)
35438+{
35439+ int err;
35440+ unsigned long ul, n;
35441+ struct qstr wh_name;
35442+ char *p;
35443+ struct hlist_head *head;
c06a8ce3 35444+ struct au_vdir_wh *pos;
1facf9fc 35445+ struct au_vdir_destr *str;
35446+
35447+ err = -ENOMEM;
537831f9 35448+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35449+ wh_name.name = p;
35450+ if (unlikely(!wh_name.name))
35451+ goto out;
35452+
35453+ err = 0;
35454+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35455+ p += AUFS_WH_PFX_LEN;
35456+ n = whlist->nh_num;
35457+ head = whlist->nh_head;
35458+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35459+ hlist_for_each_entry(pos, head, wh_hash) {
35460+ if (pos->wh_bindex != bindex)
1facf9fc 35461+ continue;
35462+
c06a8ce3 35463+ str = &pos->wh_str;
1facf9fc 35464+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35465+ memcpy(p, str->name, str->len);
35466+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35467+ err = unlink_wh_name(h_dentry, &wh_name, br);
35468+ if (!err)
35469+ continue;
35470+ break;
35471+ }
35472+ AuIOErr("whiteout name too long %.*s\n",
35473+ str->len, str->name);
35474+ err = -EIO;
35475+ break;
35476+ }
35477+ }
1c60b727 35478+ free_page((unsigned long)wh_name.name);
1facf9fc 35479+
4f0767ce 35480+out:
1facf9fc 35481+ return err;
35482+}
35483+
35484+struct del_wh_children_args {
35485+ int *errp;
35486+ struct dentry *h_dentry;
1308ab2a 35487+ struct au_nhash *whlist;
1facf9fc 35488+ aufs_bindex_t bindex;
35489+ struct au_branch *br;
35490+};
35491+
35492+static void call_del_wh_children(void *args)
35493+{
35494+ struct del_wh_children_args *a = args;
1308ab2a 35495+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35496+}
35497+
35498+/* ---------------------------------------------------------------------- */
35499+
35500+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35501+{
35502+ struct au_whtmp_rmdir *whtmp;
dece6358 35503+ int err;
1308ab2a 35504+ unsigned int rdhash;
dece6358
AM
35505+
35506+ SiMustAnyLock(sb);
1facf9fc 35507+
be52b249 35508+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35509+ if (unlikely(!whtmp)) {
35510+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35511+ goto out;
dece6358 35512+ }
1facf9fc 35513+
1308ab2a 35514+ /* no estimation for dir size */
35515+ rdhash = au_sbi(sb)->si_rdhash;
35516+ if (!rdhash)
35517+ rdhash = AUFS_RDHASH_DEF;
35518+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35519+ if (unlikely(err)) {
1c60b727 35520+ kfree(whtmp);
1308ab2a 35521+ whtmp = ERR_PTR(err);
35522+ }
dece6358 35523+
4f0767ce 35524+out:
dece6358 35525+ return whtmp;
1facf9fc 35526+}
35527+
35528+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35529+{
027c5e7a 35530+ if (whtmp->br)
5afbbe0d 35531+ au_br_put(whtmp->br);
1facf9fc 35532+ dput(whtmp->wh_dentry);
35533+ iput(whtmp->dir);
dece6358 35534+ au_nhash_wh_free(&whtmp->whlist);
1c60b727 35535+ kfree(whtmp);
1facf9fc 35536+}
35537+
35538+/*
35539+ * rmdir the whiteouted temporary named dir @h_dentry.
35540+ * @whlist: whiteouted children.
35541+ */
35542+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35543+ struct dentry *wh_dentry, struct au_nhash *whlist)
35544+{
35545+ int err;
2000de60 35546+ unsigned int h_nlink;
1facf9fc 35547+ struct path h_tmp;
35548+ struct inode *wh_inode, *h_dir;
35549+ struct au_branch *br;
35550+
5527c038 35551+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35552+ IMustLock(h_dir);
35553+
35554+ br = au_sbr(dir->i_sb, bindex);
5527c038 35555+ wh_inode = d_inode(wh_dentry);
febd17d6 35556+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35557+
35558+ /*
35559+ * someone else might change some whiteouts while we were sleeping.
35560+ * it means this whlist may have an obsoleted entry.
35561+ */
35562+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35563+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35564+ else {
35565+ int wkq_err;
35566+ struct del_wh_children_args args = {
35567+ .errp = &err,
35568+ .h_dentry = wh_dentry,
1308ab2a 35569+ .whlist = whlist,
1facf9fc 35570+ .bindex = bindex,
35571+ .br = br
35572+ };
35573+
35574+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35575+ if (unlikely(wkq_err))
35576+ err = wkq_err;
35577+ }
febd17d6 35578+ inode_unlock(wh_inode);
1facf9fc 35579+
35580+ if (!err) {
35581+ h_tmp.dentry = wh_dentry;
86dc4139 35582+ h_tmp.mnt = au_br_mnt(br);
2000de60 35583+ h_nlink = h_dir->i_nlink;
1facf9fc 35584+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35585+ /* some fs doesn't change the parent nlink in some cases */
35586+ h_nlink -= h_dir->i_nlink;
1facf9fc 35587+ }
35588+
35589+ if (!err) {
5afbbe0d 35590+ if (au_ibtop(dir) == bindex) {
7f207e10 35591+ /* todo: dir->i_mutex is necessary */
1facf9fc 35592+ au_cpup_attr_timesizes(dir);
2000de60
JR
35593+ if (h_nlink)
35594+ vfsub_drop_nlink(dir);
1facf9fc 35595+ }
35596+ return 0; /* success */
35597+ }
35598+
523b37e3 35599+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35600+ return err;
35601+}
35602+
35603+static void call_rmdir_whtmp(void *args)
35604+{
35605+ int err;
e49829fe 35606+ aufs_bindex_t bindex;
1facf9fc 35607+ struct au_whtmp_rmdir *a = args;
35608+ struct super_block *sb;
35609+ struct dentry *h_parent;
35610+ struct inode *h_dir;
1facf9fc 35611+ struct au_hinode *hdir;
35612+
35613+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35614+ /* inode_lock(a->dir); */
e49829fe 35615+ err = -EROFS;
1facf9fc 35616+ sb = a->dir->i_sb;
e49829fe
JR
35617+ si_read_lock(sb, !AuLock_FLUSH);
35618+ if (!au_br_writable(a->br->br_perm))
35619+ goto out;
35620+ bindex = au_br_index(sb, a->br->br_id);
35621+ if (unlikely(bindex < 0))
1facf9fc 35622+ goto out;
35623+
35624+ err = -EIO;
1facf9fc 35625+ ii_write_lock_parent(a->dir);
35626+ h_parent = dget_parent(a->wh_dentry);
5527c038 35627+ h_dir = d_inode(h_parent);
e49829fe 35628+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35629+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35630+ if (unlikely(err))
35631+ goto out_mnt;
5afbbe0d 35632+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35633+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35634+ a->br);
86dc4139
AM
35635+ if (!err)
35636+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35637+ au_hn_inode_unlock(hdir);
86dc4139
AM
35638+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35639+
35640+out_mnt:
1facf9fc 35641+ dput(h_parent);
35642+ ii_write_unlock(a->dir);
4f0767ce 35643+out:
febd17d6 35644+ /* inode_unlock(a->dir); */
1facf9fc 35645+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35646+ si_read_unlock(sb);
35647+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35648+ if (unlikely(err))
35649+ AuIOErr("err %d\n", err);
35650+}
35651+
35652+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35653+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35654+{
35655+ int wkq_err;
e49829fe 35656+ struct super_block *sb;
1facf9fc 35657+
35658+ IMustLock(dir);
35659+
35660+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35661+ sb = dir->i_sb;
1facf9fc 35662+ args->dir = au_igrab(dir);
e49829fe 35663+ args->br = au_sbr(sb, bindex);
5afbbe0d 35664+ au_br_get(args->br);
1facf9fc 35665+ args->wh_dentry = dget(wh_dentry);
53392da6 35666+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35667+ if (unlikely(wkq_err)) {
523b37e3 35668+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35669+ au_whtmp_rmdir_free(args);
35670+ }
35671+}
7f207e10
AM
35672diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35673--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
35674+++ linux/fs/aufs/whout.h 2018-07-19 09:46:13.063314675 +0200
35675@@ -0,0 +1,86 @@
35676+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 35677+/*
b00004a5 35678+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 35679+ *
35680+ * This program, aufs is free software; you can redistribute it and/or modify
35681+ * it under the terms of the GNU General Public License as published by
35682+ * the Free Software Foundation; either version 2 of the License, or
35683+ * (at your option) any later version.
dece6358
AM
35684+ *
35685+ * This program is distributed in the hope that it will be useful,
35686+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35687+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35688+ * GNU General Public License for more details.
35689+ *
35690+ * You should have received a copy of the GNU General Public License
523b37e3 35691+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35692+ */
35693+
35694+/*
35695+ * whiteout for logical deletion and opaque directory
35696+ */
35697+
35698+#ifndef __AUFS_WHOUT_H__
35699+#define __AUFS_WHOUT_H__
35700+
35701+#ifdef __KERNEL__
35702+
1facf9fc 35703+#include "dir.h"
35704+
35705+/* whout.c */
35706+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
35707+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35708+int au_diropq_test(struct dentry *h_dentry);
3c1bdaff 35709+struct au_branch;
1facf9fc 35710+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35711+ struct qstr *prefix);
35712+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35713+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35714+ struct dentry *dentry);
86dc4139 35715+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 35716+
35717+/* diropq flags */
35718+#define AuDiropq_CREATE 1
35719+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
35720+#define au_fset_diropq(flags, name) \
35721+ do { (flags) |= AuDiropq_##name; } while (0)
35722+#define au_fclr_diropq(flags, name) \
35723+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 35724+
35725+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35726+ unsigned int flags);
35727+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35728+ struct au_branch *br);
35729+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35730+ struct dentry *h_parent);
35731+
35732+/* real rmdir for the whiteout-ed dir */
35733+struct au_whtmp_rmdir {
35734+ struct inode *dir;
e49829fe 35735+ struct au_branch *br;
1facf9fc 35736+ struct dentry *wh_dentry;
dece6358 35737+ struct au_nhash whlist;
1facf9fc 35738+};
35739+
35740+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35741+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35742+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35743+ struct dentry *wh_dentry, struct au_nhash *whlist);
35744+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35745+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35746+
35747+/* ---------------------------------------------------------------------- */
35748+
35749+static inline struct dentry *au_diropq_create(struct dentry *dentry,
35750+ aufs_bindex_t bindex)
35751+{
35752+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35753+}
35754+
35755+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35756+{
35757+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35758+}
35759+
35760+#endif /* __KERNEL__ */
35761+#endif /* __AUFS_WHOUT_H__ */
7f207e10
AM
35762diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35763--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
35764+++ linux/fs/aufs/wkq.c 2018-07-19 09:46:13.063314675 +0200
35765@@ -0,0 +1,391 @@
35766+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 35767+/*
b00004a5 35768+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 35769+ *
35770+ * This program, aufs is free software; you can redistribute it and/or modify
35771+ * it under the terms of the GNU General Public License as published by
35772+ * the Free Software Foundation; either version 2 of the License, or
35773+ * (at your option) any later version.
dece6358
AM
35774+ *
35775+ * This program is distributed in the hope that it will be useful,
35776+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35777+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35778+ * GNU General Public License for more details.
35779+ *
35780+ * You should have received a copy of the GNU General Public License
523b37e3 35781+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35782+ */
35783+
35784+/*
35785+ * workqueue for asynchronous/super-io operations
35786+ * todo: try new dredential scheme
35787+ */
35788+
dece6358 35789+#include <linux/module.h>
1facf9fc 35790+#include "aufs.h"
35791+
9dbd164d 35792+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 35793+
9dbd164d 35794+static struct workqueue_struct *au_wkq;
1facf9fc 35795+
35796+struct au_wkinfo {
35797+ struct work_struct wk;
7f207e10 35798+ struct kobject *kobj;
1facf9fc 35799+
35800+ unsigned int flags; /* see wkq.h */
35801+
35802+ au_wkq_func_t func;
35803+ void *args;
35804+
8b6a4947
AM
35805+#ifdef CONFIG_LOCKDEP
35806+ int dont_check;
35807+ struct held_lock **hlock;
35808+#endif
35809+
1facf9fc 35810+ struct completion *comp;
35811+};
35812+
35813+/* ---------------------------------------------------------------------- */
8b6a4947
AM
35814+/*
35815+ * Aufs passes some operations to the workqueue such as the internal copyup.
35816+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35817+ * job run by workqueue depends upon the locks acquired in the other task.
35818+ * Delegating a small operation to the workqueue, aufs passes its lockdep
35819+ * information too. And the job in the workqueue restores the info in order to
35820+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
35821+ * correctly and expectedly.
35822+ */
35823+
35824+#ifndef CONFIG_LOCKDEP
35825+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35826+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35827+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35828+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35829+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35830+#else
35831+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35832+{
35833+ wkinfo->hlock = NULL;
35834+ wkinfo->dont_check = 0;
35835+}
35836+
35837+/*
35838+ * 1: matched
35839+ * 0: unmatched
35840+ */
35841+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35842+{
35843+ static DEFINE_SPINLOCK(spin);
35844+ static struct {
35845+ char *name;
35846+ struct lock_class_key *key;
35847+ } a[] = {
35848+ { .name = "&sbinfo->si_rwsem" },
35849+ { .name = "&finfo->fi_rwsem" },
35850+ { .name = "&dinfo->di_rwsem" },
35851+ { .name = "&iinfo->ii_rwsem" }
35852+ };
35853+ static int set;
35854+ int i;
35855+
35856+ /* lockless read from 'set.' see below */
35857+ if (set == ARRAY_SIZE(a)) {
35858+ for (i = 0; i < ARRAY_SIZE(a); i++)
35859+ if (a[i].key == key)
35860+ goto match;
35861+ goto unmatch;
35862+ }
35863+
35864+ spin_lock(&spin);
35865+ if (set)
35866+ for (i = 0; i < ARRAY_SIZE(a); i++)
35867+ if (a[i].key == key) {
35868+ spin_unlock(&spin);
35869+ goto match;
35870+ }
35871+ for (i = 0; i < ARRAY_SIZE(a); i++) {
35872+ if (a[i].key) {
35873+ if (unlikely(a[i].key == key)) { /* rare but possible */
35874+ spin_unlock(&spin);
35875+ goto match;
35876+ } else
35877+ continue;
35878+ }
35879+ if (strstr(a[i].name, name)) {
35880+ /*
35881+ * the order of these three lines is important for the
35882+ * lockless read above.
35883+ */
35884+ a[i].key = key;
35885+ spin_unlock(&spin);
35886+ set++;
35887+ /* AuDbg("%d, %s\n", set, name); */
35888+ goto match;
35889+ }
35890+ }
35891+ spin_unlock(&spin);
35892+ goto unmatch;
35893+
35894+match:
35895+ return 1;
35896+unmatch:
35897+ return 0;
35898+}
35899+
35900+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
35901+{
35902+ int err, n;
35903+ struct task_struct *curr;
35904+ struct held_lock **hl, *held_locks, *p;
35905+
35906+ err = 0;
35907+ curr = current;
35908+ wkinfo->dont_check = lockdep_recursing(curr);
35909+ if (wkinfo->dont_check)
35910+ goto out;
35911+ n = curr->lockdep_depth;
35912+ if (!n)
35913+ goto out;
35914+
35915+ err = -ENOMEM;
35916+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
35917+ if (unlikely(!wkinfo->hlock))
35918+ goto out;
35919+
35920+ err = 0;
35921+#if 0
35922+ if (0 && au_debug_test()) /* left for debugging */
35923+ lockdep_print_held_locks(curr);
35924+#endif
35925+ held_locks = curr->held_locks;
35926+ hl = wkinfo->hlock;
35927+ while (n--) {
35928+ p = held_locks++;
35929+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
35930+ *hl++ = p;
35931+ }
35932+ *hl = NULL;
35933+
35934+out:
35935+ return err;
35936+}
35937+
35938+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
35939+{
35940+ kfree(wkinfo->hlock);
35941+}
35942+
35943+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
35944+{
35945+ struct held_lock *p, **hl = wkinfo->hlock;
35946+ int subclass;
35947+
35948+ if (wkinfo->dont_check)
35949+ lockdep_off();
35950+ if (!hl)
35951+ return;
35952+ while ((p = *hl++)) { /* assignment */
35953+ subclass = lockdep_hlock_class(p)->subclass;
35954+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
35955+ if (p->read)
35956+ rwsem_acquire_read(p->instance, subclass, 0,
35957+ /*p->acquire_ip*/_RET_IP_);
35958+ else
35959+ rwsem_acquire(p->instance, subclass, 0,
35960+ /*p->acquire_ip*/_RET_IP_);
35961+ }
35962+}
35963+
35964+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
35965+{
35966+ struct held_lock *p, **hl = wkinfo->hlock;
35967+
35968+ if (wkinfo->dont_check)
35969+ lockdep_on();
35970+ if (!hl)
35971+ return;
35972+ while ((p = *hl++)) /* assignment */
35973+ rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
35974+}
35975+#endif
1facf9fc 35976+
1facf9fc 35977+static void wkq_func(struct work_struct *wk)
35978+{
35979+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
35980+
2dfbb274 35981+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
35982+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
35983+
8b6a4947 35984+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 35985+ wkinfo->func(wkinfo->args);
8b6a4947 35986+ au_wkq_lockdep_post(wkinfo);
1facf9fc 35987+ if (au_ftest_wkq(wkinfo->flags, WAIT))
35988+ complete(wkinfo->comp);
35989+ else {
7f207e10 35990+ kobject_put(wkinfo->kobj);
9dbd164d 35991+ module_put(THIS_MODULE); /* todo: ?? */
1c60b727 35992+ kfree(wkinfo);
1facf9fc 35993+ }
35994+}
35995+
35996+/*
35997+ * Since struct completion is large, try allocating it dynamically.
35998+ */
c2b27bf2 35999+#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
1facf9fc 36000+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36001+
36002+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36003+{
36004+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36005+ if (*comp) {
36006+ init_completion(*comp);
36007+ wkinfo->comp = *comp;
36008+ return 0;
36009+ }
36010+ return -ENOMEM;
36011+}
36012+
36013+static void au_wkq_comp_free(struct completion *comp)
36014+{
1c60b727 36015+ kfree(comp);
1facf9fc 36016+}
36017+
36018+#else
36019+
36020+/* no braces */
36021+#define AuWkqCompDeclare(name) \
36022+ DECLARE_COMPLETION_ONSTACK(_ ## name); \
36023+ struct completion *comp = &_ ## name
36024+
36025+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36026+{
36027+ wkinfo->comp = *comp;
36028+ return 0;
36029+}
36030+
36031+static void au_wkq_comp_free(struct completion *comp __maybe_unused)
36032+{
36033+ /* empty */
36034+}
36035+#endif /* 4KSTACKS */
36036+
53392da6 36037+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 36038+{
53392da6
AM
36039+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36040+ if (au_wkq_test()) {
38d290e6
JR
36041+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36042+ " due to a dead dir by UDBA?\n");
53392da6
AM
36043+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36044+ }
36045+ } else
36046+ au_dbg_verify_kthread();
36047+
36048+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 36049+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 36050+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
36051+ } else {
36052+ INIT_WORK(&wkinfo->wk, wkq_func);
36053+ schedule_work(&wkinfo->wk);
36054+ }
1facf9fc 36055+}
36056+
7f207e10
AM
36057+/*
36058+ * Be careful. It is easy to make deadlock happen.
36059+ * processA: lock, wkq and wait
36060+ * processB: wkq and wait, lock in wkq
36061+ * --> deadlock
36062+ */
b752ccd1 36063+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 36064+{
36065+ int err;
36066+ AuWkqCompDeclare(comp);
36067+ struct au_wkinfo wkinfo = {
b752ccd1 36068+ .flags = flags,
1facf9fc 36069+ .func = func,
36070+ .args = args
36071+ };
36072+
36073+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
36074+ if (unlikely(err))
36075+ goto out;
36076+ err = au_wkq_lockdep_alloc(&wkinfo);
36077+ if (unlikely(err))
36078+ goto out_comp;
1facf9fc 36079+ if (!err) {
53392da6 36080+ au_wkq_run(&wkinfo);
1facf9fc 36081+ /* no timeout, no interrupt */
36082+ wait_for_completion(wkinfo.comp);
1facf9fc 36083+ }
8b6a4947 36084+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 36085+
8b6a4947
AM
36086+out_comp:
36087+ au_wkq_comp_free(comp);
36088+out:
36089+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 36090+ return err;
1facf9fc 36091+}
36092+
027c5e7a
AM
36093+/*
36094+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36095+ * problem in a concurrent umounting.
36096+ */
53392da6
AM
36097+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36098+ unsigned int flags)
1facf9fc 36099+{
36100+ int err;
36101+ struct au_wkinfo *wkinfo;
36102+
f0c0a007 36103+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 36104+
36105+ /*
36106+ * wkq_func() must free this wkinfo.
36107+ * it highly depends upon the implementation of workqueue.
36108+ */
36109+ err = 0;
36110+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36111+ if (wkinfo) {
7f207e10 36112+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 36113+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 36114+ wkinfo->func = func;
36115+ wkinfo->args = args;
36116+ wkinfo->comp = NULL;
8b6a4947 36117+ au_wkq_lockdep_init(wkinfo);
7f207e10 36118+ kobject_get(wkinfo->kobj);
9dbd164d 36119+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 36120+
53392da6 36121+ au_wkq_run(wkinfo);
1facf9fc 36122+ } else {
36123+ err = -ENOMEM;
e49829fe 36124+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36125+ }
36126+
36127+ return err;
36128+}
36129+
36130+/* ---------------------------------------------------------------------- */
36131+
36132+void au_nwt_init(struct au_nowait_tasks *nwt)
36133+{
f0c0a007
AM
36134+ atomic_set(&nwt->nw_len, 0);
36135+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36136+ init_waitqueue_head(&nwt->nw_wq);
36137+}
36138+
36139+void au_wkq_fin(void)
36140+{
9dbd164d 36141+ destroy_workqueue(au_wkq);
1facf9fc 36142+}
36143+
36144+int __init au_wkq_init(void)
36145+{
9dbd164d 36146+ int err;
b752ccd1
AM
36147+
36148+ err = 0;
86dc4139 36149+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36150+ if (IS_ERR(au_wkq))
36151+ err = PTR_ERR(au_wkq);
36152+ else if (!au_wkq)
36153+ err = -ENOMEM;
b752ccd1
AM
36154+
36155+ return err;
1facf9fc 36156+}
7f207e10
AM
36157diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36158--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
36159+++ linux/fs/aufs/wkq.h 2018-07-19 09:46:13.063314675 +0200
36160@@ -0,0 +1,94 @@
36161+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36162+/*
b00004a5 36163+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36164+ *
36165+ * This program, aufs is free software; you can redistribute it and/or modify
36166+ * it under the terms of the GNU General Public License as published by
36167+ * the Free Software Foundation; either version 2 of the License, or
36168+ * (at your option) any later version.
dece6358
AM
36169+ *
36170+ * This program is distributed in the hope that it will be useful,
36171+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36172+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36173+ * GNU General Public License for more details.
36174+ *
36175+ * You should have received a copy of the GNU General Public License
523b37e3 36176+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36177+ */
36178+
36179+/*
36180+ * workqueue for asynchronous/super-io operations
36181+ * todo: try new credentials management scheme
36182+ */
36183+
36184+#ifndef __AUFS_WKQ_H__
36185+#define __AUFS_WKQ_H__
36186+
36187+#ifdef __KERNEL__
36188+
8b6a4947 36189+#include <linux/wait.h>
5afbbe0d 36190+
dece6358
AM
36191+struct super_block;
36192+
1facf9fc 36193+/* ---------------------------------------------------------------------- */
36194+
36195+/*
36196+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36197+ */
36198+struct au_nowait_tasks {
f0c0a007 36199+ atomic_t nw_len;
1facf9fc 36200+ wait_queue_head_t nw_wq;
36201+};
36202+
36203+/* ---------------------------------------------------------------------- */
36204+
36205+typedef void (*au_wkq_func_t)(void *args);
36206+
36207+/* wkq flags */
36208+#define AuWkq_WAIT 1
9dbd164d 36209+#define AuWkq_NEST (1 << 1)
1facf9fc 36210+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36211+#define au_fset_wkq(flags, name) \
36212+ do { (flags) |= AuWkq_##name; } while (0)
36213+#define au_fclr_wkq(flags, name) \
36214+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36215+
9dbd164d
AM
36216+#ifndef CONFIG_AUFS_HNOTIFY
36217+#undef AuWkq_NEST
36218+#define AuWkq_NEST 0
36219+#endif
36220+
1facf9fc 36221+/* wkq.c */
b752ccd1 36222+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36223+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36224+ unsigned int flags);
1facf9fc 36225+void au_nwt_init(struct au_nowait_tasks *nwt);
36226+int __init au_wkq_init(void);
36227+void au_wkq_fin(void);
36228+
36229+/* ---------------------------------------------------------------------- */
36230+
53392da6
AM
36231+static inline int au_wkq_test(void)
36232+{
36233+ return current->flags & PF_WQ_WORKER;
36234+}
36235+
b752ccd1 36236+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36237+{
b752ccd1 36238+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36239+}
36240+
36241+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36242+{
f0c0a007 36243+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36244+ wake_up_all(&nwt->nw_wq);
36245+}
36246+
36247+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36248+{
f0c0a007 36249+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36250+ return 0;
36251+}
36252+
36253+#endif /* __KERNEL__ */
36254+#endif /* __AUFS_WKQ_H__ */
c1595e42
JR
36255diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36256--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
36257+++ linux/fs/aufs/xattr.c 2018-07-19 09:46:13.066648108 +0200
36258@@ -0,0 +1,356 @@
36259+/* SPDX-License-Identifier: GPL-2.0 */
c1595e42 36260+/*
b00004a5 36261+ * Copyright (C) 2014-2018 Junjiro R. Okajima
c1595e42
JR
36262+ *
36263+ * This program, aufs is free software; you can redistribute it and/or modify
36264+ * it under the terms of the GNU General Public License as published by
36265+ * the Free Software Foundation; either version 2 of the License, or
36266+ * (at your option) any later version.
36267+ *
36268+ * This program is distributed in the hope that it will be useful,
36269+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36270+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36271+ * GNU General Public License for more details.
36272+ *
36273+ * You should have received a copy of the GNU General Public License
36274+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36275+ */
36276+
36277+/*
36278+ * handling xattr functions
36279+ */
36280+
a2654f78
AM
36281+#include <linux/fs.h>
36282+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36283+#include <linux/xattr.h>
36284+#include "aufs.h"
36285+
36286+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36287+{
36288+ if (!ignore_flags)
36289+ goto out;
36290+ switch (err) {
36291+ case -ENOMEM:
36292+ case -EDQUOT:
36293+ goto out;
36294+ }
36295+
36296+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36297+ err = 0;
36298+ goto out;
36299+ }
36300+
36301+#define cmp(brattr, prefix) do { \
36302+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36303+ XATTR_##prefix##_PREFIX_LEN)) { \
36304+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36305+ err = 0; \
36306+ goto out; \
36307+ } \
36308+ } while (0)
36309+
36310+ cmp(SEC, SECURITY);
36311+ cmp(SYS, SYSTEM);
36312+ cmp(TR, TRUSTED);
36313+ cmp(USR, USER);
36314+#undef cmp
36315+
36316+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36317+ err = 0;
36318+
36319+out:
36320+ return err;
36321+}
36322+
36323+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36324+
36325+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
36326+ char *name, char **buf, unsigned int ignore_flags,
36327+ unsigned int verbose)
c1595e42
JR
36328+{
36329+ int err;
36330+ ssize_t ssz;
36331+ struct inode *h_idst;
36332+
36333+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36334+ err = ssz;
36335+ if (unlikely(err <= 0)) {
c1595e42
JR
36336+ if (err == -ENODATA
36337+ || (err == -EOPNOTSUPP
b912730e 36338+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 36339+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
36340+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36341+ || !strcmp(name,
36342+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36343+ ))
c1595e42 36344+ err = 0;
b912730e
AM
36345+ if (err && (verbose || au_debug_test()))
36346+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36347+ goto out;
36348+ }
36349+
36350+ /* unlock it temporary */
5527c038 36351+ h_idst = d_inode(h_dst);
febd17d6 36352+ inode_unlock(h_idst);
c1595e42 36353+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 36354+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36355+ if (unlikely(err)) {
7e9cd9fe
AM
36356+ if (verbose || au_debug_test())
36357+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36358+ err = au_xattr_ignore(err, name, ignore_flags);
36359+ }
36360+
36361+out:
36362+ return err;
36363+}
36364+
7e9cd9fe
AM
36365+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36366+ unsigned int verbose)
c1595e42
JR
36367+{
36368+ int err, unlocked, acl_access, acl_default;
36369+ ssize_t ssz;
36370+ struct inode *h_isrc, *h_idst;
36371+ char *value, *p, *o, *e;
36372+
36373+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36374+ /* there should not be the parent-child relationship between them */
5527c038
JR
36375+ h_isrc = d_inode(h_src);
36376+ h_idst = d_inode(h_dst);
febd17d6 36377+ inode_unlock(h_idst);
be118d29 36378+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36379+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36380+ unlocked = 0;
36381+
36382+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36383+ ssz = vfs_listxattr(h_src, NULL, 0);
36384+ err = ssz;
36385+ if (unlikely(err < 0)) {
36386+ AuTraceErr(err);
36387+ if (err == -ENODATA
36388+ || err == -EOPNOTSUPP)
36389+ err = 0; /* ignore */
36390+ goto out;
36391+ }
36392+
36393+ err = 0;
36394+ p = NULL;
36395+ o = NULL;
36396+ if (ssz) {
36397+ err = -ENOMEM;
36398+ p = kmalloc(ssz, GFP_NOFS);
36399+ o = p;
36400+ if (unlikely(!p))
36401+ goto out;
36402+ err = vfs_listxattr(h_src, p, ssz);
36403+ }
3c1bdaff 36404+ inode_unlock_shared(h_isrc);
c1595e42
JR
36405+ unlocked = 1;
36406+ AuDbg("err %d, ssz %zd\n", err, ssz);
36407+ if (unlikely(err < 0))
36408+ goto out_free;
36409+
36410+ err = 0;
36411+ e = p + ssz;
36412+ value = NULL;
36413+ acl_access = 0;
36414+ acl_default = 0;
36415+ while (!err && p < e) {
36416+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36417+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36418+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36419+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36420+ - 1);
7e9cd9fe
AM
36421+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36422+ verbose);
c1595e42
JR
36423+ p += strlen(p) + 1;
36424+ }
36425+ AuTraceErr(err);
36426+ ignore_flags |= au_xattr_out_of_list;
36427+ if (!err && !acl_access) {
36428+ err = au_do_cpup_xattr(h_dst, h_src,
36429+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36430+ ignore_flags, verbose);
c1595e42
JR
36431+ AuTraceErr(err);
36432+ }
36433+ if (!err && !acl_default) {
36434+ err = au_do_cpup_xattr(h_dst, h_src,
36435+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36436+ ignore_flags, verbose);
c1595e42
JR
36437+ AuTraceErr(err);
36438+ }
36439+
8b6a4947 36440+ kfree(value);
c1595e42
JR
36441+
36442+out_free:
8b6a4947 36443+ kfree(o);
c1595e42
JR
36444+out:
36445+ if (!unlocked)
3c1bdaff 36446+ inode_unlock_shared(h_isrc);
c1595e42
JR
36447+ AuTraceErr(err);
36448+ return err;
36449+}
36450+
36451+/* ---------------------------------------------------------------------- */
36452+
a2654f78
AM
36453+static int au_smack_reentering(struct super_block *sb)
36454+{
36455+#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36456+ /*
36457+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36458+ * i_op->getxattr(). ouch.
36459+ */
36460+ return si_pid_test(sb);
36461+#else
36462+ return 0;
36463+#endif
36464+}
36465+
c1595e42
JR
36466+enum {
36467+ AU_XATTR_LIST,
36468+ AU_XATTR_GET
36469+};
36470+
36471+struct au_lgxattr {
36472+ int type;
36473+ union {
36474+ struct {
36475+ char *list;
36476+ size_t size;
36477+ } list;
36478+ struct {
36479+ const char *name;
36480+ void *value;
36481+ size_t size;
36482+ } get;
36483+ } u;
36484+};
36485+
36486+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36487+{
36488+ ssize_t err;
a2654f78 36489+ int reenter;
c1595e42
JR
36490+ struct path h_path;
36491+ struct super_block *sb;
36492+
36493+ sb = dentry->d_sb;
a2654f78
AM
36494+ reenter = au_smack_reentering(sb);
36495+ if (!reenter) {
36496+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36497+ if (unlikely(err))
36498+ goto out;
36499+ }
36500+ err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
c1595e42
JR
36501+ if (unlikely(err))
36502+ goto out_si;
36503+ if (unlikely(!h_path.dentry))
36504+ /* illegally overlapped or something */
36505+ goto out_di; /* pretending success */
36506+
36507+ /* always topmost entry only */
36508+ switch (arg->type) {
36509+ case AU_XATTR_LIST:
36510+ err = vfs_listxattr(h_path.dentry,
36511+ arg->u.list.list, arg->u.list.size);
36512+ break;
36513+ case AU_XATTR_GET:
5afbbe0d 36514+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
36515+ err = vfs_getxattr(h_path.dentry,
36516+ arg->u.get.name, arg->u.get.value,
36517+ arg->u.get.size);
36518+ break;
36519+ }
36520+
36521+out_di:
a2654f78
AM
36522+ if (!reenter)
36523+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36524+out_si:
a2654f78
AM
36525+ if (!reenter)
36526+ si_read_unlock(sb);
c1595e42
JR
36527+out:
36528+ AuTraceErr(err);
36529+ return err;
36530+}
36531+
36532+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36533+{
36534+ struct au_lgxattr arg = {
36535+ .type = AU_XATTR_LIST,
36536+ .u.list = {
36537+ .list = list,
36538+ .size = size
36539+ },
36540+ };
36541+
36542+ return au_lgxattr(dentry, &arg);
36543+}
36544+
f2c43d5f
AM
36545+static ssize_t au_getxattr(struct dentry *dentry,
36546+ struct inode *inode __maybe_unused,
36547+ const char *name, void *value, size_t size)
c1595e42
JR
36548+{
36549+ struct au_lgxattr arg = {
36550+ .type = AU_XATTR_GET,
36551+ .u.get = {
36552+ .name = name,
36553+ .value = value,
36554+ .size = size
36555+ },
36556+ };
36557+
36558+ return au_lgxattr(dentry, &arg);
36559+}
36560+
f2c43d5f
AM
36561+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36562+ const char *name, const void *value, size_t size,
36563+ int flags)
c1595e42 36564+{
f2c43d5f 36565+ struct au_sxattr arg = {
c1595e42
JR
36566+ .type = AU_XATTR_SET,
36567+ .u.set = {
36568+ .name = name,
36569+ .value = value,
36570+ .size = size,
36571+ .flags = flags
36572+ },
36573+ };
36574+
f2c43d5f 36575+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36576+}
36577+
36578+/* ---------------------------------------------------------------------- */
36579+
f2c43d5f
AM
36580+static int au_xattr_get(const struct xattr_handler *handler,
36581+ struct dentry *dentry, struct inode *inode,
36582+ const char *name, void *buffer, size_t size)
c1595e42 36583+{
f2c43d5f 36584+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36585+}
36586+
f2c43d5f
AM
36587+static int au_xattr_set(const struct xattr_handler *handler,
36588+ struct dentry *dentry, struct inode *inode,
36589+ const char *name, const void *value, size_t size,
36590+ int flags)
c1595e42 36591+{
f2c43d5f 36592+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36593+}
36594+
36595+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36596+ .name = "",
36597+ .prefix = "",
c1595e42
JR
36598+ .get = au_xattr_get,
36599+ .set = au_xattr_set
c1595e42
JR
36600+};
36601+
36602+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36603+#ifdef CONFIG_FS_POSIX_ACL
36604+ &posix_acl_access_xattr_handler,
36605+ &posix_acl_default_xattr_handler,
36606+#endif
36607+ &au_xattr_handler, /* must be last */
f2c43d5f 36608+ NULL
c1595e42
JR
36609+};
36610+
36611+void au_xattr_init(struct super_block *sb)
36612+{
f2c43d5f 36613+ sb->s_xattr = au_xattr_handlers;
c1595e42 36614+}
7f207e10
AM
36615diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36616--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
36617+++ linux/fs/aufs/xino.c 2018-07-19 09:46:13.066648108 +0200
36618@@ -0,0 +1,1593 @@
36619+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36620+/*
b00004a5 36621+ * Copyright (C) 2005-2018 Junjiro R. Okajima
1facf9fc 36622+ *
36623+ * This program, aufs is free software; you can redistribute it and/or modify
36624+ * it under the terms of the GNU General Public License as published by
36625+ * the Free Software Foundation; either version 2 of the License, or
36626+ * (at your option) any later version.
dece6358
AM
36627+ *
36628+ * This program is distributed in the hope that it will be useful,
36629+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36630+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36631+ * GNU General Public License for more details.
36632+ *
36633+ * You should have received a copy of the GNU General Public License
523b37e3 36634+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36635+ */
36636+
36637+/*
36638+ * external inode number translation table and bitmap
062440b3
AM
36639+ *
36640+ * things to consider
36641+ * - the lifetime
36642+ * + au_xino object
36643+ * + XINO files (xino, xib, xigen)
36644+ * + dynamic debugfs entries (xiN)
36645+ * + static debugfs entries (xib, xigen)
36646+ * + static sysfs entry (xi_path)
36647+ * - several entry points to handle them.
36648+ * + mount(2) without xino option (default)
36649+ * + mount(2) with xino option
36650+ * + mount(2) with noxino option
36651+ * + umount(2)
36652+ * + remount with add/del branches
36653+ * + remount with xino/noxino options
1facf9fc 36654+ */
36655+
36656+#include <linux/seq_file.h>
392086de 36657+#include <linux/statfs.h>
1facf9fc 36658+#include "aufs.h"
36659+
062440b3
AM
36660+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36661+ aufs_bindex_t bbot,
36662+ struct super_block *h_sb)
1facf9fc 36663+{
062440b3
AM
36664+ /* todo: try binary-search if the branches are many */
36665+ for (; btop <= bbot; btop++)
36666+ if (h_sb == au_sbr_sb(sb, btop))
36667+ return btop;
36668+ return -1;
be52b249
AM
36669+}
36670+
062440b3
AM
36671+/*
36672+ * find another branch who is on the same filesystem of the specified
36673+ * branch{@btgt}. search until @bbot.
36674+ */
36675+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36676+ aufs_bindex_t bbot)
1facf9fc 36677+{
062440b3
AM
36678+ aufs_bindex_t bindex;
36679+ struct super_block *tgt_sb;
1facf9fc 36680+
062440b3
AM
36681+ tgt_sb = au_sbr_sb(sb, btgt);
36682+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36683+ if (bindex < 0)
36684+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
1facf9fc 36685+
062440b3 36686+ return bindex;
1facf9fc 36687+}
36688+
36689+/* ---------------------------------------------------------------------- */
36690+
36691+/*
062440b3 36692+ * stop unnecessary notify events at creating xino files
1facf9fc 36693+ */
062440b3
AM
36694+struct au_xino_lock_dir {
36695+ struct au_hinode *hdir;
36696+ struct dentry *parent;
36697+ struct inode *dir;
36698+};
36699+
36700+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36701+ unsigned int lsc)
1facf9fc 36702+{
062440b3
AM
36703+ struct dentry *parent;
36704+ struct inode *dir;
1facf9fc 36705+
062440b3 36706+ parent = dget_parent(dentry);
5527c038 36707+ dir = d_inode(parent);
062440b3
AM
36708+ inode_lock_nested(dir, lsc);
36709+#if 0 /* it should not happen */
36710+ spin_lock(&dentry->d_lock);
36711+ if (unlikely(dentry->d_parent != parent)) {
36712+ spin_unlock(&dentry->d_lock);
36713+ inode_unlock(dir);
36714+ dput(parent);
36715+ parent = NULL;
1facf9fc 36716+ goto out;
36717+ }
062440b3 36718+ spin_unlock(&dentry->d_lock);
1facf9fc 36719+
4f0767ce 36720+out:
062440b3
AM
36721+#endif
36722+ return parent;
1facf9fc 36723+}
36724+
062440b3 36725+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
1facf9fc 36726+ struct au_xino_lock_dir *ldir)
36727+{
36728+ aufs_bindex_t brid, bindex;
36729+
36730+ ldir->hdir = NULL;
36731+ bindex = -1;
36732+ brid = au_xino_brid(sb);
36733+ if (brid >= 0)
36734+ bindex = au_br_index(sb, brid);
36735+ if (bindex >= 0) {
062440b3 36736+ /* rw branch root */
5527c038 36737+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 36738+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 36739+ } else {
062440b3
AM
36740+ /* other */
36741+ ldir->parent = au_dget_parent_lock(xipath->dentry,
36742+ AuLsc_I_PARENT);
febd17d6 36743+ ldir->dir = d_inode(ldir->parent);
1facf9fc 36744+ }
36745+}
36746+
36747+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36748+{
36749+ if (ldir->hdir)
5afbbe0d 36750+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 36751+ else {
febd17d6 36752+ inode_unlock(ldir->dir);
1facf9fc 36753+ dput(ldir->parent);
36754+ }
36755+}
36756+
36757+/* ---------------------------------------------------------------------- */
36758+
062440b3
AM
36759+/*
36760+ * create and set a new xino file
36761+ */
36762+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent)
36763+{
36764+ struct file *file;
36765+ struct dentry *h_parent, *d;
36766+ struct inode *h_dir, *inode;
36767+ int err;
36768+
36769+ /*
36770+ * at mount-time, and the xino file is the default path,
36771+ * hnotify is disabled so we have no notify events to ignore.
36772+ * when a user specified the xino, we cannot get au_hdir to be ignored.
36773+ */
36774+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36775+ /* | __FMODE_NONOTIFY */,
36776+ S_IRUGO | S_IWUGO);
36777+ if (IS_ERR(file)) {
36778+ if (!silent)
36779+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36780+ return file;
36781+ }
36782+
36783+ /* keep file count */
36784+ err = 0;
36785+ d = file->f_path.dentry;
36786+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36787+ /* mnt_want_write() is unnecessary here */
36788+ h_dir = d_inode(h_parent);
36789+ inode = file_inode(file);
36790+ /* no delegation since it is just created */
36791+ if (inode->i_nlink)
36792+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36793+ /*force*/0);
36794+ inode_unlock(h_dir);
36795+ dput(h_parent);
36796+ if (unlikely(err)) {
36797+ if (!silent)
36798+ pr_err("unlink %s(%d)\n", fpath, err);
36799+ goto out;
36800+ }
36801+
36802+ err = -EINVAL;
36803+ if (unlikely(sb == d->d_sb)) {
36804+ if (!silent)
36805+ pr_err("%s must be outside\n", fpath);
36806+ goto out;
36807+ }
36808+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36809+ if (!silent)
36810+ pr_err("xino doesn't support %s(%s)\n",
36811+ fpath, au_sbtype(d->d_sb));
36812+ goto out;
36813+ }
36814+ return file; /* success */
36815+
36816+out:
36817+ fput(file);
36818+ file = ERR_PTR(err);
36819+ return file;
36820+}
36821+
36822+/*
36823+ * create a new xinofile at the same place/path as @base.
36824+ */
36825+struct file *au_xino_create2(struct super_block *sb, struct path *base,
36826+ struct file *copy_src)
36827+{
36828+ struct file *file;
36829+ struct dentry *dentry, *parent;
36830+ struct inode *dir, *delegated;
36831+ struct qstr *name;
36832+ struct path path;
36833+ int err, do_unlock;
36834+ struct au_xino_lock_dir ldir;
36835+
36836+ do_unlock = 1;
36837+ au_xino_lock_dir(sb, base, &ldir);
36838+ dentry = base->dentry;
36839+ parent = dentry->d_parent; /* dir inode is locked */
36840+ dir = d_inode(parent);
36841+ IMustLock(dir);
36842+
36843+ name = &dentry->d_name;
36844+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
36845+ if (IS_ERR(path.dentry)) {
36846+ file = (void *)path.dentry;
36847+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
36848+ goto out;
36849+ }
36850+
36851+ /* no need to mnt_want_write() since we call dentry_open() later */
36852+ err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
36853+ if (unlikely(err)) {
36854+ file = ERR_PTR(err);
36855+ pr_err("%pd create err %d\n", dentry, err);
36856+ goto out_dput;
36857+ }
36858+
36859+ path.mnt = base->mnt;
36860+ file = vfsub_dentry_open(&path,
36861+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36862+ /* | __FMODE_NONOTIFY */);
36863+ if (IS_ERR(file)) {
36864+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
36865+ goto out_dput;
36866+ }
36867+
36868+ delegated = NULL;
36869+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
36870+ au_xino_unlock_dir(&ldir);
36871+ do_unlock = 0;
36872+ if (unlikely(err == -EWOULDBLOCK)) {
36873+ pr_warn("cannot retry for NFSv4 delegation"
36874+ " for an internal unlink\n");
36875+ iput(delegated);
36876+ }
36877+ if (unlikely(err)) {
36878+ pr_err("%pd unlink err %d\n", dentry, err);
36879+ goto out_fput;
36880+ }
36881+
36882+ if (copy_src) {
36883+ /* no one can touch copy_src xino */
36884+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
36885+ if (unlikely(err)) {
36886+ pr_err("%pd copy err %d\n", dentry, err);
36887+ goto out_fput;
36888+ }
36889+ }
36890+ goto out_dput; /* success */
1facf9fc 36891+
062440b3
AM
36892+out_fput:
36893+ fput(file);
36894+ file = ERR_PTR(err);
36895+out_dput:
36896+ dput(path.dentry);
36897+out:
36898+ if (do_unlock)
36899+ au_xino_unlock_dir(&ldir);
36900+ return file;
36901+}
36902+
36903+/* ---------------------------------------------------------------------- */
36904+
36905+/*
36906+ * truncate xino files
36907+ */
1facf9fc 36908+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
36909+{
36910+ int err;
392086de
AM
36911+ unsigned long jiffy;
36912+ blkcnt_t blocks;
062440b3 36913+ aufs_bindex_t bbot;
392086de 36914+ struct kstatfs *st;
1facf9fc 36915+ struct au_branch *br;
36916+ struct file *new_xino, *file;
062440b3 36917+ struct path *path;
1facf9fc 36918+
392086de 36919+ err = -ENOMEM;
be52b249 36920+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
36921+ if (unlikely(!st))
36922+ goto out;
36923+
1facf9fc 36924+ err = -EINVAL;
5afbbe0d
AM
36925+ bbot = au_sbbot(sb);
36926+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 36927+ goto out_st;
1facf9fc 36928+ br = au_sbr(sb, bindex);
062440b3 36929+ file = au_xino_file(br);
1facf9fc 36930+ if (!file)
392086de
AM
36931+ goto out_st;
36932+
062440b3
AM
36933+ path = &file->f_path;
36934+ err = vfs_statfs(path, st);
392086de
AM
36935+ if (unlikely(err))
36936+ AuErr1("statfs err %d, ignored\n", err);
36937+ jiffy = jiffies;
36938+ blocks = file_inode(file)->i_blocks;
36939+ pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36940+ bindex, (u64)blocks, st->f_bfree, st->f_blocks);
1facf9fc 36941+
062440b3 36942+ new_xino = au_xino_create2(sb, path, file);
1facf9fc 36943+ err = PTR_ERR(new_xino);
392086de
AM
36944+ if (IS_ERR(new_xino)) {
36945+ pr_err("err %d, ignored\n", err);
36946+ goto out_st;
36947+ }
1facf9fc 36948+ err = 0;
062440b3 36949+ au_xino_file_set(br, new_xino);
1facf9fc 36950+
392086de
AM
36951+ err = vfs_statfs(&new_xino->f_path, st);
36952+ if (!err) {
36953+ pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36954+ bindex, (u64)file_inode(new_xino)->i_blocks,
36955+ st->f_bfree, st->f_blocks);
36956+ if (file_inode(new_xino)->i_blocks < blocks)
36957+ au_sbi(sb)->si_xino_jiffy = jiffy;
36958+ } else
36959+ AuErr1("statfs err %d, ignored\n", err);
062440b3 36960+ fput(new_xino);
392086de
AM
36961+
36962+out_st:
1c60b727 36963+ kfree(st);
4f0767ce 36964+out:
1facf9fc 36965+ return err;
36966+}
36967+
36968+struct xino_do_trunc_args {
36969+ struct super_block *sb;
36970+ struct au_branch *br;
36971+};
36972+
36973+static void xino_do_trunc(void *_args)
36974+{
36975+ struct xino_do_trunc_args *args = _args;
36976+ struct super_block *sb;
36977+ struct au_branch *br;
36978+ struct inode *dir;
36979+ int err;
36980+ aufs_bindex_t bindex;
36981+
36982+ err = 0;
36983+ sb = args->sb;
5527c038 36984+ dir = d_inode(sb->s_root);
1facf9fc 36985+ br = args->br;
36986+
36987+ si_noflush_write_lock(sb);
36988+ ii_read_lock_parent(dir);
36989+ bindex = au_br_index(sb, br->br_id);
36990+ err = au_xino_trunc(sb, bindex);
1facf9fc 36991+ ii_read_unlock(dir);
36992+ if (unlikely(err))
392086de 36993+ pr_warn("err b%d, (%d)\n", bindex, err);
062440b3 36994+ atomic_dec(&br->br_xino->xi_truncating);
5afbbe0d 36995+ au_br_put(br);
1facf9fc 36996+ si_write_unlock(sb);
027c5e7a 36997+ au_nwt_done(&au_sbi(sb)->si_nowait);
1c60b727 36998+ kfree(args);
1facf9fc 36999+}
37000+
392086de
AM
37001+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37002+{
37003+ int err;
37004+ struct kstatfs st;
37005+ struct au_sbinfo *sbinfo;
062440b3 37006+ struct file *file;
392086de
AM
37007+
37008+ /* todo: si_xino_expire and the ratio should be customizable */
37009+ sbinfo = au_sbi(sb);
37010+ if (time_before(jiffies,
37011+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37012+ return 0;
37013+
37014+ /* truncation border */
062440b3
AM
37015+ file = au_xino_file(br);
37016+ AuDebugOn(!file);
37017+ err = vfs_statfs(&file->f_path, &st);
392086de
AM
37018+ if (unlikely(err)) {
37019+ AuErr1("statfs err %d, ignored\n", err);
37020+ return 0;
37021+ }
37022+ if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
37023+ return 0;
37024+
37025+ return 1;
37026+}
37027+
1facf9fc 37028+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37029+{
37030+ struct xino_do_trunc_args *args;
37031+ int wkq_err;
37032+
392086de 37033+ if (!xino_trunc_test(sb, br))
1facf9fc 37034+ return;
37035+
062440b3 37036+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
1facf9fc 37037+ goto out;
37038+
37039+ /* lock and kfree() will be called in trunc_xino() */
37040+ args = kmalloc(sizeof(*args), GFP_NOFS);
37041+ if (unlikely(!args)) {
37042+ AuErr1("no memory\n");
f0c0a007 37043+ goto out;
1facf9fc 37044+ }
37045+
5afbbe0d 37046+ au_br_get(br);
1facf9fc 37047+ args->sb = sb;
37048+ args->br = br;
53392da6 37049+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 37050+ if (!wkq_err)
37051+ return; /* success */
37052+
4a4d8108 37053+ pr_err("wkq %d\n", wkq_err);
5afbbe0d 37054+ au_br_put(br);
1c60b727 37055+ kfree(args);
1facf9fc 37056+
4f0767ce 37057+out:
062440b3 37058+ atomic_dec(&br->br_xino->xi_truncating);
1facf9fc 37059+}
37060+
37061+/* ---------------------------------------------------------------------- */
37062+
062440b3
AM
37063+/*
37064+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37065+ * at the position of @h_ino.
37066+ */
37067+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37068+ ino_t *ino)
37069+{
37070+ int err;
37071+ ssize_t sz;
37072+ loff_t pos;
37073+ struct au_branch *br;
37074+ struct file *file;
37075+ struct au_sbinfo *sbinfo;
37076+
37077+ *ino = 0;
37078+ if (!au_opt_test(au_mntflags(sb), XINO))
37079+ return 0; /* no xino */
37080+
37081+ err = 0;
37082+ sbinfo = au_sbi(sb);
37083+ pos = h_ino;
37084+ if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
37085+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
37086+ return -EFBIG;
37087+ }
37088+ pos *= sizeof(*ino);
37089+
37090+ br = au_sbr(sb, bindex);
37091+ file = au_xino_file(br);
37092+ if (vfsub_f_size_read(file) < pos + sizeof(*ino))
37093+ return 0; /* no ino */
37094+
37095+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
37096+ if (sz == sizeof(*ino))
37097+ return 0; /* success */
37098+
37099+ err = sz;
37100+ if (unlikely(sz >= 0)) {
37101+ err = -EIO;
37102+ AuIOErr("xino read error (%zd)\n", sz);
37103+ }
37104+
37105+ return err;
37106+}
37107+
5527c038 37108+static int au_xino_do_write(vfs_writef_t write, struct file *file,
1facf9fc 37109+ ino_t h_ino, ino_t ino)
37110+{
37111+ loff_t pos;
37112+ ssize_t sz;
37113+
37114+ pos = h_ino;
37115+ if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
37116+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
37117+ return -EFBIG;
37118+ }
37119+ pos *= sizeof(ino);
37120+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
37121+ if (sz == sizeof(ino))
37122+ return 0; /* success */
37123+
37124+ AuIOErr("write failed (%zd)\n", sz);
37125+ return -EIO;
37126+}
37127+
37128+/*
37129+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37130+ * at the position of @h_ino.
37131+ * even if @ino is zero, it is written to the xinofile and means no entry.
37132+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37133+ * try truncating it.
37134+ */
37135+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37136+ ino_t ino)
37137+{
37138+ int err;
37139+ unsigned int mnt_flags;
37140+ struct au_branch *br;
37141+
37142+ BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
37143+ || ((loff_t)-1) > 0);
dece6358 37144+ SiMustAnyLock(sb);
1facf9fc 37145+
37146+ mnt_flags = au_mntflags(sb);
37147+ if (!au_opt_test(mnt_flags, XINO))
37148+ return 0;
37149+
37150+ br = au_sbr(sb, bindex);
062440b3
AM
37151+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, au_xino_file(br), h_ino,
37152+ ino);
1facf9fc 37153+ if (!err) {
37154+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37155+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37156+ xino_try_trunc(sb, br);
37157+ return 0; /* success */
37158+ }
37159+
37160+ AuIOErr("write failed (%d)\n", err);
37161+ return -EIO;
37162+}
37163+
062440b3
AM
37164+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37165+ size_t size, loff_t *pos);
1facf9fc 37166+
062440b3
AM
37167+/* todo: unnecessary to support mmap_sem since kernel-space? */
37168+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37169+ loff_t *pos)
37170+{
37171+ ssize_t err;
37172+ mm_segment_t oldfs;
37173+ union {
37174+ void *k;
37175+ char __user *u;
37176+ } buf;
37177+ int i;
37178+ const int prevent_endless = 10;
1facf9fc 37179+
062440b3
AM
37180+ i = 0;
37181+ buf.k = kbuf;
37182+ oldfs = get_fs();
37183+ set_fs(KERNEL_DS);
37184+ do {
37185+ err = func(file, buf.u, size, pos);
37186+ if (err == -EINTR
37187+ && !au_wkq_test()
37188+ && fatal_signal_pending(current)) {
37189+ set_fs(oldfs);
37190+ err = xino_fread_wkq(func, file, kbuf, size, pos);
37191+ BUG_ON(err == -EINTR);
37192+ oldfs = get_fs();
37193+ set_fs(KERNEL_DS);
37194+ }
37195+ } while (i++ < prevent_endless
37196+ && (err == -EAGAIN || err == -EINTR));
37197+ set_fs(oldfs);
37198+
37199+#if 0 /* reserved for future use */
37200+ if (err > 0)
37201+ fsnotify_access(file->f_path.dentry);
37202+#endif
37203+
37204+ return err;
37205+}
37206+
37207+struct xino_fread_args {
37208+ ssize_t *errp;
37209+ vfs_readf_t func;
37210+ struct file *file;
37211+ void *buf;
37212+ size_t size;
37213+ loff_t *pos;
37214+};
37215+
37216+static void call_xino_fread(void *args)
37217+{
37218+ struct xino_fread_args *a = args;
37219+ *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37220+}
37221+
37222+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37223+ size_t size, loff_t *pos)
37224+{
37225+ ssize_t err;
37226+ int wkq_err;
37227+ struct xino_fread_args args = {
37228+ .errp = &err,
37229+ .func = func,
37230+ .file = file,
37231+ .buf = buf,
37232+ .size = size,
37233+ .pos = pos
37234+ };
37235+
37236+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37237+ if (unlikely(wkq_err))
37238+ err = wkq_err;
37239+
37240+ return err;
37241+}
37242+
37243+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37244+ size_t size, loff_t *pos);
37245+
37246+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37247+ size_t size, loff_t *pos)
37248+{
37249+ ssize_t err;
37250+ mm_segment_t oldfs;
37251+ union {
37252+ void *k;
37253+ const char __user *u;
37254+ } buf;
37255+ int i;
37256+ const int prevent_endless = 10;
37257+
37258+ i = 0;
37259+ buf.k = kbuf;
37260+ oldfs = get_fs();
37261+ set_fs(KERNEL_DS);
37262+ do {
37263+ err = func(file, buf.u, size, pos);
37264+ if (err == -EINTR
37265+ && !au_wkq_test()
37266+ && fatal_signal_pending(current)) {
37267+ set_fs(oldfs);
37268+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37269+ BUG_ON(err == -EINTR);
37270+ oldfs = get_fs();
37271+ set_fs(KERNEL_DS);
37272+ }
37273+ } while (i++ < prevent_endless
37274+ && (err == -EAGAIN || err == -EINTR));
37275+ set_fs(oldfs);
37276+
37277+#if 0 /* reserved for future use */
37278+ if (err > 0)
37279+ fsnotify_modify(file->f_path.dentry);
37280+#endif
37281+
37282+ return err;
37283+}
37284+
37285+struct do_xino_fwrite_args {
37286+ ssize_t *errp;
37287+ vfs_writef_t func;
37288+ struct file *file;
37289+ void *buf;
37290+ size_t size;
37291+ loff_t *pos;
37292+};
37293+
37294+static void call_do_xino_fwrite(void *args)
37295+{
37296+ struct do_xino_fwrite_args *a = args;
37297+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37298+}
37299+
37300+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37301+ size_t size, loff_t *pos)
37302+{
37303+ ssize_t err;
37304+ int wkq_err;
37305+ struct do_xino_fwrite_args args = {
37306+ .errp = &err,
37307+ .func = func,
37308+ .file = file,
37309+ .buf = buf,
37310+ .size = size,
37311+ .pos = pos
37312+ };
37313+
37314+ /*
37315+ * it breaks RLIMIT_FSIZE and normal user's limit,
37316+ * users should care about quota and real 'filesystem full.'
37317+ */
37318+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37319+ if (unlikely(wkq_err))
37320+ err = wkq_err;
37321+
37322+ return err;
37323+}
37324+
37325+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37326+ size_t size, loff_t *pos)
37327+{
37328+ ssize_t err;
37329+
37330+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37331+ lockdep_off();
37332+ err = do_xino_fwrite(func, file, buf, size, pos);
37333+ lockdep_on();
37334+ } else {
37335+ lockdep_off();
37336+ err = xino_fwrite_wkq(func, file, buf, size, pos);
37337+ lockdep_on();
37338+ }
37339+
37340+ return err;
37341+}
37342+
37343+/* ---------------------------------------------------------------------- */
37344+
37345+/*
37346+ * inode number bitmap
37347+ */
1facf9fc 37348+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37349+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37350+{
37351+ ino_t ino;
37352+
37353+ AuDebugOn(bit < 0 || page_bits <= bit);
37354+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37355+ return ino;
37356+}
37357+
37358+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37359+{
37360+ AuDebugOn(ino < AUFS_FIRST_INO);
37361+ ino -= AUFS_FIRST_INO;
37362+ *pindex = ino / page_bits;
37363+ *bit = ino % page_bits;
37364+}
37365+
37366+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37367+{
37368+ int err;
37369+ loff_t pos;
37370+ ssize_t sz;
37371+ struct au_sbinfo *sbinfo;
37372+ struct file *xib;
37373+ unsigned long *p;
37374+
37375+ sbinfo = au_sbi(sb);
37376+ MtxMustLock(&sbinfo->si_xib_mtx);
37377+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37378+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37379+
37380+ if (pindex == sbinfo->si_xib_last_pindex)
37381+ return 0;
37382+
37383+ xib = sbinfo->si_xib;
37384+ p = sbinfo->si_xib_buf;
37385+ pos = sbinfo->si_xib_last_pindex;
37386+ pos *= PAGE_SIZE;
37387+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37388+ if (unlikely(sz != PAGE_SIZE))
37389+ goto out;
37390+
37391+ pos = pindex;
37392+ pos *= PAGE_SIZE;
c06a8ce3 37393+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 37394+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37395+ else {
37396+ memset(p, 0, PAGE_SIZE);
37397+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37398+ }
37399+ if (sz == PAGE_SIZE) {
37400+ sbinfo->si_xib_last_pindex = pindex;
37401+ return 0; /* success */
37402+ }
37403+
4f0767ce 37404+out:
b752ccd1
AM
37405+ AuIOErr1("write failed (%zd)\n", sz);
37406+ err = sz;
37407+ if (sz >= 0)
37408+ err = -EIO;
37409+ return err;
37410+}
37411+
b752ccd1
AM
37412+static void au_xib_clear_bit(struct inode *inode)
37413+{
37414+ int err, bit;
37415+ unsigned long pindex;
37416+ struct super_block *sb;
37417+ struct au_sbinfo *sbinfo;
37418+
37419+ AuDebugOn(inode->i_nlink);
37420+
37421+ sb = inode->i_sb;
37422+ xib_calc_bit(inode->i_ino, &pindex, &bit);
37423+ AuDebugOn(page_bits <= bit);
37424+ sbinfo = au_sbi(sb);
37425+ mutex_lock(&sbinfo->si_xib_mtx);
37426+ err = xib_pindex(sb, pindex);
37427+ if (!err) {
37428+ clear_bit(bit, sbinfo->si_xib_buf);
37429+ sbinfo->si_xib_next_bit = bit;
37430+ }
37431+ mutex_unlock(&sbinfo->si_xib_mtx);
37432+}
37433+
1facf9fc 37434+/* ---------------------------------------------------------------------- */
37435+
1facf9fc 37436+/*
062440b3 37437+ * truncate a xino bitmap file
1facf9fc 37438+ */
1facf9fc 37439+
37440+/* todo: slow */
37441+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37442+{
37443+ int err, bit;
37444+ ssize_t sz;
37445+ unsigned long pindex;
37446+ loff_t pos, pend;
37447+ struct au_sbinfo *sbinfo;
5527c038 37448+ vfs_readf_t func;
1facf9fc 37449+ ino_t *ino;
37450+ unsigned long *p;
37451+
37452+ err = 0;
37453+ sbinfo = au_sbi(sb);
dece6358 37454+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 37455+ p = sbinfo->si_xib_buf;
37456+ func = sbinfo->si_xread;
c06a8ce3 37457+ pend = vfsub_f_size_read(file);
1facf9fc 37458+ pos = 0;
37459+ while (pos < pend) {
37460+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
37461+ err = sz;
37462+ if (unlikely(sz <= 0))
37463+ goto out;
37464+
37465+ err = 0;
37466+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37467+ if (unlikely(*ino < AUFS_FIRST_INO))
37468+ continue;
37469+
37470+ xib_calc_bit(*ino, &pindex, &bit);
37471+ AuDebugOn(page_bits <= bit);
37472+ err = xib_pindex(sb, pindex);
37473+ if (!err)
37474+ set_bit(bit, p);
37475+ else
37476+ goto out;
37477+ }
37478+ }
37479+
4f0767ce 37480+out:
1facf9fc 37481+ return err;
37482+}
37483+
37484+static int xib_restore(struct super_block *sb)
37485+{
37486+ int err;
5afbbe0d 37487+ aufs_bindex_t bindex, bbot;
1facf9fc 37488+ void *page;
062440b3 37489+ struct au_branch *br;
1facf9fc 37490+
37491+ err = -ENOMEM;
37492+ page = (void *)__get_free_page(GFP_NOFS);
37493+ if (unlikely(!page))
37494+ goto out;
37495+
37496+ err = 0;
5afbbe0d
AM
37497+ bbot = au_sbbot(sb);
37498+ for (bindex = 0; !err && bindex <= bbot; bindex++)
062440b3
AM
37499+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37500+ br = au_sbr(sb, bindex);
37501+ err = do_xib_restore(sb, au_xino_file(br), page);
37502+ } else
37503+ AuDbg("skip shared b%d\n", bindex);
1c60b727 37504+ free_page((unsigned long)page);
1facf9fc 37505+
4f0767ce 37506+out:
1facf9fc 37507+ return err;
37508+}
37509+
37510+int au_xib_trunc(struct super_block *sb)
37511+{
37512+ int err;
37513+ ssize_t sz;
37514+ loff_t pos;
1facf9fc 37515+ struct au_sbinfo *sbinfo;
37516+ unsigned long *p;
37517+ struct file *file;
37518+
dece6358
AM
37519+ SiMustWriteLock(sb);
37520+
1facf9fc 37521+ err = 0;
37522+ sbinfo = au_sbi(sb);
37523+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
37524+ goto out;
37525+
37526+ file = sbinfo->si_xib;
c06a8ce3 37527+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 37528+ goto out;
37529+
062440b3 37530+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
1facf9fc 37531+ err = PTR_ERR(file);
37532+ if (IS_ERR(file))
37533+ goto out;
37534+ fput(sbinfo->si_xib);
37535+ sbinfo->si_xib = file;
37536+
37537+ p = sbinfo->si_xib_buf;
37538+ memset(p, 0, PAGE_SIZE);
37539+ pos = 0;
37540+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
37541+ if (unlikely(sz != PAGE_SIZE)) {
37542+ err = sz;
37543+ AuIOErr("err %d\n", err);
37544+ if (sz >= 0)
37545+ err = -EIO;
37546+ goto out;
37547+ }
37548+
37549+ mutex_lock(&sbinfo->si_xib_mtx);
37550+ /* mnt_want_write() is unnecessary here */
37551+ err = xib_restore(sb);
37552+ mutex_unlock(&sbinfo->si_xib_mtx);
37553+
37554+out:
37555+ return err;
37556+}
37557+
37558+/* ---------------------------------------------------------------------- */
37559+
062440b3
AM
37560+struct au_xino *au_xino_alloc(void)
37561+{
37562+ struct au_xino *xi;
37563+
37564+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
37565+ if (unlikely(!xi))
37566+ goto out;
37567+
37568+ xi->xi_nondir.total = 8; /* initial size */
37569+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
37570+ GFP_NOFS);
37571+ if (unlikely(!xi->xi_nondir.array))
37572+ goto out_free;
37573+
37574+ spin_lock_init(&xi->xi_nondir.spin);
37575+ init_waitqueue_head(&xi->xi_nondir.wqh);
37576+ atomic_set(&xi->xi_truncating, 0);
37577+ kref_init(&xi->xi_kref);
37578+ goto out; /* success */
37579+
37580+out_free:
37581+ kfree(xi);
37582+ xi = NULL;
37583+out:
37584+ return xi;
37585+}
37586+
37587+static int au_xino_init(struct au_branch *br, struct file *file)
37588+{
37589+ int err;
37590+ struct au_xino *xi;
37591+
37592+ err = 0;
37593+ xi = au_xino_alloc();
37594+ if (unlikely(!xi)) {
37595+ err = -ENOMEM;
37596+ goto out;
37597+ }
37598+
37599+ get_file(file);
37600+ xi->xi_file = file;
37601+ AuDebugOn(br->br_xino);
37602+ br->br_xino = xi;
37603+
37604+out:
37605+ return err;
37606+}
37607+
37608+static void au_xino_release(struct kref *kref)
37609+{
37610+ struct au_xino *xi;
37611+ int i;
37612+
37613+ xi = container_of(kref, struct au_xino, xi_kref);
37614+ if (xi->xi_file)
37615+ fput(xi->xi_file);
37616+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
37617+ AuDebugOn(xi->xi_nondir.array[i]);
37618+ kfree(xi->xi_nondir.array);
37619+ kfree(xi);
37620+}
37621+
37622+int au_xino_put(struct au_branch *br)
37623+{
37624+ int ret;
37625+ struct au_xino *xi;
37626+
37627+ ret = 0;
37628+ xi = br->br_xino;
37629+ if (xi) {
37630+ br->br_xino = NULL;
37631+ ret = kref_put(&xi->xi_kref, au_xino_release);
37632+ }
37633+
37634+ return ret;
37635+}
37636+
37637+void au_xino_file_set(struct au_branch *br, struct file *file)
37638+{
37639+ struct au_xino *xi;
37640+ struct file *f;
37641+
37642+ if (file)
37643+ get_file(file);
37644+ xi = br->br_xino;
37645+ AuDebugOn(!xi);
37646+ f = xi->xi_file;
37647+ if (f)
37648+ fput(f);
37649+ xi->xi_file = file;
37650+}
37651+
37652+/* ---------------------------------------------------------------------- */
37653+
1facf9fc 37654+/*
37655+ * xino mount option handlers
37656+ */
1facf9fc 37657+
37658+/* xino bitmap */
37659+static void xino_clear_xib(struct super_block *sb)
37660+{
37661+ struct au_sbinfo *sbinfo;
37662+
dece6358
AM
37663+ SiMustWriteLock(sb);
37664+
1facf9fc 37665+ sbinfo = au_sbi(sb);
062440b3 37666+ /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
1facf9fc 37667+ if (sbinfo->si_xib)
37668+ fput(sbinfo->si_xib);
37669+ sbinfo->si_xib = NULL;
f0c0a007 37670+ if (sbinfo->si_xib_buf)
1c60b727 37671+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 37672+ sbinfo->si_xib_buf = NULL;
37673+}
37674+
062440b3 37675+static int au_xino_set_xib(struct super_block *sb, struct path *path)
1facf9fc 37676+{
37677+ int err;
37678+ loff_t pos;
37679+ struct au_sbinfo *sbinfo;
37680+ struct file *file;
37681+
dece6358
AM
37682+ SiMustWriteLock(sb);
37683+
1facf9fc 37684+ sbinfo = au_sbi(sb);
062440b3 37685+ file = au_xino_create2(sb, path, sbinfo->si_xib);
1facf9fc 37686+ err = PTR_ERR(file);
37687+ if (IS_ERR(file))
37688+ goto out;
37689+ if (sbinfo->si_xib)
37690+ fput(sbinfo->si_xib);
37691+ sbinfo->si_xib = file;
5527c038
JR
37692+ sbinfo->si_xread = vfs_readf(file);
37693+ sbinfo->si_xwrite = vfs_writef(file);
1facf9fc 37694+
37695+ err = -ENOMEM;
37696+ if (!sbinfo->si_xib_buf)
37697+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
37698+ if (unlikely(!sbinfo->si_xib_buf))
37699+ goto out_unset;
37700+
37701+ sbinfo->si_xib_last_pindex = 0;
37702+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 37703+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 37704+ pos = 0;
37705+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
37706+ PAGE_SIZE, &pos);
37707+ if (unlikely(err != PAGE_SIZE))
37708+ goto out_free;
37709+ }
37710+ err = 0;
37711+ goto out; /* success */
37712+
4f0767ce 37713+out_free:
f0c0a007 37714+ if (sbinfo->si_xib_buf)
1c60b727 37715+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
37716+ sbinfo->si_xib_buf = NULL;
37717+ if (err >= 0)
37718+ err = -EIO;
4f0767ce 37719+out_unset:
b752ccd1
AM
37720+ fput(sbinfo->si_xib);
37721+ sbinfo->si_xib = NULL;
4f0767ce 37722+out:
062440b3 37723+ AuTraceErr(err);
b752ccd1 37724+ return err;
1facf9fc 37725+}
37726+
b752ccd1
AM
37727+/* xino for each branch */
37728+static void xino_clear_br(struct super_block *sb)
37729+{
5afbbe0d 37730+ aufs_bindex_t bindex, bbot;
b752ccd1 37731+ struct au_branch *br;
1facf9fc 37732+
5afbbe0d
AM
37733+ bbot = au_sbbot(sb);
37734+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1 37735+ br = au_sbr(sb, bindex);
062440b3
AM
37736+ AuDebugOn(!br);
37737+ au_xino_put(br);
37738+ }
37739+}
37740+
37741+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
37742+ aufs_bindex_t bshared)
37743+{
37744+ struct au_branch *brshared;
b752ccd1 37745+
062440b3
AM
37746+ brshared = au_sbr(sb, bshared);
37747+ AuDebugOn(!brshared->br_xino);
37748+ AuDebugOn(!brshared->br_xino->xi_file);
37749+ if (br->br_xino != brshared->br_xino) {
37750+ au_xino_get(brshared);
37751+ au_xino_put(br);
37752+ br->br_xino = brshared->br_xino;
b752ccd1
AM
37753+ }
37754+}
37755+
062440b3
AM
37756+struct au_xino_do_set_br {
37757+ vfs_writef_t writef;
37758+ struct au_branch *br;
37759+ ino_t h_ino;
37760+ aufs_bindex_t bshared;
37761+};
37762+
37763+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
37764+ struct au_xino_do_set_br *args)
1facf9fc 37765+{
37766+ int err;
062440b3
AM
37767+ struct file *file;
37768+
37769+ if (args->bshared >= 0) {
37770+ /* shared xino */
37771+ au_xino_set_br_shared(sb, args->br, args->bshared);
37772+ file = au_xino_file(args->br);
37773+ goto out_ino; /* success */
37774+ }
37775+
37776+ /* new xino */
37777+ file = au_xino_create2(sb, path, au_xino_file(args->br));
37778+ err = PTR_ERR(file);
37779+ if (IS_ERR(file))
37780+ goto out;
37781+ if (!args->br->br_xino) {
37782+ err = au_xino_init(args->br, file);
37783+ fput(file);
37784+ if (unlikely(err))
37785+ goto out;
37786+ } else {
37787+ au_xino_file_set(args->br, file);
37788+ fput(file);
37789+ }
37790+
37791+out_ino:
37792+ err = au_xino_do_write(args->writef, file, args->h_ino, AUFS_ROOT_INO);
37793+out:
37794+ AuTraceErr(err);
37795+ return err;
37796+}
37797+
37798+static int au_xino_set_br(struct super_block *sb, struct path *path)
37799+{
37800+ int err;
37801+ aufs_bindex_t bindex, bbot;
37802+ struct au_xino_do_set_br args;
b752ccd1 37803+ struct inode *inode;
1facf9fc 37804+
b752ccd1
AM
37805+ SiMustWriteLock(sb);
37806+
5afbbe0d 37807+ bbot = au_sbbot(sb);
5527c038 37808+ inode = d_inode(sb->s_root);
062440b3
AM
37809+ args.writef = au_sbi(sb)->si_xwrite;
37810+ for (bindex = 0; bindex <= bbot; bindex++) {
37811+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
37812+ args.br = au_sbr(sb, bindex);
37813+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
37814+ err = au_xino_do_set_br(sb, path, &args);
b752ccd1 37815+ if (unlikely(err))
062440b3 37816+ break;
b752ccd1 37817+ }
1facf9fc 37818+
062440b3 37819+ AuTraceErr(err);
1facf9fc 37820+ return err;
37821+}
b752ccd1
AM
37822+
37823+void au_xino_clr(struct super_block *sb)
37824+{
37825+ struct au_sbinfo *sbinfo;
37826+
37827+ au_xigen_clr(sb);
37828+ xino_clear_xib(sb);
37829+ xino_clear_br(sb);
062440b3
AM
37830+ dbgaufs_brs_del(sb, 0);
37831+ au_xino_brid_set(sb, -1);
b752ccd1
AM
37832+ sbinfo = au_sbi(sb);
37833+ /* lvalue, do not call au_mntflags() */
37834+ au_opt_clr(sbinfo->si_mntflags, XINO);
37835+}
37836+
062440b3 37837+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
b752ccd1
AM
37838+{
37839+ int err, skip;
062440b3 37840+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
b752ccd1
AM
37841+ struct qstr *dname, *cur_name;
37842+ struct file *cur_xino;
b752ccd1 37843+ struct au_sbinfo *sbinfo;
062440b3 37844+ struct path *path, *cur_path;
b752ccd1
AM
37845+
37846+ SiMustWriteLock(sb);
37847+
37848+ err = 0;
37849+ sbinfo = au_sbi(sb);
062440b3
AM
37850+ path = &xiopt->file->f_path;
37851+ dentry = path->dentry;
37852+ parent = dget_parent(dentry);
b752ccd1
AM
37853+ if (remount) {
37854+ skip = 0;
b752ccd1
AM
37855+ cur_xino = sbinfo->si_xib;
37856+ if (cur_xino) {
062440b3
AM
37857+ cur_path = &cur_xino->f_path;
37858+ cur_dentry = cur_path->dentry;
37859+ cur_parent = dget_parent(cur_dentry);
37860+ cur_name = &cur_dentry->d_name;
37861+ dname = &dentry->d_name;
b752ccd1 37862+ skip = (cur_parent == parent
38d290e6 37863+ && au_qstreq(dname, cur_name));
b752ccd1
AM
37864+ dput(cur_parent);
37865+ }
37866+ if (skip)
37867+ goto out;
37868+ }
37869+
37870+ au_opt_set(sbinfo->si_mntflags, XINO);
062440b3
AM
37871+ err = au_xino_set_xib(sb, path);
37872+ /* si_x{read,write} are set */
b752ccd1 37873+ if (!err)
062440b3 37874+ err = au_xigen_set(sb, path);
b752ccd1 37875+ if (!err)
062440b3
AM
37876+ err = au_xino_set_br(sb, path);
37877+ if (!err) {
37878+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
b752ccd1 37879+ goto out; /* success */
062440b3 37880+ }
b752ccd1
AM
37881+
37882+ /* reset all */
062440b3
AM
37883+ AuIOErr("failed setting xino(%d).\n", err);
37884+ au_xino_clr(sb);
b752ccd1 37885+
4f0767ce 37886+out:
b752ccd1
AM
37887+ dput(parent);
37888+ return err;
37889+}
37890+
b752ccd1
AM
37891+/*
37892+ * create a xinofile at the default place/path.
37893+ */
37894+struct file *au_xino_def(struct super_block *sb)
37895+{
37896+ struct file *file;
37897+ char *page, *p;
37898+ struct au_branch *br;
37899+ struct super_block *h_sb;
37900+ struct path path;
5afbbe0d 37901+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
37902+
37903+ br = NULL;
5afbbe0d 37904+ bbot = au_sbbot(sb);
b752ccd1 37905+ bwr = -1;
5afbbe0d 37906+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
37907+ br = au_sbr(sb, bindex);
37908+ if (au_br_writable(br->br_perm)
86dc4139 37909+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
37910+ bwr = bindex;
37911+ break;
37912+ }
37913+ }
37914+
7f207e10
AM
37915+ if (bwr >= 0) {
37916+ file = ERR_PTR(-ENOMEM);
537831f9 37917+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
37918+ if (unlikely(!page))
37919+ goto out;
86dc4139 37920+ path.mnt = au_br_mnt(br);
7f207e10
AM
37921+ path.dentry = au_h_dptr(sb->s_root, bwr);
37922+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
37923+ file = (void *)p;
37924+ if (!IS_ERR(p)) {
37925+ strcat(p, "/" AUFS_XINO_FNAME);
37926+ AuDbg("%s\n", p);
37927+ file = au_xino_create(sb, p, /*silent*/0);
37928+ if (!IS_ERR(file))
37929+ au_xino_brid_set(sb, br->br_id);
37930+ }
1c60b727 37931+ free_page((unsigned long)page);
7f207e10
AM
37932+ } else {
37933+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
37934+ if (IS_ERR(file))
37935+ goto out;
2000de60 37936+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
37937+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
37938+ pr_err("xino doesn't support %s(%s)\n",
37939+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
37940+ fput(file);
37941+ file = ERR_PTR(-EINVAL);
37942+ }
37943+ if (!IS_ERR(file))
37944+ au_xino_brid_set(sb, -1);
37945+ }
0c5527e5 37946+
7f207e10
AM
37947+out:
37948+ return file;
37949+}
37950+
37951+/* ---------------------------------------------------------------------- */
37952+
062440b3
AM
37953+/*
37954+ * initialize the xinofile for the specified branch @br
37955+ * at the place/path where @base_file indicates.
37956+ * test whether another branch is on the same filesystem or not,
37957+ * if found then share the xinofile with another branch.
37958+ */
37959+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
37960+ struct path *base)
7f207e10
AM
37961+{
37962+ int err;
062440b3
AM
37963+ struct au_xino_do_set_br args = {
37964+ .h_ino = h_ino,
37965+ .br = br
37966+ };
7f207e10 37967+
062440b3
AM
37968+ args.writef = au_sbi(sb)->si_xwrite;
37969+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
37970+ au_br_sb(br));
37971+ err = au_xino_do_set_br(sb, base, &args);
79b8bda9 37972+ if (unlikely(err))
062440b3 37973+ au_xino_put(br);
7f207e10 37974+
7f207e10
AM
37975+ return err;
37976+}
521ced18
JR
37977+
37978+/* ---------------------------------------------------------------------- */
37979+
062440b3
AM
37980+/*
37981+ * get an unused inode number from bitmap
37982+ */
37983+ino_t au_xino_new_ino(struct super_block *sb)
37984+{
37985+ ino_t ino;
37986+ unsigned long *p, pindex, ul, pend;
37987+ struct au_sbinfo *sbinfo;
37988+ struct file *file;
37989+ int free_bit, err;
37990+
37991+ if (!au_opt_test(au_mntflags(sb), XINO))
37992+ return iunique(sb, AUFS_FIRST_INO);
37993+
37994+ sbinfo = au_sbi(sb);
37995+ mutex_lock(&sbinfo->si_xib_mtx);
37996+ p = sbinfo->si_xib_buf;
37997+ free_bit = sbinfo->si_xib_next_bit;
37998+ if (free_bit < page_bits && !test_bit(free_bit, p))
37999+ goto out; /* success */
38000+ free_bit = find_first_zero_bit(p, page_bits);
38001+ if (free_bit < page_bits)
38002+ goto out; /* success */
38003+
38004+ pindex = sbinfo->si_xib_last_pindex;
38005+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38006+ err = xib_pindex(sb, ul);
38007+ if (unlikely(err))
38008+ goto out_err;
38009+ free_bit = find_first_zero_bit(p, page_bits);
38010+ if (free_bit < page_bits)
38011+ goto out; /* success */
38012+ }
38013+
38014+ file = sbinfo->si_xib;
38015+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38016+ for (ul = pindex + 1; ul <= pend; ul++) {
38017+ err = xib_pindex(sb, ul);
38018+ if (unlikely(err))
38019+ goto out_err;
38020+ free_bit = find_first_zero_bit(p, page_bits);
38021+ if (free_bit < page_bits)
38022+ goto out; /* success */
38023+ }
38024+ BUG();
38025+
38026+out:
38027+ set_bit(free_bit, p);
38028+ sbinfo->si_xib_next_bit = free_bit + 1;
38029+ pindex = sbinfo->si_xib_last_pindex;
38030+ mutex_unlock(&sbinfo->si_xib_mtx);
38031+ ino = xib_calc_ino(pindex, free_bit);
38032+ AuDbg("i%lu\n", (unsigned long)ino);
38033+ return ino;
38034+out_err:
38035+ mutex_unlock(&sbinfo->si_xib_mtx);
38036+ AuDbg("i0\n");
38037+ return 0;
38038+}
38039+
38040+/* for s_op->delete_inode() */
38041+void au_xino_delete_inode(struct inode *inode, const int unlinked)
521ced18 38042+{
062440b3
AM
38043+ int err;
38044+ unsigned int mnt_flags;
38045+ aufs_bindex_t bindex, bbot, bi;
38046+ unsigned char try_trunc;
38047+ struct au_iinfo *iinfo;
38048+ struct super_block *sb;
38049+ struct au_hinode *hi;
38050+ struct inode *h_inode;
38051+ struct au_branch *br;
38052+ vfs_writef_t xwrite;
521ced18 38053+
062440b3 38054+ AuDebugOn(au_is_bad_inode(inode));
521ced18 38055+
062440b3
AM
38056+ sb = inode->i_sb;
38057+ mnt_flags = au_mntflags(sb);
38058+ if (!au_opt_test(mnt_flags, XINO)
38059+ || inode->i_ino == AUFS_ROOT_INO)
38060+ return;
38061+
38062+ if (unlinked) {
38063+ au_xigen_inc(inode);
38064+ au_xib_clear_bit(inode);
38065+ }
38066+
38067+ iinfo = au_ii(inode);
38068+ bindex = iinfo->ii_btop;
38069+ if (bindex < 0)
38070+ return;
38071+
38072+ xwrite = au_sbi(sb)->si_xwrite;
38073+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38074+ hi = au_hinode(iinfo, bindex);
38075+ bbot = iinfo->ii_bbot;
38076+ for (; bindex <= bbot; bindex++, hi++) {
38077+ h_inode = hi->hi_inode;
38078+ if (!h_inode
38079+ || (!unlinked && h_inode->i_nlink))
38080+ continue;
38081+
38082+ /* inode may not be revalidated */
38083+ bi = au_br_index(sb, hi->hi_id);
38084+ if (bi < 0)
38085+ continue;
38086+
38087+ br = au_sbr(sb, bi);
38088+ err = au_xino_do_write(xwrite, au_xino_file(br),
38089+ h_inode->i_ino, /*ino*/0);
38090+ if (!err && try_trunc
38091+ && au_test_fs_trunc_xino(au_br_sb(br)))
38092+ xino_try_trunc(sb, br);
38093+ }
521ced18
JR
38094+}
38095+
062440b3
AM
38096+/* ---------------------------------------------------------------------- */
38097+
38098+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
521ced18
JR
38099+{
38100+ int found, total, i;
38101+
38102+ found = -1;
062440b3 38103+ total = xi->xi_nondir.total;
521ced18 38104+ for (i = 0; i < total; i++) {
062440b3 38105+ if (xi->xi_nondir.array[i] != h_ino)
521ced18
JR
38106+ continue;
38107+ found = i;
38108+ break;
38109+ }
38110+
38111+ return found;
38112+}
38113+
062440b3 38114+static int au_xinondir_expand(struct au_xino *xi)
521ced18
JR
38115+{
38116+ int err, sz;
38117+ ino_t *p;
38118+
38119+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38120+
38121+ err = -ENOMEM;
062440b3 38122+ sz = xi->xi_nondir.total * sizeof(ino_t);
521ced18
JR
38123+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38124+ goto out;
062440b3 38125+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
521ced18
JR
38126+ /*may_shrink*/0);
38127+ if (p) {
062440b3
AM
38128+ xi->xi_nondir.array = p;
38129+ xi->xi_nondir.total <<= 1;
38130+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
521ced18
JR
38131+ err = 0;
38132+ }
38133+
38134+out:
38135+ return err;
38136+}
38137+
062440b3
AM
38138+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38139+ ino_t h_ino, int idx)
38140+{
38141+ struct au_xino *xi;
38142+
38143+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38144+ xi = au_sbr(sb, bindex)->br_xino;
38145+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38146+
38147+ spin_lock(&xi->xi_nondir.spin);
38148+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38149+ xi->xi_nondir.array[idx] = 0;
38150+ spin_unlock(&xi->xi_nondir.spin);
38151+ wake_up_all(&xi->xi_nondir.wqh);
38152+}
38153+
521ced18
JR
38154+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38155+ int *idx)
38156+{
38157+ int err, found, empty;
062440b3 38158+ struct au_xino *xi;
521ced18
JR
38159+
38160+ err = 0;
38161+ *idx = -1;
38162+ if (!au_opt_test(au_mntflags(sb), XINO))
38163+ goto out; /* no xino */
38164+
062440b3 38165+ xi = au_sbr(sb, bindex)->br_xino;
521ced18
JR
38166+
38167+again:
062440b3
AM
38168+ spin_lock(&xi->xi_nondir.spin);
38169+ found = au_xinondir_find(xi, h_ino);
521ced18 38170+ if (found == -1) {
062440b3 38171+ empty = au_xinondir_find(xi, /*h_ino*/0);
521ced18 38172+ if (empty == -1) {
062440b3
AM
38173+ empty = xi->xi_nondir.total;
38174+ err = au_xinondir_expand(xi);
521ced18
JR
38175+ if (unlikely(err))
38176+ goto out_unlock;
38177+ }
062440b3 38178+ xi->xi_nondir.array[empty] = h_ino;
521ced18
JR
38179+ *idx = empty;
38180+ } else {
062440b3
AM
38181+ spin_unlock(&xi->xi_nondir.spin);
38182+ wait_event(xi->xi_nondir.wqh,
38183+ xi->xi_nondir.array[found] != h_ino);
521ced18
JR
38184+ goto again;
38185+ }
38186+
38187+out_unlock:
062440b3
AM
38188+ spin_unlock(&xi->xi_nondir.spin);
38189+out:
38190+ return err;
38191+}
38192+
38193+/* ---------------------------------------------------------------------- */
38194+
38195+int au_xino_path(struct seq_file *seq, struct file *file)
38196+{
38197+ int err;
38198+
38199+ err = au_seq_path(seq, &file->f_path);
38200+ if (unlikely(err))
38201+ goto out;
38202+
38203+#define Deleted "\\040(deleted)"
38204+ seq->count -= sizeof(Deleted) - 1;
38205+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38206+ sizeof(Deleted) - 1));
38207+#undef Deleted
38208+
521ced18
JR
38209+out:
38210+ return err;
38211+}
537831f9
AM
38212diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38213--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
062440b3
AM
38214+++ linux/include/uapi/linux/aufs_type.h 2018-07-19 09:46:13.066648108 +0200
38215@@ -0,0 +1,448 @@
38216+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 38217+/*
b00004a5 38218+ * Copyright (C) 2005-2018 Junjiro R. Okajima
7f207e10
AM
38219+ *
38220+ * This program, aufs is free software; you can redistribute it and/or modify
38221+ * it under the terms of the GNU General Public License as published by
38222+ * the Free Software Foundation; either version 2 of the License, or
38223+ * (at your option) any later version.
38224+ *
38225+ * This program is distributed in the hope that it will be useful,
38226+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38227+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38228+ * GNU General Public License for more details.
38229+ *
38230+ * You should have received a copy of the GNU General Public License
523b37e3 38231+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
38232+ */
38233+
38234+#ifndef __AUFS_TYPE_H__
38235+#define __AUFS_TYPE_H__
38236+
f6c5ef8b
AM
38237+#define AUFS_NAME "aufs"
38238+
9dbd164d 38239+#ifdef __KERNEL__
f6c5ef8b
AM
38240+/*
38241+ * define it before including all other headers.
38242+ * sched.h may use pr_* macros before defining "current", so define the
38243+ * no-current version first, and re-define later.
38244+ */
38245+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38246+#include <linux/sched.h>
38247+#undef pr_fmt
a2a7ad62
AM
38248+#define pr_fmt(fmt) \
38249+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38250+ (int)sizeof(current->comm), current->comm, current->pid
9dbd164d
AM
38251+#else
38252+#include <stdint.h>
38253+#include <sys/types.h>
f6c5ef8b 38254+#endif /* __KERNEL__ */
7f207e10 38255+
f6c5ef8b
AM
38256+#include <linux/limits.h>
38257+
062440b3 38258+#define AUFS_VERSION "4.17-20180716"
7f207e10
AM
38259+
38260+/* todo? move this to linux-2.6.19/include/magic.h */
38261+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38262+
38263+/* ---------------------------------------------------------------------- */
38264+
38265+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 38266+typedef int8_t aufs_bindex_t;
7f207e10
AM
38267+#define AUFS_BRANCH_MAX 127
38268+#else
9dbd164d 38269+typedef int16_t aufs_bindex_t;
7f207e10
AM
38270+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38271+#define AUFS_BRANCH_MAX 511
38272+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38273+#define AUFS_BRANCH_MAX 1023
38274+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38275+#define AUFS_BRANCH_MAX 32767
38276+#endif
38277+#endif
38278+
38279+#ifdef __KERNEL__
38280+#ifndef AUFS_BRANCH_MAX
38281+#error unknown CONFIG_AUFS_BRANCH_MAX value
38282+#endif
38283+#endif /* __KERNEL__ */
38284+
38285+/* ---------------------------------------------------------------------- */
38286+
7f207e10
AM
38287+#define AUFS_FSTYPE AUFS_NAME
38288+
38289+#define AUFS_ROOT_INO 2
38290+#define AUFS_FIRST_INO 11
38291+
38292+#define AUFS_WH_PFX ".wh."
38293+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38294+#define AUFS_WH_TMP_LEN 4
86dc4139 38295+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
38296+#define AUFS_MAX_NAMELEN (NAME_MAX \
38297+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38298+ - 1 /* dot */\
38299+ - AUFS_WH_TMP_LEN) /* hex */
38300+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38301+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
38302+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38303+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
38304+#define AUFS_DIRWH_DEF 3
38305+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 38306+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
38307+#define AUFS_RDBLK_DEF 512 /* bytes */
38308+#define AUFS_RDHASH_DEF 32
38309+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
38310+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38311+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 38312+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 38313+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
38314+
38315+/* pseudo-link maintenace under /proc */
38316+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38317+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38318+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38319+
8b6a4947
AM
38320+/* dirren, renamed dir */
38321+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38322+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38323+/* whiteouted doubly */
38324+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38325+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38326+
7f207e10
AM
38327+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38328+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38329+
38330+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38331+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38332+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38333+
38334+/* doubly whiteouted */
38335+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38336+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38337+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38338+
1e00d052 38339+/* branch permissions and attributes */
7f207e10
AM
38340+#define AUFS_BRPERM_RW "rw"
38341+#define AUFS_BRPERM_RO "ro"
38342+#define AUFS_BRPERM_RR "rr"
076b876e
AM
38343+#define AUFS_BRATTR_COO_REG "coo_reg"
38344+#define AUFS_BRATTR_COO_ALL "coo_all"
38345+#define AUFS_BRATTR_FHSM "fhsm"
38346+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
38347+#define AUFS_BRATTR_ICEX "icex"
38348+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38349+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38350+#define AUFS_BRATTR_ICEX_TR "icextr"
38351+#define AUFS_BRATTR_ICEX_USR "icexusr"
38352+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
38353+#define AUFS_BRRATTR_WH "wh"
38354+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
38355+#define AUFS_BRWATTR_MOO "moo"
38356+
38357+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38358+#define AuBrPerm_RO (1 << 1) /* readonly */
38359+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38360+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38361+
38362+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38363+#define AuBrAttr_COO_ALL (1 << 4)
38364+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38365+
38366+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38367+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
38368+ branch. meaningless since
38369+ linux-3.18-rc1 */
38370+
38371+/* ignore error in copying XATTR */
38372+#define AuBrAttr_ICEX_SEC (1 << 7)
38373+#define AuBrAttr_ICEX_SYS (1 << 8)
38374+#define AuBrAttr_ICEX_TR (1 << 9)
38375+#define AuBrAttr_ICEX_USR (1 << 10)
38376+#define AuBrAttr_ICEX_OTH (1 << 11)
38377+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38378+ | AuBrAttr_ICEX_SYS \
38379+ | AuBrAttr_ICEX_TR \
38380+ | AuBrAttr_ICEX_USR \
38381+ | AuBrAttr_ICEX_OTH)
38382+
38383+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
38384+#define AuBrRAttr_Mask AuBrRAttr_WH
38385+
c1595e42
JR
38386+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
38387+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
38388+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38389+
38390+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38391+
c1595e42 38392+/* #warning test userspace */
076b876e
AM
38393+#ifdef __KERNEL__
38394+#ifndef CONFIG_AUFS_FHSM
38395+#undef AuBrAttr_FHSM
38396+#define AuBrAttr_FHSM 0
38397+#endif
c1595e42
JR
38398+#ifndef CONFIG_AUFS_XATTR
38399+#undef AuBrAttr_ICEX
38400+#define AuBrAttr_ICEX 0
38401+#undef AuBrAttr_ICEX_SEC
38402+#define AuBrAttr_ICEX_SEC 0
38403+#undef AuBrAttr_ICEX_SYS
38404+#define AuBrAttr_ICEX_SYS 0
38405+#undef AuBrAttr_ICEX_TR
38406+#define AuBrAttr_ICEX_TR 0
38407+#undef AuBrAttr_ICEX_USR
38408+#define AuBrAttr_ICEX_USR 0
38409+#undef AuBrAttr_ICEX_OTH
38410+#define AuBrAttr_ICEX_OTH 0
38411+#endif
076b876e
AM
38412+#endif
38413+
38414+/* the longest combination */
c1595e42
JR
38415+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38416+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
38417+ "+" AUFS_BRATTR_COO_REG \
38418+ "+" AUFS_BRATTR_FHSM \
38419+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
38420+ "+" AUFS_BRATTR_ICEX_SEC \
38421+ "+" AUFS_BRATTR_ICEX_SYS \
38422+ "+" AUFS_BRATTR_ICEX_USR \
38423+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
38424+ "+" AUFS_BRWATTR_NLWH)
38425+
38426+typedef struct {
38427+ char a[AuBrPermStrSz];
38428+} au_br_perm_str_t;
38429+
38430+static inline int au_br_writable(int brperm)
38431+{
38432+ return brperm & AuBrPerm_RW;
38433+}
38434+
38435+static inline int au_br_whable(int brperm)
38436+{
38437+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38438+}
38439+
38440+static inline int au_br_wh_linkable(int brperm)
38441+{
38442+ return !(brperm & AuBrWAttr_NoLinkWH);
38443+}
38444+
38445+static inline int au_br_cmoo(int brperm)
38446+{
38447+ return brperm & AuBrAttr_CMOO_Mask;
38448+}
38449+
38450+static inline int au_br_fhsm(int brperm)
38451+{
38452+ return brperm & AuBrAttr_FHSM;
38453+}
7f207e10
AM
38454+
38455+/* ---------------------------------------------------------------------- */
38456+
38457+/* ioctl */
38458+enum {
38459+ /* readdir in userspace */
38460+ AuCtl_RDU,
38461+ AuCtl_RDU_INO,
38462+
076b876e
AM
38463+ AuCtl_WBR_FD, /* pathconf wrapper */
38464+ AuCtl_IBUSY, /* busy inode */
38465+ AuCtl_MVDOWN, /* move-down */
38466+ AuCtl_BR, /* info about branches */
38467+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
38468+};
38469+
38470+/* borrowed from linux/include/linux/kernel.h */
38471+#ifndef ALIGN
38472+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
38473+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
38474+#endif
38475+
38476+/* borrowed from linux/include/linux/compiler-gcc3.h */
38477+#ifndef __aligned
38478+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
38479+#endif
38480+
38481+#ifdef __KERNEL__
38482+#ifndef __packed
7f207e10
AM
38483+#define __packed __attribute__((packed))
38484+#endif
53392da6 38485+#endif
7f207e10
AM
38486+
38487+struct au_rdu_cookie {
9dbd164d
AM
38488+ uint64_t h_pos;
38489+ int16_t bindex;
38490+ uint8_t flags;
38491+ uint8_t pad;
38492+ uint32_t generation;
7f207e10
AM
38493+} __aligned(8);
38494+
38495+struct au_rdu_ent {
9dbd164d
AM
38496+ uint64_t ino;
38497+ int16_t bindex;
38498+ uint8_t type;
38499+ uint8_t nlen;
38500+ uint8_t wh;
7f207e10
AM
38501+ char name[0];
38502+} __aligned(8);
38503+
38504+static inline int au_rdu_len(int nlen)
38505+{
38506+ /* include the terminating NULL */
38507+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 38508+ sizeof(uint64_t));
7f207e10
AM
38509+}
38510+
38511+union au_rdu_ent_ul {
38512+ struct au_rdu_ent __user *e;
9dbd164d 38513+ uint64_t ul;
7f207e10
AM
38514+};
38515+
38516+enum {
38517+ AufsCtlRduV_SZ,
38518+ AufsCtlRduV_End
38519+};
38520+
38521+struct aufs_rdu {
38522+ /* input */
38523+ union {
9dbd164d
AM
38524+ uint64_t sz; /* AuCtl_RDU */
38525+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
38526+ };
38527+ union au_rdu_ent_ul ent;
9dbd164d 38528+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
38529+
38530+ /* input/output */
9dbd164d 38531+ uint32_t blk;
7f207e10
AM
38532+
38533+ /* output */
38534+ union au_rdu_ent_ul tail;
38535+ /* number of entries which were added in a single call */
9dbd164d
AM
38536+ uint64_t rent;
38537+ uint8_t full;
38538+ uint8_t shwh;
7f207e10
AM
38539+
38540+ struct au_rdu_cookie cookie;
38541+} __aligned(8);
38542+
1e00d052
AM
38543+/* ---------------------------------------------------------------------- */
38544+
8b6a4947
AM
38545+/* dirren. the branch is identified by the filename who contains this */
38546+struct au_drinfo {
38547+ uint64_t ino;
38548+ union {
38549+ uint8_t oldnamelen;
38550+ uint64_t _padding;
38551+ };
38552+ uint8_t oldname[0];
38553+} __aligned(8);
38554+
38555+struct au_drinfo_fdata {
38556+ uint32_t magic;
38557+ struct au_drinfo drinfo;
38558+} __aligned(8);
38559+
38560+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
38561+/* future */
38562+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
38563+
38564+/* ---------------------------------------------------------------------- */
38565+
1e00d052 38566+struct aufs_wbr_fd {
9dbd164d
AM
38567+ uint32_t oflags;
38568+ int16_t brid;
1e00d052
AM
38569+} __aligned(8);
38570+
38571+/* ---------------------------------------------------------------------- */
38572+
027c5e7a 38573+struct aufs_ibusy {
9dbd164d
AM
38574+ uint64_t ino, h_ino;
38575+ int16_t bindex;
027c5e7a
AM
38576+} __aligned(8);
38577+
1e00d052
AM
38578+/* ---------------------------------------------------------------------- */
38579+
392086de
AM
38580+/* error code for move-down */
38581+/* the actual message strings are implemented in aufs-util.git */
38582+enum {
38583+ EAU_MVDOWN_OPAQUE = 1,
38584+ EAU_MVDOWN_WHITEOUT,
38585+ EAU_MVDOWN_UPPER,
38586+ EAU_MVDOWN_BOTTOM,
38587+ EAU_MVDOWN_NOUPPER,
38588+ EAU_MVDOWN_NOLOWERBR,
38589+ EAU_Last
38590+};
38591+
c2b27bf2 38592+/* flags for move-down */
392086de
AM
38593+#define AUFS_MVDOWN_DMSG 1
38594+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
38595+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
38596+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
38597+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
38598+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
38599+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
38600+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
38601+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
38602+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
38603+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
38604+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
38605+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 38606+
076b876e 38607+/* index for move-down */
392086de
AM
38608+enum {
38609+ AUFS_MVDOWN_UPPER,
38610+ AUFS_MVDOWN_LOWER,
38611+ AUFS_MVDOWN_NARRAY
38612+};
38613+
076b876e
AM
38614+/*
38615+ * additional info of move-down
38616+ * number of free blocks and inodes.
38617+ * subset of struct kstatfs, but smaller and always 64bit.
38618+ */
38619+struct aufs_stfs {
38620+ uint64_t f_blocks;
38621+ uint64_t f_bavail;
38622+ uint64_t f_files;
38623+ uint64_t f_ffree;
38624+};
38625+
38626+struct aufs_stbr {
38627+ int16_t brid; /* optional input */
38628+ int16_t bindex; /* output */
38629+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
38630+} __aligned(8);
38631+
c2b27bf2 38632+struct aufs_mvdown {
076b876e
AM
38633+ uint32_t flags; /* input/output */
38634+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
38635+ int8_t au_errno; /* output */
38636+} __aligned(8);
38637+
38638+/* ---------------------------------------------------------------------- */
38639+
38640+union aufs_brinfo {
38641+ /* PATH_MAX may differ between kernel-space and user-space */
38642+ char _spacer[4096];
392086de 38643+ struct {
076b876e
AM
38644+ int16_t id;
38645+ int perm;
38646+ char path[0];
38647+ };
c2b27bf2
AM
38648+} __aligned(8);
38649+
38650+/* ---------------------------------------------------------------------- */
38651+
7f207e10
AM
38652+#define AuCtlType 'A'
38653+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
38654+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
38655+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
38656+ struct aufs_wbr_fd)
027c5e7a 38657+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
38658+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
38659+ struct aufs_mvdown)
076b876e
AM
38660+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
38661+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
38662+
38663+#endif /* __AUFS_TYPE_H__ */
2121bcd9 38664SPDX-License-Identifier: GPL-2.0
c4adf169 38665aufs4.17 loopback patch
5527c038
JR
38666
38667diff --git a/drivers/block/loop.c b/drivers/block/loop.c
c4adf169 38668index bc965e5..852868a 100644
5527c038
JR
38669--- a/drivers/block/loop.c
38670+++ b/drivers/block/loop.c
c4adf169 38671@@ -622,6 +622,15 @@ static inline void loop_update_dio(struct loop_device *lo)
8b6a4947 38672 lo->use_dio);
5527c038
JR
38673 }
38674
5527c038
JR
38675+static struct file *loop_real_file(struct file *file)
38676+{
38677+ struct file *f = NULL;
38678+
38679+ if (file->f_path.dentry->d_sb->s_op->real_loop)
38680+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
38681+ return f;
8b6a4947
AM
38682+}
38683+
c2c0f25c 38684 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
38685 struct block_device *bdev)
38686 {
c4adf169 38687@@ -656,6 +665,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
38688 unsigned int arg)
38689 {
38690 struct file *file, *old_file;
38691+ struct file *f, *virt_file = NULL, *old_virt_file;
38692 struct inode *inode;
38693 int error;
38694
c4adf169 38695@@ -672,9 +682,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
062440b3
AM
38696 error = loop_validate_file(file, bdev);
38697 if (error)
38698 goto out_putf;
5527c038
JR
38699+ f = loop_real_file(file);
38700+ if (f) {
38701+ virt_file = file;
38702+ file = f;
38703+ get_file(file);
38704+ }
38705
38706 inode = file->f_mapping->host;
38707 old_file = lo->lo_backing_file;
38708+ old_virt_file = lo->lo_backing_virt_file;
38709
38710 error = -EINVAL;
38711
c4adf169 38712@@ -689,6 +706,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
38713 blk_mq_freeze_queue(lo->lo_queue);
38714 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
38715 lo->lo_backing_file = file;
38716+ lo->lo_backing_virt_file = virt_file;
38717 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
38718 mapping_set_gfp_mask(file->f_mapping,
38719 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
c4adf169 38720@@ -696,12 +714,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947 38721 blk_mq_unfreeze_queue(lo->lo_queue);
5527c038
JR
38722
38723 fput(old_file);
38724+ if (old_virt_file)
38725+ fput(old_virt_file);
38726 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
c2c0f25c 38727 loop_reread_partitions(lo, bdev);
5527c038
JR
38728 return 0;
38729
38730 out_putf:
38731 fput(file);
38732+ if (virt_file)
38733+ fput(virt_file);
38734 out:
38735 return error;
38736 }
c4adf169 38737@@ -895,7 +917,7 @@ static int loop_prepare_queue(struct loop_device *lo)
5527c038
JR
38738 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38739 struct block_device *bdev, unsigned int arg)
38740 {
062440b3 38741- struct file *file;
5527c038
JR
38742+ struct file *file, *f, *virt_file = NULL;
38743 struct inode *inode;
38744 struct address_space *mapping;
8b6a4947 38745 int lo_flags = 0;
c4adf169 38746@@ -909,6 +931,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38747 file = fget(arg);
38748 if (!file)
38749 goto out;
38750+ f = loop_real_file(file);
38751+ if (f) {
38752+ virt_file = file;
38753+ file = f;
38754+ get_file(file);
38755+ }
38756
38757 error = -EBUSY;
38758 if (lo->lo_state != Lo_unbound)
c4adf169 38759@@ -957,6 +985,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38760 lo->lo_device = bdev;
38761 lo->lo_flags = lo_flags;
38762 lo->lo_backing_file = file;
38763+ lo->lo_backing_virt_file = virt_file;
38764 lo->transfer = NULL;
38765 lo->ioctl = NULL;
38766 lo->lo_sizelimit = 0;
c4adf169 38767@@ -990,6 +1019,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
38768
38769 out_putf:
38770 fput(file);
38771+ if (virt_file)
38772+ fput(virt_file);
38773 out:
38774 /* This is safe: open() is still holding a reference. */
38775 module_put(THIS_MODULE);
c4adf169 38776@@ -1036,6 +1067,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
5527c038
JR
38777 static int loop_clr_fd(struct loop_device *lo)
38778 {
38779 struct file *filp = lo->lo_backing_file;
38780+ struct file *virt_filp = lo->lo_backing_virt_file;
38781 gfp_t gfp = lo->old_gfp_mask;
38782 struct block_device *bdev = lo->lo_device;
38783
c4adf169 38784@@ -1067,6 +1099,7 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
38785 spin_lock_irq(&lo->lo_lock);
38786 lo->lo_state = Lo_rundown;
38787 lo->lo_backing_file = NULL;
38788+ lo->lo_backing_virt_file = NULL;
38789 spin_unlock_irq(&lo->lo_lock);
38790
38791 loop_release_xfer(lo);
c4adf169 38792@@ -1115,6 +1148,8 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
38793 * bd_mutex which is usually taken before lo_ctl_mutex.
38794 */
38795 fput(filp);
38796+ if (virt_filp)
38797+ fput(virt_filp);
38798 return 0;
38799 }
38800
38801diff --git a/drivers/block/loop.h b/drivers/block/loop.h
c4adf169 38802index b78de98..2bbbd92 100644
5527c038
JR
38803--- a/drivers/block/loop.h
38804+++ b/drivers/block/loop.h
38805@@ -46,7 +46,7 @@ struct loop_device {
38806 int (*ioctl)(struct loop_device *, int cmd,
38807 unsigned long arg);
38808
38809- struct file * lo_backing_file;
38810+ struct file * lo_backing_file, *lo_backing_virt_file;
38811 struct block_device *lo_device;
5527c038 38812 void *key_data;
8b6a4947 38813
5527c038 38814diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
062440b3 38815index 7ca7f67..3d4e90d 100644
5527c038
JR
38816--- a/fs/aufs/f_op.c
38817+++ b/fs/aufs/f_op.c
2121bcd9 38818@@ -358,7 +358,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
38819 if (IS_ERR(h_file))
38820 goto out;
38821
38822- if (au_test_loopback_kthread()) {
38823+ if (0 && au_test_loopback_kthread()) {
38824 au_warn_loopback(h_file->f_path.dentry->d_sb);
38825 if (file->f_mapping != h_file->f_mapping) {
38826 file->f_mapping = h_file->f_mapping;
38827diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
062440b3 38828index 2d65b8f..5ed7903 100644
5527c038
JR
38829--- a/fs/aufs/loop.c
38830+++ b/fs/aufs/loop.c
2121bcd9 38831@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 38832 symbol_put(loop_backing_file);
1c60b727 38833 kfree(au_warn_loopback_array);
5527c038
JR
38834 }
38835+
38836+/* ---------------------------------------------------------------------- */
38837+
38838+/* support the loopback block device insude aufs */
38839+
38840+struct file *aufs_real_loop(struct file *file)
38841+{
38842+ struct file *f;
38843+
38844+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
38845+ fi_read_lock(file);
38846+ f = au_hf_top(file);
38847+ fi_read_unlock(file);
38848+ AuDebugOn(!f);
38849+ return f;
38850+}
38851diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
062440b3 38852index 05d703d..6bb23c8 100644
5527c038
JR
38853--- a/fs/aufs/loop.h
38854+++ b/fs/aufs/loop.h
2121bcd9 38855@@ -26,7 +26,11 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
38856
38857 int au_loopback_init(void);
38858 void au_loopback_fin(void);
38859+
38860+struct file *aufs_real_loop(struct file *file);
38861 #else
38862+AuStub(struct file *, loop_backing_file, return NULL)
38863+
38864 AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
38865 struct dentry *h_adding)
38866 AuStubInt0(au_test_loopback_kthread, void)
2121bcd9 38867@@ -34,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
38868
38869 AuStubInt0(au_loopback_init, void)
38870 AuStubVoid(au_loopback_fin, void)
38871+
38872+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
38873 #endif /* BLK_DEV_LOOP */
38874
38875 #endif /* __KERNEL__ */
38876diff --git a/fs/aufs/super.c b/fs/aufs/super.c
062440b3 38877index eed37ba..a3a5cd7 100644
5527c038
JR
38878--- a/fs/aufs/super.c
38879+++ b/fs/aufs/super.c
062440b3 38880@@ -840,7 +840,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
38881 .statfs = aufs_statfs,
38882 .put_super = aufs_put_super,
38883 .sync_fs = aufs_sync_fs,
38884- .remount_fs = aufs_remount_fs
38885+ .remount_fs = aufs_remount_fs,
38886+#ifdef CONFIG_AUFS_BDEV_LOOP
38887+ .real_loop = aufs_real_loop
38888+#endif
38889 };
38890
38891 /* ---------------------------------------------------------------------- */
38892diff --git a/include/linux/fs.h b/include/linux/fs.h
b00004a5 38893index 09a2542..11a6346 100644
5527c038
JR
38894--- a/include/linux/fs.h
38895+++ b/include/linux/fs.h
b00004a5 38896@@ -1852,6 +1852,10 @@ struct super_operations {
5527c038
JR
38897 struct shrink_control *);
38898 long (*free_cached_objects)(struct super_block *,
38899 struct shrink_control *);
38900+#if defined(CONFIG_BLK_DEV_LOOP) || defined(CONFIG_BLK_DEV_LOOP_MODULE)
38901+ /* and aufs */
38902+ struct file *(*real_loop)(struct file *);
38903+#endif
38904 };
38905
38906 /*
This page took 7.714243 seconds and 4 git commands to generate.