]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs5.patch
- 5.4.228
[packages/kernel.git] / kernel-aufs5.patch
CommitLineData
2121bcd9 1SPDX-License-Identifier: GPL-2.0
43982f53 2aufs5.4 kbuild patch
7f207e10 3
fbc438ed 4diff --git a/fs/Kconfig b/fs/Kconfig
43982f53 5index 2501e6f1f965..38a6a5991da9 100644
fbc438ed
JR
6--- a/fs/Kconfig
7+++ b/fs/Kconfig
43982f53 8@@ -264,6 +264,7 @@ source "fs/pstore/Kconfig"
5527c038 9 source "fs/sysv/Kconfig"
7e9cd9fe 10 source "fs/ufs/Kconfig"
9d41a5bf 11 source "fs/erofs/Kconfig"
7f207e10
AM
12+source "fs/aufs/Kconfig"
13
14 endif # MISC_FILESYSTEMS
15
16diff --git a/fs/Makefile b/fs/Makefile
43982f53 17index 14231b4cf383..8765e191ebe0 100644
7f207e10
AM
18--- a/fs/Makefile
19+++ b/fs/Makefile
9d41a5bf 20@@ -132,3 +132,4 @@ obj-$(CONFIG_CEPH_FS) += ceph/
bf0370f2 21 obj-$(CONFIG_PSTORE) += pstore/
c06a8ce3 22 obj-$(CONFIG_EFIVAR_FS) += efivarfs/
9d41a5bf 23 obj-$(CONFIG_EROFS_FS) += erofs/
86dc4139 24+obj-$(CONFIG_AUFS_FS) += aufs/
2121bcd9 25SPDX-License-Identifier: GPL-2.0
43982f53 26aufs5.4 base patch
7f207e10 27
c1595e42 28diff --git a/MAINTAINERS b/MAINTAINERS
43982f53 29index 9d3a5c54a41d..f33398b47c42 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
43982f53 32@@ -2832,6 +2832,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>
c1595e42 38+L: aufs-users@lists.sourceforge.net (members only)
eca801bf 39+L: linux-unionfs@vger.kernel.org
c1595e42 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>
cd7a4cd9 51 S: Maintained
392086de 52diff --git a/drivers/block/loop.c b/drivers/block/loop.c
43982f53 53index f6f77eaa7217..5e094699215e 100644
392086de
AM
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
eca34b5c 56@@ -738,6 +738,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
cd7a4cd9 57 return error;
392086de
AM
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
eca34b5c 82index e88cf0554e65..7ce4ccf5a51c 100644
c1595e42
JR
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
eca34b5c 85@@ -1264,7 +1264,7 @@ enum d_walk_ret {
c1595e42 86 *
cd7a4cd9 87 * The @enter() callbacks are called with d_lock held.
c1595e42
JR
88 */
89-static void d_walk(struct dentry *parent, void *data,
90+void d_walk(struct dentry *parent, void *data,
cd7a4cd9 91 enum d_walk_ret (*enter)(void *, struct dentry *))
c1595e42 92 {
cd7a4cd9 93 struct dentry *this_parent;
febd17d6 94diff --git a/fs/fcntl.c b/fs/fcntl.c
43982f53 95index 3d40771e8e7c..12dd73930961 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)
43982f53 103+int setfl(int fd, struct file *filp, unsigned long arg)
febd17d6
JR
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
43982f53 117index fef457a42882..aaeacde398ee 100644
5afbbe0d
AM
118--- a/fs/inode.c
119+++ b/fs/inode.c
43982f53 120@@ -1673,7 +1673,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 */
cd7a4cd9
AM
124-static int update_time(struct inode *inode, struct timespec64 *time, int flags)
125+int update_time(struct inode *inode, struct timespec64 *time, int flags)
5afbbe0d 126 {
cd7a4cd9 127 int (*update_time)(struct inode *, struct timespec64 *, int);
5afbbe0d 128
8b6a4947 129diff --git a/fs/namespace.c b/fs/namespace.c
43982f53 130index 2adfe7b166a3..0f7e57e5f4b7 100644
8b6a4947
AM
131--- a/fs/namespace.c
132+++ b/fs/namespace.c
eca34b5c 133@@ -776,6 +776,12 @@ static inline int check_mnt(struct mount *mnt)
8b6a4947
AM
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
43982f53 147index 5bbf587f5bc1..fa9b3994b34c 100644
5527c038
JR
148--- a/fs/read_write.c
149+++ b/fs/read_write.c
fbc438ed 150@@ -498,6 +498,28 @@ static ssize_t __vfs_write(struct file *file, const char __user *p,
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;
43982f53 162+ return ERR_PTR(-ENOSYS); /* doesn't have ->read(|_iter)() op */
5527c038
JR
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;
43982f53 173+ return ERR_PTR(-ENOSYS); /* doesn't have ->write(|_iter)() op */
5527c038
JR
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
eca34b5c 180index 98412721f056..75b489fcb66f 100644
7f207e10
AM
181--- a/fs/splice.c
182+++ b/fs/splice.c
fbc438ed 183@@ -834,8 +834,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);
fbc438ed 194@@ -851,9 +851,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
fbc438ed 208index 4d1ff010bc5a..457f4e4a5cc1 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);
5527c038 220diff --git a/include/linux/fs.h b/include/linux/fs.h
43982f53 221index e0d909d35763..381a13995011 100644
5527c038
JR
222--- a/include/linux/fs.h
223+++ b/include/linux/fs.h
43982f53 224@@ -1349,6 +1349,7 @@ extern void fasync_free(struct fasync_struct *);
febd17d6
JR
225 /* can be called from interrupts */
226 extern void kill_fasync(struct fasync_struct **, int, int);
227
43982f53 228+extern int setfl(int fd, struct file *filp, unsigned long arg);
febd17d6 229 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
3c1bdaff 230 extern int f_setown(struct file *filp, unsigned long arg, int force);
febd17d6 231 extern void f_delown(struct file *filp);
43982f53 232@@ -1835,6 +1836,7 @@ struct file_operations {
febd17d6
JR
233 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
234 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
235 int (*check_flags)(int);
236+ int (*setfl)(struct file *, unsigned long);
237 int (*flock) (struct file *, int, struct file_lock *);
238 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
239 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
43982f53 240@@ -1905,6 +1907,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
5527c038
JR
241 struct iovec *fast_pointer,
242 struct iovec **ret_pointer);
243
244+typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
245+typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
246+ loff_t *);
247+vfs_readf_t vfs_readf(struct file *file);
248+vfs_writef_t vfs_writef(struct file *file);
249+
250 extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
5527c038 251 extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
8b6a4947 252 extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
43982f53 253@@ -2325,6 +2333,7 @@ extern int current_umask(void);
5afbbe0d
AM
254 extern void ihold(struct inode * inode);
255 extern void iput(struct inode *);
cd7a4cd9
AM
256 extern int generic_update_time(struct inode *, struct timespec64 *, int);
257+extern int update_time(struct inode *, struct timespec64 *, int);
5afbbe0d
AM
258
259 /* /sys/fs */
260 extern struct kobject *fs_kobj;
43982f53 261@@ -2613,6 +2622,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
a2654f78
AM
262 return false;
263 }
264 #endif
265+extern int __sync_filesystem(struct super_block *, int);
266 extern int sync_filesystem(struct super_block *);
267 extern const struct file_operations def_blk_fops;
268 extern const struct file_operations def_chr_fops;
8b6a4947 269diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
43982f53 270index b8a835fd611b..f452521f2e05 100644
8b6a4947
AM
271--- a/include/linux/lockdep.h
272+++ b/include/linux/lockdep.h
43982f53 273@@ -331,6 +331,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
8b6a4947
AM
274 return lock->key == key;
275 }
276
277+struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
278+
279 /*
280 * Acquire a lock.
281 *
43982f53 282@@ -473,6 +475,7 @@ struct lockdep_map { };
8b6a4947
AM
283
284 #define lockdep_depth(tsk) (0)
285
286+#define lockdep_is_held(lock) (1)
287 #define lockdep_is_held_type(l, r) (1)
288
289 #define lockdep_assert_held(l) do { (void)(l); } while (0)
290diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
ba1aed25 291index 35942084cd40..24f5fd1a789d 100644
8b6a4947
AM
292--- a/include/linux/mnt_namespace.h
293+++ b/include/linux/mnt_namespace.h
2121bcd9 294@@ -6,11 +6,14 @@
8b6a4947
AM
295 struct mnt_namespace;
296 struct fs_struct;
297 struct user_namespace;
298+struct vfsmount;
299
300 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
301 struct user_namespace *, struct fs_struct *);
302 extern void put_mnt_ns(struct mnt_namespace *ns);
303
304+extern int is_current_mnt_ns(struct vfsmount *mnt);
305+
306 extern const struct file_operations proc_mounts_operations;
307 extern const struct file_operations proc_mountinfo_operations;
308 extern const struct file_operations proc_mountstats_operations;
1e00d052 309diff --git a/include/linux/splice.h b/include/linux/splice.h
ba1aed25 310index 74b4911ac16d..19789fbea567 100644
1e00d052
AM
311--- a/include/linux/splice.h
312+++ b/include/linux/splice.h
2121bcd9 313@@ -87,4 +87,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
4b3da204
AM
314
315 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
106341ce 316 extern const struct pipe_buf_operations default_pipe_buf_ops;
1e00d052
AM
317+
318+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
319+ loff_t *ppos, size_t len, unsigned int flags);
320+extern long do_splice_to(struct file *in, loff_t *ppos,
321+ struct pipe_inode_info *pipe, size_t len,
322+ unsigned int flags);
323 #endif
8b6a4947 324diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
43982f53 325index 233459c03b5a..407c08ac8ac8 100644
8b6a4947
AM
326--- a/kernel/locking/lockdep.c
327+++ b/kernel/locking/lockdep.c
eca34b5c 328@@ -153,7 +153,7 @@ static
9f237c51 329 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
eca34b5c 330 static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
8b6a4947
AM
331
332-static inline struct lock_class *hlock_class(struct held_lock *hlock)
333+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
334 {
eca34b5c
AM
335 unsigned int class_idx = hlock->class_idx;
336
337@@ -174,6 +174,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
338 */
339 return lock_classes + class_idx;
8b6a4947
AM
340 }
341+#define hlock_class(hlock) lockdep_hlock_class(hlock)
342
343 #ifdef CONFIG_LOCK_STAT
344 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
2121bcd9 345SPDX-License-Identifier: GPL-2.0
43982f53 346aufs5.4 mmap patch
fb47a38f 347
c1595e42 348diff --git a/fs/proc/base.c b/fs/proc/base.c
eca34b5c 349index ebea9501afb8..dc7edc5f7267 100644
c1595e42
JR
350--- a/fs/proc/base.c
351+++ b/fs/proc/base.c
eca34b5c
AM
352@@ -2037,7 +2037,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
353 rc = -ENOENT;
c1595e42
JR
354 vma = find_exact_vma(mm, vm_start, vm_end);
355 if (vma && vma->vm_file) {
356- *path = vma->vm_file->f_path;
357+ *path = vma_pr_or_file(vma)->f_path;
358 path_get(path);
359 rc = 0;
360 }
fb47a38f 361diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
fbc438ed 362index 14c2badb8fd9..65afe5287e43 100644
fb47a38f
JR
363--- a/fs/proc/nommu.c
364+++ b/fs/proc/nommu.c
fbc438ed 365@@ -41,7 +41,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
fb47a38f
JR
366 file = region->vm_file;
367
368 if (file) {
369- struct inode *inode = file_inode(region->vm_file);
370+ struct inode *inode;
076b876e 371+
fb47a38f
JR
372+ file = vmr_pr_or_file(region);
373+ inode = file_inode(file);
374 dev = inode->i_sb->s_dev;
375 ino = inode->i_ino;
376 }
377diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
43982f53 378index 9442631fd4af..1fa8a5fcdeee 100644
fb47a38f
JR
379--- a/fs/proc/task_mmu.c
380+++ b/fs/proc/task_mmu.c
eca34b5c 381@@ -309,7 +309,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
fb47a38f
JR
382 const char *name = NULL;
383
384 if (file) {
385- struct inode *inode = file_inode(vma->vm_file);
386+ struct inode *inode;
076b876e 387+
fb47a38f
JR
388+ file = vma_pr_or_file(vma);
389+ inode = file_inode(file);
390 dev = inode->i_sb->s_dev;
391 ino = inode->i_ino;
392 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
43982f53 393@@ -1819,7 +1822,7 @@ static int show_numa_map(struct seq_file *m, void *v)
076b876e
AM
394 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
395 struct vm_area_struct *vma = v;
396 struct numa_maps *md = &numa_priv->md;
397- struct file *file = vma->vm_file;
398+ struct file *file = vma_pr_or_file(vma);
076b876e 399 struct mm_struct *mm = vma->vm_mm;
43982f53
AM
400 struct mempolicy *pol;
401 char buffer[64];
fb47a38f 402diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
eca34b5c 403index 7907e6419e57..d17209cf52bc 100644
fb47a38f
JR
404--- a/fs/proc/task_nommu.c
405+++ b/fs/proc/task_nommu.c
acd2b654 406@@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
fb47a38f
JR
407 file = vma->vm_file;
408
409 if (file) {
410- struct inode *inode = file_inode(vma->vm_file);
411+ struct inode *inode;
076b876e 412+
b912730e 413+ file = vma_pr_or_file(vma);
fb47a38f
JR
414+ inode = file_inode(file);
415 dev = inode->i_sb->s_dev;
416 ino = inode->i_ino;
417 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
418diff --git a/include/linux/mm.h b/include/linux/mm.h
43982f53 419index a2adf95b3f9c..70e1dccc1283 100644
fb47a38f
JR
420--- a/include/linux/mm.h
421+++ b/include/linux/mm.h
43982f53 422@@ -1510,6 +1510,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
a3232e84 423 unmap_mapping_range(mapping, holebegin, holelen, 0);
fb47a38f 424 }
fb47a38f 425
076b876e
AM
426+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
427+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
f2c43d5f 428+ int);
076b876e
AM
429+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
430+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
fb47a38f 431+
f2c43d5f
AM
432+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
433+ __LINE__)
434+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
435+ __LINE__)
436+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
437+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
b912730e
AM
438+
439+#ifndef CONFIG_MMU
076b876e
AM
440+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
441+extern void vmr_do_fput(struct vm_region *, const char[], int);
442+
f2c43d5f
AM
443+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
444+ __LINE__)
445+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
b912730e 446+#endif /* !CONFIG_MMU */
fb47a38f 447+
a3232e84
JR
448 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
449 void *buf, int len, unsigned int gup_flags);
fb47a38f 450 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
fb47a38f 451diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
43982f53 452index 270aa8fd2800..33848c2165e2 100644
fb47a38f
JR
453--- a/include/linux/mm_types.h
454+++ b/include/linux/mm_types.h
43982f53 455@@ -267,6 +267,7 @@ struct vm_region {
fb47a38f
JR
456 unsigned long vm_top; /* region allocated to here */
457 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
458 struct file *vm_file; /* the backing file or NULL */
459+ struct file *vm_prfile; /* the virtual backing file or NULL */
460
461 int vm_usage; /* region usage count (access under nommu_region_sem) */
462 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
43982f53 463@@ -341,6 +342,7 @@ struct vm_area_struct {
fb47a38f 464 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
5afbbe0d 465 units */
fb47a38f
JR
466 struct file * vm_file; /* File we map to (can be NULL). */
467+ struct file *vm_prfile; /* shadow of vm_file */
468 void * vm_private_data; /* was vm_pte (shared mem) */
469
eca34b5c 470 #ifdef CONFIG_SWAP
fb47a38f 471diff --git a/kernel/fork.c b/kernel/fork.c
43982f53 472index 13b38794efb5..ede7225bae95 100644
fb47a38f
JR
473--- a/kernel/fork.c
474+++ b/kernel/fork.c
43982f53 475@@ -562,7 +562,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
fb47a38f
JR
476 struct inode *inode = file_inode(file);
477 struct address_space *mapping = file->f_mapping;
478
479- get_file(file);
480+ vma_get_file(tmp);
481 if (tmp->vm_flags & VM_DENYWRITE)
482 atomic_dec(&inode->i_writecount);
2000de60 483 i_mmap_lock_write(mapping);
076b876e 484diff --git a/mm/Makefile b/mm/Makefile
43982f53 485index d996846697ef..fbadb91df4e4 100644
076b876e
AM
486--- a/mm/Makefile
487+++ b/mm/Makefile
43982f53 488@@ -42,7 +42,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
076b876e 489 mm_init.o mmu_context.o percpu.o slab_common.o \
b00004a5 490 compaction.o vmacache.o \
076b876e 491 interval_tree.o list_lru.o workingset.o \
eca34b5c
AM
492- debug.o gup.o $(mmu-y)
493+ prfile.o debug.o gup.o $(mmu-y)
076b876e 494
fbc438ed
JR
495 # Give 'page_alloc' its own module-parameter namespace
496 page-alloc-y := page_alloc.o
fb47a38f 497diff --git a/mm/filemap.c b/mm/filemap.c
43982f53 498index 85b7d087eb45..115275a4a0e2 100644
fb47a38f
JR
499--- a/mm/filemap.c
500+++ b/mm/filemap.c
43982f53 501@@ -2696,7 +2696,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
cd7a4cd9 502 vm_fault_t ret = VM_FAULT_LOCKED;
fb47a38f
JR
503
504 sb_start_pagefault(inode->i_sb);
521ced18
JR
505- file_update_time(vmf->vma->vm_file);
506+ vma_file_update_time(vmf->vma);
fb47a38f
JR
507 lock_page(page);
508 if (page->mapping != inode->i_mapping) {
509 unlock_page(page);
fb47a38f 510diff --git a/mm/mmap.c b/mm/mmap.c
43982f53 511index a7d8c84d19b7..9c350dc3f570 100644
fb47a38f
JR
512--- a/mm/mmap.c
513+++ b/mm/mmap.c
fbc438ed 514@@ -182,7 +182,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
fb47a38f
JR
515 if (vma->vm_ops && vma->vm_ops->close)
516 vma->vm_ops->close(vma);
517 if (vma->vm_file)
518- fput(vma->vm_file);
519+ vma_fput(vma);
520 mpol_put(vma_policy(vma));
cd7a4cd9 521 vm_area_free(vma);
fb47a38f 522 return next;
43982f53 523@@ -940,7 +940,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
fb47a38f
JR
524 if (remove_next) {
525 if (file) {
526 uprobe_munmap(next, next->vm_start, next->vm_end);
527- fput(file);
528+ vma_fput(vma);
529 }
530 if (next->anon_vma)
531 anon_vma_merge(vma, next);
43982f53 532@@ -1865,8 +1865,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
35939ee7
JR
533 return addr;
534
fb47a38f 535 unmap_and_free_vma:
fb47a38f
JR
536+ vma_fput(vma);
537 vma->vm_file = NULL;
538- fput(file);
539
540 /* Undo any partial mapping done by a device driver. */
541 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
43982f53 542@@ -2695,7 +2695,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
543 goto out_free_mpol;
544
545 if (new->vm_file)
546- get_file(new->vm_file);
547+ vma_get_file(new);
548
549 if (new->vm_ops && new->vm_ops->open)
550 new->vm_ops->open(new);
43982f53 551@@ -2714,7 +2714,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
552 if (new->vm_ops && new->vm_ops->close)
553 new->vm_ops->close(new);
554 if (new->vm_file)
555- fput(new->vm_file);
556+ vma_fput(new);
557 unlink_anon_vmas(new);
558 out_free_mpol:
559 mpol_put(vma_policy(new));
43982f53 560@@ -2906,7 +2906,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
7e9cd9fe
AM
561 struct vm_area_struct *vma;
562 unsigned long populate = 0;
563 unsigned long ret = -EINVAL;
564- struct file *file;
5afbbe0d 565+ struct file *file, *prfile;
7e9cd9fe 566
cd7a4cd9 567 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
5afbbe0d 568 current->comm, current->pid);
43982f53 569@@ -2981,10 +2981,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
febd17d6 570 }
7e9cd9fe
AM
571 }
572
573- file = get_file(vma->vm_file);
574+ vma_get_file(vma);
5afbbe0d
AM
575+ file = vma->vm_file;
576+ prfile = vma->vm_prfile;
7e9cd9fe 577 ret = do_mmap_pgoff(vma->vm_file, start, size,
521ced18 578 prot, flags, pgoff, &populate, NULL);
5afbbe0d
AM
579+ if (!IS_ERR_VALUE(ret) && file && prfile) {
580+ struct vm_area_struct *new_vma;
581+
582+ new_vma = find_vma(mm, ret);
583+ if (!new_vma->vm_prfile)
584+ new_vma->vm_prfile = prfile;
585+ if (new_vma != vma)
586+ get_file(prfile);
587+ }
588+ /*
589+ * two fput()s instead of vma_fput(vma),
590+ * coz vma may not be available anymore.
591+ */
592 fput(file);
593+ if (prfile)
594+ fput(prfile);
7e9cd9fe
AM
595 out:
596 up_write(&mm->mmap_sem);
597 if (populate)
43982f53 598@@ -3274,7 +3291,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
79b8bda9
AM
599 if (anon_vma_clone(new_vma, vma))
600 goto out_free_mempol;
601 if (new_vma->vm_file)
602- get_file(new_vma->vm_file);
603+ vma_get_file(new_vma);
604 if (new_vma->vm_ops && new_vma->vm_ops->open)
605 new_vma->vm_ops->open(new_vma);
606 vma_link(mm, new_vma, prev, rb_link, rb_parent);
fb47a38f 607diff --git a/mm/nommu.c b/mm/nommu.c
43982f53 608index 99b7ec318824..de5b6cd162fe 100644
fb47a38f
JR
609--- a/mm/nommu.c
610+++ b/mm/nommu.c
eca34b5c 611@@ -552,7 +552,7 @@ static void __put_nommu_region(struct vm_region *region)
fb47a38f
JR
612 up_write(&nommu_region_sem);
613
614 if (region->vm_file)
615- fput(region->vm_file);
616+ vmr_fput(region);
617
618 /* IO memory and memory shared directly out of the pagecache
619 * from ramfs/tmpfs mustn't be released here */
eca34b5c 620@@ -690,7 +690,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
fb47a38f
JR
621 if (vma->vm_ops && vma->vm_ops->close)
622 vma->vm_ops->close(vma);
623 if (vma->vm_file)
624- fput(vma->vm_file);
625+ vma_fput(vma);
626 put_nommu_region(vma->vm_region);
cd7a4cd9 627 vm_area_free(vma);
fb47a38f 628 }
eca34b5c 629@@ -1213,7 +1213,7 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
630 goto error_just_free;
631 }
632 }
633- fput(region->vm_file);
634+ vmr_fput(region);
635 kmem_cache_free(vm_region_jar, region);
636 region = pregion;
637 result = start;
eca34b5c 638@@ -1290,10 +1290,10 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
639 up_write(&nommu_region_sem);
640 error:
641 if (region->vm_file)
642- fput(region->vm_file);
643+ vmr_fput(region);
644 kmem_cache_free(vm_region_jar, region);
645 if (vma->vm_file)
eca801bf
AM
646- fput(vma->vm_file);
647+ vma_fput(vma);
648 vm_area_free(vma);
649 return ret;
650
076b876e
AM
651diff --git a/mm/prfile.c b/mm/prfile.c
652new file mode 100644
016522bc 653index 000000000000..00d51187c325
076b876e
AM
654--- /dev/null
655+++ b/mm/prfile.c
2121bcd9 656@@ -0,0 +1,86 @@
cd7a4cd9 657+// SPDX-License-Identifier: GPL-2.0
076b876e 658+/*
1c60b727
AM
659+ * Mainly for aufs which mmap(2) different file and wants to print different
660+ * path in /proc/PID/maps.
076b876e
AM
661+ * Call these functions via macros defined in linux/mm.h.
662+ *
663+ * See Documentation/filesystems/aufs/design/06mmap.txt
664+ *
016522bc 665+ * Copyright (c) 2014-2020 Junjro R. Okajima
076b876e
AM
666+ * Copyright (c) 2014 Ian Campbell
667+ */
668+
669+#include <linux/mm.h>
670+#include <linux/file.h>
671+#include <linux/fs.h>
672+
673+/* #define PRFILE_TRACE */
674+static inline void prfile_trace(struct file *f, struct file *pr,
675+ const char func[], int line, const char func2[])
676+{
677+#ifdef PRFILE_TRACE
678+ if (pr)
1c60b727 679+ pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
076b876e
AM
680+#endif
681+}
682+
076b876e
AM
683+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
684+ int line)
685+{
686+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
687+
688+ prfile_trace(f, pr, func, line, __func__);
689+ file_update_time(f);
690+ if (f && pr)
691+ file_update_time(pr);
692+}
693+
694+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
695+ int line)
696+{
697+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
698+
699+ prfile_trace(f, pr, func, line, __func__);
700+ return (f && pr) ? pr : f;
701+}
702+
703+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
704+{
705+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
706+
707+ prfile_trace(f, pr, func, line, __func__);
708+ get_file(f);
709+ if (f && pr)
710+ get_file(pr);
711+}
712+
713+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
714+{
715+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
716+
717+ prfile_trace(f, pr, func, line, __func__);
718+ fput(f);
719+ if (f && pr)
720+ fput(pr);
721+}
b912730e
AM
722+
723+#ifndef CONFIG_MMU
076b876e
AM
724+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
725+ int line)
726+{
727+ struct file *f = region->vm_file, *pr = region->vm_prfile;
728+
729+ prfile_trace(f, pr, func, line, __func__);
730+ return (f && pr) ? pr : f;
731+}
732+
733+void vmr_do_fput(struct vm_region *region, const char func[], int line)
734+{
735+ struct file *f = region->vm_file, *pr = region->vm_prfile;
736+
737+ prfile_trace(f, pr, func, line, __func__);
738+ fput(f);
739+ if (f && pr)
740+ fput(pr);
741+}
b912730e 742+#endif /* !CONFIG_MMU */
2121bcd9 743SPDX-License-Identifier: GPL-2.0
43982f53 744aufs5.4 standalone patch
7f207e10 745
c1595e42 746diff --git a/fs/dcache.c b/fs/dcache.c
eca34b5c 747index 7ce4ccf5a51c..00d7e6a08026 100644
c1595e42
JR
748--- a/fs/dcache.c
749+++ b/fs/dcache.c
eca34b5c 750@@ -1369,6 +1369,7 @@ void d_walk(struct dentry *parent, void *data,
c1595e42
JR
751 seq = 1;
752 goto again;
753 }
febd17d6 754+EXPORT_SYMBOL_GPL(d_walk);
c1595e42 755
a2654f78
AM
756 struct check_mount {
757 struct vfsmount *mnt;
eca34b5c 758@@ -2914,6 +2915,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
f2c43d5f
AM
759
760 write_sequnlock(&rename_lock);
761 }
762+EXPORT_SYMBOL_GPL(d_exchange);
763
764 /**
765 * d_ancestor - search for an ancestor
79b8bda9 766diff --git a/fs/exec.c b/fs/exec.c
43982f53 767index 555e93c7dec8..dad39c6b3878 100644
79b8bda9
AM
768--- a/fs/exec.c
769+++ b/fs/exec.c
fbc438ed 770@@ -110,6 +110,7 @@ bool path_noexec(const struct path *path)
79b8bda9
AM
771 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
772 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
773 }
febd17d6 774+EXPORT_SYMBOL_GPL(path_noexec);
79b8bda9
AM
775
776 #ifdef CONFIG_USELIB
777 /*
febd17d6 778diff --git a/fs/fcntl.c b/fs/fcntl.c
43982f53 779index 12dd73930961..0468c845190f 100644
febd17d6
JR
780--- a/fs/fcntl.c
781+++ b/fs/fcntl.c
43982f53 782@@ -85,6 +85,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
febd17d6
JR
783 out:
784 return error;
785 }
786+EXPORT_SYMBOL_GPL(setfl);
787
788 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
789 int force)
b912730e 790diff --git a/fs/file_table.c b/fs/file_table.c
43982f53 791index 30d55c9a1744..34b9bbf4c556 100644
b912730e
AM
792--- a/fs/file_table.c
793+++ b/fs/file_table.c
fbc438ed 794@@ -162,6 +162,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
b912730e
AM
795 }
796 return ERR_PTR(-ENFILE);
797 }
acd2b654 798+EXPORT_SYMBOL_GPL(alloc_empty_file);
b912730e 799
acd2b654
AM
800 /*
801 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
5afbbe0d 802diff --git a/fs/inode.c b/fs/inode.c
43982f53 803index aaeacde398ee..5be87f2d3828 100644
5afbbe0d
AM
804--- a/fs/inode.c
805+++ b/fs/inode.c
43982f53 806@@ -1682,6 +1682,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
5afbbe0d
AM
807
808 return update_time(inode, time, flags);
809 }
810+EXPORT_SYMBOL_GPL(update_time);
811
812 /**
813 * touch_atime - update the access time
7f207e10 814diff --git a/fs/namespace.c b/fs/namespace.c
43982f53 815index 0f7e57e5f4b7..516c2f397d33 100644
7f207e10
AM
816--- a/fs/namespace.c
817+++ b/fs/namespace.c
eca34b5c 818@@ -431,6 +431,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
c06a8ce3
AM
819 mnt_dec_writers(real_mount(mnt));
820 preempt_enable();
821 }
822+EXPORT_SYMBOL_GPL(__mnt_drop_write);
823
824 /**
825 * mnt_drop_write - give up write access to a mount
eca34b5c 826@@ -781,6 +782,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
8b6a4947
AM
827 {
828 return check_mnt(real_mount(mnt));
829 }
830+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
831
832 /*
833 * vfsmount lock must be held for write
43982f53 834@@ -1903,6 +1905,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
7f207e10
AM
835 }
836 return 0;
837 }
febd17d6 838+EXPORT_SYMBOL_GPL(iterate_mounts);
7f207e10 839
fbc438ed 840 static void lock_mnt_tree(struct mount *mnt)
7f207e10 841 {
43982f53
AM
842diff --git a/fs/notify/group.c b/fs/notify/group.c
843index 133f723aca07..0b9f7f6d8390 100644
844--- a/fs/notify/group.c
845+++ b/fs/notify/group.c
846@@ -99,6 +99,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
1716fcea 847 {
2121bcd9 848 refcount_inc(&group->refcnt);
1716fcea 849 }
febd17d6 850+EXPORT_SYMBOL_GPL(fsnotify_get_group);
1716fcea
AM
851
852 /*
853 * Drop a reference to a group. Free it if it's through.
7f207e10 854diff --git a/fs/open.c b/fs/open.c
43982f53 855index b62f5c0923a8..89af4b9c7319 100644
7f207e10
AM
856--- a/fs/open.c
857+++ b/fs/open.c
fbc438ed 858@@ -65,6 +65,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
febd17d6 859 inode_unlock(dentry->d_inode);
7f207e10
AM
860 return ret;
861 }
febd17d6 862+EXPORT_SYMBOL_GPL(do_truncate);
7f207e10 863
5afbbe0d 864 long vfs_truncate(const struct path *path, loff_t length)
7f207e10 865 {
5527c038 866diff --git a/fs/read_write.c b/fs/read_write.c
43982f53 867index fa9b3994b34c..eb0e2c6ebaff 100644
5527c038
JR
868--- a/fs/read_write.c
869+++ b/fs/read_write.c
fbc438ed 870@@ -468,6 +468,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
8b6a4947
AM
871
872 return ret;
873 }
874+EXPORT_SYMBOL_GPL(vfs_read);
875
876 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
877 {
fbc438ed 878@@ -508,6 +509,7 @@ vfs_readf_t vfs_readf(struct file *file)
5527c038 879 return new_sync_read;
43982f53 880 return ERR_PTR(-ENOSYS); /* doesn't have ->read(|_iter)() op */
5527c038 881 }
febd17d6 882+EXPORT_SYMBOL_GPL(vfs_readf);
5527c038
JR
883
884 vfs_writef_t vfs_writef(struct file *file)
885 {
fbc438ed 886@@ -519,6 +521,7 @@ vfs_writef_t vfs_writef(struct file *file)
5527c038 887 return new_sync_write;
43982f53 888 return ERR_PTR(-ENOSYS); /* doesn't have ->write(|_iter)() op */
5527c038 889 }
febd17d6 890+EXPORT_SYMBOL_GPL(vfs_writef);
5527c038 891
8b6a4947
AM
892 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
893 {
fbc438ed 894@@ -588,6 +591,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
8b6a4947
AM
895
896 return ret;
897 }
898+EXPORT_SYMBOL_GPL(vfs_write);
899
fbc438ed
JR
900 /* file_ppos returns &file->f_pos or NULL if file is stream */
901 static inline loff_t *file_ppos(struct file *file)
7f207e10 902diff --git a/fs/splice.c b/fs/splice.c
eca34b5c 903index 75b489fcb66f..0a1f7498c22b 100644
7f207e10
AM
904--- a/fs/splice.c
905+++ b/fs/splice.c
fbc438ed 906@@ -847,6 +847,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
392086de
AM
907
908 return splice_write(pipe, out, ppos, len, flags);
7f207e10 909 }
febd17d6 910+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
911
912 /*
913 * Attempt to initiate a splice from a file to a pipe.
fbc438ed 914@@ -876,6 +877,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
7f207e10
AM
915
916 return splice_read(in, ppos, pipe, len, flags);
917 }
febd17d6 918+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
919
920 /**
921 * splice_direct_to_actor - splices data directly between two non-pipes
a2654f78 922diff --git a/fs/sync.c b/fs/sync.c
fbc438ed 923index 457f4e4a5cc1..67c66358f3fe 100644
a2654f78
AM
924--- a/fs/sync.c
925+++ b/fs/sync.c
2121bcd9 926@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
a2654f78
AM
927 sb->s_op->sync_fs(sb, wait);
928 return __sync_blockdev(sb->s_bdev, wait);
929 }
930+EXPORT_SYMBOL_GPL(__sync_filesystem);
931
932 /*
933 * Write out and wait upon all dirty data associated with this
c1595e42 934diff --git a/fs/xattr.c b/fs/xattr.c
fbc438ed 935index 90dd78f0eb27..40b01dd1b14a 100644
c1595e42
JR
936--- a/fs/xattr.c
937+++ b/fs/xattr.c
fbc438ed 938@@ -296,6 +296,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
c1595e42
JR
939 *xattr_value = value;
940 return error;
941 }
febd17d6 942+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 943
febd17d6 944 ssize_t
f2c43d5f 945 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947 946diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
43982f53 947index 407c08ac8ac8..d7f71842f8a2 100644
8b6a4947
AM
948--- a/kernel/locking/lockdep.c
949+++ b/kernel/locking/lockdep.c
eca34b5c
AM
950@@ -174,6 +174,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
951 */
952 return lock_classes + class_idx;
8b6a4947
AM
953 }
954+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
955 #define hlock_class(hlock) lockdep_hlock_class(hlock)
956
957 #ifdef CONFIG_LOCK_STAT
8cdd5066 958diff --git a/kernel/task_work.c b/kernel/task_work.c
ba1aed25 959index 0fef395662a6..83fb1ecfc33d 100644
8cdd5066
JR
960--- a/kernel/task_work.c
961+++ b/kernel/task_work.c
2121bcd9 962@@ -116,3 +116,4 @@ void task_work_run(void)
8cdd5066
JR
963 } while (work);
964 }
965 }
febd17d6 966+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 967diff --git a/security/device_cgroup.c b/security/device_cgroup.c
eca34b5c 968index 725674f3276d..83f6494c52a2 100644
7f207e10
AM
969--- a/security/device_cgroup.c
970+++ b/security/device_cgroup.c
83b672a5 971@@ -824,3 +824,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
7f207e10 972
2121bcd9
AM
973 return 0;
974 }
975+EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
7f207e10 976diff --git a/security/security.c b/security/security.c
43982f53 977index 1bc000f834e2..306f2c9f7ee2 100644
7f207e10
AM
978--- a/security/security.c
979+++ b/security/security.c
43982f53 980@@ -1036,6 +1036,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 981 return 0;
c2c0f25c 982 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 983 }
febd17d6 984+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 985
5afbbe0d 986 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 987 {
43982f53 988@@ -1052,6 +1053,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 989 return 0;
c2c0f25c 990 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 991 }
febd17d6 992+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 993
5afbbe0d 994 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 995 struct dentry *new_dentry)
43982f53 996@@ -1060,6 +1062,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 997 return 0;
c2c0f25c 998 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 999 }
febd17d6 1000+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 1001
5afbbe0d
AM
1002 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1003 const struct path *new_dir, struct dentry *new_dentry,
43982f53 1004@@ -1087,6 +1090,7 @@ int security_path_truncate(const struct path *path)
7f207e10 1005 return 0;
c2c0f25c 1006 return call_int_hook(path_truncate, 0, path);
7f207e10 1007 }
febd17d6 1008+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 1009
5afbbe0d 1010 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 1011 {
43982f53 1012@@ -1094,6 +1098,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 1013 return 0;
c2c0f25c 1014 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 1015 }
febd17d6 1016+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 1017
5afbbe0d 1018 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1019 {
43982f53 1020@@ -1101,6 +1106,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1021 return 0;
c2c0f25c 1022 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 1023 }
febd17d6 1024+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 1025
5afbbe0d 1026 int security_path_chroot(const struct path *path)
7f207e10 1027 {
43982f53 1028@@ -1201,6 +1207,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 1029 return 0;
c2c0f25c 1030 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 1031 }
febd17d6 1032+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 1033
1e00d052 1034 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
7f207e10 1035 {
43982f53 1036@@ -1378,6 +1385,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
1037
1038 return fsnotify_perm(file, mask);
1039 }
febd17d6 1040+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
1041
1042 int security_file_alloc(struct file *file)
1043 {
7f207e10 1044diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
eca34b5c
AM
1045--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
1046+++ linux/Documentation/ABI/testing/debugfs-aufs 2019-07-11 15:42:14.455570938 +0200
062440b3 1047@@ -0,0 +1,55 @@
7f207e10
AM
1048+What: /debug/aufs/si_<id>/
1049+Date: March 2009
f6b6e03d 1050+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1051+Description:
1052+ Under /debug/aufs, a directory named si_<id> is created
1053+ per aufs mount, where <id> is a unique id generated
1054+ internally.
1facf9fc 1055+
86dc4139
AM
1056+What: /debug/aufs/si_<id>/plink
1057+Date: Apr 2013
f6b6e03d 1058+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
1059+Description:
1060+ It has three lines and shows the information about the
1061+ pseudo-link. The first line is a single number
1062+ representing a number of buckets. The second line is a
1063+ number of pseudo-links per buckets (separated by a
1064+ blank). The last line is a single number representing a
1065+ total number of psedo-links.
1066+ When the aufs mount option 'noplink' is specified, it
1067+ will show "1\n0\n0\n".
1068+
7f207e10
AM
1069+What: /debug/aufs/si_<id>/xib
1070+Date: March 2009
f6b6e03d 1071+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1072+Description:
1073+ It shows the consumed blocks by xib (External Inode Number
1074+ Bitmap), its block size and file size.
1075+ When the aufs mount option 'noxino' is specified, it
1076+ will be empty. About XINO files, see the aufs manual.
1077+
062440b3 1078+What: /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
7f207e10 1079+Date: March 2009
f6b6e03d 1080+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1081+Description:
1082+ It shows the consumed blocks by xino (External Inode Number
1083+ Translation Table), its link count, block size and file
1084+ size.
062440b3
AM
1085+ Due to the file size limit, there may exist multiple
1086+ xino files per branch. In this case, "-N" is added to
1087+ the filename and it corresponds to the index of the
1088+ internal xino array. "-0" is omitted.
1089+ When the aufs mount option 'noxino' is specified, Those
1090+ entries won't exist. About XINO files, see the aufs
1091+ manual.
7f207e10
AM
1092+
1093+What: /debug/aufs/si_<id>/xigen
1094+Date: March 2009
f6b6e03d 1095+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1096+Description:
1097+ It shows the consumed blocks by xigen (External Inode
1098+ Generation Table), its block size and file size.
1099+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1100+ be created.
1101+ When the aufs mount option 'noxino' is specified, it
1102+ will be empty. About XINO files, see the aufs manual.
1103diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
eca34b5c
AM
1104--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
1105+++ linux/Documentation/ABI/testing/sysfs-aufs 2019-07-11 15:42:14.455570938 +0200
392086de 1106@@ -0,0 +1,31 @@
7f207e10
AM
1107+What: /sys/fs/aufs/si_<id>/
1108+Date: March 2009
f6b6e03d 1109+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1110+Description:
1111+ Under /sys/fs/aufs, a directory named si_<id> is created
1112+ per aufs mount, where <id> is a unique id generated
1113+ internally.
1114+
1115+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1116+Date: March 2009
f6b6e03d 1117+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1118+Description:
1119+ It shows the abolute path of a member directory (which
1120+ is called branch) in aufs, and its permission.
1121+
392086de
AM
1122+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1123+Date: July 2013
f6b6e03d 1124+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1125+Description:
1126+ It shows the id of a member directory (which is called
1127+ branch) in aufs.
1128+
7f207e10
AM
1129+What: /sys/fs/aufs/si_<id>/xi_path
1130+Date: March 2009
f6b6e03d 1131+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1132+Description:
1133+ It shows the abolute path of XINO (External Inode Number
1134+ Bitmap, Translation Table and Generation Table) file
1135+ even if it is the default path.
1136+ When the aufs mount option 'noxino' is specified, it
1137+ will be empty. About XINO files, see the aufs manual.
53392da6 1138diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
eca34b5c 1139--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 1140+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2020-01-27 10:57:18.162204582 +0100
1c60b727 1141@@ -0,0 +1,171 @@
53392da6 1142+
016522bc 1143+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1144+#
1145+# This program is free software; you can redistribute it and/or modify
1146+# it under the terms of the GNU General Public License as published by
1147+# the Free Software Foundation; either version 2 of the License, or
1148+# (at your option) any later version.
1149+#
1150+# This program is distributed in the hope that it will be useful,
1151+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1152+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1153+# GNU General Public License for more details.
1154+#
1155+# You should have received a copy of the GNU General Public License
523b37e3 1156+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1157+
1158+Introduction
1159+----------------------------------------
1160+
3c1bdaff 1161+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1162+1. abbrev. for "advanced multi-layered unification filesystem".
1163+2. abbrev. for "another unionfs".
1164+3. abbrev. for "auf das" in German which means "on the" in English.
1165+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1166+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1167+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1168+
1169+AUFS is a filesystem with features:
1170+- multi layered stackable unification filesystem, the member directory
1171+ is called as a branch.
1172+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1173+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1174+ combination.
1175+- internal "file copy-on-write".
1176+- logical deletion, whiteout.
1177+- dynamic branch manipulation, adding, deleting and changing permission.
1178+- allow bypassing aufs, user's direct branch access.
1179+- external inode number translation table and bitmap which maintains the
1180+ persistent aufs inode number.
1181+- seekable directory, including NFS readdir.
1182+- file mapping, mmap and sharing pages.
1183+- pseudo-link, hardlink over branches.
1184+- loopback mounted filesystem as a branch.
1185+- several policies to select one among multiple writable branches.
1186+- revert a single systemcall when an error occurs in aufs.
1187+- and more...
1188+
1189+
1190+Multi Layered Stackable Unification Filesystem
1191+----------------------------------------------------------------------
1192+Most people already knows what it is.
1193+It is a filesystem which unifies several directories and provides a
1194+merged single directory. When users access a file, the access will be
1195+passed/re-directed/converted (sorry, I am not sure which English word is
1196+correct) to the real file on the member filesystem. The member
1197+filesystem is called 'lower filesystem' or 'branch' and has a mode
1198+'readonly' and 'readwrite.' And the deletion for a file on the lower
1199+readonly branch is handled by creating 'whiteout' on the upper writable
1200+branch.
1201+
1202+On LKML, there have been discussions about UnionMount (Jan Blunck,
1203+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1204+different approaches to implement the merged-view.
1205+The former tries putting it into VFS, and the latter implements as a
1206+separate filesystem.
1207+(If I misunderstand about these implementations, please let me know and
1208+I shall correct it. Because it is a long time ago when I read their
1209+source files last time).
1210+
1211+UnionMount's approach will be able to small, but may be hard to share
1212+branches between several UnionMount since the whiteout in it is
1213+implemented in the inode on branch filesystem and always
1214+shared. According to Bharata's post, readdir does not seems to be
1215+finished yet.
1216+There are several missing features known in this implementations such as
1217+- for users, the inode number may change silently. eg. copy-up.
1218+- link(2) may break by copy-up.
1219+- read(2) may get an obsoleted filedata (fstat(2) too).
1220+- fcntl(F_SETLK) may be broken by copy-up.
1221+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1222+ open(O_RDWR).
1223+
7e9cd9fe
AM
1224+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1225+merged into mainline. This is another implementation of UnionMount as a
1226+separated filesystem. All the limitations and known problems which
1227+UnionMount are equally inherited to "overlay" filesystem.
1228+
1229+Unionfs has a longer history. When I started implementing a stackable
1230+filesystem (Aug 2005), it already existed. It has virtual super_block,
1231+inode, dentry and file objects and they have an array pointing lower
1232+same kind objects. After contributing many patches for Unionfs, I
1233+re-started my project AUFS (Jun 2006).
53392da6
AM
1234+
1235+In AUFS, the structure of filesystem resembles to Unionfs, but I
1236+implemented my own ideas, approaches and enhancements and it became
1237+totally different one.
1238+
1239+Comparing DM snapshot and fs based implementation
1240+- the number of bytes to be copied between devices is much smaller.
1241+- the type of filesystem must be one and only.
1242+- the fs must be writable, no readonly fs, even for the lower original
1243+ device. so the compression fs will not be usable. but if we use
1244+ loopback mount, we may address this issue.
1245+ for instance,
1246+ mount /cdrom/squashfs.img /sq
1247+ losetup /sq/ext2.img
1248+ losetup /somewhere/cow
1249+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1250+- it will be difficult (or needs more operations) to extract the
1251+ difference between the original device and COW.
1252+- DM snapshot-merge may help a lot when users try merging. in the
1253+ fs-layer union, users will use rsync(1).
1254+
7e9cd9fe
AM
1255+You may want to read my old paper "Filesystems in LiveCD"
1256+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1257+
7e9cd9fe
AM
1258+
1259+Several characters/aspects/persona of aufs
53392da6
AM
1260+----------------------------------------------------------------------
1261+
7e9cd9fe 1262+Aufs has several characters, aspects or persona.
53392da6
AM
1263+1. a filesystem, callee of VFS helper
1264+2. sub-VFS, caller of VFS helper for branches
1265+3. a virtual filesystem which maintains persistent inode number
1266+4. reader/writer of files on branches such like an application
1267+
1268+1. Callee of VFS Helper
1269+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1270+unlink(2) from an application reaches sys_unlink() kernel function and
1271+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1272+calls filesystem specific unlink operation. Actually aufs implements the
1273+unlink operation but it behaves like a redirector.
1274+
1275+2. Caller of VFS Helper for Branches
1276+aufs_unlink() passes the unlink request to the branch filesystem as if
1277+it were called from VFS. So the called unlink operation of the branch
1278+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1279+every necessary pre/post operation for the branch filesystem.
1280+- acquire the lock for the parent dir on a branch
1281+- lookup in a branch
1282+- revalidate dentry on a branch
1283+- mnt_want_write() for a branch
1284+- vfs_unlink() for a branch
1285+- mnt_drop_write() for a branch
1286+- release the lock on a branch
1287+
1288+3. Persistent Inode Number
1289+One of the most important issue for a filesystem is to maintain inode
1290+numbers. This is particularly important to support exporting a
1291+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1292+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1293+keep and maintain the inode numbers. It may be a large space and may not
1294+suit to keep in memory. Aufs rents some space from its first writable
1295+branch filesystem (by default) and creates file(s) on it. These files
1296+are created by aufs internally and removed soon (currently) keeping
1297+opened.
53392da6
AM
1298+Note: Because these files are removed, they are totally gone after
1299+ unmounting aufs. It means the inode numbers are not persistent
1300+ across unmount or reboot. I have a plan to make them really
1301+ persistent which will be important for aufs on NFS server.
1302+
1303+4. Read/Write Files Internally (copy-on-write)
1304+Because a branch can be readonly, when you write a file on it, aufs will
1305+"copy-up" it to the upper writable branch internally. And then write the
1306+originally requested thing to the file. Generally kernel doesn't
1307+open/read/write file actively. In aufs, even a single write may cause a
1308+internal "file copy". This behaviour is very similar to cp(1) command.
1309+
1310+Some people may think it is better to pass such work to user space
1311+helper, instead of doing in kernel space. Actually I am still thinking
1312+about it. But currently I have implemented it in kernel space.
1313diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
eca34b5c 1314--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 1315+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2020-01-27 10:57:18.162204582 +0100
7e9cd9fe 1316@@ -0,0 +1,258 @@
53392da6 1317+
016522bc 1318+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1319+#
1320+# This program is free software; you can redistribute it and/or modify
1321+# it under the terms of the GNU General Public License as published by
1322+# the Free Software Foundation; either version 2 of the License, or
1323+# (at your option) any later version.
1324+#
1325+# This program is distributed in the hope that it will be useful,
1326+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1327+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1328+# GNU General Public License for more details.
1329+#
1330+# You should have received a copy of the GNU General Public License
523b37e3 1331+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1332+
1333+Basic Aufs Internal Structure
1334+
1335+Superblock/Inode/Dentry/File Objects
1336+----------------------------------------------------------------------
1337+As like an ordinary filesystem, aufs has its own
1338+superblock/inode/dentry/file objects. All these objects have a
1339+dynamically allocated array and store the same kind of pointers to the
1340+lower filesystem, branch.
1341+For example, when you build a union with one readwrite branch and one
1342+readonly, mounted /au, /rw and /ro respectively.
1343+- /au = /rw + /ro
1344+- /ro/fileA exists but /rw/fileA
1345+
1346+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1347+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1348+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1349+- [1] = /ro/fileA
1350+
1351+This style of an array is essentially same to the aufs
1352+superblock/inode/dentry/file objects.
1353+
1354+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1355+branches dynamically, these objects has its own generation. When
1356+branches are changed, the generation in aufs superblock is
1357+incremented. And a generation in other object are compared when it is
1358+accessed. When a generation in other objects are obsoleted, aufs
1359+refreshes the internal array.
53392da6
AM
1360+
1361+
1362+Superblock
1363+----------------------------------------------------------------------
1364+Additionally aufs superblock has some data for policies to select one
1365+among multiple writable branches, XIB files, pseudo-links and kobject.
1366+See below in detail.
7e9cd9fe
AM
1367+About the policies which supports copy-down a directory, see
1368+wbr_policy.txt too.
53392da6
AM
1369+
1370+
1371+Branch and XINO(External Inode Number Translation Table)
1372+----------------------------------------------------------------------
1373+Every branch has its own xino (external inode number translation table)
1374+file. The xino file is created and unlinked by aufs internally. When two
1375+members of a union exist on the same filesystem, they share the single
1376+xino file.
1377+The struct of a xino file is simple, just a sequence of aufs inode
1378+numbers which is indexed by the lower inode number.
1379+In the above sample, assume the inode number of /ro/fileA is i111 and
1380+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1381+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1382+
1383+When the inode numbers are not contiguous, the xino file will be sparse
1384+which has a hole in it and doesn't consume as much disk space as it
1385+might appear. If your branch filesystem consumes disk space for such
1386+holes, then you should specify 'xino=' option at mounting aufs.
1387+
7e9cd9fe
AM
1388+Aufs has a mount option to free the disk blocks for such holes in XINO
1389+files on tmpfs or ramdisk. But it is not so effective actually. If you
1390+meet a problem of disk shortage due to XINO files, then you should try
1391+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1392+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1393+the holes in XINO files.
1394+
53392da6 1395+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1396+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1397+whiteout-ed doubly, so that users will never see their names in aufs
1398+hierarchy.
7e9cd9fe 1399+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1400+2. a directory to store a pseudo-link.
7e9cd9fe 1401+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1402+
1403+1. Whiteout Base
1404+ When you remove a file on a readonly branch, aufs handles it as a
1405+ logical deletion and creates a whiteout on the upper writable branch
1406+ as a hardlink of this file in order not to consume inode on the
1407+ writable branch.
1408+2. Pseudo-link Dir
1409+ See below, Pseudo-link.
1410+3. Step-Parent Dir
1411+ When "fileC" exists on the lower readonly branch only and it is
1412+ opened and removed with its parent dir, and then user writes
1413+ something into it, then aufs copies-up fileC to this
1414+ directory. Because there is no other dir to store fileC. After
1415+ creating a file under this dir, the file is unlinked.
1416+
1417+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1418+dynamically, a branch has its own id. When the branch order changes,
1419+aufs finds the new index by searching the branch id.
53392da6
AM
1420+
1421+
1422+Pseudo-link
1423+----------------------------------------------------------------------
1424+Assume "fileA" exists on the lower readonly branch only and it is
1425+hardlinked to "fileB" on the branch. When you write something to fileA,
1426+aufs copies-up it to the upper writable branch. Additionally aufs
1427+creates a hardlink under the Pseudo-link Directory of the writable
1428+branch. The inode of a pseudo-link is kept in aufs super_block as a
1429+simple list. If fileB is read after unlinking fileA, aufs returns
1430+filedata from the pseudo-link instead of the lower readonly
1431+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1432+inode number by xino (see above) is essentially necessary.
53392da6
AM
1433+
1434+All the hardlinks under the Pseudo-link Directory of the writable branch
1435+should be restored in a proper location later. Aufs provides a utility
1436+to do this. The userspace helpers executed at remounting and unmounting
1437+aufs by default.
1438+During this utility is running, it puts aufs into the pseudo-link
1439+maintenance mode. In this mode, only the process which began the
1440+maintenance mode (and its child processes) is allowed to operate in
1441+aufs. Some other processes which are not related to the pseudo-link will
1442+be allowed to run too, but the rest have to return an error or wait
1443+until the maintenance mode ends. If a process already acquires an inode
1444+mutex (in VFS), it has to return an error.
1445+
1446+
1447+XIB(external inode number bitmap)
1448+----------------------------------------------------------------------
1449+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1450+bitmap in a superblock object. It is also an internal file such like a
1451+xino file.
53392da6
AM
1452+It is a simple bitmap to mark whether the aufs inode number is in-use or
1453+not.
1454+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1455+
7e9cd9fe 1456+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1457+reduce the number of consumed disk blocks for these files.
1458+
1459+
1460+Virtual or Vertical Dir, and Readdir in Userspace
1461+----------------------------------------------------------------------
1462+In order to support multiple layers (branches), aufs readdir operation
1463+constructs a virtual dir block on memory. For readdir, aufs calls
1464+vfs_readdir() internally for each dir on branches, merges their entries
1465+with eliminating the whiteout-ed ones, and sets it to file (dir)
1466+object. So the file object has its entry list until it is closed. The
1467+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1468+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1469+
1470+The dynamically allocated memory block for the name of entries has a
1471+unit of 512 bytes (by default) and stores the names contiguously (no
1472+padding). Another block for each entry is handled by kmem_cache too.
1473+During building dir blocks, aufs creates hash list and judging whether
1474+the entry is whiteouted by its upper branch or already listed.
1475+The merged result is cached in the corresponding inode object and
1476+maintained by a customizable life-time option.
1477+
1478+Some people may call it can be a security hole or invite DoS attack
1479+since the opened and once readdir-ed dir (file object) holds its entry
1480+list and becomes a pressure for system memory. But I'd say it is similar
1481+to files under /proc or /sys. The virtual files in them also holds a
1482+memory page (generally) while they are opened. When an idea to reduce
1483+memory for them is introduced, it will be applied to aufs too.
1484+For those who really hate this situation, I've developed readdir(3)
1485+library which operates this merging in userspace. You just need to set
1486+LD_PRELOAD environment variable, and aufs will not consume no memory in
1487+kernel space for readdir(3).
1488+
1489+
1490+Workqueue
1491+----------------------------------------------------------------------
1492+Aufs sometimes requires privilege access to a branch. For instance,
1493+in copy-up/down operation. When a user process is going to make changes
1494+to a file which exists in the lower readonly branch only, and the mode
1495+of one of ancestor directories may not be writable by a user
1496+process. Here aufs copy-up the file with its ancestors and they may
1497+require privilege to set its owner/group/mode/etc.
1498+This is a typical case of a application character of aufs (see
1499+Introduction).
1500+
1501+Aufs uses workqueue synchronously for this case. It creates its own
1502+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1503+passes the request to call mkdir or write (for example), and wait for
1504+its completion. This approach solves a problem of a signal handler
1505+simply.
1506+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1507+process, then the process may receive the unexpected SIGXFSZ or other
1508+signals.
53392da6
AM
1509+
1510+Also aufs uses the system global workqueue ("events" kernel thread) too
1511+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1512+whiteout base and etc. This is unrelated to a privilege.
1513+Most of aufs operation tries acquiring a rw_semaphore for aufs
1514+superblock at the beginning, at the same time waits for the completion
1515+of all queued asynchronous tasks.
1516+
1517+
1518+Whiteout
1519+----------------------------------------------------------------------
1520+The whiteout in aufs is very similar to Unionfs's. That is represented
1521+by its filename. UnionMount takes an approach of a file mode, but I am
1522+afraid several utilities (find(1) or something) will have to support it.
1523+
1524+Basically the whiteout represents "logical deletion" which stops aufs to
1525+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1526+further lookup.
53392da6
AM
1527+
1528+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1529+In order to make several functions in a single systemcall to be
1530+revertible, aufs adopts an approach to rename a directory to a temporary
1531+unique whiteouted name.
1532+For example, in rename(2) dir where the target dir already existed, aufs
1533+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1534+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1535+update the attributes, etc). If an error happens in these actions, aufs
1536+simply renames the whiteouted name back and returns an error. If all are
1537+succeeded, aufs registers a function to remove the whiteouted unique
1538+temporary name completely and asynchronously to the system global
1539+workqueue.
1540+
1541+
1542+Copy-up
1543+----------------------------------------------------------------------
1544+It is a well-known feature or concept.
1545+When user modifies a file on a readonly branch, aufs operate "copy-up"
1546+internally and makes change to the new file on the upper writable branch.
1547+When the trigger systemcall does not update the timestamps of the parent
1548+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1549+
1550+
1551+Move-down (aufs3.9 and later)
1552+----------------------------------------------------------------------
1553+"Copy-up" is one of the essential feature in aufs. It copies a file from
1554+the lower readonly branch to the upper writable branch when a user
1555+changes something about the file.
1556+"Move-down" is an opposite action of copy-up. Basically this action is
1557+ran manually instead of automatically and internally.
076b876e
AM
1558+For desgin and implementation, aufs has to consider these issues.
1559+- whiteout for the file may exist on the lower branch.
1560+- ancestor directories may not exist on the lower branch.
1561+- diropq for the ancestor directories may exist on the upper branch.
1562+- free space on the lower branch will reduce.
1563+- another access to the file may happen during moving-down, including
7e9cd9fe 1564+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1565+- the file should not be hard-linked nor pseudo-linked. they should be
1566+ handled by auplink utility later.
c2b27bf2
AM
1567+
1568+Sometimes users want to move-down a file from the upper writable branch
1569+to the lower readonly or writable branch. For instance,
1570+- the free space of the upper writable branch is going to run out.
1571+- create a new intermediate branch between the upper and lower branch.
1572+- etc.
1573+
1574+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e 1575diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
eca34b5c 1576--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 1577+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2020-01-27 10:57:18.162204582 +0100
b912730e
AM
1578@@ -0,0 +1,85 @@
1579+
016522bc 1580+# Copyright (C) 2015-2020 Junjiro R. Okajima
b912730e
AM
1581+#
1582+# This program is free software; you can redistribute it and/or modify
1583+# it under the terms of the GNU General Public License as published by
1584+# the Free Software Foundation; either version 2 of the License, or
1585+# (at your option) any later version.
1586+#
1587+# This program is distributed in the hope that it will be useful,
1588+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1589+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1590+# GNU General Public License for more details.
1591+#
1592+# You should have received a copy of the GNU General Public License
1593+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1594+
1595+Support for a branch who has its ->atomic_open()
1596+----------------------------------------------------------------------
1597+The filesystems who implement its ->atomic_open() are not majority. For
1598+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1599+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1600+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1601+sure whether all filesystems who have ->atomic_open() behave like this,
1602+but NFSv4 surely returns the error.
1603+
1604+In order to support ->atomic_open() for aufs, there are a few
1605+approaches.
1606+
1607+A. Introduce aufs_atomic_open()
1608+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1609+ branch fs.
1610+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1611+ an aufs user Pip Cet's approach
1612+ - calls aufs_create(), VFS finish_open() and notify_change().
1613+ - pass fake-mode to finish_open(), and then correct the mode by
1614+ notify_change().
1615+C. Extend aufs_open() to call branch fs's ->atomic_open()
1616+ - no aufs_atomic_open().
1617+ - aufs_lookup() registers the TID to an aufs internal object.
1618+ - aufs_create() does nothing when the matching TID is registered, but
1619+ registers the mode.
1620+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1621+ TID is registered.
1622+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1623+ credential
1624+ - no aufs_atomic_open().
1625+ - aufs_create() registers the TID to an internal object. this info
1626+ represents "this process created this file just now."
1627+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1628+ registered TID and re-try open() with superuser's credential.
1629+
1630+Pros and cons for each approach.
1631+
1632+A.
1633+ - straightforward but highly depends upon VFS internal.
1634+ - the atomic behavaiour is kept.
1635+ - some of parameters such as nameidata are hard to reproduce for
1636+ branch fs.
1637+ - large overhead.
1638+B.
1639+ - easy to implement.
1640+ - the atomic behavaiour is lost.
1641+C.
1642+ - the atomic behavaiour is kept.
1643+ - dirty and tricky.
1644+ - VFS checks whether the file is created correctly after calling
1645+ ->create(), which means this approach doesn't work.
1646+D.
1647+ - easy to implement.
1648+ - the atomic behavaiour is lost.
1649+ - to open a file with superuser's credential and give it to a user
1650+ process is a bad idea, since the file object keeps the credential
1651+ in it. It may affect LSM or something. This approach doesn't work
1652+ either.
1653+
1654+The approach A is ideal, but it hard to implement. So here is a
1655+variation of A, which is to be implemented.
1656+
1657+A-1. Introduce aufs_atomic_open()
1658+ - calls branch fs ->atomic_open() if exists. otherwise calls
1659+ vfs_create() and finish_open().
1660+ - the demerit is that the several checks after branch fs
1661+ ->atomic_open() are lost. in the ordinary case, the checks are
1662+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1663+ be implemented in aufs, but not all I am afraid.
53392da6 1664diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
eca34b5c 1665--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 1666+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2020-01-27 10:57:18.165538015 +0100
7e9cd9fe 1667@@ -0,0 +1,113 @@
53392da6 1668+
016522bc 1669+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1670+#
1671+# This program is free software; you can redistribute it and/or modify
1672+# it under the terms of the GNU General Public License as published by
1673+# the Free Software Foundation; either version 2 of the License, or
1674+# (at your option) any later version.
1675+#
1676+# This program is distributed in the hope that it will be useful,
1677+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1678+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1679+# GNU General Public License for more details.
1680+#
1681+# You should have received a copy of the GNU General Public License
523b37e3 1682+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1683+
1684+Lookup in a Branch
1685+----------------------------------------------------------------------
1686+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1687+lookup for branches as VFS does. It may be a heavy work. But almost all
1688+lookup operation in aufs is the simplest case, ie. lookup only an entry
1689+directly connected to its parent. Digging down the directory hierarchy
1690+is unnecessary. VFS has a function lookup_one_len() for that use, and
1691+aufs calls it.
1692+
1693+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1694+->d_revalidate(), also aufs forces the hardest revalidate tests for
1695+them.
1696+For d_revalidate, aufs implements three levels of revalidate tests. See
1697+"Revalidate Dentry and UDBA" in detail.
1698+
1699+
076b876e
AM
1700+Test Only the Highest One for the Directory Permission (dirperm1 option)
1701+----------------------------------------------------------------------
1702+Let's try case study.
1703+- aufs has two branches, upper readwrite and lower readonly.
1704+ /au = /rw + /ro
1705+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1706+- user invoked "chmod a+rx /au/dirA"
1707+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1708+ permission bits are set to world readable.
076b876e
AM
1709+- then "/au/dirA" becomes world readable?
1710+
1711+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1712+or it may be a natively readonly filesystem. If aufs respects the lower
1713+branch, it should not respond readdir request from other users. But user
1714+allowed it by chmod. Should really aufs rejects showing the entries
1715+under /ro/dirA?
1716+
7e9cd9fe
AM
1717+To be honest, I don't have a good solution for this case. So aufs
1718+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1719+users.
076b876e
AM
1720+When dirperm1 is specified, aufs checks only the highest one for the
1721+directory permission, and shows the entries. Otherwise, as usual, checks
1722+every dir existing on all branches and rejects the request.
1723+
1724+As a side effect, dirperm1 option improves the performance of aufs
1725+because the number of permission check is reduced when the number of
1726+branch is many.
1727+
1728+
53392da6
AM
1729+Revalidate Dentry and UDBA (User's Direct Branch Access)
1730+----------------------------------------------------------------------
1731+Generally VFS helpers re-validate a dentry as a part of lookup.
1732+0. digging down the directory hierarchy.
1733+1. lock the parent dir by its i_mutex.
1734+2. lookup the final (child) entry.
1735+3. revalidate it.
1736+4. call the actual operation (create, unlink, etc.)
1737+5. unlock the parent dir
1738+
1739+If the filesystem implements its ->d_revalidate() (step 3), then it is
1740+called. Actually aufs implements it and checks the dentry on a branch is
1741+still valid.
1742+But it is not enough. Because aufs has to release the lock for the
1743+parent dir on a branch at the end of ->lookup() (step 2) and
1744+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1745+held by VFS.
1746+If the file on a branch is changed directly, eg. bypassing aufs, after
1747+aufs released the lock, then the subsequent operation may cause
1748+something unpleasant result.
1749+
1750+This situation is a result of VFS architecture, ->lookup() and
1751+->d_revalidate() is separated. But I never say it is wrong. It is a good
1752+design from VFS's point of view. It is just not suitable for sub-VFS
1753+character in aufs.
1754+
1755+Aufs supports such case by three level of revalidation which is
1756+selectable by user.
1757+1. Simple Revalidate
1758+ Addition to the native flow in VFS's, confirm the child-parent
1759+ relationship on the branch just after locking the parent dir on the
1760+ branch in the "actual operation" (step 4). When this validation
1761+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1762+ checks the validation of the dentry on branches.
1763+2. Monitor Changes Internally by Inotify/Fsnotify
1764+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1765+ the dentry on the branch, and returns EBUSY if it finds different
1766+ dentry.
1767+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1768+ during it is in cache. When the event is notified, aufs registers a
1769+ function to kernel 'events' thread by schedule_work(). And the
1770+ function sets some special status to the cached aufs dentry and inode
1771+ private data. If they are not cached, then aufs has nothing to
1772+ do. When the same file is accessed through aufs (step 0-3) later,
1773+ aufs will detect the status and refresh all necessary data.
1774+ In this mode, aufs has to ignore the event which is fired by aufs
1775+ itself.
1776+3. No Extra Validation
1777+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1778+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1779+ aufs performance when system surely hide the aufs branches from user,
1780+ by over-mounting something (or another method).
1781diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
eca34b5c 1782--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 1783+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2020-01-27 10:57:18.165538015 +0100
7e9cd9fe 1784@@ -0,0 +1,74 @@
53392da6 1785+
016522bc 1786+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1787+#
1788+# This program is free software; you can redistribute it and/or modify
1789+# it under the terms of the GNU General Public License as published by
1790+# the Free Software Foundation; either version 2 of the License, or
1791+# (at your option) any later version.
1792+#
1793+# This program is distributed in the hope that it will be useful,
1794+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1795+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1796+# GNU General Public License for more details.
1797+#
1798+# You should have received a copy of the GNU General Public License
523b37e3 1799+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1800+
1801+Branch Manipulation
1802+
1803+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1804+and changing its permission/attribute, there are a lot of works to do.
1805+
1806+
1807+Add a Branch
1808+----------------------------------------------------------------------
1809+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1810+ mount, and its various attributes.
53392da6
AM
1811+o Initialize the xino file and whiteout bases if necessary.
1812+ See struct.txt.
1813+
1814+o Check the owner/group/mode of the directory
1815+ When the owner/group/mode of the adding directory differs from the
1816+ existing branch, aufs issues a warning because it may impose a
1817+ security risk.
1818+ For example, when a upper writable branch has a world writable empty
1819+ top directory, a malicious user can create any files on the writable
1820+ branch directly, like copy-up and modify manually. If something like
1821+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1822+ writable branch, and the writable branch is world-writable, then a
1823+ malicious guy may create /etc/passwd on the writable branch directly
1824+ and the infected file will be valid in aufs.
7e9cd9fe 1825+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1826+ producing a warning.
1827+
1828+
1829+Delete a Branch
1830+----------------------------------------------------------------------
1831+o Confirm the deleting branch is not busy
1832+ To be general, there is one merit to adopt "remount" interface to
1833+ manipulate branches. It is to discard caches. At deleting a branch,
1834+ aufs checks the still cached (and connected) dentries and inodes. If
1835+ there are any, then they are all in-use. An inode without its
1836+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1837+
1838+ For the cached one, aufs checks whether the same named entry exists on
1839+ other branches.
1840+ If the cached one is a directory, because aufs provides a merged view
1841+ to users, as long as one dir is left on any branch aufs can show the
1842+ dir to users. In this case, the branch can be removed from aufs.
1843+ Otherwise aufs rejects deleting the branch.
1844+
1845+ If any file on the deleting branch is opened by aufs, then aufs
1846+ rejects deleting.
1847+
1848+
1849+Modify the Permission of a Branch
1850+----------------------------------------------------------------------
1851+o Re-initialize or remove the xino file and whiteout bases if necessary.
1852+ See struct.txt.
1853+
1854+o rw --> ro: Confirm the modifying branch is not busy
1855+ Aufs rejects the request if any of these conditions are true.
1856+ - a file on the branch is mmap-ed.
1857+ - a regular file on the branch is opened for write and there is no
1858+ same named entry on the upper branch.
1859diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
eca34b5c 1860--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 1861+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2020-01-27 10:57:18.165538015 +0100
523b37e3 1862@@ -0,0 +1,64 @@
53392da6 1863+
016522bc 1864+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1865+#
1866+# This program is free software; you can redistribute it and/or modify
1867+# it under the terms of the GNU General Public License as published by
1868+# the Free Software Foundation; either version 2 of the License, or
1869+# (at your option) any later version.
1870+#
1871+# This program is distributed in the hope that it will be useful,
1872+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1873+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1874+# GNU General Public License for more details.
1875+#
1876+# You should have received a copy of the GNU General Public License
523b37e3 1877+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1878+
1879+Policies to Select One among Multiple Writable Branches
1880+----------------------------------------------------------------------
1881+When the number of writable branch is more than one, aufs has to decide
1882+the target branch for file creation or copy-up. By default, the highest
1883+writable branch which has the parent (or ancestor) dir of the target
1884+file is chosen (top-down-parent policy).
1885+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
1886+writable branch, for file creation several policies, round-robin,
1887+most-free-space, and other policies. For copy-up, top-down-parent,
1888+bottom-up-parent, bottom-up and others.
53392da6
AM
1889+
1890+As expected, the round-robin policy selects the branch in circular. When
1891+you have two writable branches and creates 10 new files, 5 files will be
1892+created for each branch. mkdir(2) systemcall is an exception. When you
1893+create 10 new directories, all will be created on the same branch.
1894+And the most-free-space policy selects the one which has most free
1895+space among the writable branches. The amount of free space will be
1896+checked by aufs internally, and users can specify its time interval.
1897+
1898+The policies for copy-up is more simple,
1899+top-down-parent is equivalent to the same named on in create policy,
1900+bottom-up-parent selects the writable branch where the parent dir
1901+exists and the nearest upper one from the copyup-source,
1902+bottom-up selects the nearest upper writable branch from the
1903+copyup-source, regardless the existence of the parent dir.
1904+
1905+There are some rules or exceptions to apply these policies.
1906+- If there is a readonly branch above the policy-selected branch and
1907+ the parent dir is marked as opaque (a variation of whiteout), or the
1908+ target (creating) file is whiteout-ed on the upper readonly branch,
1909+ then the result of the policy is ignored and the target file will be
1910+ created on the nearest upper writable branch than the readonly branch.
1911+- If there is a writable branch above the policy-selected branch and
1912+ the parent dir is marked as opaque or the target file is whiteouted
1913+ on the branch, then the result of the policy is ignored and the target
1914+ file will be created on the highest one among the upper writable
1915+ branches who has diropq or whiteout. In case of whiteout, aufs removes
1916+ it as usual.
1917+- link(2) and rename(2) systemcalls are exceptions in every policy.
1918+ They try selecting the branch where the source exists as possible
1919+ since copyup a large file will take long time. If it can't be,
1920+ ie. the branch where the source exists is readonly, then they will
1921+ follow the copyup policy.
1922+- There is an exception for rename(2) when the target exists.
1923+ If the rename target exists, aufs compares the index of the branches
1924+ where the source and the target exists and selects the higher
1925+ one. If the selected branch is readonly, then aufs follows the
1926+ copyup policy.
8b6a4947 1927diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
eca34b5c
AM
1928--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1929+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2019-07-11 15:42:14.458904362 +0200
8b6a4947
AM
1930@@ -0,0 +1,31 @@
1931+
1932+// to view this graph, run dot(1) command in GRAPHVIZ.
1933+
1934+digraph G {
1935+node [shape=box];
1936+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1937+
1938+node [shape=oval];
1939+
1940+aufs_rename -> whinfo [label="store/remove"];
1941+
1942+node [shape=oval];
1943+inode_list [label="h_inum list in branch\ncache"];
1944+
1945+node [shape=box];
1946+whinode [label="h_inum list file"];
1947+
1948+node [shape=oval];
1949+brmgmt [label="br_add/del/mod/umount"];
1950+
1951+brmgmt -> inode_list [label="create/remove"];
1952+brmgmt -> whinode [label="load/store"];
1953+
1954+inode_list -> whinode [style=dashed,dir=both];
1955+
1956+aufs_rename -> inode_list [label="add/del"];
1957+
1958+aufs_lookup -> inode_list [label="search"];
1959+
1960+aufs_lookup -> whinfo [label="load/remove"];
1961+}
1962diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
eca34b5c 1963--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 1964+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2020-01-27 10:57:18.165538015 +0100
8b6a4947
AM
1965@@ -0,0 +1,102 @@
1966+
016522bc 1967+# Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
1968+#
1969+# This program is free software; you can redistribute it and/or modify
1970+# it under the terms of the GNU General Public License as published by
1971+# the Free Software Foundation; either version 2 of the License, or
1972+# (at your option) any later version.
1973+#
1974+# This program is distributed in the hope that it will be useful,
1975+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1976+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1977+# GNU General Public License for more details.
1978+#
1979+# You should have received a copy of the GNU General Public License
1980+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1981+
1982+Special handling for renaming a directory (DIRREN)
1983+----------------------------------------------------------------------
1984+First, let's assume we have a simple usecase.
1985+
1986+- /u = /rw + /ro
1987+- /rw/dirA exists
1988+- /ro/dirA and /ro/dirA/file exist too
1989+- there is no dirB on both branches
1990+- a user issues rename("dirA", "dirB")
1991+
1992+Now, what should aufs behave against this rename(2)?
1993+There are a few possible cases.
1994+
1995+A. returns EROFS.
1996+ since dirA exists on a readonly branch which cannot be renamed.
1997+B. returns EXDEV.
1998+ it is possible to copy-up dirA (only the dir itself), but the child
1999+ entries ("file" in this case) should not be. it must be a bad
2000+ approach to copy-up recursively.
2001+C. returns a success.
2002+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
2003+ is a violation of aufs' policy.
2004+D. construct an extra information which indicates that /ro/dirA should
2005+ be handled as the name of dirB.
2006+ overlayfs has a similar feature called REDIRECT.
2007+
2008+Until now, aufs implements the case B only which returns EXDEV, and
2009+expects the userspace application behaves like mv(1) which tries
2010+issueing rename(2) recursively.
2011+
2012+A new aufs feature called DIRREN is introduced which implements the case
2013+D. There are several "extra information" added.
2014+
2015+1. detailed info per renamed directory
2016+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2017+2. the inode-number list of directories on a branch
2018+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
2019+
2020+The filename of "detailed info per directory" represents the lower
2021+branch, and its format is
2022+- a type of the branch id
2023+ one of these.
2024+ + uuid (not implemented yet)
2025+ + fsid
2026+ + dev
2027+- the inode-number of the branch root dir
2028+
2029+And it contains these info in a single regular file.
2030+- magic number
2031+- branch's inode-number of the logically renamed dir
2032+- the name of the before-renamed dir
2033+
2034+The "detailed info per directory" file is created in aufs rename(2), and
2035+loaded in any lookup.
2036+The info is considered in lookup for the matching case only. Here
2037+"matching" means that the root of branch (in the info filename) is same
2038+to the current looking-up branch. After looking-up the before-renamed
2039+name, the inode-number is compared. And the matched dentry is used.
2040+
2041+The "inode-number list of directories" is a regular file which contains
2042+simply the inode-numbers on the branch. The file is created or updated
2043+in removing the branch, and loaded in adding the branch. Its lifetime is
2044+equal to the branch.
2045+The list is refered in lookup, and when the current target inode is
2046+found in the list, the aufs tries loading the "detailed info per
2047+directory" and get the changed and valid name of the dir.
2048+
2049+Theoretically these "extra informaiton" may be able to be put into XATTR
2050+in the dir inode. But aufs doesn't choose this way because
2051+1. XATTR may not be supported by the branch (or its configuration)
2052+2. XATTR may have its size limit.
2053+3. XATTR may be less easy to convert than a regular file, when the
2054+ format of the info is changed in the future.
2055+At the same time, I agree that the regular file approach is much slower
2056+than XATTR approach. So, in the future, aufs may take the XATTR or other
2057+better approach.
2058+
2059+This DIRREN feature is enabled by aufs configuration, and is activated
2060+by a new mount option.
2061+
2062+For the more complicated case, there is a work with UDBA option, which
2063+is to dected the direct access to the branches (by-passing aufs) and to
2064+maintain the cashes in aufs. Since a single cached aufs dentry may
2065+contains two names, before- and after-rename, the name comparision in
2066+UDBA handler may not work correctly. In this case, the behaviour will be
2067+equivalen to udba=reval case.
076b876e 2068diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
eca34b5c 2069--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 2070+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2020-01-27 10:57:18.165538015 +0100
076b876e
AM
2071@@ -0,0 +1,120 @@
2072+
016522bc 2073+# Copyright (C) 2011-2020 Junjiro R. Okajima
076b876e
AM
2074+#
2075+# This program is free software; you can redistribute it and/or modify
2076+# it under the terms of the GNU General Public License as published by
2077+# the Free Software Foundation; either version 2 of the License, or
2078+# (at your option) any later version.
2079+#
2080+# This program is distributed in the hope that it will be useful,
2081+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2082+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2083+# GNU General Public License for more details.
2084+#
2085+# You should have received a copy of the GNU General Public License
2086+# along with this program; if not, write to the Free Software
2087+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2088+
2089+
2090+File-based Hierarchical Storage Management (FHSM)
2091+----------------------------------------------------------------------
2092+Hierarchical Storage Management (or HSM) is a well-known feature in the
2093+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 2094+writable branches, based upon the principle of "Colder, the Lower".
076b876e 2095+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 2096+that the position in the order of the stacked branches vertically.
076b876e
AM
2097+These multiple writable branches are prioritized, ie. the topmost one
2098+should be the fastest drive and be used heavily.
2099+
2100+o Characters in aufs FHSM story
2101+- aufs itself and a new branch attribute.
2102+- a new ioctl interface to move-down and to establish a connection with
2103+ the daemon ("move-down" is a converse of "copy-up").
2104+- userspace tool and daemon.
2105+
2106+The userspace daemon establishes a connection with aufs and waits for
2107+the notification. The notified information is very similar to struct
2108+statfs containing the number of consumed blocks and inodes.
2109+When the consumed blocks/inodes of a branch exceeds the user-specified
2110+upper watermark, the daemon activates its move-down process until the
2111+consumed blocks/inodes reaches the user-specified lower watermark.
2112+
2113+The actual move-down is done by aufs based upon the request from
2114+user-space since we need to maintain the inode number and the internal
2115+pointer arrays in aufs.
2116+
2117+Currently aufs FHSM handles the regular files only. Additionally they
2118+must not be hard-linked nor pseudo-linked.
2119+
2120+
2121+o Cowork of aufs and the user-space daemon
2122+ During the userspace daemon established the connection, aufs sends a
2123+ small notification to it whenever aufs writes something into the
2124+ writable branch. But it may cost high since aufs issues statfs(2)
2125+ internally. So user can specify a new option to cache the
2126+ info. Actually the notification is controlled by these factors.
2127+ + the specified cache time.
2128+ + classified as "force" by aufs internally.
2129+ Until the specified time expires, aufs doesn't send the info
2130+ except the forced cases. When aufs decide forcing, the info is always
2131+ notified to userspace.
2132+ For example, the number of free inodes is generally large enough and
2133+ the shortage of it happens rarely. So aufs doesn't force the
2134+ notification when creating a new file, directory and others. This is
2135+ the typical case which aufs doesn't force.
2136+ When aufs writes the actual filedata and the files consumes any of new
2137+ blocks, the aufs forces notifying.
2138+
2139+
2140+o Interfaces in aufs
2141+- New branch attribute.
2142+ + fhsm
2143+ Specifies that the branch is managed by FHSM feature. In other word,
2144+ participant in the FHSM.
2145+ When nofhsm is set to the branch, it will not be the source/target
2146+ branch of the move-down operation. This attribute is set
2147+ independently from coo and moo attributes, and if you want full
2148+ FHSM, you should specify them as well.
2149+- New mount option.
2150+ + fhsm_sec
2151+ Specifies a second to suppress many less important info to be
2152+ notified.
2153+- New ioctl.
2154+ + AUFS_CTL_FHSM_FD
2155+ create a new file descriptor which userspace can read the notification
2156+ (a subset of struct statfs) from aufs.
2157+- Module parameter 'brs'
2158+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2159+ be set.
2160+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2161+ When there are two or more branches with fhsm attributes,
2162+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2163+ terminates it. As a result of remounting and branch-manipulation, the
2164+ number of branches with fhsm attribute can be one. In this case,
2165+ /sbin/mount.aufs will terminate the user-space daemon.
2166+
2167+
2168+Finally the operation is done as these steps in kernel-space.
2169+- make sure that,
2170+ + no one else is using the file.
2171+ + the file is not hard-linked.
2172+ + the file is not pseudo-linked.
2173+ + the file is a regular file.
2174+ + the parent dir is not opaqued.
2175+- find the target writable branch.
2176+- make sure the file is not whiteout-ed by the upper (than the target)
2177+ branch.
2178+- make the parent dir on the target branch.
2179+- mutex lock the inode on the branch.
2180+- unlink the whiteout on the target branch (if exists).
2181+- lookup and create the whiteout-ed temporary name on the target branch.
2182+- copy the file as the whiteout-ed temporary name on the target branch.
2183+- rename the whiteout-ed temporary name to the original name.
2184+- unlink the file on the source branch.
2185+- maintain the internal pointer array and the external inode number
2186+ table (XINO).
2187+- maintain the timestamps and other attributes of the parent dir and the
2188+ file.
2189+
2190+And of course, in every step, an error may happen. So the operation
2191+should restore the original file state after an error happens.
53392da6 2192diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
eca34b5c 2193--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 2194+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2020-01-27 10:57:18.165538015 +0100
b912730e 2195@@ -0,0 +1,72 @@
53392da6 2196+
016522bc 2197+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
2198+#
2199+# This program is free software; you can redistribute it and/or modify
2200+# it under the terms of the GNU General Public License as published by
2201+# the Free Software Foundation; either version 2 of the License, or
2202+# (at your option) any later version.
2203+#
2204+# This program is distributed in the hope that it will be useful,
2205+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2206+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2207+# GNU General Public License for more details.
2208+#
2209+# You should have received a copy of the GNU General Public License
523b37e3 2210+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2211+
2212+mmap(2) -- File Memory Mapping
2213+----------------------------------------------------------------------
2214+In aufs, the file-mapped pages are handled by a branch fs directly, no
2215+interaction with aufs. It means aufs_mmap() calls the branch fs's
2216+->mmap().
2217+This approach is simple and good, but there is one problem.
7e9cd9fe 2218+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2219+device and inode number), and the printed path will be the path on the
2220+branch fs's instead of virtual aufs's.
2221+This is not a problem in most cases, but some utilities lsof(1) (and its
2222+user) may expect the path on aufs.
2223+
2224+To address this issue, aufs adds a new member called vm_prfile in struct
2225+vm_area_struct (and struct vm_region). The original vm_file points to
2226+the file on the branch fs in order to handle everything correctly as
2227+usual. The new vm_prfile points to a virtual file in aufs, and the
2228+show-functions in procfs refers to vm_prfile if it is set.
2229+Also we need to maintain several other places where touching vm_file
2230+such like
2231+- fork()/clone() copies vma and the reference count of vm_file is
2232+ incremented.
2233+- merging vma maintains the ref count too.
2234+
7e9cd9fe 2235+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2236+leaves all behaviour around f_mapping unchanged. This is surely an
2237+advantage.
2238+Actually aufs had adopted another complicated approach which calls
2239+generic_file_mmap() and handles struct vm_operations_struct. In this
2240+approach, aufs met a hard problem and I could not solve it without
2241+switching the approach.
b912730e
AM
2242+
2243+There may be one more another approach which is
2244+- bind-mount the branch-root onto the aufs-root internally
2245+- grab the new vfsmount (ie. struct mount)
2246+- lazy-umount the branch-root internally
2247+- in open(2) the aufs-file, open the branch-file with the hidden
2248+ vfsmount (instead of the original branch's vfsmount)
2249+- ideally this "bind-mount and lazy-umount" should be done atomically,
2250+ but it may be possible from userspace by the mount helper.
2251+
2252+Adding the internal hidden vfsmount and using it in opening a file, the
2253+file path under /proc will be printed correctly. This approach looks
2254+smarter, but is not possible I am afraid.
2255+- aufs-root may be bind-mount later. when it happens, another hidden
2256+ vfsmount will be required.
2257+- it is hard to get the chance to bind-mount and lazy-umount
2258+ + in kernel-space, FS can have vfsmount in open(2) via
2259+ file->f_path, and aufs can know its vfsmount. But several locks are
2260+ already acquired, and if aufs tries to bind-mount and lazy-umount
2261+ here, then it may cause a deadlock.
2262+ + in user-space, bind-mount doesn't invoke the mount helper.
2263+- since /proc shows dev and ino, aufs has to give vma these info. it
2264+ means a new member vm_prinode will be necessary. this is essentially
2265+ equivalent to vm_prfile described above.
2266+
2267+I have to give up this "looks-smater" approach.
c1595e42 2268diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
eca34b5c 2269--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 2270+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2020-01-27 10:57:18.165538015 +0100
c1595e42
JR
2271@@ -0,0 +1,96 @@
2272+
016522bc 2273+# Copyright (C) 2014-2020 Junjiro R. Okajima
c1595e42
JR
2274+#
2275+# This program is free software; you can redistribute it and/or modify
2276+# it under the terms of the GNU General Public License as published by
2277+# the Free Software Foundation; either version 2 of the License, or
2278+# (at your option) any later version.
2279+#
2280+# This program is distributed in the hope that it will be useful,
2281+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2282+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2283+# GNU General Public License for more details.
2284+#
2285+# You should have received a copy of the GNU General Public License
2286+# along with this program; if not, write to the Free Software
2287+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2288+
2289+
2290+Listing XATTR/EA and getting the value
2291+----------------------------------------------------------------------
2292+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2293+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2294+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2295+information. But for the directories, aufs considers all the same named
2296+entries on the lower branches. Which means, if one of the lower entry
2297+rejects readdir call, then aufs returns an error even if the topmost
2298+entry allows it. This behaviour is necessary to respect the branch fs's
2299+security, but can make users confused since the user-visible standard
2300+attributes don't match the behaviour.
2301+To address this issue, aufs has a mount option called dirperm1 which
2302+checks the permission for the topmost entry only, and ignores the lower
2303+entry's permission.
2304+
2305+A similar issue can happen around XATTR.
2306+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2307+always set. Otherwise these very unpleasant situation would happen.
2308+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2309+- users may not be able to remove or reset the XATTR forever,
2310+
2311+
2312+XATTR/EA support in the internal (copy,move)-(up,down)
2313+----------------------------------------------------------------------
7e9cd9fe 2314+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2315+- "security" for LSM and capability.
2316+- "system" for posix ACL, 'acl' mount option is required for the branch
2317+ fs generally.
2318+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2319+- "user" for userspace, 'user_xattr' mount option is required for the
2320+ branch fs generally.
2321+
2322+Moreover there are some other categories. Aufs handles these rather
2323+unpopular categories as the ordinary ones, ie. there is no special
2324+condition nor exception.
2325+
2326+In copy-up, the support for XATTR on the dst branch may differ from the
2327+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2328+the original user operation which triggered the copy-up will fail. It
2329+can happen that even all copy-up will fail.
c1595e42
JR
2330+When both of src and dst branches support XATTR and if an error occurs
2331+during copying XATTR, then the copy-up should fail obviously. That is a
2332+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2333+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2334+For example, the src branch supports ACL but the dst branch doesn't
2335+because the dst branch may natively un-support it or temporary
2336+un-support it due to "noacl" mount option. Of course, the dst branch fs
2337+may NOT return an error even if the XATTR is not supported. It is
2338+totally up to the branch fs.
2339+
2340+Anyway when the aufs internal copy-up gets an error from the dst branch
2341+fs, then aufs tries removing the just copied entry and returns the error
2342+to the userspace. The worst case of this situation will be all copy-up
2343+will fail.
2344+
2345+For the copy-up operation, there two basic approaches.
2346+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2347+ error unconditionally if it happens.
c1595e42
JR
2348+- copy all XATTR, and ignore the error on the specified category only.
2349+
2350+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2351+chooses the latter approach and introduces some new branch attributes,
2352+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2353+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2354+convenient, "icex" is also provided which means all "icex*" attributes
2355+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2356+
2357+The meaning of these attributes is to ignore the error from setting
2358+XATTR on that branch.
2359+Note that aufs tries copying all XATTR unconditionally, and ignores the
2360+error from the dst branch according to the specified attributes.
2361+
2362+Some XATTR may have its default value. The default value may come from
2363+the parent dir or the environment. If the default value is set at the
2364+file creating-time, it will be overwritten by copy-up.
2365+Some contradiction may happen I am afraid.
2366+Do we need another attribute to stop copying XATTR? I am unsure. For
2367+now, aufs implements the branch attributes to ignore the error.
53392da6 2368diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
eca34b5c 2369--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 2370+++ linux/Documentation/filesystems/aufs/design/07export.txt 2020-01-27 10:57:18.165538015 +0100
523b37e3 2371@@ -0,0 +1,58 @@
53392da6 2372+
016522bc 2373+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
2374+#
2375+# This program is free software; you can redistribute it and/or modify
2376+# it under the terms of the GNU General Public License as published by
2377+# the Free Software Foundation; either version 2 of the License, or
2378+# (at your option) any later version.
2379+#
2380+# This program is distributed in the hope that it will be useful,
2381+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2382+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2383+# GNU General Public License for more details.
2384+#
2385+# You should have received a copy of the GNU General Public License
523b37e3 2386+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2387+
2388+Export Aufs via NFS
2389+----------------------------------------------------------------------
2390+Here is an approach.
2391+- like xino/xib, add a new file 'xigen' which stores aufs inode
2392+ generation.
2393+- iget_locked(): initialize aufs inode generation for a new inode, and
2394+ store it in xigen file.
2395+- destroy_inode(): increment aufs inode generation and store it in xigen
2396+ file. it is necessary even if it is not unlinked, because any data of
2397+ inode may be changed by UDBA.
2398+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2399+ build file handle by
2400+ + branch id (4 bytes)
2401+ + superblock generation (4 bytes)
2402+ + inode number (4 or 8 bytes)
2403+ + parent dir inode number (4 or 8 bytes)
2404+ + inode generation (4 bytes))
2405+ + return value of exportfs_encode_fh() for the parent on a branch (4
2406+ bytes)
2407+ + file handle for a branch (by exportfs_encode_fh())
2408+- fh_to_dentry():
2409+ + find the index of a branch from its id in handle, and check it is
2410+ still exist in aufs.
2411+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2412+ + 2nd level: if not found in cache, get the parent inode number from
2413+ the handle and search it in cache. and then open the found parent
2414+ dir, find the matching inode number by vfs_readdir() and get its
2415+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2416+ + 3rd level: if the parent dir is not cached, call
2417+ exportfs_decode_fh() for a branch and get the parent on a branch,
2418+ build a pathname of it, convert it a pathname in aufs, call
2419+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2420+ the 2nd level.
2421+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2422+ for every branch, but not itself. to get this, (currently) aufs
2423+ searches in current->nsproxy->mnt_ns list. it may not be a good
2424+ idea, but I didn't get other approach.
2425+ + test the generation of the gotten inode.
2426+- every inode operation: they may get EBUSY due to UDBA. in this case,
2427+ convert it into ESTALE for NFSD.
2428+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2429+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2430diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
eca34b5c 2431--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 2432+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2020-01-27 10:57:18.165538015 +0100
523b37e3 2433@@ -0,0 +1,52 @@
53392da6 2434+
016522bc 2435+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
2436+#
2437+# This program is free software; you can redistribute it and/or modify
2438+# it under the terms of the GNU General Public License as published by
2439+# the Free Software Foundation; either version 2 of the License, or
2440+# (at your option) any later version.
2441+#
2442+# This program is distributed in the hope that it will be useful,
2443+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2444+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2445+# GNU General Public License for more details.
2446+#
2447+# You should have received a copy of the GNU General Public License
523b37e3 2448+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2449+
2450+Show Whiteout Mode (shwh)
2451+----------------------------------------------------------------------
2452+Generally aufs hides the name of whiteouts. But in some cases, to show
2453+them is very useful for users. For instance, creating a new middle layer
2454+(branch) by merging existing layers.
2455+
2456+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2457+When you have three branches,
2458+- Bottom: 'system', squashfs (underlying base system), read-only
2459+- Middle: 'mods', squashfs, read-only
2460+- Top: 'overlay', ram (tmpfs), read-write
2461+
2462+The top layer is loaded at boot time and saved at shutdown, to preserve
2463+the changes made to the system during the session.
2464+When larger changes have been made, or smaller changes have accumulated,
2465+the size of the saved top layer data grows. At this point, it would be
2466+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2467+and rewrite the 'mods' squashfs, clearing the top layer and thus
2468+restoring save and load speed.
2469+
2470+This merging is simplified by the use of another aufs mount, of just the
2471+two overlay branches using the 'shwh' option.
2472+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2473+ aufs /livesys/merge_union
2474+
2475+A merged view of these two branches is then available at
2476+/livesys/merge_union, and the new feature is that the whiteouts are
2477+visible!
2478+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2479+writing to all branches. Also the default mode for all branches is 'ro'.
2480+It is now possible to save the combined contents of the two overlay
2481+branches to a new squashfs, e.g.:
2482+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2483+
2484+This new squashfs archive can be stored on the boot device and the
2485+initramfs will use it to replace the old one at the next boot.
2486diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
eca34b5c 2487--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
016522bc 2488+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2020-01-27 10:57:18.165538015 +0100
7e9cd9fe 2489@@ -0,0 +1,47 @@
53392da6 2490+
016522bc 2491+# Copyright (C) 2010-2020 Junjiro R. Okajima
53392da6
AM
2492+#
2493+# This program is free software; you can redistribute it and/or modify
2494+# it under the terms of the GNU General Public License as published by
2495+# the Free Software Foundation; either version 2 of the License, or
2496+# (at your option) any later version.
2497+#
2498+# This program is distributed in the hope that it will be useful,
2499+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2500+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2501+# GNU General Public License for more details.
2502+#
2503+# You should have received a copy of the GNU General Public License
523b37e3 2504+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2505+
2506+Dynamically customizable FS operations
2507+----------------------------------------------------------------------
2508+Generally FS operations (struct inode_operations, struct
2509+address_space_operations, struct file_operations, etc.) are defined as
2510+"static const", but it never means that FS have only one set of
2511+operation. Some FS have multiple sets of them. For instance, ext2 has
2512+three sets, one for XIP, for NOBH, and for normal.
2513+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2514+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2515+VFS acts differently if a function (member in the struct) is set or
2516+not. It means aufs should have several sets of operations and select one
2517+among them according to the branch FS definition.
2518+
7e9cd9fe 2519+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2520+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2521+dummy direct_IO function for struct address_space_operations, but it may
2522+not be set to the address_space_operations actually. When the branch FS
2523+doesn't have it, aufs doesn't set it to its address_space_operations
2524+while the function definition itself is still alive. So the behaviour
2525+itself will not change, and it will return an error when direct_IO is
2526+not set.
53392da6
AM
2527+
2528+The lifetime of these dynamically generated operation object is
2529+maintained by aufs branch object. When the branch is removed from aufs,
2530+the reference counter of the object is decremented. When it reaches
2531+zero, the dynamically generated operation object will be freed.
2532+
7e9cd9fe
AM
2533+This approach is designed to support AIO (io_submit), Direct I/O and
2534+XIP (DAX) mainly.
2535+Currently this approach is applied to address_space_operations for
2536+regular files only.
53392da6 2537diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
eca34b5c 2538--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
43982f53
AM
2539+++ linux/Documentation/filesystems/aufs/README 2020-01-23 09:59:36.748660743 +0100
2540@@ -0,0 +1,401 @@
53392da6 2541+
fbc438ed 2542+Aufs5 -- advanced multi layered unification filesystem version 5.x
53392da6
AM
2543+http://aufs.sf.net
2544+Junjiro R. Okajima
2545+
2546+
2547+0. Introduction
2548+----------------------------------------
2549+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2550+Unionfs Version 1.x series. Adding many original ideas, approaches,
fbc438ed 2551+improvements and implementations, it became totally different from
53392da6 2552+Unionfs while keeping the basic features.
fbc438ed 2553+Later, Unionfs Version 2.x series began taking some of the same
53392da6 2554+approaches to aufs1's.
fbc438ed 2555+Unionfs was being developed by Professor Erez Zadok at Stony Brook
53392da6
AM
2556+University and his team.
2557+
fbc438ed
JR
2558+Aufs5 supports linux-v5.0 and later, If you want older kernel version
2559+support,
2560+- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2561+- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2562+- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2563+ or aufs1 from CVS on SourceForge.
53392da6
AM
2564+
2565+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2566+ According to Christoph Hellwig, linux rejects all union-type
2567+ filesystems but UnionMount.
53392da6
AM
2568+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2569+
38d290e6
JR
2570+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2571+ UnionMount, and he pointed out an issue around a directory mutex
2572+ lock and aufs addressed it. But it is still unsure whether aufs will
2573+ be merged (or any other union solution).
076b876e 2574+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2575+
53392da6
AM
2576+
2577+1. Features
2578+----------------------------------------
2579+- unite several directories into a single virtual filesystem. The member
2580+ directory is called as a branch.
2581+- you can specify the permission flags to the branch, which are 'readonly',
2582+ 'readwrite' and 'whiteout-able.'
2583+- by upper writable branch, internal copyup and whiteout, files/dirs on
2584+ readonly branch are modifiable logically.
2585+- dynamic branch manipulation, add, del.
2586+- etc...
2587+
7e9cd9fe
AM
2588+Also there are many enhancements in aufs, such as:
2589+- test only the highest one for the directory permission (dirperm1)
2590+- copyup on open (coo=)
2591+- 'move' policy for copy-up between two writable branches, after
2592+ checking free space.
2593+- xattr, acl
53392da6
AM
2594+- readdir(3) in userspace.
2595+- keep inode number by external inode number table
2596+- keep the timestamps of file/dir in internal copyup operation
2597+- seekable directory, supporting NFS readdir.
2598+- whiteout is hardlinked in order to reduce the consumption of inodes
2599+ on branch
2600+- do not copyup, nor create a whiteout when it is unnecessary
2601+- revert a single systemcall when an error occurs in aufs
2602+- remount interface instead of ioctl
2603+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2604+- loopback mounted filesystem as a branch
2605+- kernel thread for removing the dir who has a plenty of whiteouts
2606+- support copyup sparse file (a file which has a 'hole' in it)
2607+- default permission flags for branches
2608+- selectable permission flags for ro branch, whether whiteout can
2609+ exist or not
2610+- export via NFS.
2611+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2612+- support multiple writable branches, some policies to select one
2613+ among multiple writable branches.
2614+- a new semantics for link(2) and rename(2) to support multiple
2615+ writable branches.
2616+- no glibc changes are required.
2617+- pseudo hardlink (hardlink over branches)
2618+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2619+ including NFS or remote filesystem branch.
2620+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2621+- and more...
2622+
fbc438ed 2623+Currently these features are dropped temporary from aufs5.
53392da6 2624+See design/08plan.txt in detail.
53392da6
AM
2625+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2626+ (robr)
2627+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2628+
2629+Features or just an idea in the future (see also design/*.txt),
2630+- reorder the branch index without del/re-add.
2631+- permanent xino files for NFSD
2632+- an option for refreshing the opened files after add/del branches
53392da6
AM
2633+- light version, without branch manipulation. (unnecessary?)
2634+- copyup in userspace
2635+- inotify in userspace
2636+- readv/writev
53392da6
AM
2637+
2638+
2639+2. Download
2640+----------------------------------------
fbc438ed
JR
2641+There are three GIT trees for aufs5, aufs5-linux.git,
2642+aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
1e00d052 2643+"aufs-util.git."
fbc438ed
JR
2644+While the aufs-util is always necessary, you need either of aufs5-linux
2645+or aufs5-standalone.
1e00d052 2646+
fbc438ed 2647+The aufs5-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2648+git://git.kernel.org/.../torvalds/linux.git.
2649+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
fbc438ed 2650+build aufs5 as an external kernel module.
2000de60 2651+Several extra patches are not included in this tree. Only
fbc438ed 2652+aufs5-standalone tree contains them. They are described in the later
2000de60 2653+section "Configuration and Compilation."
1e00d052 2654+
fbc438ed 2655+On the other hand, the aufs5-standalone tree has only aufs source files
53392da6 2656+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2657+But you need to apply all aufs patches manually.
53392da6 2658+
fbc438ed
JR
2659+You will find GIT branches whose name is in form of "aufs5.x" where "x"
2660+represents the linux kernel version, "linux-5.x". For instance,
2661+"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2662+"aufs5.x-rcN" branch.
1e00d052 2663+
fbc438ed 2664+o aufs5-linux tree
1e00d052 2665+$ git clone --reference /your/linux/git/tree \
fbc438ed 2666+ git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
1e00d052 2667+- if you don't have linux GIT tree, then remove "--reference ..."
fbc438ed
JR
2668+$ cd aufs5-linux.git
2669+$ git checkout origin/aufs5.0
53392da6 2670+
2000de60
JR
2671+Or You may want to directly git-pull aufs into your linux GIT tree, and
2672+leave the patch-work to GIT.
2673+$ cd /your/linux/git/tree
fbc438ed
JR
2674+$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2675+$ git fetch aufs5
2676+$ git checkout -b my5.0 v5.0
5527c038 2677+$ (add your local change...)
fbc438ed
JR
2678+$ git pull aufs5 aufs5.0
2679+- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2000de60 2680+- you may need to solve some conflicts between your_changes and
fbc438ed
JR
2681+ aufs5.0. in this case, git-rerere is recommended so that you can
2682+ solve the similar conflicts automatically when you upgrade to 5.1 or
2000de60
JR
2683+ later in the future.
2684+
fbc438ed
JR
2685+o aufs5-standalone tree
2686+$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2687+$ cd aufs5-standalone.git
2688+$ git checkout origin/aufs5.0
53392da6
AM
2689+
2690+o aufs-util tree
5527c038
JR
2691+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2692+- note that the public aufs-util.git is on SourceForge instead of
2693+ GitHUB.
53392da6 2694+$ cd aufs-util.git
fbc438ed 2695+$ git checkout origin/aufs5.0
53392da6 2696+
fbc438ed
JR
2697+Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2698+The minor version number, 'x' in '5.x', of aufs may not always
9dbd164d
AM
2699+follow the minor version number of the kernel.
2700+Because changes in the kernel that cause the use of a new
2701+minor version number do not always require changes to aufs-util.
2702+
2703+Since aufs-util has its own minor version number, you may not be
2704+able to find a GIT branch in aufs-util for your kernel's
2705+exact minor version number.
2706+In this case, you should git-checkout the branch for the
53392da6 2707+nearest lower number.
9dbd164d
AM
2708+
2709+For (an unreleased) example:
fbc438ed
JR
2710+If you are using "linux-5.10" and the "aufs5.10" branch
2711+does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
9dbd164d
AM
2712+or something numerically smaller is the branch for your kernel.
2713+
53392da6
AM
2714+Also you can view all branches by
2715+ $ git branch -a
2716+
2717+
2718+3. Configuration and Compilation
2719+----------------------------------------
2720+Make sure you have git-checkout'ed the correct branch.
2721+
fbc438ed 2722+For aufs5-linux tree,
c06a8ce3 2723+- enable CONFIG_AUFS_FS.
1e00d052
AM
2724+- set other aufs configurations if necessary.
2725+
fbc438ed 2726+For aufs5-standalone tree,
53392da6
AM
2727+There are several ways to build.
2728+
2729+1.
fbc438ed
JR
2730+- apply ./aufs5-kbuild.patch to your kernel source files.
2731+- apply ./aufs5-base.patch too.
2732+- apply ./aufs5-mmap.patch too.
2733+- apply ./aufs5-standalone.patch too, if you have a plan to set
2734+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
537831f9
AM
2735+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2736+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2737+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2738+ =m or =y.
2739+- and build your kernel as usual.
2740+- install the built kernel.
7eafdf33
AM
2741+- install the header files too by "make headers_install" to the
2742+ directory where you specify. By default, it is $PWD/usr.
b4510431 2743+ "make help" shows a brief note for headers_install.
53392da6
AM
2744+- and reboot your system.
2745+
2746+2.
2747+- module only (CONFIG_AUFS_FS=m).
fbc438ed
JR
2748+- apply ./aufs5-base.patch to your kernel source files.
2749+- apply ./aufs5-mmap.patch too.
2750+- apply ./aufs5-standalone.patch too.
53392da6
AM
2751+- build your kernel, don't forget "make headers_install", and reboot.
2752+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2753+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2754+ every aufs configurations.
2755+- build the module by simple "make".
2756+- you can specify ${KDIR} make variable which points to your kernel
2757+ source tree.
2758+- install the files
2759+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2760+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2761+ + run "make install_headers" (instead of headers_install) to install
2762+ the modified aufs header file (you can specify DESTDIR which is
2763+ available in aufs standalone version's Makefile only), or copy
2764+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2765+ you like manually. By default, the target directory is $PWD/usr.
fbc438ed 2766+- no need to apply aufs5-kbuild.patch, nor copying source files to your
53392da6
AM
2767+ kernel source tree.
2768+
b4510431 2769+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2770+ as well as "make headers_install" in the kernel source tree.
2771+ headers_install is subject to be forgotten, but it is essentially
2772+ necessary, not only for building aufs-util.
2773+ You may not meet problems without headers_install in some older
2774+ version though.
2775+
2776+And then,
2777+- read README in aufs-util, build and install it
9dbd164d
AM
2778+- note that your distribution may contain an obsoleted version of
2779+ aufs_type.h in /usr/include/linux or something. When you build aufs
2780+ utilities, make sure that your compiler refers the correct aufs header
2781+ file which is built by "make headers_install."
53392da6
AM
2782+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2783+ then run "make install_ulib" too. And refer to the aufs manual in
2784+ detail.
2785+
fbc438ed 2786+There several other patches in aufs5-standalone.git. They are all
38d290e6 2787+optional. When you meet some problems, they will help you.
fbc438ed 2788+- aufs5-loopback.patch
38d290e6
JR
2789+ Supports a nested loopback mount in a branch-fs. This patch is
2790+ unnecessary until aufs produces a message like "you may want to try
2791+ another patch for loopback file".
83b672a5
AM
2792+- proc_mounts.patch
2793+ When there are many mountpoints and many mount(2)/umount(2) are
2794+ running, then /proc/mounts may not show the all mountpoints. This
2795+ patch makes /proc/mounts always show the full mountpoints list.
2796+ If you don't want to apply this patch and meet such problem, then you
2797+ need to increase the value of 'ProcMounts_Times' make-variable in
2798+ aufs-util.git as a second best solution.
38d290e6
JR
2799+- vfs-ino.patch
2800+ Modifies a system global kernel internal function get_next_ino() in
2801+ order to stop assigning 0 for an inode-number. Not directly related to
2802+ aufs, but recommended generally.
2803+- tmpfs-idr.patch
2804+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2805+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2806+ duplication of inode number, which is important for backup tools and
2807+ other utilities. When you find aufs XINO files for tmpfs branch
2808+ growing too much, try this patch.
be52b249
AM
2809+- lockdep-debug.patch
2810+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2811+ also a caller of VFS functions for branch filesystems, subclassing of
2812+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2813+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2814+ need to apply this debug patch to expand several constant values.
43982f53
AM
2815+ If you don't know what LOCKDEP is, then you don't have apply this
2816+ patch.
38d290e6 2817+
53392da6
AM
2818+
2819+4. Usage
2820+----------------------------------------
2821+At first, make sure aufs-util are installed, and please read the aufs
2822+manual, aufs.5 in aufs-util.git tree.
2823+$ man -l aufs.5
2824+
2825+And then,
2826+$ mkdir /tmp/rw /tmp/aufs
2827+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2828+
2829+Here is another example. The result is equivalent.
2830+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2831+ Or
2832+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2833+# mount -o remount,append:${HOME} /tmp/aufs
2834+
2835+Then, you can see whole tree of your home dir through /tmp/aufs. If
2836+you modify a file under /tmp/aufs, the one on your home directory is
2837+not affected, instead the same named file will be newly created under
2838+/tmp/rw. And all of your modification to a file will be applied to
2839+the one under /tmp/rw. This is called the file based Copy on Write
2840+(COW) method.
2841+Aufs mount options are described in aufs.5.
2842+If you run chroot or something and make your aufs as a root directory,
2843+then you need to customize the shutdown script. See the aufs manual in
2844+detail.
2845+
2846+Additionally, there are some sample usages of aufs which are a
2847+diskless system with network booting, and LiveCD over NFS.
2848+See sample dir in CVS tree on SourceForge.
2849+
2850+
2851+5. Contact
2852+----------------------------------------
2853+When you have any problems or strange behaviour in aufs, please let me
2854+know with:
2855+- /proc/mounts (instead of the output of mount(8))
2856+- /sys/module/aufs/*
2857+- /sys/fs/aufs/* (if you have them)
2858+- /debug/aufs/* (if you have them)
2859+- linux kernel version
2860+ if your kernel is not plain, for example modified by distributor,
2861+ the url where i can download its source is necessary too.
2862+- aufs version which was printed at loading the module or booting the
2863+ system, instead of the date you downloaded.
2864+- configuration (define/undefine CONFIG_AUFS_xxx)
2865+- kernel configuration or /proc/config.gz (if you have it)
43982f53 2866+- LSM (linux security module, if you are using)
53392da6
AM
2867+- behaviour which you think to be incorrect
2868+- actual operation, reproducible one is better
2869+- mailto: aufs-users at lists.sourceforge.net
2870+
2871+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2872+and Feature Requests) on SourceForge. Please join and write to
2873+aufs-users ML.
2874+
2875+
2876+6. Acknowledgements
2877+----------------------------------------
2878+Thanks to everyone who have tried and are using aufs, whoever
2879+have reported a bug or any feedback.
2880+
2881+Especially donators:
2882+Tomas Matejicek(slax.org) made a donation (much more than once).
2883+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2884+ scripts) is making "doubling" donations.
2885+ Unfortunately I cannot list all of the donators, but I really
b4510431 2886+ appreciate.
53392da6
AM
2887+ It ends Aug 2010, but the ordinary donation URL is still available.
2888+ <http://sourceforge.net/donate/index.php?group_id=167503>
2889+Dai Itasaka made a donation (2007/8).
2890+Chuck Smith made a donation (2008/4, 10 and 12).
2891+Henk Schoneveld made a donation (2008/9).
2892+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2893+Francois Dupoux made a donation (2008/11).
2894+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2895+ aufs2 GIT tree (2009/2).
2896+William Grant made a donation (2009/3).
2897+Patrick Lane made a donation (2009/4).
2898+The Mail Archive (mail-archive.com) made donations (2009/5).
2899+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2900+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2901+Pavel Pronskiy made a donation (2011/2).
2902+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2903+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
2904+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2905+11).
1e00d052 2906+Sam Liddicott made a donation (2011/9).
86dc4139
AM
2907+Era Scarecrow made a donation (2013/4).
2908+Bor Ratajc made a donation (2013/4).
2909+Alessandro Gorreta made a donation (2013/4).
2910+POIRETTE Marc made a donation (2013/4).
2911+Alessandro Gorreta made a donation (2013/4).
2912+lauri kasvandik made a donation (2013/5).
392086de 2913+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
2914+The Parted Magic Project made a donation (2013/9 and 11).
2915+Pavel Barta made a donation (2013/10).
38d290e6 2916+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 2917+James B made a donation (2014/7 and 2015/7).
076b876e 2918+Stefano Di Biase made a donation (2014/8).
2000de60 2919+Daniel Epellei made a donation (2015/1).
c4adf169 2920+OmegaPhil made a donation (2016/1, 2018/4).
5afbbe0d 2921+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 2922+James Burry made a donation (2016/12).
acd2b654 2923+Carsten Rose made a donation (2018/9).
9f237c51 2924+Porteus Kiosk made a donation (2018/10).
53392da6
AM
2925+
2926+Thank you very much.
2927+Donations are always, including future donations, very important and
2928+helpful for me to keep on developing aufs.
2929+
2930+
2931+7.
2932+----------------------------------------
2933+If you are an experienced user, no explanation is needed. Aufs is
2934+just a linux filesystem.
2935+
2936+
2937+Enjoy!
2938+
2939+# Local variables: ;
2940+# mode: text;
2941+# End: ;
7f207e10 2942diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
eca34b5c 2943--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
016522bc 2944+++ linux/fs/aufs/aufs.h 2020-01-27 10:57:18.165538015 +0100
acd2b654 2945@@ -0,0 +1,62 @@
062440b3 2946+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 2947+/*
016522bc 2948+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7f207e10
AM
2949+ *
2950+ * This program, aufs is free software; you can redistribute it and/or modify
2951+ * it under the terms of the GNU General Public License as published by
2952+ * the Free Software Foundation; either version 2 of the License, or
2953+ * (at your option) any later version.
2954+ *
2955+ * This program is distributed in the hope that it will be useful,
2956+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2957+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2958+ * GNU General Public License for more details.
2959+ *
2960+ * You should have received a copy of the GNU General Public License
523b37e3 2961+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
2962+ */
2963+
2964+/*
2965+ * all header files
2966+ */
2967+
2968+#ifndef __AUFS_H__
2969+#define __AUFS_H__
2970+
2971+#ifdef __KERNEL__
2972+
2973+#define AuStub(type, name, body, ...) \
2974+ static inline type name(__VA_ARGS__) { body; }
2975+
2976+#define AuStubVoid(name, ...) \
2977+ AuStub(void, name, , __VA_ARGS__)
2978+#define AuStubInt0(name, ...) \
2979+ AuStub(int, name, return 0, __VA_ARGS__)
2980+
2981+#include "debug.h"
2982+
2983+#include "branch.h"
2984+#include "cpup.h"
2985+#include "dcsub.h"
2986+#include "dbgaufs.h"
2987+#include "dentry.h"
2988+#include "dir.h"
8b6a4947 2989+#include "dirren.h"
7f207e10
AM
2990+#include "dynop.h"
2991+#include "file.h"
2992+#include "fstype.h"
8b6a4947 2993+#include "hbl.h"
7f207e10 2994+#include "inode.h"
acd2b654 2995+#include "lcnt.h"
7f207e10
AM
2996+#include "loop.h"
2997+#include "module.h"
7f207e10
AM
2998+#include "opts.h"
2999+#include "rwsem.h"
7f207e10
AM
3000+#include "super.h"
3001+#include "sysaufs.h"
3002+#include "vfsub.h"
3003+#include "whout.h"
3004+#include "wkq.h"
3005+
3006+#endif /* __KERNEL__ */
3007+#endif /* __AUFS_H__ */
3008diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
eca34b5c 3009--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
016522bc 3010+++ linux/fs/aufs/branch.c 2020-01-27 10:57:18.165538015 +0100
eca34b5c 3011@@ -0,0 +1,1428 @@
cd7a4cd9 3012+// SPDX-License-Identifier: GPL-2.0
7f207e10 3013+/*
016522bc 3014+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7f207e10
AM
3015+ *
3016+ * This program, aufs is free software; you can redistribute it and/or modify
3017+ * it under the terms of the GNU General Public License as published by
3018+ * the Free Software Foundation; either version 2 of the License, or
3019+ * (at your option) any later version.
3020+ *
3021+ * This program is distributed in the hope that it will be useful,
3022+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3023+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3024+ * GNU General Public License for more details.
3025+ *
3026+ * You should have received a copy of the GNU General Public License
523b37e3 3027+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3028+ */
3029+
3030+/*
3031+ * branch management
3032+ */
3033+
027c5e7a 3034+#include <linux/compat.h>
7f207e10
AM
3035+#include <linux/statfs.h>
3036+#include "aufs.h"
3037+
3038+/*
3039+ * free a single branch
1facf9fc 3040+ */
3041+static void au_br_do_free(struct au_branch *br)
3042+{
3043+ int i;
3044+ struct au_wbr *wbr;
4a4d8108 3045+ struct au_dykey **key;
1facf9fc 3046+
027c5e7a 3047+ au_hnotify_fin_br(br);
8b6a4947
AM
3048+ /* always, regardless the mount option */
3049+ au_dr_hino_free(&br->br_dirren);
062440b3 3050+ au_xino_put(br);
1facf9fc 3051+
acd2b654
AM
3052+ AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
3053+ au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
3054+ AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
3055+ au_lcnt_fin(&br->br_count, /*do_sync*/0);
1facf9fc 3056+
3057+ wbr = br->br_wbr;
3058+ if (wbr) {
3059+ for (i = 0; i < AuBrWh_Last; i++)
3060+ dput(wbr->wbr_wh[i]);
3061+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 3062+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 3063+ }
3064+
076b876e
AM
3065+ if (br->br_fhsm) {
3066+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 3067+ au_kfree_try_rcu(br->br_fhsm);
076b876e
AM
3068+ }
3069+
4a4d8108
AM
3070+ key = br->br_dykey;
3071+ for (i = 0; i < AuBrDynOp; i++, key++)
3072+ if (*key)
3073+ au_dy_put(*key);
3074+ else
3075+ break;
3076+
537831f9 3077+ /* recursive lock, s_umount of branch's */
acd2b654 3078+ /* synchronize_rcu(); */ /* why? */
537831f9 3079+ lockdep_off();
86dc4139 3080+ path_put(&br->br_path);
537831f9 3081+ lockdep_on();
9f237c51 3082+ au_kfree_rcu(wbr);
acd2b654
AM
3083+ au_lcnt_wait_for_fin(&br->br_nfiles);
3084+ au_lcnt_wait_for_fin(&br->br_count);
3085+ /* I don't know why, but percpu_refcount requires this */
3086+ /* synchronize_rcu(); */
9f237c51 3087+ au_kfree_rcu(br);
1facf9fc 3088+}
3089+
3090+/*
3091+ * frees all branches
3092+ */
3093+void au_br_free(struct au_sbinfo *sbinfo)
3094+{
3095+ aufs_bindex_t bmax;
3096+ struct au_branch **br;
3097+
dece6358
AM
3098+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3099+
5afbbe0d 3100+ bmax = sbinfo->si_bbot + 1;
1facf9fc 3101+ br = sbinfo->si_branch;
3102+ while (bmax--)
3103+ au_br_do_free(*br++);
3104+}
3105+
3106+/*
3107+ * find the index of a branch which is specified by @br_id.
3108+ */
3109+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3110+{
5afbbe0d 3111+ aufs_bindex_t bindex, bbot;
1facf9fc 3112+
5afbbe0d
AM
3113+ bbot = au_sbbot(sb);
3114+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3115+ if (au_sbr_id(sb, bindex) == br_id)
3116+ return bindex;
3117+ return -1;
3118+}
3119+
3120+/* ---------------------------------------------------------------------- */
3121+
3122+/*
3123+ * add a branch
3124+ */
3125+
b752ccd1
AM
3126+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3127+ struct dentry *h_root)
1facf9fc 3128+{
b752ccd1
AM
3129+ if (unlikely(h_adding == h_root
3130+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3131+ return 1;
b752ccd1
AM
3132+ if (h_adding->d_sb != h_root->d_sb)
3133+ return 0;
3134+ return au_test_subdir(h_adding, h_root)
3135+ || au_test_subdir(h_root, h_adding);
1facf9fc 3136+}
3137+
3138+/*
3139+ * returns a newly allocated branch. @new_nbranch is a number of branches
3140+ * after adding a branch.
3141+ */
3142+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3143+ int perm)
3144+{
3145+ struct au_branch *add_branch;
3146+ struct dentry *root;
5527c038 3147+ struct inode *inode;
4a4d8108 3148+ int err;
1facf9fc 3149+
4a4d8108 3150+ err = -ENOMEM;
be52b249 3151+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3152+ if (unlikely(!add_branch))
3153+ goto out;
acd2b654 3154+ add_branch->br_xino = au_xino_alloc(/*nfile*/1);
062440b3 3155+ if (unlikely(!add_branch->br_xino))
521ced18 3156+ goto out_br;
027c5e7a
AM
3157+ err = au_hnotify_init_br(add_branch, perm);
3158+ if (unlikely(err))
062440b3 3159+ goto out_xino;
027c5e7a 3160+
1facf9fc 3161+ if (au_br_writable(perm)) {
3162+ /* may be freed separately at changing the branch permission */
be52b249 3163+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3164+ GFP_NOFS);
3165+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3166+ goto out_hnotify;
1facf9fc 3167+ }
3168+
076b876e
AM
3169+ if (au_br_fhsm(perm)) {
3170+ err = au_fhsm_br_alloc(add_branch);
3171+ if (unlikely(err))
3172+ goto out_wbr;
3173+ }
3174+
521ced18 3175+ root = sb->s_root;
e2f27e51 3176+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3177+ if (!err)
e2f27e51 3178+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3179+ if (!err) {
3180+ inode = d_inode(root);
1c60b727
AM
3181+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3182+ /*may_shrink*/0);
5527c038 3183+ }
4a4d8108
AM
3184+ if (!err)
3185+ return add_branch; /* success */
1facf9fc 3186+
076b876e 3187+out_wbr:
9f237c51 3188+ au_kfree_rcu(add_branch->br_wbr);
027c5e7a
AM
3189+out_hnotify:
3190+ au_hnotify_fin_br(add_branch);
062440b3
AM
3191+out_xino:
3192+ au_xino_put(add_branch);
4f0767ce 3193+out_br:
9f237c51 3194+ au_kfree_rcu(add_branch);
4f0767ce 3195+out:
4a4d8108 3196+ return ERR_PTR(err);
1facf9fc 3197+}
3198+
3199+/*
3200+ * test if the branch permission is legal or not.
3201+ */
3202+static int test_br(struct inode *inode, int brperm, char *path)
3203+{
3204+ int err;
3205+
4a4d8108
AM
3206+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3207+ if (!err)
3208+ goto out;
1facf9fc 3209+
4a4d8108
AM
3210+ err = -EINVAL;
3211+ pr_err("write permission for readonly mount or inode, %s\n", path);
3212+
4f0767ce 3213+out:
1facf9fc 3214+ return err;
3215+}
3216+
3217+/*
3218+ * returns:
3219+ * 0: success, the caller will add it
3220+ * plus: success, it is already unified, the caller should ignore it
3221+ * minus: error
3222+ */
3223+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3224+{
3225+ int err;
5afbbe0d 3226+ aufs_bindex_t bbot, bindex;
5527c038 3227+ struct dentry *root, *h_dentry;
1facf9fc 3228+ struct inode *inode, *h_inode;
3229+
3230+ root = sb->s_root;
5afbbe0d
AM
3231+ bbot = au_sbbot(sb);
3232+ if (unlikely(bbot >= 0
1facf9fc 3233+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3234+ err = 1;
3235+ if (!remount) {
3236+ err = -EINVAL;
4a4d8108 3237+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3238+ }
3239+ goto out;
3240+ }
3241+
3242+ err = -ENOSPC; /* -E2BIG; */
3243+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3244+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3245+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3246+ goto out;
3247+ }
3248+
3249+ err = -EDOM;
5afbbe0d 3250+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3251+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3252+ goto out;
3253+ }
3254+
5527c038 3255+ inode = d_inode(add->path.dentry);
1facf9fc 3256+ err = -ENOENT;
3257+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3258+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3259+ goto out;
3260+ }
3261+
3262+ err = -EINVAL;
3263+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3264+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3265+ goto out;
3266+ }
3267+
3268+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3269+ pr_err("unsupported filesystem, %s (%s)\n",
3270+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3271+ goto out;
3272+ }
3273+
c1595e42
JR
3274+ if (unlikely(inode->i_sb->s_stack_depth)) {
3275+ pr_err("already stacked, %s (%s)\n",
3276+ add->pathname, au_sbtype(inode->i_sb));
3277+ goto out;
3278+ }
3279+
5527c038 3280+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3281+ if (unlikely(err))
3282+ goto out;
3283+
5afbbe0d 3284+ if (bbot < 0)
1facf9fc 3285+ return 0; /* success */
3286+
3287+ err = -EINVAL;
5afbbe0d 3288+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3289+ if (unlikely(test_overlap(sb, add->path.dentry,
3290+ au_h_dptr(root, bindex)))) {
4a4d8108 3291+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3292+ goto out;
3293+ }
3294+
3295+ err = 0;
3296+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3297+ h_dentry = au_h_dptr(root, 0);
3298+ h_inode = d_inode(h_dentry);
1facf9fc 3299+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3300+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3301+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3302+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3303+ add->pathname,
3304+ i_uid_read(inode), i_gid_read(inode),
3305+ (inode->i_mode & S_IALLUGO),
3306+ i_uid_read(h_inode), i_gid_read(h_inode),
3307+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3308+ }
3309+
4f0767ce 3310+out:
1facf9fc 3311+ return err;
3312+}
3313+
3314+/*
3315+ * initialize or clean the whiteouts for an adding branch
3316+ */
3317+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3318+ int new_perm)
1facf9fc 3319+{
3320+ int err, old_perm;
3321+ aufs_bindex_t bindex;
febd17d6 3322+ struct inode *h_inode;
1facf9fc 3323+ struct au_wbr *wbr;
3324+ struct au_hinode *hdir;
5527c038 3325+ struct dentry *h_dentry;
1facf9fc 3326+
86dc4139
AM
3327+ err = vfsub_mnt_want_write(au_br_mnt(br));
3328+ if (unlikely(err))
3329+ goto out;
3330+
1facf9fc 3331+ wbr = br->br_wbr;
3332+ old_perm = br->br_perm;
3333+ br->br_perm = new_perm;
3334+ hdir = NULL;
febd17d6 3335+ h_inode = NULL;
1facf9fc 3336+ bindex = au_br_index(sb, br->br_id);
3337+ if (0 <= bindex) {
5527c038 3338+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3339+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3340+ } else {
5527c038 3341+ h_dentry = au_br_dentry(br);
febd17d6
JR
3342+ h_inode = d_inode(h_dentry);
3343+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3344+ }
3345+ if (!wbr)
86dc4139 3346+ err = au_wh_init(br, sb);
1facf9fc 3347+ else {
3348+ wbr_wh_write_lock(wbr);
86dc4139 3349+ err = au_wh_init(br, sb);
1facf9fc 3350+ wbr_wh_write_unlock(wbr);
3351+ }
3352+ if (hdir)
5afbbe0d 3353+ au_hn_inode_unlock(hdir);
1facf9fc 3354+ else
febd17d6 3355+ inode_unlock(h_inode);
86dc4139 3356+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3357+ br->br_perm = old_perm;
3358+
3359+ if (!err && wbr && !au_br_writable(new_perm)) {
9f237c51 3360+ au_kfree_rcu(wbr);
1facf9fc 3361+ br->br_wbr = NULL;
3362+ }
3363+
86dc4139 3364+out:
1facf9fc 3365+ return err;
3366+}
3367+
3368+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3369+ int perm)
1facf9fc 3370+{
3371+ int err;
4a4d8108 3372+ struct kstatfs kst;
1facf9fc 3373+ struct au_wbr *wbr;
3374+
3375+ wbr = br->br_wbr;
dece6358 3376+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3377+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3378+
4a4d8108
AM
3379+ /*
3380+ * a limit for rmdir/rename a dir
523b37e3 3381+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3382+ */
86dc4139 3383+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3384+ if (unlikely(err))
3385+ goto out;
3386+ err = -EINVAL;
3387+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3388+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3389+ else
523b37e3
AM
3390+ pr_err("%pd(%s), unsupported namelen %ld\n",
3391+ au_br_dentry(br),
86dc4139 3392+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3393+
4f0767ce 3394+out:
1facf9fc 3395+ return err;
3396+}
3397+
c1595e42 3398+/* initialize a new branch */
1facf9fc 3399+static int au_br_init(struct au_branch *br, struct super_block *sb,
3400+ struct au_opt_add *add)
3401+{
3402+ int err;
062440b3
AM
3403+ struct au_branch *brbase;
3404+ struct file *xf;
5527c038 3405+ struct inode *h_inode;
1facf9fc 3406+
3407+ err = 0;
1facf9fc 3408+ br->br_perm = add->perm;
86dc4139 3409+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3410+ spin_lock_init(&br->br_dykey_lock);
acd2b654
AM
3411+ au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3412+ au_lcnt_init(&br->br_count, /*release*/NULL);
1facf9fc 3413+ br->br_id = au_new_br_id(sb);
7f207e10 3414+ AuDebugOn(br->br_id < 0);
1facf9fc 3415+
8b6a4947
AM
3416+ /* always, regardless the given option */
3417+ err = au_dr_br_init(sb, br, &add->path);
3418+ if (unlikely(err))
3419+ goto out_err;
3420+
1facf9fc 3421+ if (au_br_writable(add->perm)) {
86dc4139 3422+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3423+ if (unlikely(err))
b752ccd1 3424+ goto out_err;
1facf9fc 3425+ }
3426+
3427+ if (au_opt_test(au_mntflags(sb), XINO)) {
062440b3 3428+ brbase = au_sbr(sb, 0);
acd2b654 3429+ xf = au_xino_file(brbase->br_xino, /*idx*/-1);
062440b3 3430+ AuDebugOn(!xf);
5527c038 3431+ h_inode = d_inode(add->path.dentry);
062440b3 3432+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
1facf9fc 3433+ if (unlikely(err)) {
acd2b654 3434+ AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
b752ccd1 3435+ goto out_err;
1facf9fc 3436+ }
3437+ }
3438+
3439+ sysaufs_br_init(br);
86dc4139 3440+ path_get(&br->br_path);
b752ccd1 3441+ goto out; /* success */
1facf9fc 3442+
4f0767ce 3443+out_err:
86dc4139 3444+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3445+out:
1facf9fc 3446+ return err;
3447+}
3448+
3449+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3450+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3451+ aufs_bindex_t amount)
3452+{
3453+ struct au_branch **brp;
3454+
dece6358
AM
3455+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3456+
1facf9fc 3457+ brp = sbinfo->si_branch + bindex;
3458+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3459+ *brp = br;
5afbbe0d
AM
3460+ sbinfo->si_bbot++;
3461+ if (unlikely(bbot < 0))
3462+ sbinfo->si_bbot = 0;
1facf9fc 3463+}
3464+
3465+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3466+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3467+{
3468+ struct au_hdentry *hdp;
3469+
1308ab2a 3470+ AuRwMustWriteLock(&dinfo->di_rwsem);
3471+
5afbbe0d 3472+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3473+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3474+ au_h_dentry_init(hdp);
5afbbe0d
AM
3475+ dinfo->di_bbot++;
3476+ if (unlikely(bbot < 0))
3477+ dinfo->di_btop = 0;
1facf9fc 3478+}
3479+
3480+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3481+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3482+{
3483+ struct au_hinode *hip;
3484+
1308ab2a 3485+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3486+
5afbbe0d 3487+ hip = au_hinode(iinfo, bindex);
1facf9fc 3488+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3489+ au_hinode_init(hip);
3490+ iinfo->ii_bbot++;
3491+ if (unlikely(bbot < 0))
3492+ iinfo->ii_btop = 0;
1facf9fc 3493+}
3494+
86dc4139
AM
3495+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3496+ aufs_bindex_t bindex)
1facf9fc 3497+{
86dc4139 3498+ struct dentry *root, *h_dentry;
5527c038 3499+ struct inode *root_inode, *h_inode;
5afbbe0d 3500+ aufs_bindex_t bbot, amount;
1facf9fc 3501+
3502+ root = sb->s_root;
5527c038 3503+ root_inode = d_inode(root);
5afbbe0d
AM
3504+ bbot = au_sbbot(sb);
3505+ amount = bbot + 1 - bindex;
86dc4139 3506+ h_dentry = au_br_dentry(br);
53392da6 3507+ au_sbilist_lock();
5afbbe0d
AM
3508+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3509+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3510+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3511+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3512+ h_inode = d_inode(h_dentry);
3513+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3514+ au_sbilist_unlock();
1facf9fc 3515+}
3516+
3517+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3518+{
3519+ int err;
5afbbe0d 3520+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3521+ struct dentry *root, *h_dentry;
3522+ struct inode *root_inode;
3523+ struct au_branch *add_branch;
3524+
3525+ root = sb->s_root;
5527c038 3526+ root_inode = d_inode(root);
1facf9fc 3527+ IMustLock(root_inode);
5afbbe0d 3528+ IiMustWriteLock(root_inode);
1facf9fc 3529+ err = test_add(sb, add, remount);
3530+ if (unlikely(err < 0))
3531+ goto out;
3532+ if (err) {
3533+ err = 0;
3534+ goto out; /* success */
3535+ }
3536+
5afbbe0d
AM
3537+ bbot = au_sbbot(sb);
3538+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3539+ err = PTR_ERR(add_branch);
3540+ if (IS_ERR(add_branch))
3541+ goto out;
3542+
3543+ err = au_br_init(add_branch, sb, add);
3544+ if (unlikely(err)) {
3545+ au_br_do_free(add_branch);
3546+ goto out;
3547+ }
3548+
3549+ add_bindex = add->bindex;
062440b3
AM
3550+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3551+ au_br_do_add(sb, add_branch, add_bindex);
3552+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3553+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
1facf9fc 3554+
86dc4139 3555+ h_dentry = add->path.dentry;
1308ab2a 3556+ if (!add_bindex) {
1facf9fc 3557+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3558+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3559+ } else
5527c038 3560+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3561+
4f0767ce 3562+out:
1facf9fc 3563+ return err;
3564+}
3565+
3566+/* ---------------------------------------------------------------------- */
3567+
79b8bda9 3568+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3569+ unsigned long long max __maybe_unused,
3570+ void *arg)
3571+{
3572+ unsigned long long n;
3573+ struct file **p, *f;
8b6a4947
AM
3574+ struct hlist_bl_head *files;
3575+ struct hlist_bl_node *pos;
076b876e 3576+ struct au_finfo *finfo;
076b876e
AM
3577+
3578+ n = 0;
3579+ p = a;
3580+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3581+ hlist_bl_lock(files);
3582+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3583+ f = finfo->fi_file;
3584+ if (file_count(f)
3585+ && !special_file(file_inode(f)->i_mode)) {
3586+ get_file(f);
3587+ *p++ = f;
3588+ n++;
3589+ AuDebugOn(n > max);
3590+ }
3591+ }
8b6a4947 3592+ hlist_bl_unlock(files);
076b876e
AM
3593+
3594+ return n;
3595+}
3596+
3597+static struct file **au_farray_alloc(struct super_block *sb,
3598+ unsigned long long *max)
3599+{
acd2b654
AM
3600+ struct au_sbinfo *sbi;
3601+
3602+ sbi = au_sbi(sb);
3603+ *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
79b8bda9 3604+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3605+}
3606+
3607+static void au_farray_free(struct file **a, unsigned long long max)
3608+{
3609+ unsigned long long ull;
3610+
3611+ for (ull = 0; ull < max; ull++)
3612+ if (a[ull])
3613+ fput(a[ull]);
be52b249 3614+ kvfree(a);
076b876e
AM
3615+}
3616+
3617+/* ---------------------------------------------------------------------- */
3618+
1facf9fc 3619+/*
3620+ * delete a branch
3621+ */
3622+
3623+/* to show the line number, do not make it inlined function */
4a4d8108 3624+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3625+ if (do_info) \
4a4d8108 3626+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3627+} while (0)
3628+
5afbbe0d
AM
3629+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3630+ aufs_bindex_t bbot)
027c5e7a 3631+{
5afbbe0d 3632+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3633+}
3634+
5afbbe0d
AM
3635+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3636+ aufs_bindex_t bbot)
027c5e7a 3637+{
5afbbe0d 3638+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3639+}
3640+
1facf9fc 3641+/*
3642+ * test if the branch is deletable or not.
3643+ */
3644+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3645+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3646+{
3647+ int err, i, j, ndentry;
5afbbe0d 3648+ aufs_bindex_t btop, bbot;
1facf9fc 3649+ struct au_dcsub_pages dpages;
3650+ struct au_dpage *dpage;
3651+ struct dentry *d;
1facf9fc 3652+
3653+ err = au_dpages_init(&dpages, GFP_NOFS);
3654+ if (unlikely(err))
3655+ goto out;
3656+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3657+ if (unlikely(err))
3658+ goto out_dpages;
3659+
1facf9fc 3660+ for (i = 0; !err && i < dpages.ndpage; i++) {
3661+ dpage = dpages.dpages + i;
3662+ ndentry = dpage->ndentry;
3663+ for (j = 0; !err && j < ndentry; j++) {
3664+ d = dpage->dentries[j];
c1595e42 3665+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3666+ if (!au_digen_test(d, sigen)) {
1facf9fc 3667+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3668+ if (unlikely(au_dbrange_test(d))) {
3669+ di_read_unlock(d, AuLock_IR);
3670+ continue;
3671+ }
3672+ } else {
1facf9fc 3673+ di_write_lock_child(d);
027c5e7a
AM
3674+ if (unlikely(au_dbrange_test(d))) {
3675+ di_write_unlock(d);
3676+ continue;
3677+ }
1facf9fc 3678+ err = au_reval_dpath(d, sigen);
3679+ if (!err)
3680+ di_downgrade_lock(d, AuLock_IR);
3681+ else {
3682+ di_write_unlock(d);
3683+ break;
3684+ }
3685+ }
3686+
027c5e7a 3687+ /* AuDbgDentry(d); */
5afbbe0d
AM
3688+ btop = au_dbtop(d);
3689+ bbot = au_dbbot(d);
3690+ if (btop <= bindex
3691+ && bindex <= bbot
1facf9fc 3692+ && au_h_dptr(d, bindex)
5afbbe0d 3693+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3694+ err = -EBUSY;
523b37e3 3695+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3696+ AuDbgDentry(d);
1facf9fc 3697+ }
3698+ di_read_unlock(d, AuLock_IR);
3699+ }
3700+ }
3701+
4f0767ce 3702+out_dpages:
1facf9fc 3703+ au_dpages_free(&dpages);
4f0767ce 3704+out:
1facf9fc 3705+ return err;
3706+}
3707+
3708+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3709+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3710+{
3711+ int err;
7f207e10
AM
3712+ unsigned long long max, ull;
3713+ struct inode *i, **array;
5afbbe0d 3714+ aufs_bindex_t btop, bbot;
1facf9fc 3715+
7f207e10
AM
3716+ array = au_iarray_alloc(sb, &max);
3717+ err = PTR_ERR(array);
3718+ if (IS_ERR(array))
3719+ goto out;
3720+
1facf9fc 3721+ err = 0;
7f207e10
AM
3722+ AuDbg("b%d\n", bindex);
3723+ for (ull = 0; !err && ull < max; ull++) {
3724+ i = array[ull];
076b876e
AM
3725+ if (unlikely(!i))
3726+ break;
7f207e10 3727+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3728+ continue;
3729+
7f207e10 3730+ /* AuDbgInode(i); */
537831f9 3731+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3732+ ii_read_lock_child(i);
3733+ else {
3734+ ii_write_lock_child(i);
027c5e7a
AM
3735+ err = au_refresh_hinode_self(i);
3736+ au_iigen_dec(i);
1facf9fc 3737+ if (!err)
3738+ ii_downgrade_lock(i);
3739+ else {
3740+ ii_write_unlock(i);
3741+ break;
3742+ }
3743+ }
3744+
5afbbe0d
AM
3745+ btop = au_ibtop(i);
3746+ bbot = au_ibbot(i);
3747+ if (btop <= bindex
3748+ && bindex <= bbot
1facf9fc 3749+ && au_h_iptr(i, bindex)
5afbbe0d 3750+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3751+ err = -EBUSY;
3752+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3753+ AuDbgInode(i);
1facf9fc 3754+ }
3755+ ii_read_unlock(i);
3756+ }
7f207e10 3757+ au_iarray_free(array, max);
1facf9fc 3758+
7f207e10 3759+out:
1facf9fc 3760+ return err;
3761+}
3762+
b752ccd1
AM
3763+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3764+ const unsigned int verbose)
1facf9fc 3765+{
3766+ int err;
3767+ unsigned int sigen;
3768+
3769+ sigen = au_sigen(root->d_sb);
3770+ DiMustNoWaiters(root);
5527c038 3771+ IiMustNoWaiters(d_inode(root));
1facf9fc 3772+ di_write_unlock(root);
b752ccd1 3773+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3774+ if (!err)
b752ccd1 3775+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3776+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3777+
3778+ return err;
3779+}
3780+
076b876e
AM
3781+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3782+ struct file **to_free, int *idx)
3783+{
3784+ int err;
c1595e42 3785+ unsigned char matched, root;
5afbbe0d 3786+ aufs_bindex_t bindex, bbot;
076b876e
AM
3787+ struct au_fidir *fidir;
3788+ struct au_hfile *hfile;
3789+
3790+ err = 0;
2000de60 3791+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3792+ if (root) {
3793+ get_file(file);
3794+ to_free[*idx] = file;
3795+ (*idx)++;
3796+ goto out;
3797+ }
3798+
076b876e 3799+ matched = 0;
076b876e
AM
3800+ fidir = au_fi(file)->fi_hdir;
3801+ AuDebugOn(!fidir);
5afbbe0d
AM
3802+ bbot = au_fbbot_dir(file);
3803+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3804+ hfile = fidir->fd_hfile + bindex;
3805+ if (!hfile->hf_file)
3806+ continue;
3807+
c1595e42 3808+ if (hfile->hf_br->br_id == br_id) {
076b876e 3809+ matched = 1;
076b876e 3810+ break;
c1595e42 3811+ }
076b876e 3812+ }
c1595e42 3813+ if (matched)
076b876e
AM
3814+ err = -EBUSY;
3815+
3816+out:
3817+ return err;
3818+}
3819+
3820+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3821+ struct file **to_free, int opened)
3822+{
3823+ int err, idx;
3824+ unsigned long long ull, max;
5afbbe0d 3825+ aufs_bindex_t btop;
076b876e 3826+ struct file *file, **array;
076b876e
AM
3827+ struct dentry *root;
3828+ struct au_hfile *hfile;
3829+
3830+ array = au_farray_alloc(sb, &max);
3831+ err = PTR_ERR(array);
3832+ if (IS_ERR(array))
3833+ goto out;
3834+
3835+ err = 0;
3836+ idx = 0;
3837+ root = sb->s_root;
3838+ di_write_unlock(root);
3839+ for (ull = 0; ull < max; ull++) {
3840+ file = array[ull];
3841+ if (unlikely(!file))
3842+ break;
3843+
3844+ /* AuDbg("%pD\n", file); */
3845+ fi_read_lock(file);
5afbbe0d 3846+ btop = au_fbtop(file);
2000de60 3847+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3848+ hfile = &au_fi(file)->fi_htop;
3849+ if (hfile->hf_br->br_id == br_id)
3850+ err = -EBUSY;
3851+ } else
3852+ err = test_dir_busy(file, br_id, to_free, &idx);
3853+ fi_read_unlock(file);
3854+ if (unlikely(err))
3855+ break;
3856+ }
3857+ di_write_lock_child(root);
3858+ au_farray_free(array, max);
3859+ AuDebugOn(idx > opened);
3860+
3861+out:
3862+ return err;
3863+}
3864+
3865+static void br_del_file(struct file **to_free, unsigned long long opened,
062440b3 3866+ aufs_bindex_t br_id)
076b876e
AM
3867+{
3868+ unsigned long long ull;
5afbbe0d 3869+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
3870+ struct file *file;
3871+ struct au_fidir *fidir;
3872+ struct au_hfile *hfile;
3873+
3874+ for (ull = 0; ull < opened; ull++) {
3875+ file = to_free[ull];
3876+ if (unlikely(!file))
3877+ break;
3878+
3879+ /* AuDbg("%pD\n", file); */
2000de60 3880+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
3881+ bfound = -1;
3882+ fidir = au_fi(file)->fi_hdir;
3883+ AuDebugOn(!fidir);
3884+ fi_write_lock(file);
5afbbe0d
AM
3885+ btop = au_fbtop(file);
3886+ bbot = au_fbbot_dir(file);
3887+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
3888+ hfile = fidir->fd_hfile + bindex;
3889+ if (!hfile->hf_file)
3890+ continue;
3891+
3892+ if (hfile->hf_br->br_id == br_id) {
3893+ bfound = bindex;
3894+ break;
3895+ }
3896+ }
3897+ AuDebugOn(bfound < 0);
3898+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
3899+ if (bfound == btop) {
3900+ for (btop++; btop <= bbot; btop++)
3901+ if (au_hf_dir(file, btop)) {
3902+ au_set_fbtop(file, btop);
076b876e
AM
3903+ break;
3904+ }
3905+ }
3906+ fi_write_unlock(file);
3907+ }
3908+}
3909+
1facf9fc 3910+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3911+ const aufs_bindex_t bindex,
5afbbe0d 3912+ const aufs_bindex_t bbot)
1facf9fc 3913+{
3914+ struct au_branch **brp, **p;
3915+
dece6358
AM
3916+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3917+
1facf9fc 3918+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
3919+ if (bindex < bbot)
3920+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3921+ sbinfo->si_branch[0 + bbot] = NULL;
3922+ sbinfo->si_bbot--;
1facf9fc 3923+
e2f27e51
AM
3924+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3925+ /*may_shrink*/1);
1facf9fc 3926+ if (p)
3927+ sbinfo->si_branch = p;
4a4d8108 3928+ /* harmless error */
1facf9fc 3929+}
3930+
3931+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 3932+ const aufs_bindex_t bbot)
1facf9fc 3933+{
3934+ struct au_hdentry *hdp, *p;
3935+
1308ab2a 3936+ AuRwMustWriteLock(&dinfo->di_rwsem);
3937+
5afbbe0d
AM
3938+ hdp = au_hdentry(dinfo, bindex);
3939+ if (bindex < bbot)
3940+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3941+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3942+ dinfo->di_bbot--;
1facf9fc 3943+
e2f27e51
AM
3944+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3945+ /*may_shrink*/1);
1facf9fc 3946+ if (p)
3947+ dinfo->di_hdentry = p;
4a4d8108 3948+ /* harmless error */
1facf9fc 3949+}
3950+
3951+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 3952+ const aufs_bindex_t bbot)
1facf9fc 3953+{
3954+ struct au_hinode *hip, *p;
3955+
1308ab2a 3956+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3957+
5afbbe0d
AM
3958+ hip = au_hinode(iinfo, bindex);
3959+ if (bindex < bbot)
3960+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3961+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
3962+ iinfo->ii_bbot--;
1facf9fc 3963+
e2f27e51
AM
3964+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3965+ /*may_shrink*/1);
1facf9fc 3966+ if (p)
3967+ iinfo->ii_hinode = p;
4a4d8108 3968+ /* harmless error */
1facf9fc 3969+}
3970+
3971+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3972+ struct au_branch *br)
3973+{
5afbbe0d 3974+ aufs_bindex_t bbot;
1facf9fc 3975+ struct au_sbinfo *sbinfo;
53392da6
AM
3976+ struct dentry *root, *h_root;
3977+ struct inode *inode, *h_inode;
3978+ struct au_hinode *hinode;
1facf9fc 3979+
dece6358
AM
3980+ SiMustWriteLock(sb);
3981+
1facf9fc 3982+ root = sb->s_root;
5527c038 3983+ inode = d_inode(root);
1facf9fc 3984+ sbinfo = au_sbi(sb);
5afbbe0d 3985+ bbot = sbinfo->si_bbot;
1facf9fc 3986+
53392da6
AM
3987+ h_root = au_h_dptr(root, bindex);
3988+ hinode = au_hi(inode, bindex);
3989+ h_inode = au_igrab(hinode->hi_inode);
3990+ au_hiput(hinode);
1facf9fc 3991+
53392da6 3992+ au_sbilist_lock();
5afbbe0d
AM
3993+ au_br_do_del_brp(sbinfo, bindex, bbot);
3994+ au_br_do_del_hdp(au_di(root), bindex, bbot);
3995+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
3996+ au_sbilist_unlock();
3997+
8b6a4947
AM
3998+ /* ignore an error */
3999+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
4000+
53392da6
AM
4001+ dput(h_root);
4002+ iput(h_inode);
4003+ au_br_do_free(br);
1facf9fc 4004+}
4005+
79b8bda9
AM
4006+static unsigned long long empty_cb(struct super_block *sb, void *array,
4007+ unsigned long long max, void *arg)
076b876e
AM
4008+{
4009+ return max;
4010+}
4011+
1facf9fc 4012+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4013+{
4014+ int err, rerr, i;
076b876e 4015+ unsigned long long opened;
1facf9fc 4016+ unsigned int mnt_flags;
5afbbe0d 4017+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 4018+ unsigned char do_wh, verbose;
4019+ struct au_branch *br;
4020+ struct au_wbr *wbr;
076b876e
AM
4021+ struct dentry *root;
4022+ struct file **to_free;
1facf9fc 4023+
4024+ err = 0;
076b876e
AM
4025+ opened = 0;
4026+ to_free = NULL;
4027+ root = sb->s_root;
4028+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 4029+ if (bindex < 0) {
4030+ if (remount)
4031+ goto out; /* success */
4032+ err = -ENOENT;
4a4d8108 4033+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 4034+ goto out;
4035+ }
4036+ AuDbg("bindex b%d\n", bindex);
4037+
4038+ err = -EBUSY;
4039+ mnt_flags = au_mntflags(sb);
4040+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
4041+ bbot = au_sbbot(sb);
4042+ if (unlikely(!bbot)) {
1facf9fc 4043+ AuVerbose(verbose, "no more branches left\n");
4044+ goto out;
4045+ }
acd2b654 4046+
1facf9fc 4047+ br = au_sbr(sb, bindex);
86dc4139 4048+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
acd2b654
AM
4049+ if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
4050+ AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
4051+ goto out;
4052+ }
076b876e
AM
4053+
4054+ br_id = br->br_id;
acd2b654 4055+ opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
076b876e 4056+ if (unlikely(opened)) {
79b8bda9 4057+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
4058+ err = PTR_ERR(to_free);
4059+ if (IS_ERR(to_free))
4060+ goto out;
4061+
4062+ err = test_file_busy(sb, br_id, to_free, opened);
4063+ if (unlikely(err)) {
4064+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
4065+ goto out;
4066+ }
1facf9fc 4067+ }
4068+
4069+ wbr = br->br_wbr;
4070+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4071+ if (do_wh) {
1308ab2a 4072+ /* instead of WbrWhMustWriteLock(wbr) */
4073+ SiMustWriteLock(sb);
1facf9fc 4074+ for (i = 0; i < AuBrWh_Last; i++) {
4075+ dput(wbr->wbr_wh[i]);
4076+ wbr->wbr_wh[i] = NULL;
4077+ }
4078+ }
4079+
076b876e 4080+ err = test_children_busy(root, bindex, verbose);
1facf9fc 4081+ if (unlikely(err)) {
4082+ if (do_wh)
4083+ goto out_wh;
4084+ goto out;
4085+ }
4086+
4087+ err = 0;
076b876e
AM
4088+ if (to_free) {
4089+ /*
4090+ * now we confirmed the branch is deletable.
4091+ * let's free the remaining opened dirs on the branch.
4092+ */
4093+ di_write_unlock(root);
4094+ br_del_file(to_free, opened, br_id);
4095+ di_write_lock_child(root);
4096+ }
4097+
062440b3
AM
4098+ sysaufs_brs_del(sb, bindex); /* remove successors */
4099+ dbgaufs_xino_del(br); /* remove one */
4100+ au_br_do_del(sb, bindex, br);
4101+ sysaufs_brs_add(sb, bindex); /* append successors */
4102+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
1facf9fc 4103+
1308ab2a 4104+ if (!bindex) {
5527c038 4105+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4106+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4107+ } else
5527c038 4108+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4109+ if (au_opt_test(mnt_flags, PLINK))
4110+ au_plink_half_refresh(sb, br_id);
4111+
1facf9fc 4112+ goto out; /* success */
4113+
4f0767ce 4114+out_wh:
1facf9fc 4115+ /* revert */
86dc4139 4116+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4117+ if (rerr)
0c3ec466
AM
4118+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4119+ del->pathname, rerr);
4f0767ce 4120+out:
076b876e
AM
4121+ if (to_free)
4122+ au_farray_free(to_free, opened);
1facf9fc 4123+ return err;
4124+}
4125+
4126+/* ---------------------------------------------------------------------- */
4127+
027c5e7a
AM
4128+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4129+{
4130+ int err;
5afbbe0d 4131+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4132+ struct aufs_ibusy ibusy;
4133+ struct inode *inode, *h_inode;
4134+
4135+ err = -EPERM;
4136+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4137+ goto out;
4138+
4139+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4140+ if (!err)
ba1aed25
AM
4141+ /* VERIFY_WRITE */
4142+ err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
027c5e7a
AM
4143+ if (unlikely(err)) {
4144+ err = -EFAULT;
4145+ AuTraceErr(err);
4146+ goto out;
4147+ }
4148+
4149+ err = -EINVAL;
4150+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4151+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4152+ goto out_unlock;
4153+
4154+ err = 0;
4155+ ibusy.h_ino = 0; /* invalid */
4156+ inode = ilookup(sb, ibusy.ino);
4157+ if (!inode
4158+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4159+ || au_is_bad_inode(inode))
027c5e7a
AM
4160+ goto out_unlock;
4161+
4162+ ii_read_lock_child(inode);
5afbbe0d
AM
4163+ btop = au_ibtop(inode);
4164+ bbot = au_ibbot(inode);
4165+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4166+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4167+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4168+ ibusy.h_ino = h_inode->i_ino;
4169+ }
4170+ ii_read_unlock(inode);
4171+ iput(inode);
4172+
4173+out_unlock:
4174+ si_read_unlock(sb);
4175+ if (!err) {
4176+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4177+ if (unlikely(err)) {
4178+ err = -EFAULT;
4179+ AuTraceErr(err);
4180+ }
4181+ }
4182+out:
4183+ return err;
4184+}
4185+
4186+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4187+{
2000de60 4188+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4189+}
4190+
4191+#ifdef CONFIG_COMPAT
4192+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4193+{
2000de60 4194+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4195+}
4196+#endif
4197+
4198+/* ---------------------------------------------------------------------- */
4199+
1facf9fc 4200+/*
4201+ * change a branch permission
4202+ */
4203+
dece6358
AM
4204+static void au_warn_ima(void)
4205+{
4206+#ifdef CONFIG_IMA
1308ab2a 4207+ /* since it doesn't support mark_files_ro() */
027c5e7a 4208+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4209+#endif
4210+}
4211+
1facf9fc 4212+static int do_need_sigen_inc(int a, int b)
4213+{
4214+ return au_br_whable(a) && !au_br_whable(b);
4215+}
4216+
4217+static int need_sigen_inc(int old, int new)
4218+{
4219+ return do_need_sigen_inc(old, new)
4220+ || do_need_sigen_inc(new, old);
4221+}
4222+
4223+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4224+{
7f207e10 4225+ int err, do_warn;
027c5e7a 4226+ unsigned int mnt_flags;
7f207e10 4227+ unsigned long long ull, max;
e49829fe 4228+ aufs_bindex_t br_id;
38d290e6 4229+ unsigned char verbose, writer;
7f207e10 4230+ struct file *file, *hf, **array;
e49829fe 4231+ struct au_hfile *hfile;
eca34b5c 4232+ struct inode *h_inode;
1facf9fc 4233+
027c5e7a
AM
4234+ mnt_flags = au_mntflags(sb);
4235+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4236+
7f207e10
AM
4237+ array = au_farray_alloc(sb, &max);
4238+ err = PTR_ERR(array);
4239+ if (IS_ERR(array))
1facf9fc 4240+ goto out;
4241+
7f207e10 4242+ do_warn = 0;
e49829fe 4243+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4244+ for (ull = 0; ull < max; ull++) {
4245+ file = array[ull];
076b876e
AM
4246+ if (unlikely(!file))
4247+ break;
1facf9fc 4248+
523b37e3 4249+ /* AuDbg("%pD\n", file); */
1facf9fc 4250+ fi_read_lock(file);
4251+ if (unlikely(au_test_mmapped(file))) {
4252+ err = -EBUSY;
523b37e3 4253+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4254+ AuDbgFile(file);
1facf9fc 4255+ FiMustNoWaiters(file);
4256+ fi_read_unlock(file);
7f207e10 4257+ goto out_array;
1facf9fc 4258+ }
4259+
e49829fe
JR
4260+ hfile = &au_fi(file)->fi_htop;
4261+ hf = hfile->hf_file;
7e9cd9fe 4262+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4263+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4264+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4265+ || !(hf->f_mode & FMODE_WRITE))
4266+ array[ull] = NULL;
4267+ else {
4268+ do_warn = 1;
4269+ get_file(file);
1facf9fc 4270+ }
4271+
1facf9fc 4272+ FiMustNoWaiters(file);
4273+ fi_read_unlock(file);
7f207e10
AM
4274+ fput(file);
4275+ }
1facf9fc 4276+
4277+ err = 0;
7f207e10 4278+ if (do_warn)
dece6358 4279+ au_warn_ima();
7f207e10
AM
4280+
4281+ for (ull = 0; ull < max; ull++) {
4282+ file = array[ull];
4283+ if (!file)
4284+ continue;
4285+
1facf9fc 4286+ /* todo: already flushed? */
523b37e3
AM
4287+ /*
4288+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4289+ * approach which resets f_mode and calls mnt_drop_write() and
4290+ * file_release_write() for each file, because the branch
4291+ * attribute in aufs world is totally different from the native
4292+ * fs rw/ro mode.
4293+ */
7f207e10
AM
4294+ /* fi_read_lock(file); */
4295+ hfile = &au_fi(file)->fi_htop;
4296+ hf = hfile->hf_file;
4297+ /* fi_read_unlock(file); */
027c5e7a 4298+ spin_lock(&hf->f_lock);
38d290e6
JR
4299+ writer = !!(hf->f_mode & FMODE_WRITER);
4300+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4301+ spin_unlock(&hf->f_lock);
38d290e6 4302+ if (writer) {
eca34b5c
AM
4303+ h_inode = file_inode(hf);
4304+ put_write_access(h_inode);
c06a8ce3 4305+ __mnt_drop_write(hf->f_path.mnt);
eca34b5c
AM
4306+ if ((hf->f_mode & (FMODE_READ | FMODE_WRITE))
4307+ == FMODE_READ)
4308+ i_readcount_inc(h_inode);
1facf9fc 4309+ }
4310+ }
4311+
7f207e10
AM
4312+out_array:
4313+ au_farray_free(array, max);
4f0767ce 4314+out:
7f207e10 4315+ AuTraceErr(err);
1facf9fc 4316+ return err;
4317+}
4318+
4319+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4320+ int *do_refresh)
1facf9fc 4321+{
4322+ int err, rerr;
4323+ aufs_bindex_t bindex;
4324+ struct dentry *root;
4325+ struct au_branch *br;
076b876e 4326+ struct au_br_fhsm *bf;
1facf9fc 4327+
4328+ root = sb->s_root;
1facf9fc 4329+ bindex = au_find_dbindex(root, mod->h_root);
4330+ if (bindex < 0) {
4331+ if (remount)
4332+ return 0; /* success */
4333+ err = -ENOENT;
4a4d8108 4334+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4335+ goto out;
4336+ }
4337+ AuDbg("bindex b%d\n", bindex);
4338+
5527c038 4339+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4340+ if (unlikely(err))
4341+ goto out;
4342+
4343+ br = au_sbr(sb, bindex);
86dc4139 4344+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4345+ if (br->br_perm == mod->perm)
4346+ return 0; /* success */
4347+
076b876e
AM
4348+ /* pre-allocate for non-fhsm --> fhsm */
4349+ bf = NULL;
4350+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4351+ err = au_fhsm_br_alloc(br);
4352+ if (unlikely(err))
4353+ goto out;
4354+ bf = br->br_fhsm;
4355+ br->br_fhsm = NULL;
4356+ }
4357+
1facf9fc 4358+ if (au_br_writable(br->br_perm)) {
4359+ /* remove whiteout base */
86dc4139 4360+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4361+ if (unlikely(err))
076b876e 4362+ goto out_bf;
1facf9fc 4363+
4364+ if (!au_br_writable(mod->perm)) {
4365+ /* rw --> ro, file might be mmapped */
4366+ DiMustNoWaiters(root);
5527c038 4367+ IiMustNoWaiters(d_inode(root));
1facf9fc 4368+ di_write_unlock(root);
4369+ err = au_br_mod_files_ro(sb, bindex);
4370+ /* aufs_write_lock() calls ..._child() */
4371+ di_write_lock_child(root);
4372+
4373+ if (unlikely(err)) {
4374+ rerr = -ENOMEM;
be52b249 4375+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4376+ GFP_NOFS);
86dc4139
AM
4377+ if (br->br_wbr)
4378+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4379+ if (unlikely(rerr)) {
4380+ AuIOErr("nested error %d (%d)\n",
4381+ rerr, err);
4382+ br->br_perm = mod->perm;
4383+ }
4384+ }
4385+ }
4386+ } else if (au_br_writable(mod->perm)) {
4387+ /* ro --> rw */
4388+ err = -ENOMEM;
be52b249 4389+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4390+ if (br->br_wbr) {
86dc4139 4391+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4392+ if (unlikely(err)) {
9f237c51 4393+ au_kfree_rcu(br->br_wbr);
1facf9fc 4394+ br->br_wbr = NULL;
4395+ }
4396+ }
4397+ }
076b876e
AM
4398+ if (unlikely(err))
4399+ goto out_bf;
4400+
4401+ if (au_br_fhsm(br->br_perm)) {
4402+ if (!au_br_fhsm(mod->perm)) {
4403+ /* fhsm --> non-fhsm */
4404+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 4405+ au_kfree_rcu(br->br_fhsm);
076b876e
AM
4406+ br->br_fhsm = NULL;
4407+ }
4408+ } else if (au_br_fhsm(mod->perm))
4409+ /* non-fhsm --> fhsm */
4410+ br->br_fhsm = bf;
4411+
076b876e
AM
4412+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4413+ br->br_perm = mod->perm;
4414+ goto out; /* success */
1facf9fc 4415+
076b876e 4416+out_bf:
9f237c51 4417+ au_kfree_try_rcu(bf);
076b876e
AM
4418+out:
4419+ AuTraceErr(err);
4420+ return err;
4421+}
4422+
4423+/* ---------------------------------------------------------------------- */
4424+
4425+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4426+{
4427+ int err;
4428+ struct kstatfs kstfs;
4429+
4430+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4431+ if (!err) {
076b876e
AM
4432+ stfs->f_blocks = kstfs.f_blocks;
4433+ stfs->f_bavail = kstfs.f_bavail;
4434+ stfs->f_files = kstfs.f_files;
4435+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4436+ }
4437+
1facf9fc 4438+ return err;
4439+}
7f207e10 4440diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
eca34b5c 4441--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
016522bc 4442+++ linux/fs/aufs/branch.h 2020-01-27 10:57:18.165538015 +0100
83b672a5 4443@@ -0,0 +1,366 @@
062440b3 4444+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 4445+/*
016522bc 4446+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 4447+ *
4448+ * This program, aufs is free software; you can redistribute it and/or modify
4449+ * it under the terms of the GNU General Public License as published by
4450+ * the Free Software Foundation; either version 2 of the License, or
4451+ * (at your option) any later version.
dece6358
AM
4452+ *
4453+ * This program is distributed in the hope that it will be useful,
4454+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4455+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4456+ * GNU General Public License for more details.
4457+ *
4458+ * You should have received a copy of the GNU General Public License
523b37e3 4459+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4460+ */
4461+
4462+/*
4463+ * branch filesystems and xino for them
4464+ */
4465+
4466+#ifndef __AUFS_BRANCH_H__
4467+#define __AUFS_BRANCH_H__
4468+
4469+#ifdef __KERNEL__
4470+
1facf9fc 4471+#include <linux/mount.h>
8b6a4947 4472+#include "dirren.h"
4a4d8108 4473+#include "dynop.h"
acd2b654 4474+#include "lcnt.h"
1facf9fc 4475+#include "rwsem.h"
4476+#include "super.h"
4477+
4478+/* ---------------------------------------------------------------------- */
4479+
4480+/* a xino file */
062440b3 4481+struct au_xino {
acd2b654
AM
4482+ struct file **xi_file;
4483+ unsigned int xi_nfile;
4484+
521ced18
JR
4485+ struct {
4486+ spinlock_t spin;
4487+ ino_t *array;
4488+ int total;
4489+ /* reserved for future use */
4490+ /* unsigned long *bitmap; */
4491+ wait_queue_head_t wqh;
4492+ } xi_nondir;
1facf9fc 4493+
acd2b654 4494+ struct mutex xi_mtx; /* protects xi_file array */
9f237c51 4495+ struct hlist_bl_head xi_writing;
acd2b654 4496+
062440b3 4497+ atomic_t xi_truncating;
1facf9fc 4498+
062440b3 4499+ struct kref xi_kref;
1facf9fc 4500+};
4501+
076b876e
AM
4502+/* File-based Hierarchical Storage Management */
4503+struct au_br_fhsm {
4504+#ifdef CONFIG_AUFS_FHSM
4505+ struct mutex bf_lock;
4506+ unsigned long bf_jiffy;
4507+ struct aufs_stfs bf_stfs;
4508+ int bf_readable;
4509+#endif
4510+};
4511+
1facf9fc 4512+/* members for writable branch only */
4513+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4514+struct au_wbr {
dece6358 4515+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4516+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4517+ atomic_t wbr_wh_running;
1facf9fc 4518+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4519+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4520+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4521+
4522+ /* mfs mode */
4523+ unsigned long long wbr_bytes;
4524+};
4525+
4a4d8108
AM
4526+/* ext2 has 3 types of operations at least, ext3 has 4 */
4527+#define AuBrDynOp (AuDyLast * 4)
4528+
1716fcea
AM
4529+#ifdef CONFIG_AUFS_HFSNOTIFY
4530+/* support for asynchronous destruction */
4531+struct au_br_hfsnotify {
4532+ struct fsnotify_group *hfsn_group;
4533+};
4534+#endif
4535+
392086de
AM
4536+/* sysfs entries */
4537+struct au_brsysfs {
4538+ char name[16];
4539+ struct attribute attr;
4540+};
4541+
4542+enum {
4543+ AuBrSysfs_BR,
4544+ AuBrSysfs_BRID,
4545+ AuBrSysfs_Last
4546+};
4547+
1facf9fc 4548+/* protected by superblock rwsem */
4549+struct au_branch {
062440b3 4550+ struct au_xino *br_xino;
1facf9fc 4551+
4552+ aufs_bindex_t br_id;
4553+
4554+ int br_perm;
86dc4139 4555+ struct path br_path;
4a4d8108
AM
4556+ spinlock_t br_dykey_lock;
4557+ struct au_dykey *br_dykey[AuBrDynOp];
acd2b654
AM
4558+ au_lcnt_t br_nfiles; /* opened files */
4559+ au_lcnt_t br_count; /* in-use for other */
1facf9fc 4560+
4561+ struct au_wbr *br_wbr;
076b876e 4562+ struct au_br_fhsm *br_fhsm;
1facf9fc 4563+
027c5e7a 4564+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4565+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4566+#endif
4567+
1facf9fc 4568+#ifdef CONFIG_SYSFS
392086de
AM
4569+ /* entries under sysfs per mount-point */
4570+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4571+#endif
8b6a4947 4572+
062440b3
AM
4573+#ifdef CONFIG_DEBUG_FS
4574+ struct dentry *br_dbgaufs; /* xino */
4575+#endif
4576+
8b6a4947 4577+ struct au_dr_br br_dirren;
1facf9fc 4578+};
4579+
4580+/* ---------------------------------------------------------------------- */
4581+
86dc4139
AM
4582+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4583+{
4584+ return br->br_path.mnt;
4585+}
4586+
4587+static inline struct dentry *au_br_dentry(struct au_branch *br)
4588+{
4589+ return br->br_path.dentry;
4590+}
4591+
4592+static inline struct super_block *au_br_sb(struct au_branch *br)
4593+{
4594+ return au_br_mnt(br)->mnt_sb;
4595+}
4596+
1facf9fc 4597+static inline int au_br_rdonly(struct au_branch *br)
4598+{
8b6a4947 4599+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4600+ || !au_br_writable(br->br_perm))
4601+ ? -EROFS : 0;
4602+}
4603+
4a4d8108 4604+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4605+{
4a4d8108 4606+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4607+ return !(brperm & AuBrPerm_RR);
1facf9fc 4608+#else
4609+ return 0;
4610+#endif
4611+}
4612+
b912730e
AM
4613+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4614+{
4615+ int err, exec_flag;
4616+
4617+ err = 0;
4618+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4619+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4620+ err = -EACCES;
4621+
4622+ return err;
4623+}
4624+
062440b3
AM
4625+static inline void au_xino_get(struct au_branch *br)
4626+{
4627+ struct au_xino *xi;
4628+
4629+ xi = br->br_xino;
4630+ if (xi)
4631+ kref_get(&xi->xi_kref);
4632+}
4633+
4634+static inline int au_xino_count(struct au_branch *br)
4635+{
4636+ int v;
4637+ struct au_xino *xi;
4638+
4639+ v = 0;
4640+ xi = br->br_xino;
4641+ if (xi)
4642+ v = kref_read(&xi->xi_kref);
4643+
4644+ return v;
4645+}
4646+
1facf9fc 4647+/* ---------------------------------------------------------------------- */
4648+
4649+/* branch.c */
4650+struct au_sbinfo;
4651+void au_br_free(struct au_sbinfo *sinfo);
4652+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4653+struct au_opt_add;
4654+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4655+struct au_opt_del;
4656+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4657+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4658+#ifdef CONFIG_COMPAT
4659+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4660+#endif
1facf9fc 4661+struct au_opt_mod;
4662+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4663+ int *do_refresh);
076b876e
AM
4664+struct aufs_stfs;
4665+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4666+
4667+/* xino.c */
4668+static const loff_t au_loff_max = LLONG_MAX;
4669+
acd2b654 4670+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
83b672a5
AM
4671+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4672+ int wbrtop);
062440b3
AM
4673+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4674+ struct file *copy_src);
acd2b654
AM
4675+struct au_xi_new {
4676+ struct au_xino *xi; /* switch between xino and xigen */
4677+ int idx;
4678+ struct path *base;
4679+ struct file *copy_src;
4680+};
4681+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
062440b3
AM
4682+
4683+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4684+ ino_t *ino);
4685+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4686+ ino_t ino);
5527c038 4687+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4688+ loff_t *pos);
5527c038
JR
4689+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4690+ size_t size, loff_t *pos);
062440b3
AM
4691+
4692+int au_xib_trunc(struct super_block *sb);
acd2b654 4693+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
1facf9fc 4694+
acd2b654 4695+struct au_xino *au_xino_alloc(unsigned int nfile);
062440b3 4696+int au_xino_put(struct au_branch *br);
acd2b654 4697+struct file *au_xino_file1(struct au_xino *xi);
062440b3 4698+
1facf9fc 4699+struct au_opt_xino;
1facf9fc 4700+void au_xino_clr(struct super_block *sb);
062440b3 4701+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
1facf9fc 4702+struct file *au_xino_def(struct super_block *sb);
062440b3
AM
4703+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4704+ struct path *base);
4705+
4706+ino_t au_xino_new_ino(struct super_block *sb);
4707+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4708+
521ced18
JR
4709+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4710+ ino_t h_ino, int idx);
4711+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4712+ int *idx);
4713+
062440b3
AM
4714+int au_xino_path(struct seq_file *seq, struct file *file);
4715+
1facf9fc 4716+/* ---------------------------------------------------------------------- */
4717+
acd2b654
AM
4718+/* @idx is signed to accept -1 meaning the first file */
4719+static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4720+{
4721+ struct file *file;
4722+
4723+ file = NULL;
4724+ if (!xi)
4725+ goto out;
4726+
4727+ if (idx >= 0) {
4728+ if (idx < xi->xi_nfile)
4729+ file = xi->xi_file[idx];
4730+ } else
4731+ file = au_xino_file1(xi);
4732+
4733+out:
4734+ return file;
4735+}
4736+
4737+/* ---------------------------------------------------------------------- */
4738+
1facf9fc 4739+/* Superblock to branch */
4740+static inline
4741+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4742+{
4743+ return au_sbr(sb, bindex)->br_id;
4744+}
4745+
4746+static inline
4747+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4748+{
86dc4139 4749+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4750+}
4751+
4752+static inline
4753+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4754+{
86dc4139 4755+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4756+}
4757+
1facf9fc 4758+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4759+{
4760+ return au_sbr(sb, bindex)->br_perm;
4761+}
4762+
4763+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4764+{
4765+ return au_br_whable(au_sbr_perm(sb, bindex));
4766+}
4767+
4768+/* ---------------------------------------------------------------------- */
4769+
8b6a4947
AM
4770+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4771+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4772+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4773+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4774+/*
8b6a4947
AM
4775+#define wbr_wh_read_trylock_nested(wbr) \
4776+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4777+#define wbr_wh_write_trylock_nested(wbr) \
4778+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4779+*/
1facf9fc 4780+
8b6a4947
AM
4781+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4782+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4783+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4784+
4785+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4786+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4787+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4788+
076b876e
AM
4789+/* ---------------------------------------------------------------------- */
4790+
4791+#ifdef CONFIG_AUFS_FHSM
4792+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4793+{
4794+ mutex_init(&brfhsm->bf_lock);
4795+ brfhsm->bf_jiffy = 0;
4796+ brfhsm->bf_readable = 0;
4797+}
4798+
4799+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4800+{
4801+ mutex_destroy(&brfhsm->bf_lock);
4802+}
4803+#else
4804+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4805+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4806+#endif
4807+
1facf9fc 4808+#endif /* __KERNEL__ */
4809+#endif /* __AUFS_BRANCH_H__ */
7f207e10 4810diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
eca34b5c
AM
4811--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
4812+++ linux/fs/aufs/conf.mk 2019-07-11 15:42:14.462237786 +0200
2121bcd9
AM
4813@@ -0,0 +1,40 @@
4814+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4815+
4816+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4817+
4818+define AuConf
4819+ifdef ${1}
4820+AuConfStr += ${1}=${${1}}
4821+endif
4822+endef
4823+
b752ccd1 4824+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4825+ SBILIST \
7f207e10 4826+ HNOTIFY HFSNOTIFY \
4a4d8108 4827+ EXPORT INO_T_64 \
c1595e42 4828+ XATTR \
076b876e 4829+ FHSM \
4a4d8108 4830+ RDU \
8b6a4947 4831+ DIRREN \
4a4d8108
AM
4832+ SHWH \
4833+ BR_RAMFS \
4834+ BR_FUSE POLL \
4835+ BR_HFSPLUS \
4836+ BDEV_LOOP \
b752ccd1
AM
4837+ DEBUG MAGIC_SYSRQ
4838+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4839+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4840+
4841+AuConfName = ${obj}/conf.str
4842+${AuConfName}.tmp: FORCE
4843+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4844+${AuConfName}: ${AuConfName}.tmp
4845+ @diff -q $< $@ > /dev/null 2>&1 || { \
4846+ echo ' GEN ' $@; \
4847+ cp -p $< $@; \
4848+ }
4849+FORCE:
4850+clean-files += ${AuConfName} ${AuConfName}.tmp
4851+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4852+
4853+-include ${srctree}/${src}/conf_priv.mk
7f207e10 4854diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
eca34b5c 4855--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
016522bc 4856+++ linux/fs/aufs/cpup.c 2020-01-27 10:57:18.165538015 +0100
9f237c51 4857@@ -0,0 +1,1458 @@
cd7a4cd9 4858+// SPDX-License-Identifier: GPL-2.0
1facf9fc 4859+/*
016522bc 4860+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 4861+ *
4862+ * This program, aufs is free software; you can redistribute it and/or modify
4863+ * it under the terms of the GNU General Public License as published by
4864+ * the Free Software Foundation; either version 2 of the License, or
4865+ * (at your option) any later version.
dece6358
AM
4866+ *
4867+ * This program is distributed in the hope that it will be useful,
4868+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4869+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4870+ * GNU General Public License for more details.
4871+ *
4872+ * You should have received a copy of the GNU General Public License
523b37e3 4873+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4874+ */
4875+
4876+/*
4877+ * copy-up functions, see wbr_policy.c for copy-down
4878+ */
4879+
4880+#include <linux/fs_stack.h>
dece6358 4881+#include <linux/mm.h>
8cdd5066 4882+#include <linux/task_work.h>
1facf9fc 4883+#include "aufs.h"
4884+
86dc4139 4885+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4886+{
4887+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4888+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4889+
86dc4139
AM
4890+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4891+
4892+ dst->i_flags |= iflags & ~mask;
1facf9fc 4893+ if (au_test_fs_notime(dst->i_sb))
4894+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4895+}
4896+
4897+void au_cpup_attr_timesizes(struct inode *inode)
4898+{
4899+ struct inode *h_inode;
4900+
5afbbe0d 4901+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4902+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 4903+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 4904+}
4905+
4906+void au_cpup_attr_nlink(struct inode *inode, int force)
4907+{
4908+ struct inode *h_inode;
4909+ struct super_block *sb;
5afbbe0d 4910+ aufs_bindex_t bindex, bbot;
1facf9fc 4911+
4912+ sb = inode->i_sb;
5afbbe0d 4913+ bindex = au_ibtop(inode);
1facf9fc 4914+ h_inode = au_h_iptr(inode, bindex);
4915+ if (!force
4916+ && !S_ISDIR(h_inode->i_mode)
4917+ && au_opt_test(au_mntflags(sb), PLINK)
4918+ && au_plink_test(inode))
4919+ return;
4920+
7eafdf33
AM
4921+ /*
4922+ * 0 can happen in revalidating.
38d290e6
JR
4923+ * h_inode->i_mutex may not be held here, but it is harmless since once
4924+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
4925+ * case.
4926+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4927+ * the incorrect link count.
7eafdf33 4928+ */
92d182d2 4929+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 4930+
4931+ /*
4932+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4933+ * it may includes whplink directory.
4934+ */
4935+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
4936+ bbot = au_ibbot(inode);
4937+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 4938+ h_inode = au_h_iptr(inode, bindex);
4939+ if (h_inode)
4940+ au_add_nlink(inode, h_inode);
4941+ }
4942+ }
4943+}
4944+
4945+void au_cpup_attr_changeable(struct inode *inode)
4946+{
4947+ struct inode *h_inode;
4948+
5afbbe0d 4949+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4950+ inode->i_mode = h_inode->i_mode;
4951+ inode->i_uid = h_inode->i_uid;
4952+ inode->i_gid = h_inode->i_gid;
4953+ au_cpup_attr_timesizes(inode);
86dc4139 4954+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 4955+}
4956+
4957+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4958+{
4959+ struct au_iinfo *iinfo = au_ii(inode);
4960+
1308ab2a 4961+ IiMustWriteLock(inode);
4962+
1facf9fc 4963+ iinfo->ii_higen = h_inode->i_generation;
4964+ iinfo->ii_hsb1 = h_inode->i_sb;
4965+}
4966+
4967+void au_cpup_attr_all(struct inode *inode, int force)
4968+{
4969+ struct inode *h_inode;
4970+
5afbbe0d 4971+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4972+ au_cpup_attr_changeable(inode);
4973+ if (inode->i_nlink > 0)
4974+ au_cpup_attr_nlink(inode, force);
4975+ inode->i_rdev = h_inode->i_rdev;
4976+ inode->i_blkbits = h_inode->i_blkbits;
4977+ au_cpup_igen(inode, h_inode);
4978+}
4979+
4980+/* ---------------------------------------------------------------------- */
4981+
4982+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4983+
4984+/* keep the timestamps of the parent dir when cpup */
4985+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4986+ struct path *h_path)
4987+{
4988+ struct inode *h_inode;
4989+
4990+ dt->dt_dentry = dentry;
4991+ dt->dt_h_path = *h_path;
5527c038 4992+ h_inode = d_inode(h_path->dentry);
1facf9fc 4993+ dt->dt_atime = h_inode->i_atime;
4994+ dt->dt_mtime = h_inode->i_mtime;
4995+ /* smp_mb(); */
4996+}
4997+
4998+void au_dtime_revert(struct au_dtime *dt)
4999+{
5000+ struct iattr attr;
5001+ int err;
5002+
5003+ attr.ia_atime = dt->dt_atime;
5004+ attr.ia_mtime = dt->dt_mtime;
5005+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5006+ | ATTR_ATIME | ATTR_ATIME_SET;
5007+
523b37e3
AM
5008+ /* no delegation since this is a directory */
5009+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 5010+ if (unlikely(err))
0c3ec466 5011+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 5012+}
5013+
5014+/* ---------------------------------------------------------------------- */
5015+
86dc4139
AM
5016+/* internal use only */
5017+struct au_cpup_reg_attr {
5018+ int valid;
5019+ struct kstat st;
5020+ unsigned int iflags; /* inode->i_flags */
5021+};
5022+
1facf9fc 5023+static noinline_for_stack
86dc4139
AM
5024+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5025+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5026+{
c1595e42 5027+ int err, sbits, icex;
7e9cd9fe
AM
5028+ unsigned int mnt_flags;
5029+ unsigned char verbose;
1facf9fc 5030+ struct iattr ia;
5031+ struct path h_path;
1308ab2a 5032+ struct inode *h_isrc, *h_idst;
86dc4139 5033+ struct kstat *h_st;
c1595e42 5034+ struct au_branch *br;
1facf9fc 5035+
5036+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 5037+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
5038+ br = au_sbr(dst->d_sb, bindex);
5039+ h_path.mnt = au_br_mnt(br);
5527c038 5040+ h_isrc = d_inode(h_src);
1308ab2a 5041+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 5042+ | ATTR_ATIME | ATTR_MTIME
5043+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
5044+ if (h_src_attr && h_src_attr->valid) {
5045+ h_st = &h_src_attr->st;
5046+ ia.ia_uid = h_st->uid;
5047+ ia.ia_gid = h_st->gid;
5048+ ia.ia_atime = h_st->atime;
5049+ ia.ia_mtime = h_st->mtime;
5050+ if (h_idst->i_mode != h_st->mode
5051+ && !S_ISLNK(h_idst->i_mode)) {
5052+ ia.ia_valid |= ATTR_MODE;
5053+ ia.ia_mode = h_st->mode;
5054+ }
5055+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5056+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5057+ } else {
5058+ ia.ia_uid = h_isrc->i_uid;
5059+ ia.ia_gid = h_isrc->i_gid;
5060+ ia.ia_atime = h_isrc->i_atime;
5061+ ia.ia_mtime = h_isrc->i_mtime;
5062+ if (h_idst->i_mode != h_isrc->i_mode
5063+ && !S_ISLNK(h_idst->i_mode)) {
5064+ ia.ia_valid |= ATTR_MODE;
5065+ ia.ia_mode = h_isrc->i_mode;
5066+ }
5067+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5068+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 5069+ }
523b37e3
AM
5070+ /* no delegation since it is just created */
5071+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5072+
5073+ /* is this nfs only? */
5074+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5075+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5076+ ia.ia_mode = h_isrc->i_mode;
523b37e3 5077+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5078+ }
5079+
c1595e42 5080+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
5081+ if (!err) {
5082+ mnt_flags = au_mntflags(dst->d_sb);
5083+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5084+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5085+ }
c1595e42 5086+
1facf9fc 5087+ return err;
5088+}
5089+
5090+/* ---------------------------------------------------------------------- */
5091+
5092+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5093+ char *buf, unsigned long blksize)
5094+{
5095+ int err;
5096+ size_t sz, rbytes, wbytes;
5097+ unsigned char all_zero;
5098+ char *p, *zp;
febd17d6 5099+ struct inode *h_inode;
1facf9fc 5100+ /* reduce stack usage */
5101+ struct iattr *ia;
5102+
5103+ zp = page_address(ZERO_PAGE(0));
5104+ if (unlikely(!zp))
5105+ return -ENOMEM; /* possible? */
5106+
5107+ err = 0;
5108+ all_zero = 0;
5109+ while (len) {
5110+ AuDbg("len %lld\n", len);
5111+ sz = blksize;
5112+ if (len < blksize)
5113+ sz = len;
5114+
5115+ rbytes = 0;
5116+ /* todo: signal_pending? */
5117+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5118+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5119+ err = rbytes;
5120+ }
5121+ if (unlikely(err < 0))
5122+ break;
5123+
5124+ all_zero = 0;
5125+ if (len >= rbytes && rbytes == blksize)
5126+ all_zero = !memcmp(buf, zp, rbytes);
5127+ if (!all_zero) {
5128+ wbytes = rbytes;
5129+ p = buf;
5130+ while (wbytes) {
5131+ size_t b;
5132+
5133+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5134+ err = b;
5135+ /* todo: signal_pending? */
5136+ if (unlikely(err == -EAGAIN || err == -EINTR))
5137+ continue;
5138+ if (unlikely(err < 0))
5139+ break;
5140+ wbytes -= b;
5141+ p += b;
5142+ }
392086de
AM
5143+ if (unlikely(err < 0))
5144+ break;
1facf9fc 5145+ } else {
5146+ loff_t res;
5147+
5148+ AuLabel(hole);
5149+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5150+ err = res;
5151+ if (unlikely(res < 0))
5152+ break;
5153+ }
5154+ len -= rbytes;
5155+ err = 0;
5156+ }
5157+
5158+ /* the last block may be a hole */
5159+ if (!err && all_zero) {
5160+ AuLabel(last hole);
5161+
5162+ err = 1;
2000de60 5163+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5164+ /* nfs requires this step to make last hole */
5165+ /* is this only nfs? */
5166+ do {
5167+ /* todo: signal_pending? */
5168+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5169+ } while (err == -EAGAIN || err == -EINTR);
5170+ if (err == 1)
5171+ dst->f_pos--;
5172+ }
5173+
5174+ if (err == 1) {
5175+ ia = (void *)buf;
5176+ ia->ia_size = dst->f_pos;
5177+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5178+ ia->ia_file = dst;
febd17d6
JR
5179+ h_inode = file_inode(dst);
5180+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5181+ /* no delegation since it is just created */
5182+ err = vfsub_notify_change(&dst->f_path, ia,
5183+ /*delegated*/NULL);
febd17d6 5184+ inode_unlock(h_inode);
1facf9fc 5185+ }
5186+ }
5187+
5188+ return err;
5189+}
5190+
5191+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5192+{
5193+ int err;
5194+ unsigned long blksize;
5195+ unsigned char do_kfree;
5196+ char *buf;
9f237c51 5197+ struct super_block *h_sb;
1facf9fc 5198+
5199+ err = -ENOMEM;
9f237c51
AM
5200+ h_sb = file_inode(dst)->i_sb;
5201+ blksize = h_sb->s_blocksize;
1facf9fc 5202+ if (!blksize || PAGE_SIZE < blksize)
5203+ blksize = PAGE_SIZE;
5204+ AuDbg("blksize %lu\n", blksize);
5205+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5206+ if (do_kfree)
5207+ buf = kmalloc(blksize, GFP_NOFS);
5208+ else
5209+ buf = (void *)__get_free_page(GFP_NOFS);
5210+ if (unlikely(!buf))
5211+ goto out;
5212+
5213+ if (len > (1 << 22))
5214+ AuDbg("copying a large file %lld\n", (long long)len);
5215+
5216+ src->f_pos = 0;
5217+ dst->f_pos = 0;
5218+ err = au_do_copy_file(dst, src, len, buf, blksize);
9f237c51
AM
5219+ if (do_kfree) {
5220+ AuDebugOn(!au_kfree_do_sz_test(blksize));
5221+ au_kfree_do_rcu(buf);
5222+ } else
1c60b727 5223+ free_page((unsigned long)buf);
1facf9fc 5224+
4f0767ce 5225+out:
1facf9fc 5226+ return err;
5227+}
5228+
1c60b727
AM
5229+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5230+{
5231+ int err;
5232+ struct super_block *h_src_sb;
5233+ struct inode *h_src_inode;
5234+
5235+ h_src_inode = file_inode(src);
5236+ h_src_sb = h_src_inode->i_sb;
5237+
5238+ /* XFS acquires inode_lock */
5239+ if (!au_test_xfs(h_src_sb))
5240+ err = au_copy_file(dst, src, len);
5241+ else {
3c1bdaff 5242+ inode_unlock_shared(h_src_inode);
1c60b727 5243+ err = au_copy_file(dst, src, len);
be118d29 5244+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5245+ }
5246+
5247+ return err;
5248+}
5249+
5250+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5251+{
5252+ int err;
9f237c51 5253+ loff_t lo;
1c60b727
AM
5254+ struct super_block *h_src_sb;
5255+ struct inode *h_src_inode;
5256+
5257+ h_src_inode = file_inode(src);
5258+ h_src_sb = h_src_inode->i_sb;
5259+ if (h_src_sb != file_inode(dst)->i_sb
9f237c51 5260+ || !dst->f_op->remap_file_range) {
1c60b727
AM
5261+ err = au_do_copy(dst, src, len);
5262+ goto out;
5263+ }
5264+
5265+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5266+ inode_unlock_shared(h_src_inode);
9f237c51 5267+ lo = vfsub_clone_file_range(src, dst, len);
be118d29 5268+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727 5269+ } else
9f237c51
AM
5270+ lo = vfsub_clone_file_range(src, dst, len);
5271+ if (lo == len) {
5272+ err = 0;
5273+ goto out; /* success */
5274+ } else if (lo >= 0)
5275+ /* todo: possible? */
5276+ /* paritially succeeded */
5277+ AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5278+ else if (lo != -EOPNOTSUPP) {
5279+ /* older XFS has a condition in cloning */
5280+ err = lo;
1c60b727 5281+ goto out;
9f237c51 5282+ }
1c60b727
AM
5283+
5284+ /* the backend fs on NFS may not support cloning */
5285+ err = au_do_copy(dst, src, len);
5286+
5287+out:
5288+ AuTraceErr(err);
5289+ return err;
5290+}
5291+
1facf9fc 5292+/*
5293+ * to support a sparse file which is opened with O_APPEND,
5294+ * we need to close the file.
5295+ */
c2b27bf2 5296+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5297+{
5298+ int err, i;
5299+ enum { SRC, DST };
5300+ struct {
5301+ aufs_bindex_t bindex;
5302+ unsigned int flags;
5303+ struct dentry *dentry;
392086de 5304+ int force_wr;
1facf9fc 5305+ struct file *file;
1facf9fc 5306+ } *f, file[] = {
5307+ {
c2b27bf2 5308+ .bindex = cpg->bsrc,
1facf9fc 5309+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
1facf9fc 5310+ },
5311+ {
c2b27bf2 5312+ .bindex = cpg->bdst,
1facf9fc 5313+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5314+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
1facf9fc 5315+ }
5316+ };
acd2b654 5317+ struct au_branch *br;
521ced18 5318+ struct super_block *sb, *h_src_sb;
e2f27e51 5319+ struct inode *h_src_inode;
8cdd5066 5320+ struct task_struct *tsk = current;
1facf9fc 5321+
5322+ /* bsrc branch can be ro/rw. */
c2b27bf2 5323+ sb = cpg->dentry->d_sb;
1facf9fc 5324+ f = file;
5325+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5326+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5327+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5328+ /*file*/NULL, f->force_wr);
9f237c51
AM
5329+ if (IS_ERR(f->file)) {
5330+ err = PTR_ERR(f->file);
5331+ if (i == SRC)
5332+ goto out;
5333+ else
5334+ goto out_src;
5335+ }
1facf9fc 5336+ }
5337+
5338+ /* try stopping to update while we copyup */
e2f27e51 5339+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5340+ h_src_sb = h_src_inode->i_sb;
5341+ if (!au_test_nfs(h_src_sb))
e2f27e51 5342+ IMustLock(h_src_inode);
1c60b727 5343+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5344+
8cdd5066
JR
5345+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5346+ if (tsk->flags & PF_KTHREAD)
5347+ __fput_sync(file[DST].file);
5348+ else {
062440b3 5349+ /* it happened actually */
8cdd5066
JR
5350+ fput(file[DST].file);
5351+ /*
5352+ * too bad.
5353+ * we have to call both since we don't know which place the file
5354+ * was added to.
5355+ */
5356+ task_work_run();
5357+ flush_delayed_fput();
5358+ }
acd2b654
AM
5359+ br = au_sbr(sb, file[DST].bindex);
5360+ au_lcnt_dec(&br->br_nfiles);
523b37e3 5361+
4f0767ce 5362+out_src:
1facf9fc 5363+ fput(file[SRC].file);
acd2b654
AM
5364+ br = au_sbr(sb, file[SRC].bindex);
5365+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 5366+out:
1facf9fc 5367+ return err;
5368+}
5369+
c2b27bf2 5370+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5371+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5372+{
5373+ int err, rerr;
5374+ loff_t l;
86dc4139 5375+ struct path h_path;
38d290e6 5376+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5377+
5378+ err = 0;
5527c038 5379+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5380+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5381+ if (cpg->len == -1 || l < cpg->len)
5382+ cpg->len = l;
5383+ if (cpg->len) {
86dc4139 5384+ /* try stopping to update while we are referencing */
be118d29 5385+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5386+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5387+
c2b27bf2
AM
5388+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5389+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5390+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5391+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5392+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5393+ else {
3c1bdaff 5394+ inode_unlock_shared(h_src_inode);
521ced18 5395+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5396+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5397+ }
86dc4139 5398+ if (unlikely(err)) {
3c1bdaff 5399+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5400+ goto out;
5401+ }
5402+ h_src_attr->valid = 1;
e2f27e51
AM
5403+ if (!au_test_nfs(h_src_inode->i_sb)) {
5404+ err = au_cp_regular(cpg);
3c1bdaff 5405+ inode_unlock_shared(h_src_inode);
e2f27e51 5406+ } else {
3c1bdaff 5407+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5408+ err = au_cp_regular(cpg);
5409+ }
c2b27bf2 5410+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5411+ if (!err && rerr)
5412+ err = rerr;
1facf9fc 5413+ }
38d290e6
JR
5414+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5415+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5416+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5417+ spin_lock(&h_dst_inode->i_lock);
5418+ h_dst_inode->i_state |= I_LINKABLE;
5419+ spin_unlock(&h_dst_inode->i_lock);
5420+ }
1facf9fc 5421+
4f0767ce 5422+out:
1facf9fc 5423+ return err;
5424+}
5425+
5426+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5427+ struct inode *h_dir)
5428+{
5429+ int err, symlen;
5430+ mm_segment_t old_fs;
b752ccd1
AM
5431+ union {
5432+ char *k;
5433+ char __user *u;
5434+ } sym;
1facf9fc 5435+
5436+ err = -ENOMEM;
537831f9 5437+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5438+ if (unlikely(!sym.k))
1facf9fc 5439+ goto out;
5440+
9dbd164d 5441+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5442+ old_fs = get_fs();
5443+ set_fs(KERNEL_DS);
a2654f78 5444+ symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5445+ err = symlen;
5446+ set_fs(old_fs);
5447+
5448+ if (symlen > 0) {
b752ccd1
AM
5449+ sym.k[symlen] = 0;
5450+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5451+ }
1c60b727 5452+ free_page((unsigned long)sym.k);
1facf9fc 5453+
4f0767ce 5454+out:
1facf9fc 5455+ return err;
5456+}
5457+
8cdd5066
JR
5458+/*
5459+ * regardless 'acl' option, reset all ACL.
5460+ * All ACL will be copied up later from the original entry on the lower branch.
5461+ */
5462+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5463+{
5464+ int err;
5465+ struct dentry *h_dentry;
5466+ struct inode *h_inode;
5467+
5468+ h_dentry = h_path->dentry;
5469+ h_inode = d_inode(h_dentry);
5470+ /* forget_all_cached_acls(h_inode)); */
5471+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5472+ AuTraceErr(err);
5473+ if (err == -EOPNOTSUPP)
5474+ err = 0;
5475+ if (!err)
5476+ err = vfsub_acl_chmod(h_inode, mode);
5477+
5478+ AuTraceErr(err);
5479+ return err;
5480+}
5481+
5482+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5483+ struct inode *h_dir, struct path *h_path)
5484+{
5485+ int err;
5486+ struct inode *dir, *inode;
5487+
5488+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5489+ AuTraceErr(err);
5490+ if (err == -EOPNOTSUPP)
5491+ err = 0;
5492+ if (unlikely(err))
5493+ goto out;
5494+
5495+ /*
5496+ * strange behaviour from the users view,
acd2b654 5497+ * particularly setattr case
8cdd5066
JR
5498+ */
5499+ dir = d_inode(dst_parent);
5afbbe0d 5500+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5501+ au_cpup_attr_nlink(dir, /*force*/1);
5502+ inode = d_inode(cpg->dentry);
5503+ au_cpup_attr_nlink(inode, /*force*/1);
5504+
5505+out:
5506+ return err;
5507+}
5508+
1facf9fc 5509+static noinline_for_stack
c2b27bf2 5510+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5511+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5512+{
5513+ int err;
5514+ umode_t mode;
5515+ unsigned int mnt_flags;
076b876e 5516+ unsigned char isdir, isreg, force;
c2b27bf2 5517+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5518+ struct au_dtime dt;
5519+ struct path h_path;
5520+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5521+ struct inode *h_inode, *h_dir;
1facf9fc 5522+ struct super_block *sb;
5523+
5524+ /* bsrc branch can be ro/rw. */
c2b27bf2 5525+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5526+ h_inode = d_inode(h_src);
5527+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5528+
5529+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5530+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5531+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5532+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5533+ AUFS_WH_PFX_LEN));
1facf9fc 5534+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5535+ h_dir = d_inode(h_parent);
1facf9fc 5536+ IMustLock(h_dir);
5537+ AuDebugOn(h_parent != h_dst->d_parent);
5538+
c2b27bf2
AM
5539+ sb = cpg->dentry->d_sb;
5540+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5541+ if (do_dt) {
5542+ h_path.dentry = h_parent;
5543+ au_dtime_store(&dt, dst_parent, &h_path);
5544+ }
5545+ h_path.dentry = h_dst;
5546+
076b876e 5547+ isreg = 0;
1facf9fc 5548+ isdir = 0;
5549+ mode = h_inode->i_mode;
5550+ switch (mode & S_IFMT) {
5551+ case S_IFREG:
076b876e 5552+ isreg = 1;
cd7a4cd9 5553+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
1facf9fc 5554+ if (!err)
c2b27bf2 5555+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5556+ break;
5557+ case S_IFDIR:
5558+ isdir = 1;
5559+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5560+ if (!err)
5561+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5562+ break;
5563+ case S_IFLNK:
5564+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5565+ break;
5566+ case S_IFCHR:
5567+ case S_IFBLK:
5568+ AuDebugOn(!capable(CAP_MKNOD));
5569+ /*FALLTHROUGH*/
5570+ case S_IFIFO:
5571+ case S_IFSOCK:
5572+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5573+ break;
5574+ default:
5575+ AuIOErr("Unknown inode type 0%o\n", mode);
5576+ err = -EIO;
5577+ }
8cdd5066
JR
5578+ if (!err)
5579+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5580+
5581+ mnt_flags = au_mntflags(sb);
5582+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5583+ && !isdir
5584+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5585+ && (h_inode->i_nlink == 1
5586+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5587+ /* todo: unnecessary? */
5527c038 5588+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5589+ && cpg->bdst < cpg->bsrc
5590+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5591+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5592+ /* ignore this error */
5593+
076b876e
AM
5594+ if (!err) {
5595+ force = 0;
5596+ if (isreg) {
5597+ force = !!cpg->len;
5598+ if (cpg->len == -1)
5599+ force = !!i_size_read(h_inode);
5600+ }
5601+ au_fhsm_wrote(sb, cpg->bdst, force);
5602+ }
5603+
1facf9fc 5604+ if (do_dt)
5605+ au_dtime_revert(&dt);
5606+ return err;
5607+}
5608+
392086de 5609+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5610+{
5611+ int err;
392086de 5612+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5613+ struct inode *h_dir;
392086de 5614+ aufs_bindex_t bdst;
86dc4139 5615+
392086de
AM
5616+ dentry = cpg->dentry;
5617+ bdst = cpg->bdst;
5618+ h_dentry = au_h_dptr(dentry, bdst);
5619+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5620+ dget(h_dentry);
5621+ au_set_h_dptr(dentry, bdst, NULL);
5622+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5623+ if (!err)
5624+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5625+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5626+ } else {
5627+ err = 0;
5628+ parent = dget_parent(dentry);
5629+ h_parent = au_h_dptr(parent, bdst);
5630+ dput(parent);
5631+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5632+ if (IS_ERR(h_path->dentry))
5633+ err = PTR_ERR(h_path->dentry);
86dc4139 5634+ }
392086de
AM
5635+ if (unlikely(err))
5636+ goto out;
86dc4139 5637+
86dc4139 5638+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5639+ h_dir = d_inode(h_parent);
86dc4139 5640+ IMustLock(h_dir);
523b37e3
AM
5641+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5642+ /* no delegation since it is just created */
f2c43d5f
AM
5643+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5644+ /*flags*/0);
86dc4139
AM
5645+ dput(h_path->dentry);
5646+
5647+out:
5648+ return err;
5649+}
5650+
1facf9fc 5651+/*
5652+ * copyup the @dentry from @bsrc to @bdst.
5653+ * the caller must set the both of lower dentries.
5654+ * @len is for truncating when it is -1 copyup the entire file.
5655+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5656+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f 5657+ * aufs doesn't touch the credential so
acd2b654 5658+ * security_inode_copy_up{,_xattr}() are unnecessary.
1facf9fc 5659+ */
c2b27bf2 5660+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5661+{
5662+ int err, rerr;
5afbbe0d 5663+ aufs_bindex_t old_ibtop;
1facf9fc 5664+ unsigned char isdir, plink;
1facf9fc 5665+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5666+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5667+ struct super_block *sb;
86dc4139 5668+ struct au_branch *br;
acd2b654 5669+ /* to reduce stack size */
c2b27bf2
AM
5670+ struct {
5671+ struct au_dtime dt;
5672+ struct path h_path;
5673+ struct au_cpup_reg_attr h_src_attr;
5674+ } *a;
1facf9fc 5675+
c2b27bf2
AM
5676+ err = -ENOMEM;
5677+ a = kmalloc(sizeof(*a), GFP_NOFS);
5678+ if (unlikely(!a))
5679+ goto out;
5680+ a->h_src_attr.valid = 0;
1facf9fc 5681+
c2b27bf2
AM
5682+ sb = cpg->dentry->d_sb;
5683+ br = au_sbr(sb, cpg->bdst);
5684+ a->h_path.mnt = au_br_mnt(br);
5685+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5686+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5687+ h_dir = d_inode(h_parent);
1facf9fc 5688+ IMustLock(h_dir);
5689+
c2b27bf2 5690+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5691+ inode = d_inode(cpg->dentry);
1facf9fc 5692+
5693+ if (!dst_parent)
c2b27bf2 5694+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5695+ else
5696+ dget(dst_parent);
5697+
5698+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5699+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5700+ if (dst_inode) {
5701+ if (unlikely(!plink)) {
5702+ err = -EIO;
027c5e7a
AM
5703+ AuIOErr("hi%lu(i%lu) exists on b%d "
5704+ "but plink is disabled\n",
c2b27bf2
AM
5705+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5706+ goto out_parent;
1facf9fc 5707+ }
5708+
5709+ if (dst_inode->i_nlink) {
c2b27bf2 5710+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5711+
c2b27bf2 5712+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5713+ err = PTR_ERR(h_src);
5714+ if (IS_ERR(h_src))
c2b27bf2 5715+ goto out_parent;
5527c038 5716+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5717+ err = -EIO;
79b8bda9 5718+ AuIOErr("i%lu exists on b%d "
027c5e7a 5719+ "but not pseudo-linked\n",
79b8bda9 5720+ inode->i_ino, cpg->bdst);
1facf9fc 5721+ dput(h_src);
c2b27bf2 5722+ goto out_parent;
1facf9fc 5723+ }
5724+
5725+ if (do_dt) {
c2b27bf2
AM
5726+ a->h_path.dentry = h_parent;
5727+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5728+ }
86dc4139 5729+
c2b27bf2 5730+ a->h_path.dentry = h_dst;
523b37e3
AM
5731+ delegated = NULL;
5732+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5733+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5734+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5735+ if (do_dt)
c2b27bf2 5736+ au_dtime_revert(&a->dt);
523b37e3
AM
5737+ if (unlikely(err == -EWOULDBLOCK)) {
5738+ pr_warn("cannot retry for NFSv4 delegation"
5739+ " for an internal link\n");
5740+ iput(delegated);
5741+ }
1facf9fc 5742+ dput(h_src);
c2b27bf2 5743+ goto out_parent;
1facf9fc 5744+ } else
5745+ /* todo: cpup_wh_file? */
5746+ /* udba work */
4a4d8108 5747+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5748+ }
5749+
86dc4139 5750+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5751+ old_ibtop = au_ibtop(inode);
c2b27bf2 5752+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5753+ if (unlikely(err))
86dc4139 5754+ goto out_rev;
5527c038 5755+ dst_inode = d_inode(h_dst);
febd17d6 5756+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5757+ /* todo: necessary? */
c2b27bf2 5758+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5759+
c2b27bf2 5760+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5761+ if (unlikely(err)) {
5762+ /* todo: necessary? */
c2b27bf2 5763+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5764+ inode_unlock(dst_inode);
86dc4139
AM
5765+ goto out_rev;
5766+ }
5767+
5afbbe0d 5768+ if (cpg->bdst < old_ibtop) {
86dc4139 5769+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5770+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5771+ if (unlikely(err)) {
c2b27bf2
AM
5772+ /* ignore an error */
5773+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5774+ inode_unlock(dst_inode);
86dc4139 5775+ goto out_rev;
4a4d8108 5776+ }
4a4d8108 5777+ }
5afbbe0d 5778+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5779+ } else
5afbbe0d 5780+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5781+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5782+ au_hi_flags(inode, isdir));
5783+
5784+ /* todo: necessary? */
c2b27bf2 5785+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5786+ inode_unlock(dst_inode);
86dc4139
AM
5787+ if (unlikely(err))
5788+ goto out_rev;
5789+
5527c038 5790+ src_inode = d_inode(h_src);
86dc4139 5791+ if (!isdir
5527c038
JR
5792+ && (src_inode->i_nlink > 1
5793+ || src_inode->i_state & I_LINKABLE)
86dc4139 5794+ && plink)
c2b27bf2 5795+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5796+
c2b27bf2
AM
5797+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5798+ a->h_path.dentry = h_dst;
392086de 5799+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5800+ }
5801+ if (!err)
c2b27bf2 5802+ goto out_parent; /* success */
1facf9fc 5803+
5804+ /* revert */
4a4d8108 5805+out_rev:
c2b27bf2
AM
5806+ a->h_path.dentry = h_parent;
5807+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5808+ a->h_path.dentry = h_dst;
86dc4139 5809+ rerr = 0;
5527c038 5810+ if (d_is_positive(h_dst)) {
523b37e3
AM
5811+ if (!isdir) {
5812+ /* no delegation since it is just created */
5813+ rerr = vfsub_unlink(h_dir, &a->h_path,
5814+ /*delegated*/NULL, /*force*/0);
5815+ } else
c2b27bf2 5816+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5817+ }
c2b27bf2 5818+ au_dtime_revert(&a->dt);
1facf9fc 5819+ if (rerr) {
5820+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5821+ err = -EIO;
5822+ }
c2b27bf2 5823+out_parent:
1facf9fc 5824+ dput(dst_parent);
9f237c51 5825+ au_kfree_rcu(a);
c2b27bf2 5826+out:
1facf9fc 5827+ return err;
5828+}
5829+
7e9cd9fe 5830+#if 0 /* reserved */
1facf9fc 5831+struct au_cpup_single_args {
5832+ int *errp;
c2b27bf2 5833+ struct au_cp_generic *cpg;
1facf9fc 5834+ struct dentry *dst_parent;
5835+};
5836+
5837+static void au_call_cpup_single(void *args)
5838+{
5839+ struct au_cpup_single_args *a = args;
86dc4139 5840+
c2b27bf2
AM
5841+ au_pin_hdir_acquire_nest(a->cpg->pin);
5842+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5843+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5844+}
c2b27bf2 5845+#endif
1facf9fc 5846+
53392da6
AM
5847+/*
5848+ * prevent SIGXFSZ in copy-up.
5849+ * testing CAP_MKNOD is for generic fs,
5850+ * but CAP_FSETID is for xfs only, currently.
5851+ */
86dc4139 5852+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5853+{
5854+ int do_sio;
86dc4139
AM
5855+ struct super_block *sb;
5856+ struct inode *h_dir;
53392da6
AM
5857+
5858+ do_sio = 0;
86dc4139 5859+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5860+ if (!au_wkq_test()
5861+ && (!au_sbi(sb)->si_plink_maint_pid
5862+ || au_plink_maint(sb, AuLock_NOPLM))) {
5863+ switch (mode & S_IFMT) {
5864+ case S_IFREG:
5865+ /* no condition about RLIMIT_FSIZE and the file size */
5866+ do_sio = 1;
5867+ break;
5868+ case S_IFCHR:
5869+ case S_IFBLK:
5870+ do_sio = !capable(CAP_MKNOD);
5871+ break;
5872+ }
5873+ if (!do_sio)
5874+ do_sio = ((mode & (S_ISUID | S_ISGID))
5875+ && !capable(CAP_FSETID));
86dc4139
AM
5876+ /* this workaround may be removed in the future */
5877+ if (!do_sio) {
5878+ h_dir = au_pinned_h_dir(pin);
5879+ do_sio = h_dir->i_mode & S_ISVTX;
5880+ }
53392da6
AM
5881+ }
5882+
5883+ return do_sio;
5884+}
5885+
7e9cd9fe 5886+#if 0 /* reserved */
c2b27bf2 5887+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5888+{
5889+ int err, wkq_err;
1facf9fc 5890+ struct dentry *h_dentry;
5891+
c2b27bf2 5892+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5893+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5894+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5895+ else {
5896+ struct au_cpup_single_args args = {
5897+ .errp = &err,
c2b27bf2
AM
5898+ .cpg = cpg,
5899+ .dst_parent = dst_parent
1facf9fc 5900+ };
5901+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5902+ if (unlikely(wkq_err))
5903+ err = wkq_err;
5904+ }
5905+
5906+ return err;
5907+}
c2b27bf2 5908+#endif
1facf9fc 5909+
5910+/*
5911+ * copyup the @dentry from the first active lower branch to @bdst,
5912+ * using au_cpup_single().
5913+ */
c2b27bf2 5914+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5915+{
5916+ int err;
c2b27bf2
AM
5917+ unsigned int flags_orig;
5918+ struct dentry *dentry;
5919+
5920+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 5921+
c2b27bf2 5922+ dentry = cpg->dentry;
86dc4139 5923+ DiMustWriteLock(dentry);
1facf9fc 5924+
c2b27bf2 5925+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 5926+ if (!err) {
c2b27bf2
AM
5927+ flags_orig = cpg->flags;
5928+ au_fset_cpup(cpg->flags, RENAME);
5929+ err = au_cpup_single(cpg, NULL);
5930+ cpg->flags = flags_orig;
1facf9fc 5931+ if (!err)
5932+ return 0; /* success */
5933+
5934+ /* revert */
c2b27bf2 5935+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 5936+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 5937+ }
5938+
5939+ return err;
5940+}
5941+
5942+struct au_cpup_simple_args {
5943+ int *errp;
c2b27bf2 5944+ struct au_cp_generic *cpg;
1facf9fc 5945+};
5946+
5947+static void au_call_cpup_simple(void *args)
5948+{
5949+ struct au_cpup_simple_args *a = args;
86dc4139 5950+
c2b27bf2
AM
5951+ au_pin_hdir_acquire_nest(a->cpg->pin);
5952+ *a->errp = au_cpup_simple(a->cpg);
5953+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5954+}
5955+
c2b27bf2 5956+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5957+{
5958+ int err, wkq_err;
c2b27bf2
AM
5959+ struct dentry *dentry, *parent;
5960+ struct file *h_file;
1facf9fc 5961+ struct inode *h_dir;
5962+
c2b27bf2
AM
5963+ dentry = cpg->dentry;
5964+ h_file = NULL;
5965+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
5966+ AuDebugOn(cpg->bsrc < 0);
392086de 5967+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
5968+ err = PTR_ERR(h_file);
5969+ if (IS_ERR(h_file))
5970+ goto out;
5971+ }
5972+
1facf9fc 5973+ parent = dget_parent(dentry);
5527c038 5974+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 5975+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 5976+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 5977+ err = au_cpup_simple(cpg);
1facf9fc 5978+ else {
5979+ struct au_cpup_simple_args args = {
5980+ .errp = &err,
c2b27bf2 5981+ .cpg = cpg
1facf9fc 5982+ };
5983+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5984+ if (unlikely(wkq_err))
5985+ err = wkq_err;
5986+ }
5987+
5988+ dput(parent);
c2b27bf2
AM
5989+ if (h_file)
5990+ au_h_open_post(dentry, cpg->bsrc, h_file);
5991+
5992+out:
1facf9fc 5993+ return err;
5994+}
5995+
c2b27bf2 5996+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 5997+{
5afbbe0d 5998+ aufs_bindex_t bsrc, bbot;
c2b27bf2 5999+ struct dentry *dentry, *h_dentry;
367653fa 6000+
c2b27bf2
AM
6001+ if (cpg->bsrc < 0) {
6002+ dentry = cpg->dentry;
5afbbe0d
AM
6003+ bbot = au_dbbot(dentry);
6004+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
6005+ h_dentry = au_h_dptr(dentry, bsrc);
6006+ if (h_dentry) {
5527c038 6007+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
6008+ break;
6009+ }
6010+ }
5afbbe0d 6011+ AuDebugOn(bsrc > bbot);
c2b27bf2 6012+ cpg->bsrc = bsrc;
367653fa 6013+ }
c2b27bf2
AM
6014+ AuDebugOn(cpg->bsrc <= cpg->bdst);
6015+ return au_do_sio_cpup_simple(cpg);
6016+}
367653fa 6017+
c2b27bf2
AM
6018+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6019+{
6020+ AuDebugOn(cpg->bdst <= cpg->bsrc);
6021+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
6022+}
6023+
1facf9fc 6024+/* ---------------------------------------------------------------------- */
6025+
6026+/*
6027+ * copyup the deleted file for writing.
6028+ */
c2b27bf2
AM
6029+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6030+ struct file *file)
1facf9fc 6031+{
6032+ int err;
c2b27bf2
AM
6033+ unsigned int flags_orig;
6034+ aufs_bindex_t bsrc_orig;
c2b27bf2 6035+ struct au_dinfo *dinfo;
5afbbe0d
AM
6036+ struct {
6037+ struct au_hdentry *hd;
6038+ struct dentry *h_dentry;
6039+ } hdst, hsrc;
1facf9fc 6040+
c2b27bf2 6041+ dinfo = au_di(cpg->dentry);
1308ab2a 6042+ AuRwMustWriteLock(&dinfo->di_rwsem);
6043+
c2b27bf2 6044+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
6045+ cpg->bsrc = dinfo->di_btop;
6046+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
6047+ hdst.h_dentry = hdst.hd->hd_dentry;
6048+ hdst.hd->hd_dentry = wh_dentry;
6049+ dinfo->di_btop = cpg->bdst;
6050+
6051+ hsrc.h_dentry = NULL;
027c5e7a 6052+ if (file) {
5afbbe0d
AM
6053+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6054+ hsrc.h_dentry = hsrc.hd->hd_dentry;
6055+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 6056+ }
c2b27bf2
AM
6057+ flags_orig = cpg->flags;
6058+ cpg->flags = !AuCpup_DTIME;
6059+ err = au_cpup_single(cpg, /*h_parent*/NULL);
6060+ cpg->flags = flags_orig;
027c5e7a
AM
6061+ if (file) {
6062+ if (!err)
6063+ err = au_reopen_nondir(file);
5afbbe0d 6064+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 6065+ }
5afbbe0d
AM
6066+ hdst.hd->hd_dentry = hdst.h_dentry;
6067+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 6068+ cpg->bsrc = bsrc_orig;
1facf9fc 6069+
6070+ return err;
6071+}
6072+
c2b27bf2 6073+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6074+{
6075+ int err;
c2b27bf2 6076+ aufs_bindex_t bdst;
1facf9fc 6077+ struct au_dtime dt;
c2b27bf2 6078+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6079+ struct au_branch *br;
6080+ struct path h_path;
6081+
c2b27bf2
AM
6082+ dentry = cpg->dentry;
6083+ bdst = cpg->bdst;
1facf9fc 6084+ br = au_sbr(dentry->d_sb, bdst);
6085+ parent = dget_parent(dentry);
6086+ h_parent = au_h_dptr(parent, bdst);
6087+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6088+ err = PTR_ERR(wh_dentry);
6089+ if (IS_ERR(wh_dentry))
6090+ goto out;
6091+
6092+ h_path.dentry = h_parent;
86dc4139 6093+ h_path.mnt = au_br_mnt(br);
1facf9fc 6094+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6095+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6096+ if (unlikely(err))
6097+ goto out_wh;
6098+
6099+ dget(wh_dentry);
6100+ h_path.dentry = wh_dentry;
2000de60 6101+ if (!d_is_dir(wh_dentry)) {
523b37e3 6102+ /* no delegation since it is just created */
5527c038 6103+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6104+ /*delegated*/NULL, /*force*/0);
6105+ } else
5527c038 6106+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6107+ if (unlikely(err)) {
523b37e3
AM
6108+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6109+ wh_dentry, err);
1facf9fc 6110+ err = -EIO;
6111+ }
6112+ au_dtime_revert(&dt);
5527c038 6113+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6114+
4f0767ce 6115+out_wh:
1facf9fc 6116+ dput(wh_dentry);
4f0767ce 6117+out:
1facf9fc 6118+ dput(parent);
6119+ return err;
6120+}
6121+
6122+struct au_cpup_wh_args {
6123+ int *errp;
c2b27bf2 6124+ struct au_cp_generic *cpg;
1facf9fc 6125+ struct file *file;
6126+};
6127+
6128+static void au_call_cpup_wh(void *args)
6129+{
6130+ struct au_cpup_wh_args *a = args;
86dc4139 6131+
c2b27bf2
AM
6132+ au_pin_hdir_acquire_nest(a->cpg->pin);
6133+ *a->errp = au_cpup_wh(a->cpg, a->file);
6134+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6135+}
6136+
c2b27bf2 6137+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6138+{
6139+ int err, wkq_err;
c2b27bf2 6140+ aufs_bindex_t bdst;
c1595e42 6141+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6142+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6143+ struct au_wbr *wbr;
c2b27bf2 6144+ struct au_pin wh_pin, *pin_orig;
1facf9fc 6145+
c2b27bf2
AM
6146+ dentry = cpg->dentry;
6147+ bdst = cpg->bdst;
1facf9fc 6148+ parent = dget_parent(dentry);
5527c038 6149+ dir = d_inode(parent);
1facf9fc 6150+ h_orph = NULL;
6151+ h_parent = NULL;
6152+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6153+ h_tmpdir = h_dir;
c2b27bf2 6154+ pin_orig = NULL;
1facf9fc 6155+ if (!h_dir->i_nlink) {
6156+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6157+ h_orph = wbr->wbr_orph;
6158+
6159+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6160+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6161+ h_tmpdir = d_inode(h_orph);
1facf9fc 6162+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6163+
febd17d6 6164+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6165+ /* todo: au_h_open_pre()? */
86dc4139 6166+
c2b27bf2 6167+ pin_orig = cpg->pin;
86dc4139 6168+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6169+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6170+ cpg->pin = &wh_pin;
1facf9fc 6171+ }
6172+
53392da6 6173+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6174+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6175+ err = au_cpup_wh(cpg, file);
1facf9fc 6176+ else {
6177+ struct au_cpup_wh_args args = {
6178+ .errp = &err,
c2b27bf2
AM
6179+ .cpg = cpg,
6180+ .file = file
1facf9fc 6181+ };
6182+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6183+ if (unlikely(wkq_err))
6184+ err = wkq_err;
6185+ }
6186+
6187+ if (h_orph) {
febd17d6 6188+ inode_unlock(h_tmpdir);
4a4d8108 6189+ /* todo: au_h_open_post()? */
1facf9fc 6190+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6191+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6192+ AuDebugOn(!pin_orig);
6193+ cpg->pin = pin_orig;
1facf9fc 6194+ }
6195+ iput(h_dir);
6196+ dput(parent);
6197+
6198+ return err;
6199+}
6200+
6201+/* ---------------------------------------------------------------------- */
6202+
6203+/*
6204+ * generic routine for both of copy-up and copy-down.
6205+ */
6206+/* cf. revalidate function in file.c */
6207+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6208+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6209+ struct au_pin *pin,
1facf9fc 6210+ struct dentry *h_parent, void *arg),
6211+ void *arg)
6212+{
6213+ int err;
6214+ struct au_pin pin;
5527c038 6215+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6216+
6217+ err = 0;
6218+ parent = dget_parent(dentry);
6219+ if (IS_ROOT(parent))
6220+ goto out;
6221+
6222+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6223+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6224+
6225+ /* do not use au_dpage */
6226+ real_parent = parent;
6227+ while (1) {
6228+ dput(parent);
6229+ parent = dget_parent(dentry);
6230+ h_parent = au_h_dptr(parent, bdst);
6231+ if (h_parent)
6232+ goto out; /* success */
6233+
6234+ /* find top dir which is necessary to cpup */
6235+ do {
6236+ d = parent;
6237+ dput(parent);
6238+ parent = dget_parent(d);
6239+ di_read_lock_parent3(parent, !AuLock_IR);
6240+ h_parent = au_h_dptr(parent, bdst);
6241+ di_read_unlock(parent, !AuLock_IR);
6242+ } while (!h_parent);
6243+
6244+ if (d != real_parent)
6245+ di_write_lock_child3(d);
6246+
6247+ /* somebody else might create while we were sleeping */
5527c038
JR
6248+ h_dentry = au_h_dptr(d, bdst);
6249+ if (!h_dentry || d_is_negative(h_dentry)) {
6250+ if (h_dentry)
5afbbe0d 6251+ au_update_dbtop(d);
1facf9fc 6252+
6253+ au_pin_set_dentry(&pin, d);
6254+ err = au_do_pin(&pin);
6255+ if (!err) {
86dc4139 6256+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6257+ au_unpin(&pin);
6258+ }
6259+ }
6260+
6261+ if (d != real_parent)
6262+ di_write_unlock(d);
6263+ if (unlikely(err))
6264+ break;
6265+ }
6266+
4f0767ce 6267+out:
1facf9fc 6268+ dput(parent);
6269+ return err;
6270+}
6271+
6272+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6273+ struct au_pin *pin,
2000de60 6274+ struct dentry *h_parent __maybe_unused,
1facf9fc 6275+ void *arg __maybe_unused)
6276+{
c2b27bf2
AM
6277+ struct au_cp_generic cpg = {
6278+ .dentry = dentry,
6279+ .bdst = bdst,
6280+ .bsrc = -1,
6281+ .len = 0,
6282+ .pin = pin,
6283+ .flags = AuCpup_DTIME
6284+ };
6285+ return au_sio_cpup_simple(&cpg);
1facf9fc 6286+}
6287+
6288+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6289+{
6290+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6291+}
6292+
6293+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6294+{
6295+ int err;
6296+ struct dentry *parent;
6297+ struct inode *dir;
6298+
6299+ parent = dget_parent(dentry);
5527c038 6300+ dir = d_inode(parent);
1facf9fc 6301+ err = 0;
6302+ if (au_h_iptr(dir, bdst))
6303+ goto out;
6304+
6305+ di_read_unlock(parent, AuLock_IR);
6306+ di_write_lock_parent(parent);
6307+ /* someone else might change our inode while we were sleeping */
6308+ if (!au_h_iptr(dir, bdst))
6309+ err = au_cpup_dirs(dentry, bdst);
6310+ di_downgrade_lock(parent, AuLock_IR);
6311+
4f0767ce 6312+out:
1facf9fc 6313+ dput(parent);
6314+ return err;
6315+}
7f207e10 6316diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
eca34b5c 6317--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
016522bc 6318+++ linux/fs/aufs/cpup.h 2020-01-27 10:57:18.168871450 +0100
062440b3
AM
6319@@ -0,0 +1,100 @@
6320+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6321+/*
016522bc 6322+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 6323+ *
6324+ * This program, aufs is free software; you can redistribute it and/or modify
6325+ * it under the terms of the GNU General Public License as published by
6326+ * the Free Software Foundation; either version 2 of the License, or
6327+ * (at your option) any later version.
dece6358
AM
6328+ *
6329+ * This program is distributed in the hope that it will be useful,
6330+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6331+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6332+ * GNU General Public License for more details.
6333+ *
6334+ * You should have received a copy of the GNU General Public License
523b37e3 6335+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6336+ */
6337+
6338+/*
6339+ * copy-up/down functions
6340+ */
6341+
6342+#ifndef __AUFS_CPUP_H__
6343+#define __AUFS_CPUP_H__
6344+
6345+#ifdef __KERNEL__
6346+
dece6358 6347+#include <linux/path.h>
1facf9fc 6348+
dece6358
AM
6349+struct inode;
6350+struct file;
86dc4139 6351+struct au_pin;
dece6358 6352+
86dc4139 6353+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6354+void au_cpup_attr_timesizes(struct inode *inode);
6355+void au_cpup_attr_nlink(struct inode *inode, int force);
6356+void au_cpup_attr_changeable(struct inode *inode);
6357+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6358+void au_cpup_attr_all(struct inode *inode, int force);
6359+
6360+/* ---------------------------------------------------------------------- */
6361+
c2b27bf2
AM
6362+struct au_cp_generic {
6363+ struct dentry *dentry;
6364+ aufs_bindex_t bdst, bsrc;
6365+ loff_t len;
6366+ struct au_pin *pin;
6367+ unsigned int flags;
6368+};
6369+
1facf9fc 6370+/* cpup flags */
392086de
AM
6371+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6372+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6373+ for link(2) */
6374+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6375+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6376+ cpup */
6377+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6378+ existing entry */
6379+#define AuCpup_RWDST (1 << 5) /* force write target even if
6380+ the branch is marked as RO */
c2b27bf2 6381+
8b6a4947
AM
6382+#ifndef CONFIG_AUFS_BR_HFSPLUS
6383+#undef AuCpup_HOPEN
6384+#define AuCpup_HOPEN 0
6385+#endif
6386+
1facf9fc 6387+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6388+#define au_fset_cpup(flags, name) \
6389+ do { (flags) |= AuCpup_##name; } while (0)
6390+#define au_fclr_cpup(flags, name) \
6391+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6392+
6393+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6394+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6395+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6396+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6397+
6398+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6399+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6400+ struct au_pin *pin,
1facf9fc 6401+ struct dentry *h_parent, void *arg),
6402+ void *arg);
6403+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6404+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6405+
6406+/* ---------------------------------------------------------------------- */
6407+
6408+/* keep timestamps when copyup */
6409+struct au_dtime {
6410+ struct dentry *dt_dentry;
6411+ struct path dt_h_path;
cd7a4cd9 6412+ struct timespec64 dt_atime, dt_mtime;
1facf9fc 6413+};
6414+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6415+ struct path *h_path);
6416+void au_dtime_revert(struct au_dtime *dt);
6417+
6418+#endif /* __KERNEL__ */
6419+#endif /* __AUFS_CPUP_H__ */
7f207e10 6420diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
eca34b5c 6421--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
016522bc 6422+++ linux/fs/aufs/dbgaufs.c 2020-01-27 10:57:18.168871450 +0100
9f237c51 6423@@ -0,0 +1,526 @@
cd7a4cd9 6424+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6425+/*
016522bc 6426+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 6427+ *
6428+ * This program, aufs is free software; you can redistribute it and/or modify
6429+ * it under the terms of the GNU General Public License as published by
6430+ * the Free Software Foundation; either version 2 of the License, or
6431+ * (at your option) any later version.
dece6358
AM
6432+ *
6433+ * This program is distributed in the hope that it will be useful,
6434+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6435+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6436+ * GNU General Public License for more details.
6437+ *
6438+ * You should have received a copy of the GNU General Public License
523b37e3 6439+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6440+ */
6441+
6442+/*
6443+ * debugfs interface
6444+ */
6445+
6446+#include <linux/debugfs.h>
6447+#include "aufs.h"
6448+
6449+#ifndef CONFIG_SYSFS
6450+#error DEBUG_FS depends upon SYSFS
6451+#endif
6452+
6453+static struct dentry *dbgaufs;
cd7a4cd9 6454+static const mode_t dbgaufs_mode = 0444;
1facf9fc 6455+
6456+/* 20 is max digits length of ulong 64 */
6457+struct dbgaufs_arg {
6458+ int n;
6459+ char a[20 * 4];
6460+};
6461+
6462+/*
6463+ * common function for all XINO files
6464+ */
6465+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6466+ struct file *file)
6467+{
9f237c51
AM
6468+ void *p;
6469+
6470+ p = file->private_data;
6471+ if (p) {
6472+ /* this is struct dbgaufs_arg */
6473+ AuDebugOn(!au_kfree_sz_test(p));
6474+ au_kfree_do_rcu(p);
6475+ }
1facf9fc 6476+ return 0;
6477+}
6478+
062440b3
AM
6479+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6480+ int cnt)
1facf9fc 6481+{
6482+ int err;
6483+ struct kstat st;
6484+ struct dbgaufs_arg *p;
6485+
6486+ err = -ENOMEM;
6487+ p = kmalloc(sizeof(*p), GFP_NOFS);
6488+ if (unlikely(!p))
6489+ goto out;
6490+
6491+ err = 0;
6492+ p->n = 0;
6493+ file->private_data = p;
6494+ if (!xf)
6495+ goto out;
6496+
521ced18 6497+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6498+ if (!err) {
6499+ if (do_fcnt)
6500+ p->n = snprintf
062440b3
AM
6501+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6502+ cnt, st.blocks, st.blksize,
1facf9fc 6503+ (long long)st.size);
6504+ else
521ced18 6505+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6506+ st.blocks, st.blksize,
6507+ (long long)st.size);
6508+ AuDebugOn(p->n >= sizeof(p->a));
6509+ } else {
6510+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6511+ err = 0;
6512+ }
6513+
4f0767ce 6514+out:
1facf9fc 6515+ return err;
1facf9fc 6516+}
6517+
6518+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6519+ size_t count, loff_t *ppos)
6520+{
6521+ struct dbgaufs_arg *p;
6522+
6523+ p = file->private_data;
6524+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6525+}
6526+
6527+/* ---------------------------------------------------------------------- */
6528+
86dc4139
AM
6529+struct dbgaufs_plink_arg {
6530+ int n;
6531+ char a[];
6532+};
6533+
6534+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6535+ struct file *file)
6536+{
1c60b727 6537+ free_page((unsigned long)file->private_data);
86dc4139
AM
6538+ return 0;
6539+}
6540+
6541+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6542+{
6543+ int err, i, limit;
6544+ unsigned long n, sum;
6545+ struct dbgaufs_plink_arg *p;
6546+ struct au_sbinfo *sbinfo;
6547+ struct super_block *sb;
8b6a4947 6548+ struct hlist_bl_head *hbl;
86dc4139
AM
6549+
6550+ err = -ENOMEM;
6551+ p = (void *)get_zeroed_page(GFP_NOFS);
6552+ if (unlikely(!p))
6553+ goto out;
6554+
6555+ err = -EFBIG;
6556+ sbinfo = inode->i_private;
6557+ sb = sbinfo->si_sb;
6558+ si_noflush_read_lock(sb);
6559+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6560+ limit = PAGE_SIZE - sizeof(p->n);
6561+
6562+ /* the number of buckets */
6563+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6564+ p->n += n;
6565+ limit -= n;
6566+
6567+ sum = 0;
8b6a4947
AM
6568+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6569+ i++, hbl++) {
6570+ n = au_hbl_count(hbl);
86dc4139
AM
6571+ sum += n;
6572+
6573+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6574+ p->n += n;
6575+ limit -= n;
6576+ if (unlikely(limit <= 0))
6577+ goto out_free;
6578+ }
6579+ p->a[p->n - 1] = '\n';
6580+
6581+ /* the sum of plinks */
6582+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6583+ p->n += n;
6584+ limit -= n;
6585+ if (unlikely(limit <= 0))
6586+ goto out_free;
6587+ } else {
6588+#define str "1\n0\n0\n"
6589+ p->n = sizeof(str) - 1;
6590+ strcpy(p->a, str);
6591+#undef str
6592+ }
6593+ si_read_unlock(sb);
6594+
6595+ err = 0;
6596+ file->private_data = p;
6597+ goto out; /* success */
6598+
6599+out_free:
1c60b727 6600+ free_page((unsigned long)p);
86dc4139
AM
6601+out:
6602+ return err;
6603+}
6604+
6605+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6606+ size_t count, loff_t *ppos)
6607+{
6608+ struct dbgaufs_plink_arg *p;
6609+
6610+ p = file->private_data;
6611+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6612+}
6613+
6614+static const struct file_operations dbgaufs_plink_fop = {
6615+ .owner = THIS_MODULE,
6616+ .open = dbgaufs_plink_open,
6617+ .release = dbgaufs_plink_release,
6618+ .read = dbgaufs_plink_read
6619+};
6620+
6621+/* ---------------------------------------------------------------------- */
6622+
1facf9fc 6623+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6624+{
6625+ int err;
6626+ struct au_sbinfo *sbinfo;
6627+ struct super_block *sb;
6628+
6629+ sbinfo = inode->i_private;
6630+ sb = sbinfo->si_sb;
6631+ si_noflush_read_lock(sb);
062440b3 6632+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6633+ si_read_unlock(sb);
6634+ return err;
6635+}
6636+
6637+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6638+ .owner = THIS_MODULE,
1facf9fc 6639+ .open = dbgaufs_xib_open,
6640+ .release = dbgaufs_xi_release,
6641+ .read = dbgaufs_xi_read
6642+};
6643+
6644+/* ---------------------------------------------------------------------- */
6645+
6646+#define DbgaufsXi_PREFIX "xi"
6647+
6648+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6649+{
acd2b654 6650+ int err, idx;
1facf9fc 6651+ long l;
acd2b654
AM
6652+ aufs_bindex_t bindex;
6653+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
1facf9fc 6654+ struct au_sbinfo *sbinfo;
6655+ struct super_block *sb;
acd2b654 6656+ struct au_xino *xi;
1facf9fc 6657+ struct file *xf;
6658+ struct qstr *name;
062440b3 6659+ struct au_branch *br;
1facf9fc 6660+
6661+ err = -ENOENT;
2000de60 6662+ name = &file->f_path.dentry->d_name;
1facf9fc 6663+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6664+ || memcmp(name->name, DbgaufsXi_PREFIX,
6665+ sizeof(DbgaufsXi_PREFIX) - 1)))
6666+ goto out;
acd2b654
AM
6667+
6668+ AuDebugOn(name->len >= sizeof(a));
6669+ memcpy(a, name->name, name->len);
6670+ a[name->len] = '\0';
6671+ p = strchr(a, '-');
6672+ if (p)
6673+ *p = '\0';
6674+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6675+ if (unlikely(err))
6676+ goto out;
acd2b654
AM
6677+ bindex = l;
6678+ idx = 0;
6679+ if (p) {
6680+ err = kstrtol(p + 1, 10, &l);
6681+ if (unlikely(err))
6682+ goto out;
6683+ idx = l;
6684+ }
1facf9fc 6685+
acd2b654 6686+ err = -ENOENT;
1facf9fc 6687+ sbinfo = inode->i_private;
6688+ sb = sbinfo->si_sb;
6689+ si_noflush_read_lock(sb);
acd2b654
AM
6690+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6691+ goto out_si;
6692+ br = au_sbr(sb, bindex);
6693+ xi = br->br_xino;
6694+ if (unlikely(idx >= xi->xi_nfile))
6695+ goto out_si;
6696+ xf = au_xino_file(xi, idx);
6697+ if (xf)
062440b3
AM
6698+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6699+ au_xino_count(br));
1facf9fc 6700+
acd2b654
AM
6701+out_si:
6702+ si_read_unlock(sb);
4f0767ce 6703+out:
acd2b654 6704+ AuTraceErr(err);
1facf9fc 6705+ return err;
6706+}
6707+
6708+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6709+ .owner = THIS_MODULE,
1facf9fc 6710+ .open = dbgaufs_xino_open,
6711+ .release = dbgaufs_xi_release,
6712+ .read = dbgaufs_xi_read
6713+};
6714+
062440b3
AM
6715+void dbgaufs_xino_del(struct au_branch *br)
6716+{
6717+ struct dentry *dbgaufs;
6718+
6719+ dbgaufs = br->br_dbgaufs;
6720+ if (!dbgaufs)
6721+ return;
6722+
6723+ br->br_dbgaufs = NULL;
6724+ /* debugfs acquires the parent i_mutex */
6725+ lockdep_off();
6726+ debugfs_remove(dbgaufs);
6727+ lockdep_on();
6728+}
6729+
1facf9fc 6730+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6731+{
5afbbe0d 6732+ aufs_bindex_t bbot;
1facf9fc 6733+ struct au_branch *br;
1facf9fc 6734+
6735+ if (!au_sbi(sb)->si_dbgaufs)
6736+ return;
6737+
5afbbe0d
AM
6738+ bbot = au_sbbot(sb);
6739+ for (; bindex <= bbot; bindex++) {
1facf9fc 6740+ br = au_sbr(sb, bindex);
062440b3
AM
6741+ dbgaufs_xino_del(br);
6742+ }
6743+}
6744+
acd2b654
AM
6745+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6746+ unsigned int idx, struct dentry *parent,
6747+ struct au_sbinfo *sbinfo)
062440b3
AM
6748+{
6749+ struct au_branch *br;
6750+ struct dentry *d;
acd2b654
AM
6751+ /* "xi" bindex(5) "-" idx(2) NULL */
6752+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
062440b3 6753+
acd2b654
AM
6754+ if (!idx)
6755+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6756+ else
6757+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6758+ bindex, idx);
062440b3
AM
6759+ br = au_sbr(sb, bindex);
6760+ if (br->br_dbgaufs) {
6761+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6762+
6763+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6764+ /* debugfs acquires the parent i_mutex */
6765+ lockdep_off();
6766+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6767+ name);
6768+ lockdep_on();
6769+ if (unlikely(!d))
6770+ pr_warn("failed renaming %pd/%s, ignored.\n",
6771+ parent, name);
6772+ }
6773+ } else {
e2f27e51 6774+ lockdep_off();
062440b3
AM
6775+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6776+ sbinfo, &dbgaufs_xino_fop);
e2f27e51 6777+ lockdep_on();
062440b3 6778+ if (unlikely(!br->br_dbgaufs))
acd2b654 6779+ pr_warn("failed creating %pd/%s, ignored.\n",
062440b3 6780+ parent, name);
1facf9fc 6781+ }
6782+}
6783+
acd2b654
AM
6784+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6785+ struct dentry *parent, struct au_sbinfo *sbinfo)
6786+{
6787+ struct au_branch *br;
6788+ struct au_xino *xi;
6789+ unsigned int u;
6790+
6791+ br = au_sbr(sb, bindex);
6792+ xi = br->br_xino;
6793+ for (u = 0; u < xi->xi_nfile; u++)
6794+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6795+}
6796+
062440b3 6797+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
1facf9fc 6798+{
6799+ struct au_sbinfo *sbinfo;
6800+ struct dentry *parent;
5afbbe0d 6801+ aufs_bindex_t bbot;
062440b3
AM
6802+
6803+ if (!au_opt_test(au_mntflags(sb), XINO))
6804+ return;
1facf9fc 6805+
6806+ sbinfo = au_sbi(sb);
6807+ parent = sbinfo->si_dbgaufs;
6808+ if (!parent)
6809+ return;
6810+
5afbbe0d 6811+ bbot = au_sbbot(sb);
062440b3
AM
6812+ if (topdown)
6813+ for (; bindex <= bbot; bindex++)
6814+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6815+ else
6816+ for (; bbot >= bindex; bbot--)
6817+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
1facf9fc 6818+}
6819+
6820+/* ---------------------------------------------------------------------- */
6821+
6822+#ifdef CONFIG_AUFS_EXPORT
6823+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6824+{
6825+ int err;
6826+ struct au_sbinfo *sbinfo;
6827+ struct super_block *sb;
6828+
6829+ sbinfo = inode->i_private;
6830+ sb = sbinfo->si_sb;
6831+ si_noflush_read_lock(sb);
062440b3 6832+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6833+ si_read_unlock(sb);
6834+ return err;
6835+}
6836+
6837+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6838+ .owner = THIS_MODULE,
1facf9fc 6839+ .open = dbgaufs_xigen_open,
6840+ .release = dbgaufs_xi_release,
6841+ .read = dbgaufs_xi_read
6842+};
6843+
6844+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6845+{
6846+ int err;
6847+
dece6358 6848+ /*
c1595e42 6849+ * This function is a dynamic '__init' function actually,
dece6358
AM
6850+ * so the tiny check for si_rwsem is unnecessary.
6851+ */
6852+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6853+
1facf9fc 6854+ err = -EIO;
6855+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6856+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6857+ &dbgaufs_xigen_fop);
6858+ if (sbinfo->si_dbgaufs_xigen)
6859+ err = 0;
6860+
6861+ return err;
6862+}
6863+#else
6864+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6865+{
6866+ return 0;
6867+}
6868+#endif /* CONFIG_AUFS_EXPORT */
6869+
6870+/* ---------------------------------------------------------------------- */
6871+
6872+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6873+{
dece6358 6874+ /*
7e9cd9fe 6875+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6876+ * so the tiny check for si_rwsem is unnecessary.
6877+ */
6878+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6879+
1facf9fc 6880+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6881+ sbinfo->si_dbgaufs = NULL;
1facf9fc 6882+}
6883+
6884+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6885+{
6886+ int err;
6887+ char name[SysaufsSiNameLen];
6888+
dece6358 6889+ /*
c1595e42 6890+ * This function is a dynamic '__init' function actually,
dece6358
AM
6891+ * so the tiny check for si_rwsem is unnecessary.
6892+ */
6893+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6894+
1facf9fc 6895+ err = -ENOENT;
6896+ if (!dbgaufs) {
6897+ AuErr1("/debug/aufs is uninitialized\n");
6898+ goto out;
6899+ }
6900+
6901+ err = -EIO;
6902+ sysaufs_name(sbinfo, name);
6903+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6904+ if (unlikely(!sbinfo->si_dbgaufs))
6905+ goto out;
1facf9fc 6906+
062440b3 6907+ /* regardless plink/noplink option */
86dc4139
AM
6908+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6909+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6910+ &dbgaufs_plink_fop);
6911+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6912+ goto out_dir;
6913+
062440b3
AM
6914+ /* regardless xino/noxino option */
6915+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6916+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6917+ &dbgaufs_xib_fop);
6918+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6919+ goto out_dir;
6920+
1facf9fc 6921+ err = dbgaufs_xigen_init(sbinfo);
6922+ if (!err)
6923+ goto out; /* success */
6924+
4f0767ce 6925+out_dir:
1facf9fc 6926+ dbgaufs_si_fin(sbinfo);
4f0767ce 6927+out:
062440b3
AM
6928+ if (unlikely(err))
6929+ pr_err("debugfs/aufs failed\n");
1facf9fc 6930+ return err;
6931+}
6932+
6933+/* ---------------------------------------------------------------------- */
6934+
6935+void dbgaufs_fin(void)
6936+{
6937+ debugfs_remove(dbgaufs);
6938+}
6939+
6940+int __init dbgaufs_init(void)
6941+{
6942+ int err;
6943+
6944+ err = -EIO;
6945+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6946+ if (dbgaufs)
6947+ err = 0;
6948+ return err;
6949+}
7f207e10 6950diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
eca34b5c 6951--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
016522bc 6952+++ linux/fs/aufs/dbgaufs.h 2020-01-27 10:57:18.168871450 +0100
062440b3
AM
6953@@ -0,0 +1,53 @@
6954+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6955+/*
016522bc 6956+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 6957+ *
6958+ * This program, aufs is free software; you can redistribute it and/or modify
6959+ * it under the terms of the GNU General Public License as published by
6960+ * the Free Software Foundation; either version 2 of the License, or
6961+ * (at your option) any later version.
dece6358
AM
6962+ *
6963+ * This program is distributed in the hope that it will be useful,
6964+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6965+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6966+ * GNU General Public License for more details.
6967+ *
6968+ * You should have received a copy of the GNU General Public License
523b37e3 6969+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6970+ */
6971+
6972+/*
6973+ * debugfs interface
6974+ */
6975+
6976+#ifndef __DBGAUFS_H__
6977+#define __DBGAUFS_H__
6978+
6979+#ifdef __KERNEL__
6980+
dece6358 6981+struct super_block;
1facf9fc 6982+struct au_sbinfo;
062440b3 6983+struct au_branch;
dece6358 6984+
1facf9fc 6985+#ifdef CONFIG_DEBUG_FS
6986+/* dbgaufs.c */
062440b3 6987+void dbgaufs_xino_del(struct au_branch *br);
1facf9fc 6988+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
062440b3 6989+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
1facf9fc 6990+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6991+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6992+void dbgaufs_fin(void);
6993+int __init dbgaufs_init(void);
1facf9fc 6994+#else
062440b3 6995+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
4a4d8108 6996+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
062440b3
AM
6997+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6998+ int topdown)
4a4d8108
AM
6999+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7000+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7001+AuStubVoid(dbgaufs_fin, void)
7002+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 7003+#endif /* CONFIG_DEBUG_FS */
7004+
7005+#endif /* __KERNEL__ */
7006+#endif /* __DBGAUFS_H__ */
7f207e10 7007diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
eca34b5c 7008--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
016522bc 7009+++ linux/fs/aufs/dcsub.c 2020-01-27 10:57:18.168871450 +0100
cd7a4cd9
AM
7010@@ -0,0 +1,225 @@
7011+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7012+/*
016522bc 7013+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7014+ *
7015+ * This program, aufs is free software; you can redistribute it and/or modify
7016+ * it under the terms of the GNU General Public License as published by
7017+ * the Free Software Foundation; either version 2 of the License, or
7018+ * (at your option) any later version.
dece6358
AM
7019+ *
7020+ * This program is distributed in the hope that it will be useful,
7021+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7022+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7023+ * GNU General Public License for more details.
7024+ *
7025+ * You should have received a copy of the GNU General Public License
523b37e3 7026+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7027+ */
7028+
7029+/*
7030+ * sub-routines for dentry cache
7031+ */
7032+
7033+#include "aufs.h"
7034+
7035+static void au_dpage_free(struct au_dpage *dpage)
7036+{
7037+ int i;
7038+ struct dentry **p;
7039+
7040+ p = dpage->dentries;
7041+ for (i = 0; i < dpage->ndentry; i++)
7042+ dput(*p++);
1c60b727 7043+ free_page((unsigned long)dpage->dentries);
1facf9fc 7044+}
7045+
7046+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7047+{
7048+ int err;
7049+ void *p;
7050+
7051+ err = -ENOMEM;
7052+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7053+ if (unlikely(!dpages->dpages))
7054+ goto out;
7055+
7056+ p = (void *)__get_free_page(gfp);
7057+ if (unlikely(!p))
7058+ goto out_dpages;
7059+
7060+ dpages->dpages[0].ndentry = 0;
7061+ dpages->dpages[0].dentries = p;
7062+ dpages->ndpage = 1;
7063+ return 0; /* success */
7064+
4f0767ce 7065+out_dpages:
9f237c51 7066+ au_kfree_try_rcu(dpages->dpages);
4f0767ce 7067+out:
1facf9fc 7068+ return err;
7069+}
7070+
7071+void au_dpages_free(struct au_dcsub_pages *dpages)
7072+{
7073+ int i;
7074+ struct au_dpage *p;
7075+
7076+ p = dpages->dpages;
7077+ for (i = 0; i < dpages->ndpage; i++)
7078+ au_dpage_free(p++);
9f237c51 7079+ au_kfree_try_rcu(dpages->dpages);
1facf9fc 7080+}
7081+
7082+static int au_dpages_append(struct au_dcsub_pages *dpages,
7083+ struct dentry *dentry, gfp_t gfp)
7084+{
7085+ int err, sz;
7086+ struct au_dpage *dpage;
7087+ void *p;
7088+
7089+ dpage = dpages->dpages + dpages->ndpage - 1;
7090+ sz = PAGE_SIZE / sizeof(dentry);
7091+ if (unlikely(dpage->ndentry >= sz)) {
7092+ AuLabel(new dpage);
7093+ err = -ENOMEM;
7094+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7095+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7096+ sz + sizeof(*dpages->dpages), gfp,
7097+ /*may_shrink*/0);
1facf9fc 7098+ if (unlikely(!p))
7099+ goto out;
7100+
7101+ dpages->dpages = p;
7102+ dpage = dpages->dpages + dpages->ndpage;
7103+ p = (void *)__get_free_page(gfp);
7104+ if (unlikely(!p))
7105+ goto out;
7106+
7107+ dpage->ndentry = 0;
7108+ dpage->dentries = p;
7109+ dpages->ndpage++;
7110+ }
7111+
c1595e42 7112+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7113+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7114+ return 0; /* success */
7115+
4f0767ce 7116+out:
1facf9fc 7117+ return err;
7118+}
7119+
c1595e42
JR
7120+/* todo: BAD approach */
7121+/* copied from linux/fs/dcache.c */
7122+enum d_walk_ret {
7123+ D_WALK_CONTINUE,
7124+ D_WALK_QUIT,
7125+ D_WALK_NORETRY,
7126+ D_WALK_SKIP,
7127+};
7128+
7129+extern void d_walk(struct dentry *parent, void *data,
cd7a4cd9 7130+ enum d_walk_ret (*enter)(void *, struct dentry *));
c1595e42
JR
7131+
7132+struct ac_dpages_arg {
1facf9fc 7133+ int err;
c1595e42
JR
7134+ struct au_dcsub_pages *dpages;
7135+ struct super_block *sb;
7136+ au_dpages_test test;
7137+ void *arg;
7138+};
1facf9fc 7139+
c1595e42
JR
7140+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7141+{
7142+ enum d_walk_ret ret;
7143+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7144+
c1595e42
JR
7145+ ret = D_WALK_CONTINUE;
7146+ if (dentry->d_sb == arg->sb
7147+ && !IS_ROOT(dentry)
7148+ && au_dcount(dentry) > 0
7149+ && au_di(dentry)
7150+ && (!arg->test || arg->test(dentry, arg->arg))) {
7151+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7152+ if (unlikely(arg->err))
7153+ ret = D_WALK_QUIT;
1facf9fc 7154+ }
7155+
c1595e42
JR
7156+ return ret;
7157+}
027c5e7a 7158+
c1595e42
JR
7159+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7160+ au_dpages_test test, void *arg)
7161+{
7162+ struct ac_dpages_arg args = {
7163+ .err = 0,
7164+ .dpages = dpages,
7165+ .sb = root->d_sb,
7166+ .test = test,
7167+ .arg = arg
7168+ };
027c5e7a 7169+
cd7a4cd9 7170+ d_walk(root, &args, au_call_dpages_append);
c1595e42
JR
7171+
7172+ return args.err;
1facf9fc 7173+}
7174+
7175+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7176+ int do_include, au_dpages_test test, void *arg)
7177+{
7178+ int err;
7179+
7180+ err = 0;
027c5e7a
AM
7181+ write_seqlock(&rename_lock);
7182+ spin_lock(&dentry->d_lock);
7183+ if (do_include
c1595e42 7184+ && au_dcount(dentry) > 0
027c5e7a 7185+ && (!test || test(dentry, arg)))
1facf9fc 7186+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7187+ spin_unlock(&dentry->d_lock);
7188+ if (unlikely(err))
7189+ goto out;
7190+
7191+ /*
523b37e3 7192+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7193+ * mount
7194+ */
1facf9fc 7195+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7196+ dentry = dentry->d_parent; /* rename_lock is locked */
7197+ spin_lock(&dentry->d_lock);
c1595e42 7198+ if (au_dcount(dentry) > 0
027c5e7a 7199+ && (!test || test(dentry, arg)))
1facf9fc 7200+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7201+ spin_unlock(&dentry->d_lock);
7202+ if (unlikely(err))
7203+ break;
1facf9fc 7204+ }
7205+
4f0767ce 7206+out:
027c5e7a 7207+ write_sequnlock(&rename_lock);
1facf9fc 7208+ return err;
7209+}
7210+
027c5e7a
AM
7211+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7212+{
7213+ return au_di(dentry) && dentry->d_sb == arg;
7214+}
7215+
7216+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7217+ struct dentry *dentry, int do_include)
7218+{
7219+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7220+ au_dcsub_dpages_aufs, dentry->d_sb);
7221+}
7222+
4a4d8108 7223+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7224+{
4a4d8108
AM
7225+ struct path path[2] = {
7226+ {
7227+ .dentry = d1
7228+ },
7229+ {
7230+ .dentry = d2
7231+ }
7232+ };
1facf9fc 7233+
4a4d8108 7234+ return path_is_under(path + 0, path + 1);
1facf9fc 7235+}
7f207e10 7236diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
eca34b5c 7237--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
016522bc 7238+++ linux/fs/aufs/dcsub.h 2020-01-27 10:57:18.168871450 +0100
062440b3
AM
7239@@ -0,0 +1,137 @@
7240+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7241+/*
016522bc 7242+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7243+ *
7244+ * This program, aufs is free software; you can redistribute it and/or modify
7245+ * it under the terms of the GNU General Public License as published by
7246+ * the Free Software Foundation; either version 2 of the License, or
7247+ * (at your option) any later version.
dece6358
AM
7248+ *
7249+ * This program is distributed in the hope that it will be useful,
7250+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7251+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7252+ * GNU General Public License for more details.
7253+ *
7254+ * You should have received a copy of the GNU General Public License
523b37e3 7255+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7256+ */
7257+
7258+/*
7259+ * sub-routines for dentry cache
7260+ */
7261+
7262+#ifndef __AUFS_DCSUB_H__
7263+#define __AUFS_DCSUB_H__
7264+
7265+#ifdef __KERNEL__
7266+
7f207e10 7267+#include <linux/dcache.h>
027c5e7a 7268+#include <linux/fs.h>
dece6358 7269+
1facf9fc 7270+struct au_dpage {
7271+ int ndentry;
7272+ struct dentry **dentries;
7273+};
7274+
7275+struct au_dcsub_pages {
7276+ int ndpage;
7277+ struct au_dpage *dpages;
7278+};
7279+
7280+/* ---------------------------------------------------------------------- */
7281+
7f207e10 7282+/* dcsub.c */
1facf9fc 7283+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7284+void au_dpages_free(struct au_dcsub_pages *dpages);
7285+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7286+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7287+ au_dpages_test test, void *arg);
7288+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7289+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7290+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7291+ struct dentry *dentry, int do_include);
4a4d8108 7292+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7293+
7f207e10
AM
7294+/* ---------------------------------------------------------------------- */
7295+
523b37e3
AM
7296+/*
7297+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7298+ * include/linux/dcache.h. Try them (in the future).
7299+ */
7300+
027c5e7a
AM
7301+static inline int au_d_hashed_positive(struct dentry *d)
7302+{
7303+ int err;
5527c038 7304+ struct inode *inode = d_inode(d);
076b876e 7305+
027c5e7a 7306+ err = 0;
5527c038
JR
7307+ if (unlikely(d_unhashed(d)
7308+ || d_is_negative(d)
7309+ || !inode->i_nlink))
027c5e7a
AM
7310+ err = -ENOENT;
7311+ return err;
7312+}
7313+
38d290e6
JR
7314+static inline int au_d_linkable(struct dentry *d)
7315+{
7316+ int err;
5527c038 7317+ struct inode *inode = d_inode(d);
076b876e 7318+
38d290e6
JR
7319+ err = au_d_hashed_positive(d);
7320+ if (err
5527c038 7321+ && d_is_positive(d)
38d290e6
JR
7322+ && (inode->i_state & I_LINKABLE))
7323+ err = 0;
7324+ return err;
7325+}
7326+
027c5e7a
AM
7327+static inline int au_d_alive(struct dentry *d)
7328+{
7329+ int err;
7330+ struct inode *inode;
076b876e 7331+
027c5e7a
AM
7332+ err = 0;
7333+ if (!IS_ROOT(d))
7334+ err = au_d_hashed_positive(d);
7335+ else {
5527c038
JR
7336+ inode = d_inode(d);
7337+ if (unlikely(d_unlinked(d)
7338+ || d_is_negative(d)
7339+ || !inode->i_nlink))
027c5e7a
AM
7340+ err = -ENOENT;
7341+ }
7342+ return err;
7343+}
7344+
7345+static inline int au_alive_dir(struct dentry *d)
7f207e10 7346+{
027c5e7a 7347+ int err;
076b876e 7348+
027c5e7a 7349+ err = au_d_alive(d);
5527c038 7350+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7351+ err = -ENOENT;
7352+ return err;
7f207e10
AM
7353+}
7354+
38d290e6
JR
7355+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7356+{
7357+ return a->len == b->len
7358+ && !memcmp(a->name, b->name, a->len);
7359+}
7360+
7e9cd9fe
AM
7361+/*
7362+ * by the commit
7363+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7364+ * taking d_lock
7365+ * the type of d_lockref.count became int, but the inlined function d_count()
7366+ * still returns unsigned int.
7367+ * I don't know why. Maybe it is for every d_count() users?
7368+ * Anyway au_dcount() lives on.
7369+ */
c1595e42
JR
7370+static inline int au_dcount(struct dentry *d)
7371+{
7372+ return (int)d_count(d);
7373+}
7374+
1facf9fc 7375+#endif /* __KERNEL__ */
7376+#endif /* __AUFS_DCSUB_H__ */
7f207e10 7377diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
eca34b5c 7378--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
016522bc 7379+++ linux/fs/aufs/debug.c 2020-01-27 10:57:18.168871450 +0100
eca801bf 7380@@ -0,0 +1,441 @@
cd7a4cd9 7381+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7382+/*
016522bc 7383+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7384+ *
7385+ * This program, aufs is free software; you can redistribute it and/or modify
7386+ * it under the terms of the GNU General Public License as published by
7387+ * the Free Software Foundation; either version 2 of the License, or
7388+ * (at your option) any later version.
dece6358
AM
7389+ *
7390+ * This program is distributed in the hope that it will be useful,
7391+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7392+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7393+ * GNU General Public License for more details.
7394+ *
7395+ * You should have received a copy of the GNU General Public License
523b37e3 7396+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7397+ */
7398+
7399+/*
7400+ * debug print functions
7401+ */
7402+
eca801bf 7403+#include <linux/iversion.h>
1facf9fc 7404+#include "aufs.h"
7405+
392086de
AM
7406+/* Returns 0, or -errno. arg is in kp->arg. */
7407+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7408+{
7409+ int err, n;
7410+
7411+ err = kstrtoint(val, 0, &n);
7412+ if (!err) {
7413+ if (n > 0)
7414+ au_debug_on();
7415+ else
7416+ au_debug_off();
7417+ }
7418+ return err;
7419+}
7420+
7421+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7422+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7423+{
7424+ atomic_t *a;
7425+
7426+ a = kp->arg;
7427+ return sprintf(buffer, "%d", atomic_read(a));
7428+}
7429+
7430+static struct kernel_param_ops param_ops_atomic_t = {
7431+ .set = param_atomic_t_set,
7432+ .get = param_atomic_t_get
7433+ /* void (*free)(void *arg) */
7434+};
7435+
7436+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7437+MODULE_PARM_DESC(debug, "debug print");
cd7a4cd9 7438+module_param_named(debug, aufs_debug, atomic_t, 0664);
1facf9fc 7439+
c1595e42 7440+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7441+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7442+#define dpri(fmt, ...) do { \
7443+ if ((au_plevel \
7444+ && strcmp(au_plevel, KERN_DEBUG)) \
7445+ || au_debug_test()) \
7446+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7447+} while (0)
7448+
7449+/* ---------------------------------------------------------------------- */
7450+
7451+void au_dpri_whlist(struct au_nhash *whlist)
7452+{
7453+ unsigned long ul, n;
7454+ struct hlist_head *head;
c06a8ce3 7455+ struct au_vdir_wh *pos;
1facf9fc 7456+
7457+ n = whlist->nh_num;
7458+ head = whlist->nh_head;
7459+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7460+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7461+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7462+ pos->wh_bindex,
7463+ pos->wh_str.len, pos->wh_str.name,
7464+ pos->wh_str.len);
1facf9fc 7465+ head++;
7466+ }
7467+}
7468+
7469+void au_dpri_vdir(struct au_vdir *vdir)
7470+{
7471+ unsigned long ul;
7472+ union au_vdir_deblk_p p;
7473+ unsigned char *o;
7474+
7475+ if (!vdir || IS_ERR(vdir)) {
7476+ dpri("err %ld\n", PTR_ERR(vdir));
7477+ return;
7478+ }
7479+
be118d29 7480+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7481+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7482+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7483+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7484+ p.deblk = vdir->vd_deblk[ul];
7485+ o = p.deblk;
7486+ dpri("[%lu]: %p\n", ul, o);
7487+ }
7488+}
7489+
53392da6 7490+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7491+ struct dentry *wh)
7492+{
7493+ char *n = NULL;
7494+ int l = 0;
7495+
7496+ if (!inode || IS_ERR(inode)) {
7497+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7498+ return -1;
7499+ }
7500+
c2b27bf2 7501+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7502+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7503+ && sizeof(inode->i_blocks) != sizeof(u64));
7504+ if (wh) {
7505+ n = (void *)wh->d_name.name;
7506+ l = wh->d_name.len;
7507+ }
7508+
53392da6
AM
7509+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7510+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7511+ bindex, inode,
1facf9fc 7512+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7513+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7514+ i_size_read(inode), (unsigned long long)inode->i_blocks,
cd7a4cd9 7515+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7516+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7517+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7518+ inode->i_generation,
1facf9fc 7519+ l ? ", wh " : "", l, n);
7520+ return 0;
7521+}
7522+
7523+void au_dpri_inode(struct inode *inode)
7524+{
7525+ struct au_iinfo *iinfo;
5afbbe0d 7526+ struct au_hinode *hi;
1facf9fc 7527+ aufs_bindex_t bindex;
53392da6 7528+ int err, hn;
1facf9fc 7529+
53392da6 7530+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7531+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7532+ return;
7533+
7534+ iinfo = au_ii(inode);
5afbbe0d
AM
7535+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7536+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7537+ if (iinfo->ii_btop < 0)
1facf9fc 7538+ return;
53392da6 7539+ hn = 0;
5afbbe0d
AM
7540+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7541+ hi = au_hinode(iinfo, bindex);
7542+ hn = !!au_hn(hi);
7543+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7544+ }
1facf9fc 7545+}
7546+
2cbb1c4b
JR
7547+void au_dpri_dalias(struct inode *inode)
7548+{
7549+ struct dentry *d;
7550+
7551+ spin_lock(&inode->i_lock);
c1595e42 7552+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7553+ au_dpri_dentry(d);
7554+ spin_unlock(&inode->i_lock);
7555+}
7556+
1facf9fc 7557+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7558+{
7559+ struct dentry *wh = NULL;
53392da6 7560+ int hn;
5afbbe0d 7561+ struct inode *inode;
076b876e 7562+ struct au_iinfo *iinfo;
5afbbe0d 7563+ struct au_hinode *hi;
1facf9fc 7564+
7565+ if (!dentry || IS_ERR(dentry)) {
7566+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7567+ return -1;
7568+ }
7569+ /* do not call dget_parent() here */
027c5e7a 7570+ /* note: access d_xxx without d_lock */
523b37e3
AM
7571+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7572+ bindex, dentry, dentry,
1facf9fc 7573+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7574+ au_dcount(dentry), dentry->d_flags,
523b37e3 7575+ d_unhashed(dentry) ? "un" : "");
53392da6 7576+ hn = -1;
5afbbe0d
AM
7577+ inode = NULL;
7578+ if (d_is_positive(dentry))
7579+ inode = d_inode(dentry);
7580+ if (inode
7581+ && au_test_aufs(dentry->d_sb)
7582+ && bindex >= 0
7583+ && !au_is_bad_inode(inode)) {
7584+ iinfo = au_ii(inode);
7585+ hi = au_hinode(iinfo, bindex);
7586+ hn = !!au_hn(hi);
7587+ wh = hi->hi_whdentry;
7588+ }
7589+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7590+ return 0;
7591+}
7592+
7593+void au_dpri_dentry(struct dentry *dentry)
7594+{
7595+ struct au_dinfo *dinfo;
7596+ aufs_bindex_t bindex;
7597+ int err;
7598+
7599+ err = do_pri_dentry(-1, dentry);
7600+ if (err || !au_test_aufs(dentry->d_sb))
7601+ return;
7602+
7603+ dinfo = au_di(dentry);
7604+ if (!dinfo)
7605+ return;
5afbbe0d
AM
7606+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7607+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7608+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7609+ dinfo->di_tmpfile);
5afbbe0d 7610+ if (dinfo->di_btop < 0)
1facf9fc 7611+ return;
5afbbe0d
AM
7612+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7613+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7614+}
7615+
7616+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7617+{
7618+ char a[32];
7619+
7620+ if (!file || IS_ERR(file)) {
7621+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7622+ return -1;
7623+ }
7624+ a[0] = 0;
7625+ if (bindex < 0
b912730e 7626+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7627+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7628+ && au_fi(file))
e49829fe 7629+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7630+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7631+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7632+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7633+ file->f_version, file->f_pos, a);
b912730e 7634+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7635+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7636+ return 0;
7637+}
7638+
7639+void au_dpri_file(struct file *file)
7640+{
7641+ struct au_finfo *finfo;
4a4d8108
AM
7642+ struct au_fidir *fidir;
7643+ struct au_hfile *hfile;
1facf9fc 7644+ aufs_bindex_t bindex;
7645+ int err;
7646+
7647+ err = do_pri_file(-1, file);
2000de60 7648+ if (err
b912730e 7649+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7650+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7651+ return;
7652+
7653+ finfo = au_fi(file);
7654+ if (!finfo)
7655+ return;
4a4d8108 7656+ if (finfo->fi_btop < 0)
1facf9fc 7657+ return;
4a4d8108
AM
7658+ fidir = finfo->fi_hdir;
7659+ if (!fidir)
7660+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7661+ else
e49829fe
JR
7662+ for (bindex = finfo->fi_btop;
7663+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7664+ bindex++) {
7665+ hfile = fidir->fd_hfile + bindex;
7666+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7667+ }
1facf9fc 7668+}
7669+
7670+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7671+{
7672+ struct vfsmount *mnt;
7673+ struct super_block *sb;
7674+
7675+ if (!br || IS_ERR(br))
7676+ goto out;
86dc4139 7677+ mnt = au_br_mnt(br);
1facf9fc 7678+ if (!mnt || IS_ERR(mnt))
7679+ goto out;
7680+ sb = mnt->mnt_sb;
7681+ if (!sb || IS_ERR(sb))
7682+ goto out;
7683+
acd2b654 7684+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
b752ccd1 7685+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7686+ "xino %d\n",
acd2b654
AM
7687+ bindex, br->br_perm, br->br_id, br->br_wbr,
7688+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7689+ sb->s_flags, sb->s_count,
acd2b654
AM
7690+ atomic_read(&sb->s_active),
7691+ !!au_xino_file(br->br_xino, /*idx*/-1));
1facf9fc 7692+ return 0;
7693+
4f0767ce 7694+out:
1facf9fc 7695+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7696+ return -1;
7697+}
7698+
7699+void au_dpri_sb(struct super_block *sb)
7700+{
7701+ struct au_sbinfo *sbinfo;
7702+ aufs_bindex_t bindex;
7703+ int err;
acd2b654 7704+ /* to reduce stack size */
1facf9fc 7705+ struct {
7706+ struct vfsmount mnt;
7707+ struct au_branch fake;
7708+ } *a;
7709+
7710+ /* this function can be called from magic sysrq */
7711+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7712+ if (unlikely(!a)) {
7713+ dpri("no memory\n");
7714+ return;
7715+ }
7716+
7717+ a->mnt.mnt_sb = sb;
86dc4139 7718+ a->fake.br_path.mnt = &a->mnt;
1facf9fc 7719+ err = do_pri_br(-1, &a->fake);
9f237c51 7720+ au_kfree_rcu(a);
1facf9fc 7721+ dpri("dev 0x%x\n", sb->s_dev);
7722+ if (err || !au_test_aufs(sb))
7723+ return;
7724+
7725+ sbinfo = au_sbi(sb);
7726+ if (!sbinfo)
7727+ return;
f0c0a007
AM
7728+ dpri("nw %d, gen %u, kobj %d\n",
7729+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7730+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7731+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7732+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7733+}
7734+
7735+/* ---------------------------------------------------------------------- */
7736+
027c5e7a
AM
7737+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7738+{
5527c038 7739+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7740+ struct dentry *h_dentry;
5afbbe0d 7741+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7742+
7743+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7744+ return;
7745+
5afbbe0d
AM
7746+ bbot = au_dbbot(dentry);
7747+ bi = au_ibbot(inode);
7748+ if (bi < bbot)
7749+ bbot = bi;
7750+ bindex = au_dbtop(dentry);
7751+ bi = au_ibtop(inode);
027c5e7a
AM
7752+ if (bi > bindex)
7753+ bindex = bi;
7754+
5afbbe0d 7755+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7756+ h_dentry = au_h_dptr(dentry, bindex);
7757+ if (!h_dentry)
7758+ continue;
7759+ h_inode = au_h_iptr(inode, bindex);
5527c038 7760+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7761+ au_debug_on();
027c5e7a
AM
7762+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7763+ AuDbgDentry(dentry);
7764+ AuDbgInode(inode);
392086de 7765+ au_debug_off();
027c5e7a
AM
7766+ BUG();
7767+ }
7768+ }
7769+}
7770+
1facf9fc 7771+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7772+{
7773+ int err, i, j;
7774+ struct au_dcsub_pages dpages;
7775+ struct au_dpage *dpage;
7776+ struct dentry **dentries;
7777+
7778+ err = au_dpages_init(&dpages, GFP_NOFS);
7779+ AuDebugOn(err);
027c5e7a 7780+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7781+ AuDebugOn(err);
7782+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7783+ dpage = dpages.dpages + i;
7784+ dentries = dpage->dentries;
7785+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7786+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7787+ }
7788+ au_dpages_free(&dpages);
7789+}
7790+
1facf9fc 7791+void au_dbg_verify_kthread(void)
7792+{
53392da6 7793+ if (au_wkq_test()) {
1facf9fc 7794+ au_dbg_blocked();
1e00d052
AM
7795+ /*
7796+ * It may be recursive, but udba=notify between two aufs mounts,
7797+ * where a single ro branch is shared, is not a problem.
7798+ */
7799+ /* WARN_ON(1); */
1facf9fc 7800+ }
7801+}
7802+
7803+/* ---------------------------------------------------------------------- */
7804+
1facf9fc 7805+int __init au_debug_init(void)
7806+{
7807+ aufs_bindex_t bindex;
7808+ struct au_vdir_destr destr;
7809+
7810+ bindex = -1;
7811+ AuDebugOn(bindex >= 0);
7812+
7813+ destr.len = -1;
7814+ AuDebugOn(destr.len < NAME_MAX);
7815+
7816+#ifdef CONFIG_4KSTACKS
0c3ec466 7817+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7818+#endif
7819+
1facf9fc 7820+ return 0;
7821+}
7f207e10 7822diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
eca34b5c 7823--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
016522bc 7824+++ linux/fs/aufs/debug.h 2020-01-27 10:57:18.168871450 +0100
062440b3
AM
7825@@ -0,0 +1,226 @@
7826+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7827+/*
016522bc 7828+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7829+ *
7830+ * This program, aufs is free software; you can redistribute it and/or modify
7831+ * it under the terms of the GNU General Public License as published by
7832+ * the Free Software Foundation; either version 2 of the License, or
7833+ * (at your option) any later version.
dece6358
AM
7834+ *
7835+ * This program is distributed in the hope that it will be useful,
7836+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7837+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7838+ * GNU General Public License for more details.
7839+ *
7840+ * You should have received a copy of the GNU General Public License
523b37e3 7841+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7842+ */
7843+
7844+/*
7845+ * debug print functions
7846+ */
7847+
7848+#ifndef __AUFS_DEBUG_H__
7849+#define __AUFS_DEBUG_H__
7850+
7851+#ifdef __KERNEL__
7852+
392086de 7853+#include <linux/atomic.h>
4a4d8108
AM
7854+#include <linux/module.h>
7855+#include <linux/kallsyms.h>
1facf9fc 7856+#include <linux/sysrq.h>
4a4d8108 7857+
1facf9fc 7858+#ifdef CONFIG_AUFS_DEBUG
7859+#define AuDebugOn(a) BUG_ON(a)
7860+
7861+/* module parameter */
392086de
AM
7862+extern atomic_t aufs_debug;
7863+static inline void au_debug_on(void)
1facf9fc 7864+{
392086de
AM
7865+ atomic_inc(&aufs_debug);
7866+}
7867+static inline void au_debug_off(void)
7868+{
7869+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7870+}
7871+
7872+static inline int au_debug_test(void)
7873+{
392086de 7874+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7875+}
7876+#else
7877+#define AuDebugOn(a) do {} while (0)
392086de
AM
7878+AuStubVoid(au_debug_on, void)
7879+AuStubVoid(au_debug_off, void)
4a4d8108 7880+AuStubInt0(au_debug_test, void)
1facf9fc 7881+#endif /* CONFIG_AUFS_DEBUG */
7882+
392086de
AM
7883+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7884+
1facf9fc 7885+/* ---------------------------------------------------------------------- */
7886+
7887+/* debug print */
7888+
4a4d8108 7889+#define AuDbg(fmt, ...) do { \
1facf9fc 7890+ if (au_debug_test()) \
4a4d8108 7891+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7892+} while (0)
4a4d8108
AM
7893+#define AuLabel(l) AuDbg(#l "\n")
7894+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7895+#define AuWarn1(fmt, ...) do { \
1facf9fc 7896+ static unsigned char _c; \
7897+ if (!_c++) \
0c3ec466 7898+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7899+} while (0)
7900+
4a4d8108 7901+#define AuErr1(fmt, ...) do { \
1facf9fc 7902+ static unsigned char _c; \
7903+ if (!_c++) \
4a4d8108 7904+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7905+} while (0)
7906+
4a4d8108 7907+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7908+ static unsigned char _c; \
7909+ if (!_c++) \
4a4d8108 7910+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7911+} while (0)
7912+
7913+#define AuUnsupportMsg "This operation is not supported." \
7914+ " Please report this application to aufs-users ML."
4a4d8108
AM
7915+#define AuUnsupport(fmt, ...) do { \
7916+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7917+ dump_stack(); \
7918+} while (0)
7919+
7920+#define AuTraceErr(e) do { \
7921+ if (unlikely((e) < 0)) \
7922+ AuDbg("err %d\n", (int)(e)); \
7923+} while (0)
7924+
7925+#define AuTraceErrPtr(p) do { \
7926+ if (IS_ERR(p)) \
7927+ AuDbg("err %ld\n", PTR_ERR(p)); \
7928+} while (0)
7929+
7930+/* dirty macros for debug print, use with "%.*s" and caution */
7931+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 7932+
7933+/* ---------------------------------------------------------------------- */
7934+
dece6358 7935+struct dentry;
1facf9fc 7936+#ifdef CONFIG_AUFS_DEBUG
c1595e42 7937+extern struct mutex au_dbg_mtx;
1facf9fc 7938+extern char *au_plevel;
7939+struct au_nhash;
7940+void au_dpri_whlist(struct au_nhash *whlist);
7941+struct au_vdir;
7942+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 7943+struct inode;
1facf9fc 7944+void au_dpri_inode(struct inode *inode);
2cbb1c4b 7945+void au_dpri_dalias(struct inode *inode);
1facf9fc 7946+void au_dpri_dentry(struct dentry *dentry);
dece6358 7947+struct file;
1facf9fc 7948+void au_dpri_file(struct file *filp);
dece6358 7949+struct super_block;
1facf9fc 7950+void au_dpri_sb(struct super_block *sb);
7951+
027c5e7a
AM
7952+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7953+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 7954+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 7955+void au_dbg_verify_kthread(void);
7956+
7957+int __init au_debug_init(void);
7e9cd9fe 7958+
1facf9fc 7959+#define AuDbgWhlist(w) do { \
c1595e42 7960+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7961+ AuDbg(#w "\n"); \
7962+ au_dpri_whlist(w); \
c1595e42 7963+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7964+} while (0)
7965+
7966+#define AuDbgVdir(v) do { \
c1595e42 7967+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7968+ AuDbg(#v "\n"); \
7969+ au_dpri_vdir(v); \
c1595e42 7970+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7971+} while (0)
7972+
7973+#define AuDbgInode(i) do { \
c1595e42 7974+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7975+ AuDbg(#i "\n"); \
7976+ au_dpri_inode(i); \
c1595e42 7977+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7978+} while (0)
7979+
2cbb1c4b 7980+#define AuDbgDAlias(i) do { \
c1595e42 7981+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
7982+ AuDbg(#i "\n"); \
7983+ au_dpri_dalias(i); \
c1595e42 7984+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
7985+} while (0)
7986+
1facf9fc 7987+#define AuDbgDentry(d) do { \
c1595e42 7988+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7989+ AuDbg(#d "\n"); \
7990+ au_dpri_dentry(d); \
c1595e42 7991+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7992+} while (0)
7993+
7994+#define AuDbgFile(f) do { \
c1595e42 7995+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7996+ AuDbg(#f "\n"); \
7997+ au_dpri_file(f); \
c1595e42 7998+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7999+} while (0)
8000+
8001+#define AuDbgSb(sb) do { \
c1595e42 8002+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8003+ AuDbg(#sb "\n"); \
8004+ au_dpri_sb(sb); \
c1595e42 8005+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8006+} while (0)
8007+
4a4d8108
AM
8008+#define AuDbgSym(addr) do { \
8009+ char sym[KSYM_SYMBOL_LEN]; \
8010+ sprint_symbol(sym, (unsigned long)addr); \
8011+ AuDbg("%s\n", sym); \
8012+} while (0)
1facf9fc 8013+#else
027c5e7a 8014+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
8015+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8016+AuStubVoid(au_dbg_verify_kthread, void)
8017+AuStubInt0(__init au_debug_init, void)
1facf9fc 8018+
1facf9fc 8019+#define AuDbgWhlist(w) do {} while (0)
8020+#define AuDbgVdir(v) do {} while (0)
8021+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 8022+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 8023+#define AuDbgDentry(d) do {} while (0)
8024+#define AuDbgFile(f) do {} while (0)
8025+#define AuDbgSb(sb) do {} while (0)
4a4d8108 8026+#define AuDbgSym(addr) do {} while (0)
1facf9fc 8027+#endif /* CONFIG_AUFS_DEBUG */
8028+
8029+/* ---------------------------------------------------------------------- */
8030+
8031+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8032+int __init au_sysrq_init(void);
8033+void au_sysrq_fin(void);
8034+
8035+#ifdef CONFIG_HW_CONSOLE
8036+#define au_dbg_blocked() do { \
8037+ WARN_ON(1); \
0c5527e5 8038+ handle_sysrq('w'); \
1facf9fc 8039+} while (0)
8040+#else
4a4d8108 8041+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8042+#endif
8043+
8044+#else
4a4d8108
AM
8045+AuStubInt0(__init au_sysrq_init, void)
8046+AuStubVoid(au_sysrq_fin, void)
8047+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8048+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8049+
8050+#endif /* __KERNEL__ */
8051+#endif /* __AUFS_DEBUG_H__ */
7f207e10 8052diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
eca34b5c 8053--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
016522bc 8054+++ linux/fs/aufs/dentry.c 2020-01-27 10:57:18.168871450 +0100
eca801bf 8055@@ -0,0 +1,1154 @@
cd7a4cd9 8056+// SPDX-License-Identifier: GPL-2.0
1facf9fc 8057+/*
016522bc 8058+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 8059+ *
8060+ * This program, aufs is free software; you can redistribute it and/or modify
8061+ * it under the terms of the GNU General Public License as published by
8062+ * the Free Software Foundation; either version 2 of the License, or
8063+ * (at your option) any later version.
dece6358
AM
8064+ *
8065+ * This program is distributed in the hope that it will be useful,
8066+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8067+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8068+ * GNU General Public License for more details.
8069+ *
8070+ * You should have received a copy of the GNU General Public License
523b37e3 8071+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8072+ */
8073+
8074+/*
8075+ * lookup and dentry operations
8076+ */
8077+
eca801bf 8078+#include <linux/iversion.h>
dece6358 8079+#include <linux/namei.h>
1facf9fc 8080+#include "aufs.h"
8081+
1facf9fc 8082+/*
8083+ * returns positive/negative dentry, NULL or an error.
8084+ * NULL means whiteout-ed or not-found.
8085+ */
8086+static struct dentry*
8087+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8088+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8089+{
8090+ struct dentry *h_dentry;
2000de60 8091+ struct inode *h_inode;
1facf9fc 8092+ struct au_branch *br;
8093+ int wh_found, opq;
8094+ unsigned char wh_able;
8095+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8096+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8097+ IGNORE_PERM);
1facf9fc 8098+
1facf9fc 8099+ wh_found = 0;
8100+ br = au_sbr(dentry->d_sb, bindex);
8101+ wh_able = !!au_br_whable(br->br_perm);
8102+ if (wh_able)
8b6a4947 8103+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
1facf9fc 8104+ h_dentry = ERR_PTR(wh_found);
8105+ if (!wh_found)
8106+ goto real_lookup;
8107+ if (unlikely(wh_found < 0))
8108+ goto out;
8109+
8110+ /* We found a whiteout */
5afbbe0d 8111+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8112+ au_set_dbwh(dentry, bindex);
8113+ if (!allow_neg)
8114+ return NULL; /* success */
8115+
4f0767ce 8116+real_lookup:
076b876e 8117+ if (!ignore_perm)
8b6a4947 8118+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8119+ else
8b6a4947 8120+ h_dentry = au_sio_lkup_one(args->name, h_parent);
2000de60
JR
8121+ if (IS_ERR(h_dentry)) {
8122+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8123+ && !allow_neg)
8124+ h_dentry = NULL;
1facf9fc 8125+ goto out;
2000de60 8126+ }
1facf9fc 8127+
5527c038
JR
8128+ h_inode = d_inode(h_dentry);
8129+ if (d_is_negative(h_dentry)) {
1facf9fc 8130+ if (!allow_neg)
8131+ goto out_neg;
8132+ } else if (wh_found
8133+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8134+ goto out_neg;
8b6a4947
AM
8135+ else if (au_ftest_lkup(args->flags, DIRREN)
8136+ /* && h_inode */
8137+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8138+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8139+ (unsigned long long)h_inode->i_ino);
8140+ goto out_neg;
8141+ }
1facf9fc 8142+
5afbbe0d
AM
8143+ if (au_dbbot(dentry) <= bindex)
8144+ au_set_dbbot(dentry, bindex);
8145+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8146+ au_set_dbtop(dentry, bindex);
1facf9fc 8147+ au_set_h_dptr(dentry, bindex, h_dentry);
8148+
2000de60
JR
8149+ if (!d_is_dir(h_dentry)
8150+ || !wh_able
5527c038 8151+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8152+ goto out; /* success */
8153+
be118d29 8154+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
076b876e 8155+ opq = au_diropq_test(h_dentry);
3c1bdaff 8156+ inode_unlock_shared(h_inode);
1facf9fc 8157+ if (opq > 0)
8158+ au_set_dbdiropq(dentry, bindex);
8159+ else if (unlikely(opq < 0)) {
8160+ au_set_h_dptr(dentry, bindex, NULL);
8161+ h_dentry = ERR_PTR(opq);
8162+ }
8163+ goto out;
8164+
4f0767ce 8165+out_neg:
1facf9fc 8166+ dput(h_dentry);
8167+ h_dentry = NULL;
4f0767ce 8168+out:
1facf9fc 8169+ return h_dentry;
8170+}
8171+
dece6358
AM
8172+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8173+{
8174+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8175+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8176+ return -EPERM;
8177+ return 0;
8178+}
8179+
1facf9fc 8180+/*
8181+ * returns the number of lower positive dentries,
8182+ * otherwise an error.
8183+ * can be called at unlinking with @type is zero.
8184+ */
5afbbe0d
AM
8185+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8186+ unsigned int flags)
1facf9fc 8187+{
8188+ int npositive, err;
8189+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8190+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8191+ struct au_do_lookup_args args = {
8b6a4947
AM
8192+ .flags = flags,
8193+ .name = &dentry->d_name
1facf9fc 8194+ };
1facf9fc 8195+ struct dentry *parent;
076b876e 8196+ struct super_block *sb;
1facf9fc 8197+
076b876e 8198+ sb = dentry->d_sb;
8b6a4947 8199+ err = au_test_shwh(sb, args.name);
dece6358 8200+ if (unlikely(err))
1facf9fc 8201+ goto out;
8202+
8b6a4947 8203+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8204+ if (unlikely(err))
8205+ goto out;
8206+
2000de60 8207+ isdir = !!d_is_dir(dentry);
076b876e 8208+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8209+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8210+ if (dirren)
8211+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8212+
8213+ npositive = 0;
4a4d8108 8214+ parent = dget_parent(dentry);
1facf9fc 8215+ btail = au_dbtaildir(parent);
5afbbe0d 8216+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8217+ struct dentry *h_parent, *h_dentry;
8218+ struct inode *h_inode, *h_dir;
8b6a4947 8219+ struct au_branch *br;
1facf9fc 8220+
8221+ h_dentry = au_h_dptr(dentry, bindex);
8222+ if (h_dentry) {
5527c038 8223+ if (d_is_positive(h_dentry))
1facf9fc 8224+ npositive++;
5afbbe0d 8225+ break;
1facf9fc 8226+ }
8227+ h_parent = au_h_dptr(parent, bindex);
2000de60 8228+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8229+ continue;
8230+
8b6a4947
AM
8231+ if (dirren) {
8232+ /* if the inum matches, then use the prepared name */
8233+ err = au_dr_lkup_name(&args, bindex);
8234+ if (unlikely(err))
8235+ goto out_parent;
8236+ }
8237+
5527c038 8238+ h_dir = d_inode(h_parent);
be118d29 8239+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8240+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8241+ inode_unlock_shared(h_dir);
1facf9fc 8242+ err = PTR_ERR(h_dentry);
8243+ if (IS_ERR(h_dentry))
4a4d8108 8244+ goto out_parent;
2000de60
JR
8245+ if (h_dentry)
8246+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8247+ if (dirperm1)
8248+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8249+
79b8bda9 8250+ if (au_dbwh(dentry) == bindex)
1facf9fc 8251+ break;
8252+ if (!h_dentry)
8253+ continue;
5527c038 8254+ if (d_is_negative(h_dentry))
1facf9fc 8255+ continue;
5527c038 8256+ h_inode = d_inode(h_dentry);
1facf9fc 8257+ npositive++;
8258+ if (!args.type)
8259+ args.type = h_inode->i_mode & S_IFMT;
8260+ if (args.type != S_IFDIR)
8261+ break;
8262+ else if (isdir) {
8263+ /* the type of lower may be different */
8264+ bdiropq = au_dbdiropq(dentry);
8265+ if (bdiropq >= 0 && bdiropq <= bindex)
8266+ break;
8267+ }
8b6a4947
AM
8268+ br = au_sbr(sb, bindex);
8269+ if (dirren
8270+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8271+ /*add_ent*/NULL)) {
8272+ /* prepare next name to lookup */
8273+ err = au_dr_lkup(&args, dentry, bindex);
8274+ if (unlikely(err))
8275+ goto out_parent;
8276+ }
1facf9fc 8277+ }
8278+
8279+ if (npositive) {
8280+ AuLabel(positive);
5afbbe0d 8281+ au_update_dbtop(dentry);
1facf9fc 8282+ }
8283+ err = npositive;
076b876e 8284+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8285+ && au_dbtop(dentry) < 0)) {
1facf9fc 8286+ err = -EIO;
523b37e3
AM
8287+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8288+ dentry, err);
027c5e7a 8289+ }
1facf9fc 8290+
4f0767ce 8291+out_parent:
4a4d8108 8292+ dput(parent);
9f237c51 8293+ au_kfree_try_rcu(args.whname.name);
8b6a4947
AM
8294+ if (dirren)
8295+ au_dr_lkup_fin(&args);
4f0767ce 8296+out:
1facf9fc 8297+ return err;
8298+}
8299+
076b876e 8300+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 8301+{
8302+ struct dentry *dentry;
8303+ int wkq_err;
8304+
5527c038 8305+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 8306+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8307+ else {
b4510431
AM
8308+ struct vfsub_lkup_one_args args = {
8309+ .errp = &dentry,
8310+ .name = name,
8311+ .parent = parent
1facf9fc 8312+ };
8313+
b4510431 8314+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8315+ if (unlikely(wkq_err))
8316+ dentry = ERR_PTR(wkq_err);
8317+ }
8318+
8319+ return dentry;
8320+}
8321+
8322+/*
8323+ * lookup @dentry on @bindex which should be negative.
8324+ */
86dc4139 8325+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8326+{
8327+ int err;
8328+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8329+ struct au_branch *br;
1facf9fc 8330+
1facf9fc 8331+ parent = dget_parent(dentry);
8332+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
8333+ br = au_sbr(dentry->d_sb, bindex);
8334+ if (wh)
8335+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8336+ else
076b876e 8337+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 8338+ err = PTR_ERR(h_dentry);
8339+ if (IS_ERR(h_dentry))
8340+ goto out;
5527c038 8341+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8342+ err = -EIO;
523b37e3 8343+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8344+ dput(h_dentry);
8345+ goto out;
8346+ }
8347+
4a4d8108 8348+ err = 0;
5afbbe0d
AM
8349+ if (bindex < au_dbtop(dentry))
8350+ au_set_dbtop(dentry, bindex);
8351+ if (au_dbbot(dentry) < bindex)
8352+ au_set_dbbot(dentry, bindex);
1facf9fc 8353+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8354+
4f0767ce 8355+out:
1facf9fc 8356+ dput(parent);
8357+ return err;
8358+}
8359+
8360+/* ---------------------------------------------------------------------- */
8361+
8362+/* subset of struct inode */
8363+struct au_iattr {
8364+ unsigned long i_ino;
8365+ /* unsigned int i_nlink; */
0c3ec466
AM
8366+ kuid_t i_uid;
8367+ kgid_t i_gid;
1facf9fc 8368+ u64 i_version;
8369+/*
8370+ loff_t i_size;
8371+ blkcnt_t i_blocks;
8372+*/
8373+ umode_t i_mode;
8374+};
8375+
8376+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8377+{
8378+ ia->i_ino = h_inode->i_ino;
8379+ /* ia->i_nlink = h_inode->i_nlink; */
8380+ ia->i_uid = h_inode->i_uid;
8381+ ia->i_gid = h_inode->i_gid;
be118d29 8382+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8383+/*
8384+ ia->i_size = h_inode->i_size;
8385+ ia->i_blocks = h_inode->i_blocks;
8386+*/
8387+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8388+}
8389+
8390+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8391+{
8392+ return ia->i_ino != h_inode->i_ino
8393+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8394+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8395+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8396+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8397+/*
8398+ || ia->i_size != h_inode->i_size
8399+ || ia->i_blocks != h_inode->i_blocks
8400+*/
8401+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8402+}
8403+
8404+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8405+ struct au_branch *br)
8406+{
8407+ int err;
8408+ struct au_iattr ia;
8409+ struct inode *h_inode;
8410+ struct dentry *h_d;
8411+ struct super_block *h_sb;
8412+
8413+ err = 0;
8414+ memset(&ia, -1, sizeof(ia));
8415+ h_sb = h_dentry->d_sb;
5527c038
JR
8416+ h_inode = NULL;
8417+ if (d_is_positive(h_dentry)) {
8418+ h_inode = d_inode(h_dentry);
1facf9fc 8419+ au_iattr_save(&ia, h_inode);
5527c038 8420+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8421+ /* nfs d_revalidate may return 0 for negative dentry */
8422+ /* fuse d_revalidate always return 0 for negative dentry */
8423+ goto out;
8424+
8425+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8426+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8427+ err = PTR_ERR(h_d);
8428+ if (IS_ERR(h_d))
8429+ goto out;
8430+
8431+ err = 0;
8432+ if (unlikely(h_d != h_dentry
5527c038 8433+ || d_inode(h_d) != h_inode
1facf9fc 8434+ || (h_inode && au_iattr_test(&ia, h_inode))))
8435+ err = au_busy_or_stale();
8436+ dput(h_d);
8437+
4f0767ce 8438+out:
1facf9fc 8439+ AuTraceErr(err);
8440+ return err;
8441+}
8442+
8443+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8444+ struct dentry *h_parent, struct au_branch *br)
8445+{
8446+ int err;
8447+
8448+ err = 0;
027c5e7a
AM
8449+ if (udba == AuOpt_UDBA_REVAL
8450+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8451+ IMustLock(h_dir);
5527c038 8452+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8453+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8454+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8455+
8456+ return err;
8457+}
8458+
8459+/* ---------------------------------------------------------------------- */
8460+
027c5e7a 8461+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8462+{
027c5e7a 8463+ int err;
5afbbe0d 8464+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8465+ struct au_hdentry tmp, *p, *q;
8466+ struct au_dinfo *dinfo;
8467+ struct super_block *sb;
1facf9fc 8468+
027c5e7a 8469+ DiMustWriteLock(dentry);
1308ab2a 8470+
027c5e7a
AM
8471+ sb = dentry->d_sb;
8472+ dinfo = au_di(dentry);
5afbbe0d 8473+ bbot = dinfo->di_bbot;
1facf9fc 8474+ bwh = dinfo->di_bwh;
8475+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8476+ bindex = dinfo->di_btop;
8477+ p = au_hdentry(dinfo, bindex);
8478+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8479+ if (!p->hd_dentry)
1facf9fc 8480+ continue;
8481+
027c5e7a
AM
8482+ new_bindex = au_br_index(sb, p->hd_id);
8483+ if (new_bindex == bindex)
1facf9fc 8484+ continue;
1facf9fc 8485+
1facf9fc 8486+ if (dinfo->di_bwh == bindex)
8487+ bwh = new_bindex;
8488+ if (dinfo->di_bdiropq == bindex)
8489+ bdiropq = new_bindex;
8490+ if (new_bindex < 0) {
8491+ au_hdput(p);
8492+ p->hd_dentry = NULL;
8493+ continue;
8494+ }
8495+
8496+ /* swap two lower dentries, and loop again */
5afbbe0d 8497+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8498+ tmp = *q;
8499+ *q = *p;
8500+ *p = tmp;
8501+ if (tmp.hd_dentry) {
8502+ bindex--;
8503+ p--;
8504+ }
8505+ }
8506+
1facf9fc 8507+ dinfo->di_bwh = -1;
5afbbe0d 8508+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8509+ dinfo->di_bwh = bwh;
8510+
8511+ dinfo->di_bdiropq = -1;
8512+ if (bdiropq >= 0
5afbbe0d 8513+ && bdiropq <= au_sbbot(sb)
1facf9fc 8514+ && au_sbr_whable(sb, bdiropq))
8515+ dinfo->di_bdiropq = bdiropq;
8516+
027c5e7a 8517+ err = -EIO;
5afbbe0d
AM
8518+ dinfo->di_btop = -1;
8519+ dinfo->di_bbot = -1;
8520+ bbot = au_dbbot(parent);
8521+ bindex = 0;
8522+ p = au_hdentry(dinfo, bindex);
8523+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8524+ if (p->hd_dentry) {
5afbbe0d 8525+ dinfo->di_btop = bindex;
1facf9fc 8526+ break;
8527+ }
8528+
5afbbe0d
AM
8529+ if (dinfo->di_btop >= 0) {
8530+ bindex = bbot;
8531+ p = au_hdentry(dinfo, bindex);
8532+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8533+ if (p->hd_dentry) {
5afbbe0d 8534+ dinfo->di_bbot = bindex;
027c5e7a
AM
8535+ err = 0;
8536+ break;
8537+ }
8538+ }
8539+
8540+ return err;
1facf9fc 8541+}
8542+
027c5e7a 8543+static void au_do_hide(struct dentry *dentry)
1facf9fc 8544+{
027c5e7a 8545+ struct inode *inode;
1facf9fc 8546+
5527c038
JR
8547+ if (d_really_is_positive(dentry)) {
8548+ inode = d_inode(dentry);
8549+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8550+ if (inode->i_nlink && !d_unhashed(dentry))
8551+ drop_nlink(inode);
8552+ } else {
8553+ clear_nlink(inode);
8554+ /* stop next lookup */
8555+ inode->i_flags |= S_DEAD;
8556+ }
8557+ smp_mb(); /* necessary? */
8558+ }
8559+ d_drop(dentry);
8560+}
1308ab2a 8561+
027c5e7a
AM
8562+static int au_hide_children(struct dentry *parent)
8563+{
8564+ int err, i, j, ndentry;
8565+ struct au_dcsub_pages dpages;
8566+ struct au_dpage *dpage;
8567+ struct dentry *dentry;
1facf9fc 8568+
027c5e7a 8569+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8570+ if (unlikely(err))
8571+ goto out;
027c5e7a
AM
8572+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8573+ if (unlikely(err))
8574+ goto out_dpages;
1facf9fc 8575+
027c5e7a
AM
8576+ /* in reverse order */
8577+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8578+ dpage = dpages.dpages + i;
8579+ ndentry = dpage->ndentry;
8580+ for (j = ndentry - 1; j >= 0; j--) {
8581+ dentry = dpage->dentries[j];
8582+ if (dentry != parent)
8583+ au_do_hide(dentry);
8584+ }
8585+ }
1facf9fc 8586+
027c5e7a
AM
8587+out_dpages:
8588+ au_dpages_free(&dpages);
4f0767ce 8589+out:
027c5e7a 8590+ return err;
1facf9fc 8591+}
8592+
027c5e7a 8593+static void au_hide(struct dentry *dentry)
1facf9fc 8594+{
027c5e7a 8595+ int err;
1facf9fc 8596+
027c5e7a 8597+ AuDbgDentry(dentry);
2000de60 8598+ if (d_is_dir(dentry)) {
027c5e7a
AM
8599+ /* shrink_dcache_parent(dentry); */
8600+ err = au_hide_children(dentry);
8601+ if (unlikely(err))
523b37e3
AM
8602+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8603+ dentry, err);
027c5e7a
AM
8604+ }
8605+ au_do_hide(dentry);
8606+}
1facf9fc 8607+
027c5e7a
AM
8608+/*
8609+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8610+ *
8611+ * a dirty branch is added
8612+ * - on the top of layers
8613+ * - in the middle of layers
8614+ * - to the bottom of layers
8615+ *
8616+ * on the added branch there exists
8617+ * - a whiteout
8618+ * - a diropq
8619+ * - a same named entry
8620+ * + exist
8621+ * * negative --> positive
8622+ * * positive --> positive
8623+ * - type is unchanged
8624+ * - type is changed
8625+ * + doesn't exist
8626+ * * negative --> negative
8627+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8628+ * - none
8629+ */
8630+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8631+ struct au_dinfo *tmp)
8632+{
8633+ int err;
5afbbe0d 8634+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8635+ struct {
8636+ struct dentry *dentry;
8637+ struct inode *inode;
8638+ mode_t mode;
be52b249
AM
8639+ } orig_h, tmp_h = {
8640+ .dentry = NULL
8641+ };
027c5e7a
AM
8642+ struct au_hdentry *hd;
8643+ struct inode *inode, *h_inode;
8644+ struct dentry *h_dentry;
8645+
8646+ err = 0;
5afbbe0d 8647+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8648+ orig_h.mode = 0;
5afbbe0d 8649+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8650+ orig_h.inode = NULL;
8651+ if (d_is_positive(orig_h.dentry)) {
8652+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8653+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8654+ }
5afbbe0d
AM
8655+ if (tmp->di_btop >= 0) {
8656+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8657+ if (d_is_positive(tmp_h.dentry)) {
8658+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8659+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8660+ }
027c5e7a
AM
8661+ }
8662+
5527c038
JR
8663+ inode = NULL;
8664+ if (d_really_is_positive(dentry))
8665+ inode = d_inode(dentry);
027c5e7a 8666+ if (!orig_h.inode) {
acd2b654 8667+ AuDbg("negative originally\n");
027c5e7a
AM
8668+ if (inode) {
8669+ au_hide(dentry);
8670+ goto out;
8671+ }
8672+ AuDebugOn(inode);
5afbbe0d 8673+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8674+ AuDebugOn(dinfo->di_bdiropq != -1);
8675+
8676+ if (!tmp_h.inode) {
8677+ AuDbg("negative --> negative\n");
8678+ /* should have only one negative lower */
5afbbe0d
AM
8679+ if (tmp->di_btop >= 0
8680+ && tmp->di_btop < dinfo->di_btop) {
8681+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8682+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8683+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8684+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8685+ hd = au_hdentry(tmp, tmp->di_btop);
8686+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8687+ dget(hd->hd_dentry));
8688+ }
8689+ au_dbg_verify_dinode(dentry);
8690+ } else {
8691+ AuDbg("negative --> positive\n");
8692+ /*
8693+ * similar to the behaviour of creating with bypassing
8694+ * aufs.
8695+ * unhash it in order to force an error in the
8696+ * succeeding create operation.
8697+ * we should not set S_DEAD here.
8698+ */
8699+ d_drop(dentry);
8700+ /* au_di_swap(tmp, dinfo); */
8701+ au_dbg_verify_dinode(dentry);
8702+ }
8703+ } else {
8704+ AuDbg("positive originally\n");
8705+ /* inode may be NULL */
8706+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8707+ if (!tmp_h.inode) {
8708+ AuDbg("positive --> negative\n");
8709+ /* or bypassing aufs */
8710+ au_hide(dentry);
5afbbe0d 8711+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8712+ dinfo->di_bwh = tmp->di_bwh;
8713+ if (inode)
8714+ err = au_refresh_hinode_self(inode);
8715+ au_dbg_verify_dinode(dentry);
8716+ } else if (orig_h.mode == tmp_h.mode) {
8717+ AuDbg("positive --> positive, same type\n");
8718+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8719+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8720+ /*
8721+ * similar to the behaviour of removing and
8722+ * creating.
8723+ */
8724+ au_hide(dentry);
8725+ if (inode)
8726+ err = au_refresh_hinode_self(inode);
8727+ au_dbg_verify_dinode(dentry);
8728+ } else {
8729+ /* fill empty slots */
5afbbe0d
AM
8730+ if (dinfo->di_btop > tmp->di_btop)
8731+ dinfo->di_btop = tmp->di_btop;
8732+ if (dinfo->di_bbot < tmp->di_bbot)
8733+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8734+ dinfo->di_bwh = tmp->di_bwh;
8735+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8736+ bbot = dinfo->di_bbot;
8737+ bindex = tmp->di_btop;
8738+ hd = au_hdentry(tmp, bindex);
8739+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8740+ if (au_h_dptr(dentry, bindex))
8741+ continue;
5afbbe0d 8742+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8743+ if (!h_dentry)
8744+ continue;
5527c038
JR
8745+ AuDebugOn(d_is_negative(h_dentry));
8746+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8747+ AuDebugOn(orig_h.mode
8748+ != (h_inode->i_mode
8749+ & S_IFMT));
8750+ au_set_h_dptr(dentry, bindex,
8751+ dget(h_dentry));
8752+ }
5afbbe0d
AM
8753+ if (inode)
8754+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8755+ au_dbg_verify_dinode(dentry);
8756+ }
8757+ } else {
8758+ AuDbg("positive --> positive, different type\n");
8759+ /* similar to the behaviour of removing and creating */
8760+ au_hide(dentry);
8761+ if (inode)
8762+ err = au_refresh_hinode_self(inode);
8763+ au_dbg_verify_dinode(dentry);
8764+ }
8765+ }
8766+
8767+out:
8768+ return err;
8769+}
8770+
79b8bda9
AM
8771+void au_refresh_dop(struct dentry *dentry, int force_reval)
8772+{
8773+ const struct dentry_operations *dop
8774+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8775+ static const unsigned int mask
8776+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8777+
8778+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8779+
8780+ if (dentry->d_op == dop)
8781+ return;
8782+
8783+ AuDbg("%pd\n", dentry);
8784+ spin_lock(&dentry->d_lock);
8785+ if (dop == &aufs_dop)
8786+ dentry->d_flags |= mask;
8787+ else
8788+ dentry->d_flags &= ~mask;
8789+ dentry->d_op = dop;
8790+ spin_unlock(&dentry->d_lock);
8791+}
8792+
027c5e7a
AM
8793+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8794+{
e2f27e51 8795+ int err, ebrange, nbr;
027c5e7a
AM
8796+ unsigned int sigen;
8797+ struct au_dinfo *dinfo, *tmp;
8798+ struct super_block *sb;
8799+ struct inode *inode;
8800+
8801+ DiMustWriteLock(dentry);
8802+ AuDebugOn(IS_ROOT(dentry));
5527c038 8803+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8804+
8805+ sb = dentry->d_sb;
027c5e7a
AM
8806+ sigen = au_sigen(sb);
8807+ err = au_digen_test(parent, sigen);
8808+ if (unlikely(err))
8809+ goto out;
8810+
e2f27e51 8811+ nbr = au_sbbot(sb) + 1;
027c5e7a 8812+ dinfo = au_di(dentry);
e2f27e51 8813+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8814+ if (unlikely(err))
8815+ goto out;
8816+ ebrange = au_dbrange_test(dentry);
8817+ if (!ebrange)
8818+ ebrange = au_do_refresh_hdentry(dentry, parent);
8819+
38d290e6 8820+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8821+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8822+ if (d_really_is_positive(dentry)) {
8823+ inode = d_inode(dentry);
027c5e7a 8824+ err = au_refresh_hinode_self(inode);
5527c038 8825+ }
027c5e7a
AM
8826+ au_dbg_verify_dinode(dentry);
8827+ if (!err)
8828+ goto out_dgen; /* success */
8829+ goto out;
8830+ }
8831+
8832+ /* temporary dinfo */
8833+ AuDbgDentry(dentry);
8834+ err = -ENOMEM;
8835+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8836+ if (unlikely(!tmp))
8837+ goto out;
8838+ au_di_swap(tmp, dinfo);
8839+ /* returns the number of positive dentries */
8840+ /*
8841+ * if current working dir is removed, it returns an error.
8842+ * but the dentry is legal.
8843+ */
5afbbe0d 8844+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8845+ AuDbgDentry(dentry);
8846+ au_di_swap(tmp, dinfo);
8847+ if (err == -ENOENT)
8848+ err = 0;
8849+ if (err >= 0) {
8850+ /* compare/refresh by dinfo */
8851+ AuDbgDentry(dentry);
8852+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8853+ au_dbg_verify_dinode(dentry);
8854+ AuTraceErr(err);
8855+ }
e2f27e51 8856+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8857+ au_rw_write_unlock(&tmp->di_rwsem);
8858+ au_di_free(tmp);
8859+ if (unlikely(err))
8860+ goto out;
8861+
8862+out_dgen:
8863+ au_update_digen(dentry);
8864+out:
8865+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8866+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8867+ AuDbgDentry(dentry);
8868+ }
8869+ AuTraceErr(err);
8870+ return err;
8871+}
8872+
b4510431
AM
8873+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8874+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8875+{
8876+ int err, valid;
027c5e7a
AM
8877+
8878+ err = 0;
8879+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8880+ goto out;
027c5e7a
AM
8881+
8882+ AuDbg("b%d\n", bindex);
b4510431
AM
8883+ /*
8884+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8885+ * due to whiteout and branch permission.
8886+ */
8887+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8888+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8889+ /* it may return tri-state */
8890+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8891+
8892+ if (unlikely(valid < 0))
8893+ err = valid;
8894+ else if (!valid)
8895+ err = -EINVAL;
8896+
4f0767ce 8897+out:
1facf9fc 8898+ AuTraceErr(err);
8899+ return err;
8900+}
8901+
8902+/* todo: remove this */
8903+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8904+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8905+{
8906+ int err;
8907+ umode_t mode, h_mode;
5afbbe0d 8908+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8909+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8910+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8911+ struct dentry *h_dentry;
8912+ struct qstr *name, *h_name;
8913+
8914+ err = 0;
8915+ plus = 0;
8916+ mode = 0;
1facf9fc 8917+ ibs = -1;
8918+ ibe = -1;
8919+ unhashed = !!d_unhashed(dentry);
8920+ is_root = !!IS_ROOT(dentry);
8921+ name = &dentry->d_name;
38d290e6 8922+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8923+
8924+ /*
7f207e10
AM
8925+ * Theoretically, REVAL test should be unnecessary in case of
8926+ * {FS,I}NOTIFY.
8927+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8928+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8929+ * Let's do REVAL test too.
8930+ */
8931+ if (do_udba && inode) {
8932+ mode = (inode->i_mode & S_IFMT);
8933+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
8934+ ibs = au_ibtop(inode);
8935+ ibe = au_ibbot(inode);
1facf9fc 8936+ }
8937+
5afbbe0d
AM
8938+ btop = au_dbtop(dentry);
8939+ btail = btop;
1facf9fc 8940+ if (inode && S_ISDIR(inode->i_mode))
8941+ btail = au_dbtaildir(dentry);
5afbbe0d 8942+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8943+ h_dentry = au_h_dptr(dentry, bindex);
8944+ if (!h_dentry)
8945+ continue;
8946+
523b37e3
AM
8947+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8948+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 8949+ spin_lock(&h_dentry->d_lock);
1facf9fc 8950+ h_name = &h_dentry->d_name;
8951+ if (unlikely(do_udba
8952+ && !is_root
523b37e3
AM
8953+ && ((!h_nfs
8954+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 8955+ || (!tmpfile && !dirren
38d290e6
JR
8956+ && !au_qstreq(name, h_name))
8957+ ))
523b37e3
AM
8958+ || (h_nfs
8959+ && !(flags & LOOKUP_OPEN)
8960+ && (h_dentry->d_flags
8961+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 8962+ )) {
38d290e6
JR
8963+ int h_unhashed;
8964+
8965+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 8966+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
8967+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8968+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 8969+ goto err;
8970+ }
027c5e7a 8971+ spin_unlock(&h_dentry->d_lock);
1facf9fc 8972+
b4510431 8973+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 8974+ if (unlikely(err))
8975+ /* do not goto err, to keep the errno */
8976+ break;
8977+
8978+ /* todo: plink too? */
8979+ if (!do_udba)
8980+ continue;
8981+
8982+ /* UDBA tests */
5527c038 8983+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 8984+ goto err;
8985+
5527c038
JR
8986+ h_inode = NULL;
8987+ if (d_is_positive(h_dentry))
8988+ h_inode = d_inode(h_dentry);
1facf9fc 8989+ h_plus = plus;
8990+ h_mode = mode;
8991+ h_cached_inode = h_inode;
8992+ if (h_inode) {
8993+ h_mode = (h_inode->i_mode & S_IFMT);
8994+ h_plus = (h_inode->i_nlink > 0);
8995+ }
8996+ if (inode && ibs <= bindex && bindex <= ibe)
8997+ h_cached_inode = au_h_iptr(inode, bindex);
8998+
523b37e3 8999+ if (!h_nfs) {
38d290e6 9000+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
9001+ goto err;
9002+ } else {
9003+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9004+ && !is_root
9005+ && !IS_ROOT(h_dentry)
9006+ && unhashed != d_unhashed(h_dentry)))
9007+ goto err;
9008+ }
9009+ if (unlikely(mode != h_mode
1facf9fc 9010+ || h_cached_inode != h_inode))
9011+ goto err;
9012+ continue;
9013+
f6b6e03d 9014+err:
1facf9fc 9015+ err = -EINVAL;
9016+ break;
9017+ }
9018+
523b37e3 9019+ AuTraceErr(err);
1facf9fc 9020+ return err;
9021+}
9022+
027c5e7a 9023+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 9024+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9025+{
9026+ int err;
9027+ struct dentry *parent;
1facf9fc 9028+
027c5e7a 9029+ if (!au_digen_test(dentry, sigen))
1facf9fc 9030+ return 0;
9031+
9032+ parent = dget_parent(dentry);
9033+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9034+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 9035+ au_dbg_verify_gen(parent, sigen);
027c5e7a 9036+ err = au_refresh_dentry(dentry, parent);
1facf9fc 9037+ di_read_unlock(parent, AuLock_IR);
9038+ dput(parent);
027c5e7a 9039+ AuTraceErr(err);
1facf9fc 9040+ return err;
9041+}
9042+
9043+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9044+{
9045+ int err;
9046+ struct dentry *d, *parent;
1facf9fc 9047+
027c5e7a 9048+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 9049+ return simple_reval_dpath(dentry, sigen);
9050+
9051+ /* slow loop, keep it simple and stupid */
9052+ /* cf: au_cpup_dirs() */
9053+ err = 0;
9054+ parent = NULL;
027c5e7a 9055+ while (au_digen_test(dentry, sigen)) {
1facf9fc 9056+ d = dentry;
9057+ while (1) {
9058+ dput(parent);
9059+ parent = dget_parent(d);
027c5e7a 9060+ if (!au_digen_test(parent, sigen))
1facf9fc 9061+ break;
9062+ d = parent;
9063+ }
9064+
1facf9fc 9065+ if (d != dentry)
027c5e7a 9066+ di_write_lock_child2(d);
1facf9fc 9067+
9068+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9069+ if (au_digen_test(d, sigen)) {
9070+ /*
9071+ * todo: consolidate with simple_reval_dpath(),
9072+ * do_refresh() and au_reval_for_attr().
9073+ */
1facf9fc 9074+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9075+ err = au_refresh_dentry(d, parent);
1facf9fc 9076+ di_read_unlock(parent, AuLock_IR);
9077+ }
9078+
9079+ if (d != dentry)
9080+ di_write_unlock(d);
9081+ dput(parent);
9082+ if (unlikely(err))
9083+ break;
9084+ }
9085+
9086+ return err;
9087+}
9088+
9089+/*
9090+ * if valid returns 1, otherwise 0.
9091+ */
b4510431 9092+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9093+{
9094+ int valid, err;
9095+ unsigned int sigen;
8b6a4947 9096+ unsigned char do_udba, dirren;
1facf9fc 9097+ struct super_block *sb;
9098+ struct inode *inode;
9099+
027c5e7a 9100+ /* todo: support rcu-walk? */
b4510431 9101+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9102+ return -ECHILD;
9103+
9104+ valid = 0;
9105+ if (unlikely(!au_di(dentry)))
9106+ goto out;
9107+
e49829fe 9108+ valid = 1;
1facf9fc 9109+ sb = dentry->d_sb;
e49829fe
JR
9110+ /*
9111+ * todo: very ugly
9112+ * i_mutex of parent dir may be held,
9113+ * but we should not return 'invalid' due to busy.
9114+ */
9115+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9116+ if (unlikely(err)) {
9117+ valid = err;
027c5e7a 9118+ AuTraceErr(err);
e49829fe
JR
9119+ goto out;
9120+ }
5527c038
JR
9121+ inode = NULL;
9122+ if (d_really_is_positive(dentry))
9123+ inode = d_inode(dentry);
5afbbe0d 9124+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9125+ err = -EINVAL;
9126+ AuTraceErr(err);
9127+ goto out_dgrade;
9128+ }
027c5e7a
AM
9129+ if (unlikely(au_dbrange_test(dentry))) {
9130+ err = -EINVAL;
9131+ AuTraceErr(err);
9132+ goto out_dgrade;
1facf9fc 9133+ }
027c5e7a
AM
9134+
9135+ sigen = au_sigen(sb);
9136+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9137+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9138+ err = au_reval_dpath(dentry, sigen);
9139+ if (unlikely(err)) {
9140+ AuTraceErr(err);
1facf9fc 9141+ goto out_dgrade;
027c5e7a 9142+ }
1facf9fc 9143+ }
9144+ di_downgrade_lock(dentry, AuLock_IR);
9145+
1facf9fc 9146+ err = -EINVAL;
c1595e42 9147+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9148+ && inode
38d290e6 9149+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9150+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9151+ AuTraceErr(err);
027c5e7a 9152+ goto out_inval;
79b8bda9 9153+ }
027c5e7a 9154+
1facf9fc 9155+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9156+ if (do_udba && inode) {
5afbbe0d 9157+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9158+ struct inode *h_inode;
1facf9fc 9159+
5afbbe0d
AM
9160+ if (btop >= 0) {
9161+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9162+ if (h_inode && au_test_higen(inode, h_inode)) {
9163+ AuTraceErr(err);
027c5e7a 9164+ goto out_inval;
79b8bda9 9165+ }
027c5e7a 9166+ }
1facf9fc 9167+ }
9168+
8b6a4947
AM
9169+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9170+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9171+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9172+ err = -EIO;
523b37e3
AM
9173+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9174+ dentry, err);
027c5e7a 9175+ }
e49829fe 9176+ goto out_inval;
1facf9fc 9177+
4f0767ce 9178+out_dgrade:
1facf9fc 9179+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9180+out_inval:
1facf9fc 9181+ aufs_read_unlock(dentry, AuLock_IR);
9182+ AuTraceErr(err);
9183+ valid = !err;
e49829fe 9184+out:
027c5e7a 9185+ if (!valid) {
523b37e3 9186+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9187+ d_drop(dentry);
9188+ }
1facf9fc 9189+ return valid;
9190+}
9191+
9192+static void aufs_d_release(struct dentry *dentry)
9193+{
027c5e7a 9194+ if (au_di(dentry)) {
4a4d8108
AM
9195+ au_di_fin(dentry);
9196+ au_hn_di_reinit(dentry);
1facf9fc 9197+ }
1facf9fc 9198+}
9199+
4a4d8108 9200+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9201+ .d_revalidate = aufs_d_revalidate,
9202+ .d_weak_revalidate = aufs_d_revalidate,
9203+ .d_release = aufs_d_release
1facf9fc 9204+};
79b8bda9
AM
9205+
9206+/* aufs_dop without d_revalidate */
9207+const struct dentry_operations aufs_dop_noreval = {
9208+ .d_release = aufs_d_release
9209+};
7f207e10 9210diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
eca34b5c 9211--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
016522bc 9212+++ linux/fs/aufs/dentry.h 2020-01-27 10:57:18.168871450 +0100
9f237c51 9213@@ -0,0 +1,268 @@
062440b3 9214+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9215+/*
016522bc 9216+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 9217+ *
9218+ * This program, aufs is free software; you can redistribute it and/or modify
9219+ * it under the terms of the GNU General Public License as published by
9220+ * the Free Software Foundation; either version 2 of the License, or
9221+ * (at your option) any later version.
dece6358
AM
9222+ *
9223+ * This program is distributed in the hope that it will be useful,
9224+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9225+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9226+ * GNU General Public License for more details.
9227+ *
9228+ * You should have received a copy of the GNU General Public License
523b37e3 9229+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9230+ */
9231+
9232+/*
9233+ * lookup and dentry operations
9234+ */
9235+
9236+#ifndef __AUFS_DENTRY_H__
9237+#define __AUFS_DENTRY_H__
9238+
9239+#ifdef __KERNEL__
9240+
dece6358 9241+#include <linux/dcache.h>
8b6a4947 9242+#include "dirren.h"
1facf9fc 9243+#include "rwsem.h"
9244+
1facf9fc 9245+struct au_hdentry {
9246+ struct dentry *hd_dentry;
027c5e7a 9247+ aufs_bindex_t hd_id;
1facf9fc 9248+};
9249+
9250+struct au_dinfo {
9251+ atomic_t di_generation;
9252+
dece6358 9253+ struct au_rwsem di_rwsem;
5afbbe0d 9254+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9255+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9256+ struct au_hdentry *di_hdentry;
9f237c51 9257+ struct rcu_head rcu;
4a4d8108 9258+} ____cacheline_aligned_in_smp;
1facf9fc 9259+
9260+/* ---------------------------------------------------------------------- */
9261+
5afbbe0d
AM
9262+/* flags for au_lkup_dentry() */
9263+#define AuLkup_ALLOW_NEG 1
9264+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9265+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9266+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9267+#define au_fset_lkup(flags, name) \
9268+ do { (flags) |= AuLkup_##name; } while (0)
9269+#define au_fclr_lkup(flags, name) \
9270+ do { (flags) &= ~AuLkup_##name; } while (0)
9271+
8b6a4947
AM
9272+#ifndef CONFIG_AUFS_DIRREN
9273+#undef AuLkup_DIRREN
9274+#define AuLkup_DIRREN 0
9275+#endif
9276+
9277+struct au_do_lookup_args {
9278+ unsigned int flags;
9279+ mode_t type;
9280+ struct qstr whname, *name;
9281+ struct au_dr_lookup dirren;
9282+};
9283+
5afbbe0d
AM
9284+/* ---------------------------------------------------------------------- */
9285+
1facf9fc 9286+/* dentry.c */
79b8bda9 9287+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9288+struct au_branch;
076b876e 9289+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 9290+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9291+ struct dentry *h_parent, struct au_branch *br);
9292+
5afbbe0d
AM
9293+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9294+ unsigned int flags);
86dc4139 9295+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9296+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9297+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9298+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9299+
9300+/* dinfo.c */
4a4d8108 9301+void au_di_init_once(void *_di);
027c5e7a
AM
9302+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9303+void au_di_free(struct au_dinfo *dinfo);
9304+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9305+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9306+int au_di_init(struct dentry *dentry);
9307+void au_di_fin(struct dentry *dentry);
e2f27e51 9308+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9309+
9310+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9311+void di_read_unlock(struct dentry *d, int flags);
9312+void di_downgrade_lock(struct dentry *d, int flags);
9313+void di_write_lock(struct dentry *d, unsigned int lsc);
9314+void di_write_unlock(struct dentry *d);
9315+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9316+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9317+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9318+
9319+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9320+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9321+aufs_bindex_t au_dbtail(struct dentry *dentry);
9322+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9323+
9324+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9325+ struct dentry *h_dentry);
027c5e7a
AM
9326+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9327+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9328+void au_update_digen(struct dentry *dentry);
9329+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9330+void au_update_dbtop(struct dentry *dentry);
9331+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9332+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9333+
9334+/* ---------------------------------------------------------------------- */
9335+
9336+static inline struct au_dinfo *au_di(struct dentry *dentry)
9337+{
9338+ return dentry->d_fsdata;
9339+}
9340+
9341+/* ---------------------------------------------------------------------- */
9342+
9343+/* lock subclass for dinfo */
9344+enum {
9345+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9346+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9347+ AuLsc_DI_CHILD3, /* copyup dirs */
9348+ AuLsc_DI_PARENT,
9349+ AuLsc_DI_PARENT2,
027c5e7a
AM
9350+ AuLsc_DI_PARENT3,
9351+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9352+};
9353+
9354+/*
9355+ * di_read_lock_child, di_write_lock_child,
9356+ * di_read_lock_child2, di_write_lock_child2,
9357+ * di_read_lock_child3, di_write_lock_child3,
9358+ * di_read_lock_parent, di_write_lock_parent,
9359+ * di_read_lock_parent2, di_write_lock_parent2,
9360+ * di_read_lock_parent3, di_write_lock_parent3,
9361+ */
9362+#define AuReadLockFunc(name, lsc) \
9363+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9364+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9365+
9366+#define AuWriteLockFunc(name, lsc) \
9367+static inline void di_write_lock_##name(struct dentry *d) \
9368+{ di_write_lock(d, AuLsc_DI_##lsc); }
9369+
9370+#define AuRWLockFuncs(name, lsc) \
9371+ AuReadLockFunc(name, lsc) \
9372+ AuWriteLockFunc(name, lsc)
9373+
9374+AuRWLockFuncs(child, CHILD);
9375+AuRWLockFuncs(child2, CHILD2);
9376+AuRWLockFuncs(child3, CHILD3);
9377+AuRWLockFuncs(parent, PARENT);
9378+AuRWLockFuncs(parent2, PARENT2);
9379+AuRWLockFuncs(parent3, PARENT3);
9380+
9381+#undef AuReadLockFunc
9382+#undef AuWriteLockFunc
9383+#undef AuRWLockFuncs
9384+
9385+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9386+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9387+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9388+
9389+/* ---------------------------------------------------------------------- */
9390+
9391+/* todo: memory barrier? */
9392+static inline unsigned int au_digen(struct dentry *d)
9393+{
9394+ return atomic_read(&au_di(d)->di_generation);
9395+}
9396+
9397+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9398+{
9399+ hdentry->hd_dentry = NULL;
9400+}
9401+
5afbbe0d
AM
9402+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9403+ aufs_bindex_t bindex)
9404+{
9405+ return di->di_hdentry + bindex;
9406+}
9407+
1facf9fc 9408+static inline void au_hdput(struct au_hdentry *hd)
9409+{
4a4d8108
AM
9410+ if (hd)
9411+ dput(hd->hd_dentry);
1facf9fc 9412+}
9413+
5afbbe0d 9414+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9415+{
1308ab2a 9416+ DiMustAnyLock(dentry);
5afbbe0d 9417+ return au_di(dentry)->di_btop;
1facf9fc 9418+}
9419+
5afbbe0d 9420+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9421+{
1308ab2a 9422+ DiMustAnyLock(dentry);
5afbbe0d 9423+ return au_di(dentry)->di_bbot;
1facf9fc 9424+}
9425+
9426+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9427+{
1308ab2a 9428+ DiMustAnyLock(dentry);
1facf9fc 9429+ return au_di(dentry)->di_bwh;
9430+}
9431+
9432+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9433+{
1308ab2a 9434+ DiMustAnyLock(dentry);
1facf9fc 9435+ return au_di(dentry)->di_bdiropq;
9436+}
9437+
9438+/* todo: hard/soft set? */
5afbbe0d 9439+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9440+{
1308ab2a 9441+ DiMustWriteLock(dentry);
5afbbe0d 9442+ au_di(dentry)->di_btop = bindex;
1facf9fc 9443+}
9444+
5afbbe0d 9445+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9446+{
1308ab2a 9447+ DiMustWriteLock(dentry);
5afbbe0d 9448+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9449+}
9450+
9451+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9452+{
1308ab2a 9453+ DiMustWriteLock(dentry);
5afbbe0d 9454+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9455+ au_di(dentry)->di_bwh = bindex;
9456+}
9457+
9458+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9459+{
1308ab2a 9460+ DiMustWriteLock(dentry);
1facf9fc 9461+ au_di(dentry)->di_bdiropq = bindex;
9462+}
9463+
9464+/* ---------------------------------------------------------------------- */
9465+
4a4d8108 9466+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9467+static inline void au_digen_dec(struct dentry *d)
9468+{
e49829fe 9469+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9470+}
9471+
4a4d8108 9472+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9473+{
9474+ dentry->d_fsdata = NULL;
9475+}
9476+#else
4a4d8108
AM
9477+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9478+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9479+
9480+#endif /* __KERNEL__ */
9481+#endif /* __AUFS_DENTRY_H__ */
7f207e10 9482diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
eca34b5c 9483--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
016522bc 9484+++ linux/fs/aufs/dinfo.c 2020-01-27 10:57:18.168871450 +0100
062440b3 9485@@ -0,0 +1,554 @@
cd7a4cd9 9486+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9487+/*
016522bc 9488+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 9489+ *
9490+ * This program, aufs is free software; you can redistribute it and/or modify
9491+ * it under the terms of the GNU General Public License as published by
9492+ * the Free Software Foundation; either version 2 of the License, or
9493+ * (at your option) any later version.
dece6358
AM
9494+ *
9495+ * This program is distributed in the hope that it will be useful,
9496+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9497+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9498+ * GNU General Public License for more details.
9499+ *
9500+ * You should have received a copy of the GNU General Public License
523b37e3 9501+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9502+ */
9503+
9504+/*
9505+ * dentry private data
9506+ */
9507+
9508+#include "aufs.h"
9509+
e49829fe 9510+void au_di_init_once(void *_dinfo)
4a4d8108 9511+{
e49829fe 9512+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9513+
e49829fe 9514+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9515+}
9516+
027c5e7a 9517+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9518+{
9519+ struct au_dinfo *dinfo;
027c5e7a 9520+ int nbr, i;
1facf9fc 9521+
9522+ dinfo = au_cache_alloc_dinfo();
9523+ if (unlikely(!dinfo))
9524+ goto out;
9525+
5afbbe0d 9526+ nbr = au_sbbot(sb) + 1;
1facf9fc 9527+ if (nbr <= 0)
9528+ nbr = 1;
9529+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9530+ if (dinfo->di_hdentry) {
9531+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9532+ dinfo->di_btop = -1;
9533+ dinfo->di_bbot = -1;
027c5e7a
AM
9534+ dinfo->di_bwh = -1;
9535+ dinfo->di_bdiropq = -1;
38d290e6 9536+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9537+ for (i = 0; i < nbr; i++)
9538+ dinfo->di_hdentry[i].hd_id = -1;
9539+ goto out;
9540+ }
1facf9fc 9541+
1c60b727 9542+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9543+ dinfo = NULL;
9544+
4f0767ce 9545+out:
027c5e7a 9546+ return dinfo;
1facf9fc 9547+}
9548+
027c5e7a 9549+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9550+{
4a4d8108 9551+ struct au_hdentry *p;
5afbbe0d 9552+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9553+
9554+ /* dentry may not be revalidated */
5afbbe0d 9555+ bindex = dinfo->di_btop;
4a4d8108 9556+ if (bindex >= 0) {
5afbbe0d
AM
9557+ bbot = dinfo->di_bbot;
9558+ p = au_hdentry(dinfo, bindex);
9559+ while (bindex++ <= bbot)
4a4d8108
AM
9560+ au_hdput(p++);
9561+ }
9f237c51 9562+ au_kfree_try_rcu(dinfo->di_hdentry);
1c60b727 9563+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9564+}
9565+
9566+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9567+{
9568+ struct au_hdentry *p;
9569+ aufs_bindex_t bi;
9570+
9571+ AuRwMustWriteLock(&a->di_rwsem);
9572+ AuRwMustWriteLock(&b->di_rwsem);
9573+
9574+#define DiSwap(v, name) \
9575+ do { \
9576+ v = a->di_##name; \
9577+ a->di_##name = b->di_##name; \
9578+ b->di_##name = v; \
9579+ } while (0)
9580+
9581+ DiSwap(p, hdentry);
5afbbe0d
AM
9582+ DiSwap(bi, btop);
9583+ DiSwap(bi, bbot);
027c5e7a
AM
9584+ DiSwap(bi, bwh);
9585+ DiSwap(bi, bdiropq);
9586+ /* smp_mb(); */
9587+
9588+#undef DiSwap
9589+}
9590+
9591+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9592+{
9593+ AuRwMustWriteLock(&dst->di_rwsem);
9594+ AuRwMustWriteLock(&src->di_rwsem);
9595+
5afbbe0d
AM
9596+ dst->di_btop = src->di_btop;
9597+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9598+ dst->di_bwh = src->di_bwh;
9599+ dst->di_bdiropq = src->di_bdiropq;
9600+ /* smp_mb(); */
9601+}
9602+
9603+int au_di_init(struct dentry *dentry)
9604+{
9605+ int err;
9606+ struct super_block *sb;
9607+ struct au_dinfo *dinfo;
9608+
9609+ err = 0;
9610+ sb = dentry->d_sb;
9611+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9612+ if (dinfo) {
9613+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9614+ /* smp_mb(); */ /* atomic_set */
9615+ dentry->d_fsdata = dinfo;
9616+ } else
9617+ err = -ENOMEM;
9618+
9619+ return err;
9620+}
9621+
9622+void au_di_fin(struct dentry *dentry)
9623+{
9624+ struct au_dinfo *dinfo;
9625+
9626+ dinfo = au_di(dentry);
9627+ AuRwDestroy(&dinfo->di_rwsem);
9628+ au_di_free(dinfo);
4a4d8108
AM
9629+}
9630+
e2f27e51 9631+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9632+{
9633+ int err, sz;
9634+ struct au_hdentry *hdp;
9635+
1308ab2a 9636+ AuRwMustWriteLock(&dinfo->di_rwsem);
9637+
1facf9fc 9638+ err = -ENOMEM;
5afbbe0d 9639+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9640+ if (!sz)
9641+ sz = sizeof(*hdp);
e2f27e51
AM
9642+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9643+ may_shrink);
1facf9fc 9644+ if (hdp) {
9645+ dinfo->di_hdentry = hdp;
9646+ err = 0;
9647+ }
9648+
9649+ return err;
9650+}
9651+
9652+/* ---------------------------------------------------------------------- */
9653+
9654+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9655+{
9656+ switch (lsc) {
9657+ case AuLsc_DI_CHILD:
9658+ ii_write_lock_child(inode);
9659+ break;
9660+ case AuLsc_DI_CHILD2:
9661+ ii_write_lock_child2(inode);
9662+ break;
9663+ case AuLsc_DI_CHILD3:
9664+ ii_write_lock_child3(inode);
9665+ break;
9666+ case AuLsc_DI_PARENT:
9667+ ii_write_lock_parent(inode);
9668+ break;
9669+ case AuLsc_DI_PARENT2:
9670+ ii_write_lock_parent2(inode);
9671+ break;
9672+ case AuLsc_DI_PARENT3:
9673+ ii_write_lock_parent3(inode);
9674+ break;
9675+ default:
9676+ BUG();
9677+ }
9678+}
9679+
9680+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9681+{
9682+ switch (lsc) {
9683+ case AuLsc_DI_CHILD:
9684+ ii_read_lock_child(inode);
9685+ break;
9686+ case AuLsc_DI_CHILD2:
9687+ ii_read_lock_child2(inode);
9688+ break;
9689+ case AuLsc_DI_CHILD3:
9690+ ii_read_lock_child3(inode);
9691+ break;
9692+ case AuLsc_DI_PARENT:
9693+ ii_read_lock_parent(inode);
9694+ break;
9695+ case AuLsc_DI_PARENT2:
9696+ ii_read_lock_parent2(inode);
9697+ break;
9698+ case AuLsc_DI_PARENT3:
9699+ ii_read_lock_parent3(inode);
9700+ break;
9701+ default:
9702+ BUG();
9703+ }
9704+}
9705+
9706+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9707+{
5527c038
JR
9708+ struct inode *inode;
9709+
dece6358 9710+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9711+ if (d_really_is_positive(d)) {
9712+ inode = d_inode(d);
1facf9fc 9713+ if (au_ftest_lock(flags, IW))
5527c038 9714+ do_ii_write_lock(inode, lsc);
1facf9fc 9715+ else if (au_ftest_lock(flags, IR))
5527c038 9716+ do_ii_read_lock(inode, lsc);
1facf9fc 9717+ }
9718+}
9719+
9720+void di_read_unlock(struct dentry *d, int flags)
9721+{
5527c038
JR
9722+ struct inode *inode;
9723+
9724+ if (d_really_is_positive(d)) {
9725+ inode = d_inode(d);
027c5e7a
AM
9726+ if (au_ftest_lock(flags, IW)) {
9727+ au_dbg_verify_dinode(d);
5527c038 9728+ ii_write_unlock(inode);
027c5e7a
AM
9729+ } else if (au_ftest_lock(flags, IR)) {
9730+ au_dbg_verify_dinode(d);
5527c038 9731+ ii_read_unlock(inode);
027c5e7a 9732+ }
1facf9fc 9733+ }
dece6358 9734+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9735+}
9736+
9737+void di_downgrade_lock(struct dentry *d, int flags)
9738+{
5527c038
JR
9739+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9740+ ii_downgrade_lock(d_inode(d));
dece6358 9741+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9742+}
9743+
9744+void di_write_lock(struct dentry *d, unsigned int lsc)
9745+{
dece6358 9746+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9747+ if (d_really_is_positive(d))
9748+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9749+}
9750+
9751+void di_write_unlock(struct dentry *d)
9752+{
027c5e7a 9753+ au_dbg_verify_dinode(d);
5527c038
JR
9754+ if (d_really_is_positive(d))
9755+ ii_write_unlock(d_inode(d));
dece6358 9756+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9757+}
9758+
9759+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9760+{
9761+ AuDebugOn(d1 == d2
5527c038 9762+ || d_inode(d1) == d_inode(d2)
1facf9fc 9763+ || d1->d_sb != d2->d_sb);
9764+
521ced18
JR
9765+ if ((isdir && au_test_subdir(d1, d2))
9766+ || d1 < d2) {
1facf9fc 9767+ di_write_lock_child(d1);
9768+ di_write_lock_child2(d2);
9769+ } else {
1facf9fc 9770+ di_write_lock_child(d2);
9771+ di_write_lock_child2(d1);
9772+ }
9773+}
9774+
9775+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9776+{
9777+ AuDebugOn(d1 == d2
5527c038 9778+ || d_inode(d1) == d_inode(d2)
1facf9fc 9779+ || d1->d_sb != d2->d_sb);
9780+
521ced18
JR
9781+ if ((isdir && au_test_subdir(d1, d2))
9782+ || d1 < d2) {
1facf9fc 9783+ di_write_lock_parent(d1);
9784+ di_write_lock_parent2(d2);
9785+ } else {
1facf9fc 9786+ di_write_lock_parent(d2);
9787+ di_write_lock_parent2(d1);
9788+ }
9789+}
9790+
9791+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9792+{
9793+ di_write_unlock(d1);
5527c038 9794+ if (d_inode(d1) == d_inode(d2))
dece6358 9795+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9796+ else
9797+ di_write_unlock(d2);
9798+}
9799+
9800+/* ---------------------------------------------------------------------- */
9801+
9802+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9803+{
9804+ struct dentry *d;
9805+
1308ab2a 9806+ DiMustAnyLock(dentry);
9807+
5afbbe0d 9808+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9809+ return NULL;
9810+ AuDebugOn(bindex < 0);
5afbbe0d 9811+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9812+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9813+ return d;
9814+}
9815+
2cbb1c4b
JR
9816+/*
9817+ * extended version of au_h_dptr().
38d290e6
JR
9818+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9819+ * error.
2cbb1c4b
JR
9820+ */
9821+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9822+{
9823+ struct dentry *h_dentry;
9824+ struct inode *inode, *h_inode;
9825+
5527c038 9826+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9827+
9828+ h_dentry = NULL;
5afbbe0d
AM
9829+ if (au_dbtop(dentry) <= bindex
9830+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9831+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9832+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9833+ dget(h_dentry);
9834+ goto out; /* success */
9835+ }
9836+
5527c038 9837+ inode = d_inode(dentry);
5afbbe0d
AM
9838+ AuDebugOn(bindex < au_ibtop(inode));
9839+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9840+ h_inode = au_h_iptr(inode, bindex);
9841+ h_dentry = d_find_alias(h_inode);
9842+ if (h_dentry) {
9843+ if (!IS_ERR(h_dentry)) {
38d290e6 9844+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9845+ goto out; /* success */
9846+ dput(h_dentry);
9847+ } else
9848+ goto out;
9849+ }
9850+
9851+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9852+ h_dentry = au_plink_lkup(inode, bindex);
9853+ AuDebugOn(!h_dentry);
9854+ if (!IS_ERR(h_dentry)) {
9855+ if (!au_d_hashed_positive(h_dentry))
9856+ goto out; /* success */
9857+ dput(h_dentry);
9858+ h_dentry = NULL;
9859+ }
9860+ }
9861+
9862+out:
9863+ AuDbgDentry(h_dentry);
9864+ return h_dentry;
9865+}
9866+
1facf9fc 9867+aufs_bindex_t au_dbtail(struct dentry *dentry)
9868+{
5afbbe0d 9869+ aufs_bindex_t bbot, bwh;
1facf9fc 9870+
5afbbe0d
AM
9871+ bbot = au_dbbot(dentry);
9872+ if (0 <= bbot) {
1facf9fc 9873+ bwh = au_dbwh(dentry);
9874+ if (!bwh)
9875+ return bwh;
5afbbe0d 9876+ if (0 < bwh && bwh < bbot)
1facf9fc 9877+ return bwh - 1;
9878+ }
5afbbe0d 9879+ return bbot;
1facf9fc 9880+}
9881+
9882+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9883+{
5afbbe0d 9884+ aufs_bindex_t bbot, bopq;
1facf9fc 9885+
5afbbe0d
AM
9886+ bbot = au_dbtail(dentry);
9887+ if (0 <= bbot) {
1facf9fc 9888+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9889+ if (0 <= bopq && bopq < bbot)
9890+ bbot = bopq;
1facf9fc 9891+ }
5afbbe0d 9892+ return bbot;
1facf9fc 9893+}
9894+
9895+/* ---------------------------------------------------------------------- */
9896+
9897+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9898+ struct dentry *h_dentry)
9899+{
5afbbe0d
AM
9900+ struct au_dinfo *dinfo;
9901+ struct au_hdentry *hd;
027c5e7a 9902+ struct au_branch *br;
1facf9fc 9903+
1308ab2a 9904+ DiMustWriteLock(dentry);
9905+
5afbbe0d
AM
9906+ dinfo = au_di(dentry);
9907+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9908+ au_hdput(hd);
1facf9fc 9909+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9910+ if (h_dentry) {
9911+ br = au_sbr(dentry->d_sb, bindex);
9912+ hd->hd_id = br->br_id;
9913+ }
9914+}
9915+
9916+int au_dbrange_test(struct dentry *dentry)
9917+{
9918+ int err;
5afbbe0d 9919+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9920+
9921+ err = 0;
5afbbe0d
AM
9922+ btop = au_dbtop(dentry);
9923+ bbot = au_dbbot(dentry);
9924+ if (btop >= 0)
9925+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9926+ else {
9927+ err = -EIO;
5afbbe0d 9928+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9929+ }
9930+
9931+ return err;
9932+}
9933+
9934+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9935+{
9936+ int err;
9937+
9938+ err = 0;
9939+ if (unlikely(au_digen(dentry) != sigen
5527c038 9940+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
9941+ err = -EIO;
9942+
9943+ return err;
1facf9fc 9944+}
9945+
9946+void au_update_digen(struct dentry *dentry)
9947+{
9948+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9949+ /* smp_mb(); */ /* atomic_set */
9950+}
9951+
9952+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9953+{
9954+ struct au_dinfo *dinfo;
9955+ struct dentry *h_d;
4a4d8108 9956+ struct au_hdentry *hdp;
5afbbe0d 9957+ aufs_bindex_t bindex, bbot;
1facf9fc 9958+
1308ab2a 9959+ DiMustWriteLock(dentry);
9960+
1facf9fc 9961+ dinfo = au_di(dentry);
5afbbe0d 9962+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 9963+ return;
9964+
9965+ if (do_put_zero) {
5afbbe0d
AM
9966+ bbot = dinfo->di_bbot;
9967+ bindex = dinfo->di_btop;
9968+ hdp = au_hdentry(dinfo, bindex);
9969+ for (; bindex <= bbot; bindex++, hdp++) {
9970+ h_d = hdp->hd_dentry;
5527c038 9971+ if (h_d && d_is_negative(h_d))
1facf9fc 9972+ au_set_h_dptr(dentry, bindex, NULL);
9973+ }
9974+ }
9975+
5afbbe0d
AM
9976+ dinfo->di_btop = 0;
9977+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9978+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9979+ if (hdp->hd_dentry)
1facf9fc 9980+ break;
5afbbe0d
AM
9981+ if (dinfo->di_btop > dinfo->di_bbot) {
9982+ dinfo->di_btop = -1;
9983+ dinfo->di_bbot = -1;
1facf9fc 9984+ return;
9985+ }
9986+
5afbbe0d
AM
9987+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9988+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9989+ if (hdp->hd_dentry)
1facf9fc 9990+ break;
5afbbe0d 9991+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 9992+}
9993+
5afbbe0d 9994+void au_update_dbtop(struct dentry *dentry)
1facf9fc 9995+{
5afbbe0d 9996+ aufs_bindex_t bindex, bbot;
1facf9fc 9997+ struct dentry *h_dentry;
9998+
5afbbe0d
AM
9999+ bbot = au_dbbot(dentry);
10000+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 10001+ h_dentry = au_h_dptr(dentry, bindex);
10002+ if (!h_dentry)
10003+ continue;
5527c038 10004+ if (d_is_positive(h_dentry)) {
5afbbe0d 10005+ au_set_dbtop(dentry, bindex);
1facf9fc 10006+ return;
10007+ }
10008+ au_set_h_dptr(dentry, bindex, NULL);
10009+ }
10010+}
10011+
5afbbe0d 10012+void au_update_dbbot(struct dentry *dentry)
1facf9fc 10013+{
5afbbe0d 10014+ aufs_bindex_t bindex, btop;
1facf9fc 10015+ struct dentry *h_dentry;
10016+
5afbbe0d
AM
10017+ btop = au_dbtop(dentry);
10018+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 10019+ h_dentry = au_h_dptr(dentry, bindex);
10020+ if (!h_dentry)
10021+ continue;
5527c038 10022+ if (d_is_positive(h_dentry)) {
5afbbe0d 10023+ au_set_dbbot(dentry, bindex);
1facf9fc 10024+ return;
10025+ }
10026+ au_set_h_dptr(dentry, bindex, NULL);
10027+ }
10028+}
10029+
10030+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10031+{
5afbbe0d 10032+ aufs_bindex_t bindex, bbot;
1facf9fc 10033+
5afbbe0d
AM
10034+ bbot = au_dbbot(dentry);
10035+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 10036+ if (au_h_dptr(dentry, bindex) == h_dentry)
10037+ return bindex;
10038+ return -1;
10039+}
7f207e10 10040diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
eca34b5c 10041--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
016522bc 10042+++ linux/fs/aufs/dir.c 2020-01-27 10:57:18.168871450 +0100
eca801bf 10043@@ -0,0 +1,763 @@
cd7a4cd9 10044+// SPDX-License-Identifier: GPL-2.0
1facf9fc 10045+/*
016522bc 10046+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 10047+ *
10048+ * This program, aufs is free software; you can redistribute it and/or modify
10049+ * it under the terms of the GNU General Public License as published by
10050+ * the Free Software Foundation; either version 2 of the License, or
10051+ * (at your option) any later version.
dece6358
AM
10052+ *
10053+ * This program is distributed in the hope that it will be useful,
10054+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10055+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10056+ * GNU General Public License for more details.
10057+ *
10058+ * You should have received a copy of the GNU General Public License
523b37e3 10059+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10060+ */
10061+
10062+/*
10063+ * directory operations
10064+ */
10065+
10066+#include <linux/fs_stack.h>
eca801bf 10067+#include <linux/iversion.h>
1facf9fc 10068+#include "aufs.h"
10069+
10070+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10071+{
9dbd164d
AM
10072+ unsigned int nlink;
10073+
1facf9fc 10074+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10075+
9dbd164d
AM
10076+ nlink = dir->i_nlink;
10077+ nlink += h_dir->i_nlink - 2;
1facf9fc 10078+ if (h_dir->i_nlink < 2)
9dbd164d 10079+ nlink += 2;
f6b6e03d 10080+ smp_mb(); /* for i_nlink */
7eafdf33 10081+ /* 0 can happen in revaliding */
92d182d2 10082+ set_nlink(dir, nlink);
1facf9fc 10083+}
10084+
10085+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10086+{
9dbd164d
AM
10087+ unsigned int nlink;
10088+
1facf9fc 10089+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10090+
9dbd164d
AM
10091+ nlink = dir->i_nlink;
10092+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10093+ if (h_dir->i_nlink < 2)
9dbd164d 10094+ nlink -= 2;
f6b6e03d 10095+ smp_mb(); /* for i_nlink */
92d182d2 10096+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10097+ set_nlink(dir, nlink);
1facf9fc 10098+}
10099+
1308ab2a 10100+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10101+{
10102+ loff_t sz;
5afbbe0d 10103+ aufs_bindex_t bindex, bbot;
1308ab2a 10104+ struct file *h_file;
10105+ struct dentry *h_dentry;
10106+
10107+ sz = 0;
10108+ if (file) {
2000de60 10109+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10110+
5afbbe0d
AM
10111+ bbot = au_fbbot_dir(file);
10112+ for (bindex = au_fbtop(file);
10113+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10114+ bindex++) {
4a4d8108 10115+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10116+ if (h_file && file_inode(h_file))
10117+ sz += vfsub_f_size_read(h_file);
1308ab2a 10118+ }
10119+ } else {
10120+ AuDebugOn(!dentry);
2000de60 10121+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10122+
5afbbe0d
AM
10123+ bbot = au_dbtaildir(dentry);
10124+ for (bindex = au_dbtop(dentry);
10125+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10126+ bindex++) {
10127+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10128+ if (h_dentry && d_is_positive(h_dentry))
10129+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10130+ }
10131+ }
10132+ if (sz < KMALLOC_MAX_SIZE)
10133+ sz = roundup_pow_of_two(sz);
10134+ if (sz > KMALLOC_MAX_SIZE)
10135+ sz = KMALLOC_MAX_SIZE;
10136+ else if (sz < NAME_MAX) {
10137+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10138+ sz = AUFS_RDBLK_DEF;
10139+ }
10140+ return sz;
10141+}
10142+
b912730e
AM
10143+struct au_dir_ts_arg {
10144+ struct dentry *dentry;
10145+ aufs_bindex_t brid;
10146+};
10147+
10148+static void au_do_dir_ts(void *arg)
10149+{
10150+ struct au_dir_ts_arg *a = arg;
10151+ struct au_dtime dt;
10152+ struct path h_path;
10153+ struct inode *dir, *h_dir;
10154+ struct super_block *sb;
10155+ struct au_branch *br;
10156+ struct au_hinode *hdir;
10157+ int err;
5afbbe0d 10158+ aufs_bindex_t btop, bindex;
b912730e
AM
10159+
10160+ sb = a->dentry->d_sb;
5527c038 10161+ if (d_really_is_negative(a->dentry))
b912730e 10162+ goto out;
5527c038 10163+ /* no dir->i_mutex lock */
b95c5147
AM
10164+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10165+
5527c038 10166+ dir = d_inode(a->dentry);
5afbbe0d 10167+ btop = au_ibtop(dir);
b912730e 10168+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10169+ if (bindex < btop)
b912730e
AM
10170+ goto out_unlock;
10171+
10172+ br = au_sbr(sb, bindex);
10173+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10174+ if (!h_path.dentry)
10175+ goto out_unlock;
10176+ h_path.mnt = au_br_mnt(br);
10177+ au_dtime_store(&dt, a->dentry, &h_path);
10178+
5afbbe0d 10179+ br = au_sbr(sb, btop);
b912730e
AM
10180+ if (!au_br_writable(br->br_perm))
10181+ goto out_unlock;
5afbbe0d 10182+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10183+ h_path.mnt = au_br_mnt(br);
10184+ err = vfsub_mnt_want_write(h_path.mnt);
10185+ if (err)
10186+ goto out_unlock;
5afbbe0d
AM
10187+ hdir = au_hi(dir, btop);
10188+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10189+ h_dir = au_h_iptr(dir, btop);
b912730e 10190+ if (h_dir->i_nlink
cd7a4cd9 10191+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
b912730e
AM
10192+ dt.dt_h_path = h_path;
10193+ au_dtime_revert(&dt);
10194+ }
5afbbe0d 10195+ au_hn_inode_unlock(hdir);
b912730e
AM
10196+ vfsub_mnt_drop_write(h_path.mnt);
10197+ au_cpup_attr_timesizes(dir);
10198+
10199+out_unlock:
10200+ aufs_read_unlock(a->dentry, AuLock_DW);
10201+out:
10202+ dput(a->dentry);
10203+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 10204+ au_kfree_try_rcu(arg);
b912730e
AM
10205+}
10206+
10207+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10208+{
10209+ int perm, wkq_err;
5afbbe0d 10210+ aufs_bindex_t btop;
b912730e
AM
10211+ struct au_dir_ts_arg *arg;
10212+ struct dentry *dentry;
10213+ struct super_block *sb;
10214+
10215+ IMustLock(dir);
10216+
10217+ dentry = d_find_any_alias(dir);
10218+ AuDebugOn(!dentry);
10219+ sb = dentry->d_sb;
5afbbe0d
AM
10220+ btop = au_ibtop(dir);
10221+ if (btop == bindex) {
b912730e
AM
10222+ au_cpup_attr_timesizes(dir);
10223+ goto out;
10224+ }
10225+
5afbbe0d 10226+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10227+ if (!au_br_writable(perm))
10228+ goto out;
10229+
10230+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10231+ if (!arg)
10232+ goto out;
10233+
10234+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10235+ arg->brid = au_sbr_id(sb, bindex);
10236+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10237+ if (unlikely(wkq_err)) {
10238+ pr_err("wkq %d\n", wkq_err);
10239+ dput(dentry);
9f237c51 10240+ au_kfree_try_rcu(arg);
b912730e
AM
10241+ }
10242+
10243+out:
10244+ dput(dentry);
10245+}
10246+
1facf9fc 10247+/* ---------------------------------------------------------------------- */
10248+
10249+static int reopen_dir(struct file *file)
10250+{
10251+ int err;
10252+ unsigned int flags;
5afbbe0d 10253+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10254+ struct dentry *dentry, *h_dentry;
10255+ struct file *h_file;
10256+
10257+ /* open all lower dirs */
2000de60 10258+ dentry = file->f_path.dentry;
5afbbe0d
AM
10259+ btop = au_dbtop(dentry);
10260+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10261+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10262+ au_set_fbtop(file, btop);
1facf9fc 10263+
10264+ btail = au_dbtaildir(dentry);
5afbbe0d 10265+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10266+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10267+ au_set_fbbot_dir(file, btail);
1facf9fc 10268+
4a4d8108 10269+ flags = vfsub_file_flags(file);
5afbbe0d 10270+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10271+ h_dentry = au_h_dptr(dentry, bindex);
10272+ if (!h_dentry)
10273+ continue;
4a4d8108 10274+ h_file = au_hf_dir(file, bindex);
1facf9fc 10275+ if (h_file)
10276+ continue;
10277+
392086de 10278+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10279+ err = PTR_ERR(h_file);
10280+ if (IS_ERR(h_file))
10281+ goto out; /* close all? */
10282+ au_set_h_fptr(file, bindex, h_file);
10283+ }
10284+ au_update_figen(file);
10285+ /* todo: necessary? */
10286+ /* file->f_ra = h_file->f_ra; */
10287+ err = 0;
10288+
4f0767ce 10289+out:
1facf9fc 10290+ return err;
10291+}
10292+
b912730e 10293+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10294+{
10295+ int err;
10296+ aufs_bindex_t bindex, btail;
10297+ struct dentry *dentry, *h_dentry;
8cdd5066 10298+ struct vfsmount *mnt;
1facf9fc 10299+
1308ab2a 10300+ FiMustWriteLock(file);
b912730e 10301+ AuDebugOn(h_file);
1308ab2a 10302+
523b37e3 10303+ err = 0;
8cdd5066 10304+ mnt = file->f_path.mnt;
2000de60 10305+ dentry = file->f_path.dentry;
be118d29 10306+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10307+ bindex = au_dbtop(dentry);
10308+ au_set_fbtop(file, bindex);
1facf9fc 10309+ btail = au_dbtaildir(dentry);
5afbbe0d 10310+ au_set_fbbot_dir(file, btail);
1facf9fc 10311+ for (; !err && bindex <= btail; bindex++) {
10312+ h_dentry = au_h_dptr(dentry, bindex);
10313+ if (!h_dentry)
10314+ continue;
10315+
8cdd5066
JR
10316+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10317+ if (unlikely(err))
10318+ break;
392086de 10319+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10320+ if (IS_ERR(h_file)) {
10321+ err = PTR_ERR(h_file);
10322+ break;
10323+ }
10324+ au_set_h_fptr(file, bindex, h_file);
10325+ }
10326+ au_update_figen(file);
10327+ /* todo: necessary? */
10328+ /* file->f_ra = h_file->f_ra; */
10329+ if (!err)
10330+ return 0; /* success */
10331+
10332+ /* close all */
5afbbe0d 10333+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10334+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10335+ au_set_fbtop(file, -1);
10336+ au_set_fbbot_dir(file, -1);
4a4d8108 10337+
1facf9fc 10338+ return err;
10339+}
10340+
10341+static int aufs_open_dir(struct inode *inode __maybe_unused,
10342+ struct file *file)
10343+{
4a4d8108
AM
10344+ int err;
10345+ struct super_block *sb;
10346+ struct au_fidir *fidir;
10347+
10348+ err = -ENOMEM;
2000de60 10349+ sb = file->f_path.dentry->d_sb;
4a4d8108 10350+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10351+ fidir = au_fidir_alloc(sb);
4a4d8108 10352+ if (fidir) {
b912730e
AM
10353+ struct au_do_open_args args = {
10354+ .open = do_open_dir,
10355+ .fidir = fidir
10356+ };
10357+ err = au_do_open(file, &args);
4a4d8108 10358+ if (unlikely(err))
9f237c51 10359+ au_kfree_rcu(fidir);
4a4d8108
AM
10360+ }
10361+ si_read_unlock(sb);
10362+ return err;
1facf9fc 10363+}
10364+
10365+static int aufs_release_dir(struct inode *inode __maybe_unused,
10366+ struct file *file)
10367+{
10368+ struct au_vdir *vdir_cache;
4a4d8108
AM
10369+ struct au_finfo *finfo;
10370+ struct au_fidir *fidir;
f0c0a007 10371+ struct au_hfile *hf;
5afbbe0d 10372+ aufs_bindex_t bindex, bbot;
1facf9fc 10373+
4a4d8108
AM
10374+ finfo = au_fi(file);
10375+ fidir = finfo->fi_hdir;
10376+ if (fidir) {
8b6a4947
AM
10377+ au_hbl_del(&finfo->fi_hlist,
10378+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10379+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10380+ if (vdir_cache)
1c60b727 10381+ au_vdir_free(vdir_cache);
4a4d8108
AM
10382+
10383+ bindex = finfo->fi_btop;
10384+ if (bindex >= 0) {
f0c0a007 10385+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10386+ /*
10387+ * calls fput() instead of filp_close(),
10388+ * since no dnotify or lock for the lower file.
10389+ */
5afbbe0d 10390+ bbot = fidir->fd_bbot;
f0c0a007
AM
10391+ for (; bindex <= bbot; bindex++, hf++)
10392+ if (hf->hf_file)
1c60b727 10393+ au_hfput(hf, /*execed*/0);
4a4d8108 10394+ }
9f237c51 10395+ au_kfree_rcu(fidir);
4a4d8108 10396+ finfo->fi_hdir = NULL;
1facf9fc 10397+ }
1c60b727 10398+ au_finfo_fin(file);
1facf9fc 10399+ return 0;
10400+}
10401+
10402+/* ---------------------------------------------------------------------- */
10403+
4a4d8108
AM
10404+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10405+{
10406+ int err;
5afbbe0d 10407+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10408+ struct file *h_file;
10409+
10410+ err = 0;
5afbbe0d
AM
10411+ bbot = au_fbbot_dir(file);
10412+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10413+ h_file = au_hf_dir(file, bindex);
10414+ if (h_file)
10415+ err = vfsub_flush(h_file, id);
10416+ }
10417+ return err;
10418+}
10419+
10420+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10421+{
10422+ return au_do_flush(file, id, au_do_flush_dir);
10423+}
10424+
10425+/* ---------------------------------------------------------------------- */
10426+
1facf9fc 10427+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10428+{
10429+ int err;
5afbbe0d 10430+ aufs_bindex_t bbot, bindex;
1facf9fc 10431+ struct inode *inode;
10432+ struct super_block *sb;
10433+
10434+ err = 0;
10435+ sb = dentry->d_sb;
5527c038 10436+ inode = d_inode(dentry);
1facf9fc 10437+ IMustLock(inode);
5afbbe0d
AM
10438+ bbot = au_dbbot(dentry);
10439+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10440+ struct path h_path;
1facf9fc 10441+
10442+ if (au_test_ro(sb, bindex, inode))
10443+ continue;
10444+ h_path.dentry = au_h_dptr(dentry, bindex);
10445+ if (!h_path.dentry)
10446+ continue;
1facf9fc 10447+
1facf9fc 10448+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10449+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10450+ }
10451+
10452+ return err;
10453+}
10454+
10455+static int au_do_fsync_dir(struct file *file, int datasync)
10456+{
10457+ int err;
5afbbe0d 10458+ aufs_bindex_t bbot, bindex;
1facf9fc 10459+ struct file *h_file;
10460+ struct super_block *sb;
10461+ struct inode *inode;
1facf9fc 10462+
521ced18 10463+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10464+ if (unlikely(err))
10465+ goto out;
10466+
c06a8ce3 10467+ inode = file_inode(file);
b912730e 10468+ sb = inode->i_sb;
5afbbe0d
AM
10469+ bbot = au_fbbot_dir(file);
10470+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10471+ h_file = au_hf_dir(file, bindex);
1facf9fc 10472+ if (!h_file || au_test_ro(sb, bindex, inode))
10473+ continue;
10474+
53392da6 10475+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10476+ }
10477+
4f0767ce 10478+out:
1facf9fc 10479+ return err;
10480+}
10481+
10482+/*
10483+ * @file may be NULL
10484+ */
1e00d052
AM
10485+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10486+ int datasync)
1facf9fc 10487+{
10488+ int err;
b752ccd1 10489+ struct dentry *dentry;
5527c038 10490+ struct inode *inode;
1facf9fc 10491+ struct super_block *sb;
1facf9fc 10492+
10493+ err = 0;
2000de60 10494+ dentry = file->f_path.dentry;
5527c038 10495+ inode = d_inode(dentry);
febd17d6 10496+ inode_lock(inode);
1facf9fc 10497+ sb = dentry->d_sb;
10498+ si_noflush_read_lock(sb);
10499+ if (file)
10500+ err = au_do_fsync_dir(file, datasync);
10501+ else {
10502+ di_write_lock_child(dentry);
10503+ err = au_do_fsync_dir_no_file(dentry, datasync);
10504+ }
5527c038 10505+ au_cpup_attr_timesizes(inode);
1facf9fc 10506+ di_write_unlock(dentry);
10507+ if (file)
10508+ fi_write_unlock(file);
10509+
10510+ si_read_unlock(sb);
febd17d6 10511+ inode_unlock(inode);
1facf9fc 10512+ return err;
10513+}
10514+
10515+/* ---------------------------------------------------------------------- */
10516+
5afbbe0d 10517+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10518+{
10519+ int err;
10520+ struct dentry *dentry;
9dbd164d 10521+ struct inode *inode, *h_inode;
1facf9fc 10522+ struct super_block *sb;
10523+
062440b3 10524+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10525+
2000de60 10526+ dentry = file->f_path.dentry;
5527c038 10527+ inode = d_inode(dentry);
1facf9fc 10528+ IMustLock(inode);
10529+
10530+ sb = dentry->d_sb;
10531+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10532+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10533+ if (unlikely(err))
10534+ goto out;
027c5e7a
AM
10535+ err = au_alive_dir(dentry);
10536+ if (!err)
10537+ err = au_vdir_init(file);
1facf9fc 10538+ di_downgrade_lock(dentry, AuLock_IR);
10539+ if (unlikely(err))
10540+ goto out_unlock;
10541+
5afbbe0d 10542+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10543+ if (!au_test_nfsd()) {
392086de 10544+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10545+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10546+ } else {
10547+ /*
10548+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10549+ * encode_fh() and others.
10550+ */
9dbd164d 10551+ atomic_inc(&h_inode->i_count);
1facf9fc 10552+ di_read_unlock(dentry, AuLock_IR);
10553+ si_read_unlock(sb);
392086de 10554+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10555+ fsstack_copy_attr_atime(inode, h_inode);
10556+ fi_write_unlock(file);
9dbd164d 10557+ iput(h_inode);
1facf9fc 10558+
10559+ AuTraceErr(err);
10560+ return err;
10561+ }
10562+
4f0767ce 10563+out_unlock:
1facf9fc 10564+ di_read_unlock(dentry, AuLock_IR);
10565+ fi_write_unlock(file);
4f0767ce 10566+out:
1facf9fc 10567+ si_read_unlock(sb);
10568+ return err;
10569+}
10570+
10571+/* ---------------------------------------------------------------------- */
10572+
10573+#define AuTestEmpty_WHONLY 1
dece6358
AM
10574+#define AuTestEmpty_CALLED (1 << 1)
10575+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10576+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10577+#define au_fset_testempty(flags, name) \
10578+ do { (flags) |= AuTestEmpty_##name; } while (0)
10579+#define au_fclr_testempty(flags, name) \
10580+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10581+
dece6358
AM
10582+#ifndef CONFIG_AUFS_SHWH
10583+#undef AuTestEmpty_SHWH
10584+#define AuTestEmpty_SHWH 0
10585+#endif
10586+
1facf9fc 10587+struct test_empty_arg {
392086de 10588+ struct dir_context ctx;
1308ab2a 10589+ struct au_nhash *whlist;
1facf9fc 10590+ unsigned int flags;
10591+ int err;
10592+ aufs_bindex_t bindex;
10593+};
10594+
392086de
AM
10595+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10596+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10597+ unsigned int d_type)
1facf9fc 10598+{
392086de
AM
10599+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10600+ ctx);
1facf9fc 10601+ char *name = (void *)__name;
10602+
10603+ arg->err = 0;
10604+ au_fset_testempty(arg->flags, CALLED);
10605+ /* smp_mb(); */
10606+ if (name[0] == '.'
10607+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10608+ goto out; /* success */
10609+
10610+ if (namelen <= AUFS_WH_PFX_LEN
10611+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10612+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10613+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10614+ arg->err = -ENOTEMPTY;
10615+ goto out;
10616+ }
10617+
10618+ name += AUFS_WH_PFX_LEN;
10619+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10620+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10621+ arg->err = au_nhash_append_wh
1308ab2a 10622+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10623+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10624+
4f0767ce 10625+out:
1facf9fc 10626+ /* smp_mb(); */
10627+ AuTraceErr(arg->err);
10628+ return arg->err;
10629+}
10630+
10631+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10632+{
10633+ int err;
10634+ struct file *h_file;
acd2b654 10635+ struct au_branch *br;
1facf9fc 10636+
10637+ h_file = au_h_open(dentry, arg->bindex,
10638+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10639+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10640+ err = PTR_ERR(h_file);
10641+ if (IS_ERR(h_file))
10642+ goto out;
10643+
10644+ err = 0;
10645+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10646+ && !file_inode(h_file)->i_nlink)
1facf9fc 10647+ goto out_put;
10648+
10649+ do {
10650+ arg->err = 0;
10651+ au_fclr_testempty(arg->flags, CALLED);
10652+ /* smp_mb(); */
392086de 10653+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10654+ if (err >= 0)
10655+ err = arg->err;
10656+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10657+
4f0767ce 10658+out_put:
1facf9fc 10659+ fput(h_file);
acd2b654
AM
10660+ br = au_sbr(dentry->d_sb, arg->bindex);
10661+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 10662+out:
1facf9fc 10663+ return err;
10664+}
10665+
10666+struct do_test_empty_args {
10667+ int *errp;
10668+ struct dentry *dentry;
10669+ struct test_empty_arg *arg;
10670+};
10671+
10672+static void call_do_test_empty(void *args)
10673+{
10674+ struct do_test_empty_args *a = args;
10675+ *a->errp = do_test_empty(a->dentry, a->arg);
10676+}
10677+
10678+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10679+{
10680+ int err, wkq_err;
10681+ struct dentry *h_dentry;
10682+ struct inode *h_inode;
10683+
10684+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10685+ h_inode = d_inode(h_dentry);
53392da6 10686+ /* todo: i_mode changes anytime? */
be118d29 10687+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10688+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10689+ inode_unlock_shared(h_inode);
1facf9fc 10690+ if (!err)
10691+ err = do_test_empty(dentry, arg);
10692+ else {
10693+ struct do_test_empty_args args = {
10694+ .errp = &err,
10695+ .dentry = dentry,
10696+ .arg = arg
10697+ };
10698+ unsigned int flags = arg->flags;
10699+
10700+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10701+ if (unlikely(wkq_err))
10702+ err = wkq_err;
10703+ arg->flags = flags;
10704+ }
10705+
10706+ return err;
10707+}
10708+
10709+int au_test_empty_lower(struct dentry *dentry)
10710+{
10711+ int err;
1308ab2a 10712+ unsigned int rdhash;
5afbbe0d 10713+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10714+ struct au_nhash whlist;
392086de
AM
10715+ struct test_empty_arg arg = {
10716+ .ctx = {
2000de60 10717+ .actor = test_empty_cb
392086de
AM
10718+ }
10719+ };
076b876e 10720+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10721+
dece6358
AM
10722+ SiMustAnyLock(dentry->d_sb);
10723+
1308ab2a 10724+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10725+ if (!rdhash)
10726+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10727+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10728+ if (unlikely(err))
1facf9fc 10729+ goto out;
10730+
1facf9fc 10731+ arg.flags = 0;
1308ab2a 10732+ arg.whlist = &whlist;
5afbbe0d 10733+ btop = au_dbtop(dentry);
dece6358
AM
10734+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10735+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10736+ test_empty = do_test_empty;
10737+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10738+ test_empty = sio_test_empty;
5afbbe0d 10739+ arg.bindex = btop;
076b876e 10740+ err = test_empty(dentry, &arg);
1facf9fc 10741+ if (unlikely(err))
10742+ goto out_whlist;
10743+
10744+ au_fset_testempty(arg.flags, WHONLY);
10745+ btail = au_dbtaildir(dentry);
5afbbe0d 10746+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10747+ struct dentry *h_dentry;
10748+
10749+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10750+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10751+ arg.bindex = bindex;
076b876e 10752+ err = test_empty(dentry, &arg);
1facf9fc 10753+ }
10754+ }
10755+
4f0767ce 10756+out_whlist:
1308ab2a 10757+ au_nhash_wh_free(&whlist);
4f0767ce 10758+out:
1facf9fc 10759+ return err;
10760+}
10761+
10762+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10763+{
10764+ int err;
392086de
AM
10765+ struct test_empty_arg arg = {
10766+ .ctx = {
2000de60 10767+ .actor = test_empty_cb
392086de
AM
10768+ }
10769+ };
1facf9fc 10770+ aufs_bindex_t bindex, btail;
10771+
10772+ err = 0;
1308ab2a 10773+ arg.whlist = whlist;
1facf9fc 10774+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10775+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10776+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10777+ btail = au_dbtaildir(dentry);
5afbbe0d 10778+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10779+ struct dentry *h_dentry;
10780+
10781+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10782+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10783+ arg.bindex = bindex;
10784+ err = sio_test_empty(dentry, &arg);
10785+ }
10786+ }
10787+
10788+ return err;
10789+}
10790+
10791+/* ---------------------------------------------------------------------- */
10792+
10793+const struct file_operations aufs_dir_fop = {
4a4d8108 10794+ .owner = THIS_MODULE,
027c5e7a 10795+ .llseek = default_llseek,
1facf9fc 10796+ .read = generic_read_dir,
5afbbe0d 10797+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10798+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10799+#ifdef CONFIG_COMPAT
10800+ .compat_ioctl = aufs_compat_ioctl_dir,
10801+#endif
1facf9fc 10802+ .open = aufs_open_dir,
10803+ .release = aufs_release_dir,
4a4d8108 10804+ .flush = aufs_flush_dir,
1facf9fc 10805+ .fsync = aufs_fsync_dir
10806+};
7f207e10 10807diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
eca34b5c 10808--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
016522bc 10809+++ linux/fs/aufs/dir.h 2020-01-27 10:57:18.168871450 +0100
9f237c51 10810@@ -0,0 +1,134 @@
062440b3 10811+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10812+/*
016522bc 10813+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 10814+ *
10815+ * This program, aufs is free software; you can redistribute it and/or modify
10816+ * it under the terms of the GNU General Public License as published by
10817+ * the Free Software Foundation; either version 2 of the License, or
10818+ * (at your option) any later version.
dece6358
AM
10819+ *
10820+ * This program is distributed in the hope that it will be useful,
10821+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10822+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10823+ * GNU General Public License for more details.
10824+ *
10825+ * You should have received a copy of the GNU General Public License
523b37e3 10826+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10827+ */
10828+
10829+/*
10830+ * directory operations
10831+ */
10832+
10833+#ifndef __AUFS_DIR_H__
10834+#define __AUFS_DIR_H__
10835+
10836+#ifdef __KERNEL__
10837+
10838+#include <linux/fs.h>
1facf9fc 10839+
10840+/* ---------------------------------------------------------------------- */
10841+
10842+/* need to be faster and smaller */
10843+
10844+struct au_nhash {
dece6358
AM
10845+ unsigned int nh_num;
10846+ struct hlist_head *nh_head;
1facf9fc 10847+};
10848+
10849+struct au_vdir_destr {
10850+ unsigned char len;
10851+ unsigned char name[0];
10852+} __packed;
10853+
10854+struct au_vdir_dehstr {
10855+ struct hlist_node hash;
1c60b727 10856+ struct au_vdir_destr *str;
9f237c51 10857+ struct rcu_head rcu;
4a4d8108 10858+} ____cacheline_aligned_in_smp;
1facf9fc 10859+
10860+struct au_vdir_de {
10861+ ino_t de_ino;
10862+ unsigned char de_type;
10863+ /* caution: packed */
10864+ struct au_vdir_destr de_str;
10865+} __packed;
10866+
10867+struct au_vdir_wh {
10868+ struct hlist_node wh_hash;
dece6358
AM
10869+#ifdef CONFIG_AUFS_SHWH
10870+ ino_t wh_ino;
1facf9fc 10871+ aufs_bindex_t wh_bindex;
dece6358
AM
10872+ unsigned char wh_type;
10873+#else
10874+ aufs_bindex_t wh_bindex;
10875+#endif
10876+ /* caution: packed */
1facf9fc 10877+ struct au_vdir_destr wh_str;
10878+} __packed;
10879+
10880+union au_vdir_deblk_p {
10881+ unsigned char *deblk;
10882+ struct au_vdir_de *de;
10883+};
10884+
10885+struct au_vdir {
10886+ unsigned char **vd_deblk;
10887+ unsigned long vd_nblk;
1facf9fc 10888+ struct {
10889+ unsigned long ul;
10890+ union au_vdir_deblk_p p;
10891+ } vd_last;
10892+
be118d29 10893+ u64 vd_version;
dece6358 10894+ unsigned int vd_deblk_sz;
9f237c51
AM
10895+ unsigned long vd_jiffy;
10896+ struct rcu_head rcu;
4a4d8108 10897+} ____cacheline_aligned_in_smp;
1facf9fc 10898+
10899+/* ---------------------------------------------------------------------- */
10900+
10901+/* dir.c */
10902+extern const struct file_operations aufs_dir_fop;
10903+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10904+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10905+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10906+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10907+int au_test_empty_lower(struct dentry *dentry);
10908+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10909+
10910+/* vdir.c */
1308ab2a 10911+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10912+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10913+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10914+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10915+ int limit);
dece6358
AM
10916+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10917+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10918+ unsigned int d_type, aufs_bindex_t bindex,
10919+ unsigned char shwh);
1c60b727 10920+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 10921+int au_vdir_init(struct file *file);
392086de 10922+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10923+
10924+/* ioctl.c */
10925+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10926+
1308ab2a 10927+#ifdef CONFIG_AUFS_RDU
10928+/* rdu.c */
10929+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10930+#ifdef CONFIG_COMPAT
10931+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10932+ unsigned long arg);
10933+#endif
1308ab2a 10934+#else
c1595e42
JR
10935+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10936+ unsigned int cmd, unsigned long arg)
b752ccd1 10937+#ifdef CONFIG_COMPAT
c1595e42
JR
10938+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10939+ unsigned int cmd, unsigned long arg)
b752ccd1 10940+#endif
1308ab2a 10941+#endif
10942+
1facf9fc 10943+#endif /* __KERNEL__ */
10944+#endif /* __AUFS_DIR_H__ */
8b6a4947 10945diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
eca34b5c 10946--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
016522bc 10947+++ linux/fs/aufs/dirren.c 2020-01-27 10:57:18.168871450 +0100
062440b3 10948@@ -0,0 +1,1316 @@
cd7a4cd9 10949+// SPDX-License-Identifier: GPL-2.0
8b6a4947 10950+/*
016522bc 10951+ * Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
10952+ *
10953+ * This program, aufs is free software; you can redistribute it and/or modify
10954+ * it under the terms of the GNU General Public License as published by
10955+ * the Free Software Foundation; either version 2 of the License, or
10956+ * (at your option) any later version.
10957+ *
10958+ * This program is distributed in the hope that it will be useful,
10959+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10960+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10961+ * GNU General Public License for more details.
10962+ *
10963+ * You should have received a copy of the GNU General Public License
10964+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10965+ */
10966+
10967+/*
acd2b654 10968+ * special handling in renaming a directory
8b6a4947
AM
10969+ * in order to support looking-up the before-renamed name on the lower readonly
10970+ * branches
10971+ */
10972+
10973+#include <linux/byteorder/generic.h>
10974+#include "aufs.h"
10975+
10976+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10977+{
10978+ int idx;
10979+
10980+ idx = au_dr_ihash(ent->dr_h_ino);
10981+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10982+}
10983+
10984+static int au_dr_hino_test_empty(struct au_dr_br *dr)
10985+{
10986+ int ret, i;
10987+ struct hlist_bl_head *hbl;
10988+
10989+ ret = 1;
10990+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
10991+ hbl = dr->dr_h_ino + i;
10992+ hlist_bl_lock(hbl);
10993+ ret &= hlist_bl_empty(hbl);
10994+ hlist_bl_unlock(hbl);
10995+ }
10996+
10997+ return ret;
10998+}
10999+
11000+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11001+{
11002+ struct au_dr_hino *found, *ent;
11003+ struct hlist_bl_head *hbl;
11004+ struct hlist_bl_node *pos;
11005+ int idx;
11006+
11007+ found = NULL;
11008+ idx = au_dr_ihash(ino);
11009+ hbl = dr->dr_h_ino + idx;
11010+ hlist_bl_lock(hbl);
11011+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11012+ if (ent->dr_h_ino == ino) {
11013+ found = ent;
11014+ break;
11015+ }
11016+ hlist_bl_unlock(hbl);
11017+
11018+ return found;
11019+}
11020+
11021+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11022+ struct au_dr_hino *add_ent)
11023+{
11024+ int found, idx;
11025+ struct hlist_bl_head *hbl;
11026+ struct hlist_bl_node *pos;
11027+ struct au_dr_hino *ent;
11028+
11029+ found = 0;
11030+ idx = au_dr_ihash(ino);
11031+ hbl = dr->dr_h_ino + idx;
43982f53 11032+#if 0 /* debug print */
8b6a4947
AM
11033+ {
11034+ struct hlist_bl_node *tmp;
11035+
11036+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11037+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11038+ }
11039+#endif
11040+ hlist_bl_lock(hbl);
11041+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11042+ if (ent->dr_h_ino == ino) {
11043+ found = 1;
11044+ break;
11045+ }
11046+ if (!found && add_ent)
11047+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11048+ hlist_bl_unlock(hbl);
11049+
11050+ if (!found && add_ent)
11051+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11052+
11053+ return found;
11054+}
11055+
11056+void au_dr_hino_free(struct au_dr_br *dr)
11057+{
11058+ int i;
11059+ struct hlist_bl_head *hbl;
11060+ struct hlist_bl_node *pos, *tmp;
11061+ struct au_dr_hino *ent;
11062+
11063+ /* SiMustWriteLock(sb); */
11064+
11065+ for (i = 0; i < AuDirren_NHASH; i++) {
11066+ hbl = dr->dr_h_ino + i;
11067+ /* no spinlock since sbinfo must be write-locked */
11068+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
9f237c51 11069+ au_kfree_rcu(ent);
8b6a4947
AM
11070+ INIT_HLIST_BL_HEAD(hbl);
11071+ }
11072+}
11073+
11074+/* returns the number of inodes or an error */
11075+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11076+ struct file *hinofile)
11077+{
11078+ int err, i;
11079+ ssize_t ssz;
11080+ loff_t pos, oldsize;
11081+ __be64 u64;
11082+ struct inode *hinoinode;
11083+ struct hlist_bl_head *hbl;
11084+ struct hlist_bl_node *n1, *n2;
11085+ struct au_dr_hino *ent;
11086+
11087+ SiMustWriteLock(sb);
11088+ AuDebugOn(!au_br_writable(br->br_perm));
11089+
11090+ hinoinode = file_inode(hinofile);
11091+ oldsize = i_size_read(hinoinode);
11092+
11093+ err = 0;
11094+ pos = 0;
11095+ hbl = br->br_dirren.dr_h_ino;
11096+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11097+ /* no bit-lock since sbinfo must be write-locked */
11098+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11099+ AuDbg("hi%llu, %pD2\n",
11100+ (unsigned long long)ent->dr_h_ino, hinofile);
11101+ u64 = cpu_to_be64(ent->dr_h_ino);
11102+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11103+ if (ssz == sizeof(u64))
11104+ continue;
11105+
11106+ /* write error */
11107+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11108+ err = -ENOSPC;
11109+ if (ssz < 0)
11110+ err = ssz;
11111+ break;
11112+ }
11113+ }
11114+ /* regardless the error */
11115+ if (pos < oldsize) {
11116+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11117+ AuTraceErr(err);
11118+ }
11119+
11120+ AuTraceErr(err);
11121+ return err;
11122+}
11123+
11124+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11125+{
11126+ int err, hidx;
11127+ ssize_t ssz;
11128+ size_t sz, n;
11129+ loff_t pos;
11130+ uint64_t u64;
11131+ struct au_dr_hino *ent;
11132+ struct inode *hinoinode;
11133+ struct hlist_bl_head *hbl;
11134+
11135+ err = 0;
11136+ pos = 0;
11137+ hbl = dr->dr_h_ino;
11138+ hinoinode = file_inode(hinofile);
11139+ sz = i_size_read(hinoinode);
11140+ AuDebugOn(sz % sizeof(u64));
11141+ n = sz / sizeof(u64);
11142+ while (n--) {
11143+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11144+ if (unlikely(ssz != sizeof(u64))) {
11145+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11146+ err = -EINVAL;
11147+ if (ssz < 0)
11148+ err = ssz;
11149+ goto out_free;
11150+ }
11151+
11152+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11153+ if (!ent) {
11154+ err = -ENOMEM;
11155+ AuTraceErr(err);
11156+ goto out_free;
11157+ }
11158+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11159+ AuDbg("hi%llu, %pD2\n",
11160+ (unsigned long long)ent->dr_h_ino, hinofile);
11161+ hidx = au_dr_ihash(ent->dr_h_ino);
11162+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11163+ }
11164+ goto out; /* success */
11165+
11166+out_free:
11167+ au_dr_hino_free(dr);
11168+out:
11169+ AuTraceErr(err);
11170+ return err;
11171+}
11172+
11173+/*
11174+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11175+ * @path is a switch to distinguish load and store.
11176+ */
11177+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11178+ struct au_branch *br, const struct path *path)
11179+{
11180+ int err, flags;
11181+ unsigned char load, suspend;
11182+ struct file *hinofile;
11183+ struct au_hinode *hdir;
11184+ struct inode *dir, *delegated;
11185+ struct path hinopath;
11186+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11187+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11188+
11189+ AuDebugOn(bindex < 0 && !br);
11190+ AuDebugOn(bindex >= 0 && br);
11191+
11192+ err = -EINVAL;
11193+ suspend = !br;
11194+ if (suspend)
11195+ br = au_sbr(sb, bindex);
11196+ load = !!path;
11197+ if (!load) {
11198+ path = &br->br_path;
11199+ AuDebugOn(!au_br_writable(br->br_perm));
11200+ if (unlikely(!au_br_writable(br->br_perm)))
11201+ goto out;
11202+ }
11203+
11204+ hdir = NULL;
11205+ if (suspend) {
11206+ dir = d_inode(sb->s_root);
11207+ hdir = au_hinode(au_ii(dir), bindex);
11208+ dir = hdir->hi_inode;
11209+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11210+ } else {
11211+ dir = d_inode(path->dentry);
11212+ inode_lock_nested(dir, AuLsc_I_CHILD);
11213+ }
11214+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11215+ err = PTR_ERR(hinopath.dentry);
11216+ if (IS_ERR(hinopath.dentry))
11217+ goto out_unlock;
11218+
11219+ err = 0;
11220+ flags = O_RDONLY;
11221+ if (load) {
11222+ if (d_is_negative(hinopath.dentry))
11223+ goto out_dput; /* success */
11224+ } else {
11225+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11226+ if (d_is_positive(hinopath.dentry)) {
11227+ delegated = NULL;
11228+ err = vfsub_unlink(dir, &hinopath, &delegated,
11229+ /*force*/0);
11230+ AuTraceErr(err);
11231+ if (unlikely(err))
11232+ pr_err("ignored err %d, %pd2\n",
11233+ err, hinopath.dentry);
11234+ if (unlikely(err == -EWOULDBLOCK))
11235+ iput(delegated);
11236+ err = 0;
11237+ }
11238+ goto out_dput;
11239+ } else if (!d_is_positive(hinopath.dentry)) {
11240+ err = vfsub_create(dir, &hinopath, 0600,
11241+ /*want_excl*/false);
11242+ AuTraceErr(err);
11243+ if (unlikely(err))
11244+ goto out_dput;
11245+ }
11246+ flags = O_WRONLY;
11247+ }
11248+ hinopath.mnt = path->mnt;
11249+ hinofile = vfsub_dentry_open(&hinopath, flags);
11250+ if (suspend)
11251+ au_hn_inode_unlock(hdir);
11252+ else
11253+ inode_unlock(dir);
11254+ dput(hinopath.dentry);
11255+ AuTraceErrPtr(hinofile);
11256+ if (IS_ERR(hinofile)) {
11257+ err = PTR_ERR(hinofile);
11258+ goto out;
11259+ }
11260+
11261+ if (load)
11262+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11263+ else
11264+ err = au_dr_hino_store(sb, br, hinofile);
11265+ fput(hinofile);
11266+ goto out;
11267+
11268+out_dput:
11269+ dput(hinopath.dentry);
11270+out_unlock:
11271+ if (suspend)
11272+ au_hn_inode_unlock(hdir);
11273+ else
11274+ inode_unlock(dir);
11275+out:
11276+ AuTraceErr(err);
11277+ return err;
11278+}
11279+
11280+/* ---------------------------------------------------------------------- */
11281+
11282+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11283+{
11284+ int err;
11285+ struct kstatfs kstfs;
11286+ dev_t dev;
11287+ struct dentry *dentry;
11288+ struct super_block *sb;
11289+
11290+ err = vfs_statfs((void *)path, &kstfs);
11291+ AuTraceErr(err);
11292+ if (unlikely(err))
11293+ goto out;
11294+
11295+ /* todo: support for UUID */
11296+
11297+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11298+ brid->type = AuBrid_FSID;
11299+ brid->fsid = kstfs.f_fsid;
11300+ } else {
11301+ dentry = path->dentry;
11302+ sb = dentry->d_sb;
11303+ dev = sb->s_dev;
11304+ if (dev) {
11305+ brid->type = AuBrid_DEV;
11306+ brid->dev = dev;
11307+ }
11308+ }
11309+
11310+out:
11311+ return err;
11312+}
11313+
11314+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11315+ const struct path *path)
11316+{
11317+ int err, i;
11318+ struct au_dr_br *dr;
11319+ struct hlist_bl_head *hbl;
11320+
11321+ dr = &br->br_dirren;
11322+ hbl = dr->dr_h_ino;
11323+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11324+ INIT_HLIST_BL_HEAD(hbl);
11325+
11326+ err = au_dr_brid_init(&dr->dr_brid, path);
11327+ if (unlikely(err))
11328+ goto out;
11329+
11330+ if (au_opt_test(au_mntflags(sb), DIRREN))
11331+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11332+
11333+out:
11334+ AuTraceErr(err);
11335+ return err;
11336+}
11337+
11338+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11339+{
11340+ int err;
11341+
11342+ err = 0;
11343+ if (au_br_writable(br->br_perm))
11344+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11345+ if (!err)
11346+ au_dr_hino_free(&br->br_dirren);
11347+
11348+ return err;
11349+}
11350+
11351+/* ---------------------------------------------------------------------- */
11352+
11353+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11354+ char *buf, size_t sz)
11355+{
11356+ int err;
11357+ unsigned int major, minor;
11358+ char *p;
11359+
11360+ p = buf;
11361+ err = snprintf(p, sz, "%d_", brid->type);
11362+ AuDebugOn(err > sz);
11363+ p += err;
11364+ sz -= err;
11365+ switch (brid->type) {
11366+ case AuBrid_Unset:
11367+ return -EINVAL;
11368+ case AuBrid_UUID:
11369+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11370+ break;
11371+ case AuBrid_FSID:
11372+ err = snprintf(p, sz, "%08x-%08x",
11373+ brid->fsid.val[0], brid->fsid.val[1]);
11374+ break;
11375+ case AuBrid_DEV:
11376+ major = MAJOR(brid->dev);
11377+ minor = MINOR(brid->dev);
11378+ if (major <= 0xff && minor <= 0xff)
11379+ err = snprintf(p, sz, "%02x%02x", major, minor);
11380+ else
11381+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11382+ break;
11383+ }
11384+ AuDebugOn(err > sz);
11385+ p += err;
11386+ sz -= err;
11387+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11388+ AuDebugOn(err > sz);
11389+ p += err;
11390+ sz -= err;
11391+
11392+ return p - buf;
11393+}
11394+
11395+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11396+{
11397+ int rlen;
11398+ struct dentry *br_dentry;
11399+ struct inode *br_inode;
11400+
11401+ br_dentry = au_br_dentry(br);
11402+ br_inode = d_inode(br_dentry);
11403+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11404+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11405+ AuDebugOn(rlen > len);
11406+
11407+ return rlen;
11408+}
11409+
11410+/* ---------------------------------------------------------------------- */
11411+
11412+/*
11413+ * from the given @h_dentry, construct drinfo at @*fdata.
11414+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11415+ * @allocated.
11416+ */
11417+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11418+ struct dentry *h_dentry,
11419+ unsigned char *allocated)
11420+{
11421+ int err, v;
11422+ struct au_drinfo_fdata *f, *p;
11423+ struct au_drinfo *drinfo;
11424+ struct inode *h_inode;
11425+ struct qstr *qname;
11426+
11427+ err = 0;
11428+ f = *fdata;
11429+ h_inode = d_inode(h_dentry);
11430+ qname = &h_dentry->d_name;
11431+ drinfo = &f->drinfo;
11432+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11433+ drinfo->oldnamelen = qname->len;
11434+ if (*allocated < sizeof(*f) + qname->len) {
11435+ v = roundup_pow_of_two(*allocated + qname->len);
11436+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11437+ if (unlikely(!p)) {
11438+ err = -ENOMEM;
11439+ AuTraceErr(err);
11440+ goto out;
11441+ }
11442+ f = p;
11443+ *fdata = f;
11444+ *allocated = v;
11445+ drinfo = &f->drinfo;
11446+ }
11447+ memcpy(drinfo->oldname, qname->name, qname->len);
11448+ AuDbg("i%llu, %.*s\n",
11449+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11450+ drinfo->oldname);
11451+
11452+out:
11453+ AuTraceErr(err);
11454+ return err;
11455+}
11456+
11457+/* callers have to free the return value */
11458+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11459+{
11460+ struct au_drinfo *ret, *drinfo;
11461+ struct au_drinfo_fdata fdata;
11462+ int len;
11463+ loff_t pos;
11464+ ssize_t ssz;
11465+
11466+ ret = ERR_PTR(-EIO);
11467+ pos = 0;
11468+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11469+ if (unlikely(ssz != sizeof(fdata))) {
11470+ AuIOErr("ssz %zd, %u, %pD2\n",
11471+ ssz, (unsigned int)sizeof(fdata), file);
11472+ goto out;
11473+ }
11474+
11475+ fdata.magic = ntohl((__force __be32)fdata.magic);
11476+ switch (fdata.magic) {
11477+ case AUFS_DRINFO_MAGIC_V1:
11478+ break;
11479+ default:
11480+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11481+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11482+ goto out;
11483+ }
11484+
11485+ drinfo = &fdata.drinfo;
11486+ len = drinfo->oldnamelen;
11487+ if (!len) {
11488+ AuIOErr("broken drinfo %pD2\n", file);
11489+ goto out;
11490+ }
11491+
11492+ ret = NULL;
11493+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11494+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11495+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11496+ (unsigned long long)drinfo->ino,
11497+ (unsigned long long)h_ino, file);
11498+ goto out; /* success */
11499+ }
11500+
11501+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11502+ if (unlikely(!ret)) {
11503+ ret = ERR_PTR(-ENOMEM);
11504+ AuTraceErrPtr(ret);
11505+ goto out;
11506+ }
11507+
11508+ *ret = *drinfo;
11509+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11510+ if (unlikely(ssz != len)) {
9f237c51 11511+ au_kfree_rcu(ret);
8b6a4947
AM
11512+ ret = ERR_PTR(-EIO);
11513+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11514+ goto out;
11515+ }
11516+
11517+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11518+
11519+out:
11520+ return ret;
11521+}
11522+
11523+/* ---------------------------------------------------------------------- */
11524+
11525+/* in order to be revertible */
11526+struct au_drinfo_rev_elm {
11527+ int created;
11528+ struct dentry *info_dentry;
11529+ struct au_drinfo *info_last;
11530+};
11531+
11532+struct au_drinfo_rev {
11533+ unsigned char already;
11534+ aufs_bindex_t nelm;
11535+ struct au_drinfo_rev_elm elm[0];
11536+};
11537+
11538+/* todo: isn't it too large? */
11539+struct au_drinfo_store {
11540+ struct path h_ppath;
11541+ struct dentry *h_dentry;
11542+ struct au_drinfo_fdata *fdata;
11543+ char *infoname; /* inside of whname, just after PFX */
11544+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11545+ aufs_bindex_t btgt, btail;
11546+ unsigned char no_sio,
11547+ allocated, /* current size of *fdata */
11548+ infonamelen, /* room size for p */
acd2b654 11549+ whnamelen, /* length of the generated name */
8b6a4947
AM
11550+ renameback; /* renamed back */
11551+};
11552+
11553+/* on rename(2) error, the caller should revert it using @elm */
11554+static int au_drinfo_do_store(struct au_drinfo_store *w,
11555+ struct au_drinfo_rev_elm *elm)
11556+{
11557+ int err, len;
11558+ ssize_t ssz;
11559+ loff_t pos;
11560+ struct path infopath = {
11561+ .mnt = w->h_ppath.mnt
11562+ };
11563+ struct inode *h_dir, *h_inode, *delegated;
11564+ struct file *infofile;
11565+ struct qstr *qname;
11566+
11567+ AuDebugOn(elm
11568+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11569+
11570+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11571+ w->whnamelen);
11572+ AuTraceErrPtr(infopath.dentry);
11573+ if (IS_ERR(infopath.dentry)) {
11574+ err = PTR_ERR(infopath.dentry);
11575+ goto out;
11576+ }
11577+
11578+ err = 0;
11579+ h_dir = d_inode(w->h_ppath.dentry);
11580+ if (elm && d_is_negative(infopath.dentry)) {
11581+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11582+ AuTraceErr(err);
11583+ if (unlikely(err))
11584+ goto out_dput;
11585+ elm->created = 1;
11586+ elm->info_dentry = dget(infopath.dentry);
11587+ }
11588+
11589+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11590+ AuTraceErrPtr(infofile);
11591+ if (IS_ERR(infofile)) {
11592+ err = PTR_ERR(infofile);
11593+ goto out_dput;
11594+ }
11595+
11596+ h_inode = d_inode(infopath.dentry);
11597+ if (elm && i_size_read(h_inode)) {
11598+ h_inode = d_inode(w->h_dentry);
11599+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11600+ AuTraceErrPtr(elm->info_last);
11601+ if (IS_ERR(elm->info_last)) {
11602+ err = PTR_ERR(elm->info_last);
11603+ elm->info_last = NULL;
11604+ AuDebugOn(elm->info_dentry);
11605+ goto out_fput;
11606+ }
11607+ }
11608+
11609+ if (elm && w->renameback) {
11610+ delegated = NULL;
11611+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11612+ AuTraceErr(err);
11613+ if (unlikely(err == -EWOULDBLOCK))
11614+ iput(delegated);
11615+ goto out_fput;
11616+ }
11617+
11618+ pos = 0;
11619+ qname = &w->h_dentry->d_name;
11620+ len = sizeof(*w->fdata) + qname->len;
11621+ if (!elm)
11622+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11623+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11624+ if (ssz == len) {
11625+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11626+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11627+ goto out_fput; /* success */
11628+ } else {
11629+ err = -EIO;
11630+ if (ssz < 0)
11631+ err = ssz;
11632+ /* the caller should revert it using @elm */
11633+ }
11634+
11635+out_fput:
11636+ fput(infofile);
11637+out_dput:
11638+ dput(infopath.dentry);
11639+out:
11640+ AuTraceErr(err);
11641+ return err;
11642+}
11643+
11644+struct au_call_drinfo_do_store_args {
11645+ int *errp;
11646+ struct au_drinfo_store *w;
11647+ struct au_drinfo_rev_elm *elm;
11648+};
11649+
11650+static void au_call_drinfo_do_store(void *args)
11651+{
11652+ struct au_call_drinfo_do_store_args *a = args;
11653+
11654+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11655+}
11656+
11657+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11658+ struct au_drinfo_rev_elm *elm)
11659+{
11660+ int err, wkq_err;
11661+
11662+ if (w->no_sio)
11663+ err = au_drinfo_do_store(w, elm);
11664+ else {
11665+ struct au_call_drinfo_do_store_args a = {
11666+ .errp = &err,
11667+ .w = w,
11668+ .elm = elm
11669+ };
11670+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11671+ if (unlikely(wkq_err))
11672+ err = wkq_err;
11673+ }
11674+ AuTraceErr(err);
11675+
11676+ return err;
11677+}
11678+
11679+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11680+ aufs_bindex_t btgt)
11681+{
11682+ int err;
11683+
11684+ memset(w, 0, sizeof(*w));
11685+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11686+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11687+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11688+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11689+ w->btgt = btgt;
11690+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11691+
11692+ err = -ENOMEM;
11693+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11694+ if (unlikely(!w->fdata)) {
11695+ AuTraceErr(err);
11696+ goto out;
11697+ }
11698+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11699+ err = 0;
11700+
11701+out:
11702+ return err;
11703+}
11704+
11705+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11706+{
9f237c51 11707+ au_kfree_rcu(w->fdata);
8b6a4947
AM
11708+}
11709+
11710+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11711+ struct au_drinfo_store *w)
11712+{
11713+ struct au_drinfo_rev_elm *elm;
11714+ struct inode *h_dir, *delegated;
11715+ int err, nelm;
11716+ struct path infopath = {
11717+ .mnt = w->h_ppath.mnt
11718+ };
11719+
11720+ h_dir = d_inode(w->h_ppath.dentry);
11721+ IMustLock(h_dir);
11722+
11723+ err = 0;
11724+ elm = rev->elm;
11725+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11726+ AuDebugOn(elm->created && elm->info_last);
11727+ if (elm->created) {
11728+ AuDbg("here\n");
11729+ delegated = NULL;
11730+ infopath.dentry = elm->info_dentry;
11731+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11732+ !w->no_sio);
11733+ AuTraceErr(err);
11734+ if (unlikely(err == -EWOULDBLOCK))
11735+ iput(delegated);
11736+ dput(elm->info_dentry);
11737+ } else if (elm->info_last) {
11738+ AuDbg("here\n");
11739+ w->fdata->drinfo = *elm->info_last;
11740+ memcpy(w->fdata->drinfo.oldname,
11741+ elm->info_last->oldname,
11742+ elm->info_last->oldnamelen);
11743+ err = au_drinfo_store_sio(w, /*elm*/NULL);
9f237c51 11744+ au_kfree_rcu(elm->info_last);
8b6a4947
AM
11745+ }
11746+ if (unlikely(err))
11747+ AuIOErr("%d, %s\n", err, w->whname);
11748+ /* go on even if err */
11749+ }
11750+}
11751+
11752+/* caller has to call au_dr_rename_fin() later */
11753+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11754+ struct qstr *dst_name, void *_rev)
11755+{
11756+ int err, sz, nelm;
11757+ aufs_bindex_t bindex, btail;
11758+ struct au_drinfo_store work;
11759+ struct au_drinfo_rev *rev, **p;
11760+ struct au_drinfo_rev_elm *elm;
11761+ struct super_block *sb;
11762+ struct au_branch *br;
11763+ struct au_hinode *hdir;
11764+
11765+ err = au_drinfo_store_work_init(&work, btgt);
11766+ AuTraceErr(err);
11767+ if (unlikely(err))
11768+ goto out;
11769+
11770+ err = -ENOMEM;
11771+ btail = au_dbtaildir(dentry);
11772+ nelm = btail - btgt;
11773+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11774+ rev = kcalloc(1, sz, GFP_NOFS);
11775+ if (unlikely(!rev)) {
11776+ AuTraceErr(err);
11777+ goto out_args;
11778+ }
11779+ rev->nelm = nelm;
11780+ elm = rev->elm;
11781+ p = _rev;
11782+ *p = rev;
11783+
11784+ err = 0;
11785+ sb = dentry->d_sb;
11786+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11787+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11788+ hdir = au_hi(d_inode(dentry), btgt);
11789+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11790+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11791+ work.h_dentry = au_h_dptr(dentry, bindex);
11792+ if (!work.h_dentry)
11793+ continue;
11794+
11795+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11796+ &work.allocated);
11797+ AuTraceErr(err);
11798+ if (unlikely(err))
11799+ break;
11800+
11801+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11802+ br = au_sbr(sb, bindex);
11803+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11804+ work.whnamelen += au_drinfo_name(br, work.infoname,
11805+ work.infonamelen);
11806+ AuDbg("whname %.*s, i%llu, %.*s\n",
11807+ work.whnamelen, work.whname,
11808+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11809+ work.fdata->drinfo.oldnamelen,
11810+ work.fdata->drinfo.oldname);
11811+
11812+ err = au_drinfo_store_sio(&work, elm);
11813+ AuTraceErr(err);
11814+ if (unlikely(err))
11815+ break;
11816+ }
11817+ if (unlikely(err)) {
11818+ /* revert all drinfo */
11819+ au_drinfo_store_rev(rev, &work);
9f237c51 11820+ au_kfree_try_rcu(rev);
8b6a4947
AM
11821+ *p = NULL;
11822+ }
11823+ au_hn_inode_unlock(hdir);
11824+
11825+out_args:
11826+ au_drinfo_store_work_fin(&work);
11827+out:
11828+ return err;
11829+}
11830+
11831+/* ---------------------------------------------------------------------- */
11832+
11833+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11834+ struct qstr *dst_name, void *_rev)
11835+{
11836+ int err, already;
11837+ ino_t ino;
11838+ struct super_block *sb;
11839+ struct au_branch *br;
11840+ struct au_dr_br *dr;
11841+ struct dentry *h_dentry;
11842+ struct inode *h_inode;
11843+ struct au_dr_hino *ent;
11844+ struct au_drinfo_rev *rev, **p;
11845+
11846+ AuDbg("bindex %d\n", bindex);
11847+
11848+ err = -ENOMEM;
11849+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11850+ if (unlikely(!ent))
11851+ goto out;
11852+
11853+ sb = src->d_sb;
11854+ br = au_sbr(sb, bindex);
11855+ dr = &br->br_dirren;
11856+ h_dentry = au_h_dptr(src, bindex);
11857+ h_inode = d_inode(h_dentry);
11858+ ino = h_inode->i_ino;
11859+ ent->dr_h_ino = ino;
11860+ already = au_dr_hino_test_add(dr, ino, ent);
11861+ AuDbg("b%d, hi%llu, already %d\n",
11862+ bindex, (unsigned long long)ino, already);
11863+
11864+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11865+ AuTraceErr(err);
11866+ if (!err) {
11867+ p = _rev;
11868+ rev = *p;
11869+ rev->already = already;
11870+ goto out; /* success */
11871+ }
11872+
11873+ /* revert */
11874+ if (!already)
11875+ au_dr_hino_del(dr, ent);
9f237c51 11876+ au_kfree_rcu(ent);
8b6a4947
AM
11877+
11878+out:
11879+ AuTraceErr(err);
11880+ return err;
11881+}
11882+
11883+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11884+{
11885+ struct au_drinfo_rev *rev;
11886+ struct au_drinfo_rev_elm *elm;
11887+ int nelm;
11888+
11889+ rev = _rev;
11890+ elm = rev->elm;
11891+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11892+ dput(elm->info_dentry);
9f237c51 11893+ au_kfree_rcu(elm->info_last);
8b6a4947 11894+ }
9f237c51 11895+ au_kfree_try_rcu(rev);
8b6a4947
AM
11896+}
11897+
11898+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11899+{
11900+ int err;
11901+ struct au_drinfo_store work;
11902+ struct au_drinfo_rev *rev = _rev;
11903+ struct super_block *sb;
11904+ struct au_branch *br;
11905+ struct inode *h_inode;
11906+ struct au_dr_br *dr;
11907+ struct au_dr_hino *ent;
11908+
11909+ err = au_drinfo_store_work_init(&work, btgt);
11910+ if (unlikely(err))
11911+ goto out;
11912+
11913+ sb = src->d_sb;
11914+ br = au_sbr(sb, btgt);
11915+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11916+ work.h_ppath.mnt = au_br_mnt(br);
11917+ au_drinfo_store_rev(rev, &work);
11918+ au_drinfo_store_work_fin(&work);
11919+ if (rev->already)
11920+ goto out;
11921+
11922+ dr = &br->br_dirren;
11923+ h_inode = d_inode(work.h_ppath.dentry);
11924+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11925+ BUG_ON(!ent);
11926+ au_dr_hino_del(dr, ent);
9f237c51 11927+ au_kfree_rcu(ent);
8b6a4947
AM
11928+
11929+out:
9f237c51 11930+ au_kfree_try_rcu(rev);
8b6a4947
AM
11931+ if (unlikely(err))
11932+ pr_err("failed to remove dirren info\n");
11933+}
11934+
11935+/* ---------------------------------------------------------------------- */
11936+
11937+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11938+ char *whname, int whnamelen,
11939+ struct dentry **info_dentry)
11940+{
11941+ struct au_drinfo *drinfo;
11942+ struct file *f;
11943+ struct inode *h_dir;
11944+ struct path infopath;
11945+ int unlocked;
11946+
11947+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11948+
11949+ *info_dentry = NULL;
11950+ drinfo = NULL;
11951+ unlocked = 0;
11952+ h_dir = d_inode(h_ppath->dentry);
be118d29 11953+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947
AM
11954+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11955+ whnamelen);
11956+ if (IS_ERR(infopath.dentry)) {
11957+ drinfo = (void *)infopath.dentry;
11958+ goto out;
11959+ }
11960+
11961+ if (d_is_negative(infopath.dentry))
11962+ goto out_dput; /* success */
11963+
11964+ infopath.mnt = h_ppath->mnt;
11965+ f = vfsub_dentry_open(&infopath, O_RDONLY);
11966+ inode_unlock_shared(h_dir);
11967+ unlocked = 1;
11968+ if (IS_ERR(f)) {
11969+ drinfo = (void *)f;
11970+ goto out_dput;
11971+ }
11972+
11973+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11974+ if (IS_ERR_OR_NULL(drinfo))
11975+ goto out_fput;
11976+
11977+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11978+ *info_dentry = dget(infopath.dentry); /* keep it alive */
11979+
11980+out_fput:
11981+ fput(f);
11982+out_dput:
11983+ dput(infopath.dentry);
11984+out:
11985+ if (!unlocked)
11986+ inode_unlock_shared(h_dir);
11987+ AuTraceErrPtr(drinfo);
11988+ return drinfo;
11989+}
11990+
11991+struct au_drinfo_do_load_args {
11992+ struct au_drinfo **drinfop;
11993+ struct path *h_ppath;
11994+ char *whname;
11995+ int whnamelen;
11996+ struct dentry **info_dentry;
11997+};
11998+
11999+static void au_call_drinfo_do_load(void *args)
12000+{
12001+ struct au_drinfo_do_load_args *a = args;
12002+
12003+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12004+ a->info_dentry);
12005+}
12006+
12007+struct au_drinfo_load {
12008+ struct path h_ppath;
12009+ struct qstr *qname;
12010+ unsigned char no_sio;
12011+
12012+ aufs_bindex_t ninfo;
12013+ struct au_drinfo **drinfo;
12014+};
12015+
12016+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12017+ struct au_branch *br)
12018+{
12019+ int err, wkq_err, whnamelen, e;
12020+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12021+ = AUFS_WH_DR_INFO_PFX;
12022+ struct au_drinfo *drinfo;
12023+ struct qstr oldname;
12024+ struct inode *h_dir, *delegated;
12025+ struct dentry *info_dentry;
12026+ struct path infopath;
12027+
12028+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12029+ whnamelen += au_drinfo_name(br, whname + whnamelen,
12030+ sizeof(whname) - whnamelen);
12031+ if (w->no_sio)
12032+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12033+ &info_dentry);
12034+ else {
12035+ struct au_drinfo_do_load_args args = {
12036+ .drinfop = &drinfo,
12037+ .h_ppath = &w->h_ppath,
12038+ .whname = whname,
12039+ .whnamelen = whnamelen,
12040+ .info_dentry = &info_dentry
12041+ };
12042+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12043+ if (unlikely(wkq_err))
12044+ drinfo = ERR_PTR(wkq_err);
12045+ }
12046+ err = PTR_ERR(drinfo);
12047+ if (IS_ERR_OR_NULL(drinfo))
12048+ goto out;
12049+
12050+ err = 0;
12051+ oldname.len = drinfo->oldnamelen;
12052+ oldname.name = drinfo->oldname;
12053+ if (au_qstreq(w->qname, &oldname)) {
12054+ /* the name is renamed back */
9f237c51 12055+ au_kfree_rcu(drinfo);
8b6a4947
AM
12056+ drinfo = NULL;
12057+
12058+ infopath.dentry = info_dentry;
12059+ infopath.mnt = w->h_ppath.mnt;
12060+ h_dir = d_inode(w->h_ppath.dentry);
12061+ delegated = NULL;
12062+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12063+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12064+ inode_unlock(h_dir);
12065+ if (unlikely(e))
12066+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12067+ if (unlikely(e == -EWOULDBLOCK))
12068+ iput(delegated);
12069+ }
9f237c51 12070+ au_kfree_rcu(w->drinfo[bindex]);
8b6a4947
AM
12071+ w->drinfo[bindex] = drinfo;
12072+ dput(info_dentry);
12073+
12074+out:
12075+ AuTraceErr(err);
12076+ return err;
12077+}
12078+
12079+/* ---------------------------------------------------------------------- */
12080+
12081+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12082+{
12083+ struct au_drinfo **p = drinfo;
12084+
12085+ while (n-- > 0)
9f237c51
AM
12086+ au_kfree_rcu(*drinfo++);
12087+ au_kfree_try_rcu(p);
8b6a4947
AM
12088+}
12089+
12090+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12091+ aufs_bindex_t btgt)
12092+{
12093+ int err, ninfo;
12094+ struct au_drinfo_load w;
12095+ aufs_bindex_t bindex, bbot;
12096+ struct au_branch *br;
12097+ struct inode *h_dir;
12098+ struct au_dr_hino *ent;
12099+ struct super_block *sb;
12100+
12101+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12102+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12103+ AuLNPair(&lkup->whname), btgt);
12104+
12105+ sb = dentry->d_sb;
12106+ bbot = au_sbbot(sb);
12107+ w.ninfo = bbot + 1;
12108+ if (!lkup->dirren.drinfo) {
12109+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12110+ sizeof(*lkup->dirren.drinfo),
12111+ GFP_NOFS);
12112+ if (unlikely(!lkup->dirren.drinfo)) {
12113+ err = -ENOMEM;
12114+ goto out;
12115+ }
12116+ lkup->dirren.ninfo = w.ninfo;
12117+ }
12118+ w.drinfo = lkup->dirren.drinfo;
12119+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12120+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12121+ AuDebugOn(!w.h_ppath.dentry);
12122+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12123+ w.qname = &dentry->d_name;
12124+
12125+ ninfo = 0;
12126+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12127+ br = au_sbr(sb, bindex);
12128+ err = au_drinfo_load(&w, bindex, br);
12129+ if (unlikely(err))
12130+ goto out_free;
12131+ if (w.drinfo[bindex])
12132+ ninfo++;
12133+ }
12134+ if (!ninfo) {
12135+ br = au_sbr(sb, btgt);
12136+ h_dir = d_inode(w.h_ppath.dentry);
12137+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12138+ AuDebugOn(!ent);
12139+ au_dr_hino_del(&br->br_dirren, ent);
9f237c51 12140+ au_kfree_rcu(ent);
8b6a4947
AM
12141+ }
12142+ goto out; /* success */
12143+
12144+out_free:
12145+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12146+ lkup->dirren.ninfo = 0;
12147+ lkup->dirren.drinfo = NULL;
12148+out:
12149+ AuTraceErr(err);
12150+ return err;
12151+}
12152+
12153+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12154+{
12155+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12156+}
12157+
12158+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12159+{
12160+ int err;
12161+ struct au_drinfo *drinfo;
12162+
12163+ err = 0;
12164+ if (!lkup->dirren.drinfo)
12165+ goto out;
43982f53
AM
12166+ AuDebugOn(lkup->dirren.ninfo <= btgt);
12167+ drinfo = lkup->dirren.drinfo[btgt];
8b6a4947
AM
12168+ if (!drinfo)
12169+ goto out;
12170+
9f237c51 12171+ au_kfree_try_rcu(lkup->whname.name);
8b6a4947
AM
12172+ lkup->whname.name = NULL;
12173+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12174+ lkup->dirren.dr_name.name = drinfo->oldname;
12175+ lkup->name = &lkup->dirren.dr_name;
12176+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12177+ if (!err)
12178+ AuDbg("name %.*s, whname %.*s, b%d\n",
12179+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12180+ btgt);
12181+
12182+out:
12183+ AuTraceErr(err);
12184+ return err;
12185+}
12186+
12187+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12188+ ino_t h_ino)
12189+{
12190+ int match;
12191+ struct au_drinfo *drinfo;
12192+
12193+ match = 1;
12194+ if (!lkup->dirren.drinfo)
12195+ goto out;
43982f53
AM
12196+ AuDebugOn(lkup->dirren.ninfo <= bindex);
12197+ drinfo = lkup->dirren.drinfo[bindex];
8b6a4947
AM
12198+ if (!drinfo)
12199+ goto out;
12200+
12201+ match = (drinfo->ino == h_ino);
12202+ AuDbg("match %d\n", match);
12203+
12204+out:
12205+ return match;
12206+}
12207+
12208+/* ---------------------------------------------------------------------- */
12209+
12210+int au_dr_opt_set(struct super_block *sb)
12211+{
12212+ int err;
12213+ aufs_bindex_t bindex, bbot;
12214+ struct au_branch *br;
12215+
12216+ err = 0;
12217+ bbot = au_sbbot(sb);
12218+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12219+ br = au_sbr(sb, bindex);
12220+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12221+ }
12222+
12223+ return err;
12224+}
12225+
12226+int au_dr_opt_flush(struct super_block *sb)
12227+{
12228+ int err;
12229+ aufs_bindex_t bindex, bbot;
12230+ struct au_branch *br;
12231+
12232+ err = 0;
12233+ bbot = au_sbbot(sb);
12234+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12235+ br = au_sbr(sb, bindex);
12236+ if (au_br_writable(br->br_perm))
12237+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12238+ }
12239+
12240+ return err;
12241+}
12242+
12243+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12244+{
12245+ int err;
12246+ aufs_bindex_t bindex, bbot;
12247+ struct au_branch *br;
12248+
12249+ err = 0;
12250+ if (!no_flush) {
12251+ err = au_dr_opt_flush(sb);
12252+ if (unlikely(err))
12253+ goto out;
12254+ }
12255+
12256+ bbot = au_sbbot(sb);
12257+ for (bindex = 0; bindex <= bbot; bindex++) {
12258+ br = au_sbr(sb, bindex);
12259+ au_dr_hino_free(&br->br_dirren);
12260+ }
12261+
12262+out:
12263+ return err;
12264+}
12265diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
eca34b5c 12266--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
016522bc 12267+++ linux/fs/aufs/dirren.h 2020-01-27 10:57:18.168871450 +0100
062440b3
AM
12268@@ -0,0 +1,140 @@
12269+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 12270+/*
016522bc 12271+ * Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
12272+ *
12273+ * This program, aufs is free software; you can redistribute it and/or modify
12274+ * it under the terms of the GNU General Public License as published by
12275+ * the Free Software Foundation; either version 2 of the License, or
12276+ * (at your option) any later version.
12277+ *
12278+ * This program is distributed in the hope that it will be useful,
12279+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12280+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12281+ * GNU General Public License for more details.
12282+ *
12283+ * You should have received a copy of the GNU General Public License
12284+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12285+ */
12286+
12287+/*
12288+ * renamed dir info
12289+ */
12290+
12291+#ifndef __AUFS_DIRREN_H__
12292+#define __AUFS_DIRREN_H__
12293+
12294+#ifdef __KERNEL__
12295+
12296+#include <linux/dcache.h>
12297+#include <linux/statfs.h>
12298+#include <linux/uuid.h>
12299+#include "hbl.h"
12300+
12301+#define AuDirren_NHASH 100
12302+
12303+#ifdef CONFIG_AUFS_DIRREN
12304+enum au_brid_type {
12305+ AuBrid_Unset,
12306+ AuBrid_UUID,
12307+ AuBrid_FSID,
12308+ AuBrid_DEV
12309+};
12310+
12311+struct au_dr_brid {
12312+ enum au_brid_type type;
12313+ union {
12314+ uuid_t uuid; /* unimplemented yet */
12315+ fsid_t fsid;
12316+ dev_t dev;
12317+ };
12318+};
12319+
12320+/* 20 is the max digits length of ulong 64 */
12321+/* brid-type "_" uuid "_" inum */
12322+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12323+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12324+
12325+struct au_dr_hino {
12326+ struct hlist_bl_node dr_hnode;
12327+ ino_t dr_h_ino;
12328+};
12329+
12330+struct au_dr_br {
12331+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12332+ struct au_dr_brid dr_brid;
12333+};
12334+
12335+struct au_dr_lookup {
12336+ /* dr_name is pointed by struct au_do_lookup_args.name */
12337+ struct qstr dr_name; /* subset of dr_info */
12338+ aufs_bindex_t ninfo;
12339+ struct au_drinfo **drinfo;
12340+};
12341+#else
12342+struct au_dr_hino;
12343+/* empty */
12344+struct au_dr_br { };
12345+struct au_dr_lookup { };
12346+#endif
12347+
12348+/* ---------------------------------------------------------------------- */
12349+
12350+struct au_branch;
12351+struct au_do_lookup_args;
12352+struct au_hinode;
12353+#ifdef CONFIG_AUFS_DIRREN
12354+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12355+ struct au_dr_hino *add_ent);
12356+void au_dr_hino_free(struct au_dr_br *dr);
12357+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12358+ const struct path *path);
12359+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12360+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12361+ struct qstr *dst_name, void *_rev);
12362+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12363+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12364+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12365+ aufs_bindex_t bindex);
12366+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12367+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12368+ ino_t h_ino);
12369+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12370+int au_dr_opt_set(struct super_block *sb);
12371+int au_dr_opt_flush(struct super_block *sb);
12372+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12373+#else
12374+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12375+ struct au_dr_hino *add_ent);
12376+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12377+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12378+ const struct path *path);
12379+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12380+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12381+ struct qstr *dst_name, void *_rev);
12382+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12383+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12384+ void *rev);
12385+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12386+ aufs_bindex_t bindex);
12387+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12388+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12389+ aufs_bindex_t bindex, ino_t h_ino);
12390+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12391+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12392+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12393+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12394+#endif
12395+
12396+/* ---------------------------------------------------------------------- */
12397+
12398+#ifdef CONFIG_AUFS_DIRREN
12399+static inline int au_dr_ihash(ino_t h_ino)
12400+{
12401+ return h_ino % AuDirren_NHASH;
12402+}
12403+#else
12404+AuStubInt0(au_dr_ihash, ino_t h_ino);
12405+#endif
12406+
12407+#endif /* __KERNEL__ */
12408+#endif /* __AUFS_DIRREN_H__ */
7f207e10 12409diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
eca34b5c 12410--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
016522bc 12411+++ linux/fs/aufs/dynop.c 2020-01-27 10:57:18.168871450 +0100
fbc438ed 12412@@ -0,0 +1,367 @@
cd7a4cd9 12413+// SPDX-License-Identifier: GPL-2.0
1facf9fc 12414+/*
016522bc 12415+ * Copyright (C) 2010-2020 Junjiro R. Okajima
1facf9fc 12416+ *
12417+ * This program, aufs is free software; you can redistribute it and/or modify
12418+ * it under the terms of the GNU General Public License as published by
12419+ * the Free Software Foundation; either version 2 of the License, or
12420+ * (at your option) any later version.
dece6358
AM
12421+ *
12422+ * This program is distributed in the hope that it will be useful,
12423+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12424+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12425+ * GNU General Public License for more details.
12426+ *
12427+ * You should have received a copy of the GNU General Public License
523b37e3 12428+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12429+ */
12430+
12431+/*
4a4d8108 12432+ * dynamically customizable operations for regular files
1facf9fc 12433+ */
12434+
1facf9fc 12435+#include "aufs.h"
12436+
4a4d8108 12437+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12438+
4a4d8108
AM
12439+/*
12440+ * How large will these lists be?
12441+ * Usually just a few elements, 20-30 at most for each, I guess.
12442+ */
8b6a4947 12443+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12444+
8b6a4947
AM
12445+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12446+ const void *h_op)
1facf9fc 12447+{
4a4d8108 12448+ struct au_dykey *key, *tmp;
8b6a4947 12449+ struct hlist_bl_node *pos;
1facf9fc 12450+
4a4d8108 12451+ key = NULL;
8b6a4947
AM
12452+ hlist_bl_lock(hbl);
12453+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12454+ if (tmp->dk_op.dy_hop == h_op) {
83b672a5
AM
12455+ if (kref_get_unless_zero(&tmp->dk_kref))
12456+ key = tmp;
4a4d8108
AM
12457+ break;
12458+ }
8b6a4947 12459+ hlist_bl_unlock(hbl);
4a4d8108
AM
12460+
12461+ return key;
1facf9fc 12462+}
12463+
4a4d8108 12464+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12465+{
4a4d8108
AM
12466+ struct au_dykey **k, *found;
12467+ const void *h_op = key->dk_op.dy_hop;
12468+ int i;
1facf9fc 12469+
4a4d8108
AM
12470+ found = NULL;
12471+ k = br->br_dykey;
12472+ for (i = 0; i < AuBrDynOp; i++)
12473+ if (k[i]) {
12474+ if (k[i]->dk_op.dy_hop == h_op) {
12475+ found = k[i];
12476+ break;
12477+ }
12478+ } else
12479+ break;
12480+ if (!found) {
12481+ spin_lock(&br->br_dykey_lock);
12482+ for (; i < AuBrDynOp; i++)
12483+ if (k[i]) {
12484+ if (k[i]->dk_op.dy_hop == h_op) {
12485+ found = k[i];
12486+ break;
12487+ }
12488+ } else {
12489+ k[i] = key;
12490+ break;
12491+ }
12492+ spin_unlock(&br->br_dykey_lock);
12493+ BUG_ON(i == AuBrDynOp); /* expand the array */
12494+ }
12495+
12496+ return found;
1facf9fc 12497+}
12498+
4a4d8108 12499+/* kref_get() if @key is already added */
8b6a4947 12500+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12501+{
12502+ struct au_dykey *tmp, *found;
8b6a4947 12503+ struct hlist_bl_node *pos;
4a4d8108 12504+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12505+
4a4d8108 12506+ found = NULL;
8b6a4947
AM
12507+ hlist_bl_lock(hbl);
12508+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12509+ if (tmp->dk_op.dy_hop == h_op) {
83b672a5
AM
12510+ if (kref_get_unless_zero(&tmp->dk_kref))
12511+ found = tmp;
4a4d8108
AM
12512+ break;
12513+ }
12514+ if (!found)
8b6a4947
AM
12515+ hlist_bl_add_head(&key->dk_hnode, hbl);
12516+ hlist_bl_unlock(hbl);
1facf9fc 12517+
4a4d8108
AM
12518+ if (!found)
12519+ DyPrSym(key);
12520+ return found;
12521+}
12522+
12523+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12524+{
4a4d8108
AM
12525+ struct au_dykey *key;
12526+
12527+ key = container_of(rcu, struct au_dykey, dk_rcu);
12528+ DyPrSym(key);
83b672a5 12529+ kfree(key);
1facf9fc 12530+}
12531+
4a4d8108
AM
12532+static void dy_free(struct kref *kref)
12533+{
12534+ struct au_dykey *key;
8b6a4947 12535+ struct hlist_bl_head *hbl;
1facf9fc 12536+
4a4d8108 12537+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12538+ hbl = dynop + key->dk_op.dy_type;
12539+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12540+ call_rcu(&key->dk_rcu, dy_free_rcu);
12541+}
12542+
12543+void au_dy_put(struct au_dykey *key)
1facf9fc 12544+{
4a4d8108
AM
12545+ kref_put(&key->dk_kref, dy_free);
12546+}
1facf9fc 12547+
4a4d8108
AM
12548+/* ---------------------------------------------------------------------- */
12549+
12550+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12551+
12552+#ifdef CONFIG_AUFS_DEBUG
12553+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12554+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12555+#else
12556+#define DyDbgDeclare(cnt) do {} while (0)
12557+#define DyDbgInc(cnt) do {} while (0)
12558+#endif
12559+
12560+#define DySet(func, dst, src, h_op, h_sb) do { \
12561+ DyDbgInc(cnt); \
12562+ if (h_op->func) { \
12563+ if (src.func) \
12564+ dst.func = src.func; \
12565+ else \
12566+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12567+ } \
12568+} while (0)
12569+
12570+#define DySetForce(func, dst, src) do { \
12571+ AuDebugOn(!src.func); \
12572+ DyDbgInc(cnt); \
12573+ dst.func = src.func; \
12574+} while (0)
12575+
12576+#define DySetAop(func) \
12577+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12578+#define DySetAopForce(func) \
12579+ DySetForce(func, dyaop->da_op, aufs_aop)
12580+
12581+static void dy_aop(struct au_dykey *key, const void *h_op,
12582+ struct super_block *h_sb __maybe_unused)
12583+{
12584+ struct au_dyaop *dyaop = (void *)key;
12585+ const struct address_space_operations *h_aop = h_op;
12586+ DyDbgDeclare(cnt);
12587+
12588+ AuDbg("%s\n", au_sbtype(h_sb));
12589+
12590+ DySetAop(writepage);
12591+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12592+ DySetAop(writepages);
12593+ DySetAop(set_page_dirty);
12594+ DySetAop(readpages);
12595+ DySetAop(write_begin);
12596+ DySetAop(write_end);
12597+ DySetAop(bmap);
12598+ DySetAop(invalidatepage);
12599+ DySetAop(releasepage);
027c5e7a 12600+ DySetAop(freepage);
7e9cd9fe 12601+ /* this one will be changed according to an aufs mount option */
4a4d8108 12602+ DySetAop(direct_IO);
4a4d8108 12603+ DySetAop(migratepage);
e2f27e51
AM
12604+ DySetAop(isolate_page);
12605+ DySetAop(putback_page);
4a4d8108
AM
12606+ DySetAop(launder_page);
12607+ DySetAop(is_partially_uptodate);
392086de 12608+ DySetAop(is_dirty_writeback);
4a4d8108 12609+ DySetAop(error_remove_page);
b4510431
AM
12610+ DySetAop(swap_activate);
12611+ DySetAop(swap_deactivate);
4a4d8108
AM
12612+
12613+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12614+}
12615+
4a4d8108
AM
12616+/* ---------------------------------------------------------------------- */
12617+
12618+static void dy_bug(struct kref *kref)
12619+{
12620+ BUG();
12621+}
12622+
12623+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12624+{
12625+ struct au_dykey *key, *old;
8b6a4947 12626+ struct hlist_bl_head *hbl;
b752ccd1 12627+ struct op {
4a4d8108 12628+ unsigned int sz;
b752ccd1
AM
12629+ void (*set)(struct au_dykey *key, const void *h_op,
12630+ struct super_block *h_sb __maybe_unused);
12631+ };
12632+ static const struct op a[] = {
4a4d8108
AM
12633+ [AuDy_AOP] = {
12634+ .sz = sizeof(struct au_dyaop),
b752ccd1 12635+ .set = dy_aop
4a4d8108 12636+ }
b752ccd1
AM
12637+ };
12638+ const struct op *p;
4a4d8108 12639+
8b6a4947
AM
12640+ hbl = dynop + op->dy_type;
12641+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12642+ if (key)
12643+ goto out_add; /* success */
12644+
12645+ p = a + op->dy_type;
12646+ key = kzalloc(p->sz, GFP_NOFS);
12647+ if (unlikely(!key)) {
12648+ key = ERR_PTR(-ENOMEM);
12649+ goto out;
12650+ }
12651+
12652+ key->dk_op.dy_hop = op->dy_hop;
12653+ kref_init(&key->dk_kref);
86dc4139 12654+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12655+ old = dy_gadd(hbl, key);
4a4d8108 12656+ if (old) {
9f237c51 12657+ au_kfree_rcu(key);
4a4d8108
AM
12658+ key = old;
12659+ }
12660+
12661+out_add:
12662+ old = dy_bradd(br, key);
12663+ if (old)
12664+ /* its ref-count should never be zero here */
12665+ kref_put(&key->dk_kref, dy_bug);
12666+out:
12667+ return key;
12668+}
12669+
12670+/* ---------------------------------------------------------------------- */
12671+/*
acd2b654 12672+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
c1595e42 12673+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12674+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12675+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12676+ * See the aufs manual in detail.
4a4d8108
AM
12677+ */
12678+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12679+{
7e9cd9fe 12680+ if (!do_dx)
4a4d8108 12681+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12682+ else
4a4d8108 12683+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12684+}
12685+
12686+static struct au_dyaop *dy_aget(struct au_branch *br,
12687+ const struct address_space_operations *h_aop,
12688+ int do_dx)
12689+{
12690+ struct au_dyaop *dyaop;
12691+ struct au_dynop op;
12692+
12693+ op.dy_type = AuDy_AOP;
12694+ op.dy_haop = h_aop;
12695+ dyaop = (void *)dy_get(&op, br);
12696+ if (IS_ERR(dyaop))
12697+ goto out;
12698+ dy_adx(dyaop, do_dx);
12699+
12700+out:
12701+ return dyaop;
12702+}
12703+
12704+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12705+ struct inode *h_inode)
12706+{
12707+ int err, do_dx;
12708+ struct super_block *sb;
12709+ struct au_branch *br;
12710+ struct au_dyaop *dyaop;
12711+
12712+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12713+ IiMustWriteLock(inode);
12714+
12715+ sb = inode->i_sb;
12716+ br = au_sbr(sb, bindex);
12717+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12718+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12719+ err = PTR_ERR(dyaop);
12720+ if (IS_ERR(dyaop))
12721+ /* unnecessary to call dy_fput() */
12722+ goto out;
12723+
12724+ err = 0;
12725+ inode->i_mapping->a_ops = &dyaop->da_op;
12726+
12727+out:
12728+ return err;
12729+}
12730+
b752ccd1
AM
12731+/*
12732+ * Is it safe to replace a_ops during the inode/file is in operation?
12733+ * Yes, I hope so.
12734+ */
12735+int au_dy_irefresh(struct inode *inode)
12736+{
12737+ int err;
5afbbe0d 12738+ aufs_bindex_t btop;
b752ccd1
AM
12739+ struct inode *h_inode;
12740+
12741+ err = 0;
12742+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12743+ btop = au_ibtop(inode);
12744+ h_inode = au_h_iptr(inode, btop);
12745+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12746+ }
12747+ return err;
12748+}
12749+
4a4d8108
AM
12750+void au_dy_arefresh(int do_dx)
12751+{
8b6a4947
AM
12752+ struct hlist_bl_head *hbl;
12753+ struct hlist_bl_node *pos;
4a4d8108
AM
12754+ struct au_dykey *key;
12755+
8b6a4947
AM
12756+ hbl = dynop + AuDy_AOP;
12757+ hlist_bl_lock(hbl);
12758+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12759+ dy_adx((void *)key, do_dx);
8b6a4947 12760+ hlist_bl_unlock(hbl);
4a4d8108
AM
12761+}
12762+
4a4d8108
AM
12763+/* ---------------------------------------------------------------------- */
12764+
12765+void __init au_dy_init(void)
12766+{
12767+ int i;
12768+
4a4d8108 12769+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12770+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12771+}
12772+
12773+void au_dy_fin(void)
12774+{
12775+ int i;
12776+
12777+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12778+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12779+}
7f207e10 12780diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
eca34b5c 12781--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
016522bc 12782+++ linux/fs/aufs/dynop.h 2020-01-27 10:57:18.168871450 +0100
fbc438ed 12783@@ -0,0 +1,77 @@
062440b3 12784+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12785+/*
016522bc 12786+ * Copyright (C) 2010-2020 Junjiro R. Okajima
4a4d8108
AM
12787+ *
12788+ * This program, aufs is free software; you can redistribute it and/or modify
12789+ * it under the terms of the GNU General Public License as published by
12790+ * the Free Software Foundation; either version 2 of the License, or
12791+ * (at your option) any later version.
12792+ *
12793+ * This program is distributed in the hope that it will be useful,
12794+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12795+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12796+ * GNU General Public License for more details.
12797+ *
12798+ * You should have received a copy of the GNU General Public License
523b37e3 12799+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12800+ */
12801+
12802+/*
12803+ * dynamically customizable operations (for regular files only)
12804+ */
12805+
12806+#ifndef __AUFS_DYNOP_H__
12807+#define __AUFS_DYNOP_H__
12808+
12809+#ifdef __KERNEL__
12810+
7e9cd9fe
AM
12811+#include <linux/fs.h>
12812+#include <linux/kref.h>
4a4d8108 12813+
2cbb1c4b 12814+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12815+
12816+struct au_dynop {
12817+ int dy_type;
12818+ union {
12819+ const void *dy_hop;
12820+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12821+ };
12822+};
12823+
12824+struct au_dykey {
12825+ union {
8b6a4947 12826+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12827+ struct rcu_head dk_rcu;
12828+ };
12829+ struct au_dynop dk_op;
12830+
12831+ /*
12832+ * during I am in the branch local array, kref is gotten. when the
12833+ * branch is removed, kref is put.
12834+ */
12835+ struct kref dk_kref;
12836+};
12837+
12838+/* stop unioning since their sizes are very different from each other */
12839+struct au_dyaop {
12840+ struct au_dykey da_key;
12841+ struct address_space_operations da_op; /* not const */
4a4d8108 12842+};
fbc438ed
JR
12843+/* make sure that 'struct au_dykey *' can be any type */
12844+static_assert(!offsetof(struct au_dyaop, da_key));
4a4d8108 12845+
4a4d8108
AM
12846+/* ---------------------------------------------------------------------- */
12847+
12848+/* dynop.c */
12849+struct au_branch;
12850+void au_dy_put(struct au_dykey *key);
12851+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12852+ struct inode *h_inode);
b752ccd1 12853+int au_dy_irefresh(struct inode *inode);
4a4d8108 12854+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12855+
12856+void __init au_dy_init(void);
12857+void au_dy_fin(void);
12858+
4a4d8108
AM
12859+#endif /* __KERNEL__ */
12860+#endif /* __AUFS_DYNOP_H__ */
7f207e10 12861diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
eca34b5c 12862--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
016522bc 12863+++ linux/fs/aufs/export.c 2020-01-27 10:57:18.168871450 +0100
062440b3 12864@@ -0,0 +1,838 @@
cd7a4cd9 12865+// SPDX-License-Identifier: GPL-2.0
4a4d8108 12866+/*
016522bc 12867+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
12868+ *
12869+ * This program, aufs is free software; you can redistribute it and/or modify
12870+ * it under the terms of the GNU General Public License as published by
12871+ * the Free Software Foundation; either version 2 of the License, or
12872+ * (at your option) any later version.
12873+ *
12874+ * This program is distributed in the hope that it will be useful,
12875+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12876+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12877+ * GNU General Public License for more details.
12878+ *
12879+ * You should have received a copy of the GNU General Public License
523b37e3 12880+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12881+ */
12882+
12883+/*
12884+ * export via nfs
12885+ */
12886+
12887+#include <linux/exportfs.h>
7eafdf33 12888+#include <linux/fs_struct.h>
4a4d8108
AM
12889+#include <linux/namei.h>
12890+#include <linux/nsproxy.h>
12891+#include <linux/random.h>
12892+#include <linux/writeback.h>
12893+#include "aufs.h"
12894+
12895+union conv {
12896+#ifdef CONFIG_AUFS_INO_T_64
12897+ __u32 a[2];
12898+#else
12899+ __u32 a[1];
12900+#endif
12901+ ino_t ino;
12902+};
12903+
12904+static ino_t decode_ino(__u32 *a)
12905+{
12906+ union conv u;
12907+
12908+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12909+ u.a[0] = a[0];
12910+#ifdef CONFIG_AUFS_INO_T_64
12911+ u.a[1] = a[1];
12912+#endif
12913+ return u.ino;
12914+}
12915+
12916+static void encode_ino(__u32 *a, ino_t ino)
12917+{
12918+ union conv u;
12919+
12920+ u.ino = ino;
12921+ a[0] = u.a[0];
12922+#ifdef CONFIG_AUFS_INO_T_64
12923+ a[1] = u.a[1];
12924+#endif
12925+}
12926+
12927+/* NFS file handle */
12928+enum {
12929+ Fh_br_id,
12930+ Fh_sigen,
12931+#ifdef CONFIG_AUFS_INO_T_64
12932+ /* support 64bit inode number */
12933+ Fh_ino1,
12934+ Fh_ino2,
12935+ Fh_dir_ino1,
12936+ Fh_dir_ino2,
12937+#else
12938+ Fh_ino1,
12939+ Fh_dir_ino1,
12940+#endif
12941+ Fh_igen,
12942+ Fh_h_type,
12943+ Fh_tail,
12944+
12945+ Fh_ino = Fh_ino1,
12946+ Fh_dir_ino = Fh_dir_ino1
12947+};
12948+
12949+static int au_test_anon(struct dentry *dentry)
12950+{
027c5e7a 12951+ /* note: read d_flags without d_lock */
4a4d8108
AM
12952+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12953+}
12954+
a2a7ad62
AM
12955+int au_test_nfsd(void)
12956+{
12957+ int ret;
12958+ struct task_struct *tsk = current;
12959+ char comm[sizeof(tsk->comm)];
12960+
12961+ ret = 0;
12962+ if (tsk->flags & PF_KTHREAD) {
12963+ get_task_comm(comm, tsk);
12964+ ret = !strcmp(comm, "nfsd");
12965+ }
12966+
12967+ return ret;
12968+}
12969+
4a4d8108
AM
12970+/* ---------------------------------------------------------------------- */
12971+/* inode generation external table */
12972+
b752ccd1 12973+void au_xigen_inc(struct inode *inode)
4a4d8108 12974+{
4a4d8108
AM
12975+ loff_t pos;
12976+ ssize_t sz;
12977+ __u32 igen;
12978+ struct super_block *sb;
12979+ struct au_sbinfo *sbinfo;
12980+
4a4d8108 12981+ sb = inode->i_sb;
b752ccd1 12982+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 12983+
b752ccd1 12984+ sbinfo = au_sbi(sb);
1facf9fc 12985+ pos = inode->i_ino;
12986+ pos *= sizeof(igen);
12987+ igen = inode->i_generation + 1;
1facf9fc 12988+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
12989+ sizeof(igen), &pos);
12990+ if (sz == sizeof(igen))
b752ccd1 12991+ return; /* success */
1facf9fc 12992+
b752ccd1 12993+ if (unlikely(sz >= 0))
1facf9fc 12994+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 12995+}
12996+
12997+int au_xigen_new(struct inode *inode)
12998+{
12999+ int err;
13000+ loff_t pos;
13001+ ssize_t sz;
13002+ struct super_block *sb;
13003+ struct au_sbinfo *sbinfo;
13004+ struct file *file;
13005+
13006+ err = 0;
13007+ /* todo: dirty, at mount time */
13008+ if (inode->i_ino == AUFS_ROOT_INO)
13009+ goto out;
13010+ sb = inode->i_sb;
dece6358 13011+ SiMustAnyLock(sb);
1facf9fc 13012+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13013+ goto out;
13014+
13015+ err = -EFBIG;
13016+ pos = inode->i_ino;
13017+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13018+ AuIOErr1("too large i%lld\n", pos);
13019+ goto out;
13020+ }
13021+ pos *= sizeof(inode->i_generation);
13022+
13023+ err = 0;
13024+ sbinfo = au_sbi(sb);
13025+ file = sbinfo->si_xigen;
13026+ BUG_ON(!file);
13027+
c06a8ce3 13028+ if (vfsub_f_size_read(file)
1facf9fc 13029+ < pos + sizeof(inode->i_generation)) {
13030+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13031+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13032+ sizeof(inode->i_generation), &pos);
13033+ } else
13034+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13035+ sizeof(inode->i_generation), &pos);
13036+ if (sz == sizeof(inode->i_generation))
13037+ goto out; /* success */
13038+
13039+ err = sz;
13040+ if (unlikely(sz >= 0)) {
13041+ err = -EIO;
13042+ AuIOErr("xigen error (%zd)\n", sz);
13043+ }
13044+
4f0767ce 13045+out:
1facf9fc 13046+ return err;
13047+}
13048+
062440b3 13049+int au_xigen_set(struct super_block *sb, struct path *path)
1facf9fc 13050+{
13051+ int err;
13052+ struct au_sbinfo *sbinfo;
13053+ struct file *file;
13054+
dece6358
AM
13055+ SiMustWriteLock(sb);
13056+
1facf9fc 13057+ sbinfo = au_sbi(sb);
062440b3 13058+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
1facf9fc 13059+ err = PTR_ERR(file);
13060+ if (IS_ERR(file))
13061+ goto out;
13062+ err = 0;
13063+ if (sbinfo->si_xigen)
13064+ fput(sbinfo->si_xigen);
13065+ sbinfo->si_xigen = file;
13066+
4f0767ce 13067+out:
062440b3 13068+ AuTraceErr(err);
1facf9fc 13069+ return err;
13070+}
13071+
13072+void au_xigen_clr(struct super_block *sb)
13073+{
13074+ struct au_sbinfo *sbinfo;
13075+
dece6358
AM
13076+ SiMustWriteLock(sb);
13077+
1facf9fc 13078+ sbinfo = au_sbi(sb);
13079+ if (sbinfo->si_xigen) {
13080+ fput(sbinfo->si_xigen);
13081+ sbinfo->si_xigen = NULL;
13082+ }
13083+}
13084+
13085+/* ---------------------------------------------------------------------- */
13086+
13087+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13088+ ino_t dir_ino)
13089+{
13090+ struct dentry *dentry, *d;
13091+ struct inode *inode;
13092+ unsigned int sigen;
13093+
13094+ dentry = NULL;
13095+ inode = ilookup(sb, ino);
13096+ if (!inode)
13097+ goto out;
13098+
13099+ dentry = ERR_PTR(-ESTALE);
13100+ sigen = au_sigen(sb);
5afbbe0d 13101+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13102+ || IS_DEADDIR(inode)
537831f9 13103+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13104+ goto out_iput;
13105+
13106+ dentry = NULL;
13107+ if (!dir_ino || S_ISDIR(inode->i_mode))
13108+ dentry = d_find_alias(inode);
13109+ else {
027c5e7a 13110+ spin_lock(&inode->i_lock);
c1595e42 13111+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13112+ spin_lock(&d->d_lock);
1facf9fc 13113+ if (!au_test_anon(d)
5527c038 13114+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13115+ dentry = dget_dlock(d);
13116+ spin_unlock(&d->d_lock);
1facf9fc 13117+ break;
13118+ }
027c5e7a
AM
13119+ spin_unlock(&d->d_lock);
13120+ }
13121+ spin_unlock(&inode->i_lock);
1facf9fc 13122+ }
027c5e7a 13123+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13124+ /* need to refresh */
1facf9fc 13125+ dput(dentry);
2cbb1c4b 13126+ dentry = NULL;
1facf9fc 13127+ }
13128+
4f0767ce 13129+out_iput:
1facf9fc 13130+ iput(inode);
4f0767ce 13131+out:
2cbb1c4b 13132+ AuTraceErrPtr(dentry);
1facf9fc 13133+ return dentry;
13134+}
13135+
13136+/* ---------------------------------------------------------------------- */
13137+
13138+/* todo: dirty? */
13139+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13140+
13141+struct au_compare_mnt_args {
13142+ /* input */
13143+ struct super_block *sb;
13144+
13145+ /* output */
13146+ struct vfsmount *mnt;
13147+};
13148+
13149+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13150+{
13151+ struct au_compare_mnt_args *a = arg;
13152+
13153+ if (mnt->mnt_sb != a->sb)
13154+ return 0;
13155+ a->mnt = mntget(mnt);
13156+ return 1;
13157+}
13158+
1facf9fc 13159+static struct vfsmount *au_mnt_get(struct super_block *sb)
13160+{
4a4d8108 13161+ int err;
7eafdf33 13162+ struct path root;
4a4d8108
AM
13163+ struct au_compare_mnt_args args = {
13164+ .sb = sb
13165+ };
1facf9fc 13166+
7eafdf33 13167+ get_fs_root(current->fs, &root);
523b37e3 13168+ rcu_read_lock();
7eafdf33 13169+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13170+ rcu_read_unlock();
7eafdf33 13171+ path_put(&root);
4a4d8108
AM
13172+ AuDebugOn(!err);
13173+ AuDebugOn(!args.mnt);
13174+ return args.mnt;
1facf9fc 13175+}
13176+
13177+struct au_nfsd_si_lock {
4a4d8108 13178+ unsigned int sigen;
027c5e7a 13179+ aufs_bindex_t bindex, br_id;
1facf9fc 13180+ unsigned char force_lock;
13181+};
13182+
027c5e7a
AM
13183+static int si_nfsd_read_lock(struct super_block *sb,
13184+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13185+{
027c5e7a 13186+ int err;
1facf9fc 13187+ aufs_bindex_t bindex;
13188+
13189+ si_read_lock(sb, AuLock_FLUSH);
13190+
13191+ /* branch id may be wrapped around */
027c5e7a 13192+ err = 0;
1facf9fc 13193+ bindex = au_br_index(sb, nsi_lock->br_id);
13194+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13195+ goto out; /* success */
13196+
027c5e7a
AM
13197+ err = -ESTALE;
13198+ bindex = -1;
1facf9fc 13199+ if (!nsi_lock->force_lock)
13200+ si_read_unlock(sb);
1facf9fc 13201+
4f0767ce 13202+out:
027c5e7a
AM
13203+ nsi_lock->bindex = bindex;
13204+ return err;
1facf9fc 13205+}
13206+
13207+struct find_name_by_ino {
392086de 13208+ struct dir_context ctx;
1facf9fc 13209+ int called, found;
13210+ ino_t ino;
13211+ char *name;
13212+ int namelen;
13213+};
13214+
13215+static int
392086de
AM
13216+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13217+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13218+{
392086de
AM
13219+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13220+ ctx);
1facf9fc 13221+
13222+ a->called++;
13223+ if (a->ino != ino)
13224+ return 0;
13225+
13226+ memcpy(a->name, name, namelen);
13227+ a->namelen = namelen;
13228+ a->found = 1;
13229+ return 1;
13230+}
13231+
13232+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13233+ struct au_nfsd_si_lock *nsi_lock)
13234+{
13235+ struct dentry *dentry, *parent;
13236+ struct file *file;
13237+ struct inode *dir;
392086de
AM
13238+ struct find_name_by_ino arg = {
13239+ .ctx = {
2000de60 13240+ .actor = find_name_by_ino
392086de
AM
13241+ }
13242+ };
1facf9fc 13243+ int err;
13244+
13245+ parent = path->dentry;
13246+ if (nsi_lock)
13247+ si_read_unlock(parent->d_sb);
4a4d8108 13248+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13249+ dentry = (void *)file;
13250+ if (IS_ERR(file))
13251+ goto out;
13252+
13253+ dentry = ERR_PTR(-ENOMEM);
537831f9 13254+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13255+ if (unlikely(!arg.name))
13256+ goto out_file;
13257+ arg.ino = ino;
13258+ arg.found = 0;
13259+ do {
13260+ arg.called = 0;
13261+ /* smp_mb(); */
392086de 13262+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13263+ } while (!err && !arg.found && arg.called);
13264+ dentry = ERR_PTR(err);
13265+ if (unlikely(err))
13266+ goto out_name;
1716fcea
AM
13267+ /* instead of ENOENT */
13268+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13269+ if (!arg.found)
13270+ goto out_name;
13271+
b4510431 13272+ /* do not call vfsub_lkup_one() */
5527c038 13273+ dir = d_inode(parent);
febd17d6 13274+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13275+ AuTraceErrPtr(dentry);
13276+ if (IS_ERR(dentry))
13277+ goto out_name;
13278+ AuDebugOn(au_test_anon(dentry));
5527c038 13279+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13280+ dput(dentry);
13281+ dentry = ERR_PTR(-ENOENT);
13282+ }
13283+
4f0767ce 13284+out_name:
1c60b727 13285+ free_page((unsigned long)arg.name);
4f0767ce 13286+out_file:
1facf9fc 13287+ fput(file);
4f0767ce 13288+out:
1facf9fc 13289+ if (unlikely(nsi_lock
13290+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13291+ if (!IS_ERR(dentry)) {
13292+ dput(dentry);
13293+ dentry = ERR_PTR(-ESTALE);
13294+ }
13295+ AuTraceErrPtr(dentry);
13296+ return dentry;
13297+}
13298+
13299+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13300+ ino_t dir_ino,
13301+ struct au_nfsd_si_lock *nsi_lock)
13302+{
13303+ struct dentry *dentry;
13304+ struct path path;
13305+
13306+ if (dir_ino != AUFS_ROOT_INO) {
13307+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13308+ dentry = path.dentry;
13309+ if (!path.dentry || IS_ERR(path.dentry))
13310+ goto out;
13311+ AuDebugOn(au_test_anon(path.dentry));
13312+ } else
13313+ path.dentry = dget(sb->s_root);
13314+
13315+ path.mnt = au_mnt_get(sb);
13316+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13317+ path_put(&path);
13318+
4f0767ce 13319+out:
1facf9fc 13320+ AuTraceErrPtr(dentry);
13321+ return dentry;
13322+}
13323+
13324+/* ---------------------------------------------------------------------- */
13325+
13326+static int h_acceptable(void *expv, struct dentry *dentry)
13327+{
13328+ return 1;
13329+}
13330+
13331+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13332+ char *buf, int len, struct super_block *sb)
13333+{
13334+ char *p;
13335+ int n;
13336+ struct path path;
13337+
13338+ p = d_path(h_rootpath, buf, len);
13339+ if (IS_ERR(p))
13340+ goto out;
13341+ n = strlen(p);
13342+
13343+ path.mnt = h_rootpath->mnt;
13344+ path.dentry = h_parent;
13345+ p = d_path(&path, buf, len);
13346+ if (IS_ERR(p))
13347+ goto out;
13348+ if (n != 1)
13349+ p += n;
13350+
13351+ path.mnt = au_mnt_get(sb);
13352+ path.dentry = sb->s_root;
13353+ p = d_path(&path, buf, len - strlen(p));
13354+ mntput(path.mnt);
13355+ if (IS_ERR(p))
13356+ goto out;
13357+ if (n != 1)
13358+ p[strlen(p)] = '/';
13359+
4f0767ce 13360+out:
1facf9fc 13361+ AuTraceErrPtr(p);
13362+ return p;
13363+}
13364+
13365+static
027c5e7a
AM
13366+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13367+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13368+{
13369+ struct dentry *dentry, *h_parent, *root;
13370+ struct super_block *h_sb;
13371+ char *pathname, *p;
13372+ struct vfsmount *h_mnt;
13373+ struct au_branch *br;
13374+ int err;
13375+ struct path path;
13376+
027c5e7a 13377+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13378+ h_mnt = au_br_mnt(br);
1facf9fc 13379+ h_sb = h_mnt->mnt_sb;
13380+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13381+ lockdep_off();
1facf9fc 13382+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13383+ fh_len - Fh_tail, fh[Fh_h_type],
13384+ h_acceptable, /*context*/NULL);
5afbbe0d 13385+ lockdep_on();
1facf9fc 13386+ dentry = h_parent;
13387+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13388+ AuWarn1("%s decode_fh failed, %ld\n",
13389+ au_sbtype(h_sb), PTR_ERR(h_parent));
13390+ goto out;
13391+ }
13392+ dentry = NULL;
13393+ if (unlikely(au_test_anon(h_parent))) {
13394+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13395+ au_sbtype(h_sb));
13396+ goto out_h_parent;
13397+ }
13398+
13399+ dentry = ERR_PTR(-ENOMEM);
13400+ pathname = (void *)__get_free_page(GFP_NOFS);
13401+ if (unlikely(!pathname))
13402+ goto out_h_parent;
13403+
13404+ root = sb->s_root;
13405+ path.mnt = h_mnt;
13406+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13407+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13408+ di_read_unlock(root, !AuLock_IR);
13409+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13410+ dentry = (void *)p;
13411+ if (IS_ERR(p))
13412+ goto out_pathname;
13413+
13414+ si_read_unlock(sb);
13415+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13416+ dentry = ERR_PTR(err);
13417+ if (unlikely(err))
13418+ goto out_relock;
13419+
13420+ dentry = ERR_PTR(-ENOENT);
13421+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13422+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13423+ goto out_path;
13424+
5527c038 13425+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13426+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13427+ else
13428+ dentry = dget(path.dentry);
13429+
4f0767ce 13430+out_path:
1facf9fc 13431+ path_put(&path);
4f0767ce 13432+out_relock:
1facf9fc 13433+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13434+ if (!IS_ERR(dentry)) {
13435+ dput(dentry);
13436+ dentry = ERR_PTR(-ESTALE);
13437+ }
4f0767ce 13438+out_pathname:
1c60b727 13439+ free_page((unsigned long)pathname);
4f0767ce 13440+out_h_parent:
1facf9fc 13441+ dput(h_parent);
4f0767ce 13442+out:
1facf9fc 13443+ AuTraceErrPtr(dentry);
13444+ return dentry;
13445+}
13446+
13447+/* ---------------------------------------------------------------------- */
13448+
13449+static struct dentry *
13450+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13451+ int fh_type)
13452+{
13453+ struct dentry *dentry;
13454+ __u32 *fh = fid->raw;
027c5e7a 13455+ struct au_branch *br;
1facf9fc 13456+ ino_t ino, dir_ino;
1facf9fc 13457+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13458+ .force_lock = 0
13459+ };
13460+
1facf9fc 13461+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13462+ /* it should never happen, but the file handle is unreliable */
13463+ if (unlikely(fh_len < Fh_tail))
13464+ goto out;
13465+ nsi_lock.sigen = fh[Fh_sigen];
13466+ nsi_lock.br_id = fh[Fh_br_id];
13467+
1facf9fc 13468+ /* branch id may be wrapped around */
027c5e7a
AM
13469+ br = NULL;
13470+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13471+ goto out;
13472+ nsi_lock.force_lock = 1;
13473+
13474+ /* is this inode still cached? */
13475+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13476+ /* it should never happen */
13477+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13478+ goto out_unlock;
4a4d8108 13479+
1facf9fc 13480+ dir_ino = decode_ino(fh + Fh_dir_ino);
13481+ dentry = decode_by_ino(sb, ino, dir_ino);
13482+ if (IS_ERR(dentry))
13483+ goto out_unlock;
13484+ if (dentry)
13485+ goto accept;
13486+
13487+ /* is the parent dir cached? */
027c5e7a 13488+ br = au_sbr(sb, nsi_lock.bindex);
acd2b654 13489+ au_lcnt_inc(&br->br_nfiles);
1facf9fc 13490+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13491+ if (IS_ERR(dentry))
13492+ goto out_unlock;
13493+ if (dentry)
13494+ goto accept;
13495+
13496+ /* lookup path */
027c5e7a 13497+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13498+ if (IS_ERR(dentry))
13499+ goto out_unlock;
13500+ if (unlikely(!dentry))
13501+ /* todo?: make it ESTALE */
13502+ goto out_unlock;
13503+
4f0767ce 13504+accept:
027c5e7a 13505+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13506+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13507+ goto out_unlock; /* success */
13508+
13509+ dput(dentry);
13510+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13511+out_unlock:
027c5e7a 13512+ if (br)
acd2b654 13513+ au_lcnt_dec(&br->br_nfiles);
1facf9fc 13514+ si_read_unlock(sb);
4f0767ce 13515+out:
1facf9fc 13516+ AuTraceErrPtr(dentry);
13517+ return dentry;
13518+}
13519+
13520+#if 0 /* reserved for future use */
13521+/* support subtreecheck option */
13522+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13523+ int fh_len, int fh_type)
13524+{
13525+ struct dentry *parent;
13526+ __u32 *fh = fid->raw;
13527+ ino_t dir_ino;
13528+
13529+ dir_ino = decode_ino(fh + Fh_dir_ino);
13530+ parent = decode_by_ino(sb, dir_ino, 0);
13531+ if (IS_ERR(parent))
13532+ goto out;
13533+ if (!parent)
13534+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13535+ dir_ino, fh, fh_len);
13536+
4f0767ce 13537+out:
1facf9fc 13538+ AuTraceErrPtr(parent);
13539+ return parent;
13540+}
13541+#endif
13542+
13543+/* ---------------------------------------------------------------------- */
13544+
0c3ec466
AM
13545+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13546+ struct inode *dir)
1facf9fc 13547+{
13548+ int err;
0c3ec466 13549+ aufs_bindex_t bindex;
1facf9fc 13550+ struct super_block *sb, *h_sb;
0c3ec466
AM
13551+ struct dentry *dentry, *parent, *h_parent;
13552+ struct inode *h_dir;
1facf9fc 13553+ struct au_branch *br;
13554+
1facf9fc 13555+ err = -ENOSPC;
13556+ if (unlikely(*max_len <= Fh_tail)) {
13557+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13558+ goto out;
13559+ }
13560+
13561+ err = FILEID_ROOT;
0c3ec466
AM
13562+ if (inode->i_ino == AUFS_ROOT_INO) {
13563+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13564+ goto out;
13565+ }
13566+
1facf9fc 13567+ h_parent = NULL;
0c3ec466
AM
13568+ sb = inode->i_sb;
13569+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13570+ if (unlikely(err))
13571+ goto out;
13572+
1facf9fc 13573+#ifdef CONFIG_AUFS_DEBUG
13574+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13575+ AuWarn1("NFS-exporting requires xino\n");
13576+#endif
027c5e7a 13577+ err = -EIO;
0c3ec466
AM
13578+ parent = NULL;
13579+ ii_read_lock_child(inode);
5afbbe0d 13580+ bindex = au_ibtop(inode);
0c3ec466 13581+ if (!dir) {
c1595e42 13582+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13583+ if (unlikely(!dentry))
13584+ goto out_unlock;
13585+ AuDebugOn(au_test_anon(dentry));
13586+ parent = dget_parent(dentry);
13587+ dput(dentry);
13588+ if (unlikely(!parent))
13589+ goto out_unlock;
5527c038
JR
13590+ if (d_really_is_positive(parent))
13591+ dir = d_inode(parent);
1facf9fc 13592+ }
0c3ec466
AM
13593+
13594+ ii_read_lock_parent(dir);
13595+ h_dir = au_h_iptr(dir, bindex);
13596+ ii_read_unlock(dir);
13597+ if (unlikely(!h_dir))
13598+ goto out_parent;
c1595e42 13599+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13600+ if (unlikely(!h_parent))
0c3ec466 13601+ goto out_hparent;
1facf9fc 13602+
13603+ err = -EPERM;
13604+ br = au_sbr(sb, bindex);
86dc4139 13605+ h_sb = au_br_sb(br);
1facf9fc 13606+ if (unlikely(!h_sb->s_export_op)) {
13607+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13608+ goto out_hparent;
1facf9fc 13609+ }
13610+
13611+ fh[Fh_br_id] = br->br_id;
13612+ fh[Fh_sigen] = au_sigen(sb);
13613+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13614+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13615+ fh[Fh_igen] = inode->i_generation;
13616+
13617+ *max_len -= Fh_tail;
13618+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13619+ max_len,
13620+ /*connectable or subtreecheck*/0);
13621+ err = fh[Fh_h_type];
13622+ *max_len += Fh_tail;
13623+ /* todo: macros? */
1716fcea 13624+ if (err != FILEID_INVALID)
1facf9fc 13625+ err = 99;
13626+ else
13627+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13628+
0c3ec466 13629+out_hparent:
1facf9fc 13630+ dput(h_parent);
0c3ec466 13631+out_parent:
1facf9fc 13632+ dput(parent);
0c3ec466
AM
13633+out_unlock:
13634+ ii_read_unlock(inode);
13635+ si_read_unlock(sb);
4f0767ce 13636+out:
1facf9fc 13637+ if (unlikely(err < 0))
1716fcea 13638+ err = FILEID_INVALID;
1facf9fc 13639+ return err;
13640+}
13641+
13642+/* ---------------------------------------------------------------------- */
13643+
4a4d8108
AM
13644+static int aufs_commit_metadata(struct inode *inode)
13645+{
13646+ int err;
13647+ aufs_bindex_t bindex;
13648+ struct super_block *sb;
13649+ struct inode *h_inode;
13650+ int (*f)(struct inode *inode);
13651+
13652+ sb = inode->i_sb;
e49829fe 13653+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13654+ ii_write_lock_child(inode);
5afbbe0d 13655+ bindex = au_ibtop(inode);
4a4d8108
AM
13656+ AuDebugOn(bindex < 0);
13657+ h_inode = au_h_iptr(inode, bindex);
13658+
13659+ f = h_inode->i_sb->s_export_op->commit_metadata;
13660+ if (f)
13661+ err = f(h_inode);
13662+ else {
13663+ struct writeback_control wbc = {
13664+ .sync_mode = WB_SYNC_ALL,
13665+ .nr_to_write = 0 /* metadata only */
13666+ };
13667+
13668+ err = sync_inode(h_inode, &wbc);
13669+ }
13670+
13671+ au_cpup_attr_timesizes(inode);
13672+ ii_write_unlock(inode);
13673+ si_read_unlock(sb);
13674+ return err;
13675+}
13676+
13677+/* ---------------------------------------------------------------------- */
13678+
1facf9fc 13679+static struct export_operations aufs_export_op = {
4a4d8108 13680+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13681+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13682+ .encode_fh = aufs_encode_fh,
13683+ .commit_metadata = aufs_commit_metadata
1facf9fc 13684+};
13685+
13686+void au_export_init(struct super_block *sb)
13687+{
13688+ struct au_sbinfo *sbinfo;
13689+ __u32 u;
13690+
5afbbe0d
AM
13691+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13692+ && IS_MODULE(CONFIG_EXPORTFS),
13693+ AUFS_NAME ": unsupported configuration "
13694+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13695+
1facf9fc 13696+ sb->s_export_op = &aufs_export_op;
13697+ sbinfo = au_sbi(sb);
13698+ sbinfo->si_xigen = NULL;
13699+ get_random_bytes(&u, sizeof(u));
13700+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13701+ atomic_set(&sbinfo->si_xigen_next, u);
13702+}
076b876e 13703diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
eca34b5c 13704--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
016522bc 13705+++ linux/fs/aufs/fhsm.c 2020-01-27 10:57:18.172204883 +0100
062440b3 13706@@ -0,0 +1,427 @@
cd7a4cd9 13707+// SPDX-License-Identifier: GPL-2.0
076b876e 13708+/*
016522bc 13709+ * Copyright (C) 2011-2020 Junjiro R. Okajima
076b876e
AM
13710+ *
13711+ * This program, aufs is free software; you can redistribute it and/or modify
13712+ * it under the terms of the GNU General Public License as published by
13713+ * the Free Software Foundation; either version 2 of the License, or
13714+ * (at your option) any later version.
13715+ *
13716+ * This program is distributed in the hope that it will be useful,
13717+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13718+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13719+ * GNU General Public License for more details.
13720+ *
13721+ * You should have received a copy of the GNU General Public License
13722+ * along with this program; if not, write to the Free Software
13723+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13724+ */
13725+
13726+/*
13727+ * File-based Hierarchy Storage Management
13728+ */
13729+
13730+#include <linux/anon_inodes.h>
13731+#include <linux/poll.h>
13732+#include <linux/seq_file.h>
13733+#include <linux/statfs.h>
13734+#include "aufs.h"
13735+
c1595e42
JR
13736+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13737+{
13738+ struct au_sbinfo *sbinfo;
13739+ struct au_fhsm *fhsm;
13740+
13741+ SiMustAnyLock(sb);
13742+
13743+ sbinfo = au_sbi(sb);
13744+ fhsm = &sbinfo->si_fhsm;
13745+ AuDebugOn(!fhsm);
13746+ return fhsm->fhsm_bottom;
13747+}
13748+
13749+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13750+{
13751+ struct au_sbinfo *sbinfo;
13752+ struct au_fhsm *fhsm;
13753+
13754+ SiMustWriteLock(sb);
13755+
13756+ sbinfo = au_sbi(sb);
13757+ fhsm = &sbinfo->si_fhsm;
13758+ AuDebugOn(!fhsm);
13759+ fhsm->fhsm_bottom = bindex;
13760+}
13761+
13762+/* ---------------------------------------------------------------------- */
13763+
076b876e
AM
13764+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13765+{
13766+ struct au_br_fhsm *bf;
13767+
13768+ bf = br->br_fhsm;
13769+ MtxMustLock(&bf->bf_lock);
13770+
13771+ return !bf->bf_readable
13772+ || time_after(jiffies,
13773+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13774+}
13775+
13776+/* ---------------------------------------------------------------------- */
13777+
13778+static void au_fhsm_notify(struct super_block *sb, int val)
13779+{
13780+ struct au_sbinfo *sbinfo;
13781+ struct au_fhsm *fhsm;
13782+
13783+ SiMustAnyLock(sb);
13784+
13785+ sbinfo = au_sbi(sb);
13786+ fhsm = &sbinfo->si_fhsm;
13787+ if (au_fhsm_pid(fhsm)
13788+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13789+ atomic_set(&fhsm->fhsm_readable, val);
13790+ if (val)
13791+ wake_up(&fhsm->fhsm_wqh);
13792+ }
13793+}
13794+
13795+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13796+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13797+{
13798+ int err;
13799+ struct au_branch *br;
13800+ struct au_br_fhsm *bf;
13801+
13802+ br = au_sbr(sb, bindex);
13803+ AuDebugOn(au_br_rdonly(br));
13804+ bf = br->br_fhsm;
13805+ AuDebugOn(!bf);
13806+
13807+ if (do_lock)
13808+ mutex_lock(&bf->bf_lock);
13809+ else
13810+ MtxMustLock(&bf->bf_lock);
13811+
13812+ /* sb->s_root for NFS is unreliable */
13813+ err = au_br_stfs(br, &bf->bf_stfs);
13814+ if (unlikely(err)) {
13815+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13816+ goto out;
13817+ }
13818+
13819+ bf->bf_jiffy = jiffies;
13820+ bf->bf_readable = 1;
13821+ if (do_notify)
13822+ au_fhsm_notify(sb, /*val*/1);
13823+ if (rstfs)
13824+ *rstfs = bf->bf_stfs;
13825+
13826+out:
13827+ if (do_lock)
13828+ mutex_unlock(&bf->bf_lock);
13829+ au_fhsm_notify(sb, /*val*/1);
13830+
13831+ return err;
13832+}
13833+
13834+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13835+{
13836+ int err;
076b876e
AM
13837+ struct au_sbinfo *sbinfo;
13838+ struct au_fhsm *fhsm;
13839+ struct au_branch *br;
13840+ struct au_br_fhsm *bf;
13841+
13842+ AuDbg("b%d, force %d\n", bindex, force);
13843+ SiMustAnyLock(sb);
13844+
13845+ sbinfo = au_sbi(sb);
13846+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13847+ if (!au_ftest_si(sbinfo, FHSM)
13848+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13849+ return;
13850+
13851+ br = au_sbr(sb, bindex);
13852+ bf = br->br_fhsm;
13853+ AuDebugOn(!bf);
13854+ mutex_lock(&bf->bf_lock);
13855+ if (force
13856+ || au_fhsm_pid(fhsm)
13857+ || au_fhsm_test_jiffy(sbinfo, br))
13858+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13859+ /*do_notify*/1);
13860+ mutex_unlock(&bf->bf_lock);
13861+}
13862+
13863+void au_fhsm_wrote_all(struct super_block *sb, int force)
13864+{
5afbbe0d 13865+ aufs_bindex_t bindex, bbot;
076b876e
AM
13866+ struct au_branch *br;
13867+
13868+ /* exclude the bottom */
5afbbe0d
AM
13869+ bbot = au_fhsm_bottom(sb);
13870+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13871+ br = au_sbr(sb, bindex);
13872+ if (au_br_fhsm(br->br_perm))
13873+ au_fhsm_wrote(sb, bindex, force);
13874+ }
13875+}
13876+
13877+/* ---------------------------------------------------------------------- */
13878+
be118d29 13879+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13880+{
be118d29 13881+ __poll_t mask;
076b876e
AM
13882+ struct au_sbinfo *sbinfo;
13883+ struct au_fhsm *fhsm;
13884+
13885+ mask = 0;
13886+ sbinfo = file->private_data;
13887+ fhsm = &sbinfo->si_fhsm;
13888+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13889+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13890+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13891+
b00004a5
AM
13892+ if (!mask)
13893+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13894+ return mask;
13895+}
13896+
13897+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13898+ struct aufs_stfs *stfs, __s16 brid)
13899+{
13900+ int err;
13901+
13902+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13903+ if (!err)
13904+ err = __put_user(brid, &stbr->brid);
13905+ if (unlikely(err))
13906+ err = -EFAULT;
13907+
13908+ return err;
13909+}
13910+
13911+static ssize_t au_fhsm_do_read(struct super_block *sb,
13912+ struct aufs_stbr __user *stbr, size_t count)
13913+{
13914+ ssize_t err;
13915+ int nstbr;
5afbbe0d 13916+ aufs_bindex_t bindex, bbot;
076b876e
AM
13917+ struct au_branch *br;
13918+ struct au_br_fhsm *bf;
13919+
13920+ /* except the bottom branch */
13921+ err = 0;
13922+ nstbr = 0;
5afbbe0d
AM
13923+ bbot = au_fhsm_bottom(sb);
13924+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
13925+ br = au_sbr(sb, bindex);
13926+ if (!au_br_fhsm(br->br_perm))
13927+ continue;
13928+
13929+ bf = br->br_fhsm;
13930+ mutex_lock(&bf->bf_lock);
13931+ if (bf->bf_readable) {
13932+ err = -EFAULT;
13933+ if (count >= sizeof(*stbr))
13934+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13935+ br->br_id);
13936+ if (!err) {
13937+ bf->bf_readable = 0;
13938+ count -= sizeof(*stbr);
13939+ nstbr++;
13940+ }
13941+ }
13942+ mutex_unlock(&bf->bf_lock);
13943+ }
13944+ if (!err)
13945+ err = sizeof(*stbr) * nstbr;
13946+
13947+ return err;
13948+}
13949+
13950+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13951+ loff_t *pos)
13952+{
13953+ ssize_t err;
13954+ int readable;
5afbbe0d 13955+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
13956+ struct au_sbinfo *sbinfo;
13957+ struct au_fhsm *fhsm;
13958+ struct au_branch *br;
13959+ struct super_block *sb;
13960+
13961+ err = 0;
13962+ sbinfo = file->private_data;
13963+ fhsm = &sbinfo->si_fhsm;
13964+need_data:
13965+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
13966+ if (!atomic_read(&fhsm->fhsm_readable)) {
13967+ if (vfsub_file_flags(file) & O_NONBLOCK)
13968+ err = -EAGAIN;
13969+ else
13970+ err = wait_event_interruptible_locked_irq
13971+ (fhsm->fhsm_wqh,
13972+ atomic_read(&fhsm->fhsm_readable));
13973+ }
13974+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13975+ if (unlikely(err))
13976+ goto out;
13977+
13978+ /* sb may already be dead */
13979+ au_rw_read_lock(&sbinfo->si_rwsem);
13980+ readable = atomic_read(&fhsm->fhsm_readable);
13981+ if (readable > 0) {
13982+ sb = sbinfo->si_sb;
13983+ AuDebugOn(!sb);
13984+ /* exclude the bottom branch */
13985+ nfhsm = 0;
5afbbe0d
AM
13986+ bbot = au_fhsm_bottom(sb);
13987+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13988+ br = au_sbr(sb, bindex);
13989+ if (au_br_fhsm(br->br_perm))
13990+ nfhsm++;
13991+ }
13992+ err = -EMSGSIZE;
13993+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13994+ atomic_set(&fhsm->fhsm_readable, 0);
13995+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13996+ count);
13997+ }
13998+ }
13999+ au_rw_read_unlock(&sbinfo->si_rwsem);
14000+ if (!readable)
14001+ goto need_data;
14002+
14003+out:
14004+ return err;
14005+}
14006+
14007+static int au_fhsm_release(struct inode *inode, struct file *file)
14008+{
14009+ struct au_sbinfo *sbinfo;
14010+ struct au_fhsm *fhsm;
14011+
14012+ /* sb may already be dead */
14013+ sbinfo = file->private_data;
14014+ fhsm = &sbinfo->si_fhsm;
14015+ spin_lock(&fhsm->fhsm_spin);
14016+ fhsm->fhsm_pid = 0;
14017+ spin_unlock(&fhsm->fhsm_spin);
14018+ kobject_put(&sbinfo->si_kobj);
14019+
14020+ return 0;
14021+}
14022+
14023+static const struct file_operations au_fhsm_fops = {
14024+ .owner = THIS_MODULE,
14025+ .llseek = noop_llseek,
14026+ .read = au_fhsm_read,
14027+ .poll = au_fhsm_poll,
14028+ .release = au_fhsm_release
14029+};
14030+
14031+int au_fhsm_fd(struct super_block *sb, int oflags)
14032+{
14033+ int err, fd;
14034+ struct au_sbinfo *sbinfo;
14035+ struct au_fhsm *fhsm;
14036+
14037+ err = -EPERM;
14038+ if (unlikely(!capable(CAP_SYS_ADMIN)))
14039+ goto out;
14040+
14041+ err = -EINVAL;
14042+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14043+ goto out;
14044+
14045+ err = 0;
14046+ sbinfo = au_sbi(sb);
14047+ fhsm = &sbinfo->si_fhsm;
14048+ spin_lock(&fhsm->fhsm_spin);
14049+ if (!fhsm->fhsm_pid)
14050+ fhsm->fhsm_pid = current->pid;
14051+ else
14052+ err = -EBUSY;
14053+ spin_unlock(&fhsm->fhsm_spin);
14054+ if (unlikely(err))
14055+ goto out;
14056+
14057+ oflags |= O_RDONLY;
14058+ /* oflags |= FMODE_NONOTIFY; */
14059+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14060+ err = fd;
14061+ if (unlikely(fd < 0))
14062+ goto out_pid;
14063+
acd2b654 14064+ /* succeed regardless 'fhsm' status */
076b876e
AM
14065+ kobject_get(&sbinfo->si_kobj);
14066+ si_noflush_read_lock(sb);
14067+ if (au_ftest_si(sbinfo, FHSM))
14068+ au_fhsm_wrote_all(sb, /*force*/0);
14069+ si_read_unlock(sb);
14070+ goto out; /* success */
14071+
14072+out_pid:
14073+ spin_lock(&fhsm->fhsm_spin);
14074+ fhsm->fhsm_pid = 0;
14075+ spin_unlock(&fhsm->fhsm_spin);
14076+out:
14077+ AuTraceErr(err);
14078+ return err;
14079+}
14080+
14081+/* ---------------------------------------------------------------------- */
14082+
14083+int au_fhsm_br_alloc(struct au_branch *br)
14084+{
14085+ int err;
14086+
14087+ err = 0;
14088+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14089+ if (br->br_fhsm)
14090+ au_br_fhsm_init(br->br_fhsm);
14091+ else
14092+ err = -ENOMEM;
14093+
14094+ return err;
14095+}
14096+
14097+/* ---------------------------------------------------------------------- */
14098+
14099+void au_fhsm_fin(struct super_block *sb)
14100+{
14101+ au_fhsm_notify(sb, /*val*/-1);
14102+}
14103+
14104+void au_fhsm_init(struct au_sbinfo *sbinfo)
14105+{
14106+ struct au_fhsm *fhsm;
14107+
14108+ fhsm = &sbinfo->si_fhsm;
14109+ spin_lock_init(&fhsm->fhsm_spin);
14110+ init_waitqueue_head(&fhsm->fhsm_wqh);
14111+ atomic_set(&fhsm->fhsm_readable, 0);
14112+ fhsm->fhsm_expire
14113+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14114+ fhsm->fhsm_bottom = -1;
076b876e
AM
14115+}
14116+
14117+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14118+{
14119+ sbinfo->si_fhsm.fhsm_expire
14120+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14121+}
14122+
14123+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14124+{
14125+ unsigned int u;
14126+
14127+ if (!au_ftest_si(sbinfo, FHSM))
14128+ return;
14129+
14130+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14131+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14132+ seq_printf(seq, ",fhsm_sec=%u", u);
14133+}
7f207e10 14134diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
eca34b5c 14135--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
016522bc 14136+++ linux/fs/aufs/file.c 2020-01-27 10:57:18.172204883 +0100
acd2b654 14137@@ -0,0 +1,863 @@
cd7a4cd9 14138+// SPDX-License-Identifier: GPL-2.0
1facf9fc 14139+/*
016522bc 14140+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 14141+ *
14142+ * This program, aufs is free software; you can redistribute it and/or modify
14143+ * it under the terms of the GNU General Public License as published by
14144+ * the Free Software Foundation; either version 2 of the License, or
14145+ * (at your option) any later version.
dece6358
AM
14146+ *
14147+ * This program is distributed in the hope that it will be useful,
14148+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14149+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14150+ * GNU General Public License for more details.
14151+ *
14152+ * You should have received a copy of the GNU General Public License
523b37e3 14153+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14154+ */
14155+
14156+/*
4a4d8108 14157+ * handling file/dir, and address_space operation
1facf9fc 14158+ */
14159+
7eafdf33
AM
14160+#ifdef CONFIG_AUFS_DEBUG
14161+#include <linux/migrate.h>
14162+#endif
4a4d8108 14163+#include <linux/pagemap.h>
1facf9fc 14164+#include "aufs.h"
14165+
4a4d8108
AM
14166+/* drop flags for writing */
14167+unsigned int au_file_roflags(unsigned int flags)
14168+{
14169+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14170+ flags |= O_RDONLY | O_NOATIME;
14171+ return flags;
14172+}
14173+
14174+/* common functions to regular file and dir */
14175+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14176+ struct file *file, int force_wr)
1facf9fc 14177+{
1308ab2a 14178+ struct file *h_file;
4a4d8108
AM
14179+ struct dentry *h_dentry;
14180+ struct inode *h_inode;
14181+ struct super_block *sb;
14182+ struct au_branch *br;
14183+ struct path h_path;
b912730e 14184+ int err;
1facf9fc 14185+
4a4d8108
AM
14186+ /* a race condition can happen between open and unlink/rmdir */
14187+ h_file = ERR_PTR(-ENOENT);
14188+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14189+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14190+ goto out;
5527c038 14191+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14192+ spin_lock(&h_dentry->d_lock);
14193+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14194+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14195+ ;
14196+ spin_unlock(&h_dentry->d_lock);
14197+ if (unlikely(err))
4a4d8108 14198+ goto out;
1facf9fc 14199+
4a4d8108
AM
14200+ sb = dentry->d_sb;
14201+ br = au_sbr(sb, bindex);
b912730e
AM
14202+ err = au_br_test_oflag(flags, br);
14203+ h_file = ERR_PTR(err);
14204+ if (unlikely(err))
027c5e7a 14205+ goto out;
1facf9fc 14206+
4a4d8108 14207+ /* drop flags for writing */
5527c038 14208+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14209+ if (force_wr && !(flags & O_WRONLY))
14210+ force_wr = 0;
4a4d8108 14211+ flags = au_file_roflags(flags);
392086de
AM
14212+ if (force_wr) {
14213+ h_file = ERR_PTR(-EROFS);
14214+ flags = au_file_roflags(flags);
14215+ if (unlikely(vfsub_native_ro(h_inode)
14216+ || IS_APPEND(h_inode)))
14217+ goto out;
14218+ flags &= ~O_ACCMODE;
14219+ flags |= O_WRONLY;
14220+ }
14221+ }
4a4d8108 14222+ flags &= ~O_CREAT;
acd2b654 14223+ au_lcnt_inc(&br->br_nfiles);
4a4d8108 14224+ h_path.dentry = h_dentry;
86dc4139 14225+ h_path.mnt = au_br_mnt(br);
38d290e6 14226+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14227+ if (IS_ERR(h_file))
14228+ goto out_br;
dece6358 14229+
b912730e 14230+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14231+ err = deny_write_access(h_file);
14232+ if (unlikely(err)) {
14233+ fput(h_file);
14234+ h_file = ERR_PTR(err);
14235+ goto out_br;
14236+ }
14237+ }
953406b4 14238+ fsnotify_open(h_file);
4a4d8108 14239+ goto out; /* success */
1facf9fc 14240+
4f0767ce 14241+out_br:
acd2b654 14242+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 14243+out:
4a4d8108
AM
14244+ return h_file;
14245+}
1308ab2a 14246+
076b876e
AM
14247+static int au_cmoo(struct dentry *dentry)
14248+{
8b6a4947 14249+ int err, cmoo, matched;
076b876e
AM
14250+ unsigned int udba;
14251+ struct path h_path;
14252+ struct au_pin pin;
14253+ struct au_cp_generic cpg = {
14254+ .dentry = dentry,
14255+ .bdst = -1,
14256+ .bsrc = -1,
14257+ .len = -1,
14258+ .pin = &pin,
14259+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14260+ };
7e9cd9fe 14261+ struct inode *delegated;
076b876e
AM
14262+ struct super_block *sb;
14263+ struct au_sbinfo *sbinfo;
14264+ struct au_fhsm *fhsm;
14265+ pid_t pid;
14266+ struct au_branch *br;
14267+ struct dentry *parent;
14268+ struct au_hinode *hdir;
14269+
14270+ DiMustWriteLock(dentry);
5527c038 14271+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14272+
14273+ err = 0;
14274+ if (IS_ROOT(dentry))
14275+ goto out;
5afbbe0d 14276+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14277+ if (!cpg.bsrc)
14278+ goto out;
14279+
14280+ sb = dentry->d_sb;
14281+ sbinfo = au_sbi(sb);
14282+ fhsm = &sbinfo->si_fhsm;
14283+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14284+ rcu_read_lock();
14285+ matched = (pid
14286+ && (current->pid == pid
14287+ || rcu_dereference(current->real_parent)->pid == pid));
14288+ rcu_read_unlock();
14289+ if (matched)
076b876e
AM
14290+ goto out;
14291+
14292+ br = au_sbr(sb, cpg.bsrc);
14293+ cmoo = au_br_cmoo(br->br_perm);
14294+ if (!cmoo)
14295+ goto out;
7e9cd9fe 14296+ if (!d_is_reg(dentry))
076b876e
AM
14297+ cmoo &= AuBrAttr_COO_ALL;
14298+ if (!cmoo)
14299+ goto out;
14300+
14301+ parent = dget_parent(dentry);
14302+ di_write_lock_parent(parent);
14303+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14304+ cpg.bdst = err;
14305+ if (unlikely(err < 0)) {
14306+ err = 0; /* there is no upper writable branch */
14307+ goto out_dgrade;
14308+ }
14309+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14310+
14311+ /* do not respect the coo attrib for the target branch */
14312+ err = au_cpup_dirs(dentry, cpg.bdst);
14313+ if (unlikely(err))
14314+ goto out_dgrade;
14315+
14316+ di_downgrade_lock(parent, AuLock_IR);
14317+ udba = au_opt_udba(sb);
14318+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14319+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14320+ if (unlikely(err))
14321+ goto out_parent;
14322+
14323+ err = au_sio_cpup_simple(&cpg);
14324+ au_unpin(&pin);
14325+ if (unlikely(err))
14326+ goto out_parent;
14327+ if (!(cmoo & AuBrWAttr_MOO))
14328+ goto out_parent; /* success */
14329+
14330+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14331+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14332+ if (unlikely(err))
14333+ goto out_parent;
14334+
14335+ h_path.mnt = au_br_mnt(br);
14336+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14337+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14338+ delegated = NULL;
14339+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14340+ au_unpin(&pin);
14341+ /* todo: keep h_dentry or not? */
14342+ if (unlikely(err == -EWOULDBLOCK)) {
14343+ pr_warn("cannot retry for NFSv4 delegation"
14344+ " for an internal unlink\n");
14345+ iput(delegated);
14346+ }
14347+ if (unlikely(err)) {
14348+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14349+ dentry, err);
14350+ err = 0;
14351+ }
14352+ goto out_parent; /* success */
14353+
14354+out_dgrade:
14355+ di_downgrade_lock(parent, AuLock_IR);
14356+out_parent:
14357+ di_read_unlock(parent, AuLock_IR);
14358+ dput(parent);
14359+out:
14360+ AuTraceErr(err);
14361+ return err;
14362+}
14363+
b912730e 14364+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14365+{
8b6a4947 14366+ int err, aopen = args->aopen;
1facf9fc 14367+ struct dentry *dentry;
076b876e 14368+ struct au_finfo *finfo;
1308ab2a 14369+
8b6a4947 14370+ if (!aopen)
b912730e
AM
14371+ err = au_finfo_init(file, args->fidir);
14372+ else {
14373+ lockdep_off();
14374+ err = au_finfo_init(file, args->fidir);
14375+ lockdep_on();
14376+ }
4a4d8108
AM
14377+ if (unlikely(err))
14378+ goto out;
1facf9fc 14379+
2000de60 14380+ dentry = file->f_path.dentry;
b912730e 14381+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14382+ di_write_lock_child(dentry);
14383+ err = au_cmoo(dentry);
14384+ di_downgrade_lock(dentry, AuLock_IR);
14385+ if (!err) {
14386+ if (!aopen)
b912730e 14387+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14388+ else {
14389+ lockdep_off();
acd2b654
AM
14390+ err = args->open(file, vfsub_file_flags(file),
14391+ args->h_file);
8b6a4947
AM
14392+ lockdep_on();
14393+ }
b912730e 14394+ }
8b6a4947 14395+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14396+
076b876e
AM
14397+ finfo = au_fi(file);
14398+ if (!err) {
14399+ finfo->fi_file = file;
8b6a4947
AM
14400+ au_hbl_add(&finfo->fi_hlist,
14401+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14402+ }
8b6a4947 14403+ if (!aopen)
b912730e
AM
14404+ fi_write_unlock(file);
14405+ else {
14406+ lockdep_off();
14407+ fi_write_unlock(file);
14408+ lockdep_on();
14409+ }
4a4d8108 14410+ if (unlikely(err)) {
076b876e 14411+ finfo->fi_hdir = NULL;
1c60b727 14412+ au_finfo_fin(file);
1308ab2a 14413+ }
4a4d8108 14414+
4f0767ce 14415+out:
8b6a4947 14416+ AuTraceErr(err);
1308ab2a 14417+ return err;
14418+}
dece6358 14419+
4a4d8108 14420+int au_reopen_nondir(struct file *file)
1308ab2a 14421+{
4a4d8108 14422+ int err;
5afbbe0d 14423+ aufs_bindex_t btop;
4a4d8108 14424+ struct dentry *dentry;
acd2b654 14425+ struct au_branch *br;
4a4d8108 14426+ struct file *h_file, *h_file_tmp;
1308ab2a 14427+
2000de60 14428+ dentry = file->f_path.dentry;
5afbbe0d 14429+ btop = au_dbtop(dentry);
acd2b654 14430+ br = au_sbr(dentry->d_sb, btop);
4a4d8108 14431+ h_file_tmp = NULL;
5afbbe0d 14432+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14433+ h_file = au_hf_top(file);
14434+ if (file->f_mode == h_file->f_mode)
14435+ return 0; /* success */
14436+ h_file_tmp = h_file;
14437+ get_file(h_file_tmp);
acd2b654 14438+ au_lcnt_inc(&br->br_nfiles);
5afbbe0d 14439+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14440+ }
14441+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14442+ /*
14443+ * it can happen
14444+ * file exists on both of rw and ro
5afbbe0d 14445+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14446+ * prepend a branch as rw, "rw" become ro
14447+ * remove rw/file
14448+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14449+ * --> dbtop is 1, fbtop is still 0
14450+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14451+ */
5afbbe0d 14452+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14453+
5afbbe0d 14454+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14455+ file, /*force_wr*/0);
4a4d8108 14456+ err = PTR_ERR(h_file);
86dc4139
AM
14457+ if (IS_ERR(h_file)) {
14458+ if (h_file_tmp) {
acd2b654 14459+ /* revert */
5afbbe0d 14460+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14461+ h_file_tmp = NULL;
14462+ }
4a4d8108 14463+ goto out; /* todo: close all? */
86dc4139 14464+ }
4a4d8108
AM
14465+
14466+ err = 0;
5afbbe0d
AM
14467+ au_set_fbtop(file, btop);
14468+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14469+ au_update_figen(file);
14470+ /* todo: necessary? */
14471+ /* file->f_ra = h_file->f_ra; */
14472+
4f0767ce 14473+out:
acd2b654 14474+ if (h_file_tmp) {
4a4d8108 14475+ fput(h_file_tmp);
acd2b654
AM
14476+ au_lcnt_dec(&br->br_nfiles);
14477+ }
4a4d8108 14478+ return err;
1facf9fc 14479+}
14480+
1308ab2a 14481+/* ---------------------------------------------------------------------- */
14482+
4a4d8108
AM
14483+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14484+ struct dentry *hi_wh)
1facf9fc 14485+{
4a4d8108 14486+ int err;
5afbbe0d 14487+ aufs_bindex_t btop;
4a4d8108
AM
14488+ struct au_dinfo *dinfo;
14489+ struct dentry *h_dentry;
14490+ struct au_hdentry *hdp;
1facf9fc 14491+
2000de60 14492+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14493+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14494+
5afbbe0d
AM
14495+ btop = dinfo->di_btop;
14496+ dinfo->di_btop = btgt;
14497+ hdp = au_hdentry(dinfo, btgt);
14498+ h_dentry = hdp->hd_dentry;
14499+ hdp->hd_dentry = hi_wh;
4a4d8108 14500+ err = au_reopen_nondir(file);
5afbbe0d
AM
14501+ hdp->hd_dentry = h_dentry;
14502+ dinfo->di_btop = btop;
1facf9fc 14503+
1facf9fc 14504+ return err;
14505+}
14506+
4a4d8108 14507+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14508+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14509+{
4a4d8108 14510+ int err;
027c5e7a 14511+ struct inode *inode, *h_inode;
c2b27bf2
AM
14512+ struct dentry *h_dentry, *hi_wh;
14513+ struct au_cp_generic cpg = {
2000de60 14514+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14515+ .bdst = bcpup,
14516+ .bsrc = -1,
14517+ .len = len,
14518+ .pin = pin
14519+ };
1facf9fc 14520+
5afbbe0d 14521+ au_update_dbtop(cpg.dentry);
5527c038 14522+ inode = d_inode(cpg.dentry);
027c5e7a 14523+ h_inode = NULL;
5afbbe0d
AM
14524+ if (au_dbtop(cpg.dentry) <= bcpup
14525+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14526+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14527+ if (h_dentry && d_is_positive(h_dentry))
14528+ h_inode = d_inode(h_dentry);
027c5e7a 14529+ }
4a4d8108 14530+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14531+ if (!hi_wh && !h_inode)
c2b27bf2 14532+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14533+ else
14534+ /* already copied-up after unlink */
14535+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14536+
4a4d8108 14537+ if (!err
38d290e6
JR
14538+ && (inode->i_nlink > 1
14539+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14540+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14541+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14542+
dece6358 14543+ return err;
1facf9fc 14544+}
14545+
4a4d8108
AM
14546+/*
14547+ * prepare the @file for writing.
14548+ */
14549+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14550+{
4a4d8108 14551+ int err;
5afbbe0d 14552+ aufs_bindex_t dbtop;
c1595e42 14553+ struct dentry *parent;
86dc4139 14554+ struct inode *inode;
1facf9fc 14555+ struct super_block *sb;
4a4d8108 14556+ struct file *h_file;
c2b27bf2 14557+ struct au_cp_generic cpg = {
2000de60 14558+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14559+ .bdst = -1,
14560+ .bsrc = -1,
14561+ .len = len,
14562+ .pin = pin,
14563+ .flags = AuCpup_DTIME
14564+ };
1facf9fc 14565+
c2b27bf2 14566+ sb = cpg.dentry->d_sb;
5527c038 14567+ inode = d_inode(cpg.dentry);
5afbbe0d 14568+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14569+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14570+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14571+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14572+ /*flags*/0);
1facf9fc 14573+ goto out;
4a4d8108 14574+ }
1facf9fc 14575+
027c5e7a 14576+ /* need to cpup or reopen */
c2b27bf2 14577+ parent = dget_parent(cpg.dentry);
4a4d8108 14578+ di_write_lock_parent(parent);
c2b27bf2
AM
14579+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14580+ cpg.bdst = err;
4a4d8108
AM
14581+ if (unlikely(err < 0))
14582+ goto out_dgrade;
14583+ err = 0;
14584+
c2b27bf2
AM
14585+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14586+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14587+ if (unlikely(err))
4a4d8108
AM
14588+ goto out_dgrade;
14589+ }
14590+
c2b27bf2 14591+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14592+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14593+ if (unlikely(err))
14594+ goto out_dgrade;
14595+
5afbbe0d
AM
14596+ dbtop = au_dbtop(cpg.dentry);
14597+ if (dbtop <= cpg.bdst)
c2b27bf2 14598+ cpg.bsrc = cpg.bdst;
027c5e7a 14599+
5afbbe0d 14600+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14601+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14602+ ) {
392086de 14603+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14604+ if (IS_ERR(h_file))
027c5e7a 14605+ err = PTR_ERR(h_file);
86dc4139 14606+ else {
027c5e7a 14607+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14608+ if (dbtop > cpg.bdst)
c2b27bf2 14609+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14610+ if (!err)
14611+ err = au_reopen_nondir(file);
c2b27bf2 14612+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14613+ }
027c5e7a
AM
14614+ } else { /* copyup as wh and reopen */
14615+ /*
14616+ * since writable hfsplus branch is not supported,
14617+ * h_open_pre/post() are unnecessary.
14618+ */
c2b27bf2 14619+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14620+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14621+ }
4a4d8108
AM
14622+
14623+ if (!err) {
14624+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14625+ goto out_dput; /* success */
14626+ }
14627+ au_unpin(pin);
14628+ goto out_unlock;
1facf9fc 14629+
4f0767ce 14630+out_dgrade:
4a4d8108 14631+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14632+out_unlock:
4a4d8108 14633+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14634+out_dput:
4a4d8108 14635+ dput(parent);
4f0767ce 14636+out:
1facf9fc 14637+ return err;
14638+}
14639+
4a4d8108
AM
14640+/* ---------------------------------------------------------------------- */
14641+
14642+int au_do_flush(struct file *file, fl_owner_t id,
14643+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14644+{
4a4d8108 14645+ int err;
1facf9fc 14646+ struct super_block *sb;
4a4d8108 14647+ struct inode *inode;
1facf9fc 14648+
c06a8ce3
AM
14649+ inode = file_inode(file);
14650+ sb = inode->i_sb;
4a4d8108
AM
14651+ si_noflush_read_lock(sb);
14652+ fi_read_lock(file);
b752ccd1 14653+ ii_read_lock_child(inode);
1facf9fc 14654+
4a4d8108
AM
14655+ err = flush(file, id);
14656+ au_cpup_attr_timesizes(inode);
1facf9fc 14657+
b752ccd1 14658+ ii_read_unlock(inode);
4a4d8108 14659+ fi_read_unlock(file);
1308ab2a 14660+ si_read_unlock(sb);
dece6358 14661+ return err;
1facf9fc 14662+}
14663+
4a4d8108
AM
14664+/* ---------------------------------------------------------------------- */
14665+
14666+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14667+{
4a4d8108 14668+ int err;
4a4d8108
AM
14669+ struct au_pin pin;
14670+ struct au_finfo *finfo;
c2b27bf2 14671+ struct dentry *parent, *hi_wh;
4a4d8108 14672+ struct inode *inode;
1facf9fc 14673+ struct super_block *sb;
c2b27bf2 14674+ struct au_cp_generic cpg = {
2000de60 14675+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14676+ .bdst = -1,
14677+ .bsrc = -1,
14678+ .len = -1,
14679+ .pin = &pin,
14680+ .flags = AuCpup_DTIME
14681+ };
1facf9fc 14682+
4a4d8108
AM
14683+ FiMustWriteLock(file);
14684+
14685+ err = 0;
14686+ finfo = au_fi(file);
c2b27bf2 14687+ sb = cpg.dentry->d_sb;
5527c038 14688+ inode = d_inode(cpg.dentry);
5afbbe0d 14689+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14690+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14691+ goto out;
dece6358 14692+
c2b27bf2
AM
14693+ parent = dget_parent(cpg.dentry);
14694+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14695+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14696+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14697+ cpg.bdst = err;
4a4d8108
AM
14698+ di_read_unlock(parent, !AuLock_IR);
14699+ if (unlikely(err < 0))
14700+ goto out_parent;
14701+ err = 0;
1facf9fc 14702+ }
1facf9fc 14703+
4a4d8108 14704+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14705+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14706+ if (!S_ISDIR(inode->i_mode)
14707+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14708+ && au_plink_test(inode)
c2b27bf2 14709+ && !d_unhashed(cpg.dentry)
5afbbe0d 14710+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14711+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14712+ if (unlikely(err))
14713+ goto out_unlock;
14714+
14715+ /* always superio. */
c2b27bf2 14716+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14717+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14718+ if (!err) {
c2b27bf2 14719+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14720+ au_unpin(&pin);
14721+ }
4a4d8108
AM
14722+ } else if (hi_wh) {
14723+ /* already copied-up after unlink */
c2b27bf2 14724+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14725+ *need_reopen = 0;
14726+ }
1facf9fc 14727+
4f0767ce 14728+out_unlock:
4a4d8108 14729+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14730+out_parent:
4a4d8108 14731+ dput(parent);
4f0767ce 14732+out:
1308ab2a 14733+ return err;
dece6358 14734+}
1facf9fc 14735+
4a4d8108 14736+static void au_do_refresh_dir(struct file *file)
dece6358 14737+{
5afbbe0d 14738+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14739+ struct au_hfile *p, tmp, *q;
14740+ struct au_finfo *finfo;
1308ab2a 14741+ struct super_block *sb;
4a4d8108 14742+ struct au_fidir *fidir;
1facf9fc 14743+
4a4d8108 14744+ FiMustWriteLock(file);
1facf9fc 14745+
2000de60 14746+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14747+ finfo = au_fi(file);
14748+ fidir = finfo->fi_hdir;
14749+ AuDebugOn(!fidir);
14750+ p = fidir->fd_hfile + finfo->fi_btop;
14751+ brid = p->hf_br->br_id;
5afbbe0d
AM
14752+ bbot = fidir->fd_bbot;
14753+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14754+ if (!p->hf_file)
14755+ continue;
1308ab2a 14756+
4a4d8108
AM
14757+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14758+ if (new_bindex == bindex)
14759+ continue;
14760+ if (new_bindex < 0) {
14761+ au_set_h_fptr(file, bindex, NULL);
14762+ continue;
14763+ }
1308ab2a 14764+
4a4d8108
AM
14765+ /* swap two lower inode, and loop again */
14766+ q = fidir->fd_hfile + new_bindex;
14767+ tmp = *q;
14768+ *q = *p;
14769+ *p = tmp;
14770+ if (tmp.hf_file) {
14771+ bindex--;
14772+ p--;
14773+ }
14774+ }
1308ab2a 14775+
4a4d8108 14776+ p = fidir->fd_hfile;
2000de60 14777+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14778+ bbot = au_sbbot(sb);
14779+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14780+ finfo->fi_btop++, p++)
14781+ if (p->hf_file) {
c06a8ce3 14782+ if (file_inode(p->hf_file))
4a4d8108 14783+ break;
1c60b727 14784+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14785+ }
14786+ } else {
5afbbe0d
AM
14787+ bbot = au_br_index(sb, brid);
14788+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14789+ finfo->fi_btop++, p++)
14790+ if (p->hf_file)
1c60b727 14791+ au_hfput(p, /*execed*/0);
5afbbe0d 14792+ bbot = au_sbbot(sb);
4a4d8108 14793+ }
1308ab2a 14794+
5afbbe0d
AM
14795+ p = fidir->fd_hfile + bbot;
14796+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14797+ fidir->fd_bbot--, p--)
14798+ if (p->hf_file) {
c06a8ce3 14799+ if (file_inode(p->hf_file))
4a4d8108 14800+ break;
1c60b727 14801+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14802+ }
14803+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14804+}
14805+
4a4d8108
AM
14806+/*
14807+ * after branch manipulating, refresh the file.
14808+ */
14809+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14810+{
e2f27e51 14811+ int err, need_reopen, nbr;
5afbbe0d 14812+ aufs_bindex_t bbot, bindex;
4a4d8108 14813+ struct dentry *dentry;
e2f27e51 14814+ struct super_block *sb;
1308ab2a 14815+ struct au_finfo *finfo;
4a4d8108 14816+ struct au_hfile *hfile;
1facf9fc 14817+
2000de60 14818+ dentry = file->f_path.dentry;
e2f27e51
AM
14819+ sb = dentry->d_sb;
14820+ nbr = au_sbbot(sb) + 1;
1308ab2a 14821+ finfo = au_fi(file);
4a4d8108
AM
14822+ if (!finfo->fi_hdir) {
14823+ hfile = &finfo->fi_htop;
14824+ AuDebugOn(!hfile->hf_file);
e2f27e51 14825+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14826+ AuDebugOn(bindex < 0);
14827+ if (bindex != finfo->fi_btop)
5afbbe0d 14828+ au_set_fbtop(file, bindex);
4a4d8108 14829+ } else {
e2f27e51 14830+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14831+ if (unlikely(err))
14832+ goto out;
14833+ au_do_refresh_dir(file);
14834+ }
1facf9fc 14835+
4a4d8108
AM
14836+ err = 0;
14837+ need_reopen = 1;
14838+ if (!au_test_mmapped(file))
14839+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14840+ if (finfo->fi_hdir)
14841+ /* harmless if err */
14842+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14843+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14844+ err = reopen(file);
14845+ if (!err) {
14846+ au_update_figen(file);
14847+ goto out; /* success */
14848+ }
14849+
14850+ /* error, close all lower files */
14851+ if (finfo->fi_hdir) {
5afbbe0d
AM
14852+ bbot = au_fbbot_dir(file);
14853+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14854+ au_set_h_fptr(file, bindex, NULL);
14855+ }
1facf9fc 14856+
4f0767ce 14857+out:
1facf9fc 14858+ return err;
14859+}
14860+
4a4d8108
AM
14861+/* common function to regular file and dir */
14862+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14863+ int wlock, unsigned int fi_lsc)
dece6358 14864+{
1308ab2a 14865+ int err;
4a4d8108 14866+ unsigned int sigen, figen;
5afbbe0d 14867+ aufs_bindex_t btop;
4a4d8108
AM
14868+ unsigned char pseudo_link;
14869+ struct dentry *dentry;
14870+ struct inode *inode;
1facf9fc 14871+
4a4d8108 14872+ err = 0;
2000de60 14873+ dentry = file->f_path.dentry;
5527c038 14874+ inode = d_inode(dentry);
4a4d8108 14875+ sigen = au_sigen(dentry->d_sb);
521ced18 14876+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14877+ figen = au_figen(file);
521ced18
JR
14878+ if (!fi_lsc)
14879+ di_write_lock_child(dentry);
14880+ else
14881+ di_write_lock_child2(dentry);
5afbbe0d
AM
14882+ btop = au_dbtop(dentry);
14883+ pseudo_link = (btop != au_ibtop(inode));
14884+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14885+ if (!wlock) {
14886+ di_downgrade_lock(dentry, AuLock_IR);
14887+ fi_downgrade_lock(file);
14888+ }
14889+ goto out; /* success */
14890+ }
dece6358 14891+
4a4d8108 14892+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14893+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14894+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14895+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14896+ }
dece6358 14897+
027c5e7a
AM
14898+ if (!err)
14899+ err = refresh_file(file, reopen);
4a4d8108
AM
14900+ if (!err) {
14901+ if (!wlock) {
14902+ di_downgrade_lock(dentry, AuLock_IR);
14903+ fi_downgrade_lock(file);
14904+ }
14905+ } else {
14906+ di_write_unlock(dentry);
14907+ fi_write_unlock(file);
14908+ }
1facf9fc 14909+
4f0767ce 14910+out:
1308ab2a 14911+ return err;
14912+}
1facf9fc 14913+
4a4d8108
AM
14914+/* ---------------------------------------------------------------------- */
14915+
14916+/* cf. aufs_nopage() */
14917+/* for madvise(2) */
14918+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 14919+{
4a4d8108
AM
14920+ unlock_page(page);
14921+ return 0;
14922+}
1facf9fc 14923+
4a4d8108 14924+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 14925+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 14926+{ BUG(); return 0; }
1facf9fc 14927+
4a4d8108
AM
14928+/* they will never be called. */
14929+#ifdef CONFIG_AUFS_DEBUG
14930+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14931+ loff_t pos, unsigned len, unsigned flags,
14932+ struct page **pagep, void **fsdata)
14933+{ AuUnsupport(); return 0; }
14934+static int aufs_write_end(struct file *file, struct address_space *mapping,
14935+ loff_t pos, unsigned len, unsigned copied,
14936+ struct page *page, void *fsdata)
14937+{ AuUnsupport(); return 0; }
14938+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14939+{ AuUnsupport(); return 0; }
1308ab2a 14940+
4a4d8108
AM
14941+static int aufs_set_page_dirty(struct page *page)
14942+{ AuUnsupport(); return 0; }
392086de
AM
14943+static void aufs_invalidatepage(struct page *page, unsigned int offset,
14944+ unsigned int length)
4a4d8108
AM
14945+{ AuUnsupport(); }
14946+static int aufs_releasepage(struct page *page, gfp_t gfp)
14947+{ AuUnsupport(); return 0; }
79b8bda9 14948+#if 0 /* called by memory compaction regardless file */
4a4d8108 14949+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 14950+ struct page *page, enum migrate_mode mode)
4a4d8108 14951+{ AuUnsupport(); return 0; }
79b8bda9 14952+#endif
e2f27e51
AM
14953+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14954+{ AuUnsupport(); return true; }
14955+static void aufs_putback_page(struct page *page)
14956+{ AuUnsupport(); }
4a4d8108
AM
14957+static int aufs_launder_page(struct page *page)
14958+{ AuUnsupport(); return 0; }
14959+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
14960+ unsigned long from,
14961+ unsigned long count)
4a4d8108 14962+{ AuUnsupport(); return 0; }
392086de
AM
14963+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14964+ bool *writeback)
14965+{ AuUnsupport(); }
4a4d8108
AM
14966+static int aufs_error_remove_page(struct address_space *mapping,
14967+ struct page *page)
14968+{ AuUnsupport(); return 0; }
b4510431
AM
14969+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14970+ sector_t *span)
14971+{ AuUnsupport(); return 0; }
14972+static void aufs_swap_deactivate(struct file *file)
14973+{ AuUnsupport(); }
4a4d8108
AM
14974+#endif /* CONFIG_AUFS_DEBUG */
14975+
14976+const struct address_space_operations aufs_aop = {
14977+ .readpage = aufs_readpage,
14978+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
14979+#ifdef CONFIG_AUFS_DEBUG
14980+ .writepage = aufs_writepage,
4a4d8108
AM
14981+ /* no writepages, because of writepage */
14982+ .set_page_dirty = aufs_set_page_dirty,
14983+ /* no readpages, because of readpage */
14984+ .write_begin = aufs_write_begin,
14985+ .write_end = aufs_write_end,
14986+ /* no bmap, no block device */
14987+ .invalidatepage = aufs_invalidatepage,
14988+ .releasepage = aufs_releasepage,
79b8bda9
AM
14989+ /* is fallback_migrate_page ok? */
14990+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
14991+ .isolate_page = aufs_isolate_page,
14992+ .putback_page = aufs_putback_page,
4a4d8108
AM
14993+ .launder_page = aufs_launder_page,
14994+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 14995+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
14996+ .error_remove_page = aufs_error_remove_page,
14997+ .swap_activate = aufs_swap_activate,
14998+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 14999+#endif /* CONFIG_AUFS_DEBUG */
dece6358 15000+};
7f207e10 15001diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
eca34b5c 15002--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
016522bc 15003+++ linux/fs/aufs/file.h 2020-01-27 10:57:18.172204883 +0100
9f237c51 15004@@ -0,0 +1,342 @@
062440b3 15005+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 15006+/*
016522bc 15007+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
15008+ *
15009+ * This program, aufs is free software; you can redistribute it and/or modify
15010+ * it under the terms of the GNU General Public License as published by
15011+ * the Free Software Foundation; either version 2 of the License, or
15012+ * (at your option) any later version.
15013+ *
15014+ * This program is distributed in the hope that it will be useful,
15015+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15016+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15017+ * GNU General Public License for more details.
15018+ *
15019+ * You should have received a copy of the GNU General Public License
523b37e3 15020+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15021+ */
1facf9fc 15022+
4a4d8108
AM
15023+/*
15024+ * file operations
15025+ */
1facf9fc 15026+
4a4d8108
AM
15027+#ifndef __AUFS_FILE_H__
15028+#define __AUFS_FILE_H__
1facf9fc 15029+
4a4d8108 15030+#ifdef __KERNEL__
1facf9fc 15031+
2cbb1c4b 15032+#include <linux/file.h>
4a4d8108 15033+#include <linux/fs.h>
3c1bdaff 15034+#include <linux/mm_types.h>
4a4d8108 15035+#include <linux/poll.h>
4a4d8108 15036+#include "rwsem.h"
1facf9fc 15037+
4a4d8108
AM
15038+struct au_branch;
15039+struct au_hfile {
15040+ struct file *hf_file;
15041+ struct au_branch *hf_br;
15042+};
1facf9fc 15043+
4a4d8108
AM
15044+struct au_vdir;
15045+struct au_fidir {
15046+ aufs_bindex_t fd_bbot;
15047+ aufs_bindex_t fd_nent;
15048+ struct au_vdir *fd_vdir_cache;
15049+ struct au_hfile fd_hfile[];
15050+};
1facf9fc 15051+
4a4d8108 15052+static inline int au_fidir_sz(int nent)
dece6358 15053+{
4f0767ce
JR
15054+ AuDebugOn(nent < 0);
15055+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 15056+}
1facf9fc 15057+
4a4d8108
AM
15058+struct au_finfo {
15059+ atomic_t fi_generation;
dece6358 15060+
4a4d8108
AM
15061+ struct au_rwsem fi_rwsem;
15062+ aufs_bindex_t fi_btop;
15063+
15064+ /* do not union them */
15065+ struct { /* for non-dir */
15066+ struct au_hfile fi_htop;
2cbb1c4b 15067+ atomic_t fi_mmapped;
4a4d8108
AM
15068+ };
15069+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15070+
8b6a4947 15071+ struct hlist_bl_node fi_hlist;
1c60b727 15072+ struct file *fi_file; /* very ugly */
9f237c51 15073+ struct rcu_head rcu;
4a4d8108 15074+} ____cacheline_aligned_in_smp;
1facf9fc 15075+
4a4d8108 15076+/* ---------------------------------------------------------------------- */
1facf9fc 15077+
4a4d8108
AM
15078+/* file.c */
15079+extern const struct address_space_operations aufs_aop;
15080+unsigned int au_file_roflags(unsigned int flags);
15081+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15082+ struct file *file, int force_wr);
b912730e 15083+struct au_do_open_args {
8b6a4947 15084+ int aopen;
b912730e
AM
15085+ int (*open)(struct file *file, int flags,
15086+ struct file *h_file);
15087+ struct au_fidir *fidir;
15088+ struct file *h_file;
15089+};
15090+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15091+int au_reopen_nondir(struct file *file);
15092+struct au_pin;
15093+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15094+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15095+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15096+int au_do_flush(struct file *file, fl_owner_t id,
15097+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15098+
4a4d8108
AM
15099+/* poll.c */
15100+#ifdef CONFIG_AUFS_POLL
cd7a4cd9 15101+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
4a4d8108 15102+#endif
1facf9fc 15103+
4a4d8108
AM
15104+#ifdef CONFIG_AUFS_BR_HFSPLUS
15105+/* hfsplus.c */
392086de
AM
15106+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15107+ int force_wr);
4a4d8108
AM
15108+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15109+ struct file *h_file);
15110+#else
c1595e42
JR
15111+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15112+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15113+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15114+ struct file *h_file);
15115+#endif
1facf9fc 15116+
4a4d8108
AM
15117+/* f_op.c */
15118+extern const struct file_operations aufs_file_fop;
b912730e 15119+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15120+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15121+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15122+
4a4d8108 15123+/* finfo.c */
f0c0a007 15124+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15125+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15126+ struct file *h_file);
1facf9fc 15127+
4a4d8108 15128+void au_update_figen(struct file *file);
4a4d8108 15129+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15130+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15131+
4a4d8108 15132+void au_fi_init_once(void *_fi);
1c60b727 15133+void au_finfo_fin(struct file *file);
4a4d8108 15134+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15135+
4a4d8108
AM
15136+/* ioctl.c */
15137+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15138+#ifdef CONFIG_COMPAT
15139+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15140+ unsigned long arg);
c2b27bf2
AM
15141+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15142+ unsigned long arg);
b752ccd1 15143+#endif
1facf9fc 15144+
4a4d8108 15145+/* ---------------------------------------------------------------------- */
1facf9fc 15146+
4a4d8108
AM
15147+static inline struct au_finfo *au_fi(struct file *file)
15148+{
38d290e6 15149+ return file->private_data;
4a4d8108 15150+}
1facf9fc 15151+
4a4d8108 15152+/* ---------------------------------------------------------------------- */
1facf9fc 15153+
8b6a4947
AM
15154+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15155+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15156+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15157+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15158+/*
8b6a4947
AM
15159+#define fi_read_trylock_nested(f) \
15160+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15161+#define fi_write_trylock_nested(f) \
15162+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15163+*/
15164+
15165+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15166+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15167+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15168+
521ced18
JR
15169+/* lock subclass for finfo */
15170+enum {
15171+ AuLsc_FI_1,
15172+ AuLsc_FI_2
15173+};
15174+
15175+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15176+{
15177+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15178+}
15179+
15180+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15181+{
15182+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15183+}
15184+
15185+/*
15186+ * fi_read_lock_1, fi_write_lock_1,
15187+ * fi_read_lock_2, fi_write_lock_2
15188+ */
15189+#define AuReadLockFunc(name) \
15190+static inline void fi_read_lock_##name(struct file *f) \
15191+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15192+
15193+#define AuWriteLockFunc(name) \
15194+static inline void fi_write_lock_##name(struct file *f) \
15195+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15196+
15197+#define AuRWLockFuncs(name) \
15198+ AuReadLockFunc(name) \
15199+ AuWriteLockFunc(name)
15200+
15201+AuRWLockFuncs(1);
15202+AuRWLockFuncs(2);
15203+
15204+#undef AuReadLockFunc
15205+#undef AuWriteLockFunc
15206+#undef AuRWLockFuncs
15207+
4a4d8108
AM
15208+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15209+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15210+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15211+
1308ab2a 15212+/* ---------------------------------------------------------------------- */
15213+
4a4d8108 15214+/* todo: hard/soft set? */
5afbbe0d 15215+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15216+{
4a4d8108
AM
15217+ FiMustAnyLock(file);
15218+ return au_fi(file)->fi_btop;
15219+}
dece6358 15220+
5afbbe0d 15221+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15222+{
15223+ FiMustAnyLock(file);
15224+ AuDebugOn(!au_fi(file)->fi_hdir);
15225+ return au_fi(file)->fi_hdir->fd_bbot;
15226+}
1facf9fc 15227+
4a4d8108
AM
15228+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15229+{
15230+ FiMustAnyLock(file);
15231+ AuDebugOn(!au_fi(file)->fi_hdir);
15232+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15233+}
1facf9fc 15234+
5afbbe0d 15235+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15236+{
15237+ FiMustWriteLock(file);
15238+ au_fi(file)->fi_btop = bindex;
15239+}
1facf9fc 15240+
5afbbe0d 15241+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15242+{
15243+ FiMustWriteLock(file);
15244+ AuDebugOn(!au_fi(file)->fi_hdir);
15245+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15246+}
1308ab2a 15247+
4a4d8108
AM
15248+static inline void au_set_fvdir_cache(struct file *file,
15249+ struct au_vdir *vdir_cache)
15250+{
15251+ FiMustWriteLock(file);
15252+ AuDebugOn(!au_fi(file)->fi_hdir);
15253+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15254+}
dece6358 15255+
4a4d8108
AM
15256+static inline struct file *au_hf_top(struct file *file)
15257+{
15258+ FiMustAnyLock(file);
15259+ AuDebugOn(au_fi(file)->fi_hdir);
15260+ return au_fi(file)->fi_htop.hf_file;
15261+}
1facf9fc 15262+
4a4d8108
AM
15263+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15264+{
15265+ FiMustAnyLock(file);
15266+ AuDebugOn(!au_fi(file)->fi_hdir);
15267+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15268+}
15269+
4a4d8108
AM
15270+/* todo: memory barrier? */
15271+static inline unsigned int au_figen(struct file *f)
dece6358 15272+{
4a4d8108
AM
15273+ return atomic_read(&au_fi(f)->fi_generation);
15274+}
dece6358 15275+
2cbb1c4b
JR
15276+static inline void au_set_mmapped(struct file *f)
15277+{
15278+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15279+ return;
0c3ec466 15280+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15281+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15282+ ;
15283+}
15284+
15285+static inline void au_unset_mmapped(struct file *f)
15286+{
15287+ atomic_dec(&au_fi(f)->fi_mmapped);
15288+}
15289+
4a4d8108
AM
15290+static inline int au_test_mmapped(struct file *f)
15291+{
2cbb1c4b
JR
15292+ return atomic_read(&au_fi(f)->fi_mmapped);
15293+}
15294+
15295+/* customize vma->vm_file */
15296+
15297+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15298+ struct file *file)
15299+{
53392da6
AM
15300+ struct file *f;
15301+
15302+ f = vma->vm_file;
2cbb1c4b
JR
15303+ get_file(file);
15304+ vma->vm_file = file;
53392da6 15305+ fput(f);
2cbb1c4b
JR
15306+}
15307+
15308+#ifdef CONFIG_MMU
15309+#define AuDbgVmRegion(file, vma) do {} while (0)
15310+
15311+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15312+ struct file *file)
15313+{
15314+ au_do_vm_file_reset(vma, file);
15315+}
15316+#else
15317+#define AuDbgVmRegion(file, vma) \
15318+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15319+
15320+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15321+ struct file *file)
15322+{
53392da6
AM
15323+ struct file *f;
15324+
2cbb1c4b 15325+ au_do_vm_file_reset(vma, file);
53392da6 15326+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15327+ get_file(file);
15328+ vma->vm_region->vm_file = file;
53392da6 15329+ fput(f);
2cbb1c4b
JR
15330+}
15331+#endif /* CONFIG_MMU */
15332+
15333+/* handle vma->vm_prfile */
fb47a38f 15334+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15335+ struct file *file)
15336+{
2cbb1c4b
JR
15337+ get_file(file);
15338+ vma->vm_prfile = file;
15339+#ifndef CONFIG_MMU
15340+ get_file(file);
15341+ vma->vm_region->vm_prfile = file;
15342+#endif
fb47a38f 15343+}
1308ab2a 15344+
4a4d8108
AM
15345+#endif /* __KERNEL__ */
15346+#endif /* __AUFS_FILE_H__ */
7f207e10 15347diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
eca34b5c 15348--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
016522bc 15349+++ linux/fs/aufs/finfo.c 2020-01-27 10:57:18.172204883 +0100
062440b3 15350@@ -0,0 +1,149 @@
cd7a4cd9 15351+// SPDX-License-Identifier: GPL-2.0
4a4d8108 15352+/*
016522bc 15353+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
15354+ *
15355+ * This program, aufs is free software; you can redistribute it and/or modify
15356+ * it under the terms of the GNU General Public License as published by
15357+ * the Free Software Foundation; either version 2 of the License, or
15358+ * (at your option) any later version.
15359+ *
15360+ * This program is distributed in the hope that it will be useful,
15361+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15362+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15363+ * GNU General Public License for more details.
15364+ *
15365+ * You should have received a copy of the GNU General Public License
523b37e3 15366+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15367+ */
1308ab2a 15368+
4a4d8108
AM
15369+/*
15370+ * file private data
15371+ */
1facf9fc 15372+
4a4d8108 15373+#include "aufs.h"
1facf9fc 15374+
f0c0a007 15375+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15376+{
f0c0a007 15377+ if (execed)
4a4d8108
AM
15378+ allow_write_access(hf->hf_file);
15379+ fput(hf->hf_file);
15380+ hf->hf_file = NULL;
acd2b654 15381+ au_lcnt_dec(&hf->hf_br->br_nfiles);
4a4d8108
AM
15382+ hf->hf_br = NULL;
15383+}
1facf9fc 15384+
4a4d8108
AM
15385+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15386+{
15387+ struct au_finfo *finfo = au_fi(file);
15388+ struct au_hfile *hf;
15389+ struct au_fidir *fidir;
15390+
15391+ fidir = finfo->fi_hdir;
15392+ if (!fidir) {
15393+ AuDebugOn(finfo->fi_btop != bindex);
15394+ hf = &finfo->fi_htop;
15395+ } else
15396+ hf = fidir->fd_hfile + bindex;
15397+
15398+ if (hf && hf->hf_file)
f0c0a007 15399+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15400+ if (val) {
15401+ FiMustWriteLock(file);
b912730e 15402+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15403+ hf->hf_file = val;
2000de60 15404+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15405+ }
4a4d8108 15406+}
1facf9fc 15407+
4a4d8108
AM
15408+void au_update_figen(struct file *file)
15409+{
2000de60 15410+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15411+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15412+}
15413+
4a4d8108
AM
15414+/* ---------------------------------------------------------------------- */
15415+
4a4d8108
AM
15416+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15417+{
15418+ struct au_fidir *fidir;
15419+ int nbr;
15420+
5afbbe0d 15421+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15422+ if (nbr < 2)
15423+ nbr = 2; /* initial allocate for 2 branches */
15424+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15425+ if (fidir) {
15426+ fidir->fd_bbot = -1;
15427+ fidir->fd_nent = nbr;
4a4d8108
AM
15428+ }
15429+
15430+ return fidir;
15431+}
15432+
e2f27e51 15433+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15434+{
15435+ int err;
15436+ struct au_fidir *fidir, *p;
15437+
15438+ AuRwMustWriteLock(&finfo->fi_rwsem);
15439+ fidir = finfo->fi_hdir;
15440+ AuDebugOn(!fidir);
15441+
15442+ err = -ENOMEM;
15443+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15444+ GFP_NOFS, may_shrink);
4a4d8108
AM
15445+ if (p) {
15446+ p->fd_nent = nbr;
15447+ finfo->fi_hdir = p;
15448+ err = 0;
15449+ }
1facf9fc 15450+
dece6358 15451+ return err;
1facf9fc 15452+}
1308ab2a 15453+
15454+/* ---------------------------------------------------------------------- */
15455+
1c60b727 15456+void au_finfo_fin(struct file *file)
1308ab2a 15457+{
4a4d8108
AM
15458+ struct au_finfo *finfo;
15459+
acd2b654 15460+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
7f207e10 15461+
4a4d8108
AM
15462+ finfo = au_fi(file);
15463+ AuDebugOn(finfo->fi_hdir);
15464+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15465+ au_cache_free_finfo(finfo);
1308ab2a 15466+}
1308ab2a 15467+
e49829fe 15468+void au_fi_init_once(void *_finfo)
4a4d8108 15469+{
e49829fe 15470+ struct au_finfo *finfo = _finfo;
1308ab2a 15471+
e49829fe 15472+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15473+}
1308ab2a 15474+
4a4d8108
AM
15475+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15476+{
1716fcea 15477+ int err;
4a4d8108
AM
15478+ struct au_finfo *finfo;
15479+ struct dentry *dentry;
15480+
15481+ err = -ENOMEM;
2000de60 15482+ dentry = file->f_path.dentry;
4a4d8108
AM
15483+ finfo = au_cache_alloc_finfo();
15484+ if (unlikely(!finfo))
15485+ goto out;
15486+
15487+ err = 0;
acd2b654 15488+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
4a4d8108
AM
15489+ au_rw_write_lock(&finfo->fi_rwsem);
15490+ finfo->fi_btop = -1;
15491+ finfo->fi_hdir = fidir;
15492+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15493+ /* smp_mb(); */ /* atomic_set */
15494+
15495+ file->private_data = finfo;
15496+
15497+out:
15498+ return err;
15499+}
7f207e10 15500diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
eca34b5c 15501--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
016522bc 15502+++ linux/fs/aufs/f_op.c 2020-01-27 10:57:18.172204883 +0100
acd2b654 15503@@ -0,0 +1,819 @@
cd7a4cd9 15504+// SPDX-License-Identifier: GPL-2.0
dece6358 15505+/*
016522bc 15506+ * Copyright (C) 2005-2020 Junjiro R. Okajima
dece6358
AM
15507+ *
15508+ * This program, aufs is free software; you can redistribute it and/or modify
15509+ * it under the terms of the GNU General Public License as published by
15510+ * the Free Software Foundation; either version 2 of the License, or
15511+ * (at your option) any later version.
15512+ *
15513+ * This program is distributed in the hope that it will be useful,
15514+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15515+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15516+ * GNU General Public License for more details.
15517+ *
15518+ * You should have received a copy of the GNU General Public License
523b37e3 15519+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15520+ */
1facf9fc 15521+
15522+/*
4a4d8108 15523+ * file and vm operations
1facf9fc 15524+ */
dece6358 15525+
86dc4139 15526+#include <linux/aio.h>
4a4d8108
AM
15527+#include <linux/fs_stack.h>
15528+#include <linux/mman.h>
4a4d8108 15529+#include <linux/security.h>
dece6358
AM
15530+#include "aufs.h"
15531+
b912730e 15532+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15533+{
4a4d8108
AM
15534+ int err;
15535+ aufs_bindex_t bindex;
8cdd5066 15536+ struct dentry *dentry, *h_dentry;
4a4d8108 15537+ struct au_finfo *finfo;
38d290e6 15538+ struct inode *h_inode;
4a4d8108
AM
15539+
15540+ FiMustWriteLock(file);
15541+
523b37e3 15542+ err = 0;
2000de60 15543+ dentry = file->f_path.dentry;
b912730e 15544+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15545+ finfo = au_fi(file);
15546+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15547+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15548+ bindex = au_dbtop(dentry);
8cdd5066
JR
15549+ if (!h_file) {
15550+ h_dentry = au_h_dptr(dentry, bindex);
15551+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15552+ if (unlikely(err))
15553+ goto out;
b912730e 15554+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
acd2b654
AM
15555+ if (IS_ERR(h_file)) {
15556+ err = PTR_ERR(h_file);
15557+ goto out;
15558+ }
8cdd5066
JR
15559+ } else {
15560+ h_dentry = h_file->f_path.dentry;
15561+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15562+ if (unlikely(err))
15563+ goto out;
acd2b654 15564+ /* br ref is already inc-ed */
8cdd5066 15565+ }
acd2b654
AM
15566+
15567+ if ((flags & __O_TMPFILE)
15568+ && !(flags & O_EXCL)) {
15569+ h_inode = file_inode(h_file);
15570+ spin_lock(&h_inode->i_lock);
15571+ h_inode->i_state |= I_LINKABLE;
15572+ spin_unlock(&h_inode->i_lock);
4a4d8108 15573+ }
acd2b654
AM
15574+ au_set_fbtop(file, bindex);
15575+ au_set_h_fptr(file, bindex, h_file);
15576+ au_update_figen(file);
15577+ /* todo: necessary? */
15578+ /* file->f_ra = h_file->f_ra; */
027c5e7a 15579+
8cdd5066 15580+out:
4a4d8108 15581+ return err;
1facf9fc 15582+}
15583+
4a4d8108
AM
15584+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15585+ struct file *file)
1facf9fc 15586+{
4a4d8108 15587+ int err;
1308ab2a 15588+ struct super_block *sb;
b912730e
AM
15589+ struct au_do_open_args args = {
15590+ .open = au_do_open_nondir
15591+ };
1facf9fc 15592+
523b37e3
AM
15593+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15594+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15595+
2000de60 15596+ sb = file->f_path.dentry->d_sb;
4a4d8108 15597+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15598+ err = au_do_open(file, &args);
4a4d8108
AM
15599+ si_read_unlock(sb);
15600+ return err;
15601+}
1facf9fc 15602+
4a4d8108
AM
15603+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15604+{
15605+ struct au_finfo *finfo;
15606+ aufs_bindex_t bindex;
1facf9fc 15607+
4a4d8108 15608+ finfo = au_fi(file);
8b6a4947
AM
15609+ au_hbl_del(&finfo->fi_hlist,
15610+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15611+ bindex = finfo->fi_btop;
b4510431 15612+ if (bindex >= 0)
4a4d8108 15613+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15614+
1c60b727 15615+ au_finfo_fin(file);
4a4d8108 15616+ return 0;
1facf9fc 15617+}
15618+
4a4d8108
AM
15619+/* ---------------------------------------------------------------------- */
15620+
15621+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15622+{
1308ab2a 15623+ int err;
4a4d8108
AM
15624+ struct file *h_file;
15625+
15626+ err = 0;
15627+ h_file = au_hf_top(file);
15628+ if (h_file)
15629+ err = vfsub_flush(h_file, id);
15630+ return err;
15631+}
15632+
15633+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15634+{
15635+ return au_do_flush(file, id, au_do_flush_nondir);
15636+}
15637+
15638+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15639+/*
15640+ * read and write functions acquire [fdi]_rwsem once, but release before
15641+ * mmap_sem. This is because to stop a race condition between mmap(2).
acd2b654 15642+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
9dbd164d
AM
15643+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15644+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15645+ */
4a4d8108 15646+
b912730e 15647+/* Callers should call au_read_post() or fput() in the end */
521ced18 15648+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15649+{
4a4d8108 15650+ struct file *h_file;
b912730e 15651+ int err;
1facf9fc 15652+
521ced18 15653+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15654+ if (!err) {
15655+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15656+ h_file = au_hf_top(file);
15657+ get_file(h_file);
15658+ if (!keep_fi)
15659+ fi_read_unlock(file);
15660+ } else
15661+ h_file = ERR_PTR(err);
15662+
15663+ return h_file;
15664+}
15665+
15666+static void au_read_post(struct inode *inode, struct file *h_file)
15667+{
15668+ /* update without lock, I don't think it a problem */
15669+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15670+ fput(h_file);
15671+}
15672+
15673+struct au_write_pre {
521ced18
JR
15674+ /* input */
15675+ unsigned int lsc;
15676+
15677+ /* output */
b912730e 15678+ blkcnt_t blks;
5afbbe0d 15679+ aufs_bindex_t btop;
b912730e
AM
15680+};
15681+
15682+/*
15683+ * return with iinfo is write-locked
15684+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15685+ * end
15686+ */
15687+static struct file *au_write_pre(struct file *file, int do_ready,
15688+ struct au_write_pre *wpre)
15689+{
15690+ struct file *h_file;
15691+ struct dentry *dentry;
15692+ int err;
521ced18 15693+ unsigned int lsc;
b912730e
AM
15694+ struct au_pin pin;
15695+
521ced18
JR
15696+ lsc = 0;
15697+ if (wpre)
15698+ lsc = wpre->lsc;
15699+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15700+ h_file = ERR_PTR(err);
dece6358
AM
15701+ if (unlikely(err))
15702+ goto out;
1facf9fc 15703+
b912730e
AM
15704+ dentry = file->f_path.dentry;
15705+ if (do_ready) {
15706+ err = au_ready_to_write(file, -1, &pin);
15707+ if (unlikely(err)) {
15708+ h_file = ERR_PTR(err);
15709+ di_write_unlock(dentry);
15710+ goto out_fi;
15711+ }
15712+ }
15713+
15714+ di_downgrade_lock(dentry, /*flags*/0);
15715+ if (wpre)
5afbbe0d 15716+ wpre->btop = au_fbtop(file);
4a4d8108 15717+ h_file = au_hf_top(file);
9dbd164d 15718+ get_file(h_file);
b912730e
AM
15719+ if (wpre)
15720+ wpre->blks = file_inode(h_file)->i_blocks;
15721+ if (do_ready)
15722+ au_unpin(&pin);
15723+ di_read_unlock(dentry, /*flags*/0);
15724+
15725+out_fi:
15726+ fi_write_unlock(file);
15727+out:
15728+ return h_file;
15729+}
15730+
15731+static void au_write_post(struct inode *inode, struct file *h_file,
15732+ struct au_write_pre *wpre, ssize_t written)
15733+{
15734+ struct inode *h_inode;
15735+
15736+ au_cpup_attr_timesizes(inode);
5afbbe0d 15737+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15738+ h_inode = file_inode(h_file);
15739+ inode->i_mode = h_inode->i_mode;
15740+ ii_write_unlock(inode);
b912730e
AM
15741+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15742+ if (written > 0)
5afbbe0d 15743+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15744+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15745+ fput(h_file);
b912730e
AM
15746+}
15747+
15748+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15749+ loff_t *ppos)
15750+{
15751+ ssize_t err;
15752+ struct inode *inode;
15753+ struct file *h_file;
15754+ struct super_block *sb;
15755+
15756+ inode = file_inode(file);
15757+ sb = inode->i_sb;
15758+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15759+
521ced18 15760+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15761+ err = PTR_ERR(h_file);
15762+ if (IS_ERR(h_file))
15763+ goto out;
9dbd164d
AM
15764+
15765+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
15766+ err = vfsub_read_u(h_file, buf, count, ppos);
15767+ /* todo: necessary? */
15768+ /* file->f_ra = h_file->f_ra; */
b912730e 15769+ au_read_post(inode, h_file);
1308ab2a 15770+
4f0767ce 15771+out:
dece6358
AM
15772+ si_read_unlock(sb);
15773+ return err;
15774+}
1facf9fc 15775+
e49829fe
JR
15776+/*
15777+ * todo: very ugly
15778+ * it locks both of i_mutex and si_rwsem for read in safe.
15779+ * if the plink maintenance mode continues forever (that is the problem),
15780+ * may loop forever.
15781+ */
15782+static void au_mtx_and_read_lock(struct inode *inode)
15783+{
15784+ int err;
15785+ struct super_block *sb = inode->i_sb;
15786+
15787+ while (1) {
febd17d6 15788+ inode_lock(inode);
e49829fe
JR
15789+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15790+ if (!err)
15791+ break;
febd17d6 15792+ inode_unlock(inode);
e49829fe
JR
15793+ si_read_lock(sb, AuLock_NOPLMW);
15794+ si_read_unlock(sb);
15795+ }
15796+}
15797+
4a4d8108
AM
15798+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15799+ size_t count, loff_t *ppos)
dece6358 15800+{
4a4d8108 15801+ ssize_t err;
b912730e
AM
15802+ struct au_write_pre wpre;
15803+ struct inode *inode;
4a4d8108
AM
15804+ struct file *h_file;
15805+ char __user *buf = (char __user *)ubuf;
1facf9fc 15806+
b912730e 15807+ inode = file_inode(file);
e49829fe 15808+ au_mtx_and_read_lock(inode);
1facf9fc 15809+
521ced18 15810+ wpre.lsc = 0;
b912730e
AM
15811+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15812+ err = PTR_ERR(h_file);
15813+ if (IS_ERR(h_file))
9dbd164d 15814+ goto out;
9dbd164d 15815+
4a4d8108 15816+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 15817+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15818+
4f0767ce 15819+out:
b912730e 15820+ si_read_unlock(inode->i_sb);
febd17d6 15821+ inode_unlock(inode);
dece6358
AM
15822+ return err;
15823+}
1facf9fc 15824+
076b876e
AM
15825+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15826+ struct iov_iter *iov_iter)
dece6358 15827+{
4a4d8108
AM
15828+ ssize_t err;
15829+ struct file *file;
076b876e 15830+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15831+
4a4d8108
AM
15832+ err = security_file_permission(h_file, rw);
15833+ if (unlikely(err))
15834+ goto out;
1facf9fc 15835+
43982f53 15836+ err = -ENOSYS; /* the branch doesn't have its ->(read|write)_iter() */
076b876e 15837+ iter = NULL;
5527c038 15838+ if (rw == MAY_READ)
076b876e 15839+ iter = h_file->f_op->read_iter;
5527c038 15840+ else if (rw == MAY_WRITE)
076b876e 15841+ iter = h_file->f_op->write_iter;
076b876e
AM
15842+
15843+ file = kio->ki_filp;
15844+ kio->ki_filp = h_file;
15845+ if (iter) {
2cbb1c4b 15846+ lockdep_off();
076b876e
AM
15847+ err = iter(kio, iov_iter);
15848+ lockdep_on();
4a4d8108
AM
15849+ } else
15850+ /* currently there is no such fs */
15851+ WARN_ON_ONCE(1);
076b876e 15852+ kio->ki_filp = file;
1facf9fc 15853+
4f0767ce 15854+out:
dece6358
AM
15855+ return err;
15856+}
1facf9fc 15857+
076b876e 15858+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15859+{
4a4d8108
AM
15860+ ssize_t err;
15861+ struct file *file, *h_file;
b912730e 15862+ struct inode *inode;
dece6358 15863+ struct super_block *sb;
1facf9fc 15864+
4a4d8108 15865+ file = kio->ki_filp;
b912730e
AM
15866+ inode = file_inode(file);
15867+ sb = inode->i_sb;
e49829fe 15868+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15869+
521ced18 15870+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15871+ err = PTR_ERR(h_file);
15872+ if (IS_ERR(h_file))
15873+ goto out;
9dbd164d 15874+
5afbbe0d
AM
15875+ if (au_test_loopback_kthread()) {
15876+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15877+ if (file->f_mapping != h_file->f_mapping) {
15878+ file->f_mapping = h_file->f_mapping;
15879+ smp_mb(); /* unnecessary? */
15880+ }
15881+ }
15882+ fi_read_unlock(file);
15883+
076b876e 15884+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15885+ /* todo: necessary? */
15886+ /* file->f_ra = h_file->f_ra; */
b912730e 15887+ au_read_post(inode, h_file);
1facf9fc 15888+
4f0767ce 15889+out:
4a4d8108 15890+ si_read_unlock(sb);
1308ab2a 15891+ return err;
15892+}
1facf9fc 15893+
076b876e 15894+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15895+{
4a4d8108 15896+ ssize_t err;
b912730e
AM
15897+ struct au_write_pre wpre;
15898+ struct inode *inode;
4a4d8108 15899+ struct file *file, *h_file;
1308ab2a 15900+
4a4d8108 15901+ file = kio->ki_filp;
b912730e 15902+ inode = file_inode(file);
e49829fe
JR
15903+ au_mtx_and_read_lock(inode);
15904+
521ced18 15905+ wpre.lsc = 0;
b912730e
AM
15906+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15907+ err = PTR_ERR(h_file);
15908+ if (IS_ERR(h_file))
9dbd164d 15909+ goto out;
9dbd164d 15910+
076b876e 15911+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 15912+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15913+
4f0767ce 15914+out:
b912730e 15915+ si_read_unlock(inode->i_sb);
febd17d6 15916+ inode_unlock(inode);
dece6358 15917+ return err;
1facf9fc 15918+}
15919+
4a4d8108
AM
15920+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15921+ struct pipe_inode_info *pipe, size_t len,
15922+ unsigned int flags)
1facf9fc 15923+{
4a4d8108
AM
15924+ ssize_t err;
15925+ struct file *h_file;
b912730e 15926+ struct inode *inode;
dece6358 15927+ struct super_block *sb;
1facf9fc 15928+
b912730e
AM
15929+ inode = file_inode(file);
15930+ sb = inode->i_sb;
e49829fe 15931+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 15932+
521ced18 15933+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15934+ err = PTR_ERR(h_file);
15935+ if (IS_ERR(h_file))
dece6358 15936+ goto out;
1facf9fc 15937+
4a4d8108 15938+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
acd2b654 15939+ /* todo: necessary? */
4a4d8108 15940+ /* file->f_ra = h_file->f_ra; */
b912730e 15941+ au_read_post(inode, h_file);
1facf9fc 15942+
4f0767ce 15943+out:
4a4d8108 15944+ si_read_unlock(sb);
dece6358 15945+ return err;
1facf9fc 15946+}
15947+
4a4d8108
AM
15948+static ssize_t
15949+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15950+ size_t len, unsigned int flags)
1facf9fc 15951+{
4a4d8108 15952+ ssize_t err;
b912730e
AM
15953+ struct au_write_pre wpre;
15954+ struct inode *inode;
076b876e 15955+ struct file *h_file;
1facf9fc 15956+
b912730e 15957+ inode = file_inode(file);
e49829fe 15958+ au_mtx_and_read_lock(inode);
9dbd164d 15959+
521ced18 15960+ wpre.lsc = 0;
b912730e
AM
15961+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15962+ err = PTR_ERR(h_file);
15963+ if (IS_ERR(h_file))
9dbd164d 15964+ goto out;
9dbd164d 15965+
4a4d8108 15966+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 15967+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15968+
4f0767ce 15969+out:
b912730e 15970+ si_read_unlock(inode->i_sb);
febd17d6 15971+ inode_unlock(inode);
4a4d8108
AM
15972+ return err;
15973+}
1facf9fc 15974+
38d290e6
JR
15975+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15976+ loff_t len)
15977+{
15978+ long err;
b912730e 15979+ struct au_write_pre wpre;
38d290e6
JR
15980+ struct inode *inode;
15981+ struct file *h_file;
15982+
b912730e 15983+ inode = file_inode(file);
38d290e6
JR
15984+ au_mtx_and_read_lock(inode);
15985+
521ced18 15986+ wpre.lsc = 0;
b912730e
AM
15987+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15988+ err = PTR_ERR(h_file);
15989+ if (IS_ERR(h_file))
38d290e6 15990+ goto out;
38d290e6
JR
15991+
15992+ lockdep_off();
03673fb0 15993+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 15994+ lockdep_on();
b912730e 15995+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
15996+
15997+out:
b912730e 15998+ si_read_unlock(inode->i_sb);
febd17d6 15999+ inode_unlock(inode);
38d290e6
JR
16000+ return err;
16001+}
16002+
521ced18
JR
16003+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16004+ struct file *dst, loff_t dst_pos,
16005+ size_t len, unsigned int flags)
16006+{
16007+ ssize_t err;
16008+ struct au_write_pre wpre;
16009+ enum { SRC, DST };
16010+ struct {
16011+ struct inode *inode;
16012+ struct file *h_file;
16013+ struct super_block *h_sb;
16014+ } a[2];
16015+#define a_src a[SRC]
16016+#define a_dst a[DST]
16017+
16018+ err = -EINVAL;
16019+ a_src.inode = file_inode(src);
16020+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16021+ goto out;
16022+ a_dst.inode = file_inode(dst);
16023+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16024+ goto out;
16025+
16026+ au_mtx_and_read_lock(a_dst.inode);
16027+ /*
16028+ * in order to match the order in di_write_lock2_{child,parent}(),
acd2b654 16029+ * use f_path.dentry for this comparison.
521ced18
JR
16030+ */
16031+ if (src->f_path.dentry < dst->f_path.dentry) {
16032+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16033+ err = PTR_ERR(a_src.h_file);
16034+ if (IS_ERR(a_src.h_file))
16035+ goto out_si;
16036+
16037+ wpre.lsc = AuLsc_FI_2;
16038+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16039+ err = PTR_ERR(a_dst.h_file);
16040+ if (IS_ERR(a_dst.h_file)) {
16041+ au_read_post(a_src.inode, a_src.h_file);
16042+ goto out_si;
16043+ }
16044+ } else {
16045+ wpre.lsc = AuLsc_FI_1;
16046+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16047+ err = PTR_ERR(a_dst.h_file);
16048+ if (IS_ERR(a_dst.h_file))
16049+ goto out_si;
16050+
16051+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16052+ err = PTR_ERR(a_src.h_file);
16053+ if (IS_ERR(a_src.h_file)) {
16054+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16055+ /*written*/0);
16056+ goto out_si;
16057+ }
16058+ }
16059+
16060+ err = -EXDEV;
16061+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16062+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16063+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16064+ AuDbgFile(src);
16065+ AuDbgFile(dst);
16066+ goto out_file;
16067+ }
16068+
16069+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16070+ dst_pos, len, flags);
16071+
16072+out_file:
16073+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16074+ fi_read_unlock(src);
16075+ au_read_post(a_src.inode, a_src.h_file);
16076+out_si:
16077+ si_read_unlock(a_dst.inode->i_sb);
16078+ inode_unlock(a_dst.inode);
16079+out:
16080+ return err;
16081+#undef a_src
16082+#undef a_dst
16083+}
16084+
4a4d8108
AM
16085+/* ---------------------------------------------------------------------- */
16086+
9dbd164d
AM
16087+/*
16088+ * The locking order around current->mmap_sem.
16089+ * - in most and regular cases
16090+ * file I/O syscall -- aufs_read() or something
16091+ * -- si_rwsem for read -- mmap_sem
16092+ * (Note that [fdi]i_rwsem are released before mmap_sem).
16093+ * - in mmap case
16094+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
acd2b654
AM
16095+ * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
16096+ * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16097+ * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
9dbd164d
AM
16098+ * It means that when aufs acquires si_rwsem for write, the process should never
16099+ * acquire mmap_sem.
16100+ *
392086de 16101+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
16102+ * problem either since any directory is not able to be mmap-ed.
16103+ * The similar scenario is applied to aufs_readlink() too.
16104+ */
16105+
38d290e6 16106+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16107+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16108+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16109+
16110+static unsigned long au_arch_prot_conv(unsigned long flags)
16111+{
16112+ /* currently ppc64 only */
16113+#ifdef CONFIG_PPC64
16114+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16115+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16116+ return AuConv_VM_PROT(flags, SAO);
16117+#else
16118+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16119+ return 0;
16120+#endif
16121+}
16122+
16123+static unsigned long au_prot_conv(unsigned long flags)
16124+{
16125+ return AuConv_VM_PROT(flags, READ)
16126+ | AuConv_VM_PROT(flags, WRITE)
16127+ | AuConv_VM_PROT(flags, EXEC)
16128+ | au_arch_prot_conv(flags);
16129+}
16130+
16131+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16132+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16133+
16134+static unsigned long au_flag_conv(unsigned long flags)
16135+{
16136+ return AuConv_VM_MAP(flags, GROWSDOWN)
16137+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16138+ | AuConv_VM_MAP(flags, LOCKED);
16139+}
38d290e6 16140+#endif
2dfbb274 16141+
9dbd164d 16142+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16143+{
4a4d8108 16144+ int err;
4a4d8108 16145+ const unsigned char wlock
9dbd164d 16146+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16147+ struct super_block *sb;
9dbd164d 16148+ struct file *h_file;
b912730e 16149+ struct inode *inode;
9dbd164d
AM
16150+
16151+ AuDbgVmRegion(file, vma);
1308ab2a 16152+
b912730e
AM
16153+ inode = file_inode(file);
16154+ sb = inode->i_sb;
9dbd164d 16155+ lockdep_off();
e49829fe 16156+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16157+
b912730e 16158+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16159+ lockdep_on();
b912730e
AM
16160+ err = PTR_ERR(h_file);
16161+ if (IS_ERR(h_file))
16162+ goto out;
1308ab2a 16163+
b912730e
AM
16164+ err = 0;
16165+ au_set_mmapped(file);
9dbd164d 16166+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16167+ /*
16168+ * we cannot call security_mmap_file() here since it may acquire
16169+ * mmap_sem or i_mutex.
16170+ *
16171+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16172+ * au_flag_conv(vma->vm_flags));
16173+ */
9dbd164d 16174+ if (!err)
521ced18 16175+ err = call_mmap(h_file, vma);
b912730e
AM
16176+ if (!err) {
16177+ au_vm_prfile_set(vma, file);
16178+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16179+ goto out_fput; /* success */
16180+ }
2cbb1c4b
JR
16181+ au_unset_mmapped(file);
16182+ au_vm_file_reset(vma, file);
b912730e 16183+
2cbb1c4b 16184+out_fput:
9dbd164d 16185+ lockdep_off();
b912730e
AM
16186+ ii_write_unlock(inode);
16187+ lockdep_on();
16188+ fput(h_file);
4f0767ce 16189+out:
b912730e 16190+ lockdep_off();
9dbd164d
AM
16191+ si_read_unlock(sb);
16192+ lockdep_on();
16193+ AuTraceErr(err);
4a4d8108
AM
16194+ return err;
16195+}
16196+
16197+/* ---------------------------------------------------------------------- */
16198+
1e00d052
AM
16199+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16200+ int datasync)
4a4d8108
AM
16201+{
16202+ int err;
b912730e 16203+ struct au_write_pre wpre;
4a4d8108
AM
16204+ struct inode *inode;
16205+ struct file *h_file;
4a4d8108
AM
16206+
16207+ err = 0; /* -EBADF; */ /* posix? */
16208+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16209+ goto out;
4a4d8108 16210+
b912730e
AM
16211+ inode = file_inode(file);
16212+ au_mtx_and_read_lock(inode);
16213+
521ced18 16214+ wpre.lsc = 0;
b912730e
AM
16215+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16216+ err = PTR_ERR(h_file);
16217+ if (IS_ERR(h_file))
4a4d8108 16218+ goto out_unlock;
4a4d8108 16219+
53392da6 16220+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16221+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16222+
4f0767ce 16223+out_unlock:
b912730e 16224+ si_read_unlock(inode->i_sb);
febd17d6 16225+ inode_unlock(inode);
b912730e 16226+out:
4a4d8108 16227+ return err;
dece6358
AM
16228+}
16229+
4a4d8108 16230+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16231+{
4a4d8108
AM
16232+ int err;
16233+ struct file *h_file;
4a4d8108 16234+ struct super_block *sb;
1308ab2a 16235+
b912730e 16236+ sb = file->f_path.dentry->d_sb;
e49829fe 16237+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16238+
521ced18 16239+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16240+ err = PTR_ERR(h_file);
16241+ if (IS_ERR(h_file))
4a4d8108
AM
16242+ goto out;
16243+
523b37e3 16244+ if (h_file->f_op->fasync)
4a4d8108 16245+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16246+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16247+
4f0767ce 16248+out:
4a4d8108 16249+ si_read_unlock(sb);
1308ab2a 16250+ return err;
dece6358 16251+}
4a4d8108 16252+
febd17d6
JR
16253+static int aufs_setfl(struct file *file, unsigned long arg)
16254+{
16255+ int err;
16256+ struct file *h_file;
16257+ struct super_block *sb;
16258+
16259+ sb = file->f_path.dentry->d_sb;
16260+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16261+
521ced18 16262+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16263+ err = PTR_ERR(h_file);
16264+ if (IS_ERR(h_file))
16265+ goto out;
16266+
1c60b727
AM
16267+ /* stop calling h_file->fasync */
16268+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16269+ err = setfl(/*unused fd*/-1, h_file, arg);
16270+ fput(h_file); /* instead of au_read_post() */
16271+
16272+out:
16273+ si_read_unlock(sb);
16274+ return err;
16275+}
16276+
4a4d8108
AM
16277+/* ---------------------------------------------------------------------- */
16278+
16279+/* no one supports this operation, currently */
43982f53 16280+#if 0 /* reserved for future use */
4a4d8108 16281+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16282+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16283+{
16284+}
16285+#endif
16286+
16287+/* ---------------------------------------------------------------------- */
16288+
16289+const struct file_operations aufs_file_fop = {
16290+ .owner = THIS_MODULE,
2cbb1c4b 16291+
027c5e7a 16292+ .llseek = default_llseek,
4a4d8108
AM
16293+
16294+ .read = aufs_read,
16295+ .write = aufs_write,
076b876e
AM
16296+ .read_iter = aufs_read_iter,
16297+ .write_iter = aufs_write_iter,
16298+
4a4d8108
AM
16299+#ifdef CONFIG_AUFS_POLL
16300+ .poll = aufs_poll,
16301+#endif
16302+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16303+#ifdef CONFIG_COMPAT
c2b27bf2 16304+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16305+#endif
4a4d8108
AM
16306+ .mmap = aufs_mmap,
16307+ .open = aufs_open_nondir,
16308+ .flush = aufs_flush_nondir,
16309+ .release = aufs_release_nondir,
16310+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16311+ .fasync = aufs_fasync,
16312+ /* .sendpage = aufs_sendpage, */
febd17d6 16313+ .setfl = aufs_setfl,
4a4d8108
AM
16314+ .splice_write = aufs_splice_write,
16315+ .splice_read = aufs_splice_read,
43982f53 16316+#if 0 /* reserved for future use */
4a4d8108 16317+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16318+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16319+#endif
521ced18
JR
16320+ .fallocate = aufs_fallocate,
16321+ .copy_file_range = aufs_copy_file_range
4a4d8108 16322+};
7f207e10 16323diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
eca34b5c 16324--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
016522bc 16325+++ linux/fs/aufs/fstype.h 2020-01-27 10:57:18.172204883 +0100
062440b3
AM
16326@@ -0,0 +1,401 @@
16327+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 16328+/*
016522bc 16329+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
16330+ *
16331+ * This program, aufs is free software; you can redistribute it and/or modify
16332+ * it under the terms of the GNU General Public License as published by
16333+ * the Free Software Foundation; either version 2 of the License, or
16334+ * (at your option) any later version.
16335+ *
16336+ * This program is distributed in the hope that it will be useful,
16337+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16338+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16339+ * GNU General Public License for more details.
16340+ *
16341+ * You should have received a copy of the GNU General Public License
523b37e3 16342+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16343+ */
16344+
16345+/*
16346+ * judging filesystem type
16347+ */
16348+
16349+#ifndef __AUFS_FSTYPE_H__
16350+#define __AUFS_FSTYPE_H__
16351+
16352+#ifdef __KERNEL__
16353+
16354+#include <linux/fs.h>
16355+#include <linux/magic.h>
b912730e 16356+#include <linux/nfs_fs.h>
b95c5147 16357+#include <linux/romfs_fs.h>
4a4d8108
AM
16358+
16359+static inline int au_test_aufs(struct super_block *sb)
16360+{
16361+ return sb->s_magic == AUFS_SUPER_MAGIC;
16362+}
16363+
16364+static inline const char *au_sbtype(struct super_block *sb)
16365+{
16366+ return sb->s_type->name;
16367+}
1308ab2a 16368+
16369+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16370+{
f0c0a007 16371+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16372+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16373+#else
16374+ return 0;
16375+#endif
16376+}
16377+
1308ab2a 16378+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16379+{
f0c0a007 16380+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16381+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16382+#else
16383+ return 0;
16384+#endif
16385+}
16386+
1308ab2a 16387+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16388+{
f0c0a007 16389+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16390+ return sb->s_magic == CRAMFS_MAGIC;
16391+#endif
16392+ return 0;
16393+}
16394+
16395+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16396+{
f0c0a007 16397+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16398+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16399+#else
16400+ return 0;
16401+#endif
16402+}
16403+
1308ab2a 16404+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16405+{
f0c0a007 16406+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16407+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16408+#else
16409+ return 0;
16410+#endif
16411+}
16412+
1308ab2a 16413+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16414+{
f0c0a007 16415+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16416+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16417+#else
16418+ return 0;
16419+#endif
16420+}
16421+
1308ab2a 16422+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16423+{
1308ab2a 16424+#ifdef CONFIG_TMPFS
16425+ return sb->s_magic == TMPFS_MAGIC;
16426+#else
16427+ return 0;
dece6358 16428+#endif
dece6358
AM
16429+}
16430+
1308ab2a 16431+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16432+{
f0c0a007 16433+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16434+ return !strcmp(au_sbtype(sb), "ecryptfs");
16435+#else
16436+ return 0;
16437+#endif
1facf9fc 16438+}
16439+
1308ab2a 16440+static inline int au_test_ramfs(struct super_block *sb)
16441+{
16442+ return sb->s_magic == RAMFS_MAGIC;
16443+}
16444+
16445+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16446+{
f0c0a007 16447+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16448+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16449+#else
16450+ return 0;
16451+#endif
16452+}
16453+
16454+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16455+{
16456+#ifdef CONFIG_PROC_FS
16457+ return sb->s_magic == PROC_SUPER_MAGIC;
16458+#else
16459+ return 0;
16460+#endif
16461+}
16462+
16463+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16464+{
16465+#ifdef CONFIG_SYSFS
16466+ return sb->s_magic == SYSFS_MAGIC;
16467+#else
16468+ return 0;
16469+#endif
16470+}
16471+
16472+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16473+{
f0c0a007 16474+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16475+ return sb->s_magic == CONFIGFS_MAGIC;
16476+#else
16477+ return 0;
16478+#endif
16479+}
16480+
16481+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16482+{
f0c0a007 16483+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16484+ return sb->s_magic == MINIX3_SUPER_MAGIC
16485+ || sb->s_magic == MINIX2_SUPER_MAGIC
16486+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16487+ || sb->s_magic == MINIX_SUPER_MAGIC
16488+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16489+#else
16490+ return 0;
16491+#endif
16492+}
16493+
1308ab2a 16494+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16495+{
f0c0a007 16496+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16497+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16498+#else
16499+ return 0;
16500+#endif
16501+}
16502+
16503+static inline int au_test_msdos(struct super_block *sb)
16504+{
16505+ return au_test_fat(sb);
16506+}
16507+
16508+static inline int au_test_vfat(struct super_block *sb)
16509+{
16510+ return au_test_fat(sb);
16511+}
16512+
16513+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16514+{
16515+#ifdef CONFIG_SECURITYFS
16516+ return sb->s_magic == SECURITYFS_MAGIC;
16517+#else
16518+ return 0;
16519+#endif
16520+}
16521+
16522+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16523+{
f0c0a007 16524+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16525+ return sb->s_magic == SQUASHFS_MAGIC;
16526+#else
16527+ return 0;
16528+#endif
16529+}
16530+
16531+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16532+{
f0c0a007 16533+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16534+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16535+#else
16536+ return 0;
16537+#endif
16538+}
16539+
16540+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16541+{
f0c0a007 16542+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16543+ return sb->s_magic == XENFS_SUPER_MAGIC;
16544+#else
16545+ return 0;
16546+#endif
16547+}
16548+
16549+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16550+{
16551+#ifdef CONFIG_DEBUG_FS
16552+ return sb->s_magic == DEBUGFS_MAGIC;
16553+#else
16554+ return 0;
16555+#endif
16556+}
16557+
16558+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16559+{
f0c0a007 16560+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16561+ return sb->s_magic == NILFS_SUPER_MAGIC;
16562+#else
16563+ return 0;
16564+#endif
16565+}
16566+
4a4d8108
AM
16567+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16568+{
f0c0a007 16569+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16570+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16571+#else
16572+ return 0;
16573+#endif
16574+}
16575+
1308ab2a 16576+/* ---------------------------------------------------------------------- */
16577+/*
16578+ * they can't be an aufs branch.
16579+ */
16580+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16581+{
16582+ return
16583+#ifndef CONFIG_AUFS_BR_RAMFS
16584+ au_test_ramfs(sb) ||
16585+#endif
16586+ au_test_procfs(sb)
16587+ || au_test_sysfs(sb)
16588+ || au_test_configfs(sb)
16589+ || au_test_debugfs(sb)
16590+ || au_test_securityfs(sb)
16591+ || au_test_xenfs(sb)
16592+ || au_test_ecryptfs(sb)
16593+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16594+ || au_test_aufs(sb); /* will be supported in next version */
16595+}
16596+
1308ab2a 16597+static inline int au_test_fs_remote(struct super_block *sb)
16598+{
16599+ return !au_test_tmpfs(sb)
16600+#ifdef CONFIG_AUFS_BR_RAMFS
16601+ && !au_test_ramfs(sb)
16602+#endif
16603+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16604+}
16605+
16606+/* ---------------------------------------------------------------------- */
16607+
16608+/*
16609+ * Note: these functions (below) are created after reading ->getattr() in all
16610+ * filesystems under linux/fs. it means we have to do so in every update...
16611+ */
16612+
16613+/*
16614+ * some filesystems require getattr to refresh the inode attributes before
16615+ * referencing.
16616+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16617+ * and leave the work for d_revalidate()
16618+ */
16619+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16620+{
16621+ return au_test_nfs(sb)
16622+ || au_test_fuse(sb)
1308ab2a 16623+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16624+ ;
16625+}
16626+
16627+/*
16628+ * filesystems which don't maintain i_size or i_blocks.
16629+ */
16630+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16631+{
16632+ return au_test_xfs(sb)
4a4d8108
AM
16633+ || au_test_btrfs(sb)
16634+ || au_test_ubifs(sb)
16635+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16636+ /* || au_test_minix(sb) */ /* untested */
16637+ ;
16638+}
16639+
16640+/*
16641+ * filesystems which don't store the correct value in some of their inode
16642+ * attributes.
16643+ */
16644+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16645+{
16646+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16647+ || au_test_fat(sb)
16648+ || au_test_msdos(sb)
16649+ || au_test_vfat(sb);
1facf9fc 16650+}
16651+
16652+/* they don't check i_nlink in link(2) */
16653+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16654+{
16655+ return au_test_tmpfs(sb)
16656+#ifdef CONFIG_AUFS_BR_RAMFS
16657+ || au_test_ramfs(sb)
16658+#endif
4a4d8108 16659+ || au_test_ubifs(sb)
4a4d8108 16660+ || au_test_hfsplus(sb);
1facf9fc 16661+}
16662+
16663+/*
16664+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16665+ */
16666+static inline int au_test_fs_notime(struct super_block *sb)
16667+{
16668+ return au_test_nfs(sb)
16669+ || au_test_fuse(sb)
dece6358 16670+ || au_test_ubifs(sb)
1facf9fc 16671+ ;
16672+}
16673+
1facf9fc 16674+/* temporary support for i#1 in cramfs */
16675+static inline int au_test_fs_unique_ino(struct inode *inode)
16676+{
16677+ if (au_test_cramfs(inode->i_sb))
16678+ return inode->i_ino != 1;
16679+ return 1;
16680+}
16681+
16682+/* ---------------------------------------------------------------------- */
16683+
16684+/*
16685+ * the filesystem where the xino files placed must support i/o after unlink and
16686+ * maintain i_size and i_blocks.
16687+ */
16688+static inline int au_test_fs_bad_xino(struct super_block *sb)
16689+{
16690+ return au_test_fs_remote(sb)
16691+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16692+ /* don't want unnecessary work for xino */
16693+ || au_test_aufs(sb)
1308ab2a 16694+ || au_test_ecryptfs(sb)
16695+ || au_test_nilfs(sb);
1facf9fc 16696+}
16697+
16698+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16699+{
16700+ return au_test_tmpfs(sb)
16701+ || au_test_ramfs(sb);
16702+}
16703+
16704+/*
16705+ * test if the @sb is real-readonly.
16706+ */
16707+static inline int au_test_fs_rr(struct super_block *sb)
16708+{
16709+ return au_test_squashfs(sb)
16710+ || au_test_iso9660(sb)
16711+ || au_test_cramfs(sb)
16712+ || au_test_romfs(sb);
16713+}
16714+
b912730e
AM
16715+/*
16716+ * test if the @inode is nfs with 'noacl' option
2121bcd9 16717+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
16718+ */
16719+static inline int au_test_nfs_noacl(struct inode *inode)
16720+{
16721+ return au_test_nfs(inode->i_sb)
16722+ /* && IS_POSIXACL(inode) */
16723+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16724+}
16725+
1facf9fc 16726+#endif /* __KERNEL__ */
16727+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947 16728diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
eca34b5c 16729--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
016522bc 16730+++ linux/fs/aufs/hbl.h 2020-01-27 10:57:18.172204883 +0100
062440b3
AM
16731@@ -0,0 +1,65 @@
16732+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 16733+/*
016522bc 16734+ * Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
16735+ *
16736+ * This program, aufs is free software; you can redistribute it and/or modify
16737+ * it under the terms of the GNU General Public License as published by
16738+ * the Free Software Foundation; either version 2 of the License, or
16739+ * (at your option) any later version.
16740+ *
16741+ * This program is distributed in the hope that it will be useful,
16742+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16743+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16744+ * GNU General Public License for more details.
16745+ *
16746+ * You should have received a copy of the GNU General Public License
16747+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16748+ */
16749+
16750+/*
16751+ * helpers for hlist_bl.h
16752+ */
16753+
16754+#ifndef __AUFS_HBL_H__
16755+#define __AUFS_HBL_H__
16756+
16757+#ifdef __KERNEL__
16758+
16759+#include <linux/list_bl.h>
16760+
16761+static inline void au_hbl_add(struct hlist_bl_node *node,
16762+ struct hlist_bl_head *hbl)
16763+{
16764+ hlist_bl_lock(hbl);
16765+ hlist_bl_add_head(node, hbl);
16766+ hlist_bl_unlock(hbl);
16767+}
16768+
16769+static inline void au_hbl_del(struct hlist_bl_node *node,
16770+ struct hlist_bl_head *hbl)
16771+{
16772+ hlist_bl_lock(hbl);
16773+ hlist_bl_del(node);
16774+ hlist_bl_unlock(hbl);
16775+}
16776+
16777+#define au_hbl_for_each(pos, head) \
16778+ for (pos = hlist_bl_first(head); \
16779+ pos; \
16780+ pos = pos->next)
16781+
16782+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16783+{
16784+ unsigned long cnt;
16785+ struct hlist_bl_node *pos;
16786+
16787+ cnt = 0;
16788+ hlist_bl_lock(hbl);
16789+ au_hbl_for_each(pos, hbl)
16790+ cnt++;
16791+ hlist_bl_unlock(hbl);
16792+ return cnt;
16793+}
16794+
16795+#endif /* __KERNEL__ */
16796+#endif /* __AUFS_HBL_H__ */
7f207e10 16797diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
eca34b5c 16798--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
016522bc 16799+++ linux/fs/aufs/hfsnotify.c 2020-01-27 10:57:18.172204883 +0100
fbc438ed 16800@@ -0,0 +1,288 @@
cd7a4cd9 16801+// SPDX-License-Identifier: GPL-2.0
1facf9fc 16802+/*
016522bc 16803+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 16804+ *
16805+ * This program, aufs is free software; you can redistribute it and/or modify
16806+ * it under the terms of the GNU General Public License as published by
16807+ * the Free Software Foundation; either version 2 of the License, or
16808+ * (at your option) any later version.
dece6358
AM
16809+ *
16810+ * This program is distributed in the hope that it will be useful,
16811+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16812+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16813+ * GNU General Public License for more details.
16814+ *
16815+ * You should have received a copy of the GNU General Public License
523b37e3 16816+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16817+ */
16818+
16819+/*
4a4d8108 16820+ * fsnotify for the lower directories
1facf9fc 16821+ */
16822+
16823+#include "aufs.h"
16824+
4a4d8108
AM
16825+/* FS_IN_IGNORED is unnecessary */
16826+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16827+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16828+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16829+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16830+
0c5527e5 16831+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16832+{
0c5527e5
AM
16833+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16834+ hn_mark);
5afbbe0d 16835+ /* AuDbg("here\n"); */
1c60b727 16836+ au_cache_free_hnotify(hn);
8b6a4947 16837+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16838+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16839+ wake_up(&au_hfsn_wq);
4a4d8108 16840+}
1facf9fc 16841+
027c5e7a 16842+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16843+{
1716fcea 16844+ int err;
027c5e7a
AM
16845+ struct au_hnotify *hn;
16846+ struct super_block *sb;
16847+ struct au_branch *br;
0c5527e5 16848+ struct fsnotify_mark *mark;
027c5e7a 16849+ aufs_bindex_t bindex;
1facf9fc 16850+
027c5e7a
AM
16851+ hn = hinode->hi_notify;
16852+ sb = hn->hn_aufs_inode->i_sb;
16853+ bindex = au_br_index(sb, hinode->hi_id);
16854+ br = au_sbr(sb, bindex);
1716fcea
AM
16855+ AuDebugOn(!br->br_hfsn);
16856+
0c5527e5 16857+ mark = &hn->hn_mark;
ffa93bbd 16858+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16859+ mark->mask = AuHfsnMask;
7f207e10
AM
16860+ /*
16861+ * by udba rename or rmdir, aufs assign a new inode to the known
16862+ * h_inode, so specify 1 to allow dups.
16863+ */
c1595e42 16864+ lockdep_off();
acd2b654 16865+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
c1595e42 16866+ lockdep_on();
1716fcea
AM
16867+
16868+ return err;
1facf9fc 16869+}
16870+
7eafdf33 16871+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16872+{
0c5527e5 16873+ struct fsnotify_mark *mark;
7eafdf33 16874+ unsigned long long ull;
1716fcea 16875+ struct fsnotify_group *group;
7eafdf33
AM
16876+
16877+ ull = atomic64_inc_return(&au_hfsn_ifree);
16878+ BUG_ON(!ull);
953406b4 16879+
0c5527e5 16880+ mark = &hn->hn_mark;
1716fcea
AM
16881+ spin_lock(&mark->lock);
16882+ group = mark->group;
16883+ fsnotify_get_group(group);
16884+ spin_unlock(&mark->lock);
c1595e42 16885+ lockdep_off();
1716fcea 16886+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16887+ fsnotify_put_mark(mark);
1716fcea 16888+ fsnotify_put_group(group);
c1595e42 16889+ lockdep_on();
7f207e10 16890+
7eafdf33
AM
16891+ /* free hn by myself */
16892+ return 0;
1facf9fc 16893+}
16894+
16895+/* ---------------------------------------------------------------------- */
16896+
4a4d8108 16897+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16898+{
0c5527e5 16899+ struct fsnotify_mark *mark;
1facf9fc 16900+
0c5527e5
AM
16901+ mark = &hinode->hi_notify->hn_mark;
16902+ spin_lock(&mark->lock);
1facf9fc 16903+ if (do_set) {
0c5527e5
AM
16904+ AuDebugOn(mark->mask & AuHfsnMask);
16905+ mark->mask |= AuHfsnMask;
1facf9fc 16906+ } else {
0c5527e5
AM
16907+ AuDebugOn(!(mark->mask & AuHfsnMask));
16908+ mark->mask &= ~AuHfsnMask;
1facf9fc 16909+ }
0c5527e5 16910+ spin_unlock(&mark->lock);
4a4d8108 16911+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 16912+}
16913+
4a4d8108 16914+/* ---------------------------------------------------------------------- */
1facf9fc 16915+
4a4d8108
AM
16916+/* #define AuDbgHnotify */
16917+#ifdef AuDbgHnotify
16918+static char *au_hfsn_name(u32 mask)
16919+{
16920+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
16921+#define test_ret(flag) \
16922+ do { \
16923+ if (mask & flag) \
16924+ return #flag; \
16925+ } while (0)
4a4d8108
AM
16926+ test_ret(FS_ACCESS);
16927+ test_ret(FS_MODIFY);
16928+ test_ret(FS_ATTRIB);
16929+ test_ret(FS_CLOSE_WRITE);
16930+ test_ret(FS_CLOSE_NOWRITE);
16931+ test_ret(FS_OPEN);
16932+ test_ret(FS_MOVED_FROM);
16933+ test_ret(FS_MOVED_TO);
16934+ test_ret(FS_CREATE);
16935+ test_ret(FS_DELETE);
16936+ test_ret(FS_DELETE_SELF);
16937+ test_ret(FS_MOVE_SELF);
16938+ test_ret(FS_UNMOUNT);
16939+ test_ret(FS_Q_OVERFLOW);
16940+ test_ret(FS_IN_IGNORED);
b912730e 16941+ test_ret(FS_ISDIR);
4a4d8108
AM
16942+ test_ret(FS_IN_ONESHOT);
16943+ test_ret(FS_EVENT_ON_CHILD);
16944+ return "";
16945+#undef test_ret
16946+#else
16947+ return "??";
16948+#endif
1facf9fc 16949+}
4a4d8108 16950+#endif
1facf9fc 16951+
16952+/* ---------------------------------------------------------------------- */
16953+
1716fcea
AM
16954+static void au_hfsn_free_group(struct fsnotify_group *group)
16955+{
16956+ struct au_br_hfsnotify *hfsn = group->private;
16957+
5afbbe0d 16958+ /* AuDbg("here\n"); */
9f237c51 16959+ au_kfree_try_rcu(hfsn);
1716fcea
AM
16960+}
16961+
4a4d8108 16962+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 16963+ struct inode *inode,
a2654f78 16964+ u32 mask, const void *data, int data_type,
83b672a5 16965+ const struct qstr *file_name, u32 cookie,
ffa93bbd 16966+ struct fsnotify_iter_info *iter_info)
1facf9fc 16967+{
16968+ int err;
4a4d8108
AM
16969+ struct au_hnotify *hnotify;
16970+ struct inode *h_dir, *h_inode;
cd7a4cd9 16971+ struct fsnotify_mark *inode_mark;
4a4d8108 16972+
fb47a38f 16973+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 16974+
16975+ err = 0;
0c5527e5 16976+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 16977+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 16978+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 16979+ goto out;
1facf9fc 16980+
fb47a38f
JR
16981+ h_dir = inode;
16982+ h_inode = NULL;
4a4d8108 16983+#ifdef AuDbgHnotify
392086de 16984+ au_debug_on();
fbc438ed
JR
16985+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16986+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
4a4d8108
AM
16987+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16988+ h_dir->i_ino, mask, au_hfsn_name(mask),
fbc438ed 16989+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
4a4d8108 16990+ /* WARN_ON(1); */
1facf9fc 16991+ }
392086de 16992+ au_debug_off();
1facf9fc 16993+#endif
4a4d8108 16994+
cd7a4cd9 16995+ inode_mark = fsnotify_iter_inode_mark(iter_info);
0c5527e5
AM
16996+ AuDebugOn(!inode_mark);
16997+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
b62f1e65 16998+ err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
1facf9fc 16999+
4a4d8108
AM
17000+out:
17001+ return err;
17002+}
1facf9fc 17003+
4a4d8108 17004+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 17005+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
17006+ .free_group_priv = au_hfsn_free_group,
17007+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
17008+};
17009+
17010+/* ---------------------------------------------------------------------- */
17011+
027c5e7a
AM
17012+static void au_hfsn_fin_br(struct au_branch *br)
17013+{
1716fcea 17014+ struct au_br_hfsnotify *hfsn;
027c5e7a 17015+
1716fcea 17016+ hfsn = br->br_hfsn;
c1595e42
JR
17017+ if (hfsn) {
17018+ lockdep_off();
1716fcea 17019+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
17020+ lockdep_on();
17021+ }
027c5e7a
AM
17022+}
17023+
1716fcea 17024+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
17025+{
17026+ int err;
1716fcea
AM
17027+ struct fsnotify_group *group;
17028+ struct au_br_hfsnotify *hfsn;
1facf9fc 17029+
4a4d8108 17030+ err = 0;
1716fcea
AM
17031+ br->br_hfsn = NULL;
17032+ if (!au_br_hnotifyable(perm))
027c5e7a 17033+ goto out;
027c5e7a 17034+
1716fcea
AM
17035+ err = -ENOMEM;
17036+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17037+ if (unlikely(!hfsn))
027c5e7a
AM
17038+ goto out;
17039+
1716fcea
AM
17040+ err = 0;
17041+ group = fsnotify_alloc_group(&au_hfsn_ops);
17042+ if (IS_ERR(group)) {
17043+ err = PTR_ERR(group);
0c5527e5 17044+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 17045+ goto out_hfsn;
4a4d8108 17046+ }
1facf9fc 17047+
1716fcea
AM
17048+ group->private = hfsn;
17049+ hfsn->hfsn_group = group;
17050+ br->br_hfsn = hfsn;
17051+ goto out; /* success */
17052+
17053+out_hfsn:
9f237c51 17054+ au_kfree_try_rcu(hfsn);
027c5e7a 17055+out:
1716fcea
AM
17056+ return err;
17057+}
17058+
17059+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17060+{
17061+ int err;
17062+
17063+ err = 0;
17064+ if (!br->br_hfsn)
17065+ err = au_hfsn_init_br(br, perm);
17066+
1facf9fc 17067+ return err;
17068+}
17069+
7eafdf33
AM
17070+/* ---------------------------------------------------------------------- */
17071+
17072+static void au_hfsn_fin(void)
17073+{
17074+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17075+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17076+}
17077+
4a4d8108
AM
17078+const struct au_hnotify_op au_hnotify_op = {
17079+ .ctl = au_hfsn_ctl,
17080+ .alloc = au_hfsn_alloc,
17081+ .free = au_hfsn_free,
1facf9fc 17082+
7eafdf33
AM
17083+ .fin = au_hfsn_fin,
17084+
027c5e7a
AM
17085+ .reset_br = au_hfsn_reset_br,
17086+ .fin_br = au_hfsn_fin_br,
17087+ .init_br = au_hfsn_init_br
4a4d8108 17088+};
7f207e10 17089diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
eca34b5c 17090--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
016522bc 17091+++ linux/fs/aufs/hfsplus.c 2020-01-27 10:57:18.172204883 +0100
acd2b654 17092@@ -0,0 +1,60 @@
cd7a4cd9 17093+// SPDX-License-Identifier: GPL-2.0
4a4d8108 17094+/*
016522bc 17095+ * Copyright (C) 2010-2020 Junjiro R. Okajima
4a4d8108
AM
17096+ *
17097+ * This program, aufs is free software; you can redistribute it and/or modify
17098+ * it under the terms of the GNU General Public License as published by
17099+ * the Free Software Foundation; either version 2 of the License, or
17100+ * (at your option) any later version.
17101+ *
17102+ * This program is distributed in the hope that it will be useful,
17103+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17104+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17105+ * GNU General Public License for more details.
17106+ *
17107+ * You should have received a copy of the GNU General Public License
523b37e3 17108+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17109+ */
1facf9fc 17110+
4a4d8108 17111+/*
acd2b654 17112+ * special support for filesystems which acquires an inode mutex
4a4d8108
AM
17113+ * at final closing a file, eg, hfsplus.
17114+ *
17115+ * This trick is very simple and stupid, just to open the file before really
acd2b654 17116+ * necessary open to tell hfsplus that this is not the final closing.
4a4d8108
AM
17117+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17118+ * and au_h_open_post() after releasing it.
17119+ */
1facf9fc 17120+
4a4d8108 17121+#include "aufs.h"
1facf9fc 17122+
392086de
AM
17123+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17124+ int force_wr)
4a4d8108
AM
17125+{
17126+ struct file *h_file;
17127+ struct dentry *h_dentry;
1facf9fc 17128+
4a4d8108
AM
17129+ h_dentry = au_h_dptr(dentry, bindex);
17130+ AuDebugOn(!h_dentry);
5527c038 17131+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17132+
17133+ h_file = NULL;
17134+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17135+ && d_is_reg(h_dentry))
4a4d8108
AM
17136+ h_file = au_h_open(dentry, bindex,
17137+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17138+ /*file*/NULL, force_wr);
4a4d8108 17139+ return h_file;
1facf9fc 17140+}
17141+
4a4d8108
AM
17142+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17143+ struct file *h_file)
17144+{
acd2b654
AM
17145+ struct au_branch *br;
17146+
4a4d8108
AM
17147+ if (h_file) {
17148+ fput(h_file);
acd2b654
AM
17149+ br = au_sbr(dentry->d_sb, bindex);
17150+ au_lcnt_dec(&br->br_nfiles);
4a4d8108
AM
17151+ }
17152+}
7f207e10 17153diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
eca34b5c 17154--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
016522bc 17155+++ linux/fs/aufs/hnotify.c 2020-01-27 10:57:18.172204883 +0100
43982f53 17156@@ -0,0 +1,715 @@
cd7a4cd9 17157+// SPDX-License-Identifier: GPL-2.0
e49829fe 17158+/*
016522bc 17159+ * Copyright (C) 2005-2020 Junjiro R. Okajima
e49829fe
JR
17160+ *
17161+ * This program, aufs is free software; you can redistribute it and/or modify
17162+ * it under the terms of the GNU General Public License as published by
17163+ * the Free Software Foundation; either version 2 of the License, or
17164+ * (at your option) any later version.
17165+ *
17166+ * This program is distributed in the hope that it will be useful,
17167+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17168+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17169+ * GNU General Public License for more details.
17170+ *
17171+ * You should have received a copy of the GNU General Public License
523b37e3 17172+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17173+ */
17174+
17175+/*
7f207e10 17176+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17177+ */
17178+
eca801bf 17179+/* #include <linux/iversion.h> */
e49829fe
JR
17180+#include "aufs.h"
17181+
027c5e7a 17182+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17183+{
17184+ int err;
7f207e10 17185+ struct au_hnotify *hn;
1facf9fc 17186+
4a4d8108
AM
17187+ err = -ENOMEM;
17188+ hn = au_cache_alloc_hnotify();
17189+ if (hn) {
17190+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17191+ hinode->hi_notify = hn;
17192+ err = au_hnotify_op.alloc(hinode);
17193+ AuTraceErr(err);
17194+ if (unlikely(err)) {
17195+ hinode->hi_notify = NULL;
1c60b727 17196+ au_cache_free_hnotify(hn);
4a4d8108
AM
17197+ /*
17198+ * The upper dir was removed by udba, but the same named
acd2b654 17199+ * dir left. In this case, aufs assigns a new inode
4a4d8108 17200+ * number and set the monitor again.
acd2b654 17201+ * For the lower dir, the old monitor is still left.
4a4d8108
AM
17202+ */
17203+ if (err == -EEXIST)
17204+ err = 0;
17205+ }
1308ab2a 17206+ }
1308ab2a 17207+
027c5e7a 17208+ AuTraceErr(err);
1308ab2a 17209+ return err;
dece6358 17210+}
1facf9fc 17211+
4a4d8108 17212+void au_hn_free(struct au_hinode *hinode)
dece6358 17213+{
4a4d8108 17214+ struct au_hnotify *hn;
1facf9fc 17215+
4a4d8108
AM
17216+ hn = hinode->hi_notify;
17217+ if (hn) {
4a4d8108 17218+ hinode->hi_notify = NULL;
7eafdf33 17219+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17220+ au_cache_free_hnotify(hn);
4a4d8108
AM
17221+ }
17222+}
dece6358 17223+
4a4d8108 17224+/* ---------------------------------------------------------------------- */
dece6358 17225+
4a4d8108
AM
17226+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17227+{
17228+ if (hinode->hi_notify)
17229+ au_hnotify_op.ctl(hinode, do_set);
17230+}
17231+
17232+void au_hn_reset(struct inode *inode, unsigned int flags)
17233+{
5afbbe0d 17234+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17235+ struct inode *hi;
17236+ struct dentry *iwhdentry;
1facf9fc 17237+
5afbbe0d
AM
17238+ bbot = au_ibbot(inode);
17239+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17240+ hi = au_h_iptr(inode, bindex);
17241+ if (!hi)
17242+ continue;
1308ab2a 17243+
febd17d6 17244+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17245+ iwhdentry = au_hi_wh(inode, bindex);
17246+ if (iwhdentry)
17247+ dget(iwhdentry);
17248+ au_igrab(hi);
17249+ au_set_h_iptr(inode, bindex, NULL, 0);
17250+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17251+ flags & ~AuHi_XINO);
17252+ iput(hi);
17253+ dput(iwhdentry);
febd17d6 17254+ /* inode_unlock(hi); */
1facf9fc 17255+ }
1facf9fc 17256+}
17257+
1308ab2a 17258+/* ---------------------------------------------------------------------- */
1facf9fc 17259+
4a4d8108 17260+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17261+{
4a4d8108 17262+ int err;
5afbbe0d 17263+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17264+ struct inode *h_i;
1facf9fc 17265+
4a4d8108
AM
17266+ err = 0;
17267+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17268+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17269+ goto out;
17270+ }
1facf9fc 17271+
4a4d8108 17272+ bfound = -1;
5afbbe0d
AM
17273+ bbot = au_ibbot(inode);
17274+ btop = au_ibtop(inode);
4a4d8108 17275+#if 0 /* reserved for future use */
5afbbe0d 17276+ if (bindex == bbot) {
4a4d8108
AM
17277+ /* keep this ino in rename case */
17278+ goto out;
17279+ }
17280+#endif
5afbbe0d 17281+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17282+ if (au_h_iptr(inode, bindex) == h_inode) {
17283+ bfound = bindex;
17284+ break;
17285+ }
17286+ if (bfound < 0)
1308ab2a 17287+ goto out;
1facf9fc 17288+
5afbbe0d 17289+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17290+ h_i = au_h_iptr(inode, bindex);
17291+ if (!h_i)
17292+ continue;
1facf9fc 17293+
4a4d8108
AM
17294+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17295+ /* ignore this error */
17296+ /* bad action? */
1facf9fc 17297+ }
1facf9fc 17298+
4a4d8108 17299+ /* children inode number will be broken */
1facf9fc 17300+
4f0767ce 17301+out:
4a4d8108
AM
17302+ AuTraceErr(err);
17303+ return err;
1facf9fc 17304+}
17305+
4a4d8108 17306+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17307+{
4a4d8108
AM
17308+ int err, i, j, ndentry;
17309+ struct au_dcsub_pages dpages;
17310+ struct au_dpage *dpage;
17311+ struct dentry **dentries;
1facf9fc 17312+
4a4d8108
AM
17313+ err = au_dpages_init(&dpages, GFP_NOFS);
17314+ if (unlikely(err))
17315+ goto out;
17316+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17317+ if (unlikely(err))
17318+ goto out_dpages;
1facf9fc 17319+
4a4d8108
AM
17320+ for (i = 0; i < dpages.ndpage; i++) {
17321+ dpage = dpages.dpages + i;
17322+ dentries = dpage->dentries;
17323+ ndentry = dpage->ndentry;
17324+ for (j = 0; j < ndentry; j++) {
17325+ struct dentry *d;
17326+
17327+ d = dentries[j];
17328+ if (IS_ROOT(d))
17329+ continue;
17330+
4a4d8108 17331+ au_digen_dec(d);
5527c038 17332+ if (d_really_is_positive(d))
4a4d8108
AM
17333+ /* todo: reset children xino?
17334+ cached children only? */
5527c038 17335+ au_iigen_dec(d_inode(d));
1308ab2a 17336+ }
dece6358 17337+ }
1facf9fc 17338+
4f0767ce 17339+out_dpages:
4a4d8108 17340+ au_dpages_free(&dpages);
4f0767ce 17341+out:
dece6358
AM
17342+ return err;
17343+}
17344+
1308ab2a 17345+/*
4a4d8108 17346+ * return 0 if processed.
1308ab2a 17347+ */
4a4d8108
AM
17348+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17349+ const unsigned int isdir)
dece6358 17350+{
1308ab2a 17351+ int err;
4a4d8108
AM
17352+ struct dentry *d;
17353+ struct qstr *dname;
1facf9fc 17354+
4a4d8108
AM
17355+ err = 1;
17356+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17357+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17358+ err = 0;
17359+ goto out;
17360+ }
dece6358 17361+
4a4d8108
AM
17362+ if (!isdir) {
17363+ AuDebugOn(!name);
17364+ au_iigen_dec(inode);
027c5e7a 17365+ spin_lock(&inode->i_lock);
c1595e42 17366+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17367+ spin_lock(&d->d_lock);
4a4d8108
AM
17368+ dname = &d->d_name;
17369+ if (dname->len != nlen
027c5e7a
AM
17370+ && memcmp(dname->name, name, nlen)) {
17371+ spin_unlock(&d->d_lock);
4a4d8108 17372+ continue;
027c5e7a 17373+ }
4a4d8108 17374+ err = 0;
4a4d8108
AM
17375+ au_digen_dec(d);
17376+ spin_unlock(&d->d_lock);
17377+ break;
1facf9fc 17378+ }
027c5e7a 17379+ spin_unlock(&inode->i_lock);
1308ab2a 17380+ } else {
027c5e7a 17381+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17382+ d = d_find_any_alias(inode);
4a4d8108
AM
17383+ if (!d) {
17384+ au_iigen_dec(inode);
17385+ goto out;
17386+ }
1facf9fc 17387+
027c5e7a 17388+ spin_lock(&d->d_lock);
4a4d8108 17389+ dname = &d->d_name;
027c5e7a
AM
17390+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17391+ spin_unlock(&d->d_lock);
4a4d8108 17392+ err = hn_gen_tree(d);
027c5e7a
AM
17393+ spin_lock(&d->d_lock);
17394+ }
17395+ spin_unlock(&d->d_lock);
4a4d8108
AM
17396+ dput(d);
17397+ }
1facf9fc 17398+
4f0767ce 17399+out:
4a4d8108 17400+ AuTraceErr(err);
1308ab2a 17401+ return err;
17402+}
dece6358 17403+
4a4d8108 17404+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17405+{
4a4d8108 17406+ int err;
1facf9fc 17407+
5527c038 17408+ if (IS_ROOT(dentry)) {
0c3ec466 17409+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17410+ return 0;
17411+ }
1308ab2a 17412+
4a4d8108
AM
17413+ err = 0;
17414+ if (!isdir) {
4a4d8108 17415+ au_digen_dec(dentry);
5527c038
JR
17416+ if (d_really_is_positive(dentry))
17417+ au_iigen_dec(d_inode(dentry));
4a4d8108 17418+ } else {
027c5e7a 17419+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17420+ if (d_really_is_positive(dentry))
4a4d8108
AM
17421+ err = hn_gen_tree(dentry);
17422+ }
17423+
17424+ AuTraceErr(err);
17425+ return err;
1facf9fc 17426+}
17427+
4a4d8108 17428+/* ---------------------------------------------------------------------- */
1facf9fc 17429+
4a4d8108
AM
17430+/* hnotify job flags */
17431+#define AuHnJob_XINO0 1
17432+#define AuHnJob_GEN (1 << 1)
17433+#define AuHnJob_DIRENT (1 << 2)
17434+#define AuHnJob_ISDIR (1 << 3)
17435+#define AuHnJob_TRYXINO0 (1 << 4)
17436+#define AuHnJob_MNTPNT (1 << 5)
17437+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17438+#define au_fset_hnjob(flags, name) \
17439+ do { (flags) |= AuHnJob_##name; } while (0)
17440+#define au_fclr_hnjob(flags, name) \
17441+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17442+
4a4d8108
AM
17443+enum {
17444+ AuHn_CHILD,
17445+ AuHn_PARENT,
17446+ AuHnLast
17447+};
1facf9fc 17448+
4a4d8108
AM
17449+struct au_hnotify_args {
17450+ struct inode *h_dir, *dir, *h_child_inode;
17451+ u32 mask;
17452+ unsigned int flags[AuHnLast];
17453+ unsigned int h_child_nlen;
17454+ char h_child_name[];
17455+};
1facf9fc 17456+
4a4d8108
AM
17457+struct hn_job_args {
17458+ unsigned int flags;
17459+ struct inode *inode, *h_inode, *dir, *h_dir;
17460+ struct dentry *dentry;
17461+ char *h_name;
17462+ int h_nlen;
17463+};
1308ab2a 17464+
4a4d8108
AM
17465+static int hn_job(struct hn_job_args *a)
17466+{
17467+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17468+ int e;
1308ab2a 17469+
4a4d8108
AM
17470+ /* reset xino */
17471+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17472+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17473+
4a4d8108
AM
17474+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17475+ && a->inode
17476+ && a->h_inode) {
be118d29 17477+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17478+ if (!a->h_inode->i_nlink
17479+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17480+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17481+ inode_unlock_shared(a->h_inode);
1308ab2a 17482+ }
1facf9fc 17483+
4a4d8108
AM
17484+ /* make the generation obsolete */
17485+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17486+ e = -1;
4a4d8108 17487+ if (a->inode)
076b876e 17488+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17489+ isdir);
076b876e 17490+ if (e && a->dentry)
4a4d8108
AM
17491+ hn_gen_by_name(a->dentry, isdir);
17492+ /* ignore this error */
1facf9fc 17493+ }
1facf9fc 17494+
4a4d8108
AM
17495+ /* make dir entries obsolete */
17496+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17497+ struct au_vdir *vdir;
1facf9fc 17498+
4a4d8108
AM
17499+ vdir = au_ivdir(a->inode);
17500+ if (vdir)
17501+ vdir->vd_jiffy = 0;
17502+ /* IMustLock(a->inode); */
be118d29 17503+ /* inode_inc_iversion(a->inode); */
4a4d8108 17504+ }
1facf9fc 17505+
4a4d8108
AM
17506+ /* can do nothing but warn */
17507+ if (au_ftest_hnjob(a->flags, MNTPNT)
17508+ && a->dentry
17509+ && d_mountpoint(a->dentry))
523b37e3 17510+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17511+
4a4d8108 17512+ return 0;
1308ab2a 17513+}
1facf9fc 17514+
1308ab2a 17515+/* ---------------------------------------------------------------------- */
1facf9fc 17516+
4a4d8108
AM
17517+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17518+ struct inode *dir)
1308ab2a 17519+{
4a4d8108
AM
17520+ struct dentry *dentry, *d, *parent;
17521+ struct qstr *dname;
1308ab2a 17522+
c1595e42 17523+ parent = d_find_any_alias(dir);
4a4d8108
AM
17524+ if (!parent)
17525+ return NULL;
1308ab2a 17526+
4a4d8108 17527+ dentry = NULL;
027c5e7a 17528+ spin_lock(&parent->d_lock);
c1595e42 17529+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17530+ /* AuDbg("%pd\n", d); */
027c5e7a 17531+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17532+ dname = &d->d_name;
17533+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17534+ goto cont_unlock;
17535+ if (au_di(d))
17536+ au_digen_dec(d);
17537+ else
17538+ goto cont_unlock;
c1595e42 17539+ if (au_dcount(d) > 0) {
027c5e7a 17540+ dentry = dget_dlock(d);
4a4d8108 17541+ spin_unlock(&d->d_lock);
027c5e7a 17542+ break;
dece6358 17543+ }
1facf9fc 17544+
f6b6e03d 17545+cont_unlock:
027c5e7a 17546+ spin_unlock(&d->d_lock);
1308ab2a 17547+ }
027c5e7a 17548+ spin_unlock(&parent->d_lock);
4a4d8108 17549+ dput(parent);
1facf9fc 17550+
4a4d8108
AM
17551+ if (dentry)
17552+ di_write_lock_child(dentry);
1308ab2a 17553+
4a4d8108
AM
17554+ return dentry;
17555+}
dece6358 17556+
4a4d8108
AM
17557+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17558+ aufs_bindex_t bindex, ino_t h_ino)
17559+{
17560+ struct inode *inode;
17561+ ino_t ino;
17562+ int err;
17563+
17564+ inode = NULL;
17565+ err = au_xino_read(sb, bindex, h_ino, &ino);
17566+ if (!err && ino)
17567+ inode = ilookup(sb, ino);
17568+ if (!inode)
17569+ goto out;
17570+
17571+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17572+ pr_warn("wrong root branch\n");
4a4d8108
AM
17573+ iput(inode);
17574+ inode = NULL;
17575+ goto out;
1308ab2a 17576+ }
17577+
4a4d8108 17578+ ii_write_lock_child(inode);
1308ab2a 17579+
4f0767ce 17580+out:
4a4d8108 17581+ return inode;
dece6358
AM
17582+}
17583+
4a4d8108 17584+static void au_hn_bh(void *_args)
1facf9fc 17585+{
4a4d8108
AM
17586+ struct au_hnotify_args *a = _args;
17587+ struct super_block *sb;
5afbbe0d 17588+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17589+ unsigned char xino, try_iput;
1facf9fc 17590+ int err;
1308ab2a 17591+ struct inode *inode;
4a4d8108
AM
17592+ ino_t h_ino;
17593+ struct hn_job_args args;
17594+ struct dentry *dentry;
17595+ struct au_sbinfo *sbinfo;
1facf9fc 17596+
4a4d8108
AM
17597+ AuDebugOn(!_args);
17598+ AuDebugOn(!a->h_dir);
17599+ AuDebugOn(!a->dir);
17600+ AuDebugOn(!a->mask);
17601+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17602+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17603+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17604+
4a4d8108
AM
17605+ inode = NULL;
17606+ dentry = NULL;
17607+ /*
17608+ * do not lock a->dir->i_mutex here
17609+ * because of d_revalidate() may cause a deadlock.
17610+ */
17611+ sb = a->dir->i_sb;
17612+ AuDebugOn(!sb);
17613+ sbinfo = au_sbi(sb);
17614+ AuDebugOn(!sbinfo);
7f207e10 17615+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17616+
8b6a4947
AM
17617+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17618+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17619+ case FS_MOVED_FROM:
17620+ case FS_MOVED_TO:
17621+ AuWarn1("DIRREN with UDBA may not work correctly "
17622+ "for the direct rename(2)\n");
17623+ }
17624+
4a4d8108
AM
17625+ ii_read_lock_parent(a->dir);
17626+ bfound = -1;
5afbbe0d
AM
17627+ bbot = au_ibbot(a->dir);
17628+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17629+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17630+ bfound = bindex;
17631+ break;
17632+ }
17633+ ii_read_unlock(a->dir);
17634+ if (unlikely(bfound < 0))
17635+ goto out;
1facf9fc 17636+
4a4d8108
AM
17637+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17638+ h_ino = 0;
17639+ if (a->h_child_inode)
17640+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17641+
4a4d8108
AM
17642+ if (a->h_child_nlen
17643+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17644+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17645+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17646+ a->dir);
17647+ try_iput = 0;
5527c038
JR
17648+ if (dentry && d_really_is_positive(dentry))
17649+ inode = d_inode(dentry);
4a4d8108
AM
17650+ if (xino && !inode && h_ino
17651+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17652+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17653+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17654+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17655+ try_iput = 1;
f0c0a007 17656+ }
1facf9fc 17657+
4a4d8108
AM
17658+ args.flags = a->flags[AuHn_CHILD];
17659+ args.dentry = dentry;
17660+ args.inode = inode;
17661+ args.h_inode = a->h_child_inode;
17662+ args.dir = a->dir;
17663+ args.h_dir = a->h_dir;
17664+ args.h_name = a->h_child_name;
17665+ args.h_nlen = a->h_child_nlen;
17666+ err = hn_job(&args);
17667+ if (dentry) {
027c5e7a 17668+ if (au_di(dentry))
4a4d8108
AM
17669+ di_write_unlock(dentry);
17670+ dput(dentry);
17671+ }
17672+ if (inode && try_iput) {
17673+ ii_write_unlock(inode);
17674+ iput(inode);
17675+ }
1facf9fc 17676+
4a4d8108
AM
17677+ ii_write_lock_parent(a->dir);
17678+ args.flags = a->flags[AuHn_PARENT];
17679+ args.dentry = NULL;
17680+ args.inode = a->dir;
17681+ args.h_inode = a->h_dir;
17682+ args.dir = NULL;
17683+ args.h_dir = NULL;
17684+ args.h_name = NULL;
17685+ args.h_nlen = 0;
17686+ err = hn_job(&args);
17687+ ii_write_unlock(a->dir);
1facf9fc 17688+
4f0767ce 17689+out:
4a4d8108
AM
17690+ iput(a->h_child_inode);
17691+ iput(a->h_dir);
17692+ iput(a->dir);
027c5e7a
AM
17693+ si_write_unlock(sb);
17694+ au_nwt_done(&sbinfo->si_nowait);
9f237c51 17695+ au_kfree_rcu(a);
dece6358 17696+}
1facf9fc 17697+
4a4d8108
AM
17698+/* ---------------------------------------------------------------------- */
17699+
17700+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
b62f1e65 17701+ const struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17702+{
4a4d8108 17703+ int err, len;
53392da6 17704+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17705+ unsigned char isdir, isroot, wh;
17706+ struct inode *dir;
17707+ struct au_hnotify_args *args;
17708+ char *p, *h_child_name;
dece6358 17709+
1308ab2a 17710+ err = 0;
4a4d8108
AM
17711+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17712+ dir = igrab(hnotify->hn_aufs_inode);
17713+ if (!dir)
17714+ goto out;
1facf9fc 17715+
4a4d8108
AM
17716+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17717+ wh = 0;
17718+ h_child_name = (void *)h_child_qstr->name;
17719+ len = h_child_qstr->len;
17720+ if (h_child_name) {
17721+ if (len > AUFS_WH_PFX_LEN
17722+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17723+ h_child_name += AUFS_WH_PFX_LEN;
17724+ len -= AUFS_WH_PFX_LEN;
17725+ wh = 1;
17726+ }
1facf9fc 17727+ }
dece6358 17728+
4a4d8108
AM
17729+ isdir = 0;
17730+ if (h_child_inode)
17731+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17732+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17733+ flags[AuHn_CHILD] = 0;
17734+ if (isdir)
17735+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17736+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17737+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
fbc438ed 17738+ switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
4a4d8108
AM
17739+ case FS_MOVED_FROM:
17740+ case FS_MOVED_TO:
17741+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17742+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17743+ /*FALLTHROUGH*/
17744+ case FS_CREATE:
fb47a38f 17745+ AuDebugOn(!h_child_name);
4a4d8108 17746+ break;
1facf9fc 17747+
4a4d8108
AM
17748+ case FS_DELETE:
17749+ /*
17750+ * aufs never be able to get this child inode.
17751+ * revalidation should be in d_revalidate()
17752+ * by checking i_nlink, i_generation or d_unhashed().
17753+ */
17754+ AuDebugOn(!h_child_name);
17755+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17756+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17757+ break;
dece6358 17758+
4a4d8108
AM
17759+ default:
17760+ AuDebugOn(1);
17761+ }
1308ab2a 17762+
4a4d8108
AM
17763+ if (wh)
17764+ h_child_inode = NULL;
1308ab2a 17765+
4a4d8108
AM
17766+ err = -ENOMEM;
17767+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17768+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17769+ if (unlikely(!args)) {
17770+ AuErr1("no memory\n");
17771+ iput(dir);
17772+ goto out;
17773+ }
17774+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17775+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17776+ args->mask = mask;
17777+ args->dir = dir;
17778+ args->h_dir = igrab(h_dir);
17779+ if (h_child_inode)
17780+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17781+ args->h_child_inode = h_child_inode;
17782+ args->h_child_nlen = len;
17783+ if (len) {
17784+ p = (void *)args;
17785+ p += sizeof(*args);
17786+ memcpy(p, h_child_name, len);
17787+ p[len] = 0;
1308ab2a 17788+ }
1308ab2a 17789+
38d290e6 17790+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17791+ f = 0;
38d290e6
JR
17792+ if (!dir->i_nlink
17793+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17794+ f = AuWkq_NEST;
17795+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17796+ if (unlikely(err)) {
17797+ pr_err("wkq %d\n", err);
17798+ iput(args->h_child_inode);
17799+ iput(args->h_dir);
17800+ iput(args->dir);
9f237c51 17801+ au_kfree_rcu(args);
1facf9fc 17802+ }
1facf9fc 17803+
4a4d8108 17804+out:
1facf9fc 17805+ return err;
17806+}
17807+
027c5e7a
AM
17808+/* ---------------------------------------------------------------------- */
17809+
17810+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17811+{
17812+ int err;
17813+
17814+ AuDebugOn(!(udba & AuOptMask_UDBA));
17815+
17816+ err = 0;
17817+ if (au_hnotify_op.reset_br)
17818+ err = au_hnotify_op.reset_br(udba, br, perm);
17819+
17820+ return err;
17821+}
17822+
17823+int au_hnotify_init_br(struct au_branch *br, int perm)
17824+{
17825+ int err;
17826+
17827+ err = 0;
17828+ if (au_hnotify_op.init_br)
17829+ err = au_hnotify_op.init_br(br, perm);
17830+
17831+ return err;
17832+}
17833+
17834+void au_hnotify_fin_br(struct au_branch *br)
17835+{
17836+ if (au_hnotify_op.fin_br)
17837+ au_hnotify_op.fin_br(br);
17838+}
17839+
4a4d8108
AM
17840+static void au_hn_destroy_cache(void)
17841+{
1c60b727
AM
17842+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17843+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17844+}
1308ab2a 17845+
4a4d8108 17846+int __init au_hnotify_init(void)
1facf9fc 17847+{
1308ab2a 17848+ int err;
1308ab2a 17849+
4a4d8108 17850+ err = -ENOMEM;
1c60b727
AM
17851+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17852+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17853+ err = 0;
17854+ if (au_hnotify_op.init)
17855+ err = au_hnotify_op.init();
4a4d8108
AM
17856+ if (unlikely(err))
17857+ au_hn_destroy_cache();
1308ab2a 17858+ }
1308ab2a 17859+ AuTraceErr(err);
4a4d8108 17860+ return err;
1308ab2a 17861+}
17862+
4a4d8108 17863+void au_hnotify_fin(void)
1308ab2a 17864+{
027c5e7a
AM
17865+ if (au_hnotify_op.fin)
17866+ au_hnotify_op.fin();
f0c0a007 17867+
4a4d8108 17868+ /* cf. au_cache_fin() */
1c60b727 17869+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17870+ au_hn_destroy_cache();
dece6358 17871+}
7f207e10 17872diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
eca34b5c 17873--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
016522bc 17874+++ linux/fs/aufs/iinfo.c 2020-01-27 10:57:18.175538316 +0100
062440b3 17875@@ -0,0 +1,286 @@
cd7a4cd9 17876+// SPDX-License-Identifier: GPL-2.0
dece6358 17877+/*
016522bc 17878+ * Copyright (C) 2005-2020 Junjiro R. Okajima
dece6358
AM
17879+ *
17880+ * This program, aufs is free software; you can redistribute it and/or modify
17881+ * it under the terms of the GNU General Public License as published by
17882+ * the Free Software Foundation; either version 2 of the License, or
17883+ * (at your option) any later version.
17884+ *
17885+ * This program is distributed in the hope that it will be useful,
17886+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17887+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17888+ * GNU General Public License for more details.
17889+ *
17890+ * You should have received a copy of the GNU General Public License
523b37e3 17891+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17892+ */
1facf9fc 17893+
dece6358 17894+/*
4a4d8108 17895+ * inode private data
dece6358 17896+ */
1facf9fc 17897+
1308ab2a 17898+#include "aufs.h"
1facf9fc 17899+
4a4d8108 17900+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17901+{
4a4d8108 17902+ struct inode *h_inode;
5afbbe0d 17903+ struct au_hinode *hinode;
1facf9fc 17904+
4a4d8108 17905+ IiMustAnyLock(inode);
1facf9fc 17906+
5afbbe0d
AM
17907+ hinode = au_hinode(au_ii(inode), bindex);
17908+ h_inode = hinode->hi_inode;
4a4d8108
AM
17909+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17910+ return h_inode;
17911+}
1facf9fc 17912+
4a4d8108
AM
17913+/* todo: hard/soft set? */
17914+void au_hiput(struct au_hinode *hinode)
17915+{
17916+ au_hn_free(hinode);
17917+ dput(hinode->hi_whdentry);
17918+ iput(hinode->hi_inode);
17919+}
1facf9fc 17920+
4a4d8108
AM
17921+unsigned int au_hi_flags(struct inode *inode, int isdir)
17922+{
17923+ unsigned int flags;
17924+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 17925+
4a4d8108
AM
17926+ flags = 0;
17927+ if (au_opt_test(mnt_flags, XINO))
17928+ au_fset_hi(flags, XINO);
17929+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17930+ au_fset_hi(flags, HNOTIFY);
17931+ return flags;
1facf9fc 17932+}
17933+
4a4d8108
AM
17934+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17935+ struct inode *h_inode, unsigned int flags)
1308ab2a 17936+{
4a4d8108
AM
17937+ struct au_hinode *hinode;
17938+ struct inode *hi;
17939+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 17940+
4a4d8108 17941+ IiMustWriteLock(inode);
dece6358 17942+
5afbbe0d 17943+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
17944+ hi = hinode->hi_inode;
17945+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17946+
17947+ if (hi)
17948+ au_hiput(hinode);
17949+ hinode->hi_inode = h_inode;
17950+ if (h_inode) {
17951+ int err;
17952+ struct super_block *sb = inode->i_sb;
17953+ struct au_branch *br;
17954+
027c5e7a
AM
17955+ AuDebugOn(inode->i_mode
17956+ && (h_inode->i_mode & S_IFMT)
17957+ != (inode->i_mode & S_IFMT));
5afbbe0d 17958+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
17959+ au_cpup_igen(inode, h_inode);
17960+ br = au_sbr(sb, bindex);
17961+ hinode->hi_id = br->br_id;
17962+ if (au_ftest_hi(flags, XINO)) {
17963+ err = au_xino_write(sb, bindex, h_inode->i_ino,
17964+ inode->i_ino);
17965+ if (unlikely(err))
17966+ AuIOErr1("failed au_xino_write() %d\n", err);
17967+ }
17968+
17969+ if (au_ftest_hi(flags, HNOTIFY)
17970+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 17971+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
17972+ if (unlikely(err))
17973+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 17974+ }
17975+ }
4a4d8108 17976+}
dece6358 17977+
4a4d8108
AM
17978+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17979+ struct dentry *h_wh)
17980+{
17981+ struct au_hinode *hinode;
dece6358 17982+
4a4d8108
AM
17983+ IiMustWriteLock(inode);
17984+
5afbbe0d 17985+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
17986+ AuDebugOn(hinode->hi_whdentry);
17987+ hinode->hi_whdentry = h_wh;
1facf9fc 17988+}
17989+
537831f9 17990+void au_update_iigen(struct inode *inode, int half)
1308ab2a 17991+{
537831f9
AM
17992+ struct au_iinfo *iinfo;
17993+ struct au_iigen *iigen;
17994+ unsigned int sigen;
17995+
17996+ sigen = au_sigen(inode->i_sb);
17997+ iinfo = au_ii(inode);
17998+ iigen = &iinfo->ii_generation;
be52b249 17999+ spin_lock(&iigen->ig_spin);
537831f9
AM
18000+ iigen->ig_generation = sigen;
18001+ if (half)
18002+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18003+ else
18004+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 18005+ spin_unlock(&iigen->ig_spin);
4a4d8108 18006+}
1facf9fc 18007+
4a4d8108
AM
18008+/* it may be called at remount time, too */
18009+void au_update_ibrange(struct inode *inode, int do_put_zero)
18010+{
18011+ struct au_iinfo *iinfo;
5afbbe0d 18012+ aufs_bindex_t bindex, bbot;
1facf9fc 18013+
5afbbe0d 18014+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18015+ IiMustWriteLock(inode);
1facf9fc 18016+
5afbbe0d
AM
18017+ iinfo = au_ii(inode);
18018+ if (do_put_zero && iinfo->ii_btop >= 0) {
18019+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18020+ bindex++) {
18021+ struct inode *h_i;
1facf9fc 18022+
5afbbe0d 18023+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
18024+ if (h_i
18025+ && !h_i->i_nlink
18026+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
18027+ au_set_h_iptr(inode, bindex, NULL, 0);
18028+ }
4a4d8108
AM
18029+ }
18030+
5afbbe0d
AM
18031+ iinfo->ii_btop = -1;
18032+ iinfo->ii_bbot = -1;
18033+ bbot = au_sbbot(inode->i_sb);
18034+ for (bindex = 0; bindex <= bbot; bindex++)
18035+ if (au_hinode(iinfo, bindex)->hi_inode) {
18036+ iinfo->ii_btop = bindex;
4a4d8108 18037+ break;
027c5e7a 18038+ }
5afbbe0d
AM
18039+ if (iinfo->ii_btop >= 0)
18040+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18041+ if (au_hinode(iinfo, bindex)->hi_inode) {
18042+ iinfo->ii_bbot = bindex;
027c5e7a
AM
18043+ break;
18044+ }
5afbbe0d 18045+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 18046+}
1facf9fc 18047+
dece6358 18048+/* ---------------------------------------------------------------------- */
1facf9fc 18049+
4a4d8108 18050+void au_icntnr_init_once(void *_c)
dece6358 18051+{
4a4d8108
AM
18052+ struct au_icntnr *c = _c;
18053+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 18054+
be52b249 18055+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
18056+ au_rw_init(&iinfo->ii_rwsem);
18057+ inode_init_once(&c->vfs_inode);
18058+}
1facf9fc 18059+
5afbbe0d
AM
18060+void au_hinode_init(struct au_hinode *hinode)
18061+{
18062+ hinode->hi_inode = NULL;
18063+ hinode->hi_id = -1;
18064+ au_hn_init(hinode);
18065+ hinode->hi_whdentry = NULL;
18066+}
18067+
4a4d8108
AM
18068+int au_iinfo_init(struct inode *inode)
18069+{
18070+ struct au_iinfo *iinfo;
18071+ struct super_block *sb;
5afbbe0d 18072+ struct au_hinode *hi;
4a4d8108 18073+ int nbr, i;
1facf9fc 18074+
4a4d8108
AM
18075+ sb = inode->i_sb;
18076+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 18077+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
18078+ if (unlikely(nbr <= 0))
18079+ nbr = 1;
5afbbe0d
AM
18080+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18081+ if (hi) {
acd2b654 18082+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
5afbbe0d
AM
18083+
18084+ iinfo->ii_hinode = hi;
18085+ for (i = 0; i < nbr; i++, hi++)
18086+ au_hinode_init(hi);
1facf9fc 18087+
537831f9 18088+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
18089+ iinfo->ii_btop = -1;
18090+ iinfo->ii_bbot = -1;
4a4d8108
AM
18091+ iinfo->ii_vdir = NULL;
18092+ return 0;
1308ab2a 18093+ }
4a4d8108
AM
18094+ return -ENOMEM;
18095+}
1facf9fc 18096+
e2f27e51 18097+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 18098+{
5afbbe0d 18099+ int err, i;
4a4d8108 18100+ struct au_hinode *hip;
1facf9fc 18101+
4a4d8108
AM
18102+ AuRwMustWriteLock(&iinfo->ii_rwsem);
18103+
18104+ err = -ENOMEM;
e2f27e51
AM
18105+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18106+ may_shrink);
4a4d8108
AM
18107+ if (hip) {
18108+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18109+ i = iinfo->ii_bbot + 1;
18110+ hip += i;
18111+ for (; i < nbr; i++, hip++)
18112+ au_hinode_init(hip);
4a4d8108 18113+ err = 0;
1308ab2a 18114+ }
4a4d8108 18115+
1308ab2a 18116+ return err;
1facf9fc 18117+}
18118+
4a4d8108 18119+void au_iinfo_fin(struct inode *inode)
1facf9fc 18120+{
4a4d8108
AM
18121+ struct au_iinfo *iinfo;
18122+ struct au_hinode *hi;
18123+ struct super_block *sb;
5afbbe0d 18124+ aufs_bindex_t bindex, bbot;
b752ccd1 18125+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18126+
5afbbe0d 18127+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18128+
b752ccd1 18129+ sb = inode->i_sb;
acd2b654 18130+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
b752ccd1
AM
18131+ if (si_pid_test(sb))
18132+ au_xino_delete_inode(inode, unlinked);
18133+ else {
18134+ /*
18135+ * it is safe to hide the dependency between sbinfo and
18136+ * sb->s_umount.
18137+ */
18138+ lockdep_off();
18139+ si_noflush_read_lock(sb);
18140+ au_xino_delete_inode(inode, unlinked);
18141+ si_read_unlock(sb);
18142+ lockdep_on();
18143+ }
18144+
5afbbe0d 18145+ iinfo = au_ii(inode);
4a4d8108 18146+ if (iinfo->ii_vdir)
1c60b727 18147+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18148+
5afbbe0d 18149+ bindex = iinfo->ii_btop;
b752ccd1 18150+ if (bindex >= 0) {
5afbbe0d
AM
18151+ hi = au_hinode(iinfo, bindex);
18152+ bbot = iinfo->ii_bbot;
18153+ while (bindex++ <= bbot) {
b752ccd1 18154+ if (hi->hi_inode)
4a4d8108 18155+ au_hiput(hi);
4a4d8108
AM
18156+ hi++;
18157+ }
18158+ }
9f237c51 18159+ au_kfree_rcu(iinfo->ii_hinode);
4a4d8108 18160+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18161+}
7f207e10 18162diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
eca34b5c 18163--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
016522bc 18164+++ linux/fs/aufs/inode.c 2020-01-27 10:57:18.175538316 +0100
eca801bf 18165@@ -0,0 +1,529 @@
cd7a4cd9 18166+// SPDX-License-Identifier: GPL-2.0
4a4d8108 18167+/*
016522bc 18168+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
18169+ *
18170+ * This program, aufs is free software; you can redistribute it and/or modify
18171+ * it under the terms of the GNU General Public License as published by
18172+ * the Free Software Foundation; either version 2 of the License, or
18173+ * (at your option) any later version.
18174+ *
18175+ * This program is distributed in the hope that it will be useful,
18176+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18177+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18178+ * GNU General Public License for more details.
18179+ *
18180+ * You should have received a copy of the GNU General Public License
523b37e3 18181+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18182+ */
1facf9fc 18183+
4a4d8108
AM
18184+/*
18185+ * inode functions
18186+ */
1facf9fc 18187+
eca801bf 18188+#include <linux/iversion.h>
4a4d8108 18189+#include "aufs.h"
1308ab2a 18190+
4a4d8108
AM
18191+struct inode *au_igrab(struct inode *inode)
18192+{
18193+ if (inode) {
18194+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18195+ ihold(inode);
1facf9fc 18196+ }
4a4d8108
AM
18197+ return inode;
18198+}
1facf9fc 18199+
4a4d8108
AM
18200+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18201+{
18202+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18203+ au_update_iigen(inode, /*half*/1);
4a4d8108 18204+ if (do_version)
be118d29 18205+ inode_inc_iversion(inode);
dece6358 18206+}
1facf9fc 18207+
027c5e7a 18208+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18209+{
e2f27e51 18210+ int err, e, nbr;
027c5e7a 18211+ umode_t type;
4a4d8108 18212+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18213+ struct super_block *sb;
4a4d8108 18214+ struct au_iinfo *iinfo;
027c5e7a 18215+ struct au_hinode *p, *q, tmp;
1facf9fc 18216+
5afbbe0d 18217+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18218+ IiMustWriteLock(inode);
1facf9fc 18219+
027c5e7a 18220+ *update = 0;
4a4d8108 18221+ sb = inode->i_sb;
e2f27e51 18222+ nbr = au_sbbot(sb) + 1;
027c5e7a 18223+ type = inode->i_mode & S_IFMT;
4a4d8108 18224+ iinfo = au_ii(inode);
e2f27e51 18225+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18226+ if (unlikely(err))
1308ab2a 18227+ goto out;
1facf9fc 18228+
5afbbe0d
AM
18229+ AuDebugOn(iinfo->ii_btop < 0);
18230+ p = au_hinode(iinfo, iinfo->ii_btop);
18231+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18232+ bindex++, p++) {
18233+ if (!p->hi_inode)
18234+ continue;
1facf9fc 18235+
027c5e7a 18236+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18237+ new_bindex = au_br_index(sb, p->hi_id);
18238+ if (new_bindex == bindex)
18239+ continue;
1facf9fc 18240+
4a4d8108 18241+ if (new_bindex < 0) {
027c5e7a 18242+ *update = 1;
4a4d8108
AM
18243+ au_hiput(p);
18244+ p->hi_inode = NULL;
18245+ continue;
1308ab2a 18246+ }
4a4d8108 18247+
5afbbe0d
AM
18248+ if (new_bindex < iinfo->ii_btop)
18249+ iinfo->ii_btop = new_bindex;
18250+ if (iinfo->ii_bbot < new_bindex)
18251+ iinfo->ii_bbot = new_bindex;
4a4d8108 18252+ /* swap two lower inode, and loop again */
5afbbe0d 18253+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18254+ tmp = *q;
18255+ *q = *p;
18256+ *p = tmp;
18257+ if (tmp.hi_inode) {
18258+ bindex--;
18259+ p--;
1308ab2a 18260+ }
18261+ }
4a4d8108 18262+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18263+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18264+ e = au_dy_irefresh(inode);
18265+ if (unlikely(e && !err))
18266+ err = e;
1facf9fc 18267+
4f0767ce 18268+out:
027c5e7a
AM
18269+ AuTraceErr(err);
18270+ return err;
18271+}
18272+
b95c5147
AM
18273+void au_refresh_iop(struct inode *inode, int force_getattr)
18274+{
18275+ int type;
18276+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18277+ const struct inode_operations *iop
18278+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18279+
18280+ if (inode->i_op == iop)
18281+ return;
18282+
18283+ switch (inode->i_mode & S_IFMT) {
18284+ case S_IFDIR:
18285+ type = AuIop_DIR;
18286+ break;
18287+ case S_IFLNK:
18288+ type = AuIop_SYMLINK;
18289+ break;
18290+ default:
18291+ type = AuIop_OTHER;
18292+ break;
18293+ }
18294+
18295+ inode->i_op = iop + type;
18296+ /* unnecessary smp_wmb() */
18297+}
18298+
027c5e7a
AM
18299+int au_refresh_hinode_self(struct inode *inode)
18300+{
18301+ int err, update;
18302+
18303+ err = au_ii_refresh(inode, &update);
18304+ if (!err)
18305+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18306+
18307+ AuTraceErr(err);
4a4d8108
AM
18308+ return err;
18309+}
1facf9fc 18310+
4a4d8108
AM
18311+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18312+{
027c5e7a 18313+ int err, e, update;
4a4d8108 18314+ unsigned int flags;
027c5e7a 18315+ umode_t mode;
5afbbe0d 18316+ aufs_bindex_t bindex, bbot;
027c5e7a 18317+ unsigned char isdir;
4a4d8108
AM
18318+ struct au_hinode *p;
18319+ struct au_iinfo *iinfo;
1facf9fc 18320+
027c5e7a 18321+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18322+ if (unlikely(err))
18323+ goto out;
18324+
18325+ update = 0;
18326+ iinfo = au_ii(inode);
5afbbe0d 18327+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18328+ mode = (inode->i_mode & S_IFMT);
18329+ isdir = S_ISDIR(mode);
4a4d8108 18330+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18331+ bbot = au_dbbot(dentry);
18332+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18333+ struct inode *h_i, *h_inode;
4a4d8108
AM
18334+ struct dentry *h_d;
18335+
18336+ h_d = au_h_dptr(dentry, bindex);
5527c038 18337+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18338+ continue;
18339+
5527c038
JR
18340+ h_inode = d_inode(h_d);
18341+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18342+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18343+ h_i = au_h_iptr(inode, bindex);
18344+ if (h_i) {
5527c038 18345+ if (h_i == h_inode)
4a4d8108
AM
18346+ continue;
18347+ err = -EIO;
18348+ break;
18349+ }
18350+ }
5afbbe0d
AM
18351+ if (bindex < iinfo->ii_btop)
18352+ iinfo->ii_btop = bindex;
18353+ if (iinfo->ii_bbot < bindex)
18354+ iinfo->ii_bbot = bindex;
5527c038 18355+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18356+ update = 1;
1308ab2a 18357+ }
4a4d8108
AM
18358+ au_update_ibrange(inode, /*do_put_zero*/0);
18359+ e = au_dy_irefresh(inode);
18360+ if (unlikely(e && !err))
18361+ err = e;
027c5e7a
AM
18362+ if (!err)
18363+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18364+
4f0767ce 18365+out:
4a4d8108 18366+ AuTraceErr(err);
1308ab2a 18367+ return err;
dece6358
AM
18368+}
18369+
4a4d8108 18370+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18371+{
4a4d8108
AM
18372+ int err;
18373+ unsigned int flags;
18374+ umode_t mode;
5afbbe0d 18375+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18376+ unsigned char isdir;
18377+ struct dentry *h_dentry;
18378+ struct inode *h_inode;
18379+ struct au_iinfo *iinfo;
43982f53 18380+ const struct inode_operations *iop;
dece6358 18381+
4a4d8108 18382+ IiMustWriteLock(inode);
dece6358 18383+
4a4d8108
AM
18384+ err = 0;
18385+ isdir = 0;
b95c5147 18386+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18387+ btop = au_dbtop(dentry);
18388+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18389+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18390+ mode = h_inode->i_mode;
18391+ switch (mode & S_IFMT) {
18392+ case S_IFREG:
18393+ btail = au_dbtail(dentry);
b95c5147 18394+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18395+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18396+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18397+ if (unlikely(err))
18398+ goto out;
18399+ break;
18400+ case S_IFDIR:
18401+ isdir = 1;
18402+ btail = au_dbtaildir(dentry);
b95c5147 18403+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18404+ inode->i_fop = &aufs_dir_fop;
18405+ break;
18406+ case S_IFLNK:
18407+ btail = au_dbtail(dentry);
b95c5147 18408+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18409+ break;
18410+ case S_IFBLK:
18411+ case S_IFCHR:
18412+ case S_IFIFO:
18413+ case S_IFSOCK:
18414+ btail = au_dbtail(dentry);
b95c5147 18415+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18416+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18417+ break;
18418+ default:
18419+ AuIOErr("Unknown file type 0%o\n", mode);
18420+ err = -EIO;
1308ab2a 18421+ goto out;
4a4d8108 18422+ }
dece6358 18423+
4a4d8108
AM
18424+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18425+ flags = au_hi_flags(inode, isdir);
18426+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18427+ && au_ftest_hi(flags, HNOTIFY)
18428+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18429+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18430+ au_fclr_hi(flags, HNOTIFY);
18431+ iinfo = au_ii(inode);
5afbbe0d
AM
18432+ iinfo->ii_btop = btop;
18433+ iinfo->ii_bbot = btail;
18434+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18435+ h_dentry = au_h_dptr(dentry, bindex);
18436+ if (h_dentry)
18437+ au_set_h_iptr(inode, bindex,
5527c038 18438+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18439+ }
18440+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18441+ /*
18442+ * to force calling aufs_get_acl() every time,
18443+ * do not call cache_no_acl() for aufs inode.
18444+ */
dece6358 18445+
4f0767ce 18446+out:
4a4d8108
AM
18447+ return err;
18448+}
dece6358 18449+
027c5e7a
AM
18450+/*
18451+ * successful returns with iinfo write_locked
18452+ * minus: errno
18453+ * zero: success, matched
18454+ * plus: no error, but unmatched
18455+ */
18456+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18457+{
18458+ int err;
cfc41e69 18459+ unsigned int gen, igflags;
5afbbe0d 18460+ aufs_bindex_t bindex, bbot;
4a4d8108 18461+ struct inode *h_inode, *h_dinode;
5527c038 18462+ struct dentry *h_dentry;
dece6358 18463+
4a4d8108
AM
18464+ /*
18465+ * before this function, if aufs got any iinfo lock, it must be only
18466+ * one, the parent dir.
18467+ * it can happen by UDBA and the obsoleted inode number.
18468+ */
18469+ err = -EIO;
18470+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18471+ goto out;
18472+
027c5e7a 18473+ err = 1;
4a4d8108 18474+ ii_write_lock_new_child(inode);
5afbbe0d 18475+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18476+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18477+ bbot = au_ibbot(inode);
18478+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18479+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18480+ if (!h_inode || h_inode != h_dinode)
18481+ continue;
18482+
18483+ err = 0;
cfc41e69 18484+ gen = au_iigen(inode, &igflags);
537831f9 18485+ if (gen == au_digen(dentry)
cfc41e69 18486+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18487+ break;
537831f9
AM
18488+
18489+ /* fully refresh inode using dentry */
18490+ err = au_refresh_hinode(inode, dentry);
18491+ if (!err)
18492+ au_update_iigen(inode, /*half*/0);
18493+ break;
1facf9fc 18494+ }
dece6358 18495+
4a4d8108
AM
18496+ if (unlikely(err))
18497+ ii_write_unlock(inode);
4f0767ce 18498+out:
1facf9fc 18499+ return err;
18500+}
1facf9fc 18501+
4a4d8108
AM
18502+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18503+ unsigned int d_type, ino_t *ino)
1facf9fc 18504+{
521ced18
JR
18505+ int err, idx;
18506+ const int isnondir = d_type != DT_DIR;
1facf9fc 18507+
b752ccd1 18508+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18509+ if (isnondir) {
18510+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18511+ if (unlikely(err))
18512+ goto out;
4a4d8108 18513+ }
521ced18 18514+
4a4d8108
AM
18515+ err = au_xino_read(sb, bindex, h_ino, ino);
18516+ if (unlikely(err))
521ced18 18517+ goto out_xinondir;
1308ab2a 18518+
4a4d8108
AM
18519+ if (!*ino) {
18520+ err = -EIO;
18521+ *ino = au_xino_new_ino(sb);
18522+ if (unlikely(!*ino))
521ced18 18523+ goto out_xinondir;
4a4d8108
AM
18524+ err = au_xino_write(sb, bindex, h_ino, *ino);
18525+ if (unlikely(err))
521ced18 18526+ goto out_xinondir;
1308ab2a 18527+ }
1facf9fc 18528+
521ced18
JR
18529+out_xinondir:
18530+ if (isnondir && idx >= 0)
18531+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18532+out:
1facf9fc 18533+ return err;
18534+}
18535+
4a4d8108
AM
18536+/* successful returns with iinfo write_locked */
18537+/* todo: return with unlocked? */
18538+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18539+{
5527c038 18540+ struct inode *inode, *h_inode;
4a4d8108
AM
18541+ struct dentry *h_dentry;
18542+ struct super_block *sb;
18543+ ino_t h_ino, ino;
521ced18 18544+ int err, idx, hlinked;
5afbbe0d 18545+ aufs_bindex_t btop;
1facf9fc 18546+
4a4d8108 18547+ sb = dentry->d_sb;
5afbbe0d
AM
18548+ btop = au_dbtop(dentry);
18549+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18550+ h_inode = d_inode(h_dentry);
18551+ h_ino = h_inode->i_ino;
521ced18 18552+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18553+
521ced18 18554+new_ino:
b752ccd1
AM
18555+ /*
18556+ * stop 'race'-ing between hardlinks under different
18557+ * parents.
18558+ */
521ced18
JR
18559+ if (hlinked) {
18560+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18561+ inode = ERR_PTR(err);
18562+ if (unlikely(err))
18563+ goto out;
18564+ }
b752ccd1 18565+
5afbbe0d 18566+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18567+ inode = ERR_PTR(err);
18568+ if (unlikely(err))
521ced18 18569+ goto out_xinondir;
b752ccd1 18570+
4a4d8108
AM
18571+ if (!ino) {
18572+ ino = au_xino_new_ino(sb);
18573+ if (unlikely(!ino)) {
18574+ inode = ERR_PTR(-EIO);
521ced18 18575+ goto out_xinondir;
dece6358
AM
18576+ }
18577+ }
1facf9fc 18578+
4a4d8108
AM
18579+ AuDbg("i%lu\n", (unsigned long)ino);
18580+ inode = au_iget_locked(sb, ino);
18581+ err = PTR_ERR(inode);
18582+ if (IS_ERR(inode))
521ced18 18583+ goto out_xinondir;
1facf9fc 18584+
4a4d8108
AM
18585+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18586+ if (inode->i_state & I_NEW) {
18587+ ii_write_lock_new_child(inode);
18588+ err = set_inode(inode, dentry);
18589+ if (!err) {
18590+ unlock_new_inode(inode);
521ced18 18591+ goto out_xinondir; /* success */
4a4d8108 18592+ }
1308ab2a 18593+
027c5e7a
AM
18594+ /*
18595+ * iget_failed() calls iput(), but we need to call
18596+ * ii_write_unlock() after iget_failed(). so dirty hack for
18597+ * i_count.
18598+ */
18599+ atomic_inc(&inode->i_count);
4a4d8108 18600+ iget_failed(inode);
027c5e7a 18601+ ii_write_unlock(inode);
5afbbe0d 18602+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18603+ /* ignore this error */
18604+ goto out_iput;
18605+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18606+ /*
18607+ * horrible race condition between lookup, readdir and copyup
18608+ * (or something).
18609+ */
521ced18
JR
18610+ if (hlinked && idx >= 0)
18611+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18612+ err = reval_inode(inode, dentry);
18613+ if (unlikely(err < 0)) {
521ced18 18614+ hlinked = 0;
027c5e7a
AM
18615+ goto out_iput;
18616+ }
521ced18 18617+ if (!err)
4a4d8108 18618+ goto out; /* success */
521ced18
JR
18619+ else if (hlinked && idx >= 0) {
18620+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18621+ if (unlikely(err)) {
18622+ iput(inode);
18623+ inode = ERR_PTR(err);
18624+ goto out;
18625+ }
18626+ }
4a4d8108
AM
18627+ }
18628+
5527c038 18629+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18630+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18631+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18632+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18633+ (unsigned long)h_ino, (unsigned long)ino);
18634+ ino = 0;
5afbbe0d 18635+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18636+ if (!err) {
18637+ iput(inode);
521ced18
JR
18638+ if (hlinked && idx >= 0)
18639+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18640+ goto new_ino;
18641+ }
1308ab2a 18642+
4f0767ce 18643+out_iput:
4a4d8108 18644+ iput(inode);
4a4d8108 18645+ inode = ERR_PTR(err);
521ced18
JR
18646+out_xinondir:
18647+ if (hlinked && idx >= 0)
18648+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18649+out:
4a4d8108 18650+ return inode;
1facf9fc 18651+}
18652+
4a4d8108 18653+/* ---------------------------------------------------------------------- */
1facf9fc 18654+
4a4d8108
AM
18655+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18656+ struct inode *inode)
18657+{
18658+ int err;
076b876e 18659+ struct inode *hi;
1facf9fc 18660+
4a4d8108 18661+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18662+
4a4d8108
AM
18663+ /* pseudo-link after flushed may happen out of bounds */
18664+ if (!err
18665+ && inode
5afbbe0d
AM
18666+ && au_ibtop(inode) <= bindex
18667+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18668+ /*
18669+ * permission check is unnecessary since vfsub routine
18670+ * will be called later
18671+ */
076b876e 18672+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18673+ if (hi)
18674+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18675+ }
18676+
4a4d8108
AM
18677+ return err;
18678+}
dece6358 18679+
4a4d8108
AM
18680+int au_test_h_perm(struct inode *h_inode, int mask)
18681+{
2dfbb274 18682+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
18683+ return 0;
18684+ return inode_permission(h_inode, mask);
18685+}
1facf9fc 18686+
4a4d8108
AM
18687+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18688+{
18689+ if (au_test_nfs(h_inode->i_sb)
18690+ && (mask & MAY_WRITE)
18691+ && S_ISDIR(h_inode->i_mode))
18692+ mask |= MAY_READ; /* force permission check */
18693+ return au_test_h_perm(h_inode, mask);
1facf9fc 18694+}
7f207e10 18695diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
eca34b5c 18696--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
016522bc 18697+++ linux/fs/aufs/inode.h 2020-01-27 10:57:18.175538316 +0100
9f237c51 18698@@ -0,0 +1,698 @@
062440b3 18699+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 18700+/*
016522bc 18701+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
18702+ *
18703+ * This program, aufs is free software; you can redistribute it and/or modify
18704+ * it under the terms of the GNU General Public License as published by
18705+ * the Free Software Foundation; either version 2 of the License, or
18706+ * (at your option) any later version.
18707+ *
18708+ * This program is distributed in the hope that it will be useful,
18709+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18710+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18711+ * GNU General Public License for more details.
18712+ *
18713+ * You should have received a copy of the GNU General Public License
523b37e3 18714+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18715+ */
1facf9fc 18716+
1308ab2a 18717+/*
4a4d8108 18718+ * inode operations
1308ab2a 18719+ */
dece6358 18720+
4a4d8108
AM
18721+#ifndef __AUFS_INODE_H__
18722+#define __AUFS_INODE_H__
dece6358 18723+
4a4d8108 18724+#ifdef __KERNEL__
1308ab2a 18725+
4a4d8108 18726+#include <linux/fsnotify.h>
4a4d8108 18727+#include "rwsem.h"
1308ab2a 18728+
4a4d8108 18729+struct vfsmount;
1facf9fc 18730+
4a4d8108
AM
18731+struct au_hnotify {
18732+#ifdef CONFIG_AUFS_HNOTIFY
18733+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18734+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18735+ struct fsnotify_mark hn_mark;
4a4d8108 18736+#endif
1c60b727 18737+ struct inode *hn_aufs_inode; /* no get/put */
9f237c51 18738+ struct rcu_head rcu;
4a4d8108
AM
18739+#endif
18740+} ____cacheline_aligned_in_smp;
1facf9fc 18741+
4a4d8108
AM
18742+struct au_hinode {
18743+ struct inode *hi_inode;
18744+ aufs_bindex_t hi_id;
18745+#ifdef CONFIG_AUFS_HNOTIFY
18746+ struct au_hnotify *hi_notify;
18747+#endif
dece6358 18748+
4a4d8108
AM
18749+ /* reference to the copied-up whiteout with get/put */
18750+ struct dentry *hi_whdentry;
18751+};
dece6358 18752+
537831f9
AM
18753+/* ig_flags */
18754+#define AuIG_HALF_REFRESHED 1
18755+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18756+#define au_ig_fset(flags, name) \
18757+ do { (flags) |= AuIG_##name; } while (0)
18758+#define au_ig_fclr(flags, name) \
18759+ do { (flags) &= ~AuIG_##name; } while (0)
18760+
18761+struct au_iigen {
be52b249 18762+ spinlock_t ig_spin;
537831f9
AM
18763+ __u32 ig_generation, ig_flags;
18764+};
18765+
4a4d8108
AM
18766+struct au_vdir;
18767+struct au_iinfo {
7a9e40b8 18768+ struct au_iigen ii_generation;
4a4d8108 18769+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18770+
4a4d8108 18771+ struct au_rwsem ii_rwsem;
5afbbe0d 18772+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18773+ __u32 ii_higen;
18774+ struct au_hinode *ii_hinode;
18775+ struct au_vdir *ii_vdir;
18776+};
1facf9fc 18777+
4a4d8108 18778+struct au_icntnr {
9f237c51
AM
18779+ struct au_iinfo iinfo;
18780+ struct inode vfs_inode;
18781+ struct hlist_bl_node plink;
18782+ struct rcu_head rcu;
4a4d8108 18783+} ____cacheline_aligned_in_smp;
1308ab2a 18784+
4a4d8108
AM
18785+/* au_pin flags */
18786+#define AuPin_DI_LOCKED 1
18787+#define AuPin_MNT_WRITE (1 << 1)
18788+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18789+#define au_fset_pin(flags, name) \
18790+ do { (flags) |= AuPin_##name; } while (0)
18791+#define au_fclr_pin(flags, name) \
18792+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18793+
18794+struct au_pin {
18795+ /* input */
18796+ struct dentry *dentry;
18797+ unsigned int udba;
18798+ unsigned char lsc_di, lsc_hi, flags;
18799+ aufs_bindex_t bindex;
18800+
18801+ /* output */
18802+ struct dentry *parent;
18803+ struct au_hinode *hdir;
18804+ struct vfsmount *h_mnt;
86dc4139
AM
18805+
18806+ /* temporary unlock/relock for copyup */
18807+ struct dentry *h_dentry, *h_parent;
18808+ struct au_branch *br;
18809+ struct task_struct *task;
4a4d8108 18810+};
1facf9fc 18811+
86dc4139 18812+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18813+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18814+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18815+void au_pin_hdir_acquire_nest(struct au_pin *p);
18816+void au_pin_hdir_release(struct au_pin *p);
18817+
1308ab2a 18818+/* ---------------------------------------------------------------------- */
18819+
4a4d8108 18820+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18821+{
5afbbe0d
AM
18822+ BUG_ON(is_bad_inode(inode));
18823+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18824+}
1facf9fc 18825+
4a4d8108 18826+/* ---------------------------------------------------------------------- */
1facf9fc 18827+
4a4d8108
AM
18828+/* inode.c */
18829+struct inode *au_igrab(struct inode *inode);
b95c5147 18830+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18831+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18832+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18833+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18834+ unsigned int d_type, ino_t *ino);
18835+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18836+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18837+ struct inode *inode);
18838+int au_test_h_perm(struct inode *h_inode, int mask);
18839+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 18840+
4a4d8108
AM
18841+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18842+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18843+{
18844+#ifdef CONFIG_AUFS_SHWH
18845+ return au_ino(sb, bindex, h_ino, d_type, ino);
18846+#else
18847+ return 0;
18848+#endif
18849+}
1facf9fc 18850+
4a4d8108 18851+/* i_op.c */
b95c5147
AM
18852+enum {
18853+ AuIop_SYMLINK,
18854+ AuIop_DIR,
18855+ AuIop_OTHER,
18856+ AuIop_Last
18857+};
43982f53 18858+extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
b95c5147 18859+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18860+
4a4d8108
AM
18861+/* au_wr_dir flags */
18862+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18863+#define AuWrDir_ISDIR (1 << 1)
18864+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18865+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18866+#define au_fset_wrdir(flags, name) \
18867+ do { (flags) |= AuWrDir_##name; } while (0)
18868+#define au_fclr_wrdir(flags, name) \
18869+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18870+
4a4d8108
AM
18871+struct au_wr_dir_args {
18872+ aufs_bindex_t force_btgt;
18873+ unsigned char flags;
18874+};
18875+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18876+ struct au_wr_dir_args *args);
dece6358 18877+
4a4d8108
AM
18878+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18879+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18880+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18881+ unsigned int udba, unsigned char flags);
18882+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18883+ unsigned int udba, unsigned char flags) __must_check;
18884+int au_do_pin(struct au_pin *pin) __must_check;
18885+void au_unpin(struct au_pin *pin);
c1595e42
JR
18886+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18887+
18888+#define AuIcpup_DID_CPUP 1
18889+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18890+#define au_fset_icpup(flags, name) \
18891+ do { (flags) |= AuIcpup_##name; } while (0)
18892+#define au_fclr_icpup(flags, name) \
18893+ do { (flags) &= ~AuIcpup_##name; } while (0)
18894+
18895+struct au_icpup_args {
18896+ unsigned char flags;
18897+ unsigned char pin_flags;
18898+ aufs_bindex_t btgt;
18899+ unsigned int udba;
18900+ struct au_pin pin;
18901+ struct path h_path;
18902+ struct inode *h_inode;
18903+};
18904+
18905+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18906+ struct au_icpup_args *a);
18907+
a2654f78
AM
18908+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
18909+ int locked);
1facf9fc 18910+
4a4d8108
AM
18911+/* i_op_add.c */
18912+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18913+ struct dentry *h_parent, int isdir);
7eafdf33
AM
18914+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18915+ dev_t dev);
4a4d8108 18916+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 18917+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 18918+ bool want_excl);
b912730e
AM
18919+struct vfsub_aopen_args;
18920+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18921+ struct vfsub_aopen_args *args);
38d290e6 18922+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
18923+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18924+ struct dentry *dentry);
7eafdf33 18925+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 18926+
4a4d8108
AM
18927+/* i_op_del.c */
18928+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18929+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18930+ struct dentry *h_parent, int isdir);
18931+int aufs_unlink(struct inode *dir, struct dentry *dentry);
18932+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 18933+
4a4d8108
AM
18934+/* i_op_ren.c */
18935+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18936+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
f2c43d5f
AM
18937+ struct inode *dir, struct dentry *dentry,
18938+ unsigned int flags);
1facf9fc 18939+
4a4d8108
AM
18940+/* iinfo.c */
18941+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18942+void au_hiput(struct au_hinode *hinode);
18943+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18944+ struct dentry *h_wh);
18945+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 18946+
4a4d8108
AM
18947+/* hinode flags */
18948+#define AuHi_XINO 1
18949+#define AuHi_HNOTIFY (1 << 1)
18950+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
18951+#define au_fset_hi(flags, name) \
18952+ do { (flags) |= AuHi_##name; } while (0)
18953+#define au_fclr_hi(flags, name) \
18954+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 18955+
4a4d8108
AM
18956+#ifndef CONFIG_AUFS_HNOTIFY
18957+#undef AuHi_HNOTIFY
18958+#define AuHi_HNOTIFY 0
18959+#endif
1facf9fc 18960+
4a4d8108
AM
18961+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18962+ struct inode *h_inode, unsigned int flags);
1facf9fc 18963+
537831f9 18964+void au_update_iigen(struct inode *inode, int half);
4a4d8108 18965+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 18966+
4a4d8108 18967+void au_icntnr_init_once(void *_c);
5afbbe0d 18968+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
18969+int au_iinfo_init(struct inode *inode);
18970+void au_iinfo_fin(struct inode *inode);
e2f27e51 18971+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 18972+
e49829fe 18973+#ifdef CONFIG_PROC_FS
4a4d8108 18974+/* plink.c */
e49829fe 18975+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 18976+struct au_sbinfo;
e49829fe
JR
18977+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18978+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
18979+#ifdef CONFIG_AUFS_DEBUG
18980+void au_plink_list(struct super_block *sb);
18981+#else
18982+AuStubVoid(au_plink_list, struct super_block *sb)
18983+#endif
18984+int au_plink_test(struct inode *inode);
18985+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18986+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18987+ struct dentry *h_dentry);
e49829fe
JR
18988+void au_plink_put(struct super_block *sb, int verbose);
18989+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 18990+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
18991+#else
18992+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18993+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18994+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18995+AuStubVoid(au_plink_list, struct super_block *sb);
18996+AuStubInt0(au_plink_test, struct inode *inode);
18997+AuStub(struct dentry *, au_plink_lkup, return NULL,
18998+ struct inode *inode, aufs_bindex_t bindex);
18999+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19000+ struct dentry *h_dentry);
19001+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19002+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19003+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19004+#endif /* CONFIG_PROC_FS */
1facf9fc 19005+
c1595e42
JR
19006+#ifdef CONFIG_AUFS_XATTR
19007+/* xattr.c */
7e9cd9fe
AM
19008+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19009+ unsigned int verbose);
c1595e42 19010+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 19011+void au_xattr_init(struct super_block *sb);
c1595e42
JR
19012+#else
19013+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 19014+ int ignore_flags, unsigned int verbose);
f2c43d5f 19015+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
19016+#endif
19017+
19018+#ifdef CONFIG_FS_POSIX_ACL
19019+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19020+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19021+#endif
19022+
19023+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19024+enum {
19025+ AU_XATTR_SET,
c1595e42
JR
19026+ AU_ACL_SET
19027+};
19028+
f2c43d5f 19029+struct au_sxattr {
c1595e42
JR
19030+ int type;
19031+ union {
19032+ struct {
19033+ const char *name;
19034+ const void *value;
19035+ size_t size;
19036+ int flags;
19037+ } set;
19038+ struct {
c1595e42
JR
19039+ struct posix_acl *acl;
19040+ int type;
19041+ } acl_set;
19042+ } u;
19043+};
f2c43d5f
AM
19044+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19045+ struct au_sxattr *arg);
c1595e42
JR
19046+#endif
19047+
4a4d8108 19048+/* ---------------------------------------------------------------------- */
1308ab2a 19049+
4a4d8108
AM
19050+/* lock subclass for iinfo */
19051+enum {
19052+ AuLsc_II_CHILD, /* child first */
19053+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
19054+ AuLsc_II_CHILD3, /* copyup dirs */
19055+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
19056+ AuLsc_II_PARENT2,
19057+ AuLsc_II_PARENT3, /* copyup dirs */
19058+ AuLsc_II_NEW_CHILD
19059+};
1308ab2a 19060+
1facf9fc 19061+/*
4a4d8108
AM
19062+ * ii_read_lock_child, ii_write_lock_child,
19063+ * ii_read_lock_child2, ii_write_lock_child2,
19064+ * ii_read_lock_child3, ii_write_lock_child3,
19065+ * ii_read_lock_parent, ii_write_lock_parent,
19066+ * ii_read_lock_parent2, ii_write_lock_parent2,
19067+ * ii_read_lock_parent3, ii_write_lock_parent3,
19068+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 19069+ */
4a4d8108
AM
19070+#define AuReadLockFunc(name, lsc) \
19071+static inline void ii_read_lock_##name(struct inode *i) \
19072+{ \
19073+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19074+}
19075+
19076+#define AuWriteLockFunc(name, lsc) \
19077+static inline void ii_write_lock_##name(struct inode *i) \
19078+{ \
19079+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19080+}
19081+
19082+#define AuRWLockFuncs(name, lsc) \
19083+ AuReadLockFunc(name, lsc) \
19084+ AuWriteLockFunc(name, lsc)
19085+
19086+AuRWLockFuncs(child, CHILD);
19087+AuRWLockFuncs(child2, CHILD2);
19088+AuRWLockFuncs(child3, CHILD3);
19089+AuRWLockFuncs(parent, PARENT);
19090+AuRWLockFuncs(parent2, PARENT2);
19091+AuRWLockFuncs(parent3, PARENT3);
19092+AuRWLockFuncs(new_child, NEW_CHILD);
19093+
19094+#undef AuReadLockFunc
19095+#undef AuWriteLockFunc
19096+#undef AuRWLockFuncs
1facf9fc 19097+
8b6a4947
AM
19098+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19099+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19100+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19101+
4a4d8108
AM
19102+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19103+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19104+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19105+
4a4d8108 19106+/* ---------------------------------------------------------------------- */
1308ab2a 19107+
027c5e7a
AM
19108+static inline void au_icntnr_init(struct au_icntnr *c)
19109+{
19110+#ifdef CONFIG_AUFS_DEBUG
19111+ c->vfs_inode.i_mode = 0;
19112+#endif
19113+}
19114+
cfc41e69 19115+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19116+{
537831f9
AM
19117+ unsigned int gen;
19118+ struct au_iinfo *iinfo;
be52b249 19119+ struct au_iigen *iigen;
537831f9
AM
19120+
19121+ iinfo = au_ii(inode);
be52b249
AM
19122+ iigen = &iinfo->ii_generation;
19123+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19124+ if (igflags)
19125+ *igflags = iigen->ig_flags;
be52b249
AM
19126+ gen = iigen->ig_generation;
19127+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19128+
19129+ return gen;
4a4d8108 19130+}
1308ab2a 19131+
4a4d8108
AM
19132+/* tiny test for inode number */
19133+/* tmpfs generation is too rough */
19134+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19135+{
19136+ struct au_iinfo *iinfo;
1308ab2a 19137+
4a4d8108
AM
19138+ iinfo = au_ii(inode);
19139+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19140+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19141+ && iinfo->ii_higen == h_inode->i_generation);
19142+}
1308ab2a 19143+
4a4d8108
AM
19144+static inline void au_iigen_dec(struct inode *inode)
19145+{
537831f9 19146+ struct au_iinfo *iinfo;
be52b249 19147+ struct au_iigen *iigen;
537831f9
AM
19148+
19149+ iinfo = au_ii(inode);
be52b249
AM
19150+ iigen = &iinfo->ii_generation;
19151+ spin_lock(&iigen->ig_spin);
19152+ iigen->ig_generation--;
19153+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19154+}
19155+
19156+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19157+{
19158+ int err;
19159+
19160+ err = 0;
537831f9 19161+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19162+ err = -EIO;
19163+
19164+ return err;
4a4d8108 19165+}
1308ab2a 19166+
4a4d8108 19167+/* ---------------------------------------------------------------------- */
1308ab2a 19168+
5afbbe0d
AM
19169+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19170+ aufs_bindex_t bindex)
19171+{
19172+ return iinfo->ii_hinode + bindex;
19173+}
19174+
19175+static inline int au_is_bad_inode(struct inode *inode)
19176+{
19177+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19178+}
19179+
4a4d8108
AM
19180+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19181+ aufs_bindex_t bindex)
19182+{
19183+ IiMustAnyLock(inode);
5afbbe0d 19184+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19185+}
1308ab2a 19186+
5afbbe0d 19187+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19188+{
19189+ IiMustAnyLock(inode);
5afbbe0d 19190+ return au_ii(inode)->ii_btop;
4a4d8108 19191+}
1308ab2a 19192+
5afbbe0d 19193+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19194+{
19195+ IiMustAnyLock(inode);
5afbbe0d 19196+ return au_ii(inode)->ii_bbot;
4a4d8108 19197+}
1308ab2a 19198+
4a4d8108
AM
19199+static inline struct au_vdir *au_ivdir(struct inode *inode)
19200+{
19201+ IiMustAnyLock(inode);
19202+ return au_ii(inode)->ii_vdir;
19203+}
1308ab2a 19204+
4a4d8108
AM
19205+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19206+{
19207+ IiMustAnyLock(inode);
5afbbe0d 19208+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19209+}
1308ab2a 19210+
5afbbe0d 19211+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19212+{
4a4d8108 19213+ IiMustWriteLock(inode);
5afbbe0d 19214+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19215+}
1308ab2a 19216+
5afbbe0d 19217+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19218+{
19219+ IiMustWriteLock(inode);
5afbbe0d 19220+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19221+}
19222+
4a4d8108
AM
19223+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19224+{
19225+ IiMustWriteLock(inode);
19226+ au_ii(inode)->ii_vdir = vdir;
19227+}
1facf9fc 19228+
4a4d8108 19229+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19230+{
4a4d8108 19231+ IiMustAnyLock(inode);
5afbbe0d 19232+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19233+}
dece6358 19234+
4a4d8108 19235+/* ---------------------------------------------------------------------- */
1facf9fc 19236+
4a4d8108
AM
19237+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19238+{
19239+ if (pin)
19240+ return pin->parent;
19241+ return NULL;
1facf9fc 19242+}
19243+
4a4d8108 19244+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19245+{
4a4d8108
AM
19246+ if (pin && pin->hdir)
19247+ return pin->hdir->hi_inode;
19248+ return NULL;
1308ab2a 19249+}
1facf9fc 19250+
4a4d8108
AM
19251+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19252+{
19253+ if (pin)
19254+ return pin->hdir;
19255+ return NULL;
19256+}
1facf9fc 19257+
4a4d8108 19258+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19259+{
4a4d8108
AM
19260+ if (pin)
19261+ pin->dentry = dentry;
19262+}
1308ab2a 19263+
4a4d8108
AM
19264+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19265+ unsigned char lflag)
19266+{
19267+ if (pin) {
7f207e10 19268+ if (lflag)
4a4d8108 19269+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19270+ else
4a4d8108 19271+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19272+ }
4a4d8108
AM
19273+}
19274+
7e9cd9fe 19275+#if 0 /* reserved */
4a4d8108
AM
19276+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19277+{
19278+ if (pin) {
19279+ dput(pin->parent);
19280+ pin->parent = dget(parent);
1facf9fc 19281+ }
4a4d8108 19282+}
7e9cd9fe 19283+#endif
1facf9fc 19284+
4a4d8108
AM
19285+/* ---------------------------------------------------------------------- */
19286+
027c5e7a 19287+struct au_branch;
4a4d8108
AM
19288+#ifdef CONFIG_AUFS_HNOTIFY
19289+struct au_hnotify_op {
19290+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19291+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19292+
19293+ /*
19294+ * if it returns true, the the caller should free hinode->hi_notify,
19295+ * otherwise ->free() frees it.
19296+ */
19297+ int (*free)(struct au_hinode *hinode,
19298+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19299+
19300+ void (*fin)(void);
19301+ int (*init)(void);
027c5e7a
AM
19302+
19303+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19304+ void (*fin_br)(struct au_branch *br);
19305+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19306+};
19307+
19308+/* hnotify.c */
027c5e7a 19309+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19310+void au_hn_free(struct au_hinode *hinode);
19311+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19312+void au_hn_reset(struct inode *inode, unsigned int flags);
19313+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
fbc438ed 19314+ const struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19315+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19316+int au_hnotify_init_br(struct au_branch *br, int perm);
19317+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19318+int __init au_hnotify_init(void);
19319+void au_hnotify_fin(void);
19320+
7f207e10 19321+/* hfsnotify.c */
4a4d8108
AM
19322+extern const struct au_hnotify_op au_hnotify_op;
19323+
19324+static inline
19325+void au_hn_init(struct au_hinode *hinode)
19326+{
19327+ hinode->hi_notify = NULL;
1308ab2a 19328+}
19329+
53392da6
AM
19330+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19331+{
19332+ return hinode->hi_notify;
19333+}
19334+
4a4d8108 19335+#else
c1595e42
JR
19336+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19337+ struct au_hinode *hinode __maybe_unused,
19338+ struct inode *inode __maybe_unused)
19339+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19340+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19341+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19342+ int do_set __maybe_unused)
19343+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19344+ unsigned int flags __maybe_unused)
027c5e7a
AM
19345+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19346+ struct au_branch *br __maybe_unused,
19347+ int perm __maybe_unused)
19348+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19349+ int perm __maybe_unused)
19350+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19351+AuStubInt0(__init au_hnotify_init, void)
19352+AuStubVoid(au_hnotify_fin, void)
19353+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19354+#endif /* CONFIG_AUFS_HNOTIFY */
19355+
19356+static inline void au_hn_suspend(struct au_hinode *hdir)
19357+{
19358+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19359+}
19360+
4a4d8108 19361+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19362+{
4a4d8108
AM
19363+ au_hn_ctl(hdir, /*do_set*/1);
19364+}
1308ab2a 19365+
5afbbe0d 19366+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19367+{
febd17d6 19368+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19369+ au_hn_suspend(hdir);
19370+}
dece6358 19371+
5afbbe0d 19372+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19373+ unsigned int sc __maybe_unused)
19374+{
febd17d6 19375+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19376+ au_hn_suspend(hdir);
1facf9fc 19377+}
1facf9fc 19378+
8b6a4947
AM
19379+#if 0 /* unused */
19380+#include "vfsub.h"
3c1bdaff
AM
19381+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19382+ unsigned int sc)
19383+{
be118d29 19384+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
19385+ au_hn_suspend(hdir);
19386+}
8b6a4947 19387+#endif
3c1bdaff 19388+
5afbbe0d 19389+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19390+{
19391+ au_hn_resume(hdir);
febd17d6 19392+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19393+}
19394+
19395+#endif /* __KERNEL__ */
19396+#endif /* __AUFS_INODE_H__ */
7f207e10 19397diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
eca34b5c 19398--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
016522bc 19399+++ linux/fs/aufs/ioctl.c 2020-01-27 10:57:18.175538316 +0100
062440b3 19400@@ -0,0 +1,220 @@
cd7a4cd9 19401+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19402+/*
016522bc 19403+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
19404+ *
19405+ * This program, aufs is free software; you can redistribute it and/or modify
19406+ * it under the terms of the GNU General Public License as published by
19407+ * the Free Software Foundation; either version 2 of the License, or
19408+ * (at your option) any later version.
19409+ *
19410+ * This program is distributed in the hope that it will be useful,
19411+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19412+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19413+ * GNU General Public License for more details.
19414+ *
19415+ * You should have received a copy of the GNU General Public License
523b37e3 19416+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19417+ */
19418+
19419+/*
19420+ * ioctl
19421+ * plink-management and readdir in userspace.
19422+ * assist the pathconf(3) wrapper library.
c2b27bf2 19423+ * move-down
076b876e 19424+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19425+ */
19426+
c2b27bf2
AM
19427+#include <linux/compat.h>
19428+#include <linux/file.h>
4a4d8108
AM
19429+#include "aufs.h"
19430+
1e00d052 19431+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19432+{
19433+ int err, fd;
5afbbe0d 19434+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19435+ struct file *h_file;
19436+ struct super_block *sb;
19437+ struct dentry *root;
1e00d052
AM
19438+ struct au_branch *br;
19439+ struct aufs_wbr_fd wbrfd = {
19440+ .oflags = au_dir_roflags,
19441+ .brid = -1
19442+ };
19443+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19444+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19445+
1e00d052
AM
19446+ AuDebugOn(wbrfd.oflags & ~valid);
19447+
19448+ if (arg) {
19449+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19450+ if (unlikely(err)) {
19451+ err = -EFAULT;
19452+ goto out;
19453+ }
19454+
19455+ err = -EINVAL;
19456+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19457+ wbrfd.oflags |= au_dir_roflags;
19458+ AuDbg("0%o\n", wbrfd.oflags);
19459+ if (unlikely(wbrfd.oflags & ~valid))
19460+ goto out;
19461+ }
19462+
2000de60 19463+ fd = get_unused_fd_flags(0);
1e00d052
AM
19464+ err = fd;
19465+ if (unlikely(fd < 0))
4a4d8108 19466+ goto out;
4a4d8108 19467+
1e00d052 19468+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19469+ wbi = 0;
1e00d052 19470+ br = NULL;
4a4d8108
AM
19471+ sb = path->dentry->d_sb;
19472+ root = sb->s_root;
19473+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19474+ bbot = au_sbbot(sb);
1e00d052
AM
19475+ if (wbrfd.brid >= 0) {
19476+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19477+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19478+ goto out_unlock;
19479+ }
19480+
19481+ h_file = ERR_PTR(-ENOENT);
19482+ br = au_sbr(sb, wbi);
19483+ if (!au_br_writable(br->br_perm)) {
19484+ if (arg)
19485+ goto out_unlock;
19486+
19487+ bindex = wbi + 1;
19488+ wbi = -1;
5afbbe0d 19489+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19490+ br = au_sbr(sb, bindex);
19491+ if (au_br_writable(br->br_perm)) {
4a4d8108 19492+ wbi = bindex;
1e00d052 19493+ br = au_sbr(sb, wbi);
4a4d8108
AM
19494+ break;
19495+ }
19496+ }
4a4d8108
AM
19497+ }
19498+ AuDbg("wbi %d\n", wbi);
1e00d052 19499+ if (wbi >= 0)
392086de
AM
19500+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19501+ /*force_wr*/0);
1e00d052
AM
19502+
19503+out_unlock:
4a4d8108
AM
19504+ aufs_read_unlock(root, AuLock_IR);
19505+ err = PTR_ERR(h_file);
19506+ if (IS_ERR(h_file))
19507+ goto out_fd;
19508+
acd2b654 19509+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
4a4d8108
AM
19510+ fd_install(fd, h_file);
19511+ err = fd;
19512+ goto out; /* success */
19513+
4f0767ce 19514+out_fd:
4a4d8108 19515+ put_unused_fd(fd);
4f0767ce 19516+out:
1e00d052 19517+ AuTraceErr(err);
4a4d8108
AM
19518+ return err;
19519+}
19520+
19521+/* ---------------------------------------------------------------------- */
19522+
19523+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19524+{
19525+ long err;
c1595e42 19526+ struct dentry *dentry;
4a4d8108
AM
19527+
19528+ switch (cmd) {
4a4d8108
AM
19529+ case AUFS_CTL_RDU:
19530+ case AUFS_CTL_RDU_INO:
19531+ err = au_rdu_ioctl(file, cmd, arg);
19532+ break;
19533+
19534+ case AUFS_CTL_WBR_FD:
1e00d052 19535+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19536+ break;
19537+
027c5e7a
AM
19538+ case AUFS_CTL_IBUSY:
19539+ err = au_ibusy_ioctl(file, arg);
19540+ break;
19541+
076b876e
AM
19542+ case AUFS_CTL_BRINFO:
19543+ err = au_brinfo_ioctl(file, arg);
19544+ break;
19545+
19546+ case AUFS_CTL_FHSM_FD:
2000de60 19547+ dentry = file->f_path.dentry;
c1595e42
JR
19548+ if (IS_ROOT(dentry))
19549+ err = au_fhsm_fd(dentry->d_sb, arg);
19550+ else
19551+ err = -ENOTTY;
076b876e
AM
19552+ break;
19553+
4a4d8108
AM
19554+ default:
19555+ /* do not call the lower */
19556+ AuDbg("0x%x\n", cmd);
19557+ err = -ENOTTY;
19558+ }
19559+
19560+ AuTraceErr(err);
19561+ return err;
19562+}
19563+
19564+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19565+{
19566+ long err;
19567+
19568+ switch (cmd) {
c2b27bf2 19569+ case AUFS_CTL_MVDOWN:
2000de60 19570+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19571+ break;
19572+
4a4d8108 19573+ case AUFS_CTL_WBR_FD:
1e00d052 19574+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19575+ break;
19576+
19577+ default:
19578+ /* do not call the lower */
19579+ AuDbg("0x%x\n", cmd);
19580+ err = -ENOTTY;
19581+ }
19582+
19583+ AuTraceErr(err);
19584+ return err;
19585+}
b752ccd1
AM
19586+
19587+#ifdef CONFIG_COMPAT
19588+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19589+ unsigned long arg)
19590+{
19591+ long err;
19592+
19593+ switch (cmd) {
19594+ case AUFS_CTL_RDU:
19595+ case AUFS_CTL_RDU_INO:
19596+ err = au_rdu_compat_ioctl(file, cmd, arg);
19597+ break;
19598+
027c5e7a
AM
19599+ case AUFS_CTL_IBUSY:
19600+ err = au_ibusy_compat_ioctl(file, arg);
19601+ break;
19602+
076b876e
AM
19603+ case AUFS_CTL_BRINFO:
19604+ err = au_brinfo_compat_ioctl(file, arg);
19605+ break;
19606+
b752ccd1
AM
19607+ default:
19608+ err = aufs_ioctl_dir(file, cmd, arg);
19609+ }
19610+
19611+ AuTraceErr(err);
19612+ return err;
19613+}
19614+
b752ccd1
AM
19615+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19616+ unsigned long arg)
19617+{
19618+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19619+}
19620+#endif
7f207e10 19621diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
eca34b5c 19622--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
016522bc 19623+++ linux/fs/aufs/i_op_add.c 2020-01-27 10:57:18.172204883 +0100
eca801bf 19624@@ -0,0 +1,936 @@
cd7a4cd9 19625+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19626+/*
016522bc 19627+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
19628+ *
19629+ * This program, aufs is free software; you can redistribute it and/or modify
19630+ * it under the terms of the GNU General Public License as published by
19631+ * the Free Software Foundation; either version 2 of the License, or
19632+ * (at your option) any later version.
19633+ *
19634+ * This program is distributed in the hope that it will be useful,
19635+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19636+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19637+ * GNU General Public License for more details.
19638+ *
19639+ * You should have received a copy of the GNU General Public License
523b37e3 19640+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19641+ */
19642+
19643+/*
19644+ * inode operations (add entry)
19645+ */
19646+
eca801bf 19647+#include <linux/iversion.h>
4a4d8108
AM
19648+#include "aufs.h"
19649+
19650+/*
19651+ * final procedure of adding a new entry, except link(2).
19652+ * remove whiteout, instantiate, copyup the parent dir's times and size
19653+ * and update version.
19654+ * if it failed, re-create the removed whiteout.
19655+ */
19656+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19657+ struct dentry *wh_dentry, struct dentry *dentry)
19658+{
19659+ int err, rerr;
19660+ aufs_bindex_t bwh;
19661+ struct path h_path;
076b876e 19662+ struct super_block *sb;
4a4d8108
AM
19663+ struct inode *inode, *h_dir;
19664+ struct dentry *wh;
19665+
19666+ bwh = -1;
076b876e 19667+ sb = dir->i_sb;
4a4d8108 19668+ if (wh_dentry) {
5527c038 19669+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19670+ IMustLock(h_dir);
19671+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19672+ bwh = au_dbwh(dentry);
19673+ h_path.dentry = wh_dentry;
076b876e 19674+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19675+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19676+ dentry);
19677+ if (unlikely(err))
19678+ goto out;
19679+ }
19680+
19681+ inode = au_new_inode(dentry, /*must_new*/1);
19682+ if (!IS_ERR(inode)) {
19683+ d_instantiate(dentry, inode);
5527c038 19684+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19685+ IMustLock(dir);
b912730e 19686+ au_dir_ts(dir, bindex);
be118d29 19687+ inode_inc_iversion(dir);
076b876e 19688+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19689+ return 0; /* success */
19690+ }
19691+
19692+ err = PTR_ERR(inode);
19693+ if (!wh_dentry)
19694+ goto out;
19695+
19696+ /* revert */
19697+ /* dir inode is locked */
19698+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19699+ rerr = PTR_ERR(wh);
19700+ if (IS_ERR(wh)) {
523b37e3
AM
19701+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19702+ dentry, err, rerr);
4a4d8108
AM
19703+ err = -EIO;
19704+ } else
19705+ dput(wh);
19706+
4f0767ce 19707+out:
4a4d8108
AM
19708+ return err;
19709+}
19710+
027c5e7a
AM
19711+static int au_d_may_add(struct dentry *dentry)
19712+{
19713+ int err;
19714+
19715+ err = 0;
19716+ if (unlikely(d_unhashed(dentry)))
19717+ err = -ENOENT;
5527c038 19718+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19719+ err = -EEXIST;
19720+ return err;
19721+}
19722+
4a4d8108
AM
19723+/*
19724+ * simple tests for the adding inode operations.
19725+ * following the checks in vfs, plus the parent-child relationship.
19726+ */
19727+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19728+ struct dentry *h_parent, int isdir)
19729+{
19730+ int err;
19731+ umode_t h_mode;
19732+ struct dentry *h_dentry;
19733+ struct inode *h_inode;
19734+
19735+ err = -ENAMETOOLONG;
19736+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19737+ goto out;
19738+
19739+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19740+ if (d_really_is_negative(dentry)) {
4a4d8108 19741+ err = -EEXIST;
5527c038 19742+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19743+ goto out;
19744+ } else {
19745+ /* rename(2) case */
19746+ err = -EIO;
5527c038
JR
19747+ if (unlikely(d_is_negative(h_dentry)))
19748+ goto out;
19749+ h_inode = d_inode(h_dentry);
19750+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19751+ goto out;
19752+
19753+ h_mode = h_inode->i_mode;
19754+ if (!isdir) {
19755+ err = -EISDIR;
19756+ if (unlikely(S_ISDIR(h_mode)))
19757+ goto out;
19758+ } else if (unlikely(!S_ISDIR(h_mode))) {
19759+ err = -ENOTDIR;
19760+ goto out;
19761+ }
19762+ }
19763+
19764+ err = 0;
19765+ /* expected parent dir is locked */
19766+ if (unlikely(h_parent != h_dentry->d_parent))
19767+ err = -EIO;
19768+
4f0767ce 19769+out:
4a4d8108
AM
19770+ AuTraceErr(err);
19771+ return err;
19772+}
19773+
19774+/*
19775+ * initial procedure of adding a new entry.
19776+ * prepare writable branch and the parent dir, lock it,
19777+ * and lookup whiteout for the new entry.
19778+ */
19779+static struct dentry*
19780+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19781+ struct dentry *src_dentry, struct au_pin *pin,
19782+ struct au_wr_dir_args *wr_dir_args)
19783+{
19784+ struct dentry *wh_dentry, *h_parent;
19785+ struct super_block *sb;
19786+ struct au_branch *br;
19787+ int err;
19788+ unsigned int udba;
19789+ aufs_bindex_t bcpup;
19790+
523b37e3 19791+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19792+
19793+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19794+ bcpup = err;
19795+ wh_dentry = ERR_PTR(err);
19796+ if (unlikely(err < 0))
19797+ goto out;
19798+
19799+ sb = dentry->d_sb;
19800+ udba = au_opt_udba(sb);
19801+ err = au_pin(pin, dentry, bcpup, udba,
19802+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19803+ wh_dentry = ERR_PTR(err);
19804+ if (unlikely(err))
19805+ goto out;
19806+
19807+ h_parent = au_pinned_h_parent(pin);
19808+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19809+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19810+ err = au_may_add(dentry, bcpup, h_parent,
19811+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19812+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19813+ err = -ENAMETOOLONG;
19814+ wh_dentry = ERR_PTR(err);
19815+ if (unlikely(err))
19816+ goto out_unpin;
19817+
19818+ br = au_sbr(sb, bcpup);
19819+ if (dt) {
19820+ struct path tmp = {
19821+ .dentry = h_parent,
86dc4139 19822+ .mnt = au_br_mnt(br)
4a4d8108
AM
19823+ };
19824+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19825+ }
19826+
19827+ wh_dentry = NULL;
19828+ if (bcpup != au_dbwh(dentry))
19829+ goto out; /* success */
19830+
2000de60
JR
19831+ /*
19832+ * ENAMETOOLONG here means that if we allowed create such name, then it
19833+ * would not be able to removed in the future. So we don't allow such
19834+ * name here and we don't handle ENAMETOOLONG differently here.
19835+ */
4a4d8108
AM
19836+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19837+
4f0767ce 19838+out_unpin:
4a4d8108
AM
19839+ if (IS_ERR(wh_dentry))
19840+ au_unpin(pin);
4f0767ce 19841+out:
4a4d8108
AM
19842+ return wh_dentry;
19843+}
19844+
19845+/* ---------------------------------------------------------------------- */
19846+
19847+enum { Mknod, Symlink, Creat };
19848+struct simple_arg {
19849+ int type;
19850+ union {
19851+ struct {
b912730e
AM
19852+ umode_t mode;
19853+ bool want_excl;
19854+ bool try_aopen;
19855+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19856+ } c;
19857+ struct {
19858+ const char *symname;
19859+ } s;
19860+ struct {
7eafdf33 19861+ umode_t mode;
4a4d8108
AM
19862+ dev_t dev;
19863+ } m;
19864+ } u;
19865+};
19866+
19867+static int add_simple(struct inode *dir, struct dentry *dentry,
19868+ struct simple_arg *arg)
19869+{
076b876e 19870+ int err, rerr;
5afbbe0d 19871+ aufs_bindex_t btop;
4a4d8108 19872+ unsigned char created;
b912730e
AM
19873+ const unsigned char try_aopen
19874+ = (arg->type == Creat && arg->u.c.try_aopen);
acd2b654 19875+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
4a4d8108
AM
19876+ struct dentry *wh_dentry, *parent;
19877+ struct inode *h_dir;
b912730e
AM
19878+ struct super_block *sb;
19879+ struct au_branch *br;
acd2b654 19880+ /* to reduce stack size */
c2b27bf2
AM
19881+ struct {
19882+ struct au_dtime dt;
19883+ struct au_pin pin;
19884+ struct path h_path;
19885+ struct au_wr_dir_args wr_dir_args;
19886+ } *a;
4a4d8108 19887+
523b37e3 19888+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19889+ IMustLock(dir);
19890+
c2b27bf2
AM
19891+ err = -ENOMEM;
19892+ a = kmalloc(sizeof(*a), GFP_NOFS);
19893+ if (unlikely(!a))
19894+ goto out;
19895+ a->wr_dir_args.force_btgt = -1;
19896+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19897+
4a4d8108 19898+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
19899+ if (!try_aopen) {
19900+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19901+ if (unlikely(err))
19902+ goto out_free;
19903+ }
027c5e7a
AM
19904+ err = au_d_may_add(dentry);
19905+ if (unlikely(err))
19906+ goto out_unlock;
b912730e
AM
19907+ if (!try_aopen)
19908+ di_write_lock_parent(parent);
c2b27bf2
AM
19909+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19910+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
19911+ err = PTR_ERR(wh_dentry);
19912+ if (IS_ERR(wh_dentry))
027c5e7a 19913+ goto out_parent;
4a4d8108 19914+
5afbbe0d 19915+ btop = au_dbtop(dentry);
b912730e 19916+ sb = dentry->d_sb;
5afbbe0d
AM
19917+ br = au_sbr(sb, btop);
19918+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 19919+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 19920+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
19921+ switch (arg->type) {
19922+ case Creat:
acd2b654 19923+ if (!try_aopen || !h_dir->i_op->atomic_open) {
b912730e
AM
19924+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19925+ arg->u.c.want_excl);
acd2b654
AM
19926+ created = !err;
19927+ if (!err && try_aopen)
19928+ aopen->file->f_mode |= FMODE_CREATED;
19929+ } else {
19930+ aopen->br = br;
19931+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19932+ AuDbg("err %d\n", err);
19933+ AuDbgFile(aopen->file);
19934+ created = err >= 0
19935+ && !!(aopen->file->f_mode & FMODE_CREATED);
19936+ }
4a4d8108
AM
19937+ break;
19938+ case Symlink:
c2b27bf2 19939+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
acd2b654 19940+ created = !err;
4a4d8108
AM
19941+ break;
19942+ case Mknod:
c2b27bf2
AM
19943+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19944+ arg->u.m.dev);
acd2b654 19945+ created = !err;
4a4d8108
AM
19946+ break;
19947+ default:
19948+ BUG();
19949+ }
acd2b654
AM
19950+ if (unlikely(err < 0))
19951+ goto out_unpin;
19952+
19953+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108 19954+ if (!err)
acd2b654 19955+ goto out_unpin; /* success */
4a4d8108
AM
19956+
19957+ /* revert */
acd2b654 19958+ if (created /* && d_is_positive(a->h_path.dentry) */) {
523b37e3
AM
19959+ /* no delegation since it is just created */
19960+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19961+ /*force*/0);
4a4d8108 19962+ if (rerr) {
523b37e3
AM
19963+ AuIOErr("%pd revert failure(%d, %d)\n",
19964+ dentry, err, rerr);
4a4d8108
AM
19965+ err = -EIO;
19966+ }
c2b27bf2 19967+ au_dtime_revert(&a->dt);
4a4d8108 19968+ }
acd2b654
AM
19969+ if (try_aopen && h_dir->i_op->atomic_open
19970+ && (aopen->file->f_mode & FMODE_OPENED))
19971+ /* aopen->file is still opened */
19972+ au_lcnt_dec(&aopen->br->br_nfiles);
4a4d8108 19973+
acd2b654 19974+out_unpin:
c2b27bf2 19975+ au_unpin(&a->pin);
4a4d8108 19976+ dput(wh_dentry);
027c5e7a 19977+out_parent:
b912730e
AM
19978+ if (!try_aopen)
19979+ di_write_unlock(parent);
027c5e7a 19980+out_unlock:
4a4d8108 19981+ if (unlikely(err)) {
5afbbe0d 19982+ au_update_dbtop(dentry);
4a4d8108
AM
19983+ d_drop(dentry);
19984+ }
b912730e
AM
19985+ if (!try_aopen)
19986+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 19987+out_free:
9f237c51 19988+ au_kfree_rcu(a);
027c5e7a 19989+out:
4a4d8108
AM
19990+ return err;
19991+}
19992+
7eafdf33
AM
19993+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19994+ dev_t dev)
4a4d8108
AM
19995+{
19996+ struct simple_arg arg = {
19997+ .type = Mknod,
19998+ .u.m = {
19999+ .mode = mode,
20000+ .dev = dev
20001+ }
20002+ };
20003+ return add_simple(dir, dentry, &arg);
20004+}
20005+
20006+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20007+{
20008+ struct simple_arg arg = {
20009+ .type = Symlink,
20010+ .u.s.symname = symname
20011+ };
20012+ return add_simple(dir, dentry, &arg);
20013+}
20014+
7eafdf33 20015+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 20016+ bool want_excl)
4a4d8108
AM
20017+{
20018+ struct simple_arg arg = {
20019+ .type = Creat,
20020+ .u.c = {
b4510431
AM
20021+ .mode = mode,
20022+ .want_excl = want_excl
4a4d8108
AM
20023+ }
20024+ };
20025+ return add_simple(dir, dentry, &arg);
20026+}
20027+
b912730e
AM
20028+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20029+ struct vfsub_aopen_args *aopen_args)
20030+{
20031+ struct simple_arg arg = {
20032+ .type = Creat,
20033+ .u.c = {
20034+ .mode = aopen_args->create_mode,
20035+ .want_excl = aopen_args->open_flag & O_EXCL,
20036+ .try_aopen = true,
20037+ .aopen = aopen_args
20038+ }
20039+ };
20040+ return add_simple(dir, dentry, &arg);
20041+}
20042+
38d290e6
JR
20043+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20044+{
20045+ int err;
20046+ aufs_bindex_t bindex;
20047+ struct super_block *sb;
20048+ struct dentry *parent, *h_parent, *h_dentry;
20049+ struct inode *h_dir, *inode;
20050+ struct vfsmount *h_mnt;
20051+ struct au_wr_dir_args wr_dir_args = {
20052+ .force_btgt = -1,
20053+ .flags = AuWrDir_TMPFILE
20054+ };
20055+
20056+ /* copy-up may happen */
febd17d6 20057+ inode_lock(dir);
38d290e6
JR
20058+
20059+ sb = dir->i_sb;
20060+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20061+ if (unlikely(err))
20062+ goto out;
20063+
20064+ err = au_di_init(dentry);
20065+ if (unlikely(err))
20066+ goto out_si;
20067+
20068+ err = -EBUSY;
20069+ parent = d_find_any_alias(dir);
20070+ AuDebugOn(!parent);
20071+ di_write_lock_parent(parent);
5527c038 20072+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
20073+ goto out_parent;
20074+
20075+ err = au_digen_test(parent, au_sigen(sb));
20076+ if (unlikely(err))
20077+ goto out_parent;
20078+
5afbbe0d
AM
20079+ bindex = au_dbtop(parent);
20080+ au_set_dbtop(dentry, bindex);
20081+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20082+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20083+ bindex = err;
20084+ if (unlikely(err < 0))
20085+ goto out_parent;
20086+
20087+ err = -EOPNOTSUPP;
20088+ h_dir = au_h_iptr(dir, bindex);
20089+ if (unlikely(!h_dir->i_op->tmpfile))
20090+ goto out_parent;
20091+
20092+ h_mnt = au_sbr_mnt(sb, bindex);
20093+ err = vfsub_mnt_want_write(h_mnt);
20094+ if (unlikely(err))
20095+ goto out_parent;
20096+
20097+ h_parent = au_h_dptr(parent, bindex);
521ced18
JR
20098+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20099+ if (IS_ERR(h_dentry)) {
20100+ err = PTR_ERR(h_dentry);
38d290e6 20101+ goto out_mnt;
521ced18 20102+ }
38d290e6 20103+
5afbbe0d
AM
20104+ au_set_dbtop(dentry, bindex);
20105+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20106+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20107+ inode = au_new_inode(dentry, /*must_new*/1);
20108+ if (IS_ERR(inode)) {
20109+ err = PTR_ERR(inode);
20110+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20111+ au_set_dbtop(dentry, -1);
20112+ au_set_dbbot(dentry, -1);
38d290e6
JR
20113+ } else {
20114+ if (!inode->i_nlink)
20115+ set_nlink(inode, 1);
20116+ d_tmpfile(dentry, inode);
20117+ au_di(dentry)->di_tmpfile = 1;
20118+
20119+ /* update without i_mutex */
5afbbe0d 20120+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20121+ au_cpup_attr_timesizes(dir);
20122+ }
38d290e6 20123+ dput(h_dentry);
521ced18 20124+
38d290e6
JR
20125+out_mnt:
20126+ vfsub_mnt_drop_write(h_mnt);
20127+out_parent:
20128+ di_write_unlock(parent);
20129+ dput(parent);
20130+ di_write_unlock(dentry);
5afbbe0d 20131+ if (unlikely(err)) {
38d290e6
JR
20132+ au_di_fin(dentry);
20133+ dentry->d_fsdata = NULL;
20134+ }
20135+out_si:
20136+ si_read_unlock(sb);
20137+out:
febd17d6 20138+ inode_unlock(dir);
38d290e6
JR
20139+ return err;
20140+}
20141+
4a4d8108
AM
20142+/* ---------------------------------------------------------------------- */
20143+
20144+struct au_link_args {
20145+ aufs_bindex_t bdst, bsrc;
20146+ struct au_pin pin;
20147+ struct path h_path;
20148+ struct dentry *src_parent, *parent;
20149+};
20150+
20151+static int au_cpup_before_link(struct dentry *src_dentry,
20152+ struct au_link_args *a)
20153+{
20154+ int err;
20155+ struct dentry *h_src_dentry;
c2b27bf2
AM
20156+ struct au_cp_generic cpg = {
20157+ .dentry = src_dentry,
20158+ .bdst = a->bdst,
20159+ .bsrc = a->bsrc,
20160+ .len = -1,
20161+ .pin = &a->pin,
20162+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20163+ };
4a4d8108
AM
20164+
20165+ di_read_lock_parent(a->src_parent, AuLock_IR);
20166+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20167+ if (unlikely(err))
20168+ goto out;
20169+
20170+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20171+ err = au_pin(&a->pin, src_dentry, a->bdst,
20172+ au_opt_udba(src_dentry->d_sb),
20173+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20174+ if (unlikely(err))
20175+ goto out;
367653fa 20176+
c2b27bf2 20177+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20178+ au_unpin(&a->pin);
20179+
4f0767ce 20180+out:
4a4d8108
AM
20181+ di_read_unlock(a->src_parent, AuLock_IR);
20182+ return err;
20183+}
20184+
86dc4139
AM
20185+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20186+ struct au_link_args *a)
4a4d8108
AM
20187+{
20188+ int err;
20189+ unsigned char plink;
5afbbe0d 20190+ aufs_bindex_t bbot;
4a4d8108 20191+ struct dentry *h_src_dentry;
523b37e3 20192+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20193+ struct super_block *sb;
20194+ struct file *h_file;
20195+
20196+ plink = 0;
20197+ h_inode = NULL;
20198+ sb = src_dentry->d_sb;
5527c038 20199+ inode = d_inode(src_dentry);
5afbbe0d 20200+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20201+ h_inode = au_h_iptr(inode, a->bdst);
20202+ if (!h_inode || !h_inode->i_nlink) {
20203+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20204+ bbot = au_dbbot(dentry);
20205+ if (bbot < a->bsrc)
20206+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20207+ au_set_h_dptr(dentry, a->bsrc,
20208+ dget(au_h_dptr(src_dentry, a->bsrc)));
20209+ dget(a->h_path.dentry);
20210+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20211+ AuDbg("temporary d_inode...\n");
20212+ spin_lock(&dentry->d_lock);
5527c038 20213+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20214+ spin_unlock(&dentry->d_lock);
392086de 20215+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20216+ if (IS_ERR(h_file))
4a4d8108 20217+ err = PTR_ERR(h_file);
86dc4139 20218+ else {
c2b27bf2
AM
20219+ struct au_cp_generic cpg = {
20220+ .dentry = dentry,
20221+ .bdst = a->bdst,
20222+ .bsrc = -1,
20223+ .len = -1,
20224+ .pin = &a->pin,
20225+ .flags = AuCpup_KEEPLINO
20226+ };
20227+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20228+ au_h_open_post(dentry, a->bsrc, h_file);
20229+ if (!err) {
20230+ dput(a->h_path.dentry);
20231+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20232+ } else
20233+ au_set_h_dptr(dentry, a->bdst,
20234+ a->h_path.dentry);
20235+ }
c1595e42 20236+ spin_lock(&dentry->d_lock);
86dc4139 20237+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20238+ spin_unlock(&dentry->d_lock);
20239+ AuDbg("temporary d_inode...done\n");
86dc4139 20240+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20241+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20242+ } else {
20243+ /* the inode of src_dentry already exists on a.bdst branch */
20244+ h_src_dentry = d_find_alias(h_inode);
20245+ if (!h_src_dentry && au_plink_test(inode)) {
20246+ plink = 1;
20247+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20248+ err = PTR_ERR(h_src_dentry);
20249+ if (IS_ERR(h_src_dentry))
20250+ goto out;
20251+
5527c038 20252+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20253+ dput(h_src_dentry);
20254+ h_src_dentry = NULL;
20255+ }
20256+
20257+ }
20258+ if (h_src_dentry) {
523b37e3 20259+ delegated = NULL;
4a4d8108 20260+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20261+ &a->h_path, &delegated);
20262+ if (unlikely(err == -EWOULDBLOCK)) {
20263+ pr_warn("cannot retry for NFSv4 delegation"
20264+ " for an internal link\n");
20265+ iput(delegated);
20266+ }
4a4d8108
AM
20267+ dput(h_src_dentry);
20268+ } else {
20269+ AuIOErr("no dentry found for hi%lu on b%d\n",
20270+ h_inode->i_ino, a->bdst);
20271+ err = -EIO;
20272+ }
20273+ }
20274+
20275+ if (!err && !plink)
20276+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20277+
20278+out:
2cbb1c4b 20279+ AuTraceErr(err);
4a4d8108
AM
20280+ return err;
20281+}
20282+
20283+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20284+ struct dentry *dentry)
20285+{
20286+ int err, rerr;
20287+ struct au_dtime dt;
20288+ struct au_link_args *a;
20289+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20290+ struct inode *inode, *delegated;
4a4d8108
AM
20291+ struct super_block *sb;
20292+ struct au_wr_dir_args wr_dir_args = {
20293+ /* .force_btgt = -1, */
20294+ .flags = AuWrDir_ADD_ENTRY
20295+ };
20296+
20297+ IMustLock(dir);
5527c038 20298+ inode = d_inode(src_dentry);
4a4d8108
AM
20299+ IMustLock(inode);
20300+
4a4d8108
AM
20301+ err = -ENOMEM;
20302+ a = kzalloc(sizeof(*a), GFP_NOFS);
20303+ if (unlikely(!a))
20304+ goto out;
20305+
20306+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20307+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20308+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20309+ if (unlikely(err))
20310+ goto out_kfree;
38d290e6 20311+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20312+ if (unlikely(err))
20313+ goto out_unlock;
20314+ err = au_d_may_add(dentry);
20315+ if (unlikely(err))
20316+ goto out_unlock;
e49829fe 20317+
4a4d8108 20318+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20319+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20320+
20321+ di_write_lock_parent(a->parent);
20322+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20323+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20324+ &wr_dir_args);
20325+ err = PTR_ERR(wh_dentry);
20326+ if (IS_ERR(wh_dentry))
027c5e7a 20327+ goto out_parent;
4a4d8108
AM
20328+
20329+ err = 0;
20330+ sb = dentry->d_sb;
5afbbe0d 20331+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20332+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20333+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20334+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20335+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20336+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20337+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20338+ if (!h_src_dentry) {
5afbbe0d 20339+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20340+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20341+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20342+ } else if (IS_ERR(h_src_dentry)) {
20343+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20344+ goto out_parent;
38d290e6 20345+ }
2cbb1c4b 20346+
f2c43d5f
AM
20347+ /*
20348+ * aufs doesn't touch the credential so
acd2b654 20349+ * security_dentry_create_files_as() is unnecessary.
f2c43d5f 20350+ */
4a4d8108
AM
20351+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20352+ if (a->bdst < a->bsrc
20353+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20354+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20355+ else {
20356+ delegated = NULL;
4a4d8108 20357+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20358+ &a->h_path, &delegated);
20359+ if (unlikely(err == -EWOULDBLOCK)) {
20360+ pr_warn("cannot retry for NFSv4 delegation"
20361+ " for an internal link\n");
20362+ iput(delegated);
20363+ }
20364+ }
2cbb1c4b 20365+ dput(h_src_dentry);
4a4d8108
AM
20366+ } else {
20367+ /*
20368+ * copyup src_dentry to the branch we process,
20369+ * and then link(2) to it.
20370+ */
2cbb1c4b 20371+ dput(h_src_dentry);
4a4d8108
AM
20372+ if (a->bdst < a->bsrc
20373+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20374+ au_unpin(&a->pin);
20375+ di_write_unlock(a->parent);
20376+ err = au_cpup_before_link(src_dentry, a);
20377+ di_write_lock_parent(a->parent);
20378+ if (!err)
20379+ err = au_pin(&a->pin, dentry, a->bdst,
20380+ au_opt_udba(sb),
20381+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20382+ if (unlikely(err))
20383+ goto out_wh;
20384+ }
20385+ if (!err) {
20386+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20387+ err = -ENOENT;
5527c038 20388+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20389+ delegated = NULL;
4a4d8108
AM
20390+ err = vfsub_link(h_src_dentry,
20391+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20392+ &a->h_path, &delegated);
20393+ if (unlikely(err == -EWOULDBLOCK)) {
20394+ pr_warn("cannot retry"
20395+ " for NFSv4 delegation"
20396+ " for an internal link\n");
20397+ iput(delegated);
20398+ }
20399+ }
4a4d8108
AM
20400+ }
20401+ }
20402+ if (unlikely(err))
20403+ goto out_unpin;
20404+
20405+ if (wh_dentry) {
20406+ a->h_path.dentry = wh_dentry;
20407+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20408+ dentry);
20409+ if (unlikely(err))
20410+ goto out_revert;
20411+ }
20412+
b912730e 20413+ au_dir_ts(dir, a->bdst);
be118d29 20414+ inode_inc_iversion(dir);
4a4d8108
AM
20415+ inc_nlink(inode);
20416+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20417+ d_instantiate(dentry, au_igrab(inode));
20418+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20419+ /* some filesystem calls d_drop() */
20420+ d_drop(dentry);
076b876e
AM
20421+ /* some filesystems consume an inode even hardlink */
20422+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20423+ goto out_unpin; /* success */
20424+
4f0767ce 20425+out_revert:
523b37e3
AM
20426+ /* no delegation since it is just created */
20427+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20428+ /*delegated*/NULL, /*force*/0);
027c5e7a 20429+ if (unlikely(rerr)) {
523b37e3 20430+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20431+ err = -EIO;
20432+ }
4a4d8108 20433+ au_dtime_revert(&dt);
4f0767ce 20434+out_unpin:
4a4d8108 20435+ au_unpin(&a->pin);
4f0767ce 20436+out_wh:
4a4d8108 20437+ dput(wh_dentry);
027c5e7a
AM
20438+out_parent:
20439+ di_write_unlock(a->parent);
20440+ dput(a->src_parent);
4f0767ce 20441+out_unlock:
4a4d8108 20442+ if (unlikely(err)) {
5afbbe0d 20443+ au_update_dbtop(dentry);
4a4d8108
AM
20444+ d_drop(dentry);
20445+ }
4a4d8108 20446+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20447+out_kfree:
9f237c51 20448+ au_kfree_rcu(a);
4f0767ce 20449+out:
86dc4139 20450+ AuTraceErr(err);
4a4d8108
AM
20451+ return err;
20452+}
20453+
7eafdf33 20454+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
20455+{
20456+ int err, rerr;
20457+ aufs_bindex_t bindex;
20458+ unsigned char diropq;
20459+ struct path h_path;
20460+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20461+ struct inode *h_inode;
4a4d8108
AM
20462+ struct super_block *sb;
20463+ struct {
20464+ struct au_pin pin;
20465+ struct au_dtime dt;
20466+ } *a; /* reduce the stack usage */
20467+ struct au_wr_dir_args wr_dir_args = {
20468+ .force_btgt = -1,
20469+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20470+ };
20471+
20472+ IMustLock(dir);
20473+
20474+ err = -ENOMEM;
20475+ a = kmalloc(sizeof(*a), GFP_NOFS);
20476+ if (unlikely(!a))
20477+ goto out;
20478+
027c5e7a
AM
20479+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20480+ if (unlikely(err))
20481+ goto out_free;
20482+ err = au_d_may_add(dentry);
20483+ if (unlikely(err))
20484+ goto out_unlock;
20485+
4a4d8108
AM
20486+ parent = dentry->d_parent; /* dir inode is locked */
20487+ di_write_lock_parent(parent);
20488+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20489+ &a->pin, &wr_dir_args);
20490+ err = PTR_ERR(wh_dentry);
20491+ if (IS_ERR(wh_dentry))
027c5e7a 20492+ goto out_parent;
4a4d8108
AM
20493+
20494+ sb = dentry->d_sb;
5afbbe0d 20495+ bindex = au_dbtop(dentry);
4a4d8108
AM
20496+ h_path.dentry = au_h_dptr(dentry, bindex);
20497+ h_path.mnt = au_sbr_mnt(sb, bindex);
20498+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20499+ if (unlikely(err))
027c5e7a 20500+ goto out_unpin;
4a4d8108
AM
20501+
20502+ /* make the dir opaque */
20503+ diropq = 0;
febd17d6 20504+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20505+ if (wh_dentry
20506+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20507+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20508+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20509+ inode_unlock(h_inode);
4a4d8108
AM
20510+ err = PTR_ERR(opq_dentry);
20511+ if (IS_ERR(opq_dentry))
20512+ goto out_dir;
20513+ dput(opq_dentry);
20514+ diropq = 1;
20515+ }
20516+
20517+ err = epilog(dir, bindex, wh_dentry, dentry);
20518+ if (!err) {
20519+ inc_nlink(dir);
027c5e7a 20520+ goto out_unpin; /* success */
4a4d8108
AM
20521+ }
20522+
20523+ /* revert */
20524+ if (diropq) {
20525+ AuLabel(revert opq);
febd17d6 20526+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20527+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20528+ inode_unlock(h_inode);
4a4d8108 20529+ if (rerr) {
523b37e3
AM
20530+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20531+ dentry, err, rerr);
4a4d8108
AM
20532+ err = -EIO;
20533+ }
20534+ }
20535+
4f0767ce 20536+out_dir:
4a4d8108
AM
20537+ AuLabel(revert dir);
20538+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20539+ if (rerr) {
523b37e3
AM
20540+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20541+ dentry, err, rerr);
4a4d8108
AM
20542+ err = -EIO;
20543+ }
4a4d8108 20544+ au_dtime_revert(&a->dt);
027c5e7a 20545+out_unpin:
4a4d8108
AM
20546+ au_unpin(&a->pin);
20547+ dput(wh_dentry);
027c5e7a
AM
20548+out_parent:
20549+ di_write_unlock(parent);
20550+out_unlock:
4a4d8108 20551+ if (unlikely(err)) {
5afbbe0d 20552+ au_update_dbtop(dentry);
4a4d8108
AM
20553+ d_drop(dentry);
20554+ }
4a4d8108 20555+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20556+out_free:
9f237c51 20557+ au_kfree_rcu(a);
4f0767ce 20558+out:
4a4d8108
AM
20559+ return err;
20560+}
7f207e10 20561diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
eca34b5c 20562--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
016522bc 20563+++ linux/fs/aufs/i_op.c 2020-04-03 08:16:47.547461775 +0200
43982f53 20564@@ -0,0 +1,1498 @@
cd7a4cd9 20565+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20566+/*
016522bc 20567+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
20568+ *
20569+ * This program, aufs is free software; you can redistribute it and/or modify
20570+ * it under the terms of the GNU General Public License as published by
20571+ * the Free Software Foundation; either version 2 of the License, or
20572+ * (at your option) any later version.
20573+ *
20574+ * This program is distributed in the hope that it will be useful,
20575+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20576+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20577+ * GNU General Public License for more details.
20578+ *
20579+ * You should have received a copy of the GNU General Public License
523b37e3 20580+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20581+ */
1facf9fc 20582+
1308ab2a 20583+/*
4a4d8108 20584+ * inode operations (except add/del/rename)
1308ab2a 20585+ */
4a4d8108
AM
20586+
20587+#include <linux/device_cgroup.h>
20588+#include <linux/fs_stack.h>
eca801bf 20589+#include <linux/iversion.h>
4a4d8108
AM
20590+#include <linux/namei.h>
20591+#include <linux/security.h>
4a4d8108
AM
20592+#include "aufs.h"
20593+
1e00d052 20594+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20595+ struct path *h_path, int brperm)
1facf9fc 20596+{
1308ab2a 20597+ int err;
4a4d8108 20598+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 20599+
e2f27e51
AM
20600+ err = -EPERM;
20601+ if (write_mask && IS_IMMUTABLE(h_inode))
20602+ goto out;
20603+
4a4d8108 20604+ err = -EACCES;
e2f27e51
AM
20605+ if (((mask & MAY_EXEC)
20606+ && S_ISREG(h_inode->i_mode)
20607+ && (path_noexec(h_path)
cd7a4cd9 20608+ || !(h_inode->i_mode & 0111))))
4a4d8108
AM
20609+ goto out;
20610+
20611+ /*
20612+ * - skip the lower fs test in the case of write to ro branch.
20613+ * - nfs dir permission write check is optimized, but a policy for
20614+ * link/rename requires a real check.
2121bcd9 20615+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 20616+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
20617+ */
20618+ if ((write_mask && !au_br_writable(brperm))
20619+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20620+ && write_mask && !(mask & MAY_READ))
20621+ || !h_inode->i_op->permission) {
20622+ /* AuLabel(generic_permission); */
062440b3 20623+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
1e00d052 20624+ err = generic_permission(h_inode, mask);
b912730e
AM
20625+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20626+ err = h_inode->i_op->permission(h_inode, mask);
20627+ AuTraceErr(err);
1308ab2a 20628+ } else {
4a4d8108 20629+ /* AuLabel(h_inode->permission); */
1e00d052 20630+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
20631+ AuTraceErr(err);
20632+ }
1facf9fc 20633+
4a4d8108
AM
20634+ if (!err)
20635+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20636+ if (!err)
4a4d8108 20637+ err = security_inode_permission(h_inode, mask);
4a4d8108 20638+
4f0767ce 20639+out:
1308ab2a 20640+ return err;
20641+}
dece6358 20642+
1e00d052 20643+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 20644+{
20645+ int err;
5afbbe0d 20646+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20647+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20648+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20649+ struct inode *h_inode;
20650+ struct super_block *sb;
20651+ struct au_branch *br;
1facf9fc 20652+
027c5e7a 20653+ /* todo: support rcu-walk? */
1e00d052 20654+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20655+ return -ECHILD;
20656+
4a4d8108
AM
20657+ sb = inode->i_sb;
20658+ si_read_lock(sb, AuLock_FLUSH);
20659+ ii_read_lock_child(inode);
43982f53
AM
20660+#if 0 /* reserved for future use */
20661+ /*
20662+ * This test may be rather 'too much' since the test is essentially done
20663+ * in the aufs_lookup(). Theoretically it is possible that the inode
20664+ * generation doesn't match to the superblock's here. But it isn't a
20665+ * big deal I suppose.
20666+ */
027c5e7a
AM
20667+ err = au_iigen_test(inode, au_sigen(sb));
20668+ if (unlikely(err))
20669+ goto out;
20670+#endif
dece6358 20671+
076b876e
AM
20672+ if (!isdir
20673+ || write_mask
20674+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20675+ err = au_busy_or_stale();
5afbbe0d 20676+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20677+ if (unlikely(!h_inode
20678+ || (h_inode->i_mode & S_IFMT)
20679+ != (inode->i_mode & S_IFMT)))
20680+ goto out;
1facf9fc 20681+
4a4d8108 20682+ err = 0;
5afbbe0d 20683+ bindex = au_ibtop(inode);
4a4d8108 20684+ br = au_sbr(sb, bindex);
79b8bda9 20685+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20686+ if (write_mask
20687+ && !err
20688+ && !special_file(h_inode->i_mode)) {
20689+ /* test whether the upper writable branch exists */
20690+ err = -EROFS;
20691+ for (; bindex >= 0; bindex--)
20692+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20693+ err = 0;
20694+ break;
20695+ }
20696+ }
20697+ goto out;
20698+ }
dece6358 20699+
4a4d8108 20700+ /* non-write to dir */
1308ab2a 20701+ err = 0;
5afbbe0d
AM
20702+ bbot = au_ibbot(inode);
20703+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20704+ h_inode = au_h_iptr(inode, bindex);
20705+ if (h_inode) {
20706+ err = au_busy_or_stale();
20707+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20708+ break;
20709+
20710+ br = au_sbr(sb, bindex);
79b8bda9 20711+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20712+ br->br_perm);
20713+ }
20714+ }
1308ab2a 20715+
4f0767ce 20716+out:
4a4d8108
AM
20717+ ii_read_unlock(inode);
20718+ si_read_unlock(sb);
1308ab2a 20719+ return err;
20720+}
20721+
4a4d8108 20722+/* ---------------------------------------------------------------------- */
1facf9fc 20723+
4a4d8108 20724+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20725+ unsigned int flags)
4a4d8108
AM
20726+{
20727+ struct dentry *ret, *parent;
b752ccd1 20728+ struct inode *inode;
4a4d8108 20729+ struct super_block *sb;
1716fcea 20730+ int err, npositive;
dece6358 20731+
4a4d8108 20732+ IMustLock(dir);
1308ab2a 20733+
537831f9
AM
20734+ /* todo: support rcu-walk? */
20735+ ret = ERR_PTR(-ECHILD);
20736+ if (flags & LOOKUP_RCU)
20737+ goto out;
20738+
20739+ ret = ERR_PTR(-ENAMETOOLONG);
20740+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20741+ goto out;
20742+
4a4d8108 20743+ sb = dir->i_sb;
7f207e10
AM
20744+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20745+ ret = ERR_PTR(err);
20746+ if (unlikely(err))
20747+ goto out;
20748+
4a4d8108
AM
20749+ err = au_di_init(dentry);
20750+ ret = ERR_PTR(err);
20751+ if (unlikely(err))
7f207e10 20752+ goto out_si;
1308ab2a 20753+
9dbd164d 20754+ inode = NULL;
027c5e7a 20755+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20756+ parent = dentry->d_parent; /* dir inode is locked */
20757+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20758+ err = au_alive_dir(parent);
20759+ if (!err)
20760+ err = au_digen_test(parent, au_sigen(sb));
20761+ if (!err) {
5afbbe0d
AM
20762+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20763+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20764+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20765+ err = npositive;
20766+ }
4a4d8108 20767+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20768+ ret = ERR_PTR(err);
20769+ if (unlikely(err < 0))
20770+ goto out_unlock;
1308ab2a 20771+
4a4d8108 20772+ if (npositive) {
b752ccd1 20773+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20774+ if (IS_ERR(inode)) {
20775+ ret = (void *)inode;
20776+ inode = NULL;
20777+ goto out_unlock;
20778+ }
9dbd164d 20779+ }
4a4d8108 20780+
c1595e42
JR
20781+ if (inode)
20782+ atomic_inc(&inode->i_count);
4a4d8108 20783+ ret = d_splice_alias(inode, dentry);
43982f53 20784+#if 0 /* reserved for future use */
537831f9
AM
20785+ if (unlikely(d_need_lookup(dentry))) {
20786+ spin_lock(&dentry->d_lock);
20787+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20788+ spin_unlock(&dentry->d_lock);
20789+ } else
20790+#endif
c1595e42 20791+ if (inode) {
2000de60 20792+ if (!IS_ERR(ret)) {
c1595e42 20793+ iput(inode);
2000de60
JR
20794+ if (ret && ret != dentry)
20795+ ii_write_unlock(inode);
20796+ } else {
c1595e42
JR
20797+ ii_write_unlock(inode);
20798+ iput(inode);
20799+ inode = NULL;
20800+ }
7f207e10 20801+ }
1facf9fc 20802+
4f0767ce 20803+out_unlock:
4a4d8108 20804+ di_write_unlock(dentry);
7f207e10 20805+out_si:
4a4d8108 20806+ si_read_unlock(sb);
7f207e10 20807+out:
4a4d8108
AM
20808+ return ret;
20809+}
1facf9fc 20810+
4a4d8108 20811+/* ---------------------------------------------------------------------- */
1facf9fc 20812+
acd2b654
AM
20813+/*
20814+ * very dirty and complicated aufs ->atomic_open().
20815+ * aufs_atomic_open()
20816+ * + au_aopen_or_create()
20817+ * + add_simple()
20818+ * + vfsub_atomic_open()
20819+ * + branch fs ->atomic_open()
20820+ * may call the actual 'open' for h_file
20821+ * + inc br_nfiles only if opened
20822+ * + au_aopen_no_open() or au_aopen_do_open()
20823+ *
20824+ * au_aopen_do_open()
20825+ * + finish_open()
20826+ * + au_do_aopen()
20827+ * + au_do_open() the body of all 'open'
20828+ * + au_do_open_nondir()
20829+ * set the passed h_file
20830+ *
20831+ * au_aopen_no_open()
20832+ * + finish_no_open()
20833+ */
20834+
b912730e 20835+struct aopen_node {
8b6a4947 20836+ struct hlist_bl_node hblist;
b912730e
AM
20837+ struct file *file, *h_file;
20838+};
20839+
20840+static int au_do_aopen(struct inode *inode, struct file *file)
20841+{
8b6a4947
AM
20842+ struct hlist_bl_head *aopen;
20843+ struct hlist_bl_node *pos;
b912730e
AM
20844+ struct aopen_node *node;
20845+ struct au_do_open_args args = {
8b6a4947
AM
20846+ .aopen = 1,
20847+ .open = au_do_open_nondir
b912730e
AM
20848+ };
20849+
20850+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20851+ hlist_bl_lock(aopen);
20852+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20853+ if (node->file == file) {
20854+ args.h_file = node->h_file;
20855+ break;
20856+ }
8b6a4947 20857+ hlist_bl_unlock(aopen);
b912730e
AM
20858+ /* AuDebugOn(!args.h_file); */
20859+
20860+ return au_do_open(file, &args);
20861+}
20862+
acd2b654
AM
20863+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20864+ struct aopen_node *aopen_node)
20865+{
20866+ int err;
20867+ struct hlist_bl_head *aopen;
20868+
20869+ AuLabel(here);
20870+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
20871+ au_hbl_add(&aopen_node->hblist, aopen);
20872+ err = finish_open(file, dentry, au_do_aopen);
20873+ au_hbl_del(&aopen_node->hblist, aopen);
20874+ /* AuDbgFile(file); */
20875+ AuDbg("%pd%s%s\n", dentry,
20876+ (file->f_mode & FMODE_CREATED) ? " created" : "",
20877+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
20878+
20879+ AuTraceErr(err);
20880+ return err;
20881+}
20882+
20883+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20884+{
20885+ int err;
20886+
20887+ AuLabel(here);
20888+ dget(dentry);
20889+ err = finish_no_open(file, dentry);
20890+
20891+ AuTraceErr(err);
20892+ return err;
20893+}
20894+
b912730e
AM
20895+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20896+ struct file *file, unsigned int open_flag,
acd2b654 20897+ umode_t create_mode)
b912730e 20898+{
acd2b654 20899+ int err, did_open;
5afbbe0d 20900+ unsigned int lkup_flags;
acd2b654
AM
20901+ aufs_bindex_t bindex;
20902+ struct super_block *sb;
f0c0a007 20903+ struct dentry *parent, *d;
b912730e
AM
20904+ struct vfsub_aopen_args args = {
20905+ .open_flag = open_flag,
acd2b654 20906+ .create_mode = create_mode
b912730e
AM
20907+ };
20908+ struct aopen_node aopen_node = {
20909+ .file = file
20910+ };
20911+
20912+ IMustLock(dir);
5afbbe0d 20913+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
20914+ AuDbgDentry(dentry);
20915+
20916+ err = 0;
20917+ if (!au_di(dentry)) {
5afbbe0d
AM
20918+ lkup_flags = LOOKUP_OPEN;
20919+ if (open_flag & O_CREAT)
20920+ lkup_flags |= LOOKUP_CREATE;
20921+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
20922+ if (IS_ERR(d)) {
20923+ err = PTR_ERR(d);
5afbbe0d 20924+ AuTraceErr(err);
b912730e
AM
20925+ goto out;
20926+ } else if (d) {
20927+ /*
20928+ * obsoleted dentry found.
20929+ * another error will be returned later.
20930+ */
20931+ d_drop(d);
b912730e 20932+ AuDbgDentry(d);
5afbbe0d 20933+ dput(d);
b912730e
AM
20934+ }
20935+ AuDbgDentry(dentry);
20936+ }
20937+
20938+ if (d_is_positive(dentry)
20939+ || d_unhashed(dentry)
20940+ || d_unlinked(dentry)
acd2b654
AM
20941+ || !(open_flag & O_CREAT)) {
20942+ err = au_aopen_no_open(file, dentry);
20943+ goto out; /* success */
20944+ }
b912730e
AM
20945+
20946+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20947+ if (unlikely(err))
20948+ goto out;
20949+
acd2b654 20950+ sb = dentry->d_sb;
b912730e
AM
20951+ parent = dentry->d_parent; /* dir is locked */
20952+ di_write_lock_parent(parent);
5afbbe0d 20953+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
acd2b654
AM
20954+ if (unlikely(err < 0))
20955+ goto out_parent;
b912730e
AM
20956+
20957+ AuDbgDentry(dentry);
acd2b654
AM
20958+ if (d_is_positive(dentry)) {
20959+ err = au_aopen_no_open(file, dentry);
20960+ goto out_parent; /* success */
20961+ }
b912730e 20962+
acd2b654 20963+ args.file = alloc_empty_file(file->f_flags, current_cred());
b912730e
AM
20964+ err = PTR_ERR(args.file);
20965+ if (IS_ERR(args.file))
acd2b654 20966+ goto out_parent;
b912730e 20967+
acd2b654 20968+ bindex = au_dbtop(dentry);
b912730e
AM
20969+ err = au_aopen_or_create(dir, dentry, &args);
20970+ AuTraceErr(err);
20971+ AuDbgFile(args.file);
acd2b654
AM
20972+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20973+ did_open = !!(args.file->f_mode & FMODE_OPENED);
20974+ if (!did_open) {
20975+ fput(args.file);
20976+ args.file = NULL;
b912730e 20977+ }
8b6a4947
AM
20978+ di_write_unlock(parent);
20979+ di_write_unlock(dentry);
acd2b654
AM
20980+ if (unlikely(err < 0)) {
20981+ if (args.file)
20982+ fput(args.file);
20983+ goto out_sb;
b912730e 20984+ }
b912730e 20985+
acd2b654
AM
20986+ if (!did_open)
20987+ err = au_aopen_no_open(file, dentry);
8b6a4947 20988+ else {
acd2b654
AM
20989+ aopen_node.h_file = args.file;
20990+ err = au_aopen_do_open(file, dentry, &aopen_node);
8b6a4947 20991+ }
acd2b654
AM
20992+ if (unlikely(err < 0)) {
20993+ if (args.file)
20994+ fput(args.file);
20995+ if (did_open)
20996+ au_lcnt_dec(&args.br->br_nfiles);
b912730e 20997+ }
acd2b654
AM
20998+ goto out_sb; /* success */
20999+
21000+out_parent:
21001+ di_write_unlock(parent);
21002+ di_write_unlock(dentry);
21003+out_sb:
21004+ si_read_unlock(sb);
b912730e 21005+out:
b912730e 21006+ AuTraceErr(err);
acd2b654 21007+ AuDbgFile(file);
b912730e
AM
21008+ return err;
21009+}
21010+
21011+
21012+/* ---------------------------------------------------------------------- */
21013+
4a4d8108
AM
21014+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21015+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 21016+ aufs_bindex_t btop)
4a4d8108
AM
21017+{
21018+ int err;
21019+ struct dentry *h_parent;
21020+ struct inode *h_dir;
1facf9fc 21021+
027c5e7a 21022+ if (add_entry)
5527c038 21023+ IMustLock(d_inode(parent));
027c5e7a 21024+ else
4a4d8108
AM
21025+ di_write_lock_parent(parent);
21026+
21027+ err = 0;
21028+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 21029+ if (btop > bcpup)
c2b27bf2 21030+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 21031+ else if (btop < bcpup)
4a4d8108
AM
21032+ err = au_cpdown_dirs(dentry, bcpup);
21033+ else
c2b27bf2 21034+ BUG();
4a4d8108 21035+ }
38d290e6 21036+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 21037+ h_parent = au_h_dptr(parent, bcpup);
5527c038 21038+ h_dir = d_inode(h_parent);
be118d29 21039+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 21040+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 21041+ /* todo: no unlock here */
3c1bdaff 21042+ inode_unlock_shared(h_dir);
027c5e7a
AM
21043+
21044+ AuDbg("bcpup %d\n", bcpup);
21045+ if (!err) {
5527c038 21046+ if (d_really_is_negative(dentry))
5afbbe0d 21047+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
21048+ au_update_dbrange(dentry, /*do_put_zero*/0);
21049+ }
1308ab2a 21050+ }
1facf9fc 21051+
4a4d8108
AM
21052+ if (!add_entry)
21053+ di_write_unlock(parent);
21054+ if (!err)
21055+ err = bcpup; /* success */
1308ab2a 21056+
027c5e7a 21057+ AuTraceErr(err);
4a4d8108
AM
21058+ return err;
21059+}
1facf9fc 21060+
4a4d8108
AM
21061+/*
21062+ * decide the branch and the parent dir where we will create a new entry.
21063+ * returns new bindex or an error.
21064+ * copyup the parent dir if needed.
21065+ */
21066+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21067+ struct au_wr_dir_args *args)
21068+{
21069+ int err;
392086de 21070+ unsigned int flags;
5afbbe0d 21071+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
21072+ const unsigned char add_entry
21073+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 21074+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
21075+ struct super_block *sb;
21076+ struct dentry *parent;
21077+ struct au_sbinfo *sbinfo;
1facf9fc 21078+
4a4d8108
AM
21079+ sb = dentry->d_sb;
21080+ sbinfo = au_sbi(sb);
21081+ parent = dget_parent(dentry);
5afbbe0d
AM
21082+ btop = au_dbtop(dentry);
21083+ bcpup = btop;
4a4d8108
AM
21084+ if (args->force_btgt < 0) {
21085+ if (src_dentry) {
5afbbe0d
AM
21086+ src_btop = au_dbtop(src_dentry);
21087+ if (src_btop < btop)
21088+ bcpup = src_btop;
4a4d8108 21089+ } else if (add_entry) {
392086de
AM
21090+ flags = 0;
21091+ if (au_ftest_wrdir(args->flags, ISDIR))
21092+ au_fset_wbr(flags, DIR);
21093+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
21094+ bcpup = err;
21095+ }
1facf9fc 21096+
5527c038 21097+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
21098+ if (add_entry)
21099+ err = AuWbrCopyup(sbinfo, dentry);
21100+ else {
21101+ if (!IS_ROOT(dentry)) {
21102+ di_read_lock_parent(parent, !AuLock_IR);
21103+ err = AuWbrCopyup(sbinfo, dentry);
21104+ di_read_unlock(parent, !AuLock_IR);
21105+ } else
21106+ err = AuWbrCopyup(sbinfo, dentry);
21107+ }
21108+ bcpup = err;
21109+ if (unlikely(err < 0))
21110+ goto out;
21111+ }
21112+ } else {
21113+ bcpup = args->force_btgt;
5527c038 21114+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21115+ }
027c5e7a 21116+
5afbbe0d 21117+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21118+ err = bcpup;
5afbbe0d 21119+ if (bcpup == btop)
4a4d8108 21120+ goto out; /* success */
4a4d8108
AM
21121+
21122+ /* copyup the new parent into the branch we process */
5afbbe0d 21123+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21124+ if (err >= 0) {
5527c038 21125+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21126+ au_set_h_dptr(dentry, btop, NULL);
21127+ au_set_dbtop(dentry, bcpup);
21128+ au_set_dbbot(dentry, bcpup);
027c5e7a 21129+ }
38d290e6
JR
21130+ AuDebugOn(add_entry
21131+ && !au_ftest_wrdir(args->flags, TMPFILE)
21132+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21133+ }
86dc4139
AM
21134+
21135+out:
21136+ dput(parent);
21137+ return err;
21138+}
21139+
21140+/* ---------------------------------------------------------------------- */
21141+
21142+void au_pin_hdir_unlock(struct au_pin *p)
21143+{
21144+ if (p->hdir)
5afbbe0d 21145+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21146+}
21147+
c1595e42 21148+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21149+{
21150+ int err;
21151+
21152+ err = 0;
21153+ if (!p->hdir)
21154+ goto out;
21155+
21156+ /* even if an error happens later, keep this lock */
5afbbe0d 21157+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21158+
21159+ err = -EBUSY;
5527c038 21160+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21161+ goto out;
21162+
21163+ err = 0;
21164+ if (p->h_dentry)
21165+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21166+ p->h_parent, p->br);
21167+
21168+out:
21169+ return err;
21170+}
21171+
21172+int au_pin_hdir_relock(struct au_pin *p)
21173+{
21174+ int err, i;
21175+ struct inode *h_i;
21176+ struct dentry *h_d[] = {
21177+ p->h_dentry,
21178+ p->h_parent
21179+ };
21180+
21181+ err = au_pin_hdir_lock(p);
21182+ if (unlikely(err))
21183+ goto out;
21184+
21185+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21186+ if (!h_d[i])
21187+ continue;
5527c038
JR
21188+ if (d_is_positive(h_d[i])) {
21189+ h_i = d_inode(h_d[i]);
86dc4139 21190+ err = !h_i->i_nlink;
5527c038 21191+ }
86dc4139
AM
21192+ }
21193+
21194+out:
21195+ return err;
21196+}
21197+
5afbbe0d 21198+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21199+{
eca34b5c 21200+ atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
86dc4139
AM
21201+}
21202+
21203+void au_pin_hdir_acquire_nest(struct au_pin *p)
21204+{
21205+ if (p->hdir) {
5afbbe0d 21206+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21207+ p->lsc_hi, 0, NULL, _RET_IP_);
21208+ au_pin_hdir_set_owner(p, current);
21209+ }
dece6358 21210+}
1facf9fc 21211+
86dc4139
AM
21212+void au_pin_hdir_release(struct au_pin *p)
21213+{
21214+ if (p->hdir) {
21215+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 21216+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
21217+ }
21218+}
1308ab2a 21219+
4a4d8108 21220+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21221+{
4a4d8108
AM
21222+ if (pin && pin->parent)
21223+ return au_h_dptr(pin->parent, pin->bindex);
21224+ return NULL;
dece6358 21225+}
1facf9fc 21226+
4a4d8108 21227+void au_unpin(struct au_pin *p)
dece6358 21228+{
86dc4139
AM
21229+ if (p->hdir)
21230+ au_pin_hdir_unlock(p);
e49829fe 21231+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21232+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21233+ if (!p->hdir)
21234+ return;
1facf9fc 21235+
4a4d8108
AM
21236+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21237+ di_read_unlock(p->parent, AuLock_IR);
21238+ iput(p->hdir->hi_inode);
21239+ dput(p->parent);
21240+ p->parent = NULL;
21241+ p->hdir = NULL;
21242+ p->h_mnt = NULL;
86dc4139 21243+ /* do not clear p->task */
4a4d8108 21244+}
1308ab2a 21245+
4a4d8108
AM
21246+int au_do_pin(struct au_pin *p)
21247+{
21248+ int err;
21249+ struct super_block *sb;
4a4d8108
AM
21250+ struct inode *h_dir;
21251+
21252+ err = 0;
21253+ sb = p->dentry->d_sb;
86dc4139 21254+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21255+ if (IS_ROOT(p->dentry)) {
21256+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21257+ p->h_mnt = au_br_mnt(p->br);
b4510431 21258+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21259+ if (unlikely(err)) {
21260+ au_fclr_pin(p->flags, MNT_WRITE);
21261+ goto out_err;
21262+ }
21263+ }
dece6358 21264+ goto out;
1facf9fc 21265+ }
21266+
86dc4139 21267+ p->h_dentry = NULL;
5afbbe0d 21268+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21269+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21270+
4a4d8108
AM
21271+ p->parent = dget_parent(p->dentry);
21272+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21273+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21274+
4a4d8108 21275+ h_dir = NULL;
86dc4139 21276+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21277+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21278+ if (p->hdir)
21279+ h_dir = p->hdir->hi_inode;
dece6358 21280+
b752ccd1
AM
21281+ /*
21282+ * udba case, or
21283+ * if DI_LOCKED is not set, then p->parent may be different
21284+ * and h_parent can be NULL.
21285+ */
86dc4139 21286+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21287+ err = -EBUSY;
4a4d8108
AM
21288+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21289+ di_read_unlock(p->parent, AuLock_IR);
21290+ dput(p->parent);
21291+ p->parent = NULL;
21292+ goto out_err;
21293+ }
1308ab2a 21294+
4a4d8108 21295+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21296+ p->h_mnt = au_br_mnt(p->br);
b4510431 21297+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21298+ if (unlikely(err)) {
4a4d8108 21299+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
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;
dece6358
AM
21305+ }
21306+ }
4a4d8108 21307+
86dc4139
AM
21308+ au_igrab(h_dir);
21309+ err = au_pin_hdir_lock(p);
21310+ if (!err)
21311+ goto out; /* success */
21312+
076b876e
AM
21313+ au_unpin(p);
21314+
4f0767ce 21315+out_err:
4a4d8108
AM
21316+ pr_err("err %d\n", err);
21317+ err = au_busy_or_stale();
4f0767ce 21318+out:
1facf9fc 21319+ return err;
21320+}
21321+
4a4d8108
AM
21322+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21323+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21324+ unsigned int udba, unsigned char flags)
21325+{
21326+ p->dentry = dentry;
21327+ p->udba = udba;
21328+ p->lsc_di = lsc_di;
21329+ p->lsc_hi = lsc_hi;
21330+ p->flags = flags;
21331+ p->bindex = bindex;
21332+
21333+ p->parent = NULL;
21334+ p->hdir = NULL;
21335+ p->h_mnt = NULL;
86dc4139
AM
21336+
21337+ p->h_dentry = NULL;
21338+ p->h_parent = NULL;
21339+ p->br = NULL;
21340+ p->task = current;
4a4d8108
AM
21341+}
21342+
21343+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21344+ unsigned int udba, unsigned char flags)
21345+{
21346+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21347+ udba, flags);
21348+ return au_do_pin(pin);
21349+}
21350+
dece6358
AM
21351+/* ---------------------------------------------------------------------- */
21352+
1308ab2a 21353+/*
4a4d8108
AM
21354+ * ->setattr() and ->getattr() are called in various cases.
21355+ * chmod, stat: dentry is revalidated.
21356+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21357+ * unhashed.
21358+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21359+ */
027c5e7a 21360+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21361+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21362+{
4a4d8108 21363+ int err;
4a4d8108 21364+ struct dentry *parent;
1facf9fc 21365+
1308ab2a 21366+ err = 0;
027c5e7a 21367+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21368+ parent = dget_parent(dentry);
21369+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21370+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21371+ di_read_unlock(parent, AuLock_IR);
21372+ dput(parent);
dece6358 21373+ }
1facf9fc 21374+
4a4d8108 21375+ AuTraceErr(err);
1308ab2a 21376+ return err;
21377+}
dece6358 21378+
c1595e42
JR
21379+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21380+ struct au_icpup_args *a)
1308ab2a 21381+{
21382+ int err;
4a4d8108 21383+ loff_t sz;
5afbbe0d 21384+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21385+ struct dentry *hi_wh, *parent;
21386+ struct inode *inode;
4a4d8108
AM
21387+ struct au_wr_dir_args wr_dir_args = {
21388+ .force_btgt = -1,
21389+ .flags = 0
21390+ };
21391+
2000de60 21392+ if (d_is_dir(dentry))
4a4d8108
AM
21393+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21394+ /* plink or hi_wh() case */
5afbbe0d 21395+ btop = au_dbtop(dentry);
5527c038 21396+ inode = d_inode(dentry);
5afbbe0d
AM
21397+ ibtop = au_ibtop(inode);
21398+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21399+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21400+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21401+ if (unlikely(err < 0))
21402+ goto out;
21403+ a->btgt = err;
5afbbe0d 21404+ if (err != btop)
4a4d8108
AM
21405+ au_fset_icpup(a->flags, DID_CPUP);
21406+
21407+ err = 0;
21408+ a->pin_flags = AuPin_MNT_WRITE;
21409+ parent = NULL;
21410+ if (!IS_ROOT(dentry)) {
21411+ au_fset_pin(a->pin_flags, DI_LOCKED);
21412+ parent = dget_parent(dentry);
21413+ di_write_lock_parent(parent);
21414+ }
21415+
21416+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21417+ if (unlikely(err))
21418+ goto out_parent;
21419+
4a4d8108 21420+ sz = -1;
5afbbe0d 21421+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21422+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21423+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 21424+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21425+ if (ia->ia_size < i_size_read(a->h_inode))
21426+ sz = ia->ia_size;
3c1bdaff 21427+ inode_unlock_shared(a->h_inode);
c1595e42 21428+ }
4a4d8108 21429+
4a4d8108 21430+ hi_wh = NULL;
027c5e7a 21431+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21432+ hi_wh = au_hi_wh(inode, a->btgt);
21433+ if (!hi_wh) {
c2b27bf2
AM
21434+ struct au_cp_generic cpg = {
21435+ .dentry = dentry,
21436+ .bdst = a->btgt,
21437+ .bsrc = -1,
21438+ .len = sz,
21439+ .pin = &a->pin
21440+ };
21441+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21442+ if (unlikely(err))
21443+ goto out_unlock;
21444+ hi_wh = au_hi_wh(inode, a->btgt);
21445+ /* todo: revalidate hi_wh? */
21446+ }
21447+ }
21448+
21449+ if (parent) {
21450+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21451+ di_downgrade_lock(parent, AuLock_IR);
21452+ dput(parent);
21453+ parent = NULL;
21454+ }
21455+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21456+ goto out; /* success */
21457+
21458+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21459+ struct au_cp_generic cpg = {
21460+ .dentry = dentry,
21461+ .bdst = a->btgt,
5afbbe0d 21462+ .bsrc = btop,
c2b27bf2
AM
21463+ .len = sz,
21464+ .pin = &a->pin,
21465+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21466+ };
21467+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21468+ if (!err)
21469+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21470+ } else if (!hi_wh)
21471+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21472+ else
21473+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21474+
4f0767ce 21475+out_unlock:
5527c038 21476+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21477+ if (!err)
dece6358 21478+ goto out; /* success */
4a4d8108 21479+ au_unpin(&a->pin);
4f0767ce 21480+out_parent:
4a4d8108
AM
21481+ if (parent) {
21482+ di_write_unlock(parent);
21483+ dput(parent);
21484+ }
4f0767ce 21485+out:
86dc4139 21486+ if (!err)
febd17d6 21487+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21488+ return err;
21489+}
21490+
4a4d8108 21491+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 21492+{
4a4d8108 21493+ int err;
523b37e3 21494+ struct inode *inode, *delegated;
4a4d8108
AM
21495+ struct super_block *sb;
21496+ struct file *file;
21497+ struct au_icpup_args *a;
1facf9fc 21498+
5527c038 21499+ inode = d_inode(dentry);
4a4d8108 21500+ IMustLock(inode);
dece6358 21501+
f2c43d5f
AM
21502+ err = setattr_prepare(dentry, ia);
21503+ if (unlikely(err))
21504+ goto out;
21505+
4a4d8108
AM
21506+ err = -ENOMEM;
21507+ a = kzalloc(sizeof(*a), GFP_NOFS);
21508+ if (unlikely(!a))
21509+ goto out;
1facf9fc 21510+
4a4d8108
AM
21511+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21512+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21513+
4a4d8108
AM
21514+ file = NULL;
21515+ sb = dentry->d_sb;
e49829fe
JR
21516+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21517+ if (unlikely(err))
21518+ goto out_kfree;
21519+
4a4d8108
AM
21520+ if (ia->ia_valid & ATTR_FILE) {
21521+ /* currently ftruncate(2) only */
7e9cd9fe 21522+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21523+ file = ia->ia_file;
521ced18
JR
21524+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21525+ /*fi_lsc*/0);
4a4d8108
AM
21526+ if (unlikely(err))
21527+ goto out_si;
21528+ ia->ia_file = au_hf_top(file);
21529+ a->udba = AuOpt_UDBA_NONE;
21530+ } else {
21531+ /* fchmod() doesn't pass ia_file */
21532+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21533+ di_write_lock_child(dentry);
21534+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21535+ if (d_unhashed(dentry))
21536+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21537+ if (a->udba != AuOpt_UDBA_NONE) {
21538+ AuDebugOn(IS_ROOT(dentry));
21539+ err = au_reval_for_attr(dentry, au_sigen(sb));
21540+ if (unlikely(err))
21541+ goto out_dentry;
21542+ }
dece6358 21543+ }
dece6358 21544+
4a4d8108
AM
21545+ err = au_pin_and_icpup(dentry, ia, a);
21546+ if (unlikely(err < 0))
21547+ goto out_dentry;
21548+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21549+ ia->ia_file = NULL;
21550+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21551+ }
dece6358 21552+
4a4d8108
AM
21553+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21554+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21555+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21556+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21557+ if (unlikely(err))
21558+ goto out_unlock;
21559+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21560+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21561+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21562+ if (unlikely(err))
21563+ goto out_unlock;
21564+ }
dece6358 21565+
4a4d8108
AM
21566+ if (ia->ia_valid & ATTR_SIZE) {
21567+ struct file *f;
1308ab2a 21568+
953406b4 21569+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21570+ /* unmap only */
953406b4 21571+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21572+
4a4d8108
AM
21573+ f = NULL;
21574+ if (ia->ia_valid & ATTR_FILE)
21575+ f = ia->ia_file;
febd17d6 21576+ inode_unlock(a->h_inode);
4a4d8108 21577+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21578+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21579+ } else {
21580+ delegated = NULL;
21581+ while (1) {
21582+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21583+ if (delegated) {
21584+ err = break_deleg_wait(&delegated);
21585+ if (!err)
21586+ continue;
21587+ }
21588+ break;
21589+ }
21590+ }
8cdd5066
JR
21591+ /*
21592+ * regardless aufs 'acl' option setting.
21593+ * why don't all acl-aware fs call this func from their ->setattr()?
21594+ */
21595+ if (!err && (ia->ia_valid & ATTR_MODE))
21596+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
21597+ if (!err)
21598+ au_cpup_attr_changeable(inode);
1308ab2a 21599+
4f0767ce 21600+out_unlock:
febd17d6 21601+ inode_unlock(a->h_inode);
4a4d8108 21602+ au_unpin(&a->pin);
027c5e7a 21603+ if (unlikely(err))
5afbbe0d 21604+ au_update_dbtop(dentry);
4f0767ce 21605+out_dentry:
4a4d8108
AM
21606+ di_write_unlock(dentry);
21607+ if (file) {
21608+ fi_write_unlock(file);
21609+ ia->ia_file = file;
21610+ ia->ia_valid |= ATTR_FILE;
21611+ }
4f0767ce 21612+out_si:
4a4d8108 21613+ si_read_unlock(sb);
e49829fe 21614+out_kfree:
9f237c51 21615+ au_kfree_rcu(a);
4f0767ce 21616+out:
4a4d8108
AM
21617+ AuTraceErr(err);
21618+ return err;
1facf9fc 21619+}
21620+
c1595e42
JR
21621+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21622+static int au_h_path_to_set_attr(struct dentry *dentry,
21623+ struct au_icpup_args *a, struct path *h_path)
21624+{
21625+ int err;
21626+ struct super_block *sb;
21627+
21628+ sb = dentry->d_sb;
21629+ a->udba = au_opt_udba(sb);
21630+ /* no d_unlinked(), to set UDBA_NONE for root */
21631+ if (d_unhashed(dentry))
21632+ a->udba = AuOpt_UDBA_NONE;
21633+ if (a->udba != AuOpt_UDBA_NONE) {
21634+ AuDebugOn(IS_ROOT(dentry));
21635+ err = au_reval_for_attr(dentry, au_sigen(sb));
21636+ if (unlikely(err))
21637+ goto out;
21638+ }
21639+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21640+ if (unlikely(err < 0))
21641+ goto out;
21642+
21643+ h_path->dentry = a->h_path.dentry;
21644+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21645+
21646+out:
21647+ return err;
21648+}
21649+
f2c43d5f
AM
21650+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21651+ struct au_sxattr *arg)
c1595e42
JR
21652+{
21653+ int err;
21654+ struct path h_path;
21655+ struct super_block *sb;
21656+ struct au_icpup_args *a;
5afbbe0d 21657+ struct inode *h_inode;
c1595e42 21658+
c1595e42
JR
21659+ IMustLock(inode);
21660+
21661+ err = -ENOMEM;
21662+ a = kzalloc(sizeof(*a), GFP_NOFS);
21663+ if (unlikely(!a))
21664+ goto out;
21665+
21666+ sb = dentry->d_sb;
21667+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21668+ if (unlikely(err))
21669+ goto out_kfree;
21670+
21671+ h_path.dentry = NULL; /* silence gcc */
21672+ di_write_lock_child(dentry);
21673+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21674+ if (unlikely(err))
21675+ goto out_di;
21676+
febd17d6 21677+ inode_unlock(a->h_inode);
c1595e42
JR
21678+ switch (arg->type) {
21679+ case AU_XATTR_SET:
5afbbe0d 21680+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
21681+ err = vfsub_setxattr(h_path.dentry,
21682+ arg->u.set.name, arg->u.set.value,
21683+ arg->u.set.size, arg->u.set.flags);
21684+ break;
c1595e42
JR
21685+ case AU_ACL_SET:
21686+ err = -EOPNOTSUPP;
5527c038 21687+ h_inode = d_inode(h_path.dentry);
c1595e42 21688+ if (h_inode->i_op->set_acl)
f2c43d5f 21689+ /* this will call posix_acl_update_mode */
c1595e42
JR
21690+ err = h_inode->i_op->set_acl(h_inode,
21691+ arg->u.acl_set.acl,
21692+ arg->u.acl_set.type);
21693+ break;
21694+ }
21695+ if (!err)
21696+ au_cpup_attr_timesizes(inode);
21697+
21698+ au_unpin(&a->pin);
21699+ if (unlikely(err))
5afbbe0d 21700+ au_update_dbtop(dentry);
c1595e42
JR
21701+
21702+out_di:
21703+ di_write_unlock(dentry);
21704+ si_read_unlock(sb);
21705+out_kfree:
9f237c51 21706+ au_kfree_rcu(a);
c1595e42
JR
21707+out:
21708+ AuTraceErr(err);
21709+ return err;
21710+}
21711+#endif
21712+
4a4d8108
AM
21713+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21714+ unsigned int nlink)
1facf9fc 21715+{
9dbd164d
AM
21716+ unsigned int n;
21717+
4a4d8108 21718+ inode->i_mode = st->mode;
86dc4139
AM
21719+ /* don't i_[ug]id_write() here */
21720+ inode->i_uid = st->uid;
21721+ inode->i_gid = st->gid;
4a4d8108
AM
21722+ inode->i_atime = st->atime;
21723+ inode->i_mtime = st->mtime;
21724+ inode->i_ctime = st->ctime;
1facf9fc 21725+
4a4d8108
AM
21726+ au_cpup_attr_nlink(inode, /*force*/0);
21727+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21728+ n = inode->i_nlink;
21729+ n -= nlink;
21730+ n += st->nlink;
f6b6e03d 21731+ smp_mb(); /* for i_nlink */
7eafdf33 21732+ /* 0 can happen */
92d182d2 21733+ set_nlink(inode, n);
4a4d8108 21734+ }
1facf9fc 21735+
4a4d8108
AM
21736+ spin_lock(&inode->i_lock);
21737+ inode->i_blocks = st->blocks;
21738+ i_size_write(inode, st->size);
21739+ spin_unlock(&inode->i_lock);
1facf9fc 21740+}
21741+
c1595e42 21742+/*
f2c43d5f 21743+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21744+ * returns zero or negative (an error).
21745+ * @dentry will be read-locked in success.
21746+ */
a2654f78
AM
21747+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21748+ int locked)
1facf9fc 21749+{
4a4d8108 21750+ int err;
076b876e 21751+ unsigned int mnt_flags, sigen;
c1595e42 21752+ unsigned char udba_none;
4a4d8108 21753+ aufs_bindex_t bindex;
4a4d8108
AM
21754+ struct super_block *sb, *h_sb;
21755+ struct inode *inode;
1facf9fc 21756+
c1595e42
JR
21757+ h_path->mnt = NULL;
21758+ h_path->dentry = NULL;
21759+
21760+ err = 0;
4a4d8108 21761+ sb = dentry->d_sb;
4a4d8108
AM
21762+ mnt_flags = au_mntflags(sb);
21763+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21764+
a2654f78
AM
21765+ if (unlikely(locked))
21766+ goto body; /* skip locking dinfo */
21767+
4a4d8108 21768+ /* support fstat(2) */
027c5e7a 21769+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21770+ sigen = au_sigen(sb);
027c5e7a
AM
21771+ err = au_digen_test(dentry, sigen);
21772+ if (!err) {
4a4d8108 21773+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21774+ err = au_dbrange_test(dentry);
c1595e42
JR
21775+ if (unlikely(err)) {
21776+ di_read_unlock(dentry, AuLock_IR);
21777+ goto out;
21778+ }
027c5e7a 21779+ } else {
4a4d8108
AM
21780+ AuDebugOn(IS_ROOT(dentry));
21781+ di_write_lock_child(dentry);
027c5e7a
AM
21782+ err = au_dbrange_test(dentry);
21783+ if (!err)
21784+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21785+ if (!err)
21786+ di_downgrade_lock(dentry, AuLock_IR);
21787+ else {
21788+ di_write_unlock(dentry);
21789+ goto out;
21790+ }
4a4d8108
AM
21791+ }
21792+ } else
21793+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21794+
a2654f78 21795+body:
5527c038 21796+ inode = d_inode(dentry);
5afbbe0d 21797+ bindex = au_ibtop(inode);
c1595e42
JR
21798+ h_path->mnt = au_sbr_mnt(sb, bindex);
21799+ h_sb = h_path->mnt->mnt_sb;
21800+ if (!force
21801+ && !au_test_fs_bad_iattr(h_sb)
21802+ && udba_none)
21803+ goto out; /* success */
1facf9fc 21804+
5afbbe0d 21805+ if (au_dbtop(dentry) == bindex)
c1595e42 21806+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21807+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21808+ h_path->dentry = au_plink_lkup(inode, bindex);
21809+ if (IS_ERR(h_path->dentry))
21810+ /* pretending success */
21811+ h_path->dentry = NULL;
21812+ else
21813+ dput(h_path->dentry);
4a4d8108 21814+ }
c1595e42
JR
21815+
21816+out:
21817+ return err;
21818+}
21819+
521ced18
JR
21820+static int aufs_getattr(const struct path *path, struct kstat *st,
21821+ u32 request, unsigned int query)
c1595e42
JR
21822+{
21823+ int err;
21824+ unsigned char positive;
21825+ struct path h_path;
521ced18 21826+ struct dentry *dentry;
c1595e42
JR
21827+ struct inode *inode;
21828+ struct super_block *sb;
21829+
521ced18 21830+ dentry = path->dentry;
5527c038 21831+ inode = d_inode(dentry);
c1595e42
JR
21832+ sb = dentry->d_sb;
21833+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21834+ if (unlikely(err))
21835+ goto out;
a2654f78 21836+ err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
c1595e42
JR
21837+ if (unlikely(err))
21838+ goto out_si;
c06a8ce3 21839+ if (unlikely(!h_path.dentry))
c1595e42 21840+ /* illegally overlapped or something */
4a4d8108
AM
21841+ goto out_fill; /* pretending success */
21842+
5527c038 21843+ positive = d_is_positive(h_path.dentry);
4a4d8108 21844+ if (positive)
521ced18
JR
21845+ /* no vfsub version */
21846+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21847+ if (!err) {
21848+ if (positive)
c06a8ce3 21849+ au_refresh_iattr(inode, st,
5527c038 21850+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21851+ goto out_fill; /* success */
1facf9fc 21852+ }
7f207e10 21853+ AuTraceErr(err);
c1595e42 21854+ goto out_di;
4a4d8108 21855+
4f0767ce 21856+out_fill:
4a4d8108 21857+ generic_fillattr(inode, st);
c1595e42 21858+out_di:
4a4d8108 21859+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21860+out_si:
4a4d8108 21861+ si_read_unlock(sb);
7f207e10
AM
21862+out:
21863+ AuTraceErr(err);
4a4d8108 21864+ return err;
1facf9fc 21865+}
21866+
21867+/* ---------------------------------------------------------------------- */
21868+
febd17d6
JR
21869+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21870+ struct delayed_call *done)
4a4d8108 21871+{
c2c0f25c 21872+ const char *ret;
c2c0f25c 21873+ struct dentry *h_dentry;
febd17d6 21874+ struct inode *h_inode;
4a4d8108 21875+ int err;
c2c0f25c 21876+ aufs_bindex_t bindex;
1facf9fc 21877+
79b8bda9 21878+ ret = NULL; /* suppress a warning */
febd17d6
JR
21879+ err = -ECHILD;
21880+ if (!dentry)
21881+ goto out;
21882+
027c5e7a
AM
21883+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21884+ if (unlikely(err))
c2c0f25c 21885+ goto out;
027c5e7a
AM
21886+
21887+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
21888+ if (unlikely(err))
21889+ goto out_unlock;
21890+
21891+ err = -EINVAL;
21892+ inode = d_inode(dentry);
5afbbe0d 21893+ bindex = au_ibtop(inode);
c2c0f25c 21894+ h_inode = au_h_iptr(inode, bindex);
febd17d6 21895+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
21896+ goto out_unlock;
21897+
21898+ err = -EBUSY;
21899+ h_dentry = NULL;
5afbbe0d 21900+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
21901+ h_dentry = au_h_dptr(dentry, bindex);
21902+ if (h_dentry)
21903+ dget(h_dentry);
027c5e7a 21904+ }
c2c0f25c
AM
21905+ if (!h_dentry) {
21906+ h_dentry = d_find_any_alias(h_inode);
21907+ if (IS_ERR(h_dentry)) {
21908+ err = PTR_ERR(h_dentry);
febd17d6 21909+ goto out_unlock;
c2c0f25c
AM
21910+ }
21911+ }
21912+ if (unlikely(!h_dentry))
febd17d6 21913+ goto out_unlock;
1facf9fc 21914+
c2c0f25c 21915+ err = 0;
062440b3 21916+ AuDbg("%ps\n", h_inode->i_op->get_link);
c2c0f25c 21917+ AuDbgDentry(h_dentry);
f2c43d5f 21918+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 21919+ dput(h_dentry);
febd17d6
JR
21920+ if (IS_ERR(ret))
21921+ err = PTR_ERR(ret);
c2c0f25c 21922+
c2c0f25c
AM
21923+out_unlock:
21924+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 21925+out:
c2c0f25c
AM
21926+ if (unlikely(err))
21927+ ret = ERR_PTR(err);
21928+ AuTraceErrPtr(ret);
21929+ return ret;
4a4d8108 21930+}
1facf9fc 21931+
4a4d8108 21932+/* ---------------------------------------------------------------------- */
1facf9fc 21933+
e2f27e51
AM
21934+static int au_is_special(struct inode *inode)
21935+{
21936+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21937+}
21938+
acd2b654
AM
21939+static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21940+ int flags)
4a4d8108 21941+{
0c3ec466 21942+ int err;
e2f27e51 21943+ aufs_bindex_t bindex;
0c3ec466
AM
21944+ struct super_block *sb;
21945+ struct inode *h_inode;
e2f27e51 21946+ struct vfsmount *h_mnt;
0c3ec466
AM
21947+
21948+ sb = inode->i_sb;
e2f27e51
AM
21949+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21950+ "unexpected s_flags 0x%lx", sb->s_flags);
21951+
0c3ec466
AM
21952+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
21953+ lockdep_off();
21954+ si_read_lock(sb, AuLock_FLUSH);
21955+ ii_write_lock_child(inode);
e2f27e51
AM
21956+
21957+ err = 0;
21958+ bindex = au_ibtop(inode);
21959+ h_inode = au_h_iptr(inode, bindex);
21960+ if (!au_test_ro(sb, bindex, inode)) {
21961+ h_mnt = au_sbr_mnt(sb, bindex);
21962+ err = vfsub_mnt_want_write(h_mnt);
21963+ if (!err) {
21964+ err = vfsub_update_time(h_inode, ts, flags);
21965+ vfsub_mnt_drop_write(h_mnt);
21966+ }
21967+ } else if (au_is_special(h_inode)) {
21968+ /*
21969+ * Never copy-up here.
21970+ * These special files may already be opened and used for
21971+ * communicating. If we copied it up, then the communication
21972+ * would be corrupted.
21973+ */
21974+ AuWarn1("timestamps for i%lu are ignored "
21975+ "since it is on readonly branch (hi%lu).\n",
21976+ inode->i_ino, h_inode->i_ino);
21977+ } else if (flags & ~S_ATIME) {
21978+ err = -EIO;
21979+ AuIOErr1("unexpected flags 0x%x\n", flags);
21980+ AuDebugOn(1);
21981+ }
21982+
38d290e6
JR
21983+ if (!err)
21984+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
21985+ ii_write_unlock(inode);
21986+ si_read_unlock(sb);
21987+ lockdep_on();
38d290e6
JR
21988+
21989+ if (!err && (flags & S_VERSION))
21990+ inode_inc_iversion(inode);
21991+
0c3ec466 21992+ return err;
4a4d8108 21993+}
1facf9fc 21994+
4a4d8108 21995+/* ---------------------------------------------------------------------- */
1308ab2a 21996+
b95c5147
AM
21997+/* no getattr version will be set by module.c:aufs_init() */
21998+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21999+ aufs_iop[] = {
22000+ [AuIop_SYMLINK] = {
22001+ .permission = aufs_permission,
c1595e42 22002+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22003+ .get_acl = aufs_get_acl,
22004+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
22005+#endif
22006+
b95c5147
AM
22007+ .setattr = aufs_setattr,
22008+ .getattr = aufs_getattr,
0c3ec466 22009+
c1595e42 22010+#ifdef CONFIG_AUFS_XATTR
b95c5147 22011+ .listxattr = aufs_listxattr,
c1595e42
JR
22012+#endif
22013+
febd17d6 22014+ .get_link = aufs_get_link,
0c3ec466 22015+
b95c5147
AM
22016+ /* .update_time = aufs_update_time */
22017+ },
22018+ [AuIop_DIR] = {
22019+ .create = aufs_create,
22020+ .lookup = aufs_lookup,
22021+ .link = aufs_link,
22022+ .unlink = aufs_unlink,
22023+ .symlink = aufs_symlink,
22024+ .mkdir = aufs_mkdir,
22025+ .rmdir = aufs_rmdir,
22026+ .mknod = aufs_mknod,
22027+ .rename = aufs_rename,
22028+
22029+ .permission = aufs_permission,
c1595e42 22030+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22031+ .get_acl = aufs_get_acl,
22032+ .set_acl = aufs_set_acl,
c1595e42
JR
22033+#endif
22034+
b95c5147
AM
22035+ .setattr = aufs_setattr,
22036+ .getattr = aufs_getattr,
0c3ec466 22037+
c1595e42 22038+#ifdef CONFIG_AUFS_XATTR
b95c5147 22039+ .listxattr = aufs_listxattr,
c1595e42
JR
22040+#endif
22041+
b95c5147
AM
22042+ .update_time = aufs_update_time,
22043+ .atomic_open = aufs_atomic_open,
22044+ .tmpfile = aufs_tmpfile
22045+ },
22046+ [AuIop_OTHER] = {
22047+ .permission = aufs_permission,
c1595e42 22048+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22049+ .get_acl = aufs_get_acl,
22050+ .set_acl = aufs_set_acl,
c1595e42
JR
22051+#endif
22052+
b95c5147
AM
22053+ .setattr = aufs_setattr,
22054+ .getattr = aufs_getattr,
0c3ec466 22055+
c1595e42 22056+#ifdef CONFIG_AUFS_XATTR
b95c5147 22057+ .listxattr = aufs_listxattr,
c1595e42
JR
22058+#endif
22059+
b95c5147
AM
22060+ .update_time = aufs_update_time
22061+ }
4a4d8108 22062+};
7f207e10 22063diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
eca34b5c 22064--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
016522bc 22065+++ linux/fs/aufs/i_op_del.c 2020-01-27 10:57:18.172204883 +0100
eca801bf 22066@@ -0,0 +1,513 @@
cd7a4cd9 22067+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22068+/*
016522bc 22069+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 22070+ *
22071+ * This program, aufs is free software; you can redistribute it and/or modify
22072+ * it under the terms of the GNU General Public License as published by
22073+ * the Free Software Foundation; either version 2 of the License, or
22074+ * (at your option) any later version.
dece6358
AM
22075+ *
22076+ * This program is distributed in the hope that it will be useful,
22077+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22078+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22079+ * GNU General Public License for more details.
22080+ *
22081+ * You should have received a copy of the GNU General Public License
523b37e3 22082+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22083+ */
22084+
22085+/*
4a4d8108 22086+ * inode operations (del entry)
1308ab2a 22087+ */
dece6358 22088+
eca801bf 22089+#include <linux/iversion.h>
1308ab2a 22090+#include "aufs.h"
dece6358 22091+
4a4d8108
AM
22092+/*
22093+ * decide if a new whiteout for @dentry is necessary or not.
22094+ * when it is necessary, prepare the parent dir for the upper branch whose
22095+ * branch index is @bcpup for creation. the actual creation of the whiteout will
22096+ * be done by caller.
22097+ * return value:
22098+ * 0: wh is unnecessary
22099+ * plus: wh is necessary
22100+ * minus: error
22101+ */
22102+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 22103+{
4a4d8108 22104+ int need_wh, err;
5afbbe0d 22105+ aufs_bindex_t btop;
4a4d8108 22106+ struct super_block *sb;
dece6358 22107+
4a4d8108 22108+ sb = dentry->d_sb;
5afbbe0d 22109+ btop = au_dbtop(dentry);
4a4d8108 22110+ if (*bcpup < 0) {
5afbbe0d
AM
22111+ *bcpup = btop;
22112+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
22113+ err = AuWbrCopyup(au_sbi(sb), dentry);
22114+ *bcpup = err;
22115+ if (unlikely(err < 0))
22116+ goto out;
22117+ }
22118+ } else
5afbbe0d 22119+ AuDebugOn(btop < *bcpup
5527c038 22120+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22121+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22122+
5afbbe0d 22123+ if (*bcpup != btop) {
4a4d8108
AM
22124+ err = au_cpup_dirs(dentry, *bcpup);
22125+ if (unlikely(err))
22126+ goto out;
22127+ need_wh = 1;
22128+ } else {
027c5e7a 22129+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22130+
027c5e7a
AM
22131+ need_wh = -ENOMEM;
22132+ dinfo = au_di(dentry);
22133+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22134+ if (tmp) {
22135+ au_di_cp(tmp, dinfo);
22136+ au_di_swap(tmp, dinfo);
22137+ /* returns the number of positive dentries */
5afbbe0d
AM
22138+ need_wh = au_lkup_dentry(dentry, btop + 1,
22139+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22140+ au_di_swap(tmp, dinfo);
22141+ au_rw_write_unlock(&tmp->di_rwsem);
22142+ au_di_free(tmp);
4a4d8108
AM
22143+ }
22144+ }
22145+ AuDbg("need_wh %d\n", need_wh);
22146+ err = need_wh;
22147+
4f0767ce 22148+out:
4a4d8108 22149+ return err;
1facf9fc 22150+}
22151+
4a4d8108
AM
22152+/*
22153+ * simple tests for the del-entry operations.
22154+ * following the checks in vfs, plus the parent-child relationship.
22155+ */
22156+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22157+ struct dentry *h_parent, int isdir)
1facf9fc 22158+{
4a4d8108
AM
22159+ int err;
22160+ umode_t h_mode;
22161+ struct dentry *h_dentry, *h_latest;
1308ab2a 22162+ struct inode *h_inode;
1facf9fc 22163+
4a4d8108 22164+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22165+ if (d_really_is_positive(dentry)) {
4a4d8108 22166+ err = -ENOENT;
5527c038
JR
22167+ if (unlikely(d_is_negative(h_dentry)))
22168+ goto out;
22169+ h_inode = d_inode(h_dentry);
22170+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22171+ goto out;
1facf9fc 22172+
4a4d8108
AM
22173+ h_mode = h_inode->i_mode;
22174+ if (!isdir) {
22175+ err = -EISDIR;
22176+ if (unlikely(S_ISDIR(h_mode)))
22177+ goto out;
22178+ } else if (unlikely(!S_ISDIR(h_mode))) {
22179+ err = -ENOTDIR;
22180+ goto out;
22181+ }
22182+ } else {
22183+ /* rename(2) case */
22184+ err = -EIO;
5527c038 22185+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22186+ goto out;
22187+ }
1facf9fc 22188+
4a4d8108
AM
22189+ err = -ENOENT;
22190+ /* expected parent dir is locked */
22191+ if (unlikely(h_parent != h_dentry->d_parent))
22192+ goto out;
22193+ err = 0;
22194+
22195+ /*
22196+ * rmdir a dir may break the consistency on some filesystem.
22197+ * let's try heavy test.
22198+ */
22199+ err = -EACCES;
076b876e 22200+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 22201+ && au_test_h_perm(d_inode(h_parent),
076b876e 22202+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22203+ goto out;
22204+
076b876e 22205+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
22206+ err = -EIO;
22207+ if (IS_ERR(h_latest))
22208+ goto out;
22209+ if (h_latest == h_dentry)
22210+ err = 0;
22211+ dput(h_latest);
22212+
4f0767ce 22213+out:
4a4d8108 22214+ return err;
1308ab2a 22215+}
1facf9fc 22216+
4a4d8108
AM
22217+/*
22218+ * decide the branch where we operate for @dentry. the branch index will be set
acd2b654 22219+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
4a4d8108
AM
22220+ * dir for reverting.
22221+ * when a new whiteout is necessary, create it.
22222+ */
22223+static struct dentry*
22224+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22225+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22226+{
4a4d8108
AM
22227+ struct dentry *wh_dentry;
22228+ struct super_block *sb;
22229+ struct path h_path;
22230+ int err, need_wh;
22231+ unsigned int udba;
22232+ aufs_bindex_t bcpup;
dece6358 22233+
4a4d8108
AM
22234+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22235+ wh_dentry = ERR_PTR(need_wh);
22236+ if (unlikely(need_wh < 0))
22237+ goto out;
22238+
22239+ sb = dentry->d_sb;
22240+ udba = au_opt_udba(sb);
22241+ bcpup = *rbcpup;
22242+ err = au_pin(pin, dentry, bcpup, udba,
22243+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22244+ wh_dentry = ERR_PTR(err);
22245+ if (unlikely(err))
22246+ goto out;
22247+
22248+ h_path.dentry = au_pinned_h_parent(pin);
22249+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22250+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22251+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22252+ wh_dentry = ERR_PTR(err);
22253+ if (unlikely(err))
22254+ goto out_unpin;
22255+ }
22256+
22257+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22258+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22259+ wh_dentry = NULL;
22260+ if (!need_wh)
22261+ goto out; /* success, no need to create whiteout */
22262+
22263+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22264+ if (IS_ERR(wh_dentry))
22265+ goto out_unpin;
22266+
22267+ /* returns with the parent is locked and wh_dentry is dget-ed */
22268+ goto out; /* success */
22269+
4f0767ce 22270+out_unpin:
4a4d8108 22271+ au_unpin(pin);
4f0767ce 22272+out:
4a4d8108 22273+ return wh_dentry;
1facf9fc 22274+}
22275+
4a4d8108
AM
22276+/*
22277+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22278+ * in order to be revertible and save time for removing many child whiteouts
22279+ * under the dir.
22280+ * returns 1 when there are too many child whiteout and caller should remove
22281+ * them asynchronously. returns 0 when the number of children is enough small to
22282+ * remove now or the branch fs is a remote fs.
22283+ * otherwise return an error.
22284+ */
22285+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22286+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22287+{
4a4d8108
AM
22288+ int rmdir_later, err, dirwh;
22289+ struct dentry *h_dentry;
22290+ struct super_block *sb;
5527c038 22291+ struct inode *inode;
4a4d8108
AM
22292+
22293+ sb = dentry->d_sb;
22294+ SiMustAnyLock(sb);
22295+ h_dentry = au_h_dptr(dentry, bindex);
22296+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22297+ if (unlikely(err))
22298+ goto out;
22299+
22300+ /* stop monitoring */
5527c038
JR
22301+ inode = d_inode(dentry);
22302+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22303+
22304+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22305+ dirwh = au_sbi(sb)->si_dirwh;
22306+ rmdir_later = (dirwh <= 1);
22307+ if (!rmdir_later)
22308+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22309+ dirwh);
22310+ if (rmdir_later)
22311+ return rmdir_later;
22312+ }
1facf9fc 22313+
4a4d8108
AM
22314+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22315+ if (unlikely(err)) {
523b37e3
AM
22316+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22317+ h_dentry, bindex, err);
4a4d8108
AM
22318+ err = 0;
22319+ }
dece6358 22320+
4f0767ce 22321+out:
4a4d8108
AM
22322+ AuTraceErr(err);
22323+ return err;
22324+}
1308ab2a 22325+
4a4d8108
AM
22326+/*
22327+ * final procedure for deleting a entry.
22328+ * maintain dentry and iattr.
22329+ */
22330+static void epilog(struct inode *dir, struct dentry *dentry,
22331+ aufs_bindex_t bindex)
22332+{
22333+ struct inode *inode;
1308ab2a 22334+
5527c038 22335+ inode = d_inode(dentry);
4a4d8108
AM
22336+ d_drop(dentry);
22337+ inode->i_ctime = dir->i_ctime;
1308ab2a 22338+
b912730e 22339+ au_dir_ts(dir, bindex);
be118d29 22340+ inode_inc_iversion(dir);
1facf9fc 22341+}
22342+
4a4d8108
AM
22343+/*
22344+ * when an error happened, remove the created whiteout and revert everything.
22345+ */
7f207e10
AM
22346+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22347+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22348+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22349+{
4a4d8108
AM
22350+ int rerr;
22351+ struct path h_path = {
22352+ .dentry = wh_dentry,
7f207e10 22353+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22354+ };
dece6358 22355+
7f207e10 22356+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22357+ if (!rerr) {
22358+ au_set_dbwh(dentry, bwh);
22359+ au_dtime_revert(dt);
22360+ return 0;
22361+ }
dece6358 22362+
523b37e3 22363+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22364+ return -EIO;
1facf9fc 22365+}
22366+
4a4d8108 22367+/* ---------------------------------------------------------------------- */
1facf9fc 22368+
4a4d8108 22369+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22370+{
4a4d8108 22371+ int err;
5afbbe0d 22372+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22373+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22374+ struct dentry *parent, *wh_dentry;
acd2b654 22375+ /* to reduce stack size */
c2b27bf2
AM
22376+ struct {
22377+ struct au_dtime dt;
22378+ struct au_pin pin;
22379+ struct path h_path;
22380+ } *a;
1facf9fc 22381+
4a4d8108 22382+ IMustLock(dir);
027c5e7a 22383+
c2b27bf2
AM
22384+ err = -ENOMEM;
22385+ a = kmalloc(sizeof(*a), GFP_NOFS);
22386+ if (unlikely(!a))
22387+ goto out;
22388+
027c5e7a
AM
22389+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22390+ if (unlikely(err))
c2b27bf2 22391+ goto out_free;
027c5e7a
AM
22392+ err = au_d_hashed_positive(dentry);
22393+ if (unlikely(err))
22394+ goto out_unlock;
5527c038 22395+ inode = d_inode(dentry);
4a4d8108 22396+ IMustLock(inode);
027c5e7a 22397+ err = -EISDIR;
2000de60 22398+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22399+ goto out_unlock; /* possible? */
1facf9fc 22400+
5afbbe0d 22401+ btop = au_dbtop(dentry);
4a4d8108
AM
22402+ bwh = au_dbwh(dentry);
22403+ bindex = -1;
027c5e7a
AM
22404+ parent = dentry->d_parent; /* dir inode is locked */
22405+ di_write_lock_parent(parent);
c2b27bf2
AM
22406+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22407+ &a->pin);
4a4d8108
AM
22408+ err = PTR_ERR(wh_dentry);
22409+ if (IS_ERR(wh_dentry))
027c5e7a 22410+ goto out_parent;
1facf9fc 22411+
5afbbe0d
AM
22412+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22413+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22414+ dget(a->h_path.dentry);
5afbbe0d 22415+ if (bindex == btop) {
c2b27bf2 22416+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22417+ delegated = NULL;
22418+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22419+ if (unlikely(err == -EWOULDBLOCK)) {
22420+ pr_warn("cannot retry for NFSv4 delegation"
22421+ " for an internal unlink\n");
22422+ iput(delegated);
22423+ }
4a4d8108
AM
22424+ } else {
22425+ /* dir inode is locked */
5527c038 22426+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22427+ IMustLock(h_dir);
22428+ err = 0;
22429+ }
dece6358 22430+
4a4d8108 22431+ if (!err) {
7f207e10 22432+ vfsub_drop_nlink(inode);
4a4d8108
AM
22433+ epilog(dir, dentry, bindex);
22434+
22435+ /* update target timestamps */
5afbbe0d 22436+ if (bindex == btop) {
c2b27bf2
AM
22437+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22438+ /*ignore*/
5527c038 22439+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22440+ } else
22441+ /* todo: this timestamp may be reverted later */
22442+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22443+ goto out_unpin; /* success */
1facf9fc 22444+ }
22445+
4a4d8108
AM
22446+ /* revert */
22447+ if (wh_dentry) {
22448+ int rerr;
22449+
c2b27bf2
AM
22450+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22451+ &a->dt);
4a4d8108
AM
22452+ if (rerr)
22453+ err = rerr;
dece6358 22454+ }
1facf9fc 22455+
027c5e7a 22456+out_unpin:
c2b27bf2 22457+ au_unpin(&a->pin);
4a4d8108 22458+ dput(wh_dentry);
c2b27bf2 22459+ dput(a->h_path.dentry);
027c5e7a 22460+out_parent:
4a4d8108 22461+ di_write_unlock(parent);
027c5e7a 22462+out_unlock:
4a4d8108 22463+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22464+out_free:
9f237c51 22465+ au_kfree_rcu(a);
027c5e7a 22466+out:
4a4d8108 22467+ return err;
dece6358
AM
22468+}
22469+
4a4d8108 22470+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22471+{
4a4d8108 22472+ int err, rmdir_later;
5afbbe0d 22473+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22474+ struct inode *inode;
22475+ struct dentry *parent, *wh_dentry, *h_dentry;
22476+ struct au_whtmp_rmdir *args;
acd2b654 22477+ /* to reduce stack size */
c2b27bf2
AM
22478+ struct {
22479+ struct au_dtime dt;
22480+ struct au_pin pin;
22481+ } *a;
1facf9fc 22482+
4a4d8108 22483+ IMustLock(dir);
027c5e7a 22484+
c2b27bf2
AM
22485+ err = -ENOMEM;
22486+ a = kmalloc(sizeof(*a), GFP_NOFS);
22487+ if (unlikely(!a))
22488+ goto out;
22489+
027c5e7a
AM
22490+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22491+ if (unlikely(err))
c2b27bf2 22492+ goto out_free;
53392da6
AM
22493+ err = au_alive_dir(dentry);
22494+ if (unlikely(err))
027c5e7a 22495+ goto out_unlock;
5527c038 22496+ inode = d_inode(dentry);
4a4d8108 22497+ IMustLock(inode);
027c5e7a 22498+ err = -ENOTDIR;
2000de60 22499+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22500+ goto out_unlock; /* possible? */
dece6358 22501+
4a4d8108
AM
22502+ err = -ENOMEM;
22503+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22504+ if (unlikely(!args))
22505+ goto out_unlock;
dece6358 22506+
4a4d8108
AM
22507+ parent = dentry->d_parent; /* dir inode is locked */
22508+ di_write_lock_parent(parent);
22509+ err = au_test_empty(dentry, &args->whlist);
22510+ if (unlikely(err))
027c5e7a 22511+ goto out_parent;
1facf9fc 22512+
5afbbe0d 22513+ btop = au_dbtop(dentry);
4a4d8108
AM
22514+ bwh = au_dbwh(dentry);
22515+ bindex = -1;
c2b27bf2
AM
22516+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22517+ &a->pin);
4a4d8108
AM
22518+ err = PTR_ERR(wh_dentry);
22519+ if (IS_ERR(wh_dentry))
027c5e7a 22520+ goto out_parent;
1facf9fc 22521+
5afbbe0d 22522+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22523+ dget(h_dentry);
22524+ rmdir_later = 0;
5afbbe0d
AM
22525+ if (bindex == btop) {
22526+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22527+ if (err > 0) {
22528+ rmdir_later = err;
22529+ err = 0;
22530+ }
22531+ } else {
22532+ /* stop monitoring */
5afbbe0d 22533+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22534+
22535+ /* dir inode is locked */
5527c038 22536+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22537+ err = 0;
22538+ }
22539+
4a4d8108 22540+ if (!err) {
027c5e7a 22541+ vfsub_dead_dir(inode);
4a4d8108
AM
22542+ au_set_dbdiropq(dentry, -1);
22543+ epilog(dir, dentry, bindex);
1308ab2a 22544+
4a4d8108 22545+ if (rmdir_later) {
5afbbe0d 22546+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22547+ args = NULL;
22548+ }
1308ab2a 22549+
4a4d8108 22550+ goto out_unpin; /* success */
1facf9fc 22551+ }
22552+
4a4d8108
AM
22553+ /* revert */
22554+ AuLabel(revert);
22555+ if (wh_dentry) {
22556+ int rerr;
1308ab2a 22557+
c2b27bf2
AM
22558+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22559+ &a->dt);
4a4d8108
AM
22560+ if (rerr)
22561+ err = rerr;
1facf9fc 22562+ }
22563+
4f0767ce 22564+out_unpin:
c2b27bf2 22565+ au_unpin(&a->pin);
4a4d8108
AM
22566+ dput(wh_dentry);
22567+ dput(h_dentry);
027c5e7a 22568+out_parent:
4a4d8108
AM
22569+ di_write_unlock(parent);
22570+ if (args)
22571+ au_whtmp_rmdir_free(args);
4f0767ce 22572+out_unlock:
4a4d8108 22573+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22574+out_free:
9f237c51 22575+ au_kfree_rcu(a);
4f0767ce 22576+out:
4a4d8108
AM
22577+ AuTraceErr(err);
22578+ return err;
dece6358 22579+}
7f207e10 22580diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
eca34b5c 22581--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
016522bc 22582+++ linux/fs/aufs/i_op_ren.c 2020-01-27 10:57:18.172204883 +0100
eca801bf 22583@@ -0,0 +1,1250 @@
cd7a4cd9 22584+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22585+/*
016522bc 22586+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 22587+ *
22588+ * This program, aufs is free software; you can redistribute it and/or modify
22589+ * it under the terms of the GNU General Public License as published by
22590+ * the Free Software Foundation; either version 2 of the License, or
22591+ * (at your option) any later version.
dece6358
AM
22592+ *
22593+ * This program is distributed in the hope that it will be useful,
22594+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22595+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22596+ * GNU General Public License for more details.
22597+ *
22598+ * You should have received a copy of the GNU General Public License
523b37e3 22599+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22600+ */
22601+
22602+/*
4a4d8108
AM
22603+ * inode operation (rename entry)
22604+ * todo: this is crazy monster
1facf9fc 22605+ */
22606+
eca801bf 22607+#include <linux/iversion.h>
1facf9fc 22608+#include "aufs.h"
22609+
4a4d8108
AM
22610+enum { AuSRC, AuDST, AuSrcDst };
22611+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22612+
f2c43d5f
AM
22613+#define AuRen_ISDIR_SRC 1
22614+#define AuRen_ISDIR_DST (1 << 1)
22615+#define AuRen_ISSAMEDIR (1 << 2)
22616+#define AuRen_WHSRC (1 << 3)
22617+#define AuRen_WHDST (1 << 4)
22618+#define AuRen_MNT_WRITE (1 << 5)
22619+#define AuRen_DT_DSTDIR (1 << 6)
22620+#define AuRen_DIROPQ_SRC (1 << 7)
22621+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22622+#define AuRen_DIRREN (1 << 9)
22623+#define AuRen_DROPPED_SRC (1 << 10)
22624+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22625+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22626+#define au_fset_ren(flags, name) \
22627+ do { (flags) |= AuRen_##name; } while (0)
22628+#define au_fclr_ren(flags, name) \
22629+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22630+
8b6a4947
AM
22631+#ifndef CONFIG_AUFS_DIRREN
22632+#undef AuRen_DIRREN
22633+#define AuRen_DIRREN 0
22634+#endif
22635+
4a4d8108
AM
22636+struct au_ren_args {
22637+ struct {
22638+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22639+ *wh_dentry;
22640+ struct inode *dir, *inode;
f2c43d5f 22641+ struct au_hinode *hdir, *hinode;
4a4d8108 22642+ struct au_dtime dt[AuParentChild];
f2c43d5f 22643+ aufs_bindex_t btop, bdiropq;
4a4d8108 22644+ } sd[AuSrcDst];
1facf9fc 22645+
4a4d8108
AM
22646+#define src_dentry sd[AuSRC].dentry
22647+#define src_dir sd[AuSRC].dir
22648+#define src_inode sd[AuSRC].inode
22649+#define src_h_dentry sd[AuSRC].h_dentry
22650+#define src_parent sd[AuSRC].parent
22651+#define src_h_parent sd[AuSRC].h_parent
22652+#define src_wh_dentry sd[AuSRC].wh_dentry
22653+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22654+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22655+#define src_h_dir sd[AuSRC].hdir->hi_inode
22656+#define src_dt sd[AuSRC].dt
5afbbe0d 22657+#define src_btop sd[AuSRC].btop
f2c43d5f 22658+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22659+
4a4d8108
AM
22660+#define dst_dentry sd[AuDST].dentry
22661+#define dst_dir sd[AuDST].dir
22662+#define dst_inode sd[AuDST].inode
22663+#define dst_h_dentry sd[AuDST].h_dentry
22664+#define dst_parent sd[AuDST].parent
22665+#define dst_h_parent sd[AuDST].h_parent
22666+#define dst_wh_dentry sd[AuDST].wh_dentry
22667+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22668+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22669+#define dst_h_dir sd[AuDST].hdir->hi_inode
22670+#define dst_dt sd[AuDST].dt
5afbbe0d 22671+#define dst_btop sd[AuDST].btop
f2c43d5f 22672+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22673+
22674+ struct dentry *h_trap;
22675+ struct au_branch *br;
4a4d8108
AM
22676+ struct path h_path;
22677+ struct au_nhash whlist;
f2c43d5f 22678+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22679+
f2c43d5f
AM
22680+ struct {
22681+ unsigned short auren_flags;
22682+ unsigned char flags; /* syscall parameter */
22683+ unsigned char exchange;
22684+ } __packed;
1facf9fc 22685+
4a4d8108
AM
22686+ struct au_whtmp_rmdir *thargs;
22687+ struct dentry *h_dst;
8b6a4947 22688+ struct au_hinode *h_root;
4a4d8108 22689+};
1308ab2a 22690+
4a4d8108 22691+/* ---------------------------------------------------------------------- */
1308ab2a 22692+
4a4d8108
AM
22693+/*
22694+ * functions for reverting.
22695+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22696+ * everything as if nothing happened.
4a4d8108
AM
22697+ * we don't need to revert the copied-up/down the parent dir since they are
22698+ * harmless.
22699+ */
1facf9fc 22700+
4a4d8108
AM
22701+#define RevertFailure(fmt, ...) do { \
22702+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22703+ ##__VA_ARGS__, err, rerr); \
22704+ err = -EIO; \
22705+} while (0)
1facf9fc 22706+
f2c43d5f 22707+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22708+{
4a4d8108 22709+ int rerr;
f2c43d5f
AM
22710+ struct dentry *d;
22711+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22712+
f2c43d5f
AM
22713+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22714+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22715+ rerr = au_diropq_remove(d, a->btgt);
22716+ au_hn_inode_unlock(src_or_dst(hinode));
22717+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22718+ if (rerr)
f2c43d5f
AM
22719+ RevertFailure("remove diropq %pd", d);
22720+
22721+#undef src_or_dst_
22722+}
22723+
22724+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22725+{
22726+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22727+ au_ren_do_rev_diropq(err, a, AuSRC);
22728+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22729+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22730+}
1facf9fc 22731+
4a4d8108
AM
22732+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22733+{
22734+ int rerr;
523b37e3 22735+ struct inode *delegated;
1facf9fc 22736+
b4510431
AM
22737+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22738+ a->src_h_parent);
4a4d8108
AM
22739+ rerr = PTR_ERR(a->h_path.dentry);
22740+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22741+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22742+ return;
1facf9fc 22743+ }
22744+
523b37e3 22745+ delegated = NULL;
4a4d8108
AM
22746+ rerr = vfsub_rename(a->dst_h_dir,
22747+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22748+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22749+ if (unlikely(rerr == -EWOULDBLOCK)) {
22750+ pr_warn("cannot retry for NFSv4 delegation"
22751+ " for an internal rename\n");
22752+ iput(delegated);
22753+ }
4a4d8108
AM
22754+ d_drop(a->h_path.dentry);
22755+ dput(a->h_path.dentry);
22756+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22757+ if (rerr)
523b37e3 22758+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22759+}
22760+
4a4d8108 22761+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22762+{
4a4d8108 22763+ int rerr;
523b37e3 22764+ struct inode *delegated;
dece6358 22765+
b4510431
AM
22766+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22767+ a->dst_h_parent);
4a4d8108
AM
22768+ rerr = PTR_ERR(a->h_path.dentry);
22769+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22770+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22771+ return;
22772+ }
5527c038 22773+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22774+ d_drop(a->h_path.dentry);
22775+ dput(a->h_path.dentry);
22776+ return;
dece6358
AM
22777+ }
22778+
523b37e3
AM
22779+ delegated = NULL;
22780+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22781+ &delegated, a->flags);
523b37e3
AM
22782+ if (unlikely(rerr == -EWOULDBLOCK)) {
22783+ pr_warn("cannot retry for NFSv4 delegation"
22784+ " for an internal rename\n");
22785+ iput(delegated);
22786+ }
4a4d8108
AM
22787+ d_drop(a->h_path.dentry);
22788+ dput(a->h_path.dentry);
22789+ if (!rerr)
22790+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22791+ else
523b37e3 22792+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22793+}
1308ab2a 22794+
4a4d8108
AM
22795+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22796+{
22797+ int rerr;
1308ab2a 22798+
4a4d8108
AM
22799+ a->h_path.dentry = a->src_wh_dentry;
22800+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22801+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22802+ if (rerr)
523b37e3 22803+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22804+}
4a4d8108 22805+#undef RevertFailure
1facf9fc 22806+
1308ab2a 22807+/* ---------------------------------------------------------------------- */
22808+
4a4d8108
AM
22809+/*
22810+ * when we have to copyup the renaming entry, do it with the rename-target name
22811+ * in order to minimize the cost (the later actual rename is unnecessary).
22812+ * otherwise rename it on the target branch.
22813+ */
22814+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22815+{
dece6358 22816+ int err;
4a4d8108 22817+ struct dentry *d;
523b37e3 22818+ struct inode *delegated;
1facf9fc 22819+
4a4d8108 22820+ d = a->src_dentry;
5afbbe0d 22821+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22822+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22823+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22824+ delegated = NULL;
4a4d8108 22825+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22826+ a->dst_h_dir, &a->h_path, &delegated,
22827+ a->flags);
523b37e3
AM
22828+ if (unlikely(err == -EWOULDBLOCK)) {
22829+ pr_warn("cannot retry for NFSv4 delegation"
22830+ " for an internal rename\n");
22831+ iput(delegated);
22832+ }
c2b27bf2 22833+ } else
86dc4139 22834+ BUG();
1308ab2a 22835+
027c5e7a
AM
22836+ if (!err && a->h_dst)
22837+ /* it will be set to dinfo later */
22838+ dget(a->h_dst);
1facf9fc 22839+
dece6358
AM
22840+ return err;
22841+}
1facf9fc 22842+
4a4d8108
AM
22843+/* cf. aufs_rmdir() */
22844+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22845+{
4a4d8108
AM
22846+ int err;
22847+ struct inode *dir;
1facf9fc 22848+
4a4d8108
AM
22849+ dir = a->dst_dir;
22850+ SiMustAnyLock(dir->i_sb);
22851+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22852+ au_sbi(dir->i_sb)->si_dirwh)
22853+ || au_test_fs_remote(a->h_dst->d_sb)) {
22854+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22855+ if (unlikely(err))
523b37e3
AM
22856+ pr_warn("failed removing whtmp dir %pd (%d), "
22857+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22858+ } else {
22859+ au_nhash_wh_free(&a->thargs->whlist);
22860+ a->thargs->whlist = a->whlist;
22861+ a->whlist.nh_num = 0;
22862+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22863+ dput(a->h_dst);
22864+ a->thargs = NULL;
22865+ }
22866+
22867+ return 0;
1308ab2a 22868+}
1facf9fc 22869+
4a4d8108 22870+/* make it 'opaque' dir. */
f2c43d5f 22871+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22872+{
22873+ int err;
f2c43d5f
AM
22874+ struct dentry *d, *diropq;
22875+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22876+
4a4d8108 22877+ err = 0;
f2c43d5f
AM
22878+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22879+ src_or_dst(bdiropq) = au_dbdiropq(d);
22880+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22881+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22882+ diropq = au_diropq_create(d, a->btgt);
22883+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22884+ if (IS_ERR(diropq))
22885+ err = PTR_ERR(diropq);
076b876e
AM
22886+ else
22887+ dput(diropq);
1facf9fc 22888+
f2c43d5f 22889+#undef src_or_dst_
4a4d8108
AM
22890+ return err;
22891+}
1facf9fc 22892+
f2c43d5f 22893+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
22894+{
22895+ int err;
f2c43d5f
AM
22896+ unsigned char always;
22897+ struct dentry *d;
1facf9fc 22898+
f2c43d5f
AM
22899+ err = 0;
22900+ d = a->dst_dentry; /* already renamed on the branch */
22901+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22902+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 22903+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
22904+ && a->btgt != au_dbdiropq(a->src_dentry)
22905+ && (a->dst_wh_dentry
22906+ || a->btgt <= au_dbdiropq(d)
22907+ /* hide the lower to keep xino */
22908+ /* the lowers may not be a dir, but we hide them anyway */
22909+ || a->btgt < au_dbbot(d)
22910+ || always)) {
22911+ AuDbg("here\n");
22912+ err = au_ren_do_diropq(a, AuSRC);
22913+ if (unlikely(err))
4a4d8108 22914+ goto out;
f2c43d5f 22915+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 22916+ }
f2c43d5f
AM
22917+ if (!a->exchange)
22918+ goto out; /* success */
1facf9fc 22919+
f2c43d5f
AM
22920+ d = a->src_dentry; /* already renamed on the branch */
22921+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22922+ && a->btgt != au_dbdiropq(a->dst_dentry)
22923+ && (a->btgt < au_dbdiropq(d)
22924+ || a->btgt < au_dbbot(d)
22925+ || always)) {
22926+ AuDbgDentry(a->src_dentry);
22927+ AuDbgDentry(a->dst_dentry);
22928+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 22929+ if (unlikely(err))
f2c43d5f
AM
22930+ goto out_rev_src;
22931+ au_fset_ren(a->auren_flags, DIROPQ_DST);
22932+ }
22933+ goto out; /* success */
dece6358 22934+
f2c43d5f
AM
22935+out_rev_src:
22936+ AuDbg("err %d, reverting src\n", err);
22937+ au_ren_rev_diropq(err, a);
22938+out:
22939+ return err;
22940+}
22941+
22942+static int do_rename(struct au_ren_args *a)
22943+{
22944+ int err;
22945+ struct dentry *d, *h_d;
22946+
22947+ if (!a->exchange) {
22948+ /* prepare workqueue args for asynchronous rmdir */
22949+ h_d = a->dst_h_dentry;
22950+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 22951+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
22952+ && d_is_positive(h_d)) {
22953+ err = -ENOMEM;
22954+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22955+ GFP_NOFS);
22956+ if (unlikely(!a->thargs))
22957+ goto out;
22958+ a->h_dst = dget(h_d);
22959+ }
22960+
22961+ /* create whiteout for src_dentry */
22962+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
22963+ a->src_bwh = au_dbwh(a->src_dentry);
22964+ AuDebugOn(a->src_bwh >= 0);
22965+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22966+ a->src_h_parent);
22967+ err = PTR_ERR(a->src_wh_dentry);
22968+ if (IS_ERR(a->src_wh_dentry))
22969+ goto out_thargs;
22970+ }
22971+
22972+ /* lookup whiteout for dentry */
22973+ if (au_ftest_ren(a->auren_flags, WHDST)) {
22974+ h_d = au_wh_lkup(a->dst_h_parent,
22975+ &a->dst_dentry->d_name, a->br);
22976+ err = PTR_ERR(h_d);
22977+ if (IS_ERR(h_d))
22978+ goto out_whsrc;
22979+ if (d_is_negative(h_d))
22980+ dput(h_d);
22981+ else
22982+ a->dst_wh_dentry = h_d;
22983+ }
22984+
22985+ /* rename dentry to tmpwh */
22986+ if (a->thargs) {
22987+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
22988+ if (unlikely(err))
22989+ goto out_whdst;
22990+
22991+ d = a->dst_dentry;
22992+ au_set_h_dptr(d, a->btgt, NULL);
22993+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
22994+ if (unlikely(err))
22995+ goto out_whtmp;
22996+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
22997+ }
4a4d8108 22998+ }
1facf9fc 22999+
5afbbe0d 23000+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
43982f53 23001+#if 0 /* debugging */
8b6a4947
AM
23002+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23003+ && d_is_positive(a->dst_h_dentry)
23004+ && a->src_btop != a->btgt);
23005+#endif
1facf9fc 23006+
4a4d8108 23007+ /* rename by vfs_rename or cpup */
4a4d8108
AM
23008+ err = au_ren_or_cpup(a);
23009+ if (unlikely(err))
23010+ /* leave the copied-up one */
23011+ goto out_whtmp;
1308ab2a 23012+
4a4d8108 23013+ /* make dir opaque */
f2c43d5f
AM
23014+ err = au_ren_diropq(a);
23015+ if (unlikely(err))
23016+ goto out_rename;
1308ab2a 23017+
4a4d8108 23018+ /* update target timestamps */
f2c43d5f
AM
23019+ if (a->exchange) {
23020+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23021+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23022+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23023+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23024+ }
5afbbe0d 23025+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
23026+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23027+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 23028+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 23029+
f2c43d5f
AM
23030+ if (!a->exchange) {
23031+ /* remove whiteout for dentry */
23032+ if (a->dst_wh_dentry) {
23033+ a->h_path.dentry = a->dst_wh_dentry;
23034+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23035+ a->dst_dentry);
23036+ if (unlikely(err))
23037+ goto out_diropq;
23038+ }
1facf9fc 23039+
f2c43d5f
AM
23040+ /* remove whtmp */
23041+ if (a->thargs)
23042+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 23043+
f2c43d5f
AM
23044+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23045+ }
4a4d8108
AM
23046+ err = 0;
23047+ goto out_success;
23048+
4f0767ce 23049+out_diropq:
f2c43d5f 23050+ au_ren_rev_diropq(err, a);
4f0767ce 23051+out_rename:
7e9cd9fe 23052+ au_ren_rev_rename(err, a);
027c5e7a 23053+ dput(a->h_dst);
4f0767ce 23054+out_whtmp:
4a4d8108
AM
23055+ if (a->thargs)
23056+ au_ren_rev_whtmp(err, a);
4f0767ce 23057+out_whdst:
4a4d8108
AM
23058+ dput(a->dst_wh_dentry);
23059+ a->dst_wh_dentry = NULL;
4f0767ce 23060+out_whsrc:
4a4d8108
AM
23061+ if (a->src_wh_dentry)
23062+ au_ren_rev_whsrc(err, a);
4f0767ce 23063+out_success:
4a4d8108
AM
23064+ dput(a->src_wh_dentry);
23065+ dput(a->dst_wh_dentry);
4f0767ce 23066+out_thargs:
4a4d8108
AM
23067+ if (a->thargs) {
23068+ dput(a->h_dst);
23069+ au_whtmp_rmdir_free(a->thargs);
23070+ a->thargs = NULL;
23071+ }
4f0767ce 23072+out:
4a4d8108 23073+ return err;
dece6358 23074+}
1facf9fc 23075+
1308ab2a 23076+/* ---------------------------------------------------------------------- */
1facf9fc 23077+
4a4d8108
AM
23078+/*
23079+ * test if @dentry dir can be rename destination or not.
23080+ * success means, it is a logically empty dir.
23081+ */
23082+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 23083+{
4a4d8108 23084+ return au_test_empty(dentry, whlist);
1308ab2a 23085+}
1facf9fc 23086+
4a4d8108 23087+/*
8b6a4947
AM
23088+ * test if @a->src_dentry dir can be rename source or not.
23089+ * if it can, return 0.
4a4d8108
AM
23090+ * success means,
23091+ * - it is a logically empty dir.
23092+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 23093+ * on the lower branch unless DIRREN is on.
4a4d8108 23094+ */
8b6a4947 23095+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
23096+{
23097+ int err;
23098+ unsigned int rdhash;
8b6a4947
AM
23099+ aufs_bindex_t btop, btgt;
23100+ struct dentry *dentry;
23101+ struct super_block *sb;
23102+ struct au_sbinfo *sbinfo;
1facf9fc 23103+
8b6a4947
AM
23104+ dentry = a->src_dentry;
23105+ sb = dentry->d_sb;
23106+ sbinfo = au_sbi(sb);
23107+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23108+ au_fset_ren(a->auren_flags, DIRREN);
23109+
23110+ btgt = a->btgt;
5afbbe0d
AM
23111+ btop = au_dbtop(dentry);
23112+ if (btop != btgt) {
4a4d8108 23113+ struct au_nhash whlist;
dece6358 23114+
8b6a4947
AM
23115+ SiMustAnyLock(sb);
23116+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23117+ if (!rdhash)
23118+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23119+ dentry));
23120+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23121+ if (unlikely(err))
23122+ goto out;
23123+ err = au_test_empty(dentry, &whlist);
23124+ au_nhash_wh_free(&whlist);
23125+ goto out;
23126+ }
dece6358 23127+
5afbbe0d 23128+ if (btop == au_dbtaildir(dentry))
4a4d8108 23129+ return 0; /* success */
dece6358 23130+
4a4d8108 23131+ err = au_test_empty_lower(dentry);
1facf9fc 23132+
4f0767ce 23133+out:
4a4d8108 23134+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23135+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23136+ err = 0;
23137+ } else {
23138+ AuWarn1("renaming dir who has child(ren) on multiple "
23139+ "branches, is not supported\n");
23140+ err = -EXDEV;
23141+ }
4a4d8108
AM
23142+ }
23143+ return err;
23144+}
1308ab2a 23145+
4a4d8108
AM
23146+/* side effect: sets whlist and h_dentry */
23147+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23148+{
4a4d8108
AM
23149+ int err;
23150+ unsigned int rdhash;
23151+ struct dentry *d;
1facf9fc 23152+
4a4d8108
AM
23153+ d = a->dst_dentry;
23154+ SiMustAnyLock(d->d_sb);
1facf9fc 23155+
4a4d8108 23156+ err = 0;
f2c43d5f 23157+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23158+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23159+ if (!rdhash)
23160+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23161+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23162+ if (unlikely(err))
23163+ goto out;
1308ab2a 23164+
f2c43d5f
AM
23165+ if (!a->exchange) {
23166+ au_set_dbtop(d, a->dst_btop);
23167+ err = may_rename_dstdir(d, &a->whlist);
23168+ au_set_dbtop(d, a->btgt);
23169+ } else
8b6a4947 23170+ err = may_rename_srcdir(a);
4a4d8108 23171+ }
5afbbe0d 23172+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23173+ if (unlikely(err))
23174+ goto out;
23175+
23176+ d = a->src_dentry;
5afbbe0d 23177+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23178+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23179+ err = may_rename_srcdir(a);
4a4d8108
AM
23180+ if (unlikely(err)) {
23181+ au_nhash_wh_free(&a->whlist);
23182+ a->whlist.nh_num = 0;
23183+ }
23184+ }
4f0767ce 23185+out:
4a4d8108 23186+ return err;
1facf9fc 23187+}
23188+
4a4d8108 23189+/* ---------------------------------------------------------------------- */
1facf9fc 23190+
4a4d8108
AM
23191+/*
23192+ * simple tests for rename.
23193+ * following the checks in vfs, plus the parent-child relationship.
23194+ */
23195+static int au_may_ren(struct au_ren_args *a)
23196+{
23197+ int err, isdir;
23198+ struct inode *h_inode;
1facf9fc 23199+
5afbbe0d 23200+ if (a->src_btop == a->btgt) {
4a4d8108 23201+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23202+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23203+ if (unlikely(err))
23204+ goto out;
23205+ err = -EINVAL;
23206+ if (unlikely(a->src_h_dentry == a->h_trap))
23207+ goto out;
23208+ }
1facf9fc 23209+
4a4d8108 23210+ err = 0;
5afbbe0d 23211+ if (a->dst_btop != a->btgt)
4a4d8108 23212+ goto out;
1facf9fc 23213+
027c5e7a
AM
23214+ err = -ENOTEMPTY;
23215+ if (unlikely(a->dst_h_dentry == a->h_trap))
23216+ goto out;
23217+
4a4d8108 23218+ err = -EIO;
f2c43d5f 23219+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23220+ if (d_really_is_negative(a->dst_dentry)) {
23221+ if (d_is_negative(a->dst_h_dentry))
23222+ err = au_may_add(a->dst_dentry, a->btgt,
23223+ a->dst_h_parent, isdir);
4a4d8108 23224+ } else {
5527c038 23225+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23226+ goto out;
5527c038
JR
23227+ h_inode = d_inode(a->dst_h_dentry);
23228+ if (h_inode->i_nlink)
23229+ err = au_may_del(a->dst_dentry, a->btgt,
23230+ a->dst_h_parent, isdir);
4a4d8108 23231+ }
1facf9fc 23232+
4f0767ce 23233+out:
4a4d8108
AM
23234+ if (unlikely(err == -ENOENT || err == -EEXIST))
23235+ err = -EIO;
23236+ AuTraceErr(err);
23237+ return err;
23238+}
1facf9fc 23239+
1308ab2a 23240+/* ---------------------------------------------------------------------- */
1facf9fc 23241+
4a4d8108
AM
23242+/*
23243+ * locking order
23244+ * (VFS)
23245+ * - src_dir and dir by lock_rename()
acd2b654 23246+ * - inode if exists
4a4d8108
AM
23247+ * (aufs)
23248+ * - lock all
23249+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23250+ * + si_read_lock
23251+ * + di_write_lock2_child()
23252+ * + di_write_lock_child()
23253+ * + ii_write_lock_child()
23254+ * + di_write_lock_child2()
23255+ * + ii_write_lock_child2()
23256+ * + src_parent and parent
23257+ * + di_write_lock_parent()
23258+ * + ii_write_lock_parent()
23259+ * + di_write_lock_parent2()
23260+ * + ii_write_lock_parent2()
23261+ * + lower src_dir and dir by vfsub_lock_rename()
23262+ * + verify the every relationships between child and parent. if any
23263+ * of them failed, unlock all and return -EBUSY.
23264+ */
23265+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23266+{
4a4d8108
AM
23267+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23268+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23269+ if (au_ftest_ren(a->auren_flags, DIRREN)
23270+ && a->h_root)
23271+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23272+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23273+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23274+}
23275+
4a4d8108 23276+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23277+{
4a4d8108
AM
23278+ int err;
23279+ unsigned int udba;
1308ab2a 23280+
4a4d8108
AM
23281+ err = 0;
23282+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23283+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23284+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23285+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23286+
23287+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23288+ if (unlikely(err))
23289+ goto out;
f2c43d5f 23290+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23291+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23292+ struct dentry *root;
23293+ struct inode *dir;
23294+
23295+ /*
23296+ * sbinfo is already locked, so this ii_read_lock is
23297+ * unnecessary. but our debugging feature checks it.
23298+ */
23299+ root = a->src_inode->i_sb->s_root;
23300+ if (root != a->src_parent && root != a->dst_parent) {
23301+ dir = d_inode(root);
23302+ ii_read_lock_parent3(dir);
23303+ a->h_root = au_hi(dir, a->btgt);
23304+ ii_read_unlock(dir);
23305+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23306+ }
23307+ }
4a4d8108
AM
23308+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23309+ a->dst_h_parent, a->dst_hdir);
23310+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23311+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23312+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23313+ err = au_busy_or_stale();
5afbbe0d 23314+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23315+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23316+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23317+ a->br);
5afbbe0d 23318+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23319+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23320+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23321+ a->br);
86dc4139 23322+ if (!err)
4a4d8108 23323+ goto out; /* success */
4a4d8108
AM
23324+
23325+ err = au_busy_or_stale();
4a4d8108 23326+ au_ren_unlock(a);
86dc4139 23327+
4f0767ce 23328+out:
4a4d8108 23329+ return err;
1facf9fc 23330+}
23331+
23332+/* ---------------------------------------------------------------------- */
23333+
4a4d8108 23334+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23335+{
4a4d8108 23336+ struct inode *dir;
dece6358 23337+
4a4d8108 23338+ dir = a->dst_dir;
be118d29 23339+ inode_inc_iversion(dir);
f2c43d5f 23340+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23341+ /* is this updating defined in POSIX? */
23342+ au_cpup_attr_timesizes(a->src_inode);
23343+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23344+ }
b912730e 23345+ au_dir_ts(dir, a->btgt);
dece6358 23346+
f2c43d5f
AM
23347+ if (a->exchange) {
23348+ dir = a->src_dir;
be118d29 23349+ inode_inc_iversion(dir);
f2c43d5f
AM
23350+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23351+ /* is this updating defined in POSIX? */
23352+ au_cpup_attr_timesizes(a->dst_inode);
23353+ au_cpup_attr_nlink(dir, /*force*/1);
23354+ }
23355+ au_dir_ts(dir, a->btgt);
23356+ }
23357+
23358+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23359+ return;
dece6358 23360+
4a4d8108 23361+ dir = a->src_dir;
be118d29 23362+ inode_inc_iversion(dir);
f2c43d5f 23363+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23364+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23365+ au_dir_ts(dir, a->btgt);
1facf9fc 23366+}
23367+
4a4d8108 23368+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23369+{
5afbbe0d 23370+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23371+ struct dentry *d, *h_d;
23372+ struct inode *i, *h_i;
23373+ struct super_block *sb;
dece6358 23374+
027c5e7a
AM
23375+ d = a->dst_dentry;
23376+ d_drop(d);
23377+ if (a->h_dst)
23378+ /* already dget-ed by au_ren_or_cpup() */
23379+ au_set_h_dptr(d, a->btgt, a->h_dst);
23380+
23381+ i = a->dst_inode;
23382+ if (i) {
f2c43d5f
AM
23383+ if (!a->exchange) {
23384+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23385+ vfsub_drop_nlink(i);
23386+ else {
23387+ vfsub_dead_dir(i);
23388+ au_cpup_attr_timesizes(i);
23389+ }
23390+ au_update_dbrange(d, /*do_put_zero*/1);
23391+ } else
23392+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23393+ } else {
5afbbe0d
AM
23394+ bbot = a->btgt;
23395+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23396+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23397+ bbot = au_dbbot(d);
23398+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23399+ au_set_h_dptr(d, bindex, NULL);
23400+ au_update_dbrange(d, /*do_put_zero*/0);
23401+ }
23402+
8b6a4947
AM
23403+ if (a->exchange
23404+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23405+ d_drop(a->src_dentry);
23406+ if (au_ftest_ren(a->auren_flags, DIRREN))
23407+ au_set_dbwh(a->src_dentry, -1);
23408+ return;
23409+ }
23410+
4a4d8108 23411+ d = a->src_dentry;
8b6a4947
AM
23412+ au_set_dbwh(d, -1);
23413+ bbot = au_dbbot(d);
23414+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23415+ h_d = au_h_dptr(d, bindex);
23416+ if (h_d)
23417+ au_set_h_dptr(d, bindex, NULL);
23418+ }
23419+ au_set_dbbot(d, a->btgt);
4a4d8108 23420+
8b6a4947
AM
23421+ sb = d->d_sb;
23422+ i = a->src_inode;
23423+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23424+ return; /* success */
4a4d8108 23425+
8b6a4947
AM
23426+ bbot = au_ibbot(i);
23427+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23428+ h_i = au_h_iptr(i, bindex);
23429+ if (h_i) {
23430+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23431+ /* ignore this error */
23432+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23433+ }
23434+ }
8b6a4947 23435+ au_set_ibbot(i, a->btgt);
1308ab2a 23436+}
dece6358 23437+
4a4d8108
AM
23438+/* ---------------------------------------------------------------------- */
23439+
23440+/* mainly for link(2) and rename(2) */
23441+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23442+{
4a4d8108
AM
23443+ aufs_bindex_t bdiropq, bwh;
23444+ struct dentry *parent;
23445+ struct au_branch *br;
23446+
23447+ parent = dentry->d_parent;
5527c038 23448+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23449+
23450+ bdiropq = au_dbdiropq(parent);
23451+ bwh = au_dbwh(dentry);
23452+ br = au_sbr(dentry->d_sb, btgt);
23453+ if (au_br_rdonly(br)
23454+ || (0 <= bdiropq && bdiropq < btgt)
23455+ || (0 <= bwh && bwh < btgt))
23456+ btgt = -1;
23457+
23458+ AuDbg("btgt %d\n", btgt);
23459+ return btgt;
1facf9fc 23460+}
23461+
5afbbe0d 23462+/* sets src_btop, dst_btop and btgt */
4a4d8108 23463+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23464+{
4a4d8108
AM
23465+ int err;
23466+ struct au_wr_dir_args wr_dir_args = {
23467+ /* .force_btgt = -1, */
23468+ .flags = AuWrDir_ADD_ENTRY
23469+ };
dece6358 23470+
5afbbe0d
AM
23471+ a->src_btop = au_dbtop(a->src_dentry);
23472+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23473+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23474+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23475+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23476+ wr_dir_args.force_btgt = a->src_btop;
23477+ if (a->dst_inode && a->dst_btop < a->src_btop)
23478+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23479+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23480+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23481+ a->btgt = err;
f2c43d5f
AM
23482+ if (a->exchange)
23483+ au_update_dbtop(a->dst_dentry);
dece6358 23484+
4a4d8108 23485+ return err;
1facf9fc 23486+}
23487+
4a4d8108 23488+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23489+{
4a4d8108
AM
23490+ a->h_path.dentry = a->src_h_parent;
23491+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23492+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23493+ a->h_path.dentry = a->dst_h_parent;
23494+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23495+ }
1facf9fc 23496+
f2c43d5f
AM
23497+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23498+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23499+ && !a->exchange)
4a4d8108 23500+ return;
dece6358 23501+
4a4d8108
AM
23502+ a->h_path.dentry = a->src_h_dentry;
23503+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23504+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23505+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23506+ a->h_path.dentry = a->dst_h_dentry;
23507+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23508+ }
1308ab2a 23509+}
dece6358 23510+
4a4d8108 23511+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23512+{
4a4d8108 23513+ struct dentry *h_d;
febd17d6 23514+ struct inode *h_inode;
4a4d8108
AM
23515+
23516+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23517+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23518+ au_dtime_revert(a->dst_dt + AuPARENT);
23519+
f2c43d5f 23520+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23521+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23522+ h_inode = d_inode(h_d);
23523+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23524+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23525+ inode_unlock(h_inode);
4a4d8108 23526+
f2c43d5f 23527+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23528+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23529+ h_inode = d_inode(h_d);
23530+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23531+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 23532+ inode_unlock(h_inode);
1facf9fc 23533+ }
23534+ }
23535+}
23536+
4a4d8108
AM
23537+/* ---------------------------------------------------------------------- */
23538+
23539+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23540+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23541+ unsigned int _flags)
1facf9fc 23542+{
f2c43d5f 23543+ int err, lock_flags;
8b6a4947 23544+ void *rev;
4a4d8108
AM
23545+ /* reduce stack space */
23546+ struct au_ren_args *a;
f2c43d5f 23547+ struct au_pin pin;
4a4d8108 23548+
f2c43d5f 23549+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23550+ IMustLock(_src_dir);
23551+ IMustLock(_dst_dir);
23552+
f2c43d5f
AM
23553+ err = -EINVAL;
23554+ if (unlikely(_flags & RENAME_WHITEOUT))
23555+ goto out;
23556+
4a4d8108
AM
23557+ err = -ENOMEM;
23558+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23559+ a = kzalloc(sizeof(*a), GFP_NOFS);
23560+ if (unlikely(!a))
23561+ goto out;
23562+
f2c43d5f 23563+ a->flags = _flags;
acd2b654
AM
23564+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23565+ && RENAME_EXCHANGE > U8_MAX);
f2c43d5f 23566+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23567+ a->src_dir = _src_dir;
23568+ a->src_dentry = _src_dentry;
5527c038
JR
23569+ a->src_inode = NULL;
23570+ if (d_really_is_positive(a->src_dentry))
23571+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23572+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23573+ a->dst_dir = _dst_dir;
23574+ a->dst_dentry = _dst_dentry;
5527c038
JR
23575+ a->dst_inode = NULL;
23576+ if (d_really_is_positive(a->dst_dentry))
23577+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23578+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23579+ if (a->dst_inode) {
f2c43d5f
AM
23580+ /*
23581+ * if EXCHANGE && src is non-dir && dst is dir,
23582+ * dst is not locked.
23583+ */
23584+ /* IMustLock(a->dst_inode); */
4a4d8108 23585+ au_igrab(a->dst_inode);
1facf9fc 23586+ }
1facf9fc 23587+
4a4d8108 23588+ err = -ENOTDIR;
f2c43d5f 23589+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23590+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23591+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23592+ if (unlikely(!a->exchange
23593+ && d_really_is_positive(a->dst_dentry)
2000de60 23594+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23595+ goto out_free;
f2c43d5f
AM
23596+ lock_flags |= AuLock_DIRS;
23597+ }
23598+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23599+ au_fset_ren(a->auren_flags, ISDIR_DST);
23600+ if (unlikely(!a->exchange
23601+ && d_really_is_positive(a->src_dentry)
23602+ && !d_is_dir(a->src_dentry)))
23603+ goto out_free;
23604+ lock_flags |= AuLock_DIRS;
b95c5147 23605+ }
8b6a4947
AM
23606+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23607+ lock_flags);
e49829fe
JR
23608+ if (unlikely(err))
23609+ goto out_free;
1facf9fc 23610+
027c5e7a
AM
23611+ err = au_d_hashed_positive(a->src_dentry);
23612+ if (unlikely(err))
23613+ goto out_unlock;
23614+ err = -ENOENT;
23615+ if (a->dst_inode) {
23616+ /*
f2c43d5f 23617+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23618+ * function. It means we cannot rely upon d_unhashed().
23619+ */
23620+ if (unlikely(!a->dst_inode->i_nlink))
23621+ goto out_unlock;
f2c43d5f 23622+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23623+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23624+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23625+ goto out_unlock;
23626+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23627+ goto out_unlock;
23628+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23629+ goto out_unlock;
23630+
7eafdf33
AM
23631+ /*
23632+ * is it possible?
79b8bda9 23633+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23634+ * there may exist a problem somewhere else.
23635+ */
23636+ err = -EINVAL;
5527c038 23637+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23638+ goto out_unlock;
23639+
f2c43d5f 23640+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23641+ di_write_lock_parent(a->dst_parent);
1facf9fc 23642+
4a4d8108
AM
23643+ /* which branch we process */
23644+ err = au_ren_wbr(a);
23645+ if (unlikely(err < 0))
027c5e7a 23646+ goto out_parent;
4a4d8108 23647+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23648+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23649+
4a4d8108
AM
23650+ /* are they available to be renamed */
23651+ err = au_ren_may_dir(a);
23652+ if (unlikely(err))
23653+ goto out_children;
1facf9fc 23654+
4a4d8108 23655+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23656+ if (a->dst_btop == a->btgt) {
f2c43d5f 23657+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23658+ } else {
23659+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23660+ if (unlikely(err))
23661+ goto out_children;
23662+ }
1facf9fc 23663+
f2c43d5f
AM
23664+ err = 0;
23665+ if (!a->exchange) {
23666+ if (a->src_dir != a->dst_dir) {
23667+ /*
23668+ * this temporary unlock is safe,
23669+ * because both dir->i_mutex are locked.
23670+ */
23671+ di_write_unlock(a->dst_parent);
23672+ di_write_lock_parent(a->src_parent);
23673+ err = au_wr_dir_need_wh(a->src_dentry,
23674+ au_ftest_ren(a->auren_flags,
23675+ ISDIR_SRC),
23676+ &a->btgt);
23677+ di_write_unlock(a->src_parent);
23678+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23679+ /*isdir*/1);
23680+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23681+ } else
23682+ err = au_wr_dir_need_wh(a->src_dentry,
23683+ au_ftest_ren(a->auren_flags,
23684+ ISDIR_SRC),
23685+ &a->btgt);
23686+ }
4a4d8108
AM
23687+ if (unlikely(err < 0))
23688+ goto out_children;
23689+ if (err)
f2c43d5f 23690+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23691+
86dc4139 23692+ /* cpup src */
5afbbe0d 23693+ if (a->src_btop != a->btgt) {
86dc4139
AM
23694+ err = au_pin(&pin, a->src_dentry, a->btgt,
23695+ au_opt_udba(a->src_dentry->d_sb),
23696+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23697+ if (!err) {
c2b27bf2
AM
23698+ struct au_cp_generic cpg = {
23699+ .dentry = a->src_dentry,
23700+ .bdst = a->btgt,
5afbbe0d 23701+ .bsrc = a->src_btop,
c2b27bf2
AM
23702+ .len = -1,
23703+ .pin = &pin,
23704+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23705+ };
5afbbe0d 23706+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23707+ err = au_sio_cpup_simple(&cpg);
367653fa 23708+ au_unpin(&pin);
86dc4139 23709+ }
86dc4139
AM
23710+ if (unlikely(err))
23711+ goto out_children;
5afbbe0d 23712+ a->src_btop = a->btgt;
86dc4139 23713+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23714+ if (!a->exchange)
23715+ au_fset_ren(a->auren_flags, WHSRC);
23716+ }
23717+
23718+ /* cpup dst */
23719+ if (a->exchange && a->dst_inode
23720+ && a->dst_btop != a->btgt) {
23721+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23722+ au_opt_udba(a->dst_dentry->d_sb),
23723+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23724+ if (!err) {
23725+ struct au_cp_generic cpg = {
23726+ .dentry = a->dst_dentry,
23727+ .bdst = a->btgt,
23728+ .bsrc = a->dst_btop,
23729+ .len = -1,
23730+ .pin = &pin,
23731+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23732+ };
23733+ err = au_sio_cpup_simple(&cpg);
23734+ au_unpin(&pin);
23735+ }
23736+ if (unlikely(err))
23737+ goto out_children;
23738+ a->dst_btop = a->btgt;
23739+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23740+ }
23741+
4a4d8108
AM
23742+ /* lock them all */
23743+ err = au_ren_lock(a);
23744+ if (unlikely(err))
86dc4139 23745+ /* leave the copied-up one */
4a4d8108 23746+ goto out_children;
1facf9fc 23747+
f2c43d5f
AM
23748+ if (!a->exchange) {
23749+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23750+ err = au_may_ren(a);
23751+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23752+ err = -ENAMETOOLONG;
23753+ if (unlikely(err))
23754+ goto out_hdir;
23755+ }
1facf9fc 23756+
4a4d8108
AM
23757+ /* store timestamps to be revertible */
23758+ au_ren_dt(a);
1facf9fc 23759+
8b6a4947
AM
23760+ /* store dirren info */
23761+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23762+ err = au_dr_rename(a->src_dentry, a->btgt,
23763+ &a->dst_dentry->d_name, &rev);
23764+ AuTraceErr(err);
23765+ if (unlikely(err))
23766+ goto out_dt;
23767+ }
23768+
4a4d8108
AM
23769+ /* here we go */
23770+ err = do_rename(a);
23771+ if (unlikely(err))
8b6a4947
AM
23772+ goto out_dirren;
23773+
23774+ if (au_ftest_ren(a->auren_flags, DIRREN))
23775+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23776+
23777+ /* update dir attributes */
23778+ au_ren_refresh_dir(a);
23779+
23780+ /* dput/iput all lower dentries */
23781+ au_ren_refresh(a);
23782+
23783+ goto out_hdir; /* success */
23784+
8b6a4947
AM
23785+out_dirren:
23786+ if (au_ftest_ren(a->auren_flags, DIRREN))
23787+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23788+out_dt:
4a4d8108 23789+ au_ren_rev_dt(err, a);
4f0767ce 23790+out_hdir:
4a4d8108 23791+ au_ren_unlock(a);
4f0767ce 23792+out_children:
4a4d8108 23793+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23794+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23795+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23796+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23797+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23798+ }
027c5e7a 23799+out_parent:
f2c43d5f 23800+ if (!err) {
8b6a4947
AM
23801+ if (d_unhashed(a->src_dentry))
23802+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23803+ if (d_unhashed(a->dst_dentry))
23804+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23805+ if (!a->exchange)
23806+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23807+ else {
f2c43d5f 23808+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23809+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23810+ d_drop(a->dst_dentry);
23811+ }
23812+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23813+ d_drop(a->src_dentry);
f2c43d5f 23814+ } else {
5afbbe0d 23815+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23816+ if (!a->dst_inode)
23817+ d_drop(a->dst_dentry);
23818+ }
f2c43d5f 23819+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23820+ di_write_unlock(a->dst_parent);
23821+ else
23822+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23823+out_unlock:
4a4d8108 23824+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23825+out_free:
4a4d8108
AM
23826+ iput(a->dst_inode);
23827+ if (a->thargs)
23828+ au_whtmp_rmdir_free(a->thargs);
9f237c51 23829+ au_kfree_rcu(a);
4f0767ce 23830+out:
4a4d8108
AM
23831+ AuTraceErr(err);
23832+ return err;
1308ab2a 23833+}
7f207e10 23834diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
eca34b5c
AM
23835--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
23836+++ linux/fs/aufs/Kconfig 2019-07-11 15:42:14.458904362 +0200
2121bcd9
AM
23837@@ -0,0 +1,199 @@
23838+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
23839+config AUFS_FS
23840+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23841+ help
23842+ Aufs is a stackable unification filesystem such as Unionfs,
23843+ which unifies several directories and provides a merged single
23844+ directory.
23845+ In the early days, aufs was entirely re-designed and
23846+ re-implemented Unionfs Version 1.x series. Introducing many
23847+ original ideas, approaches and improvements, it becomes totally
23848+ different from Unionfs while keeping the basic features.
1facf9fc 23849+
4a4d8108
AM
23850+if AUFS_FS
23851+choice
23852+ prompt "Maximum number of branches"
23853+ default AUFS_BRANCH_MAX_127
23854+ help
23855+ Specifies the maximum number of branches (or member directories)
23856+ in a single aufs. The larger value consumes more system
23857+ resources and has a minor impact to performance.
23858+config AUFS_BRANCH_MAX_127
23859+ bool "127"
23860+ help
23861+ Specifies the maximum number of branches (or member directories)
23862+ in a single aufs. The larger value consumes more system
23863+ resources and has a minor impact to performance.
23864+config AUFS_BRANCH_MAX_511
23865+ bool "511"
23866+ help
23867+ Specifies the maximum number of branches (or member directories)
23868+ in a single aufs. The larger value consumes more system
23869+ resources and has a minor impact to performance.
23870+config AUFS_BRANCH_MAX_1023
23871+ bool "1023"
23872+ help
23873+ Specifies the maximum number of branches (or member directories)
23874+ in a single aufs. The larger value consumes more system
23875+ resources and has a minor impact to performance.
23876+config AUFS_BRANCH_MAX_32767
23877+ bool "32767"
23878+ help
23879+ Specifies the maximum number of branches (or member directories)
23880+ in a single aufs. The larger value consumes more system
23881+ resources and has a minor impact to performance.
23882+endchoice
1facf9fc 23883+
e49829fe
JR
23884+config AUFS_SBILIST
23885+ bool
23886+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23887+ default y
23888+ help
23889+ Automatic configuration for internal use.
23890+ When aufs supports Magic SysRq or /proc, enabled automatically.
23891+
4a4d8108
AM
23892+config AUFS_HNOTIFY
23893+ bool "Detect direct branch access (bypassing aufs)"
23894+ help
23895+ If you want to modify files on branches directly, eg. bypassing aufs,
23896+ and want aufs to detect the changes of them fully, then enable this
23897+ option and use 'udba=notify' mount option.
7f207e10 23898+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
23899+ It will have a negative impact to the performance.
23900+ See detail in aufs.5.
dece6358 23901+
4a4d8108
AM
23902+choice
23903+ prompt "method" if AUFS_HNOTIFY
23904+ default AUFS_HFSNOTIFY
23905+config AUFS_HFSNOTIFY
23906+ bool "fsnotify"
23907+ select FSNOTIFY
4a4d8108 23908+endchoice
1facf9fc 23909+
4a4d8108
AM
23910+config AUFS_EXPORT
23911+ bool "NFS-exportable aufs"
2cbb1c4b 23912+ depends on EXPORTFS
4a4d8108
AM
23913+ help
23914+ If you want to export your mounted aufs via NFS, then enable this
23915+ option. There are several requirements for this configuration.
23916+ See detail in aufs.5.
1facf9fc 23917+
4a4d8108
AM
23918+config AUFS_INO_T_64
23919+ bool
23920+ depends on AUFS_EXPORT
23921+ depends on 64BIT && !(ALPHA || S390)
23922+ default y
23923+ help
23924+ Automatic configuration for internal use.
23925+ /* typedef unsigned long/int __kernel_ino_t */
23926+ /* alpha and s390x are int */
1facf9fc 23927+
c1595e42
JR
23928+config AUFS_XATTR
23929+ bool "support for XATTR/EA (including Security Labels)"
23930+ help
23931+ If your branch fs supports XATTR/EA and you want to make them
23932+ available in aufs too, then enable this opsion and specify the
23933+ branch attributes for EA.
23934+ See detail in aufs.5.
23935+
076b876e
AM
23936+config AUFS_FHSM
23937+ bool "File-based Hierarchical Storage Management"
23938+ help
23939+ Hierarchical Storage Management (or HSM) is a well-known feature
23940+ in the storage world. Aufs provides this feature as file-based.
23941+ with multiple branches.
23942+ These multiple branches are prioritized, ie. the topmost one
23943+ should be the fastest drive and be used heavily.
23944+
4a4d8108
AM
23945+config AUFS_RDU
23946+ bool "Readdir in userspace"
23947+ help
23948+ Aufs has two methods to provide a merged view for a directory,
23949+ by a user-space library and by kernel-space natively. The latter
23950+ is always enabled but sometimes large and slow.
23951+ If you enable this option, install the library in aufs2-util
23952+ package, and set some environment variables for your readdir(3),
23953+ then the work will be handled in user-space which generally
23954+ shows better performance in most cases.
23955+ See detail in aufs.5.
1facf9fc 23956+
8b6a4947
AM
23957+config AUFS_DIRREN
23958+ bool "Workaround for rename(2)-ing a directory"
23959+ help
23960+ By default, aufs returns EXDEV error in renameing a dir who has
23961+ his child on the lower branch, since it is a bad idea to issue
23962+ rename(2) internally for every lower branch. But user may not
23963+ accept this behaviour. So here is a workaround to allow such
23964+ rename(2) and store some extra infromation on the writable
23965+ branch. Obviously this costs high (and I don't like it).
23966+ To use this feature, you need to enable this configuration AND
23967+ to specify the mount option `dirren.'
23968+ See details in aufs.5 and the design documents.
23969+
4a4d8108
AM
23970+config AUFS_SHWH
23971+ bool "Show whiteouts"
23972+ help
23973+ If you want to make the whiteouts in aufs visible, then enable
23974+ this option and specify 'shwh' mount option. Although it may
23975+ sounds like philosophy or something, but in technically it
23976+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 23977+
4a4d8108
AM
23978+config AUFS_BR_RAMFS
23979+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
23980+ help
23981+ If you want to use ramfs as an aufs branch fs, then enable this
23982+ option. Generally tmpfs is recommended.
23983+ Aufs prohibited them to be a branch fs by default, because
23984+ initramfs becomes unusable after switch_root or something
23985+ generally. If you sets initramfs as an aufs branch and boot your
23986+ system by switch_root, you will meet a problem easily since the
23987+ files in initramfs may be inaccessible.
23988+ Unless you are going to use ramfs as an aufs branch fs without
23989+ switch_root or something, leave it N.
1facf9fc 23990+
4a4d8108
AM
23991+config AUFS_BR_FUSE
23992+ bool "Fuse fs as an aufs branch"
23993+ depends on FUSE_FS
23994+ select AUFS_POLL
23995+ help
23996+ If you want to use fuse-based userspace filesystem as an aufs
23997+ branch fs, then enable this option.
23998+ It implements the internal poll(2) operation which is
23999+ implemented by fuse only (curretnly).
1facf9fc 24000+
4a4d8108
AM
24001+config AUFS_POLL
24002+ bool
24003+ help
24004+ Automatic configuration for internal use.
1facf9fc 24005+
4a4d8108
AM
24006+config AUFS_BR_HFSPLUS
24007+ bool "Hfsplus as an aufs branch"
24008+ depends on HFSPLUS_FS
24009+ default y
24010+ help
24011+ If you want to use hfsplus fs as an aufs branch fs, then enable
24012+ this option. This option introduces a small overhead at
24013+ copying-up a file on hfsplus.
1facf9fc 24014+
4a4d8108
AM
24015+config AUFS_BDEV_LOOP
24016+ bool
24017+ depends on BLK_DEV_LOOP
24018+ default y
24019+ help
24020+ Automatic configuration for internal use.
24021+ Convert =[ym] into =y.
1308ab2a 24022+
4a4d8108
AM
24023+config AUFS_DEBUG
24024+ bool "Debug aufs"
24025+ help
24026+ Enable this to compile aufs internal debug code.
24027+ It will have a negative impact to the performance.
24028+
24029+config AUFS_MAGIC_SYSRQ
24030+ bool
24031+ depends on AUFS_DEBUG && MAGIC_SYSRQ
24032+ default y
24033+ help
24034+ Automatic configuration for internal use.
24035+ When aufs supports Magic SysRq, enabled automatically.
24036+endif
acd2b654 24037diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
eca34b5c 24038--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
016522bc 24039+++ linux/fs/aufs/lcnt.h 2020-01-27 10:57:18.175538316 +0100
acd2b654
AM
24040@@ -0,0 +1,186 @@
24041+/* SPDX-License-Identifier: GPL-2.0 */
24042+/*
016522bc 24043+ * Copyright (C) 2018-2020 Junjiro R. Okajima
acd2b654
AM
24044+ *
24045+ * This program, aufs is free software; you can redistribute it and/or modify
24046+ * it under the terms of the GNU General Public License as published by
24047+ * the Free Software Foundation; either version 2 of the License, or
24048+ * (at your option) any later version.
24049+ *
24050+ * This program is distributed in the hope that it will be useful,
24051+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24052+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24053+ * GNU General Public License for more details.
24054+ *
24055+ * You should have received a copy of the GNU General Public License
24056+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24057+ */
24058+
24059+/*
24060+ * simple long counter wrapper
24061+ */
24062+
24063+#ifndef __AUFS_LCNT_H__
24064+#define __AUFS_LCNT_H__
24065+
24066+#ifdef __KERNEL__
24067+
24068+#include "debug.h"
24069+
24070+#define AuLCntATOMIC 1
24071+#define AuLCntPCPUCNT 2
24072+/*
24073+ * why does percpu_refcount require extra synchronize_rcu()s in
24074+ * au_br_do_free()
24075+ */
24076+#define AuLCntPCPUREF 3
24077+
24078+/* #define AuLCntChosen AuLCntATOMIC */
24079+#define AuLCntChosen AuLCntPCPUCNT
24080+/* #define AuLCntChosen AuLCntPCPUREF */
24081+
24082+#if AuLCntChosen == AuLCntATOMIC
24083+#include <linux/atomic.h>
24084+
24085+typedef atomic_long_t au_lcnt_t;
24086+
24087+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24088+{
24089+ atomic_long_set(cnt, 0);
24090+ return 0;
24091+}
24092+
24093+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24094+{
24095+ /* empty */
24096+}
24097+
24098+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24099+ int do_sync __maybe_unused)
24100+{
24101+ /* empty */
24102+}
24103+
24104+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24105+{
24106+ atomic_long_inc(cnt);
24107+}
24108+
24109+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24110+{
24111+ atomic_long_dec(cnt);
24112+}
24113+
24114+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24115+{
24116+ return atomic_long_read(cnt);
24117+}
24118+#endif
24119+
24120+#if AuLCntChosen == AuLCntPCPUCNT
24121+#include <linux/percpu_counter.h>
24122+
24123+typedef struct percpu_counter au_lcnt_t;
24124+
24125+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24126+{
24127+ return percpu_counter_init(cnt, 0, GFP_NOFS);
24128+}
24129+
24130+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24131+{
24132+ /* empty */
24133+}
24134+
24135+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24136+{
24137+ percpu_counter_destroy(cnt);
24138+}
24139+
24140+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24141+{
24142+ percpu_counter_inc(cnt);
24143+}
24144+
24145+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24146+{
24147+ percpu_counter_dec(cnt);
24148+}
24149+
24150+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24151+{
24152+ s64 n;
24153+
24154+ n = percpu_counter_sum(cnt);
24155+ BUG_ON(n < 0);
24156+ if (LONG_MAX != LLONG_MAX
24157+ && n > LONG_MAX)
24158+ AuWarn1("%s\n", "wrap-around");
24159+
24160+ return n;
24161+}
24162+#endif
24163+
24164+#if AuLCntChosen == AuLCntPCPUREF
24165+#include <linux/percpu-refcount.h>
24166+
24167+typedef struct percpu_ref au_lcnt_t;
24168+
24169+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24170+{
24171+ if (!release)
24172+ release = percpu_ref_exit;
24173+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24174+}
24175+
24176+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24177+{
24178+ synchronize_rcu();
24179+}
24180+
24181+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24182+{
24183+ percpu_ref_kill(cnt);
24184+ if (do_sync)
24185+ au_lcnt_wait_for_fin(cnt);
24186+}
24187+
24188+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24189+{
24190+ percpu_ref_get(cnt);
24191+}
24192+
24193+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24194+{
24195+ percpu_ref_put(cnt);
24196+}
24197+
24198+/*
24199+ * avoid calling this func as possible.
24200+ */
24201+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24202+{
24203+ long l;
24204+
24205+ percpu_ref_switch_to_atomic_sync(cnt);
24206+ l = atomic_long_read(&cnt->count);
24207+ if (do_rev)
24208+ percpu_ref_switch_to_percpu(cnt);
24209+
24210+ /* percpu_ref is initialized by 1 instead of 0 */
24211+ return l - 1;
24212+}
24213+#endif
24214+
24215+#ifdef CONFIG_AUFS_DEBUG
24216+#define AuLCntZero(val) do { \
24217+ long l = val; \
24218+ if (l) \
24219+ AuDbg("%s = %ld\n", #val, l); \
24220+} while (0)
24221+#else
24222+#define AuLCntZero(val) do {} while (0)
24223+#endif
24224+
24225+#endif /* __KERNEL__ */
24226+#endif /* __AUFS_LCNT_H__ */
7f207e10 24227diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
eca34b5c 24228--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
016522bc 24229+++ linux/fs/aufs/loop.c 2020-01-27 10:57:18.175538316 +0100
062440b3 24230@@ -0,0 +1,148 @@
cd7a4cd9 24231+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24232+/*
016522bc 24233+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24234+ *
24235+ * This program, aufs is free software; you can redistribute it and/or modify
24236+ * it under the terms of the GNU General Public License as published by
24237+ * the Free Software Foundation; either version 2 of the License, or
24238+ * (at your option) any later version.
dece6358
AM
24239+ *
24240+ * This program is distributed in the hope that it will be useful,
24241+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24242+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24243+ * GNU General Public License for more details.
24244+ *
24245+ * You should have received a copy of the GNU General Public License
523b37e3 24246+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24247+ */
24248+
24249+/*
24250+ * support for loopback block device as a branch
24251+ */
24252+
1facf9fc 24253+#include "aufs.h"
24254+
392086de
AM
24255+/* added into drivers/block/loop.c */
24256+static struct file *(*backing_file_func)(struct super_block *sb);
24257+
1facf9fc 24258+/*
24259+ * test if two lower dentries have overlapping branches.
24260+ */
b752ccd1 24261+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 24262+{
b752ccd1 24263+ struct super_block *h_sb;
392086de
AM
24264+ struct file *backing_file;
24265+
24266+ if (unlikely(!backing_file_func)) {
24267+ /* don't load "loop" module here */
24268+ backing_file_func = symbol_get(loop_backing_file);
24269+ if (unlikely(!backing_file_func))
24270+ /* "loop" module is not loaded */
24271+ return 0;
24272+ }
1facf9fc 24273+
b752ccd1 24274+ h_sb = h_adding->d_sb;
392086de
AM
24275+ backing_file = backing_file_func(h_sb);
24276+ if (!backing_file)
1facf9fc 24277+ return 0;
24278+
2000de60 24279+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
24280+ /*
24281+ * h_adding can be local NFS.
24282+ * in this case aufs cannot detect the loop.
24283+ */
24284+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 24285+ return 1;
b752ccd1 24286+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 24287+}
24288+
24289+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24290+int au_test_loopback_kthread(void)
24291+{
b752ccd1
AM
24292+ int ret;
24293+ struct task_struct *tsk = current;
a2a7ad62 24294+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
24295+
24296+ ret = 0;
24297+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
24298+ get_task_comm(comm, tsk);
24299+ c = comm[4];
b752ccd1 24300+ ret = ('0' <= c && c <= '9'
a2a7ad62 24301+ && !strncmp(comm, "loop", 4));
b752ccd1 24302+ }
1facf9fc 24303+
b752ccd1 24304+ return ret;
1facf9fc 24305+}
87a755f4
AM
24306+
24307+/* ---------------------------------------------------------------------- */
24308+
24309+#define au_warn_loopback_step 16
24310+static int au_warn_loopback_nelem = au_warn_loopback_step;
24311+static unsigned long *au_warn_loopback_array;
24312+
24313+void au_warn_loopback(struct super_block *h_sb)
24314+{
24315+ int i, new_nelem;
24316+ unsigned long *a, magic;
24317+ static DEFINE_SPINLOCK(spin);
24318+
24319+ magic = h_sb->s_magic;
24320+ spin_lock(&spin);
24321+ a = au_warn_loopback_array;
24322+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24323+ if (a[i] == magic) {
24324+ spin_unlock(&spin);
24325+ return;
24326+ }
24327+
24328+ /* h_sb is new to us, print it */
24329+ if (i < au_warn_loopback_nelem) {
24330+ a[i] = magic;
24331+ goto pr;
24332+ }
24333+
24334+ /* expand the array */
24335+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24336+ a = au_kzrealloc(au_warn_loopback_array,
24337+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24338+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24339+ /*may_shrink*/0);
87a755f4
AM
24340+ if (a) {
24341+ au_warn_loopback_nelem = new_nelem;
24342+ au_warn_loopback_array = a;
24343+ a[i] = magic;
24344+ goto pr;
24345+ }
24346+
24347+ spin_unlock(&spin);
24348+ AuWarn1("realloc failed, ignored\n");
24349+ return;
24350+
24351+pr:
24352+ spin_unlock(&spin);
0c3ec466
AM
24353+ pr_warn("you may want to try another patch for loopback file "
24354+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24355+}
24356+
24357+int au_loopback_init(void)
24358+{
24359+ int err;
24360+ struct super_block *sb __maybe_unused;
24361+
fbc438ed 24362+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
87a755f4
AM
24363+
24364+ err = 0;
24365+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24366+ sizeof(unsigned long), GFP_NOFS);
24367+ if (unlikely(!au_warn_loopback_array))
24368+ err = -ENOMEM;
24369+
24370+ return err;
24371+}
24372+
24373+void au_loopback_fin(void)
24374+{
79b8bda9
AM
24375+ if (backing_file_func)
24376+ symbol_put(loop_backing_file);
9f237c51 24377+ au_kfree_try_rcu(au_warn_loopback_array);
87a755f4 24378+}
7f207e10 24379diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
eca34b5c 24380--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
016522bc 24381+++ linux/fs/aufs/loop.h 2020-01-27 10:57:18.175538316 +0100
eca801bf 24382@@ -0,0 +1,55 @@
062440b3 24383+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24384+/*
016522bc 24385+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24386+ *
24387+ * This program, aufs is free software; you can redistribute it and/or modify
24388+ * it under the terms of the GNU General Public License as published by
24389+ * the Free Software Foundation; either version 2 of the License, or
24390+ * (at your option) any later version.
dece6358
AM
24391+ *
24392+ * This program is distributed in the hope that it will be useful,
24393+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24394+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24395+ * GNU General Public License for more details.
24396+ *
24397+ * You should have received a copy of the GNU General Public License
523b37e3 24398+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24399+ */
24400+
24401+/*
24402+ * support for loopback mount as a branch
24403+ */
24404+
24405+#ifndef __AUFS_LOOP_H__
24406+#define __AUFS_LOOP_H__
24407+
24408+#ifdef __KERNEL__
24409+
dece6358
AM
24410+struct dentry;
24411+struct super_block;
1facf9fc 24412+
24413+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24414+/* drivers/block/loop.c */
24415+struct file *loop_backing_file(struct super_block *sb);
24416+
1facf9fc 24417+/* loop.c */
b752ccd1 24418+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24419+int au_test_loopback_kthread(void);
87a755f4
AM
24420+void au_warn_loopback(struct super_block *h_sb);
24421+
24422+int au_loopback_init(void);
24423+void au_loopback_fin(void);
1facf9fc 24424+#else
83b672a5 24425+AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
eca801bf 24426+
4a4d8108 24427+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24428+ struct dentry *h_adding)
4a4d8108 24429+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24430+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24431+
24432+AuStubInt0(au_loopback_init, void)
24433+AuStubVoid(au_loopback_fin, void)
1facf9fc 24434+#endif /* BLK_DEV_LOOP */
24435+
24436+#endif /* __KERNEL__ */
24437+#endif /* __AUFS_LOOP_H__ */
7f207e10 24438diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
eca34b5c
AM
24439--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
24440+++ linux/fs/aufs/magic.mk 2019-07-11 15:42:14.468904634 +0200
2121bcd9
AM
24441@@ -0,0 +1,31 @@
24442+# SPDX-License-Identifier: GPL-2.0
1facf9fc 24443+
24444+# defined in ${srctree}/fs/fuse/inode.c
24445+# tristate
24446+ifdef CONFIG_FUSE_FS
24447+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24448+endif
24449+
1facf9fc 24450+# defined in ${srctree}/fs/xfs/xfs_sb.h
24451+# tristate
24452+ifdef CONFIG_XFS_FS
24453+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24454+endif
24455+
24456+# defined in ${srctree}/fs/configfs/mount.c
24457+# tristate
24458+ifdef CONFIG_CONFIGFS_FS
24459+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24460+endif
24461+
1facf9fc 24462+# defined in ${srctree}/fs/ubifs/ubifs.h
24463+# tristate
24464+ifdef CONFIG_UBIFS_FS
24465+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24466+endif
4a4d8108
AM
24467+
24468+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24469+# tristate
24470+ifdef CONFIG_HFSPLUS_FS
24471+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24472+endif
7f207e10 24473diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
eca34b5c
AM
24474--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
24475+++ linux/fs/aufs/Makefile 2019-07-11 15:42:14.462237786 +0200
2121bcd9
AM
24476@@ -0,0 +1,46 @@
24477+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
24478+
24479+include ${src}/magic.mk
24480+ifeq (${CONFIG_AUFS_FS},m)
24481+include ${src}/conf.mk
24482+endif
24483+-include ${src}/priv_def.mk
24484+
24485+# cf. include/linux/kernel.h
24486+# enable pr_debug
24487+ccflags-y += -DDEBUG
f6c5ef8b
AM
24488+# sparse requires the full pathname
24489+ifdef M
523b37e3 24490+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24491+else
523b37e3 24492+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24493+endif
4a4d8108
AM
24494+
24495+obj-$(CONFIG_AUFS_FS) += aufs.o
24496+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24497+ wkq.o vfsub.o dcsub.o \
e49829fe 24498+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24499+ dinfo.o dentry.o \
24500+ dynop.o \
24501+ finfo.o file.o f_op.o \
24502+ dir.o vdir.o \
24503+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24504+ mvdown.o ioctl.o
4a4d8108
AM
24505+
24506+# all are boolean
e49829fe 24507+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24508+aufs-$(CONFIG_SYSFS) += sysfs.o
24509+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24510+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24511+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24512+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24513+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24514+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24515+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24516+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24517+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24518+aufs-$(CONFIG_AUFS_POLL) += poll.o
24519+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24520+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24521+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24522+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10 24523diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
eca34b5c 24524--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
016522bc 24525+++ linux/fs/aufs/module.c 2020-01-27 10:57:18.175538316 +0100
062440b3 24526@@ -0,0 +1,273 @@
cd7a4cd9 24527+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24528+/*
016522bc 24529+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24530+ *
24531+ * This program, aufs is free software; you can redistribute it and/or modify
24532+ * it under the terms of the GNU General Public License as published by
24533+ * the Free Software Foundation; either version 2 of the License, or
24534+ * (at your option) any later version.
dece6358
AM
24535+ *
24536+ * This program is distributed in the hope that it will be useful,
24537+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24538+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24539+ * GNU General Public License for more details.
24540+ *
24541+ * You should have received a copy of the GNU General Public License
523b37e3 24542+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24543+ */
24544+
24545+/*
24546+ * module global variables and operations
24547+ */
24548+
24549+#include <linux/module.h>
24550+#include <linux/seq_file.h>
24551+#include "aufs.h"
24552+
e2f27e51
AM
24553+/* shrinkable realloc */
24554+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24555+{
e2f27e51
AM
24556+ size_t sz;
24557+ int diff;
1facf9fc 24558+
e2f27e51
AM
24559+ sz = 0;
24560+ diff = -1;
24561+ if (p) {
24562+#if 0 /* unused */
24563+ if (!new_sz) {
9f237c51 24564+ au_kfree_rcu(p);
e2f27e51
AM
24565+ p = NULL;
24566+ goto out;
24567+ }
24568+#else
24569+ AuDebugOn(!new_sz);
24570+#endif
24571+ sz = ksize(p);
24572+ diff = au_kmidx_sub(sz, new_sz);
24573+ }
24574+ if (sz && !diff)
24575+ goto out;
24576+
24577+ if (sz < new_sz)
24578+ /* expand or SLOB */
24579+ p = krealloc(p, new_sz, gfp);
24580+ else if (new_sz < sz && may_shrink) {
24581+ /* shrink */
24582+ void *q;
24583+
24584+ q = kmalloc(new_sz, gfp);
24585+ if (q) {
24586+ if (p) {
24587+ memcpy(q, p, new_sz);
9f237c51 24588+ au_kfree_try_rcu(p);
e2f27e51
AM
24589+ }
24590+ p = q;
24591+ } else
24592+ p = NULL;
24593+ }
24594+
24595+out:
24596+ return p;
24597+}
24598+
24599+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24600+ int may_shrink)
24601+{
24602+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24603+ if (p && new_sz > nused)
1facf9fc 24604+ memset(p + nused, 0, new_sz - nused);
24605+ return p;
24606+}
24607+
24608+/* ---------------------------------------------------------------------- */
1facf9fc 24609+/*
24610+ * aufs caches
24611+ */
1c60b727 24612+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24613+
24614+static void au_cache_fin(void)
24615+{
24616+ int i;
24617+
24618+ /*
24619+ * Make sure all delayed rcu free inodes are flushed before we
24620+ * destroy cache.
24621+ */
24622+ rcu_barrier();
24623+
24624+ /* excluding AuCache_HNOTIFY */
24625+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24626+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24627+ kmem_cache_destroy(au_cache[i]);
24628+ au_cache[i] = NULL;
5afbbe0d
AM
24629+ }
24630+}
24631+
1facf9fc 24632+static int __init au_cache_init(void)
24633+{
1c60b727
AM
24634+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24635+ if (au_cache[AuCache_DINFO])
027c5e7a 24636+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24637+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24638+ au_icntnr_init_once);
1c60b727
AM
24639+ if (au_cache[AuCache_ICNTNR])
24640+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24641+ au_fi_init_once);
1c60b727
AM
24642+ if (au_cache[AuCache_FINFO])
24643+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24644+ if (au_cache[AuCache_VDIR])
24645+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24646+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24647+ return 0;
24648+
5afbbe0d 24649+ au_cache_fin();
1facf9fc 24650+ return -ENOMEM;
24651+}
24652+
1facf9fc 24653+/* ---------------------------------------------------------------------- */
24654+
24655+int au_dir_roflags;
24656+
e49829fe 24657+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24658+/*
24659+ * iterate_supers_type() doesn't protect us from
24660+ * remounting (branch management)
24661+ */
8b6a4947 24662+struct hlist_bl_head au_sbilist;
e49829fe
JR
24663+#endif
24664+
1facf9fc 24665+/*
24666+ * functions for module interface.
24667+ */
24668+MODULE_LICENSE("GPL");
24669+/* MODULE_LICENSE("GPL v2"); */
dece6358 24670+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24671+MODULE_DESCRIPTION(AUFS_NAME
24672+ " -- Advanced multi layered unification filesystem");
24673+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24674+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24675+
1facf9fc 24676+/* this module parameter has no meaning when SYSFS is disabled */
24677+int sysaufs_brs = 1;
24678+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
cd7a4cd9 24679+module_param_named(brs, sysaufs_brs, int, 0444);
1facf9fc 24680+
076b876e 24681+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24682+bool au_userns;
076b876e 24683+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
cd7a4cd9 24684+module_param_named(allow_userns, au_userns, bool, 0444);
076b876e 24685+
1facf9fc 24686+/* ---------------------------------------------------------------------- */
24687+
24688+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24689+
24690+int au_seq_path(struct seq_file *seq, struct path *path)
24691+{
79b8bda9
AM
24692+ int err;
24693+
24694+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24695+ if (err >= 0)
79b8bda9 24696+ err = 0;
1c60b727 24697+ else
79b8bda9
AM
24698+ err = -ENOMEM;
24699+
24700+ return err;
1facf9fc 24701+}
24702+
24703+/* ---------------------------------------------------------------------- */
24704+
24705+static int __init aufs_init(void)
24706+{
24707+ int err, i;
24708+ char *p;
24709+
24710+ p = au_esc_chars;
24711+ for (i = 1; i <= ' '; i++)
24712+ *p++ = i;
24713+ *p++ = '\\';
24714+ *p++ = '\x7f';
24715+ *p = 0;
24716+
24717+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24718+
b95c5147
AM
24719+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24720+ for (i = 0; i < AuIop_Last; i++)
24721+ aufs_iop_nogetattr[i].getattr = NULL;
24722+
1c60b727 24723+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24724+
e49829fe 24725+ au_sbilist_init();
1facf9fc 24726+ sysaufs_brs_init();
24727+ au_debug_init();
4a4d8108 24728+ au_dy_init();
1facf9fc 24729+ err = sysaufs_init();
24730+ if (unlikely(err))
24731+ goto out;
062440b3 24732+ err = dbgaufs_init();
4f0767ce 24733+ if (unlikely(err))
953406b4 24734+ goto out_sysaufs;
062440b3
AM
24735+ err = au_procfs_init();
24736+ if (unlikely(err))
24737+ goto out_dbgaufs;
e49829fe
JR
24738+ err = au_wkq_init();
24739+ if (unlikely(err))
24740+ goto out_procfs;
87a755f4 24741+ err = au_loopback_init();
1facf9fc 24742+ if (unlikely(err))
24743+ goto out_wkq;
87a755f4
AM
24744+ err = au_hnotify_init();
24745+ if (unlikely(err))
24746+ goto out_loopback;
1facf9fc 24747+ err = au_sysrq_init();
24748+ if (unlikely(err))
24749+ goto out_hin;
24750+ err = au_cache_init();
24751+ if (unlikely(err))
24752+ goto out_sysrq;
076b876e
AM
24753+
24754+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24755+ err = register_filesystem(&aufs_fs_type);
24756+ if (unlikely(err))
24757+ goto out_cache;
076b876e 24758+
4a4d8108
AM
24759+ /* since we define pr_fmt, call printk directly */
24760+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24761+ goto out; /* success */
24762+
4f0767ce 24763+out_cache:
1facf9fc 24764+ au_cache_fin();
4f0767ce 24765+out_sysrq:
1facf9fc 24766+ au_sysrq_fin();
4f0767ce 24767+out_hin:
4a4d8108 24768+ au_hnotify_fin();
87a755f4
AM
24769+out_loopback:
24770+ au_loopback_fin();
4f0767ce 24771+out_wkq:
1facf9fc 24772+ au_wkq_fin();
e49829fe
JR
24773+out_procfs:
24774+ au_procfs_fin();
062440b3
AM
24775+out_dbgaufs:
24776+ dbgaufs_fin();
4f0767ce 24777+out_sysaufs:
1facf9fc 24778+ sysaufs_fin();
4a4d8108 24779+ au_dy_fin();
4f0767ce 24780+out:
1facf9fc 24781+ return err;
24782+}
24783+
24784+static void __exit aufs_exit(void)
24785+{
24786+ unregister_filesystem(&aufs_fs_type);
24787+ au_cache_fin();
24788+ au_sysrq_fin();
4a4d8108 24789+ au_hnotify_fin();
87a755f4 24790+ au_loopback_fin();
1facf9fc 24791+ au_wkq_fin();
e49829fe 24792+ au_procfs_fin();
062440b3 24793+ dbgaufs_fin();
1facf9fc 24794+ sysaufs_fin();
4a4d8108 24795+ au_dy_fin();
1facf9fc 24796+}
24797+
24798+module_init(aufs_init);
24799+module_exit(aufs_exit);
7f207e10 24800diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
eca34b5c 24801--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
016522bc 24802+++ linux/fs/aufs/module.h 2020-01-27 10:57:18.175538316 +0100
9f237c51 24803@@ -0,0 +1,166 @@
062440b3 24804+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24805+/*
016522bc 24806+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24807+ *
24808+ * This program, aufs is free software; you can redistribute it and/or modify
24809+ * it under the terms of the GNU General Public License as published by
24810+ * the Free Software Foundation; either version 2 of the License, or
24811+ * (at your option) any later version.
dece6358
AM
24812+ *
24813+ * This program is distributed in the hope that it will be useful,
24814+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24815+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24816+ * GNU General Public License for more details.
24817+ *
24818+ * You should have received a copy of the GNU General Public License
523b37e3 24819+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24820+ */
24821+
24822+/*
24823+ * module initialization and module-global
24824+ */
24825+
24826+#ifndef __AUFS_MODULE_H__
24827+#define __AUFS_MODULE_H__
24828+
24829+#ifdef __KERNEL__
24830+
24831+#include <linux/slab.h>
9f237c51
AM
24832+#include "debug.h"
24833+#include "dentry.h"
24834+#include "dir.h"
24835+#include "file.h"
24836+#include "inode.h"
1facf9fc 24837+
dece6358
AM
24838+struct path;
24839+struct seq_file;
24840+
1facf9fc 24841+/* module parameters */
1facf9fc 24842+extern int sysaufs_brs;
8cdd5066 24843+extern bool au_userns;
1facf9fc 24844+
24845+/* ---------------------------------------------------------------------- */
24846+
24847+extern int au_dir_roflags;
24848+
e2f27e51
AM
24849+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24850+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24851+ int may_shrink);
24852+
9f237c51
AM
24853+/*
24854+ * Comparing the size of the object with sizeof(struct rcu_head)
24855+ * case 1: object is always larger
24856+ * --> au_kfree_rcu() or au_kfree_do_rcu()
24857+ * case 2: object is always smaller
24858+ * --> au_kfree_small()
24859+ * case 3: object can be any size
24860+ * --> au_kfree_try_rcu()
24861+ */
24862+
24863+static inline void au_kfree_do_rcu(const void *p)
24864+{
24865+ struct {
24866+ struct rcu_head rcu;
24867+ } *a = (void *)p;
24868+
24869+ kfree_rcu(a, rcu);
24870+}
24871+
24872+#define au_kfree_rcu(_p) do { \
24873+ typeof(_p) p = (_p); \
24874+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
24875+ if (p) \
24876+ au_kfree_do_rcu(p); \
24877+ } while (0)
24878+
24879+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
24880+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
24881+
24882+static inline void au_kfree_try_rcu(const void *p)
24883+{
24884+ if (!p)
24885+ return;
24886+ if (au_kfree_sz_test(p))
24887+ au_kfree_do_rcu(p);
24888+ else
24889+ kfree(p);
24890+}
24891+
24892+static inline void au_kfree_small(const void *p)
24893+{
24894+ if (!p)
24895+ return;
24896+ AuDebugOn(au_kfree_sz_test(p));
24897+ kfree(p);
24898+}
24899+
e2f27e51
AM
24900+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24901+{
24902+#ifndef CONFIG_SLOB
24903+ return kmalloc_index(sz) - kmalloc_index(new_sz);
24904+#else
24905+ return -1; /* SLOB is untested */
24906+#endif
24907+}
24908+
1facf9fc 24909+int au_seq_path(struct seq_file *seq, struct path *path);
24910+
e49829fe
JR
24911+#ifdef CONFIG_PROC_FS
24912+/* procfs.c */
24913+int __init au_procfs_init(void);
24914+void au_procfs_fin(void);
24915+#else
24916+AuStubInt0(au_procfs_init, void);
24917+AuStubVoid(au_procfs_fin, void);
24918+#endif
24919+
4f0767ce
JR
24920+/* ---------------------------------------------------------------------- */
24921+
1c60b727 24922+/* kmem cache */
1facf9fc 24923+enum {
24924+ AuCache_DINFO,
24925+ AuCache_ICNTNR,
24926+ AuCache_FINFO,
24927+ AuCache_VDIR,
24928+ AuCache_DEHSTR,
7eafdf33 24929+ AuCache_HNOTIFY, /* must be last */
1facf9fc 24930+ AuCache_Last
24931+};
24932+
1c60b727 24933+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 24934+
4a4d8108
AM
24935+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24936+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
24937+#define AuCacheCtor(type, ctor) \
24938+ kmem_cache_create(#type, sizeof(struct type), \
24939+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 24940+
9f237c51
AM
24941+#define AuCacheFuncs(name, index) \
24942+ static inline struct au_##name *au_cache_alloc_##name(void) \
24943+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24944+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24945+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
24946+ \
24947+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24948+ { void *p = rcu; \
24949+ p -= offsetof(struct au_##name, rcu); \
24950+ kmem_cache_free(au_cache[AuCache_##index], p); } \
24951+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24952+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24953+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
24954+ \
24955+ static inline void au_cache_free_##name(struct au_##name *p) \
24956+ { /* au_cache_free_##name##_norcu(p); */ \
24957+ au_cache_free_##name##_rcu(p); }
1facf9fc 24958+
24959+AuCacheFuncs(dinfo, DINFO);
24960+AuCacheFuncs(icntnr, ICNTNR);
24961+AuCacheFuncs(finfo, FINFO);
24962+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
24963+AuCacheFuncs(vdir_dehstr, DEHSTR);
24964+#ifdef CONFIG_AUFS_HNOTIFY
24965+AuCacheFuncs(hnotify, HNOTIFY);
24966+#endif
1facf9fc 24967+
4a4d8108
AM
24968+#endif /* __KERNEL__ */
24969+#endif /* __AUFS_MODULE_H__ */
c2b27bf2 24970diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
eca34b5c 24971--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
016522bc 24972+++ linux/fs/aufs/mvdown.c 2020-01-27 10:57:18.175538316 +0100
ba1aed25 24973@@ -0,0 +1,706 @@
cd7a4cd9 24974+// SPDX-License-Identifier: GPL-2.0
c2b27bf2 24975+/*
016522bc 24976+ * Copyright (C) 2011-2020 Junjiro R. Okajima
c2b27bf2
AM
24977+ *
24978+ * This program, aufs is free software; you can redistribute it and/or modify
24979+ * it under the terms of the GNU General Public License as published by
24980+ * the Free Software Foundation; either version 2 of the License, or
24981+ * (at your option) any later version.
24982+ *
24983+ * This program is distributed in the hope that it will be useful,
24984+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24985+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24986+ * GNU General Public License for more details.
24987+ *
24988+ * You should have received a copy of the GNU General Public License
523b37e3
AM
24989+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24990+ */
24991+
24992+/*
24993+ * move-down, opposite of copy-up
c2b27bf2
AM
24994+ */
24995+
24996+#include "aufs.h"
24997+
c2b27bf2
AM
24998+struct au_mvd_args {
24999+ struct {
c2b27bf2
AM
25000+ struct super_block *h_sb;
25001+ struct dentry *h_parent;
25002+ struct au_hinode *hdir;
392086de 25003+ struct inode *h_dir, *h_inode;
c1595e42 25004+ struct au_pin pin;
c2b27bf2
AM
25005+ } info[AUFS_MVDOWN_NARRAY];
25006+
25007+ struct aufs_mvdown mvdown;
25008+ struct dentry *dentry, *parent;
25009+ struct inode *inode, *dir;
25010+ struct super_block *sb;
25011+ aufs_bindex_t bopq, bwh, bfound;
25012+ unsigned char rename_lock;
c2b27bf2
AM
25013+};
25014+
392086de 25015+#define mvd_errno mvdown.au_errno
076b876e
AM
25016+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25017+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25018+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25019+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 25020+
392086de
AM
25021+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
25022+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
25023+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
25024+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
25025+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 25026+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
25027+
25028+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
25029+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
25030+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
25031+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
25032+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 25033+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
25034+
25035+#define AU_MVD_PR(flag, ...) do { \
25036+ if (flag) \
25037+ pr_err(__VA_ARGS__); \
25038+ } while (0)
25039+
076b876e
AM
25040+static int find_lower_writable(struct au_mvd_args *a)
25041+{
25042+ struct super_block *sb;
5afbbe0d 25043+ aufs_bindex_t bindex, bbot;
076b876e
AM
25044+ struct au_branch *br;
25045+
25046+ sb = a->sb;
25047+ bindex = a->mvd_bsrc;
5afbbe0d 25048+ bbot = au_sbbot(sb);
076b876e 25049+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 25050+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25051+ br = au_sbr(sb, bindex);
25052+ if (au_br_fhsm(br->br_perm)
8b6a4947 25053+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
25054+ return bindex;
25055+ }
25056+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 25057+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25058+ br = au_sbr(sb, bindex);
25059+ if (!au_br_rdonly(br))
25060+ return bindex;
25061+ }
25062+ else
5afbbe0d 25063+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 25064+ br = au_sbr(sb, bindex);
8b6a4947 25065+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
25066+ if (au_br_rdonly(br))
25067+ a->mvdown.flags
25068+ |= AUFS_MVDOWN_ROLOWER_R;
25069+ return bindex;
25070+ }
25071+ }
25072+
25073+ return -1;
25074+}
25075+
c2b27bf2 25076+/* make the parent dir on bdst */
392086de 25077+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25078+{
25079+ int err;
25080+
25081+ err = 0;
25082+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25083+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25084+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25085+ a->mvd_h_dst_parent = NULL;
5afbbe0d 25086+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
25087+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25088+ if (!a->mvd_h_dst_parent) {
25089+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25090+ if (unlikely(err)) {
392086de 25091+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
25092+ goto out;
25093+ }
25094+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25095+ }
25096+
25097+out:
25098+ AuTraceErr(err);
25099+ return err;
25100+}
25101+
25102+/* lock them all */
392086de 25103+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25104+{
25105+ int err;
25106+ struct dentry *h_trap;
25107+
25108+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25109+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
25110+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25111+ au_opt_udba(a->sb),
25112+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25113+ AuTraceErr(err);
25114+ if (unlikely(err)) {
25115+ AU_MVD_PR(dmsg, "pin_dst failed\n");
25116+ goto out;
25117+ }
25118+
c2b27bf2
AM
25119+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25120+ a->rename_lock = 0;
c1595e42
JR
25121+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25122+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25123+ au_opt_udba(a->sb),
25124+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25125+ err = au_do_pin(&a->mvd_pin_src);
25126+ AuTraceErr(err);
5527c038 25127+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25128+ if (unlikely(err)) {
25129+ AU_MVD_PR(dmsg, "pin_src failed\n");
25130+ goto out_dst;
25131+ }
25132+ goto out; /* success */
c2b27bf2
AM
25133+ }
25134+
c2b27bf2 25135+ a->rename_lock = 1;
c1595e42
JR
25136+ au_pin_hdir_unlock(&a->mvd_pin_dst);
25137+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25138+ au_opt_udba(a->sb),
25139+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25140+ AuTraceErr(err);
5527c038 25141+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25142+ if (unlikely(err)) {
25143+ AU_MVD_PR(dmsg, "pin_src failed\n");
25144+ au_pin_hdir_lock(&a->mvd_pin_dst);
25145+ goto out_dst;
25146+ }
25147+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
25148+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25149+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25150+ if (h_trap) {
25151+ err = (h_trap != a->mvd_h_src_parent);
25152+ if (err)
25153+ err = (h_trap != a->mvd_h_dst_parent);
25154+ }
25155+ BUG_ON(err); /* it should never happen */
c1595e42
JR
25156+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25157+ err = -EBUSY;
25158+ AuTraceErr(err);
25159+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25160+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25161+ au_pin_hdir_lock(&a->mvd_pin_src);
25162+ au_unpin(&a->mvd_pin_src);
25163+ au_pin_hdir_lock(&a->mvd_pin_dst);
25164+ goto out_dst;
25165+ }
25166+ goto out; /* success */
c2b27bf2 25167+
c1595e42
JR
25168+out_dst:
25169+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25170+out:
25171+ AuTraceErr(err);
25172+ return err;
25173+}
25174+
392086de 25175+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 25176+{
c1595e42
JR
25177+ if (!a->rename_lock)
25178+ au_unpin(&a->mvd_pin_src);
25179+ else {
c2b27bf2
AM
25180+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25181+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
25182+ au_pin_hdir_lock(&a->mvd_pin_src);
25183+ au_unpin(&a->mvd_pin_src);
25184+ au_pin_hdir_lock(&a->mvd_pin_dst);
25185+ }
25186+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25187+}
25188+
25189+/* copy-down the file */
392086de 25190+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25191+{
25192+ int err;
25193+ struct au_cp_generic cpg = {
25194+ .dentry = a->dentry,
25195+ .bdst = a->mvd_bdst,
25196+ .bsrc = a->mvd_bsrc,
25197+ .len = -1,
c1595e42 25198+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
25199+ .flags = AuCpup_DTIME | AuCpup_HOPEN
25200+ };
25201+
25202+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
25203+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25204+ au_fset_cpup(cpg.flags, OVERWRITE);
25205+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25206+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
25207+ err = au_sio_cpdown_simple(&cpg);
25208+ if (unlikely(err))
392086de 25209+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
25210+
25211+ AuTraceErr(err);
25212+ return err;
25213+}
25214+
25215+/*
25216+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
25217+ * were sleeping
25218+ */
392086de 25219+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25220+{
25221+ int err;
25222+ struct path h_path;
25223+ struct au_branch *br;
523b37e3 25224+ struct inode *delegated;
c2b27bf2
AM
25225+
25226+ br = au_sbr(a->sb, a->mvd_bdst);
25227+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25228+ err = PTR_ERR(h_path.dentry);
25229+ if (IS_ERR(h_path.dentry)) {
392086de 25230+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
25231+ goto out;
25232+ }
25233+
25234+ err = 0;
5527c038 25235+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 25236+ h_path.mnt = au_br_mnt(br);
523b37e3 25237+ delegated = NULL;
5527c038 25238+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
25239+ &delegated, /*force*/0);
25240+ if (unlikely(err == -EWOULDBLOCK)) {
25241+ pr_warn("cannot retry for NFSv4 delegation"
25242+ " for an internal unlink\n");
25243+ iput(delegated);
25244+ }
c2b27bf2 25245+ if (unlikely(err))
392086de 25246+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
25247+ }
25248+ dput(h_path.dentry);
25249+
25250+out:
25251+ AuTraceErr(err);
25252+ return err;
25253+}
25254+
25255+/*
25256+ * unlink the topmost h_dentry
c2b27bf2 25257+ */
392086de 25258+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25259+{
25260+ int err;
25261+ struct path h_path;
523b37e3 25262+ struct inode *delegated;
c2b27bf2
AM
25263+
25264+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25265+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
25266+ delegated = NULL;
25267+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25268+ if (unlikely(err == -EWOULDBLOCK)) {
25269+ pr_warn("cannot retry for NFSv4 delegation"
25270+ " for an internal unlink\n");
25271+ iput(delegated);
25272+ }
c2b27bf2 25273+ if (unlikely(err))
392086de 25274+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
25275+
25276+ AuTraceErr(err);
25277+ return err;
25278+}
25279+
076b876e
AM
25280+/* Since mvdown succeeded, we ignore an error of this function */
25281+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25282+{
25283+ int err;
25284+ struct au_branch *br;
25285+
25286+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25287+ br = au_sbr(a->sb, a->mvd_bsrc);
25288+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25289+ if (!err) {
25290+ br = au_sbr(a->sb, a->mvd_bdst);
25291+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25292+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25293+ }
25294+ if (!err)
25295+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25296+ else
25297+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25298+}
25299+
c2b27bf2
AM
25300+/*
25301+ * copy-down the file and unlink the bsrc file.
25302+ * - unlink the bdst whout if exist
25303+ * - copy-down the file (with whtmp name and rename)
25304+ * - unlink the bsrc file
25305+ */
392086de 25306+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25307+{
25308+ int err;
25309+
392086de 25310+ err = au_do_mkdir(dmsg, a);
c2b27bf2 25311+ if (!err)
392086de 25312+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
25313+ if (unlikely(err))
25314+ goto out;
25315+
25316+ /*
25317+ * do not revert the activities we made on bdst since they should be
25318+ * harmless in aufs.
25319+ */
25320+
392086de 25321+ err = au_do_cpdown(dmsg, a);
c2b27bf2 25322+ if (!err)
392086de
AM
25323+ err = au_do_unlink_wh(dmsg, a);
25324+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25325+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
25326+ if (unlikely(err))
25327+ goto out_unlock;
25328+
c1595e42
JR
25329+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25330+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
25331+ if (find_lower_writable(a) < 0)
25332+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25333+
25334+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25335+ au_do_stfs(dmsg, a);
25336+
c2b27bf2 25337+ /* maintain internal array */
392086de
AM
25338+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25339+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 25340+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 25341+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 25342+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
25343+ } else {
25344+ /* hide the lower */
25345+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 25346+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 25347+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 25348+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 25349+ }
5afbbe0d
AM
25350+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25351+ au_set_dbbot(a->dentry, a->mvd_bdst);
25352+ if (au_ibbot(a->inode) < a->mvd_bdst)
25353+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
25354+
25355+out_unlock:
392086de 25356+ au_do_unlock(dmsg, a);
c2b27bf2
AM
25357+out:
25358+ AuTraceErr(err);
25359+ return err;
25360+}
25361+
25362+/* ---------------------------------------------------------------------- */
25363+
c2b27bf2 25364+/* make sure the file is idle */
392086de 25365+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25366+{
25367+ int err, plinked;
c2b27bf2
AM
25368+
25369+ err = 0;
c2b27bf2 25370+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 25371+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 25372+ && au_dcount(a->dentry) == 1
c2b27bf2 25373+ && atomic_read(&a->inode->i_count) == 1
392086de 25374+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
25375+ && (!plinked || !au_plink_test(a->inode))
25376+ && a->inode->i_nlink == 1)
25377+ goto out;
25378+
25379+ err = -EBUSY;
392086de 25380+ AU_MVD_PR(dmsg,
c1595e42 25381+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 25382+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 25383+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 25384+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
25385+ plinked, plinked ? au_plink_test(a->inode) : 0);
25386+
25387+out:
25388+ AuTraceErr(err);
25389+ return err;
25390+}
25391+
25392+/* make sure the parent dir is fine */
392086de 25393+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25394+ struct au_mvd_args *a)
25395+{
25396+ int err;
25397+ aufs_bindex_t bindex;
25398+
25399+ err = 0;
25400+ if (unlikely(au_alive_dir(a->parent))) {
25401+ err = -ENOENT;
392086de 25402+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25403+ goto out;
25404+ }
25405+
25406+ a->bopq = au_dbdiropq(a->parent);
25407+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25408+ AuDbg("b%d\n", bindex);
25409+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25410+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25411+ err = -EINVAL;
392086de
AM
25412+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25413+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25414+ a->bopq, a->mvd_bdst);
25415+ }
25416+
25417+out:
25418+ AuTraceErr(err);
25419+ return err;
25420+}
25421+
392086de 25422+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25423+ struct au_mvd_args *a)
25424+{
25425+ int err;
25426+ struct au_dinfo *dinfo, *tmp;
25427+
25428+ /* lookup the next lower positive entry */
25429+ err = -ENOMEM;
25430+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25431+ if (unlikely(!tmp))
25432+ goto out;
25433+
25434+ a->bfound = -1;
25435+ a->bwh = -1;
25436+ dinfo = au_di(a->dentry);
25437+ au_di_cp(tmp, dinfo);
25438+ au_di_swap(tmp, dinfo);
25439+
25440+ /* returns the number of positive dentries */
5afbbe0d
AM
25441+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25442+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25443+ if (!err)
25444+ a->bwh = au_dbwh(a->dentry);
25445+ else if (err > 0)
5afbbe0d 25446+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25447+
25448+ au_di_swap(tmp, dinfo);
25449+ au_rw_write_unlock(&tmp->di_rwsem);
25450+ au_di_free(tmp);
25451+ if (unlikely(err < 0))
392086de 25452+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25453+
25454+ /*
25455+ * here, we have these cases.
25456+ * bfound == -1
25457+ * no positive dentry under bsrc. there are more sub-cases.
25458+ * bwh < 0
25459+ * there no whiteout, we can safely move-down.
25460+ * bwh <= bsrc
25461+ * impossible
25462+ * bsrc < bwh && bwh < bdst
25463+ * there is a whiteout on RO branch. cannot proceed.
25464+ * bwh == bdst
25465+ * there is a whiteout on the RW target branch. it should
25466+ * be removed.
25467+ * bdst < bwh
25468+ * there is a whiteout somewhere unrelated branch.
25469+ * -1 < bfound && bfound <= bsrc
25470+ * impossible.
25471+ * bfound < bdst
25472+ * found, but it is on RO branch between bsrc and bdst. cannot
25473+ * proceed.
25474+ * bfound == bdst
25475+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25476+ * error.
25477+ * bdst < bfound
25478+ * found, after we create the file on bdst, it will be hidden.
25479+ */
25480+
25481+ AuDebugOn(a->bfound == -1
25482+ && a->bwh != -1
25483+ && a->bwh <= a->mvd_bsrc);
25484+ AuDebugOn(-1 < a->bfound
25485+ && a->bfound <= a->mvd_bsrc);
25486+
25487+ err = -EINVAL;
25488+ if (a->bfound == -1
25489+ && a->mvd_bsrc < a->bwh
25490+ && a->bwh != -1
25491+ && a->bwh < a->mvd_bdst) {
392086de
AM
25492+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25493+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25494+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25495+ goto out;
25496+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25497+ a->mvd_errno = EAU_MVDOWN_UPPER;
25498+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25499+ a->mvd_bdst, a->bfound);
25500+ goto out;
25501+ }
25502+
25503+ err = 0; /* success */
25504+
25505+out:
25506+ AuTraceErr(err);
25507+ return err;
25508+}
25509+
392086de 25510+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25511+{
25512+ int err;
25513+
392086de
AM
25514+ err = 0;
25515+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25516+ && a->bfound == a->mvd_bdst)
25517+ err = -EEXIST;
c2b27bf2
AM
25518+ AuTraceErr(err);
25519+ return err;
25520+}
25521+
392086de 25522+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25523+{
25524+ int err;
25525+ struct au_branch *br;
25526+
25527+ err = -EISDIR;
25528+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25529+ goto out;
25530+
25531+ err = -EINVAL;
392086de 25532+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25533+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25534+ else {
25535+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25536+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25537+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25538+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25539+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25540+ || (a->mvd_bsrc < au_ibtop(a->inode)
25541+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25542+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25543+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25544+ AU_MVD_PR(dmsg, "no upper\n");
25545+ goto out;
25546+ }
25547+ }
5afbbe0d 25548+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25549+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25550+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25551+ goto out;
25552+ }
392086de 25553+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25554+ br = au_sbr(a->sb, a->mvd_bsrc);
25555+ err = au_br_rdonly(br);
392086de
AM
25556+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25557+ if (unlikely(err))
25558+ goto out;
25559+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25560+ || IS_APPEND(a->mvd_h_src_inode))) {
25561+ if (err)
25562+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25563+ /* go on */
25564+ } else
c2b27bf2
AM
25565+ goto out;
25566+
25567+ err = -EINVAL;
392086de
AM
25568+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25569+ a->mvd_bdst = find_lower_writable(a);
25570+ if (unlikely(a->mvd_bdst < 0)) {
25571+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25572+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25573+ goto out;
25574+ }
25575+ } else {
25576+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25577+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25578+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25579+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25580+ AU_MVD_PR(dmsg, "no lower brid\n");
25581+ goto out;
25582+ }
c2b27bf2
AM
25583+ }
25584+
392086de 25585+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25586+ if (!err)
392086de 25587+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25588+ if (!err)
392086de 25589+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25590+ if (!err)
392086de 25591+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25592+ if (!err)
25593+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25594+
25595+out:
25596+ AuTraceErr(err);
25597+ return err;
25598+}
25599+
25600+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25601+{
392086de
AM
25602+ int err, e;
25603+ unsigned char dmsg;
25604+ struct au_mvd_args *args;
79b8bda9 25605+ struct inode *inode;
c2b27bf2 25606+
79b8bda9 25607+ inode = d_inode(dentry);
c2b27bf2
AM
25608+ err = -EPERM;
25609+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25610+ goto out;
25611+
392086de
AM
25612+ err = -ENOMEM;
25613+ args = kmalloc(sizeof(*args), GFP_NOFS);
25614+ if (unlikely(!args))
25615+ goto out;
25616+
25617+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25618+ if (!err)
ba1aed25
AM
25619+ /* VERIFY_WRITE */
25620+ err = !access_ok(uarg, sizeof(*uarg));
c2b27bf2
AM
25621+ if (unlikely(err)) {
25622+ err = -EFAULT;
392086de
AM
25623+ AuTraceErr(err);
25624+ goto out_free;
c2b27bf2 25625+ }
392086de
AM
25626+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25627+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25628+ args->mvdown.au_errno = 0;
25629+ args->dentry = dentry;
79b8bda9 25630+ args->inode = inode;
392086de 25631+ args->sb = dentry->d_sb;
c2b27bf2 25632+
392086de
AM
25633+ err = -ENOENT;
25634+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25635+ args->parent = dget_parent(dentry);
5527c038 25636+ args->dir = d_inode(args->parent);
febd17d6 25637+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25638+ dput(args->parent);
25639+ if (unlikely(args->parent != dentry->d_parent)) {
25640+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25641+ goto out_dir;
25642+ }
25643+
febd17d6 25644+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25645+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25646+ if (unlikely(err))
25647+ goto out_inode;
25648+
392086de
AM
25649+ di_write_lock_parent(args->parent);
25650+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25651+ if (unlikely(err))
25652+ goto out_parent;
25653+
392086de 25654+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25655+ if (unlikely(err))
25656+ goto out_parent;
c2b27bf2 25657+
392086de 25658+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25659+ au_cpup_attr_timesizes(inode);
25660+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25661+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25662+ /* au_digen_dec(dentry); */
25663+
25664+out_parent:
392086de 25665+ di_write_unlock(args->parent);
c2b27bf2
AM
25666+ aufs_read_unlock(dentry, AuLock_DW);
25667+out_inode:
febd17d6 25668+ inode_unlock(inode);
c2b27bf2 25669+out_dir:
febd17d6 25670+ inode_unlock(args->dir);
392086de
AM
25671+out_free:
25672+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25673+ if (unlikely(e))
25674+ err = -EFAULT;
9f237c51 25675+ au_kfree_rcu(args);
c2b27bf2
AM
25676+out:
25677+ AuTraceErr(err);
25678+ return err;
25679+}
25680diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
eca34b5c 25681--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
016522bc 25682+++ linux/fs/aufs/opts.c 2020-01-27 10:57:18.175538316 +0100
eca34b5c 25683@@ -0,0 +1,1880 @@
cd7a4cd9 25684+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25685+/*
016522bc 25686+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 25687+ *
25688+ * This program, aufs is free software; you can redistribute it and/or modify
25689+ * it under the terms of the GNU General Public License as published by
25690+ * the Free Software Foundation; either version 2 of the License, or
25691+ * (at your option) any later version.
dece6358
AM
25692+ *
25693+ * This program is distributed in the hope that it will be useful,
25694+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25695+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25696+ * GNU General Public License for more details.
25697+ *
25698+ * You should have received a copy of the GNU General Public License
523b37e3 25699+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25700+ */
25701+
25702+/*
25703+ * mount options/flags
25704+ */
25705+
dece6358 25706+#include <linux/namei.h>
1facf9fc 25707+#include <linux/types.h> /* a distribution requires */
25708+#include <linux/parser.h>
25709+#include "aufs.h"
25710+
25711+/* ---------------------------------------------------------------------- */
25712+
25713+enum {
25714+ Opt_br,
7e9cd9fe
AM
25715+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25716+ Opt_idel, Opt_imod,
25717+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25718+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25719+ Opt_xino, Opt_noxino,
1facf9fc 25720+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25721+ Opt_trunc_xino_path, Opt_itrunc_xino,
25722+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25723+ Opt_shwh, Opt_noshwh,
1facf9fc 25724+ Opt_plink, Opt_noplink, Opt_list_plink,
25725+ Opt_udba,
4a4d8108 25726+ Opt_dio, Opt_nodio,
1facf9fc 25727+ Opt_diropq_a, Opt_diropq_w,
25728+ Opt_warn_perm, Opt_nowarn_perm,
25729+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25730+ Opt_fhsm_sec,
1facf9fc 25731+ Opt_verbose, Opt_noverbose,
25732+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25733+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25734+ Opt_dirren, Opt_nodirren,
c1595e42 25735+ Opt_acl, Opt_noacl,
1facf9fc 25736+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25737+};
25738+
25739+static match_table_t options = {
25740+ {Opt_br, "br=%s"},
25741+ {Opt_br, "br:%s"},
25742+
25743+ {Opt_add, "add=%d:%s"},
25744+ {Opt_add, "add:%d:%s"},
25745+ {Opt_add, "ins=%d:%s"},
25746+ {Opt_add, "ins:%d:%s"},
25747+ {Opt_append, "append=%s"},
25748+ {Opt_append, "append:%s"},
25749+ {Opt_prepend, "prepend=%s"},
25750+ {Opt_prepend, "prepend:%s"},
25751+
25752+ {Opt_del, "del=%s"},
25753+ {Opt_del, "del:%s"},
25754+ /* {Opt_idel, "idel:%d"}, */
25755+ {Opt_mod, "mod=%s"},
25756+ {Opt_mod, "mod:%s"},
25757+ /* {Opt_imod, "imod:%d:%s"}, */
25758+
25759+ {Opt_dirwh, "dirwh=%d"},
25760+
25761+ {Opt_xino, "xino=%s"},
25762+ {Opt_noxino, "noxino"},
25763+ {Opt_trunc_xino, "trunc_xino"},
25764+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25765+ {Opt_notrunc_xino, "notrunc_xino"},
25766+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25767+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25768+ /* {Opt_zxino, "zxino=%s"}, */
25769+ {Opt_trunc_xib, "trunc_xib"},
25770+ {Opt_notrunc_xib, "notrunc_xib"},
25771+
e49829fe 25772+#ifdef CONFIG_PROC_FS
1facf9fc 25773+ {Opt_plink, "plink"},
e49829fe
JR
25774+#else
25775+ {Opt_ignore_silent, "plink"},
25776+#endif
25777+
1facf9fc 25778+ {Opt_noplink, "noplink"},
e49829fe 25779+
1facf9fc 25780+#ifdef CONFIG_AUFS_DEBUG
25781+ {Opt_list_plink, "list_plink"},
25782+#endif
25783+
25784+ {Opt_udba, "udba=%s"},
25785+
4a4d8108
AM
25786+ {Opt_dio, "dio"},
25787+ {Opt_nodio, "nodio"},
25788+
8b6a4947
AM
25789+#ifdef CONFIG_AUFS_DIRREN
25790+ {Opt_dirren, "dirren"},
25791+ {Opt_nodirren, "nodirren"},
25792+#else
25793+ {Opt_ignore, "dirren"},
25794+ {Opt_ignore_silent, "nodirren"},
25795+#endif
25796+
076b876e
AM
25797+#ifdef CONFIG_AUFS_FHSM
25798+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25799+#else
8b6a4947 25800+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25801+#endif
25802+
1facf9fc 25803+ {Opt_diropq_a, "diropq=always"},
25804+ {Opt_diropq_a, "diropq=a"},
25805+ {Opt_diropq_w, "diropq=whiteouted"},
25806+ {Opt_diropq_w, "diropq=w"},
25807+
25808+ {Opt_warn_perm, "warn_perm"},
25809+ {Opt_nowarn_perm, "nowarn_perm"},
25810+
25811+ /* keep them temporary */
1facf9fc 25812+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25813+ {Opt_ignore, "clean_plink"},
1facf9fc 25814+
dece6358
AM
25815+#ifdef CONFIG_AUFS_SHWH
25816+ {Opt_shwh, "shwh"},
25817+#endif
25818+ {Opt_noshwh, "noshwh"},
25819+
076b876e
AM
25820+ {Opt_dirperm1, "dirperm1"},
25821+ {Opt_nodirperm1, "nodirperm1"},
25822+
1facf9fc 25823+ {Opt_verbose, "verbose"},
25824+ {Opt_verbose, "v"},
25825+ {Opt_noverbose, "noverbose"},
25826+ {Opt_noverbose, "quiet"},
25827+ {Opt_noverbose, "q"},
25828+ {Opt_noverbose, "silent"},
25829+
25830+ {Opt_sum, "sum"},
25831+ {Opt_nosum, "nosum"},
25832+ {Opt_wsum, "wsum"},
25833+
25834+ {Opt_rdcache, "rdcache=%d"},
25835+ {Opt_rdblk, "rdblk=%d"},
dece6358 25836+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25837+ {Opt_rdhash, "rdhash=%d"},
dece6358 25838+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25839+
25840+ {Opt_wbr_create, "create=%s"},
25841+ {Opt_wbr_create, "create_policy=%s"},
25842+ {Opt_wbr_copyup, "cpup=%s"},
25843+ {Opt_wbr_copyup, "copyup=%s"},
25844+ {Opt_wbr_copyup, "copyup_policy=%s"},
25845+
c1595e42
JR
25846+ /* generic VFS flag */
25847+#ifdef CONFIG_FS_POSIX_ACL
25848+ {Opt_acl, "acl"},
25849+ {Opt_noacl, "noacl"},
25850+#else
8b6a4947 25851+ {Opt_ignore, "acl"},
c1595e42
JR
25852+ {Opt_ignore_silent, "noacl"},
25853+#endif
25854+
1facf9fc 25855+ /* internal use for the scripts */
25856+ {Opt_ignore_silent, "si=%s"},
25857+
25858+ {Opt_br, "dirs=%s"},
25859+ {Opt_ignore, "debug=%d"},
25860+ {Opt_ignore, "delete=whiteout"},
25861+ {Opt_ignore, "delete=all"},
25862+ {Opt_ignore, "imap=%s"},
25863+
1308ab2a 25864+ /* temporary workaround, due to old mount(8)? */
25865+ {Opt_ignore_silent, "relatime"},
25866+
1facf9fc 25867+ {Opt_err, NULL}
25868+};
25869+
25870+/* ---------------------------------------------------------------------- */
25871+
076b876e 25872+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25873+{
076b876e
AM
25874+ struct match_token *p;
25875+
25876+ p = tbl;
25877+ while (p->pattern) {
25878+ if (p->token == val)
25879+ return p->pattern;
25880+ p++;
1facf9fc 25881+ }
25882+ BUG();
25883+ return "??";
25884+}
25885+
076b876e
AM
25886+static const char *au_optstr(int *val, match_table_t tbl)
25887+{
25888+ struct match_token *p;
25889+ int v;
25890+
25891+ v = *val;
2000de60
JR
25892+ if (!v)
25893+ goto out;
076b876e 25894+ p = tbl;
2000de60
JR
25895+ while (p->pattern) {
25896+ if (p->token
25897+ && (v & p->token) == p->token) {
076b876e
AM
25898+ *val &= ~p->token;
25899+ return p->pattern;
25900+ }
25901+ p++;
25902+ }
2000de60
JR
25903+
25904+out:
076b876e
AM
25905+ return NULL;
25906+}
25907+
1facf9fc 25908+/* ---------------------------------------------------------------------- */
25909+
1e00d052 25910+static match_table_t brperm = {
1facf9fc 25911+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25912+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25913+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
25914+ {0, NULL}
25915+};
1facf9fc 25916+
86dc4139 25917+static match_table_t brattr = {
076b876e
AM
25918+ /* general */
25919+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25920+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 25921+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 25922+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 25923+#ifdef CONFIG_AUFS_FHSM
076b876e 25924+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
25925+#endif
25926+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
25927+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25928+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25929+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25930+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25931+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25932+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 25933+#endif
076b876e
AM
25934+
25935+ /* ro/rr branch */
1e00d052 25936+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
25937+
25938+ /* rw branch */
25939+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 25940+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 25941+
1e00d052 25942+ {0, NULL}
1facf9fc 25943+};
25944+
1e00d052
AM
25945+static int br_attr_val(char *str, match_table_t table, substring_t args[])
25946+{
25947+ int attr, v;
25948+ char *p;
25949+
25950+ attr = 0;
25951+ do {
25952+ p = strchr(str, '+');
25953+ if (p)
25954+ *p = 0;
25955+ v = match_token(str, table, args);
076b876e
AM
25956+ if (v) {
25957+ if (v & AuBrAttr_CMOO_Mask)
25958+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 25959+ attr |= v;
076b876e 25960+ } else {
1e00d052
AM
25961+ if (p)
25962+ *p = '+';
0c3ec466 25963+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
25964+ break;
25965+ }
25966+ if (p)
25967+ str = p + 1;
25968+ } while (p);
25969+
25970+ return attr;
25971+}
25972+
076b876e
AM
25973+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25974+{
25975+ int sz;
25976+ const char *p;
25977+ char *q;
25978+
076b876e
AM
25979+ q = str->a;
25980+ *q = 0;
25981+ p = au_optstr(&perm, brattr);
25982+ if (p) {
25983+ sz = strlen(p);
25984+ memcpy(q, p, sz + 1);
25985+ q += sz;
25986+ } else
25987+ goto out;
25988+
25989+ do {
25990+ p = au_optstr(&perm, brattr);
25991+ if (p) {
25992+ *q++ = '+';
25993+ sz = strlen(p);
25994+ memcpy(q, p, sz + 1);
25995+ q += sz;
25996+ }
25997+ } while (p);
25998+
25999+out:
c1595e42 26000+ return q - str->a;
076b876e
AM
26001+}
26002+
4a4d8108 26003+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 26004+{
076b876e
AM
26005+ int val, bad, sz;
26006+ char *p;
1facf9fc 26007+ substring_t args[MAX_OPT_ARGS];
076b876e 26008+ au_br_perm_str_t attr;
1facf9fc 26009+
1e00d052
AM
26010+ p = strchr(perm, '+');
26011+ if (p)
26012+ *p = 0;
26013+ val = match_token(perm, brperm, args);
26014+ if (!val) {
26015+ if (p)
26016+ *p = '+';
0c3ec466 26017+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
26018+ val = AuBrPerm_RO;
26019+ goto out;
26020+ }
26021+ if (!p)
26022+ goto out;
26023+
076b876e
AM
26024+ val |= br_attr_val(p + 1, brattr, args);
26025+
26026+ bad = 0;
86dc4139 26027+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
26028+ case AuBrPerm_RO:
26029+ case AuBrPerm_RR:
076b876e
AM
26030+ bad = val & AuBrWAttr_Mask;
26031+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
26032+ break;
26033+ case AuBrPerm_RW:
076b876e
AM
26034+ bad = val & AuBrRAttr_Mask;
26035+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
26036+ break;
26037+ }
c1595e42
JR
26038+
26039+ /*
26040+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26041+ * does not treat it as an error, just warning.
26042+ * this is a tiny guard for the user operation.
26043+ */
26044+ if (val & AuBrAttr_UNPIN) {
26045+ bad |= AuBrAttr_UNPIN;
26046+ val &= ~AuBrAttr_UNPIN;
26047+ }
26048+
076b876e
AM
26049+ if (unlikely(bad)) {
26050+ sz = au_do_optstr_br_attr(&attr, bad);
26051+ AuDebugOn(!sz);
26052+ pr_warn("ignored branch attribute %s\n", attr.a);
26053+ }
1e00d052
AM
26054+
26055+out:
1facf9fc 26056+ return val;
26057+}
26058+
076b876e 26059+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 26060+{
076b876e
AM
26061+ au_br_perm_str_t attr;
26062+ const char *p;
26063+ char *q;
1e00d052
AM
26064+ int sz;
26065+
076b876e
AM
26066+ q = str->a;
26067+ p = au_optstr(&perm, brperm);
26068+ AuDebugOn(!p || !*p);
26069+ sz = strlen(p);
26070+ memcpy(q, p, sz + 1);
26071+ q += sz;
1e00d052 26072+
076b876e
AM
26073+ sz = au_do_optstr_br_attr(&attr, perm);
26074+ if (sz) {
26075+ *q++ = '+';
26076+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
26077+ }
26078+
076b876e 26079+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 26080+}
26081+
26082+/* ---------------------------------------------------------------------- */
26083+
26084+static match_table_t udbalevel = {
26085+ {AuOpt_UDBA_REVAL, "reval"},
26086+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
26087+#ifdef CONFIG_AUFS_HNOTIFY
26088+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26089+#ifdef CONFIG_AUFS_HFSNOTIFY
26090+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 26091+#endif
1facf9fc 26092+#endif
26093+ {-1, NULL}
26094+};
26095+
4a4d8108 26096+static int noinline_for_stack udba_val(char *str)
1facf9fc 26097+{
26098+ substring_t args[MAX_OPT_ARGS];
26099+
7f207e10 26100+ return match_token(str, udbalevel, args);
1facf9fc 26101+}
26102+
26103+const char *au_optstr_udba(int udba)
26104+{
076b876e 26105+ return au_parser_pattern(udba, udbalevel);
1facf9fc 26106+}
26107+
26108+/* ---------------------------------------------------------------------- */
26109+
26110+static match_table_t au_wbr_create_policy = {
26111+ {AuWbrCreate_TDP, "tdp"},
26112+ {AuWbrCreate_TDP, "top-down-parent"},
26113+ {AuWbrCreate_RR, "rr"},
26114+ {AuWbrCreate_RR, "round-robin"},
26115+ {AuWbrCreate_MFS, "mfs"},
26116+ {AuWbrCreate_MFS, "most-free-space"},
26117+ {AuWbrCreate_MFSV, "mfs:%d"},
26118+ {AuWbrCreate_MFSV, "most-free-space:%d"},
26119+
f2c43d5f
AM
26120+ /* top-down regardless the parent, and then mfs */
26121+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
26122+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26123+
1facf9fc 26124+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
26125+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26126+ {AuWbrCreate_PMFS, "pmfs"},
26127+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
26128+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26129+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 26130+
26131+ {-1, NULL}
26132+};
26133+
1facf9fc 26134+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26135+ struct au_opt_wbr_create *create)
26136+{
26137+ int err;
26138+ unsigned long long ull;
26139+
26140+ err = 0;
a2654f78 26141+ if (!match_u64(arg, &ull))
1facf9fc 26142+ create->mfsrr_watermark = ull;
26143+ else {
4a4d8108 26144+ pr_err("bad integer in %s\n", str);
1facf9fc 26145+ err = -EINVAL;
26146+ }
26147+
26148+ return err;
26149+}
26150+
26151+static int au_wbr_mfs_sec(substring_t *arg, char *str,
26152+ struct au_opt_wbr_create *create)
26153+{
26154+ int n, err;
26155+
26156+ err = 0;
027c5e7a 26157+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 26158+ create->mfs_second = n;
26159+ else {
4a4d8108 26160+ pr_err("bad integer in %s\n", str);
1facf9fc 26161+ err = -EINVAL;
26162+ }
26163+
26164+ return err;
26165+}
26166+
4a4d8108
AM
26167+static int noinline_for_stack
26168+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 26169+{
26170+ int err, e;
26171+ substring_t args[MAX_OPT_ARGS];
26172+
26173+ err = match_token(str, au_wbr_create_policy, args);
26174+ create->wbr_create = err;
26175+ switch (err) {
26176+ case AuWbrCreate_MFSRRV:
f2c43d5f 26177+ case AuWbrCreate_TDMFSV:
392086de 26178+ case AuWbrCreate_PMFSRRV:
1facf9fc 26179+ e = au_wbr_mfs_wmark(&args[0], str, create);
26180+ if (!e)
26181+ e = au_wbr_mfs_sec(&args[1], str, create);
26182+ if (unlikely(e))
26183+ err = e;
26184+ break;
26185+ case AuWbrCreate_MFSRR:
f2c43d5f 26186+ case AuWbrCreate_TDMFS:
392086de 26187+ case AuWbrCreate_PMFSRR:
1facf9fc 26188+ e = au_wbr_mfs_wmark(&args[0], str, create);
26189+ if (unlikely(e)) {
26190+ err = e;
26191+ break;
26192+ }
26193+ /*FALLTHROUGH*/
26194+ case AuWbrCreate_MFS:
26195+ case AuWbrCreate_PMFS:
027c5e7a 26196+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 26197+ break;
26198+ case AuWbrCreate_MFSV:
26199+ case AuWbrCreate_PMFSV:
26200+ e = au_wbr_mfs_sec(&args[0], str, create);
26201+ if (unlikely(e))
26202+ err = e;
26203+ break;
26204+ }
26205+
26206+ return err;
26207+}
26208+
26209+const char *au_optstr_wbr_create(int wbr_create)
26210+{
076b876e 26211+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 26212+}
26213+
26214+static match_table_t au_wbr_copyup_policy = {
26215+ {AuWbrCopyup_TDP, "tdp"},
26216+ {AuWbrCopyup_TDP, "top-down-parent"},
26217+ {AuWbrCopyup_BUP, "bup"},
26218+ {AuWbrCopyup_BUP, "bottom-up-parent"},
26219+ {AuWbrCopyup_BU, "bu"},
26220+ {AuWbrCopyup_BU, "bottom-up"},
26221+ {-1, NULL}
26222+};
26223+
4a4d8108 26224+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 26225+{
26226+ substring_t args[MAX_OPT_ARGS];
26227+
26228+ return match_token(str, au_wbr_copyup_policy, args);
26229+}
26230+
26231+const char *au_optstr_wbr_copyup(int wbr_copyup)
26232+{
076b876e 26233+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 26234+}
26235+
26236+/* ---------------------------------------------------------------------- */
26237+
26238+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26239+
26240+static void dump_opts(struct au_opts *opts)
26241+{
26242+#ifdef CONFIG_AUFS_DEBUG
26243+ /* reduce stack space */
26244+ union {
26245+ struct au_opt_add *add;
26246+ struct au_opt_del *del;
26247+ struct au_opt_mod *mod;
26248+ struct au_opt_xino *xino;
26249+ struct au_opt_xino_itrunc *xino_itrunc;
26250+ struct au_opt_wbr_create *create;
26251+ } u;
26252+ struct au_opt *opt;
26253+
26254+ opt = opts->opt;
26255+ while (opt->type != Opt_tail) {
26256+ switch (opt->type) {
26257+ case Opt_add:
26258+ u.add = &opt->add;
26259+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26260+ u.add->bindex, u.add->pathname, u.add->perm,
26261+ u.add->path.dentry);
26262+ break;
26263+ case Opt_del:
26264+ case Opt_idel:
26265+ u.del = &opt->del;
26266+ AuDbg("del {%s, %p}\n",
26267+ u.del->pathname, u.del->h_path.dentry);
26268+ break;
26269+ case Opt_mod:
26270+ case Opt_imod:
26271+ u.mod = &opt->mod;
26272+ AuDbg("mod {%s, 0x%x, %p}\n",
26273+ u.mod->path, u.mod->perm, u.mod->h_root);
26274+ break;
26275+ case Opt_append:
26276+ u.add = &opt->add;
26277+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26278+ u.add->bindex, u.add->pathname, u.add->perm,
26279+ u.add->path.dentry);
26280+ break;
26281+ case Opt_prepend:
26282+ u.add = &opt->add;
26283+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26284+ u.add->bindex, u.add->pathname, u.add->perm,
26285+ u.add->path.dentry);
26286+ break;
26287+ case Opt_dirwh:
26288+ AuDbg("dirwh %d\n", opt->dirwh);
26289+ break;
26290+ case Opt_rdcache:
26291+ AuDbg("rdcache %d\n", opt->rdcache);
26292+ break;
26293+ case Opt_rdblk:
26294+ AuDbg("rdblk %u\n", opt->rdblk);
26295+ break;
dece6358
AM
26296+ case Opt_rdblk_def:
26297+ AuDbg("rdblk_def\n");
26298+ break;
1facf9fc 26299+ case Opt_rdhash:
26300+ AuDbg("rdhash %u\n", opt->rdhash);
26301+ break;
dece6358
AM
26302+ case Opt_rdhash_def:
26303+ AuDbg("rdhash_def\n");
26304+ break;
1facf9fc 26305+ case Opt_xino:
26306+ u.xino = &opt->xino;
523b37e3 26307+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 26308+ break;
26309+ case Opt_trunc_xino:
26310+ AuLabel(trunc_xino);
26311+ break;
26312+ case Opt_notrunc_xino:
26313+ AuLabel(notrunc_xino);
26314+ break;
26315+ case Opt_trunc_xino_path:
26316+ case Opt_itrunc_xino:
26317+ u.xino_itrunc = &opt->xino_itrunc;
26318+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26319+ break;
1facf9fc 26320+ case Opt_noxino:
26321+ AuLabel(noxino);
26322+ break;
26323+ case Opt_trunc_xib:
26324+ AuLabel(trunc_xib);
26325+ break;
26326+ case Opt_notrunc_xib:
26327+ AuLabel(notrunc_xib);
26328+ break;
dece6358
AM
26329+ case Opt_shwh:
26330+ AuLabel(shwh);
26331+ break;
26332+ case Opt_noshwh:
26333+ AuLabel(noshwh);
26334+ break;
076b876e
AM
26335+ case Opt_dirperm1:
26336+ AuLabel(dirperm1);
26337+ break;
26338+ case Opt_nodirperm1:
26339+ AuLabel(nodirperm1);
26340+ break;
1facf9fc 26341+ case Opt_plink:
26342+ AuLabel(plink);
26343+ break;
26344+ case Opt_noplink:
26345+ AuLabel(noplink);
26346+ break;
26347+ case Opt_list_plink:
26348+ AuLabel(list_plink);
26349+ break;
26350+ case Opt_udba:
26351+ AuDbg("udba %d, %s\n",
26352+ opt->udba, au_optstr_udba(opt->udba));
26353+ break;
4a4d8108
AM
26354+ case Opt_dio:
26355+ AuLabel(dio);
26356+ break;
26357+ case Opt_nodio:
26358+ AuLabel(nodio);
26359+ break;
1facf9fc 26360+ case Opt_diropq_a:
26361+ AuLabel(diropq_a);
26362+ break;
26363+ case Opt_diropq_w:
26364+ AuLabel(diropq_w);
26365+ break;
26366+ case Opt_warn_perm:
26367+ AuLabel(warn_perm);
26368+ break;
26369+ case Opt_nowarn_perm:
26370+ AuLabel(nowarn_perm);
26371+ break;
1facf9fc 26372+ case Opt_verbose:
26373+ AuLabel(verbose);
26374+ break;
26375+ case Opt_noverbose:
26376+ AuLabel(noverbose);
26377+ break;
26378+ case Opt_sum:
26379+ AuLabel(sum);
26380+ break;
26381+ case Opt_nosum:
26382+ AuLabel(nosum);
26383+ break;
26384+ case Opt_wsum:
26385+ AuLabel(wsum);
26386+ break;
26387+ case Opt_wbr_create:
26388+ u.create = &opt->wbr_create;
26389+ AuDbg("create %d, %s\n", u.create->wbr_create,
26390+ au_optstr_wbr_create(u.create->wbr_create));
26391+ switch (u.create->wbr_create) {
26392+ case AuWbrCreate_MFSV:
26393+ case AuWbrCreate_PMFSV:
26394+ AuDbg("%d sec\n", u.create->mfs_second);
26395+ break;
26396+ case AuWbrCreate_MFSRR:
f2c43d5f 26397+ case AuWbrCreate_TDMFS:
1facf9fc 26398+ AuDbg("%llu watermark\n",
26399+ u.create->mfsrr_watermark);
26400+ break;
26401+ case AuWbrCreate_MFSRRV:
f2c43d5f 26402+ case AuWbrCreate_TDMFSV:
392086de 26403+ case AuWbrCreate_PMFSRRV:
1facf9fc 26404+ AuDbg("%llu watermark, %d sec\n",
26405+ u.create->mfsrr_watermark,
26406+ u.create->mfs_second);
26407+ break;
26408+ }
26409+ break;
26410+ case Opt_wbr_copyup:
26411+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26412+ au_optstr_wbr_copyup(opt->wbr_copyup));
26413+ break;
076b876e
AM
26414+ case Opt_fhsm_sec:
26415+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26416+ break;
8b6a4947
AM
26417+ case Opt_dirren:
26418+ AuLabel(dirren);
26419+ break;
26420+ case Opt_nodirren:
26421+ AuLabel(nodirren);
26422+ break;
c1595e42
JR
26423+ case Opt_acl:
26424+ AuLabel(acl);
26425+ break;
26426+ case Opt_noacl:
26427+ AuLabel(noacl);
26428+ break;
1facf9fc 26429+ default:
26430+ BUG();
26431+ }
26432+ opt++;
26433+ }
26434+#endif
26435+}
26436+
26437+void au_opts_free(struct au_opts *opts)
26438+{
26439+ struct au_opt *opt;
26440+
26441+ opt = opts->opt;
26442+ while (opt->type != Opt_tail) {
26443+ switch (opt->type) {
26444+ case Opt_add:
26445+ case Opt_append:
26446+ case Opt_prepend:
26447+ path_put(&opt->add.path);
26448+ break;
26449+ case Opt_del:
26450+ case Opt_idel:
26451+ path_put(&opt->del.h_path);
26452+ break;
26453+ case Opt_mod:
26454+ case Opt_imod:
26455+ dput(opt->mod.h_root);
26456+ break;
26457+ case Opt_xino:
26458+ fput(opt->xino.file);
26459+ break;
26460+ }
26461+ opt++;
26462+ }
26463+}
26464+
26465+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26466+ aufs_bindex_t bindex)
26467+{
26468+ int err;
26469+ struct au_opt_add *add = &opt->add;
26470+ char *p;
26471+
26472+ add->bindex = bindex;
1e00d052 26473+ add->perm = AuBrPerm_RO;
1facf9fc 26474+ add->pathname = opt_str;
26475+ p = strchr(opt_str, '=');
26476+ if (p) {
26477+ *p++ = 0;
26478+ if (*p)
26479+ add->perm = br_perm_val(p);
26480+ }
26481+
26482+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26483+ if (!err) {
26484+ if (!p) {
26485+ add->perm = AuBrPerm_RO;
26486+ if (au_test_fs_rr(add->path.dentry->d_sb))
26487+ add->perm = AuBrPerm_RR;
2121bcd9 26488+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 26489+ add->perm = AuBrPerm_RW;
26490+ }
26491+ opt->type = Opt_add;
26492+ goto out;
26493+ }
4a4d8108 26494+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26495+ err = -EINVAL;
26496+
4f0767ce 26497+out:
1facf9fc 26498+ return err;
26499+}
26500+
26501+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26502+{
26503+ int err;
26504+
26505+ del->pathname = args[0].from;
26506+ AuDbg("del path %s\n", del->pathname);
26507+
26508+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26509+ if (unlikely(err))
4a4d8108 26510+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26511+
26512+ return err;
26513+}
26514+
26515+#if 0 /* reserved for future use */
26516+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26517+ struct au_opt_del *del, substring_t args[])
26518+{
26519+ int err;
26520+ struct dentry *root;
26521+
26522+ err = -EINVAL;
26523+ root = sb->s_root;
26524+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26525+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26526+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26527+ goto out;
26528+ }
26529+
26530+ err = 0;
26531+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26532+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26533+
4f0767ce 26534+out:
1facf9fc 26535+ aufs_read_unlock(root, !AuLock_IR);
26536+ return err;
26537+}
26538+#endif
26539+
4a4d8108
AM
26540+static int noinline_for_stack
26541+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26542+{
26543+ int err;
26544+ struct path path;
26545+ char *p;
26546+
26547+ err = -EINVAL;
26548+ mod->path = args[0].from;
26549+ p = strchr(mod->path, '=');
26550+ if (unlikely(!p)) {
acd2b654 26551+ pr_err("no permission %s\n", args[0].from);
1facf9fc 26552+ goto out;
26553+ }
26554+
26555+ *p++ = 0;
26556+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26557+ if (unlikely(err)) {
4a4d8108 26558+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26559+ goto out;
26560+ }
26561+
26562+ mod->perm = br_perm_val(p);
26563+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26564+ mod->h_root = dget(path.dentry);
26565+ path_put(&path);
26566+
4f0767ce 26567+out:
1facf9fc 26568+ return err;
26569+}
26570+
26571+#if 0 /* reserved for future use */
26572+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26573+ struct au_opt_mod *mod, substring_t args[])
26574+{
26575+ int err;
26576+ struct dentry *root;
26577+
26578+ err = -EINVAL;
26579+ root = sb->s_root;
26580+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26581+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26582+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26583+ goto out;
26584+ }
26585+
26586+ err = 0;
26587+ mod->perm = br_perm_val(args[1].from);
26588+ AuDbg("mod path %s, perm 0x%x, %s\n",
26589+ mod->path, mod->perm, args[1].from);
26590+ mod->h_root = dget(au_h_dptr(root, bindex));
26591+
4f0767ce 26592+out:
1facf9fc 26593+ aufs_read_unlock(root, !AuLock_IR);
26594+ return err;
26595+}
26596+#endif
26597+
26598+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26599+ substring_t args[])
26600+{
26601+ int err;
26602+ struct file *file;
26603+
83b672a5 26604+ file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
1facf9fc 26605+ err = PTR_ERR(file);
26606+ if (IS_ERR(file))
26607+ goto out;
26608+
26609+ err = -EINVAL;
2000de60 26610+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26611+ fput(file);
4a4d8108 26612+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26613+ goto out;
26614+ }
26615+
26616+ err = 0;
26617+ xino->file = file;
26618+ xino->path = args[0].from;
26619+
4f0767ce 26620+out:
1facf9fc 26621+ return err;
26622+}
26623+
4a4d8108
AM
26624+static int noinline_for_stack
26625+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26626+ struct au_opt_xino_itrunc *xino_itrunc,
26627+ substring_t args[])
1facf9fc 26628+{
26629+ int err;
5afbbe0d 26630+ aufs_bindex_t bbot, bindex;
1facf9fc 26631+ struct path path;
26632+ struct dentry *root;
26633+
26634+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26635+ if (unlikely(err)) {
4a4d8108 26636+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26637+ goto out;
26638+ }
26639+
26640+ xino_itrunc->bindex = -1;
26641+ root = sb->s_root;
26642+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26643+ bbot = au_sbbot(sb);
26644+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26645+ if (au_h_dptr(root, bindex) == path.dentry) {
26646+ xino_itrunc->bindex = bindex;
26647+ break;
26648+ }
26649+ }
26650+ aufs_read_unlock(root, !AuLock_IR);
26651+ path_put(&path);
26652+
26653+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26654+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26655+ err = -EINVAL;
26656+ }
26657+
4f0767ce 26658+out:
1facf9fc 26659+ return err;
26660+}
26661+
26662+/* called without aufs lock */
26663+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26664+{
26665+ int err, n, token;
26666+ aufs_bindex_t bindex;
26667+ unsigned char skipped;
26668+ struct dentry *root;
26669+ struct au_opt *opt, *opt_tail;
26670+ char *opt_str;
26671+ /* reduce the stack space */
26672+ union {
26673+ struct au_opt_xino_itrunc *xino_itrunc;
26674+ struct au_opt_wbr_create *create;
26675+ } u;
26676+ struct {
26677+ substring_t args[MAX_OPT_ARGS];
26678+ } *a;
26679+
26680+ err = -ENOMEM;
26681+ a = kmalloc(sizeof(*a), GFP_NOFS);
26682+ if (unlikely(!a))
26683+ goto out;
26684+
26685+ root = sb->s_root;
26686+ err = 0;
26687+ bindex = 0;
26688+ opt = opts->opt;
26689+ opt_tail = opt + opts->max_opt - 1;
26690+ opt->type = Opt_tail;
26691+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26692+ err = -EINVAL;
26693+ skipped = 0;
26694+ token = match_token(opt_str, options, a->args);
26695+ switch (token) {
26696+ case Opt_br:
26697+ err = 0;
26698+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26699+ && *opt_str) {
26700+ err = opt_add(opt, opt_str, opts->sb_flags,
26701+ bindex++);
26702+ if (unlikely(!err && ++opt > opt_tail)) {
26703+ err = -E2BIG;
26704+ break;
26705+ }
26706+ opt->type = Opt_tail;
26707+ skipped = 1;
26708+ }
26709+ break;
26710+ case Opt_add:
26711+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26712+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26713+ break;
26714+ }
26715+ bindex = n;
26716+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26717+ bindex);
26718+ if (!err)
26719+ opt->type = token;
26720+ break;
26721+ case Opt_append:
26722+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26723+ /*dummy bindex*/1);
26724+ if (!err)
26725+ opt->type = token;
26726+ break;
26727+ case Opt_prepend:
26728+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26729+ /*bindex*/0);
26730+ if (!err)
26731+ opt->type = token;
26732+ break;
26733+ case Opt_del:
26734+ err = au_opts_parse_del(&opt->del, a->args);
26735+ if (!err)
26736+ opt->type = token;
26737+ break;
26738+#if 0 /* reserved for future use */
26739+ case Opt_idel:
26740+ del->pathname = "(indexed)";
26741+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26742+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26743+ break;
26744+ }
26745+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26746+ if (!err)
26747+ opt->type = token;
26748+ break;
26749+#endif
26750+ case Opt_mod:
26751+ err = au_opts_parse_mod(&opt->mod, a->args);
26752+ if (!err)
26753+ opt->type = token;
26754+ break;
26755+#ifdef IMOD /* reserved for future use */
26756+ case Opt_imod:
26757+ u.mod->path = "(indexed)";
26758+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26759+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26760+ break;
26761+ }
26762+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26763+ if (!err)
26764+ opt->type = token;
26765+ break;
26766+#endif
26767+ case Opt_xino:
26768+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26769+ if (!err)
26770+ opt->type = token;
26771+ break;
26772+
26773+ case Opt_trunc_xino_path:
26774+ err = au_opts_parse_xino_itrunc_path
26775+ (sb, &opt->xino_itrunc, a->args);
26776+ if (!err)
26777+ opt->type = token;
26778+ break;
26779+
26780+ case Opt_itrunc_xino:
26781+ u.xino_itrunc = &opt->xino_itrunc;
26782+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26783+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26784+ break;
26785+ }
26786+ u.xino_itrunc->bindex = n;
26787+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26788+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26789+ pr_err("out of bounds, %d\n", n);
1facf9fc 26790+ aufs_read_unlock(root, !AuLock_IR);
26791+ break;
26792+ }
26793+ aufs_read_unlock(root, !AuLock_IR);
26794+ err = 0;
26795+ opt->type = token;
26796+ break;
26797+
26798+ case Opt_dirwh:
26799+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26800+ break;
26801+ err = 0;
26802+ opt->type = token;
26803+ break;
26804+
26805+ case Opt_rdcache:
027c5e7a
AM
26806+ if (unlikely(match_int(&a->args[0], &n))) {
26807+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26808+ break;
027c5e7a
AM
26809+ }
26810+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26811+ pr_err("rdcache must be smaller than %d\n",
26812+ AUFS_RDCACHE_MAX);
26813+ break;
26814+ }
26815+ opt->rdcache = n;
1facf9fc 26816+ err = 0;
26817+ opt->type = token;
26818+ break;
26819+ case Opt_rdblk:
26820+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26821+ || n < 0
1facf9fc 26822+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26823+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26824+ break;
26825+ }
1308ab2a 26826+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26827+ pr_err("rdblk must be larger than %d\n",
26828+ NAME_MAX);
1facf9fc 26829+ break;
26830+ }
26831+ opt->rdblk = n;
26832+ err = 0;
26833+ opt->type = token;
26834+ break;
26835+ case Opt_rdhash:
26836+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26837+ || n < 0
1facf9fc 26838+ || n * sizeof(struct hlist_head)
26839+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26840+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26841+ break;
26842+ }
26843+ opt->rdhash = n;
26844+ err = 0;
26845+ opt->type = token;
26846+ break;
26847+
26848+ case Opt_trunc_xino:
26849+ case Opt_notrunc_xino:
26850+ case Opt_noxino:
26851+ case Opt_trunc_xib:
26852+ case Opt_notrunc_xib:
dece6358
AM
26853+ case Opt_shwh:
26854+ case Opt_noshwh:
076b876e
AM
26855+ case Opt_dirperm1:
26856+ case Opt_nodirperm1:
1facf9fc 26857+ case Opt_plink:
26858+ case Opt_noplink:
26859+ case Opt_list_plink:
4a4d8108
AM
26860+ case Opt_dio:
26861+ case Opt_nodio:
1facf9fc 26862+ case Opt_diropq_a:
26863+ case Opt_diropq_w:
26864+ case Opt_warn_perm:
26865+ case Opt_nowarn_perm:
1facf9fc 26866+ case Opt_verbose:
26867+ case Opt_noverbose:
26868+ case Opt_sum:
26869+ case Opt_nosum:
26870+ case Opt_wsum:
dece6358
AM
26871+ case Opt_rdblk_def:
26872+ case Opt_rdhash_def:
8b6a4947
AM
26873+ case Opt_dirren:
26874+ case Opt_nodirren:
c1595e42
JR
26875+ case Opt_acl:
26876+ case Opt_noacl:
1facf9fc 26877+ err = 0;
26878+ opt->type = token;
26879+ break;
26880+
26881+ case Opt_udba:
26882+ opt->udba = udba_val(a->args[0].from);
26883+ if (opt->udba >= 0) {
26884+ err = 0;
26885+ opt->type = token;
26886+ } else
4a4d8108 26887+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26888+ break;
26889+
26890+ case Opt_wbr_create:
26891+ u.create = &opt->wbr_create;
26892+ u.create->wbr_create
26893+ = au_wbr_create_val(a->args[0].from, u.create);
26894+ if (u.create->wbr_create >= 0) {
26895+ err = 0;
26896+ opt->type = token;
26897+ } else
4a4d8108 26898+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26899+ break;
26900+ case Opt_wbr_copyup:
26901+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26902+ if (opt->wbr_copyup >= 0) {
26903+ err = 0;
26904+ opt->type = token;
26905+ } else
4a4d8108 26906+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26907+ break;
26908+
076b876e
AM
26909+ case Opt_fhsm_sec:
26910+ if (unlikely(match_int(&a->args[0], &n)
26911+ || n < 0)) {
26912+ pr_err("bad integer in %s\n", opt_str);
26913+ break;
26914+ }
26915+ if (sysaufs_brs) {
26916+ opt->fhsm_second = n;
26917+ opt->type = token;
26918+ } else
26919+ pr_warn("ignored %s\n", opt_str);
26920+ err = 0;
26921+ break;
26922+
1facf9fc 26923+ case Opt_ignore:
0c3ec466 26924+ pr_warn("ignored %s\n", opt_str);
1facf9fc 26925+ /*FALLTHROUGH*/
26926+ case Opt_ignore_silent:
26927+ skipped = 1;
26928+ err = 0;
26929+ break;
26930+ case Opt_err:
4a4d8108 26931+ pr_err("unknown option %s\n", opt_str);
1facf9fc 26932+ break;
26933+ }
26934+
26935+ if (!err && !skipped) {
26936+ if (unlikely(++opt > opt_tail)) {
26937+ err = -E2BIG;
26938+ opt--;
26939+ opt->type = Opt_tail;
26940+ break;
26941+ }
26942+ opt->type = Opt_tail;
26943+ }
26944+ }
26945+
9f237c51 26946+ au_kfree_rcu(a);
1facf9fc 26947+ dump_opts(opts);
26948+ if (unlikely(err))
26949+ au_opts_free(opts);
26950+
4f0767ce 26951+out:
1facf9fc 26952+ return err;
26953+}
26954+
26955+static int au_opt_wbr_create(struct super_block *sb,
26956+ struct au_opt_wbr_create *create)
26957+{
26958+ int err;
26959+ struct au_sbinfo *sbinfo;
26960+
dece6358
AM
26961+ SiMustWriteLock(sb);
26962+
1facf9fc 26963+ err = 1; /* handled */
26964+ sbinfo = au_sbi(sb);
26965+ if (sbinfo->si_wbr_create_ops->fin) {
26966+ err = sbinfo->si_wbr_create_ops->fin(sb);
26967+ if (!err)
26968+ err = 1;
26969+ }
26970+
26971+ sbinfo->si_wbr_create = create->wbr_create;
26972+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26973+ switch (create->wbr_create) {
26974+ case AuWbrCreate_MFSRRV:
26975+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
26976+ case AuWbrCreate_TDMFS:
26977+ case AuWbrCreate_TDMFSV:
392086de
AM
26978+ case AuWbrCreate_PMFSRR:
26979+ case AuWbrCreate_PMFSRRV:
1facf9fc 26980+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26981+ /*FALLTHROUGH*/
26982+ case AuWbrCreate_MFS:
26983+ case AuWbrCreate_MFSV:
26984+ case AuWbrCreate_PMFS:
26985+ case AuWbrCreate_PMFSV:
e49829fe
JR
26986+ sbinfo->si_wbr_mfs.mfs_expire
26987+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 26988+ break;
26989+ }
26990+
26991+ if (sbinfo->si_wbr_create_ops->init)
26992+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26993+
26994+ return err;
26995+}
26996+
26997+/*
26998+ * returns,
26999+ * plus: processed without an error
27000+ * zero: unprocessed
27001+ */
27002+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27003+ struct au_opts *opts)
27004+{
27005+ int err;
27006+ struct au_sbinfo *sbinfo;
27007+
dece6358
AM
27008+ SiMustWriteLock(sb);
27009+
1facf9fc 27010+ err = 1; /* handled */
27011+ sbinfo = au_sbi(sb);
27012+ switch (opt->type) {
27013+ case Opt_udba:
27014+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27015+ sbinfo->si_mntflags |= opt->udba;
27016+ opts->given_udba |= opt->udba;
27017+ break;
27018+
27019+ case Opt_plink:
27020+ au_opt_set(sbinfo->si_mntflags, PLINK);
27021+ break;
27022+ case Opt_noplink:
27023+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 27024+ au_plink_put(sb, /*verbose*/1);
1facf9fc 27025+ au_opt_clr(sbinfo->si_mntflags, PLINK);
27026+ break;
27027+ case Opt_list_plink:
27028+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
27029+ au_plink_list(sb);
27030+ break;
27031+
4a4d8108
AM
27032+ case Opt_dio:
27033+ au_opt_set(sbinfo->si_mntflags, DIO);
27034+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27035+ break;
27036+ case Opt_nodio:
27037+ au_opt_clr(sbinfo->si_mntflags, DIO);
27038+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27039+ break;
27040+
076b876e
AM
27041+ case Opt_fhsm_sec:
27042+ au_fhsm_set(sbinfo, opt->fhsm_second);
27043+ break;
27044+
1facf9fc 27045+ case Opt_diropq_a:
27046+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27047+ break;
27048+ case Opt_diropq_w:
27049+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27050+ break;
27051+
27052+ case Opt_warn_perm:
27053+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27054+ break;
27055+ case Opt_nowarn_perm:
27056+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27057+ break;
27058+
1facf9fc 27059+ case Opt_verbose:
27060+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
27061+ break;
27062+ case Opt_noverbose:
27063+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27064+ break;
27065+
27066+ case Opt_sum:
27067+ au_opt_set(sbinfo->si_mntflags, SUM);
27068+ break;
27069+ case Opt_wsum:
27070+ au_opt_clr(sbinfo->si_mntflags, SUM);
27071+ au_opt_set(sbinfo->si_mntflags, SUM_W);
eca34b5c 27072+ break;
1facf9fc 27073+ case Opt_nosum:
27074+ au_opt_clr(sbinfo->si_mntflags, SUM);
27075+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
27076+ break;
27077+
27078+ case Opt_wbr_create:
27079+ err = au_opt_wbr_create(sb, &opt->wbr_create);
27080+ break;
27081+ case Opt_wbr_copyup:
27082+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
27083+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27084+ break;
27085+
27086+ case Opt_dirwh:
27087+ sbinfo->si_dirwh = opt->dirwh;
27088+ break;
27089+
27090+ case Opt_rdcache:
e49829fe
JR
27091+ sbinfo->si_rdcache
27092+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 27093+ break;
27094+ case Opt_rdblk:
27095+ sbinfo->si_rdblk = opt->rdblk;
27096+ break;
dece6358
AM
27097+ case Opt_rdblk_def:
27098+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27099+ break;
1facf9fc 27100+ case Opt_rdhash:
27101+ sbinfo->si_rdhash = opt->rdhash;
27102+ break;
dece6358
AM
27103+ case Opt_rdhash_def:
27104+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27105+ break;
27106+
27107+ case Opt_shwh:
27108+ au_opt_set(sbinfo->si_mntflags, SHWH);
27109+ break;
27110+ case Opt_noshwh:
27111+ au_opt_clr(sbinfo->si_mntflags, SHWH);
27112+ break;
1facf9fc 27113+
076b876e
AM
27114+ case Opt_dirperm1:
27115+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27116+ break;
27117+ case Opt_nodirperm1:
27118+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27119+ break;
27120+
1facf9fc 27121+ case Opt_trunc_xino:
27122+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27123+ break;
27124+ case Opt_notrunc_xino:
27125+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27126+ break;
27127+
27128+ case Opt_trunc_xino_path:
27129+ case Opt_itrunc_xino:
acd2b654
AM
27130+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27131+ /*idx_begin*/0);
1facf9fc 27132+ if (!err)
27133+ err = 1;
27134+ break;
27135+
27136+ case Opt_trunc_xib:
27137+ au_fset_opts(opts->flags, TRUNC_XIB);
27138+ break;
27139+ case Opt_notrunc_xib:
27140+ au_fclr_opts(opts->flags, TRUNC_XIB);
27141+ break;
27142+
8b6a4947
AM
27143+ case Opt_dirren:
27144+ err = 1;
27145+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27146+ err = au_dr_opt_set(sb);
27147+ if (!err)
27148+ err = 1;
27149+ }
27150+ if (err == 1)
27151+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27152+ break;
27153+ case Opt_nodirren:
27154+ err = 1;
27155+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27156+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27157+ DR_FLUSHED));
27158+ if (!err)
27159+ err = 1;
27160+ }
27161+ if (err == 1)
27162+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
27163+ break;
27164+
c1595e42 27165+ case Opt_acl:
2121bcd9 27166+ sb->s_flags |= SB_POSIXACL;
c1595e42
JR
27167+ break;
27168+ case Opt_noacl:
2121bcd9 27169+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
27170+ break;
27171+
1facf9fc 27172+ default:
27173+ err = 0;
27174+ break;
27175+ }
27176+
27177+ return err;
27178+}
27179+
27180+/*
27181+ * returns tri-state.
27182+ * plus: processed without an error
27183+ * zero: unprocessed
27184+ * minus: error
27185+ */
27186+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27187+ struct au_opts *opts)
27188+{
27189+ int err, do_refresh;
27190+
27191+ err = 0;
27192+ switch (opt->type) {
27193+ case Opt_append:
5afbbe0d 27194+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 27195+ if (opt->add.bindex < 0)
27196+ opt->add.bindex = 0;
27197+ goto add;
eca34b5c 27198+ /* Always goto add, not fallthrough */
1facf9fc 27199+ case Opt_prepend:
27200+ opt->add.bindex = 0;
eca34b5c 27201+ /* fallthrough */
f6b6e03d 27202+ add: /* indented label */
1facf9fc 27203+ case Opt_add:
27204+ err = au_br_add(sb, &opt->add,
27205+ au_ftest_opts(opts->flags, REMOUNT));
27206+ if (!err) {
27207+ err = 1;
027c5e7a 27208+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27209+ }
27210+ break;
27211+
27212+ case Opt_del:
27213+ case Opt_idel:
27214+ err = au_br_del(sb, &opt->del,
27215+ au_ftest_opts(opts->flags, REMOUNT));
27216+ if (!err) {
27217+ err = 1;
27218+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 27219+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27220+ }
27221+ break;
27222+
27223+ case Opt_mod:
27224+ case Opt_imod:
27225+ err = au_br_mod(sb, &opt->mod,
27226+ au_ftest_opts(opts->flags, REMOUNT),
27227+ &do_refresh);
27228+ if (!err) {
27229+ err = 1;
027c5e7a
AM
27230+ if (do_refresh)
27231+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27232+ }
27233+ break;
27234+ }
1facf9fc 27235+ return err;
27236+}
27237+
27238+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27239+ struct au_opt_xino **opt_xino,
27240+ struct au_opts *opts)
27241+{
27242+ int err;
1facf9fc 27243+
27244+ err = 0;
27245+ switch (opt->type) {
27246+ case Opt_xino:
062440b3
AM
27247+ err = au_xino_set(sb, &opt->xino,
27248+ !!au_ftest_opts(opts->flags, REMOUNT));
27249+ if (unlikely(err))
27250+ break;
27251+
27252+ *opt_xino = &opt->xino;
1facf9fc 27253+ break;
27254+
27255+ case Opt_noxino:
27256+ au_xino_clr(sb);
1facf9fc 27257+ *opt_xino = (void *)-1;
27258+ break;
27259+ }
27260+
27261+ return err;
27262+}
27263+
27264+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27265+ unsigned int pending)
27266+{
076b876e 27267+ int err, fhsm;
5afbbe0d 27268+ aufs_bindex_t bindex, bbot;
79b8bda9 27269+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 27270+ struct au_branch *br;
27271+ struct au_wbr *wbr;
79b8bda9 27272+ struct dentry *root, *dentry;
1facf9fc 27273+ struct inode *dir, *h_dir;
27274+ struct au_sbinfo *sbinfo;
27275+ struct au_hinode *hdir;
27276+
dece6358
AM
27277+ SiMustAnyLock(sb);
27278+
1facf9fc 27279+ sbinfo = au_sbi(sb);
27280+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27281+
2121bcd9 27282+ if (!(sb_flags & SB_RDONLY)) {
dece6358 27283+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 27284+ pr_warn("first branch should be rw\n");
dece6358 27285+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 27286+ pr_warn_once("shwh should be used with ro\n");
dece6358 27287+ }
1facf9fc 27288+
4a4d8108 27289+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 27290+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 27291+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 27292+
076b876e 27293+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
27294+ pr_warn_once("dirperm1 breaks the protection"
27295+ " by the permission bits on the lower branch\n");
076b876e 27296+
1facf9fc 27297+ err = 0;
076b876e 27298+ fhsm = 0;
1facf9fc 27299+ root = sb->s_root;
5527c038 27300+ dir = d_inode(root);
1facf9fc 27301+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
27302+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27303+ UDBA_NONE);
5afbbe0d
AM
27304+ bbot = au_sbbot(sb);
27305+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 27306+ skip = 0;
27307+ h_dir = au_h_iptr(dir, bindex);
27308+ br = au_sbr(sb, bindex);
1facf9fc 27309+
c1595e42
JR
27310+ if ((br->br_perm & AuBrAttr_ICEX)
27311+ && !h_dir->i_op->listxattr)
27312+ br->br_perm &= ~AuBrAttr_ICEX;
43982f53 27313+#if 0 /* untested */
c1595e42 27314+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 27315+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
27316+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27317+#endif
27318+
27319+ do_free = 0;
1facf9fc 27320+ wbr = br->br_wbr;
27321+ if (wbr)
27322+ wbr_wh_read_lock(wbr);
27323+
1e00d052 27324+ if (!au_br_writable(br->br_perm)) {
1facf9fc 27325+ do_free = !!wbr;
27326+ skip = (!wbr
27327+ || (!wbr->wbr_whbase
27328+ && !wbr->wbr_plink
27329+ && !wbr->wbr_orph));
1e00d052 27330+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 27331+ /* skip = (!br->br_whbase && !br->br_orph); */
27332+ skip = (!wbr || !wbr->wbr_whbase);
27333+ if (skip && wbr) {
27334+ if (do_plink)
27335+ skip = !!wbr->wbr_plink;
27336+ else
27337+ skip = !wbr->wbr_plink;
27338+ }
1e00d052 27339+ } else {
1facf9fc 27340+ /* skip = (br->br_whbase && br->br_ohph); */
27341+ skip = (wbr && wbr->wbr_whbase);
27342+ if (skip) {
27343+ if (do_plink)
27344+ skip = !!wbr->wbr_plink;
27345+ else
27346+ skip = !wbr->wbr_plink;
27347+ }
1facf9fc 27348+ }
27349+ if (wbr)
27350+ wbr_wh_read_unlock(wbr);
27351+
79b8bda9
AM
27352+ if (can_no_dreval) {
27353+ dentry = br->br_path.dentry;
27354+ spin_lock(&dentry->d_lock);
27355+ if (dentry->d_flags &
27356+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27357+ can_no_dreval = 0;
27358+ spin_unlock(&dentry->d_lock);
27359+ }
27360+
076b876e
AM
27361+ if (au_br_fhsm(br->br_perm)) {
27362+ fhsm++;
27363+ AuDebugOn(!br->br_fhsm);
27364+ }
27365+
1facf9fc 27366+ if (skip)
27367+ continue;
27368+
27369+ hdir = au_hi(dir, bindex);
5afbbe0d 27370+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27371+ if (wbr)
27372+ wbr_wh_write_lock(wbr);
86dc4139 27373+ err = au_wh_init(br, sb);
1facf9fc 27374+ if (wbr)
27375+ wbr_wh_write_unlock(wbr);
5afbbe0d 27376+ au_hn_inode_unlock(hdir);
1facf9fc 27377+
27378+ if (!err && do_free) {
9f237c51 27379+ au_kfree_rcu(wbr);
1facf9fc 27380+ br->br_wbr = NULL;
27381+ }
27382+ }
27383+
79b8bda9
AM
27384+ if (can_no_dreval)
27385+ au_fset_si(sbinfo, NO_DREVAL);
27386+ else
27387+ au_fclr_si(sbinfo, NO_DREVAL);
27388+
c1595e42 27389+ if (fhsm >= 2) {
076b876e 27390+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27391+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27392+ br = au_sbr(sb, bindex);
27393+ if (au_br_fhsm(br->br_perm)) {
27394+ au_fhsm_set_bottom(sb, bindex);
27395+ break;
27396+ }
27397+ }
27398+ } else {
076b876e 27399+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27400+ au_fhsm_set_bottom(sb, -1);
27401+ }
076b876e 27402+
1facf9fc 27403+ return err;
27404+}
27405+
27406+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27407+{
27408+ int err;
27409+ unsigned int tmp;
5afbbe0d 27410+ aufs_bindex_t bindex, bbot;
1facf9fc 27411+ struct au_opt *opt;
27412+ struct au_opt_xino *opt_xino, xino;
27413+ struct au_sbinfo *sbinfo;
027c5e7a 27414+ struct au_branch *br;
076b876e 27415+ struct inode *dir;
1facf9fc 27416+
dece6358
AM
27417+ SiMustWriteLock(sb);
27418+
1facf9fc 27419+ err = 0;
27420+ opt_xino = NULL;
27421+ opt = opts->opt;
27422+ while (err >= 0 && opt->type != Opt_tail)
27423+ err = au_opt_simple(sb, opt++, opts);
27424+ if (err > 0)
27425+ err = 0;
27426+ else if (unlikely(err < 0))
27427+ goto out;
27428+
27429+ /* disable xino and udba temporary */
27430+ sbinfo = au_sbi(sb);
27431+ tmp = sbinfo->si_mntflags;
27432+ au_opt_clr(sbinfo->si_mntflags, XINO);
27433+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27434+
27435+ opt = opts->opt;
27436+ while (err >= 0 && opt->type != Opt_tail)
27437+ err = au_opt_br(sb, opt++, opts);
27438+ if (err > 0)
27439+ err = 0;
27440+ else if (unlikely(err < 0))
27441+ goto out;
27442+
5afbbe0d
AM
27443+ bbot = au_sbbot(sb);
27444+ if (unlikely(bbot < 0)) {
1facf9fc 27445+ err = -EINVAL;
4a4d8108 27446+ pr_err("no branches\n");
1facf9fc 27447+ goto out;
27448+ }
27449+
27450+ if (au_opt_test(tmp, XINO))
27451+ au_opt_set(sbinfo->si_mntflags, XINO);
27452+ opt = opts->opt;
27453+ while (!err && opt->type != Opt_tail)
27454+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27455+ if (unlikely(err))
27456+ goto out;
27457+
27458+ err = au_opts_verify(sb, sb->s_flags, tmp);
27459+ if (unlikely(err))
27460+ goto out;
27461+
27462+ /* restore xino */
27463+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27464+ xino.file = au_xino_def(sb);
27465+ err = PTR_ERR(xino.file);
27466+ if (IS_ERR(xino.file))
27467+ goto out;
27468+
27469+ err = au_xino_set(sb, &xino, /*remount*/0);
27470+ fput(xino.file);
27471+ if (unlikely(err))
27472+ goto out;
27473+ }
27474+
27475+ /* restore udba */
027c5e7a 27476+ tmp &= AuOptMask_UDBA;
1facf9fc 27477+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27478+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27479+ bbot = au_sbbot(sb);
27480+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27481+ br = au_sbr(sb, bindex);
27482+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27483+ if (unlikely(err))
27484+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27485+ bindex, err);
27486+ /* go on even if err */
27487+ }
4a4d8108 27488+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27489+ dir = d_inode(sb->s_root);
4a4d8108 27490+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27491+ }
27492+
4f0767ce 27493+out:
1facf9fc 27494+ return err;
27495+}
27496+
27497+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27498+{
27499+ int err, rerr;
79b8bda9 27500+ unsigned char no_dreval;
1facf9fc 27501+ struct inode *dir;
27502+ struct au_opt_xino *opt_xino;
27503+ struct au_opt *opt;
27504+ struct au_sbinfo *sbinfo;
27505+
dece6358
AM
27506+ SiMustWriteLock(sb);
27507+
8b6a4947
AM
27508+ err = au_dr_opt_flush(sb);
27509+ if (unlikely(err))
27510+ goto out;
27511+ au_fset_opts(opts->flags, DR_FLUSHED);
27512+
5527c038 27513+ dir = d_inode(sb->s_root);
1facf9fc 27514+ sbinfo = au_sbi(sb);
1facf9fc 27515+ opt_xino = NULL;
27516+ opt = opts->opt;
27517+ while (err >= 0 && opt->type != Opt_tail) {
27518+ err = au_opt_simple(sb, opt, opts);
27519+ if (!err)
27520+ err = au_opt_br(sb, opt, opts);
27521+ if (!err)
27522+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27523+ opt++;
27524+ }
27525+ if (err > 0)
27526+ err = 0;
27527+ AuTraceErr(err);
27528+ /* go on even err */
27529+
79b8bda9 27530+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27531+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27532+ if (unlikely(rerr && !err))
27533+ err = rerr;
27534+
79b8bda9 27535+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27536+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27537+
1facf9fc 27538+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27539+ rerr = au_xib_trunc(sb);
27540+ if (unlikely(rerr && !err))
27541+ err = rerr;
27542+ }
27543+
27544+ /* will be handled by the caller */
027c5e7a 27545+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27546+ && (opts->given_udba
27547+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27548+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27549+ ))
027c5e7a 27550+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27551+
27552+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27553+
27554+out:
1facf9fc 27555+ return err;
27556+}
27557+
27558+/* ---------------------------------------------------------------------- */
27559+
27560+unsigned int au_opt_udba(struct super_block *sb)
27561+{
27562+ return au_mntflags(sb) & AuOptMask_UDBA;
27563+}
7f207e10 27564diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
eca34b5c 27565--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
016522bc 27566+++ linux/fs/aufs/opts.h 2020-01-27 10:57:18.175538316 +0100
062440b3
AM
27567@@ -0,0 +1,225 @@
27568+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27569+/*
016522bc 27570+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 27571+ *
27572+ * This program, aufs is free software; you can redistribute it and/or modify
27573+ * it under the terms of the GNU General Public License as published by
27574+ * the Free Software Foundation; either version 2 of the License, or
27575+ * (at your option) any later version.
dece6358
AM
27576+ *
27577+ * This program is distributed in the hope that it will be useful,
27578+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27579+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27580+ * GNU General Public License for more details.
27581+ *
27582+ * You should have received a copy of the GNU General Public License
523b37e3 27583+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27584+ */
27585+
27586+/*
27587+ * mount options/flags
27588+ */
27589+
27590+#ifndef __AUFS_OPTS_H__
27591+#define __AUFS_OPTS_H__
27592+
27593+#ifdef __KERNEL__
27594+
dece6358 27595+#include <linux/path.h>
1facf9fc 27596+
dece6358 27597+struct file;
dece6358 27598+
1facf9fc 27599+/* ---------------------------------------------------------------------- */
27600+
27601+/* mount flags */
27602+#define AuOpt_XINO 1 /* external inode number bitmap
27603+ and translation table */
27604+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27605+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27606+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27607+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27608+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27609+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27610+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27611+ bits */
dece6358
AM
27612+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27613+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27614+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27615+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
eca801bf 27616+#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
4a4d8108 27617+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27618+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27619+
4a4d8108
AM
27620+#ifndef CONFIG_AUFS_HNOTIFY
27621+#undef AuOpt_UDBA_HNOTIFY
27622+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27623+#endif
8b6a4947
AM
27624+#ifndef CONFIG_AUFS_DIRREN
27625+#undef AuOpt_DIRREN
27626+#define AuOpt_DIRREN 0
27627+#endif
dece6358
AM
27628+#ifndef CONFIG_AUFS_SHWH
27629+#undef AuOpt_SHWH
27630+#define AuOpt_SHWH 0
27631+#endif
1facf9fc 27632+
27633+#define AuOpt_Def (AuOpt_XINO \
27634+ | AuOpt_UDBA_REVAL \
27635+ | AuOpt_PLINK \
27636+ /* | AuOpt_DIRPERM1 */ \
27637+ | AuOpt_WARN_PERM)
27638+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27639+ | AuOpt_UDBA_REVAL \
4a4d8108 27640+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27641+
27642+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27643+#define au_opt_set(flags, name) do { \
27644+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27645+ ((flags) |= AuOpt_##name); \
27646+} while (0)
27647+#define au_opt_set_udba(flags, name) do { \
27648+ (flags) &= ~AuOptMask_UDBA; \
27649+ ((flags) |= AuOpt_##name); \
27650+} while (0)
7f207e10
AM
27651+#define au_opt_clr(flags, name) do { \
27652+ ((flags) &= ~AuOpt_##name); \
27653+} while (0)
1facf9fc 27654+
e49829fe
JR
27655+static inline unsigned int au_opts_plink(unsigned int mntflags)
27656+{
27657+#ifdef CONFIG_PROC_FS
27658+ return mntflags;
27659+#else
27660+ return mntflags & ~AuOpt_PLINK;
27661+#endif
27662+}
27663+
1facf9fc 27664+/* ---------------------------------------------------------------------- */
27665+
27666+/* policies to select one among multiple writable branches */
27667+enum {
27668+ AuWbrCreate_TDP, /* top down parent */
27669+ AuWbrCreate_RR, /* round robin */
27670+ AuWbrCreate_MFS, /* most free space */
27671+ AuWbrCreate_MFSV, /* mfs with seconds */
27672+ AuWbrCreate_MFSRR, /* mfs then rr */
27673+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27674+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27675+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27676+ AuWbrCreate_PMFS, /* parent and mfs */
27677+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27678+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27679+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27680+
27681+ AuWbrCreate_Def = AuWbrCreate_TDP
27682+};
27683+
27684+enum {
27685+ AuWbrCopyup_TDP, /* top down parent */
27686+ AuWbrCopyup_BUP, /* bottom up parent */
27687+ AuWbrCopyup_BU, /* bottom up */
27688+
27689+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27690+};
27691+
27692+/* ---------------------------------------------------------------------- */
27693+
27694+struct au_opt_add {
27695+ aufs_bindex_t bindex;
27696+ char *pathname;
27697+ int perm;
27698+ struct path path;
27699+};
27700+
27701+struct au_opt_del {
27702+ char *pathname;
27703+ struct path h_path;
27704+};
27705+
27706+struct au_opt_mod {
27707+ char *path;
27708+ int perm;
27709+ struct dentry *h_root;
27710+};
27711+
27712+struct au_opt_xino {
27713+ char *path;
27714+ struct file *file;
27715+};
27716+
27717+struct au_opt_xino_itrunc {
27718+ aufs_bindex_t bindex;
27719+};
27720+
27721+struct au_opt_wbr_create {
27722+ int wbr_create;
27723+ int mfs_second;
27724+ unsigned long long mfsrr_watermark;
27725+};
27726+
27727+struct au_opt {
27728+ int type;
27729+ union {
27730+ struct au_opt_xino xino;
27731+ struct au_opt_xino_itrunc xino_itrunc;
27732+ struct au_opt_add add;
27733+ struct au_opt_del del;
27734+ struct au_opt_mod mod;
27735+ int dirwh;
27736+ int rdcache;
27737+ unsigned int rdblk;
27738+ unsigned int rdhash;
27739+ int udba;
27740+ struct au_opt_wbr_create wbr_create;
27741+ int wbr_copyup;
076b876e 27742+ unsigned int fhsm_second;
1facf9fc 27743+ };
27744+};
27745+
27746+/* opts flags */
27747+#define AuOpts_REMOUNT 1
027c5e7a
AM
27748+#define AuOpts_REFRESH (1 << 1)
27749+#define AuOpts_TRUNC_XIB (1 << 2)
27750+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27751+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27752+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27753+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27754+#define au_fset_opts(flags, name) \
27755+ do { (flags) |= AuOpts_##name; } while (0)
27756+#define au_fclr_opts(flags, name) \
27757+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27758+
8b6a4947
AM
27759+#ifndef CONFIG_AUFS_DIRREN
27760+#undef AuOpts_DR_FLUSHED
27761+#define AuOpts_DR_FLUSHED 0
27762+#endif
27763+
1facf9fc 27764+struct au_opts {
27765+ struct au_opt *opt;
27766+ int max_opt;
27767+
27768+ unsigned int given_udba;
27769+ unsigned int flags;
27770+ unsigned long sb_flags;
27771+};
27772+
27773+/* ---------------------------------------------------------------------- */
27774+
7e9cd9fe 27775+/* opts.c */
076b876e 27776+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27777+const char *au_optstr_udba(int udba);
27778+const char *au_optstr_wbr_copyup(int wbr_copyup);
27779+const char *au_optstr_wbr_create(int wbr_create);
27780+
27781+void au_opts_free(struct au_opts *opts);
3c1bdaff 27782+struct super_block;
1facf9fc 27783+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27784+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27785+ unsigned int pending);
27786+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27787+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27788+
27789+unsigned int au_opt_udba(struct super_block *sb);
27790+
1facf9fc 27791+#endif /* __KERNEL__ */
27792+#endif /* __AUFS_OPTS_H__ */
7f207e10 27793diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
eca34b5c 27794--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
016522bc 27795+++ linux/fs/aufs/plink.c 2020-01-27 10:57:18.175538316 +0100
062440b3 27796@@ -0,0 +1,516 @@
cd7a4cd9 27797+// SPDX-License-Identifier: GPL-2.0
1facf9fc 27798+/*
016522bc 27799+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 27800+ *
27801+ * This program, aufs is free software; you can redistribute it and/or modify
27802+ * it under the terms of the GNU General Public License as published by
27803+ * the Free Software Foundation; either version 2 of the License, or
27804+ * (at your option) any later version.
dece6358
AM
27805+ *
27806+ * This program is distributed in the hope that it will be useful,
27807+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27808+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27809+ * GNU General Public License for more details.
27810+ *
27811+ * You should have received a copy of the GNU General Public License
523b37e3 27812+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27813+ */
27814+
27815+/*
27816+ * pseudo-link
27817+ */
27818+
27819+#include "aufs.h"
27820+
27821+/*
e49829fe 27822+ * the pseudo-link maintenance mode.
1facf9fc 27823+ * during a user process maintains the pseudo-links,
27824+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27825+ *
27826+ * Flags
27827+ * NOPLM:
27828+ * For entry functions which will handle plink, and i_mutex is already held
27829+ * in VFS.
27830+ * They cannot wait and should return an error at once.
27831+ * Callers has to check the error.
27832+ * NOPLMW:
27833+ * For entry functions which will handle plink, but i_mutex is not held
27834+ * in VFS.
27835+ * They can wait the plink maintenance mode to finish.
27836+ *
27837+ * They behave like F_SETLK and F_SETLKW.
27838+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27839+ */
e49829fe
JR
27840+
27841+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27842+{
e49829fe
JR
27843+ int err;
27844+ pid_t pid, ppid;
f0c0a007 27845+ struct task_struct *parent, *prev;
e49829fe 27846+ struct au_sbinfo *sbi;
dece6358
AM
27847+
27848+ SiMustAnyLock(sb);
27849+
e49829fe
JR
27850+ err = 0;
27851+ if (!au_opt_test(au_mntflags(sb), PLINK))
27852+ goto out;
27853+
27854+ sbi = au_sbi(sb);
27855+ pid = sbi->si_plink_maint_pid;
27856+ if (!pid || pid == current->pid)
27857+ goto out;
27858+
27859+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27860+ prev = NULL;
27861+ parent = current;
27862+ ppid = 0;
e49829fe 27863+ rcu_read_lock();
f0c0a007
AM
27864+ while (1) {
27865+ parent = rcu_dereference(parent->real_parent);
27866+ if (parent == prev)
27867+ break;
27868+ ppid = task_pid_vnr(parent);
27869+ if (pid == ppid) {
27870+ rcu_read_unlock();
27871+ goto out;
27872+ }
27873+ prev = parent;
27874+ }
e49829fe 27875+ rcu_read_unlock();
e49829fe
JR
27876+
27877+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27878+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27879+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27880+ while (sbi->si_plink_maint_pid) {
27881+ si_read_unlock(sb);
27882+ /* gave up wake_up_bit() */
27883+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27884+
27885+ if (au_ftest_lock(flags, FLUSH))
27886+ au_nwt_flush(&sbi->si_nowait);
27887+ si_noflush_read_lock(sb);
27888+ }
27889+ } else if (au_ftest_lock(flags, NOPLM)) {
27890+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27891+ err = -EAGAIN;
27892+ }
27893+
27894+out:
27895+ return err;
4a4d8108
AM
27896+}
27897+
e49829fe 27898+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 27899+{
4a4d8108 27900+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 27901+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 27902+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 27903+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
27904+}
27905+
e49829fe 27906+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
27907+{
27908+ int err;
4a4d8108
AM
27909+ struct au_sbinfo *sbinfo;
27910+
27911+ err = 0;
4a4d8108
AM
27912+ sbinfo = au_sbi(sb);
27913+ /* make sure i am the only one in this fs */
e49829fe
JR
27914+ si_write_lock(sb, AuLock_FLUSH);
27915+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27916+ spin_lock(&sbinfo->si_plink_maint_lock);
27917+ if (!sbinfo->si_plink_maint_pid)
27918+ sbinfo->si_plink_maint_pid = current->pid;
27919+ else
27920+ err = -EBUSY;
27921+ spin_unlock(&sbinfo->si_plink_maint_lock);
27922+ }
4a4d8108
AM
27923+ si_write_unlock(sb);
27924+
27925+ return err;
1facf9fc 27926+}
27927+
27928+/* ---------------------------------------------------------------------- */
27929+
1facf9fc 27930+#ifdef CONFIG_AUFS_DEBUG
27931+void au_plink_list(struct super_block *sb)
27932+{
86dc4139 27933+ int i;
1facf9fc 27934+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27935+ struct hlist_bl_head *hbl;
27936+ struct hlist_bl_node *pos;
5afbbe0d 27937+ struct au_icntnr *icntnr;
1facf9fc 27938+
dece6358
AM
27939+ SiMustAnyLock(sb);
27940+
1facf9fc 27941+ sbinfo = au_sbi(sb);
27942+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27943+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27944+
86dc4139 27945+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27946+ hbl = sbinfo->si_plink + i;
27947+ hlist_bl_lock(hbl);
27948+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27949+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 27950+ hlist_bl_unlock(hbl);
86dc4139 27951+ }
1facf9fc 27952+}
27953+#endif
27954+
27955+/* is the inode pseudo-linked? */
27956+int au_plink_test(struct inode *inode)
27957+{
86dc4139 27958+ int found, i;
1facf9fc 27959+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27960+ struct hlist_bl_head *hbl;
27961+ struct hlist_bl_node *pos;
5afbbe0d 27962+ struct au_icntnr *icntnr;
1facf9fc 27963+
27964+ sbinfo = au_sbi(inode->i_sb);
dece6358 27965+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 27966+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 27967+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 27968+
27969+ found = 0;
86dc4139 27970+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
27971+ hbl = sbinfo->si_plink + i;
27972+ hlist_bl_lock(hbl);
27973+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27974+ if (&icntnr->vfs_inode == inode) {
1facf9fc 27975+ found = 1;
27976+ break;
27977+ }
8b6a4947 27978+ hlist_bl_unlock(hbl);
1facf9fc 27979+ return found;
27980+}
27981+
27982+/* ---------------------------------------------------------------------- */
27983+
27984+/*
27985+ * generate a name for plink.
27986+ * the file will be stored under AUFS_WH_PLINKDIR.
27987+ */
27988+/* 20 is max digits length of ulong 64 */
27989+#define PLINK_NAME_LEN ((20 + 1) * 2)
27990+
27991+static int plink_name(char *name, int len, struct inode *inode,
27992+ aufs_bindex_t bindex)
27993+{
27994+ int rlen;
27995+ struct inode *h_inode;
27996+
27997+ h_inode = au_h_iptr(inode, bindex);
27998+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27999+ return rlen;
28000+}
28001+
7f207e10
AM
28002+struct au_do_plink_lkup_args {
28003+ struct dentry **errp;
28004+ struct qstr *tgtname;
28005+ struct dentry *h_parent;
28006+ struct au_branch *br;
28007+};
28008+
28009+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28010+ struct dentry *h_parent,
28011+ struct au_branch *br)
28012+{
28013+ struct dentry *h_dentry;
febd17d6 28014+ struct inode *h_inode;
7f207e10 28015+
febd17d6 28016+ h_inode = d_inode(h_parent);
be118d29 28017+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 28018+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 28019+ inode_unlock_shared(h_inode);
7f207e10
AM
28020+ return h_dentry;
28021+}
28022+
28023+static void au_call_do_plink_lkup(void *args)
28024+{
28025+ struct au_do_plink_lkup_args *a = args;
28026+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
28027+}
28028+
1facf9fc 28029+/* lookup the plink-ed @inode under the branch at @bindex */
28030+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28031+{
28032+ struct dentry *h_dentry, *h_parent;
28033+ struct au_branch *br;
7f207e10 28034+ int wkq_err;
1facf9fc 28035+ char a[PLINK_NAME_LEN];
0c3ec466 28036+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28037+
e49829fe
JR
28038+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28039+
1facf9fc 28040+ br = au_sbr(inode->i_sb, bindex);
28041+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 28042+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28043+
2dfbb274 28044+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
28045+ struct au_do_plink_lkup_args args = {
28046+ .errp = &h_dentry,
28047+ .tgtname = &tgtname,
28048+ .h_parent = h_parent,
28049+ .br = br
28050+ };
28051+
28052+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28053+ if (unlikely(wkq_err))
28054+ h_dentry = ERR_PTR(wkq_err);
28055+ } else
28056+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
28057+
1facf9fc 28058+ return h_dentry;
28059+}
28060+
28061+/* create a pseudo-link */
28062+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
28063+ struct dentry *h_dentry, struct au_branch *br)
28064+{
28065+ int err;
28066+ struct path h_path = {
86dc4139 28067+ .mnt = au_br_mnt(br)
1facf9fc 28068+ };
523b37e3 28069+ struct inode *h_dir, *delegated;
1facf9fc 28070+
5527c038 28071+ h_dir = d_inode(h_parent);
febd17d6 28072+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 28073+again:
b4510431 28074+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 28075+ err = PTR_ERR(h_path.dentry);
28076+ if (IS_ERR(h_path.dentry))
28077+ goto out;
28078+
28079+ err = 0;
28080+ /* wh.plink dir is not monitored */
7f207e10 28081+ /* todo: is it really safe? */
5527c038
JR
28082+ if (d_is_positive(h_path.dentry)
28083+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
28084+ delegated = NULL;
28085+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28086+ if (unlikely(err == -EWOULDBLOCK)) {
28087+ pr_warn("cannot retry for NFSv4 delegation"
28088+ " for an internal unlink\n");
28089+ iput(delegated);
28090+ }
1facf9fc 28091+ dput(h_path.dentry);
28092+ h_path.dentry = NULL;
28093+ if (!err)
28094+ goto again;
28095+ }
5527c038 28096+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
28097+ delegated = NULL;
28098+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28099+ if (unlikely(err == -EWOULDBLOCK)) {
28100+ pr_warn("cannot retry for NFSv4 delegation"
28101+ " for an internal link\n");
28102+ iput(delegated);
28103+ }
28104+ }
1facf9fc 28105+ dput(h_path.dentry);
28106+
4f0767ce 28107+out:
febd17d6 28108+ inode_unlock(h_dir);
1facf9fc 28109+ return err;
28110+}
28111+
28112+struct do_whplink_args {
28113+ int *errp;
28114+ struct qstr *tgt;
28115+ struct dentry *h_parent;
28116+ struct dentry *h_dentry;
28117+ struct au_branch *br;
28118+};
28119+
28120+static void call_do_whplink(void *args)
28121+{
28122+ struct do_whplink_args *a = args;
28123+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28124+}
28125+
28126+static int whplink(struct dentry *h_dentry, struct inode *inode,
28127+ aufs_bindex_t bindex, struct au_branch *br)
28128+{
28129+ int err, wkq_err;
28130+ struct au_wbr *wbr;
28131+ struct dentry *h_parent;
1facf9fc 28132+ char a[PLINK_NAME_LEN];
0c3ec466 28133+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28134+
28135+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28136+ h_parent = wbr->wbr_plink;
1facf9fc 28137+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28138+
28139+ /* always superio. */
2dfbb274 28140+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 28141+ struct do_whplink_args args = {
28142+ .errp = &err,
28143+ .tgt = &tgtname,
28144+ .h_parent = h_parent,
28145+ .h_dentry = h_dentry,
28146+ .br = br
28147+ };
28148+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28149+ if (unlikely(wkq_err))
28150+ err = wkq_err;
28151+ } else
28152+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 28153+
28154+ return err;
28155+}
28156+
1facf9fc 28157+/*
28158+ * create a new pseudo-link for @h_dentry on @bindex.
28159+ * the linked inode is held in aufs @inode.
28160+ */
28161+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28162+ struct dentry *h_dentry)
28163+{
28164+ struct super_block *sb;
28165+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28166+ struct hlist_bl_head *hbl;
28167+ struct hlist_bl_node *pos;
5afbbe0d 28168+ struct au_icntnr *icntnr;
86dc4139 28169+ int found, err, cnt, i;
1facf9fc 28170+
28171+ sb = inode->i_sb;
28172+ sbinfo = au_sbi(sb);
28173+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28174+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28175+
86dc4139 28176+ found = au_plink_test(inode);
4a4d8108 28177+ if (found)
1facf9fc 28178+ return;
4a4d8108 28179+
86dc4139 28180+ i = au_plink_hash(inode->i_ino);
8b6a4947 28181+ hbl = sbinfo->si_plink + i;
5afbbe0d 28182+ au_igrab(inode);
1facf9fc 28183+
8b6a4947
AM
28184+ hlist_bl_lock(hbl);
28185+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 28186+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
28187+ found = 1;
28188+ break;
28189+ }
1facf9fc 28190+ }
5afbbe0d
AM
28191+ if (!found) {
28192+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 28193+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 28194+ }
8b6a4947 28195+ hlist_bl_unlock(hbl);
4a4d8108 28196+ if (!found) {
8b6a4947 28197+ cnt = au_hbl_count(hbl);
acd2b654 28198+#define msg "unexpectedly unbalanced or too many pseudo-links"
86dc4139
AM
28199+ if (cnt > AUFS_PLINK_WARN)
28200+ AuWarn1(msg ", %d\n", cnt);
28201+#undef msg
1facf9fc 28202+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
28203+ if (unlikely(err)) {
28204+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 28205+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 28206+ iput(&icntnr->vfs_inode);
4a4d8108 28207+ }
5afbbe0d
AM
28208+ } else
28209+ iput(&icntnr->vfs_inode);
1facf9fc 28210+}
28211+
28212+/* free all plinks */
e49829fe 28213+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 28214+{
86dc4139 28215+ int i, warned;
1facf9fc 28216+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28217+ struct hlist_bl_head *hbl;
28218+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28219+ struct au_icntnr *icntnr;
1facf9fc 28220+
dece6358
AM
28221+ SiMustWriteLock(sb);
28222+
1facf9fc 28223+ sbinfo = au_sbi(sb);
28224+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28225+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28226+
1facf9fc 28227+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
28228+ warned = 0;
28229+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28230+ hbl = sbinfo->si_plink + i;
28231+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
28232+ pr_warn("pseudo-link is not flushed");
28233+ warned = 1;
28234+ }
8b6a4947 28235+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 28236+ iput(&icntnr->vfs_inode);
8b6a4947 28237+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 28238+ }
1facf9fc 28239+}
28240+
e49829fe
JR
28241+void au_plink_clean(struct super_block *sb, int verbose)
28242+{
28243+ struct dentry *root;
28244+
28245+ root = sb->s_root;
28246+ aufs_write_lock(root);
28247+ if (au_opt_test(au_mntflags(sb), PLINK))
28248+ au_plink_put(sb, verbose);
28249+ aufs_write_unlock(root);
28250+}
28251+
86dc4139
AM
28252+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28253+{
28254+ int do_put;
5afbbe0d 28255+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
28256+
28257+ do_put = 0;
5afbbe0d
AM
28258+ btop = au_ibtop(inode);
28259+ bbot = au_ibbot(inode);
28260+ if (btop >= 0) {
28261+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
28262+ if (!au_h_iptr(inode, bindex)
28263+ || au_ii_br_id(inode, bindex) != br_id)
28264+ continue;
28265+ au_set_h_iptr(inode, bindex, NULL, 0);
28266+ do_put = 1;
28267+ break;
28268+ }
28269+ if (do_put)
5afbbe0d 28270+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
28271+ if (au_h_iptr(inode, bindex)) {
28272+ do_put = 0;
28273+ break;
28274+ }
28275+ } else
28276+ do_put = 1;
28277+
28278+ return do_put;
28279+}
28280+
1facf9fc 28281+/* free the plinks on a branch specified by @br_id */
28282+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28283+{
28284+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28285+ struct hlist_bl_head *hbl;
28286+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28287+ struct au_icntnr *icntnr;
1facf9fc 28288+ struct inode *inode;
86dc4139 28289+ int i, do_put;
1facf9fc 28290+
dece6358
AM
28291+ SiMustWriteLock(sb);
28292+
1facf9fc 28293+ sbinfo = au_sbi(sb);
28294+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28295+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28296+
8b6a4947 28297+ /* no bit_lock since sbinfo is write-locked */
86dc4139 28298+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28299+ hbl = sbinfo->si_plink + i;
28300+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 28301+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
28302+ ii_write_lock_child(inode);
28303+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 28304+ if (do_put) {
8b6a4947 28305+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
28306+ iput(inode);
28307+ }
86dc4139
AM
28308+ ii_write_unlock(inode);
28309+ iput(inode);
dece6358 28310+ }
dece6358
AM
28311+ }
28312+}
7f207e10 28313diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
eca34b5c 28314--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
016522bc 28315+++ linux/fs/aufs/poll.c 2020-01-27 10:57:18.175538316 +0100
cd7a4cd9
AM
28316@@ -0,0 +1,51 @@
28317+// SPDX-License-Identifier: GPL-2.0
dece6358 28318+/*
016522bc 28319+ * Copyright (C) 2005-2020 Junjiro R. Okajima
dece6358
AM
28320+ *
28321+ * This program, aufs is free software; you can redistribute it and/or modify
28322+ * it under the terms of the GNU General Public License as published by
28323+ * the Free Software Foundation; either version 2 of the License, or
28324+ * (at your option) any later version.
28325+ *
28326+ * This program is distributed in the hope that it will be useful,
28327+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28328+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28329+ * GNU General Public License for more details.
28330+ *
28331+ * You should have received a copy of the GNU General Public License
523b37e3 28332+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
28333+ */
28334+
1308ab2a 28335+/*
28336+ * poll operation
28337+ * There is only one filesystem which implements ->poll operation, currently.
28338+ */
28339+
28340+#include "aufs.h"
28341+
cd7a4cd9 28342+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
1308ab2a 28343+{
be118d29 28344+ __poll_t mask;
1308ab2a 28345+ struct file *h_file;
1308ab2a 28346+ struct super_block *sb;
28347+
28348+ /* We should pretend an error happened. */
be118d29 28349+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 28350+ sb = file->f_path.dentry->d_sb;
e49829fe 28351+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 28352+
521ced18 28353+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
062440b3
AM
28354+ if (IS_ERR(h_file)) {
28355+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
1308ab2a 28356+ goto out;
062440b3 28357+ }
1308ab2a 28358+
cd7a4cd9 28359+ mask = vfs_poll(h_file, pt);
b912730e 28360+ fput(h_file); /* instead of au_read_post() */
1308ab2a 28361+
4f0767ce 28362+out:
1308ab2a 28363+ si_read_unlock(sb);
062440b3 28364+ if (mask & EPOLLERR)
b00004a5 28365+ AuDbg("mask 0x%x\n", mask);
1308ab2a 28366+ return mask;
28367+}
c1595e42 28368diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
eca34b5c 28369--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
016522bc 28370+++ linux/fs/aufs/posix_acl.c 2020-01-27 10:57:18.175538316 +0100
9f00928d 28371@@ -0,0 +1,105 @@
cd7a4cd9 28372+// SPDX-License-Identifier: GPL-2.0
c1595e42 28373+/*
016522bc 28374+ * Copyright (C) 2014-2020 Junjiro R. Okajima
c1595e42
JR
28375+ *
28376+ * This program, aufs is free software; you can redistribute it and/or modify
28377+ * it under the terms of the GNU General Public License as published by
28378+ * the Free Software Foundation; either version 2 of the License, or
28379+ * (at your option) any later version.
28380+ *
28381+ * This program is distributed in the hope that it will be useful,
28382+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28383+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28384+ * GNU General Public License for more details.
28385+ *
28386+ * You should have received a copy of the GNU General Public License
28387+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28388+ */
28389+
28390+/*
28391+ * posix acl operations
28392+ */
28393+
28394+#include <linux/fs.h>
c1595e42
JR
28395+#include "aufs.h"
28396+
28397+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28398+{
28399+ struct posix_acl *acl;
28400+ int err;
28401+ aufs_bindex_t bindex;
28402+ struct inode *h_inode;
28403+ struct super_block *sb;
28404+
28405+ acl = NULL;
28406+ sb = inode->i_sb;
28407+ si_read_lock(sb, AuLock_FLUSH);
28408+ ii_read_lock_child(inode);
2121bcd9 28409+ if (!(sb->s_flags & SB_POSIXACL))
c1595e42
JR
28410+ goto out;
28411+
5afbbe0d 28412+ bindex = au_ibtop(inode);
c1595e42
JR
28413+ h_inode = au_h_iptr(inode, bindex);
28414+ if (unlikely(!h_inode
28415+ || ((h_inode->i_mode & S_IFMT)
28416+ != (inode->i_mode & S_IFMT)))) {
28417+ err = au_busy_or_stale();
28418+ acl = ERR_PTR(err);
28419+ goto out;
28420+ }
28421+
28422+ /* always topmost only */
28423+ acl = get_acl(h_inode, type);
9f00928d
AM
28424+ if (IS_ERR(acl))
28425+ forget_cached_acl(inode, type);
28426+ else
a2654f78 28427+ set_cached_acl(inode, type, acl);
c1595e42
JR
28428+
28429+out:
28430+ ii_read_unlock(inode);
28431+ si_read_unlock(sb);
28432+
28433+ AuTraceErrPtr(acl);
28434+ return acl;
28435+}
28436+
28437+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28438+{
28439+ int err;
28440+ ssize_t ssz;
28441+ struct dentry *dentry;
f2c43d5f 28442+ struct au_sxattr arg = {
c1595e42
JR
28443+ .type = AU_ACL_SET,
28444+ .u.acl_set = {
28445+ .acl = acl,
28446+ .type = type
28447+ },
28448+ };
28449+
5afbbe0d
AM
28450+ IMustLock(inode);
28451+
c1595e42
JR
28452+ if (inode->i_ino == AUFS_ROOT_INO)
28453+ dentry = dget(inode->i_sb->s_root);
28454+ else {
28455+ dentry = d_find_alias(inode);
28456+ if (!dentry)
28457+ dentry = d_find_any_alias(inode);
28458+ if (!dentry) {
28459+ pr_warn("cannot handle this inode, "
28460+ "please report to aufs-users ML\n");
28461+ err = -ENOENT;
28462+ goto out;
28463+ }
28464+ }
28465+
f2c43d5f 28466+ ssz = au_sxattr(dentry, inode, &arg);
9f00928d
AM
28467+ /* forget even it if succeeds since the branch might set differently */
28468+ forget_cached_acl(inode, type);
c1595e42
JR
28469+ dput(dentry);
28470+ err = ssz;
9f00928d 28471+ if (ssz >= 0)
c1595e42
JR
28472+ err = 0;
28473+
28474+out:
c1595e42
JR
28475+ return err;
28476+}
7f207e10 28477diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
eca34b5c 28478--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
016522bc 28479+++ linux/fs/aufs/procfs.c 2020-04-03 08:16:47.547461775 +0200
062440b3 28480@@ -0,0 +1,171 @@
cd7a4cd9 28481+// SPDX-License-Identifier: GPL-2.0
e49829fe 28482+/*
016522bc 28483+ * Copyright (C) 2010-2020 Junjiro R. Okajima
e49829fe
JR
28484+ *
28485+ * This program, aufs is free software; you can redistribute it and/or modify
28486+ * it under the terms of the GNU General Public License as published by
28487+ * the Free Software Foundation; either version 2 of the License, or
28488+ * (at your option) any later version.
28489+ *
28490+ * This program is distributed in the hope that it will be useful,
28491+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28492+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28493+ * GNU General Public License for more details.
28494+ *
28495+ * You should have received a copy of the GNU General Public License
523b37e3 28496+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28497+ */
28498+
28499+/*
28500+ * procfs interfaces
28501+ */
28502+
28503+#include <linux/proc_fs.h>
28504+#include "aufs.h"
28505+
28506+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28507+{
28508+ struct au_sbinfo *sbinfo;
28509+
28510+ sbinfo = file->private_data;
28511+ if (sbinfo) {
28512+ au_plink_maint_leave(sbinfo);
28513+ kobject_put(&sbinfo->si_kobj);
28514+ }
28515+
28516+ return 0;
28517+}
28518+
28519+static void au_procfs_plm_write_clean(struct file *file)
28520+{
28521+ struct au_sbinfo *sbinfo;
28522+
28523+ sbinfo = file->private_data;
28524+ if (sbinfo)
28525+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28526+}
28527+
28528+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28529+{
28530+ int err;
28531+ struct super_block *sb;
28532+ struct au_sbinfo *sbinfo;
8b6a4947 28533+ struct hlist_bl_node *pos;
e49829fe
JR
28534+
28535+ err = -EBUSY;
28536+ if (unlikely(file->private_data))
28537+ goto out;
28538+
28539+ sb = NULL;
53392da6 28540+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28541+ hlist_bl_lock(&au_sbilist);
28542+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe 28543+ if (id == sysaufs_si_id(sbinfo)) {
83b672a5
AM
28544+ if (kobject_get_unless_zero(&sbinfo->si_kobj))
28545+ sb = sbinfo->si_sb;
e49829fe
JR
28546+ break;
28547+ }
8b6a4947 28548+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28549+
28550+ err = -EINVAL;
28551+ if (unlikely(!sb))
28552+ goto out;
28553+
28554+ err = au_plink_maint_enter(sb);
28555+ if (!err)
28556+ /* keep kobject_get() */
28557+ file->private_data = sbinfo;
28558+ else
28559+ kobject_put(&sbinfo->si_kobj);
28560+out:
28561+ return err;
28562+}
28563+
28564+/*
28565+ * Accept a valid "si=xxxx" only.
28566+ * Once it is accepted successfully, accept "clean" too.
28567+ */
28568+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28569+ size_t count, loff_t *ppos)
28570+{
28571+ ssize_t err;
28572+ unsigned long id;
28573+ /* last newline is allowed */
28574+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28575+
28576+ err = -EACCES;
28577+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28578+ goto out;
28579+
28580+ err = -EINVAL;
28581+ if (unlikely(count > sizeof(buf)))
28582+ goto out;
28583+
28584+ err = copy_from_user(buf, ubuf, count);
28585+ if (unlikely(err)) {
28586+ err = -EFAULT;
28587+ goto out;
28588+ }
28589+ buf[count] = 0;
28590+
28591+ err = -EINVAL;
28592+ if (!strcmp("clean", buf)) {
28593+ au_procfs_plm_write_clean(file);
28594+ goto out_success;
28595+ } else if (unlikely(strncmp("si=", buf, 3)))
28596+ goto out;
28597+
9dbd164d 28598+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28599+ if (unlikely(err))
28600+ goto out;
28601+
28602+ err = au_procfs_plm_write_si(file, id);
28603+ if (unlikely(err))
28604+ goto out;
28605+
28606+out_success:
28607+ err = count; /* success */
28608+out:
28609+ return err;
28610+}
28611+
28612+static const struct file_operations au_procfs_plm_fop = {
28613+ .write = au_procfs_plm_write,
28614+ .release = au_procfs_plm_release,
28615+ .owner = THIS_MODULE
28616+};
28617+
28618+/* ---------------------------------------------------------------------- */
28619+
28620+static struct proc_dir_entry *au_procfs_dir;
28621+
28622+void au_procfs_fin(void)
28623+{
28624+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28625+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28626+}
28627+
28628+int __init au_procfs_init(void)
28629+{
28630+ int err;
28631+ struct proc_dir_entry *entry;
28632+
28633+ err = -ENOMEM;
28634+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28635+ if (unlikely(!au_procfs_dir))
28636+ goto out;
28637+
cd7a4cd9 28638+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
e49829fe
JR
28639+ au_procfs_dir, &au_procfs_plm_fop);
28640+ if (unlikely(!entry))
28641+ goto out_dir;
28642+
28643+ err = 0;
28644+ goto out; /* success */
28645+
28646+
28647+out_dir:
28648+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28649+out:
28650+ return err;
28651+}
7f207e10 28652diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
eca34b5c 28653--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
016522bc 28654+++ linux/fs/aufs/rdu.c 2020-01-27 10:57:18.178871751 +0100
ba1aed25 28655@@ -0,0 +1,384 @@
cd7a4cd9 28656+// SPDX-License-Identifier: GPL-2.0
1308ab2a 28657+/*
016522bc 28658+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1308ab2a 28659+ *
28660+ * This program, aufs is free software; you can redistribute it and/or modify
28661+ * it under the terms of the GNU General Public License as published by
28662+ * the Free Software Foundation; either version 2 of the License, or
28663+ * (at your option) any later version.
28664+ *
28665+ * This program is distributed in the hope that it will be useful,
28666+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28667+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28668+ * GNU General Public License for more details.
28669+ *
28670+ * You should have received a copy of the GNU General Public License
523b37e3 28671+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28672+ */
28673+
28674+/*
28675+ * readdir in userspace.
28676+ */
28677+
b752ccd1 28678+#include <linux/compat.h>
4a4d8108 28679+#include <linux/fs_stack.h>
1308ab2a 28680+#include <linux/security.h>
1308ab2a 28681+#include "aufs.h"
28682+
28683+/* bits for struct aufs_rdu.flags */
28684+#define AuRdu_CALLED 1
28685+#define AuRdu_CONT (1 << 1)
28686+#define AuRdu_FULL (1 << 2)
28687+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28688+#define au_fset_rdu(flags, name) \
28689+ do { (flags) |= AuRdu_##name; } while (0)
28690+#define au_fclr_rdu(flags, name) \
28691+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28692+
28693+struct au_rdu_arg {
392086de 28694+ struct dir_context ctx;
1308ab2a 28695+ struct aufs_rdu *rdu;
28696+ union au_rdu_ent_ul ent;
28697+ unsigned long end;
28698+
28699+ struct super_block *sb;
28700+ int err;
28701+};
28702+
392086de 28703+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28704+ loff_t offset, u64 h_ino, unsigned int d_type)
28705+{
28706+ int err, len;
392086de 28707+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28708+ struct aufs_rdu *rdu = arg->rdu;
28709+ struct au_rdu_ent ent;
28710+
28711+ err = 0;
28712+ arg->err = 0;
28713+ au_fset_rdu(rdu->cookie.flags, CALLED);
28714+ len = au_rdu_len(nlen);
28715+ if (arg->ent.ul + len < arg->end) {
28716+ ent.ino = h_ino;
28717+ ent.bindex = rdu->cookie.bindex;
28718+ ent.type = d_type;
28719+ ent.nlen = nlen;
4a4d8108
AM
28720+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28721+ ent.type = DT_UNKNOWN;
1308ab2a 28722+
9dbd164d 28723+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28724+ err = -EFAULT;
28725+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28726+ goto out;
28727+ if (copy_to_user(arg->ent.e->name, name, nlen))
28728+ goto out;
28729+ /* the terminating NULL */
28730+ if (__put_user(0, arg->ent.e->name + nlen))
28731+ goto out;
28732+ err = 0;
28733+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28734+ arg->ent.ul += len;
28735+ rdu->rent++;
28736+ } else {
28737+ err = -EFAULT;
28738+ au_fset_rdu(rdu->cookie.flags, FULL);
28739+ rdu->full = 1;
28740+ rdu->tail = arg->ent;
28741+ }
28742+
4f0767ce 28743+out:
1308ab2a 28744+ /* AuTraceErr(err); */
28745+ return err;
28746+}
28747+
28748+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28749+{
28750+ int err;
28751+ loff_t offset;
28752+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28753+
92d182d2 28754+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28755+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28756+ err = offset;
28757+ if (unlikely(offset != cookie->h_pos))
28758+ goto out;
28759+
28760+ err = 0;
28761+ do {
28762+ arg->err = 0;
28763+ au_fclr_rdu(cookie->flags, CALLED);
28764+ /* smp_mb(); */
392086de 28765+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28766+ if (err >= 0)
28767+ err = arg->err;
28768+ } while (!err
28769+ && au_ftest_rdu(cookie->flags, CALLED)
28770+ && !au_ftest_rdu(cookie->flags, FULL));
28771+ cookie->h_pos = h_file->f_pos;
28772+
4f0767ce 28773+out:
1308ab2a 28774+ AuTraceErr(err);
28775+ return err;
28776+}
28777+
28778+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28779+{
28780+ int err;
5afbbe0d 28781+ aufs_bindex_t bbot;
392086de
AM
28782+ struct au_rdu_arg arg = {
28783+ .ctx = {
2000de60 28784+ .actor = au_rdu_fill
392086de
AM
28785+ }
28786+ };
1308ab2a 28787+ struct dentry *dentry;
28788+ struct inode *inode;
28789+ struct file *h_file;
28790+ struct au_rdu_cookie *cookie = &rdu->cookie;
28791+
ba1aed25
AM
28792+ /* VERIFY_WRITE */
28793+ err = !access_ok(rdu->ent.e, rdu->sz);
1308ab2a 28794+ if (unlikely(err)) {
28795+ err = -EFAULT;
28796+ AuTraceErr(err);
28797+ goto out;
28798+ }
28799+ rdu->rent = 0;
28800+ rdu->tail = rdu->ent;
28801+ rdu->full = 0;
28802+ arg.rdu = rdu;
28803+ arg.ent = rdu->ent;
28804+ arg.end = arg.ent.ul;
28805+ arg.end += rdu->sz;
28806+
28807+ err = -ENOTDIR;
5afbbe0d 28808+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28809+ goto out;
28810+
28811+ err = security_file_permission(file, MAY_READ);
28812+ AuTraceErr(err);
28813+ if (unlikely(err))
28814+ goto out;
28815+
2000de60 28816+ dentry = file->f_path.dentry;
5527c038 28817+ inode = d_inode(dentry);
5afbbe0d 28818+ inode_lock_shared(inode);
1308ab2a 28819+
28820+ arg.sb = inode->i_sb;
e49829fe
JR
28821+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28822+ if (unlikely(err))
28823+ goto out_mtx;
027c5e7a
AM
28824+ err = au_alive_dir(dentry);
28825+ if (unlikely(err))
28826+ goto out_si;
e49829fe 28827+ /* todo: reval? */
1308ab2a 28828+ fi_read_lock(file);
28829+
28830+ err = -EAGAIN;
28831+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28832+ && cookie->generation != au_figen(file)))
28833+ goto out_unlock;
28834+
28835+ err = 0;
28836+ if (!rdu->blk) {
28837+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28838+ if (!rdu->blk)
28839+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28840+ }
5afbbe0d
AM
28841+ bbot = au_fbtop(file);
28842+ if (cookie->bindex < bbot)
28843+ cookie->bindex = bbot;
28844+ bbot = au_fbbot_dir(file);
28845+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28846+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28847+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28848+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28849+ if (!h_file)
28850+ continue;
28851+
28852+ au_fclr_rdu(cookie->flags, FULL);
28853+ err = au_rdu_do(h_file, &arg);
28854+ AuTraceErr(err);
28855+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28856+ break;
28857+ }
28858+ AuDbg("rent %llu\n", rdu->rent);
28859+
28860+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28861+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28862+ au_fset_rdu(cookie->flags, CONT);
28863+ cookie->generation = au_figen(file);
28864+ }
28865+
28866+ ii_read_lock_child(inode);
5afbbe0d 28867+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28868+ ii_read_unlock(inode);
28869+
4f0767ce 28870+out_unlock:
1308ab2a 28871+ fi_read_unlock(file);
027c5e7a 28872+out_si:
1308ab2a 28873+ si_read_unlock(arg.sb);
4f0767ce 28874+out_mtx:
5afbbe0d 28875+ inode_unlock_shared(inode);
4f0767ce 28876+out:
1308ab2a 28877+ AuTraceErr(err);
28878+ return err;
28879+}
28880+
28881+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28882+{
28883+ int err;
28884+ ino_t ino;
28885+ unsigned long long nent;
28886+ union au_rdu_ent_ul *u;
28887+ struct au_rdu_ent ent;
28888+ struct super_block *sb;
28889+
28890+ err = 0;
28891+ nent = rdu->nent;
28892+ u = &rdu->ent;
2000de60 28893+ sb = file->f_path.dentry->d_sb;
1308ab2a 28894+ si_read_lock(sb, AuLock_FLUSH);
28895+ while (nent-- > 0) {
9dbd164d 28896+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28897+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108 28898+ if (!err)
ba1aed25
AM
28899+ /* VERIFY_WRITE */
28900+ err = !access_ok(&u->e->ino, sizeof(ino));
1308ab2a 28901+ if (unlikely(err)) {
28902+ err = -EFAULT;
28903+ AuTraceErr(err);
28904+ break;
28905+ }
28906+
28907+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28908+ if (!ent.wh)
28909+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28910+ else
28911+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28912+ &ino);
28913+ if (unlikely(err)) {
28914+ AuTraceErr(err);
28915+ break;
28916+ }
28917+
28918+ err = __put_user(ino, &u->e->ino);
28919+ if (unlikely(err)) {
28920+ err = -EFAULT;
28921+ AuTraceErr(err);
28922+ break;
28923+ }
28924+ u->ul += au_rdu_len(ent.nlen);
28925+ }
28926+ si_read_unlock(sb);
28927+
28928+ return err;
28929+}
28930+
28931+/* ---------------------------------------------------------------------- */
28932+
28933+static int au_rdu_verify(struct aufs_rdu *rdu)
28934+{
b752ccd1 28935+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 28936+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 28937+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 28938+ rdu->blk,
28939+ rdu->rent, rdu->shwh, rdu->full,
28940+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28941+ rdu->cookie.generation);
dece6358 28942+
b752ccd1 28943+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 28944+ return 0;
dece6358 28945+
b752ccd1
AM
28946+ AuDbg("%u:%u\n",
28947+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 28948+ return -EINVAL;
28949+}
28950+
28951+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 28952+{
1308ab2a 28953+ long err, e;
28954+ struct aufs_rdu rdu;
28955+ void __user *p = (void __user *)arg;
dece6358 28956+
1308ab2a 28957+ err = copy_from_user(&rdu, p, sizeof(rdu));
28958+ if (unlikely(err)) {
28959+ err = -EFAULT;
28960+ AuTraceErr(err);
28961+ goto out;
28962+ }
28963+ err = au_rdu_verify(&rdu);
dece6358
AM
28964+ if (unlikely(err))
28965+ goto out;
28966+
1308ab2a 28967+ switch (cmd) {
28968+ case AUFS_CTL_RDU:
28969+ err = au_rdu(file, &rdu);
28970+ if (unlikely(err))
28971+ break;
dece6358 28972+
1308ab2a 28973+ e = copy_to_user(p, &rdu, sizeof(rdu));
28974+ if (unlikely(e)) {
28975+ err = -EFAULT;
28976+ AuTraceErr(err);
28977+ }
28978+ break;
28979+ case AUFS_CTL_RDU_INO:
28980+ err = au_rdu_ino(file, &rdu);
28981+ break;
28982+
28983+ default:
4a4d8108 28984+ /* err = -ENOTTY; */
1308ab2a 28985+ err = -EINVAL;
28986+ }
dece6358 28987+
4f0767ce 28988+out:
1308ab2a 28989+ AuTraceErr(err);
28990+ return err;
1facf9fc 28991+}
b752ccd1
AM
28992+
28993+#ifdef CONFIG_COMPAT
28994+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28995+{
28996+ long err, e;
28997+ struct aufs_rdu rdu;
28998+ void __user *p = compat_ptr(arg);
28999+
29000+ /* todo: get_user()? */
29001+ err = copy_from_user(&rdu, p, sizeof(rdu));
29002+ if (unlikely(err)) {
29003+ err = -EFAULT;
29004+ AuTraceErr(err);
29005+ goto out;
29006+ }
29007+ rdu.ent.e = compat_ptr(rdu.ent.ul);
29008+ err = au_rdu_verify(&rdu);
29009+ if (unlikely(err))
29010+ goto out;
29011+
29012+ switch (cmd) {
29013+ case AUFS_CTL_RDU:
29014+ err = au_rdu(file, &rdu);
29015+ if (unlikely(err))
29016+ break;
29017+
29018+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29019+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29020+ e = copy_to_user(p, &rdu, sizeof(rdu));
29021+ if (unlikely(e)) {
29022+ err = -EFAULT;
29023+ AuTraceErr(err);
29024+ }
29025+ break;
29026+ case AUFS_CTL_RDU_INO:
29027+ err = au_rdu_ino(file, &rdu);
29028+ break;
29029+
29030+ default:
29031+ /* err = -ENOTTY; */
29032+ err = -EINVAL;
29033+ }
29034+
4f0767ce 29035+out:
b752ccd1
AM
29036+ AuTraceErr(err);
29037+ return err;
29038+}
29039+#endif
7f207e10 29040diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
eca34b5c 29041--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
016522bc 29042+++ linux/fs/aufs/rwsem.h 2020-01-27 10:57:18.178871751 +0100
062440b3
AM
29043@@ -0,0 +1,73 @@
29044+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 29045+/*
016522bc 29046+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 29047+ *
29048+ * This program, aufs is free software; you can redistribute it and/or modify
29049+ * it under the terms of the GNU General Public License as published by
29050+ * the Free Software Foundation; either version 2 of the License, or
29051+ * (at your option) any later version.
dece6358
AM
29052+ *
29053+ * This program is distributed in the hope that it will be useful,
29054+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29055+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29056+ * GNU General Public License for more details.
29057+ *
29058+ * You should have received a copy of the GNU General Public License
523b37e3 29059+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29060+ */
29061+
29062+/*
29063+ * simple read-write semaphore wrappers
29064+ */
29065+
29066+#ifndef __AUFS_RWSEM_H__
29067+#define __AUFS_RWSEM_H__
29068+
29069+#ifdef __KERNEL__
29070+
4a4d8108 29071+#include "debug.h"
dece6358 29072+
acd2b654 29073+/* in the future, the name 'au_rwsem' will be totally gone */
8b6a4947 29074+#define au_rwsem rw_semaphore
dece6358
AM
29075+
29076+/* to debug easier, do not make them inlined functions */
8b6a4947 29077+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
dece6358 29078+/* rwsem_is_locked() is unusable */
8b6a4947
AM
29079+#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
29080+ && debug_locks \
29081+ && !lockdep_is_held_type(rw, 1))
29082+#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
29083+ && debug_locks \
29084+ && !lockdep_is_held_type(rw, 0))
29085+#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
29086+ && debug_locks \
29087+ && !lockdep_is_held(rw))
29088+#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
29089+ && debug_locks \
29090+ && lockdep_is_held(rw))
29091+
29092+#define au_rw_init(rw) init_rwsem(rw)
dece6358 29093+
5afbbe0d
AM
29094+#define au_rw_init_wlock(rw) do { \
29095+ au_rw_init(rw); \
8b6a4947 29096+ down_write(rw); \
5afbbe0d 29097+ } while (0)
dece6358 29098+
8b6a4947
AM
29099+#define au_rw_init_wlock_nested(rw, lsc) do { \
29100+ au_rw_init(rw); \
29101+ down_write_nested(rw, lsc); \
5afbbe0d 29102+ } while (0)
dece6358 29103+
8b6a4947
AM
29104+#define au_rw_read_lock(rw) down_read(rw)
29105+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
29106+#define au_rw_read_unlock(rw) up_read(rw)
29107+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
29108+#define au_rw_write_lock(rw) down_write(rw)
29109+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29110+#define au_rw_write_unlock(rw) up_write(rw)
29111+/* why is not _nested version defined? */
29112+#define au_rw_read_trylock(rw) down_read_trylock(rw)
29113+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 29114+
29115+#endif /* __KERNEL__ */
29116+#endif /* __AUFS_RWSEM_H__ */
7f207e10 29117diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
eca34b5c 29118--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
016522bc 29119+++ linux/fs/aufs/sbinfo.c 2020-01-27 10:57:18.178871751 +0100
eca801bf 29120@@ -0,0 +1,314 @@
cd7a4cd9 29121+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29122+/*
016522bc 29123+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 29124+ *
29125+ * This program, aufs is free software; you can redistribute it and/or modify
29126+ * it under the terms of the GNU General Public License as published by
29127+ * the Free Software Foundation; either version 2 of the License, or
29128+ * (at your option) any later version.
dece6358
AM
29129+ *
29130+ * This program is distributed in the hope that it will be useful,
29131+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29132+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29133+ * GNU General Public License for more details.
29134+ *
29135+ * You should have received a copy of the GNU General Public License
523b37e3 29136+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29137+ */
29138+
29139+/*
29140+ * superblock private data
29141+ */
29142+
eca801bf 29143+#include <linux/iversion.h>
1facf9fc 29144+#include "aufs.h"
29145+
29146+/*
29147+ * they are necessary regardless sysfs is disabled.
29148+ */
29149+void au_si_free(struct kobject *kobj)
29150+{
86dc4139 29151+ int i;
1facf9fc 29152+ struct au_sbinfo *sbinfo;
b752ccd1 29153+ char *locked __maybe_unused; /* debug only */
1facf9fc 29154+
29155+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 29156+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29157+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 29158+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d 29159+
acd2b654
AM
29160+ AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29161+ au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29162+ AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29163+ au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
1facf9fc 29164+
062440b3 29165+ dbgaufs_si_fin(sbinfo);
e49829fe 29166+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 29167+ au_br_free(sbinfo);
e49829fe 29168+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 29169+
9f237c51 29170+ au_kfree_try_rcu(sbinfo->si_branch);
1facf9fc 29171+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 29172+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 29173+
acd2b654
AM
29174+ au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29175+ /* si_nfiles is waited too */
9f237c51 29176+ au_kfree_rcu(sbinfo);
1facf9fc 29177+}
29178+
29179+int au_si_alloc(struct super_block *sb)
29180+{
86dc4139 29181+ int err, i;
1facf9fc 29182+ struct au_sbinfo *sbinfo;
29183+
29184+ err = -ENOMEM;
4a4d8108 29185+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 29186+ if (unlikely(!sbinfo))
29187+ goto out;
29188+
29189+ /* will be reallocated separately */
29190+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29191+ if (unlikely(!sbinfo->si_branch))
febd17d6 29192+ goto out_sbinfo;
1facf9fc 29193+
1facf9fc 29194+ err = sysaufs_si_init(sbinfo);
062440b3
AM
29195+ if (!err) {
29196+ dbgaufs_si_null(sbinfo);
29197+ err = dbgaufs_si_init(sbinfo);
29198+ if (unlikely(err))
29199+ kobject_put(&sbinfo->si_kobj);
29200+ }
1facf9fc 29201+ if (unlikely(err))
29202+ goto out_br;
29203+
29204+ au_nwt_init(&sbinfo->si_nowait);
dece6358 29205+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 29206+
acd2b654
AM
29207+ au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29208+ au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
7f207e10 29209+
5afbbe0d 29210+ sbinfo->si_bbot = -1;
392086de 29211+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 29212+
29213+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29214+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
29215+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29216+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 29217+
076b876e
AM
29218+ au_fhsm_init(sbinfo);
29219+
e49829fe 29220+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 29221+
392086de
AM
29222+ sbinfo->si_xino_jiffy = jiffies;
29223+ sbinfo->si_xino_expire
29224+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 29225+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 29226+ /* leave si_xib_last_pindex and si_xib_next_bit */
29227+
8b6a4947 29228+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 29229+
e49829fe 29230+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 29231+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29232+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29233+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29234+
86dc4139 29235+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29236+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 29237+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 29238+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 29239+
8b6a4947 29240+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 29241+
b95c5147
AM
29242+ /* with getattr by default */
29243+ sbinfo->si_iop_array = aufs_iop;
29244+
1facf9fc 29245+ /* leave other members for sysaufs and si_mnt. */
29246+ sbinfo->si_sb = sb;
29247+ sb->s_fs_info = sbinfo;
b752ccd1 29248+ si_pid_set(sb);
1facf9fc 29249+ return 0; /* success */
29250+
4f0767ce 29251+out_br:
9f237c51 29252+ au_kfree_try_rcu(sbinfo->si_branch);
4f0767ce 29253+out_sbinfo:
9f237c51 29254+ au_kfree_rcu(sbinfo);
4f0767ce 29255+out:
1facf9fc 29256+ return err;
29257+}
29258+
e2f27e51 29259+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 29260+{
29261+ int err, sz;
29262+ struct au_branch **brp;
29263+
dece6358
AM
29264+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29265+
1facf9fc 29266+ err = -ENOMEM;
5afbbe0d 29267+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 29268+ if (unlikely(!sz))
29269+ sz = sizeof(*brp);
e2f27e51
AM
29270+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29271+ may_shrink);
1facf9fc 29272+ if (brp) {
29273+ sbinfo->si_branch = brp;
29274+ err = 0;
29275+ }
29276+
29277+ return err;
29278+}
29279+
29280+/* ---------------------------------------------------------------------- */
29281+
29282+unsigned int au_sigen_inc(struct super_block *sb)
29283+{
29284+ unsigned int gen;
5527c038 29285+ struct inode *inode;
1facf9fc 29286+
dece6358
AM
29287+ SiMustWriteLock(sb);
29288+
1facf9fc 29289+ gen = ++au_sbi(sb)->si_generation;
29290+ au_update_digen(sb->s_root);
5527c038
JR
29291+ inode = d_inode(sb->s_root);
29292+ au_update_iigen(inode, /*half*/0);
be118d29 29293+ inode_inc_iversion(inode);
1facf9fc 29294+ return gen;
29295+}
29296+
29297+aufs_bindex_t au_new_br_id(struct super_block *sb)
29298+{
29299+ aufs_bindex_t br_id;
29300+ int i;
29301+ struct au_sbinfo *sbinfo;
29302+
dece6358
AM
29303+ SiMustWriteLock(sb);
29304+
1facf9fc 29305+ sbinfo = au_sbi(sb);
29306+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29307+ br_id = ++sbinfo->si_last_br_id;
7f207e10 29308+ AuDebugOn(br_id < 0);
1facf9fc 29309+ if (br_id && au_br_index(sb, br_id) < 0)
29310+ return br_id;
29311+ }
29312+
29313+ return -1;
29314+}
29315+
29316+/* ---------------------------------------------------------------------- */
29317+
e49829fe
JR
29318+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29319+int si_read_lock(struct super_block *sb, int flags)
29320+{
29321+ int err;
29322+
29323+ err = 0;
29324+ if (au_ftest_lock(flags, FLUSH))
29325+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29326+
29327+ si_noflush_read_lock(sb);
29328+ err = au_plink_maint(sb, flags);
29329+ if (unlikely(err))
29330+ si_read_unlock(sb);
29331+
29332+ return err;
29333+}
29334+
29335+int si_write_lock(struct super_block *sb, int flags)
29336+{
29337+ int err;
29338+
29339+ if (au_ftest_lock(flags, FLUSH))
29340+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29341+
29342+ si_noflush_write_lock(sb);
29343+ err = au_plink_maint(sb, flags);
29344+ if (unlikely(err))
29345+ si_write_unlock(sb);
29346+
29347+ return err;
29348+}
29349+
1facf9fc 29350+/* dentry and super_block lock. call at entry point */
e49829fe 29351+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 29352+{
e49829fe 29353+ int err;
027c5e7a 29354+ struct super_block *sb;
e49829fe 29355+
027c5e7a
AM
29356+ sb = dentry->d_sb;
29357+ err = si_read_lock(sb, flags);
29358+ if (unlikely(err))
29359+ goto out;
29360+
29361+ if (au_ftest_lock(flags, DW))
29362+ di_write_lock_child(dentry);
29363+ else
29364+ di_read_lock_child(dentry, flags);
29365+
29366+ if (au_ftest_lock(flags, GEN)) {
29367+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
29368+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29369+ AuDebugOn(!err && au_dbrange_test(dentry));
29370+ else if (!err)
29371+ err = au_dbrange_test(dentry);
027c5e7a
AM
29372+ if (unlikely(err))
29373+ aufs_read_unlock(dentry, flags);
e49829fe
JR
29374+ }
29375+
027c5e7a 29376+out:
e49829fe 29377+ return err;
1facf9fc 29378+}
29379+
29380+void aufs_read_unlock(struct dentry *dentry, int flags)
29381+{
29382+ if (au_ftest_lock(flags, DW))
29383+ di_write_unlock(dentry);
29384+ else
29385+ di_read_unlock(dentry, flags);
29386+ si_read_unlock(dentry->d_sb);
29387+}
29388+
29389+void aufs_write_lock(struct dentry *dentry)
29390+{
e49829fe 29391+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29392+ di_write_lock_child(dentry);
29393+}
29394+
29395+void aufs_write_unlock(struct dentry *dentry)
29396+{
29397+ di_write_unlock(dentry);
29398+ si_write_unlock(dentry->d_sb);
29399+}
29400+
e49829fe 29401+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29402+{
e49829fe 29403+ int err;
027c5e7a
AM
29404+ unsigned int sigen;
29405+ struct super_block *sb;
e49829fe 29406+
027c5e7a
AM
29407+ sb = d1->d_sb;
29408+ err = si_read_lock(sb, flags);
29409+ if (unlikely(err))
29410+ goto out;
29411+
b95c5147 29412+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29413+
29414+ if (au_ftest_lock(flags, GEN)) {
29415+ sigen = au_sigen(sb);
29416+ err = au_digen_test(d1, sigen);
29417+ AuDebugOn(!err && au_dbrange_test(d1));
29418+ if (!err) {
29419+ err = au_digen_test(d2, sigen);
29420+ AuDebugOn(!err && au_dbrange_test(d2));
29421+ }
29422+ if (unlikely(err))
29423+ aufs_read_and_write_unlock2(d1, d2);
29424+ }
29425+
29426+out:
e49829fe 29427+ return err;
1facf9fc 29428+}
29429+
29430+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29431+{
29432+ di_write_unlock2(d1, d2);
29433+ si_read_unlock(d1->d_sb);
29434+}
7f207e10 29435diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
eca34b5c 29436--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
016522bc 29437+++ linux/fs/aufs/super.c 2020-01-27 10:57:18.178871751 +0100
fbc438ed 29438@@ -0,0 +1,1047 @@
cd7a4cd9 29439+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29440+/*
016522bc 29441+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 29442+ *
29443+ * This program, aufs is free software; you can redistribute it and/or modify
29444+ * it under the terms of the GNU General Public License as published by
29445+ * the Free Software Foundation; either version 2 of the License, or
29446+ * (at your option) any later version.
dece6358
AM
29447+ *
29448+ * This program is distributed in the hope that it will be useful,
29449+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29450+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29451+ * GNU General Public License for more details.
29452+ *
29453+ * You should have received a copy of the GNU General Public License
523b37e3 29454+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29455+ */
29456+
29457+/*
29458+ * mount and super_block operations
29459+ */
29460+
eca801bf 29461+#include <linux/iversion.h>
f6c5ef8b 29462+#include <linux/mm.h>
1facf9fc 29463+#include <linux/seq_file.h>
29464+#include <linux/statfs.h>
7f207e10 29465+#include <linux/vmalloc.h>
1facf9fc 29466+#include "aufs.h"
29467+
29468+/*
29469+ * super_operations
29470+ */
29471+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29472+{
29473+ struct au_icntnr *c;
29474+
29475+ c = au_cache_alloc_icntnr();
29476+ if (c) {
027c5e7a 29477+ au_icntnr_init(c);
be118d29 29478+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29479+ c->iinfo.ii_hinode = NULL;
29480+ return &c->vfs_inode;
29481+ }
29482+ return NULL;
29483+}
29484+
29485+static void aufs_destroy_inode(struct inode *inode)
29486+{
5afbbe0d
AM
29487+ if (!au_is_bad_inode(inode))
29488+ au_iinfo_fin(inode);
fbc438ed
JR
29489+}
29490+
29491+static void aufs_free_inode(struct inode *inode)
29492+{
29493+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
1facf9fc 29494+}
29495+
29496+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29497+{
29498+ struct inode *inode;
29499+ int err;
29500+
29501+ inode = iget_locked(sb, ino);
29502+ if (unlikely(!inode)) {
29503+ inode = ERR_PTR(-ENOMEM);
29504+ goto out;
29505+ }
29506+ if (!(inode->i_state & I_NEW))
29507+ goto out;
29508+
29509+ err = au_xigen_new(inode);
29510+ if (!err)
29511+ err = au_iinfo_init(inode);
29512+ if (!err)
be118d29 29513+ inode_inc_iversion(inode);
1facf9fc 29514+ else {
29515+ iget_failed(inode);
29516+ inode = ERR_PTR(err);
29517+ }
29518+
4f0767ce 29519+out:
1facf9fc 29520+ /* never return NULL */
29521+ AuDebugOn(!inode);
29522+ AuTraceErrPtr(inode);
29523+ return inode;
29524+}
29525+
29526+/* lock free root dinfo */
29527+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29528+{
29529+ int err;
5afbbe0d 29530+ aufs_bindex_t bindex, bbot;
1facf9fc 29531+ struct path path;
4a4d8108 29532+ struct au_hdentry *hdp;
1facf9fc 29533+ struct au_branch *br;
076b876e 29534+ au_br_perm_str_t perm;
1facf9fc 29535+
29536+ err = 0;
5afbbe0d
AM
29537+ bbot = au_sbbot(sb);
29538+ bindex = 0;
29539+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29540+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29541+ br = au_sbr(sb, bindex);
86dc4139 29542+ path.mnt = au_br_mnt(br);
5afbbe0d 29543+ path.dentry = hdp->hd_dentry;
1facf9fc 29544+ err = au_seq_path(seq, &path);
79b8bda9 29545+ if (!err) {
076b876e 29546+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29547+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29548+ if (bindex != bbot)
79b8bda9 29549+ seq_putc(seq, ':');
1e00d052 29550+ }
1facf9fc 29551+ }
79b8bda9
AM
29552+ if (unlikely(err || seq_has_overflowed(seq)))
29553+ err = -E2BIG;
1facf9fc 29554+
29555+ return err;
29556+}
29557+
f2c43d5f
AM
29558+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29559+ const char *append)
29560+{
29561+ char *p;
29562+
29563+ p = fmt;
29564+ while (*pat != ':')
29565+ *p++ = *pat++;
29566+ *p++ = *pat++;
29567+ strcpy(p, append);
29568+ AuDebugOn(strlen(fmt) >= len);
29569+}
29570+
1facf9fc 29571+static void au_show_wbr_create(struct seq_file *m, int v,
29572+ struct au_sbinfo *sbinfo)
29573+{
29574+ const char *pat;
f2c43d5f
AM
29575+ char fmt[32];
29576+ struct au_wbr_mfs *mfs;
1facf9fc 29577+
dece6358
AM
29578+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29579+
c2b27bf2 29580+ seq_puts(m, ",create=");
1facf9fc 29581+ pat = au_optstr_wbr_create(v);
f2c43d5f 29582+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29583+ switch (v) {
29584+ case AuWbrCreate_TDP:
29585+ case AuWbrCreate_RR:
29586+ case AuWbrCreate_MFS:
29587+ case AuWbrCreate_PMFS:
c2b27bf2 29588+ seq_puts(m, pat);
1facf9fc 29589+ break;
f2c43d5f
AM
29590+ case AuWbrCreate_MFSRR:
29591+ case AuWbrCreate_TDMFS:
29592+ case AuWbrCreate_PMFSRR:
29593+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29594+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29595+ break;
f2c43d5f 29596+ case AuWbrCreate_MFSV:
1facf9fc 29597+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29598+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29599+ seq_printf(m, fmt,
29600+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29601+ / MSEC_PER_SEC);
1facf9fc 29602+ break;
1facf9fc 29603+ case AuWbrCreate_MFSRRV:
f2c43d5f 29604+ case AuWbrCreate_TDMFSV:
392086de 29605+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29606+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29607+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29608+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29609+ break;
f2c43d5f
AM
29610+ default:
29611+ BUG();
1facf9fc 29612+ }
29613+}
29614+
7eafdf33 29615+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29616+{
29617+#ifdef CONFIG_SYSFS
29618+ return 0;
29619+#else
29620+ int err;
29621+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29622+ aufs_bindex_t bindex, brid;
1facf9fc 29623+ struct qstr *name;
29624+ struct file *f;
29625+ struct dentry *d, *h_root;
acd2b654 29626+ struct au_branch *br;
1facf9fc 29627+
dece6358
AM
29628+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29629+
1facf9fc 29630+ err = 0;
1facf9fc 29631+ f = au_sbi(sb)->si_xib;
29632+ if (!f)
29633+ goto out;
29634+
29635+ /* stop printing the default xino path on the first writable branch */
29636+ h_root = NULL;
acd2b654
AM
29637+ bindex = au_xi_root(sb, f->f_path.dentry);
29638+ if (bindex >= 0) {
29639+ br = au_sbr_sb(sb, bindex);
29640+ h_root = au_br_dentry(br);
1facf9fc 29641+ }
acd2b654 29642+
2000de60 29643+ d = f->f_path.dentry;
1facf9fc 29644+ name = &d->d_name;
29645+ /* safe ->d_parent because the file is unlinked */
29646+ if (d->d_parent == h_root
29647+ && name->len == len
29648+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29649+ goto out;
29650+
29651+ seq_puts(seq, ",xino=");
29652+ err = au_xino_path(seq, f);
29653+
4f0767ce 29654+out:
1facf9fc 29655+ return err;
29656+#endif
29657+}
29658+
29659+/* seq_file will re-call me in case of too long string */
7eafdf33 29660+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29661+{
027c5e7a 29662+ int err;
1facf9fc 29663+ unsigned int mnt_flags, v;
29664+ struct super_block *sb;
29665+ struct au_sbinfo *sbinfo;
29666+
29667+#define AuBool(name, str) do { \
29668+ v = au_opt_test(mnt_flags, name); \
29669+ if (v != au_opt_test(AuOpt_Def, name)) \
29670+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29671+} while (0)
29672+
29673+#define AuStr(name, str) do { \
29674+ v = mnt_flags & AuOptMask_##name; \
29675+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29676+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29677+} while (0)
29678+
29679+#define AuUInt(name, str, val) do { \
29680+ if (val != AUFS_##name##_DEF) \
29681+ seq_printf(m, "," #str "=%u", val); \
29682+} while (0)
29683+
7eafdf33 29684+ sb = dentry->d_sb;
2121bcd9 29685+ if (sb->s_flags & SB_POSIXACL)
c1595e42 29686+ seq_puts(m, ",acl");
43982f53 29687+#if 0 /* reserved for future use */
be118d29
JR
29688+ if (sb->s_flags & SB_I_VERSION)
29689+ seq_puts(m, ",i_version");
29690+#endif
c1595e42
JR
29691+
29692+ /* lock free root dinfo */
1facf9fc 29693+ si_noflush_read_lock(sb);
29694+ sbinfo = au_sbi(sb);
29695+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29696+
29697+ mnt_flags = au_mntflags(sb);
29698+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29699+ err = au_show_xino(m, sb);
1facf9fc 29700+ if (unlikely(err))
29701+ goto out;
29702+ } else
29703+ seq_puts(m, ",noxino");
29704+
29705+ AuBool(TRUNC_XINO, trunc_xino);
29706+ AuStr(UDBA, udba);
dece6358 29707+ AuBool(SHWH, shwh);
1facf9fc 29708+ AuBool(PLINK, plink);
4a4d8108 29709+ AuBool(DIO, dio);
076b876e 29710+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29711+
29712+ v = sbinfo->si_wbr_create;
29713+ if (v != AuWbrCreate_Def)
29714+ au_show_wbr_create(m, v, sbinfo);
29715+
29716+ v = sbinfo->si_wbr_copyup;
29717+ if (v != AuWbrCopyup_Def)
29718+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29719+
29720+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29721+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29722+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29723+
29724+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29725+
027c5e7a
AM
29726+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29727+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29728+
29729+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29730+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29731+
076b876e
AM
29732+ au_fhsm_show(m, sbinfo);
29733+
8b6a4947 29734+ AuBool(DIRREN, dirren);
1facf9fc 29735+ AuBool(SUM, sum);
29736+ /* AuBool(SUM_W, wsum); */
29737+ AuBool(WARN_PERM, warn_perm);
29738+ AuBool(VERBOSE, verbose);
29739+
4f0767ce 29740+out:
1facf9fc 29741+ /* be sure to print "br:" last */
29742+ if (!sysaufs_brs) {
29743+ seq_puts(m, ",br:");
29744+ au_show_brs(m, sb);
29745+ }
29746+ si_read_unlock(sb);
29747+ return 0;
29748+
1facf9fc 29749+#undef AuBool
29750+#undef AuStr
4a4d8108 29751+#undef AuUInt
1facf9fc 29752+}
29753+
29754+/* ---------------------------------------------------------------------- */
29755+
29756+/* sum mode which returns the summation for statfs(2) */
29757+
29758+static u64 au_add_till_max(u64 a, u64 b)
29759+{
29760+ u64 old;
29761+
29762+ old = a;
29763+ a += b;
92d182d2
AM
29764+ if (old <= a)
29765+ return a;
29766+ return ULLONG_MAX;
29767+}
29768+
29769+static u64 au_mul_till_max(u64 a, long mul)
29770+{
29771+ u64 old;
29772+
29773+ old = a;
29774+ a *= mul;
29775+ if (old <= a)
1facf9fc 29776+ return a;
29777+ return ULLONG_MAX;
29778+}
29779+
29780+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29781+{
29782+ int err;
92d182d2 29783+ long bsize, factor;
1facf9fc 29784+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29785+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29786+ unsigned char shared;
7f207e10 29787+ struct path h_path;
1facf9fc 29788+ struct super_block *h_sb;
29789+
92d182d2
AM
29790+ err = 0;
29791+ bsize = LONG_MAX;
29792+ files = 0;
29793+ ffree = 0;
1facf9fc 29794+ blocks = 0;
29795+ bfree = 0;
29796+ bavail = 0;
5afbbe0d
AM
29797+ bbot = au_sbbot(sb);
29798+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29799+ h_path.mnt = au_sbr_mnt(sb, bindex);
29800+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29801+ shared = 0;
92d182d2 29802+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29803+ shared = (au_sbr_sb(sb, i) == h_sb);
29804+ if (shared)
29805+ continue;
29806+
29807+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29808+ h_path.dentry = h_path.mnt->mnt_root;
29809+ err = vfs_statfs(&h_path, buf);
1facf9fc 29810+ if (unlikely(err))
29811+ goto out;
29812+
92d182d2
AM
29813+ if (bsize > buf->f_bsize) {
29814+ /*
29815+ * we will reduce bsize, so we have to expand blocks
29816+ * etc. to match them again
29817+ */
29818+ factor = (bsize / buf->f_bsize);
29819+ blocks = au_mul_till_max(blocks, factor);
29820+ bfree = au_mul_till_max(bfree, factor);
29821+ bavail = au_mul_till_max(bavail, factor);
29822+ bsize = buf->f_bsize;
29823+ }
29824+
29825+ factor = (buf->f_bsize / bsize);
29826+ blocks = au_add_till_max(blocks,
29827+ au_mul_till_max(buf->f_blocks, factor));
29828+ bfree = au_add_till_max(bfree,
29829+ au_mul_till_max(buf->f_bfree, factor));
29830+ bavail = au_add_till_max(bavail,
29831+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29832+ files = au_add_till_max(files, buf->f_files);
29833+ ffree = au_add_till_max(ffree, buf->f_ffree);
29834+ }
29835+
92d182d2 29836+ buf->f_bsize = bsize;
1facf9fc 29837+ buf->f_blocks = blocks;
29838+ buf->f_bfree = bfree;
29839+ buf->f_bavail = bavail;
29840+ buf->f_files = files;
29841+ buf->f_ffree = ffree;
92d182d2 29842+ buf->f_frsize = 0;
1facf9fc 29843+
4f0767ce 29844+out:
1facf9fc 29845+ return err;
29846+}
29847+
29848+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29849+{
29850+ int err;
7f207e10 29851+ struct path h_path;
1facf9fc 29852+ struct super_block *sb;
29853+
29854+ /* lock free root dinfo */
29855+ sb = dentry->d_sb;
29856+ si_noflush_read_lock(sb);
7f207e10 29857+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29858+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29859+ h_path.mnt = au_sbr_mnt(sb, 0);
29860+ h_path.dentry = h_path.mnt->mnt_root;
29861+ err = vfs_statfs(&h_path, buf);
29862+ } else
1facf9fc 29863+ err = au_statfs_sum(sb, buf);
29864+ si_read_unlock(sb);
29865+
29866+ if (!err) {
29867+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29868+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29869+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29870+ }
29871+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29872+
29873+ return err;
29874+}
29875+
29876+/* ---------------------------------------------------------------------- */
29877+
537831f9
AM
29878+static int aufs_sync_fs(struct super_block *sb, int wait)
29879+{
29880+ int err, e;
5afbbe0d 29881+ aufs_bindex_t bbot, bindex;
537831f9
AM
29882+ struct au_branch *br;
29883+ struct super_block *h_sb;
29884+
29885+ err = 0;
29886+ si_noflush_read_lock(sb);
5afbbe0d
AM
29887+ bbot = au_sbbot(sb);
29888+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29889+ br = au_sbr(sb, bindex);
29890+ if (!au_br_writable(br->br_perm))
29891+ continue;
29892+
29893+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29894+ e = vfsub_sync_filesystem(h_sb, wait);
29895+ if (unlikely(e && !err))
29896+ err = e;
29897+ /* go on even if an error happens */
537831f9
AM
29898+ }
29899+ si_read_unlock(sb);
29900+
29901+ return err;
29902+}
29903+
29904+/* ---------------------------------------------------------------------- */
29905+
1facf9fc 29906+/* final actions when unmounting a file system */
29907+static void aufs_put_super(struct super_block *sb)
29908+{
29909+ struct au_sbinfo *sbinfo;
29910+
29911+ sbinfo = au_sbi(sb);
062440b3
AM
29912+ if (sbinfo)
29913+ kobject_put(&sbinfo->si_kobj);
1facf9fc 29914+}
29915+
29916+/* ---------------------------------------------------------------------- */
29917+
79b8bda9
AM
29918+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29919+ struct super_block *sb, void *arg)
7f207e10
AM
29920+{
29921+ void *array;
076b876e 29922+ unsigned long long n, sz;
7f207e10
AM
29923+
29924+ array = NULL;
29925+ n = 0;
29926+ if (!*hint)
29927+ goto out;
29928+
29929+ if (*hint > ULLONG_MAX / sizeof(array)) {
29930+ array = ERR_PTR(-EMFILE);
29931+ pr_err("hint %llu\n", *hint);
29932+ goto out;
29933+ }
29934+
076b876e
AM
29935+ sz = sizeof(array) * *hint;
29936+ array = kzalloc(sz, GFP_NOFS);
7f207e10 29937+ if (unlikely(!array))
076b876e 29938+ array = vzalloc(sz);
7f207e10
AM
29939+ if (unlikely(!array)) {
29940+ array = ERR_PTR(-ENOMEM);
29941+ goto out;
29942+ }
29943+
79b8bda9 29944+ n = cb(sb, array, *hint, arg);
7f207e10
AM
29945+ AuDebugOn(n > *hint);
29946+
29947+out:
29948+ *hint = n;
29949+ return array;
29950+}
29951+
79b8bda9 29952+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
29953+ unsigned long long max __maybe_unused,
29954+ void *arg)
29955+{
29956+ unsigned long long n;
29957+ struct inode **p, *inode;
29958+ struct list_head *head;
29959+
29960+ n = 0;
29961+ p = a;
29962+ head = arg;
79b8bda9 29963+ spin_lock(&sb->s_inode_list_lock);
7f207e10 29964+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
29965+ if (!au_is_bad_inode(inode)
29966+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
29967+ spin_lock(&inode->i_lock);
29968+ if (atomic_read(&inode->i_count)) {
29969+ au_igrab(inode);
29970+ *p++ = inode;
29971+ n++;
29972+ AuDebugOn(n > max);
29973+ }
29974+ spin_unlock(&inode->i_lock);
7f207e10
AM
29975+ }
29976+ }
79b8bda9 29977+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
29978+
29979+ return n;
29980+}
29981+
29982+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29983+{
acd2b654
AM
29984+ struct au_sbinfo *sbi;
29985+
29986+ sbi = au_sbi(sb);
29987+ *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
79b8bda9 29988+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
29989+}
29990+
29991+void au_iarray_free(struct inode **a, unsigned long long max)
29992+{
29993+ unsigned long long ull;
29994+
29995+ for (ull = 0; ull < max; ull++)
29996+ iput(a[ull]);
be52b249 29997+ kvfree(a);
7f207e10
AM
29998+}
29999+
30000+/* ---------------------------------------------------------------------- */
30001+
1facf9fc 30002+/*
30003+ * refresh dentry and inode at remount time.
30004+ */
027c5e7a
AM
30005+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30006+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30007+ struct dentry *parent)
1facf9fc 30008+{
30009+ int err;
1facf9fc 30010+
30011+ di_write_lock_child(dentry);
1facf9fc 30012+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
30013+ err = au_refresh_dentry(dentry, parent);
30014+ if (!err && dir_flags)
5527c038 30015+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 30016+ di_read_unlock(parent, AuLock_IR);
1facf9fc 30017+ di_write_unlock(dentry);
30018+
30019+ return err;
30020+}
30021+
027c5e7a
AM
30022+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30023+ struct au_sbinfo *sbinfo,
b95c5147 30024+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 30025+{
027c5e7a
AM
30026+ int err;
30027+ struct dentry *parent;
027c5e7a
AM
30028+
30029+ err = 0;
30030+ parent = dget_parent(dentry);
30031+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
30032+ if (d_really_is_positive(dentry)) {
30033+ if (!d_is_dir(dentry))
027c5e7a
AM
30034+ err = au_do_refresh(dentry, /*dir_flags*/0,
30035+ parent);
30036+ else {
30037+ err = au_do_refresh(dentry, dir_flags, parent);
30038+ if (unlikely(err))
30039+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30040+ }
30041+ } else
30042+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30043+ AuDbgDentry(dentry);
30044+ }
30045+ dput(parent);
30046+
79b8bda9 30047+ if (!err) {
b95c5147 30048+ if (do_idop)
79b8bda9
AM
30049+ au_refresh_dop(dentry, /*force_reval*/0);
30050+ } else
30051+ au_refresh_dop(dentry, /*force_reval*/1);
30052+
027c5e7a
AM
30053+ AuTraceErr(err);
30054+ return err;
1facf9fc 30055+}
30056+
b95c5147 30057+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 30058+{
30059+ int err, i, j, ndentry, e;
027c5e7a 30060+ unsigned int sigen;
1facf9fc 30061+ struct au_dcsub_pages dpages;
30062+ struct au_dpage *dpage;
027c5e7a
AM
30063+ struct dentry **dentries, *d;
30064+ struct au_sbinfo *sbinfo;
30065+ struct dentry *root = sb->s_root;
5527c038 30066+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 30067+
b95c5147 30068+ if (do_idop)
79b8bda9
AM
30069+ au_refresh_dop(root, /*force_reval*/0);
30070+
027c5e7a
AM
30071+ err = au_dpages_init(&dpages, GFP_NOFS);
30072+ if (unlikely(err))
1facf9fc 30073+ goto out;
027c5e7a
AM
30074+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
30075+ if (unlikely(err))
1facf9fc 30076+ goto out_dpages;
1facf9fc 30077+
027c5e7a
AM
30078+ sigen = au_sigen(sb);
30079+ sbinfo = au_sbi(sb);
30080+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 30081+ dpage = dpages.dpages + i;
30082+ dentries = dpage->dentries;
30083+ ndentry = dpage->ndentry;
027c5e7a 30084+ for (j = 0; j < ndentry; j++) {
1facf9fc 30085+ d = dentries[j];
79b8bda9 30086+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 30087+ do_idop);
027c5e7a
AM
30088+ if (unlikely(e && !err))
30089+ err = e;
30090+ /* go on even err */
1facf9fc 30091+ }
30092+ }
30093+
4f0767ce 30094+out_dpages:
1facf9fc 30095+ au_dpages_free(&dpages);
4f0767ce 30096+out:
1facf9fc 30097+ return err;
30098+}
30099+
b95c5147 30100+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 30101+{
027c5e7a
AM
30102+ int err, e;
30103+ unsigned int sigen;
30104+ unsigned long long max, ull;
30105+ struct inode *inode, **array;
1facf9fc 30106+
027c5e7a
AM
30107+ array = au_iarray_alloc(sb, &max);
30108+ err = PTR_ERR(array);
30109+ if (IS_ERR(array))
30110+ goto out;
1facf9fc 30111+
30112+ err = 0;
027c5e7a
AM
30113+ sigen = au_sigen(sb);
30114+ for (ull = 0; ull < max; ull++) {
30115+ inode = array[ull];
076b876e
AM
30116+ if (unlikely(!inode))
30117+ break;
b95c5147
AM
30118+
30119+ e = 0;
30120+ ii_write_lock_child(inode);
537831f9 30121+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 30122+ e = au_refresh_hinode_self(inode);
1facf9fc 30123+ if (unlikely(e)) {
b95c5147 30124+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 30125+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 30126+ if (!err)
30127+ err = e;
30128+ /* go on even if err */
30129+ }
30130+ }
b95c5147
AM
30131+ if (!e && do_idop)
30132+ au_refresh_iop(inode, /*force_getattr*/0);
30133+ ii_write_unlock(inode);
1facf9fc 30134+ }
30135+
027c5e7a 30136+ au_iarray_free(array, max);
1facf9fc 30137+
4f0767ce 30138+out:
1facf9fc 30139+ return err;
30140+}
30141+
b95c5147 30142+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 30143+{
027c5e7a
AM
30144+ int err, e;
30145+ unsigned int udba;
5afbbe0d 30146+ aufs_bindex_t bindex, bbot;
1facf9fc 30147+ struct dentry *root;
30148+ struct inode *inode;
027c5e7a 30149+ struct au_branch *br;
79b8bda9 30150+ struct au_sbinfo *sbi;
1facf9fc 30151+
30152+ au_sigen_inc(sb);
79b8bda9
AM
30153+ sbi = au_sbi(sb);
30154+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 30155+
30156+ root = sb->s_root;
30157+ DiMustNoWaiters(root);
5527c038 30158+ inode = d_inode(root);
1facf9fc 30159+ IiMustNoWaiters(inode);
1facf9fc 30160+
027c5e7a 30161+ udba = au_opt_udba(sb);
5afbbe0d
AM
30162+ bbot = au_sbbot(sb);
30163+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
30164+ br = au_sbr(sb, bindex);
30165+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 30166+ if (unlikely(err))
027c5e7a
AM
30167+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
30168+ bindex, err);
30169+ /* go on even if err */
1facf9fc 30170+ }
027c5e7a 30171+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 30172+
b95c5147 30173+ if (do_idop) {
79b8bda9
AM
30174+ if (au_ftest_si(sbi, NO_DREVAL)) {
30175+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30176+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
30177+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30178+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
30179+ } else {
30180+ AuDebugOn(sb->s_d_op == &aufs_dop);
30181+ sb->s_d_op = &aufs_dop;
b95c5147
AM
30182+ AuDebugOn(sbi->si_iop_array == aufs_iop);
30183+ sbi->si_iop_array = aufs_iop;
79b8bda9 30184+ }
062440b3 30185+ pr_info("reset to %ps and %ps\n",
b95c5147 30186+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
30187+ }
30188+
027c5e7a 30189+ di_write_unlock(root);
b95c5147
AM
30190+ err = au_refresh_d(sb, do_idop);
30191+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
30192+ if (unlikely(e && !err))
30193+ err = e;
1facf9fc 30194+ /* aufs_write_lock() calls ..._child() */
30195+ di_write_lock_child(root);
027c5e7a
AM
30196+
30197+ au_cpup_attr_all(inode, /*force*/1);
30198+
30199+ if (unlikely(err))
30200+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 30201+}
30202+
30203+/* stop extra interpretation of errno in mount(8), and strange error messages */
30204+static int cvt_err(int err)
30205+{
30206+ AuTraceErr(err);
30207+
30208+ switch (err) {
30209+ case -ENOENT:
30210+ case -ENOTDIR:
30211+ case -EEXIST:
30212+ case -EIO:
30213+ err = -EINVAL;
30214+ }
30215+ return err;
30216+}
30217+
30218+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30219+{
4a4d8108
AM
30220+ int err, do_dx;
30221+ unsigned int mntflags;
be52b249
AM
30222+ struct au_opts opts = {
30223+ .opt = NULL
30224+ };
1facf9fc 30225+ struct dentry *root;
30226+ struct inode *inode;
30227+ struct au_sbinfo *sbinfo;
30228+
30229+ err = 0;
30230+ root = sb->s_root;
30231+ if (!data || !*data) {
e49829fe
JR
30232+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30233+ if (!err) {
30234+ di_write_lock_child(root);
30235+ err = au_opts_verify(sb, *flags, /*pending*/0);
30236+ aufs_write_unlock(root);
30237+ }
1facf9fc 30238+ goto out;
30239+ }
30240+
30241+ err = -ENOMEM;
1facf9fc 30242+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30243+ if (unlikely(!opts.opt))
30244+ goto out;
30245+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30246+ opts.flags = AuOpts_REMOUNT;
30247+ opts.sb_flags = *flags;
30248+
30249+ /* parse it before aufs lock */
30250+ err = au_opts_parse(sb, data, &opts);
30251+ if (unlikely(err))
30252+ goto out_opts;
30253+
30254+ sbinfo = au_sbi(sb);
5527c038 30255+ inode = d_inode(root);
febd17d6 30256+ inode_lock(inode);
e49829fe
JR
30257+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30258+ if (unlikely(err))
30259+ goto out_mtx;
30260+ di_write_lock_child(root);
1facf9fc 30261+
30262+ /* au_opts_remount() may return an error */
30263+ err = au_opts_remount(sb, &opts);
30264+ au_opts_free(&opts);
30265+
027c5e7a 30266+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 30267+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 30268+
4a4d8108
AM
30269+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30270+ mntflags = au_mntflags(sb);
30271+ do_dx = !!au_opt_test(mntflags, DIO);
30272+ au_dy_arefresh(do_dx);
30273+ }
30274+
076b876e 30275+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 30276+ aufs_write_unlock(root);
953406b4 30277+
e49829fe 30278+out_mtx:
febd17d6 30279+ inode_unlock(inode);
4f0767ce 30280+out_opts:
1c60b727 30281+ free_page((unsigned long)opts.opt);
4f0767ce 30282+out:
1facf9fc 30283+ err = cvt_err(err);
30284+ AuTraceErr(err);
30285+ return err;
30286+}
30287+
4a4d8108 30288+static const struct super_operations aufs_sop = {
1facf9fc 30289+ .alloc_inode = aufs_alloc_inode,
30290+ .destroy_inode = aufs_destroy_inode,
fbc438ed 30291+ .free_inode = aufs_free_inode,
b752ccd1 30292+ /* always deleting, no clearing */
1facf9fc 30293+ .drop_inode = generic_delete_inode,
30294+ .show_options = aufs_show_options,
30295+ .statfs = aufs_statfs,
30296+ .put_super = aufs_put_super,
537831f9 30297+ .sync_fs = aufs_sync_fs,
1facf9fc 30298+ .remount_fs = aufs_remount_fs
30299+};
30300+
30301+/* ---------------------------------------------------------------------- */
30302+
30303+static int alloc_root(struct super_block *sb)
30304+{
30305+ int err;
30306+ struct inode *inode;
30307+ struct dentry *root;
30308+
30309+ err = -ENOMEM;
30310+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30311+ err = PTR_ERR(inode);
30312+ if (IS_ERR(inode))
30313+ goto out;
30314+
b95c5147 30315+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 30316+ inode->i_fop = &aufs_dir_fop;
30317+ inode->i_mode = S_IFDIR;
9dbd164d 30318+ set_nlink(inode, 2);
1facf9fc 30319+ unlock_new_inode(inode);
30320+
92d182d2 30321+ root = d_make_root(inode);
1facf9fc 30322+ if (unlikely(!root))
92d182d2 30323+ goto out;
1facf9fc 30324+ err = PTR_ERR(root);
30325+ if (IS_ERR(root))
92d182d2 30326+ goto out;
1facf9fc 30327+
4a4d8108 30328+ err = au_di_init(root);
1facf9fc 30329+ if (!err) {
30330+ sb->s_root = root;
30331+ return 0; /* success */
30332+ }
30333+ dput(root);
1facf9fc 30334+
4f0767ce 30335+out:
1facf9fc 30336+ return err;
1facf9fc 30337+}
30338+
30339+static int aufs_fill_super(struct super_block *sb, void *raw_data,
30340+ int silent __maybe_unused)
30341+{
30342+ int err;
be52b249
AM
30343+ struct au_opts opts = {
30344+ .opt = NULL
30345+ };
79b8bda9 30346+ struct au_sbinfo *sbinfo;
1facf9fc 30347+ struct dentry *root;
30348+ struct inode *inode;
30349+ char *arg = raw_data;
30350+
30351+ if (unlikely(!arg || !*arg)) {
30352+ err = -EINVAL;
4a4d8108 30353+ pr_err("no arg\n");
1facf9fc 30354+ goto out;
30355+ }
30356+
30357+ err = -ENOMEM;
1facf9fc 30358+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30359+ if (unlikely(!opts.opt))
30360+ goto out;
30361+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30362+ opts.sb_flags = sb->s_flags;
30363+
30364+ err = au_si_alloc(sb);
30365+ if (unlikely(err))
30366+ goto out_opts;
79b8bda9 30367+ sbinfo = au_sbi(sb);
1facf9fc 30368+
30369+ /* all timestamps always follow the ones on the branch */
2121bcd9 30370+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
be118d29 30371+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
1facf9fc 30372+ sb->s_op = &aufs_sop;
027c5e7a 30373+ sb->s_d_op = &aufs_dop;
1facf9fc 30374+ sb->s_magic = AUFS_SUPER_MAGIC;
30375+ sb->s_maxbytes = 0;
c1595e42 30376+ sb->s_stack_depth = 1;
1facf9fc 30377+ au_export_init(sb);
f2c43d5f 30378+ au_xattr_init(sb);
1facf9fc 30379+
30380+ err = alloc_root(sb);
30381+ if (unlikely(err)) {
30382+ si_write_unlock(sb);
30383+ goto out_info;
30384+ }
30385+ root = sb->s_root;
5527c038 30386+ inode = d_inode(root);
1facf9fc 30387+
30388+ /*
30389+ * actually we can parse options regardless aufs lock here.
30390+ * but at remount time, parsing must be done before aufs lock.
30391+ * so we follow the same rule.
30392+ */
30393+ ii_write_lock_parent(inode);
30394+ aufs_write_unlock(root);
30395+ err = au_opts_parse(sb, arg, &opts);
30396+ if (unlikely(err))
30397+ goto out_root;
30398+
30399+ /* lock vfs_inode first, then aufs. */
febd17d6 30400+ inode_lock(inode);
1facf9fc 30401+ aufs_write_lock(root);
30402+ err = au_opts_mount(sb, &opts);
30403+ au_opts_free(&opts);
79b8bda9
AM
30404+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30405+ sb->s_d_op = &aufs_dop_noreval;
062440b3 30406+ pr_info("%ps\n", sb->s_d_op);
79b8bda9 30407+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30408+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30409+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30410+ }
1facf9fc 30411+ aufs_write_unlock(root);
febd17d6 30412+ inode_unlock(inode);
4a4d8108
AM
30413+ if (!err)
30414+ goto out_opts; /* success */
1facf9fc 30415+
4f0767ce 30416+out_root:
1facf9fc 30417+ dput(root);
30418+ sb->s_root = NULL;
4f0767ce 30419+out_info:
79b8bda9 30420+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30421+ sb->s_fs_info = NULL;
4f0767ce 30422+out_opts:
1c60b727 30423+ free_page((unsigned long)opts.opt);
4f0767ce 30424+out:
1facf9fc 30425+ AuTraceErr(err);
30426+ err = cvt_err(err);
30427+ AuTraceErr(err);
30428+ return err;
30429+}
30430+
30431+/* ---------------------------------------------------------------------- */
30432+
027c5e7a
AM
30433+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30434+ const char *dev_name __maybe_unused,
30435+ void *raw_data)
1facf9fc 30436+{
027c5e7a 30437+ struct dentry *root;
1facf9fc 30438+
30439+ /* all timestamps always follow the ones on the branch */
30440+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30441+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30442+ if (IS_ERR(root))
30443+ goto out;
30444+
062440b3 30445+ au_sbilist_add(root->d_sb);
027c5e7a
AM
30446+
30447+out:
30448+ return root;
1facf9fc 30449+}
30450+
e49829fe
JR
30451+static void aufs_kill_sb(struct super_block *sb)
30452+{
30453+ struct au_sbinfo *sbinfo;
30454+
30455+ sbinfo = au_sbi(sb);
30456+ if (sbinfo) {
30457+ au_sbilist_del(sb);
30458+ aufs_write_lock(sb->s_root);
076b876e 30459+ au_fhsm_fin(sb);
e49829fe
JR
30460+ if (sbinfo->si_wbr_create_ops->fin)
30461+ sbinfo->si_wbr_create_ops->fin(sb);
30462+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30463+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30464+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30465+ }
30466+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30467+ au_plink_put(sb, /*verbose*/1);
30468+ au_xino_clr(sb);
8b6a4947 30469+ au_dr_opt_flush(sb);
1e00d052 30470+ sbinfo->si_sb = NULL;
e49829fe 30471+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30472+ au_nwt_flush(&sbinfo->si_nowait);
30473+ }
98d9a5b1 30474+ kill_anon_super(sb);
e49829fe
JR
30475+}
30476+
1facf9fc 30477+struct file_system_type aufs_fs_type = {
30478+ .name = AUFS_FSTYPE,
c06a8ce3
AM
30479+ /* a race between rename and others */
30480+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 30481+ .mount = aufs_mount,
e49829fe 30482+ .kill_sb = aufs_kill_sb,
1facf9fc 30483+ /* no need to __module_get() and module_put(). */
30484+ .owner = THIS_MODULE,
30485+};
7f207e10 30486diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
eca34b5c 30487--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
016522bc 30488+++ linux/fs/aufs/super.h 2020-01-27 10:57:18.178871751 +0100
acd2b654 30489@@ -0,0 +1,589 @@
062440b3 30490+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30491+/*
016522bc 30492+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 30493+ *
30494+ * This program, aufs is free software; you can redistribute it and/or modify
30495+ * it under the terms of the GNU General Public License as published by
30496+ * the Free Software Foundation; either version 2 of the License, or
30497+ * (at your option) any later version.
dece6358
AM
30498+ *
30499+ * This program is distributed in the hope that it will be useful,
30500+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30501+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30502+ * GNU General Public License for more details.
30503+ *
30504+ * You should have received a copy of the GNU General Public License
523b37e3 30505+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30506+ */
30507+
30508+/*
30509+ * super_block operations
30510+ */
30511+
30512+#ifndef __AUFS_SUPER_H__
30513+#define __AUFS_SUPER_H__
30514+
30515+#ifdef __KERNEL__
30516+
30517+#include <linux/fs.h>
5527c038 30518+#include <linux/kobject.h>
8b6a4947 30519+#include "hbl.h"
acd2b654 30520+#include "lcnt.h"
1facf9fc 30521+#include "rwsem.h"
1facf9fc 30522+#include "wkq.h"
30523+
1facf9fc 30524+/* policies to select one among multiple writable branches */
30525+struct au_wbr_copyup_operations {
30526+ int (*copyup)(struct dentry *dentry);
30527+};
30528+
392086de
AM
30529+#define AuWbr_DIR 1 /* target is a dir */
30530+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30531+
30532+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30533+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30534+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30535+
1facf9fc 30536+struct au_wbr_create_operations {
392086de 30537+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30538+ int (*init)(struct super_block *sb);
30539+ int (*fin)(struct super_block *sb);
30540+};
30541+
30542+struct au_wbr_mfs {
30543+ struct mutex mfs_lock; /* protect this structure */
30544+ unsigned long mfs_jiffy;
30545+ unsigned long mfs_expire;
30546+ aufs_bindex_t mfs_bindex;
30547+
30548+ unsigned long long mfsrr_bytes;
30549+ unsigned long long mfsrr_watermark;
30550+};
30551+
86dc4139
AM
30552+#define AuPlink_NHASH 100
30553+static inline int au_plink_hash(ino_t ino)
30554+{
30555+ return ino % AuPlink_NHASH;
30556+}
30557+
076b876e
AM
30558+/* File-based Hierarchical Storage Management */
30559+struct au_fhsm {
30560+#ifdef CONFIG_AUFS_FHSM
30561+ /* allow only one process who can receive the notification */
30562+ spinlock_t fhsm_spin;
30563+ pid_t fhsm_pid;
30564+ wait_queue_head_t fhsm_wqh;
30565+ atomic_t fhsm_readable;
30566+
c1595e42 30567+ /* these are protected by si_rwsem */
076b876e 30568+ unsigned long fhsm_expire;
c1595e42 30569+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30570+#endif
30571+};
30572+
1facf9fc 30573+struct au_branch;
30574+struct au_sbinfo {
30575+ /* nowait tasks in the system-wide workqueue */
30576+ struct au_nowait_tasks si_nowait;
30577+
b752ccd1 30578+ /*
acd2b654 30579+ * tried sb->s_umount, but failed due to the dependency between i_mutex.
b752ccd1
AM
30580+ * rwsem for au_sbinfo is necessary.
30581+ */
dece6358 30582+ struct au_rwsem si_rwsem;
1facf9fc 30583+
7f207e10 30584+ /*
523b37e3
AM
30585+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30586+ * remount.
7f207e10 30587+ */
acd2b654 30588+ au_lcnt_t si_ninodes, si_nfiles;
7f207e10 30589+
1facf9fc 30590+ /* branch management */
30591+ unsigned int si_generation;
30592+
2000de60 30593+ /* see AuSi_ flags */
1facf9fc 30594+ unsigned char au_si_status;
30595+
5afbbe0d 30596+ aufs_bindex_t si_bbot;
7f207e10
AM
30597+
30598+ /* dirty trick to keep br_id plus */
30599+ unsigned int si_last_br_id :
30600+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30601+ struct au_branch **si_branch;
30602+
30603+ /* policy to select a writable branch */
30604+ unsigned char si_wbr_copyup;
30605+ unsigned char si_wbr_create;
30606+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30607+ struct au_wbr_create_operations *si_wbr_create_ops;
30608+
30609+ /* round robin */
30610+ atomic_t si_wbr_rr_next;
30611+
30612+ /* most free space */
30613+ struct au_wbr_mfs si_wbr_mfs;
30614+
076b876e
AM
30615+ /* File-based Hierarchical Storage Management */
30616+ struct au_fhsm si_fhsm;
30617+
1facf9fc 30618+ /* mount flags */
30619+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30620+ unsigned int si_mntflags;
30621+
30622+ /* external inode number (bitmap and translation table) */
5527c038
JR
30623+ vfs_readf_t si_xread;
30624+ vfs_writef_t si_xwrite;
acd2b654
AM
30625+ loff_t si_ximaxent; /* max entries in a xino */
30626+
1facf9fc 30627+ struct file *si_xib;
30628+ struct mutex si_xib_mtx; /* protect xib members */
30629+ unsigned long *si_xib_buf;
30630+ unsigned long si_xib_last_pindex;
30631+ int si_xib_next_bit;
acd2b654 30632+
392086de
AM
30633+ unsigned long si_xino_jiffy;
30634+ unsigned long si_xino_expire;
1facf9fc 30635+ /* reserved for future use */
30636+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30637+
30638+#ifdef CONFIG_AUFS_EXPORT
30639+ /* i_generation */
acd2b654 30640+ /* todo: make xigen file an array to support many inode numbers */
1facf9fc 30641+ struct file *si_xigen;
30642+ atomic_t si_xigen_next;
30643+#endif
30644+
acd2b654 30645+ /* dirty trick to support atomic_open */
8b6a4947 30646+ struct hlist_bl_head si_aopen;
b912730e 30647+
1facf9fc 30648+ /* vdir parameters */
e49829fe 30649+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30650+ unsigned int si_rdblk; /* deblk size */
30651+ unsigned int si_rdhash; /* hash size */
30652+
30653+ /*
30654+ * If the number of whiteouts are larger than si_dirwh, leave all of
30655+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30656+ * future fsck.aufs or kernel thread will remove them later.
30657+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30658+ */
30659+ unsigned int si_dirwh;
30660+
1facf9fc 30661+ /* pseudo_link list */
8b6a4947 30662+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30663+ wait_queue_head_t si_plink_wq;
4a4d8108 30664+ spinlock_t si_plink_maint_lock;
e49829fe 30665+ pid_t si_plink_maint_pid;
1facf9fc 30666+
523b37e3 30667+ /* file list */
8b6a4947 30668+ struct hlist_bl_head si_files;
523b37e3 30669+
b95c5147 30670+ /* with/without getattr, brother of sb->s_d_op */
43982f53 30671+ const struct inode_operations *si_iop_array;
b95c5147 30672+
1facf9fc 30673+ /*
30674+ * sysfs and lifetime management.
30675+ * this is not a small structure and it may be a waste of memory in case
acd2b654 30676+ * of sysfs is disabled, particularly when many aufs-es are mounted.
1facf9fc 30677+ * but using sysfs is majority.
30678+ */
30679+ struct kobject si_kobj;
30680+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30681+ struct dentry *si_dbgaufs;
30682+ struct dentry *si_dbgaufs_plink;
30683+ struct dentry *si_dbgaufs_xib;
1facf9fc 30684+#ifdef CONFIG_AUFS_EXPORT
30685+ struct dentry *si_dbgaufs_xigen;
30686+#endif
30687+#endif
30688+
e49829fe 30689+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30690+ struct hlist_bl_node si_list;
e49829fe
JR
30691+#endif
30692+
1facf9fc 30693+ /* dirty, necessary for unmounting, sysfs and sysrq */
30694+ struct super_block *si_sb;
30695+};
30696+
dece6358
AM
30697+/* sbinfo status flags */
30698+/*
30699+ * set true when refresh_dirs() failed at remount time.
30700+ * then try refreshing dirs at access time again.
062440b3 30701+ * if it is false, refreshing dirs at access time is unnecessary
dece6358 30702+ */
027c5e7a 30703+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30704+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30705+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30706+
30707+#ifndef CONFIG_AUFS_FHSM
30708+#undef AuSi_FHSM
30709+#define AuSi_FHSM 0
30710+#endif
30711+
dece6358
AM
30712+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30713+ unsigned int flag)
30714+{
30715+ AuRwMustAnyLock(&sbi->si_rwsem);
30716+ return sbi->au_si_status & flag;
30717+}
30718+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30719+#define au_fset_si(sbinfo, name) do { \
30720+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30721+ (sbinfo)->au_si_status |= AuSi_##name; \
30722+} while (0)
30723+#define au_fclr_si(sbinfo, name) do { \
30724+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30725+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30726+} while (0)
30727+
1facf9fc 30728+/* ---------------------------------------------------------------------- */
30729+
30730+/* policy to select one among writable branches */
4a4d8108
AM
30731+#define AuWbrCopyup(sbinfo, ...) \
30732+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30733+#define AuWbrCreate(sbinfo, ...) \
30734+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30735+
30736+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30737+#define AuLock_DW 1 /* write-lock dentry */
30738+#define AuLock_IR (1 << 1) /* read-lock inode */
30739+#define AuLock_IW (1 << 2) /* write-lock inode */
30740+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30741+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30742+ /* except RENAME_EXCHANGE */
e49829fe
JR
30743+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30744+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30745+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30746+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30747+#define au_fset_lock(flags, name) \
30748+ do { (flags) |= AuLock_##name; } while (0)
30749+#define au_fclr_lock(flags, name) \
30750+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30751+
30752+/* ---------------------------------------------------------------------- */
30753+
30754+/* super.c */
30755+extern struct file_system_type aufs_fs_type;
30756+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30757+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30758+ unsigned long long max, void *arg);
79b8bda9
AM
30759+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30760+ struct super_block *sb, void *arg);
7f207e10
AM
30761+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30762+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30763+
30764+/* sbinfo.c */
30765+void au_si_free(struct kobject *kobj);
30766+int au_si_alloc(struct super_block *sb);
e2f27e51 30767+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30768+
30769+unsigned int au_sigen_inc(struct super_block *sb);
30770+aufs_bindex_t au_new_br_id(struct super_block *sb);
30771+
e49829fe
JR
30772+int si_read_lock(struct super_block *sb, int flags);
30773+int si_write_lock(struct super_block *sb, int flags);
30774+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30775+void aufs_read_unlock(struct dentry *dentry, int flags);
30776+void aufs_write_lock(struct dentry *dentry);
30777+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30778+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30779+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30780+
30781+/* wbr_policy.c */
30782+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30783+extern struct au_wbr_create_operations au_wbr_create_ops[];
30784+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30785+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30786+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30787+
30788+/* mvdown.c */
30789+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30790+
076b876e
AM
30791+#ifdef CONFIG_AUFS_FHSM
30792+/* fhsm.c */
30793+
30794+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30795+{
30796+ pid_t pid;
30797+
30798+ spin_lock(&fhsm->fhsm_spin);
30799+ pid = fhsm->fhsm_pid;
30800+ spin_unlock(&fhsm->fhsm_spin);
30801+
30802+ return pid;
30803+}
30804+
30805+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30806+void au_fhsm_wrote_all(struct super_block *sb, int force);
30807+int au_fhsm_fd(struct super_block *sb, int oflags);
30808+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30809+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30810+void au_fhsm_fin(struct super_block *sb);
30811+void au_fhsm_init(struct au_sbinfo *sbinfo);
30812+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30813+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30814+#else
30815+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30816+ int force)
30817+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30818+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30819+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30820+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30821+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30822+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30823+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30824+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30825+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30826+#endif
30827+
1facf9fc 30828+/* ---------------------------------------------------------------------- */
30829+
30830+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30831+{
30832+ return sb->s_fs_info;
30833+}
30834+
30835+/* ---------------------------------------------------------------------- */
30836+
30837+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30838+int au_test_nfsd(void);
1facf9fc 30839+void au_export_init(struct super_block *sb);
b752ccd1 30840+void au_xigen_inc(struct inode *inode);
1facf9fc 30841+int au_xigen_new(struct inode *inode);
062440b3 30842+int au_xigen_set(struct super_block *sb, struct path *path);
1facf9fc 30843+void au_xigen_clr(struct super_block *sb);
30844+
30845+static inline int au_busy_or_stale(void)
30846+{
b752ccd1 30847+ if (!au_test_nfsd())
1facf9fc 30848+ return -EBUSY;
30849+ return -ESTALE;
30850+}
30851+#else
b752ccd1 30852+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30853+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30854+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108 30855+AuStubInt0(au_xigen_new, struct inode *inode)
062440b3 30856+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
4a4d8108 30857+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30858+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30859+#endif /* CONFIG_AUFS_EXPORT */
30860+
30861+/* ---------------------------------------------------------------------- */
30862+
e49829fe
JR
30863+#ifdef CONFIG_AUFS_SBILIST
30864+/* module.c */
8b6a4947 30865+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30866+
30867+static inline void au_sbilist_init(void)
30868+{
8b6a4947 30869+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30870+}
30871+
30872+static inline void au_sbilist_add(struct super_block *sb)
30873+{
8b6a4947 30874+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30875+}
30876+
30877+static inline void au_sbilist_del(struct super_block *sb)
30878+{
8b6a4947 30879+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30880+}
53392da6
AM
30881+
30882+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30883+static inline void au_sbilist_lock(void)
30884+{
8b6a4947 30885+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30886+}
30887+
30888+static inline void au_sbilist_unlock(void)
30889+{
8b6a4947 30890+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30891+}
30892+#define AuGFP_SBILIST GFP_ATOMIC
30893+#else
30894+AuStubVoid(au_sbilist_lock, void)
30895+AuStubVoid(au_sbilist_unlock, void)
30896+#define AuGFP_SBILIST GFP_NOFS
30897+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
30898+#else
30899+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
30900+AuStubVoid(au_sbilist_add, struct super_block *sb)
30901+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
30902+AuStubVoid(au_sbilist_lock, void)
30903+AuStubVoid(au_sbilist_unlock, void)
30904+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
30905+#endif
30906+
30907+/* ---------------------------------------------------------------------- */
30908+
1facf9fc 30909+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30910+{
dece6358 30911+ /*
c1595e42 30912+ * This function is a dynamic '__init' function actually,
dece6358
AM
30913+ * so the tiny check for si_rwsem is unnecessary.
30914+ */
30915+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 30916+#ifdef CONFIG_DEBUG_FS
30917+ sbinfo->si_dbgaufs = NULL;
86dc4139 30918+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 30919+ sbinfo->si_dbgaufs_xib = NULL;
30920+#ifdef CONFIG_AUFS_EXPORT
30921+ sbinfo->si_dbgaufs_xigen = NULL;
30922+#endif
30923+#endif
30924+}
30925+
30926+/* ---------------------------------------------------------------------- */
30927+
a2654f78
AM
30928+/* current->atomic_flags */
30929+/* this value should never corrupt the ones defined in linux/sched.h */
fbc438ed 30930+#define PFA_AUFS 0x10
a2654f78
AM
30931+
30932+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30933+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
30934+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
30935+
30936+static inline int si_pid_test(struct super_block *sb)
30937+{
a2654f78 30938+ return !!task_test_aufs(current);
b752ccd1
AM
30939+}
30940+
30941+static inline void si_pid_clr(struct super_block *sb)
30942+{
a2654f78
AM
30943+ AuDebugOn(!task_test_aufs(current));
30944+ task_clear_aufs(current);
b752ccd1
AM
30945+}
30946+
a2654f78
AM
30947+static inline void si_pid_set(struct super_block *sb)
30948+{
30949+ AuDebugOn(task_test_aufs(current));
30950+ task_set_aufs(current);
30951+}
febd17d6 30952+
b752ccd1
AM
30953+/* ---------------------------------------------------------------------- */
30954+
1facf9fc 30955+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
30956+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30957+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30958+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30959+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30960+/*
30961+#define __si_read_trylock_nested(sb) \
30962+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30963+#define __si_write_trylock_nested(sb) \
30964+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30965+*/
30966+
30967+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30968+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30969+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 30970+
dece6358
AM
30971+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30972+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30973+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30974+
b752ccd1
AM
30975+static inline void si_noflush_read_lock(struct super_block *sb)
30976+{
30977+ __si_read_lock(sb);
30978+ si_pid_set(sb);
30979+}
30980+
30981+static inline int si_noflush_read_trylock(struct super_block *sb)
30982+{
076b876e
AM
30983+ int locked;
30984+
30985+ locked = __si_read_trylock(sb);
b752ccd1
AM
30986+ if (locked)
30987+ si_pid_set(sb);
30988+ return locked;
30989+}
30990+
30991+static inline void si_noflush_write_lock(struct super_block *sb)
30992+{
30993+ __si_write_lock(sb);
30994+ si_pid_set(sb);
30995+}
30996+
30997+static inline int si_noflush_write_trylock(struct super_block *sb)
30998+{
076b876e
AM
30999+ int locked;
31000+
31001+ locked = __si_write_trylock(sb);
b752ccd1
AM
31002+ if (locked)
31003+ si_pid_set(sb);
31004+ return locked;
31005+}
31006+
7e9cd9fe 31007+#if 0 /* reserved */
1facf9fc 31008+static inline int si_read_trylock(struct super_block *sb, int flags)
31009+{
31010+ if (au_ftest_lock(flags, FLUSH))
31011+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31012+ return si_noflush_read_trylock(sb);
31013+}
e49829fe 31014+#endif
1facf9fc 31015+
b752ccd1
AM
31016+static inline void si_read_unlock(struct super_block *sb)
31017+{
31018+ si_pid_clr(sb);
31019+ __si_read_unlock(sb);
31020+}
31021+
7e9cd9fe 31022+#if 0 /* reserved */
1facf9fc 31023+static inline int si_write_trylock(struct super_block *sb, int flags)
31024+{
31025+ if (au_ftest_lock(flags, FLUSH))
31026+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31027+ return si_noflush_write_trylock(sb);
31028+}
b752ccd1
AM
31029+#endif
31030+
31031+static inline void si_write_unlock(struct super_block *sb)
31032+{
31033+ si_pid_clr(sb);
31034+ __si_write_unlock(sb);
31035+}
31036+
7e9cd9fe 31037+#if 0 /* reserved */
b752ccd1
AM
31038+static inline void si_downgrade_lock(struct super_block *sb)
31039+{
31040+ __si_downgrade_lock(sb);
31041+}
31042+#endif
1facf9fc 31043+
31044+/* ---------------------------------------------------------------------- */
31045+
5afbbe0d 31046+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 31047+{
dece6358 31048+ SiMustAnyLock(sb);
5afbbe0d 31049+ return au_sbi(sb)->si_bbot;
1facf9fc 31050+}
31051+
31052+static inline unsigned int au_mntflags(struct super_block *sb)
31053+{
dece6358 31054+ SiMustAnyLock(sb);
1facf9fc 31055+ return au_sbi(sb)->si_mntflags;
31056+}
31057+
31058+static inline unsigned int au_sigen(struct super_block *sb)
31059+{
dece6358 31060+ SiMustAnyLock(sb);
1facf9fc 31061+ return au_sbi(sb)->si_generation;
31062+}
31063+
31064+static inline struct au_branch *au_sbr(struct super_block *sb,
31065+ aufs_bindex_t bindex)
31066+{
dece6358 31067+ SiMustAnyLock(sb);
1facf9fc 31068+ return au_sbi(sb)->si_branch[0 + bindex];
31069+}
31070+
acd2b654 31071+static inline loff_t au_xi_maxent(struct super_block *sb)
1facf9fc 31072+{
dece6358 31073+ SiMustAnyLock(sb);
acd2b654 31074+ return au_sbi(sb)->si_ximaxent;
1facf9fc 31075+}
31076+
31077+#endif /* __KERNEL__ */
31078+#endif /* __AUFS_SUPER_H__ */
7f207e10 31079diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
eca34b5c 31080--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
016522bc 31081+++ linux/fs/aufs/sysaufs.c 2020-01-27 10:57:18.178871751 +0100
062440b3 31082@@ -0,0 +1,93 @@
cd7a4cd9 31083+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31084+/*
016522bc 31085+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31086+ *
31087+ * This program, aufs is free software; you can redistribute it and/or modify
31088+ * it under the terms of the GNU General Public License as published by
31089+ * the Free Software Foundation; either version 2 of the License, or
31090+ * (at your option) any later version.
dece6358
AM
31091+ *
31092+ * This program is distributed in the hope that it will be useful,
31093+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31094+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31095+ * GNU General Public License for more details.
31096+ *
31097+ * You should have received a copy of the GNU General Public License
523b37e3 31098+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31099+ */
31100+
31101+/*
31102+ * sysfs interface and lifetime management
31103+ * they are necessary regardless sysfs is disabled.
31104+ */
31105+
1facf9fc 31106+#include <linux/random.h>
1facf9fc 31107+#include "aufs.h"
31108+
31109+unsigned long sysaufs_si_mask;
e49829fe 31110+struct kset *sysaufs_kset;
1facf9fc 31111+
31112+#define AuSiAttr(_name) { \
31113+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
31114+ .show = sysaufs_si_##_name, \
31115+}
31116+
31117+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31118+struct attribute *sysaufs_si_attrs[] = {
31119+ &sysaufs_si_attr_xi_path.attr,
31120+ NULL,
31121+};
31122+
4a4d8108 31123+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 31124+ .show = sysaufs_si_show
31125+};
31126+
31127+static struct kobj_type au_sbi_ktype = {
31128+ .release = au_si_free,
31129+ .sysfs_ops = &au_sbi_ops,
31130+ .default_attrs = sysaufs_si_attrs
31131+};
31132+
31133+/* ---------------------------------------------------------------------- */
31134+
31135+int sysaufs_si_init(struct au_sbinfo *sbinfo)
31136+{
31137+ int err;
31138+
e49829fe 31139+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 31140+ /* cf. sysaufs_name() */
31141+ err = kobject_init_and_add
e49829fe 31142+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 31143+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31144+
1facf9fc 31145+ return err;
31146+}
31147+
31148+void sysaufs_fin(void)
31149+{
e49829fe
JR
31150+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31151+ kset_unregister(sysaufs_kset);
1facf9fc 31152+}
31153+
31154+int __init sysaufs_init(void)
31155+{
31156+ int err;
31157+
31158+ do {
31159+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31160+ } while (!sysaufs_si_mask);
31161+
4a4d8108 31162+ err = -EINVAL;
e49829fe
JR
31163+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31164+ if (unlikely(!sysaufs_kset))
4a4d8108 31165+ goto out;
e49829fe
JR
31166+ err = PTR_ERR(sysaufs_kset);
31167+ if (IS_ERR(sysaufs_kset))
1facf9fc 31168+ goto out;
e49829fe 31169+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
062440b3 31170+ if (unlikely(err))
e49829fe 31171+ kset_unregister(sysaufs_kset);
1facf9fc 31172+
4f0767ce 31173+out:
1facf9fc 31174+ return err;
31175+}
7f207e10 31176diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
eca34b5c 31177--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
016522bc 31178+++ linux/fs/aufs/sysaufs.h 2020-01-27 10:57:18.178871751 +0100
062440b3
AM
31179@@ -0,0 +1,102 @@
31180+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31181+/*
016522bc 31182+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31183+ *
31184+ * This program, aufs is free software; you can redistribute it and/or modify
31185+ * it under the terms of the GNU General Public License as published by
31186+ * the Free Software Foundation; either version 2 of the License, or
31187+ * (at your option) any later version.
dece6358
AM
31188+ *
31189+ * This program is distributed in the hope that it will be useful,
31190+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31191+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31192+ * GNU General Public License for more details.
31193+ *
31194+ * You should have received a copy of the GNU General Public License
523b37e3 31195+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31196+ */
31197+
31198+/*
31199+ * sysfs interface and mount lifetime management
31200+ */
31201+
31202+#ifndef __SYSAUFS_H__
31203+#define __SYSAUFS_H__
31204+
31205+#ifdef __KERNEL__
31206+
1facf9fc 31207+#include <linux/sysfs.h>
1facf9fc 31208+#include "module.h"
31209+
dece6358
AM
31210+struct super_block;
31211+struct au_sbinfo;
31212+
1facf9fc 31213+struct sysaufs_si_attr {
31214+ struct attribute attr;
31215+ int (*show)(struct seq_file *seq, struct super_block *sb);
31216+};
31217+
31218+/* ---------------------------------------------------------------------- */
31219+
31220+/* sysaufs.c */
31221+extern unsigned long sysaufs_si_mask;
e49829fe 31222+extern struct kset *sysaufs_kset;
1facf9fc 31223+extern struct attribute *sysaufs_si_attrs[];
31224+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31225+int __init sysaufs_init(void);
31226+void sysaufs_fin(void);
31227+
31228+/* ---------------------------------------------------------------------- */
31229+
31230+/* some people doesn't like to show a pointer in kernel */
31231+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31232+{
31233+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31234+}
31235+
31236+#define SysaufsSiNamePrefix "si_"
31237+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31238+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31239+{
31240+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31241+ sysaufs_si_id(sbinfo));
31242+}
31243+
31244+struct au_branch;
31245+#ifdef CONFIG_SYSFS
31246+/* sysfs.c */
31247+extern struct attribute_group *sysaufs_attr_group;
31248+
31249+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31250+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31251+ char *buf);
076b876e
AM
31252+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31253+#ifdef CONFIG_COMPAT
31254+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31255+#endif
1facf9fc 31256+
31257+void sysaufs_br_init(struct au_branch *br);
31258+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31259+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31260+
31261+#define sysaufs_brs_init() do {} while (0)
31262+
31263+#else
31264+#define sysaufs_attr_group NULL
31265+
4a4d8108 31266+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
31267+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31268+ struct attribute *attr, char *buf)
4a4d8108
AM
31269+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31270+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31271+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 31272+
31273+static inline void sysaufs_brs_init(void)
31274+{
31275+ sysaufs_brs = 0;
31276+}
31277+
31278+#endif /* CONFIG_SYSFS */
31279+
31280+#endif /* __KERNEL__ */
31281+#endif /* __SYSAUFS_H__ */
7f207e10 31282diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
eca34b5c 31283--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
016522bc 31284+++ linux/fs/aufs/sysfs.c 2020-01-27 10:57:18.178871751 +0100
ba1aed25 31285@@ -0,0 +1,374 @@
cd7a4cd9 31286+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31287+/*
016522bc 31288+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31289+ *
31290+ * This program, aufs is free software; you can redistribute it and/or modify
31291+ * it under the terms of the GNU General Public License as published by
31292+ * the Free Software Foundation; either version 2 of the License, or
31293+ * (at your option) any later version.
dece6358
AM
31294+ *
31295+ * This program is distributed in the hope that it will be useful,
31296+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31297+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31298+ * GNU General Public License for more details.
31299+ *
31300+ * You should have received a copy of the GNU General Public License
523b37e3 31301+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31302+ */
31303+
31304+/*
31305+ * sysfs interface
31306+ */
31307+
076b876e 31308+#include <linux/compat.h>
1facf9fc 31309+#include <linux/seq_file.h>
1facf9fc 31310+#include "aufs.h"
31311+
4a4d8108
AM
31312+#ifdef CONFIG_AUFS_FS_MODULE
31313+/* this entry violates the "one line per file" policy of sysfs */
31314+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31315+ char *buf)
31316+{
31317+ ssize_t err;
31318+ static char *conf =
31319+/* this file is generated at compiling */
31320+#include "conf.str"
31321+ ;
31322+
31323+ err = snprintf(buf, PAGE_SIZE, conf);
31324+ if (unlikely(err >= PAGE_SIZE))
31325+ err = -EFBIG;
31326+ return err;
31327+}
31328+
31329+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31330+#endif
31331+
1facf9fc 31332+static struct attribute *au_attr[] = {
4a4d8108
AM
31333+#ifdef CONFIG_AUFS_FS_MODULE
31334+ &au_config_attr.attr,
31335+#endif
1facf9fc 31336+ NULL, /* need to NULL terminate the list of attributes */
31337+};
31338+
31339+static struct attribute_group sysaufs_attr_group_body = {
31340+ .attrs = au_attr
31341+};
31342+
31343+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31344+
31345+/* ---------------------------------------------------------------------- */
31346+
31347+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31348+{
31349+ int err;
31350+
dece6358
AM
31351+ SiMustAnyLock(sb);
31352+
1facf9fc 31353+ err = 0;
31354+ if (au_opt_test(au_mntflags(sb), XINO)) {
31355+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31356+ seq_putc(seq, '\n');
31357+ }
31358+ return err;
31359+}
31360+
31361+/*
31362+ * the lifetime of branch is independent from the entry under sysfs.
31363+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31364+ * unlinked.
31365+ */
31366+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31367+ aufs_bindex_t bindex, int idx)
1facf9fc 31368+{
1e00d052 31369+ int err;
1facf9fc 31370+ struct path path;
31371+ struct dentry *root;
31372+ struct au_branch *br;
076b876e 31373+ au_br_perm_str_t perm;
1facf9fc 31374+
31375+ AuDbg("b%d\n", bindex);
31376+
1e00d052 31377+ err = 0;
1facf9fc 31378+ root = sb->s_root;
31379+ di_read_lock_parent(root, !AuLock_IR);
31380+ br = au_sbr(sb, bindex);
392086de
AM
31381+
31382+ switch (idx) {
31383+ case AuBrSysfs_BR:
31384+ path.mnt = au_br_mnt(br);
31385+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31386+ err = au_seq_path(seq, &path);
31387+ if (!err) {
31388+ au_optstr_br_perm(&perm, br->br_perm);
31389+ seq_printf(seq, "=%s\n", perm.a);
31390+ }
392086de
AM
31391+ break;
31392+ case AuBrSysfs_BRID:
79b8bda9 31393+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31394+ break;
31395+ }
076b876e 31396+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31397+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31398+ err = -E2BIG;
392086de 31399+
1e00d052 31400+ return err;
1facf9fc 31401+}
31402+
31403+/* ---------------------------------------------------------------------- */
31404+
31405+static struct seq_file *au_seq(char *p, ssize_t len)
31406+{
31407+ struct seq_file *seq;
31408+
31409+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31410+ if (seq) {
31411+ /* mutex_init(&seq.lock); */
31412+ seq->buf = p;
31413+ seq->size = len;
31414+ return seq; /* success */
31415+ }
31416+
31417+ seq = ERR_PTR(-ENOMEM);
31418+ return seq;
31419+}
31420+
392086de
AM
31421+#define SysaufsBr_PREFIX "br"
31422+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31423+
31424+/* todo: file size may exceed PAGE_SIZE */
31425+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31426+ char *buf)
1facf9fc 31427+{
31428+ ssize_t err;
392086de 31429+ int idx;
1facf9fc 31430+ long l;
5afbbe0d 31431+ aufs_bindex_t bbot;
1facf9fc 31432+ struct au_sbinfo *sbinfo;
31433+ struct super_block *sb;
31434+ struct seq_file *seq;
31435+ char *name;
31436+ struct attribute **cattr;
31437+
31438+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31439+ sb = sbinfo->si_sb;
1308ab2a 31440+
31441+ /*
31442+ * prevent a race condition between sysfs and aufs.
31443+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31444+ * prohibits maintaining the sysfs entries.
31445+ * hew we acquire read lock after sysfs_get_active_two().
31446+ * on the other hand, the remount process may maintain the sysfs/aufs
31447+ * entries after acquiring write lock.
31448+ * it can cause a deadlock.
31449+ * simply we gave up processing read here.
31450+ */
31451+ err = -EBUSY;
31452+ if (unlikely(!si_noflush_read_trylock(sb)))
31453+ goto out;
1facf9fc 31454+
31455+ seq = au_seq(buf, PAGE_SIZE);
31456+ err = PTR_ERR(seq);
31457+ if (IS_ERR(seq))
1308ab2a 31458+ goto out_unlock;
1facf9fc 31459+
31460+ name = (void *)attr->name;
31461+ cattr = sysaufs_si_attrs;
31462+ while (*cattr) {
31463+ if (!strcmp(name, (*cattr)->name)) {
31464+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31465+ ->show(seq, sb);
31466+ goto out_seq;
31467+ }
31468+ cattr++;
31469+ }
31470+
392086de
AM
31471+ if (!strncmp(name, SysaufsBrid_PREFIX,
31472+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31473+ idx = AuBrSysfs_BRID;
31474+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31475+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31476+ sizeof(SysaufsBr_PREFIX) - 1)) {
31477+ idx = AuBrSysfs_BR;
1facf9fc 31478+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31479+ } else
31480+ BUG();
31481+
31482+ err = kstrtol(name, 10, &l);
31483+ if (!err) {
5afbbe0d
AM
31484+ bbot = au_sbbot(sb);
31485+ if (l <= bbot)
392086de
AM
31486+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31487+ else
31488+ err = -ENOENT;
1facf9fc 31489+ }
1facf9fc 31490+
4f0767ce 31491+out_seq:
1facf9fc 31492+ if (!err) {
31493+ err = seq->count;
31494+ /* sysfs limit */
31495+ if (unlikely(err == PAGE_SIZE))
31496+ err = -EFBIG;
31497+ }
9f237c51 31498+ au_kfree_rcu(seq);
4f0767ce 31499+out_unlock:
1facf9fc 31500+ si_read_unlock(sb);
4f0767ce 31501+out:
1facf9fc 31502+ return err;
31503+}
31504+
31505+/* ---------------------------------------------------------------------- */
31506+
076b876e
AM
31507+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31508+{
31509+ int err;
31510+ int16_t brid;
5afbbe0d 31511+ aufs_bindex_t bindex, bbot;
076b876e
AM
31512+ size_t sz;
31513+ char *buf;
31514+ struct seq_file *seq;
31515+ struct au_branch *br;
31516+
31517+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31518+ bbot = au_sbbot(sb);
31519+ err = bbot + 1;
076b876e
AM
31520+ if (!arg)
31521+ goto out;
31522+
31523+ err = -ENOMEM;
31524+ buf = (void *)__get_free_page(GFP_NOFS);
31525+ if (unlikely(!buf))
31526+ goto out;
31527+
31528+ seq = au_seq(buf, PAGE_SIZE);
31529+ err = PTR_ERR(seq);
31530+ if (IS_ERR(seq))
31531+ goto out_buf;
31532+
31533+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31534+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
ba1aed25
AM
31535+ /* VERIFY_WRITE */
31536+ err = !access_ok(arg, sizeof(*arg));
076b876e
AM
31537+ if (unlikely(err))
31538+ break;
31539+
31540+ br = au_sbr(sb, bindex);
31541+ brid = br->br_id;
31542+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31543+ err = __put_user(brid, &arg->id);
31544+ if (unlikely(err))
31545+ break;
31546+
31547+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31548+ err = __put_user(br->br_perm, &arg->perm);
31549+ if (unlikely(err))
31550+ break;
31551+
79b8bda9
AM
31552+ err = au_seq_path(seq, &br->br_path);
31553+ if (unlikely(err))
31554+ break;
31555+ seq_putc(seq, '\0');
31556+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31557+ err = copy_to_user(arg->path, seq->buf, seq->count);
31558+ seq->count = 0;
31559+ if (unlikely(err))
31560+ break;
31561+ } else {
31562+ err = -E2BIG;
31563+ goto out_seq;
31564+ }
31565+ }
31566+ if (unlikely(err))
31567+ err = -EFAULT;
31568+
31569+out_seq:
9f237c51 31570+ au_kfree_rcu(seq);
076b876e 31571+out_buf:
1c60b727 31572+ free_page((unsigned long)buf);
076b876e
AM
31573+out:
31574+ si_read_unlock(sb);
31575+ return err;
31576+}
31577+
31578+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31579+{
2000de60 31580+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31581+}
31582+
31583+#ifdef CONFIG_COMPAT
31584+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31585+{
2000de60 31586+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31587+}
31588+#endif
31589+
31590+/* ---------------------------------------------------------------------- */
31591+
1facf9fc 31592+void sysaufs_br_init(struct au_branch *br)
31593+{
392086de
AM
31594+ int i;
31595+ struct au_brsysfs *br_sysfs;
31596+ struct attribute *attr;
4a4d8108 31597+
392086de
AM
31598+ br_sysfs = br->br_sysfs;
31599+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31600+ attr = &br_sysfs->attr;
31601+ sysfs_attr_init(attr);
31602+ attr->name = br_sysfs->name;
cd7a4cd9 31603+ attr->mode = 0444;
392086de
AM
31604+ br_sysfs++;
31605+ }
1facf9fc 31606+}
31607+
31608+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31609+{
31610+ struct au_branch *br;
31611+ struct kobject *kobj;
392086de
AM
31612+ struct au_brsysfs *br_sysfs;
31613+ int i;
5afbbe0d 31614+ aufs_bindex_t bbot;
1facf9fc 31615+
1facf9fc 31616+ if (!sysaufs_brs)
31617+ return;
31618+
31619+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31620+ bbot = au_sbbot(sb);
31621+ for (; bindex <= bbot; bindex++) {
1facf9fc 31622+ br = au_sbr(sb, bindex);
392086de
AM
31623+ br_sysfs = br->br_sysfs;
31624+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31625+ sysfs_remove_file(kobj, &br_sysfs->attr);
31626+ br_sysfs++;
31627+ }
1facf9fc 31628+ }
31629+}
31630+
31631+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31632+{
392086de 31633+ int err, i;
5afbbe0d 31634+ aufs_bindex_t bbot;
1facf9fc 31635+ struct kobject *kobj;
31636+ struct au_branch *br;
392086de 31637+ struct au_brsysfs *br_sysfs;
1facf9fc 31638+
1facf9fc 31639+ if (!sysaufs_brs)
31640+ return;
31641+
31642+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31643+ bbot = au_sbbot(sb);
31644+ for (; bindex <= bbot; bindex++) {
1facf9fc 31645+ br = au_sbr(sb, bindex);
392086de
AM
31646+ br_sysfs = br->br_sysfs;
31647+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31648+ SysaufsBr_PREFIX "%d", bindex);
31649+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31650+ SysaufsBrid_PREFIX "%d", bindex);
31651+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31652+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31653+ if (unlikely(err))
31654+ pr_warn("failed %s under sysfs(%d)\n",
31655+ br_sysfs->name, err);
31656+ br_sysfs++;
31657+ }
1facf9fc 31658+ }
31659+}
7f207e10 31660diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
eca34b5c 31661--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
016522bc 31662+++ linux/fs/aufs/sysrq.c 2020-01-27 10:57:18.178871751 +0100
43982f53 31663@@ -0,0 +1,149 @@
cd7a4cd9 31664+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31665+/*
016522bc 31666+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31667+ *
31668+ * This program, aufs is free software; you can redistribute it and/or modify
31669+ * it under the terms of the GNU General Public License as published by
31670+ * the Free Software Foundation; either version 2 of the License, or
31671+ * (at your option) any later version.
dece6358
AM
31672+ *
31673+ * This program is distributed in the hope that it will be useful,
31674+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31675+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31676+ * GNU General Public License for more details.
31677+ *
31678+ * You should have received a copy of the GNU General Public License
523b37e3 31679+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31680+ */
31681+
31682+/*
acd2b654 31683+ * magic sysrq handler
1facf9fc 31684+ */
31685+
1facf9fc 31686+/* #include <linux/sysrq.h> */
027c5e7a 31687+#include <linux/writeback.h>
1facf9fc 31688+#include "aufs.h"
31689+
31690+/* ---------------------------------------------------------------------- */
31691+
31692+static void sysrq_sb(struct super_block *sb)
31693+{
31694+ char *plevel;
31695+ struct au_sbinfo *sbinfo;
31696+ struct file *file;
8b6a4947
AM
31697+ struct hlist_bl_head *files;
31698+ struct hlist_bl_node *pos;
523b37e3 31699+ struct au_finfo *finfo;
43982f53 31700+ struct inode *i;
1facf9fc 31701+
31702+ plevel = au_plevel;
31703+ au_plevel = KERN_WARNING;
1facf9fc 31704+
4a4d8108 31705+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31706+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31707+
31708+ sbinfo = au_sbi(sb);
4a4d8108 31709+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31710+ pr("superblock\n");
1facf9fc 31711+ au_dpri_sb(sb);
027c5e7a 31712+
43982f53 31713+#if 0 /* reserved */
027c5e7a
AM
31714+ do {
31715+ int err, i, j, ndentry;
31716+ struct au_dcsub_pages dpages;
31717+ struct au_dpage *dpage;
31718+
31719+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31720+ if (unlikely(err))
31721+ break;
31722+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31723+ if (!err)
31724+ for (i = 0; i < dpages.ndpage; i++) {
31725+ dpage = dpages.dpages + i;
31726+ ndentry = dpage->ndentry;
31727+ for (j = 0; j < ndentry; j++)
31728+ au_dpri_dentry(dpage->dentries[j]);
31729+ }
31730+ au_dpages_free(&dpages);
31731+ } while (0);
31732+#endif
31733+
43982f53
AM
31734+ pr("isolated inode\n");
31735+ spin_lock(&sb->s_inode_list_lock);
31736+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31737+ spin_lock(&i->i_lock);
31738+ if (hlist_empty(&i->i_dentry))
31739+ au_dpri_inode(i);
31740+ spin_unlock(&i->i_lock);
027c5e7a 31741+ }
43982f53
AM
31742+ spin_unlock(&sb->s_inode_list_lock);
31743+
c06a8ce3 31744+ pr("files\n");
523b37e3 31745+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31746+ hlist_bl_lock(files);
31747+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31748+ umode_t mode;
076b876e 31749+
523b37e3 31750+ file = finfo->fi_file;
c06a8ce3 31751+ mode = file_inode(file)->i_mode;
38d290e6 31752+ if (!special_file(mode))
1facf9fc 31753+ au_dpri_file(file);
523b37e3 31754+ }
8b6a4947 31755+ hlist_bl_unlock(files);
c06a8ce3 31756+ pr("done\n");
1facf9fc 31757+
c06a8ce3 31758+#undef pr
1facf9fc 31759+ au_plevel = plevel;
1facf9fc 31760+}
31761+
31762+/* ---------------------------------------------------------------------- */
31763+
31764+/* module parameter */
31765+static char *aufs_sysrq_key = "a";
cd7a4cd9 31766+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
1facf9fc 31767+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31768+
0c5527e5 31769+static void au_sysrq(int key __maybe_unused)
1facf9fc 31770+{
1facf9fc 31771+ struct au_sbinfo *sbinfo;
8b6a4947 31772+ struct hlist_bl_node *pos;
1facf9fc 31773+
027c5e7a 31774+ lockdep_off();
53392da6 31775+ au_sbilist_lock();
8b6a4947 31776+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31777+ sysrq_sb(sbinfo->si_sb);
53392da6 31778+ au_sbilist_unlock();
027c5e7a 31779+ lockdep_on();
1facf9fc 31780+}
31781+
31782+static struct sysrq_key_op au_sysrq_op = {
31783+ .handler = au_sysrq,
31784+ .help_msg = "Aufs",
31785+ .action_msg = "Aufs",
31786+ .enable_mask = SYSRQ_ENABLE_DUMP
31787+};
31788+
31789+/* ---------------------------------------------------------------------- */
31790+
31791+int __init au_sysrq_init(void)
31792+{
31793+ int err;
31794+ char key;
31795+
31796+ err = -1;
31797+ key = *aufs_sysrq_key;
31798+ if ('a' <= key && key <= 'z')
31799+ err = register_sysrq_key(key, &au_sysrq_op);
31800+ if (unlikely(err))
4a4d8108 31801+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31802+ return err;
31803+}
31804+
31805+void au_sysrq_fin(void)
31806+{
31807+ int err;
076b876e 31808+
1facf9fc 31809+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31810+ if (unlikely(err))
4a4d8108 31811+ pr_err("err %d (ignored)\n", err);
1facf9fc 31812+}
7f207e10 31813diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
eca34b5c 31814--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
016522bc 31815+++ linux/fs/aufs/vdir.c 2020-01-27 10:57:18.178871751 +0100
eca801bf 31816@@ -0,0 +1,896 @@
cd7a4cd9 31817+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31818+/*
016522bc 31819+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31820+ *
31821+ * This program, aufs is free software; you can redistribute it and/or modify
31822+ * it under the terms of the GNU General Public License as published by
31823+ * the Free Software Foundation; either version 2 of the License, or
31824+ * (at your option) any later version.
dece6358
AM
31825+ *
31826+ * This program is distributed in the hope that it will be useful,
31827+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31828+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31829+ * GNU General Public License for more details.
31830+ *
31831+ * You should have received a copy of the GNU General Public License
523b37e3 31832+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31833+ */
31834+
31835+/*
31836+ * virtual or vertical directory
31837+ */
31838+
eca801bf 31839+#include <linux/iversion.h>
1facf9fc 31840+#include "aufs.h"
31841+
dece6358 31842+static unsigned int calc_size(int nlen)
1facf9fc 31843+{
dece6358 31844+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31845+}
31846+
31847+static int set_deblk_end(union au_vdir_deblk_p *p,
31848+ union au_vdir_deblk_p *deblk_end)
31849+{
31850+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31851+ p->de->de_str.len = 0;
31852+ /* smp_mb(); */
31853+ return 0;
31854+ }
31855+ return -1; /* error */
31856+}
31857+
31858+/* returns true or false */
31859+static int is_deblk_end(union au_vdir_deblk_p *p,
31860+ union au_vdir_deblk_p *deblk_end)
31861+{
31862+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31863+ return !p->de->de_str.len;
31864+ return 1;
31865+}
31866+
31867+static unsigned char *last_deblk(struct au_vdir *vdir)
31868+{
31869+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31870+}
31871+
31872+/* ---------------------------------------------------------------------- */
31873+
79b8bda9 31874+/* estimate the appropriate size for name hash table */
1308ab2a 31875+unsigned int au_rdhash_est(loff_t sz)
31876+{
31877+ unsigned int n;
31878+
31879+ n = UINT_MAX;
31880+ sz >>= 10;
31881+ if (sz < n)
31882+ n = sz;
31883+ if (sz < AUFS_RDHASH_DEF)
31884+ n = AUFS_RDHASH_DEF;
4a4d8108 31885+ /* pr_info("n %u\n", n); */
1308ab2a 31886+ return n;
31887+}
31888+
1facf9fc 31889+/*
31890+ * the allocated memory has to be freed by
dece6358 31891+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 31892+ */
dece6358 31893+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 31894+{
1facf9fc 31895+ struct hlist_head *head;
dece6358 31896+ unsigned int u;
076b876e 31897+ size_t sz;
1facf9fc 31898+
076b876e
AM
31899+ sz = sizeof(*nhash->nh_head) * num_hash;
31900+ head = kmalloc(sz, gfp);
dece6358
AM
31901+ if (head) {
31902+ nhash->nh_num = num_hash;
31903+ nhash->nh_head = head;
31904+ for (u = 0; u < num_hash; u++)
1facf9fc 31905+ INIT_HLIST_HEAD(head++);
dece6358 31906+ return 0; /* success */
1facf9fc 31907+ }
1facf9fc 31908+
dece6358 31909+ return -ENOMEM;
1facf9fc 31910+}
31911+
dece6358
AM
31912+static void nhash_count(struct hlist_head *head)
31913+{
43982f53 31914+#if 0 /* debugging */
dece6358
AM
31915+ unsigned long n;
31916+ struct hlist_node *pos;
31917+
31918+ n = 0;
31919+ hlist_for_each(pos, head)
31920+ n++;
4a4d8108 31921+ pr_info("%lu\n", n);
dece6358
AM
31922+#endif
31923+}
31924+
31925+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 31926+{
c06a8ce3
AM
31927+ struct au_vdir_wh *pos;
31928+ struct hlist_node *node;
1facf9fc 31929+
c06a8ce3 31930+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
9f237c51 31931+ au_kfree_rcu(pos);
1facf9fc 31932+}
31933+
dece6358 31934+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 31935+{
c06a8ce3
AM
31936+ struct au_vdir_dehstr *pos;
31937+ struct hlist_node *node;
1facf9fc 31938+
c06a8ce3 31939+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 31940+ au_cache_free_vdir_dehstr(pos);
1facf9fc 31941+}
31942+
dece6358
AM
31943+static void au_nhash_do_free(struct au_nhash *nhash,
31944+ void (*free)(struct hlist_head *head))
1facf9fc 31945+{
1308ab2a 31946+ unsigned int n;
1facf9fc 31947+ struct hlist_head *head;
1facf9fc 31948+
dece6358 31949+ n = nhash->nh_num;
1308ab2a 31950+ if (!n)
31951+ return;
31952+
dece6358 31953+ head = nhash->nh_head;
1308ab2a 31954+ while (n-- > 0) {
dece6358
AM
31955+ nhash_count(head);
31956+ free(head++);
1facf9fc 31957+ }
9f237c51 31958+ au_kfree_try_rcu(nhash->nh_head);
1facf9fc 31959+}
31960+
dece6358 31961+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 31962+{
dece6358
AM
31963+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
31964+}
1facf9fc 31965+
dece6358
AM
31966+static void au_nhash_de_free(struct au_nhash *delist)
31967+{
31968+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 31969+}
31970+
31971+/* ---------------------------------------------------------------------- */
31972+
31973+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31974+ int limit)
31975+{
31976+ int num;
31977+ unsigned int u, n;
31978+ struct hlist_head *head;
c06a8ce3 31979+ struct au_vdir_wh *pos;
1facf9fc 31980+
31981+ num = 0;
31982+ n = whlist->nh_num;
31983+ head = whlist->nh_head;
1308ab2a 31984+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
31985+ hlist_for_each_entry(pos, head, wh_hash)
31986+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 31987+ return 1;
1facf9fc 31988+ return 0;
31989+}
31990+
31991+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 31992+ unsigned char *name,
1facf9fc 31993+ unsigned int len)
31994+{
dece6358
AM
31995+ unsigned int v;
31996+ /* const unsigned int magic_bit = 12; */
31997+
1308ab2a 31998+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31999+
dece6358 32000+ v = 0;
f0c0a007
AM
32001+ if (len > 8)
32002+ len = 8;
dece6358
AM
32003+ while (len--)
32004+ v += *name++;
32005+ /* v = hash_long(v, magic_bit); */
32006+ v %= nhash->nh_num;
32007+ return nhash->nh_head + v;
32008+}
32009+
32010+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32011+ int nlen)
32012+{
32013+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 32014+}
32015+
32016+/* returns found or not */
dece6358 32017+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 32018+{
32019+ struct hlist_head *head;
c06a8ce3 32020+ struct au_vdir_wh *pos;
1facf9fc 32021+ struct au_vdir_destr *str;
32022+
dece6358 32023+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
32024+ hlist_for_each_entry(pos, head, wh_hash) {
32025+ str = &pos->wh_str;
1facf9fc 32026+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
32027+ if (au_nhash_test_name(str, name, nlen))
32028+ return 1;
32029+ }
32030+ return 0;
32031+}
32032+
32033+/* returns found(true) or not */
32034+static int test_known(struct au_nhash *delist, char *name, int nlen)
32035+{
32036+ struct hlist_head *head;
c06a8ce3 32037+ struct au_vdir_dehstr *pos;
dece6358
AM
32038+ struct au_vdir_destr *str;
32039+
32040+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
32041+ hlist_for_each_entry(pos, head, hash) {
32042+ str = pos->str;
dece6358
AM
32043+ AuDbg("%.*s\n", str->len, str->name);
32044+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 32045+ return 1;
32046+ }
32047+ return 0;
32048+}
32049+
dece6358
AM
32050+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32051+ unsigned char d_type)
32052+{
32053+#ifdef CONFIG_AUFS_SHWH
32054+ wh->wh_ino = ino;
32055+ wh->wh_type = d_type;
32056+#endif
32057+}
32058+
32059+/* ---------------------------------------------------------------------- */
32060+
32061+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32062+ unsigned int d_type, aufs_bindex_t bindex,
32063+ unsigned char shwh)
1facf9fc 32064+{
32065+ int err;
32066+ struct au_vdir_destr *str;
32067+ struct au_vdir_wh *wh;
32068+
dece6358 32069+ AuDbg("%.*s\n", nlen, name);
1308ab2a 32070+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32071+
1facf9fc 32072+ err = -ENOMEM;
dece6358 32073+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 32074+ if (unlikely(!wh))
32075+ goto out;
32076+
32077+ err = 0;
32078+ wh->wh_bindex = bindex;
dece6358
AM
32079+ if (shwh)
32080+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 32081+ str = &wh->wh_str;
dece6358
AM
32082+ str->len = nlen;
32083+ memcpy(str->name, name, nlen);
32084+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 32085+ /* smp_mb(); */
32086+
4f0767ce 32087+out:
1facf9fc 32088+ return err;
32089+}
32090+
1facf9fc 32091+static int append_deblk(struct au_vdir *vdir)
32092+{
32093+ int err;
dece6358 32094+ unsigned long ul;
1facf9fc 32095+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32096+ union au_vdir_deblk_p p, deblk_end;
32097+ unsigned char **o;
32098+
32099+ err = -ENOMEM;
e2f27e51
AM
32100+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32101+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32102+ if (unlikely(!o))
32103+ goto out;
32104+
32105+ vdir->vd_deblk = o;
32106+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32107+ if (p.deblk) {
32108+ ul = vdir->vd_nblk++;
32109+ vdir->vd_deblk[ul] = p.deblk;
32110+ vdir->vd_last.ul = ul;
32111+ vdir->vd_last.p.deblk = p.deblk;
32112+ deblk_end.deblk = p.deblk + deblk_sz;
32113+ err = set_deblk_end(&p, &deblk_end);
32114+ }
32115+
4f0767ce 32116+out:
1facf9fc 32117+ return err;
32118+}
32119+
dece6358
AM
32120+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32121+ unsigned int d_type, struct au_nhash *delist)
32122+{
32123+ int err;
32124+ unsigned int sz;
32125+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32126+ union au_vdir_deblk_p p, *room, deblk_end;
32127+ struct au_vdir_dehstr *dehstr;
32128+
32129+ p.deblk = last_deblk(vdir);
32130+ deblk_end.deblk = p.deblk + deblk_sz;
32131+ room = &vdir->vd_last.p;
32132+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32133+ || !is_deblk_end(room, &deblk_end));
32134+
32135+ sz = calc_size(nlen);
32136+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32137+ err = append_deblk(vdir);
32138+ if (unlikely(err))
32139+ goto out;
32140+
32141+ p.deblk = last_deblk(vdir);
32142+ deblk_end.deblk = p.deblk + deblk_sz;
32143+ /* smp_mb(); */
32144+ AuDebugOn(room->deblk != p.deblk);
32145+ }
32146+
32147+ err = -ENOMEM;
4a4d8108 32148+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
32149+ if (unlikely(!dehstr))
32150+ goto out;
32151+
32152+ dehstr->str = &room->de->de_str;
32153+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32154+ room->de->de_ino = ino;
32155+ room->de->de_type = d_type;
32156+ room->de->de_str.len = nlen;
32157+ memcpy(room->de->de_str.name, name, nlen);
32158+
32159+ err = 0;
32160+ room->deblk += sz;
32161+ if (unlikely(set_deblk_end(room, &deblk_end)))
32162+ err = append_deblk(vdir);
32163+ /* smp_mb(); */
32164+
4f0767ce 32165+out:
dece6358
AM
32166+ return err;
32167+}
32168+
32169+/* ---------------------------------------------------------------------- */
32170+
1c60b727 32171+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
32172+{
32173+ unsigned char **deblk;
32174+
32175+ deblk = vdir->vd_deblk;
1c60b727 32176+ while (vdir->vd_nblk--)
9f237c51
AM
32177+ au_kfree_try_rcu(*deblk++);
32178+ au_kfree_try_rcu(vdir->vd_deblk);
1c60b727 32179+ au_cache_free_vdir(vdir);
dece6358
AM
32180+}
32181+
1308ab2a 32182+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 32183+{
32184+ struct au_vdir *vdir;
1308ab2a 32185+ struct super_block *sb;
1facf9fc 32186+ int err;
32187+
2000de60 32188+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32189+ SiMustAnyLock(sb);
32190+
1facf9fc 32191+ err = -ENOMEM;
32192+ vdir = au_cache_alloc_vdir();
32193+ if (unlikely(!vdir))
32194+ goto out;
32195+
32196+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32197+ if (unlikely(!vdir->vd_deblk))
32198+ goto out_free;
32199+
32200+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 32201+ if (!vdir->vd_deblk_sz) {
79b8bda9 32202+ /* estimate the appropriate size for deblk */
1308ab2a 32203+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 32204+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 32205+ }
1facf9fc 32206+ vdir->vd_nblk = 0;
32207+ vdir->vd_version = 0;
32208+ vdir->vd_jiffy = 0;
32209+ err = append_deblk(vdir);
32210+ if (!err)
32211+ return vdir; /* success */
32212+
9f237c51 32213+ au_kfree_try_rcu(vdir->vd_deblk);
1facf9fc 32214+
4f0767ce 32215+out_free:
1c60b727 32216+ au_cache_free_vdir(vdir);
4f0767ce 32217+out:
1facf9fc 32218+ vdir = ERR_PTR(err);
32219+ return vdir;
32220+}
32221+
32222+static int reinit_vdir(struct au_vdir *vdir)
32223+{
32224+ int err;
32225+ union au_vdir_deblk_p p, deblk_end;
32226+
32227+ while (vdir->vd_nblk > 1) {
9f237c51 32228+ au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 32229+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32230+ vdir->vd_nblk--;
32231+ }
32232+ p.deblk = vdir->vd_deblk[0];
32233+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32234+ err = set_deblk_end(&p, &deblk_end);
32235+ /* keep vd_dblk_sz */
32236+ vdir->vd_last.ul = 0;
32237+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32238+ vdir->vd_version = 0;
32239+ vdir->vd_jiffy = 0;
32240+ /* smp_mb(); */
32241+ return err;
32242+}
32243+
32244+/* ---------------------------------------------------------------------- */
32245+
1facf9fc 32246+#define AuFillVdir_CALLED 1
32247+#define AuFillVdir_WHABLE (1 << 1)
dece6358 32248+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 32249+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
32250+#define au_fset_fillvdir(flags, name) \
32251+ do { (flags) |= AuFillVdir_##name; } while (0)
32252+#define au_fclr_fillvdir(flags, name) \
32253+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 32254+
dece6358
AM
32255+#ifndef CONFIG_AUFS_SHWH
32256+#undef AuFillVdir_SHWH
32257+#define AuFillVdir_SHWH 0
32258+#endif
32259+
1facf9fc 32260+struct fillvdir_arg {
392086de 32261+ struct dir_context ctx;
1facf9fc 32262+ struct file *file;
32263+ struct au_vdir *vdir;
dece6358
AM
32264+ struct au_nhash delist;
32265+ struct au_nhash whlist;
1facf9fc 32266+ aufs_bindex_t bindex;
32267+ unsigned int flags;
32268+ int err;
32269+};
32270+
392086de 32271+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 32272+ loff_t offset __maybe_unused, u64 h_ino,
32273+ unsigned int d_type)
32274+{
392086de 32275+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 32276+ char *name = (void *)__name;
32277+ struct super_block *sb;
1facf9fc 32278+ ino_t ino;
dece6358 32279+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 32280+
1facf9fc 32281+ arg->err = 0;
2000de60 32282+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 32283+ au_fset_fillvdir(arg->flags, CALLED);
32284+ /* smp_mb(); */
dece6358 32285+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 32286+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
32287+ if (test_known(&arg->delist, name, nlen)
32288+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32289+ goto out; /* already exists or whiteouted */
1facf9fc 32290+
dece6358 32291+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
32292+ if (!arg->err) {
32293+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32294+ d_type = DT_UNKNOWN;
dece6358
AM
32295+ arg->err = append_de(arg->vdir, name, nlen, ino,
32296+ d_type, &arg->delist);
4a4d8108 32297+ }
1facf9fc 32298+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32299+ name += AUFS_WH_PFX_LEN;
dece6358
AM
32300+ nlen -= AUFS_WH_PFX_LEN;
32301+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32302+ goto out; /* already whiteouted */
1facf9fc 32303+
acd2b654 32304+ ino = 0; /* just to suppress a warning */
dece6358
AM
32305+ if (shwh)
32306+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32307+ &ino);
4a4d8108
AM
32308+ if (!arg->err) {
32309+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32310+ d_type = DT_UNKNOWN;
1facf9fc 32311+ arg->err = au_nhash_append_wh
dece6358
AM
32312+ (&arg->whlist, name, nlen, ino, d_type,
32313+ arg->bindex, shwh);
4a4d8108 32314+ }
1facf9fc 32315+ }
32316+
4f0767ce 32317+out:
1facf9fc 32318+ if (!arg->err)
32319+ arg->vdir->vd_jiffy = jiffies;
32320+ /* smp_mb(); */
32321+ AuTraceErr(arg->err);
32322+ return arg->err;
32323+}
32324+
dece6358
AM
32325+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32326+ struct au_nhash *whlist, struct au_nhash *delist)
32327+{
32328+#ifdef CONFIG_AUFS_SHWH
32329+ int err;
32330+ unsigned int nh, u;
32331+ struct hlist_head *head;
c06a8ce3
AM
32332+ struct au_vdir_wh *pos;
32333+ struct hlist_node *n;
dece6358
AM
32334+ char *p, *o;
32335+ struct au_vdir_destr *destr;
32336+
32337+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32338+
32339+ err = -ENOMEM;
537831f9 32340+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32341+ if (unlikely(!p))
32342+ goto out;
32343+
32344+ err = 0;
32345+ nh = whlist->nh_num;
32346+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32347+ p += AUFS_WH_PFX_LEN;
32348+ for (u = 0; u < nh; u++) {
32349+ head = whlist->nh_head + u;
c06a8ce3
AM
32350+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32351+ destr = &pos->wh_str;
dece6358
AM
32352+ memcpy(p, destr->name, destr->len);
32353+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32354+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32355+ if (unlikely(err))
32356+ break;
32357+ }
32358+ }
32359+
1c60b727 32360+ free_page((unsigned long)o);
dece6358 32361+
4f0767ce 32362+out:
dece6358
AM
32363+ AuTraceErr(err);
32364+ return err;
32365+#else
32366+ return 0;
32367+#endif
32368+}
32369+
1facf9fc 32370+static int au_do_read_vdir(struct fillvdir_arg *arg)
32371+{
32372+ int err;
dece6358 32373+ unsigned int rdhash;
1facf9fc 32374+ loff_t offset;
5afbbe0d 32375+ aufs_bindex_t bbot, bindex, btop;
dece6358 32376+ unsigned char shwh;
1facf9fc 32377+ struct file *hf, *file;
32378+ struct super_block *sb;
32379+
1facf9fc 32380+ file = arg->file;
2000de60 32381+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32382+ SiMustAnyLock(sb);
32383+
32384+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32385+ if (!rdhash)
32386+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32387+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32388+ if (unlikely(err))
1facf9fc 32389+ goto out;
dece6358
AM
32390+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32391+ if (unlikely(err))
1facf9fc 32392+ goto out_delist;
32393+
32394+ err = 0;
32395+ arg->flags = 0;
dece6358
AM
32396+ shwh = 0;
32397+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32398+ shwh = 1;
32399+ au_fset_fillvdir(arg->flags, SHWH);
32400+ }
5afbbe0d
AM
32401+ btop = au_fbtop(file);
32402+ bbot = au_fbbot_dir(file);
32403+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32404+ hf = au_hf_dir(file, bindex);
1facf9fc 32405+ if (!hf)
32406+ continue;
32407+
32408+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32409+ err = offset;
32410+ if (unlikely(offset))
32411+ break;
32412+
32413+ arg->bindex = bindex;
32414+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32415+ if (shwh
5afbbe0d 32416+ || (bindex != bbot
dece6358 32417+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32418+ au_fset_fillvdir(arg->flags, WHABLE);
32419+ do {
32420+ arg->err = 0;
32421+ au_fclr_fillvdir(arg->flags, CALLED);
32422+ /* smp_mb(); */
392086de 32423+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32424+ if (err >= 0)
32425+ err = arg->err;
32426+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32427+
32428+ /*
32429+ * dir_relax() may be good for concurrency, but aufs should not
32430+ * use it since it will cause a lockdep problem.
32431+ */
1facf9fc 32432+ }
dece6358
AM
32433+
32434+ if (!err && shwh)
32435+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32436+
32437+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32438+
4f0767ce 32439+out_delist:
dece6358 32440+ au_nhash_de_free(&arg->delist);
4f0767ce 32441+out:
1facf9fc 32442+ return err;
32443+}
32444+
32445+static int read_vdir(struct file *file, int may_read)
32446+{
32447+ int err;
32448+ unsigned long expire;
32449+ unsigned char do_read;
392086de
AM
32450+ struct fillvdir_arg arg = {
32451+ .ctx = {
2000de60 32452+ .actor = fillvdir
392086de
AM
32453+ }
32454+ };
1facf9fc 32455+ struct inode *inode;
32456+ struct au_vdir *vdir, *allocated;
32457+
32458+ err = 0;
c06a8ce3 32459+ inode = file_inode(file);
1facf9fc 32460+ IMustLock(inode);
5afbbe0d 32461+ IiMustWriteLock(inode);
dece6358
AM
32462+ SiMustAnyLock(inode->i_sb);
32463+
1facf9fc 32464+ allocated = NULL;
32465+ do_read = 0;
32466+ expire = au_sbi(inode->i_sb)->si_rdcache;
32467+ vdir = au_ivdir(inode);
32468+ if (!vdir) {
32469+ do_read = 1;
1308ab2a 32470+ vdir = alloc_vdir(file);
1facf9fc 32471+ err = PTR_ERR(vdir);
32472+ if (IS_ERR(vdir))
32473+ goto out;
32474+ err = 0;
32475+ allocated = vdir;
32476+ } else if (may_read
be118d29 32477+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32478+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32479+ do_read = 1;
32480+ err = reinit_vdir(vdir);
32481+ if (unlikely(err))
32482+ goto out;
32483+ }
32484+
32485+ if (!do_read)
32486+ return 0; /* success */
32487+
32488+ arg.file = file;
32489+ arg.vdir = vdir;
32490+ err = au_do_read_vdir(&arg);
32491+ if (!err) {
392086de 32492+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32493+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32494+ vdir->vd_last.ul = 0;
32495+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32496+ if (allocated)
32497+ au_set_ivdir(inode, allocated);
32498+ } else if (allocated)
1c60b727 32499+ au_vdir_free(allocated);
1facf9fc 32500+
4f0767ce 32501+out:
1facf9fc 32502+ return err;
32503+}
32504+
32505+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32506+{
32507+ int err, rerr;
32508+ unsigned long ul, n;
32509+ const unsigned int deblk_sz = src->vd_deblk_sz;
32510+
32511+ AuDebugOn(tgt->vd_nblk != 1);
32512+
32513+ err = -ENOMEM;
32514+ if (tgt->vd_nblk < src->vd_nblk) {
32515+ unsigned char **p;
32516+
e2f27e51
AM
32517+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32518+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32519+ if (unlikely(!p))
32520+ goto out;
32521+ tgt->vd_deblk = p;
32522+ }
32523+
1308ab2a 32524+ if (tgt->vd_deblk_sz != deblk_sz) {
32525+ unsigned char *p;
32526+
32527+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32528+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32529+ /*may_shrink*/1);
1308ab2a 32530+ if (unlikely(!p))
32531+ goto out;
32532+ tgt->vd_deblk[0] = p;
32533+ }
1facf9fc 32534+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32535+ tgt->vd_version = src->vd_version;
32536+ tgt->vd_jiffy = src->vd_jiffy;
32537+
32538+ n = src->vd_nblk;
32539+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32540+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32541+ GFP_NOFS);
32542+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32543+ goto out;
1308ab2a 32544+ tgt->vd_nblk++;
1facf9fc 32545+ }
1308ab2a 32546+ tgt->vd_nblk = n;
32547+ tgt->vd_last.ul = tgt->vd_last.ul;
32548+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32549+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32550+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32551+ /* smp_mb(); */
32552+ return 0; /* success */
32553+
4f0767ce 32554+out:
1facf9fc 32555+ rerr = reinit_vdir(tgt);
32556+ BUG_ON(rerr);
32557+ return err;
32558+}
32559+
32560+int au_vdir_init(struct file *file)
32561+{
32562+ int err;
32563+ struct inode *inode;
32564+ struct au_vdir *vdir_cache, *allocated;
32565+
392086de 32566+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32567+ err = read_vdir(file, !file->f_pos);
32568+ if (unlikely(err))
32569+ goto out;
32570+
32571+ allocated = NULL;
32572+ vdir_cache = au_fvdir_cache(file);
32573+ if (!vdir_cache) {
1308ab2a 32574+ vdir_cache = alloc_vdir(file);
1facf9fc 32575+ err = PTR_ERR(vdir_cache);
32576+ if (IS_ERR(vdir_cache))
32577+ goto out;
32578+ allocated = vdir_cache;
32579+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32580+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32581+ err = reinit_vdir(vdir_cache);
32582+ if (unlikely(err))
32583+ goto out;
32584+ } else
32585+ return 0; /* success */
32586+
c06a8ce3 32587+ inode = file_inode(file);
1facf9fc 32588+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32589+ if (!err) {
be118d29 32590+ file->f_version = inode_query_iversion(inode);
1facf9fc 32591+ if (allocated)
32592+ au_set_fvdir_cache(file, allocated);
32593+ } else if (allocated)
1c60b727 32594+ au_vdir_free(allocated);
1facf9fc 32595+
4f0767ce 32596+out:
1facf9fc 32597+ return err;
32598+}
32599+
32600+static loff_t calc_offset(struct au_vdir *vdir)
32601+{
32602+ loff_t offset;
32603+ union au_vdir_deblk_p p;
32604+
32605+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32606+ offset = vdir->vd_last.p.deblk - p.deblk;
32607+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32608+ return offset;
32609+}
32610+
32611+/* returns true or false */
392086de 32612+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32613+{
32614+ int valid;
32615+ unsigned int deblk_sz;
32616+ unsigned long ul, n;
32617+ loff_t offset;
32618+ union au_vdir_deblk_p p, deblk_end;
32619+ struct au_vdir *vdir_cache;
32620+
32621+ valid = 1;
32622+ vdir_cache = au_fvdir_cache(file);
32623+ offset = calc_offset(vdir_cache);
32624+ AuDbg("offset %lld\n", offset);
392086de 32625+ if (ctx->pos == offset)
1facf9fc 32626+ goto out;
32627+
32628+ vdir_cache->vd_last.ul = 0;
32629+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32630+ if (!ctx->pos)
1facf9fc 32631+ goto out;
32632+
32633+ valid = 0;
32634+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32635+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32636+ AuDbg("ul %lu\n", ul);
32637+ if (ul >= vdir_cache->vd_nblk)
32638+ goto out;
32639+
32640+ n = vdir_cache->vd_nblk;
32641+ for (; ul < n; ul++) {
32642+ p.deblk = vdir_cache->vd_deblk[ul];
32643+ deblk_end.deblk = p.deblk + deblk_sz;
32644+ offset = ul;
32645+ offset *= deblk_sz;
392086de 32646+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32647+ unsigned int l;
32648+
32649+ l = calc_size(p.de->de_str.len);
32650+ offset += l;
32651+ p.deblk += l;
32652+ }
32653+ if (!is_deblk_end(&p, &deblk_end)) {
32654+ valid = 1;
32655+ vdir_cache->vd_last.ul = ul;
32656+ vdir_cache->vd_last.p = p;
32657+ break;
32658+ }
32659+ }
32660+
4f0767ce 32661+out:
1facf9fc 32662+ /* smp_mb(); */
b00004a5
AM
32663+ if (!valid)
32664+ AuDbg("valid %d\n", !valid);
1facf9fc 32665+ return valid;
32666+}
32667+
392086de 32668+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32669+{
1facf9fc 32670+ unsigned int l, deblk_sz;
32671+ union au_vdir_deblk_p deblk_end;
32672+ struct au_vdir *vdir_cache;
32673+ struct au_vdir_de *de;
32674+
392086de 32675+ if (!seek_vdir(file, ctx))
1facf9fc 32676+ return 0;
32677+
acd2b654 32678+ vdir_cache = au_fvdir_cache(file);
1facf9fc 32679+ deblk_sz = vdir_cache->vd_deblk_sz;
32680+ while (1) {
32681+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32682+ deblk_end.deblk += deblk_sz;
32683+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32684+ de = vdir_cache->vd_last.p.de;
32685+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32686+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32687+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32688+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32689+ de->de_str.len, de->de_ino,
32690+ de->de_type))) {
1facf9fc 32691+ /* todo: ignore the error caused by udba? */
32692+ /* return err; */
32693+ return 0;
32694+ }
32695+
32696+ l = calc_size(de->de_str.len);
32697+ vdir_cache->vd_last.p.deblk += l;
392086de 32698+ ctx->pos += l;
1facf9fc 32699+ }
32700+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32701+ vdir_cache->vd_last.ul++;
32702+ vdir_cache->vd_last.p.deblk
32703+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32704+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32705+ continue;
32706+ }
32707+ break;
32708+ }
32709+
32710+ /* smp_mb(); */
32711+ return 0;
32712+}
7f207e10 32713diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
eca34b5c 32714--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
016522bc 32715+++ linux/fs/aufs/vfsub.c 2020-01-27 10:57:18.178871751 +0100
acd2b654 32716@@ -0,0 +1,902 @@
cd7a4cd9 32717+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32718+/*
016522bc 32719+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 32720+ *
32721+ * This program, aufs is free software; you can redistribute it and/or modify
32722+ * it under the terms of the GNU General Public License as published by
32723+ * the Free Software Foundation; either version 2 of the License, or
32724+ * (at your option) any later version.
dece6358
AM
32725+ *
32726+ * This program is distributed in the hope that it will be useful,
32727+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32728+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32729+ * GNU General Public License for more details.
32730+ *
32731+ * You should have received a copy of the GNU General Public License
523b37e3 32732+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32733+ */
32734+
32735+/*
32736+ * sub-routines for VFS
32737+ */
32738+
8b6a4947 32739+#include <linux/mnt_namespace.h>
dece6358 32740+#include <linux/namei.h>
8cdd5066 32741+#include <linux/nsproxy.h>
dece6358
AM
32742+#include <linux/security.h>
32743+#include <linux/splice.h>
1facf9fc 32744+#include "aufs.h"
32745+
8cdd5066
JR
32746+#ifdef CONFIG_AUFS_BR_FUSE
32747+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32748+{
8cdd5066
JR
32749+ if (!au_test_fuse(h_sb) || !au_userns)
32750+ return 0;
32751+
8b6a4947 32752+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32753+}
32754+#endif
32755+
a2654f78
AM
32756+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32757+{
32758+ int err;
32759+
32760+ lockdep_off();
32761+ down_read(&h_sb->s_umount);
32762+ err = __sync_filesystem(h_sb, wait);
32763+ up_read(&h_sb->s_umount);
32764+ lockdep_on();
32765+
32766+ return err;
32767+}
32768+
8cdd5066
JR
32769+/* ---------------------------------------------------------------------- */
32770+
1facf9fc 32771+int vfsub_update_h_iattr(struct path *h_path, int *did)
32772+{
32773+ int err;
32774+ struct kstat st;
32775+ struct super_block *h_sb;
32776+
32777+ /* for remote fs, leave work for its getattr or d_revalidate */
32778+ /* for bad i_attr fs, handle them in aufs_getattr() */
32779+ /* still some fs may acquire i_mutex. we need to skip them */
32780+ err = 0;
32781+ if (!did)
32782+ did = &err;
32783+ h_sb = h_path->dentry->d_sb;
32784+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32785+ if (*did)
521ced18 32786+ err = vfsub_getattr(h_path, &st);
1facf9fc 32787+
32788+ return err;
32789+}
32790+
32791+/* ---------------------------------------------------------------------- */
32792+
4a4d8108 32793+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32794+{
32795+ struct file *file;
32796+
b4510431 32797+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32798+ current_cred());
2cbb1c4b
JR
32799+ if (!IS_ERR_OR_NULL(file)
32800+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 32801+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 32802+
1308ab2a 32803+ return file;
32804+}
32805+
1facf9fc 32806+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32807+{
32808+ struct file *file;
32809+
2cbb1c4b 32810+ lockdep_off();
7f207e10 32811+ file = filp_open(path,
2cbb1c4b 32812+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32813+ mode);
2cbb1c4b 32814+ lockdep_on();
1facf9fc 32815+ if (IS_ERR(file))
32816+ goto out;
32817+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32818+
4f0767ce 32819+out:
1facf9fc 32820+ return file;
32821+}
32822+
b912730e
AM
32823+/*
32824+ * Ideally this function should call VFS:do_last() in order to keep all its
32825+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32826+ * structure such as nameidata. This is a second (or third) best approach.
32827+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32828+ */
32829+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 32830+ struct vfsub_aopen_args *args)
b912730e
AM
32831+{
32832+ int err;
acd2b654 32833+ struct au_branch *br = args->br;
b912730e
AM
32834+ struct file *file = args->file;
32835+ /* copied from linux/fs/namei.c:atomic_open() */
32836+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32837+
32838+ IMustLock(dir);
32839+ AuDebugOn(!dir->i_op->atomic_open);
32840+
32841+ err = au_br_test_oflag(args->open_flag, br);
32842+ if (unlikely(err))
32843+ goto out;
32844+
acd2b654
AM
32845+ au_lcnt_inc(&br->br_nfiles);
32846+ file->f_path.dentry = DENTRY_NOT_SET;
32847+ file->f_path.mnt = au_br_mnt(br);
32848+ AuDbg("%ps\n", dir->i_op->atomic_open);
b912730e 32849+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
acd2b654
AM
32850+ args->create_mode);
32851+ if (unlikely(err < 0)) {
32852+ au_lcnt_dec(&br->br_nfiles);
b912730e 32853+ goto out;
acd2b654 32854+ }
b912730e 32855+
acd2b654
AM
32856+ /* temporary workaround for nfsv4 branch */
32857+ if (au_test_nfs(dir->i_sb))
32858+ nfs_mark_for_revalidate(dir);
b912730e 32859+
acd2b654
AM
32860+ if (file->f_mode & FMODE_CREATED)
32861+ fsnotify_create(dir, dentry);
32862+ if (!(file->f_mode & FMODE_OPENED)) {
32863+ au_lcnt_dec(&br->br_nfiles);
32864+ goto out;
b912730e
AM
32865+ }
32866+
acd2b654
AM
32867+ /* todo: call VFS:may_open() here */
32868+ /* todo: ima_file_check() too? */
32869+ if (!err && (args->open_flag & __FMODE_EXEC))
32870+ err = deny_write_access(file);
32871+ if (!err)
32872+ fsnotify_open(file);
32873+ else
32874+ au_lcnt_dec(&br->br_nfiles);
32875+ /* note that the file is created and still opened */
b912730e
AM
32876+
32877+out:
32878+ return err;
32879+}
32880+
1facf9fc 32881+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32882+{
32883+ int err;
32884+
1facf9fc 32885+ err = kern_path(name, flags, path);
5527c038 32886+ if (!err && d_is_positive(path->dentry))
1facf9fc 32887+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32888+ return err;
32889+}
32890+
febd17d6
JR
32891+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32892+ struct dentry *parent, int len)
32893+{
32894+ struct path path = {
32895+ .mnt = NULL
32896+ };
32897+
32898+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32899+ if (IS_ERR(path.dentry))
32900+ goto out;
32901+ if (d_is_positive(path.dentry))
32902+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32903+
32904+out:
32905+ AuTraceErrPtr(path.dentry);
32906+ return path.dentry;
32907+}
32908+
1facf9fc 32909+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32910+ int len)
32911+{
32912+ struct path path = {
32913+ .mnt = NULL
32914+ };
32915+
1308ab2a 32916+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 32917+ IMustLock(d_inode(parent));
1facf9fc 32918+
32919+ path.dentry = lookup_one_len(name, parent, len);
32920+ if (IS_ERR(path.dentry))
32921+ goto out;
5527c038 32922+ if (d_is_positive(path.dentry))
1facf9fc 32923+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32924+
4f0767ce 32925+out:
4a4d8108 32926+ AuTraceErrPtr(path.dentry);
1facf9fc 32927+ return path.dentry;
32928+}
32929+
b4510431 32930+void vfsub_call_lkup_one(void *args)
2cbb1c4b 32931+{
b4510431
AM
32932+ struct vfsub_lkup_one_args *a = args;
32933+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
32934+}
32935+
1facf9fc 32936+/* ---------------------------------------------------------------------- */
32937+
32938+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32939+ struct dentry *d2, struct au_hinode *hdir2)
32940+{
32941+ struct dentry *d;
32942+
2cbb1c4b 32943+ lockdep_off();
1facf9fc 32944+ d = lock_rename(d1, d2);
2cbb1c4b 32945+ lockdep_on();
4a4d8108 32946+ au_hn_suspend(hdir1);
1facf9fc 32947+ if (hdir1 != hdir2)
4a4d8108 32948+ au_hn_suspend(hdir2);
1facf9fc 32949+
32950+ return d;
32951+}
32952+
32953+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32954+ struct dentry *d2, struct au_hinode *hdir2)
32955+{
4a4d8108 32956+ au_hn_resume(hdir1);
1facf9fc 32957+ if (hdir1 != hdir2)
4a4d8108 32958+ au_hn_resume(hdir2);
2cbb1c4b 32959+ lockdep_off();
1facf9fc 32960+ unlock_rename(d1, d2);
2cbb1c4b 32961+ lockdep_on();
1facf9fc 32962+}
32963+
32964+/* ---------------------------------------------------------------------- */
32965+
b4510431 32966+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 32967+{
32968+ int err;
32969+ struct dentry *d;
32970+
32971+ IMustLock(dir);
32972+
32973+ d = path->dentry;
32974+ path->dentry = d->d_parent;
b752ccd1 32975+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 32976+ path->dentry = d;
32977+ if (unlikely(err))
32978+ goto out;
32979+
c1595e42 32980+ lockdep_off();
b4510431 32981+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 32982+ lockdep_on();
1facf9fc 32983+ if (!err) {
32984+ struct path tmp = *path;
32985+ int did;
32986+
32987+ vfsub_update_h_iattr(&tmp, &did);
32988+ if (did) {
32989+ tmp.dentry = path->dentry->d_parent;
32990+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32991+ }
32992+ /*ignore*/
32993+ }
32994+
4f0767ce 32995+out:
1facf9fc 32996+ return err;
32997+}
32998+
32999+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33000+{
33001+ int err;
33002+ struct dentry *d;
33003+
33004+ IMustLock(dir);
33005+
33006+ d = path->dentry;
33007+ path->dentry = d->d_parent;
b752ccd1 33008+ err = security_path_symlink(path, d, symname);
1facf9fc 33009+ path->dentry = d;
33010+ if (unlikely(err))
33011+ goto out;
33012+
c1595e42 33013+ lockdep_off();
1facf9fc 33014+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 33015+ lockdep_on();
1facf9fc 33016+ if (!err) {
33017+ struct path tmp = *path;
33018+ int did;
33019+
33020+ vfsub_update_h_iattr(&tmp, &did);
33021+ if (did) {
33022+ tmp.dentry = path->dentry->d_parent;
33023+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33024+ }
33025+ /*ignore*/
33026+ }
33027+
4f0767ce 33028+out:
1facf9fc 33029+ return err;
33030+}
33031+
33032+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33033+{
33034+ int err;
33035+ struct dentry *d;
33036+
33037+ IMustLock(dir);
33038+
33039+ d = path->dentry;
33040+ path->dentry = d->d_parent;
027c5e7a 33041+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 33042+ path->dentry = d;
33043+ if (unlikely(err))
33044+ goto out;
33045+
c1595e42 33046+ lockdep_off();
1facf9fc 33047+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 33048+ lockdep_on();
1facf9fc 33049+ if (!err) {
33050+ struct path tmp = *path;
33051+ int did;
33052+
33053+ vfsub_update_h_iattr(&tmp, &did);
33054+ if (did) {
33055+ tmp.dentry = path->dentry->d_parent;
33056+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33057+ }
33058+ /*ignore*/
33059+ }
33060+
4f0767ce 33061+out:
1facf9fc 33062+ return err;
33063+}
33064+
33065+static int au_test_nlink(struct inode *inode)
33066+{
33067+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33068+
33069+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
33070+ || inode->i_nlink < link_max)
33071+ return 0;
33072+ return -EMLINK;
33073+}
33074+
523b37e3
AM
33075+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33076+ struct inode **delegated_inode)
1facf9fc 33077+{
33078+ int err;
33079+ struct dentry *d;
33080+
33081+ IMustLock(dir);
33082+
5527c038 33083+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 33084+ if (unlikely(err))
33085+ return err;
33086+
b4510431 33087+ /* we don't call may_linkat() */
1facf9fc 33088+ d = path->dentry;
33089+ path->dentry = d->d_parent;
b752ccd1 33090+ err = security_path_link(src_dentry, path, d);
1facf9fc 33091+ path->dentry = d;
33092+ if (unlikely(err))
33093+ goto out;
33094+
2cbb1c4b 33095+ lockdep_off();
523b37e3 33096+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 33097+ lockdep_on();
1facf9fc 33098+ if (!err) {
33099+ struct path tmp = *path;
33100+ int did;
33101+
33102+ /* fuse has different memory inode for the same inumber */
33103+ vfsub_update_h_iattr(&tmp, &did);
33104+ if (did) {
33105+ tmp.dentry = path->dentry->d_parent;
33106+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33107+ tmp.dentry = src_dentry;
33108+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33109+ }
33110+ /*ignore*/
33111+ }
33112+
4f0767ce 33113+out:
1facf9fc 33114+ return err;
33115+}
33116+
33117+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 33118+ struct inode *dir, struct path *path,
f2c43d5f 33119+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 33120+{
33121+ int err;
33122+ struct path tmp = {
33123+ .mnt = path->mnt
33124+ };
33125+ struct dentry *d;
33126+
33127+ IMustLock(dir);
33128+ IMustLock(src_dir);
33129+
33130+ d = path->dentry;
33131+ path->dentry = d->d_parent;
33132+ tmp.dentry = src_dentry->d_parent;
38d290e6 33133+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 33134+ path->dentry = d;
33135+ if (unlikely(err))
33136+ goto out;
33137+
2cbb1c4b 33138+ lockdep_off();
523b37e3 33139+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
f2c43d5f 33140+ delegated_inode, flags);
2cbb1c4b 33141+ lockdep_on();
1facf9fc 33142+ if (!err) {
33143+ int did;
33144+
33145+ tmp.dentry = d->d_parent;
33146+ vfsub_update_h_iattr(&tmp, &did);
33147+ if (did) {
33148+ tmp.dentry = src_dentry;
33149+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33150+ tmp.dentry = src_dentry->d_parent;
33151+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33152+ }
33153+ /*ignore*/
33154+ }
33155+
4f0767ce 33156+out:
1facf9fc 33157+ return err;
33158+}
33159+
33160+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33161+{
33162+ int err;
33163+ struct dentry *d;
33164+
33165+ IMustLock(dir);
33166+
33167+ d = path->dentry;
33168+ path->dentry = d->d_parent;
b752ccd1 33169+ err = security_path_mkdir(path, d, mode);
1facf9fc 33170+ path->dentry = d;
33171+ if (unlikely(err))
33172+ goto out;
33173+
c1595e42 33174+ lockdep_off();
1facf9fc 33175+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 33176+ lockdep_on();
1facf9fc 33177+ if (!err) {
33178+ struct path tmp = *path;
33179+ int did;
33180+
33181+ vfsub_update_h_iattr(&tmp, &did);
33182+ if (did) {
33183+ tmp.dentry = path->dentry->d_parent;
33184+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33185+ }
33186+ /*ignore*/
33187+ }
33188+
4f0767ce 33189+out:
1facf9fc 33190+ return err;
33191+}
33192+
33193+int vfsub_rmdir(struct inode *dir, struct path *path)
33194+{
33195+ int err;
33196+ struct dentry *d;
33197+
33198+ IMustLock(dir);
33199+
33200+ d = path->dentry;
33201+ path->dentry = d->d_parent;
b752ccd1 33202+ err = security_path_rmdir(path, d);
1facf9fc 33203+ path->dentry = d;
33204+ if (unlikely(err))
33205+ goto out;
33206+
2cbb1c4b 33207+ lockdep_off();
1facf9fc 33208+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 33209+ lockdep_on();
1facf9fc 33210+ if (!err) {
33211+ struct path tmp = {
33212+ .dentry = path->dentry->d_parent,
33213+ .mnt = path->mnt
33214+ };
33215+
33216+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33217+ }
33218+
4f0767ce 33219+out:
1facf9fc 33220+ return err;
33221+}
33222+
33223+/* ---------------------------------------------------------------------- */
33224+
9dbd164d 33225+/* todo: support mmap_sem? */
1facf9fc 33226+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33227+ loff_t *ppos)
33228+{
33229+ ssize_t err;
33230+
2cbb1c4b 33231+ lockdep_off();
1facf9fc 33232+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 33233+ lockdep_on();
1facf9fc 33234+ if (err >= 0)
33235+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33236+ return err;
33237+}
33238+
33239+/* todo: kernel_read()? */
33240+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33241+ loff_t *ppos)
33242+{
33243+ ssize_t err;
33244+ mm_segment_t oldfs;
b752ccd1
AM
33245+ union {
33246+ void *k;
33247+ char __user *u;
33248+ } buf;
1facf9fc 33249+
b752ccd1 33250+ buf.k = kbuf;
1facf9fc 33251+ oldfs = get_fs();
33252+ set_fs(KERNEL_DS);
b752ccd1 33253+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 33254+ set_fs(oldfs);
33255+ return err;
33256+}
33257+
33258+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33259+ loff_t *ppos)
33260+{
33261+ ssize_t err;
33262+
2cbb1c4b 33263+ lockdep_off();
1facf9fc 33264+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 33265+ lockdep_on();
1facf9fc 33266+ if (err >= 0)
33267+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33268+ return err;
33269+}
33270+
33271+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33272+{
33273+ ssize_t err;
33274+ mm_segment_t oldfs;
b752ccd1
AM
33275+ union {
33276+ void *k;
33277+ const char __user *u;
33278+ } buf;
1facf9fc 33279+
b752ccd1 33280+ buf.k = kbuf;
1facf9fc 33281+ oldfs = get_fs();
33282+ set_fs(KERNEL_DS);
b752ccd1 33283+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 33284+ set_fs(oldfs);
33285+ return err;
33286+}
33287+
4a4d8108
AM
33288+int vfsub_flush(struct file *file, fl_owner_t id)
33289+{
33290+ int err;
33291+
33292+ err = 0;
523b37e3 33293+ if (file->f_op->flush) {
2000de60 33294+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
33295+ err = file->f_op->flush(file, id);
33296+ else {
33297+ lockdep_off();
33298+ err = file->f_op->flush(file, id);
33299+ lockdep_on();
33300+ }
4a4d8108
AM
33301+ if (!err)
33302+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33303+ /*ignore*/
33304+ }
33305+ return err;
33306+}
33307+
392086de 33308+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 33309+{
33310+ int err;
33311+
062440b3 33312+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 33313+
2cbb1c4b 33314+ lockdep_off();
392086de 33315+ err = iterate_dir(file, ctx);
2cbb1c4b 33316+ lockdep_on();
1facf9fc 33317+ if (err >= 0)
33318+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 33319+
1facf9fc 33320+ return err;
33321+}
33322+
33323+long vfsub_splice_to(struct file *in, loff_t *ppos,
33324+ struct pipe_inode_info *pipe, size_t len,
33325+ unsigned int flags)
33326+{
33327+ long err;
33328+
2cbb1c4b 33329+ lockdep_off();
0fc653ad 33330+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 33331+ lockdep_on();
4a4d8108 33332+ file_accessed(in);
1facf9fc 33333+ if (err >= 0)
33334+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33335+ return err;
33336+}
33337+
33338+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33339+ loff_t *ppos, size_t len, unsigned int flags)
33340+{
33341+ long err;
33342+
2cbb1c4b 33343+ lockdep_off();
0fc653ad 33344+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33345+ lockdep_on();
1facf9fc 33346+ if (err >= 0)
33347+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33348+ return err;
33349+}
33350+
53392da6
AM
33351+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33352+{
33353+ int err;
33354+
33355+ /* file can be NULL */
33356+ lockdep_off();
33357+ err = vfs_fsync(file, datasync);
33358+ lockdep_on();
33359+ if (!err) {
33360+ if (!path) {
33361+ AuDebugOn(!file);
33362+ path = &file->f_path;
33363+ }
33364+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33365+ }
33366+ return err;
33367+}
33368+
1facf9fc 33369+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33370+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33371+ struct file *h_file)
33372+{
33373+ int err;
33374+ struct inode *h_inode;
c06a8ce3 33375+ struct super_block *h_sb;
1facf9fc 33376+
1facf9fc 33377+ if (!h_file) {
c06a8ce3
AM
33378+ err = vfsub_truncate(h_path, length);
33379+ goto out;
1facf9fc 33380+ }
33381+
5527c038 33382+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33383+ h_sb = h_inode->i_sb;
33384+ lockdep_off();
33385+ sb_start_write(h_sb);
33386+ lockdep_on();
1facf9fc 33387+ err = locks_verify_truncate(h_inode, h_file, length);
33388+ if (!err)
953406b4 33389+ err = security_path_truncate(h_path);
2cbb1c4b
JR
33390+ if (!err) {
33391+ lockdep_off();
1facf9fc 33392+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
33393+ lockdep_on();
33394+ }
c06a8ce3
AM
33395+ lockdep_off();
33396+ sb_end_write(h_sb);
33397+ lockdep_on();
1facf9fc 33398+
4f0767ce 33399+out:
1facf9fc 33400+ return err;
33401+}
33402+
33403+/* ---------------------------------------------------------------------- */
33404+
33405+struct au_vfsub_mkdir_args {
33406+ int *errp;
33407+ struct inode *dir;
33408+ struct path *path;
33409+ int mode;
33410+};
33411+
33412+static void au_call_vfsub_mkdir(void *args)
33413+{
33414+ struct au_vfsub_mkdir_args *a = args;
33415+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33416+}
33417+
33418+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33419+{
33420+ int err, do_sio, wkq_err;
33421+
33422+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33423+ if (!do_sio) {
33424+ lockdep_off();
1facf9fc 33425+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33426+ lockdep_on();
33427+ } else {
1facf9fc 33428+ struct au_vfsub_mkdir_args args = {
33429+ .errp = &err,
33430+ .dir = dir,
33431+ .path = path,
33432+ .mode = mode
33433+ };
33434+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33435+ if (unlikely(wkq_err))
33436+ err = wkq_err;
33437+ }
33438+
33439+ return err;
33440+}
33441+
33442+struct au_vfsub_rmdir_args {
33443+ int *errp;
33444+ struct inode *dir;
33445+ struct path *path;
33446+};
33447+
33448+static void au_call_vfsub_rmdir(void *args)
33449+{
33450+ struct au_vfsub_rmdir_args *a = args;
33451+ *a->errp = vfsub_rmdir(a->dir, a->path);
33452+}
33453+
33454+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33455+{
33456+ int err, do_sio, wkq_err;
33457+
33458+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33459+ if (!do_sio) {
33460+ lockdep_off();
1facf9fc 33461+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33462+ lockdep_on();
33463+ } else {
1facf9fc 33464+ struct au_vfsub_rmdir_args args = {
33465+ .errp = &err,
33466+ .dir = dir,
33467+ .path = path
33468+ };
33469+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33470+ if (unlikely(wkq_err))
33471+ err = wkq_err;
33472+ }
33473+
33474+ return err;
33475+}
33476+
33477+/* ---------------------------------------------------------------------- */
33478+
33479+struct notify_change_args {
33480+ int *errp;
33481+ struct path *path;
33482+ struct iattr *ia;
523b37e3 33483+ struct inode **delegated_inode;
1facf9fc 33484+};
33485+
33486+static void call_notify_change(void *args)
33487+{
33488+ struct notify_change_args *a = args;
33489+ struct inode *h_inode;
33490+
5527c038 33491+ h_inode = d_inode(a->path->dentry);
1facf9fc 33492+ IMustLock(h_inode);
33493+
33494+ *a->errp = -EPERM;
33495+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 33496+ lockdep_off();
523b37e3
AM
33497+ *a->errp = notify_change(a->path->dentry, a->ia,
33498+ a->delegated_inode);
c1595e42 33499+ lockdep_on();
1facf9fc 33500+ if (!*a->errp)
33501+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33502+ }
33503+ AuTraceErr(*a->errp);
33504+}
33505+
523b37e3
AM
33506+int vfsub_notify_change(struct path *path, struct iattr *ia,
33507+ struct inode **delegated_inode)
1facf9fc 33508+{
33509+ int err;
33510+ struct notify_change_args args = {
523b37e3
AM
33511+ .errp = &err,
33512+ .path = path,
33513+ .ia = ia,
33514+ .delegated_inode = delegated_inode
1facf9fc 33515+ };
33516+
33517+ call_notify_change(&args);
33518+
33519+ return err;
33520+}
33521+
523b37e3
AM
33522+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33523+ struct inode **delegated_inode)
1facf9fc 33524+{
33525+ int err, wkq_err;
33526+ struct notify_change_args args = {
523b37e3
AM
33527+ .errp = &err,
33528+ .path = path,
33529+ .ia = ia,
33530+ .delegated_inode = delegated_inode
1facf9fc 33531+ };
33532+
33533+ wkq_err = au_wkq_wait(call_notify_change, &args);
33534+ if (unlikely(wkq_err))
33535+ err = wkq_err;
33536+
33537+ return err;
33538+}
33539+
33540+/* ---------------------------------------------------------------------- */
33541+
33542+struct unlink_args {
33543+ int *errp;
33544+ struct inode *dir;
33545+ struct path *path;
523b37e3 33546+ struct inode **delegated_inode;
1facf9fc 33547+};
33548+
33549+static void call_unlink(void *args)
33550+{
33551+ struct unlink_args *a = args;
33552+ struct dentry *d = a->path->dentry;
33553+ struct inode *h_inode;
33554+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33555+ && au_dcount(d) == 1);
1facf9fc 33556+
33557+ IMustLock(a->dir);
33558+
33559+ a->path->dentry = d->d_parent;
33560+ *a->errp = security_path_unlink(a->path, d);
33561+ a->path->dentry = d;
33562+ if (unlikely(*a->errp))
33563+ return;
33564+
33565+ if (!stop_sillyrename)
33566+ dget(d);
5527c038
JR
33567+ h_inode = NULL;
33568+ if (d_is_positive(d)) {
33569+ h_inode = d_inode(d);
027c5e7a 33570+ ihold(h_inode);
5527c038 33571+ }
1facf9fc 33572+
2cbb1c4b 33573+ lockdep_off();
523b37e3 33574+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 33575+ lockdep_on();
1facf9fc 33576+ if (!*a->errp) {
33577+ struct path tmp = {
33578+ .dentry = d->d_parent,
33579+ .mnt = a->path->mnt
33580+ };
33581+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33582+ }
33583+
33584+ if (!stop_sillyrename)
33585+ dput(d);
33586+ if (h_inode)
33587+ iput(h_inode);
33588+
33589+ AuTraceErr(*a->errp);
33590+}
33591+
33592+/*
33593+ * @dir: must be locked.
33594+ * @dentry: target dentry.
33595+ */
523b37e3
AM
33596+int vfsub_unlink(struct inode *dir, struct path *path,
33597+ struct inode **delegated_inode, int force)
1facf9fc 33598+{
33599+ int err;
33600+ struct unlink_args args = {
523b37e3
AM
33601+ .errp = &err,
33602+ .dir = dir,
33603+ .path = path,
33604+ .delegated_inode = delegated_inode
1facf9fc 33605+ };
33606+
33607+ if (!force)
33608+ call_unlink(&args);
33609+ else {
33610+ int wkq_err;
33611+
33612+ wkq_err = au_wkq_wait(call_unlink, &args);
33613+ if (unlikely(wkq_err))
33614+ err = wkq_err;
33615+ }
33616+
33617+ return err;
33618+}
7f207e10 33619diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
eca34b5c 33620--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
016522bc 33621+++ linux/fs/aufs/vfsub.h 2020-01-27 10:57:18.178871751 +0100
eca801bf 33622@@ -0,0 +1,354 @@
062440b3 33623+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33624+/*
016522bc 33625+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 33626+ *
33627+ * This program, aufs is free software; you can redistribute it and/or modify
33628+ * it under the terms of the GNU General Public License as published by
33629+ * the Free Software Foundation; either version 2 of the License, or
33630+ * (at your option) any later version.
dece6358
AM
33631+ *
33632+ * This program is distributed in the hope that it will be useful,
33633+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33634+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33635+ * GNU General Public License for more details.
33636+ *
33637+ * You should have received a copy of the GNU General Public License
523b37e3 33638+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33639+ */
33640+
33641+/*
33642+ * sub-routines for VFS
33643+ */
33644+
33645+#ifndef __AUFS_VFSUB_H__
33646+#define __AUFS_VFSUB_H__
33647+
33648+#ifdef __KERNEL__
33649+
33650+#include <linux/fs.h>
b4510431 33651+#include <linux/mount.h>
8cdd5066 33652+#include <linux/posix_acl.h>
c1595e42 33653+#include <linux/xattr.h>
7f207e10 33654+#include "debug.h"
1facf9fc 33655+
7f207e10 33656+/* copied from linux/fs/internal.h */
2cbb1c4b 33657+/* todo: BAD approach!! */
c06a8ce3 33658+extern void __mnt_drop_write(struct vfsmount *);
acd2b654 33659+extern struct file *alloc_empty_file(int, const struct cred *);
7f207e10
AM
33660+
33661+/* ---------------------------------------------------------------------- */
1facf9fc 33662+
33663+/* lock subclass for lower inode */
33664+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33665+/* reduce? gave up. */
33666+enum {
c1595e42 33667+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33668+ AuLsc_I_PARENT, /* lower inode, parent first */
33669+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33670+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33671+ AuLsc_I_CHILD,
33672+ AuLsc_I_CHILD2,
33673+ AuLsc_I_End
33674+};
33675+
33676+/* to debug easier, do not make them inlined functions */
33677+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33678+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33679+
33680+/* ---------------------------------------------------------------------- */
33681+
7f207e10
AM
33682+static inline void vfsub_drop_nlink(struct inode *inode)
33683+{
33684+ AuDebugOn(!inode->i_nlink);
33685+ drop_nlink(inode);
33686+}
33687+
027c5e7a
AM
33688+static inline void vfsub_dead_dir(struct inode *inode)
33689+{
33690+ AuDebugOn(!S_ISDIR(inode->i_mode));
33691+ inode->i_flags |= S_DEAD;
33692+ clear_nlink(inode);
33693+}
33694+
392086de
AM
33695+static inline int vfsub_native_ro(struct inode *inode)
33696+{
8b6a4947 33697+ return sb_rdonly(inode->i_sb)
392086de
AM
33698+ || IS_RDONLY(inode)
33699+ /* || IS_APPEND(inode) */
33700+ || IS_IMMUTABLE(inode);
33701+}
33702+
8cdd5066
JR
33703+#ifdef CONFIG_AUFS_BR_FUSE
33704+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33705+#else
33706+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33707+#endif
33708+
a2654f78
AM
33709+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33710+
7f207e10
AM
33711+/* ---------------------------------------------------------------------- */
33712+
33713+int vfsub_update_h_iattr(struct path *h_path, int *did);
33714+struct file *vfsub_dentry_open(struct path *path, int flags);
33715+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
acd2b654 33716+struct au_branch;
b912730e 33717+struct vfsub_aopen_args {
acd2b654
AM
33718+ struct file *file;
33719+ unsigned int open_flag;
33720+ umode_t create_mode;
33721+ struct au_branch *br;
b912730e 33722+};
b912730e 33723+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 33724+ struct vfsub_aopen_args *args);
1facf9fc 33725+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33726+
febd17d6
JR
33727+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33728+ struct dentry *parent, int len);
1facf9fc 33729+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33730+ int len);
b4510431
AM
33731+
33732+struct vfsub_lkup_one_args {
33733+ struct dentry **errp;
33734+ struct qstr *name;
33735+ struct dentry *parent;
33736+};
33737+
33738+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33739+ struct dentry *parent)
33740+{
33741+ return vfsub_lookup_one_len(name->name, parent, name->len);
33742+}
33743+
33744+void vfsub_call_lkup_one(void *args);
33745+
33746+/* ---------------------------------------------------------------------- */
33747+
33748+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33749+{
33750+ int err;
076b876e 33751+
b4510431
AM
33752+ lockdep_off();
33753+ err = mnt_want_write(mnt);
33754+ lockdep_on();
33755+ return err;
33756+}
33757+
33758+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33759+{
33760+ lockdep_off();
33761+ mnt_drop_write(mnt);
33762+ lockdep_on();
33763+}
1facf9fc 33764+
7e9cd9fe 33765+#if 0 /* reserved */
c06a8ce3
AM
33766+static inline void vfsub_mnt_drop_write_file(struct file *file)
33767+{
33768+ lockdep_off();
33769+ mnt_drop_write_file(file);
33770+ lockdep_on();
33771+}
7e9cd9fe 33772+#endif
c06a8ce3 33773+
1facf9fc 33774+/* ---------------------------------------------------------------------- */
33775+
33776+struct au_hinode;
33777+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33778+ struct dentry *d2, struct au_hinode *hdir2);
33779+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33780+ struct dentry *d2, struct au_hinode *hdir2);
33781+
537831f9
AM
33782+int vfsub_create(struct inode *dir, struct path *path, int mode,
33783+ bool want_excl);
1facf9fc 33784+int vfsub_symlink(struct inode *dir, struct path *path,
33785+ const char *symname);
33786+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33787+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33788+ struct path *path, struct inode **delegated_inode);
1facf9fc 33789+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33790+ struct inode *hdir, struct path *path,
f2c43d5f 33791+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33792+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33793+int vfsub_rmdir(struct inode *dir, struct path *path);
33794+
33795+/* ---------------------------------------------------------------------- */
33796+
33797+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33798+ loff_t *ppos);
33799+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33800+ loff_t *ppos);
33801+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33802+ loff_t *ppos);
33803+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33804+ loff_t *ppos);
4a4d8108 33805+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33806+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33807+
c06a8ce3
AM
33808+static inline loff_t vfsub_f_size_read(struct file *file)
33809+{
33810+ return i_size_read(file_inode(file));
33811+}
33812+
4a4d8108
AM
33813+static inline unsigned int vfsub_file_flags(struct file *file)
33814+{
33815+ unsigned int flags;
33816+
33817+ spin_lock(&file->f_lock);
33818+ flags = file->f_flags;
33819+ spin_unlock(&file->f_lock);
33820+
33821+ return flags;
33822+}
1308ab2a 33823+
f0c0a007
AM
33824+static inline int vfsub_file_execed(struct file *file)
33825+{
33826+ /* todo: direct access f_flags */
33827+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33828+}
33829+
7e9cd9fe 33830+#if 0 /* reserved */
1facf9fc 33831+static inline void vfsub_file_accessed(struct file *h_file)
33832+{
33833+ file_accessed(h_file);
33834+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33835+}
7e9cd9fe 33836+#endif
1facf9fc 33837+
79b8bda9 33838+#if 0 /* reserved */
1facf9fc 33839+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33840+ struct dentry *h_dentry)
33841+{
33842+ struct path h_path = {
33843+ .dentry = h_dentry,
33844+ .mnt = h_mnt
33845+ };
92d182d2 33846+ touch_atime(&h_path);
1facf9fc 33847+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33848+}
79b8bda9 33849+#endif
1facf9fc 33850+
cd7a4cd9
AM
33851+static inline int vfsub_update_time(struct inode *h_inode,
33852+ struct timespec64 *ts, int flags)
0c3ec466 33853+{
5afbbe0d 33854+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33855+ /* no vfsub_update_h_iattr() since we don't have struct path */
33856+}
33857+
8cdd5066
JR
33858+#ifdef CONFIG_FS_POSIX_ACL
33859+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33860+{
33861+ int err;
33862+
33863+ err = posix_acl_chmod(h_inode, h_mode);
33864+ if (err == -EOPNOTSUPP)
33865+ err = 0;
33866+ return err;
33867+}
33868+#else
33869+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33870+#endif
33871+
4a4d8108
AM
33872+long vfsub_splice_to(struct file *in, loff_t *ppos,
33873+ struct pipe_inode_info *pipe, size_t len,
33874+ unsigned int flags);
33875+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33876+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33877+
33878+static inline long vfsub_truncate(struct path *path, loff_t length)
33879+{
33880+ long err;
076b876e 33881+
c06a8ce3
AM
33882+ lockdep_off();
33883+ err = vfs_truncate(path, length);
33884+ lockdep_on();
33885+ return err;
33886+}
33887+
4a4d8108
AM
33888+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33889+ struct file *h_file);
53392da6 33890+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 33891+
521ced18
JR
33892+/*
33893+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33894+ * ioctl.
33895+ */
9f237c51
AM
33896+static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33897+ loff_t len)
521ced18 33898+{
9f237c51 33899+ loff_t err;
521ced18
JR
33900+
33901+ lockdep_off();
9f237c51 33902+ err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
521ced18
JR
33903+ lockdep_on();
33904+
33905+ return err;
33906+}
33907+
33908+/* copy_file_range(2) is a systemcall */
33909+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33910+ struct file *dst, loff_t dst_pos,
33911+ size_t len, unsigned int flags)
33912+{
33913+ ssize_t ssz;
33914+
33915+ lockdep_off();
33916+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33917+ lockdep_on();
33918+
33919+ return ssz;
33920+}
33921+
1facf9fc 33922+/* ---------------------------------------------------------------------- */
33923+
33924+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33925+{
33926+ loff_t err;
33927+
2cbb1c4b 33928+ lockdep_off();
1facf9fc 33929+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 33930+ lockdep_on();
1facf9fc 33931+ return err;
33932+}
33933+
33934+/* ---------------------------------------------------------------------- */
33935+
4a4d8108
AM
33936+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33937+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
33938+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33939+ struct inode **delegated_inode);
33940+int vfsub_notify_change(struct path *path, struct iattr *ia,
33941+ struct inode **delegated_inode);
33942+int vfsub_unlink(struct inode *dir, struct path *path,
33943+ struct inode **delegated_inode, int force);
4a4d8108 33944+
521ced18
JR
33945+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33946+{
33947+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33948+}
33949+
c1595e42
JR
33950+/* ---------------------------------------------------------------------- */
33951+
33952+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33953+ const void *value, size_t size, int flags)
33954+{
33955+ int err;
33956+
33957+ lockdep_off();
33958+ err = vfs_setxattr(dentry, name, value, size, flags);
33959+ lockdep_on();
33960+
33961+ return err;
33962+}
33963+
33964+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33965+{
33966+ int err;
33967+
33968+ lockdep_off();
33969+ err = vfs_removexattr(dentry, name);
33970+ lockdep_on();
33971+
33972+ return err;
33973+}
33974+
1facf9fc 33975+#endif /* __KERNEL__ */
33976+#endif /* __AUFS_VFSUB_H__ */
7f207e10 33977diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
eca34b5c 33978--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
016522bc 33979+++ linux/fs/aufs/wbr_policy.c 2020-01-27 10:57:18.178871751 +0100
cd7a4cd9
AM
33980@@ -0,0 +1,830 @@
33981+// SPDX-License-Identifier: GPL-2.0
1facf9fc 33982+/*
016522bc 33983+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 33984+ *
33985+ * This program, aufs is free software; you can redistribute it and/or modify
33986+ * it under the terms of the GNU General Public License as published by
33987+ * the Free Software Foundation; either version 2 of the License, or
33988+ * (at your option) any later version.
dece6358
AM
33989+ *
33990+ * This program is distributed in the hope that it will be useful,
33991+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33992+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33993+ * GNU General Public License for more details.
33994+ *
33995+ * You should have received a copy of the GNU General Public License
523b37e3 33996+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33997+ */
33998+
33999+/*
34000+ * policies for selecting one among multiple writable branches
34001+ */
34002+
34003+#include <linux/statfs.h>
34004+#include "aufs.h"
34005+
34006+/* subset of cpup_attr() */
34007+static noinline_for_stack
34008+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34009+{
34010+ int err, sbits;
34011+ struct iattr ia;
34012+ struct inode *h_isrc;
34013+
5527c038 34014+ h_isrc = d_inode(h_src);
1facf9fc 34015+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34016+ ia.ia_mode = h_isrc->i_mode;
34017+ ia.ia_uid = h_isrc->i_uid;
34018+ ia.ia_gid = h_isrc->i_gid;
34019+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 34020+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
34021+ /* no delegation since it is just created */
34022+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34023+
34024+ /* is this nfs only? */
34025+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34026+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34027+ ia.ia_mode = h_isrc->i_mode;
523b37e3 34028+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34029+ }
34030+
34031+ return err;
34032+}
34033+
34034+#define AuCpdown_PARENT_OPQ 1
34035+#define AuCpdown_WHED (1 << 1)
34036+#define AuCpdown_MADE_DIR (1 << 2)
34037+#define AuCpdown_DIROPQ (1 << 3)
34038+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
34039+#define au_fset_cpdown(flags, name) \
34040+ do { (flags) |= AuCpdown_##name; } while (0)
34041+#define au_fclr_cpdown(flags, name) \
34042+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 34043+
1facf9fc 34044+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 34045+ unsigned int *flags)
1facf9fc 34046+{
34047+ int err;
34048+ struct dentry *opq_dentry;
34049+
34050+ opq_dentry = au_diropq_create(dentry, bdst);
34051+ err = PTR_ERR(opq_dentry);
34052+ if (IS_ERR(opq_dentry))
34053+ goto out;
34054+ dput(opq_dentry);
c2b27bf2 34055+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 34056+
4f0767ce 34057+out:
1facf9fc 34058+ return err;
34059+}
34060+
34061+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34062+ struct inode *dir, aufs_bindex_t bdst)
34063+{
34064+ int err;
34065+ struct path h_path;
34066+ struct au_branch *br;
34067+
34068+ br = au_sbr(dentry->d_sb, bdst);
34069+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34070+ err = PTR_ERR(h_path.dentry);
34071+ if (IS_ERR(h_path.dentry))
34072+ goto out;
34073+
34074+ err = 0;
5527c038 34075+ if (d_is_positive(h_path.dentry)) {
86dc4139 34076+ h_path.mnt = au_br_mnt(br);
1facf9fc 34077+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34078+ dentry);
34079+ }
34080+ dput(h_path.dentry);
34081+
4f0767ce 34082+out:
1facf9fc 34083+ return err;
34084+}
34085+
34086+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 34087+ struct au_pin *pin,
1facf9fc 34088+ struct dentry *h_parent, void *arg)
34089+{
34090+ int err, rerr;
5afbbe0d 34091+ aufs_bindex_t bopq, btop;
1facf9fc 34092+ struct path h_path;
34093+ struct dentry *parent;
34094+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 34095+ unsigned int *flags = arg;
1facf9fc 34096+
5afbbe0d 34097+ btop = au_dbtop(dentry);
1facf9fc 34098+ /* dentry is di-locked */
34099+ parent = dget_parent(dentry);
5527c038
JR
34100+ dir = d_inode(parent);
34101+ h_dir = d_inode(h_parent);
1facf9fc 34102+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34103+ IMustLock(h_dir);
34104+
86dc4139 34105+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 34106+ if (unlikely(err < 0))
34107+ goto out;
34108+ h_path.dentry = au_h_dptr(dentry, bdst);
34109+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
cd7a4cd9 34110+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
1facf9fc 34111+ if (unlikely(err))
34112+ goto out_put;
c2b27bf2 34113+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 34114+
1facf9fc 34115+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
34116+ au_fclr_cpdown(*flags, WHED);
34117+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 34118+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
34119+ au_fset_cpdown(*flags, WHED);
34120+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34121+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 34122+ h_inode = d_inode(h_path.dentry);
febd17d6 34123+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
34124+ if (au_ftest_cpdown(*flags, WHED)) {
34125+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 34126+ if (unlikely(err)) {
febd17d6 34127+ inode_unlock(h_inode);
1facf9fc 34128+ goto out_dir;
34129+ }
34130+ }
34131+
5afbbe0d 34132+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 34133+ inode_unlock(h_inode);
1facf9fc 34134+ if (unlikely(err))
34135+ goto out_opq;
34136+
c2b27bf2 34137+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 34138+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34139+ if (unlikely(err))
34140+ goto out_opq;
34141+ }
34142+
5527c038 34143+ inode = d_inode(dentry);
5afbbe0d
AM
34144+ if (au_ibbot(inode) < bdst)
34145+ au_set_ibbot(inode, bdst);
1facf9fc 34146+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34147+ au_hi_flags(inode, /*isdir*/1));
076b876e 34148+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 34149+ goto out; /* success */
34150+
34151+ /* revert */
4f0767ce 34152+out_opq:
c2b27bf2 34153+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 34154+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 34155+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 34156+ inode_unlock(h_inode);
1facf9fc 34157+ if (unlikely(rerr)) {
523b37e3
AM
34158+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34159+ dentry, bdst, rerr);
1facf9fc 34160+ err = -EIO;
34161+ goto out;
34162+ }
34163+ }
4f0767ce 34164+out_dir:
c2b27bf2 34165+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 34166+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34167+ if (unlikely(rerr)) {
523b37e3
AM
34168+ AuIOErr("failed removing %pd b%d (%d)\n",
34169+ dentry, bdst, rerr);
1facf9fc 34170+ err = -EIO;
34171+ }
34172+ }
4f0767ce 34173+out_put:
1facf9fc 34174+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
34175+ if (au_dbbot(dentry) == bdst)
34176+ au_update_dbbot(dentry);
4f0767ce 34177+out:
1facf9fc 34178+ dput(parent);
34179+ return err;
34180+}
34181+
34182+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34183+{
34184+ int err;
c2b27bf2 34185+ unsigned int flags;
1facf9fc 34186+
c2b27bf2
AM
34187+ flags = 0;
34188+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 34189+
34190+ return err;
34191+}
34192+
34193+/* ---------------------------------------------------------------------- */
34194+
34195+/* policies for create */
34196+
c2b27bf2 34197+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
34198+{
34199+ int err, i, j, ndentry;
34200+ aufs_bindex_t bopq;
34201+ struct au_dcsub_pages dpages;
34202+ struct au_dpage *dpage;
34203+ struct dentry **dentries, *parent, *d;
34204+
34205+ err = au_dpages_init(&dpages, GFP_NOFS);
34206+ if (unlikely(err))
34207+ goto out;
34208+ parent = dget_parent(dentry);
027c5e7a 34209+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
34210+ if (unlikely(err))
34211+ goto out_free;
34212+
34213+ err = bindex;
34214+ for (i = 0; i < dpages.ndpage; i++) {
34215+ dpage = dpages.dpages + i;
34216+ dentries = dpage->dentries;
34217+ ndentry = dpage->ndentry;
34218+ for (j = 0; j < ndentry; j++) {
34219+ d = dentries[j];
34220+ di_read_lock_parent2(d, !AuLock_IR);
34221+ bopq = au_dbdiropq(d);
34222+ di_read_unlock(d, !AuLock_IR);
34223+ if (bopq >= 0 && bopq < err)
34224+ err = bopq;
34225+ }
34226+ }
34227+
34228+out_free:
34229+ dput(parent);
34230+ au_dpages_free(&dpages);
34231+out:
34232+ return err;
34233+}
34234+
1facf9fc 34235+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34236+{
34237+ for (; bindex >= 0; bindex--)
34238+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34239+ return bindex;
34240+ return -EROFS;
34241+}
34242+
34243+/* top down parent */
392086de
AM
34244+static int au_wbr_create_tdp(struct dentry *dentry,
34245+ unsigned int flags __maybe_unused)
1facf9fc 34246+{
34247+ int err;
5afbbe0d 34248+ aufs_bindex_t btop, bindex;
1facf9fc 34249+ struct super_block *sb;
34250+ struct dentry *parent, *h_parent;
34251+
34252+ sb = dentry->d_sb;
5afbbe0d
AM
34253+ btop = au_dbtop(dentry);
34254+ err = btop;
34255+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 34256+ goto out;
34257+
34258+ err = -EROFS;
34259+ parent = dget_parent(dentry);
5afbbe0d 34260+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 34261+ h_parent = au_h_dptr(parent, bindex);
5527c038 34262+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34263+ continue;
34264+
34265+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34266+ err = bindex;
34267+ break;
34268+ }
34269+ }
34270+ dput(parent);
34271+
34272+ /* bottom up here */
4a4d8108 34273+ if (unlikely(err < 0)) {
5afbbe0d 34274+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
34275+ if (err >= 0)
34276+ err = au_wbr_nonopq(dentry, err);
34277+ }
1facf9fc 34278+
4f0767ce 34279+out:
1facf9fc 34280+ AuDbg("b%d\n", err);
34281+ return err;
34282+}
34283+
34284+/* ---------------------------------------------------------------------- */
34285+
34286+/* an exception for the policy other than tdp */
34287+static int au_wbr_create_exp(struct dentry *dentry)
34288+{
34289+ int err;
34290+ aufs_bindex_t bwh, bdiropq;
34291+ struct dentry *parent;
34292+
34293+ err = -1;
34294+ bwh = au_dbwh(dentry);
34295+ parent = dget_parent(dentry);
34296+ bdiropq = au_dbdiropq(parent);
34297+ if (bwh >= 0) {
34298+ if (bdiropq >= 0)
34299+ err = min(bdiropq, bwh);
34300+ else
34301+ err = bwh;
34302+ AuDbg("%d\n", err);
34303+ } else if (bdiropq >= 0) {
34304+ err = bdiropq;
34305+ AuDbg("%d\n", err);
34306+ }
34307+ dput(parent);
34308+
4a4d8108
AM
34309+ if (err >= 0)
34310+ err = au_wbr_nonopq(dentry, err);
34311+
1facf9fc 34312+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34313+ err = -1;
34314+
34315+ AuDbg("%d\n", err);
34316+ return err;
34317+}
34318+
34319+/* ---------------------------------------------------------------------- */
34320+
34321+/* round robin */
34322+static int au_wbr_create_init_rr(struct super_block *sb)
34323+{
34324+ int err;
34325+
5afbbe0d 34326+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 34327+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 34328+ /* smp_mb(); */
1facf9fc 34329+
34330+ AuDbg("b%d\n", err);
34331+ return err;
34332+}
34333+
392086de 34334+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 34335+{
34336+ int err, nbr;
34337+ unsigned int u;
5afbbe0d 34338+ aufs_bindex_t bindex, bbot;
1facf9fc 34339+ struct super_block *sb;
34340+ atomic_t *next;
34341+
34342+ err = au_wbr_create_exp(dentry);
34343+ if (err >= 0)
34344+ goto out;
34345+
34346+ sb = dentry->d_sb;
34347+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34348+ bbot = au_sbbot(sb);
34349+ nbr = bbot + 1;
34350+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34351+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34352+ err = atomic_dec_return(next) + 1;
34353+ /* modulo for 0 is meaningless */
34354+ if (unlikely(!err))
34355+ err = atomic_dec_return(next) + 1;
34356+ } else
34357+ err = atomic_read(next);
34358+ AuDbg("%d\n", err);
34359+ u = err;
34360+ err = u % nbr;
34361+ AuDbg("%d\n", err);
34362+ if (!au_br_rdonly(au_sbr(sb, err)))
34363+ break;
34364+ err = -EROFS;
34365+ }
34366+
4a4d8108
AM
34367+ if (err >= 0)
34368+ err = au_wbr_nonopq(dentry, err);
34369+
4f0767ce 34370+out:
1facf9fc 34371+ AuDbg("%d\n", err);
34372+ return err;
34373+}
34374+
34375+/* ---------------------------------------------------------------------- */
34376+
34377+/* most free space */
392086de 34378+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34379+{
34380+ struct super_block *sb;
34381+ struct au_branch *br;
34382+ struct au_wbr_mfs *mfs;
392086de 34383+ struct dentry *h_parent;
5afbbe0d 34384+ aufs_bindex_t bindex, bbot;
1facf9fc 34385+ int err;
34386+ unsigned long long b, bavail;
7f207e10 34387+ struct path h_path;
1facf9fc 34388+ /* reduce the stack usage */
34389+ struct kstatfs *st;
34390+
34391+ st = kmalloc(sizeof(*st), GFP_NOFS);
34392+ if (unlikely(!st)) {
34393+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34394+ return;
34395+ }
34396+
34397+ bavail = 0;
34398+ sb = dentry->d_sb;
34399+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34400+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34401+ mfs->mfs_bindex = -EROFS;
34402+ mfs->mfsrr_bytes = 0;
392086de
AM
34403+ if (!parent) {
34404+ bindex = 0;
5afbbe0d 34405+ bbot = au_sbbot(sb);
392086de 34406+ } else {
5afbbe0d
AM
34407+ bindex = au_dbtop(parent);
34408+ bbot = au_dbtaildir(parent);
392086de
AM
34409+ }
34410+
5afbbe0d 34411+ for (; bindex <= bbot; bindex++) {
392086de
AM
34412+ if (parent) {
34413+ h_parent = au_h_dptr(parent, bindex);
5527c038 34414+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34415+ continue;
34416+ }
1facf9fc 34417+ br = au_sbr(sb, bindex);
34418+ if (au_br_rdonly(br))
34419+ continue;
34420+
34421+ /* sb->s_root for NFS is unreliable */
86dc4139 34422+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34423+ h_path.dentry = h_path.mnt->mnt_root;
34424+ err = vfs_statfs(&h_path, st);
1facf9fc 34425+ if (unlikely(err)) {
34426+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34427+ continue;
34428+ }
34429+
34430+ /* when the available size is equal, select the lower one */
34431+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34432+ || sizeof(b) < sizeof(st->f_bsize));
34433+ b = st->f_bavail * st->f_bsize;
34434+ br->br_wbr->wbr_bytes = b;
34435+ if (b >= bavail) {
34436+ bavail = b;
34437+ mfs->mfs_bindex = bindex;
34438+ mfs->mfs_jiffy = jiffies;
34439+ }
34440+ }
34441+
34442+ mfs->mfsrr_bytes = bavail;
34443+ AuDbg("b%d\n", mfs->mfs_bindex);
9f237c51 34444+ au_kfree_rcu(st);
1facf9fc 34445+}
34446+
392086de 34447+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34448+{
34449+ int err;
392086de 34450+ struct dentry *parent;
1facf9fc 34451+ struct super_block *sb;
34452+ struct au_wbr_mfs *mfs;
34453+
34454+ err = au_wbr_create_exp(dentry);
34455+ if (err >= 0)
34456+ goto out;
34457+
34458+ sb = dentry->d_sb;
392086de
AM
34459+ parent = NULL;
34460+ if (au_ftest_wbr(flags, PARENT))
34461+ parent = dget_parent(dentry);
1facf9fc 34462+ mfs = &au_sbi(sb)->si_wbr_mfs;
34463+ mutex_lock(&mfs->mfs_lock);
34464+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34465+ || mfs->mfs_bindex < 0
34466+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34467+ au_mfs(dentry, parent);
1facf9fc 34468+ mutex_unlock(&mfs->mfs_lock);
34469+ err = mfs->mfs_bindex;
392086de 34470+ dput(parent);
1facf9fc 34471+
4a4d8108
AM
34472+ if (err >= 0)
34473+ err = au_wbr_nonopq(dentry, err);
34474+
4f0767ce 34475+out:
1facf9fc 34476+ AuDbg("b%d\n", err);
34477+ return err;
34478+}
34479+
34480+static int au_wbr_create_init_mfs(struct super_block *sb)
34481+{
34482+ struct au_wbr_mfs *mfs;
34483+
34484+ mfs = &au_sbi(sb)->si_wbr_mfs;
34485+ mutex_init(&mfs->mfs_lock);
34486+ mfs->mfs_jiffy = 0;
34487+ mfs->mfs_bindex = -EROFS;
34488+
34489+ return 0;
34490+}
34491+
34492+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34493+{
34494+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34495+ return 0;
34496+}
34497+
34498+/* ---------------------------------------------------------------------- */
34499+
f2c43d5f
AM
34500+/* top down regardless parent, and then mfs */
34501+static int au_wbr_create_tdmfs(struct dentry *dentry,
34502+ unsigned int flags __maybe_unused)
34503+{
34504+ int err;
34505+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34506+ unsigned long long watermark;
34507+ struct super_block *sb;
34508+ struct au_wbr_mfs *mfs;
34509+ struct au_branch *br;
34510+ struct dentry *parent;
34511+
34512+ sb = dentry->d_sb;
34513+ mfs = &au_sbi(sb)->si_wbr_mfs;
34514+ mutex_lock(&mfs->mfs_lock);
34515+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34516+ || mfs->mfs_bindex < 0)
34517+ au_mfs(dentry, /*parent*/NULL);
34518+ watermark = mfs->mfsrr_watermark;
34519+ bmfs = mfs->mfs_bindex;
34520+ mutex_unlock(&mfs->mfs_lock);
34521+
34522+ /* another style of au_wbr_create_exp() */
34523+ bwh = au_dbwh(dentry);
34524+ parent = dget_parent(dentry);
34525+ btail = au_dbtaildir(parent);
34526+ if (bwh >= 0 && bwh < btail)
34527+ btail = bwh;
34528+
34529+ err = au_wbr_nonopq(dentry, btail);
34530+ if (unlikely(err < 0))
34531+ goto out;
34532+ btail = err;
34533+ bfound = -1;
34534+ for (bindex = 0; bindex <= btail; bindex++) {
34535+ br = au_sbr(sb, bindex);
34536+ if (au_br_rdonly(br))
34537+ continue;
34538+ if (br->br_wbr->wbr_bytes > watermark) {
34539+ bfound = bindex;
34540+ break;
34541+ }
34542+ }
34543+ err = bfound;
34544+ if (err < 0)
34545+ err = bmfs;
34546+
34547+out:
34548+ dput(parent);
34549+ AuDbg("b%d\n", err);
34550+ return err;
34551+}
34552+
34553+/* ---------------------------------------------------------------------- */
34554+
1facf9fc 34555+/* most free space and then round robin */
392086de 34556+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34557+{
34558+ int err;
34559+ struct au_wbr_mfs *mfs;
34560+
392086de 34561+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34562+ if (err >= 0) {
34563+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34564+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34565+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34566+ err = au_wbr_create_rr(dentry, flags);
dece6358 34567+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34568+ }
34569+
34570+ AuDbg("b%d\n", err);
34571+ return err;
34572+}
34573+
34574+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34575+{
34576+ int err;
34577+
34578+ au_wbr_create_init_mfs(sb); /* ignore */
34579+ err = au_wbr_create_init_rr(sb);
34580+
34581+ return err;
34582+}
34583+
34584+/* ---------------------------------------------------------------------- */
34585+
34586+/* top down parent and most free space */
392086de 34587+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34588+{
34589+ int err, e2;
34590+ unsigned long long b;
5afbbe0d 34591+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34592+ struct super_block *sb;
34593+ struct dentry *parent, *h_parent;
34594+ struct au_branch *br;
34595+
392086de 34596+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34597+ if (unlikely(err < 0))
34598+ goto out;
34599+ parent = dget_parent(dentry);
5afbbe0d
AM
34600+ btop = au_dbtop(parent);
34601+ bbot = au_dbtaildir(parent);
34602+ if (btop == bbot)
1facf9fc 34603+ goto out_parent; /* success */
34604+
392086de 34605+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34606+ if (e2 < 0)
34607+ goto out_parent; /* success */
34608+
34609+ /* when the available size is equal, select upper one */
34610+ sb = dentry->d_sb;
34611+ br = au_sbr(sb, err);
34612+ b = br->br_wbr->wbr_bytes;
34613+ AuDbg("b%d, %llu\n", err, b);
34614+
5afbbe0d 34615+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34616+ h_parent = au_h_dptr(parent, bindex);
5527c038 34617+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34618+ continue;
34619+
34620+ br = au_sbr(sb, bindex);
34621+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34622+ b = br->br_wbr->wbr_bytes;
34623+ err = bindex;
34624+ AuDbg("b%d, %llu\n", err, b);
34625+ }
34626+ }
34627+
4a4d8108
AM
34628+ if (err >= 0)
34629+ err = au_wbr_nonopq(dentry, err);
34630+
4f0767ce 34631+out_parent:
1facf9fc 34632+ dput(parent);
4f0767ce 34633+out:
1facf9fc 34634+ AuDbg("b%d\n", err);
34635+ return err;
34636+}
34637+
34638+/* ---------------------------------------------------------------------- */
34639+
392086de
AM
34640+/*
34641+ * - top down parent
34642+ * - most free space with parent
34643+ * - most free space round-robin regardless parent
34644+ */
34645+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34646+{
34647+ int err;
34648+ unsigned long long watermark;
34649+ struct super_block *sb;
34650+ struct au_branch *br;
34651+ struct au_wbr_mfs *mfs;
34652+
34653+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34654+ if (unlikely(err < 0))
34655+ goto out;
34656+
34657+ sb = dentry->d_sb;
34658+ br = au_sbr(sb, err);
34659+ mfs = &au_sbi(sb)->si_wbr_mfs;
34660+ mutex_lock(&mfs->mfs_lock);
34661+ watermark = mfs->mfsrr_watermark;
34662+ mutex_unlock(&mfs->mfs_lock);
34663+ if (br->br_wbr->wbr_bytes < watermark)
34664+ /* regardless the parent dir */
34665+ err = au_wbr_create_mfsrr(dentry, flags);
34666+
34667+out:
34668+ AuDbg("b%d\n", err);
34669+ return err;
34670+}
34671+
34672+/* ---------------------------------------------------------------------- */
34673+
1facf9fc 34674+/* policies for copyup */
34675+
34676+/* top down parent */
34677+static int au_wbr_copyup_tdp(struct dentry *dentry)
34678+{
392086de 34679+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34680+}
34681+
34682+/* bottom up parent */
34683+static int au_wbr_copyup_bup(struct dentry *dentry)
34684+{
34685+ int err;
5afbbe0d 34686+ aufs_bindex_t bindex, btop;
1facf9fc 34687+ struct dentry *parent, *h_parent;
34688+ struct super_block *sb;
34689+
34690+ err = -EROFS;
34691+ sb = dentry->d_sb;
34692+ parent = dget_parent(dentry);
5afbbe0d
AM
34693+ btop = au_dbtop(parent);
34694+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34695+ h_parent = au_h_dptr(parent, bindex);
5527c038 34696+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34697+ continue;
34698+
34699+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34700+ err = bindex;
34701+ break;
34702+ }
34703+ }
34704+ dput(parent);
34705+
34706+ /* bottom up here */
34707+ if (unlikely(err < 0))
5afbbe0d 34708+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34709+
34710+ AuDbg("b%d\n", err);
34711+ return err;
34712+}
34713+
34714+/* bottom up */
5afbbe0d 34715+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34716+{
34717+ int err;
34718+
5afbbe0d 34719+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34720+ AuDbg("b%d\n", err);
5afbbe0d 34721+ if (err > btop)
4a4d8108 34722+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34723+
34724+ AuDbg("b%d\n", err);
34725+ return err;
34726+}
34727+
076b876e
AM
34728+static int au_wbr_copyup_bu(struct dentry *dentry)
34729+{
34730+ int err;
5afbbe0d 34731+ aufs_bindex_t btop;
076b876e 34732+
5afbbe0d
AM
34733+ btop = au_dbtop(dentry);
34734+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34735+ return err;
34736+}
34737+
1facf9fc 34738+/* ---------------------------------------------------------------------- */
34739+
34740+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34741+ [AuWbrCopyup_TDP] = {
34742+ .copyup = au_wbr_copyup_tdp
34743+ },
34744+ [AuWbrCopyup_BUP] = {
34745+ .copyup = au_wbr_copyup_bup
34746+ },
34747+ [AuWbrCopyup_BU] = {
34748+ .copyup = au_wbr_copyup_bu
34749+ }
34750+};
34751+
34752+struct au_wbr_create_operations au_wbr_create_ops[] = {
34753+ [AuWbrCreate_TDP] = {
34754+ .create = au_wbr_create_tdp
34755+ },
34756+ [AuWbrCreate_RR] = {
34757+ .create = au_wbr_create_rr,
34758+ .init = au_wbr_create_init_rr
34759+ },
34760+ [AuWbrCreate_MFS] = {
34761+ .create = au_wbr_create_mfs,
34762+ .init = au_wbr_create_init_mfs,
34763+ .fin = au_wbr_create_fin_mfs
34764+ },
34765+ [AuWbrCreate_MFSV] = {
34766+ .create = au_wbr_create_mfs,
34767+ .init = au_wbr_create_init_mfs,
34768+ .fin = au_wbr_create_fin_mfs
34769+ },
34770+ [AuWbrCreate_MFSRR] = {
34771+ .create = au_wbr_create_mfsrr,
34772+ .init = au_wbr_create_init_mfsrr,
34773+ .fin = au_wbr_create_fin_mfs
34774+ },
34775+ [AuWbrCreate_MFSRRV] = {
34776+ .create = au_wbr_create_mfsrr,
34777+ .init = au_wbr_create_init_mfsrr,
34778+ .fin = au_wbr_create_fin_mfs
34779+ },
f2c43d5f
AM
34780+ [AuWbrCreate_TDMFS] = {
34781+ .create = au_wbr_create_tdmfs,
34782+ .init = au_wbr_create_init_mfs,
34783+ .fin = au_wbr_create_fin_mfs
34784+ },
34785+ [AuWbrCreate_TDMFSV] = {
34786+ .create = au_wbr_create_tdmfs,
34787+ .init = au_wbr_create_init_mfs,
34788+ .fin = au_wbr_create_fin_mfs
34789+ },
1facf9fc 34790+ [AuWbrCreate_PMFS] = {
34791+ .create = au_wbr_create_pmfs,
34792+ .init = au_wbr_create_init_mfs,
34793+ .fin = au_wbr_create_fin_mfs
34794+ },
34795+ [AuWbrCreate_PMFSV] = {
34796+ .create = au_wbr_create_pmfs,
34797+ .init = au_wbr_create_init_mfs,
34798+ .fin = au_wbr_create_fin_mfs
392086de
AM
34799+ },
34800+ [AuWbrCreate_PMFSRR] = {
34801+ .create = au_wbr_create_pmfsrr,
34802+ .init = au_wbr_create_init_mfsrr,
34803+ .fin = au_wbr_create_fin_mfs
34804+ },
34805+ [AuWbrCreate_PMFSRRV] = {
34806+ .create = au_wbr_create_pmfsrr,
34807+ .init = au_wbr_create_init_mfsrr,
34808+ .fin = au_wbr_create_fin_mfs
1facf9fc 34809+ }
34810+};
7f207e10 34811diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
eca34b5c 34812--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
016522bc 34813+++ linux/fs/aufs/whout.c 2020-01-27 10:57:18.178871751 +0100
062440b3 34814@@ -0,0 +1,1062 @@
cd7a4cd9 34815+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34816+/*
016522bc 34817+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 34818+ *
34819+ * This program, aufs is free software; you can redistribute it and/or modify
34820+ * it under the terms of the GNU General Public License as published by
34821+ * the Free Software Foundation; either version 2 of the License, or
34822+ * (at your option) any later version.
dece6358
AM
34823+ *
34824+ * This program is distributed in the hope that it will be useful,
34825+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34826+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34827+ * GNU General Public License for more details.
34828+ *
34829+ * You should have received a copy of the GNU General Public License
523b37e3 34830+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34831+ */
34832+
34833+/*
34834+ * whiteout for logical deletion and opaque directory
34835+ */
34836+
1facf9fc 34837+#include "aufs.h"
34838+
cd7a4cd9 34839+#define WH_MASK 0444
1facf9fc 34840+
34841+/*
34842+ * If a directory contains this file, then it is opaque. We start with the
34843+ * .wh. flag so that it is blocked by lookup.
34844+ */
0c3ec466
AM
34845+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34846+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34847+
34848+/*
34849+ * generate whiteout name, which is NOT terminated by NULL.
34850+ * @name: original d_name.name
34851+ * @len: original d_name.len
34852+ * @wh: whiteout qstr
34853+ * returns zero when succeeds, otherwise error.
34854+ * succeeded value as wh->name should be freed by kfree().
34855+ */
34856+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34857+{
34858+ char *p;
34859+
34860+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34861+ return -ENAMETOOLONG;
34862+
34863+ wh->len = name->len + AUFS_WH_PFX_LEN;
34864+ p = kmalloc(wh->len, GFP_NOFS);
34865+ wh->name = p;
34866+ if (p) {
34867+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34868+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34869+ /* smp_mb(); */
34870+ return 0;
34871+ }
34872+ return -ENOMEM;
34873+}
34874+
34875+/* ---------------------------------------------------------------------- */
34876+
34877+/*
34878+ * test if the @wh_name exists under @h_parent.
34879+ * @try_sio specifies the necessary of super-io.
34880+ */
076b876e 34881+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 34882+{
34883+ int err;
34884+ struct dentry *wh_dentry;
1facf9fc 34885+
1facf9fc 34886+ if (!try_sio)
b4510431 34887+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 34888+ else
076b876e 34889+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 34890+ err = PTR_ERR(wh_dentry);
2000de60
JR
34891+ if (IS_ERR(wh_dentry)) {
34892+ if (err == -ENAMETOOLONG)
34893+ err = 0;
1facf9fc 34894+ goto out;
2000de60 34895+ }
1facf9fc 34896+
34897+ err = 0;
5527c038 34898+ if (d_is_negative(wh_dentry))
1facf9fc 34899+ goto out_wh; /* success */
34900+
34901+ err = 1;
7e9cd9fe 34902+ if (d_is_reg(wh_dentry))
1facf9fc 34903+ goto out_wh; /* success */
34904+
34905+ err = -EIO;
523b37e3 34906+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 34907+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 34908+
4f0767ce 34909+out_wh:
1facf9fc 34910+ dput(wh_dentry);
4f0767ce 34911+out:
1facf9fc 34912+ return err;
34913+}
34914+
34915+/*
34916+ * test if the @h_dentry sets opaque or not.
34917+ */
076b876e 34918+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 34919+{
34920+ int err;
34921+ struct inode *h_dir;
34922+
5527c038 34923+ h_dir = d_inode(h_dentry);
076b876e 34924+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 34925+ au_test_h_perm_sio(h_dir, MAY_EXEC));
34926+ return err;
34927+}
34928+
34929+/*
34930+ * returns a negative dentry whose name is unique and temporary.
34931+ */
34932+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34933+ struct qstr *prefix)
34934+{
1facf9fc 34935+ struct dentry *dentry;
34936+ int i;
027c5e7a 34937+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 34938+ *name, *p;
027c5e7a 34939+ /* strict atomic_t is unnecessary here */
1facf9fc 34940+ static unsigned short cnt;
34941+ struct qstr qs;
34942+
4a4d8108
AM
34943+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34944+
1facf9fc 34945+ name = defname;
027c5e7a
AM
34946+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34947+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 34948+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 34949+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 34950+ goto out;
34951+ dentry = ERR_PTR(-ENOMEM);
34952+ name = kmalloc(qs.len + 1, GFP_NOFS);
34953+ if (unlikely(!name))
34954+ goto out;
34955+ }
34956+
34957+ /* doubly whiteout-ed */
34958+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34959+ p = name + AUFS_WH_PFX_LEN * 2;
34960+ memcpy(p, prefix->name, prefix->len);
34961+ p += prefix->len;
34962+ *p++ = '.';
4a4d8108 34963+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 34964+
34965+ qs.name = name;
34966+ for (i = 0; i < 3; i++) {
b752ccd1 34967+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 34968+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 34969+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 34970+ goto out_name;
34971+ dput(dentry);
34972+ }
0c3ec466 34973+ /* pr_warn("could not get random name\n"); */
1facf9fc 34974+ dentry = ERR_PTR(-EEXIST);
34975+ AuDbg("%.*s\n", AuLNPair(&qs));
34976+ BUG();
34977+
4f0767ce 34978+out_name:
1facf9fc 34979+ if (name != defname)
9f237c51 34980+ au_kfree_try_rcu(name);
4f0767ce 34981+out:
4a4d8108 34982+ AuTraceErrPtr(dentry);
1facf9fc 34983+ return dentry;
1facf9fc 34984+}
34985+
34986+/*
34987+ * rename the @h_dentry on @br to the whiteouted temporary name.
34988+ */
34989+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34990+{
34991+ int err;
34992+ struct path h_path = {
86dc4139 34993+ .mnt = au_br_mnt(br)
1facf9fc 34994+ };
523b37e3 34995+ struct inode *h_dir, *delegated;
1facf9fc 34996+ struct dentry *h_parent;
34997+
34998+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 34999+ h_dir = d_inode(h_parent);
1facf9fc 35000+ IMustLock(h_dir);
35001+
35002+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35003+ err = PTR_ERR(h_path.dentry);
35004+ if (IS_ERR(h_path.dentry))
35005+ goto out;
35006+
35007+ /* under the same dir, no need to lock_rename() */
523b37e3 35008+ delegated = NULL;
f2c43d5f
AM
35009+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35010+ /*flags*/0);
1facf9fc 35011+ AuTraceErr(err);
523b37e3
AM
35012+ if (unlikely(err == -EWOULDBLOCK)) {
35013+ pr_warn("cannot retry for NFSv4 delegation"
35014+ " for an internal rename\n");
35015+ iput(delegated);
35016+ }
1facf9fc 35017+ dput(h_path.dentry);
35018+
4f0767ce 35019+out:
4a4d8108 35020+ AuTraceErr(err);
1facf9fc 35021+ return err;
35022+}
35023+
35024+/* ---------------------------------------------------------------------- */
35025+/*
35026+ * functions for removing a whiteout
35027+ */
35028+
35029+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35030+{
523b37e3
AM
35031+ int err, force;
35032+ struct inode *delegated;
1facf9fc 35033+
35034+ /*
35035+ * forces superio when the dir has a sticky bit.
35036+ * this may be a violation of unix fs semantics.
35037+ */
35038+ force = (h_dir->i_mode & S_ISVTX)
5527c038 35039+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
35040+ delegated = NULL;
35041+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
35042+ if (unlikely(err == -EWOULDBLOCK)) {
35043+ pr_warn("cannot retry for NFSv4 delegation"
35044+ " for an internal unlink\n");
35045+ iput(delegated);
35046+ }
35047+ return err;
1facf9fc 35048+}
35049+
35050+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35051+ struct dentry *dentry)
35052+{
35053+ int err;
35054+
35055+ err = do_unlink_wh(h_dir, h_path);
35056+ if (!err && dentry)
35057+ au_set_dbwh(dentry, -1);
35058+
35059+ return err;
35060+}
35061+
35062+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35063+ struct au_branch *br)
35064+{
35065+ int err;
35066+ struct path h_path = {
86dc4139 35067+ .mnt = au_br_mnt(br)
1facf9fc 35068+ };
35069+
35070+ err = 0;
b4510431 35071+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 35072+ if (IS_ERR(h_path.dentry))
35073+ err = PTR_ERR(h_path.dentry);
35074+ else {
5527c038
JR
35075+ if (d_is_reg(h_path.dentry))
35076+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 35077+ dput(h_path.dentry);
35078+ }
35079+
35080+ return err;
35081+}
35082+
35083+/* ---------------------------------------------------------------------- */
35084+/*
35085+ * initialize/clean whiteout for a branch
35086+ */
35087+
35088+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35089+ const int isdir)
35090+{
35091+ int err;
523b37e3 35092+ struct inode *delegated;
1facf9fc 35093+
5527c038 35094+ if (d_is_negative(whpath->dentry))
1facf9fc 35095+ return;
35096+
86dc4139
AM
35097+ if (isdir)
35098+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
35099+ else {
35100+ delegated = NULL;
35101+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35102+ if (unlikely(err == -EWOULDBLOCK)) {
35103+ pr_warn("cannot retry for NFSv4 delegation"
35104+ " for an internal unlink\n");
35105+ iput(delegated);
35106+ }
35107+ }
1facf9fc 35108+ if (unlikely(err))
523b37e3
AM
35109+ pr_warn("failed removing %pd (%d), ignored.\n",
35110+ whpath->dentry, err);
1facf9fc 35111+}
35112+
35113+static int test_linkable(struct dentry *h_root)
35114+{
5527c038 35115+ struct inode *h_dir = d_inode(h_root);
1facf9fc 35116+
35117+ if (h_dir->i_op->link)
35118+ return 0;
35119+
523b37e3
AM
35120+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35121+ h_root, au_sbtype(h_root->d_sb));
43982f53 35122+ return -ENOSYS; /* the branch doesn't have its ->link() */
1facf9fc 35123+}
35124+
35125+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35126+static int au_whdir(struct inode *h_dir, struct path *path)
35127+{
35128+ int err;
35129+
35130+ err = -EEXIST;
5527c038 35131+ if (d_is_negative(path->dentry)) {
cd7a4cd9 35132+ int mode = 0700;
1facf9fc 35133+
35134+ if (au_test_nfs(path->dentry->d_sb))
cd7a4cd9 35135+ mode |= 0111;
86dc4139 35136+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 35137+ } else if (d_is_dir(path->dentry))
1facf9fc 35138+ err = 0;
35139+ else
523b37e3 35140+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 35141+
35142+ return err;
35143+}
35144+
35145+struct au_wh_base {
35146+ const struct qstr *name;
35147+ struct dentry *dentry;
35148+};
35149+
35150+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35151+ struct path *h_path)
35152+{
35153+ h_path->dentry = base[AuBrWh_BASE].dentry;
35154+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35155+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35156+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35157+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35158+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35159+}
35160+
35161+/*
35162+ * returns tri-state,
c1595e42 35163+ * minus: error, caller should print the message
acd2b654 35164+ * zero: success
c1595e42 35165+ * plus: error, caller should NOT print the message
1facf9fc 35166+ */
35167+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35168+ int do_plink, struct au_wh_base base[],
35169+ struct path *h_path)
35170+{
35171+ int err;
35172+ struct inode *h_dir;
35173+
5527c038 35174+ h_dir = d_inode(h_root);
1facf9fc 35175+ h_path->dentry = base[AuBrWh_BASE].dentry;
35176+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35177+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35178+ if (do_plink) {
35179+ err = test_linkable(h_root);
35180+ if (unlikely(err)) {
35181+ err = 1;
35182+ goto out;
35183+ }
35184+
35185+ err = au_whdir(h_dir, h_path);
35186+ if (unlikely(err))
35187+ goto out;
35188+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35189+ } else
35190+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35191+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35192+ err = au_whdir(h_dir, h_path);
35193+ if (unlikely(err))
35194+ goto out;
35195+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35196+
4f0767ce 35197+out:
1facf9fc 35198+ return err;
35199+}
35200+
35201+/*
35202+ * for the moment, aufs supports the branch filesystem which does not support
35203+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35204+ * copyup failed. finally, such filesystem will not be used as the writable
35205+ * branch.
35206+ *
35207+ * returns tri-state, see above.
35208+ */
35209+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35210+ int do_plink, struct au_wh_base base[],
35211+ struct path *h_path)
35212+{
35213+ int err;
35214+ struct inode *h_dir;
35215+
1308ab2a 35216+ WbrWhMustWriteLock(wbr);
35217+
1facf9fc 35218+ err = test_linkable(h_root);
35219+ if (unlikely(err)) {
35220+ err = 1;
35221+ goto out;
35222+ }
35223+
35224+ /*
35225+ * todo: should this create be done in /sbin/mount.aufs helper?
35226+ */
35227+ err = -EEXIST;
5527c038
JR
35228+ h_dir = d_inode(h_root);
35229+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
35230+ h_path->dentry = base[AuBrWh_BASE].dentry;
35231+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 35232+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 35233+ err = 0;
35234+ else
523b37e3 35235+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 35236+ if (unlikely(err))
35237+ goto out;
35238+
35239+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35240+ if (do_plink) {
35241+ err = au_whdir(h_dir, h_path);
35242+ if (unlikely(err))
35243+ goto out;
35244+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35245+ } else
35246+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35247+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35248+
35249+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35250+ err = au_whdir(h_dir, h_path);
35251+ if (unlikely(err))
35252+ goto out;
35253+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35254+
4f0767ce 35255+out:
1facf9fc 35256+ return err;
35257+}
35258+
35259+/*
35260+ * initialize the whiteout base file/dir for @br.
35261+ */
86dc4139 35262+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 35263+{
35264+ int err, i;
35265+ const unsigned char do_plink
35266+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 35267+ struct inode *h_dir;
86dc4139
AM
35268+ struct path path = br->br_path;
35269+ struct dentry *h_root = path.dentry;
1facf9fc 35270+ struct au_wbr *wbr = br->br_wbr;
35271+ static const struct qstr base_name[] = {
0c3ec466
AM
35272+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35273+ sizeof(AUFS_BASE_NAME) - 1),
35274+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35275+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35276+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35277+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 35278+ };
35279+ struct au_wh_base base[] = {
35280+ [AuBrWh_BASE] = {
35281+ .name = base_name + AuBrWh_BASE,
35282+ .dentry = NULL
35283+ },
35284+ [AuBrWh_PLINK] = {
35285+ .name = base_name + AuBrWh_PLINK,
35286+ .dentry = NULL
35287+ },
35288+ [AuBrWh_ORPH] = {
35289+ .name = base_name + AuBrWh_ORPH,
35290+ .dentry = NULL
35291+ }
35292+ };
35293+
1308ab2a 35294+ if (wbr)
35295+ WbrWhMustWriteLock(wbr);
1facf9fc 35296+
1facf9fc 35297+ for (i = 0; i < AuBrWh_Last; i++) {
35298+ /* doubly whiteouted */
35299+ struct dentry *d;
35300+
35301+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35302+ err = PTR_ERR(d);
35303+ if (IS_ERR(d))
35304+ goto out;
35305+
35306+ base[i].dentry = d;
35307+ AuDebugOn(wbr
35308+ && wbr->wbr_wh[i]
35309+ && wbr->wbr_wh[i] != base[i].dentry);
35310+ }
35311+
35312+ if (wbr)
35313+ for (i = 0; i < AuBrWh_Last; i++) {
35314+ dput(wbr->wbr_wh[i]);
35315+ wbr->wbr_wh[i] = NULL;
35316+ }
35317+
35318+ err = 0;
1e00d052 35319+ if (!au_br_writable(br->br_perm)) {
5527c038 35320+ h_dir = d_inode(h_root);
1facf9fc 35321+ au_wh_init_ro(h_dir, base, &path);
1e00d052 35322+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 35323+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35324+ if (err > 0)
35325+ goto out;
35326+ else if (err)
35327+ goto out_err;
1e00d052 35328+ } else {
1facf9fc 35329+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35330+ if (err > 0)
35331+ goto out;
35332+ else if (err)
35333+ goto out_err;
1facf9fc 35334+ }
35335+ goto out; /* success */
35336+
4f0767ce 35337+out_err:
523b37e3
AM
35338+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35339+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35340+out:
1facf9fc 35341+ for (i = 0; i < AuBrWh_Last; i++)
35342+ dput(base[i].dentry);
35343+ return err;
35344+}
35345+
35346+/* ---------------------------------------------------------------------- */
35347+/*
35348+ * whiteouts are all hard-linked usually.
35349+ * when its link count reaches a ceiling, we create a new whiteout base
35350+ * asynchronously.
35351+ */
35352+
35353+struct reinit_br_wh {
35354+ struct super_block *sb;
35355+ struct au_branch *br;
35356+};
35357+
35358+static void reinit_br_wh(void *arg)
35359+{
35360+ int err;
35361+ aufs_bindex_t bindex;
35362+ struct path h_path;
35363+ struct reinit_br_wh *a = arg;
35364+ struct au_wbr *wbr;
523b37e3 35365+ struct inode *dir, *delegated;
1facf9fc 35366+ struct dentry *h_root;
35367+ struct au_hinode *hdir;
35368+
35369+ err = 0;
35370+ wbr = a->br->br_wbr;
35371+ /* big aufs lock */
35372+ si_noflush_write_lock(a->sb);
35373+ if (!au_br_writable(a->br->br_perm))
35374+ goto out;
35375+ bindex = au_br_index(a->sb, a->br->br_id);
35376+ if (unlikely(bindex < 0))
35377+ goto out;
35378+
1308ab2a 35379+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35380+ dir = d_inode(a->sb->s_root);
1facf9fc 35381+ hdir = au_hi(dir, bindex);
35382+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35383+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35384+
5afbbe0d 35385+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35386+ wbr_wh_write_lock(wbr);
35387+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35388+ h_root, a->br);
35389+ if (!err) {
86dc4139
AM
35390+ h_path.dentry = wbr->wbr_whbase;
35391+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35392+ delegated = NULL;
35393+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35394+ /*force*/0);
35395+ if (unlikely(err == -EWOULDBLOCK)) {
35396+ pr_warn("cannot retry for NFSv4 delegation"
35397+ " for an internal unlink\n");
35398+ iput(delegated);
35399+ }
1facf9fc 35400+ } else {
523b37e3 35401+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35402+ err = 0;
35403+ }
35404+ dput(wbr->wbr_whbase);
35405+ wbr->wbr_whbase = NULL;
35406+ if (!err)
86dc4139 35407+ err = au_wh_init(a->br, a->sb);
1facf9fc 35408+ wbr_wh_write_unlock(wbr);
5afbbe0d 35409+ au_hn_inode_unlock(hdir);
1308ab2a 35410+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35411+ if (!err)
35412+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35413+
4f0767ce 35414+out:
1facf9fc 35415+ if (wbr)
35416+ atomic_dec(&wbr->wbr_wh_running);
acd2b654 35417+ au_lcnt_dec(&a->br->br_count);
1facf9fc 35418+ si_write_unlock(a->sb);
027c5e7a 35419+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
9f237c51 35420+ au_kfree_rcu(a);
1facf9fc 35421+ if (unlikely(err))
35422+ AuIOErr("err %d\n", err);
35423+}
35424+
35425+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35426+{
35427+ int do_dec, wkq_err;
35428+ struct reinit_br_wh *arg;
35429+
35430+ do_dec = 1;
35431+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35432+ goto out;
35433+
35434+ /* ignore ENOMEM */
35435+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35436+ if (arg) {
35437+ /*
35438+ * dec(wh_running), kfree(arg) and dec(br_count)
35439+ * in reinit function
35440+ */
35441+ arg->sb = sb;
35442+ arg->br = br;
acd2b654 35443+ au_lcnt_inc(&br->br_count);
53392da6 35444+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35445+ if (unlikely(wkq_err)) {
35446+ atomic_dec(&br->br_wbr->wbr_wh_running);
acd2b654 35447+ au_lcnt_dec(&br->br_count);
9f237c51 35448+ au_kfree_rcu(arg);
1facf9fc 35449+ }
35450+ do_dec = 0;
35451+ }
35452+
4f0767ce 35453+out:
1facf9fc 35454+ if (do_dec)
35455+ atomic_dec(&br->br_wbr->wbr_wh_running);
35456+}
35457+
35458+/* ---------------------------------------------------------------------- */
35459+
35460+/*
35461+ * create the whiteout @wh.
35462+ */
35463+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35464+ struct dentry *wh)
35465+{
35466+ int err;
35467+ struct path h_path = {
35468+ .dentry = wh
35469+ };
35470+ struct au_branch *br;
35471+ struct au_wbr *wbr;
35472+ struct dentry *h_parent;
523b37e3 35473+ struct inode *h_dir, *delegated;
1facf9fc 35474+
35475+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35476+ h_dir = d_inode(h_parent);
1facf9fc 35477+ IMustLock(h_dir);
35478+
35479+ br = au_sbr(sb, bindex);
86dc4139 35480+ h_path.mnt = au_br_mnt(br);
1facf9fc 35481+ wbr = br->br_wbr;
35482+ wbr_wh_read_lock(wbr);
35483+ if (wbr->wbr_whbase) {
523b37e3
AM
35484+ delegated = NULL;
35485+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35486+ if (unlikely(err == -EWOULDBLOCK)) {
35487+ pr_warn("cannot retry for NFSv4 delegation"
35488+ " for an internal link\n");
35489+ iput(delegated);
35490+ }
1facf9fc 35491+ if (!err || err != -EMLINK)
35492+ goto out;
35493+
35494+ /* link count full. re-initialize br_whbase. */
35495+ kick_reinit_br_wh(sb, br);
35496+ }
35497+
35498+ /* return this error in this context */
b4510431 35499+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35500+ if (!err)
35501+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35502+
4f0767ce 35503+out:
1facf9fc 35504+ wbr_wh_read_unlock(wbr);
35505+ return err;
35506+}
35507+
35508+/* ---------------------------------------------------------------------- */
35509+
35510+/*
35511+ * create or remove the diropq.
35512+ */
35513+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35514+ unsigned int flags)
35515+{
35516+ struct dentry *opq_dentry, *h_dentry;
35517+ struct super_block *sb;
35518+ struct au_branch *br;
35519+ int err;
35520+
35521+ sb = dentry->d_sb;
35522+ br = au_sbr(sb, bindex);
35523+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35524+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35525+ if (IS_ERR(opq_dentry))
35526+ goto out;
35527+
35528+ if (au_ftest_diropq(flags, CREATE)) {
35529+ err = link_or_create_wh(sb, bindex, opq_dentry);
35530+ if (!err) {
35531+ au_set_dbdiropq(dentry, bindex);
35532+ goto out; /* success */
35533+ }
35534+ } else {
35535+ struct path tmp = {
35536+ .dentry = opq_dentry,
86dc4139 35537+ .mnt = au_br_mnt(br)
1facf9fc 35538+ };
5527c038 35539+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35540+ if (!err)
35541+ au_set_dbdiropq(dentry, -1);
35542+ }
35543+ dput(opq_dentry);
35544+ opq_dentry = ERR_PTR(err);
35545+
4f0767ce 35546+out:
1facf9fc 35547+ return opq_dentry;
35548+}
35549+
35550+struct do_diropq_args {
35551+ struct dentry **errp;
35552+ struct dentry *dentry;
35553+ aufs_bindex_t bindex;
35554+ unsigned int flags;
35555+};
35556+
35557+static void call_do_diropq(void *args)
35558+{
35559+ struct do_diropq_args *a = args;
35560+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35561+}
35562+
35563+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35564+ unsigned int flags)
35565+{
35566+ struct dentry *diropq, *h_dentry;
35567+
35568+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 35569+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 35570+ diropq = do_diropq(dentry, bindex, flags);
35571+ else {
35572+ int wkq_err;
35573+ struct do_diropq_args args = {
35574+ .errp = &diropq,
35575+ .dentry = dentry,
35576+ .bindex = bindex,
35577+ .flags = flags
35578+ };
35579+
35580+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35581+ if (unlikely(wkq_err))
35582+ diropq = ERR_PTR(wkq_err);
35583+ }
35584+
35585+ return diropq;
35586+}
35587+
35588+/* ---------------------------------------------------------------------- */
35589+
35590+/*
35591+ * lookup whiteout dentry.
35592+ * @h_parent: lower parent dentry which must exist and be locked
35593+ * @base_name: name of dentry which will be whiteouted
35594+ * returns dentry for whiteout.
35595+ */
35596+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35597+ struct au_branch *br)
35598+{
35599+ int err;
35600+ struct qstr wh_name;
35601+ struct dentry *wh_dentry;
35602+
35603+ err = au_wh_name_alloc(&wh_name, base_name);
35604+ wh_dentry = ERR_PTR(err);
35605+ if (!err) {
b4510431 35606+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
9f237c51 35607+ au_kfree_try_rcu(wh_name.name);
1facf9fc 35608+ }
35609+ return wh_dentry;
35610+}
35611+
35612+/*
35613+ * link/create a whiteout for @dentry on @bindex.
35614+ */
35615+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35616+ struct dentry *h_parent)
35617+{
35618+ struct dentry *wh_dentry;
35619+ struct super_block *sb;
35620+ int err;
35621+
35622+ sb = dentry->d_sb;
35623+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35624+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35625+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35626+ if (!err) {
1facf9fc 35627+ au_set_dbwh(dentry, bindex);
076b876e
AM
35628+ au_fhsm_wrote(sb, bindex, /*force*/0);
35629+ } else {
1facf9fc 35630+ dput(wh_dentry);
35631+ wh_dentry = ERR_PTR(err);
35632+ }
35633+ }
35634+
35635+ return wh_dentry;
35636+}
35637+
35638+/* ---------------------------------------------------------------------- */
35639+
35640+/* Delete all whiteouts in this directory on branch bindex. */
35641+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35642+ aufs_bindex_t bindex, struct au_branch *br)
35643+{
35644+ int err;
35645+ unsigned long ul, n;
35646+ struct qstr wh_name;
35647+ char *p;
35648+ struct hlist_head *head;
c06a8ce3 35649+ struct au_vdir_wh *pos;
1facf9fc 35650+ struct au_vdir_destr *str;
35651+
35652+ err = -ENOMEM;
537831f9 35653+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35654+ wh_name.name = p;
35655+ if (unlikely(!wh_name.name))
35656+ goto out;
35657+
35658+ err = 0;
35659+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35660+ p += AUFS_WH_PFX_LEN;
35661+ n = whlist->nh_num;
35662+ head = whlist->nh_head;
35663+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35664+ hlist_for_each_entry(pos, head, wh_hash) {
35665+ if (pos->wh_bindex != bindex)
1facf9fc 35666+ continue;
35667+
c06a8ce3 35668+ str = &pos->wh_str;
1facf9fc 35669+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35670+ memcpy(p, str->name, str->len);
35671+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35672+ err = unlink_wh_name(h_dentry, &wh_name, br);
35673+ if (!err)
35674+ continue;
35675+ break;
35676+ }
35677+ AuIOErr("whiteout name too long %.*s\n",
35678+ str->len, str->name);
35679+ err = -EIO;
35680+ break;
35681+ }
35682+ }
1c60b727 35683+ free_page((unsigned long)wh_name.name);
1facf9fc 35684+
4f0767ce 35685+out:
1facf9fc 35686+ return err;
35687+}
35688+
35689+struct del_wh_children_args {
35690+ int *errp;
35691+ struct dentry *h_dentry;
1308ab2a 35692+ struct au_nhash *whlist;
1facf9fc 35693+ aufs_bindex_t bindex;
35694+ struct au_branch *br;
35695+};
35696+
35697+static void call_del_wh_children(void *args)
35698+{
35699+ struct del_wh_children_args *a = args;
1308ab2a 35700+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35701+}
35702+
35703+/* ---------------------------------------------------------------------- */
35704+
35705+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35706+{
35707+ struct au_whtmp_rmdir *whtmp;
dece6358 35708+ int err;
1308ab2a 35709+ unsigned int rdhash;
dece6358
AM
35710+
35711+ SiMustAnyLock(sb);
1facf9fc 35712+
be52b249 35713+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35714+ if (unlikely(!whtmp)) {
35715+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35716+ goto out;
dece6358 35717+ }
1facf9fc 35718+
1308ab2a 35719+ /* no estimation for dir size */
35720+ rdhash = au_sbi(sb)->si_rdhash;
35721+ if (!rdhash)
35722+ rdhash = AUFS_RDHASH_DEF;
35723+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35724+ if (unlikely(err)) {
9f237c51 35725+ au_kfree_rcu(whtmp);
1308ab2a 35726+ whtmp = ERR_PTR(err);
35727+ }
dece6358 35728+
4f0767ce 35729+out:
dece6358 35730+ return whtmp;
1facf9fc 35731+}
35732+
35733+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35734+{
027c5e7a 35735+ if (whtmp->br)
acd2b654 35736+ au_lcnt_dec(&whtmp->br->br_count);
1facf9fc 35737+ dput(whtmp->wh_dentry);
35738+ iput(whtmp->dir);
dece6358 35739+ au_nhash_wh_free(&whtmp->whlist);
9f237c51 35740+ au_kfree_rcu(whtmp);
1facf9fc 35741+}
35742+
35743+/*
35744+ * rmdir the whiteouted temporary named dir @h_dentry.
35745+ * @whlist: whiteouted children.
35746+ */
35747+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35748+ struct dentry *wh_dentry, struct au_nhash *whlist)
35749+{
35750+ int err;
2000de60 35751+ unsigned int h_nlink;
1facf9fc 35752+ struct path h_tmp;
35753+ struct inode *wh_inode, *h_dir;
35754+ struct au_branch *br;
35755+
5527c038 35756+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35757+ IMustLock(h_dir);
35758+
35759+ br = au_sbr(dir->i_sb, bindex);
5527c038 35760+ wh_inode = d_inode(wh_dentry);
febd17d6 35761+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35762+
35763+ /*
35764+ * someone else might change some whiteouts while we were sleeping.
35765+ * it means this whlist may have an obsoleted entry.
35766+ */
35767+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35768+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35769+ else {
35770+ int wkq_err;
35771+ struct del_wh_children_args args = {
35772+ .errp = &err,
35773+ .h_dentry = wh_dentry,
1308ab2a 35774+ .whlist = whlist,
1facf9fc 35775+ .bindex = bindex,
35776+ .br = br
35777+ };
35778+
35779+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35780+ if (unlikely(wkq_err))
35781+ err = wkq_err;
35782+ }
febd17d6 35783+ inode_unlock(wh_inode);
1facf9fc 35784+
35785+ if (!err) {
35786+ h_tmp.dentry = wh_dentry;
86dc4139 35787+ h_tmp.mnt = au_br_mnt(br);
2000de60 35788+ h_nlink = h_dir->i_nlink;
1facf9fc 35789+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35790+ /* some fs doesn't change the parent nlink in some cases */
35791+ h_nlink -= h_dir->i_nlink;
1facf9fc 35792+ }
35793+
35794+ if (!err) {
5afbbe0d 35795+ if (au_ibtop(dir) == bindex) {
7f207e10 35796+ /* todo: dir->i_mutex is necessary */
1facf9fc 35797+ au_cpup_attr_timesizes(dir);
2000de60
JR
35798+ if (h_nlink)
35799+ vfsub_drop_nlink(dir);
1facf9fc 35800+ }
35801+ return 0; /* success */
35802+ }
35803+
523b37e3 35804+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35805+ return err;
35806+}
35807+
35808+static void call_rmdir_whtmp(void *args)
35809+{
35810+ int err;
e49829fe 35811+ aufs_bindex_t bindex;
1facf9fc 35812+ struct au_whtmp_rmdir *a = args;
35813+ struct super_block *sb;
35814+ struct dentry *h_parent;
35815+ struct inode *h_dir;
1facf9fc 35816+ struct au_hinode *hdir;
35817+
35818+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35819+ /* inode_lock(a->dir); */
e49829fe 35820+ err = -EROFS;
1facf9fc 35821+ sb = a->dir->i_sb;
e49829fe
JR
35822+ si_read_lock(sb, !AuLock_FLUSH);
35823+ if (!au_br_writable(a->br->br_perm))
35824+ goto out;
35825+ bindex = au_br_index(sb, a->br->br_id);
35826+ if (unlikely(bindex < 0))
1facf9fc 35827+ goto out;
35828+
35829+ err = -EIO;
1facf9fc 35830+ ii_write_lock_parent(a->dir);
35831+ h_parent = dget_parent(a->wh_dentry);
5527c038 35832+ h_dir = d_inode(h_parent);
e49829fe 35833+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35834+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35835+ if (unlikely(err))
35836+ goto out_mnt;
5afbbe0d 35837+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35838+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35839+ a->br);
86dc4139
AM
35840+ if (!err)
35841+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35842+ au_hn_inode_unlock(hdir);
86dc4139
AM
35843+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35844+
35845+out_mnt:
1facf9fc 35846+ dput(h_parent);
35847+ ii_write_unlock(a->dir);
4f0767ce 35848+out:
febd17d6 35849+ /* inode_unlock(a->dir); */
1facf9fc 35850+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35851+ si_read_unlock(sb);
35852+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35853+ if (unlikely(err))
35854+ AuIOErr("err %d\n", err);
35855+}
35856+
35857+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35858+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35859+{
35860+ int wkq_err;
e49829fe 35861+ struct super_block *sb;
1facf9fc 35862+
35863+ IMustLock(dir);
35864+
35865+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35866+ sb = dir->i_sb;
1facf9fc 35867+ args->dir = au_igrab(dir);
e49829fe 35868+ args->br = au_sbr(sb, bindex);
acd2b654 35869+ au_lcnt_inc(&args->br->br_count);
1facf9fc 35870+ args->wh_dentry = dget(wh_dentry);
53392da6 35871+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35872+ if (unlikely(wkq_err)) {
523b37e3 35873+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35874+ au_whtmp_rmdir_free(args);
35875+ }
35876+}
7f207e10 35877diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
eca34b5c 35878--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
016522bc 35879+++ linux/fs/aufs/whout.h 2020-01-27 10:57:18.178871751 +0100
062440b3
AM
35880@@ -0,0 +1,86 @@
35881+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 35882+/*
016522bc 35883+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 35884+ *
35885+ * This program, aufs is free software; you can redistribute it and/or modify
35886+ * it under the terms of the GNU General Public License as published by
35887+ * the Free Software Foundation; either version 2 of the License, or
35888+ * (at your option) any later version.
dece6358
AM
35889+ *
35890+ * This program is distributed in the hope that it will be useful,
35891+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35892+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35893+ * GNU General Public License for more details.
35894+ *
35895+ * You should have received a copy of the GNU General Public License
523b37e3 35896+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35897+ */
35898+
35899+/*
35900+ * whiteout for logical deletion and opaque directory
35901+ */
35902+
35903+#ifndef __AUFS_WHOUT_H__
35904+#define __AUFS_WHOUT_H__
35905+
35906+#ifdef __KERNEL__
35907+
1facf9fc 35908+#include "dir.h"
35909+
35910+/* whout.c */
35911+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
35912+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35913+int au_diropq_test(struct dentry *h_dentry);
3c1bdaff 35914+struct au_branch;
1facf9fc 35915+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35916+ struct qstr *prefix);
35917+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35918+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35919+ struct dentry *dentry);
86dc4139 35920+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 35921+
35922+/* diropq flags */
35923+#define AuDiropq_CREATE 1
35924+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
35925+#define au_fset_diropq(flags, name) \
35926+ do { (flags) |= AuDiropq_##name; } while (0)
35927+#define au_fclr_diropq(flags, name) \
35928+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 35929+
35930+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35931+ unsigned int flags);
35932+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35933+ struct au_branch *br);
35934+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35935+ struct dentry *h_parent);
35936+
35937+/* real rmdir for the whiteout-ed dir */
35938+struct au_whtmp_rmdir {
35939+ struct inode *dir;
e49829fe 35940+ struct au_branch *br;
1facf9fc 35941+ struct dentry *wh_dentry;
dece6358 35942+ struct au_nhash whlist;
1facf9fc 35943+};
35944+
35945+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35946+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35947+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35948+ struct dentry *wh_dentry, struct au_nhash *whlist);
35949+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35950+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35951+
35952+/* ---------------------------------------------------------------------- */
35953+
35954+static inline struct dentry *au_diropq_create(struct dentry *dentry,
35955+ aufs_bindex_t bindex)
35956+{
35957+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35958+}
35959+
35960+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35961+{
35962+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35963+}
35964+
35965+#endif /* __KERNEL__ */
35966+#endif /* __AUFS_WHOUT_H__ */
7f207e10 35967diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
eca34b5c 35968--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
016522bc 35969+++ linux/fs/aufs/wkq.c 2020-04-03 08:16:47.547461775 +0200
43982f53 35970@@ -0,0 +1,372 @@
cd7a4cd9 35971+// SPDX-License-Identifier: GPL-2.0
1facf9fc 35972+/*
016522bc 35973+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 35974+ *
35975+ * This program, aufs is free software; you can redistribute it and/or modify
35976+ * it under the terms of the GNU General Public License as published by
35977+ * the Free Software Foundation; either version 2 of the License, or
35978+ * (at your option) any later version.
dece6358
AM
35979+ *
35980+ * This program is distributed in the hope that it will be useful,
35981+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35982+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35983+ * GNU General Public License for more details.
35984+ *
35985+ * You should have received a copy of the GNU General Public License
523b37e3 35986+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35987+ */
35988+
35989+/*
35990+ * workqueue for asynchronous/super-io operations
acd2b654 35991+ * todo: try new credential scheme
1facf9fc 35992+ */
35993+
dece6358 35994+#include <linux/module.h>
1facf9fc 35995+#include "aufs.h"
35996+
9dbd164d 35997+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 35998+
9dbd164d 35999+static struct workqueue_struct *au_wkq;
1facf9fc 36000+
36001+struct au_wkinfo {
36002+ struct work_struct wk;
7f207e10 36003+ struct kobject *kobj;
1facf9fc 36004+
36005+ unsigned int flags; /* see wkq.h */
36006+
36007+ au_wkq_func_t func;
36008+ void *args;
36009+
8b6a4947
AM
36010+#ifdef CONFIG_LOCKDEP
36011+ int dont_check;
36012+ struct held_lock **hlock;
36013+#endif
36014+
1facf9fc 36015+ struct completion *comp;
36016+};
36017+
36018+/* ---------------------------------------------------------------------- */
8b6a4947
AM
36019+/*
36020+ * Aufs passes some operations to the workqueue such as the internal copyup.
36021+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36022+ * job run by workqueue depends upon the locks acquired in the other task.
36023+ * Delegating a small operation to the workqueue, aufs passes its lockdep
36024+ * information too. And the job in the workqueue restores the info in order to
36025+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
36026+ * correctly and expectedly.
36027+ */
36028+
36029+#ifndef CONFIG_LOCKDEP
36030+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36031+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36032+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36033+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36034+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36035+#else
36036+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36037+{
36038+ wkinfo->hlock = NULL;
36039+ wkinfo->dont_check = 0;
36040+}
36041+
36042+/*
36043+ * 1: matched
36044+ * 0: unmatched
36045+ */
36046+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36047+{
36048+ static DEFINE_SPINLOCK(spin);
36049+ static struct {
36050+ char *name;
36051+ struct lock_class_key *key;
36052+ } a[] = {
36053+ { .name = "&sbinfo->si_rwsem" },
36054+ { .name = "&finfo->fi_rwsem" },
36055+ { .name = "&dinfo->di_rwsem" },
36056+ { .name = "&iinfo->ii_rwsem" }
36057+ };
36058+ static int set;
36059+ int i;
36060+
36061+ /* lockless read from 'set.' see below */
36062+ if (set == ARRAY_SIZE(a)) {
36063+ for (i = 0; i < ARRAY_SIZE(a); i++)
36064+ if (a[i].key == key)
36065+ goto match;
36066+ goto unmatch;
36067+ }
36068+
36069+ spin_lock(&spin);
36070+ if (set)
36071+ for (i = 0; i < ARRAY_SIZE(a); i++)
36072+ if (a[i].key == key) {
36073+ spin_unlock(&spin);
36074+ goto match;
36075+ }
36076+ for (i = 0; i < ARRAY_SIZE(a); i++) {
36077+ if (a[i].key) {
36078+ if (unlikely(a[i].key == key)) { /* rare but possible */
36079+ spin_unlock(&spin);
36080+ goto match;
36081+ } else
36082+ continue;
36083+ }
36084+ if (strstr(a[i].name, name)) {
36085+ /*
36086+ * the order of these three lines is important for the
36087+ * lockless read above.
36088+ */
36089+ a[i].key = key;
36090+ spin_unlock(&spin);
36091+ set++;
36092+ /* AuDbg("%d, %s\n", set, name); */
36093+ goto match;
36094+ }
36095+ }
36096+ spin_unlock(&spin);
36097+ goto unmatch;
36098+
36099+match:
36100+ return 1;
36101+unmatch:
36102+ return 0;
36103+}
36104+
36105+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36106+{
36107+ int err, n;
36108+ struct task_struct *curr;
36109+ struct held_lock **hl, *held_locks, *p;
36110+
36111+ err = 0;
36112+ curr = current;
36113+ wkinfo->dont_check = lockdep_recursing(curr);
36114+ if (wkinfo->dont_check)
36115+ goto out;
36116+ n = curr->lockdep_depth;
36117+ if (!n)
36118+ goto out;
36119+
36120+ err = -ENOMEM;
36121+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36122+ if (unlikely(!wkinfo->hlock))
36123+ goto out;
36124+
36125+ err = 0;
43982f53
AM
36126+#if 0 /* left for debugging */
36127+ if (0 && au_debug_test())
8b6a4947
AM
36128+ lockdep_print_held_locks(curr);
36129+#endif
36130+ held_locks = curr->held_locks;
36131+ hl = wkinfo->hlock;
36132+ while (n--) {
36133+ p = held_locks++;
36134+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36135+ *hl++ = p;
36136+ }
36137+ *hl = NULL;
36138+
36139+out:
36140+ return err;
36141+}
36142+
36143+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36144+{
9f237c51 36145+ au_kfree_try_rcu(wkinfo->hlock);
8b6a4947
AM
36146+}
36147+
36148+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36149+{
36150+ struct held_lock *p, **hl = wkinfo->hlock;
36151+ int subclass;
36152+
36153+ if (wkinfo->dont_check)
36154+ lockdep_off();
36155+ if (!hl)
36156+ return;
36157+ while ((p = *hl++)) { /* assignment */
36158+ subclass = lockdep_hlock_class(p)->subclass;
36159+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36160+ if (p->read)
36161+ rwsem_acquire_read(p->instance, subclass, 0,
36162+ /*p->acquire_ip*/_RET_IP_);
36163+ else
36164+ rwsem_acquire(p->instance, subclass, 0,
36165+ /*p->acquire_ip*/_RET_IP_);
36166+ }
36167+}
36168+
36169+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36170+{
36171+ struct held_lock *p, **hl = wkinfo->hlock;
36172+
36173+ if (wkinfo->dont_check)
36174+ lockdep_on();
36175+ if (!hl)
36176+ return;
36177+ while ((p = *hl++)) /* assignment */
36178+ rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
36179+}
36180+#endif
1facf9fc 36181+
1facf9fc 36182+static void wkq_func(struct work_struct *wk)
36183+{
36184+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36185+
2dfbb274 36186+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
36187+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36188+
8b6a4947 36189+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 36190+ wkinfo->func(wkinfo->args);
8b6a4947 36191+ au_wkq_lockdep_post(wkinfo);
1facf9fc 36192+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36193+ complete(wkinfo->comp);
36194+ else {
7f207e10 36195+ kobject_put(wkinfo->kobj);
9dbd164d 36196+ module_put(THIS_MODULE); /* todo: ?? */
9f237c51 36197+ au_kfree_rcu(wkinfo);
1facf9fc 36198+ }
36199+}
36200+
36201+/*
36202+ * Since struct completion is large, try allocating it dynamically.
36203+ */
1facf9fc 36204+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36205+
36206+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36207+{
36208+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36209+ if (*comp) {
36210+ init_completion(*comp);
36211+ wkinfo->comp = *comp;
36212+ return 0;
36213+ }
36214+ return -ENOMEM;
36215+}
36216+
36217+static void au_wkq_comp_free(struct completion *comp)
36218+{
9f237c51 36219+ au_kfree_rcu(comp);
1facf9fc 36220+}
36221+
53392da6 36222+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 36223+{
53392da6
AM
36224+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36225+ if (au_wkq_test()) {
38d290e6 36226+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
acd2b654
AM
36227+ " due to a dead dir by UDBA,"
36228+ " or async xino write?\n");
53392da6
AM
36229+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36230+ }
36231+ } else
36232+ au_dbg_verify_kthread();
36233+
36234+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 36235+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 36236+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
36237+ } else {
36238+ INIT_WORK(&wkinfo->wk, wkq_func);
36239+ schedule_work(&wkinfo->wk);
36240+ }
1facf9fc 36241+}
36242+
7f207e10
AM
36243+/*
36244+ * Be careful. It is easy to make deadlock happen.
36245+ * processA: lock, wkq and wait
36246+ * processB: wkq and wait, lock in wkq
36247+ * --> deadlock
36248+ */
b752ccd1 36249+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 36250+{
36251+ int err;
36252+ AuWkqCompDeclare(comp);
36253+ struct au_wkinfo wkinfo = {
b752ccd1 36254+ .flags = flags,
1facf9fc 36255+ .func = func,
36256+ .args = args
36257+ };
36258+
36259+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
36260+ if (unlikely(err))
36261+ goto out;
36262+ err = au_wkq_lockdep_alloc(&wkinfo);
36263+ if (unlikely(err))
36264+ goto out_comp;
1facf9fc 36265+ if (!err) {
53392da6 36266+ au_wkq_run(&wkinfo);
1facf9fc 36267+ /* no timeout, no interrupt */
36268+ wait_for_completion(wkinfo.comp);
1facf9fc 36269+ }
8b6a4947 36270+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 36271+
8b6a4947
AM
36272+out_comp:
36273+ au_wkq_comp_free(comp);
36274+out:
36275+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 36276+ return err;
1facf9fc 36277+}
36278+
027c5e7a
AM
36279+/*
36280+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36281+ * problem in a concurrent umounting.
36282+ */
53392da6
AM
36283+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36284+ unsigned int flags)
1facf9fc 36285+{
36286+ int err;
36287+ struct au_wkinfo *wkinfo;
36288+
f0c0a007 36289+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 36290+
36291+ /*
36292+ * wkq_func() must free this wkinfo.
36293+ * it highly depends upon the implementation of workqueue.
36294+ */
36295+ err = 0;
36296+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36297+ if (wkinfo) {
7f207e10 36298+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 36299+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 36300+ wkinfo->func = func;
36301+ wkinfo->args = args;
36302+ wkinfo->comp = NULL;
8b6a4947 36303+ au_wkq_lockdep_init(wkinfo);
7f207e10 36304+ kobject_get(wkinfo->kobj);
9dbd164d 36305+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 36306+
53392da6 36307+ au_wkq_run(wkinfo);
1facf9fc 36308+ } else {
36309+ err = -ENOMEM;
e49829fe 36310+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36311+ }
36312+
36313+ return err;
36314+}
36315+
36316+/* ---------------------------------------------------------------------- */
36317+
36318+void au_nwt_init(struct au_nowait_tasks *nwt)
36319+{
f0c0a007
AM
36320+ atomic_set(&nwt->nw_len, 0);
36321+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36322+ init_waitqueue_head(&nwt->nw_wq);
36323+}
36324+
36325+void au_wkq_fin(void)
36326+{
9dbd164d 36327+ destroy_workqueue(au_wkq);
1facf9fc 36328+}
36329+
36330+int __init au_wkq_init(void)
36331+{
9dbd164d 36332+ int err;
b752ccd1
AM
36333+
36334+ err = 0;
86dc4139 36335+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36336+ if (IS_ERR(au_wkq))
36337+ err = PTR_ERR(au_wkq);
36338+ else if (!au_wkq)
36339+ err = -ENOMEM;
b752ccd1
AM
36340+
36341+ return err;
1facf9fc 36342+}
7f207e10 36343diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
eca34b5c 36344--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
016522bc 36345+++ linux/fs/aufs/wkq.h 2020-01-27 10:57:18.182205184 +0100
acd2b654 36346@@ -0,0 +1,89 @@
062440b3 36347+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36348+/*
016522bc 36349+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 36350+ *
36351+ * This program, aufs is free software; you can redistribute it and/or modify
36352+ * it under the terms of the GNU General Public License as published by
36353+ * the Free Software Foundation; either version 2 of the License, or
36354+ * (at your option) any later version.
dece6358
AM
36355+ *
36356+ * This program is distributed in the hope that it will be useful,
36357+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36358+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36359+ * GNU General Public License for more details.
36360+ *
36361+ * You should have received a copy of the GNU General Public License
523b37e3 36362+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36363+ */
36364+
36365+/*
36366+ * workqueue for asynchronous/super-io operations
36367+ * todo: try new credentials management scheme
36368+ */
36369+
36370+#ifndef __AUFS_WKQ_H__
36371+#define __AUFS_WKQ_H__
36372+
36373+#ifdef __KERNEL__
36374+
8b6a4947 36375+#include <linux/wait.h>
5afbbe0d 36376+
dece6358
AM
36377+struct super_block;
36378+
1facf9fc 36379+/* ---------------------------------------------------------------------- */
36380+
36381+/*
36382+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36383+ */
36384+struct au_nowait_tasks {
f0c0a007 36385+ atomic_t nw_len;
1facf9fc 36386+ wait_queue_head_t nw_wq;
36387+};
36388+
36389+/* ---------------------------------------------------------------------- */
36390+
36391+typedef void (*au_wkq_func_t)(void *args);
36392+
36393+/* wkq flags */
36394+#define AuWkq_WAIT 1
9dbd164d 36395+#define AuWkq_NEST (1 << 1)
1facf9fc 36396+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36397+#define au_fset_wkq(flags, name) \
36398+ do { (flags) |= AuWkq_##name; } while (0)
36399+#define au_fclr_wkq(flags, name) \
36400+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36401+
36402+/* wkq.c */
b752ccd1 36403+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36404+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36405+ unsigned int flags);
1facf9fc 36406+void au_nwt_init(struct au_nowait_tasks *nwt);
36407+int __init au_wkq_init(void);
36408+void au_wkq_fin(void);
36409+
36410+/* ---------------------------------------------------------------------- */
36411+
53392da6
AM
36412+static inline int au_wkq_test(void)
36413+{
36414+ return current->flags & PF_WQ_WORKER;
36415+}
36416+
b752ccd1 36417+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36418+{
b752ccd1 36419+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36420+}
36421+
36422+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36423+{
f0c0a007 36424+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36425+ wake_up_all(&nwt->nw_wq);
36426+}
36427+
36428+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36429+{
f0c0a007 36430+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36431+ return 0;
36432+}
36433+
36434+#endif /* __KERNEL__ */
36435+#endif /* __AUFS_WKQ_H__ */
c1595e42 36436diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
eca34b5c 36437--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
016522bc 36438+++ linux/fs/aufs/xattr.c 2020-01-27 10:57:18.182205184 +0100
062440b3 36439@@ -0,0 +1,356 @@
cd7a4cd9 36440+// SPDX-License-Identifier: GPL-2.0
c1595e42 36441+/*
016522bc 36442+ * Copyright (C) 2014-2020 Junjiro R. Okajima
c1595e42
JR
36443+ *
36444+ * This program, aufs is free software; you can redistribute it and/or modify
36445+ * it under the terms of the GNU General Public License as published by
36446+ * the Free Software Foundation; either version 2 of the License, or
36447+ * (at your option) any later version.
36448+ *
36449+ * This program is distributed in the hope that it will be useful,
36450+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36451+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36452+ * GNU General Public License for more details.
36453+ *
36454+ * You should have received a copy of the GNU General Public License
36455+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36456+ */
36457+
36458+/*
36459+ * handling xattr functions
36460+ */
36461+
a2654f78
AM
36462+#include <linux/fs.h>
36463+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36464+#include <linux/xattr.h>
36465+#include "aufs.h"
36466+
36467+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36468+{
36469+ if (!ignore_flags)
36470+ goto out;
36471+ switch (err) {
36472+ case -ENOMEM:
36473+ case -EDQUOT:
36474+ goto out;
36475+ }
36476+
36477+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36478+ err = 0;
36479+ goto out;
36480+ }
36481+
36482+#define cmp(brattr, prefix) do { \
36483+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36484+ XATTR_##prefix##_PREFIX_LEN)) { \
36485+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36486+ err = 0; \
36487+ goto out; \
36488+ } \
36489+ } while (0)
36490+
36491+ cmp(SEC, SECURITY);
36492+ cmp(SYS, SYSTEM);
36493+ cmp(TR, TRUSTED);
36494+ cmp(USR, USER);
36495+#undef cmp
36496+
36497+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36498+ err = 0;
36499+
36500+out:
36501+ return err;
36502+}
36503+
36504+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36505+
36506+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
36507+ char *name, char **buf, unsigned int ignore_flags,
36508+ unsigned int verbose)
c1595e42
JR
36509+{
36510+ int err;
36511+ ssize_t ssz;
36512+ struct inode *h_idst;
36513+
36514+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36515+ err = ssz;
36516+ if (unlikely(err <= 0)) {
c1595e42
JR
36517+ if (err == -ENODATA
36518+ || (err == -EOPNOTSUPP
b912730e 36519+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 36520+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
36521+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36522+ || !strcmp(name,
36523+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36524+ ))
c1595e42 36525+ err = 0;
b912730e
AM
36526+ if (err && (verbose || au_debug_test()))
36527+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36528+ goto out;
36529+ }
36530+
36531+ /* unlock it temporary */
5527c038 36532+ h_idst = d_inode(h_dst);
febd17d6 36533+ inode_unlock(h_idst);
c1595e42 36534+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 36535+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36536+ if (unlikely(err)) {
7e9cd9fe
AM
36537+ if (verbose || au_debug_test())
36538+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36539+ err = au_xattr_ignore(err, name, ignore_flags);
36540+ }
36541+
36542+out:
36543+ return err;
36544+}
36545+
7e9cd9fe
AM
36546+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36547+ unsigned int verbose)
c1595e42
JR
36548+{
36549+ int err, unlocked, acl_access, acl_default;
36550+ ssize_t ssz;
36551+ struct inode *h_isrc, *h_idst;
36552+ char *value, *p, *o, *e;
36553+
36554+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36555+ /* there should not be the parent-child relationship between them */
5527c038
JR
36556+ h_isrc = d_inode(h_src);
36557+ h_idst = d_inode(h_dst);
febd17d6 36558+ inode_unlock(h_idst);
be118d29 36559+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36560+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36561+ unlocked = 0;
36562+
36563+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36564+ ssz = vfs_listxattr(h_src, NULL, 0);
36565+ err = ssz;
36566+ if (unlikely(err < 0)) {
36567+ AuTraceErr(err);
36568+ if (err == -ENODATA
36569+ || err == -EOPNOTSUPP)
36570+ err = 0; /* ignore */
36571+ goto out;
36572+ }
36573+
36574+ err = 0;
36575+ p = NULL;
36576+ o = NULL;
36577+ if (ssz) {
36578+ err = -ENOMEM;
36579+ p = kmalloc(ssz, GFP_NOFS);
36580+ o = p;
36581+ if (unlikely(!p))
36582+ goto out;
36583+ err = vfs_listxattr(h_src, p, ssz);
36584+ }
3c1bdaff 36585+ inode_unlock_shared(h_isrc);
c1595e42
JR
36586+ unlocked = 1;
36587+ AuDbg("err %d, ssz %zd\n", err, ssz);
36588+ if (unlikely(err < 0))
36589+ goto out_free;
36590+
36591+ err = 0;
36592+ e = p + ssz;
36593+ value = NULL;
36594+ acl_access = 0;
36595+ acl_default = 0;
36596+ while (!err && p < e) {
36597+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36598+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36599+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36600+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36601+ - 1);
7e9cd9fe
AM
36602+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36603+ verbose);
c1595e42
JR
36604+ p += strlen(p) + 1;
36605+ }
36606+ AuTraceErr(err);
36607+ ignore_flags |= au_xattr_out_of_list;
36608+ if (!err && !acl_access) {
36609+ err = au_do_cpup_xattr(h_dst, h_src,
36610+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36611+ ignore_flags, verbose);
c1595e42
JR
36612+ AuTraceErr(err);
36613+ }
36614+ if (!err && !acl_default) {
36615+ err = au_do_cpup_xattr(h_dst, h_src,
36616+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36617+ ignore_flags, verbose);
c1595e42
JR
36618+ AuTraceErr(err);
36619+ }
36620+
9f237c51 36621+ au_kfree_try_rcu(value);
c1595e42
JR
36622+
36623+out_free:
9f237c51 36624+ au_kfree_try_rcu(o);
c1595e42
JR
36625+out:
36626+ if (!unlocked)
3c1bdaff 36627+ inode_unlock_shared(h_isrc);
c1595e42
JR
36628+ AuTraceErr(err);
36629+ return err;
36630+}
36631+
36632+/* ---------------------------------------------------------------------- */
36633+
a2654f78
AM
36634+static int au_smack_reentering(struct super_block *sb)
36635+{
36636+#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36637+ /*
36638+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36639+ * i_op->getxattr(). ouch.
36640+ */
36641+ return si_pid_test(sb);
36642+#else
36643+ return 0;
36644+#endif
36645+}
36646+
c1595e42
JR
36647+enum {
36648+ AU_XATTR_LIST,
36649+ AU_XATTR_GET
36650+};
36651+
36652+struct au_lgxattr {
36653+ int type;
36654+ union {
36655+ struct {
36656+ char *list;
36657+ size_t size;
36658+ } list;
36659+ struct {
36660+ const char *name;
36661+ void *value;
36662+ size_t size;
36663+ } get;
36664+ } u;
36665+};
36666+
36667+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36668+{
36669+ ssize_t err;
a2654f78 36670+ int reenter;
c1595e42
JR
36671+ struct path h_path;
36672+ struct super_block *sb;
36673+
36674+ sb = dentry->d_sb;
a2654f78
AM
36675+ reenter = au_smack_reentering(sb);
36676+ if (!reenter) {
36677+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36678+ if (unlikely(err))
36679+ goto out;
36680+ }
36681+ err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
c1595e42
JR
36682+ if (unlikely(err))
36683+ goto out_si;
36684+ if (unlikely(!h_path.dentry))
36685+ /* illegally overlapped or something */
36686+ goto out_di; /* pretending success */
36687+
36688+ /* always topmost entry only */
36689+ switch (arg->type) {
36690+ case AU_XATTR_LIST:
36691+ err = vfs_listxattr(h_path.dentry,
36692+ arg->u.list.list, arg->u.list.size);
36693+ break;
36694+ case AU_XATTR_GET:
5afbbe0d 36695+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
36696+ err = vfs_getxattr(h_path.dentry,
36697+ arg->u.get.name, arg->u.get.value,
36698+ arg->u.get.size);
36699+ break;
36700+ }
36701+
36702+out_di:
a2654f78
AM
36703+ if (!reenter)
36704+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36705+out_si:
a2654f78
AM
36706+ if (!reenter)
36707+ si_read_unlock(sb);
c1595e42
JR
36708+out:
36709+ AuTraceErr(err);
36710+ return err;
36711+}
36712+
36713+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36714+{
36715+ struct au_lgxattr arg = {
36716+ .type = AU_XATTR_LIST,
36717+ .u.list = {
36718+ .list = list,
36719+ .size = size
36720+ },
36721+ };
36722+
36723+ return au_lgxattr(dentry, &arg);
36724+}
36725+
f2c43d5f
AM
36726+static ssize_t au_getxattr(struct dentry *dentry,
36727+ struct inode *inode __maybe_unused,
36728+ const char *name, void *value, size_t size)
c1595e42
JR
36729+{
36730+ struct au_lgxattr arg = {
36731+ .type = AU_XATTR_GET,
36732+ .u.get = {
36733+ .name = name,
36734+ .value = value,
36735+ .size = size
36736+ },
36737+ };
36738+
36739+ return au_lgxattr(dentry, &arg);
36740+}
36741+
f2c43d5f
AM
36742+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36743+ const char *name, const void *value, size_t size,
36744+ int flags)
c1595e42 36745+{
f2c43d5f 36746+ struct au_sxattr arg = {
c1595e42
JR
36747+ .type = AU_XATTR_SET,
36748+ .u.set = {
36749+ .name = name,
36750+ .value = value,
36751+ .size = size,
36752+ .flags = flags
36753+ },
36754+ };
36755+
f2c43d5f 36756+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36757+}
36758+
36759+/* ---------------------------------------------------------------------- */
36760+
f2c43d5f
AM
36761+static int au_xattr_get(const struct xattr_handler *handler,
36762+ struct dentry *dentry, struct inode *inode,
36763+ const char *name, void *buffer, size_t size)
c1595e42 36764+{
f2c43d5f 36765+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36766+}
36767+
f2c43d5f
AM
36768+static int au_xattr_set(const struct xattr_handler *handler,
36769+ struct dentry *dentry, struct inode *inode,
36770+ const char *name, const void *value, size_t size,
36771+ int flags)
c1595e42 36772+{
f2c43d5f 36773+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36774+}
36775+
36776+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36777+ .name = "",
36778+ .prefix = "",
c1595e42
JR
36779+ .get = au_xattr_get,
36780+ .set = au_xattr_set
c1595e42
JR
36781+};
36782+
36783+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36784+#ifdef CONFIG_FS_POSIX_ACL
36785+ &posix_acl_access_xattr_handler,
36786+ &posix_acl_default_xattr_handler,
36787+#endif
36788+ &au_xattr_handler, /* must be last */
f2c43d5f 36789+ NULL
c1595e42
JR
36790+};
36791+
36792+void au_xattr_init(struct super_block *sb)
36793+{
f2c43d5f 36794+ sb->s_xattr = au_xattr_handlers;
c1595e42 36795+}
7f207e10 36796diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
eca34b5c 36797--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
016522bc
AM
36798+++ linux/fs/aufs/xino.c 2020-04-03 08:16:49.834195677 +0200
36799@@ -0,0 +1,1966 @@
cd7a4cd9 36800+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36801+/*
016522bc 36802+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 36803+ *
36804+ * This program, aufs is free software; you can redistribute it and/or modify
36805+ * it under the terms of the GNU General Public License as published by
36806+ * the Free Software Foundation; either version 2 of the License, or
36807+ * (at your option) any later version.
dece6358
AM
36808+ *
36809+ * This program is distributed in the hope that it will be useful,
36810+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36811+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36812+ * GNU General Public License for more details.
36813+ *
36814+ * You should have received a copy of the GNU General Public License
523b37e3 36815+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36816+ */
36817+
36818+/*
36819+ * external inode number translation table and bitmap
062440b3
AM
36820+ *
36821+ * things to consider
36822+ * - the lifetime
36823+ * + au_xino object
36824+ * + XINO files (xino, xib, xigen)
36825+ * + dynamic debugfs entries (xiN)
36826+ * + static debugfs entries (xib, xigen)
36827+ * + static sysfs entry (xi_path)
36828+ * - several entry points to handle them.
36829+ * + mount(2) without xino option (default)
36830+ * + mount(2) with xino option
36831+ * + mount(2) with noxino option
36832+ * + umount(2)
36833+ * + remount with add/del branches
36834+ * + remount with xino/noxino options
1facf9fc 36835+ */
36836+
36837+#include <linux/seq_file.h>
392086de 36838+#include <linux/statfs.h>
1facf9fc 36839+#include "aufs.h"
36840+
062440b3
AM
36841+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36842+ aufs_bindex_t bbot,
36843+ struct super_block *h_sb)
1facf9fc 36844+{
062440b3
AM
36845+ /* todo: try binary-search if the branches are many */
36846+ for (; btop <= bbot; btop++)
36847+ if (h_sb == au_sbr_sb(sb, btop))
36848+ return btop;
36849+ return -1;
be52b249
AM
36850+}
36851+
062440b3
AM
36852+/*
36853+ * find another branch who is on the same filesystem of the specified
36854+ * branch{@btgt}. search until @bbot.
36855+ */
36856+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36857+ aufs_bindex_t bbot)
1facf9fc 36858+{
062440b3
AM
36859+ aufs_bindex_t bindex;
36860+ struct super_block *tgt_sb;
1facf9fc 36861+
062440b3
AM
36862+ tgt_sb = au_sbr_sb(sb, btgt);
36863+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36864+ if (bindex < 0)
36865+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
1facf9fc 36866+
062440b3 36867+ return bindex;
1facf9fc 36868+}
36869+
36870+/* ---------------------------------------------------------------------- */
36871+
36872+/*
062440b3 36873+ * stop unnecessary notify events at creating xino files
1facf9fc 36874+ */
acd2b654
AM
36875+
36876+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36877+{
36878+ aufs_bindex_t bfound, bindex, bbot;
36879+ struct dentry *parent;
36880+ struct au_branch *br;
36881+
36882+ bfound = -1;
36883+ parent = dentry->d_parent; /* safe d_parent access */
36884+ bbot = au_sbbot(sb);
36885+ for (bindex = 0; bindex <= bbot; bindex++) {
36886+ br = au_sbr(sb, bindex);
36887+ if (au_br_dentry(br) == parent) {
36888+ bfound = bindex;
36889+ break;
36890+ }
36891+ }
36892+
36893+ AuDbg("bfound b%d\n", bfound);
36894+ return bfound;
36895+}
36896+
062440b3
AM
36897+struct au_xino_lock_dir {
36898+ struct au_hinode *hdir;
36899+ struct dentry *parent;
36900+ struct inode *dir;
36901+};
36902+
36903+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36904+ unsigned int lsc)
1facf9fc 36905+{
062440b3
AM
36906+ struct dentry *parent;
36907+ struct inode *dir;
1facf9fc 36908+
062440b3 36909+ parent = dget_parent(dentry);
5527c038 36910+ dir = d_inode(parent);
062440b3
AM
36911+ inode_lock_nested(dir, lsc);
36912+#if 0 /* it should not happen */
36913+ spin_lock(&dentry->d_lock);
36914+ if (unlikely(dentry->d_parent != parent)) {
36915+ spin_unlock(&dentry->d_lock);
36916+ inode_unlock(dir);
36917+ dput(parent);
36918+ parent = NULL;
1facf9fc 36919+ goto out;
36920+ }
062440b3 36921+ spin_unlock(&dentry->d_lock);
1facf9fc 36922+
4f0767ce 36923+out:
062440b3
AM
36924+#endif
36925+ return parent;
1facf9fc 36926+}
36927+
062440b3 36928+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
1facf9fc 36929+ struct au_xino_lock_dir *ldir)
36930+{
acd2b654 36931+ aufs_bindex_t bindex;
1facf9fc 36932+
36933+ ldir->hdir = NULL;
acd2b654 36934+ bindex = au_xi_root(sb, xipath->dentry);
1facf9fc 36935+ if (bindex >= 0) {
062440b3 36936+ /* rw branch root */
5527c038 36937+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 36938+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 36939+ } else {
062440b3
AM
36940+ /* other */
36941+ ldir->parent = au_dget_parent_lock(xipath->dentry,
36942+ AuLsc_I_PARENT);
febd17d6 36943+ ldir->dir = d_inode(ldir->parent);
1facf9fc 36944+ }
36945+}
36946+
36947+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36948+{
36949+ if (ldir->hdir)
5afbbe0d 36950+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 36951+ else {
febd17d6 36952+ inode_unlock(ldir->dir);
1facf9fc 36953+ dput(ldir->parent);
36954+ }
36955+}
36956+
36957+/* ---------------------------------------------------------------------- */
36958+
062440b3
AM
36959+/*
36960+ * create and set a new xino file
36961+ */
83b672a5
AM
36962+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36963+ int wbrtop)
062440b3
AM
36964+{
36965+ struct file *file;
36966+ struct dentry *h_parent, *d;
36967+ struct inode *h_dir, *inode;
36968+ int err;
83b672a5 36969+ static DEFINE_MUTEX(mtx);
062440b3
AM
36970+
36971+ /*
36972+ * at mount-time, and the xino file is the default path,
36973+ * hnotify is disabled so we have no notify events to ignore.
36974+ * when a user specified the xino, we cannot get au_hdir to be ignored.
36975+ */
83b672a5
AM
36976+ if (!wbrtop)
36977+ mutex_lock(&mtx);
062440b3
AM
36978+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36979+ /* | __FMODE_NONOTIFY */,
cd7a4cd9 36980+ 0666);
062440b3 36981+ if (IS_ERR(file)) {
83b672a5
AM
36982+ if (!wbrtop)
36983+ mutex_unlock(&mtx);
062440b3
AM
36984+ if (!silent)
36985+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36986+ return file;
36987+ }
36988+
36989+ /* keep file count */
36990+ err = 0;
36991+ d = file->f_path.dentry;
36992+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
83b672a5
AM
36993+ if (!wbrtop)
36994+ mutex_unlock(&mtx);
062440b3
AM
36995+ /* mnt_want_write() is unnecessary here */
36996+ h_dir = d_inode(h_parent);
36997+ inode = file_inode(file);
36998+ /* no delegation since it is just created */
36999+ if (inode->i_nlink)
37000+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37001+ /*force*/0);
37002+ inode_unlock(h_dir);
37003+ dput(h_parent);
37004+ if (unlikely(err)) {
37005+ if (!silent)
37006+ pr_err("unlink %s(%d)\n", fpath, err);
37007+ goto out;
37008+ }
37009+
37010+ err = -EINVAL;
37011+ if (unlikely(sb == d->d_sb)) {
37012+ if (!silent)
37013+ pr_err("%s must be outside\n", fpath);
37014+ goto out;
37015+ }
37016+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37017+ if (!silent)
37018+ pr_err("xino doesn't support %s(%s)\n",
37019+ fpath, au_sbtype(d->d_sb));
37020+ goto out;
37021+ }
37022+ return file; /* success */
37023+
37024+out:
37025+ fput(file);
37026+ file = ERR_PTR(err);
37027+ return file;
37028+}
37029+
37030+/*
37031+ * create a new xinofile at the same place/path as @base.
37032+ */
37033+struct file *au_xino_create2(struct super_block *sb, struct path *base,
37034+ struct file *copy_src)
37035+{
37036+ struct file *file;
37037+ struct dentry *dentry, *parent;
37038+ struct inode *dir, *delegated;
37039+ struct qstr *name;
37040+ struct path path;
37041+ int err, do_unlock;
37042+ struct au_xino_lock_dir ldir;
37043+
37044+ do_unlock = 1;
37045+ au_xino_lock_dir(sb, base, &ldir);
37046+ dentry = base->dentry;
37047+ parent = dentry->d_parent; /* dir inode is locked */
37048+ dir = d_inode(parent);
37049+ IMustLock(dir);
37050+
37051+ name = &dentry->d_name;
37052+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37053+ if (IS_ERR(path.dentry)) {
37054+ file = (void *)path.dentry;
37055+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37056+ goto out;
37057+ }
37058+
37059+ /* no need to mnt_want_write() since we call dentry_open() later */
cd7a4cd9 37060+ err = vfs_create(dir, path.dentry, 0666, NULL);
062440b3
AM
37061+ if (unlikely(err)) {
37062+ file = ERR_PTR(err);
37063+ pr_err("%pd create err %d\n", dentry, err);
37064+ goto out_dput;
37065+ }
37066+
37067+ path.mnt = base->mnt;
37068+ file = vfsub_dentry_open(&path,
37069+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37070+ /* | __FMODE_NONOTIFY */);
37071+ if (IS_ERR(file)) {
37072+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37073+ goto out_dput;
37074+ }
37075+
37076+ delegated = NULL;
37077+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37078+ au_xino_unlock_dir(&ldir);
37079+ do_unlock = 0;
37080+ if (unlikely(err == -EWOULDBLOCK)) {
37081+ pr_warn("cannot retry for NFSv4 delegation"
37082+ " for an internal unlink\n");
37083+ iput(delegated);
37084+ }
37085+ if (unlikely(err)) {
37086+ pr_err("%pd unlink err %d\n", dentry, err);
37087+ goto out_fput;
37088+ }
37089+
37090+ if (copy_src) {
37091+ /* no one can touch copy_src xino */
37092+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37093+ if (unlikely(err)) {
37094+ pr_err("%pd copy err %d\n", dentry, err);
37095+ goto out_fput;
37096+ }
37097+ }
37098+ goto out_dput; /* success */
1facf9fc 37099+
062440b3
AM
37100+out_fput:
37101+ fput(file);
37102+ file = ERR_PTR(err);
37103+out_dput:
37104+ dput(path.dentry);
37105+out:
37106+ if (do_unlock)
37107+ au_xino_unlock_dir(&ldir);
37108+ return file;
37109+}
37110+
acd2b654
AM
37111+struct file *au_xino_file1(struct au_xino *xi)
37112+{
37113+ struct file *file;
37114+ unsigned int u, nfile;
37115+
37116+ file = NULL;
37117+ nfile = xi->xi_nfile;
37118+ for (u = 0; u < nfile; u++) {
37119+ file = xi->xi_file[u];
37120+ if (file)
37121+ break;
37122+ }
37123+
37124+ return file;
37125+}
37126+
37127+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37128+{
37129+ int err;
37130+ struct file *f;
37131+ void *p;
37132+
37133+ if (file)
37134+ get_file(file);
37135+
37136+ err = 0;
37137+ f = NULL;
37138+ if (idx < xi->xi_nfile) {
37139+ f = xi->xi_file[idx];
37140+ if (f)
37141+ fput(f);
37142+ } else {
37143+ p = au_kzrealloc(xi->xi_file,
37144+ sizeof(*xi->xi_file) * xi->xi_nfile,
37145+ sizeof(*xi->xi_file) * (idx + 1),
37146+ GFP_NOFS, /*may_shrink*/0);
37147+ if (p) {
37148+ MtxMustLock(&xi->xi_mtx);
37149+ xi->xi_file = p;
37150+ xi->xi_nfile = idx + 1;
37151+ } else {
37152+ err = -ENOMEM;
37153+ if (file)
37154+ fput(file);
37155+ goto out;
37156+ }
37157+ }
37158+ xi->xi_file[idx] = file;
37159+
37160+out:
37161+ return err;
37162+}
37163+
37164+/*
37165+ * if @xinew->xi is not set, then create new xigen file.
37166+ */
37167+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37168+{
37169+ struct file *file;
37170+ int err;
37171+
37172+ SiMustAnyLock(sb);
37173+
37174+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37175+ if (IS_ERR(file)) {
37176+ err = PTR_ERR(file);
37177+ pr_err("%s[%d], err %d\n",
37178+ xinew->xi ? "xino" : "xigen",
37179+ xinew->idx, err);
37180+ goto out;
37181+ }
37182+
37183+ if (xinew->xi)
37184+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37185+ else {
37186+ BUG();
37187+ /* todo: make xigen file an array */
37188+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37189+ }
37190+ fput(file);
37191+ if (unlikely(err))
37192+ file = ERR_PTR(err);
37193+
37194+out:
37195+ return file;
37196+}
37197+
062440b3
AM
37198+/* ---------------------------------------------------------------------- */
37199+
37200+/*
37201+ * truncate xino files
37202+ */
acd2b654
AM
37203+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37204+ int idx, struct kstatfs *st)
1facf9fc 37205+{
37206+ int err;
392086de 37207+ blkcnt_t blocks;
acd2b654
AM
37208+ struct file *file, *new_xino;
37209+ struct au_xi_new xinew = {
37210+ .idx = idx
37211+ };
37212+
37213+ err = 0;
37214+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37215+ file = au_xino_file(xinew.xi, idx);
37216+ if (!file)
37217+ goto out;
37218+
37219+ xinew.base = &file->f_path;
37220+ err = vfs_statfs(xinew.base, st);
37221+ if (unlikely(err)) {
37222+ AuErr1("statfs err %d, ignored\n", err);
37223+ err = 0;
37224+ goto out;
37225+ }
37226+
37227+ blocks = file_inode(file)->i_blocks;
37228+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37229+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37230+
37231+ xinew.copy_src = file;
37232+ new_xino = au_xi_new(sb, &xinew);
37233+ if (IS_ERR(new_xino)) {
37234+ err = PTR_ERR(new_xino);
37235+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37236+ goto out;
37237+ }
37238+
37239+ err = vfs_statfs(&new_xino->f_path, st);
37240+ if (!err)
37241+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37242+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37243+ st->f_bfree, st->f_blocks);
37244+ else {
37245+ AuErr1("statfs err %d, ignored\n", err);
37246+ err = 0;
37247+ }
37248+
37249+out:
37250+ return err;
37251+}
37252+
37253+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37254+{
37255+ int err, i;
37256+ unsigned long jiffy;
062440b3 37257+ aufs_bindex_t bbot;
392086de 37258+ struct kstatfs *st;
1facf9fc 37259+ struct au_branch *br;
acd2b654 37260+ struct au_xino *xi;
1facf9fc 37261+
392086de 37262+ err = -ENOMEM;
be52b249 37263+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
37264+ if (unlikely(!st))
37265+ goto out;
37266+
1facf9fc 37267+ err = -EINVAL;
5afbbe0d
AM
37268+ bbot = au_sbbot(sb);
37269+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 37270+ goto out_st;
392086de 37271+
1facf9fc 37272+ err = 0;
acd2b654
AM
37273+ jiffy = jiffies;
37274+ br = au_sbr(sb, bindex);
37275+ xi = br->br_xino;
37276+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37277+ err = au_xino_do_trunc(sb, bindex, i, st);
37278+ if (!err)
37279+ au_sbi(sb)->si_xino_jiffy = jiffy;
392086de
AM
37280+
37281+out_st:
9f237c51 37282+ au_kfree_rcu(st);
4f0767ce 37283+out:
1facf9fc 37284+ return err;
37285+}
37286+
37287+struct xino_do_trunc_args {
37288+ struct super_block *sb;
37289+ struct au_branch *br;
acd2b654 37290+ int idx;
1facf9fc 37291+};
37292+
37293+static void xino_do_trunc(void *_args)
37294+{
37295+ struct xino_do_trunc_args *args = _args;
37296+ struct super_block *sb;
37297+ struct au_branch *br;
37298+ struct inode *dir;
acd2b654 37299+ int err, idx;
1facf9fc 37300+ aufs_bindex_t bindex;
37301+
37302+ err = 0;
37303+ sb = args->sb;
5527c038 37304+ dir = d_inode(sb->s_root);
1facf9fc 37305+ br = args->br;
acd2b654 37306+ idx = args->idx;
1facf9fc 37307+
37308+ si_noflush_write_lock(sb);
37309+ ii_read_lock_parent(dir);
37310+ bindex = au_br_index(sb, br->br_id);
acd2b654 37311+ err = au_xino_trunc(sb, bindex, idx);
1facf9fc 37312+ ii_read_unlock(dir);
37313+ if (unlikely(err))
392086de 37314+ pr_warn("err b%d, (%d)\n", bindex, err);
062440b3 37315+ atomic_dec(&br->br_xino->xi_truncating);
acd2b654 37316+ au_lcnt_dec(&br->br_count);
1facf9fc 37317+ si_write_unlock(sb);
027c5e7a 37318+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 37319+ au_kfree_rcu(args);
1facf9fc 37320+}
37321+
acd2b654
AM
37322+/*
37323+ * returns the index in the xi_file array whose corresponding file is necessary
37324+ * to truncate, or -1 which means no need to truncate.
37325+ */
392086de
AM
37326+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37327+{
37328+ int err;
acd2b654 37329+ unsigned int u;
392086de
AM
37330+ struct kstatfs st;
37331+ struct au_sbinfo *sbinfo;
acd2b654 37332+ struct au_xino *xi;
062440b3 37333+ struct file *file;
392086de
AM
37334+
37335+ /* todo: si_xino_expire and the ratio should be customizable */
37336+ sbinfo = au_sbi(sb);
37337+ if (time_before(jiffies,
37338+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
acd2b654 37339+ return -1;
392086de
AM
37340+
37341+ /* truncation border */
acd2b654
AM
37342+ xi = br->br_xino;
37343+ for (u = 0; u < xi->xi_nfile; u++) {
37344+ file = au_xino_file(xi, u);
37345+ if (!file)
37346+ continue;
37347+
37348+ err = vfs_statfs(&file->f_path, &st);
37349+ if (unlikely(err)) {
37350+ AuErr1("statfs err %d, ignored\n", err);
37351+ return -1;
37352+ }
37353+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37354+ >= AUFS_XINO_DEF_TRUNC)
37355+ return u;
392086de 37356+ }
392086de 37357+
acd2b654 37358+ return -1;
392086de
AM
37359+}
37360+
1facf9fc 37361+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37362+{
acd2b654 37363+ int idx;
1facf9fc 37364+ struct xino_do_trunc_args *args;
37365+ int wkq_err;
37366+
acd2b654
AM
37367+ idx = xino_trunc_test(sb, br);
37368+ if (idx < 0)
1facf9fc 37369+ return;
37370+
062440b3 37371+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
1facf9fc 37372+ goto out;
37373+
37374+ /* lock and kfree() will be called in trunc_xino() */
37375+ args = kmalloc(sizeof(*args), GFP_NOFS);
37376+ if (unlikely(!args)) {
37377+ AuErr1("no memory\n");
f0c0a007 37378+ goto out;
1facf9fc 37379+ }
37380+
acd2b654 37381+ au_lcnt_inc(&br->br_count);
1facf9fc 37382+ args->sb = sb;
37383+ args->br = br;
acd2b654 37384+ args->idx = idx;
53392da6 37385+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 37386+ if (!wkq_err)
37387+ return; /* success */
37388+
4a4d8108 37389+ pr_err("wkq %d\n", wkq_err);
acd2b654 37390+ au_lcnt_dec(&br->br_count);
9f237c51 37391+ au_kfree_rcu(args);
1facf9fc 37392+
4f0767ce 37393+out:
062440b3 37394+ atomic_dec(&br->br_xino->xi_truncating);
1facf9fc 37395+}
37396+
37397+/* ---------------------------------------------------------------------- */
37398+
acd2b654
AM
37399+struct au_xi_calc {
37400+ int idx;
37401+ loff_t pos;
37402+};
37403+
37404+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37405+ struct au_xi_calc *calc)
37406+{
37407+ loff_t maxent;
37408+
37409+ maxent = au_xi_maxent(sb);
37410+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37411+ calc->pos *= sizeof(ino_t);
37412+}
37413+
37414+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37415+ struct au_xi_calc *calc)
37416+{
37417+ int err;
37418+ struct file *file;
37419+ struct au_xino *xi = br->br_xino;
37420+ struct au_xi_new xinew = {
37421+ .xi = xi
37422+ };
37423+
37424+ SiMustAnyLock(sb);
37425+
37426+ err = 0;
37427+ if (!xi)
37428+ goto out;
37429+
37430+ mutex_lock(&xi->xi_mtx);
37431+ file = au_xino_file(xi, calc->idx);
37432+ if (file)
37433+ goto out_mtx;
37434+
37435+ file = au_xino_file(xi, /*idx*/-1);
37436+ AuDebugOn(!file);
37437+ xinew.idx = calc->idx;
37438+ xinew.base = &file->f_path;
37439+ /* xinew.copy_src = NULL; */
37440+ file = au_xi_new(sb, &xinew);
37441+ if (IS_ERR(file))
37442+ err = PTR_ERR(file);
37443+
37444+out_mtx:
37445+ mutex_unlock(&xi->xi_mtx);
37446+out:
37447+ return err;
37448+}
37449+
37450+struct au_xino_do_new_async_args {
37451+ struct super_block *sb;
37452+ struct au_branch *br;
37453+ struct au_xi_calc calc;
37454+ ino_t ino;
37455+};
37456+
9f237c51
AM
37457+struct au_xi_writing {
37458+ struct hlist_bl_node node;
37459+ ino_t h_ino, ino;
37460+};
37461+
acd2b654
AM
37462+static int au_xino_do_write(vfs_writef_t write, struct file *file,
37463+ struct au_xi_calc *calc, ino_t ino);
37464+
37465+static void au_xino_call_do_new_async(void *args)
37466+{
37467+ struct au_xino_do_new_async_args *a = args;
37468+ struct au_branch *br;
37469+ struct super_block *sb;
37470+ struct au_sbinfo *sbi;
37471+ struct inode *root;
37472+ struct file *file;
9f237c51
AM
37473+ struct au_xi_writing *del, *p;
37474+ struct hlist_bl_head *hbl;
37475+ struct hlist_bl_node *pos;
acd2b654
AM
37476+ int err;
37477+
37478+ br = a->br;
37479+ sb = a->sb;
37480+ sbi = au_sbi(sb);
37481+ si_noflush_read_lock(sb);
37482+ root = d_inode(sb->s_root);
37483+ ii_read_lock_child(root);
37484+ err = au_xino_do_new_async(sb, br, &a->calc);
9f237c51
AM
37485+ if (unlikely(err)) {
37486+ AuIOErr("err %d\n", err);
37487+ goto out;
37488+ }
37489+
37490+ file = au_xino_file(br->br_xino, a->calc.idx);
37491+ AuDebugOn(!file);
37492+ err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
37493+ if (unlikely(err)) {
acd2b654 37494+ AuIOErr("err %d\n", err);
9f237c51
AM
37495+ goto out;
37496+ }
37497+
37498+ del = NULL;
37499+ hbl = &br->br_xino->xi_writing;
37500+ hlist_bl_lock(hbl);
37501+ au_hbl_for_each(pos, hbl) {
37502+ p = container_of(pos, struct au_xi_writing, node);
37503+ if (p->ino == a->ino) {
37504+ del = p;
37505+ hlist_bl_del(&p->node);
37506+ break;
37507+ }
37508+ }
37509+ hlist_bl_unlock(hbl);
37510+ au_kfree_rcu(del);
37511+
37512+out:
acd2b654
AM
37513+ au_lcnt_dec(&br->br_count);
37514+ ii_read_unlock(root);
37515+ si_read_unlock(sb);
37516+ au_nwt_done(&sbi->si_nowait);
9f237c51 37517+ au_kfree_rcu(a);
acd2b654
AM
37518+}
37519+
37520+/*
37521+ * create a new xino file asynchronously
37522+ */
37523+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37524+ struct au_xi_calc *calc, ino_t ino)
37525+{
37526+ int err;
37527+ struct au_xino_do_new_async_args *arg;
37528+
37529+ err = -ENOMEM;
37530+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37531+ if (unlikely(!arg))
37532+ goto out;
37533+
37534+ arg->sb = sb;
37535+ arg->br = br;
37536+ arg->calc = *calc;
37537+ arg->ino = ino;
37538+ au_lcnt_inc(&br->br_count);
37539+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37540+ if (unlikely(err)) {
37541+ pr_err("wkq %d\n", err);
37542+ au_lcnt_dec(&br->br_count);
9f237c51 37543+ au_kfree_rcu(arg);
acd2b654
AM
37544+ }
37545+
37546+out:
37547+ return err;
37548+}
37549+
062440b3
AM
37550+/*
37551+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37552+ * at the position of @h_ino.
37553+ */
37554+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37555+ ino_t *ino)
37556+{
37557+ int err;
37558+ ssize_t sz;
acd2b654 37559+ struct au_xi_calc calc;
062440b3 37560+ struct au_sbinfo *sbinfo;
acd2b654 37561+ struct file *file;
9f237c51
AM
37562+ struct au_xino *xi;
37563+ struct hlist_bl_head *hbl;
37564+ struct hlist_bl_node *pos;
37565+ struct au_xi_writing *p;
062440b3
AM
37566+
37567+ *ino = 0;
37568+ if (!au_opt_test(au_mntflags(sb), XINO))
37569+ return 0; /* no xino */
37570+
37571+ err = 0;
acd2b654 37572+ au_xi_calc(sb, h_ino, &calc);
9f237c51
AM
37573+ xi = au_sbr(sb, bindex)->br_xino;
37574+ file = au_xino_file(xi, calc.idx);
37575+ if (!file) {
37576+ hbl = &xi->xi_writing;
37577+ hlist_bl_lock(hbl);
37578+ au_hbl_for_each(pos, hbl) {
37579+ p = container_of(pos, struct au_xi_writing, node);
37580+ if (p->h_ino == h_ino) {
37581+ AuDbg("hi%llu, i%llu, found\n",
37582+ (u64)p->h_ino, (u64)p->ino);
37583+ *ino = p->ino;
37584+ break;
37585+ }
37586+ }
37587+ hlist_bl_unlock(hbl);
37588+ return 0;
37589+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37590+ return 0; /* no xino */
062440b3 37591+
acd2b654
AM
37592+ sbinfo = au_sbi(sb);
37593+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
062440b3
AM
37594+ if (sz == sizeof(*ino))
37595+ return 0; /* success */
37596+
37597+ err = sz;
37598+ if (unlikely(sz >= 0)) {
37599+ err = -EIO;
37600+ AuIOErr("xino read error (%zd)\n", sz);
37601+ }
062440b3
AM
37602+ return err;
37603+}
37604+
5527c038 37605+static int au_xino_do_write(vfs_writef_t write, struct file *file,
acd2b654 37606+ struct au_xi_calc *calc, ino_t ino)
1facf9fc 37607+{
1facf9fc 37608+ ssize_t sz;
37609+
acd2b654 37610+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
1facf9fc 37611+ if (sz == sizeof(ino))
37612+ return 0; /* success */
37613+
37614+ AuIOErr("write failed (%zd)\n", sz);
37615+ return -EIO;
37616+}
37617+
37618+/*
37619+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37620+ * at the position of @h_ino.
37621+ * even if @ino is zero, it is written to the xinofile and means no entry.
37622+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37623+ * try truncating it.
37624+ */
37625+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37626+ ino_t ino)
37627+{
37628+ int err;
37629+ unsigned int mnt_flags;
acd2b654
AM
37630+ struct au_xi_calc calc;
37631+ struct file *file;
1facf9fc 37632+ struct au_branch *br;
acd2b654 37633+ struct au_xino *xi;
9f237c51 37634+ struct au_xi_writing *p;
1facf9fc 37635+
dece6358 37636+ SiMustAnyLock(sb);
1facf9fc 37637+
37638+ mnt_flags = au_mntflags(sb);
37639+ if (!au_opt_test(mnt_flags, XINO))
37640+ return 0;
37641+
acd2b654 37642+ au_xi_calc(sb, h_ino, &calc);
1facf9fc 37643+ br = au_sbr(sb, bindex);
acd2b654
AM
37644+ xi = br->br_xino;
37645+ file = au_xino_file(xi, calc.idx);
37646+ if (!file) {
9f237c51
AM
37647+ /* store the inum pair into the list */
37648+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37649+ p->h_ino = h_ino;
37650+ p->ino = ino;
37651+ au_hbl_add(&p->node, &xi->xi_writing);
37652+
acd2b654
AM
37653+ /* create and write a new xino file asynchronously */
37654+ err = au_xino_new_async(sb, br, &calc, ino);
37655+ if (!err)
37656+ return 0; /* success */
37657+ goto out;
37658+ }
37659+
37660+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
1facf9fc 37661+ if (!err) {
acd2b654 37662+ br = au_sbr(sb, bindex);
1facf9fc 37663+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37664+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37665+ xino_try_trunc(sb, br);
37666+ return 0; /* success */
37667+ }
37668+
acd2b654 37669+out:
1facf9fc 37670+ AuIOErr("write failed (%d)\n", err);
37671+ return -EIO;
37672+}
37673+
062440b3
AM
37674+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37675+ size_t size, loff_t *pos);
1facf9fc 37676+
062440b3
AM
37677+/* todo: unnecessary to support mmap_sem since kernel-space? */
37678+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37679+ loff_t *pos)
37680+{
37681+ ssize_t err;
37682+ mm_segment_t oldfs;
37683+ union {
37684+ void *k;
37685+ char __user *u;
37686+ } buf;
37687+ int i;
37688+ const int prevent_endless = 10;
1facf9fc 37689+
062440b3
AM
37690+ i = 0;
37691+ buf.k = kbuf;
37692+ oldfs = get_fs();
37693+ set_fs(KERNEL_DS);
37694+ do {
37695+ err = func(file, buf.u, size, pos);
37696+ if (err == -EINTR
37697+ && !au_wkq_test()
37698+ && fatal_signal_pending(current)) {
37699+ set_fs(oldfs);
37700+ err = xino_fread_wkq(func, file, kbuf, size, pos);
37701+ BUG_ON(err == -EINTR);
37702+ oldfs = get_fs();
37703+ set_fs(KERNEL_DS);
37704+ }
37705+ } while (i++ < prevent_endless
37706+ && (err == -EAGAIN || err == -EINTR));
37707+ set_fs(oldfs);
37708+
37709+#if 0 /* reserved for future use */
37710+ if (err > 0)
37711+ fsnotify_access(file->f_path.dentry);
37712+#endif
37713+
37714+ return err;
37715+}
37716+
37717+struct xino_fread_args {
37718+ ssize_t *errp;
37719+ vfs_readf_t func;
37720+ struct file *file;
37721+ void *buf;
37722+ size_t size;
37723+ loff_t *pos;
37724+};
37725+
37726+static void call_xino_fread(void *args)
37727+{
37728+ struct xino_fread_args *a = args;
37729+ *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37730+}
37731+
37732+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37733+ size_t size, loff_t *pos)
37734+{
37735+ ssize_t err;
37736+ int wkq_err;
37737+ struct xino_fread_args args = {
37738+ .errp = &err,
37739+ .func = func,
37740+ .file = file,
37741+ .buf = buf,
37742+ .size = size,
37743+ .pos = pos
37744+ };
37745+
37746+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37747+ if (unlikely(wkq_err))
37748+ err = wkq_err;
37749+
37750+ return err;
37751+}
37752+
37753+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37754+ size_t size, loff_t *pos);
37755+
37756+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37757+ size_t size, loff_t *pos)
37758+{
37759+ ssize_t err;
37760+ mm_segment_t oldfs;
37761+ union {
37762+ void *k;
37763+ const char __user *u;
37764+ } buf;
37765+ int i;
37766+ const int prevent_endless = 10;
37767+
37768+ i = 0;
37769+ buf.k = kbuf;
37770+ oldfs = get_fs();
37771+ set_fs(KERNEL_DS);
37772+ do {
37773+ err = func(file, buf.u, size, pos);
37774+ if (err == -EINTR
37775+ && !au_wkq_test()
37776+ && fatal_signal_pending(current)) {
37777+ set_fs(oldfs);
37778+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37779+ BUG_ON(err == -EINTR);
37780+ oldfs = get_fs();
37781+ set_fs(KERNEL_DS);
37782+ }
37783+ } while (i++ < prevent_endless
37784+ && (err == -EAGAIN || err == -EINTR));
37785+ set_fs(oldfs);
37786+
37787+#if 0 /* reserved for future use */
37788+ if (err > 0)
37789+ fsnotify_modify(file->f_path.dentry);
37790+#endif
37791+
37792+ return err;
37793+}
37794+
37795+struct do_xino_fwrite_args {
37796+ ssize_t *errp;
37797+ vfs_writef_t func;
37798+ struct file *file;
37799+ void *buf;
37800+ size_t size;
37801+ loff_t *pos;
37802+};
37803+
37804+static void call_do_xino_fwrite(void *args)
37805+{
37806+ struct do_xino_fwrite_args *a = args;
37807+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37808+}
37809+
37810+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37811+ size_t size, loff_t *pos)
37812+{
37813+ ssize_t err;
37814+ int wkq_err;
37815+ struct do_xino_fwrite_args args = {
37816+ .errp = &err,
37817+ .func = func,
37818+ .file = file,
37819+ .buf = buf,
37820+ .size = size,
37821+ .pos = pos
37822+ };
37823+
37824+ /*
37825+ * it breaks RLIMIT_FSIZE and normal user's limit,
37826+ * users should care about quota and real 'filesystem full.'
37827+ */
37828+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37829+ if (unlikely(wkq_err))
37830+ err = wkq_err;
37831+
37832+ return err;
37833+}
37834+
37835+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37836+ size_t size, loff_t *pos)
37837+{
37838+ ssize_t err;
37839+
37840+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37841+ lockdep_off();
37842+ err = do_xino_fwrite(func, file, buf, size, pos);
37843+ lockdep_on();
37844+ } else {
37845+ lockdep_off();
37846+ err = xino_fwrite_wkq(func, file, buf, size, pos);
37847+ lockdep_on();
37848+ }
37849+
37850+ return err;
37851+}
37852+
37853+/* ---------------------------------------------------------------------- */
37854+
37855+/*
37856+ * inode number bitmap
37857+ */
1facf9fc 37858+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37859+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37860+{
37861+ ino_t ino;
37862+
37863+ AuDebugOn(bit < 0 || page_bits <= bit);
37864+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37865+ return ino;
37866+}
37867+
37868+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37869+{
37870+ AuDebugOn(ino < AUFS_FIRST_INO);
37871+ ino -= AUFS_FIRST_INO;
37872+ *pindex = ino / page_bits;
37873+ *bit = ino % page_bits;
37874+}
37875+
37876+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37877+{
37878+ int err;
37879+ loff_t pos;
37880+ ssize_t sz;
37881+ struct au_sbinfo *sbinfo;
37882+ struct file *xib;
37883+ unsigned long *p;
37884+
37885+ sbinfo = au_sbi(sb);
37886+ MtxMustLock(&sbinfo->si_xib_mtx);
37887+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37888+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37889+
37890+ if (pindex == sbinfo->si_xib_last_pindex)
37891+ return 0;
37892+
37893+ xib = sbinfo->si_xib;
37894+ p = sbinfo->si_xib_buf;
37895+ pos = sbinfo->si_xib_last_pindex;
37896+ pos *= PAGE_SIZE;
37897+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37898+ if (unlikely(sz != PAGE_SIZE))
37899+ goto out;
37900+
37901+ pos = pindex;
37902+ pos *= PAGE_SIZE;
c06a8ce3 37903+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 37904+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37905+ else {
37906+ memset(p, 0, PAGE_SIZE);
37907+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37908+ }
37909+ if (sz == PAGE_SIZE) {
37910+ sbinfo->si_xib_last_pindex = pindex;
37911+ return 0; /* success */
37912+ }
37913+
4f0767ce 37914+out:
b752ccd1
AM
37915+ AuIOErr1("write failed (%zd)\n", sz);
37916+ err = sz;
37917+ if (sz >= 0)
37918+ err = -EIO;
37919+ return err;
37920+}
37921+
b752ccd1
AM
37922+static void au_xib_clear_bit(struct inode *inode)
37923+{
37924+ int err, bit;
37925+ unsigned long pindex;
37926+ struct super_block *sb;
37927+ struct au_sbinfo *sbinfo;
37928+
37929+ AuDebugOn(inode->i_nlink);
37930+
37931+ sb = inode->i_sb;
37932+ xib_calc_bit(inode->i_ino, &pindex, &bit);
37933+ AuDebugOn(page_bits <= bit);
37934+ sbinfo = au_sbi(sb);
37935+ mutex_lock(&sbinfo->si_xib_mtx);
37936+ err = xib_pindex(sb, pindex);
37937+ if (!err) {
37938+ clear_bit(bit, sbinfo->si_xib_buf);
37939+ sbinfo->si_xib_next_bit = bit;
37940+ }
37941+ mutex_unlock(&sbinfo->si_xib_mtx);
37942+}
37943+
1facf9fc 37944+/* ---------------------------------------------------------------------- */
37945+
1facf9fc 37946+/*
062440b3 37947+ * truncate a xino bitmap file
1facf9fc 37948+ */
1facf9fc 37949+
37950+/* todo: slow */
37951+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37952+{
37953+ int err, bit;
37954+ ssize_t sz;
37955+ unsigned long pindex;
37956+ loff_t pos, pend;
37957+ struct au_sbinfo *sbinfo;
5527c038 37958+ vfs_readf_t func;
1facf9fc 37959+ ino_t *ino;
37960+ unsigned long *p;
37961+
37962+ err = 0;
37963+ sbinfo = au_sbi(sb);
dece6358 37964+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 37965+ p = sbinfo->si_xib_buf;
37966+ func = sbinfo->si_xread;
c06a8ce3 37967+ pend = vfsub_f_size_read(file);
1facf9fc 37968+ pos = 0;
37969+ while (pos < pend) {
37970+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
37971+ err = sz;
37972+ if (unlikely(sz <= 0))
37973+ goto out;
37974+
37975+ err = 0;
37976+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37977+ if (unlikely(*ino < AUFS_FIRST_INO))
37978+ continue;
37979+
37980+ xib_calc_bit(*ino, &pindex, &bit);
37981+ AuDebugOn(page_bits <= bit);
37982+ err = xib_pindex(sb, pindex);
37983+ if (!err)
37984+ set_bit(bit, p);
37985+ else
37986+ goto out;
37987+ }
37988+ }
37989+
4f0767ce 37990+out:
1facf9fc 37991+ return err;
37992+}
37993+
37994+static int xib_restore(struct super_block *sb)
37995+{
acd2b654
AM
37996+ int err, i;
37997+ unsigned int nfile;
5afbbe0d 37998+ aufs_bindex_t bindex, bbot;
1facf9fc 37999+ void *page;
062440b3 38000+ struct au_branch *br;
acd2b654
AM
38001+ struct au_xino *xi;
38002+ struct file *file;
1facf9fc 38003+
38004+ err = -ENOMEM;
38005+ page = (void *)__get_free_page(GFP_NOFS);
38006+ if (unlikely(!page))
38007+ goto out;
38008+
38009+ err = 0;
5afbbe0d
AM
38010+ bbot = au_sbbot(sb);
38011+ for (bindex = 0; !err && bindex <= bbot; bindex++)
062440b3
AM
38012+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38013+ br = au_sbr(sb, bindex);
acd2b654
AM
38014+ xi = br->br_xino;
38015+ nfile = xi->xi_nfile;
38016+ for (i = 0; i < nfile; i++) {
38017+ file = au_xino_file(xi, i);
38018+ if (file)
38019+ err = do_xib_restore(sb, file, page);
38020+ }
062440b3
AM
38021+ } else
38022+ AuDbg("skip shared b%d\n", bindex);
1c60b727 38023+ free_page((unsigned long)page);
1facf9fc 38024+
4f0767ce 38025+out:
1facf9fc 38026+ return err;
38027+}
38028+
38029+int au_xib_trunc(struct super_block *sb)
38030+{
38031+ int err;
38032+ ssize_t sz;
38033+ loff_t pos;
1facf9fc 38034+ struct au_sbinfo *sbinfo;
38035+ unsigned long *p;
38036+ struct file *file;
38037+
dece6358
AM
38038+ SiMustWriteLock(sb);
38039+
1facf9fc 38040+ err = 0;
38041+ sbinfo = au_sbi(sb);
38042+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
38043+ goto out;
38044+
38045+ file = sbinfo->si_xib;
c06a8ce3 38046+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 38047+ goto out;
38048+
062440b3 38049+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
1facf9fc 38050+ err = PTR_ERR(file);
38051+ if (IS_ERR(file))
38052+ goto out;
38053+ fput(sbinfo->si_xib);
38054+ sbinfo->si_xib = file;
38055+
38056+ p = sbinfo->si_xib_buf;
38057+ memset(p, 0, PAGE_SIZE);
38058+ pos = 0;
38059+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
38060+ if (unlikely(sz != PAGE_SIZE)) {
38061+ err = sz;
38062+ AuIOErr("err %d\n", err);
38063+ if (sz >= 0)
38064+ err = -EIO;
38065+ goto out;
38066+ }
38067+
38068+ mutex_lock(&sbinfo->si_xib_mtx);
38069+ /* mnt_want_write() is unnecessary here */
38070+ err = xib_restore(sb);
38071+ mutex_unlock(&sbinfo->si_xib_mtx);
38072+
38073+out:
38074+ return err;
38075+}
38076+
38077+/* ---------------------------------------------------------------------- */
38078+
acd2b654 38079+struct au_xino *au_xino_alloc(unsigned int nfile)
062440b3
AM
38080+{
38081+ struct au_xino *xi;
38082+
38083+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
38084+ if (unlikely(!xi))
38085+ goto out;
acd2b654
AM
38086+ xi->xi_nfile = nfile;
38087+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38088+ if (unlikely(!xi->xi_file))
38089+ goto out_free;
062440b3
AM
38090+
38091+ xi->xi_nondir.total = 8; /* initial size */
38092+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38093+ GFP_NOFS);
38094+ if (unlikely(!xi->xi_nondir.array))
acd2b654 38095+ goto out_file;
062440b3
AM
38096+
38097+ spin_lock_init(&xi->xi_nondir.spin);
38098+ init_waitqueue_head(&xi->xi_nondir.wqh);
acd2b654 38099+ mutex_init(&xi->xi_mtx);
9f237c51 38100+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
062440b3
AM
38101+ atomic_set(&xi->xi_truncating, 0);
38102+ kref_init(&xi->xi_kref);
38103+ goto out; /* success */
38104+
acd2b654 38105+out_file:
9f237c51 38106+ au_kfree_try_rcu(xi->xi_file);
062440b3 38107+out_free:
9f237c51 38108+ au_kfree_rcu(xi);
062440b3
AM
38109+ xi = NULL;
38110+out:
38111+ return xi;
38112+}
38113+
acd2b654 38114+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
062440b3
AM
38115+{
38116+ int err;
38117+ struct au_xino *xi;
38118+
38119+ err = 0;
acd2b654 38120+ xi = au_xino_alloc(idx + 1);
062440b3
AM
38121+ if (unlikely(!xi)) {
38122+ err = -ENOMEM;
38123+ goto out;
38124+ }
38125+
acd2b654
AM
38126+ if (file)
38127+ get_file(file);
38128+ xi->xi_file[idx] = file;
062440b3
AM
38129+ AuDebugOn(br->br_xino);
38130+ br->br_xino = xi;
38131+
38132+out:
38133+ return err;
38134+}
38135+
38136+static void au_xino_release(struct kref *kref)
38137+{
38138+ struct au_xino *xi;
38139+ int i;
9f237c51
AM
38140+ unsigned long ul;
38141+ struct hlist_bl_head *hbl;
38142+ struct hlist_bl_node *pos, *n;
38143+ struct au_xi_writing *p;
062440b3
AM
38144+
38145+ xi = container_of(kref, struct au_xino, xi_kref);
acd2b654
AM
38146+ for (i = 0; i < xi->xi_nfile; i++)
38147+ if (xi->xi_file[i])
38148+ fput(xi->xi_file[i]);
062440b3
AM
38149+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38150+ AuDebugOn(xi->xi_nondir.array[i]);
acd2b654 38151+ mutex_destroy(&xi->xi_mtx);
9f237c51
AM
38152+ hbl = &xi->xi_writing;
38153+ ul = au_hbl_count(hbl);
38154+ if (unlikely(ul)) {
38155+ pr_warn("xi_writing %lu\n", ul);
38156+ hlist_bl_lock(hbl);
43982f53 38157+ hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
9f237c51 38158+ hlist_bl_del(&p->node);
016522bc
AM
38159+ /* kmemleak reported au_kfree_rcu() doesn't free it */
38160+ kfree(p);
9f237c51
AM
38161+ }
38162+ hlist_bl_unlock(hbl);
38163+ }
38164+ au_kfree_try_rcu(xi->xi_file);
38165+ au_kfree_try_rcu(xi->xi_nondir.array);
38166+ au_kfree_rcu(xi);
062440b3
AM
38167+}
38168+
38169+int au_xino_put(struct au_branch *br)
38170+{
38171+ int ret;
38172+ struct au_xino *xi;
38173+
38174+ ret = 0;
38175+ xi = br->br_xino;
38176+ if (xi) {
38177+ br->br_xino = NULL;
38178+ ret = kref_put(&xi->xi_kref, au_xino_release);
38179+ }
38180+
38181+ return ret;
38182+}
38183+
062440b3
AM
38184+/* ---------------------------------------------------------------------- */
38185+
1facf9fc 38186+/*
38187+ * xino mount option handlers
38188+ */
1facf9fc 38189+
38190+/* xino bitmap */
38191+static void xino_clear_xib(struct super_block *sb)
38192+{
38193+ struct au_sbinfo *sbinfo;
38194+
dece6358
AM
38195+ SiMustWriteLock(sb);
38196+
1facf9fc 38197+ sbinfo = au_sbi(sb);
062440b3 38198+ /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
1facf9fc 38199+ if (sbinfo->si_xib)
38200+ fput(sbinfo->si_xib);
38201+ sbinfo->si_xib = NULL;
f0c0a007 38202+ if (sbinfo->si_xib_buf)
1c60b727 38203+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 38204+ sbinfo->si_xib_buf = NULL;
38205+}
38206+
062440b3 38207+static int au_xino_set_xib(struct super_block *sb, struct path *path)
1facf9fc 38208+{
38209+ int err;
38210+ loff_t pos;
38211+ struct au_sbinfo *sbinfo;
38212+ struct file *file;
acd2b654 38213+ struct super_block *xi_sb;
1facf9fc 38214+
dece6358
AM
38215+ SiMustWriteLock(sb);
38216+
1facf9fc 38217+ sbinfo = au_sbi(sb);
062440b3 38218+ file = au_xino_create2(sb, path, sbinfo->si_xib);
1facf9fc 38219+ err = PTR_ERR(file);
38220+ if (IS_ERR(file))
38221+ goto out;
38222+ if (sbinfo->si_xib)
38223+ fput(sbinfo->si_xib);
38224+ sbinfo->si_xib = file;
5527c038
JR
38225+ sbinfo->si_xread = vfs_readf(file);
38226+ sbinfo->si_xwrite = vfs_writef(file);
acd2b654
AM
38227+ xi_sb = file_inode(file)->i_sb;
38228+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38229+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38230+ err = -EIO;
38231+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38232+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38233+ goto out_unset;
38234+ }
38235+ sbinfo->si_ximaxent /= sizeof(ino_t);
1facf9fc 38236+
38237+ err = -ENOMEM;
38238+ if (!sbinfo->si_xib_buf)
38239+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38240+ if (unlikely(!sbinfo->si_xib_buf))
38241+ goto out_unset;
38242+
38243+ sbinfo->si_xib_last_pindex = 0;
38244+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 38245+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 38246+ pos = 0;
38247+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
38248+ PAGE_SIZE, &pos);
38249+ if (unlikely(err != PAGE_SIZE))
38250+ goto out_free;
38251+ }
38252+ err = 0;
38253+ goto out; /* success */
38254+
4f0767ce 38255+out_free:
f0c0a007 38256+ if (sbinfo->si_xib_buf)
1c60b727 38257+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
38258+ sbinfo->si_xib_buf = NULL;
38259+ if (err >= 0)
38260+ err = -EIO;
4f0767ce 38261+out_unset:
b752ccd1
AM
38262+ fput(sbinfo->si_xib);
38263+ sbinfo->si_xib = NULL;
4f0767ce 38264+out:
062440b3 38265+ AuTraceErr(err);
b752ccd1 38266+ return err;
1facf9fc 38267+}
38268+
b752ccd1
AM
38269+/* xino for each branch */
38270+static void xino_clear_br(struct super_block *sb)
38271+{
5afbbe0d 38272+ aufs_bindex_t bindex, bbot;
b752ccd1 38273+ struct au_branch *br;
1facf9fc 38274+
5afbbe0d
AM
38275+ bbot = au_sbbot(sb);
38276+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1 38277+ br = au_sbr(sb, bindex);
062440b3
AM
38278+ AuDebugOn(!br);
38279+ au_xino_put(br);
38280+ }
38281+}
38282+
38283+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38284+ aufs_bindex_t bshared)
38285+{
38286+ struct au_branch *brshared;
b752ccd1 38287+
062440b3
AM
38288+ brshared = au_sbr(sb, bshared);
38289+ AuDebugOn(!brshared->br_xino);
38290+ AuDebugOn(!brshared->br_xino->xi_file);
38291+ if (br->br_xino != brshared->br_xino) {
38292+ au_xino_get(brshared);
38293+ au_xino_put(br);
38294+ br->br_xino = brshared->br_xino;
b752ccd1
AM
38295+ }
38296+}
38297+
062440b3
AM
38298+struct au_xino_do_set_br {
38299+ vfs_writef_t writef;
38300+ struct au_branch *br;
38301+ ino_t h_ino;
38302+ aufs_bindex_t bshared;
38303+};
38304+
38305+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38306+ struct au_xino_do_set_br *args)
1facf9fc 38307+{
38308+ int err;
acd2b654 38309+ struct au_xi_calc calc;
062440b3 38310+ struct file *file;
acd2b654
AM
38311+ struct au_branch *br;
38312+ struct au_xi_new xinew = {
38313+ .base = path
38314+ };
062440b3 38315+
acd2b654
AM
38316+ br = args->br;
38317+ xinew.xi = br->br_xino;
38318+ au_xi_calc(sb, args->h_ino, &calc);
38319+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38320+ if (args->bshared >= 0)
062440b3 38321+ /* shared xino */
acd2b654
AM
38322+ au_xino_set_br_shared(sb, br, args->bshared);
38323+ else if (!xinew.xi) {
38324+ /* new xino */
38325+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38326+ if (unlikely(err))
38327+ goto out;
062440b3
AM
38328+ }
38329+
acd2b654
AM
38330+ /* force re-creating */
38331+ xinew.xi = br->br_xino;
38332+ xinew.idx = calc.idx;
38333+ mutex_lock(&xinew.xi->xi_mtx);
38334+ file = au_xi_new(sb, &xinew);
38335+ mutex_unlock(&xinew.xi->xi_mtx);
062440b3
AM
38336+ err = PTR_ERR(file);
38337+ if (IS_ERR(file))
38338+ goto out;
acd2b654
AM
38339+ AuDebugOn(!file);
38340+
38341+ err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
38342+ if (unlikely(err))
38343+ au_xino_put(br);
062440b3 38344+
062440b3
AM
38345+out:
38346+ AuTraceErr(err);
38347+ return err;
38348+}
38349+
38350+static int au_xino_set_br(struct super_block *sb, struct path *path)
38351+{
38352+ int err;
38353+ aufs_bindex_t bindex, bbot;
38354+ struct au_xino_do_set_br args;
b752ccd1 38355+ struct inode *inode;
1facf9fc 38356+
b752ccd1
AM
38357+ SiMustWriteLock(sb);
38358+
5afbbe0d 38359+ bbot = au_sbbot(sb);
5527c038 38360+ inode = d_inode(sb->s_root);
062440b3
AM
38361+ args.writef = au_sbi(sb)->si_xwrite;
38362+ for (bindex = 0; bindex <= bbot; bindex++) {
38363+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38364+ args.br = au_sbr(sb, bindex);
38365+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38366+ err = au_xino_do_set_br(sb, path, &args);
b752ccd1 38367+ if (unlikely(err))
062440b3 38368+ break;
b752ccd1 38369+ }
1facf9fc 38370+
062440b3 38371+ AuTraceErr(err);
1facf9fc 38372+ return err;
38373+}
b752ccd1
AM
38374+
38375+void au_xino_clr(struct super_block *sb)
38376+{
38377+ struct au_sbinfo *sbinfo;
38378+
38379+ au_xigen_clr(sb);
38380+ xino_clear_xib(sb);
38381+ xino_clear_br(sb);
062440b3 38382+ dbgaufs_brs_del(sb, 0);
b752ccd1
AM
38383+ sbinfo = au_sbi(sb);
38384+ /* lvalue, do not call au_mntflags() */
38385+ au_opt_clr(sbinfo->si_mntflags, XINO);
38386+}
38387+
062440b3 38388+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
b752ccd1
AM
38389+{
38390+ int err, skip;
062440b3 38391+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
b752ccd1
AM
38392+ struct qstr *dname, *cur_name;
38393+ struct file *cur_xino;
b752ccd1 38394+ struct au_sbinfo *sbinfo;
062440b3 38395+ struct path *path, *cur_path;
b752ccd1
AM
38396+
38397+ SiMustWriteLock(sb);
38398+
38399+ err = 0;
38400+ sbinfo = au_sbi(sb);
062440b3
AM
38401+ path = &xiopt->file->f_path;
38402+ dentry = path->dentry;
38403+ parent = dget_parent(dentry);
b752ccd1
AM
38404+ if (remount) {
38405+ skip = 0;
b752ccd1
AM
38406+ cur_xino = sbinfo->si_xib;
38407+ if (cur_xino) {
062440b3
AM
38408+ cur_path = &cur_xino->f_path;
38409+ cur_dentry = cur_path->dentry;
38410+ cur_parent = dget_parent(cur_dentry);
38411+ cur_name = &cur_dentry->d_name;
38412+ dname = &dentry->d_name;
b752ccd1 38413+ skip = (cur_parent == parent
38d290e6 38414+ && au_qstreq(dname, cur_name));
b752ccd1
AM
38415+ dput(cur_parent);
38416+ }
38417+ if (skip)
38418+ goto out;
38419+ }
38420+
38421+ au_opt_set(sbinfo->si_mntflags, XINO);
062440b3
AM
38422+ err = au_xino_set_xib(sb, path);
38423+ /* si_x{read,write} are set */
b752ccd1 38424+ if (!err)
062440b3 38425+ err = au_xigen_set(sb, path);
b752ccd1 38426+ if (!err)
062440b3
AM
38427+ err = au_xino_set_br(sb, path);
38428+ if (!err) {
38429+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
b752ccd1 38430+ goto out; /* success */
062440b3 38431+ }
b752ccd1
AM
38432+
38433+ /* reset all */
062440b3
AM
38434+ AuIOErr("failed setting xino(%d).\n", err);
38435+ au_xino_clr(sb);
b752ccd1 38436+
4f0767ce 38437+out:
b752ccd1
AM
38438+ dput(parent);
38439+ return err;
38440+}
38441+
b752ccd1
AM
38442+/*
38443+ * create a xinofile at the default place/path.
38444+ */
38445+struct file *au_xino_def(struct super_block *sb)
38446+{
38447+ struct file *file;
38448+ char *page, *p;
38449+ struct au_branch *br;
38450+ struct super_block *h_sb;
38451+ struct path path;
5afbbe0d 38452+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
38453+
38454+ br = NULL;
5afbbe0d 38455+ bbot = au_sbbot(sb);
b752ccd1 38456+ bwr = -1;
5afbbe0d 38457+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
38458+ br = au_sbr(sb, bindex);
38459+ if (au_br_writable(br->br_perm)
86dc4139 38460+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
38461+ bwr = bindex;
38462+ break;
38463+ }
38464+ }
38465+
7f207e10
AM
38466+ if (bwr >= 0) {
38467+ file = ERR_PTR(-ENOMEM);
537831f9 38468+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
38469+ if (unlikely(!page))
38470+ goto out;
86dc4139 38471+ path.mnt = au_br_mnt(br);
7f207e10
AM
38472+ path.dentry = au_h_dptr(sb->s_root, bwr);
38473+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38474+ file = (void *)p;
38475+ if (!IS_ERR(p)) {
38476+ strcat(p, "/" AUFS_XINO_FNAME);
38477+ AuDbg("%s\n", p);
83b672a5 38478+ file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
7f207e10 38479+ }
1c60b727 38480+ free_page((unsigned long)page);
7f207e10 38481+ } else {
83b672a5
AM
38482+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38483+ /*wbrtop*/0);
7f207e10
AM
38484+ if (IS_ERR(file))
38485+ goto out;
2000de60 38486+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
38487+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38488+ pr_err("xino doesn't support %s(%s)\n",
38489+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38490+ fput(file);
38491+ file = ERR_PTR(-EINVAL);
38492+ }
7f207e10 38493+ }
0c5527e5 38494+
7f207e10
AM
38495+out:
38496+ return file;
38497+}
38498+
38499+/* ---------------------------------------------------------------------- */
38500+
062440b3
AM
38501+/*
38502+ * initialize the xinofile for the specified branch @br
38503+ * at the place/path where @base_file indicates.
38504+ * test whether another branch is on the same filesystem or not,
38505+ * if found then share the xinofile with another branch.
38506+ */
38507+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38508+ struct path *base)
7f207e10
AM
38509+{
38510+ int err;
062440b3
AM
38511+ struct au_xino_do_set_br args = {
38512+ .h_ino = h_ino,
38513+ .br = br
38514+ };
7f207e10 38515+
062440b3
AM
38516+ args.writef = au_sbi(sb)->si_xwrite;
38517+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38518+ au_br_sb(br));
38519+ err = au_xino_do_set_br(sb, base, &args);
79b8bda9 38520+ if (unlikely(err))
062440b3 38521+ au_xino_put(br);
7f207e10 38522+
7f207e10
AM
38523+ return err;
38524+}
521ced18
JR
38525+
38526+/* ---------------------------------------------------------------------- */
38527+
062440b3
AM
38528+/*
38529+ * get an unused inode number from bitmap
38530+ */
38531+ino_t au_xino_new_ino(struct super_block *sb)
38532+{
38533+ ino_t ino;
38534+ unsigned long *p, pindex, ul, pend;
38535+ struct au_sbinfo *sbinfo;
38536+ struct file *file;
38537+ int free_bit, err;
38538+
38539+ if (!au_opt_test(au_mntflags(sb), XINO))
38540+ return iunique(sb, AUFS_FIRST_INO);
38541+
38542+ sbinfo = au_sbi(sb);
38543+ mutex_lock(&sbinfo->si_xib_mtx);
38544+ p = sbinfo->si_xib_buf;
38545+ free_bit = sbinfo->si_xib_next_bit;
38546+ if (free_bit < page_bits && !test_bit(free_bit, p))
38547+ goto out; /* success */
38548+ free_bit = find_first_zero_bit(p, page_bits);
38549+ if (free_bit < page_bits)
38550+ goto out; /* success */
38551+
38552+ pindex = sbinfo->si_xib_last_pindex;
38553+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38554+ err = xib_pindex(sb, ul);
38555+ if (unlikely(err))
38556+ goto out_err;
38557+ free_bit = find_first_zero_bit(p, page_bits);
38558+ if (free_bit < page_bits)
38559+ goto out; /* success */
38560+ }
38561+
38562+ file = sbinfo->si_xib;
38563+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38564+ for (ul = pindex + 1; ul <= pend; ul++) {
38565+ err = xib_pindex(sb, ul);
38566+ if (unlikely(err))
38567+ goto out_err;
38568+ free_bit = find_first_zero_bit(p, page_bits);
38569+ if (free_bit < page_bits)
38570+ goto out; /* success */
38571+ }
38572+ BUG();
38573+
38574+out:
38575+ set_bit(free_bit, p);
38576+ sbinfo->si_xib_next_bit = free_bit + 1;
38577+ pindex = sbinfo->si_xib_last_pindex;
38578+ mutex_unlock(&sbinfo->si_xib_mtx);
38579+ ino = xib_calc_ino(pindex, free_bit);
38580+ AuDbg("i%lu\n", (unsigned long)ino);
38581+ return ino;
38582+out_err:
38583+ mutex_unlock(&sbinfo->si_xib_mtx);
38584+ AuDbg("i0\n");
38585+ return 0;
38586+}
38587+
38588+/* for s_op->delete_inode() */
38589+void au_xino_delete_inode(struct inode *inode, const int unlinked)
521ced18 38590+{
062440b3
AM
38591+ int err;
38592+ unsigned int mnt_flags;
38593+ aufs_bindex_t bindex, bbot, bi;
38594+ unsigned char try_trunc;
38595+ struct au_iinfo *iinfo;
38596+ struct super_block *sb;
38597+ struct au_hinode *hi;
38598+ struct inode *h_inode;
38599+ struct au_branch *br;
38600+ vfs_writef_t xwrite;
acd2b654
AM
38601+ struct au_xi_calc calc;
38602+ struct file *file;
521ced18 38603+
062440b3 38604+ AuDebugOn(au_is_bad_inode(inode));
521ced18 38605+
062440b3
AM
38606+ sb = inode->i_sb;
38607+ mnt_flags = au_mntflags(sb);
38608+ if (!au_opt_test(mnt_flags, XINO)
38609+ || inode->i_ino == AUFS_ROOT_INO)
38610+ return;
38611+
38612+ if (unlinked) {
38613+ au_xigen_inc(inode);
38614+ au_xib_clear_bit(inode);
38615+ }
38616+
38617+ iinfo = au_ii(inode);
38618+ bindex = iinfo->ii_btop;
38619+ if (bindex < 0)
38620+ return;
38621+
38622+ xwrite = au_sbi(sb)->si_xwrite;
38623+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38624+ hi = au_hinode(iinfo, bindex);
38625+ bbot = iinfo->ii_bbot;
38626+ for (; bindex <= bbot; bindex++, hi++) {
38627+ h_inode = hi->hi_inode;
38628+ if (!h_inode
38629+ || (!unlinked && h_inode->i_nlink))
38630+ continue;
38631+
38632+ /* inode may not be revalidated */
38633+ bi = au_br_index(sb, hi->hi_id);
38634+ if (bi < 0)
38635+ continue;
38636+
38637+ br = au_sbr(sb, bi);
acd2b654
AM
38638+ au_xi_calc(sb, h_inode->i_ino, &calc);
38639+ file = au_xino_file(br->br_xino, calc.idx);
38640+ if (IS_ERR_OR_NULL(file))
38641+ continue;
38642+
38643+ err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
062440b3
AM
38644+ if (!err && try_trunc
38645+ && au_test_fs_trunc_xino(au_br_sb(br)))
38646+ xino_try_trunc(sb, br);
38647+ }
521ced18
JR
38648+}
38649+
062440b3
AM
38650+/* ---------------------------------------------------------------------- */
38651+
38652+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
521ced18
JR
38653+{
38654+ int found, total, i;
38655+
38656+ found = -1;
062440b3 38657+ total = xi->xi_nondir.total;
521ced18 38658+ for (i = 0; i < total; i++) {
062440b3 38659+ if (xi->xi_nondir.array[i] != h_ino)
521ced18
JR
38660+ continue;
38661+ found = i;
38662+ break;
38663+ }
38664+
38665+ return found;
38666+}
38667+
062440b3 38668+static int au_xinondir_expand(struct au_xino *xi)
521ced18
JR
38669+{
38670+ int err, sz;
38671+ ino_t *p;
38672+
38673+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38674+
38675+ err = -ENOMEM;
062440b3 38676+ sz = xi->xi_nondir.total * sizeof(ino_t);
521ced18
JR
38677+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38678+ goto out;
062440b3 38679+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
521ced18
JR
38680+ /*may_shrink*/0);
38681+ if (p) {
062440b3
AM
38682+ xi->xi_nondir.array = p;
38683+ xi->xi_nondir.total <<= 1;
38684+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
521ced18
JR
38685+ err = 0;
38686+ }
38687+
38688+out:
38689+ return err;
38690+}
38691+
062440b3
AM
38692+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38693+ ino_t h_ino, int idx)
38694+{
38695+ struct au_xino *xi;
38696+
38697+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38698+ xi = au_sbr(sb, bindex)->br_xino;
38699+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38700+
38701+ spin_lock(&xi->xi_nondir.spin);
38702+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38703+ xi->xi_nondir.array[idx] = 0;
38704+ spin_unlock(&xi->xi_nondir.spin);
38705+ wake_up_all(&xi->xi_nondir.wqh);
38706+}
38707+
521ced18
JR
38708+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38709+ int *idx)
38710+{
38711+ int err, found, empty;
062440b3 38712+ struct au_xino *xi;
521ced18
JR
38713+
38714+ err = 0;
38715+ *idx = -1;
38716+ if (!au_opt_test(au_mntflags(sb), XINO))
38717+ goto out; /* no xino */
38718+
062440b3 38719+ xi = au_sbr(sb, bindex)->br_xino;
521ced18
JR
38720+
38721+again:
062440b3
AM
38722+ spin_lock(&xi->xi_nondir.spin);
38723+ found = au_xinondir_find(xi, h_ino);
521ced18 38724+ if (found == -1) {
062440b3 38725+ empty = au_xinondir_find(xi, /*h_ino*/0);
521ced18 38726+ if (empty == -1) {
062440b3
AM
38727+ empty = xi->xi_nondir.total;
38728+ err = au_xinondir_expand(xi);
521ced18
JR
38729+ if (unlikely(err))
38730+ goto out_unlock;
38731+ }
062440b3 38732+ xi->xi_nondir.array[empty] = h_ino;
521ced18
JR
38733+ *idx = empty;
38734+ } else {
062440b3
AM
38735+ spin_unlock(&xi->xi_nondir.spin);
38736+ wait_event(xi->xi_nondir.wqh,
38737+ xi->xi_nondir.array[found] != h_ino);
521ced18
JR
38738+ goto again;
38739+ }
38740+
38741+out_unlock:
062440b3
AM
38742+ spin_unlock(&xi->xi_nondir.spin);
38743+out:
38744+ return err;
38745+}
38746+
38747+/* ---------------------------------------------------------------------- */
38748+
38749+int au_xino_path(struct seq_file *seq, struct file *file)
38750+{
38751+ int err;
38752+
38753+ err = au_seq_path(seq, &file->f_path);
38754+ if (unlikely(err))
38755+ goto out;
38756+
38757+#define Deleted "\\040(deleted)"
38758+ seq->count -= sizeof(Deleted) - 1;
38759+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38760+ sizeof(Deleted) - 1));
38761+#undef Deleted
38762+
521ced18
JR
38763+out:
38764+ return err;
38765+}
537831f9 38766diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
eca34b5c 38767--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
016522bc 38768+++ linux/include/uapi/linux/aufs_type.h 2020-04-03 08:16:49.834195677 +0200
eca34b5c
AM
38769@@ -0,0 +1,452 @@
38770+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
7f207e10 38771+/*
016522bc 38772+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7f207e10
AM
38773+ *
38774+ * This program, aufs is free software; you can redistribute it and/or modify
38775+ * it under the terms of the GNU General Public License as published by
38776+ * the Free Software Foundation; either version 2 of the License, or
38777+ * (at your option) any later version.
38778+ *
38779+ * This program is distributed in the hope that it will be useful,
38780+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38781+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38782+ * GNU General Public License for more details.
38783+ *
38784+ * You should have received a copy of the GNU General Public License
523b37e3 38785+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
38786+ */
38787+
38788+#ifndef __AUFS_TYPE_H__
38789+#define __AUFS_TYPE_H__
38790+
f6c5ef8b
AM
38791+#define AUFS_NAME "aufs"
38792+
9dbd164d 38793+#ifdef __KERNEL__
f6c5ef8b
AM
38794+/*
38795+ * define it before including all other headers.
38796+ * sched.h may use pr_* macros before defining "current", so define the
38797+ * no-current version first, and re-define later.
38798+ */
38799+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38800+#include <linux/sched.h>
38801+#undef pr_fmt
a2a7ad62
AM
38802+#define pr_fmt(fmt) \
38803+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38804+ (int)sizeof(current->comm), current->comm, current->pid
43982f53 38805+#include <linux/limits.h>
9dbd164d
AM
38806+#else
38807+#include <stdint.h>
38808+#include <sys/types.h>
43982f53 38809+#include <limits.h>
f6c5ef8b 38810+#endif /* __KERNEL__ */
7f207e10 38811+
016522bc 38812+#define AUFS_VERSION "5.4-20200302"
7f207e10
AM
38813+
38814+/* todo? move this to linux-2.6.19/include/magic.h */
38815+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38816+
38817+/* ---------------------------------------------------------------------- */
38818+
43982f53 38819+#ifdef __KERNEL__
7f207e10 38820+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 38821+typedef int8_t aufs_bindex_t;
7f207e10
AM
38822+#define AUFS_BRANCH_MAX 127
38823+#else
9dbd164d 38824+typedef int16_t aufs_bindex_t;
7f207e10
AM
38825+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38826+#define AUFS_BRANCH_MAX 511
38827+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38828+#define AUFS_BRANCH_MAX 1023
38829+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38830+#define AUFS_BRANCH_MAX 32767
38831+#endif
38832+#endif
38833+
7f207e10
AM
38834+#ifndef AUFS_BRANCH_MAX
38835+#error unknown CONFIG_AUFS_BRANCH_MAX value
38836+#endif
38837+#endif /* __KERNEL__ */
38838+
38839+/* ---------------------------------------------------------------------- */
38840+
7f207e10
AM
38841+#define AUFS_FSTYPE AUFS_NAME
38842+
38843+#define AUFS_ROOT_INO 2
38844+#define AUFS_FIRST_INO 11
38845+
38846+#define AUFS_WH_PFX ".wh."
38847+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38848+#define AUFS_WH_TMP_LEN 4
86dc4139 38849+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
38850+#define AUFS_MAX_NAMELEN (NAME_MAX \
38851+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38852+ - 1 /* dot */\
38853+ - AUFS_WH_TMP_LEN) /* hex */
38854+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38855+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
38856+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38857+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
38858+#define AUFS_DIRWH_DEF 3
38859+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 38860+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
38861+#define AUFS_RDBLK_DEF 512 /* bytes */
38862+#define AUFS_RDHASH_DEF 32
38863+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
38864+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38865+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 38866+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 38867+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
38868+
38869+/* pseudo-link maintenace under /proc */
38870+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38871+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38872+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38873+
8b6a4947
AM
38874+/* dirren, renamed dir */
38875+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38876+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38877+/* whiteouted doubly */
38878+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38879+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38880+
7f207e10
AM
38881+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38882+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38883+
38884+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38885+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38886+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38887+
38888+/* doubly whiteouted */
38889+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38890+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38891+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38892+
1e00d052 38893+/* branch permissions and attributes */
7f207e10
AM
38894+#define AUFS_BRPERM_RW "rw"
38895+#define AUFS_BRPERM_RO "ro"
38896+#define AUFS_BRPERM_RR "rr"
076b876e
AM
38897+#define AUFS_BRATTR_COO_REG "coo_reg"
38898+#define AUFS_BRATTR_COO_ALL "coo_all"
38899+#define AUFS_BRATTR_FHSM "fhsm"
38900+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
38901+#define AUFS_BRATTR_ICEX "icex"
38902+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38903+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38904+#define AUFS_BRATTR_ICEX_TR "icextr"
38905+#define AUFS_BRATTR_ICEX_USR "icexusr"
38906+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
38907+#define AUFS_BRRATTR_WH "wh"
38908+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
38909+#define AUFS_BRWATTR_MOO "moo"
38910+
38911+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38912+#define AuBrPerm_RO (1 << 1) /* readonly */
38913+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38914+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38915+
38916+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38917+#define AuBrAttr_COO_ALL (1 << 4)
38918+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38919+
38920+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38921+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
38922+ branch. meaningless since
38923+ linux-3.18-rc1 */
38924+
38925+/* ignore error in copying XATTR */
38926+#define AuBrAttr_ICEX_SEC (1 << 7)
38927+#define AuBrAttr_ICEX_SYS (1 << 8)
38928+#define AuBrAttr_ICEX_TR (1 << 9)
38929+#define AuBrAttr_ICEX_USR (1 << 10)
38930+#define AuBrAttr_ICEX_OTH (1 << 11)
38931+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38932+ | AuBrAttr_ICEX_SYS \
38933+ | AuBrAttr_ICEX_TR \
38934+ | AuBrAttr_ICEX_USR \
38935+ | AuBrAttr_ICEX_OTH)
38936+
38937+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
38938+#define AuBrRAttr_Mask AuBrRAttr_WH
38939+
c1595e42
JR
38940+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
38941+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
38942+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38943+
38944+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38945+
c1595e42 38946+/* #warning test userspace */
076b876e
AM
38947+#ifdef __KERNEL__
38948+#ifndef CONFIG_AUFS_FHSM
38949+#undef AuBrAttr_FHSM
38950+#define AuBrAttr_FHSM 0
38951+#endif
c1595e42
JR
38952+#ifndef CONFIG_AUFS_XATTR
38953+#undef AuBrAttr_ICEX
38954+#define AuBrAttr_ICEX 0
38955+#undef AuBrAttr_ICEX_SEC
38956+#define AuBrAttr_ICEX_SEC 0
38957+#undef AuBrAttr_ICEX_SYS
38958+#define AuBrAttr_ICEX_SYS 0
38959+#undef AuBrAttr_ICEX_TR
38960+#define AuBrAttr_ICEX_TR 0
38961+#undef AuBrAttr_ICEX_USR
38962+#define AuBrAttr_ICEX_USR 0
38963+#undef AuBrAttr_ICEX_OTH
38964+#define AuBrAttr_ICEX_OTH 0
38965+#endif
076b876e
AM
38966+#endif
38967+
38968+/* the longest combination */
c1595e42
JR
38969+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38970+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
38971+ "+" AUFS_BRATTR_COO_REG \
38972+ "+" AUFS_BRATTR_FHSM \
38973+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
38974+ "+" AUFS_BRATTR_ICEX_SEC \
38975+ "+" AUFS_BRATTR_ICEX_SYS \
38976+ "+" AUFS_BRATTR_ICEX_USR \
38977+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
38978+ "+" AUFS_BRWATTR_NLWH)
38979+
38980+typedef struct {
38981+ char a[AuBrPermStrSz];
38982+} au_br_perm_str_t;
38983+
38984+static inline int au_br_writable(int brperm)
38985+{
38986+ return brperm & AuBrPerm_RW;
38987+}
38988+
38989+static inline int au_br_whable(int brperm)
38990+{
38991+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38992+}
38993+
38994+static inline int au_br_wh_linkable(int brperm)
38995+{
38996+ return !(brperm & AuBrWAttr_NoLinkWH);
38997+}
38998+
38999+static inline int au_br_cmoo(int brperm)
39000+{
39001+ return brperm & AuBrAttr_CMOO_Mask;
39002+}
39003+
39004+static inline int au_br_fhsm(int brperm)
39005+{
39006+ return brperm & AuBrAttr_FHSM;
39007+}
7f207e10
AM
39008+
39009+/* ---------------------------------------------------------------------- */
39010+
39011+/* ioctl */
39012+enum {
39013+ /* readdir in userspace */
39014+ AuCtl_RDU,
39015+ AuCtl_RDU_INO,
39016+
076b876e
AM
39017+ AuCtl_WBR_FD, /* pathconf wrapper */
39018+ AuCtl_IBUSY, /* busy inode */
39019+ AuCtl_MVDOWN, /* move-down */
39020+ AuCtl_BR, /* info about branches */
39021+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
39022+};
39023+
39024+/* borrowed from linux/include/linux/kernel.h */
39025+#ifndef ALIGN
eca34b5c 39026+#ifdef _GNU_SOURCE
7f207e10 39027+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
eca34b5c
AM
39028+#else
39029+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
39030+#endif
7f207e10
AM
39031+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
39032+#endif
39033+
39034+/* borrowed from linux/include/linux/compiler-gcc3.h */
39035+#ifndef __aligned
39036+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
39037+#endif
39038+
39039+#ifdef __KERNEL__
39040+#ifndef __packed
7f207e10
AM
39041+#define __packed __attribute__((packed))
39042+#endif
53392da6 39043+#endif
7f207e10
AM
39044+
39045+struct au_rdu_cookie {
9dbd164d
AM
39046+ uint64_t h_pos;
39047+ int16_t bindex;
39048+ uint8_t flags;
39049+ uint8_t pad;
39050+ uint32_t generation;
7f207e10
AM
39051+} __aligned(8);
39052+
39053+struct au_rdu_ent {
9dbd164d
AM
39054+ uint64_t ino;
39055+ int16_t bindex;
39056+ uint8_t type;
39057+ uint8_t nlen;
39058+ uint8_t wh;
7f207e10
AM
39059+ char name[0];
39060+} __aligned(8);
39061+
39062+static inline int au_rdu_len(int nlen)
39063+{
39064+ /* include the terminating NULL */
39065+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 39066+ sizeof(uint64_t));
7f207e10
AM
39067+}
39068+
39069+union au_rdu_ent_ul {
39070+ struct au_rdu_ent __user *e;
9dbd164d 39071+ uint64_t ul;
7f207e10
AM
39072+};
39073+
39074+enum {
39075+ AufsCtlRduV_SZ,
39076+ AufsCtlRduV_End
39077+};
39078+
39079+struct aufs_rdu {
39080+ /* input */
39081+ union {
9dbd164d
AM
39082+ uint64_t sz; /* AuCtl_RDU */
39083+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
39084+ };
39085+ union au_rdu_ent_ul ent;
9dbd164d 39086+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
39087+
39088+ /* input/output */
9dbd164d 39089+ uint32_t blk;
7f207e10
AM
39090+
39091+ /* output */
39092+ union au_rdu_ent_ul tail;
39093+ /* number of entries which were added in a single call */
9dbd164d
AM
39094+ uint64_t rent;
39095+ uint8_t full;
39096+ uint8_t shwh;
7f207e10
AM
39097+
39098+ struct au_rdu_cookie cookie;
39099+} __aligned(8);
39100+
1e00d052
AM
39101+/* ---------------------------------------------------------------------- */
39102+
8b6a4947
AM
39103+/* dirren. the branch is identified by the filename who contains this */
39104+struct au_drinfo {
39105+ uint64_t ino;
39106+ union {
39107+ uint8_t oldnamelen;
39108+ uint64_t _padding;
39109+ };
39110+ uint8_t oldname[0];
39111+} __aligned(8);
39112+
39113+struct au_drinfo_fdata {
39114+ uint32_t magic;
39115+ struct au_drinfo drinfo;
39116+} __aligned(8);
39117+
39118+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39119+/* future */
39120+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39121+
39122+/* ---------------------------------------------------------------------- */
39123+
1e00d052 39124+struct aufs_wbr_fd {
9dbd164d
AM
39125+ uint32_t oflags;
39126+ int16_t brid;
1e00d052
AM
39127+} __aligned(8);
39128+
39129+/* ---------------------------------------------------------------------- */
39130+
027c5e7a 39131+struct aufs_ibusy {
9dbd164d
AM
39132+ uint64_t ino, h_ino;
39133+ int16_t bindex;
027c5e7a
AM
39134+} __aligned(8);
39135+
1e00d052
AM
39136+/* ---------------------------------------------------------------------- */
39137+
392086de
AM
39138+/* error code for move-down */
39139+/* the actual message strings are implemented in aufs-util.git */
39140+enum {
39141+ EAU_MVDOWN_OPAQUE = 1,
39142+ EAU_MVDOWN_WHITEOUT,
39143+ EAU_MVDOWN_UPPER,
39144+ EAU_MVDOWN_BOTTOM,
39145+ EAU_MVDOWN_NOUPPER,
39146+ EAU_MVDOWN_NOLOWERBR,
39147+ EAU_Last
39148+};
39149+
c2b27bf2 39150+/* flags for move-down */
392086de
AM
39151+#define AUFS_MVDOWN_DMSG 1
39152+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39153+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39154+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39155+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39156+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39157+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39158+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39159+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
39160+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39161+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39162+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39163+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 39164+
076b876e 39165+/* index for move-down */
392086de
AM
39166+enum {
39167+ AUFS_MVDOWN_UPPER,
39168+ AUFS_MVDOWN_LOWER,
39169+ AUFS_MVDOWN_NARRAY
39170+};
39171+
076b876e
AM
39172+/*
39173+ * additional info of move-down
39174+ * number of free blocks and inodes.
39175+ * subset of struct kstatfs, but smaller and always 64bit.
39176+ */
39177+struct aufs_stfs {
39178+ uint64_t f_blocks;
39179+ uint64_t f_bavail;
39180+ uint64_t f_files;
39181+ uint64_t f_ffree;
39182+};
39183+
39184+struct aufs_stbr {
39185+ int16_t brid; /* optional input */
39186+ int16_t bindex; /* output */
39187+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39188+} __aligned(8);
39189+
c2b27bf2 39190+struct aufs_mvdown {
076b876e
AM
39191+ uint32_t flags; /* input/output */
39192+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39193+ int8_t au_errno; /* output */
39194+} __aligned(8);
39195+
39196+/* ---------------------------------------------------------------------- */
39197+
39198+union aufs_brinfo {
39199+ /* PATH_MAX may differ between kernel-space and user-space */
39200+ char _spacer[4096];
392086de 39201+ struct {
076b876e
AM
39202+ int16_t id;
39203+ int perm;
39204+ char path[0];
39205+ };
c2b27bf2
AM
39206+} __aligned(8);
39207+
39208+/* ---------------------------------------------------------------------- */
39209+
7f207e10
AM
39210+#define AuCtlType 'A'
39211+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39212+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
39213+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39214+ struct aufs_wbr_fd)
027c5e7a 39215+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
39216+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39217+ struct aufs_mvdown)
076b876e
AM
39218+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39219+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
39220+
39221+#endif /* __AUFS_TYPE_H__ */
2121bcd9 39222SPDX-License-Identifier: GPL-2.0
43982f53 39223aufs5.4 loopback patch
5527c038
JR
39224
39225diff --git a/drivers/block/loop.c b/drivers/block/loop.c
43982f53 39226index 5e094699215e..22b2ecb6cfe8 100644
5527c038
JR
39227--- a/drivers/block/loop.c
39228+++ b/drivers/block/loop.c
eca34b5c 39229@@ -625,6 +625,15 @@ static inline void loop_update_dio(struct loop_device *lo)
8b6a4947 39230 lo->use_dio);
5527c038
JR
39231 }
39232
5527c038
JR
39233+static struct file *loop_real_file(struct file *file)
39234+{
39235+ struct file *f = NULL;
39236+
39237+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39238+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39239+ return f;
8b6a4947
AM
39240+}
39241+
c2c0f25c 39242 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
39243 struct block_device *bdev)
39244 {
eca34b5c 39245@@ -678,6 +687,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
39246 unsigned int arg)
39247 {
e1106418 39248 struct file *file = NULL, *old_file;
5527c038 39249+ struct file *f, *virt_file = NULL, *old_virt_file;
5527c038 39250 int error;
ba1aed25 39251 bool partscan;
5527c038 39252
eca34b5c 39253@@ -697,12 +707,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
cd7a4cd9
AM
39254 file = fget(arg);
39255 if (!file)
e1106418 39256 goto out_err;
5527c038
JR
39257+ f = loop_real_file(file);
39258+ if (f) {
39259+ virt_file = file;
39260+ file = f;
39261+ get_file(file);
39262+ }
39263
cd7a4cd9
AM
39264 error = loop_validate_file(file, bdev);
39265 if (error)
e1106418 39266 goto out_err;
cd7a4cd9 39267
5527c038
JR
39268 old_file = lo->lo_backing_file;
39269+ old_virt_file = lo->lo_backing_virt_file;
39270
39271 error = -EINVAL;
39272
eca34b5c 39273@@ -714,6 +731,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
39274 blk_mq_freeze_queue(lo->lo_queue);
39275 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39276 lo->lo_backing_file = file;
39277+ lo->lo_backing_virt_file = virt_file;
39278 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39279 mapping_set_gfp_mask(file->f_mapping,
39280 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
eca34b5c 39281@@ -727,6 +745,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
e1106418
JR
39282 * dependency.
39283 */
5527c038
JR
39284 fput(old_file);
39285+ if (old_virt_file)
39286+ fput(old_virt_file);
e1106418 39287 if (partscan)
c2c0f25c 39288 loop_reread_partitions(lo, bdev);
5527c038 39289 return 0;
eca34b5c 39290@@ -735,6 +755,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
e1106418
JR
39291 mutex_unlock(&loop_ctl_mutex);
39292 if (file)
39293 fput(file);
5527c038
JR
39294+ if (virt_file)
39295+ fput(virt_file);
5527c038
JR
39296 return error;
39297 }
e1106418 39298
eca34b5c 39299@@ -939,7 +961,7 @@ static void loop_update_rotational(struct loop_device *lo)
5527c038
JR
39300 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39301 struct block_device *bdev, unsigned int arg)
39302 {
062440b3 39303- struct file *file;
5527c038
JR
39304+ struct file *file, *f, *virt_file = NULL;
39305 struct inode *inode;
39306 struct address_space *mapping;
eca34b5c
AM
39307 struct block_device *claimed_bdev = NULL;
39308@@ -955,6 +977,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39309 file = fget(arg);
39310 if (!file)
39311 goto out;
39312+ f = loop_real_file(file);
39313+ if (f) {
39314+ virt_file = file;
39315+ file = f;
39316+ get_file(file);
39317+ }
39318
fbc438ed
JR
39319 /*
39320 * If we don't hold exclusive handle for the device, upgrade to it
eca34b5c 39321@@ -1003,6 +1031,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39322 lo->lo_device = bdev;
39323 lo->lo_flags = lo_flags;
39324 lo->lo_backing_file = file;
39325+ lo->lo_backing_virt_file = virt_file;
39326 lo->transfer = NULL;
39327 lo->ioctl = NULL;
39328 lo->lo_sizelimit = 0;
43982f53 39329@@ -1056,6 +1085,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
eca34b5c 39330 bd_abort_claiming(bdev, claimed_bdev, loop_set_fd);
e1106418 39331 out_putf:
5527c038
JR
39332 fput(file);
39333+ if (virt_file)
39334+ fput(virt_file);
e1106418 39335 out:
5527c038
JR
39336 /* This is safe: open() is still holding a reference. */
39337 module_put(THIS_MODULE);
43982f53 39338@@ -1102,6 +1133,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
ba1aed25 39339 static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39340 {
e1106418 39341 struct file *filp = NULL;
5527c038
JR
39342+ struct file *virt_filp = lo->lo_backing_virt_file;
39343 gfp_t gfp = lo->old_gfp_mask;
39344 struct block_device *bdev = lo->lo_device;
ba1aed25 39345 int err = 0;
43982f53 39346@@ -1125,6 +1157,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39347
5527c038 39348 spin_lock_irq(&lo->lo_lock);
5527c038
JR
39349 lo->lo_backing_file = NULL;
39350+ lo->lo_backing_virt_file = NULL;
39351 spin_unlock_irq(&lo->lo_lock);
39352
39353 loop_release_xfer(lo);
43982f53 39354@@ -1207,6 +1240,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39355 */
e1106418
JR
39356 if (filp)
39357 fput(filp);
5527c038
JR
39358+ if (virt_filp)
39359+ fput(virt_filp);
e1106418 39360 return err;
5527c038
JR
39361 }
39362
39363diff --git a/drivers/block/loop.h b/drivers/block/loop.h
43982f53 39364index af75a5ee4094..1d847cb194ff 100644
5527c038
JR
39365--- a/drivers/block/loop.h
39366+++ b/drivers/block/loop.h
39367@@ -46,7 +46,7 @@ struct loop_device {
39368 int (*ioctl)(struct loop_device *, int cmd,
39369 unsigned long arg);
39370
39371- struct file * lo_backing_file;
43982f53 39372+ struct file *lo_backing_file, *lo_backing_virt_file;
5527c038 39373 struct block_device *lo_device;
5527c038 39374 void *key_data;
8b6a4947 39375
5527c038 39376diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
016522bc 39377index 6fb4a4ed8cc7..ba9a959f2db2 100644
5527c038
JR
39378--- a/fs/aufs/f_op.c
39379+++ b/fs/aufs/f_op.c
acd2b654 39380@@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
39381 if (IS_ERR(h_file))
39382 goto out;
39383
39384- if (au_test_loopback_kthread()) {
39385+ if (0 && au_test_loopback_kthread()) {
39386 au_warn_loopback(h_file->f_path.dentry->d_sb);
39387 if (file->f_mapping != h_file->f_mapping) {
39388 file->f_mapping = h_file->f_mapping;
39389diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
016522bc 39390index a8b63acc6204..9d97c3af5686 100644
5527c038
JR
39391--- a/fs/aufs/loop.c
39392+++ b/fs/aufs/loop.c
2121bcd9 39393@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 39394 symbol_put(loop_backing_file);
9f237c51 39395 au_kfree_try_rcu(au_warn_loopback_array);
5527c038
JR
39396 }
39397+
39398+/* ---------------------------------------------------------------------- */
39399+
39400+/* support the loopback block device insude aufs */
39401+
39402+struct file *aufs_real_loop(struct file *file)
39403+{
39404+ struct file *f;
39405+
39406+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39407+ fi_read_lock(file);
39408+ f = au_hf_top(file);
39409+ fi_read_unlock(file);
39410+ AuDebugOn(!f);
39411+ return f;
39412+}
39413diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
016522bc 39414index 94f4f80ae33b..ca1194354aff 100644
5527c038
JR
39415--- a/fs/aufs/loop.h
39416+++ b/fs/aufs/loop.h
eca801bf 39417@@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
39418
39419 int au_loopback_init(void);
39420 void au_loopback_fin(void);
39421+
39422+struct file *aufs_real_loop(struct file *file);
39423 #else
83b672a5 39424 AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
eca801bf
AM
39425
39426@@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
39427
39428 AuStubInt0(au_loopback_init, void)
39429 AuStubVoid(au_loopback_fin, void)
39430+
39431+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39432 #endif /* BLK_DEV_LOOP */
39433
39434 #endif /* __KERNEL__ */
39435diff --git a/fs/aufs/super.c b/fs/aufs/super.c
016522bc 39436index 589dd0122020..801e0a7faec5 100644
5527c038
JR
39437--- a/fs/aufs/super.c
39438+++ b/fs/aufs/super.c
fbc438ed 39439@@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
39440 .statfs = aufs_statfs,
39441 .put_super = aufs_put_super,
39442 .sync_fs = aufs_sync_fs,
39443- .remount_fs = aufs_remount_fs
39444+ .remount_fs = aufs_remount_fs,
39445+#ifdef CONFIG_AUFS_BDEV_LOOP
39446+ .real_loop = aufs_real_loop
39447+#endif
39448 };
39449
39450 /* ---------------------------------------------------------------------- */
39451diff --git a/include/linux/fs.h b/include/linux/fs.h
43982f53 39452index 381a13995011..215b76e0f9dc 100644
5527c038
JR
39453--- a/include/linux/fs.h
39454+++ b/include/linux/fs.h
43982f53 39455@@ -1973,6 +1973,10 @@ struct super_operations {
5527c038
JR
39456 struct shrink_control *);
39457 long (*free_cached_objects)(struct super_block *,
39458 struct shrink_control *);
43982f53 39459+#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
5527c038
JR
39460+ /* and aufs */
39461+ struct file *(*real_loop)(struct file *);
39462+#endif
39463 };
39464
39465 /*
This page took 7.25573 seconds and 4 git commands to generate.