]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs5.patch
- up to 5.4.13
[packages/kernel.git] / kernel-aufs5.patch
CommitLineData
2121bcd9 1SPDX-License-Identifier: GPL-2.0
fbc438ed 2aufs5.x-rcN kbuild patch
7f207e10 3
fbc438ed 4diff --git a/fs/Kconfig b/fs/Kconfig
eca34b5c 5index bfb1c6095c7a..bddba5bb307e 100644
fbc438ed
JR
6--- a/fs/Kconfig
7+++ b/fs/Kconfig
eca34b5c 8@@ -261,6 +261,7 @@ source "fs/romfs/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
eca34b5c 17index d60089fd689b..924c16a40b00 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
fbc438ed 26aufs5.x-rcN base patch
7f207e10 27
c1595e42 28diff --git a/MAINTAINERS b/MAINTAINERS
f9d78841 29index a50e97a63bc8..bd7c76319a85 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
eca34b5c 32@@ -2822,6 +2822,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
eca34b5c 53index ab7ca5989097..80d06084b043 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
fbc438ed 95index 3d40771e8e7c..d62303c5472a 100644
febd17d6
JR
96--- a/fs/fcntl.c
97+++ b/fs/fcntl.c
2121bcd9 98@@ -32,7 +32,7 @@
febd17d6
JR
99
100 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101
102-static int setfl(int fd, struct file * filp, unsigned long arg)
103+int setfl(int fd, struct file * filp, unsigned long arg)
104 {
105 struct inode * inode = file_inode(filp);
106 int error = 0;
2121bcd9 107@@ -63,6 +63,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
febd17d6
JR
108
109 if (filp->f_op->check_flags)
110 error = filp->f_op->check_flags(arg);
111+ if (!error && filp->f_op->setfl)
112+ error = filp->f_op->setfl(filp, arg);
113 if (error)
114 return error;
115
5afbbe0d 116diff --git a/fs/inode.c b/fs/inode.c
eca34b5c 117index 0f1e3b563c47..43df8452f97d 100644
5afbbe0d
AM
118--- a/fs/inode.c
119+++ b/fs/inode.c
fbc438ed 120@@ -1670,7 +1670,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
eca34b5c 130index d28d30b13043..34c8093ddb1d 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
eca34b5c 147index 5bbf587f5bc1..3265bb84f152 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;
162+ return ERR_PTR(-ENOSYS);
163+}
164+
165+vfs_writef_t vfs_writef(struct file *file)
166+{
167+ const struct file_operations *fop = file->f_op;
168+
169+ if (fop->write)
170+ return fop->write;
171+ if (fop->write_iter)
172+ return new_sync_write;
173+ return ERR_PTR(-ENOSYS);
174+}
175+
8b6a4947 176 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
5527c038
JR
177 {
178 mm_segment_t old_fs;
7f207e10 179diff --git a/fs/splice.c b/fs/splice.c
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
eca34b5c 221index 997a530ff4e9..3dbec51c2037 100644
5527c038
JR
222--- a/include/linux/fs.h
223+++ b/include/linux/fs.h
eca34b5c 224@@ -1331,6 +1331,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
228+extern int setfl(int fd, struct file * filp, unsigned long arg);
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);
eca34b5c 232@@ -1810,6 +1811,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);
eca34b5c 240@@ -1880,6 +1882,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 *);
eca34b5c 253@@ -2298,6 +2306,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;
eca34b5c 261@@ -2586,6 +2595,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
eca34b5c 270index 0b0d7259276d..12d18e180348 100644
8b6a4947
AM
271--- a/include/linux/lockdep.h
272+++ b/include/linux/lockdep.h
eca34b5c 273@@ -334,6 +334,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 *
eca34b5c 282@@ -476,6 +478,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
eca34b5c 325index 4861cf8e274b..ec87ccf82025 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
fbc438ed 346aufs5.x-rcN 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
eca34b5c 378index 731642e0f5a0..e8d6259de65b 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;
eca34b5c 393@@ -1807,7 +1810,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;
7e9cd9fe
AM
400 struct mm_walk walk = {
401 .hugetlb_entry = gather_hugetlb_stats,
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
eca34b5c 419index 0334ca97c584..3bdae823daee 100644
fb47a38f
JR
420--- a/include/linux/mm.h
421+++ b/include/linux/mm.h
eca34b5c 422@@ -1538,6 +1538,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
eca34b5c 452index 6a7a1083b6fb..461db8843e32 100644
fb47a38f
JR
453--- a/include/linux/mm_types.h
454+++ b/include/linux/mm_types.h
eca34b5c 455@@ -262,6 +262,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
eca34b5c 463@@ -336,6 +337,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
f9d78841 472index 541fd805fb88..3208fcf3b3c9 100644
fb47a38f
JR
473--- a/kernel/fork.c
474+++ b/kernel/fork.c
eca34b5c 475@@ -553,7 +553,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
eca34b5c 485index d0b295c3b764..6156cd3d1ab0 100644
076b876e
AM
486--- a/mm/Makefile
487+++ b/mm/Makefile
acd2b654 488@@ -39,7 +39,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
eca34b5c 498index d0cf700bf201..5cac93e7bf67 100644
fb47a38f
JR
499--- a/mm/filemap.c
500+++ b/mm/filemap.c
eca34b5c 501@@ -2721,7 +2721,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
fbc438ed 511index 7e8c3e8ae75f..d8a903f61262 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;
fbc438ed 523@@ -931,7 +931,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);
fbc438ed 532@@ -1847,8 +1847,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);
fbc438ed 542@@ -2680,7 +2680,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);
fbc438ed 551@@ -2699,7 +2699,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));
fbc438ed 560@@ -2890,7 +2890,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);
fbc438ed 569@@ -2965,10 +2965,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)
fbc438ed 598@@ -3258,7 +3275,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
eca34b5c 608index fed1b6e9c89b..0d43b248ed54 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
ba1aed25 653index 000000000000..024cdcfae1b1
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+ *
ba1aed25 665+ * Copyright (c) 2014-2019 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
fbc438ed 744aufs5.x-rcN 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
eca34b5c 767index f7f6a140856a..1a740f3c42ba 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
fbc438ed 779index d62303c5472a..d4ad34463848 100644
febd17d6
JR
780--- a/fs/fcntl.c
781+++ b/fs/fcntl.c
2121bcd9 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
fbc438ed 791index b07b53f24ff5..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.
fbc438ed 802@@ -374,6 +376,7 @@ void __fput_sync(struct file *file)
8cdd5066
JR
803 }
804
805 EXPORT_SYMBOL(fput);
febd17d6 806+EXPORT_SYMBOL_GPL(__fput_sync);
8cdd5066 807
79b8bda9 808 void __init files_init(void)
8b6a4947 809 {
5afbbe0d 810diff --git a/fs/inode.c b/fs/inode.c
eca34b5c 811index 43df8452f97d..8a5c14630ff0 100644
5afbbe0d
AM
812--- a/fs/inode.c
813+++ b/fs/inode.c
fbc438ed 814@@ -1679,6 +1679,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
5afbbe0d
AM
815
816 return update_time(inode, time, flags);
817 }
818+EXPORT_SYMBOL_GPL(update_time);
819
820 /**
821 * touch_atime - update the access time
7f207e10 822diff --git a/fs/namespace.c b/fs/namespace.c
eca34b5c 823index 34c8093ddb1d..2b64af58b643 100644
7f207e10
AM
824--- a/fs/namespace.c
825+++ b/fs/namespace.c
eca34b5c 826@@ -431,6 +431,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
c06a8ce3
AM
827 mnt_dec_writers(real_mount(mnt));
828 preempt_enable();
829 }
830+EXPORT_SYMBOL_GPL(__mnt_drop_write);
831
832 /**
833 * mnt_drop_write - give up write access to a mount
eca34b5c 834@@ -781,6 +782,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
8b6a4947
AM
835 {
836 return check_mnt(real_mount(mnt));
837 }
838+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
839
840 /*
841 * vfsmount lock must be held for write
eca34b5c 842@@ -1900,6 +1902,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
7f207e10
AM
843 }
844 return 0;
845 }
febd17d6 846+EXPORT_SYMBOL_GPL(iterate_mounts);
7f207e10 847
fbc438ed 848 static void lock_mnt_tree(struct mount *mnt)
7f207e10 849 {
9d41a5bf
AM
850--- linux-5.4/fs/notify/group.c~ 2019-11-25 01:32:01.000000000 +0100
851+++ linux-5.4/fs/notify/group.c 2019-11-26 20:14:34.917150671 +0100
852@@ -99,6 +99,7 @@ void fsnotify_get_group(struct fsnotify_
1716fcea 853 {
2121bcd9 854 refcount_inc(&group->refcnt);
1716fcea 855 }
febd17d6 856+EXPORT_SYMBOL_GPL(fsnotify_get_group);
1716fcea
AM
857
858 /*
859 * Drop a reference to a group. Free it if it's through.
7f207e10 860diff --git a/fs/open.c b/fs/open.c
eca34b5c 861index a59abe3c669a..78809163ba03 100644
7f207e10
AM
862--- a/fs/open.c
863+++ b/fs/open.c
fbc438ed 864@@ -65,6 +65,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
febd17d6 865 inode_unlock(dentry->d_inode);
7f207e10
AM
866 return ret;
867 }
febd17d6 868+EXPORT_SYMBOL_GPL(do_truncate);
7f207e10 869
5afbbe0d 870 long vfs_truncate(const struct path *path, loff_t length)
7f207e10 871 {
5527c038 872diff --git a/fs/read_write.c b/fs/read_write.c
eca34b5c 873index 3265bb84f152..5b2dbddb0efe 100644
5527c038
JR
874--- a/fs/read_write.c
875+++ b/fs/read_write.c
fbc438ed 876@@ -468,6 +468,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
8b6a4947
AM
877
878 return ret;
879 }
880+EXPORT_SYMBOL_GPL(vfs_read);
881
882 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
883 {
fbc438ed 884@@ -508,6 +509,7 @@ vfs_readf_t vfs_readf(struct file *file)
5527c038
JR
885 return new_sync_read;
886 return ERR_PTR(-ENOSYS);
887 }
febd17d6 888+EXPORT_SYMBOL_GPL(vfs_readf);
5527c038
JR
889
890 vfs_writef_t vfs_writef(struct file *file)
891 {
fbc438ed 892@@ -519,6 +521,7 @@ vfs_writef_t vfs_writef(struct file *file)
5527c038
JR
893 return new_sync_write;
894 return ERR_PTR(-ENOSYS);
895 }
febd17d6 896+EXPORT_SYMBOL_GPL(vfs_writef);
5527c038 897
8b6a4947
AM
898 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
899 {
fbc438ed 900@@ -588,6 +591,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
8b6a4947
AM
901
902 return ret;
903 }
904+EXPORT_SYMBOL_GPL(vfs_write);
905
fbc438ed
JR
906 /* file_ppos returns &file->f_pos or NULL if file is stream */
907 static inline loff_t *file_ppos(struct file *file)
7f207e10 908diff --git a/fs/splice.c b/fs/splice.c
eca34b5c 909index 75b489fcb66f..0a1f7498c22b 100644
7f207e10
AM
910--- a/fs/splice.c
911+++ b/fs/splice.c
fbc438ed 912@@ -847,6 +847,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
392086de
AM
913
914 return splice_write(pipe, out, ppos, len, flags);
7f207e10 915 }
febd17d6 916+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
917
918 /*
919 * Attempt to initiate a splice from a file to a pipe.
fbc438ed 920@@ -876,6 +877,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
7f207e10
AM
921
922 return splice_read(in, ppos, pipe, len, flags);
923 }
febd17d6 924+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
925
926 /**
927 * splice_direct_to_actor - splices data directly between two non-pipes
a2654f78 928diff --git a/fs/sync.c b/fs/sync.c
fbc438ed 929index 457f4e4a5cc1..67c66358f3fe 100644
a2654f78
AM
930--- a/fs/sync.c
931+++ b/fs/sync.c
2121bcd9 932@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
a2654f78
AM
933 sb->s_op->sync_fs(sb, wait);
934 return __sync_blockdev(sb->s_bdev, wait);
935 }
936+EXPORT_SYMBOL_GPL(__sync_filesystem);
937
938 /*
939 * Write out and wait upon all dirty data associated with this
c1595e42 940diff --git a/fs/xattr.c b/fs/xattr.c
fbc438ed 941index 90dd78f0eb27..40b01dd1b14a 100644
c1595e42
JR
942--- a/fs/xattr.c
943+++ b/fs/xattr.c
fbc438ed 944@@ -296,6 +296,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
c1595e42
JR
945 *xattr_value = value;
946 return error;
947 }
febd17d6 948+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 949
febd17d6 950 ssize_t
f2c43d5f 951 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947 952diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
eca34b5c 953index ec87ccf82025..739d1c8a81f5 100644
8b6a4947
AM
954--- a/kernel/locking/lockdep.c
955+++ b/kernel/locking/lockdep.c
eca34b5c
AM
956@@ -174,6 +174,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
957 */
958 return lock_classes + class_idx;
8b6a4947
AM
959 }
960+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
961 #define hlock_class(hlock) lockdep_hlock_class(hlock)
962
963 #ifdef CONFIG_LOCK_STAT
8cdd5066 964diff --git a/kernel/task_work.c b/kernel/task_work.c
ba1aed25 965index 0fef395662a6..83fb1ecfc33d 100644
8cdd5066
JR
966--- a/kernel/task_work.c
967+++ b/kernel/task_work.c
2121bcd9 968@@ -116,3 +116,4 @@ void task_work_run(void)
8cdd5066
JR
969 } while (work);
970 }
971 }
febd17d6 972+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 973diff --git a/security/device_cgroup.c b/security/device_cgroup.c
eca34b5c 974index 725674f3276d..83f6494c52a2 100644
7f207e10
AM
975--- a/security/device_cgroup.c
976+++ b/security/device_cgroup.c
83b672a5 977@@ -824,3 +824,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
7f207e10 978
2121bcd9
AM
979 return 0;
980 }
981+EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
7f207e10 982diff --git a/security/security.c b/security/security.c
eca34b5c 983index 250ee2d76406..2765042d4846 100644
7f207e10
AM
984--- a/security/security.c
985+++ b/security/security.c
eca34b5c 986@@ -996,6 +996,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 987 return 0;
c2c0f25c 988 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 989 }
febd17d6 990+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 991
5afbbe0d 992 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 993 {
eca34b5c 994@@ -1012,6 +1013,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 995 return 0;
c2c0f25c 996 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 997 }
febd17d6 998+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 999
5afbbe0d 1000 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1001 struct dentry *new_dentry)
eca34b5c 1002@@ -1020,6 +1022,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1003 return 0;
c2c0f25c 1004 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 1005 }
febd17d6 1006+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 1007
5afbbe0d
AM
1008 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1009 const struct path *new_dir, struct dentry *new_dentry,
eca34b5c 1010@@ -1047,6 +1050,7 @@ int security_path_truncate(const struct path *path)
7f207e10 1011 return 0;
c2c0f25c 1012 return call_int_hook(path_truncate, 0, path);
7f207e10 1013 }
febd17d6 1014+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 1015
5afbbe0d 1016 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 1017 {
eca34b5c 1018@@ -1054,6 +1058,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 1019 return 0;
c2c0f25c 1020 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 1021 }
febd17d6 1022+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 1023
5afbbe0d 1024 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1025 {
eca34b5c 1026@@ -1061,6 +1066,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1027 return 0;
c2c0f25c 1028 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 1029 }
febd17d6 1030+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 1031
5afbbe0d 1032 int security_path_chroot(const struct path *path)
7f207e10 1033 {
eca34b5c 1034@@ -1161,6 +1167,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 1035 return 0;
c2c0f25c 1036 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 1037 }
febd17d6 1038+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 1039
1e00d052 1040 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
7f207e10 1041 {
eca34b5c 1042@@ -1338,6 +1345,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
1043
1044 return fsnotify_perm(file, mask);
1045 }
febd17d6 1046+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
1047
1048 int security_file_alloc(struct file *file)
1049 {
7f207e10 1050diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
eca34b5c
AM
1051--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
1052+++ linux/Documentation/ABI/testing/debugfs-aufs 2019-07-11 15:42:14.455570938 +0200
062440b3 1053@@ -0,0 +1,55 @@
7f207e10
AM
1054+What: /debug/aufs/si_<id>/
1055+Date: March 2009
f6b6e03d 1056+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1057+Description:
1058+ Under /debug/aufs, a directory named si_<id> is created
1059+ per aufs mount, where <id> is a unique id generated
1060+ internally.
1facf9fc 1061+
86dc4139
AM
1062+What: /debug/aufs/si_<id>/plink
1063+Date: Apr 2013
f6b6e03d 1064+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
1065+Description:
1066+ It has three lines and shows the information about the
1067+ pseudo-link. The first line is a single number
1068+ representing a number of buckets. The second line is a
1069+ number of pseudo-links per buckets (separated by a
1070+ blank). The last line is a single number representing a
1071+ total number of psedo-links.
1072+ When the aufs mount option 'noplink' is specified, it
1073+ will show "1\n0\n0\n".
1074+
7f207e10
AM
1075+What: /debug/aufs/si_<id>/xib
1076+Date: March 2009
f6b6e03d 1077+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1078+Description:
1079+ It shows the consumed blocks by xib (External Inode Number
1080+ Bitmap), its block size and file size.
1081+ When the aufs mount option 'noxino' is specified, it
1082+ will be empty. About XINO files, see the aufs manual.
1083+
062440b3 1084+What: /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
7f207e10 1085+Date: March 2009
f6b6e03d 1086+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1087+Description:
1088+ It shows the consumed blocks by xino (External Inode Number
1089+ Translation Table), its link count, block size and file
1090+ size.
062440b3
AM
1091+ Due to the file size limit, there may exist multiple
1092+ xino files per branch. In this case, "-N" is added to
1093+ the filename and it corresponds to the index of the
1094+ internal xino array. "-0" is omitted.
1095+ When the aufs mount option 'noxino' is specified, Those
1096+ entries won't exist. About XINO files, see the aufs
1097+ manual.
7f207e10
AM
1098+
1099+What: /debug/aufs/si_<id>/xigen
1100+Date: March 2009
f6b6e03d 1101+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1102+Description:
1103+ It shows the consumed blocks by xigen (External Inode
1104+ Generation Table), its block size and file size.
1105+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1106+ be created.
1107+ When the aufs mount option 'noxino' is specified, it
1108+ will be empty. About XINO files, see the aufs manual.
1109diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
eca34b5c
AM
1110--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
1111+++ linux/Documentation/ABI/testing/sysfs-aufs 2019-07-11 15:42:14.455570938 +0200
392086de 1112@@ -0,0 +1,31 @@
7f207e10
AM
1113+What: /sys/fs/aufs/si_<id>/
1114+Date: March 2009
f6b6e03d 1115+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1116+Description:
1117+ Under /sys/fs/aufs, a directory named si_<id> is created
1118+ per aufs mount, where <id> is a unique id generated
1119+ internally.
1120+
1121+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1122+Date: March 2009
f6b6e03d 1123+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1124+Description:
1125+ It shows the abolute path of a member directory (which
1126+ is called branch) in aufs, and its permission.
1127+
392086de
AM
1128+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1129+Date: July 2013
f6b6e03d 1130+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1131+Description:
1132+ It shows the id of a member directory (which is called
1133+ branch) in aufs.
1134+
7f207e10
AM
1135+What: /sys/fs/aufs/si_<id>/xi_path
1136+Date: March 2009
f6b6e03d 1137+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1138+Description:
1139+ It shows the abolute path of XINO (External Inode Number
1140+ Bitmap, Translation Table and Generation Table) file
1141+ even if it is the default path.
1142+ When the aufs mount option 'noxino' is specified, it
1143+ will be empty. About XINO files, see the aufs manual.
53392da6 1144diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
eca34b5c
AM
1145--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
1146+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2019-07-11 15:42:14.455570938 +0200
1c60b727 1147@@ -0,0 +1,171 @@
53392da6 1148+
ba1aed25 1149+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
1150+#
1151+# This program is free software; you can redistribute it and/or modify
1152+# it under the terms of the GNU General Public License as published by
1153+# the Free Software Foundation; either version 2 of the License, or
1154+# (at your option) any later version.
1155+#
1156+# This program is distributed in the hope that it will be useful,
1157+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1158+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1159+# GNU General Public License for more details.
1160+#
1161+# You should have received a copy of the GNU General Public License
523b37e3 1162+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1163+
1164+Introduction
1165+----------------------------------------
1166+
3c1bdaff 1167+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1168+1. abbrev. for "advanced multi-layered unification filesystem".
1169+2. abbrev. for "another unionfs".
1170+3. abbrev. for "auf das" in German which means "on the" in English.
1171+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1172+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1173+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1174+
1175+AUFS is a filesystem with features:
1176+- multi layered stackable unification filesystem, the member directory
1177+ is called as a branch.
1178+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1179+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1180+ combination.
1181+- internal "file copy-on-write".
1182+- logical deletion, whiteout.
1183+- dynamic branch manipulation, adding, deleting and changing permission.
1184+- allow bypassing aufs, user's direct branch access.
1185+- external inode number translation table and bitmap which maintains the
1186+ persistent aufs inode number.
1187+- seekable directory, including NFS readdir.
1188+- file mapping, mmap and sharing pages.
1189+- pseudo-link, hardlink over branches.
1190+- loopback mounted filesystem as a branch.
1191+- several policies to select one among multiple writable branches.
1192+- revert a single systemcall when an error occurs in aufs.
1193+- and more...
1194+
1195+
1196+Multi Layered Stackable Unification Filesystem
1197+----------------------------------------------------------------------
1198+Most people already knows what it is.
1199+It is a filesystem which unifies several directories and provides a
1200+merged single directory. When users access a file, the access will be
1201+passed/re-directed/converted (sorry, I am not sure which English word is
1202+correct) to the real file on the member filesystem. The member
1203+filesystem is called 'lower filesystem' or 'branch' and has a mode
1204+'readonly' and 'readwrite.' And the deletion for a file on the lower
1205+readonly branch is handled by creating 'whiteout' on the upper writable
1206+branch.
1207+
1208+On LKML, there have been discussions about UnionMount (Jan Blunck,
1209+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1210+different approaches to implement the merged-view.
1211+The former tries putting it into VFS, and the latter implements as a
1212+separate filesystem.
1213+(If I misunderstand about these implementations, please let me know and
1214+I shall correct it. Because it is a long time ago when I read their
1215+source files last time).
1216+
1217+UnionMount's approach will be able to small, but may be hard to share
1218+branches between several UnionMount since the whiteout in it is
1219+implemented in the inode on branch filesystem and always
1220+shared. According to Bharata's post, readdir does not seems to be
1221+finished yet.
1222+There are several missing features known in this implementations such as
1223+- for users, the inode number may change silently. eg. copy-up.
1224+- link(2) may break by copy-up.
1225+- read(2) may get an obsoleted filedata (fstat(2) too).
1226+- fcntl(F_SETLK) may be broken by copy-up.
1227+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1228+ open(O_RDWR).
1229+
7e9cd9fe
AM
1230+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1231+merged into mainline. This is another implementation of UnionMount as a
1232+separated filesystem. All the limitations and known problems which
1233+UnionMount are equally inherited to "overlay" filesystem.
1234+
1235+Unionfs has a longer history. When I started implementing a stackable
1236+filesystem (Aug 2005), it already existed. It has virtual super_block,
1237+inode, dentry and file objects and they have an array pointing lower
1238+same kind objects. After contributing many patches for Unionfs, I
1239+re-started my project AUFS (Jun 2006).
53392da6
AM
1240+
1241+In AUFS, the structure of filesystem resembles to Unionfs, but I
1242+implemented my own ideas, approaches and enhancements and it became
1243+totally different one.
1244+
1245+Comparing DM snapshot and fs based implementation
1246+- the number of bytes to be copied between devices is much smaller.
1247+- the type of filesystem must be one and only.
1248+- the fs must be writable, no readonly fs, even for the lower original
1249+ device. so the compression fs will not be usable. but if we use
1250+ loopback mount, we may address this issue.
1251+ for instance,
1252+ mount /cdrom/squashfs.img /sq
1253+ losetup /sq/ext2.img
1254+ losetup /somewhere/cow
1255+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1256+- it will be difficult (or needs more operations) to extract the
1257+ difference between the original device and COW.
1258+- DM snapshot-merge may help a lot when users try merging. in the
1259+ fs-layer union, users will use rsync(1).
1260+
7e9cd9fe
AM
1261+You may want to read my old paper "Filesystems in LiveCD"
1262+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1263+
7e9cd9fe
AM
1264+
1265+Several characters/aspects/persona of aufs
53392da6
AM
1266+----------------------------------------------------------------------
1267+
7e9cd9fe 1268+Aufs has several characters, aspects or persona.
53392da6
AM
1269+1. a filesystem, callee of VFS helper
1270+2. sub-VFS, caller of VFS helper for branches
1271+3. a virtual filesystem which maintains persistent inode number
1272+4. reader/writer of files on branches such like an application
1273+
1274+1. Callee of VFS Helper
1275+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1276+unlink(2) from an application reaches sys_unlink() kernel function and
1277+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1278+calls filesystem specific unlink operation. Actually aufs implements the
1279+unlink operation but it behaves like a redirector.
1280+
1281+2. Caller of VFS Helper for Branches
1282+aufs_unlink() passes the unlink request to the branch filesystem as if
1283+it were called from VFS. So the called unlink operation of the branch
1284+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1285+every necessary pre/post operation for the branch filesystem.
1286+- acquire the lock for the parent dir on a branch
1287+- lookup in a branch
1288+- revalidate dentry on a branch
1289+- mnt_want_write() for a branch
1290+- vfs_unlink() for a branch
1291+- mnt_drop_write() for a branch
1292+- release the lock on a branch
1293+
1294+3. Persistent Inode Number
1295+One of the most important issue for a filesystem is to maintain inode
1296+numbers. This is particularly important to support exporting a
1297+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1298+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1299+keep and maintain the inode numbers. It may be a large space and may not
1300+suit to keep in memory. Aufs rents some space from its first writable
1301+branch filesystem (by default) and creates file(s) on it. These files
1302+are created by aufs internally and removed soon (currently) keeping
1303+opened.
53392da6
AM
1304+Note: Because these files are removed, they are totally gone after
1305+ unmounting aufs. It means the inode numbers are not persistent
1306+ across unmount or reboot. I have a plan to make them really
1307+ persistent which will be important for aufs on NFS server.
1308+
1309+4. Read/Write Files Internally (copy-on-write)
1310+Because a branch can be readonly, when you write a file on it, aufs will
1311+"copy-up" it to the upper writable branch internally. And then write the
1312+originally requested thing to the file. Generally kernel doesn't
1313+open/read/write file actively. In aufs, even a single write may cause a
1314+internal "file copy". This behaviour is very similar to cp(1) command.
1315+
1316+Some people may think it is better to pass such work to user space
1317+helper, instead of doing in kernel space. Actually I am still thinking
1318+about it. But currently I have implemented it in kernel space.
1319diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
eca34b5c
AM
1320--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1321+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2019-07-11 15:42:14.455570938 +0200
7e9cd9fe 1322@@ -0,0 +1,258 @@
53392da6 1323+
ba1aed25 1324+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
1325+#
1326+# This program is free software; you can redistribute it and/or modify
1327+# it under the terms of the GNU General Public License as published by
1328+# the Free Software Foundation; either version 2 of the License, or
1329+# (at your option) any later version.
1330+#
1331+# This program is distributed in the hope that it will be useful,
1332+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1333+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1334+# GNU General Public License for more details.
1335+#
1336+# You should have received a copy of the GNU General Public License
523b37e3 1337+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1338+
1339+Basic Aufs Internal Structure
1340+
1341+Superblock/Inode/Dentry/File Objects
1342+----------------------------------------------------------------------
1343+As like an ordinary filesystem, aufs has its own
1344+superblock/inode/dentry/file objects. All these objects have a
1345+dynamically allocated array and store the same kind of pointers to the
1346+lower filesystem, branch.
1347+For example, when you build a union with one readwrite branch and one
1348+readonly, mounted /au, /rw and /ro respectively.
1349+- /au = /rw + /ro
1350+- /ro/fileA exists but /rw/fileA
1351+
1352+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1353+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1354+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1355+- [1] = /ro/fileA
1356+
1357+This style of an array is essentially same to the aufs
1358+superblock/inode/dentry/file objects.
1359+
1360+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1361+branches dynamically, these objects has its own generation. When
1362+branches are changed, the generation in aufs superblock is
1363+incremented. And a generation in other object are compared when it is
1364+accessed. When a generation in other objects are obsoleted, aufs
1365+refreshes the internal array.
53392da6
AM
1366+
1367+
1368+Superblock
1369+----------------------------------------------------------------------
1370+Additionally aufs superblock has some data for policies to select one
1371+among multiple writable branches, XIB files, pseudo-links and kobject.
1372+See below in detail.
7e9cd9fe
AM
1373+About the policies which supports copy-down a directory, see
1374+wbr_policy.txt too.
53392da6
AM
1375+
1376+
1377+Branch and XINO(External Inode Number Translation Table)
1378+----------------------------------------------------------------------
1379+Every branch has its own xino (external inode number translation table)
1380+file. The xino file is created and unlinked by aufs internally. When two
1381+members of a union exist on the same filesystem, they share the single
1382+xino file.
1383+The struct of a xino file is simple, just a sequence of aufs inode
1384+numbers which is indexed by the lower inode number.
1385+In the above sample, assume the inode number of /ro/fileA is i111 and
1386+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1387+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1388+
1389+When the inode numbers are not contiguous, the xino file will be sparse
1390+which has a hole in it and doesn't consume as much disk space as it
1391+might appear. If your branch filesystem consumes disk space for such
1392+holes, then you should specify 'xino=' option at mounting aufs.
1393+
7e9cd9fe
AM
1394+Aufs has a mount option to free the disk blocks for such holes in XINO
1395+files on tmpfs or ramdisk. But it is not so effective actually. If you
1396+meet a problem of disk shortage due to XINO files, then you should try
1397+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1398+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1399+the holes in XINO files.
1400+
53392da6 1401+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1402+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1403+whiteout-ed doubly, so that users will never see their names in aufs
1404+hierarchy.
7e9cd9fe 1405+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1406+2. a directory to store a pseudo-link.
7e9cd9fe 1407+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1408+
1409+1. Whiteout Base
1410+ When you remove a file on a readonly branch, aufs handles it as a
1411+ logical deletion and creates a whiteout on the upper writable branch
1412+ as a hardlink of this file in order not to consume inode on the
1413+ writable branch.
1414+2. Pseudo-link Dir
1415+ See below, Pseudo-link.
1416+3. Step-Parent Dir
1417+ When "fileC" exists on the lower readonly branch only and it is
1418+ opened and removed with its parent dir, and then user writes
1419+ something into it, then aufs copies-up fileC to this
1420+ directory. Because there is no other dir to store fileC. After
1421+ creating a file under this dir, the file is unlinked.
1422+
1423+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1424+dynamically, a branch has its own id. When the branch order changes,
1425+aufs finds the new index by searching the branch id.
53392da6
AM
1426+
1427+
1428+Pseudo-link
1429+----------------------------------------------------------------------
1430+Assume "fileA" exists on the lower readonly branch only and it is
1431+hardlinked to "fileB" on the branch. When you write something to fileA,
1432+aufs copies-up it to the upper writable branch. Additionally aufs
1433+creates a hardlink under the Pseudo-link Directory of the writable
1434+branch. The inode of a pseudo-link is kept in aufs super_block as a
1435+simple list. If fileB is read after unlinking fileA, aufs returns
1436+filedata from the pseudo-link instead of the lower readonly
1437+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1438+inode number by xino (see above) is essentially necessary.
53392da6
AM
1439+
1440+All the hardlinks under the Pseudo-link Directory of the writable branch
1441+should be restored in a proper location later. Aufs provides a utility
1442+to do this. The userspace helpers executed at remounting and unmounting
1443+aufs by default.
1444+During this utility is running, it puts aufs into the pseudo-link
1445+maintenance mode. In this mode, only the process which began the
1446+maintenance mode (and its child processes) is allowed to operate in
1447+aufs. Some other processes which are not related to the pseudo-link will
1448+be allowed to run too, but the rest have to return an error or wait
1449+until the maintenance mode ends. If a process already acquires an inode
1450+mutex (in VFS), it has to return an error.
1451+
1452+
1453+XIB(external inode number bitmap)
1454+----------------------------------------------------------------------
1455+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1456+bitmap in a superblock object. It is also an internal file such like a
1457+xino file.
53392da6
AM
1458+It is a simple bitmap to mark whether the aufs inode number is in-use or
1459+not.
1460+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1461+
7e9cd9fe 1462+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1463+reduce the number of consumed disk blocks for these files.
1464+
1465+
1466+Virtual or Vertical Dir, and Readdir in Userspace
1467+----------------------------------------------------------------------
1468+In order to support multiple layers (branches), aufs readdir operation
1469+constructs a virtual dir block on memory. For readdir, aufs calls
1470+vfs_readdir() internally for each dir on branches, merges their entries
1471+with eliminating the whiteout-ed ones, and sets it to file (dir)
1472+object. So the file object has its entry list until it is closed. The
1473+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1474+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1475+
1476+The dynamically allocated memory block for the name of entries has a
1477+unit of 512 bytes (by default) and stores the names contiguously (no
1478+padding). Another block for each entry is handled by kmem_cache too.
1479+During building dir blocks, aufs creates hash list and judging whether
1480+the entry is whiteouted by its upper branch or already listed.
1481+The merged result is cached in the corresponding inode object and
1482+maintained by a customizable life-time option.
1483+
1484+Some people may call it can be a security hole or invite DoS attack
1485+since the opened and once readdir-ed dir (file object) holds its entry
1486+list and becomes a pressure for system memory. But I'd say it is similar
1487+to files under /proc or /sys. The virtual files in them also holds a
1488+memory page (generally) while they are opened. When an idea to reduce
1489+memory for them is introduced, it will be applied to aufs too.
1490+For those who really hate this situation, I've developed readdir(3)
1491+library which operates this merging in userspace. You just need to set
1492+LD_PRELOAD environment variable, and aufs will not consume no memory in
1493+kernel space for readdir(3).
1494+
1495+
1496+Workqueue
1497+----------------------------------------------------------------------
1498+Aufs sometimes requires privilege access to a branch. For instance,
1499+in copy-up/down operation. When a user process is going to make changes
1500+to a file which exists in the lower readonly branch only, and the mode
1501+of one of ancestor directories may not be writable by a user
1502+process. Here aufs copy-up the file with its ancestors and they may
1503+require privilege to set its owner/group/mode/etc.
1504+This is a typical case of a application character of aufs (see
1505+Introduction).
1506+
1507+Aufs uses workqueue synchronously for this case. It creates its own
1508+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1509+passes the request to call mkdir or write (for example), and wait for
1510+its completion. This approach solves a problem of a signal handler
1511+simply.
1512+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1513+process, then the process may receive the unexpected SIGXFSZ or other
1514+signals.
53392da6
AM
1515+
1516+Also aufs uses the system global workqueue ("events" kernel thread) too
1517+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1518+whiteout base and etc. This is unrelated to a privilege.
1519+Most of aufs operation tries acquiring a rw_semaphore for aufs
1520+superblock at the beginning, at the same time waits for the completion
1521+of all queued asynchronous tasks.
1522+
1523+
1524+Whiteout
1525+----------------------------------------------------------------------
1526+The whiteout in aufs is very similar to Unionfs's. That is represented
1527+by its filename. UnionMount takes an approach of a file mode, but I am
1528+afraid several utilities (find(1) or something) will have to support it.
1529+
1530+Basically the whiteout represents "logical deletion" which stops aufs to
1531+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1532+further lookup.
53392da6
AM
1533+
1534+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1535+In order to make several functions in a single systemcall to be
1536+revertible, aufs adopts an approach to rename a directory to a temporary
1537+unique whiteouted name.
1538+For example, in rename(2) dir where the target dir already existed, aufs
1539+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1540+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1541+update the attributes, etc). If an error happens in these actions, aufs
1542+simply renames the whiteouted name back and returns an error. If all are
1543+succeeded, aufs registers a function to remove the whiteouted unique
1544+temporary name completely and asynchronously to the system global
1545+workqueue.
1546+
1547+
1548+Copy-up
1549+----------------------------------------------------------------------
1550+It is a well-known feature or concept.
1551+When user modifies a file on a readonly branch, aufs operate "copy-up"
1552+internally and makes change to the new file on the upper writable branch.
1553+When the trigger systemcall does not update the timestamps of the parent
1554+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1555+
1556+
1557+Move-down (aufs3.9 and later)
1558+----------------------------------------------------------------------
1559+"Copy-up" is one of the essential feature in aufs. It copies a file from
1560+the lower readonly branch to the upper writable branch when a user
1561+changes something about the file.
1562+"Move-down" is an opposite action of copy-up. Basically this action is
1563+ran manually instead of automatically and internally.
076b876e
AM
1564+For desgin and implementation, aufs has to consider these issues.
1565+- whiteout for the file may exist on the lower branch.
1566+- ancestor directories may not exist on the lower branch.
1567+- diropq for the ancestor directories may exist on the upper branch.
1568+- free space on the lower branch will reduce.
1569+- another access to the file may happen during moving-down, including
7e9cd9fe 1570+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1571+- the file should not be hard-linked nor pseudo-linked. they should be
1572+ handled by auplink utility later.
c2b27bf2
AM
1573+
1574+Sometimes users want to move-down a file from the upper writable branch
1575+to the lower readonly or writable branch. For instance,
1576+- the free space of the upper writable branch is going to run out.
1577+- create a new intermediate branch between the upper and lower branch.
1578+- etc.
1579+
1580+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e 1581diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
eca34b5c
AM
1582--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
1583+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2019-07-11 15:42:14.458904362 +0200
b912730e
AM
1584@@ -0,0 +1,85 @@
1585+
ba1aed25 1586+# Copyright (C) 2015-2019 Junjiro R. Okajima
b912730e
AM
1587+#
1588+# This program is free software; you can redistribute it and/or modify
1589+# it under the terms of the GNU General Public License as published by
1590+# the Free Software Foundation; either version 2 of the License, or
1591+# (at your option) any later version.
1592+#
1593+# This program is distributed in the hope that it will be useful,
1594+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1595+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1596+# GNU General Public License for more details.
1597+#
1598+# You should have received a copy of the GNU General Public License
1599+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1600+
1601+Support for a branch who has its ->atomic_open()
1602+----------------------------------------------------------------------
1603+The filesystems who implement its ->atomic_open() are not majority. For
1604+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1605+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1606+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1607+sure whether all filesystems who have ->atomic_open() behave like this,
1608+but NFSv4 surely returns the error.
1609+
1610+In order to support ->atomic_open() for aufs, there are a few
1611+approaches.
1612+
1613+A. Introduce aufs_atomic_open()
1614+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1615+ branch fs.
1616+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1617+ an aufs user Pip Cet's approach
1618+ - calls aufs_create(), VFS finish_open() and notify_change().
1619+ - pass fake-mode to finish_open(), and then correct the mode by
1620+ notify_change().
1621+C. Extend aufs_open() to call branch fs's ->atomic_open()
1622+ - no aufs_atomic_open().
1623+ - aufs_lookup() registers the TID to an aufs internal object.
1624+ - aufs_create() does nothing when the matching TID is registered, but
1625+ registers the mode.
1626+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1627+ TID is registered.
1628+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1629+ credential
1630+ - no aufs_atomic_open().
1631+ - aufs_create() registers the TID to an internal object. this info
1632+ represents "this process created this file just now."
1633+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1634+ registered TID and re-try open() with superuser's credential.
1635+
1636+Pros and cons for each approach.
1637+
1638+A.
1639+ - straightforward but highly depends upon VFS internal.
1640+ - the atomic behavaiour is kept.
1641+ - some of parameters such as nameidata are hard to reproduce for
1642+ branch fs.
1643+ - large overhead.
1644+B.
1645+ - easy to implement.
1646+ - the atomic behavaiour is lost.
1647+C.
1648+ - the atomic behavaiour is kept.
1649+ - dirty and tricky.
1650+ - VFS checks whether the file is created correctly after calling
1651+ ->create(), which means this approach doesn't work.
1652+D.
1653+ - easy to implement.
1654+ - the atomic behavaiour is lost.
1655+ - to open a file with superuser's credential and give it to a user
1656+ process is a bad idea, since the file object keeps the credential
1657+ in it. It may affect LSM or something. This approach doesn't work
1658+ either.
1659+
1660+The approach A is ideal, but it hard to implement. So here is a
1661+variation of A, which is to be implemented.
1662+
1663+A-1. Introduce aufs_atomic_open()
1664+ - calls branch fs ->atomic_open() if exists. otherwise calls
1665+ vfs_create() and finish_open().
1666+ - the demerit is that the several checks after branch fs
1667+ ->atomic_open() are lost. in the ordinary case, the checks are
1668+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1669+ be implemented in aufs, but not all I am afraid.
53392da6 1670diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
eca34b5c
AM
1671--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1672+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2019-07-11 15:42:14.458904362 +0200
7e9cd9fe 1673@@ -0,0 +1,113 @@
53392da6 1674+
ba1aed25 1675+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
1676+#
1677+# This program is free software; you can redistribute it and/or modify
1678+# it under the terms of the GNU General Public License as published by
1679+# the Free Software Foundation; either version 2 of the License, or
1680+# (at your option) any later version.
1681+#
1682+# This program is distributed in the hope that it will be useful,
1683+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1684+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1685+# GNU General Public License for more details.
1686+#
1687+# You should have received a copy of the GNU General Public License
523b37e3 1688+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1689+
1690+Lookup in a Branch
1691+----------------------------------------------------------------------
1692+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1693+lookup for branches as VFS does. It may be a heavy work. But almost all
1694+lookup operation in aufs is the simplest case, ie. lookup only an entry
1695+directly connected to its parent. Digging down the directory hierarchy
1696+is unnecessary. VFS has a function lookup_one_len() for that use, and
1697+aufs calls it.
1698+
1699+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1700+->d_revalidate(), also aufs forces the hardest revalidate tests for
1701+them.
1702+For d_revalidate, aufs implements three levels of revalidate tests. See
1703+"Revalidate Dentry and UDBA" in detail.
1704+
1705+
076b876e
AM
1706+Test Only the Highest One for the Directory Permission (dirperm1 option)
1707+----------------------------------------------------------------------
1708+Let's try case study.
1709+- aufs has two branches, upper readwrite and lower readonly.
1710+ /au = /rw + /ro
1711+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1712+- user invoked "chmod a+rx /au/dirA"
1713+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1714+ permission bits are set to world readable.
076b876e
AM
1715+- then "/au/dirA" becomes world readable?
1716+
1717+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1718+or it may be a natively readonly filesystem. If aufs respects the lower
1719+branch, it should not respond readdir request from other users. But user
1720+allowed it by chmod. Should really aufs rejects showing the entries
1721+under /ro/dirA?
1722+
7e9cd9fe
AM
1723+To be honest, I don't have a good solution for this case. So aufs
1724+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1725+users.
076b876e
AM
1726+When dirperm1 is specified, aufs checks only the highest one for the
1727+directory permission, and shows the entries. Otherwise, as usual, checks
1728+every dir existing on all branches and rejects the request.
1729+
1730+As a side effect, dirperm1 option improves the performance of aufs
1731+because the number of permission check is reduced when the number of
1732+branch is many.
1733+
1734+
53392da6
AM
1735+Revalidate Dentry and UDBA (User's Direct Branch Access)
1736+----------------------------------------------------------------------
1737+Generally VFS helpers re-validate a dentry as a part of lookup.
1738+0. digging down the directory hierarchy.
1739+1. lock the parent dir by its i_mutex.
1740+2. lookup the final (child) entry.
1741+3. revalidate it.
1742+4. call the actual operation (create, unlink, etc.)
1743+5. unlock the parent dir
1744+
1745+If the filesystem implements its ->d_revalidate() (step 3), then it is
1746+called. Actually aufs implements it and checks the dentry on a branch is
1747+still valid.
1748+But it is not enough. Because aufs has to release the lock for the
1749+parent dir on a branch at the end of ->lookup() (step 2) and
1750+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1751+held by VFS.
1752+If the file on a branch is changed directly, eg. bypassing aufs, after
1753+aufs released the lock, then the subsequent operation may cause
1754+something unpleasant result.
1755+
1756+This situation is a result of VFS architecture, ->lookup() and
1757+->d_revalidate() is separated. But I never say it is wrong. It is a good
1758+design from VFS's point of view. It is just not suitable for sub-VFS
1759+character in aufs.
1760+
1761+Aufs supports such case by three level of revalidation which is
1762+selectable by user.
1763+1. Simple Revalidate
1764+ Addition to the native flow in VFS's, confirm the child-parent
1765+ relationship on the branch just after locking the parent dir on the
1766+ branch in the "actual operation" (step 4). When this validation
1767+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1768+ checks the validation of the dentry on branches.
1769+2. Monitor Changes Internally by Inotify/Fsnotify
1770+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1771+ the dentry on the branch, and returns EBUSY if it finds different
1772+ dentry.
1773+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1774+ during it is in cache. When the event is notified, aufs registers a
1775+ function to kernel 'events' thread by schedule_work(). And the
1776+ function sets some special status to the cached aufs dentry and inode
1777+ private data. If they are not cached, then aufs has nothing to
1778+ do. When the same file is accessed through aufs (step 0-3) later,
1779+ aufs will detect the status and refresh all necessary data.
1780+ In this mode, aufs has to ignore the event which is fired by aufs
1781+ itself.
1782+3. No Extra Validation
1783+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1784+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1785+ aufs performance when system surely hide the aufs branches from user,
1786+ by over-mounting something (or another method).
1787diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
eca34b5c
AM
1788--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1789+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2019-07-11 15:42:14.458904362 +0200
7e9cd9fe 1790@@ -0,0 +1,74 @@
53392da6 1791+
ba1aed25 1792+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
1793+#
1794+# This program is free software; you can redistribute it and/or modify
1795+# it under the terms of the GNU General Public License as published by
1796+# the Free Software Foundation; either version 2 of the License, or
1797+# (at your option) any later version.
1798+#
1799+# This program is distributed in the hope that it will be useful,
1800+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1801+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1802+# GNU General Public License for more details.
1803+#
1804+# You should have received a copy of the GNU General Public License
523b37e3 1805+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1806+
1807+Branch Manipulation
1808+
1809+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1810+and changing its permission/attribute, there are a lot of works to do.
1811+
1812+
1813+Add a Branch
1814+----------------------------------------------------------------------
1815+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1816+ mount, and its various attributes.
53392da6
AM
1817+o Initialize the xino file and whiteout bases if necessary.
1818+ See struct.txt.
1819+
1820+o Check the owner/group/mode of the directory
1821+ When the owner/group/mode of the adding directory differs from the
1822+ existing branch, aufs issues a warning because it may impose a
1823+ security risk.
1824+ For example, when a upper writable branch has a world writable empty
1825+ top directory, a malicious user can create any files on the writable
1826+ branch directly, like copy-up and modify manually. If something like
1827+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1828+ writable branch, and the writable branch is world-writable, then a
1829+ malicious guy may create /etc/passwd on the writable branch directly
1830+ and the infected file will be valid in aufs.
7e9cd9fe 1831+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1832+ producing a warning.
1833+
1834+
1835+Delete a Branch
1836+----------------------------------------------------------------------
1837+o Confirm the deleting branch is not busy
1838+ To be general, there is one merit to adopt "remount" interface to
1839+ manipulate branches. It is to discard caches. At deleting a branch,
1840+ aufs checks the still cached (and connected) dentries and inodes. If
1841+ there are any, then they are all in-use. An inode without its
1842+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1843+
1844+ For the cached one, aufs checks whether the same named entry exists on
1845+ other branches.
1846+ If the cached one is a directory, because aufs provides a merged view
1847+ to users, as long as one dir is left on any branch aufs can show the
1848+ dir to users. In this case, the branch can be removed from aufs.
1849+ Otherwise aufs rejects deleting the branch.
1850+
1851+ If any file on the deleting branch is opened by aufs, then aufs
1852+ rejects deleting.
1853+
1854+
1855+Modify the Permission of a Branch
1856+----------------------------------------------------------------------
1857+o Re-initialize or remove the xino file and whiteout bases if necessary.
1858+ See struct.txt.
1859+
1860+o rw --> ro: Confirm the modifying branch is not busy
1861+ Aufs rejects the request if any of these conditions are true.
1862+ - a file on the branch is mmap-ed.
1863+ - a regular file on the branch is opened for write and there is no
1864+ same named entry on the upper branch.
1865diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
eca34b5c
AM
1866--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
1867+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2019-07-11 15:42:14.458904362 +0200
523b37e3 1868@@ -0,0 +1,64 @@
53392da6 1869+
ba1aed25 1870+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
1871+#
1872+# This program is free software; you can redistribute it and/or modify
1873+# it under the terms of the GNU General Public License as published by
1874+# the Free Software Foundation; either version 2 of the License, or
1875+# (at your option) any later version.
1876+#
1877+# This program is distributed in the hope that it will be useful,
1878+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1879+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1880+# GNU General Public License for more details.
1881+#
1882+# You should have received a copy of the GNU General Public License
523b37e3 1883+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1884+
1885+Policies to Select One among Multiple Writable Branches
1886+----------------------------------------------------------------------
1887+When the number of writable branch is more than one, aufs has to decide
1888+the target branch for file creation or copy-up. By default, the highest
1889+writable branch which has the parent (or ancestor) dir of the target
1890+file is chosen (top-down-parent policy).
1891+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
1892+writable branch, for file creation several policies, round-robin,
1893+most-free-space, and other policies. For copy-up, top-down-parent,
1894+bottom-up-parent, bottom-up and others.
53392da6
AM
1895+
1896+As expected, the round-robin policy selects the branch in circular. When
1897+you have two writable branches and creates 10 new files, 5 files will be
1898+created for each branch. mkdir(2) systemcall is an exception. When you
1899+create 10 new directories, all will be created on the same branch.
1900+And the most-free-space policy selects the one which has most free
1901+space among the writable branches. The amount of free space will be
1902+checked by aufs internally, and users can specify its time interval.
1903+
1904+The policies for copy-up is more simple,
1905+top-down-parent is equivalent to the same named on in create policy,
1906+bottom-up-parent selects the writable branch where the parent dir
1907+exists and the nearest upper one from the copyup-source,
1908+bottom-up selects the nearest upper writable branch from the
1909+copyup-source, regardless the existence of the parent dir.
1910+
1911+There are some rules or exceptions to apply these policies.
1912+- If there is a readonly branch above the policy-selected branch and
1913+ the parent dir is marked as opaque (a variation of whiteout), or the
1914+ target (creating) file is whiteout-ed on the upper readonly branch,
1915+ then the result of the policy is ignored and the target file will be
1916+ created on the nearest upper writable branch than the readonly branch.
1917+- If there is a writable branch above the policy-selected branch and
1918+ the parent dir is marked as opaque or the target file is whiteouted
1919+ on the branch, then the result of the policy is ignored and the target
1920+ file will be created on the highest one among the upper writable
1921+ branches who has diropq or whiteout. In case of whiteout, aufs removes
1922+ it as usual.
1923+- link(2) and rename(2) systemcalls are exceptions in every policy.
1924+ They try selecting the branch where the source exists as possible
1925+ since copyup a large file will take long time. If it can't be,
1926+ ie. the branch where the source exists is readonly, then they will
1927+ follow the copyup policy.
1928+- There is an exception for rename(2) when the target exists.
1929+ If the rename target exists, aufs compares the index of the branches
1930+ where the source and the target exists and selects the higher
1931+ one. If the selected branch is readonly, then aufs follows the
1932+ copyup policy.
8b6a4947 1933diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
eca34b5c
AM
1934--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1935+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2019-07-11 15:42:14.458904362 +0200
8b6a4947
AM
1936@@ -0,0 +1,31 @@
1937+
1938+// to view this graph, run dot(1) command in GRAPHVIZ.
1939+
1940+digraph G {
1941+node [shape=box];
1942+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1943+
1944+node [shape=oval];
1945+
1946+aufs_rename -> whinfo [label="store/remove"];
1947+
1948+node [shape=oval];
1949+inode_list [label="h_inum list in branch\ncache"];
1950+
1951+node [shape=box];
1952+whinode [label="h_inum list file"];
1953+
1954+node [shape=oval];
1955+brmgmt [label="br_add/del/mod/umount"];
1956+
1957+brmgmt -> inode_list [label="create/remove"];
1958+brmgmt -> whinode [label="load/store"];
1959+
1960+inode_list -> whinode [style=dashed,dir=both];
1961+
1962+aufs_rename -> inode_list [label="add/del"];
1963+
1964+aufs_lookup -> inode_list [label="search"];
1965+
1966+aufs_lookup -> whinfo [label="load/remove"];
1967+}
1968diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
eca34b5c
AM
1969--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1970+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2019-07-11 15:42:14.458904362 +0200
8b6a4947
AM
1971@@ -0,0 +1,102 @@
1972+
ba1aed25 1973+# Copyright (C) 2017-2019 Junjiro R. Okajima
8b6a4947
AM
1974+#
1975+# This program is free software; you can redistribute it and/or modify
1976+# it under the terms of the GNU General Public License as published by
1977+# the Free Software Foundation; either version 2 of the License, or
1978+# (at your option) any later version.
1979+#
1980+# This program is distributed in the hope that it will be useful,
1981+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1982+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1983+# GNU General Public License for more details.
1984+#
1985+# You should have received a copy of the GNU General Public License
1986+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1987+
1988+Special handling for renaming a directory (DIRREN)
1989+----------------------------------------------------------------------
1990+First, let's assume we have a simple usecase.
1991+
1992+- /u = /rw + /ro
1993+- /rw/dirA exists
1994+- /ro/dirA and /ro/dirA/file exist too
1995+- there is no dirB on both branches
1996+- a user issues rename("dirA", "dirB")
1997+
1998+Now, what should aufs behave against this rename(2)?
1999+There are a few possible cases.
2000+
2001+A. returns EROFS.
2002+ since dirA exists on a readonly branch which cannot be renamed.
2003+B. returns EXDEV.
2004+ it is possible to copy-up dirA (only the dir itself), but the child
2005+ entries ("file" in this case) should not be. it must be a bad
2006+ approach to copy-up recursively.
2007+C. returns a success.
2008+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
2009+ is a violation of aufs' policy.
2010+D. construct an extra information which indicates that /ro/dirA should
2011+ be handled as the name of dirB.
2012+ overlayfs has a similar feature called REDIRECT.
2013+
2014+Until now, aufs implements the case B only which returns EXDEV, and
2015+expects the userspace application behaves like mv(1) which tries
2016+issueing rename(2) recursively.
2017+
2018+A new aufs feature called DIRREN is introduced which implements the case
2019+D. There are several "extra information" added.
2020+
2021+1. detailed info per renamed directory
2022+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2023+2. the inode-number list of directories on a branch
2024+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
2025+
2026+The filename of "detailed info per directory" represents the lower
2027+branch, and its format is
2028+- a type of the branch id
2029+ one of these.
2030+ + uuid (not implemented yet)
2031+ + fsid
2032+ + dev
2033+- the inode-number of the branch root dir
2034+
2035+And it contains these info in a single regular file.
2036+- magic number
2037+- branch's inode-number of the logically renamed dir
2038+- the name of the before-renamed dir
2039+
2040+The "detailed info per directory" file is created in aufs rename(2), and
2041+loaded in any lookup.
2042+The info is considered in lookup for the matching case only. Here
2043+"matching" means that the root of branch (in the info filename) is same
2044+to the current looking-up branch. After looking-up the before-renamed
2045+name, the inode-number is compared. And the matched dentry is used.
2046+
2047+The "inode-number list of directories" is a regular file which contains
2048+simply the inode-numbers on the branch. The file is created or updated
2049+in removing the branch, and loaded in adding the branch. Its lifetime is
2050+equal to the branch.
2051+The list is refered in lookup, and when the current target inode is
2052+found in the list, the aufs tries loading the "detailed info per
2053+directory" and get the changed and valid name of the dir.
2054+
2055+Theoretically these "extra informaiton" may be able to be put into XATTR
2056+in the dir inode. But aufs doesn't choose this way because
2057+1. XATTR may not be supported by the branch (or its configuration)
2058+2. XATTR may have its size limit.
2059+3. XATTR may be less easy to convert than a regular file, when the
2060+ format of the info is changed in the future.
2061+At the same time, I agree that the regular file approach is much slower
2062+than XATTR approach. So, in the future, aufs may take the XATTR or other
2063+better approach.
2064+
2065+This DIRREN feature is enabled by aufs configuration, and is activated
2066+by a new mount option.
2067+
2068+For the more complicated case, there is a work with UDBA option, which
2069+is to dected the direct access to the branches (by-passing aufs) and to
2070+maintain the cashes in aufs. Since a single cached aufs dentry may
2071+contains two names, before- and after-rename, the name comparision in
2072+UDBA handler may not work correctly. In this case, the behaviour will be
2073+equivalen to udba=reval case.
076b876e 2074diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
eca34b5c
AM
2075--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
2076+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2019-07-11 15:42:14.458904362 +0200
076b876e
AM
2077@@ -0,0 +1,120 @@
2078+
ba1aed25 2079+# Copyright (C) 2011-2019 Junjiro R. Okajima
076b876e
AM
2080+#
2081+# This program is free software; you can redistribute it and/or modify
2082+# it under the terms of the GNU General Public License as published by
2083+# the Free Software Foundation; either version 2 of the License, or
2084+# (at your option) any later version.
2085+#
2086+# This program is distributed in the hope that it will be useful,
2087+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2088+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2089+# GNU General Public License for more details.
2090+#
2091+# You should have received a copy of the GNU General Public License
2092+# along with this program; if not, write to the Free Software
2093+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2094+
2095+
2096+File-based Hierarchical Storage Management (FHSM)
2097+----------------------------------------------------------------------
2098+Hierarchical Storage Management (or HSM) is a well-known feature in the
2099+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 2100+writable branches, based upon the principle of "Colder, the Lower".
076b876e 2101+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 2102+that the position in the order of the stacked branches vertically.
076b876e
AM
2103+These multiple writable branches are prioritized, ie. the topmost one
2104+should be the fastest drive and be used heavily.
2105+
2106+o Characters in aufs FHSM story
2107+- aufs itself and a new branch attribute.
2108+- a new ioctl interface to move-down and to establish a connection with
2109+ the daemon ("move-down" is a converse of "copy-up").
2110+- userspace tool and daemon.
2111+
2112+The userspace daemon establishes a connection with aufs and waits for
2113+the notification. The notified information is very similar to struct
2114+statfs containing the number of consumed blocks and inodes.
2115+When the consumed blocks/inodes of a branch exceeds the user-specified
2116+upper watermark, the daemon activates its move-down process until the
2117+consumed blocks/inodes reaches the user-specified lower watermark.
2118+
2119+The actual move-down is done by aufs based upon the request from
2120+user-space since we need to maintain the inode number and the internal
2121+pointer arrays in aufs.
2122+
2123+Currently aufs FHSM handles the regular files only. Additionally they
2124+must not be hard-linked nor pseudo-linked.
2125+
2126+
2127+o Cowork of aufs and the user-space daemon
2128+ During the userspace daemon established the connection, aufs sends a
2129+ small notification to it whenever aufs writes something into the
2130+ writable branch. But it may cost high since aufs issues statfs(2)
2131+ internally. So user can specify a new option to cache the
2132+ info. Actually the notification is controlled by these factors.
2133+ + the specified cache time.
2134+ + classified as "force" by aufs internally.
2135+ Until the specified time expires, aufs doesn't send the info
2136+ except the forced cases. When aufs decide forcing, the info is always
2137+ notified to userspace.
2138+ For example, the number of free inodes is generally large enough and
2139+ the shortage of it happens rarely. So aufs doesn't force the
2140+ notification when creating a new file, directory and others. This is
2141+ the typical case which aufs doesn't force.
2142+ When aufs writes the actual filedata and the files consumes any of new
2143+ blocks, the aufs forces notifying.
2144+
2145+
2146+o Interfaces in aufs
2147+- New branch attribute.
2148+ + fhsm
2149+ Specifies that the branch is managed by FHSM feature. In other word,
2150+ participant in the FHSM.
2151+ When nofhsm is set to the branch, it will not be the source/target
2152+ branch of the move-down operation. This attribute is set
2153+ independently from coo and moo attributes, and if you want full
2154+ FHSM, you should specify them as well.
2155+- New mount option.
2156+ + fhsm_sec
2157+ Specifies a second to suppress many less important info to be
2158+ notified.
2159+- New ioctl.
2160+ + AUFS_CTL_FHSM_FD
2161+ create a new file descriptor which userspace can read the notification
2162+ (a subset of struct statfs) from aufs.
2163+- Module parameter 'brs'
2164+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2165+ be set.
2166+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2167+ When there are two or more branches with fhsm attributes,
2168+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2169+ terminates it. As a result of remounting and branch-manipulation, the
2170+ number of branches with fhsm attribute can be one. In this case,
2171+ /sbin/mount.aufs will terminate the user-space daemon.
2172+
2173+
2174+Finally the operation is done as these steps in kernel-space.
2175+- make sure that,
2176+ + no one else is using the file.
2177+ + the file is not hard-linked.
2178+ + the file is not pseudo-linked.
2179+ + the file is a regular file.
2180+ + the parent dir is not opaqued.
2181+- find the target writable branch.
2182+- make sure the file is not whiteout-ed by the upper (than the target)
2183+ branch.
2184+- make the parent dir on the target branch.
2185+- mutex lock the inode on the branch.
2186+- unlink the whiteout on the target branch (if exists).
2187+- lookup and create the whiteout-ed temporary name on the target branch.
2188+- copy the file as the whiteout-ed temporary name on the target branch.
2189+- rename the whiteout-ed temporary name to the original name.
2190+- unlink the file on the source branch.
2191+- maintain the internal pointer array and the external inode number
2192+ table (XINO).
2193+- maintain the timestamps and other attributes of the parent dir and the
2194+ file.
2195+
2196+And of course, in every step, an error may happen. So the operation
2197+should restore the original file state after an error happens.
53392da6 2198diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
eca34b5c
AM
2199--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
2200+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2019-07-11 15:42:14.458904362 +0200
b912730e 2201@@ -0,0 +1,72 @@
53392da6 2202+
ba1aed25 2203+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
2204+#
2205+# This program is free software; you can redistribute it and/or modify
2206+# it under the terms of the GNU General Public License as published by
2207+# the Free Software Foundation; either version 2 of the License, or
2208+# (at your option) any later version.
2209+#
2210+# This program is distributed in the hope that it will be useful,
2211+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2212+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2213+# GNU General Public License for more details.
2214+#
2215+# You should have received a copy of the GNU General Public License
523b37e3 2216+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2217+
2218+mmap(2) -- File Memory Mapping
2219+----------------------------------------------------------------------
2220+In aufs, the file-mapped pages are handled by a branch fs directly, no
2221+interaction with aufs. It means aufs_mmap() calls the branch fs's
2222+->mmap().
2223+This approach is simple and good, but there is one problem.
7e9cd9fe 2224+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2225+device and inode number), and the printed path will be the path on the
2226+branch fs's instead of virtual aufs's.
2227+This is not a problem in most cases, but some utilities lsof(1) (and its
2228+user) may expect the path on aufs.
2229+
2230+To address this issue, aufs adds a new member called vm_prfile in struct
2231+vm_area_struct (and struct vm_region). The original vm_file points to
2232+the file on the branch fs in order to handle everything correctly as
2233+usual. The new vm_prfile points to a virtual file in aufs, and the
2234+show-functions in procfs refers to vm_prfile if it is set.
2235+Also we need to maintain several other places where touching vm_file
2236+such like
2237+- fork()/clone() copies vma and the reference count of vm_file is
2238+ incremented.
2239+- merging vma maintains the ref count too.
2240+
7e9cd9fe 2241+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2242+leaves all behaviour around f_mapping unchanged. This is surely an
2243+advantage.
2244+Actually aufs had adopted another complicated approach which calls
2245+generic_file_mmap() and handles struct vm_operations_struct. In this
2246+approach, aufs met a hard problem and I could not solve it without
2247+switching the approach.
b912730e
AM
2248+
2249+There may be one more another approach which is
2250+- bind-mount the branch-root onto the aufs-root internally
2251+- grab the new vfsmount (ie. struct mount)
2252+- lazy-umount the branch-root internally
2253+- in open(2) the aufs-file, open the branch-file with the hidden
2254+ vfsmount (instead of the original branch's vfsmount)
2255+- ideally this "bind-mount and lazy-umount" should be done atomically,
2256+ but it may be possible from userspace by the mount helper.
2257+
2258+Adding the internal hidden vfsmount and using it in opening a file, the
2259+file path under /proc will be printed correctly. This approach looks
2260+smarter, but is not possible I am afraid.
2261+- aufs-root may be bind-mount later. when it happens, another hidden
2262+ vfsmount will be required.
2263+- it is hard to get the chance to bind-mount and lazy-umount
2264+ + in kernel-space, FS can have vfsmount in open(2) via
2265+ file->f_path, and aufs can know its vfsmount. But several locks are
2266+ already acquired, and if aufs tries to bind-mount and lazy-umount
2267+ here, then it may cause a deadlock.
2268+ + in user-space, bind-mount doesn't invoke the mount helper.
2269+- since /proc shows dev and ino, aufs has to give vma these info. it
2270+ means a new member vm_prinode will be necessary. this is essentially
2271+ equivalent to vm_prfile described above.
2272+
2273+I have to give up this "looks-smater" approach.
c1595e42 2274diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
eca34b5c
AM
2275--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
2276+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2019-07-11 15:42:14.458904362 +0200
c1595e42
JR
2277@@ -0,0 +1,96 @@
2278+
ba1aed25 2279+# Copyright (C) 2014-2019 Junjiro R. Okajima
c1595e42
JR
2280+#
2281+# This program is free software; you can redistribute it and/or modify
2282+# it under the terms of the GNU General Public License as published by
2283+# the Free Software Foundation; either version 2 of the License, or
2284+# (at your option) any later version.
2285+#
2286+# This program is distributed in the hope that it will be useful,
2287+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2288+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2289+# GNU General Public License for more details.
2290+#
2291+# You should have received a copy of the GNU General Public License
2292+# along with this program; if not, write to the Free Software
2293+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2294+
2295+
2296+Listing XATTR/EA and getting the value
2297+----------------------------------------------------------------------
2298+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2299+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2300+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2301+information. But for the directories, aufs considers all the same named
2302+entries on the lower branches. Which means, if one of the lower entry
2303+rejects readdir call, then aufs returns an error even if the topmost
2304+entry allows it. This behaviour is necessary to respect the branch fs's
2305+security, but can make users confused since the user-visible standard
2306+attributes don't match the behaviour.
2307+To address this issue, aufs has a mount option called dirperm1 which
2308+checks the permission for the topmost entry only, and ignores the lower
2309+entry's permission.
2310+
2311+A similar issue can happen around XATTR.
2312+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2313+always set. Otherwise these very unpleasant situation would happen.
2314+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2315+- users may not be able to remove or reset the XATTR forever,
2316+
2317+
2318+XATTR/EA support in the internal (copy,move)-(up,down)
2319+----------------------------------------------------------------------
7e9cd9fe 2320+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2321+- "security" for LSM and capability.
2322+- "system" for posix ACL, 'acl' mount option is required for the branch
2323+ fs generally.
2324+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2325+- "user" for userspace, 'user_xattr' mount option is required for the
2326+ branch fs generally.
2327+
2328+Moreover there are some other categories. Aufs handles these rather
2329+unpopular categories as the ordinary ones, ie. there is no special
2330+condition nor exception.
2331+
2332+In copy-up, the support for XATTR on the dst branch may differ from the
2333+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2334+the original user operation which triggered the copy-up will fail. It
2335+can happen that even all copy-up will fail.
c1595e42
JR
2336+When both of src and dst branches support XATTR and if an error occurs
2337+during copying XATTR, then the copy-up should fail obviously. That is a
2338+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2339+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2340+For example, the src branch supports ACL but the dst branch doesn't
2341+because the dst branch may natively un-support it or temporary
2342+un-support it due to "noacl" mount option. Of course, the dst branch fs
2343+may NOT return an error even if the XATTR is not supported. It is
2344+totally up to the branch fs.
2345+
2346+Anyway when the aufs internal copy-up gets an error from the dst branch
2347+fs, then aufs tries removing the just copied entry and returns the error
2348+to the userspace. The worst case of this situation will be all copy-up
2349+will fail.
2350+
2351+For the copy-up operation, there two basic approaches.
2352+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2353+ error unconditionally if it happens.
c1595e42
JR
2354+- copy all XATTR, and ignore the error on the specified category only.
2355+
2356+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2357+chooses the latter approach and introduces some new branch attributes,
2358+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2359+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2360+convenient, "icex" is also provided which means all "icex*" attributes
2361+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2362+
2363+The meaning of these attributes is to ignore the error from setting
2364+XATTR on that branch.
2365+Note that aufs tries copying all XATTR unconditionally, and ignores the
2366+error from the dst branch according to the specified attributes.
2367+
2368+Some XATTR may have its default value. The default value may come from
2369+the parent dir or the environment. If the default value is set at the
2370+file creating-time, it will be overwritten by copy-up.
2371+Some contradiction may happen I am afraid.
2372+Do we need another attribute to stop copying XATTR? I am unsure. For
2373+now, aufs implements the branch attributes to ignore the error.
53392da6 2374diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
eca34b5c
AM
2375--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2376+++ linux/Documentation/filesystems/aufs/design/07export.txt 2019-07-11 15:42:14.458904362 +0200
523b37e3 2377@@ -0,0 +1,58 @@
53392da6 2378+
ba1aed25 2379+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
2380+#
2381+# This program is free software; you can redistribute it and/or modify
2382+# it under the terms of the GNU General Public License as published by
2383+# the Free Software Foundation; either version 2 of the License, or
2384+# (at your option) any later version.
2385+#
2386+# This program is distributed in the hope that it will be useful,
2387+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2388+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2389+# GNU General Public License for more details.
2390+#
2391+# You should have received a copy of the GNU General Public License
523b37e3 2392+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2393+
2394+Export Aufs via NFS
2395+----------------------------------------------------------------------
2396+Here is an approach.
2397+- like xino/xib, add a new file 'xigen' which stores aufs inode
2398+ generation.
2399+- iget_locked(): initialize aufs inode generation for a new inode, and
2400+ store it in xigen file.
2401+- destroy_inode(): increment aufs inode generation and store it in xigen
2402+ file. it is necessary even if it is not unlinked, because any data of
2403+ inode may be changed by UDBA.
2404+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2405+ build file handle by
2406+ + branch id (4 bytes)
2407+ + superblock generation (4 bytes)
2408+ + inode number (4 or 8 bytes)
2409+ + parent dir inode number (4 or 8 bytes)
2410+ + inode generation (4 bytes))
2411+ + return value of exportfs_encode_fh() for the parent on a branch (4
2412+ bytes)
2413+ + file handle for a branch (by exportfs_encode_fh())
2414+- fh_to_dentry():
2415+ + find the index of a branch from its id in handle, and check it is
2416+ still exist in aufs.
2417+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2418+ + 2nd level: if not found in cache, get the parent inode number from
2419+ the handle and search it in cache. and then open the found parent
2420+ dir, find the matching inode number by vfs_readdir() and get its
2421+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2422+ + 3rd level: if the parent dir is not cached, call
2423+ exportfs_decode_fh() for a branch and get the parent on a branch,
2424+ build a pathname of it, convert it a pathname in aufs, call
2425+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2426+ the 2nd level.
2427+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2428+ for every branch, but not itself. to get this, (currently) aufs
2429+ searches in current->nsproxy->mnt_ns list. it may not be a good
2430+ idea, but I didn't get other approach.
2431+ + test the generation of the gotten inode.
2432+- every inode operation: they may get EBUSY due to UDBA. in this case,
2433+ convert it into ESTALE for NFSD.
2434+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2435+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2436diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
eca34b5c
AM
2437--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
2438+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2019-07-11 15:42:14.458904362 +0200
523b37e3 2439@@ -0,0 +1,52 @@
53392da6 2440+
ba1aed25 2441+# Copyright (C) 2005-2019 Junjiro R. Okajima
53392da6
AM
2442+#
2443+# This program is free software; you can redistribute it and/or modify
2444+# it under the terms of the GNU General Public License as published by
2445+# the Free Software Foundation; either version 2 of the License, or
2446+# (at your option) any later version.
2447+#
2448+# This program is distributed in the hope that it will be useful,
2449+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2450+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2451+# GNU General Public License for more details.
2452+#
2453+# You should have received a copy of the GNU General Public License
523b37e3 2454+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2455+
2456+Show Whiteout Mode (shwh)
2457+----------------------------------------------------------------------
2458+Generally aufs hides the name of whiteouts. But in some cases, to show
2459+them is very useful for users. For instance, creating a new middle layer
2460+(branch) by merging existing layers.
2461+
2462+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2463+When you have three branches,
2464+- Bottom: 'system', squashfs (underlying base system), read-only
2465+- Middle: 'mods', squashfs, read-only
2466+- Top: 'overlay', ram (tmpfs), read-write
2467+
2468+The top layer is loaded at boot time and saved at shutdown, to preserve
2469+the changes made to the system during the session.
2470+When larger changes have been made, or smaller changes have accumulated,
2471+the size of the saved top layer data grows. At this point, it would be
2472+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2473+and rewrite the 'mods' squashfs, clearing the top layer and thus
2474+restoring save and load speed.
2475+
2476+This merging is simplified by the use of another aufs mount, of just the
2477+two overlay branches using the 'shwh' option.
2478+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2479+ aufs /livesys/merge_union
2480+
2481+A merged view of these two branches is then available at
2482+/livesys/merge_union, and the new feature is that the whiteouts are
2483+visible!
2484+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2485+writing to all branches. Also the default mode for all branches is 'ro'.
2486+It is now possible to save the combined contents of the two overlay
2487+branches to a new squashfs, e.g.:
2488+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2489+
2490+This new squashfs archive can be stored on the boot device and the
2491+initramfs will use it to replace the old one at the next boot.
2492diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
eca34b5c
AM
2493--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
2494+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2019-07-11 15:42:14.458904362 +0200
7e9cd9fe 2495@@ -0,0 +1,47 @@
53392da6 2496+
ba1aed25 2497+# Copyright (C) 2010-2019 Junjiro R. Okajima
53392da6
AM
2498+#
2499+# This program is free software; you can redistribute it and/or modify
2500+# it under the terms of the GNU General Public License as published by
2501+# the Free Software Foundation; either version 2 of the License, or
2502+# (at your option) any later version.
2503+#
2504+# This program is distributed in the hope that it will be useful,
2505+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2506+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2507+# GNU General Public License for more details.
2508+#
2509+# You should have received a copy of the GNU General Public License
523b37e3 2510+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2511+
2512+Dynamically customizable FS operations
2513+----------------------------------------------------------------------
2514+Generally FS operations (struct inode_operations, struct
2515+address_space_operations, struct file_operations, etc.) are defined as
2516+"static const", but it never means that FS have only one set of
2517+operation. Some FS have multiple sets of them. For instance, ext2 has
2518+three sets, one for XIP, for NOBH, and for normal.
2519+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2520+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2521+VFS acts differently if a function (member in the struct) is set or
2522+not. It means aufs should have several sets of operations and select one
2523+among them according to the branch FS definition.
2524+
7e9cd9fe 2525+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2526+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2527+dummy direct_IO function for struct address_space_operations, but it may
2528+not be set to the address_space_operations actually. When the branch FS
2529+doesn't have it, aufs doesn't set it to its address_space_operations
2530+while the function definition itself is still alive. So the behaviour
2531+itself will not change, and it will return an error when direct_IO is
2532+not set.
53392da6
AM
2533+
2534+The lifetime of these dynamically generated operation object is
2535+maintained by aufs branch object. When the branch is removed from aufs,
2536+the reference counter of the object is decremented. When it reaches
2537+zero, the dynamically generated operation object will be freed.
2538+
7e9cd9fe
AM
2539+This approach is designed to support AIO (io_submit), Direct I/O and
2540+XIP (DAX) mainly.
2541+Currently this approach is applied to address_space_operations for
2542+regular files only.
53392da6 2543diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
eca34b5c
AM
2544--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
2545+++ linux/Documentation/filesystems/aufs/README 2019-07-11 15:42:14.455570938 +0200
fbc438ed 2546@@ -0,0 +1,399 @@
53392da6 2547+
fbc438ed 2548+Aufs5 -- advanced multi layered unification filesystem version 5.x
53392da6
AM
2549+http://aufs.sf.net
2550+Junjiro R. Okajima
2551+
2552+
2553+0. Introduction
2554+----------------------------------------
2555+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2556+Unionfs Version 1.x series. Adding many original ideas, approaches,
fbc438ed 2557+improvements and implementations, it became totally different from
53392da6 2558+Unionfs while keeping the basic features.
fbc438ed 2559+Later, Unionfs Version 2.x series began taking some of the same
53392da6 2560+approaches to aufs1's.
fbc438ed 2561+Unionfs was being developed by Professor Erez Zadok at Stony Brook
53392da6
AM
2562+University and his team.
2563+
fbc438ed
JR
2564+Aufs5 supports linux-v5.0 and later, If you want older kernel version
2565+support,
2566+- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2567+- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2568+- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2569+ or aufs1 from CVS on SourceForge.
53392da6
AM
2570+
2571+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2572+ According to Christoph Hellwig, linux rejects all union-type
2573+ filesystems but UnionMount.
53392da6
AM
2574+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2575+
38d290e6
JR
2576+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2577+ UnionMount, and he pointed out an issue around a directory mutex
2578+ lock and aufs addressed it. But it is still unsure whether aufs will
2579+ be merged (or any other union solution).
076b876e 2580+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2581+
53392da6
AM
2582+
2583+1. Features
2584+----------------------------------------
2585+- unite several directories into a single virtual filesystem. The member
2586+ directory is called as a branch.
2587+- you can specify the permission flags to the branch, which are 'readonly',
2588+ 'readwrite' and 'whiteout-able.'
2589+- by upper writable branch, internal copyup and whiteout, files/dirs on
2590+ readonly branch are modifiable logically.
2591+- dynamic branch manipulation, add, del.
2592+- etc...
2593+
7e9cd9fe
AM
2594+Also there are many enhancements in aufs, such as:
2595+- test only the highest one for the directory permission (dirperm1)
2596+- copyup on open (coo=)
2597+- 'move' policy for copy-up between two writable branches, after
2598+ checking free space.
2599+- xattr, acl
53392da6
AM
2600+- readdir(3) in userspace.
2601+- keep inode number by external inode number table
2602+- keep the timestamps of file/dir in internal copyup operation
2603+- seekable directory, supporting NFS readdir.
2604+- whiteout is hardlinked in order to reduce the consumption of inodes
2605+ on branch
2606+- do not copyup, nor create a whiteout when it is unnecessary
2607+- revert a single systemcall when an error occurs in aufs
2608+- remount interface instead of ioctl
2609+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2610+- loopback mounted filesystem as a branch
2611+- kernel thread for removing the dir who has a plenty of whiteouts
2612+- support copyup sparse file (a file which has a 'hole' in it)
2613+- default permission flags for branches
2614+- selectable permission flags for ro branch, whether whiteout can
2615+ exist or not
2616+- export via NFS.
2617+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2618+- support multiple writable branches, some policies to select one
2619+ among multiple writable branches.
2620+- a new semantics for link(2) and rename(2) to support multiple
2621+ writable branches.
2622+- no glibc changes are required.
2623+- pseudo hardlink (hardlink over branches)
2624+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2625+ including NFS or remote filesystem branch.
2626+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2627+- and more...
2628+
fbc438ed 2629+Currently these features are dropped temporary from aufs5.
53392da6 2630+See design/08plan.txt in detail.
53392da6
AM
2631+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2632+ (robr)
2633+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2634+
2635+Features or just an idea in the future (see also design/*.txt),
2636+- reorder the branch index without del/re-add.
2637+- permanent xino files for NFSD
2638+- an option for refreshing the opened files after add/del branches
53392da6
AM
2639+- light version, without branch manipulation. (unnecessary?)
2640+- copyup in userspace
2641+- inotify in userspace
2642+- readv/writev
53392da6
AM
2643+
2644+
2645+2. Download
2646+----------------------------------------
fbc438ed
JR
2647+There are three GIT trees for aufs5, aufs5-linux.git,
2648+aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
1e00d052 2649+"aufs-util.git."
fbc438ed
JR
2650+While the aufs-util is always necessary, you need either of aufs5-linux
2651+or aufs5-standalone.
1e00d052 2652+
fbc438ed 2653+The aufs5-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2654+git://git.kernel.org/.../torvalds/linux.git.
2655+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
fbc438ed 2656+build aufs5 as an external kernel module.
2000de60 2657+Several extra patches are not included in this tree. Only
fbc438ed 2658+aufs5-standalone tree contains them. They are described in the later
2000de60 2659+section "Configuration and Compilation."
1e00d052 2660+
fbc438ed 2661+On the other hand, the aufs5-standalone tree has only aufs source files
53392da6 2662+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2663+But you need to apply all aufs patches manually.
53392da6 2664+
fbc438ed
JR
2665+You will find GIT branches whose name is in form of "aufs5.x" where "x"
2666+represents the linux kernel version, "linux-5.x". For instance,
2667+"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2668+"aufs5.x-rcN" branch.
1e00d052 2669+
fbc438ed 2670+o aufs5-linux tree
1e00d052 2671+$ git clone --reference /your/linux/git/tree \
fbc438ed 2672+ git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
1e00d052 2673+- if you don't have linux GIT tree, then remove "--reference ..."
fbc438ed
JR
2674+$ cd aufs5-linux.git
2675+$ git checkout origin/aufs5.0
53392da6 2676+
2000de60
JR
2677+Or You may want to directly git-pull aufs into your linux GIT tree, and
2678+leave the patch-work to GIT.
2679+$ cd /your/linux/git/tree
fbc438ed
JR
2680+$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2681+$ git fetch aufs5
2682+$ git checkout -b my5.0 v5.0
5527c038 2683+$ (add your local change...)
fbc438ed
JR
2684+$ git pull aufs5 aufs5.0
2685+- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2000de60 2686+- you may need to solve some conflicts between your_changes and
fbc438ed
JR
2687+ aufs5.0. in this case, git-rerere is recommended so that you can
2688+ solve the similar conflicts automatically when you upgrade to 5.1 or
2000de60
JR
2689+ later in the future.
2690+
fbc438ed
JR
2691+o aufs5-standalone tree
2692+$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2693+$ cd aufs5-standalone.git
2694+$ git checkout origin/aufs5.0
53392da6
AM
2695+
2696+o aufs-util tree
5527c038
JR
2697+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2698+- note that the public aufs-util.git is on SourceForge instead of
2699+ GitHUB.
53392da6 2700+$ cd aufs-util.git
fbc438ed 2701+$ git checkout origin/aufs5.0
53392da6 2702+
fbc438ed
JR
2703+Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2704+The minor version number, 'x' in '5.x', of aufs may not always
9dbd164d
AM
2705+follow the minor version number of the kernel.
2706+Because changes in the kernel that cause the use of a new
2707+minor version number do not always require changes to aufs-util.
2708+
2709+Since aufs-util has its own minor version number, you may not be
2710+able to find a GIT branch in aufs-util for your kernel's
2711+exact minor version number.
2712+In this case, you should git-checkout the branch for the
53392da6 2713+nearest lower number.
9dbd164d
AM
2714+
2715+For (an unreleased) example:
fbc438ed
JR
2716+If you are using "linux-5.10" and the "aufs5.10" branch
2717+does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
9dbd164d
AM
2718+or something numerically smaller is the branch for your kernel.
2719+
53392da6
AM
2720+Also you can view all branches by
2721+ $ git branch -a
2722+
2723+
2724+3. Configuration and Compilation
2725+----------------------------------------
2726+Make sure you have git-checkout'ed the correct branch.
2727+
fbc438ed 2728+For aufs5-linux tree,
c06a8ce3 2729+- enable CONFIG_AUFS_FS.
1e00d052
AM
2730+- set other aufs configurations if necessary.
2731+
fbc438ed 2732+For aufs5-standalone tree,
53392da6
AM
2733+There are several ways to build.
2734+
2735+1.
fbc438ed
JR
2736+- apply ./aufs5-kbuild.patch to your kernel source files.
2737+- apply ./aufs5-base.patch too.
2738+- apply ./aufs5-mmap.patch too.
2739+- apply ./aufs5-standalone.patch too, if you have a plan to set
2740+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
537831f9
AM
2741+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2742+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2743+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2744+ =m or =y.
2745+- and build your kernel as usual.
2746+- install the built kernel.
7eafdf33
AM
2747+- install the header files too by "make headers_install" to the
2748+ directory where you specify. By default, it is $PWD/usr.
b4510431 2749+ "make help" shows a brief note for headers_install.
53392da6
AM
2750+- and reboot your system.
2751+
2752+2.
2753+- module only (CONFIG_AUFS_FS=m).
fbc438ed
JR
2754+- apply ./aufs5-base.patch to your kernel source files.
2755+- apply ./aufs5-mmap.patch too.
2756+- apply ./aufs5-standalone.patch too.
53392da6
AM
2757+- build your kernel, don't forget "make headers_install", and reboot.
2758+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2759+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2760+ every aufs configurations.
2761+- build the module by simple "make".
2762+- you can specify ${KDIR} make variable which points to your kernel
2763+ source tree.
2764+- install the files
2765+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2766+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2767+ + run "make install_headers" (instead of headers_install) to install
2768+ the modified aufs header file (you can specify DESTDIR which is
2769+ available in aufs standalone version's Makefile only), or copy
2770+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2771+ you like manually. By default, the target directory is $PWD/usr.
fbc438ed 2772+- no need to apply aufs5-kbuild.patch, nor copying source files to your
53392da6
AM
2773+ kernel source tree.
2774+
b4510431 2775+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2776+ as well as "make headers_install" in the kernel source tree.
2777+ headers_install is subject to be forgotten, but it is essentially
2778+ necessary, not only for building aufs-util.
2779+ You may not meet problems without headers_install in some older
2780+ version though.
2781+
2782+And then,
2783+- read README in aufs-util, build and install it
9dbd164d
AM
2784+- note that your distribution may contain an obsoleted version of
2785+ aufs_type.h in /usr/include/linux or something. When you build aufs
2786+ utilities, make sure that your compiler refers the correct aufs header
2787+ file which is built by "make headers_install."
53392da6
AM
2788+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2789+ then run "make install_ulib" too. And refer to the aufs manual in
2790+ detail.
2791+
fbc438ed 2792+There several other patches in aufs5-standalone.git. They are all
38d290e6 2793+optional. When you meet some problems, they will help you.
fbc438ed 2794+- aufs5-loopback.patch
38d290e6
JR
2795+ Supports a nested loopback mount in a branch-fs. This patch is
2796+ unnecessary until aufs produces a message like "you may want to try
2797+ another patch for loopback file".
83b672a5
AM
2798+- proc_mounts.patch
2799+ When there are many mountpoints and many mount(2)/umount(2) are
2800+ running, then /proc/mounts may not show the all mountpoints. This
2801+ patch makes /proc/mounts always show the full mountpoints list.
2802+ If you don't want to apply this patch and meet such problem, then you
2803+ need to increase the value of 'ProcMounts_Times' make-variable in
2804+ aufs-util.git as a second best solution.
38d290e6
JR
2805+- vfs-ino.patch
2806+ Modifies a system global kernel internal function get_next_ino() in
2807+ order to stop assigning 0 for an inode-number. Not directly related to
2808+ aufs, but recommended generally.
2809+- tmpfs-idr.patch
2810+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2811+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2812+ duplication of inode number, which is important for backup tools and
2813+ other utilities. When you find aufs XINO files for tmpfs branch
2814+ growing too much, try this patch.
be52b249
AM
2815+- lockdep-debug.patch
2816+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2817+ also a caller of VFS functions for branch filesystems, subclassing of
2818+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2819+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2820+ need to apply this debug patch to expand several constant values.
fbc438ed 2821+ If don't know what LOCKDEP is, then you don't have apply this patch.
38d290e6 2822+
53392da6
AM
2823+
2824+4. Usage
2825+----------------------------------------
2826+At first, make sure aufs-util are installed, and please read the aufs
2827+manual, aufs.5 in aufs-util.git tree.
2828+$ man -l aufs.5
2829+
2830+And then,
2831+$ mkdir /tmp/rw /tmp/aufs
2832+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2833+
2834+Here is another example. The result is equivalent.
2835+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2836+ Or
2837+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2838+# mount -o remount,append:${HOME} /tmp/aufs
2839+
2840+Then, you can see whole tree of your home dir through /tmp/aufs. If
2841+you modify a file under /tmp/aufs, the one on your home directory is
2842+not affected, instead the same named file will be newly created under
2843+/tmp/rw. And all of your modification to a file will be applied to
2844+the one under /tmp/rw. This is called the file based Copy on Write
2845+(COW) method.
2846+Aufs mount options are described in aufs.5.
2847+If you run chroot or something and make your aufs as a root directory,
2848+then you need to customize the shutdown script. See the aufs manual in
2849+detail.
2850+
2851+Additionally, there are some sample usages of aufs which are a
2852+diskless system with network booting, and LiveCD over NFS.
2853+See sample dir in CVS tree on SourceForge.
2854+
2855+
2856+5. Contact
2857+----------------------------------------
2858+When you have any problems or strange behaviour in aufs, please let me
2859+know with:
2860+- /proc/mounts (instead of the output of mount(8))
2861+- /sys/module/aufs/*
2862+- /sys/fs/aufs/* (if you have them)
2863+- /debug/aufs/* (if you have them)
2864+- linux kernel version
2865+ if your kernel is not plain, for example modified by distributor,
2866+ the url where i can download its source is necessary too.
2867+- aufs version which was printed at loading the module or booting the
2868+ system, instead of the date you downloaded.
2869+- configuration (define/undefine CONFIG_AUFS_xxx)
2870+- kernel configuration or /proc/config.gz (if you have it)
2871+- behaviour which you think to be incorrect
2872+- actual operation, reproducible one is better
2873+- mailto: aufs-users at lists.sourceforge.net
2874+
2875+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2876+and Feature Requests) on SourceForge. Please join and write to
2877+aufs-users ML.
2878+
2879+
2880+6. Acknowledgements
2881+----------------------------------------
2882+Thanks to everyone who have tried and are using aufs, whoever
2883+have reported a bug or any feedback.
2884+
2885+Especially donators:
2886+Tomas Matejicek(slax.org) made a donation (much more than once).
2887+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2888+ scripts) is making "doubling" donations.
2889+ Unfortunately I cannot list all of the donators, but I really
b4510431 2890+ appreciate.
53392da6
AM
2891+ It ends Aug 2010, but the ordinary donation URL is still available.
2892+ <http://sourceforge.net/donate/index.php?group_id=167503>
2893+Dai Itasaka made a donation (2007/8).
2894+Chuck Smith made a donation (2008/4, 10 and 12).
2895+Henk Schoneveld made a donation (2008/9).
2896+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2897+Francois Dupoux made a donation (2008/11).
2898+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2899+ aufs2 GIT tree (2009/2).
2900+William Grant made a donation (2009/3).
2901+Patrick Lane made a donation (2009/4).
2902+The Mail Archive (mail-archive.com) made donations (2009/5).
2903+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2904+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2905+Pavel Pronskiy made a donation (2011/2).
2906+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2907+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
2908+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2909+11).
1e00d052 2910+Sam Liddicott made a donation (2011/9).
86dc4139
AM
2911+Era Scarecrow made a donation (2013/4).
2912+Bor Ratajc made a donation (2013/4).
2913+Alessandro Gorreta made a donation (2013/4).
2914+POIRETTE Marc made a donation (2013/4).
2915+Alessandro Gorreta made a donation (2013/4).
2916+lauri kasvandik made a donation (2013/5).
392086de 2917+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
2918+The Parted Magic Project made a donation (2013/9 and 11).
2919+Pavel Barta made a donation (2013/10).
38d290e6 2920+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 2921+James B made a donation (2014/7 and 2015/7).
076b876e 2922+Stefano Di Biase made a donation (2014/8).
2000de60 2923+Daniel Epellei made a donation (2015/1).
c4adf169 2924+OmegaPhil made a donation (2016/1, 2018/4).
5afbbe0d 2925+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 2926+James Burry made a donation (2016/12).
acd2b654 2927+Carsten Rose made a donation (2018/9).
9f237c51 2928+Porteus Kiosk made a donation (2018/10).
53392da6
AM
2929+
2930+Thank you very much.
2931+Donations are always, including future donations, very important and
2932+helpful for me to keep on developing aufs.
2933+
2934+
2935+7.
2936+----------------------------------------
2937+If you are an experienced user, no explanation is needed. Aufs is
2938+just a linux filesystem.
2939+
2940+
2941+Enjoy!
2942+
2943+# Local variables: ;
2944+# mode: text;
2945+# End: ;
7f207e10 2946diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
eca34b5c
AM
2947--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
2948+++ linux/fs/aufs/aufs.h 2019-07-11 15:42:14.462237786 +0200
acd2b654 2949@@ -0,0 +1,62 @@
062440b3 2950+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 2951+/*
ba1aed25 2952+ * Copyright (C) 2005-2019 Junjiro R. Okajima
7f207e10
AM
2953+ *
2954+ * This program, aufs is free software; you can redistribute it and/or modify
2955+ * it under the terms of the GNU General Public License as published by
2956+ * the Free Software Foundation; either version 2 of the License, or
2957+ * (at your option) any later version.
2958+ *
2959+ * This program is distributed in the hope that it will be useful,
2960+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2961+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2962+ * GNU General Public License for more details.
2963+ *
2964+ * You should have received a copy of the GNU General Public License
523b37e3 2965+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
2966+ */
2967+
2968+/*
2969+ * all header files
2970+ */
2971+
2972+#ifndef __AUFS_H__
2973+#define __AUFS_H__
2974+
2975+#ifdef __KERNEL__
2976+
2977+#define AuStub(type, name, body, ...) \
2978+ static inline type name(__VA_ARGS__) { body; }
2979+
2980+#define AuStubVoid(name, ...) \
2981+ AuStub(void, name, , __VA_ARGS__)
2982+#define AuStubInt0(name, ...) \
2983+ AuStub(int, name, return 0, __VA_ARGS__)
2984+
2985+#include "debug.h"
2986+
2987+#include "branch.h"
2988+#include "cpup.h"
2989+#include "dcsub.h"
2990+#include "dbgaufs.h"
2991+#include "dentry.h"
2992+#include "dir.h"
8b6a4947 2993+#include "dirren.h"
7f207e10
AM
2994+#include "dynop.h"
2995+#include "file.h"
2996+#include "fstype.h"
8b6a4947 2997+#include "hbl.h"
7f207e10 2998+#include "inode.h"
acd2b654 2999+#include "lcnt.h"
7f207e10
AM
3000+#include "loop.h"
3001+#include "module.h"
7f207e10
AM
3002+#include "opts.h"
3003+#include "rwsem.h"
7f207e10
AM
3004+#include "super.h"
3005+#include "sysaufs.h"
3006+#include "vfsub.h"
3007+#include "whout.h"
3008+#include "wkq.h"
3009+
3010+#endif /* __KERNEL__ */
3011+#endif /* __AUFS_H__ */
3012diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
eca34b5c
AM
3013--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
3014+++ linux/fs/aufs/branch.c 2019-09-16 09:38:43.216175640 +0200
3015@@ -0,0 +1,1428 @@
cd7a4cd9 3016+// SPDX-License-Identifier: GPL-2.0
7f207e10 3017+/*
ba1aed25 3018+ * Copyright (C) 2005-2019 Junjiro R. Okajima
7f207e10
AM
3019+ *
3020+ * This program, aufs is free software; you can redistribute it and/or modify
3021+ * it under the terms of the GNU General Public License as published by
3022+ * the Free Software Foundation; either version 2 of the License, or
3023+ * (at your option) any later version.
3024+ *
3025+ * This program is distributed in the hope that it will be useful,
3026+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3027+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3028+ * GNU General Public License for more details.
3029+ *
3030+ * You should have received a copy of the GNU General Public License
523b37e3 3031+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3032+ */
3033+
3034+/*
3035+ * branch management
3036+ */
3037+
027c5e7a 3038+#include <linux/compat.h>
7f207e10
AM
3039+#include <linux/statfs.h>
3040+#include "aufs.h"
3041+
3042+/*
3043+ * free a single branch
1facf9fc 3044+ */
3045+static void au_br_do_free(struct au_branch *br)
3046+{
3047+ int i;
3048+ struct au_wbr *wbr;
4a4d8108 3049+ struct au_dykey **key;
1facf9fc 3050+
027c5e7a 3051+ au_hnotify_fin_br(br);
8b6a4947
AM
3052+ /* always, regardless the mount option */
3053+ au_dr_hino_free(&br->br_dirren);
062440b3 3054+ au_xino_put(br);
1facf9fc 3055+
acd2b654
AM
3056+ AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
3057+ au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
3058+ AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
3059+ au_lcnt_fin(&br->br_count, /*do_sync*/0);
1facf9fc 3060+
3061+ wbr = br->br_wbr;
3062+ if (wbr) {
3063+ for (i = 0; i < AuBrWh_Last; i++)
3064+ dput(wbr->wbr_wh[i]);
3065+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 3066+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 3067+ }
3068+
076b876e
AM
3069+ if (br->br_fhsm) {
3070+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 3071+ au_kfree_try_rcu(br->br_fhsm);
076b876e
AM
3072+ }
3073+
4a4d8108
AM
3074+ key = br->br_dykey;
3075+ for (i = 0; i < AuBrDynOp; i++, key++)
3076+ if (*key)
3077+ au_dy_put(*key);
3078+ else
3079+ break;
3080+
537831f9 3081+ /* recursive lock, s_umount of branch's */
acd2b654 3082+ /* synchronize_rcu(); */ /* why? */
537831f9 3083+ lockdep_off();
86dc4139 3084+ path_put(&br->br_path);
537831f9 3085+ lockdep_on();
9f237c51 3086+ au_kfree_rcu(wbr);
acd2b654
AM
3087+ au_lcnt_wait_for_fin(&br->br_nfiles);
3088+ au_lcnt_wait_for_fin(&br->br_count);
3089+ /* I don't know why, but percpu_refcount requires this */
3090+ /* synchronize_rcu(); */
9f237c51 3091+ au_kfree_rcu(br);
1facf9fc 3092+}
3093+
3094+/*
3095+ * frees all branches
3096+ */
3097+void au_br_free(struct au_sbinfo *sbinfo)
3098+{
3099+ aufs_bindex_t bmax;
3100+ struct au_branch **br;
3101+
dece6358
AM
3102+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3103+
5afbbe0d 3104+ bmax = sbinfo->si_bbot + 1;
1facf9fc 3105+ br = sbinfo->si_branch;
3106+ while (bmax--)
3107+ au_br_do_free(*br++);
3108+}
3109+
3110+/*
3111+ * find the index of a branch which is specified by @br_id.
3112+ */
3113+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3114+{
5afbbe0d 3115+ aufs_bindex_t bindex, bbot;
1facf9fc 3116+
5afbbe0d
AM
3117+ bbot = au_sbbot(sb);
3118+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3119+ if (au_sbr_id(sb, bindex) == br_id)
3120+ return bindex;
3121+ return -1;
3122+}
3123+
3124+/* ---------------------------------------------------------------------- */
3125+
3126+/*
3127+ * add a branch
3128+ */
3129+
b752ccd1
AM
3130+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3131+ struct dentry *h_root)
1facf9fc 3132+{
b752ccd1
AM
3133+ if (unlikely(h_adding == h_root
3134+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3135+ return 1;
b752ccd1
AM
3136+ if (h_adding->d_sb != h_root->d_sb)
3137+ return 0;
3138+ return au_test_subdir(h_adding, h_root)
3139+ || au_test_subdir(h_root, h_adding);
1facf9fc 3140+}
3141+
3142+/*
3143+ * returns a newly allocated branch. @new_nbranch is a number of branches
3144+ * after adding a branch.
3145+ */
3146+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3147+ int perm)
3148+{
3149+ struct au_branch *add_branch;
3150+ struct dentry *root;
5527c038 3151+ struct inode *inode;
4a4d8108 3152+ int err;
1facf9fc 3153+
4a4d8108 3154+ err = -ENOMEM;
be52b249 3155+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3156+ if (unlikely(!add_branch))
3157+ goto out;
acd2b654 3158+ add_branch->br_xino = au_xino_alloc(/*nfile*/1);
062440b3 3159+ if (unlikely(!add_branch->br_xino))
521ced18 3160+ goto out_br;
027c5e7a
AM
3161+ err = au_hnotify_init_br(add_branch, perm);
3162+ if (unlikely(err))
062440b3 3163+ goto out_xino;
027c5e7a 3164+
1facf9fc 3165+ if (au_br_writable(perm)) {
3166+ /* may be freed separately at changing the branch permission */
be52b249 3167+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3168+ GFP_NOFS);
3169+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3170+ goto out_hnotify;
1facf9fc 3171+ }
3172+
076b876e
AM
3173+ if (au_br_fhsm(perm)) {
3174+ err = au_fhsm_br_alloc(add_branch);
3175+ if (unlikely(err))
3176+ goto out_wbr;
3177+ }
3178+
521ced18 3179+ root = sb->s_root;
e2f27e51 3180+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3181+ if (!err)
e2f27e51 3182+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3183+ if (!err) {
3184+ inode = d_inode(root);
1c60b727
AM
3185+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3186+ /*may_shrink*/0);
5527c038 3187+ }
4a4d8108
AM
3188+ if (!err)
3189+ return add_branch; /* success */
1facf9fc 3190+
076b876e 3191+out_wbr:
9f237c51 3192+ au_kfree_rcu(add_branch->br_wbr);
027c5e7a
AM
3193+out_hnotify:
3194+ au_hnotify_fin_br(add_branch);
062440b3
AM
3195+out_xino:
3196+ au_xino_put(add_branch);
4f0767ce 3197+out_br:
9f237c51 3198+ au_kfree_rcu(add_branch);
4f0767ce 3199+out:
4a4d8108 3200+ return ERR_PTR(err);
1facf9fc 3201+}
3202+
3203+/*
3204+ * test if the branch permission is legal or not.
3205+ */
3206+static int test_br(struct inode *inode, int brperm, char *path)
3207+{
3208+ int err;
3209+
4a4d8108
AM
3210+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3211+ if (!err)
3212+ goto out;
1facf9fc 3213+
4a4d8108
AM
3214+ err = -EINVAL;
3215+ pr_err("write permission for readonly mount or inode, %s\n", path);
3216+
4f0767ce 3217+out:
1facf9fc 3218+ return err;
3219+}
3220+
3221+/*
3222+ * returns:
3223+ * 0: success, the caller will add it
3224+ * plus: success, it is already unified, the caller should ignore it
3225+ * minus: error
3226+ */
3227+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3228+{
3229+ int err;
5afbbe0d 3230+ aufs_bindex_t bbot, bindex;
5527c038 3231+ struct dentry *root, *h_dentry;
1facf9fc 3232+ struct inode *inode, *h_inode;
3233+
3234+ root = sb->s_root;
5afbbe0d
AM
3235+ bbot = au_sbbot(sb);
3236+ if (unlikely(bbot >= 0
1facf9fc 3237+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3238+ err = 1;
3239+ if (!remount) {
3240+ err = -EINVAL;
4a4d8108 3241+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3242+ }
3243+ goto out;
3244+ }
3245+
3246+ err = -ENOSPC; /* -E2BIG; */
3247+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3248+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3249+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3250+ goto out;
3251+ }
3252+
3253+ err = -EDOM;
5afbbe0d 3254+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3255+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3256+ goto out;
3257+ }
3258+
5527c038 3259+ inode = d_inode(add->path.dentry);
1facf9fc 3260+ err = -ENOENT;
3261+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3262+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3263+ goto out;
3264+ }
3265+
3266+ err = -EINVAL;
3267+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3268+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3269+ goto out;
3270+ }
3271+
3272+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3273+ pr_err("unsupported filesystem, %s (%s)\n",
3274+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3275+ goto out;
3276+ }
3277+
c1595e42
JR
3278+ if (unlikely(inode->i_sb->s_stack_depth)) {
3279+ pr_err("already stacked, %s (%s)\n",
3280+ add->pathname, au_sbtype(inode->i_sb));
3281+ goto out;
3282+ }
3283+
5527c038 3284+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3285+ if (unlikely(err))
3286+ goto out;
3287+
5afbbe0d 3288+ if (bbot < 0)
1facf9fc 3289+ return 0; /* success */
3290+
3291+ err = -EINVAL;
5afbbe0d 3292+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3293+ if (unlikely(test_overlap(sb, add->path.dentry,
3294+ au_h_dptr(root, bindex)))) {
4a4d8108 3295+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3296+ goto out;
3297+ }
3298+
3299+ err = 0;
3300+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3301+ h_dentry = au_h_dptr(root, 0);
3302+ h_inode = d_inode(h_dentry);
1facf9fc 3303+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3304+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3305+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3306+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3307+ add->pathname,
3308+ i_uid_read(inode), i_gid_read(inode),
3309+ (inode->i_mode & S_IALLUGO),
3310+ i_uid_read(h_inode), i_gid_read(h_inode),
3311+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3312+ }
3313+
4f0767ce 3314+out:
1facf9fc 3315+ return err;
3316+}
3317+
3318+/*
3319+ * initialize or clean the whiteouts for an adding branch
3320+ */
3321+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3322+ int new_perm)
1facf9fc 3323+{
3324+ int err, old_perm;
3325+ aufs_bindex_t bindex;
febd17d6 3326+ struct inode *h_inode;
1facf9fc 3327+ struct au_wbr *wbr;
3328+ struct au_hinode *hdir;
5527c038 3329+ struct dentry *h_dentry;
1facf9fc 3330+
86dc4139
AM
3331+ err = vfsub_mnt_want_write(au_br_mnt(br));
3332+ if (unlikely(err))
3333+ goto out;
3334+
1facf9fc 3335+ wbr = br->br_wbr;
3336+ old_perm = br->br_perm;
3337+ br->br_perm = new_perm;
3338+ hdir = NULL;
febd17d6 3339+ h_inode = NULL;
1facf9fc 3340+ bindex = au_br_index(sb, br->br_id);
3341+ if (0 <= bindex) {
5527c038 3342+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3343+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3344+ } else {
5527c038 3345+ h_dentry = au_br_dentry(br);
febd17d6
JR
3346+ h_inode = d_inode(h_dentry);
3347+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3348+ }
3349+ if (!wbr)
86dc4139 3350+ err = au_wh_init(br, sb);
1facf9fc 3351+ else {
3352+ wbr_wh_write_lock(wbr);
86dc4139 3353+ err = au_wh_init(br, sb);
1facf9fc 3354+ wbr_wh_write_unlock(wbr);
3355+ }
3356+ if (hdir)
5afbbe0d 3357+ au_hn_inode_unlock(hdir);
1facf9fc 3358+ else
febd17d6 3359+ inode_unlock(h_inode);
86dc4139 3360+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3361+ br->br_perm = old_perm;
3362+
3363+ if (!err && wbr && !au_br_writable(new_perm)) {
9f237c51 3364+ au_kfree_rcu(wbr);
1facf9fc 3365+ br->br_wbr = NULL;
3366+ }
3367+
86dc4139 3368+out:
1facf9fc 3369+ return err;
3370+}
3371+
3372+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3373+ int perm)
1facf9fc 3374+{
3375+ int err;
4a4d8108 3376+ struct kstatfs kst;
1facf9fc 3377+ struct au_wbr *wbr;
3378+
3379+ wbr = br->br_wbr;
dece6358 3380+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3381+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3382+
4a4d8108
AM
3383+ /*
3384+ * a limit for rmdir/rename a dir
523b37e3 3385+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3386+ */
86dc4139 3387+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3388+ if (unlikely(err))
3389+ goto out;
3390+ err = -EINVAL;
3391+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3392+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3393+ else
523b37e3
AM
3394+ pr_err("%pd(%s), unsupported namelen %ld\n",
3395+ au_br_dentry(br),
86dc4139 3396+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3397+
4f0767ce 3398+out:
1facf9fc 3399+ return err;
3400+}
3401+
c1595e42 3402+/* initialize a new branch */
1facf9fc 3403+static int au_br_init(struct au_branch *br, struct super_block *sb,
3404+ struct au_opt_add *add)
3405+{
3406+ int err;
062440b3
AM
3407+ struct au_branch *brbase;
3408+ struct file *xf;
5527c038 3409+ struct inode *h_inode;
1facf9fc 3410+
3411+ err = 0;
1facf9fc 3412+ br->br_perm = add->perm;
86dc4139 3413+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3414+ spin_lock_init(&br->br_dykey_lock);
acd2b654
AM
3415+ au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3416+ au_lcnt_init(&br->br_count, /*release*/NULL);
1facf9fc 3417+ br->br_id = au_new_br_id(sb);
7f207e10 3418+ AuDebugOn(br->br_id < 0);
1facf9fc 3419+
8b6a4947
AM
3420+ /* always, regardless the given option */
3421+ err = au_dr_br_init(sb, br, &add->path);
3422+ if (unlikely(err))
3423+ goto out_err;
3424+
1facf9fc 3425+ if (au_br_writable(add->perm)) {
86dc4139 3426+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3427+ if (unlikely(err))
b752ccd1 3428+ goto out_err;
1facf9fc 3429+ }
3430+
3431+ if (au_opt_test(au_mntflags(sb), XINO)) {
062440b3 3432+ brbase = au_sbr(sb, 0);
acd2b654 3433+ xf = au_xino_file(brbase->br_xino, /*idx*/-1);
062440b3 3434+ AuDebugOn(!xf);
5527c038 3435+ h_inode = d_inode(add->path.dentry);
062440b3 3436+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
1facf9fc 3437+ if (unlikely(err)) {
acd2b654 3438+ AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
b752ccd1 3439+ goto out_err;
1facf9fc 3440+ }
3441+ }
3442+
3443+ sysaufs_br_init(br);
86dc4139 3444+ path_get(&br->br_path);
b752ccd1 3445+ goto out; /* success */
1facf9fc 3446+
4f0767ce 3447+out_err:
86dc4139 3448+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3449+out:
1facf9fc 3450+ return err;
3451+}
3452+
3453+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3454+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3455+ aufs_bindex_t amount)
3456+{
3457+ struct au_branch **brp;
3458+
dece6358
AM
3459+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3460+
1facf9fc 3461+ brp = sbinfo->si_branch + bindex;
3462+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3463+ *brp = br;
5afbbe0d
AM
3464+ sbinfo->si_bbot++;
3465+ if (unlikely(bbot < 0))
3466+ sbinfo->si_bbot = 0;
1facf9fc 3467+}
3468+
3469+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3470+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3471+{
3472+ struct au_hdentry *hdp;
3473+
1308ab2a 3474+ AuRwMustWriteLock(&dinfo->di_rwsem);
3475+
5afbbe0d 3476+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3477+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3478+ au_h_dentry_init(hdp);
5afbbe0d
AM
3479+ dinfo->di_bbot++;
3480+ if (unlikely(bbot < 0))
3481+ dinfo->di_btop = 0;
1facf9fc 3482+}
3483+
3484+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3485+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3486+{
3487+ struct au_hinode *hip;
3488+
1308ab2a 3489+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3490+
5afbbe0d 3491+ hip = au_hinode(iinfo, bindex);
1facf9fc 3492+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3493+ au_hinode_init(hip);
3494+ iinfo->ii_bbot++;
3495+ if (unlikely(bbot < 0))
3496+ iinfo->ii_btop = 0;
1facf9fc 3497+}
3498+
86dc4139
AM
3499+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3500+ aufs_bindex_t bindex)
1facf9fc 3501+{
86dc4139 3502+ struct dentry *root, *h_dentry;
5527c038 3503+ struct inode *root_inode, *h_inode;
5afbbe0d 3504+ aufs_bindex_t bbot, amount;
1facf9fc 3505+
3506+ root = sb->s_root;
5527c038 3507+ root_inode = d_inode(root);
5afbbe0d
AM
3508+ bbot = au_sbbot(sb);
3509+ amount = bbot + 1 - bindex;
86dc4139 3510+ h_dentry = au_br_dentry(br);
53392da6 3511+ au_sbilist_lock();
5afbbe0d
AM
3512+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3513+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3514+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3515+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3516+ h_inode = d_inode(h_dentry);
3517+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3518+ au_sbilist_unlock();
1facf9fc 3519+}
3520+
3521+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3522+{
3523+ int err;
5afbbe0d 3524+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3525+ struct dentry *root, *h_dentry;
3526+ struct inode *root_inode;
3527+ struct au_branch *add_branch;
3528+
3529+ root = sb->s_root;
5527c038 3530+ root_inode = d_inode(root);
1facf9fc 3531+ IMustLock(root_inode);
5afbbe0d 3532+ IiMustWriteLock(root_inode);
1facf9fc 3533+ err = test_add(sb, add, remount);
3534+ if (unlikely(err < 0))
3535+ goto out;
3536+ if (err) {
3537+ err = 0;
3538+ goto out; /* success */
3539+ }
3540+
5afbbe0d
AM
3541+ bbot = au_sbbot(sb);
3542+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3543+ err = PTR_ERR(add_branch);
3544+ if (IS_ERR(add_branch))
3545+ goto out;
3546+
3547+ err = au_br_init(add_branch, sb, add);
3548+ if (unlikely(err)) {
3549+ au_br_do_free(add_branch);
3550+ goto out;
3551+ }
3552+
3553+ add_bindex = add->bindex;
062440b3
AM
3554+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3555+ au_br_do_add(sb, add_branch, add_bindex);
3556+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3557+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
1facf9fc 3558+
86dc4139 3559+ h_dentry = add->path.dentry;
1308ab2a 3560+ if (!add_bindex) {
1facf9fc 3561+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3562+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3563+ } else
5527c038 3564+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3565+
4f0767ce 3566+out:
1facf9fc 3567+ return err;
3568+}
3569+
3570+/* ---------------------------------------------------------------------- */
3571+
79b8bda9 3572+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3573+ unsigned long long max __maybe_unused,
3574+ void *arg)
3575+{
3576+ unsigned long long n;
3577+ struct file **p, *f;
8b6a4947
AM
3578+ struct hlist_bl_head *files;
3579+ struct hlist_bl_node *pos;
076b876e 3580+ struct au_finfo *finfo;
076b876e
AM
3581+
3582+ n = 0;
3583+ p = a;
3584+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3585+ hlist_bl_lock(files);
3586+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3587+ f = finfo->fi_file;
3588+ if (file_count(f)
3589+ && !special_file(file_inode(f)->i_mode)) {
3590+ get_file(f);
3591+ *p++ = f;
3592+ n++;
3593+ AuDebugOn(n > max);
3594+ }
3595+ }
8b6a4947 3596+ hlist_bl_unlock(files);
076b876e
AM
3597+
3598+ return n;
3599+}
3600+
3601+static struct file **au_farray_alloc(struct super_block *sb,
3602+ unsigned long long *max)
3603+{
acd2b654
AM
3604+ struct au_sbinfo *sbi;
3605+
3606+ sbi = au_sbi(sb);
3607+ *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
79b8bda9 3608+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3609+}
3610+
3611+static void au_farray_free(struct file **a, unsigned long long max)
3612+{
3613+ unsigned long long ull;
3614+
3615+ for (ull = 0; ull < max; ull++)
3616+ if (a[ull])
3617+ fput(a[ull]);
be52b249 3618+ kvfree(a);
076b876e
AM
3619+}
3620+
3621+/* ---------------------------------------------------------------------- */
3622+
1facf9fc 3623+/*
3624+ * delete a branch
3625+ */
3626+
3627+/* to show the line number, do not make it inlined function */
4a4d8108 3628+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3629+ if (do_info) \
4a4d8108 3630+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3631+} while (0)
3632+
5afbbe0d
AM
3633+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3634+ aufs_bindex_t bbot)
027c5e7a 3635+{
5afbbe0d 3636+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3637+}
3638+
5afbbe0d
AM
3639+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3640+ aufs_bindex_t bbot)
027c5e7a 3641+{
5afbbe0d 3642+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3643+}
3644+
1facf9fc 3645+/*
3646+ * test if the branch is deletable or not.
3647+ */
3648+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3649+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3650+{
3651+ int err, i, j, ndentry;
5afbbe0d 3652+ aufs_bindex_t btop, bbot;
1facf9fc 3653+ struct au_dcsub_pages dpages;
3654+ struct au_dpage *dpage;
3655+ struct dentry *d;
1facf9fc 3656+
3657+ err = au_dpages_init(&dpages, GFP_NOFS);
3658+ if (unlikely(err))
3659+ goto out;
3660+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3661+ if (unlikely(err))
3662+ goto out_dpages;
3663+
1facf9fc 3664+ for (i = 0; !err && i < dpages.ndpage; i++) {
3665+ dpage = dpages.dpages + i;
3666+ ndentry = dpage->ndentry;
3667+ for (j = 0; !err && j < ndentry; j++) {
3668+ d = dpage->dentries[j];
c1595e42 3669+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3670+ if (!au_digen_test(d, sigen)) {
1facf9fc 3671+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3672+ if (unlikely(au_dbrange_test(d))) {
3673+ di_read_unlock(d, AuLock_IR);
3674+ continue;
3675+ }
3676+ } else {
1facf9fc 3677+ di_write_lock_child(d);
027c5e7a
AM
3678+ if (unlikely(au_dbrange_test(d))) {
3679+ di_write_unlock(d);
3680+ continue;
3681+ }
1facf9fc 3682+ err = au_reval_dpath(d, sigen);
3683+ if (!err)
3684+ di_downgrade_lock(d, AuLock_IR);
3685+ else {
3686+ di_write_unlock(d);
3687+ break;
3688+ }
3689+ }
3690+
027c5e7a 3691+ /* AuDbgDentry(d); */
5afbbe0d
AM
3692+ btop = au_dbtop(d);
3693+ bbot = au_dbbot(d);
3694+ if (btop <= bindex
3695+ && bindex <= bbot
1facf9fc 3696+ && au_h_dptr(d, bindex)
5afbbe0d 3697+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3698+ err = -EBUSY;
523b37e3 3699+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3700+ AuDbgDentry(d);
1facf9fc 3701+ }
3702+ di_read_unlock(d, AuLock_IR);
3703+ }
3704+ }
3705+
4f0767ce 3706+out_dpages:
1facf9fc 3707+ au_dpages_free(&dpages);
4f0767ce 3708+out:
1facf9fc 3709+ return err;
3710+}
3711+
3712+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3713+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3714+{
3715+ int err;
7f207e10
AM
3716+ unsigned long long max, ull;
3717+ struct inode *i, **array;
5afbbe0d 3718+ aufs_bindex_t btop, bbot;
1facf9fc 3719+
7f207e10
AM
3720+ array = au_iarray_alloc(sb, &max);
3721+ err = PTR_ERR(array);
3722+ if (IS_ERR(array))
3723+ goto out;
3724+
1facf9fc 3725+ err = 0;
7f207e10
AM
3726+ AuDbg("b%d\n", bindex);
3727+ for (ull = 0; !err && ull < max; ull++) {
3728+ i = array[ull];
076b876e
AM
3729+ if (unlikely(!i))
3730+ break;
7f207e10 3731+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3732+ continue;
3733+
7f207e10 3734+ /* AuDbgInode(i); */
537831f9 3735+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3736+ ii_read_lock_child(i);
3737+ else {
3738+ ii_write_lock_child(i);
027c5e7a
AM
3739+ err = au_refresh_hinode_self(i);
3740+ au_iigen_dec(i);
1facf9fc 3741+ if (!err)
3742+ ii_downgrade_lock(i);
3743+ else {
3744+ ii_write_unlock(i);
3745+ break;
3746+ }
3747+ }
3748+
5afbbe0d
AM
3749+ btop = au_ibtop(i);
3750+ bbot = au_ibbot(i);
3751+ if (btop <= bindex
3752+ && bindex <= bbot
1facf9fc 3753+ && au_h_iptr(i, bindex)
5afbbe0d 3754+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3755+ err = -EBUSY;
3756+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3757+ AuDbgInode(i);
1facf9fc 3758+ }
3759+ ii_read_unlock(i);
3760+ }
7f207e10 3761+ au_iarray_free(array, max);
1facf9fc 3762+
7f207e10 3763+out:
1facf9fc 3764+ return err;
3765+}
3766+
b752ccd1
AM
3767+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3768+ const unsigned int verbose)
1facf9fc 3769+{
3770+ int err;
3771+ unsigned int sigen;
3772+
3773+ sigen = au_sigen(root->d_sb);
3774+ DiMustNoWaiters(root);
5527c038 3775+ IiMustNoWaiters(d_inode(root));
1facf9fc 3776+ di_write_unlock(root);
b752ccd1 3777+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3778+ if (!err)
b752ccd1 3779+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3780+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3781+
3782+ return err;
3783+}
3784+
076b876e
AM
3785+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3786+ struct file **to_free, int *idx)
3787+{
3788+ int err;
c1595e42 3789+ unsigned char matched, root;
5afbbe0d 3790+ aufs_bindex_t bindex, bbot;
076b876e
AM
3791+ struct au_fidir *fidir;
3792+ struct au_hfile *hfile;
3793+
3794+ err = 0;
2000de60 3795+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3796+ if (root) {
3797+ get_file(file);
3798+ to_free[*idx] = file;
3799+ (*idx)++;
3800+ goto out;
3801+ }
3802+
076b876e 3803+ matched = 0;
076b876e
AM
3804+ fidir = au_fi(file)->fi_hdir;
3805+ AuDebugOn(!fidir);
5afbbe0d
AM
3806+ bbot = au_fbbot_dir(file);
3807+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3808+ hfile = fidir->fd_hfile + bindex;
3809+ if (!hfile->hf_file)
3810+ continue;
3811+
c1595e42 3812+ if (hfile->hf_br->br_id == br_id) {
076b876e 3813+ matched = 1;
076b876e 3814+ break;
c1595e42 3815+ }
076b876e 3816+ }
c1595e42 3817+ if (matched)
076b876e
AM
3818+ err = -EBUSY;
3819+
3820+out:
3821+ return err;
3822+}
3823+
3824+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3825+ struct file **to_free, int opened)
3826+{
3827+ int err, idx;
3828+ unsigned long long ull, max;
5afbbe0d 3829+ aufs_bindex_t btop;
076b876e 3830+ struct file *file, **array;
076b876e
AM
3831+ struct dentry *root;
3832+ struct au_hfile *hfile;
3833+
3834+ array = au_farray_alloc(sb, &max);
3835+ err = PTR_ERR(array);
3836+ if (IS_ERR(array))
3837+ goto out;
3838+
3839+ err = 0;
3840+ idx = 0;
3841+ root = sb->s_root;
3842+ di_write_unlock(root);
3843+ for (ull = 0; ull < max; ull++) {
3844+ file = array[ull];
3845+ if (unlikely(!file))
3846+ break;
3847+
3848+ /* AuDbg("%pD\n", file); */
3849+ fi_read_lock(file);
5afbbe0d 3850+ btop = au_fbtop(file);
2000de60 3851+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3852+ hfile = &au_fi(file)->fi_htop;
3853+ if (hfile->hf_br->br_id == br_id)
3854+ err = -EBUSY;
3855+ } else
3856+ err = test_dir_busy(file, br_id, to_free, &idx);
3857+ fi_read_unlock(file);
3858+ if (unlikely(err))
3859+ break;
3860+ }
3861+ di_write_lock_child(root);
3862+ au_farray_free(array, max);
3863+ AuDebugOn(idx > opened);
3864+
3865+out:
3866+ return err;
3867+}
3868+
3869+static void br_del_file(struct file **to_free, unsigned long long opened,
062440b3 3870+ aufs_bindex_t br_id)
076b876e
AM
3871+{
3872+ unsigned long long ull;
5afbbe0d 3873+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
3874+ struct file *file;
3875+ struct au_fidir *fidir;
3876+ struct au_hfile *hfile;
3877+
3878+ for (ull = 0; ull < opened; ull++) {
3879+ file = to_free[ull];
3880+ if (unlikely(!file))
3881+ break;
3882+
3883+ /* AuDbg("%pD\n", file); */
2000de60 3884+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
3885+ bfound = -1;
3886+ fidir = au_fi(file)->fi_hdir;
3887+ AuDebugOn(!fidir);
3888+ fi_write_lock(file);
5afbbe0d
AM
3889+ btop = au_fbtop(file);
3890+ bbot = au_fbbot_dir(file);
3891+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
3892+ hfile = fidir->fd_hfile + bindex;
3893+ if (!hfile->hf_file)
3894+ continue;
3895+
3896+ if (hfile->hf_br->br_id == br_id) {
3897+ bfound = bindex;
3898+ break;
3899+ }
3900+ }
3901+ AuDebugOn(bfound < 0);
3902+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
3903+ if (bfound == btop) {
3904+ for (btop++; btop <= bbot; btop++)
3905+ if (au_hf_dir(file, btop)) {
3906+ au_set_fbtop(file, btop);
076b876e
AM
3907+ break;
3908+ }
3909+ }
3910+ fi_write_unlock(file);
3911+ }
3912+}
3913+
1facf9fc 3914+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3915+ const aufs_bindex_t bindex,
5afbbe0d 3916+ const aufs_bindex_t bbot)
1facf9fc 3917+{
3918+ struct au_branch **brp, **p;
3919+
dece6358
AM
3920+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3921+
1facf9fc 3922+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
3923+ if (bindex < bbot)
3924+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3925+ sbinfo->si_branch[0 + bbot] = NULL;
3926+ sbinfo->si_bbot--;
1facf9fc 3927+
e2f27e51
AM
3928+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3929+ /*may_shrink*/1);
1facf9fc 3930+ if (p)
3931+ sbinfo->si_branch = p;
4a4d8108 3932+ /* harmless error */
1facf9fc 3933+}
3934+
3935+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 3936+ const aufs_bindex_t bbot)
1facf9fc 3937+{
3938+ struct au_hdentry *hdp, *p;
3939+
1308ab2a 3940+ AuRwMustWriteLock(&dinfo->di_rwsem);
3941+
5afbbe0d
AM
3942+ hdp = au_hdentry(dinfo, bindex);
3943+ if (bindex < bbot)
3944+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3945+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3946+ dinfo->di_bbot--;
1facf9fc 3947+
e2f27e51
AM
3948+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3949+ /*may_shrink*/1);
1facf9fc 3950+ if (p)
3951+ dinfo->di_hdentry = p;
4a4d8108 3952+ /* harmless error */
1facf9fc 3953+}
3954+
3955+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 3956+ const aufs_bindex_t bbot)
1facf9fc 3957+{
3958+ struct au_hinode *hip, *p;
3959+
1308ab2a 3960+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3961+
5afbbe0d
AM
3962+ hip = au_hinode(iinfo, bindex);
3963+ if (bindex < bbot)
3964+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3965+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
3966+ iinfo->ii_bbot--;
1facf9fc 3967+
e2f27e51
AM
3968+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3969+ /*may_shrink*/1);
1facf9fc 3970+ if (p)
3971+ iinfo->ii_hinode = p;
4a4d8108 3972+ /* harmless error */
1facf9fc 3973+}
3974+
3975+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3976+ struct au_branch *br)
3977+{
5afbbe0d 3978+ aufs_bindex_t bbot;
1facf9fc 3979+ struct au_sbinfo *sbinfo;
53392da6
AM
3980+ struct dentry *root, *h_root;
3981+ struct inode *inode, *h_inode;
3982+ struct au_hinode *hinode;
1facf9fc 3983+
dece6358
AM
3984+ SiMustWriteLock(sb);
3985+
1facf9fc 3986+ root = sb->s_root;
5527c038 3987+ inode = d_inode(root);
1facf9fc 3988+ sbinfo = au_sbi(sb);
5afbbe0d 3989+ bbot = sbinfo->si_bbot;
1facf9fc 3990+
53392da6
AM
3991+ h_root = au_h_dptr(root, bindex);
3992+ hinode = au_hi(inode, bindex);
3993+ h_inode = au_igrab(hinode->hi_inode);
3994+ au_hiput(hinode);
1facf9fc 3995+
53392da6 3996+ au_sbilist_lock();
5afbbe0d
AM
3997+ au_br_do_del_brp(sbinfo, bindex, bbot);
3998+ au_br_do_del_hdp(au_di(root), bindex, bbot);
3999+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
4000+ au_sbilist_unlock();
4001+
8b6a4947
AM
4002+ /* ignore an error */
4003+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
4004+
53392da6
AM
4005+ dput(h_root);
4006+ iput(h_inode);
4007+ au_br_do_free(br);
1facf9fc 4008+}
4009+
79b8bda9
AM
4010+static unsigned long long empty_cb(struct super_block *sb, void *array,
4011+ unsigned long long max, void *arg)
076b876e
AM
4012+{
4013+ return max;
4014+}
4015+
1facf9fc 4016+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4017+{
4018+ int err, rerr, i;
076b876e 4019+ unsigned long long opened;
1facf9fc 4020+ unsigned int mnt_flags;
5afbbe0d 4021+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 4022+ unsigned char do_wh, verbose;
4023+ struct au_branch *br;
4024+ struct au_wbr *wbr;
076b876e
AM
4025+ struct dentry *root;
4026+ struct file **to_free;
1facf9fc 4027+
4028+ err = 0;
076b876e
AM
4029+ opened = 0;
4030+ to_free = NULL;
4031+ root = sb->s_root;
4032+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 4033+ if (bindex < 0) {
4034+ if (remount)
4035+ goto out; /* success */
4036+ err = -ENOENT;
4a4d8108 4037+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 4038+ goto out;
4039+ }
4040+ AuDbg("bindex b%d\n", bindex);
4041+
4042+ err = -EBUSY;
4043+ mnt_flags = au_mntflags(sb);
4044+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
4045+ bbot = au_sbbot(sb);
4046+ if (unlikely(!bbot)) {
1facf9fc 4047+ AuVerbose(verbose, "no more branches left\n");
4048+ goto out;
4049+ }
acd2b654 4050+
1facf9fc 4051+ br = au_sbr(sb, bindex);
86dc4139 4052+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
acd2b654
AM
4053+ if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
4054+ AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
4055+ goto out;
4056+ }
076b876e
AM
4057+
4058+ br_id = br->br_id;
acd2b654 4059+ opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
076b876e 4060+ if (unlikely(opened)) {
79b8bda9 4061+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
4062+ err = PTR_ERR(to_free);
4063+ if (IS_ERR(to_free))
4064+ goto out;
4065+
4066+ err = test_file_busy(sb, br_id, to_free, opened);
4067+ if (unlikely(err)) {
4068+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
4069+ goto out;
4070+ }
1facf9fc 4071+ }
4072+
4073+ wbr = br->br_wbr;
4074+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4075+ if (do_wh) {
1308ab2a 4076+ /* instead of WbrWhMustWriteLock(wbr) */
4077+ SiMustWriteLock(sb);
1facf9fc 4078+ for (i = 0; i < AuBrWh_Last; i++) {
4079+ dput(wbr->wbr_wh[i]);
4080+ wbr->wbr_wh[i] = NULL;
4081+ }
4082+ }
4083+
076b876e 4084+ err = test_children_busy(root, bindex, verbose);
1facf9fc 4085+ if (unlikely(err)) {
4086+ if (do_wh)
4087+ goto out_wh;
4088+ goto out;
4089+ }
4090+
4091+ err = 0;
076b876e
AM
4092+ if (to_free) {
4093+ /*
4094+ * now we confirmed the branch is deletable.
4095+ * let's free the remaining opened dirs on the branch.
4096+ */
4097+ di_write_unlock(root);
4098+ br_del_file(to_free, opened, br_id);
4099+ di_write_lock_child(root);
4100+ }
4101+
062440b3
AM
4102+ sysaufs_brs_del(sb, bindex); /* remove successors */
4103+ dbgaufs_xino_del(br); /* remove one */
4104+ au_br_do_del(sb, bindex, br);
4105+ sysaufs_brs_add(sb, bindex); /* append successors */
4106+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
1facf9fc 4107+
1308ab2a 4108+ if (!bindex) {
5527c038 4109+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4110+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4111+ } else
5527c038 4112+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4113+ if (au_opt_test(mnt_flags, PLINK))
4114+ au_plink_half_refresh(sb, br_id);
4115+
1facf9fc 4116+ goto out; /* success */
4117+
4f0767ce 4118+out_wh:
1facf9fc 4119+ /* revert */
86dc4139 4120+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4121+ if (rerr)
0c3ec466
AM
4122+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4123+ del->pathname, rerr);
4f0767ce 4124+out:
076b876e
AM
4125+ if (to_free)
4126+ au_farray_free(to_free, opened);
1facf9fc 4127+ return err;
4128+}
4129+
4130+/* ---------------------------------------------------------------------- */
4131+
027c5e7a
AM
4132+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4133+{
4134+ int err;
5afbbe0d 4135+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4136+ struct aufs_ibusy ibusy;
4137+ struct inode *inode, *h_inode;
4138+
4139+ err = -EPERM;
4140+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4141+ goto out;
4142+
4143+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4144+ if (!err)
ba1aed25
AM
4145+ /* VERIFY_WRITE */
4146+ err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
027c5e7a
AM
4147+ if (unlikely(err)) {
4148+ err = -EFAULT;
4149+ AuTraceErr(err);
4150+ goto out;
4151+ }
4152+
4153+ err = -EINVAL;
4154+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4155+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4156+ goto out_unlock;
4157+
4158+ err = 0;
4159+ ibusy.h_ino = 0; /* invalid */
4160+ inode = ilookup(sb, ibusy.ino);
4161+ if (!inode
4162+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4163+ || au_is_bad_inode(inode))
027c5e7a
AM
4164+ goto out_unlock;
4165+
4166+ ii_read_lock_child(inode);
5afbbe0d
AM
4167+ btop = au_ibtop(inode);
4168+ bbot = au_ibbot(inode);
4169+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4170+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4171+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4172+ ibusy.h_ino = h_inode->i_ino;
4173+ }
4174+ ii_read_unlock(inode);
4175+ iput(inode);
4176+
4177+out_unlock:
4178+ si_read_unlock(sb);
4179+ if (!err) {
4180+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4181+ if (unlikely(err)) {
4182+ err = -EFAULT;
4183+ AuTraceErr(err);
4184+ }
4185+ }
4186+out:
4187+ return err;
4188+}
4189+
4190+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4191+{
2000de60 4192+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4193+}
4194+
4195+#ifdef CONFIG_COMPAT
4196+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4197+{
2000de60 4198+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4199+}
4200+#endif
4201+
4202+/* ---------------------------------------------------------------------- */
4203+
1facf9fc 4204+/*
4205+ * change a branch permission
4206+ */
4207+
dece6358
AM
4208+static void au_warn_ima(void)
4209+{
4210+#ifdef CONFIG_IMA
1308ab2a 4211+ /* since it doesn't support mark_files_ro() */
027c5e7a 4212+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4213+#endif
4214+}
4215+
1facf9fc 4216+static int do_need_sigen_inc(int a, int b)
4217+{
4218+ return au_br_whable(a) && !au_br_whable(b);
4219+}
4220+
4221+static int need_sigen_inc(int old, int new)
4222+{
4223+ return do_need_sigen_inc(old, new)
4224+ || do_need_sigen_inc(new, old);
4225+}
4226+
4227+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4228+{
7f207e10 4229+ int err, do_warn;
027c5e7a 4230+ unsigned int mnt_flags;
7f207e10 4231+ unsigned long long ull, max;
e49829fe 4232+ aufs_bindex_t br_id;
38d290e6 4233+ unsigned char verbose, writer;
7f207e10 4234+ struct file *file, *hf, **array;
e49829fe 4235+ struct au_hfile *hfile;
eca34b5c 4236+ struct inode *h_inode;
1facf9fc 4237+
027c5e7a
AM
4238+ mnt_flags = au_mntflags(sb);
4239+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4240+
7f207e10
AM
4241+ array = au_farray_alloc(sb, &max);
4242+ err = PTR_ERR(array);
4243+ if (IS_ERR(array))
1facf9fc 4244+ goto out;
4245+
7f207e10 4246+ do_warn = 0;
e49829fe 4247+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4248+ for (ull = 0; ull < max; ull++) {
4249+ file = array[ull];
076b876e
AM
4250+ if (unlikely(!file))
4251+ break;
1facf9fc 4252+
523b37e3 4253+ /* AuDbg("%pD\n", file); */
1facf9fc 4254+ fi_read_lock(file);
4255+ if (unlikely(au_test_mmapped(file))) {
4256+ err = -EBUSY;
523b37e3 4257+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4258+ AuDbgFile(file);
1facf9fc 4259+ FiMustNoWaiters(file);
4260+ fi_read_unlock(file);
7f207e10 4261+ goto out_array;
1facf9fc 4262+ }
4263+
e49829fe
JR
4264+ hfile = &au_fi(file)->fi_htop;
4265+ hf = hfile->hf_file;
7e9cd9fe 4266+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4267+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4268+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4269+ || !(hf->f_mode & FMODE_WRITE))
4270+ array[ull] = NULL;
4271+ else {
4272+ do_warn = 1;
4273+ get_file(file);
1facf9fc 4274+ }
4275+
1facf9fc 4276+ FiMustNoWaiters(file);
4277+ fi_read_unlock(file);
7f207e10
AM
4278+ fput(file);
4279+ }
1facf9fc 4280+
4281+ err = 0;
7f207e10 4282+ if (do_warn)
dece6358 4283+ au_warn_ima();
7f207e10
AM
4284+
4285+ for (ull = 0; ull < max; ull++) {
4286+ file = array[ull];
4287+ if (!file)
4288+ continue;
4289+
1facf9fc 4290+ /* todo: already flushed? */
523b37e3
AM
4291+ /*
4292+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4293+ * approach which resets f_mode and calls mnt_drop_write() and
4294+ * file_release_write() for each file, because the branch
4295+ * attribute in aufs world is totally different from the native
4296+ * fs rw/ro mode.
4297+ */
7f207e10
AM
4298+ /* fi_read_lock(file); */
4299+ hfile = &au_fi(file)->fi_htop;
4300+ hf = hfile->hf_file;
4301+ /* fi_read_unlock(file); */
027c5e7a 4302+ spin_lock(&hf->f_lock);
38d290e6
JR
4303+ writer = !!(hf->f_mode & FMODE_WRITER);
4304+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4305+ spin_unlock(&hf->f_lock);
38d290e6 4306+ if (writer) {
eca34b5c
AM
4307+ h_inode = file_inode(hf);
4308+ put_write_access(h_inode);
c06a8ce3 4309+ __mnt_drop_write(hf->f_path.mnt);
eca34b5c
AM
4310+ if ((hf->f_mode & (FMODE_READ | FMODE_WRITE))
4311+ == FMODE_READ)
4312+ i_readcount_inc(h_inode);
1facf9fc 4313+ }
4314+ }
4315+
7f207e10
AM
4316+out_array:
4317+ au_farray_free(array, max);
4f0767ce 4318+out:
7f207e10 4319+ AuTraceErr(err);
1facf9fc 4320+ return err;
4321+}
4322+
4323+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4324+ int *do_refresh)
1facf9fc 4325+{
4326+ int err, rerr;
4327+ aufs_bindex_t bindex;
4328+ struct dentry *root;
4329+ struct au_branch *br;
076b876e 4330+ struct au_br_fhsm *bf;
1facf9fc 4331+
4332+ root = sb->s_root;
1facf9fc 4333+ bindex = au_find_dbindex(root, mod->h_root);
4334+ if (bindex < 0) {
4335+ if (remount)
4336+ return 0; /* success */
4337+ err = -ENOENT;
4a4d8108 4338+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4339+ goto out;
4340+ }
4341+ AuDbg("bindex b%d\n", bindex);
4342+
5527c038 4343+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4344+ if (unlikely(err))
4345+ goto out;
4346+
4347+ br = au_sbr(sb, bindex);
86dc4139 4348+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4349+ if (br->br_perm == mod->perm)
4350+ return 0; /* success */
4351+
076b876e
AM
4352+ /* pre-allocate for non-fhsm --> fhsm */
4353+ bf = NULL;
4354+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4355+ err = au_fhsm_br_alloc(br);
4356+ if (unlikely(err))
4357+ goto out;
4358+ bf = br->br_fhsm;
4359+ br->br_fhsm = NULL;
4360+ }
4361+
1facf9fc 4362+ if (au_br_writable(br->br_perm)) {
4363+ /* remove whiteout base */
86dc4139 4364+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4365+ if (unlikely(err))
076b876e 4366+ goto out_bf;
1facf9fc 4367+
4368+ if (!au_br_writable(mod->perm)) {
4369+ /* rw --> ro, file might be mmapped */
4370+ DiMustNoWaiters(root);
5527c038 4371+ IiMustNoWaiters(d_inode(root));
1facf9fc 4372+ di_write_unlock(root);
4373+ err = au_br_mod_files_ro(sb, bindex);
4374+ /* aufs_write_lock() calls ..._child() */
4375+ di_write_lock_child(root);
4376+
4377+ if (unlikely(err)) {
4378+ rerr = -ENOMEM;
be52b249 4379+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4380+ GFP_NOFS);
86dc4139
AM
4381+ if (br->br_wbr)
4382+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4383+ if (unlikely(rerr)) {
4384+ AuIOErr("nested error %d (%d)\n",
4385+ rerr, err);
4386+ br->br_perm = mod->perm;
4387+ }
4388+ }
4389+ }
4390+ } else if (au_br_writable(mod->perm)) {
4391+ /* ro --> rw */
4392+ err = -ENOMEM;
be52b249 4393+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4394+ if (br->br_wbr) {
86dc4139 4395+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4396+ if (unlikely(err)) {
9f237c51 4397+ au_kfree_rcu(br->br_wbr);
1facf9fc 4398+ br->br_wbr = NULL;
4399+ }
4400+ }
4401+ }
076b876e
AM
4402+ if (unlikely(err))
4403+ goto out_bf;
4404+
4405+ if (au_br_fhsm(br->br_perm)) {
4406+ if (!au_br_fhsm(mod->perm)) {
4407+ /* fhsm --> non-fhsm */
4408+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 4409+ au_kfree_rcu(br->br_fhsm);
076b876e
AM
4410+ br->br_fhsm = NULL;
4411+ }
4412+ } else if (au_br_fhsm(mod->perm))
4413+ /* non-fhsm --> fhsm */
4414+ br->br_fhsm = bf;
4415+
076b876e
AM
4416+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4417+ br->br_perm = mod->perm;
4418+ goto out; /* success */
1facf9fc 4419+
076b876e 4420+out_bf:
9f237c51 4421+ au_kfree_try_rcu(bf);
076b876e
AM
4422+out:
4423+ AuTraceErr(err);
4424+ return err;
4425+}
4426+
4427+/* ---------------------------------------------------------------------- */
4428+
4429+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4430+{
4431+ int err;
4432+ struct kstatfs kstfs;
4433+
4434+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4435+ if (!err) {
076b876e
AM
4436+ stfs->f_blocks = kstfs.f_blocks;
4437+ stfs->f_bavail = kstfs.f_bavail;
4438+ stfs->f_files = kstfs.f_files;
4439+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4440+ }
4441+
1facf9fc 4442+ return err;
4443+}
7f207e10 4444diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
eca34b5c
AM
4445--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
4446+++ linux/fs/aufs/branch.h 2019-07-11 15:42:14.462237786 +0200
83b672a5 4447@@ -0,0 +1,366 @@
062440b3 4448+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 4449+/*
ba1aed25 4450+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 4451+ *
4452+ * This program, aufs is free software; you can redistribute it and/or modify
4453+ * it under the terms of the GNU General Public License as published by
4454+ * the Free Software Foundation; either version 2 of the License, or
4455+ * (at your option) any later version.
dece6358
AM
4456+ *
4457+ * This program is distributed in the hope that it will be useful,
4458+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4459+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4460+ * GNU General Public License for more details.
4461+ *
4462+ * You should have received a copy of the GNU General Public License
523b37e3 4463+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4464+ */
4465+
4466+/*
4467+ * branch filesystems and xino for them
4468+ */
4469+
4470+#ifndef __AUFS_BRANCH_H__
4471+#define __AUFS_BRANCH_H__
4472+
4473+#ifdef __KERNEL__
4474+
1facf9fc 4475+#include <linux/mount.h>
8b6a4947 4476+#include "dirren.h"
4a4d8108 4477+#include "dynop.h"
acd2b654 4478+#include "lcnt.h"
1facf9fc 4479+#include "rwsem.h"
4480+#include "super.h"
4481+
4482+/* ---------------------------------------------------------------------- */
4483+
4484+/* a xino file */
062440b3 4485+struct au_xino {
acd2b654
AM
4486+ struct file **xi_file;
4487+ unsigned int xi_nfile;
4488+
521ced18
JR
4489+ struct {
4490+ spinlock_t spin;
4491+ ino_t *array;
4492+ int total;
4493+ /* reserved for future use */
4494+ /* unsigned long *bitmap; */
4495+ wait_queue_head_t wqh;
4496+ } xi_nondir;
1facf9fc 4497+
acd2b654 4498+ struct mutex xi_mtx; /* protects xi_file array */
9f237c51 4499+ struct hlist_bl_head xi_writing;
acd2b654 4500+
062440b3 4501+ atomic_t xi_truncating;
1facf9fc 4502+
062440b3 4503+ struct kref xi_kref;
1facf9fc 4504+};
4505+
076b876e
AM
4506+/* File-based Hierarchical Storage Management */
4507+struct au_br_fhsm {
4508+#ifdef CONFIG_AUFS_FHSM
4509+ struct mutex bf_lock;
4510+ unsigned long bf_jiffy;
4511+ struct aufs_stfs bf_stfs;
4512+ int bf_readable;
4513+#endif
4514+};
4515+
1facf9fc 4516+/* members for writable branch only */
4517+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4518+struct au_wbr {
dece6358 4519+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4520+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4521+ atomic_t wbr_wh_running;
1facf9fc 4522+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4523+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4524+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4525+
4526+ /* mfs mode */
4527+ unsigned long long wbr_bytes;
4528+};
4529+
4a4d8108
AM
4530+/* ext2 has 3 types of operations at least, ext3 has 4 */
4531+#define AuBrDynOp (AuDyLast * 4)
4532+
1716fcea
AM
4533+#ifdef CONFIG_AUFS_HFSNOTIFY
4534+/* support for asynchronous destruction */
4535+struct au_br_hfsnotify {
4536+ struct fsnotify_group *hfsn_group;
4537+};
4538+#endif
4539+
392086de
AM
4540+/* sysfs entries */
4541+struct au_brsysfs {
4542+ char name[16];
4543+ struct attribute attr;
4544+};
4545+
4546+enum {
4547+ AuBrSysfs_BR,
4548+ AuBrSysfs_BRID,
4549+ AuBrSysfs_Last
4550+};
4551+
1facf9fc 4552+/* protected by superblock rwsem */
4553+struct au_branch {
062440b3 4554+ struct au_xino *br_xino;
1facf9fc 4555+
4556+ aufs_bindex_t br_id;
4557+
4558+ int br_perm;
86dc4139 4559+ struct path br_path;
4a4d8108
AM
4560+ spinlock_t br_dykey_lock;
4561+ struct au_dykey *br_dykey[AuBrDynOp];
acd2b654
AM
4562+ au_lcnt_t br_nfiles; /* opened files */
4563+ au_lcnt_t br_count; /* in-use for other */
1facf9fc 4564+
4565+ struct au_wbr *br_wbr;
076b876e 4566+ struct au_br_fhsm *br_fhsm;
1facf9fc 4567+
027c5e7a 4568+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4569+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4570+#endif
4571+
1facf9fc 4572+#ifdef CONFIG_SYSFS
392086de
AM
4573+ /* entries under sysfs per mount-point */
4574+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4575+#endif
8b6a4947 4576+
062440b3
AM
4577+#ifdef CONFIG_DEBUG_FS
4578+ struct dentry *br_dbgaufs; /* xino */
4579+#endif
4580+
8b6a4947 4581+ struct au_dr_br br_dirren;
1facf9fc 4582+};
4583+
4584+/* ---------------------------------------------------------------------- */
4585+
86dc4139
AM
4586+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4587+{
4588+ return br->br_path.mnt;
4589+}
4590+
4591+static inline struct dentry *au_br_dentry(struct au_branch *br)
4592+{
4593+ return br->br_path.dentry;
4594+}
4595+
4596+static inline struct super_block *au_br_sb(struct au_branch *br)
4597+{
4598+ return au_br_mnt(br)->mnt_sb;
4599+}
4600+
1facf9fc 4601+static inline int au_br_rdonly(struct au_branch *br)
4602+{
8b6a4947 4603+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4604+ || !au_br_writable(br->br_perm))
4605+ ? -EROFS : 0;
4606+}
4607+
4a4d8108 4608+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4609+{
4a4d8108 4610+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4611+ return !(brperm & AuBrPerm_RR);
1facf9fc 4612+#else
4613+ return 0;
4614+#endif
4615+}
4616+
b912730e
AM
4617+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4618+{
4619+ int err, exec_flag;
4620+
4621+ err = 0;
4622+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4623+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4624+ err = -EACCES;
4625+
4626+ return err;
4627+}
4628+
062440b3
AM
4629+static inline void au_xino_get(struct au_branch *br)
4630+{
4631+ struct au_xino *xi;
4632+
4633+ xi = br->br_xino;
4634+ if (xi)
4635+ kref_get(&xi->xi_kref);
4636+}
4637+
4638+static inline int au_xino_count(struct au_branch *br)
4639+{
4640+ int v;
4641+ struct au_xino *xi;
4642+
4643+ v = 0;
4644+ xi = br->br_xino;
4645+ if (xi)
4646+ v = kref_read(&xi->xi_kref);
4647+
4648+ return v;
4649+}
4650+
1facf9fc 4651+/* ---------------------------------------------------------------------- */
4652+
4653+/* branch.c */
4654+struct au_sbinfo;
4655+void au_br_free(struct au_sbinfo *sinfo);
4656+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4657+struct au_opt_add;
4658+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4659+struct au_opt_del;
4660+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4661+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4662+#ifdef CONFIG_COMPAT
4663+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4664+#endif
1facf9fc 4665+struct au_opt_mod;
4666+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4667+ int *do_refresh);
076b876e
AM
4668+struct aufs_stfs;
4669+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4670+
4671+/* xino.c */
4672+static const loff_t au_loff_max = LLONG_MAX;
4673+
acd2b654 4674+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
83b672a5
AM
4675+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4676+ int wbrtop);
062440b3
AM
4677+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4678+ struct file *copy_src);
acd2b654
AM
4679+struct au_xi_new {
4680+ struct au_xino *xi; /* switch between xino and xigen */
4681+ int idx;
4682+ struct path *base;
4683+ struct file *copy_src;
4684+};
4685+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
062440b3
AM
4686+
4687+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4688+ ino_t *ino);
4689+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4690+ ino_t ino);
5527c038 4691+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4692+ loff_t *pos);
5527c038
JR
4693+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4694+ size_t size, loff_t *pos);
062440b3
AM
4695+
4696+int au_xib_trunc(struct super_block *sb);
acd2b654 4697+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
1facf9fc 4698+
acd2b654 4699+struct au_xino *au_xino_alloc(unsigned int nfile);
062440b3 4700+int au_xino_put(struct au_branch *br);
acd2b654 4701+struct file *au_xino_file1(struct au_xino *xi);
062440b3 4702+
1facf9fc 4703+struct au_opt_xino;
1facf9fc 4704+void au_xino_clr(struct super_block *sb);
062440b3 4705+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
1facf9fc 4706+struct file *au_xino_def(struct super_block *sb);
062440b3
AM
4707+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4708+ struct path *base);
4709+
4710+ino_t au_xino_new_ino(struct super_block *sb);
4711+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4712+
521ced18
JR
4713+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4714+ ino_t h_ino, int idx);
4715+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4716+ int *idx);
4717+
062440b3
AM
4718+int au_xino_path(struct seq_file *seq, struct file *file);
4719+
1facf9fc 4720+/* ---------------------------------------------------------------------- */
4721+
acd2b654
AM
4722+/* @idx is signed to accept -1 meaning the first file */
4723+static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4724+{
4725+ struct file *file;
4726+
4727+ file = NULL;
4728+ if (!xi)
4729+ goto out;
4730+
4731+ if (idx >= 0) {
4732+ if (idx < xi->xi_nfile)
4733+ file = xi->xi_file[idx];
4734+ } else
4735+ file = au_xino_file1(xi);
4736+
4737+out:
4738+ return file;
4739+}
4740+
4741+/* ---------------------------------------------------------------------- */
4742+
1facf9fc 4743+/* Superblock to branch */
4744+static inline
4745+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4746+{
4747+ return au_sbr(sb, bindex)->br_id;
4748+}
4749+
4750+static inline
4751+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4752+{
86dc4139 4753+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4754+}
4755+
4756+static inline
4757+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4758+{
86dc4139 4759+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4760+}
4761+
1facf9fc 4762+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4763+{
4764+ return au_sbr(sb, bindex)->br_perm;
4765+}
4766+
4767+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4768+{
4769+ return au_br_whable(au_sbr_perm(sb, bindex));
4770+}
4771+
4772+/* ---------------------------------------------------------------------- */
4773+
8b6a4947
AM
4774+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4775+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4776+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4777+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4778+/*
8b6a4947
AM
4779+#define wbr_wh_read_trylock_nested(wbr) \
4780+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4781+#define wbr_wh_write_trylock_nested(wbr) \
4782+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4783+*/
1facf9fc 4784+
8b6a4947
AM
4785+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4786+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4787+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4788+
4789+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4790+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4791+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4792+
076b876e
AM
4793+/* ---------------------------------------------------------------------- */
4794+
4795+#ifdef CONFIG_AUFS_FHSM
4796+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4797+{
4798+ mutex_init(&brfhsm->bf_lock);
4799+ brfhsm->bf_jiffy = 0;
4800+ brfhsm->bf_readable = 0;
4801+}
4802+
4803+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4804+{
4805+ mutex_destroy(&brfhsm->bf_lock);
4806+}
4807+#else
4808+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4809+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4810+#endif
4811+
1facf9fc 4812+#endif /* __KERNEL__ */
4813+#endif /* __AUFS_BRANCH_H__ */
7f207e10 4814diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
eca34b5c
AM
4815--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
4816+++ linux/fs/aufs/conf.mk 2019-07-11 15:42:14.462237786 +0200
2121bcd9
AM
4817@@ -0,0 +1,40 @@
4818+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4819+
4820+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4821+
4822+define AuConf
4823+ifdef ${1}
4824+AuConfStr += ${1}=${${1}}
4825+endif
4826+endef
4827+
b752ccd1 4828+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4829+ SBILIST \
7f207e10 4830+ HNOTIFY HFSNOTIFY \
4a4d8108 4831+ EXPORT INO_T_64 \
c1595e42 4832+ XATTR \
076b876e 4833+ FHSM \
4a4d8108 4834+ RDU \
8b6a4947 4835+ DIRREN \
4a4d8108
AM
4836+ SHWH \
4837+ BR_RAMFS \
4838+ BR_FUSE POLL \
4839+ BR_HFSPLUS \
4840+ BDEV_LOOP \
b752ccd1
AM
4841+ DEBUG MAGIC_SYSRQ
4842+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4843+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4844+
4845+AuConfName = ${obj}/conf.str
4846+${AuConfName}.tmp: FORCE
4847+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4848+${AuConfName}: ${AuConfName}.tmp
4849+ @diff -q $< $@ > /dev/null 2>&1 || { \
4850+ echo ' GEN ' $@; \
4851+ cp -p $< $@; \
4852+ }
4853+FORCE:
4854+clean-files += ${AuConfName} ${AuConfName}.tmp
4855+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4856+
4857+-include ${srctree}/${src}/conf_priv.mk
7f207e10 4858diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
eca34b5c
AM
4859--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
4860+++ linux/fs/aufs/cpup.c 2019-07-11 15:42:14.462237786 +0200
9f237c51 4861@@ -0,0 +1,1458 @@
cd7a4cd9 4862+// SPDX-License-Identifier: GPL-2.0
1facf9fc 4863+/*
ba1aed25 4864+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 4865+ *
4866+ * This program, aufs is free software; you can redistribute it and/or modify
4867+ * it under the terms of the GNU General Public License as published by
4868+ * the Free Software Foundation; either version 2 of the License, or
4869+ * (at your option) any later version.
dece6358
AM
4870+ *
4871+ * This program is distributed in the hope that it will be useful,
4872+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4873+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4874+ * GNU General Public License for more details.
4875+ *
4876+ * You should have received a copy of the GNU General Public License
523b37e3 4877+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4878+ */
4879+
4880+/*
4881+ * copy-up functions, see wbr_policy.c for copy-down
4882+ */
4883+
4884+#include <linux/fs_stack.h>
dece6358 4885+#include <linux/mm.h>
8cdd5066 4886+#include <linux/task_work.h>
1facf9fc 4887+#include "aufs.h"
4888+
86dc4139 4889+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4890+{
4891+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4892+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4893+
86dc4139
AM
4894+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4895+
4896+ dst->i_flags |= iflags & ~mask;
1facf9fc 4897+ if (au_test_fs_notime(dst->i_sb))
4898+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4899+}
4900+
4901+void au_cpup_attr_timesizes(struct inode *inode)
4902+{
4903+ struct inode *h_inode;
4904+
5afbbe0d 4905+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4906+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 4907+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 4908+}
4909+
4910+void au_cpup_attr_nlink(struct inode *inode, int force)
4911+{
4912+ struct inode *h_inode;
4913+ struct super_block *sb;
5afbbe0d 4914+ aufs_bindex_t bindex, bbot;
1facf9fc 4915+
4916+ sb = inode->i_sb;
5afbbe0d 4917+ bindex = au_ibtop(inode);
1facf9fc 4918+ h_inode = au_h_iptr(inode, bindex);
4919+ if (!force
4920+ && !S_ISDIR(h_inode->i_mode)
4921+ && au_opt_test(au_mntflags(sb), PLINK)
4922+ && au_plink_test(inode))
4923+ return;
4924+
7eafdf33
AM
4925+ /*
4926+ * 0 can happen in revalidating.
38d290e6
JR
4927+ * h_inode->i_mutex may not be held here, but it is harmless since once
4928+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
4929+ * case.
4930+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4931+ * the incorrect link count.
7eafdf33 4932+ */
92d182d2 4933+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 4934+
4935+ /*
4936+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4937+ * it may includes whplink directory.
4938+ */
4939+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
4940+ bbot = au_ibbot(inode);
4941+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 4942+ h_inode = au_h_iptr(inode, bindex);
4943+ if (h_inode)
4944+ au_add_nlink(inode, h_inode);
4945+ }
4946+ }
4947+}
4948+
4949+void au_cpup_attr_changeable(struct inode *inode)
4950+{
4951+ struct inode *h_inode;
4952+
5afbbe0d 4953+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4954+ inode->i_mode = h_inode->i_mode;
4955+ inode->i_uid = h_inode->i_uid;
4956+ inode->i_gid = h_inode->i_gid;
4957+ au_cpup_attr_timesizes(inode);
86dc4139 4958+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 4959+}
4960+
4961+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4962+{
4963+ struct au_iinfo *iinfo = au_ii(inode);
4964+
1308ab2a 4965+ IiMustWriteLock(inode);
4966+
1facf9fc 4967+ iinfo->ii_higen = h_inode->i_generation;
4968+ iinfo->ii_hsb1 = h_inode->i_sb;
4969+}
4970+
4971+void au_cpup_attr_all(struct inode *inode, int force)
4972+{
4973+ struct inode *h_inode;
4974+
5afbbe0d 4975+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4976+ au_cpup_attr_changeable(inode);
4977+ if (inode->i_nlink > 0)
4978+ au_cpup_attr_nlink(inode, force);
4979+ inode->i_rdev = h_inode->i_rdev;
4980+ inode->i_blkbits = h_inode->i_blkbits;
4981+ au_cpup_igen(inode, h_inode);
4982+}
4983+
4984+/* ---------------------------------------------------------------------- */
4985+
4986+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4987+
4988+/* keep the timestamps of the parent dir when cpup */
4989+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4990+ struct path *h_path)
4991+{
4992+ struct inode *h_inode;
4993+
4994+ dt->dt_dentry = dentry;
4995+ dt->dt_h_path = *h_path;
5527c038 4996+ h_inode = d_inode(h_path->dentry);
1facf9fc 4997+ dt->dt_atime = h_inode->i_atime;
4998+ dt->dt_mtime = h_inode->i_mtime;
4999+ /* smp_mb(); */
5000+}
5001+
5002+void au_dtime_revert(struct au_dtime *dt)
5003+{
5004+ struct iattr attr;
5005+ int err;
5006+
5007+ attr.ia_atime = dt->dt_atime;
5008+ attr.ia_mtime = dt->dt_mtime;
5009+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5010+ | ATTR_ATIME | ATTR_ATIME_SET;
5011+
523b37e3
AM
5012+ /* no delegation since this is a directory */
5013+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 5014+ if (unlikely(err))
0c3ec466 5015+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 5016+}
5017+
5018+/* ---------------------------------------------------------------------- */
5019+
86dc4139
AM
5020+/* internal use only */
5021+struct au_cpup_reg_attr {
5022+ int valid;
5023+ struct kstat st;
5024+ unsigned int iflags; /* inode->i_flags */
5025+};
5026+
1facf9fc 5027+static noinline_for_stack
86dc4139
AM
5028+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5029+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5030+{
c1595e42 5031+ int err, sbits, icex;
7e9cd9fe
AM
5032+ unsigned int mnt_flags;
5033+ unsigned char verbose;
1facf9fc 5034+ struct iattr ia;
5035+ struct path h_path;
1308ab2a 5036+ struct inode *h_isrc, *h_idst;
86dc4139 5037+ struct kstat *h_st;
c1595e42 5038+ struct au_branch *br;
1facf9fc 5039+
5040+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 5041+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
5042+ br = au_sbr(dst->d_sb, bindex);
5043+ h_path.mnt = au_br_mnt(br);
5527c038 5044+ h_isrc = d_inode(h_src);
1308ab2a 5045+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 5046+ | ATTR_ATIME | ATTR_MTIME
5047+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
5048+ if (h_src_attr && h_src_attr->valid) {
5049+ h_st = &h_src_attr->st;
5050+ ia.ia_uid = h_st->uid;
5051+ ia.ia_gid = h_st->gid;
5052+ ia.ia_atime = h_st->atime;
5053+ ia.ia_mtime = h_st->mtime;
5054+ if (h_idst->i_mode != h_st->mode
5055+ && !S_ISLNK(h_idst->i_mode)) {
5056+ ia.ia_valid |= ATTR_MODE;
5057+ ia.ia_mode = h_st->mode;
5058+ }
5059+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5060+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5061+ } else {
5062+ ia.ia_uid = h_isrc->i_uid;
5063+ ia.ia_gid = h_isrc->i_gid;
5064+ ia.ia_atime = h_isrc->i_atime;
5065+ ia.ia_mtime = h_isrc->i_mtime;
5066+ if (h_idst->i_mode != h_isrc->i_mode
5067+ && !S_ISLNK(h_idst->i_mode)) {
5068+ ia.ia_valid |= ATTR_MODE;
5069+ ia.ia_mode = h_isrc->i_mode;
5070+ }
5071+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5072+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 5073+ }
523b37e3
AM
5074+ /* no delegation since it is just created */
5075+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5076+
5077+ /* is this nfs only? */
5078+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5079+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5080+ ia.ia_mode = h_isrc->i_mode;
523b37e3 5081+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5082+ }
5083+
c1595e42 5084+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
5085+ if (!err) {
5086+ mnt_flags = au_mntflags(dst->d_sb);
5087+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5088+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5089+ }
c1595e42 5090+
1facf9fc 5091+ return err;
5092+}
5093+
5094+/* ---------------------------------------------------------------------- */
5095+
5096+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5097+ char *buf, unsigned long blksize)
5098+{
5099+ int err;
5100+ size_t sz, rbytes, wbytes;
5101+ unsigned char all_zero;
5102+ char *p, *zp;
febd17d6 5103+ struct inode *h_inode;
1facf9fc 5104+ /* reduce stack usage */
5105+ struct iattr *ia;
5106+
5107+ zp = page_address(ZERO_PAGE(0));
5108+ if (unlikely(!zp))
5109+ return -ENOMEM; /* possible? */
5110+
5111+ err = 0;
5112+ all_zero = 0;
5113+ while (len) {
5114+ AuDbg("len %lld\n", len);
5115+ sz = blksize;
5116+ if (len < blksize)
5117+ sz = len;
5118+
5119+ rbytes = 0;
5120+ /* todo: signal_pending? */
5121+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5122+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5123+ err = rbytes;
5124+ }
5125+ if (unlikely(err < 0))
5126+ break;
5127+
5128+ all_zero = 0;
5129+ if (len >= rbytes && rbytes == blksize)
5130+ all_zero = !memcmp(buf, zp, rbytes);
5131+ if (!all_zero) {
5132+ wbytes = rbytes;
5133+ p = buf;
5134+ while (wbytes) {
5135+ size_t b;
5136+
5137+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5138+ err = b;
5139+ /* todo: signal_pending? */
5140+ if (unlikely(err == -EAGAIN || err == -EINTR))
5141+ continue;
5142+ if (unlikely(err < 0))
5143+ break;
5144+ wbytes -= b;
5145+ p += b;
5146+ }
392086de
AM
5147+ if (unlikely(err < 0))
5148+ break;
1facf9fc 5149+ } else {
5150+ loff_t res;
5151+
5152+ AuLabel(hole);
5153+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5154+ err = res;
5155+ if (unlikely(res < 0))
5156+ break;
5157+ }
5158+ len -= rbytes;
5159+ err = 0;
5160+ }
5161+
5162+ /* the last block may be a hole */
5163+ if (!err && all_zero) {
5164+ AuLabel(last hole);
5165+
5166+ err = 1;
2000de60 5167+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5168+ /* nfs requires this step to make last hole */
5169+ /* is this only nfs? */
5170+ do {
5171+ /* todo: signal_pending? */
5172+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5173+ } while (err == -EAGAIN || err == -EINTR);
5174+ if (err == 1)
5175+ dst->f_pos--;
5176+ }
5177+
5178+ if (err == 1) {
5179+ ia = (void *)buf;
5180+ ia->ia_size = dst->f_pos;
5181+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5182+ ia->ia_file = dst;
febd17d6
JR
5183+ h_inode = file_inode(dst);
5184+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5185+ /* no delegation since it is just created */
5186+ err = vfsub_notify_change(&dst->f_path, ia,
5187+ /*delegated*/NULL);
febd17d6 5188+ inode_unlock(h_inode);
1facf9fc 5189+ }
5190+ }
5191+
5192+ return err;
5193+}
5194+
5195+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5196+{
5197+ int err;
5198+ unsigned long blksize;
5199+ unsigned char do_kfree;
5200+ char *buf;
9f237c51 5201+ struct super_block *h_sb;
1facf9fc 5202+
5203+ err = -ENOMEM;
9f237c51
AM
5204+ h_sb = file_inode(dst)->i_sb;
5205+ blksize = h_sb->s_blocksize;
1facf9fc 5206+ if (!blksize || PAGE_SIZE < blksize)
5207+ blksize = PAGE_SIZE;
5208+ AuDbg("blksize %lu\n", blksize);
5209+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5210+ if (do_kfree)
5211+ buf = kmalloc(blksize, GFP_NOFS);
5212+ else
5213+ buf = (void *)__get_free_page(GFP_NOFS);
5214+ if (unlikely(!buf))
5215+ goto out;
5216+
5217+ if (len > (1 << 22))
5218+ AuDbg("copying a large file %lld\n", (long long)len);
5219+
5220+ src->f_pos = 0;
5221+ dst->f_pos = 0;
5222+ err = au_do_copy_file(dst, src, len, buf, blksize);
9f237c51
AM
5223+ if (do_kfree) {
5224+ AuDebugOn(!au_kfree_do_sz_test(blksize));
5225+ au_kfree_do_rcu(buf);
5226+ } else
1c60b727 5227+ free_page((unsigned long)buf);
1facf9fc 5228+
4f0767ce 5229+out:
1facf9fc 5230+ return err;
5231+}
5232+
1c60b727
AM
5233+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5234+{
5235+ int err;
5236+ struct super_block *h_src_sb;
5237+ struct inode *h_src_inode;
5238+
5239+ h_src_inode = file_inode(src);
5240+ h_src_sb = h_src_inode->i_sb;
5241+
5242+ /* XFS acquires inode_lock */
5243+ if (!au_test_xfs(h_src_sb))
5244+ err = au_copy_file(dst, src, len);
5245+ else {
3c1bdaff 5246+ inode_unlock_shared(h_src_inode);
1c60b727 5247+ err = au_copy_file(dst, src, len);
be118d29 5248+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5249+ }
5250+
5251+ return err;
5252+}
5253+
5254+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5255+{
5256+ int err;
9f237c51 5257+ loff_t lo;
1c60b727
AM
5258+ struct super_block *h_src_sb;
5259+ struct inode *h_src_inode;
5260+
5261+ h_src_inode = file_inode(src);
5262+ h_src_sb = h_src_inode->i_sb;
5263+ if (h_src_sb != file_inode(dst)->i_sb
9f237c51 5264+ || !dst->f_op->remap_file_range) {
1c60b727
AM
5265+ err = au_do_copy(dst, src, len);
5266+ goto out;
5267+ }
5268+
5269+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5270+ inode_unlock_shared(h_src_inode);
9f237c51 5271+ lo = vfsub_clone_file_range(src, dst, len);
be118d29 5272+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727 5273+ } else
9f237c51
AM
5274+ lo = vfsub_clone_file_range(src, dst, len);
5275+ if (lo == len) {
5276+ err = 0;
5277+ goto out; /* success */
5278+ } else if (lo >= 0)
5279+ /* todo: possible? */
5280+ /* paritially succeeded */
5281+ AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5282+ else if (lo != -EOPNOTSUPP) {
5283+ /* older XFS has a condition in cloning */
5284+ err = lo;
1c60b727 5285+ goto out;
9f237c51 5286+ }
1c60b727
AM
5287+
5288+ /* the backend fs on NFS may not support cloning */
5289+ err = au_do_copy(dst, src, len);
5290+
5291+out:
5292+ AuTraceErr(err);
5293+ return err;
5294+}
5295+
1facf9fc 5296+/*
5297+ * to support a sparse file which is opened with O_APPEND,
5298+ * we need to close the file.
5299+ */
c2b27bf2 5300+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5301+{
5302+ int err, i;
5303+ enum { SRC, DST };
5304+ struct {
5305+ aufs_bindex_t bindex;
5306+ unsigned int flags;
5307+ struct dentry *dentry;
392086de 5308+ int force_wr;
1facf9fc 5309+ struct file *file;
1facf9fc 5310+ } *f, file[] = {
5311+ {
c2b27bf2 5312+ .bindex = cpg->bsrc,
1facf9fc 5313+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
1facf9fc 5314+ },
5315+ {
c2b27bf2 5316+ .bindex = cpg->bdst,
1facf9fc 5317+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5318+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
1facf9fc 5319+ }
5320+ };
acd2b654 5321+ struct au_branch *br;
521ced18 5322+ struct super_block *sb, *h_src_sb;
e2f27e51 5323+ struct inode *h_src_inode;
8cdd5066 5324+ struct task_struct *tsk = current;
1facf9fc 5325+
5326+ /* bsrc branch can be ro/rw. */
c2b27bf2 5327+ sb = cpg->dentry->d_sb;
1facf9fc 5328+ f = file;
5329+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5330+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5331+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5332+ /*file*/NULL, f->force_wr);
9f237c51
AM
5333+ if (IS_ERR(f->file)) {
5334+ err = PTR_ERR(f->file);
5335+ if (i == SRC)
5336+ goto out;
5337+ else
5338+ goto out_src;
5339+ }
1facf9fc 5340+ }
5341+
5342+ /* try stopping to update while we copyup */
e2f27e51 5343+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5344+ h_src_sb = h_src_inode->i_sb;
5345+ if (!au_test_nfs(h_src_sb))
e2f27e51 5346+ IMustLock(h_src_inode);
1c60b727 5347+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5348+
8cdd5066
JR
5349+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5350+ if (tsk->flags & PF_KTHREAD)
5351+ __fput_sync(file[DST].file);
5352+ else {
062440b3 5353+ /* it happened actually */
8cdd5066
JR
5354+ fput(file[DST].file);
5355+ /*
5356+ * too bad.
5357+ * we have to call both since we don't know which place the file
5358+ * was added to.
5359+ */
5360+ task_work_run();
5361+ flush_delayed_fput();
5362+ }
acd2b654
AM
5363+ br = au_sbr(sb, file[DST].bindex);
5364+ au_lcnt_dec(&br->br_nfiles);
523b37e3 5365+
4f0767ce 5366+out_src:
1facf9fc 5367+ fput(file[SRC].file);
acd2b654
AM
5368+ br = au_sbr(sb, file[SRC].bindex);
5369+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 5370+out:
1facf9fc 5371+ return err;
5372+}
5373+
c2b27bf2 5374+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5375+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5376+{
5377+ int err, rerr;
5378+ loff_t l;
86dc4139 5379+ struct path h_path;
38d290e6 5380+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5381+
5382+ err = 0;
5527c038 5383+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5384+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5385+ if (cpg->len == -1 || l < cpg->len)
5386+ cpg->len = l;
5387+ if (cpg->len) {
86dc4139 5388+ /* try stopping to update while we are referencing */
be118d29 5389+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5390+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5391+
c2b27bf2
AM
5392+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5393+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5394+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5395+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5396+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5397+ else {
3c1bdaff 5398+ inode_unlock_shared(h_src_inode);
521ced18 5399+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5400+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5401+ }
86dc4139 5402+ if (unlikely(err)) {
3c1bdaff 5403+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5404+ goto out;
5405+ }
5406+ h_src_attr->valid = 1;
e2f27e51
AM
5407+ if (!au_test_nfs(h_src_inode->i_sb)) {
5408+ err = au_cp_regular(cpg);
3c1bdaff 5409+ inode_unlock_shared(h_src_inode);
e2f27e51 5410+ } else {
3c1bdaff 5411+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5412+ err = au_cp_regular(cpg);
5413+ }
c2b27bf2 5414+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5415+ if (!err && rerr)
5416+ err = rerr;
1facf9fc 5417+ }
38d290e6
JR
5418+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5419+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5420+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5421+ spin_lock(&h_dst_inode->i_lock);
5422+ h_dst_inode->i_state |= I_LINKABLE;
5423+ spin_unlock(&h_dst_inode->i_lock);
5424+ }
1facf9fc 5425+
4f0767ce 5426+out:
1facf9fc 5427+ return err;
5428+}
5429+
5430+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5431+ struct inode *h_dir)
5432+{
5433+ int err, symlen;
5434+ mm_segment_t old_fs;
b752ccd1
AM
5435+ union {
5436+ char *k;
5437+ char __user *u;
5438+ } sym;
1facf9fc 5439+
5440+ err = -ENOMEM;
537831f9 5441+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5442+ if (unlikely(!sym.k))
1facf9fc 5443+ goto out;
5444+
9dbd164d 5445+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5446+ old_fs = get_fs();
5447+ set_fs(KERNEL_DS);
a2654f78 5448+ symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5449+ err = symlen;
5450+ set_fs(old_fs);
5451+
5452+ if (symlen > 0) {
b752ccd1
AM
5453+ sym.k[symlen] = 0;
5454+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5455+ }
1c60b727 5456+ free_page((unsigned long)sym.k);
1facf9fc 5457+
4f0767ce 5458+out:
1facf9fc 5459+ return err;
5460+}
5461+
8cdd5066
JR
5462+/*
5463+ * regardless 'acl' option, reset all ACL.
5464+ * All ACL will be copied up later from the original entry on the lower branch.
5465+ */
5466+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5467+{
5468+ int err;
5469+ struct dentry *h_dentry;
5470+ struct inode *h_inode;
5471+
5472+ h_dentry = h_path->dentry;
5473+ h_inode = d_inode(h_dentry);
5474+ /* forget_all_cached_acls(h_inode)); */
5475+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5476+ AuTraceErr(err);
5477+ if (err == -EOPNOTSUPP)
5478+ err = 0;
5479+ if (!err)
5480+ err = vfsub_acl_chmod(h_inode, mode);
5481+
5482+ AuTraceErr(err);
5483+ return err;
5484+}
5485+
5486+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5487+ struct inode *h_dir, struct path *h_path)
5488+{
5489+ int err;
5490+ struct inode *dir, *inode;
5491+
5492+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5493+ AuTraceErr(err);
5494+ if (err == -EOPNOTSUPP)
5495+ err = 0;
5496+ if (unlikely(err))
5497+ goto out;
5498+
5499+ /*
5500+ * strange behaviour from the users view,
acd2b654 5501+ * particularly setattr case
8cdd5066
JR
5502+ */
5503+ dir = d_inode(dst_parent);
5afbbe0d 5504+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5505+ au_cpup_attr_nlink(dir, /*force*/1);
5506+ inode = d_inode(cpg->dentry);
5507+ au_cpup_attr_nlink(inode, /*force*/1);
5508+
5509+out:
5510+ return err;
5511+}
5512+
1facf9fc 5513+static noinline_for_stack
c2b27bf2 5514+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5515+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5516+{
5517+ int err;
5518+ umode_t mode;
5519+ unsigned int mnt_flags;
076b876e 5520+ unsigned char isdir, isreg, force;
c2b27bf2 5521+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5522+ struct au_dtime dt;
5523+ struct path h_path;
5524+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5525+ struct inode *h_inode, *h_dir;
1facf9fc 5526+ struct super_block *sb;
5527+
5528+ /* bsrc branch can be ro/rw. */
c2b27bf2 5529+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5530+ h_inode = d_inode(h_src);
5531+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5532+
5533+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5534+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5535+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5536+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5537+ AUFS_WH_PFX_LEN));
1facf9fc 5538+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5539+ h_dir = d_inode(h_parent);
1facf9fc 5540+ IMustLock(h_dir);
5541+ AuDebugOn(h_parent != h_dst->d_parent);
5542+
c2b27bf2
AM
5543+ sb = cpg->dentry->d_sb;
5544+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5545+ if (do_dt) {
5546+ h_path.dentry = h_parent;
5547+ au_dtime_store(&dt, dst_parent, &h_path);
5548+ }
5549+ h_path.dentry = h_dst;
5550+
076b876e 5551+ isreg = 0;
1facf9fc 5552+ isdir = 0;
5553+ mode = h_inode->i_mode;
5554+ switch (mode & S_IFMT) {
5555+ case S_IFREG:
076b876e 5556+ isreg = 1;
cd7a4cd9 5557+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
1facf9fc 5558+ if (!err)
c2b27bf2 5559+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5560+ break;
5561+ case S_IFDIR:
5562+ isdir = 1;
5563+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5564+ if (!err)
5565+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5566+ break;
5567+ case S_IFLNK:
5568+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5569+ break;
5570+ case S_IFCHR:
5571+ case S_IFBLK:
5572+ AuDebugOn(!capable(CAP_MKNOD));
5573+ /*FALLTHROUGH*/
5574+ case S_IFIFO:
5575+ case S_IFSOCK:
5576+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5577+ break;
5578+ default:
5579+ AuIOErr("Unknown inode type 0%o\n", mode);
5580+ err = -EIO;
5581+ }
8cdd5066
JR
5582+ if (!err)
5583+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5584+
5585+ mnt_flags = au_mntflags(sb);
5586+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5587+ && !isdir
5588+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5589+ && (h_inode->i_nlink == 1
5590+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5591+ /* todo: unnecessary? */
5527c038 5592+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5593+ && cpg->bdst < cpg->bsrc
5594+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5595+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5596+ /* ignore this error */
5597+
076b876e
AM
5598+ if (!err) {
5599+ force = 0;
5600+ if (isreg) {
5601+ force = !!cpg->len;
5602+ if (cpg->len == -1)
5603+ force = !!i_size_read(h_inode);
5604+ }
5605+ au_fhsm_wrote(sb, cpg->bdst, force);
5606+ }
5607+
1facf9fc 5608+ if (do_dt)
5609+ au_dtime_revert(&dt);
5610+ return err;
5611+}
5612+
392086de 5613+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5614+{
5615+ int err;
392086de 5616+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5617+ struct inode *h_dir;
392086de 5618+ aufs_bindex_t bdst;
86dc4139 5619+
392086de
AM
5620+ dentry = cpg->dentry;
5621+ bdst = cpg->bdst;
5622+ h_dentry = au_h_dptr(dentry, bdst);
5623+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5624+ dget(h_dentry);
5625+ au_set_h_dptr(dentry, bdst, NULL);
5626+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5627+ if (!err)
5628+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5629+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5630+ } else {
5631+ err = 0;
5632+ parent = dget_parent(dentry);
5633+ h_parent = au_h_dptr(parent, bdst);
5634+ dput(parent);
5635+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5636+ if (IS_ERR(h_path->dentry))
5637+ err = PTR_ERR(h_path->dentry);
86dc4139 5638+ }
392086de
AM
5639+ if (unlikely(err))
5640+ goto out;
86dc4139 5641+
86dc4139 5642+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5643+ h_dir = d_inode(h_parent);
86dc4139 5644+ IMustLock(h_dir);
523b37e3
AM
5645+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5646+ /* no delegation since it is just created */
f2c43d5f
AM
5647+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5648+ /*flags*/0);
86dc4139
AM
5649+ dput(h_path->dentry);
5650+
5651+out:
5652+ return err;
5653+}
5654+
1facf9fc 5655+/*
5656+ * copyup the @dentry from @bsrc to @bdst.
5657+ * the caller must set the both of lower dentries.
5658+ * @len is for truncating when it is -1 copyup the entire file.
5659+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5660+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f 5661+ * aufs doesn't touch the credential so
acd2b654 5662+ * security_inode_copy_up{,_xattr}() are unnecessary.
1facf9fc 5663+ */
c2b27bf2 5664+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5665+{
5666+ int err, rerr;
5afbbe0d 5667+ aufs_bindex_t old_ibtop;
1facf9fc 5668+ unsigned char isdir, plink;
1facf9fc 5669+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5670+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5671+ struct super_block *sb;
86dc4139 5672+ struct au_branch *br;
acd2b654 5673+ /* to reduce stack size */
c2b27bf2
AM
5674+ struct {
5675+ struct au_dtime dt;
5676+ struct path h_path;
5677+ struct au_cpup_reg_attr h_src_attr;
5678+ } *a;
1facf9fc 5679+
c2b27bf2
AM
5680+ err = -ENOMEM;
5681+ a = kmalloc(sizeof(*a), GFP_NOFS);
5682+ if (unlikely(!a))
5683+ goto out;
5684+ a->h_src_attr.valid = 0;
1facf9fc 5685+
c2b27bf2
AM
5686+ sb = cpg->dentry->d_sb;
5687+ br = au_sbr(sb, cpg->bdst);
5688+ a->h_path.mnt = au_br_mnt(br);
5689+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5690+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5691+ h_dir = d_inode(h_parent);
1facf9fc 5692+ IMustLock(h_dir);
5693+
c2b27bf2 5694+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5695+ inode = d_inode(cpg->dentry);
1facf9fc 5696+
5697+ if (!dst_parent)
c2b27bf2 5698+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5699+ else
5700+ dget(dst_parent);
5701+
5702+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5703+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5704+ if (dst_inode) {
5705+ if (unlikely(!plink)) {
5706+ err = -EIO;
027c5e7a
AM
5707+ AuIOErr("hi%lu(i%lu) exists on b%d "
5708+ "but plink is disabled\n",
c2b27bf2
AM
5709+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5710+ goto out_parent;
1facf9fc 5711+ }
5712+
5713+ if (dst_inode->i_nlink) {
c2b27bf2 5714+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5715+
c2b27bf2 5716+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5717+ err = PTR_ERR(h_src);
5718+ if (IS_ERR(h_src))
c2b27bf2 5719+ goto out_parent;
5527c038 5720+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5721+ err = -EIO;
79b8bda9 5722+ AuIOErr("i%lu exists on b%d "
027c5e7a 5723+ "but not pseudo-linked\n",
79b8bda9 5724+ inode->i_ino, cpg->bdst);
1facf9fc 5725+ dput(h_src);
c2b27bf2 5726+ goto out_parent;
1facf9fc 5727+ }
5728+
5729+ if (do_dt) {
c2b27bf2
AM
5730+ a->h_path.dentry = h_parent;
5731+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5732+ }
86dc4139 5733+
c2b27bf2 5734+ a->h_path.dentry = h_dst;
523b37e3
AM
5735+ delegated = NULL;
5736+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5737+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5738+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5739+ if (do_dt)
c2b27bf2 5740+ au_dtime_revert(&a->dt);
523b37e3
AM
5741+ if (unlikely(err == -EWOULDBLOCK)) {
5742+ pr_warn("cannot retry for NFSv4 delegation"
5743+ " for an internal link\n");
5744+ iput(delegated);
5745+ }
1facf9fc 5746+ dput(h_src);
c2b27bf2 5747+ goto out_parent;
1facf9fc 5748+ } else
5749+ /* todo: cpup_wh_file? */
5750+ /* udba work */
4a4d8108 5751+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5752+ }
5753+
86dc4139 5754+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5755+ old_ibtop = au_ibtop(inode);
c2b27bf2 5756+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5757+ if (unlikely(err))
86dc4139 5758+ goto out_rev;
5527c038 5759+ dst_inode = d_inode(h_dst);
febd17d6 5760+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5761+ /* todo: necessary? */
c2b27bf2 5762+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5763+
c2b27bf2 5764+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5765+ if (unlikely(err)) {
5766+ /* todo: necessary? */
c2b27bf2 5767+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5768+ inode_unlock(dst_inode);
86dc4139
AM
5769+ goto out_rev;
5770+ }
5771+
5afbbe0d 5772+ if (cpg->bdst < old_ibtop) {
86dc4139 5773+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5774+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5775+ if (unlikely(err)) {
c2b27bf2
AM
5776+ /* ignore an error */
5777+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5778+ inode_unlock(dst_inode);
86dc4139 5779+ goto out_rev;
4a4d8108 5780+ }
4a4d8108 5781+ }
5afbbe0d 5782+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5783+ } else
5afbbe0d 5784+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5785+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5786+ au_hi_flags(inode, isdir));
5787+
5788+ /* todo: necessary? */
c2b27bf2 5789+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5790+ inode_unlock(dst_inode);
86dc4139
AM
5791+ if (unlikely(err))
5792+ goto out_rev;
5793+
5527c038 5794+ src_inode = d_inode(h_src);
86dc4139 5795+ if (!isdir
5527c038
JR
5796+ && (src_inode->i_nlink > 1
5797+ || src_inode->i_state & I_LINKABLE)
86dc4139 5798+ && plink)
c2b27bf2 5799+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5800+
c2b27bf2
AM
5801+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5802+ a->h_path.dentry = h_dst;
392086de 5803+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5804+ }
5805+ if (!err)
c2b27bf2 5806+ goto out_parent; /* success */
1facf9fc 5807+
5808+ /* revert */
4a4d8108 5809+out_rev:
c2b27bf2
AM
5810+ a->h_path.dentry = h_parent;
5811+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5812+ a->h_path.dentry = h_dst;
86dc4139 5813+ rerr = 0;
5527c038 5814+ if (d_is_positive(h_dst)) {
523b37e3
AM
5815+ if (!isdir) {
5816+ /* no delegation since it is just created */
5817+ rerr = vfsub_unlink(h_dir, &a->h_path,
5818+ /*delegated*/NULL, /*force*/0);
5819+ } else
c2b27bf2 5820+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5821+ }
c2b27bf2 5822+ au_dtime_revert(&a->dt);
1facf9fc 5823+ if (rerr) {
5824+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5825+ err = -EIO;
5826+ }
c2b27bf2 5827+out_parent:
1facf9fc 5828+ dput(dst_parent);
9f237c51 5829+ au_kfree_rcu(a);
c2b27bf2 5830+out:
1facf9fc 5831+ return err;
5832+}
5833+
7e9cd9fe 5834+#if 0 /* reserved */
1facf9fc 5835+struct au_cpup_single_args {
5836+ int *errp;
c2b27bf2 5837+ struct au_cp_generic *cpg;
1facf9fc 5838+ struct dentry *dst_parent;
5839+};
5840+
5841+static void au_call_cpup_single(void *args)
5842+{
5843+ struct au_cpup_single_args *a = args;
86dc4139 5844+
c2b27bf2
AM
5845+ au_pin_hdir_acquire_nest(a->cpg->pin);
5846+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5847+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5848+}
c2b27bf2 5849+#endif
1facf9fc 5850+
53392da6
AM
5851+/*
5852+ * prevent SIGXFSZ in copy-up.
5853+ * testing CAP_MKNOD is for generic fs,
5854+ * but CAP_FSETID is for xfs only, currently.
5855+ */
86dc4139 5856+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5857+{
5858+ int do_sio;
86dc4139
AM
5859+ struct super_block *sb;
5860+ struct inode *h_dir;
53392da6
AM
5861+
5862+ do_sio = 0;
86dc4139 5863+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5864+ if (!au_wkq_test()
5865+ && (!au_sbi(sb)->si_plink_maint_pid
5866+ || au_plink_maint(sb, AuLock_NOPLM))) {
5867+ switch (mode & S_IFMT) {
5868+ case S_IFREG:
5869+ /* no condition about RLIMIT_FSIZE and the file size */
5870+ do_sio = 1;
5871+ break;
5872+ case S_IFCHR:
5873+ case S_IFBLK:
5874+ do_sio = !capable(CAP_MKNOD);
5875+ break;
5876+ }
5877+ if (!do_sio)
5878+ do_sio = ((mode & (S_ISUID | S_ISGID))
5879+ && !capable(CAP_FSETID));
86dc4139
AM
5880+ /* this workaround may be removed in the future */
5881+ if (!do_sio) {
5882+ h_dir = au_pinned_h_dir(pin);
5883+ do_sio = h_dir->i_mode & S_ISVTX;
5884+ }
53392da6
AM
5885+ }
5886+
5887+ return do_sio;
5888+}
5889+
7e9cd9fe 5890+#if 0 /* reserved */
c2b27bf2 5891+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5892+{
5893+ int err, wkq_err;
1facf9fc 5894+ struct dentry *h_dentry;
5895+
c2b27bf2 5896+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5897+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5898+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5899+ else {
5900+ struct au_cpup_single_args args = {
5901+ .errp = &err,
c2b27bf2
AM
5902+ .cpg = cpg,
5903+ .dst_parent = dst_parent
1facf9fc 5904+ };
5905+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5906+ if (unlikely(wkq_err))
5907+ err = wkq_err;
5908+ }
5909+
5910+ return err;
5911+}
c2b27bf2 5912+#endif
1facf9fc 5913+
5914+/*
5915+ * copyup the @dentry from the first active lower branch to @bdst,
5916+ * using au_cpup_single().
5917+ */
c2b27bf2 5918+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5919+{
5920+ int err;
c2b27bf2
AM
5921+ unsigned int flags_orig;
5922+ struct dentry *dentry;
5923+
5924+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 5925+
c2b27bf2 5926+ dentry = cpg->dentry;
86dc4139 5927+ DiMustWriteLock(dentry);
1facf9fc 5928+
c2b27bf2 5929+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 5930+ if (!err) {
c2b27bf2
AM
5931+ flags_orig = cpg->flags;
5932+ au_fset_cpup(cpg->flags, RENAME);
5933+ err = au_cpup_single(cpg, NULL);
5934+ cpg->flags = flags_orig;
1facf9fc 5935+ if (!err)
5936+ return 0; /* success */
5937+
5938+ /* revert */
c2b27bf2 5939+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 5940+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 5941+ }
5942+
5943+ return err;
5944+}
5945+
5946+struct au_cpup_simple_args {
5947+ int *errp;
c2b27bf2 5948+ struct au_cp_generic *cpg;
1facf9fc 5949+};
5950+
5951+static void au_call_cpup_simple(void *args)
5952+{
5953+ struct au_cpup_simple_args *a = args;
86dc4139 5954+
c2b27bf2
AM
5955+ au_pin_hdir_acquire_nest(a->cpg->pin);
5956+ *a->errp = au_cpup_simple(a->cpg);
5957+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5958+}
5959+
c2b27bf2 5960+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5961+{
5962+ int err, wkq_err;
c2b27bf2
AM
5963+ struct dentry *dentry, *parent;
5964+ struct file *h_file;
1facf9fc 5965+ struct inode *h_dir;
5966+
c2b27bf2
AM
5967+ dentry = cpg->dentry;
5968+ h_file = NULL;
5969+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
5970+ AuDebugOn(cpg->bsrc < 0);
392086de 5971+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
5972+ err = PTR_ERR(h_file);
5973+ if (IS_ERR(h_file))
5974+ goto out;
5975+ }
5976+
1facf9fc 5977+ parent = dget_parent(dentry);
5527c038 5978+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 5979+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 5980+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 5981+ err = au_cpup_simple(cpg);
1facf9fc 5982+ else {
5983+ struct au_cpup_simple_args args = {
5984+ .errp = &err,
c2b27bf2 5985+ .cpg = cpg
1facf9fc 5986+ };
5987+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5988+ if (unlikely(wkq_err))
5989+ err = wkq_err;
5990+ }
5991+
5992+ dput(parent);
c2b27bf2
AM
5993+ if (h_file)
5994+ au_h_open_post(dentry, cpg->bsrc, h_file);
5995+
5996+out:
1facf9fc 5997+ return err;
5998+}
5999+
c2b27bf2 6000+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 6001+{
5afbbe0d 6002+ aufs_bindex_t bsrc, bbot;
c2b27bf2 6003+ struct dentry *dentry, *h_dentry;
367653fa 6004+
c2b27bf2
AM
6005+ if (cpg->bsrc < 0) {
6006+ dentry = cpg->dentry;
5afbbe0d
AM
6007+ bbot = au_dbbot(dentry);
6008+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
6009+ h_dentry = au_h_dptr(dentry, bsrc);
6010+ if (h_dentry) {
5527c038 6011+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
6012+ break;
6013+ }
6014+ }
5afbbe0d 6015+ AuDebugOn(bsrc > bbot);
c2b27bf2 6016+ cpg->bsrc = bsrc;
367653fa 6017+ }
c2b27bf2
AM
6018+ AuDebugOn(cpg->bsrc <= cpg->bdst);
6019+ return au_do_sio_cpup_simple(cpg);
6020+}
367653fa 6021+
c2b27bf2
AM
6022+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6023+{
6024+ AuDebugOn(cpg->bdst <= cpg->bsrc);
6025+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
6026+}
6027+
1facf9fc 6028+/* ---------------------------------------------------------------------- */
6029+
6030+/*
6031+ * copyup the deleted file for writing.
6032+ */
c2b27bf2
AM
6033+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6034+ struct file *file)
1facf9fc 6035+{
6036+ int err;
c2b27bf2
AM
6037+ unsigned int flags_orig;
6038+ aufs_bindex_t bsrc_orig;
c2b27bf2 6039+ struct au_dinfo *dinfo;
5afbbe0d
AM
6040+ struct {
6041+ struct au_hdentry *hd;
6042+ struct dentry *h_dentry;
6043+ } hdst, hsrc;
1facf9fc 6044+
c2b27bf2 6045+ dinfo = au_di(cpg->dentry);
1308ab2a 6046+ AuRwMustWriteLock(&dinfo->di_rwsem);
6047+
c2b27bf2 6048+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
6049+ cpg->bsrc = dinfo->di_btop;
6050+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
6051+ hdst.h_dentry = hdst.hd->hd_dentry;
6052+ hdst.hd->hd_dentry = wh_dentry;
6053+ dinfo->di_btop = cpg->bdst;
6054+
6055+ hsrc.h_dentry = NULL;
027c5e7a 6056+ if (file) {
5afbbe0d
AM
6057+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6058+ hsrc.h_dentry = hsrc.hd->hd_dentry;
6059+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 6060+ }
c2b27bf2
AM
6061+ flags_orig = cpg->flags;
6062+ cpg->flags = !AuCpup_DTIME;
6063+ err = au_cpup_single(cpg, /*h_parent*/NULL);
6064+ cpg->flags = flags_orig;
027c5e7a
AM
6065+ if (file) {
6066+ if (!err)
6067+ err = au_reopen_nondir(file);
5afbbe0d 6068+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 6069+ }
5afbbe0d
AM
6070+ hdst.hd->hd_dentry = hdst.h_dentry;
6071+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 6072+ cpg->bsrc = bsrc_orig;
1facf9fc 6073+
6074+ return err;
6075+}
6076+
c2b27bf2 6077+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6078+{
6079+ int err;
c2b27bf2 6080+ aufs_bindex_t bdst;
1facf9fc 6081+ struct au_dtime dt;
c2b27bf2 6082+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6083+ struct au_branch *br;
6084+ struct path h_path;
6085+
c2b27bf2
AM
6086+ dentry = cpg->dentry;
6087+ bdst = cpg->bdst;
1facf9fc 6088+ br = au_sbr(dentry->d_sb, bdst);
6089+ parent = dget_parent(dentry);
6090+ h_parent = au_h_dptr(parent, bdst);
6091+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6092+ err = PTR_ERR(wh_dentry);
6093+ if (IS_ERR(wh_dentry))
6094+ goto out;
6095+
6096+ h_path.dentry = h_parent;
86dc4139 6097+ h_path.mnt = au_br_mnt(br);
1facf9fc 6098+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6099+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6100+ if (unlikely(err))
6101+ goto out_wh;
6102+
6103+ dget(wh_dentry);
6104+ h_path.dentry = wh_dentry;
2000de60 6105+ if (!d_is_dir(wh_dentry)) {
523b37e3 6106+ /* no delegation since it is just created */
5527c038 6107+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6108+ /*delegated*/NULL, /*force*/0);
6109+ } else
5527c038 6110+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6111+ if (unlikely(err)) {
523b37e3
AM
6112+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6113+ wh_dentry, err);
1facf9fc 6114+ err = -EIO;
6115+ }
6116+ au_dtime_revert(&dt);
5527c038 6117+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6118+
4f0767ce 6119+out_wh:
1facf9fc 6120+ dput(wh_dentry);
4f0767ce 6121+out:
1facf9fc 6122+ dput(parent);
6123+ return err;
6124+}
6125+
6126+struct au_cpup_wh_args {
6127+ int *errp;
c2b27bf2 6128+ struct au_cp_generic *cpg;
1facf9fc 6129+ struct file *file;
6130+};
6131+
6132+static void au_call_cpup_wh(void *args)
6133+{
6134+ struct au_cpup_wh_args *a = args;
86dc4139 6135+
c2b27bf2
AM
6136+ au_pin_hdir_acquire_nest(a->cpg->pin);
6137+ *a->errp = au_cpup_wh(a->cpg, a->file);
6138+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6139+}
6140+
c2b27bf2 6141+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6142+{
6143+ int err, wkq_err;
c2b27bf2 6144+ aufs_bindex_t bdst;
c1595e42 6145+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6146+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6147+ struct au_wbr *wbr;
c2b27bf2 6148+ struct au_pin wh_pin, *pin_orig;
1facf9fc 6149+
c2b27bf2
AM
6150+ dentry = cpg->dentry;
6151+ bdst = cpg->bdst;
1facf9fc 6152+ parent = dget_parent(dentry);
5527c038 6153+ dir = d_inode(parent);
1facf9fc 6154+ h_orph = NULL;
6155+ h_parent = NULL;
6156+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6157+ h_tmpdir = h_dir;
c2b27bf2 6158+ pin_orig = NULL;
1facf9fc 6159+ if (!h_dir->i_nlink) {
6160+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6161+ h_orph = wbr->wbr_orph;
6162+
6163+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6164+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6165+ h_tmpdir = d_inode(h_orph);
1facf9fc 6166+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6167+
febd17d6 6168+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6169+ /* todo: au_h_open_pre()? */
86dc4139 6170+
c2b27bf2 6171+ pin_orig = cpg->pin;
86dc4139 6172+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6173+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6174+ cpg->pin = &wh_pin;
1facf9fc 6175+ }
6176+
53392da6 6177+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6178+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6179+ err = au_cpup_wh(cpg, file);
1facf9fc 6180+ else {
6181+ struct au_cpup_wh_args args = {
6182+ .errp = &err,
c2b27bf2
AM
6183+ .cpg = cpg,
6184+ .file = file
1facf9fc 6185+ };
6186+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6187+ if (unlikely(wkq_err))
6188+ err = wkq_err;
6189+ }
6190+
6191+ if (h_orph) {
febd17d6 6192+ inode_unlock(h_tmpdir);
4a4d8108 6193+ /* todo: au_h_open_post()? */
1facf9fc 6194+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6195+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6196+ AuDebugOn(!pin_orig);
6197+ cpg->pin = pin_orig;
1facf9fc 6198+ }
6199+ iput(h_dir);
6200+ dput(parent);
6201+
6202+ return err;
6203+}
6204+
6205+/* ---------------------------------------------------------------------- */
6206+
6207+/*
6208+ * generic routine for both of copy-up and copy-down.
6209+ */
6210+/* cf. revalidate function in file.c */
6211+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6212+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6213+ struct au_pin *pin,
1facf9fc 6214+ struct dentry *h_parent, void *arg),
6215+ void *arg)
6216+{
6217+ int err;
6218+ struct au_pin pin;
5527c038 6219+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6220+
6221+ err = 0;
6222+ parent = dget_parent(dentry);
6223+ if (IS_ROOT(parent))
6224+ goto out;
6225+
6226+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6227+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6228+
6229+ /* do not use au_dpage */
6230+ real_parent = parent;
6231+ while (1) {
6232+ dput(parent);
6233+ parent = dget_parent(dentry);
6234+ h_parent = au_h_dptr(parent, bdst);
6235+ if (h_parent)
6236+ goto out; /* success */
6237+
6238+ /* find top dir which is necessary to cpup */
6239+ do {
6240+ d = parent;
6241+ dput(parent);
6242+ parent = dget_parent(d);
6243+ di_read_lock_parent3(parent, !AuLock_IR);
6244+ h_parent = au_h_dptr(parent, bdst);
6245+ di_read_unlock(parent, !AuLock_IR);
6246+ } while (!h_parent);
6247+
6248+ if (d != real_parent)
6249+ di_write_lock_child3(d);
6250+
6251+ /* somebody else might create while we were sleeping */
5527c038
JR
6252+ h_dentry = au_h_dptr(d, bdst);
6253+ if (!h_dentry || d_is_negative(h_dentry)) {
6254+ if (h_dentry)
5afbbe0d 6255+ au_update_dbtop(d);
1facf9fc 6256+
6257+ au_pin_set_dentry(&pin, d);
6258+ err = au_do_pin(&pin);
6259+ if (!err) {
86dc4139 6260+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6261+ au_unpin(&pin);
6262+ }
6263+ }
6264+
6265+ if (d != real_parent)
6266+ di_write_unlock(d);
6267+ if (unlikely(err))
6268+ break;
6269+ }
6270+
4f0767ce 6271+out:
1facf9fc 6272+ dput(parent);
6273+ return err;
6274+}
6275+
6276+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6277+ struct au_pin *pin,
2000de60 6278+ struct dentry *h_parent __maybe_unused,
1facf9fc 6279+ void *arg __maybe_unused)
6280+{
c2b27bf2
AM
6281+ struct au_cp_generic cpg = {
6282+ .dentry = dentry,
6283+ .bdst = bdst,
6284+ .bsrc = -1,
6285+ .len = 0,
6286+ .pin = pin,
6287+ .flags = AuCpup_DTIME
6288+ };
6289+ return au_sio_cpup_simple(&cpg);
1facf9fc 6290+}
6291+
6292+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6293+{
6294+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6295+}
6296+
6297+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6298+{
6299+ int err;
6300+ struct dentry *parent;
6301+ struct inode *dir;
6302+
6303+ parent = dget_parent(dentry);
5527c038 6304+ dir = d_inode(parent);
1facf9fc 6305+ err = 0;
6306+ if (au_h_iptr(dir, bdst))
6307+ goto out;
6308+
6309+ di_read_unlock(parent, AuLock_IR);
6310+ di_write_lock_parent(parent);
6311+ /* someone else might change our inode while we were sleeping */
6312+ if (!au_h_iptr(dir, bdst))
6313+ err = au_cpup_dirs(dentry, bdst);
6314+ di_downgrade_lock(parent, AuLock_IR);
6315+
4f0767ce 6316+out:
1facf9fc 6317+ dput(parent);
6318+ return err;
6319+}
7f207e10 6320diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
eca34b5c
AM
6321--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
6322+++ linux/fs/aufs/cpup.h 2019-07-11 15:42:14.462237786 +0200
062440b3
AM
6323@@ -0,0 +1,100 @@
6324+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6325+/*
ba1aed25 6326+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 6327+ *
6328+ * This program, aufs is free software; you can redistribute it and/or modify
6329+ * it under the terms of the GNU General Public License as published by
6330+ * the Free Software Foundation; either version 2 of the License, or
6331+ * (at your option) any later version.
dece6358
AM
6332+ *
6333+ * This program is distributed in the hope that it will be useful,
6334+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6335+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6336+ * GNU General Public License for more details.
6337+ *
6338+ * You should have received a copy of the GNU General Public License
523b37e3 6339+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6340+ */
6341+
6342+/*
6343+ * copy-up/down functions
6344+ */
6345+
6346+#ifndef __AUFS_CPUP_H__
6347+#define __AUFS_CPUP_H__
6348+
6349+#ifdef __KERNEL__
6350+
dece6358 6351+#include <linux/path.h>
1facf9fc 6352+
dece6358
AM
6353+struct inode;
6354+struct file;
86dc4139 6355+struct au_pin;
dece6358 6356+
86dc4139 6357+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6358+void au_cpup_attr_timesizes(struct inode *inode);
6359+void au_cpup_attr_nlink(struct inode *inode, int force);
6360+void au_cpup_attr_changeable(struct inode *inode);
6361+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6362+void au_cpup_attr_all(struct inode *inode, int force);
6363+
6364+/* ---------------------------------------------------------------------- */
6365+
c2b27bf2
AM
6366+struct au_cp_generic {
6367+ struct dentry *dentry;
6368+ aufs_bindex_t bdst, bsrc;
6369+ loff_t len;
6370+ struct au_pin *pin;
6371+ unsigned int flags;
6372+};
6373+
1facf9fc 6374+/* cpup flags */
392086de
AM
6375+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6376+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6377+ for link(2) */
6378+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6379+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6380+ cpup */
6381+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6382+ existing entry */
6383+#define AuCpup_RWDST (1 << 5) /* force write target even if
6384+ the branch is marked as RO */
c2b27bf2 6385+
8b6a4947
AM
6386+#ifndef CONFIG_AUFS_BR_HFSPLUS
6387+#undef AuCpup_HOPEN
6388+#define AuCpup_HOPEN 0
6389+#endif
6390+
1facf9fc 6391+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6392+#define au_fset_cpup(flags, name) \
6393+ do { (flags) |= AuCpup_##name; } while (0)
6394+#define au_fclr_cpup(flags, name) \
6395+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6396+
6397+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6398+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6399+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6400+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6401+
6402+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6403+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6404+ struct au_pin *pin,
1facf9fc 6405+ struct dentry *h_parent, void *arg),
6406+ void *arg);
6407+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6408+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6409+
6410+/* ---------------------------------------------------------------------- */
6411+
6412+/* keep timestamps when copyup */
6413+struct au_dtime {
6414+ struct dentry *dt_dentry;
6415+ struct path dt_h_path;
cd7a4cd9 6416+ struct timespec64 dt_atime, dt_mtime;
1facf9fc 6417+};
6418+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6419+ struct path *h_path);
6420+void au_dtime_revert(struct au_dtime *dt);
6421+
6422+#endif /* __KERNEL__ */
6423+#endif /* __AUFS_CPUP_H__ */
7f207e10 6424diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
eca34b5c
AM
6425--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
6426+++ linux/fs/aufs/dbgaufs.c 2019-07-11 15:42:14.462237786 +0200
9f237c51 6427@@ -0,0 +1,526 @@
cd7a4cd9 6428+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6429+/*
ba1aed25 6430+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 6431+ *
6432+ * This program, aufs is free software; you can redistribute it and/or modify
6433+ * it under the terms of the GNU General Public License as published by
6434+ * the Free Software Foundation; either version 2 of the License, or
6435+ * (at your option) any later version.
dece6358
AM
6436+ *
6437+ * This program is distributed in the hope that it will be useful,
6438+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6439+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6440+ * GNU General Public License for more details.
6441+ *
6442+ * You should have received a copy of the GNU General Public License
523b37e3 6443+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6444+ */
6445+
6446+/*
6447+ * debugfs interface
6448+ */
6449+
6450+#include <linux/debugfs.h>
6451+#include "aufs.h"
6452+
6453+#ifndef CONFIG_SYSFS
6454+#error DEBUG_FS depends upon SYSFS
6455+#endif
6456+
6457+static struct dentry *dbgaufs;
cd7a4cd9 6458+static const mode_t dbgaufs_mode = 0444;
1facf9fc 6459+
6460+/* 20 is max digits length of ulong 64 */
6461+struct dbgaufs_arg {
6462+ int n;
6463+ char a[20 * 4];
6464+};
6465+
6466+/*
6467+ * common function for all XINO files
6468+ */
6469+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6470+ struct file *file)
6471+{
9f237c51
AM
6472+ void *p;
6473+
6474+ p = file->private_data;
6475+ if (p) {
6476+ /* this is struct dbgaufs_arg */
6477+ AuDebugOn(!au_kfree_sz_test(p));
6478+ au_kfree_do_rcu(p);
6479+ }
1facf9fc 6480+ return 0;
6481+}
6482+
062440b3
AM
6483+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6484+ int cnt)
1facf9fc 6485+{
6486+ int err;
6487+ struct kstat st;
6488+ struct dbgaufs_arg *p;
6489+
6490+ err = -ENOMEM;
6491+ p = kmalloc(sizeof(*p), GFP_NOFS);
6492+ if (unlikely(!p))
6493+ goto out;
6494+
6495+ err = 0;
6496+ p->n = 0;
6497+ file->private_data = p;
6498+ if (!xf)
6499+ goto out;
6500+
521ced18 6501+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6502+ if (!err) {
6503+ if (do_fcnt)
6504+ p->n = snprintf
062440b3
AM
6505+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6506+ cnt, st.blocks, st.blksize,
1facf9fc 6507+ (long long)st.size);
6508+ else
521ced18 6509+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6510+ st.blocks, st.blksize,
6511+ (long long)st.size);
6512+ AuDebugOn(p->n >= sizeof(p->a));
6513+ } else {
6514+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6515+ err = 0;
6516+ }
6517+
4f0767ce 6518+out:
1facf9fc 6519+ return err;
1facf9fc 6520+}
6521+
6522+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6523+ size_t count, loff_t *ppos)
6524+{
6525+ struct dbgaufs_arg *p;
6526+
6527+ p = file->private_data;
6528+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6529+}
6530+
6531+/* ---------------------------------------------------------------------- */
6532+
86dc4139
AM
6533+struct dbgaufs_plink_arg {
6534+ int n;
6535+ char a[];
6536+};
6537+
6538+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6539+ struct file *file)
6540+{
1c60b727 6541+ free_page((unsigned long)file->private_data);
86dc4139
AM
6542+ return 0;
6543+}
6544+
6545+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6546+{
6547+ int err, i, limit;
6548+ unsigned long n, sum;
6549+ struct dbgaufs_plink_arg *p;
6550+ struct au_sbinfo *sbinfo;
6551+ struct super_block *sb;
8b6a4947 6552+ struct hlist_bl_head *hbl;
86dc4139
AM
6553+
6554+ err = -ENOMEM;
6555+ p = (void *)get_zeroed_page(GFP_NOFS);
6556+ if (unlikely(!p))
6557+ goto out;
6558+
6559+ err = -EFBIG;
6560+ sbinfo = inode->i_private;
6561+ sb = sbinfo->si_sb;
6562+ si_noflush_read_lock(sb);
6563+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6564+ limit = PAGE_SIZE - sizeof(p->n);
6565+
6566+ /* the number of buckets */
6567+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6568+ p->n += n;
6569+ limit -= n;
6570+
6571+ sum = 0;
8b6a4947
AM
6572+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6573+ i++, hbl++) {
6574+ n = au_hbl_count(hbl);
86dc4139
AM
6575+ sum += n;
6576+
6577+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6578+ p->n += n;
6579+ limit -= n;
6580+ if (unlikely(limit <= 0))
6581+ goto out_free;
6582+ }
6583+ p->a[p->n - 1] = '\n';
6584+
6585+ /* the sum of plinks */
6586+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6587+ p->n += n;
6588+ limit -= n;
6589+ if (unlikely(limit <= 0))
6590+ goto out_free;
6591+ } else {
6592+#define str "1\n0\n0\n"
6593+ p->n = sizeof(str) - 1;
6594+ strcpy(p->a, str);
6595+#undef str
6596+ }
6597+ si_read_unlock(sb);
6598+
6599+ err = 0;
6600+ file->private_data = p;
6601+ goto out; /* success */
6602+
6603+out_free:
1c60b727 6604+ free_page((unsigned long)p);
86dc4139
AM
6605+out:
6606+ return err;
6607+}
6608+
6609+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6610+ size_t count, loff_t *ppos)
6611+{
6612+ struct dbgaufs_plink_arg *p;
6613+
6614+ p = file->private_data;
6615+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6616+}
6617+
6618+static const struct file_operations dbgaufs_plink_fop = {
6619+ .owner = THIS_MODULE,
6620+ .open = dbgaufs_plink_open,
6621+ .release = dbgaufs_plink_release,
6622+ .read = dbgaufs_plink_read
6623+};
6624+
6625+/* ---------------------------------------------------------------------- */
6626+
1facf9fc 6627+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6628+{
6629+ int err;
6630+ struct au_sbinfo *sbinfo;
6631+ struct super_block *sb;
6632+
6633+ sbinfo = inode->i_private;
6634+ sb = sbinfo->si_sb;
6635+ si_noflush_read_lock(sb);
062440b3 6636+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6637+ si_read_unlock(sb);
6638+ return err;
6639+}
6640+
6641+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6642+ .owner = THIS_MODULE,
1facf9fc 6643+ .open = dbgaufs_xib_open,
6644+ .release = dbgaufs_xi_release,
6645+ .read = dbgaufs_xi_read
6646+};
6647+
6648+/* ---------------------------------------------------------------------- */
6649+
6650+#define DbgaufsXi_PREFIX "xi"
6651+
6652+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6653+{
acd2b654 6654+ int err, idx;
1facf9fc 6655+ long l;
acd2b654
AM
6656+ aufs_bindex_t bindex;
6657+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
1facf9fc 6658+ struct au_sbinfo *sbinfo;
6659+ struct super_block *sb;
acd2b654 6660+ struct au_xino *xi;
1facf9fc 6661+ struct file *xf;
6662+ struct qstr *name;
062440b3 6663+ struct au_branch *br;
1facf9fc 6664+
6665+ err = -ENOENT;
2000de60 6666+ name = &file->f_path.dentry->d_name;
1facf9fc 6667+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6668+ || memcmp(name->name, DbgaufsXi_PREFIX,
6669+ sizeof(DbgaufsXi_PREFIX) - 1)))
6670+ goto out;
acd2b654
AM
6671+
6672+ AuDebugOn(name->len >= sizeof(a));
6673+ memcpy(a, name->name, name->len);
6674+ a[name->len] = '\0';
6675+ p = strchr(a, '-');
6676+ if (p)
6677+ *p = '\0';
6678+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6679+ if (unlikely(err))
6680+ goto out;
acd2b654
AM
6681+ bindex = l;
6682+ idx = 0;
6683+ if (p) {
6684+ err = kstrtol(p + 1, 10, &l);
6685+ if (unlikely(err))
6686+ goto out;
6687+ idx = l;
6688+ }
1facf9fc 6689+
acd2b654 6690+ err = -ENOENT;
1facf9fc 6691+ sbinfo = inode->i_private;
6692+ sb = sbinfo->si_sb;
6693+ si_noflush_read_lock(sb);
acd2b654
AM
6694+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6695+ goto out_si;
6696+ br = au_sbr(sb, bindex);
6697+ xi = br->br_xino;
6698+ if (unlikely(idx >= xi->xi_nfile))
6699+ goto out_si;
6700+ xf = au_xino_file(xi, idx);
6701+ if (xf)
062440b3
AM
6702+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6703+ au_xino_count(br));
1facf9fc 6704+
acd2b654
AM
6705+out_si:
6706+ si_read_unlock(sb);
4f0767ce 6707+out:
acd2b654 6708+ AuTraceErr(err);
1facf9fc 6709+ return err;
6710+}
6711+
6712+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6713+ .owner = THIS_MODULE,
1facf9fc 6714+ .open = dbgaufs_xino_open,
6715+ .release = dbgaufs_xi_release,
6716+ .read = dbgaufs_xi_read
6717+};
6718+
062440b3
AM
6719+void dbgaufs_xino_del(struct au_branch *br)
6720+{
6721+ struct dentry *dbgaufs;
6722+
6723+ dbgaufs = br->br_dbgaufs;
6724+ if (!dbgaufs)
6725+ return;
6726+
6727+ br->br_dbgaufs = NULL;
6728+ /* debugfs acquires the parent i_mutex */
6729+ lockdep_off();
6730+ debugfs_remove(dbgaufs);
6731+ lockdep_on();
6732+}
6733+
1facf9fc 6734+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6735+{
5afbbe0d 6736+ aufs_bindex_t bbot;
1facf9fc 6737+ struct au_branch *br;
1facf9fc 6738+
6739+ if (!au_sbi(sb)->si_dbgaufs)
6740+ return;
6741+
5afbbe0d
AM
6742+ bbot = au_sbbot(sb);
6743+ for (; bindex <= bbot; bindex++) {
1facf9fc 6744+ br = au_sbr(sb, bindex);
062440b3
AM
6745+ dbgaufs_xino_del(br);
6746+ }
6747+}
6748+
acd2b654
AM
6749+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6750+ unsigned int idx, struct dentry *parent,
6751+ struct au_sbinfo *sbinfo)
062440b3
AM
6752+{
6753+ struct au_branch *br;
6754+ struct dentry *d;
acd2b654
AM
6755+ /* "xi" bindex(5) "-" idx(2) NULL */
6756+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
062440b3 6757+
acd2b654
AM
6758+ if (!idx)
6759+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6760+ else
6761+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6762+ bindex, idx);
062440b3
AM
6763+ br = au_sbr(sb, bindex);
6764+ if (br->br_dbgaufs) {
6765+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6766+
6767+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6768+ /* debugfs acquires the parent i_mutex */
6769+ lockdep_off();
6770+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6771+ name);
6772+ lockdep_on();
6773+ if (unlikely(!d))
6774+ pr_warn("failed renaming %pd/%s, ignored.\n",
6775+ parent, name);
6776+ }
6777+ } else {
e2f27e51 6778+ lockdep_off();
062440b3
AM
6779+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6780+ sbinfo, &dbgaufs_xino_fop);
e2f27e51 6781+ lockdep_on();
062440b3 6782+ if (unlikely(!br->br_dbgaufs))
acd2b654 6783+ pr_warn("failed creating %pd/%s, ignored.\n",
062440b3 6784+ parent, name);
1facf9fc 6785+ }
6786+}
6787+
acd2b654
AM
6788+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6789+ struct dentry *parent, struct au_sbinfo *sbinfo)
6790+{
6791+ struct au_branch *br;
6792+ struct au_xino *xi;
6793+ unsigned int u;
6794+
6795+ br = au_sbr(sb, bindex);
6796+ xi = br->br_xino;
6797+ for (u = 0; u < xi->xi_nfile; u++)
6798+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6799+}
6800+
062440b3 6801+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
1facf9fc 6802+{
6803+ struct au_sbinfo *sbinfo;
6804+ struct dentry *parent;
5afbbe0d 6805+ aufs_bindex_t bbot;
062440b3
AM
6806+
6807+ if (!au_opt_test(au_mntflags(sb), XINO))
6808+ return;
1facf9fc 6809+
6810+ sbinfo = au_sbi(sb);
6811+ parent = sbinfo->si_dbgaufs;
6812+ if (!parent)
6813+ return;
6814+
5afbbe0d 6815+ bbot = au_sbbot(sb);
062440b3
AM
6816+ if (topdown)
6817+ for (; bindex <= bbot; bindex++)
6818+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6819+ else
6820+ for (; bbot >= bindex; bbot--)
6821+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
1facf9fc 6822+}
6823+
6824+/* ---------------------------------------------------------------------- */
6825+
6826+#ifdef CONFIG_AUFS_EXPORT
6827+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6828+{
6829+ int err;
6830+ struct au_sbinfo *sbinfo;
6831+ struct super_block *sb;
6832+
6833+ sbinfo = inode->i_private;
6834+ sb = sbinfo->si_sb;
6835+ si_noflush_read_lock(sb);
062440b3 6836+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6837+ si_read_unlock(sb);
6838+ return err;
6839+}
6840+
6841+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6842+ .owner = THIS_MODULE,
1facf9fc 6843+ .open = dbgaufs_xigen_open,
6844+ .release = dbgaufs_xi_release,
6845+ .read = dbgaufs_xi_read
6846+};
6847+
6848+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6849+{
6850+ int err;
6851+
dece6358 6852+ /*
c1595e42 6853+ * This function is a dynamic '__init' function actually,
dece6358
AM
6854+ * so the tiny check for si_rwsem is unnecessary.
6855+ */
6856+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6857+
1facf9fc 6858+ err = -EIO;
6859+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6860+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6861+ &dbgaufs_xigen_fop);
6862+ if (sbinfo->si_dbgaufs_xigen)
6863+ err = 0;
6864+
6865+ return err;
6866+}
6867+#else
6868+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6869+{
6870+ return 0;
6871+}
6872+#endif /* CONFIG_AUFS_EXPORT */
6873+
6874+/* ---------------------------------------------------------------------- */
6875+
6876+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6877+{
dece6358 6878+ /*
7e9cd9fe 6879+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6880+ * so the tiny check for si_rwsem is unnecessary.
6881+ */
6882+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6883+
1facf9fc 6884+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6885+ sbinfo->si_dbgaufs = NULL;
1facf9fc 6886+}
6887+
6888+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6889+{
6890+ int err;
6891+ char name[SysaufsSiNameLen];
6892+
dece6358 6893+ /*
c1595e42 6894+ * This function is a dynamic '__init' function actually,
dece6358
AM
6895+ * so the tiny check for si_rwsem is unnecessary.
6896+ */
6897+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6898+
1facf9fc 6899+ err = -ENOENT;
6900+ if (!dbgaufs) {
6901+ AuErr1("/debug/aufs is uninitialized\n");
6902+ goto out;
6903+ }
6904+
6905+ err = -EIO;
6906+ sysaufs_name(sbinfo, name);
6907+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6908+ if (unlikely(!sbinfo->si_dbgaufs))
6909+ goto out;
1facf9fc 6910+
062440b3 6911+ /* regardless plink/noplink option */
86dc4139
AM
6912+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6913+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6914+ &dbgaufs_plink_fop);
6915+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6916+ goto out_dir;
6917+
062440b3
AM
6918+ /* regardless xino/noxino option */
6919+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6920+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6921+ &dbgaufs_xib_fop);
6922+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6923+ goto out_dir;
6924+
1facf9fc 6925+ err = dbgaufs_xigen_init(sbinfo);
6926+ if (!err)
6927+ goto out; /* success */
6928+
4f0767ce 6929+out_dir:
1facf9fc 6930+ dbgaufs_si_fin(sbinfo);
4f0767ce 6931+out:
062440b3
AM
6932+ if (unlikely(err))
6933+ pr_err("debugfs/aufs failed\n");
1facf9fc 6934+ return err;
6935+}
6936+
6937+/* ---------------------------------------------------------------------- */
6938+
6939+void dbgaufs_fin(void)
6940+{
6941+ debugfs_remove(dbgaufs);
6942+}
6943+
6944+int __init dbgaufs_init(void)
6945+{
6946+ int err;
6947+
6948+ err = -EIO;
6949+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6950+ if (dbgaufs)
6951+ err = 0;
6952+ return err;
6953+}
7f207e10 6954diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
eca34b5c
AM
6955--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
6956+++ linux/fs/aufs/dbgaufs.h 2019-07-11 15:42:14.462237786 +0200
062440b3
AM
6957@@ -0,0 +1,53 @@
6958+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6959+/*
ba1aed25 6960+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 6961+ *
6962+ * This program, aufs is free software; you can redistribute it and/or modify
6963+ * it under the terms of the GNU General Public License as published by
6964+ * the Free Software Foundation; either version 2 of the License, or
6965+ * (at your option) any later version.
dece6358
AM
6966+ *
6967+ * This program is distributed in the hope that it will be useful,
6968+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6969+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6970+ * GNU General Public License for more details.
6971+ *
6972+ * You should have received a copy of the GNU General Public License
523b37e3 6973+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6974+ */
6975+
6976+/*
6977+ * debugfs interface
6978+ */
6979+
6980+#ifndef __DBGAUFS_H__
6981+#define __DBGAUFS_H__
6982+
6983+#ifdef __KERNEL__
6984+
dece6358 6985+struct super_block;
1facf9fc 6986+struct au_sbinfo;
062440b3 6987+struct au_branch;
dece6358 6988+
1facf9fc 6989+#ifdef CONFIG_DEBUG_FS
6990+/* dbgaufs.c */
062440b3 6991+void dbgaufs_xino_del(struct au_branch *br);
1facf9fc 6992+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
062440b3 6993+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
1facf9fc 6994+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6995+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6996+void dbgaufs_fin(void);
6997+int __init dbgaufs_init(void);
1facf9fc 6998+#else
062440b3 6999+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
4a4d8108 7000+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
062440b3
AM
7001+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
7002+ int topdown)
4a4d8108
AM
7003+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7004+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7005+AuStubVoid(dbgaufs_fin, void)
7006+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 7007+#endif /* CONFIG_DEBUG_FS */
7008+
7009+#endif /* __KERNEL__ */
7010+#endif /* __DBGAUFS_H__ */
7f207e10 7011diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
eca34b5c
AM
7012--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
7013+++ linux/fs/aufs/dcsub.c 2019-07-11 15:42:14.462237786 +0200
cd7a4cd9
AM
7014@@ -0,0 +1,225 @@
7015+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7016+/*
ba1aed25 7017+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 7018+ *
7019+ * This program, aufs is free software; you can redistribute it and/or modify
7020+ * it under the terms of the GNU General Public License as published by
7021+ * the Free Software Foundation; either version 2 of the License, or
7022+ * (at your option) any later version.
dece6358
AM
7023+ *
7024+ * This program is distributed in the hope that it will be useful,
7025+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7026+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7027+ * GNU General Public License for more details.
7028+ *
7029+ * You should have received a copy of the GNU General Public License
523b37e3 7030+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7031+ */
7032+
7033+/*
7034+ * sub-routines for dentry cache
7035+ */
7036+
7037+#include "aufs.h"
7038+
7039+static void au_dpage_free(struct au_dpage *dpage)
7040+{
7041+ int i;
7042+ struct dentry **p;
7043+
7044+ p = dpage->dentries;
7045+ for (i = 0; i < dpage->ndentry; i++)
7046+ dput(*p++);
1c60b727 7047+ free_page((unsigned long)dpage->dentries);
1facf9fc 7048+}
7049+
7050+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7051+{
7052+ int err;
7053+ void *p;
7054+
7055+ err = -ENOMEM;
7056+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7057+ if (unlikely(!dpages->dpages))
7058+ goto out;
7059+
7060+ p = (void *)__get_free_page(gfp);
7061+ if (unlikely(!p))
7062+ goto out_dpages;
7063+
7064+ dpages->dpages[0].ndentry = 0;
7065+ dpages->dpages[0].dentries = p;
7066+ dpages->ndpage = 1;
7067+ return 0; /* success */
7068+
4f0767ce 7069+out_dpages:
9f237c51 7070+ au_kfree_try_rcu(dpages->dpages);
4f0767ce 7071+out:
1facf9fc 7072+ return err;
7073+}
7074+
7075+void au_dpages_free(struct au_dcsub_pages *dpages)
7076+{
7077+ int i;
7078+ struct au_dpage *p;
7079+
7080+ p = dpages->dpages;
7081+ for (i = 0; i < dpages->ndpage; i++)
7082+ au_dpage_free(p++);
9f237c51 7083+ au_kfree_try_rcu(dpages->dpages);
1facf9fc 7084+}
7085+
7086+static int au_dpages_append(struct au_dcsub_pages *dpages,
7087+ struct dentry *dentry, gfp_t gfp)
7088+{
7089+ int err, sz;
7090+ struct au_dpage *dpage;
7091+ void *p;
7092+
7093+ dpage = dpages->dpages + dpages->ndpage - 1;
7094+ sz = PAGE_SIZE / sizeof(dentry);
7095+ if (unlikely(dpage->ndentry >= sz)) {
7096+ AuLabel(new dpage);
7097+ err = -ENOMEM;
7098+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7099+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7100+ sz + sizeof(*dpages->dpages), gfp,
7101+ /*may_shrink*/0);
1facf9fc 7102+ if (unlikely(!p))
7103+ goto out;
7104+
7105+ dpages->dpages = p;
7106+ dpage = dpages->dpages + dpages->ndpage;
7107+ p = (void *)__get_free_page(gfp);
7108+ if (unlikely(!p))
7109+ goto out;
7110+
7111+ dpage->ndentry = 0;
7112+ dpage->dentries = p;
7113+ dpages->ndpage++;
7114+ }
7115+
c1595e42 7116+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7117+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7118+ return 0; /* success */
7119+
4f0767ce 7120+out:
1facf9fc 7121+ return err;
7122+}
7123+
c1595e42
JR
7124+/* todo: BAD approach */
7125+/* copied from linux/fs/dcache.c */
7126+enum d_walk_ret {
7127+ D_WALK_CONTINUE,
7128+ D_WALK_QUIT,
7129+ D_WALK_NORETRY,
7130+ D_WALK_SKIP,
7131+};
7132+
7133+extern void d_walk(struct dentry *parent, void *data,
cd7a4cd9 7134+ enum d_walk_ret (*enter)(void *, struct dentry *));
c1595e42
JR
7135+
7136+struct ac_dpages_arg {
1facf9fc 7137+ int err;
c1595e42
JR
7138+ struct au_dcsub_pages *dpages;
7139+ struct super_block *sb;
7140+ au_dpages_test test;
7141+ void *arg;
7142+};
1facf9fc 7143+
c1595e42
JR
7144+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7145+{
7146+ enum d_walk_ret ret;
7147+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7148+
c1595e42
JR
7149+ ret = D_WALK_CONTINUE;
7150+ if (dentry->d_sb == arg->sb
7151+ && !IS_ROOT(dentry)
7152+ && au_dcount(dentry) > 0
7153+ && au_di(dentry)
7154+ && (!arg->test || arg->test(dentry, arg->arg))) {
7155+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7156+ if (unlikely(arg->err))
7157+ ret = D_WALK_QUIT;
1facf9fc 7158+ }
7159+
c1595e42
JR
7160+ return ret;
7161+}
027c5e7a 7162+
c1595e42
JR
7163+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7164+ au_dpages_test test, void *arg)
7165+{
7166+ struct ac_dpages_arg args = {
7167+ .err = 0,
7168+ .dpages = dpages,
7169+ .sb = root->d_sb,
7170+ .test = test,
7171+ .arg = arg
7172+ };
027c5e7a 7173+
cd7a4cd9 7174+ d_walk(root, &args, au_call_dpages_append);
c1595e42
JR
7175+
7176+ return args.err;
1facf9fc 7177+}
7178+
7179+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7180+ int do_include, au_dpages_test test, void *arg)
7181+{
7182+ int err;
7183+
7184+ err = 0;
027c5e7a
AM
7185+ write_seqlock(&rename_lock);
7186+ spin_lock(&dentry->d_lock);
7187+ if (do_include
c1595e42 7188+ && au_dcount(dentry) > 0
027c5e7a 7189+ && (!test || test(dentry, arg)))
1facf9fc 7190+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7191+ spin_unlock(&dentry->d_lock);
7192+ if (unlikely(err))
7193+ goto out;
7194+
7195+ /*
523b37e3 7196+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7197+ * mount
7198+ */
1facf9fc 7199+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7200+ dentry = dentry->d_parent; /* rename_lock is locked */
7201+ spin_lock(&dentry->d_lock);
c1595e42 7202+ if (au_dcount(dentry) > 0
027c5e7a 7203+ && (!test || test(dentry, arg)))
1facf9fc 7204+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7205+ spin_unlock(&dentry->d_lock);
7206+ if (unlikely(err))
7207+ break;
1facf9fc 7208+ }
7209+
4f0767ce 7210+out:
027c5e7a 7211+ write_sequnlock(&rename_lock);
1facf9fc 7212+ return err;
7213+}
7214+
027c5e7a
AM
7215+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7216+{
7217+ return au_di(dentry) && dentry->d_sb == arg;
7218+}
7219+
7220+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7221+ struct dentry *dentry, int do_include)
7222+{
7223+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7224+ au_dcsub_dpages_aufs, dentry->d_sb);
7225+}
7226+
4a4d8108 7227+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7228+{
4a4d8108
AM
7229+ struct path path[2] = {
7230+ {
7231+ .dentry = d1
7232+ },
7233+ {
7234+ .dentry = d2
7235+ }
7236+ };
1facf9fc 7237+
4a4d8108 7238+ return path_is_under(path + 0, path + 1);
1facf9fc 7239+}
7f207e10 7240diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
eca34b5c
AM
7241--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
7242+++ linux/fs/aufs/dcsub.h 2019-07-11 15:42:14.462237786 +0200
062440b3
AM
7243@@ -0,0 +1,137 @@
7244+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7245+/*
ba1aed25 7246+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 7247+ *
7248+ * This program, aufs is free software; you can redistribute it and/or modify
7249+ * it under the terms of the GNU General Public License as published by
7250+ * the Free Software Foundation; either version 2 of the License, or
7251+ * (at your option) any later version.
dece6358
AM
7252+ *
7253+ * This program is distributed in the hope that it will be useful,
7254+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7255+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7256+ * GNU General Public License for more details.
7257+ *
7258+ * You should have received a copy of the GNU General Public License
523b37e3 7259+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7260+ */
7261+
7262+/*
7263+ * sub-routines for dentry cache
7264+ */
7265+
7266+#ifndef __AUFS_DCSUB_H__
7267+#define __AUFS_DCSUB_H__
7268+
7269+#ifdef __KERNEL__
7270+
7f207e10 7271+#include <linux/dcache.h>
027c5e7a 7272+#include <linux/fs.h>
dece6358 7273+
1facf9fc 7274+struct au_dpage {
7275+ int ndentry;
7276+ struct dentry **dentries;
7277+};
7278+
7279+struct au_dcsub_pages {
7280+ int ndpage;
7281+ struct au_dpage *dpages;
7282+};
7283+
7284+/* ---------------------------------------------------------------------- */
7285+
7f207e10 7286+/* dcsub.c */
1facf9fc 7287+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7288+void au_dpages_free(struct au_dcsub_pages *dpages);
7289+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7290+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7291+ au_dpages_test test, void *arg);
7292+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7293+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7294+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7295+ struct dentry *dentry, int do_include);
4a4d8108 7296+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7297+
7f207e10
AM
7298+/* ---------------------------------------------------------------------- */
7299+
523b37e3
AM
7300+/*
7301+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7302+ * include/linux/dcache.h. Try them (in the future).
7303+ */
7304+
027c5e7a
AM
7305+static inline int au_d_hashed_positive(struct dentry *d)
7306+{
7307+ int err;
5527c038 7308+ struct inode *inode = d_inode(d);
076b876e 7309+
027c5e7a 7310+ err = 0;
5527c038
JR
7311+ if (unlikely(d_unhashed(d)
7312+ || d_is_negative(d)
7313+ || !inode->i_nlink))
027c5e7a
AM
7314+ err = -ENOENT;
7315+ return err;
7316+}
7317+
38d290e6
JR
7318+static inline int au_d_linkable(struct dentry *d)
7319+{
7320+ int err;
5527c038 7321+ struct inode *inode = d_inode(d);
076b876e 7322+
38d290e6
JR
7323+ err = au_d_hashed_positive(d);
7324+ if (err
5527c038 7325+ && d_is_positive(d)
38d290e6
JR
7326+ && (inode->i_state & I_LINKABLE))
7327+ err = 0;
7328+ return err;
7329+}
7330+
027c5e7a
AM
7331+static inline int au_d_alive(struct dentry *d)
7332+{
7333+ int err;
7334+ struct inode *inode;
076b876e 7335+
027c5e7a
AM
7336+ err = 0;
7337+ if (!IS_ROOT(d))
7338+ err = au_d_hashed_positive(d);
7339+ else {
5527c038
JR
7340+ inode = d_inode(d);
7341+ if (unlikely(d_unlinked(d)
7342+ || d_is_negative(d)
7343+ || !inode->i_nlink))
027c5e7a
AM
7344+ err = -ENOENT;
7345+ }
7346+ return err;
7347+}
7348+
7349+static inline int au_alive_dir(struct dentry *d)
7f207e10 7350+{
027c5e7a 7351+ int err;
076b876e 7352+
027c5e7a 7353+ err = au_d_alive(d);
5527c038 7354+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7355+ err = -ENOENT;
7356+ return err;
7f207e10
AM
7357+}
7358+
38d290e6
JR
7359+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7360+{
7361+ return a->len == b->len
7362+ && !memcmp(a->name, b->name, a->len);
7363+}
7364+
7e9cd9fe
AM
7365+/*
7366+ * by the commit
7367+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7368+ * taking d_lock
7369+ * the type of d_lockref.count became int, but the inlined function d_count()
7370+ * still returns unsigned int.
7371+ * I don't know why. Maybe it is for every d_count() users?
7372+ * Anyway au_dcount() lives on.
7373+ */
c1595e42
JR
7374+static inline int au_dcount(struct dentry *d)
7375+{
7376+ return (int)d_count(d);
7377+}
7378+
1facf9fc 7379+#endif /* __KERNEL__ */
7380+#endif /* __AUFS_DCSUB_H__ */
7f207e10 7381diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
eca34b5c
AM
7382--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
7383+++ linux/fs/aufs/debug.c 2019-07-11 15:42:14.462237786 +0200
eca801bf 7384@@ -0,0 +1,441 @@
cd7a4cd9 7385+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7386+/*
ba1aed25 7387+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 7388+ *
7389+ * This program, aufs is free software; you can redistribute it and/or modify
7390+ * it under the terms of the GNU General Public License as published by
7391+ * the Free Software Foundation; either version 2 of the License, or
7392+ * (at your option) any later version.
dece6358
AM
7393+ *
7394+ * This program is distributed in the hope that it will be useful,
7395+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7396+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7397+ * GNU General Public License for more details.
7398+ *
7399+ * You should have received a copy of the GNU General Public License
523b37e3 7400+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7401+ */
7402+
7403+/*
7404+ * debug print functions
7405+ */
7406+
eca801bf 7407+#include <linux/iversion.h>
1facf9fc 7408+#include "aufs.h"
7409+
392086de
AM
7410+/* Returns 0, or -errno. arg is in kp->arg. */
7411+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7412+{
7413+ int err, n;
7414+
7415+ err = kstrtoint(val, 0, &n);
7416+ if (!err) {
7417+ if (n > 0)
7418+ au_debug_on();
7419+ else
7420+ au_debug_off();
7421+ }
7422+ return err;
7423+}
7424+
7425+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7426+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7427+{
7428+ atomic_t *a;
7429+
7430+ a = kp->arg;
7431+ return sprintf(buffer, "%d", atomic_read(a));
7432+}
7433+
7434+static struct kernel_param_ops param_ops_atomic_t = {
7435+ .set = param_atomic_t_set,
7436+ .get = param_atomic_t_get
7437+ /* void (*free)(void *arg) */
7438+};
7439+
7440+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7441+MODULE_PARM_DESC(debug, "debug print");
cd7a4cd9 7442+module_param_named(debug, aufs_debug, atomic_t, 0664);
1facf9fc 7443+
c1595e42 7444+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7445+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7446+#define dpri(fmt, ...) do { \
7447+ if ((au_plevel \
7448+ && strcmp(au_plevel, KERN_DEBUG)) \
7449+ || au_debug_test()) \
7450+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7451+} while (0)
7452+
7453+/* ---------------------------------------------------------------------- */
7454+
7455+void au_dpri_whlist(struct au_nhash *whlist)
7456+{
7457+ unsigned long ul, n;
7458+ struct hlist_head *head;
c06a8ce3 7459+ struct au_vdir_wh *pos;
1facf9fc 7460+
7461+ n = whlist->nh_num;
7462+ head = whlist->nh_head;
7463+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7464+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7465+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7466+ pos->wh_bindex,
7467+ pos->wh_str.len, pos->wh_str.name,
7468+ pos->wh_str.len);
1facf9fc 7469+ head++;
7470+ }
7471+}
7472+
7473+void au_dpri_vdir(struct au_vdir *vdir)
7474+{
7475+ unsigned long ul;
7476+ union au_vdir_deblk_p p;
7477+ unsigned char *o;
7478+
7479+ if (!vdir || IS_ERR(vdir)) {
7480+ dpri("err %ld\n", PTR_ERR(vdir));
7481+ return;
7482+ }
7483+
be118d29 7484+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7485+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7486+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7487+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7488+ p.deblk = vdir->vd_deblk[ul];
7489+ o = p.deblk;
7490+ dpri("[%lu]: %p\n", ul, o);
7491+ }
7492+}
7493+
53392da6 7494+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7495+ struct dentry *wh)
7496+{
7497+ char *n = NULL;
7498+ int l = 0;
7499+
7500+ if (!inode || IS_ERR(inode)) {
7501+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7502+ return -1;
7503+ }
7504+
c2b27bf2 7505+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7506+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7507+ && sizeof(inode->i_blocks) != sizeof(u64));
7508+ if (wh) {
7509+ n = (void *)wh->d_name.name;
7510+ l = wh->d_name.len;
7511+ }
7512+
53392da6
AM
7513+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7514+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7515+ bindex, inode,
1facf9fc 7516+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7517+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7518+ i_size_read(inode), (unsigned long long)inode->i_blocks,
cd7a4cd9 7519+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7520+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7521+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7522+ inode->i_generation,
1facf9fc 7523+ l ? ", wh " : "", l, n);
7524+ return 0;
7525+}
7526+
7527+void au_dpri_inode(struct inode *inode)
7528+{
7529+ struct au_iinfo *iinfo;
5afbbe0d 7530+ struct au_hinode *hi;
1facf9fc 7531+ aufs_bindex_t bindex;
53392da6 7532+ int err, hn;
1facf9fc 7533+
53392da6 7534+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7535+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7536+ return;
7537+
7538+ iinfo = au_ii(inode);
5afbbe0d
AM
7539+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7540+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7541+ if (iinfo->ii_btop < 0)
1facf9fc 7542+ return;
53392da6 7543+ hn = 0;
5afbbe0d
AM
7544+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7545+ hi = au_hinode(iinfo, bindex);
7546+ hn = !!au_hn(hi);
7547+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7548+ }
1facf9fc 7549+}
7550+
2cbb1c4b
JR
7551+void au_dpri_dalias(struct inode *inode)
7552+{
7553+ struct dentry *d;
7554+
7555+ spin_lock(&inode->i_lock);
c1595e42 7556+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7557+ au_dpri_dentry(d);
7558+ spin_unlock(&inode->i_lock);
7559+}
7560+
1facf9fc 7561+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7562+{
7563+ struct dentry *wh = NULL;
53392da6 7564+ int hn;
5afbbe0d 7565+ struct inode *inode;
076b876e 7566+ struct au_iinfo *iinfo;
5afbbe0d 7567+ struct au_hinode *hi;
1facf9fc 7568+
7569+ if (!dentry || IS_ERR(dentry)) {
7570+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7571+ return -1;
7572+ }
7573+ /* do not call dget_parent() here */
027c5e7a 7574+ /* note: access d_xxx without d_lock */
523b37e3
AM
7575+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7576+ bindex, dentry, dentry,
1facf9fc 7577+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7578+ au_dcount(dentry), dentry->d_flags,
523b37e3 7579+ d_unhashed(dentry) ? "un" : "");
53392da6 7580+ hn = -1;
5afbbe0d
AM
7581+ inode = NULL;
7582+ if (d_is_positive(dentry))
7583+ inode = d_inode(dentry);
7584+ if (inode
7585+ && au_test_aufs(dentry->d_sb)
7586+ && bindex >= 0
7587+ && !au_is_bad_inode(inode)) {
7588+ iinfo = au_ii(inode);
7589+ hi = au_hinode(iinfo, bindex);
7590+ hn = !!au_hn(hi);
7591+ wh = hi->hi_whdentry;
7592+ }
7593+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7594+ return 0;
7595+}
7596+
7597+void au_dpri_dentry(struct dentry *dentry)
7598+{
7599+ struct au_dinfo *dinfo;
7600+ aufs_bindex_t bindex;
7601+ int err;
7602+
7603+ err = do_pri_dentry(-1, dentry);
7604+ if (err || !au_test_aufs(dentry->d_sb))
7605+ return;
7606+
7607+ dinfo = au_di(dentry);
7608+ if (!dinfo)
7609+ return;
5afbbe0d
AM
7610+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7611+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7612+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7613+ dinfo->di_tmpfile);
5afbbe0d 7614+ if (dinfo->di_btop < 0)
1facf9fc 7615+ return;
5afbbe0d
AM
7616+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7617+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7618+}
7619+
7620+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7621+{
7622+ char a[32];
7623+
7624+ if (!file || IS_ERR(file)) {
7625+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7626+ return -1;
7627+ }
7628+ a[0] = 0;
7629+ if (bindex < 0
b912730e 7630+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7631+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7632+ && au_fi(file))
e49829fe 7633+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7634+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7635+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7636+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7637+ file->f_version, file->f_pos, a);
b912730e 7638+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7639+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7640+ return 0;
7641+}
7642+
7643+void au_dpri_file(struct file *file)
7644+{
7645+ struct au_finfo *finfo;
4a4d8108
AM
7646+ struct au_fidir *fidir;
7647+ struct au_hfile *hfile;
1facf9fc 7648+ aufs_bindex_t bindex;
7649+ int err;
7650+
7651+ err = do_pri_file(-1, file);
2000de60 7652+ if (err
b912730e 7653+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7654+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7655+ return;
7656+
7657+ finfo = au_fi(file);
7658+ if (!finfo)
7659+ return;
4a4d8108 7660+ if (finfo->fi_btop < 0)
1facf9fc 7661+ return;
4a4d8108
AM
7662+ fidir = finfo->fi_hdir;
7663+ if (!fidir)
7664+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7665+ else
e49829fe
JR
7666+ for (bindex = finfo->fi_btop;
7667+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7668+ bindex++) {
7669+ hfile = fidir->fd_hfile + bindex;
7670+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7671+ }
1facf9fc 7672+}
7673+
7674+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7675+{
7676+ struct vfsmount *mnt;
7677+ struct super_block *sb;
7678+
7679+ if (!br || IS_ERR(br))
7680+ goto out;
86dc4139 7681+ mnt = au_br_mnt(br);
1facf9fc 7682+ if (!mnt || IS_ERR(mnt))
7683+ goto out;
7684+ sb = mnt->mnt_sb;
7685+ if (!sb || IS_ERR(sb))
7686+ goto out;
7687+
acd2b654 7688+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
b752ccd1 7689+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7690+ "xino %d\n",
acd2b654
AM
7691+ bindex, br->br_perm, br->br_id, br->br_wbr,
7692+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7693+ sb->s_flags, sb->s_count,
acd2b654
AM
7694+ atomic_read(&sb->s_active),
7695+ !!au_xino_file(br->br_xino, /*idx*/-1));
1facf9fc 7696+ return 0;
7697+
4f0767ce 7698+out:
1facf9fc 7699+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7700+ return -1;
7701+}
7702+
7703+void au_dpri_sb(struct super_block *sb)
7704+{
7705+ struct au_sbinfo *sbinfo;
7706+ aufs_bindex_t bindex;
7707+ int err;
acd2b654 7708+ /* to reduce stack size */
1facf9fc 7709+ struct {
7710+ struct vfsmount mnt;
7711+ struct au_branch fake;
7712+ } *a;
7713+
7714+ /* this function can be called from magic sysrq */
7715+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7716+ if (unlikely(!a)) {
7717+ dpri("no memory\n");
7718+ return;
7719+ }
7720+
7721+ a->mnt.mnt_sb = sb;
86dc4139 7722+ a->fake.br_path.mnt = &a->mnt;
1facf9fc 7723+ err = do_pri_br(-1, &a->fake);
9f237c51 7724+ au_kfree_rcu(a);
1facf9fc 7725+ dpri("dev 0x%x\n", sb->s_dev);
7726+ if (err || !au_test_aufs(sb))
7727+ return;
7728+
7729+ sbinfo = au_sbi(sb);
7730+ if (!sbinfo)
7731+ return;
f0c0a007
AM
7732+ dpri("nw %d, gen %u, kobj %d\n",
7733+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7734+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7735+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7736+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7737+}
7738+
7739+/* ---------------------------------------------------------------------- */
7740+
027c5e7a
AM
7741+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7742+{
5527c038 7743+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7744+ struct dentry *h_dentry;
5afbbe0d 7745+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7746+
7747+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7748+ return;
7749+
5afbbe0d
AM
7750+ bbot = au_dbbot(dentry);
7751+ bi = au_ibbot(inode);
7752+ if (bi < bbot)
7753+ bbot = bi;
7754+ bindex = au_dbtop(dentry);
7755+ bi = au_ibtop(inode);
027c5e7a
AM
7756+ if (bi > bindex)
7757+ bindex = bi;
7758+
5afbbe0d 7759+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7760+ h_dentry = au_h_dptr(dentry, bindex);
7761+ if (!h_dentry)
7762+ continue;
7763+ h_inode = au_h_iptr(inode, bindex);
5527c038 7764+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7765+ au_debug_on();
027c5e7a
AM
7766+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7767+ AuDbgDentry(dentry);
7768+ AuDbgInode(inode);
392086de 7769+ au_debug_off();
027c5e7a
AM
7770+ BUG();
7771+ }
7772+ }
7773+}
7774+
1facf9fc 7775+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7776+{
7777+ int err, i, j;
7778+ struct au_dcsub_pages dpages;
7779+ struct au_dpage *dpage;
7780+ struct dentry **dentries;
7781+
7782+ err = au_dpages_init(&dpages, GFP_NOFS);
7783+ AuDebugOn(err);
027c5e7a 7784+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7785+ AuDebugOn(err);
7786+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7787+ dpage = dpages.dpages + i;
7788+ dentries = dpage->dentries;
7789+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7790+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7791+ }
7792+ au_dpages_free(&dpages);
7793+}
7794+
1facf9fc 7795+void au_dbg_verify_kthread(void)
7796+{
53392da6 7797+ if (au_wkq_test()) {
1facf9fc 7798+ au_dbg_blocked();
1e00d052
AM
7799+ /*
7800+ * It may be recursive, but udba=notify between two aufs mounts,
7801+ * where a single ro branch is shared, is not a problem.
7802+ */
7803+ /* WARN_ON(1); */
1facf9fc 7804+ }
7805+}
7806+
7807+/* ---------------------------------------------------------------------- */
7808+
1facf9fc 7809+int __init au_debug_init(void)
7810+{
7811+ aufs_bindex_t bindex;
7812+ struct au_vdir_destr destr;
7813+
7814+ bindex = -1;
7815+ AuDebugOn(bindex >= 0);
7816+
7817+ destr.len = -1;
7818+ AuDebugOn(destr.len < NAME_MAX);
7819+
7820+#ifdef CONFIG_4KSTACKS
0c3ec466 7821+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7822+#endif
7823+
1facf9fc 7824+ return 0;
7825+}
7f207e10 7826diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
eca34b5c
AM
7827--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
7828+++ linux/fs/aufs/debug.h 2019-07-11 15:42:14.462237786 +0200
062440b3
AM
7829@@ -0,0 +1,226 @@
7830+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7831+/*
ba1aed25 7832+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 7833+ *
7834+ * This program, aufs is free software; you can redistribute it and/or modify
7835+ * it under the terms of the GNU General Public License as published by
7836+ * the Free Software Foundation; either version 2 of the License, or
7837+ * (at your option) any later version.
dece6358
AM
7838+ *
7839+ * This program is distributed in the hope that it will be useful,
7840+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7841+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7842+ * GNU General Public License for more details.
7843+ *
7844+ * You should have received a copy of the GNU General Public License
523b37e3 7845+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7846+ */
7847+
7848+/*
7849+ * debug print functions
7850+ */
7851+
7852+#ifndef __AUFS_DEBUG_H__
7853+#define __AUFS_DEBUG_H__
7854+
7855+#ifdef __KERNEL__
7856+
392086de 7857+#include <linux/atomic.h>
4a4d8108
AM
7858+#include <linux/module.h>
7859+#include <linux/kallsyms.h>
1facf9fc 7860+#include <linux/sysrq.h>
4a4d8108 7861+
1facf9fc 7862+#ifdef CONFIG_AUFS_DEBUG
7863+#define AuDebugOn(a) BUG_ON(a)
7864+
7865+/* module parameter */
392086de
AM
7866+extern atomic_t aufs_debug;
7867+static inline void au_debug_on(void)
1facf9fc 7868+{
392086de
AM
7869+ atomic_inc(&aufs_debug);
7870+}
7871+static inline void au_debug_off(void)
7872+{
7873+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7874+}
7875+
7876+static inline int au_debug_test(void)
7877+{
392086de 7878+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7879+}
7880+#else
7881+#define AuDebugOn(a) do {} while (0)
392086de
AM
7882+AuStubVoid(au_debug_on, void)
7883+AuStubVoid(au_debug_off, void)
4a4d8108 7884+AuStubInt0(au_debug_test, void)
1facf9fc 7885+#endif /* CONFIG_AUFS_DEBUG */
7886+
392086de
AM
7887+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7888+
1facf9fc 7889+/* ---------------------------------------------------------------------- */
7890+
7891+/* debug print */
7892+
4a4d8108 7893+#define AuDbg(fmt, ...) do { \
1facf9fc 7894+ if (au_debug_test()) \
4a4d8108 7895+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7896+} while (0)
4a4d8108
AM
7897+#define AuLabel(l) AuDbg(#l "\n")
7898+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7899+#define AuWarn1(fmt, ...) do { \
1facf9fc 7900+ static unsigned char _c; \
7901+ if (!_c++) \
0c3ec466 7902+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7903+} while (0)
7904+
4a4d8108 7905+#define AuErr1(fmt, ...) do { \
1facf9fc 7906+ static unsigned char _c; \
7907+ if (!_c++) \
4a4d8108 7908+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7909+} while (0)
7910+
4a4d8108 7911+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7912+ static unsigned char _c; \
7913+ if (!_c++) \
4a4d8108 7914+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7915+} while (0)
7916+
7917+#define AuUnsupportMsg "This operation is not supported." \
7918+ " Please report this application to aufs-users ML."
4a4d8108
AM
7919+#define AuUnsupport(fmt, ...) do { \
7920+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7921+ dump_stack(); \
7922+} while (0)
7923+
7924+#define AuTraceErr(e) do { \
7925+ if (unlikely((e) < 0)) \
7926+ AuDbg("err %d\n", (int)(e)); \
7927+} while (0)
7928+
7929+#define AuTraceErrPtr(p) do { \
7930+ if (IS_ERR(p)) \
7931+ AuDbg("err %ld\n", PTR_ERR(p)); \
7932+} while (0)
7933+
7934+/* dirty macros for debug print, use with "%.*s" and caution */
7935+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 7936+
7937+/* ---------------------------------------------------------------------- */
7938+
dece6358 7939+struct dentry;
1facf9fc 7940+#ifdef CONFIG_AUFS_DEBUG
c1595e42 7941+extern struct mutex au_dbg_mtx;
1facf9fc 7942+extern char *au_plevel;
7943+struct au_nhash;
7944+void au_dpri_whlist(struct au_nhash *whlist);
7945+struct au_vdir;
7946+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 7947+struct inode;
1facf9fc 7948+void au_dpri_inode(struct inode *inode);
2cbb1c4b 7949+void au_dpri_dalias(struct inode *inode);
1facf9fc 7950+void au_dpri_dentry(struct dentry *dentry);
dece6358 7951+struct file;
1facf9fc 7952+void au_dpri_file(struct file *filp);
dece6358 7953+struct super_block;
1facf9fc 7954+void au_dpri_sb(struct super_block *sb);
7955+
027c5e7a
AM
7956+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7957+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 7958+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 7959+void au_dbg_verify_kthread(void);
7960+
7961+int __init au_debug_init(void);
7e9cd9fe 7962+
1facf9fc 7963+#define AuDbgWhlist(w) do { \
c1595e42 7964+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7965+ AuDbg(#w "\n"); \
7966+ au_dpri_whlist(w); \
c1595e42 7967+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7968+} while (0)
7969+
7970+#define AuDbgVdir(v) do { \
c1595e42 7971+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7972+ AuDbg(#v "\n"); \
7973+ au_dpri_vdir(v); \
c1595e42 7974+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7975+} while (0)
7976+
7977+#define AuDbgInode(i) do { \
c1595e42 7978+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7979+ AuDbg(#i "\n"); \
7980+ au_dpri_inode(i); \
c1595e42 7981+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7982+} while (0)
7983+
2cbb1c4b 7984+#define AuDbgDAlias(i) do { \
c1595e42 7985+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
7986+ AuDbg(#i "\n"); \
7987+ au_dpri_dalias(i); \
c1595e42 7988+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
7989+} while (0)
7990+
1facf9fc 7991+#define AuDbgDentry(d) do { \
c1595e42 7992+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7993+ AuDbg(#d "\n"); \
7994+ au_dpri_dentry(d); \
c1595e42 7995+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7996+} while (0)
7997+
7998+#define AuDbgFile(f) do { \
c1595e42 7999+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8000+ AuDbg(#f "\n"); \
8001+ au_dpri_file(f); \
c1595e42 8002+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8003+} while (0)
8004+
8005+#define AuDbgSb(sb) do { \
c1595e42 8006+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8007+ AuDbg(#sb "\n"); \
8008+ au_dpri_sb(sb); \
c1595e42 8009+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8010+} while (0)
8011+
4a4d8108
AM
8012+#define AuDbgSym(addr) do { \
8013+ char sym[KSYM_SYMBOL_LEN]; \
8014+ sprint_symbol(sym, (unsigned long)addr); \
8015+ AuDbg("%s\n", sym); \
8016+} while (0)
1facf9fc 8017+#else
027c5e7a 8018+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
8019+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8020+AuStubVoid(au_dbg_verify_kthread, void)
8021+AuStubInt0(__init au_debug_init, void)
1facf9fc 8022+
1facf9fc 8023+#define AuDbgWhlist(w) do {} while (0)
8024+#define AuDbgVdir(v) do {} while (0)
8025+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 8026+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 8027+#define AuDbgDentry(d) do {} while (0)
8028+#define AuDbgFile(f) do {} while (0)
8029+#define AuDbgSb(sb) do {} while (0)
4a4d8108 8030+#define AuDbgSym(addr) do {} while (0)
1facf9fc 8031+#endif /* CONFIG_AUFS_DEBUG */
8032+
8033+/* ---------------------------------------------------------------------- */
8034+
8035+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8036+int __init au_sysrq_init(void);
8037+void au_sysrq_fin(void);
8038+
8039+#ifdef CONFIG_HW_CONSOLE
8040+#define au_dbg_blocked() do { \
8041+ WARN_ON(1); \
0c5527e5 8042+ handle_sysrq('w'); \
1facf9fc 8043+} while (0)
8044+#else
4a4d8108 8045+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8046+#endif
8047+
8048+#else
4a4d8108
AM
8049+AuStubInt0(__init au_sysrq_init, void)
8050+AuStubVoid(au_sysrq_fin, void)
8051+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8052+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8053+
8054+#endif /* __KERNEL__ */
8055+#endif /* __AUFS_DEBUG_H__ */
7f207e10 8056diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
eca34b5c
AM
8057--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
8058+++ linux/fs/aufs/dentry.c 2019-07-11 15:42:14.462237786 +0200
eca801bf 8059@@ -0,0 +1,1154 @@
cd7a4cd9 8060+// SPDX-License-Identifier: GPL-2.0
1facf9fc 8061+/*
ba1aed25 8062+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 8063+ *
8064+ * This program, aufs is free software; you can redistribute it and/or modify
8065+ * it under the terms of the GNU General Public License as published by
8066+ * the Free Software Foundation; either version 2 of the License, or
8067+ * (at your option) any later version.
dece6358
AM
8068+ *
8069+ * This program is distributed in the hope that it will be useful,
8070+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8071+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8072+ * GNU General Public License for more details.
8073+ *
8074+ * You should have received a copy of the GNU General Public License
523b37e3 8075+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8076+ */
8077+
8078+/*
8079+ * lookup and dentry operations
8080+ */
8081+
eca801bf 8082+#include <linux/iversion.h>
dece6358 8083+#include <linux/namei.h>
1facf9fc 8084+#include "aufs.h"
8085+
1facf9fc 8086+/*
8087+ * returns positive/negative dentry, NULL or an error.
8088+ * NULL means whiteout-ed or not-found.
8089+ */
8090+static struct dentry*
8091+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8092+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8093+{
8094+ struct dentry *h_dentry;
2000de60 8095+ struct inode *h_inode;
1facf9fc 8096+ struct au_branch *br;
8097+ int wh_found, opq;
8098+ unsigned char wh_able;
8099+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8100+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8101+ IGNORE_PERM);
1facf9fc 8102+
1facf9fc 8103+ wh_found = 0;
8104+ br = au_sbr(dentry->d_sb, bindex);
8105+ wh_able = !!au_br_whable(br->br_perm);
8106+ if (wh_able)
8b6a4947 8107+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
1facf9fc 8108+ h_dentry = ERR_PTR(wh_found);
8109+ if (!wh_found)
8110+ goto real_lookup;
8111+ if (unlikely(wh_found < 0))
8112+ goto out;
8113+
8114+ /* We found a whiteout */
5afbbe0d 8115+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8116+ au_set_dbwh(dentry, bindex);
8117+ if (!allow_neg)
8118+ return NULL; /* success */
8119+
4f0767ce 8120+real_lookup:
076b876e 8121+ if (!ignore_perm)
8b6a4947 8122+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8123+ else
8b6a4947 8124+ h_dentry = au_sio_lkup_one(args->name, h_parent);
2000de60
JR
8125+ if (IS_ERR(h_dentry)) {
8126+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8127+ && !allow_neg)
8128+ h_dentry = NULL;
1facf9fc 8129+ goto out;
2000de60 8130+ }
1facf9fc 8131+
5527c038
JR
8132+ h_inode = d_inode(h_dentry);
8133+ if (d_is_negative(h_dentry)) {
1facf9fc 8134+ if (!allow_neg)
8135+ goto out_neg;
8136+ } else if (wh_found
8137+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8138+ goto out_neg;
8b6a4947
AM
8139+ else if (au_ftest_lkup(args->flags, DIRREN)
8140+ /* && h_inode */
8141+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8142+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8143+ (unsigned long long)h_inode->i_ino);
8144+ goto out_neg;
8145+ }
1facf9fc 8146+
5afbbe0d
AM
8147+ if (au_dbbot(dentry) <= bindex)
8148+ au_set_dbbot(dentry, bindex);
8149+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8150+ au_set_dbtop(dentry, bindex);
1facf9fc 8151+ au_set_h_dptr(dentry, bindex, h_dentry);
8152+
2000de60
JR
8153+ if (!d_is_dir(h_dentry)
8154+ || !wh_able
5527c038 8155+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8156+ goto out; /* success */
8157+
be118d29 8158+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
076b876e 8159+ opq = au_diropq_test(h_dentry);
3c1bdaff 8160+ inode_unlock_shared(h_inode);
1facf9fc 8161+ if (opq > 0)
8162+ au_set_dbdiropq(dentry, bindex);
8163+ else if (unlikely(opq < 0)) {
8164+ au_set_h_dptr(dentry, bindex, NULL);
8165+ h_dentry = ERR_PTR(opq);
8166+ }
8167+ goto out;
8168+
4f0767ce 8169+out_neg:
1facf9fc 8170+ dput(h_dentry);
8171+ h_dentry = NULL;
4f0767ce 8172+out:
1facf9fc 8173+ return h_dentry;
8174+}
8175+
dece6358
AM
8176+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8177+{
8178+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8179+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8180+ return -EPERM;
8181+ return 0;
8182+}
8183+
1facf9fc 8184+/*
8185+ * returns the number of lower positive dentries,
8186+ * otherwise an error.
8187+ * can be called at unlinking with @type is zero.
8188+ */
5afbbe0d
AM
8189+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8190+ unsigned int flags)
1facf9fc 8191+{
8192+ int npositive, err;
8193+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8194+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8195+ struct au_do_lookup_args args = {
8b6a4947
AM
8196+ .flags = flags,
8197+ .name = &dentry->d_name
1facf9fc 8198+ };
1facf9fc 8199+ struct dentry *parent;
076b876e 8200+ struct super_block *sb;
1facf9fc 8201+
076b876e 8202+ sb = dentry->d_sb;
8b6a4947 8203+ err = au_test_shwh(sb, args.name);
dece6358 8204+ if (unlikely(err))
1facf9fc 8205+ goto out;
8206+
8b6a4947 8207+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8208+ if (unlikely(err))
8209+ goto out;
8210+
2000de60 8211+ isdir = !!d_is_dir(dentry);
076b876e 8212+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8213+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8214+ if (dirren)
8215+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8216+
8217+ npositive = 0;
4a4d8108 8218+ parent = dget_parent(dentry);
1facf9fc 8219+ btail = au_dbtaildir(parent);
5afbbe0d 8220+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8221+ struct dentry *h_parent, *h_dentry;
8222+ struct inode *h_inode, *h_dir;
8b6a4947 8223+ struct au_branch *br;
1facf9fc 8224+
8225+ h_dentry = au_h_dptr(dentry, bindex);
8226+ if (h_dentry) {
5527c038 8227+ if (d_is_positive(h_dentry))
1facf9fc 8228+ npositive++;
5afbbe0d 8229+ break;
1facf9fc 8230+ }
8231+ h_parent = au_h_dptr(parent, bindex);
2000de60 8232+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8233+ continue;
8234+
8b6a4947
AM
8235+ if (dirren) {
8236+ /* if the inum matches, then use the prepared name */
8237+ err = au_dr_lkup_name(&args, bindex);
8238+ if (unlikely(err))
8239+ goto out_parent;
8240+ }
8241+
5527c038 8242+ h_dir = d_inode(h_parent);
be118d29 8243+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8244+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8245+ inode_unlock_shared(h_dir);
1facf9fc 8246+ err = PTR_ERR(h_dentry);
8247+ if (IS_ERR(h_dentry))
4a4d8108 8248+ goto out_parent;
2000de60
JR
8249+ if (h_dentry)
8250+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8251+ if (dirperm1)
8252+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8253+
79b8bda9 8254+ if (au_dbwh(dentry) == bindex)
1facf9fc 8255+ break;
8256+ if (!h_dentry)
8257+ continue;
5527c038 8258+ if (d_is_negative(h_dentry))
1facf9fc 8259+ continue;
5527c038 8260+ h_inode = d_inode(h_dentry);
1facf9fc 8261+ npositive++;
8262+ if (!args.type)
8263+ args.type = h_inode->i_mode & S_IFMT;
8264+ if (args.type != S_IFDIR)
8265+ break;
8266+ else if (isdir) {
8267+ /* the type of lower may be different */
8268+ bdiropq = au_dbdiropq(dentry);
8269+ if (bdiropq >= 0 && bdiropq <= bindex)
8270+ break;
8271+ }
8b6a4947
AM
8272+ br = au_sbr(sb, bindex);
8273+ if (dirren
8274+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8275+ /*add_ent*/NULL)) {
8276+ /* prepare next name to lookup */
8277+ err = au_dr_lkup(&args, dentry, bindex);
8278+ if (unlikely(err))
8279+ goto out_parent;
8280+ }
1facf9fc 8281+ }
8282+
8283+ if (npositive) {
8284+ AuLabel(positive);
5afbbe0d 8285+ au_update_dbtop(dentry);
1facf9fc 8286+ }
8287+ err = npositive;
076b876e 8288+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8289+ && au_dbtop(dentry) < 0)) {
1facf9fc 8290+ err = -EIO;
523b37e3
AM
8291+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8292+ dentry, err);
027c5e7a 8293+ }
1facf9fc 8294+
4f0767ce 8295+out_parent:
4a4d8108 8296+ dput(parent);
9f237c51 8297+ au_kfree_try_rcu(args.whname.name);
8b6a4947
AM
8298+ if (dirren)
8299+ au_dr_lkup_fin(&args);
4f0767ce 8300+out:
1facf9fc 8301+ return err;
8302+}
8303+
076b876e 8304+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 8305+{
8306+ struct dentry *dentry;
8307+ int wkq_err;
8308+
5527c038 8309+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 8310+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8311+ else {
b4510431
AM
8312+ struct vfsub_lkup_one_args args = {
8313+ .errp = &dentry,
8314+ .name = name,
8315+ .parent = parent
1facf9fc 8316+ };
8317+
b4510431 8318+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8319+ if (unlikely(wkq_err))
8320+ dentry = ERR_PTR(wkq_err);
8321+ }
8322+
8323+ return dentry;
8324+}
8325+
8326+/*
8327+ * lookup @dentry on @bindex which should be negative.
8328+ */
86dc4139 8329+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8330+{
8331+ int err;
8332+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8333+ struct au_branch *br;
1facf9fc 8334+
1facf9fc 8335+ parent = dget_parent(dentry);
8336+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
8337+ br = au_sbr(dentry->d_sb, bindex);
8338+ if (wh)
8339+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8340+ else
076b876e 8341+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 8342+ err = PTR_ERR(h_dentry);
8343+ if (IS_ERR(h_dentry))
8344+ goto out;
5527c038 8345+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8346+ err = -EIO;
523b37e3 8347+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8348+ dput(h_dentry);
8349+ goto out;
8350+ }
8351+
4a4d8108 8352+ err = 0;
5afbbe0d
AM
8353+ if (bindex < au_dbtop(dentry))
8354+ au_set_dbtop(dentry, bindex);
8355+ if (au_dbbot(dentry) < bindex)
8356+ au_set_dbbot(dentry, bindex);
1facf9fc 8357+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8358+
4f0767ce 8359+out:
1facf9fc 8360+ dput(parent);
8361+ return err;
8362+}
8363+
8364+/* ---------------------------------------------------------------------- */
8365+
8366+/* subset of struct inode */
8367+struct au_iattr {
8368+ unsigned long i_ino;
8369+ /* unsigned int i_nlink; */
0c3ec466
AM
8370+ kuid_t i_uid;
8371+ kgid_t i_gid;
1facf9fc 8372+ u64 i_version;
8373+/*
8374+ loff_t i_size;
8375+ blkcnt_t i_blocks;
8376+*/
8377+ umode_t i_mode;
8378+};
8379+
8380+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8381+{
8382+ ia->i_ino = h_inode->i_ino;
8383+ /* ia->i_nlink = h_inode->i_nlink; */
8384+ ia->i_uid = h_inode->i_uid;
8385+ ia->i_gid = h_inode->i_gid;
be118d29 8386+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8387+/*
8388+ ia->i_size = h_inode->i_size;
8389+ ia->i_blocks = h_inode->i_blocks;
8390+*/
8391+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8392+}
8393+
8394+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8395+{
8396+ return ia->i_ino != h_inode->i_ino
8397+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8398+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8399+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8400+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8401+/*
8402+ || ia->i_size != h_inode->i_size
8403+ || ia->i_blocks != h_inode->i_blocks
8404+*/
8405+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8406+}
8407+
8408+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8409+ struct au_branch *br)
8410+{
8411+ int err;
8412+ struct au_iattr ia;
8413+ struct inode *h_inode;
8414+ struct dentry *h_d;
8415+ struct super_block *h_sb;
8416+
8417+ err = 0;
8418+ memset(&ia, -1, sizeof(ia));
8419+ h_sb = h_dentry->d_sb;
5527c038
JR
8420+ h_inode = NULL;
8421+ if (d_is_positive(h_dentry)) {
8422+ h_inode = d_inode(h_dentry);
1facf9fc 8423+ au_iattr_save(&ia, h_inode);
5527c038 8424+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8425+ /* nfs d_revalidate may return 0 for negative dentry */
8426+ /* fuse d_revalidate always return 0 for negative dentry */
8427+ goto out;
8428+
8429+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8430+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8431+ err = PTR_ERR(h_d);
8432+ if (IS_ERR(h_d))
8433+ goto out;
8434+
8435+ err = 0;
8436+ if (unlikely(h_d != h_dentry
5527c038 8437+ || d_inode(h_d) != h_inode
1facf9fc 8438+ || (h_inode && au_iattr_test(&ia, h_inode))))
8439+ err = au_busy_or_stale();
8440+ dput(h_d);
8441+
4f0767ce 8442+out:
1facf9fc 8443+ AuTraceErr(err);
8444+ return err;
8445+}
8446+
8447+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8448+ struct dentry *h_parent, struct au_branch *br)
8449+{
8450+ int err;
8451+
8452+ err = 0;
027c5e7a
AM
8453+ if (udba == AuOpt_UDBA_REVAL
8454+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8455+ IMustLock(h_dir);
5527c038 8456+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8457+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8458+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8459+
8460+ return err;
8461+}
8462+
8463+/* ---------------------------------------------------------------------- */
8464+
027c5e7a 8465+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8466+{
027c5e7a 8467+ int err;
5afbbe0d 8468+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8469+ struct au_hdentry tmp, *p, *q;
8470+ struct au_dinfo *dinfo;
8471+ struct super_block *sb;
1facf9fc 8472+
027c5e7a 8473+ DiMustWriteLock(dentry);
1308ab2a 8474+
027c5e7a
AM
8475+ sb = dentry->d_sb;
8476+ dinfo = au_di(dentry);
5afbbe0d 8477+ bbot = dinfo->di_bbot;
1facf9fc 8478+ bwh = dinfo->di_bwh;
8479+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8480+ bindex = dinfo->di_btop;
8481+ p = au_hdentry(dinfo, bindex);
8482+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8483+ if (!p->hd_dentry)
1facf9fc 8484+ continue;
8485+
027c5e7a
AM
8486+ new_bindex = au_br_index(sb, p->hd_id);
8487+ if (new_bindex == bindex)
1facf9fc 8488+ continue;
1facf9fc 8489+
1facf9fc 8490+ if (dinfo->di_bwh == bindex)
8491+ bwh = new_bindex;
8492+ if (dinfo->di_bdiropq == bindex)
8493+ bdiropq = new_bindex;
8494+ if (new_bindex < 0) {
8495+ au_hdput(p);
8496+ p->hd_dentry = NULL;
8497+ continue;
8498+ }
8499+
8500+ /* swap two lower dentries, and loop again */
5afbbe0d 8501+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8502+ tmp = *q;
8503+ *q = *p;
8504+ *p = tmp;
8505+ if (tmp.hd_dentry) {
8506+ bindex--;
8507+ p--;
8508+ }
8509+ }
8510+
1facf9fc 8511+ dinfo->di_bwh = -1;
5afbbe0d 8512+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8513+ dinfo->di_bwh = bwh;
8514+
8515+ dinfo->di_bdiropq = -1;
8516+ if (bdiropq >= 0
5afbbe0d 8517+ && bdiropq <= au_sbbot(sb)
1facf9fc 8518+ && au_sbr_whable(sb, bdiropq))
8519+ dinfo->di_bdiropq = bdiropq;
8520+
027c5e7a 8521+ err = -EIO;
5afbbe0d
AM
8522+ dinfo->di_btop = -1;
8523+ dinfo->di_bbot = -1;
8524+ bbot = au_dbbot(parent);
8525+ bindex = 0;
8526+ p = au_hdentry(dinfo, bindex);
8527+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8528+ if (p->hd_dentry) {
5afbbe0d 8529+ dinfo->di_btop = bindex;
1facf9fc 8530+ break;
8531+ }
8532+
5afbbe0d
AM
8533+ if (dinfo->di_btop >= 0) {
8534+ bindex = bbot;
8535+ p = au_hdentry(dinfo, bindex);
8536+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8537+ if (p->hd_dentry) {
5afbbe0d 8538+ dinfo->di_bbot = bindex;
027c5e7a
AM
8539+ err = 0;
8540+ break;
8541+ }
8542+ }
8543+
8544+ return err;
1facf9fc 8545+}
8546+
027c5e7a 8547+static void au_do_hide(struct dentry *dentry)
1facf9fc 8548+{
027c5e7a 8549+ struct inode *inode;
1facf9fc 8550+
5527c038
JR
8551+ if (d_really_is_positive(dentry)) {
8552+ inode = d_inode(dentry);
8553+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8554+ if (inode->i_nlink && !d_unhashed(dentry))
8555+ drop_nlink(inode);
8556+ } else {
8557+ clear_nlink(inode);
8558+ /* stop next lookup */
8559+ inode->i_flags |= S_DEAD;
8560+ }
8561+ smp_mb(); /* necessary? */
8562+ }
8563+ d_drop(dentry);
8564+}
1308ab2a 8565+
027c5e7a
AM
8566+static int au_hide_children(struct dentry *parent)
8567+{
8568+ int err, i, j, ndentry;
8569+ struct au_dcsub_pages dpages;
8570+ struct au_dpage *dpage;
8571+ struct dentry *dentry;
1facf9fc 8572+
027c5e7a 8573+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8574+ if (unlikely(err))
8575+ goto out;
027c5e7a
AM
8576+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8577+ if (unlikely(err))
8578+ goto out_dpages;
1facf9fc 8579+
027c5e7a
AM
8580+ /* in reverse order */
8581+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8582+ dpage = dpages.dpages + i;
8583+ ndentry = dpage->ndentry;
8584+ for (j = ndentry - 1; j >= 0; j--) {
8585+ dentry = dpage->dentries[j];
8586+ if (dentry != parent)
8587+ au_do_hide(dentry);
8588+ }
8589+ }
1facf9fc 8590+
027c5e7a
AM
8591+out_dpages:
8592+ au_dpages_free(&dpages);
4f0767ce 8593+out:
027c5e7a 8594+ return err;
1facf9fc 8595+}
8596+
027c5e7a 8597+static void au_hide(struct dentry *dentry)
1facf9fc 8598+{
027c5e7a 8599+ int err;
1facf9fc 8600+
027c5e7a 8601+ AuDbgDentry(dentry);
2000de60 8602+ if (d_is_dir(dentry)) {
027c5e7a
AM
8603+ /* shrink_dcache_parent(dentry); */
8604+ err = au_hide_children(dentry);
8605+ if (unlikely(err))
523b37e3
AM
8606+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8607+ dentry, err);
027c5e7a
AM
8608+ }
8609+ au_do_hide(dentry);
8610+}
1facf9fc 8611+
027c5e7a
AM
8612+/*
8613+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8614+ *
8615+ * a dirty branch is added
8616+ * - on the top of layers
8617+ * - in the middle of layers
8618+ * - to the bottom of layers
8619+ *
8620+ * on the added branch there exists
8621+ * - a whiteout
8622+ * - a diropq
8623+ * - a same named entry
8624+ * + exist
8625+ * * negative --> positive
8626+ * * positive --> positive
8627+ * - type is unchanged
8628+ * - type is changed
8629+ * + doesn't exist
8630+ * * negative --> negative
8631+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8632+ * - none
8633+ */
8634+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8635+ struct au_dinfo *tmp)
8636+{
8637+ int err;
5afbbe0d 8638+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8639+ struct {
8640+ struct dentry *dentry;
8641+ struct inode *inode;
8642+ mode_t mode;
be52b249
AM
8643+ } orig_h, tmp_h = {
8644+ .dentry = NULL
8645+ };
027c5e7a
AM
8646+ struct au_hdentry *hd;
8647+ struct inode *inode, *h_inode;
8648+ struct dentry *h_dentry;
8649+
8650+ err = 0;
5afbbe0d 8651+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8652+ orig_h.mode = 0;
5afbbe0d 8653+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8654+ orig_h.inode = NULL;
8655+ if (d_is_positive(orig_h.dentry)) {
8656+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8657+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8658+ }
5afbbe0d
AM
8659+ if (tmp->di_btop >= 0) {
8660+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8661+ if (d_is_positive(tmp_h.dentry)) {
8662+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8663+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8664+ }
027c5e7a
AM
8665+ }
8666+
5527c038
JR
8667+ inode = NULL;
8668+ if (d_really_is_positive(dentry))
8669+ inode = d_inode(dentry);
027c5e7a 8670+ if (!orig_h.inode) {
acd2b654 8671+ AuDbg("negative originally\n");
027c5e7a
AM
8672+ if (inode) {
8673+ au_hide(dentry);
8674+ goto out;
8675+ }
8676+ AuDebugOn(inode);
5afbbe0d 8677+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8678+ AuDebugOn(dinfo->di_bdiropq != -1);
8679+
8680+ if (!tmp_h.inode) {
8681+ AuDbg("negative --> negative\n");
8682+ /* should have only one negative lower */
5afbbe0d
AM
8683+ if (tmp->di_btop >= 0
8684+ && tmp->di_btop < dinfo->di_btop) {
8685+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8686+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8687+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8688+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8689+ hd = au_hdentry(tmp, tmp->di_btop);
8690+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8691+ dget(hd->hd_dentry));
8692+ }
8693+ au_dbg_verify_dinode(dentry);
8694+ } else {
8695+ AuDbg("negative --> positive\n");
8696+ /*
8697+ * similar to the behaviour of creating with bypassing
8698+ * aufs.
8699+ * unhash it in order to force an error in the
8700+ * succeeding create operation.
8701+ * we should not set S_DEAD here.
8702+ */
8703+ d_drop(dentry);
8704+ /* au_di_swap(tmp, dinfo); */
8705+ au_dbg_verify_dinode(dentry);
8706+ }
8707+ } else {
8708+ AuDbg("positive originally\n");
8709+ /* inode may be NULL */
8710+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8711+ if (!tmp_h.inode) {
8712+ AuDbg("positive --> negative\n");
8713+ /* or bypassing aufs */
8714+ au_hide(dentry);
5afbbe0d 8715+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8716+ dinfo->di_bwh = tmp->di_bwh;
8717+ if (inode)
8718+ err = au_refresh_hinode_self(inode);
8719+ au_dbg_verify_dinode(dentry);
8720+ } else if (orig_h.mode == tmp_h.mode) {
8721+ AuDbg("positive --> positive, same type\n");
8722+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8723+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8724+ /*
8725+ * similar to the behaviour of removing and
8726+ * creating.
8727+ */
8728+ au_hide(dentry);
8729+ if (inode)
8730+ err = au_refresh_hinode_self(inode);
8731+ au_dbg_verify_dinode(dentry);
8732+ } else {
8733+ /* fill empty slots */
5afbbe0d
AM
8734+ if (dinfo->di_btop > tmp->di_btop)
8735+ dinfo->di_btop = tmp->di_btop;
8736+ if (dinfo->di_bbot < tmp->di_bbot)
8737+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8738+ dinfo->di_bwh = tmp->di_bwh;
8739+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8740+ bbot = dinfo->di_bbot;
8741+ bindex = tmp->di_btop;
8742+ hd = au_hdentry(tmp, bindex);
8743+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8744+ if (au_h_dptr(dentry, bindex))
8745+ continue;
5afbbe0d 8746+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8747+ if (!h_dentry)
8748+ continue;
5527c038
JR
8749+ AuDebugOn(d_is_negative(h_dentry));
8750+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8751+ AuDebugOn(orig_h.mode
8752+ != (h_inode->i_mode
8753+ & S_IFMT));
8754+ au_set_h_dptr(dentry, bindex,
8755+ dget(h_dentry));
8756+ }
5afbbe0d
AM
8757+ if (inode)
8758+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8759+ au_dbg_verify_dinode(dentry);
8760+ }
8761+ } else {
8762+ AuDbg("positive --> positive, different type\n");
8763+ /* similar to the behaviour of removing and creating */
8764+ au_hide(dentry);
8765+ if (inode)
8766+ err = au_refresh_hinode_self(inode);
8767+ au_dbg_verify_dinode(dentry);
8768+ }
8769+ }
8770+
8771+out:
8772+ return err;
8773+}
8774+
79b8bda9
AM
8775+void au_refresh_dop(struct dentry *dentry, int force_reval)
8776+{
8777+ const struct dentry_operations *dop
8778+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8779+ static const unsigned int mask
8780+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8781+
8782+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8783+
8784+ if (dentry->d_op == dop)
8785+ return;
8786+
8787+ AuDbg("%pd\n", dentry);
8788+ spin_lock(&dentry->d_lock);
8789+ if (dop == &aufs_dop)
8790+ dentry->d_flags |= mask;
8791+ else
8792+ dentry->d_flags &= ~mask;
8793+ dentry->d_op = dop;
8794+ spin_unlock(&dentry->d_lock);
8795+}
8796+
027c5e7a
AM
8797+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8798+{
e2f27e51 8799+ int err, ebrange, nbr;
027c5e7a
AM
8800+ unsigned int sigen;
8801+ struct au_dinfo *dinfo, *tmp;
8802+ struct super_block *sb;
8803+ struct inode *inode;
8804+
8805+ DiMustWriteLock(dentry);
8806+ AuDebugOn(IS_ROOT(dentry));
5527c038 8807+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8808+
8809+ sb = dentry->d_sb;
027c5e7a
AM
8810+ sigen = au_sigen(sb);
8811+ err = au_digen_test(parent, sigen);
8812+ if (unlikely(err))
8813+ goto out;
8814+
e2f27e51 8815+ nbr = au_sbbot(sb) + 1;
027c5e7a 8816+ dinfo = au_di(dentry);
e2f27e51 8817+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8818+ if (unlikely(err))
8819+ goto out;
8820+ ebrange = au_dbrange_test(dentry);
8821+ if (!ebrange)
8822+ ebrange = au_do_refresh_hdentry(dentry, parent);
8823+
38d290e6 8824+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8825+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8826+ if (d_really_is_positive(dentry)) {
8827+ inode = d_inode(dentry);
027c5e7a 8828+ err = au_refresh_hinode_self(inode);
5527c038 8829+ }
027c5e7a
AM
8830+ au_dbg_verify_dinode(dentry);
8831+ if (!err)
8832+ goto out_dgen; /* success */
8833+ goto out;
8834+ }
8835+
8836+ /* temporary dinfo */
8837+ AuDbgDentry(dentry);
8838+ err = -ENOMEM;
8839+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8840+ if (unlikely(!tmp))
8841+ goto out;
8842+ au_di_swap(tmp, dinfo);
8843+ /* returns the number of positive dentries */
8844+ /*
8845+ * if current working dir is removed, it returns an error.
8846+ * but the dentry is legal.
8847+ */
5afbbe0d 8848+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8849+ AuDbgDentry(dentry);
8850+ au_di_swap(tmp, dinfo);
8851+ if (err == -ENOENT)
8852+ err = 0;
8853+ if (err >= 0) {
8854+ /* compare/refresh by dinfo */
8855+ AuDbgDentry(dentry);
8856+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8857+ au_dbg_verify_dinode(dentry);
8858+ AuTraceErr(err);
8859+ }
e2f27e51 8860+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8861+ au_rw_write_unlock(&tmp->di_rwsem);
8862+ au_di_free(tmp);
8863+ if (unlikely(err))
8864+ goto out;
8865+
8866+out_dgen:
8867+ au_update_digen(dentry);
8868+out:
8869+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8870+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8871+ AuDbgDentry(dentry);
8872+ }
8873+ AuTraceErr(err);
8874+ return err;
8875+}
8876+
b4510431
AM
8877+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8878+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8879+{
8880+ int err, valid;
027c5e7a
AM
8881+
8882+ err = 0;
8883+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8884+ goto out;
027c5e7a
AM
8885+
8886+ AuDbg("b%d\n", bindex);
b4510431
AM
8887+ /*
8888+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8889+ * due to whiteout and branch permission.
8890+ */
8891+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8892+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8893+ /* it may return tri-state */
8894+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8895+
8896+ if (unlikely(valid < 0))
8897+ err = valid;
8898+ else if (!valid)
8899+ err = -EINVAL;
8900+
4f0767ce 8901+out:
1facf9fc 8902+ AuTraceErr(err);
8903+ return err;
8904+}
8905+
8906+/* todo: remove this */
8907+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8908+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8909+{
8910+ int err;
8911+ umode_t mode, h_mode;
5afbbe0d 8912+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8913+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8914+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8915+ struct dentry *h_dentry;
8916+ struct qstr *name, *h_name;
8917+
8918+ err = 0;
8919+ plus = 0;
8920+ mode = 0;
1facf9fc 8921+ ibs = -1;
8922+ ibe = -1;
8923+ unhashed = !!d_unhashed(dentry);
8924+ is_root = !!IS_ROOT(dentry);
8925+ name = &dentry->d_name;
38d290e6 8926+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8927+
8928+ /*
7f207e10
AM
8929+ * Theoretically, REVAL test should be unnecessary in case of
8930+ * {FS,I}NOTIFY.
8931+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8932+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8933+ * Let's do REVAL test too.
8934+ */
8935+ if (do_udba && inode) {
8936+ mode = (inode->i_mode & S_IFMT);
8937+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
8938+ ibs = au_ibtop(inode);
8939+ ibe = au_ibbot(inode);
1facf9fc 8940+ }
8941+
5afbbe0d
AM
8942+ btop = au_dbtop(dentry);
8943+ btail = btop;
1facf9fc 8944+ if (inode && S_ISDIR(inode->i_mode))
8945+ btail = au_dbtaildir(dentry);
5afbbe0d 8946+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8947+ h_dentry = au_h_dptr(dentry, bindex);
8948+ if (!h_dentry)
8949+ continue;
8950+
523b37e3
AM
8951+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8952+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 8953+ spin_lock(&h_dentry->d_lock);
1facf9fc 8954+ h_name = &h_dentry->d_name;
8955+ if (unlikely(do_udba
8956+ && !is_root
523b37e3
AM
8957+ && ((!h_nfs
8958+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 8959+ || (!tmpfile && !dirren
38d290e6
JR
8960+ && !au_qstreq(name, h_name))
8961+ ))
523b37e3
AM
8962+ || (h_nfs
8963+ && !(flags & LOOKUP_OPEN)
8964+ && (h_dentry->d_flags
8965+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 8966+ )) {
38d290e6
JR
8967+ int h_unhashed;
8968+
8969+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 8970+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
8971+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8972+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 8973+ goto err;
8974+ }
027c5e7a 8975+ spin_unlock(&h_dentry->d_lock);
1facf9fc 8976+
b4510431 8977+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 8978+ if (unlikely(err))
8979+ /* do not goto err, to keep the errno */
8980+ break;
8981+
8982+ /* todo: plink too? */
8983+ if (!do_udba)
8984+ continue;
8985+
8986+ /* UDBA tests */
5527c038 8987+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 8988+ goto err;
8989+
5527c038
JR
8990+ h_inode = NULL;
8991+ if (d_is_positive(h_dentry))
8992+ h_inode = d_inode(h_dentry);
1facf9fc 8993+ h_plus = plus;
8994+ h_mode = mode;
8995+ h_cached_inode = h_inode;
8996+ if (h_inode) {
8997+ h_mode = (h_inode->i_mode & S_IFMT);
8998+ h_plus = (h_inode->i_nlink > 0);
8999+ }
9000+ if (inode && ibs <= bindex && bindex <= ibe)
9001+ h_cached_inode = au_h_iptr(inode, bindex);
9002+
523b37e3 9003+ if (!h_nfs) {
38d290e6 9004+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
9005+ goto err;
9006+ } else {
9007+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9008+ && !is_root
9009+ && !IS_ROOT(h_dentry)
9010+ && unhashed != d_unhashed(h_dentry)))
9011+ goto err;
9012+ }
9013+ if (unlikely(mode != h_mode
1facf9fc 9014+ || h_cached_inode != h_inode))
9015+ goto err;
9016+ continue;
9017+
f6b6e03d 9018+err:
1facf9fc 9019+ err = -EINVAL;
9020+ break;
9021+ }
9022+
523b37e3 9023+ AuTraceErr(err);
1facf9fc 9024+ return err;
9025+}
9026+
027c5e7a 9027+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 9028+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9029+{
9030+ int err;
9031+ struct dentry *parent;
1facf9fc 9032+
027c5e7a 9033+ if (!au_digen_test(dentry, sigen))
1facf9fc 9034+ return 0;
9035+
9036+ parent = dget_parent(dentry);
9037+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9038+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 9039+ au_dbg_verify_gen(parent, sigen);
027c5e7a 9040+ err = au_refresh_dentry(dentry, parent);
1facf9fc 9041+ di_read_unlock(parent, AuLock_IR);
9042+ dput(parent);
027c5e7a 9043+ AuTraceErr(err);
1facf9fc 9044+ return err;
9045+}
9046+
9047+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9048+{
9049+ int err;
9050+ struct dentry *d, *parent;
1facf9fc 9051+
027c5e7a 9052+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 9053+ return simple_reval_dpath(dentry, sigen);
9054+
9055+ /* slow loop, keep it simple and stupid */
9056+ /* cf: au_cpup_dirs() */
9057+ err = 0;
9058+ parent = NULL;
027c5e7a 9059+ while (au_digen_test(dentry, sigen)) {
1facf9fc 9060+ d = dentry;
9061+ while (1) {
9062+ dput(parent);
9063+ parent = dget_parent(d);
027c5e7a 9064+ if (!au_digen_test(parent, sigen))
1facf9fc 9065+ break;
9066+ d = parent;
9067+ }
9068+
1facf9fc 9069+ if (d != dentry)
027c5e7a 9070+ di_write_lock_child2(d);
1facf9fc 9071+
9072+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9073+ if (au_digen_test(d, sigen)) {
9074+ /*
9075+ * todo: consolidate with simple_reval_dpath(),
9076+ * do_refresh() and au_reval_for_attr().
9077+ */
1facf9fc 9078+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9079+ err = au_refresh_dentry(d, parent);
1facf9fc 9080+ di_read_unlock(parent, AuLock_IR);
9081+ }
9082+
9083+ if (d != dentry)
9084+ di_write_unlock(d);
9085+ dput(parent);
9086+ if (unlikely(err))
9087+ break;
9088+ }
9089+
9090+ return err;
9091+}
9092+
9093+/*
9094+ * if valid returns 1, otherwise 0.
9095+ */
b4510431 9096+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9097+{
9098+ int valid, err;
9099+ unsigned int sigen;
8b6a4947 9100+ unsigned char do_udba, dirren;
1facf9fc 9101+ struct super_block *sb;
9102+ struct inode *inode;
9103+
027c5e7a 9104+ /* todo: support rcu-walk? */
b4510431 9105+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9106+ return -ECHILD;
9107+
9108+ valid = 0;
9109+ if (unlikely(!au_di(dentry)))
9110+ goto out;
9111+
e49829fe 9112+ valid = 1;
1facf9fc 9113+ sb = dentry->d_sb;
e49829fe
JR
9114+ /*
9115+ * todo: very ugly
9116+ * i_mutex of parent dir may be held,
9117+ * but we should not return 'invalid' due to busy.
9118+ */
9119+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9120+ if (unlikely(err)) {
9121+ valid = err;
027c5e7a 9122+ AuTraceErr(err);
e49829fe
JR
9123+ goto out;
9124+ }
5527c038
JR
9125+ inode = NULL;
9126+ if (d_really_is_positive(dentry))
9127+ inode = d_inode(dentry);
5afbbe0d 9128+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9129+ err = -EINVAL;
9130+ AuTraceErr(err);
9131+ goto out_dgrade;
9132+ }
027c5e7a
AM
9133+ if (unlikely(au_dbrange_test(dentry))) {
9134+ err = -EINVAL;
9135+ AuTraceErr(err);
9136+ goto out_dgrade;
1facf9fc 9137+ }
027c5e7a
AM
9138+
9139+ sigen = au_sigen(sb);
9140+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9141+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9142+ err = au_reval_dpath(dentry, sigen);
9143+ if (unlikely(err)) {
9144+ AuTraceErr(err);
1facf9fc 9145+ goto out_dgrade;
027c5e7a 9146+ }
1facf9fc 9147+ }
9148+ di_downgrade_lock(dentry, AuLock_IR);
9149+
1facf9fc 9150+ err = -EINVAL;
c1595e42 9151+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9152+ && inode
38d290e6 9153+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9154+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9155+ AuTraceErr(err);
027c5e7a 9156+ goto out_inval;
79b8bda9 9157+ }
027c5e7a 9158+
1facf9fc 9159+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9160+ if (do_udba && inode) {
5afbbe0d 9161+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9162+ struct inode *h_inode;
1facf9fc 9163+
5afbbe0d
AM
9164+ if (btop >= 0) {
9165+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9166+ if (h_inode && au_test_higen(inode, h_inode)) {
9167+ AuTraceErr(err);
027c5e7a 9168+ goto out_inval;
79b8bda9 9169+ }
027c5e7a 9170+ }
1facf9fc 9171+ }
9172+
8b6a4947
AM
9173+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9174+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9175+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9176+ err = -EIO;
523b37e3
AM
9177+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9178+ dentry, err);
027c5e7a 9179+ }
e49829fe 9180+ goto out_inval;
1facf9fc 9181+
4f0767ce 9182+out_dgrade:
1facf9fc 9183+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9184+out_inval:
1facf9fc 9185+ aufs_read_unlock(dentry, AuLock_IR);
9186+ AuTraceErr(err);
9187+ valid = !err;
e49829fe 9188+out:
027c5e7a 9189+ if (!valid) {
523b37e3 9190+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9191+ d_drop(dentry);
9192+ }
1facf9fc 9193+ return valid;
9194+}
9195+
9196+static void aufs_d_release(struct dentry *dentry)
9197+{
027c5e7a 9198+ if (au_di(dentry)) {
4a4d8108
AM
9199+ au_di_fin(dentry);
9200+ au_hn_di_reinit(dentry);
1facf9fc 9201+ }
1facf9fc 9202+}
9203+
4a4d8108 9204+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9205+ .d_revalidate = aufs_d_revalidate,
9206+ .d_weak_revalidate = aufs_d_revalidate,
9207+ .d_release = aufs_d_release
1facf9fc 9208+};
79b8bda9
AM
9209+
9210+/* aufs_dop without d_revalidate */
9211+const struct dentry_operations aufs_dop_noreval = {
9212+ .d_release = aufs_d_release
9213+};
7f207e10 9214diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
eca34b5c
AM
9215--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
9216+++ linux/fs/aufs/dentry.h 2019-07-11 15:42:14.462237786 +0200
9f237c51 9217@@ -0,0 +1,268 @@
062440b3 9218+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9219+/*
ba1aed25 9220+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 9221+ *
9222+ * This program, aufs is free software; you can redistribute it and/or modify
9223+ * it under the terms of the GNU General Public License as published by
9224+ * the Free Software Foundation; either version 2 of the License, or
9225+ * (at your option) any later version.
dece6358
AM
9226+ *
9227+ * This program is distributed in the hope that it will be useful,
9228+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9229+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9230+ * GNU General Public License for more details.
9231+ *
9232+ * You should have received a copy of the GNU General Public License
523b37e3 9233+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9234+ */
9235+
9236+/*
9237+ * lookup and dentry operations
9238+ */
9239+
9240+#ifndef __AUFS_DENTRY_H__
9241+#define __AUFS_DENTRY_H__
9242+
9243+#ifdef __KERNEL__
9244+
dece6358 9245+#include <linux/dcache.h>
8b6a4947 9246+#include "dirren.h"
1facf9fc 9247+#include "rwsem.h"
9248+
1facf9fc 9249+struct au_hdentry {
9250+ struct dentry *hd_dentry;
027c5e7a 9251+ aufs_bindex_t hd_id;
1facf9fc 9252+};
9253+
9254+struct au_dinfo {
9255+ atomic_t di_generation;
9256+
dece6358 9257+ struct au_rwsem di_rwsem;
5afbbe0d 9258+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9259+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9260+ struct au_hdentry *di_hdentry;
9f237c51 9261+ struct rcu_head rcu;
4a4d8108 9262+} ____cacheline_aligned_in_smp;
1facf9fc 9263+
9264+/* ---------------------------------------------------------------------- */
9265+
5afbbe0d
AM
9266+/* flags for au_lkup_dentry() */
9267+#define AuLkup_ALLOW_NEG 1
9268+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9269+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9270+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9271+#define au_fset_lkup(flags, name) \
9272+ do { (flags) |= AuLkup_##name; } while (0)
9273+#define au_fclr_lkup(flags, name) \
9274+ do { (flags) &= ~AuLkup_##name; } while (0)
9275+
8b6a4947
AM
9276+#ifndef CONFIG_AUFS_DIRREN
9277+#undef AuLkup_DIRREN
9278+#define AuLkup_DIRREN 0
9279+#endif
9280+
9281+struct au_do_lookup_args {
9282+ unsigned int flags;
9283+ mode_t type;
9284+ struct qstr whname, *name;
9285+ struct au_dr_lookup dirren;
9286+};
9287+
5afbbe0d
AM
9288+/* ---------------------------------------------------------------------- */
9289+
1facf9fc 9290+/* dentry.c */
79b8bda9 9291+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9292+struct au_branch;
076b876e 9293+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 9294+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9295+ struct dentry *h_parent, struct au_branch *br);
9296+
5afbbe0d
AM
9297+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9298+ unsigned int flags);
86dc4139 9299+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9300+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9301+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9302+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9303+
9304+/* dinfo.c */
4a4d8108 9305+void au_di_init_once(void *_di);
027c5e7a
AM
9306+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9307+void au_di_free(struct au_dinfo *dinfo);
9308+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9309+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9310+int au_di_init(struct dentry *dentry);
9311+void au_di_fin(struct dentry *dentry);
e2f27e51 9312+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9313+
9314+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9315+void di_read_unlock(struct dentry *d, int flags);
9316+void di_downgrade_lock(struct dentry *d, int flags);
9317+void di_write_lock(struct dentry *d, unsigned int lsc);
9318+void di_write_unlock(struct dentry *d);
9319+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9320+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9321+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9322+
9323+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9324+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9325+aufs_bindex_t au_dbtail(struct dentry *dentry);
9326+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9327+
9328+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9329+ struct dentry *h_dentry);
027c5e7a
AM
9330+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9331+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9332+void au_update_digen(struct dentry *dentry);
9333+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9334+void au_update_dbtop(struct dentry *dentry);
9335+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9336+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9337+
9338+/* ---------------------------------------------------------------------- */
9339+
9340+static inline struct au_dinfo *au_di(struct dentry *dentry)
9341+{
9342+ return dentry->d_fsdata;
9343+}
9344+
9345+/* ---------------------------------------------------------------------- */
9346+
9347+/* lock subclass for dinfo */
9348+enum {
9349+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9350+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9351+ AuLsc_DI_CHILD3, /* copyup dirs */
9352+ AuLsc_DI_PARENT,
9353+ AuLsc_DI_PARENT2,
027c5e7a
AM
9354+ AuLsc_DI_PARENT3,
9355+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9356+};
9357+
9358+/*
9359+ * di_read_lock_child, di_write_lock_child,
9360+ * di_read_lock_child2, di_write_lock_child2,
9361+ * di_read_lock_child3, di_write_lock_child3,
9362+ * di_read_lock_parent, di_write_lock_parent,
9363+ * di_read_lock_parent2, di_write_lock_parent2,
9364+ * di_read_lock_parent3, di_write_lock_parent3,
9365+ */
9366+#define AuReadLockFunc(name, lsc) \
9367+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9368+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9369+
9370+#define AuWriteLockFunc(name, lsc) \
9371+static inline void di_write_lock_##name(struct dentry *d) \
9372+{ di_write_lock(d, AuLsc_DI_##lsc); }
9373+
9374+#define AuRWLockFuncs(name, lsc) \
9375+ AuReadLockFunc(name, lsc) \
9376+ AuWriteLockFunc(name, lsc)
9377+
9378+AuRWLockFuncs(child, CHILD);
9379+AuRWLockFuncs(child2, CHILD2);
9380+AuRWLockFuncs(child3, CHILD3);
9381+AuRWLockFuncs(parent, PARENT);
9382+AuRWLockFuncs(parent2, PARENT2);
9383+AuRWLockFuncs(parent3, PARENT3);
9384+
9385+#undef AuReadLockFunc
9386+#undef AuWriteLockFunc
9387+#undef AuRWLockFuncs
9388+
9389+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9390+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9391+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9392+
9393+/* ---------------------------------------------------------------------- */
9394+
9395+/* todo: memory barrier? */
9396+static inline unsigned int au_digen(struct dentry *d)
9397+{
9398+ return atomic_read(&au_di(d)->di_generation);
9399+}
9400+
9401+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9402+{
9403+ hdentry->hd_dentry = NULL;
9404+}
9405+
5afbbe0d
AM
9406+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9407+ aufs_bindex_t bindex)
9408+{
9409+ return di->di_hdentry + bindex;
9410+}
9411+
1facf9fc 9412+static inline void au_hdput(struct au_hdentry *hd)
9413+{
4a4d8108
AM
9414+ if (hd)
9415+ dput(hd->hd_dentry);
1facf9fc 9416+}
9417+
5afbbe0d 9418+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9419+{
1308ab2a 9420+ DiMustAnyLock(dentry);
5afbbe0d 9421+ return au_di(dentry)->di_btop;
1facf9fc 9422+}
9423+
5afbbe0d 9424+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9425+{
1308ab2a 9426+ DiMustAnyLock(dentry);
5afbbe0d 9427+ return au_di(dentry)->di_bbot;
1facf9fc 9428+}
9429+
9430+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9431+{
1308ab2a 9432+ DiMustAnyLock(dentry);
1facf9fc 9433+ return au_di(dentry)->di_bwh;
9434+}
9435+
9436+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9437+{
1308ab2a 9438+ DiMustAnyLock(dentry);
1facf9fc 9439+ return au_di(dentry)->di_bdiropq;
9440+}
9441+
9442+/* todo: hard/soft set? */
5afbbe0d 9443+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9444+{
1308ab2a 9445+ DiMustWriteLock(dentry);
5afbbe0d 9446+ au_di(dentry)->di_btop = bindex;
1facf9fc 9447+}
9448+
5afbbe0d 9449+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9450+{
1308ab2a 9451+ DiMustWriteLock(dentry);
5afbbe0d 9452+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9453+}
9454+
9455+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9456+{
1308ab2a 9457+ DiMustWriteLock(dentry);
5afbbe0d 9458+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9459+ au_di(dentry)->di_bwh = bindex;
9460+}
9461+
9462+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9463+{
1308ab2a 9464+ DiMustWriteLock(dentry);
1facf9fc 9465+ au_di(dentry)->di_bdiropq = bindex;
9466+}
9467+
9468+/* ---------------------------------------------------------------------- */
9469+
4a4d8108 9470+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9471+static inline void au_digen_dec(struct dentry *d)
9472+{
e49829fe 9473+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9474+}
9475+
4a4d8108 9476+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9477+{
9478+ dentry->d_fsdata = NULL;
9479+}
9480+#else
4a4d8108
AM
9481+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9482+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9483+
9484+#endif /* __KERNEL__ */
9485+#endif /* __AUFS_DENTRY_H__ */
7f207e10 9486diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
eca34b5c
AM
9487--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
9488+++ linux/fs/aufs/dinfo.c 2019-07-11 15:42:14.462237786 +0200
062440b3 9489@@ -0,0 +1,554 @@
cd7a4cd9 9490+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9491+/*
ba1aed25 9492+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 9493+ *
9494+ * This program, aufs is free software; you can redistribute it and/or modify
9495+ * it under the terms of the GNU General Public License as published by
9496+ * the Free Software Foundation; either version 2 of the License, or
9497+ * (at your option) any later version.
dece6358
AM
9498+ *
9499+ * This program is distributed in the hope that it will be useful,
9500+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9501+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9502+ * GNU General Public License for more details.
9503+ *
9504+ * You should have received a copy of the GNU General Public License
523b37e3 9505+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9506+ */
9507+
9508+/*
9509+ * dentry private data
9510+ */
9511+
9512+#include "aufs.h"
9513+
e49829fe 9514+void au_di_init_once(void *_dinfo)
4a4d8108 9515+{
e49829fe 9516+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9517+
e49829fe 9518+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9519+}
9520+
027c5e7a 9521+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9522+{
9523+ struct au_dinfo *dinfo;
027c5e7a 9524+ int nbr, i;
1facf9fc 9525+
9526+ dinfo = au_cache_alloc_dinfo();
9527+ if (unlikely(!dinfo))
9528+ goto out;
9529+
5afbbe0d 9530+ nbr = au_sbbot(sb) + 1;
1facf9fc 9531+ if (nbr <= 0)
9532+ nbr = 1;
9533+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9534+ if (dinfo->di_hdentry) {
9535+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9536+ dinfo->di_btop = -1;
9537+ dinfo->di_bbot = -1;
027c5e7a
AM
9538+ dinfo->di_bwh = -1;
9539+ dinfo->di_bdiropq = -1;
38d290e6 9540+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9541+ for (i = 0; i < nbr; i++)
9542+ dinfo->di_hdentry[i].hd_id = -1;
9543+ goto out;
9544+ }
1facf9fc 9545+
1c60b727 9546+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9547+ dinfo = NULL;
9548+
4f0767ce 9549+out:
027c5e7a 9550+ return dinfo;
1facf9fc 9551+}
9552+
027c5e7a 9553+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9554+{
4a4d8108 9555+ struct au_hdentry *p;
5afbbe0d 9556+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9557+
9558+ /* dentry may not be revalidated */
5afbbe0d 9559+ bindex = dinfo->di_btop;
4a4d8108 9560+ if (bindex >= 0) {
5afbbe0d
AM
9561+ bbot = dinfo->di_bbot;
9562+ p = au_hdentry(dinfo, bindex);
9563+ while (bindex++ <= bbot)
4a4d8108
AM
9564+ au_hdput(p++);
9565+ }
9f237c51 9566+ au_kfree_try_rcu(dinfo->di_hdentry);
1c60b727 9567+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9568+}
9569+
9570+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9571+{
9572+ struct au_hdentry *p;
9573+ aufs_bindex_t bi;
9574+
9575+ AuRwMustWriteLock(&a->di_rwsem);
9576+ AuRwMustWriteLock(&b->di_rwsem);
9577+
9578+#define DiSwap(v, name) \
9579+ do { \
9580+ v = a->di_##name; \
9581+ a->di_##name = b->di_##name; \
9582+ b->di_##name = v; \
9583+ } while (0)
9584+
9585+ DiSwap(p, hdentry);
5afbbe0d
AM
9586+ DiSwap(bi, btop);
9587+ DiSwap(bi, bbot);
027c5e7a
AM
9588+ DiSwap(bi, bwh);
9589+ DiSwap(bi, bdiropq);
9590+ /* smp_mb(); */
9591+
9592+#undef DiSwap
9593+}
9594+
9595+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9596+{
9597+ AuRwMustWriteLock(&dst->di_rwsem);
9598+ AuRwMustWriteLock(&src->di_rwsem);
9599+
5afbbe0d
AM
9600+ dst->di_btop = src->di_btop;
9601+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9602+ dst->di_bwh = src->di_bwh;
9603+ dst->di_bdiropq = src->di_bdiropq;
9604+ /* smp_mb(); */
9605+}
9606+
9607+int au_di_init(struct dentry *dentry)
9608+{
9609+ int err;
9610+ struct super_block *sb;
9611+ struct au_dinfo *dinfo;
9612+
9613+ err = 0;
9614+ sb = dentry->d_sb;
9615+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9616+ if (dinfo) {
9617+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9618+ /* smp_mb(); */ /* atomic_set */
9619+ dentry->d_fsdata = dinfo;
9620+ } else
9621+ err = -ENOMEM;
9622+
9623+ return err;
9624+}
9625+
9626+void au_di_fin(struct dentry *dentry)
9627+{
9628+ struct au_dinfo *dinfo;
9629+
9630+ dinfo = au_di(dentry);
9631+ AuRwDestroy(&dinfo->di_rwsem);
9632+ au_di_free(dinfo);
4a4d8108
AM
9633+}
9634+
e2f27e51 9635+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9636+{
9637+ int err, sz;
9638+ struct au_hdentry *hdp;
9639+
1308ab2a 9640+ AuRwMustWriteLock(&dinfo->di_rwsem);
9641+
1facf9fc 9642+ err = -ENOMEM;
5afbbe0d 9643+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9644+ if (!sz)
9645+ sz = sizeof(*hdp);
e2f27e51
AM
9646+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9647+ may_shrink);
1facf9fc 9648+ if (hdp) {
9649+ dinfo->di_hdentry = hdp;
9650+ err = 0;
9651+ }
9652+
9653+ return err;
9654+}
9655+
9656+/* ---------------------------------------------------------------------- */
9657+
9658+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9659+{
9660+ switch (lsc) {
9661+ case AuLsc_DI_CHILD:
9662+ ii_write_lock_child(inode);
9663+ break;
9664+ case AuLsc_DI_CHILD2:
9665+ ii_write_lock_child2(inode);
9666+ break;
9667+ case AuLsc_DI_CHILD3:
9668+ ii_write_lock_child3(inode);
9669+ break;
9670+ case AuLsc_DI_PARENT:
9671+ ii_write_lock_parent(inode);
9672+ break;
9673+ case AuLsc_DI_PARENT2:
9674+ ii_write_lock_parent2(inode);
9675+ break;
9676+ case AuLsc_DI_PARENT3:
9677+ ii_write_lock_parent3(inode);
9678+ break;
9679+ default:
9680+ BUG();
9681+ }
9682+}
9683+
9684+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9685+{
9686+ switch (lsc) {
9687+ case AuLsc_DI_CHILD:
9688+ ii_read_lock_child(inode);
9689+ break;
9690+ case AuLsc_DI_CHILD2:
9691+ ii_read_lock_child2(inode);
9692+ break;
9693+ case AuLsc_DI_CHILD3:
9694+ ii_read_lock_child3(inode);
9695+ break;
9696+ case AuLsc_DI_PARENT:
9697+ ii_read_lock_parent(inode);
9698+ break;
9699+ case AuLsc_DI_PARENT2:
9700+ ii_read_lock_parent2(inode);
9701+ break;
9702+ case AuLsc_DI_PARENT3:
9703+ ii_read_lock_parent3(inode);
9704+ break;
9705+ default:
9706+ BUG();
9707+ }
9708+}
9709+
9710+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9711+{
5527c038
JR
9712+ struct inode *inode;
9713+
dece6358 9714+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9715+ if (d_really_is_positive(d)) {
9716+ inode = d_inode(d);
1facf9fc 9717+ if (au_ftest_lock(flags, IW))
5527c038 9718+ do_ii_write_lock(inode, lsc);
1facf9fc 9719+ else if (au_ftest_lock(flags, IR))
5527c038 9720+ do_ii_read_lock(inode, lsc);
1facf9fc 9721+ }
9722+}
9723+
9724+void di_read_unlock(struct dentry *d, int flags)
9725+{
5527c038
JR
9726+ struct inode *inode;
9727+
9728+ if (d_really_is_positive(d)) {
9729+ inode = d_inode(d);
027c5e7a
AM
9730+ if (au_ftest_lock(flags, IW)) {
9731+ au_dbg_verify_dinode(d);
5527c038 9732+ ii_write_unlock(inode);
027c5e7a
AM
9733+ } else if (au_ftest_lock(flags, IR)) {
9734+ au_dbg_verify_dinode(d);
5527c038 9735+ ii_read_unlock(inode);
027c5e7a 9736+ }
1facf9fc 9737+ }
dece6358 9738+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9739+}
9740+
9741+void di_downgrade_lock(struct dentry *d, int flags)
9742+{
5527c038
JR
9743+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9744+ ii_downgrade_lock(d_inode(d));
dece6358 9745+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9746+}
9747+
9748+void di_write_lock(struct dentry *d, unsigned int lsc)
9749+{
dece6358 9750+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9751+ if (d_really_is_positive(d))
9752+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9753+}
9754+
9755+void di_write_unlock(struct dentry *d)
9756+{
027c5e7a 9757+ au_dbg_verify_dinode(d);
5527c038
JR
9758+ if (d_really_is_positive(d))
9759+ ii_write_unlock(d_inode(d));
dece6358 9760+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9761+}
9762+
9763+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9764+{
9765+ AuDebugOn(d1 == d2
5527c038 9766+ || d_inode(d1) == d_inode(d2)
1facf9fc 9767+ || d1->d_sb != d2->d_sb);
9768+
521ced18
JR
9769+ if ((isdir && au_test_subdir(d1, d2))
9770+ || d1 < d2) {
1facf9fc 9771+ di_write_lock_child(d1);
9772+ di_write_lock_child2(d2);
9773+ } else {
1facf9fc 9774+ di_write_lock_child(d2);
9775+ di_write_lock_child2(d1);
9776+ }
9777+}
9778+
9779+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9780+{
9781+ AuDebugOn(d1 == d2
5527c038 9782+ || d_inode(d1) == d_inode(d2)
1facf9fc 9783+ || d1->d_sb != d2->d_sb);
9784+
521ced18
JR
9785+ if ((isdir && au_test_subdir(d1, d2))
9786+ || d1 < d2) {
1facf9fc 9787+ di_write_lock_parent(d1);
9788+ di_write_lock_parent2(d2);
9789+ } else {
1facf9fc 9790+ di_write_lock_parent(d2);
9791+ di_write_lock_parent2(d1);
9792+ }
9793+}
9794+
9795+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9796+{
9797+ di_write_unlock(d1);
5527c038 9798+ if (d_inode(d1) == d_inode(d2))
dece6358 9799+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9800+ else
9801+ di_write_unlock(d2);
9802+}
9803+
9804+/* ---------------------------------------------------------------------- */
9805+
9806+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9807+{
9808+ struct dentry *d;
9809+
1308ab2a 9810+ DiMustAnyLock(dentry);
9811+
5afbbe0d 9812+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9813+ return NULL;
9814+ AuDebugOn(bindex < 0);
5afbbe0d 9815+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9816+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9817+ return d;
9818+}
9819+
2cbb1c4b
JR
9820+/*
9821+ * extended version of au_h_dptr().
38d290e6
JR
9822+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9823+ * error.
2cbb1c4b
JR
9824+ */
9825+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9826+{
9827+ struct dentry *h_dentry;
9828+ struct inode *inode, *h_inode;
9829+
5527c038 9830+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9831+
9832+ h_dentry = NULL;
5afbbe0d
AM
9833+ if (au_dbtop(dentry) <= bindex
9834+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9835+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9836+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9837+ dget(h_dentry);
9838+ goto out; /* success */
9839+ }
9840+
5527c038 9841+ inode = d_inode(dentry);
5afbbe0d
AM
9842+ AuDebugOn(bindex < au_ibtop(inode));
9843+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9844+ h_inode = au_h_iptr(inode, bindex);
9845+ h_dentry = d_find_alias(h_inode);
9846+ if (h_dentry) {
9847+ if (!IS_ERR(h_dentry)) {
38d290e6 9848+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9849+ goto out; /* success */
9850+ dput(h_dentry);
9851+ } else
9852+ goto out;
9853+ }
9854+
9855+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9856+ h_dentry = au_plink_lkup(inode, bindex);
9857+ AuDebugOn(!h_dentry);
9858+ if (!IS_ERR(h_dentry)) {
9859+ if (!au_d_hashed_positive(h_dentry))
9860+ goto out; /* success */
9861+ dput(h_dentry);
9862+ h_dentry = NULL;
9863+ }
9864+ }
9865+
9866+out:
9867+ AuDbgDentry(h_dentry);
9868+ return h_dentry;
9869+}
9870+
1facf9fc 9871+aufs_bindex_t au_dbtail(struct dentry *dentry)
9872+{
5afbbe0d 9873+ aufs_bindex_t bbot, bwh;
1facf9fc 9874+
5afbbe0d
AM
9875+ bbot = au_dbbot(dentry);
9876+ if (0 <= bbot) {
1facf9fc 9877+ bwh = au_dbwh(dentry);
9878+ if (!bwh)
9879+ return bwh;
5afbbe0d 9880+ if (0 < bwh && bwh < bbot)
1facf9fc 9881+ return bwh - 1;
9882+ }
5afbbe0d 9883+ return bbot;
1facf9fc 9884+}
9885+
9886+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9887+{
5afbbe0d 9888+ aufs_bindex_t bbot, bopq;
1facf9fc 9889+
5afbbe0d
AM
9890+ bbot = au_dbtail(dentry);
9891+ if (0 <= bbot) {
1facf9fc 9892+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9893+ if (0 <= bopq && bopq < bbot)
9894+ bbot = bopq;
1facf9fc 9895+ }
5afbbe0d 9896+ return bbot;
1facf9fc 9897+}
9898+
9899+/* ---------------------------------------------------------------------- */
9900+
9901+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9902+ struct dentry *h_dentry)
9903+{
5afbbe0d
AM
9904+ struct au_dinfo *dinfo;
9905+ struct au_hdentry *hd;
027c5e7a 9906+ struct au_branch *br;
1facf9fc 9907+
1308ab2a 9908+ DiMustWriteLock(dentry);
9909+
5afbbe0d
AM
9910+ dinfo = au_di(dentry);
9911+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9912+ au_hdput(hd);
1facf9fc 9913+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9914+ if (h_dentry) {
9915+ br = au_sbr(dentry->d_sb, bindex);
9916+ hd->hd_id = br->br_id;
9917+ }
9918+}
9919+
9920+int au_dbrange_test(struct dentry *dentry)
9921+{
9922+ int err;
5afbbe0d 9923+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9924+
9925+ err = 0;
5afbbe0d
AM
9926+ btop = au_dbtop(dentry);
9927+ bbot = au_dbbot(dentry);
9928+ if (btop >= 0)
9929+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9930+ else {
9931+ err = -EIO;
5afbbe0d 9932+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9933+ }
9934+
9935+ return err;
9936+}
9937+
9938+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9939+{
9940+ int err;
9941+
9942+ err = 0;
9943+ if (unlikely(au_digen(dentry) != sigen
5527c038 9944+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
9945+ err = -EIO;
9946+
9947+ return err;
1facf9fc 9948+}
9949+
9950+void au_update_digen(struct dentry *dentry)
9951+{
9952+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9953+ /* smp_mb(); */ /* atomic_set */
9954+}
9955+
9956+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9957+{
9958+ struct au_dinfo *dinfo;
9959+ struct dentry *h_d;
4a4d8108 9960+ struct au_hdentry *hdp;
5afbbe0d 9961+ aufs_bindex_t bindex, bbot;
1facf9fc 9962+
1308ab2a 9963+ DiMustWriteLock(dentry);
9964+
1facf9fc 9965+ dinfo = au_di(dentry);
5afbbe0d 9966+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 9967+ return;
9968+
9969+ if (do_put_zero) {
5afbbe0d
AM
9970+ bbot = dinfo->di_bbot;
9971+ bindex = dinfo->di_btop;
9972+ hdp = au_hdentry(dinfo, bindex);
9973+ for (; bindex <= bbot; bindex++, hdp++) {
9974+ h_d = hdp->hd_dentry;
5527c038 9975+ if (h_d && d_is_negative(h_d))
1facf9fc 9976+ au_set_h_dptr(dentry, bindex, NULL);
9977+ }
9978+ }
9979+
5afbbe0d
AM
9980+ dinfo->di_btop = 0;
9981+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9982+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9983+ if (hdp->hd_dentry)
1facf9fc 9984+ break;
5afbbe0d
AM
9985+ if (dinfo->di_btop > dinfo->di_bbot) {
9986+ dinfo->di_btop = -1;
9987+ dinfo->di_bbot = -1;
1facf9fc 9988+ return;
9989+ }
9990+
5afbbe0d
AM
9991+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9992+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9993+ if (hdp->hd_dentry)
1facf9fc 9994+ break;
5afbbe0d 9995+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 9996+}
9997+
5afbbe0d 9998+void au_update_dbtop(struct dentry *dentry)
1facf9fc 9999+{
5afbbe0d 10000+ aufs_bindex_t bindex, bbot;
1facf9fc 10001+ struct dentry *h_dentry;
10002+
5afbbe0d
AM
10003+ bbot = au_dbbot(dentry);
10004+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 10005+ h_dentry = au_h_dptr(dentry, bindex);
10006+ if (!h_dentry)
10007+ continue;
5527c038 10008+ if (d_is_positive(h_dentry)) {
5afbbe0d 10009+ au_set_dbtop(dentry, bindex);
1facf9fc 10010+ return;
10011+ }
10012+ au_set_h_dptr(dentry, bindex, NULL);
10013+ }
10014+}
10015+
5afbbe0d 10016+void au_update_dbbot(struct dentry *dentry)
1facf9fc 10017+{
5afbbe0d 10018+ aufs_bindex_t bindex, btop;
1facf9fc 10019+ struct dentry *h_dentry;
10020+
5afbbe0d
AM
10021+ btop = au_dbtop(dentry);
10022+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 10023+ h_dentry = au_h_dptr(dentry, bindex);
10024+ if (!h_dentry)
10025+ continue;
5527c038 10026+ if (d_is_positive(h_dentry)) {
5afbbe0d 10027+ au_set_dbbot(dentry, bindex);
1facf9fc 10028+ return;
10029+ }
10030+ au_set_h_dptr(dentry, bindex, NULL);
10031+ }
10032+}
10033+
10034+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10035+{
5afbbe0d 10036+ aufs_bindex_t bindex, bbot;
1facf9fc 10037+
5afbbe0d
AM
10038+ bbot = au_dbbot(dentry);
10039+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 10040+ if (au_h_dptr(dentry, bindex) == h_dentry)
10041+ return bindex;
10042+ return -1;
10043+}
7f207e10 10044diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
eca34b5c
AM
10045--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
10046+++ linux/fs/aufs/dir.c 2019-07-11 15:42:14.462237786 +0200
eca801bf 10047@@ -0,0 +1,763 @@
cd7a4cd9 10048+// SPDX-License-Identifier: GPL-2.0
1facf9fc 10049+/*
ba1aed25 10050+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 10051+ *
10052+ * This program, aufs is free software; you can redistribute it and/or modify
10053+ * it under the terms of the GNU General Public License as published by
10054+ * the Free Software Foundation; either version 2 of the License, or
10055+ * (at your option) any later version.
dece6358
AM
10056+ *
10057+ * This program is distributed in the hope that it will be useful,
10058+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10059+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10060+ * GNU General Public License for more details.
10061+ *
10062+ * You should have received a copy of the GNU General Public License
523b37e3 10063+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10064+ */
10065+
10066+/*
10067+ * directory operations
10068+ */
10069+
10070+#include <linux/fs_stack.h>
eca801bf 10071+#include <linux/iversion.h>
1facf9fc 10072+#include "aufs.h"
10073+
10074+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10075+{
9dbd164d
AM
10076+ unsigned int nlink;
10077+
1facf9fc 10078+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10079+
9dbd164d
AM
10080+ nlink = dir->i_nlink;
10081+ nlink += h_dir->i_nlink - 2;
1facf9fc 10082+ if (h_dir->i_nlink < 2)
9dbd164d 10083+ nlink += 2;
f6b6e03d 10084+ smp_mb(); /* for i_nlink */
7eafdf33 10085+ /* 0 can happen in revaliding */
92d182d2 10086+ set_nlink(dir, nlink);
1facf9fc 10087+}
10088+
10089+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10090+{
9dbd164d
AM
10091+ unsigned int nlink;
10092+
1facf9fc 10093+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10094+
9dbd164d
AM
10095+ nlink = dir->i_nlink;
10096+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10097+ if (h_dir->i_nlink < 2)
9dbd164d 10098+ nlink -= 2;
f6b6e03d 10099+ smp_mb(); /* for i_nlink */
92d182d2 10100+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10101+ set_nlink(dir, nlink);
1facf9fc 10102+}
10103+
1308ab2a 10104+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10105+{
10106+ loff_t sz;
5afbbe0d 10107+ aufs_bindex_t bindex, bbot;
1308ab2a 10108+ struct file *h_file;
10109+ struct dentry *h_dentry;
10110+
10111+ sz = 0;
10112+ if (file) {
2000de60 10113+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10114+
5afbbe0d
AM
10115+ bbot = au_fbbot_dir(file);
10116+ for (bindex = au_fbtop(file);
10117+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10118+ bindex++) {
4a4d8108 10119+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10120+ if (h_file && file_inode(h_file))
10121+ sz += vfsub_f_size_read(h_file);
1308ab2a 10122+ }
10123+ } else {
10124+ AuDebugOn(!dentry);
2000de60 10125+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10126+
5afbbe0d
AM
10127+ bbot = au_dbtaildir(dentry);
10128+ for (bindex = au_dbtop(dentry);
10129+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10130+ bindex++) {
10131+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10132+ if (h_dentry && d_is_positive(h_dentry))
10133+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10134+ }
10135+ }
10136+ if (sz < KMALLOC_MAX_SIZE)
10137+ sz = roundup_pow_of_two(sz);
10138+ if (sz > KMALLOC_MAX_SIZE)
10139+ sz = KMALLOC_MAX_SIZE;
10140+ else if (sz < NAME_MAX) {
10141+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10142+ sz = AUFS_RDBLK_DEF;
10143+ }
10144+ return sz;
10145+}
10146+
b912730e
AM
10147+struct au_dir_ts_arg {
10148+ struct dentry *dentry;
10149+ aufs_bindex_t brid;
10150+};
10151+
10152+static void au_do_dir_ts(void *arg)
10153+{
10154+ struct au_dir_ts_arg *a = arg;
10155+ struct au_dtime dt;
10156+ struct path h_path;
10157+ struct inode *dir, *h_dir;
10158+ struct super_block *sb;
10159+ struct au_branch *br;
10160+ struct au_hinode *hdir;
10161+ int err;
5afbbe0d 10162+ aufs_bindex_t btop, bindex;
b912730e
AM
10163+
10164+ sb = a->dentry->d_sb;
5527c038 10165+ if (d_really_is_negative(a->dentry))
b912730e 10166+ goto out;
5527c038 10167+ /* no dir->i_mutex lock */
b95c5147
AM
10168+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10169+
5527c038 10170+ dir = d_inode(a->dentry);
5afbbe0d 10171+ btop = au_ibtop(dir);
b912730e 10172+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10173+ if (bindex < btop)
b912730e
AM
10174+ goto out_unlock;
10175+
10176+ br = au_sbr(sb, bindex);
10177+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10178+ if (!h_path.dentry)
10179+ goto out_unlock;
10180+ h_path.mnt = au_br_mnt(br);
10181+ au_dtime_store(&dt, a->dentry, &h_path);
10182+
5afbbe0d 10183+ br = au_sbr(sb, btop);
b912730e
AM
10184+ if (!au_br_writable(br->br_perm))
10185+ goto out_unlock;
5afbbe0d 10186+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10187+ h_path.mnt = au_br_mnt(br);
10188+ err = vfsub_mnt_want_write(h_path.mnt);
10189+ if (err)
10190+ goto out_unlock;
5afbbe0d
AM
10191+ hdir = au_hi(dir, btop);
10192+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10193+ h_dir = au_h_iptr(dir, btop);
b912730e 10194+ if (h_dir->i_nlink
cd7a4cd9 10195+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
b912730e
AM
10196+ dt.dt_h_path = h_path;
10197+ au_dtime_revert(&dt);
10198+ }
5afbbe0d 10199+ au_hn_inode_unlock(hdir);
b912730e
AM
10200+ vfsub_mnt_drop_write(h_path.mnt);
10201+ au_cpup_attr_timesizes(dir);
10202+
10203+out_unlock:
10204+ aufs_read_unlock(a->dentry, AuLock_DW);
10205+out:
10206+ dput(a->dentry);
10207+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 10208+ au_kfree_try_rcu(arg);
b912730e
AM
10209+}
10210+
10211+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10212+{
10213+ int perm, wkq_err;
5afbbe0d 10214+ aufs_bindex_t btop;
b912730e
AM
10215+ struct au_dir_ts_arg *arg;
10216+ struct dentry *dentry;
10217+ struct super_block *sb;
10218+
10219+ IMustLock(dir);
10220+
10221+ dentry = d_find_any_alias(dir);
10222+ AuDebugOn(!dentry);
10223+ sb = dentry->d_sb;
5afbbe0d
AM
10224+ btop = au_ibtop(dir);
10225+ if (btop == bindex) {
b912730e
AM
10226+ au_cpup_attr_timesizes(dir);
10227+ goto out;
10228+ }
10229+
5afbbe0d 10230+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10231+ if (!au_br_writable(perm))
10232+ goto out;
10233+
10234+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10235+ if (!arg)
10236+ goto out;
10237+
10238+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10239+ arg->brid = au_sbr_id(sb, bindex);
10240+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10241+ if (unlikely(wkq_err)) {
10242+ pr_err("wkq %d\n", wkq_err);
10243+ dput(dentry);
9f237c51 10244+ au_kfree_try_rcu(arg);
b912730e
AM
10245+ }
10246+
10247+out:
10248+ dput(dentry);
10249+}
10250+
1facf9fc 10251+/* ---------------------------------------------------------------------- */
10252+
10253+static int reopen_dir(struct file *file)
10254+{
10255+ int err;
10256+ unsigned int flags;
5afbbe0d 10257+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10258+ struct dentry *dentry, *h_dentry;
10259+ struct file *h_file;
10260+
10261+ /* open all lower dirs */
2000de60 10262+ dentry = file->f_path.dentry;
5afbbe0d
AM
10263+ btop = au_dbtop(dentry);
10264+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10265+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10266+ au_set_fbtop(file, btop);
1facf9fc 10267+
10268+ btail = au_dbtaildir(dentry);
5afbbe0d 10269+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10270+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10271+ au_set_fbbot_dir(file, btail);
1facf9fc 10272+
4a4d8108 10273+ flags = vfsub_file_flags(file);
5afbbe0d 10274+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10275+ h_dentry = au_h_dptr(dentry, bindex);
10276+ if (!h_dentry)
10277+ continue;
4a4d8108 10278+ h_file = au_hf_dir(file, bindex);
1facf9fc 10279+ if (h_file)
10280+ continue;
10281+
392086de 10282+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10283+ err = PTR_ERR(h_file);
10284+ if (IS_ERR(h_file))
10285+ goto out; /* close all? */
10286+ au_set_h_fptr(file, bindex, h_file);
10287+ }
10288+ au_update_figen(file);
10289+ /* todo: necessary? */
10290+ /* file->f_ra = h_file->f_ra; */
10291+ err = 0;
10292+
4f0767ce 10293+out:
1facf9fc 10294+ return err;
10295+}
10296+
b912730e 10297+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10298+{
10299+ int err;
10300+ aufs_bindex_t bindex, btail;
10301+ struct dentry *dentry, *h_dentry;
8cdd5066 10302+ struct vfsmount *mnt;
1facf9fc 10303+
1308ab2a 10304+ FiMustWriteLock(file);
b912730e 10305+ AuDebugOn(h_file);
1308ab2a 10306+
523b37e3 10307+ err = 0;
8cdd5066 10308+ mnt = file->f_path.mnt;
2000de60 10309+ dentry = file->f_path.dentry;
be118d29 10310+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10311+ bindex = au_dbtop(dentry);
10312+ au_set_fbtop(file, bindex);
1facf9fc 10313+ btail = au_dbtaildir(dentry);
5afbbe0d 10314+ au_set_fbbot_dir(file, btail);
1facf9fc 10315+ for (; !err && bindex <= btail; bindex++) {
10316+ h_dentry = au_h_dptr(dentry, bindex);
10317+ if (!h_dentry)
10318+ continue;
10319+
8cdd5066
JR
10320+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10321+ if (unlikely(err))
10322+ break;
392086de 10323+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10324+ if (IS_ERR(h_file)) {
10325+ err = PTR_ERR(h_file);
10326+ break;
10327+ }
10328+ au_set_h_fptr(file, bindex, h_file);
10329+ }
10330+ au_update_figen(file);
10331+ /* todo: necessary? */
10332+ /* file->f_ra = h_file->f_ra; */
10333+ if (!err)
10334+ return 0; /* success */
10335+
10336+ /* close all */
5afbbe0d 10337+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10338+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10339+ au_set_fbtop(file, -1);
10340+ au_set_fbbot_dir(file, -1);
4a4d8108 10341+
1facf9fc 10342+ return err;
10343+}
10344+
10345+static int aufs_open_dir(struct inode *inode __maybe_unused,
10346+ struct file *file)
10347+{
4a4d8108
AM
10348+ int err;
10349+ struct super_block *sb;
10350+ struct au_fidir *fidir;
10351+
10352+ err = -ENOMEM;
2000de60 10353+ sb = file->f_path.dentry->d_sb;
4a4d8108 10354+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10355+ fidir = au_fidir_alloc(sb);
4a4d8108 10356+ if (fidir) {
b912730e
AM
10357+ struct au_do_open_args args = {
10358+ .open = do_open_dir,
10359+ .fidir = fidir
10360+ };
10361+ err = au_do_open(file, &args);
4a4d8108 10362+ if (unlikely(err))
9f237c51 10363+ au_kfree_rcu(fidir);
4a4d8108
AM
10364+ }
10365+ si_read_unlock(sb);
10366+ return err;
1facf9fc 10367+}
10368+
10369+static int aufs_release_dir(struct inode *inode __maybe_unused,
10370+ struct file *file)
10371+{
10372+ struct au_vdir *vdir_cache;
4a4d8108
AM
10373+ struct au_finfo *finfo;
10374+ struct au_fidir *fidir;
f0c0a007 10375+ struct au_hfile *hf;
5afbbe0d 10376+ aufs_bindex_t bindex, bbot;
1facf9fc 10377+
4a4d8108
AM
10378+ finfo = au_fi(file);
10379+ fidir = finfo->fi_hdir;
10380+ if (fidir) {
8b6a4947
AM
10381+ au_hbl_del(&finfo->fi_hlist,
10382+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10383+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10384+ if (vdir_cache)
1c60b727 10385+ au_vdir_free(vdir_cache);
4a4d8108
AM
10386+
10387+ bindex = finfo->fi_btop;
10388+ if (bindex >= 0) {
f0c0a007 10389+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10390+ /*
10391+ * calls fput() instead of filp_close(),
10392+ * since no dnotify or lock for the lower file.
10393+ */
5afbbe0d 10394+ bbot = fidir->fd_bbot;
f0c0a007
AM
10395+ for (; bindex <= bbot; bindex++, hf++)
10396+ if (hf->hf_file)
1c60b727 10397+ au_hfput(hf, /*execed*/0);
4a4d8108 10398+ }
9f237c51 10399+ au_kfree_rcu(fidir);
4a4d8108 10400+ finfo->fi_hdir = NULL;
1facf9fc 10401+ }
1c60b727 10402+ au_finfo_fin(file);
1facf9fc 10403+ return 0;
10404+}
10405+
10406+/* ---------------------------------------------------------------------- */
10407+
4a4d8108
AM
10408+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10409+{
10410+ int err;
5afbbe0d 10411+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10412+ struct file *h_file;
10413+
10414+ err = 0;
5afbbe0d
AM
10415+ bbot = au_fbbot_dir(file);
10416+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10417+ h_file = au_hf_dir(file, bindex);
10418+ if (h_file)
10419+ err = vfsub_flush(h_file, id);
10420+ }
10421+ return err;
10422+}
10423+
10424+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10425+{
10426+ return au_do_flush(file, id, au_do_flush_dir);
10427+}
10428+
10429+/* ---------------------------------------------------------------------- */
10430+
1facf9fc 10431+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10432+{
10433+ int err;
5afbbe0d 10434+ aufs_bindex_t bbot, bindex;
1facf9fc 10435+ struct inode *inode;
10436+ struct super_block *sb;
10437+
10438+ err = 0;
10439+ sb = dentry->d_sb;
5527c038 10440+ inode = d_inode(dentry);
1facf9fc 10441+ IMustLock(inode);
5afbbe0d
AM
10442+ bbot = au_dbbot(dentry);
10443+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10444+ struct path h_path;
1facf9fc 10445+
10446+ if (au_test_ro(sb, bindex, inode))
10447+ continue;
10448+ h_path.dentry = au_h_dptr(dentry, bindex);
10449+ if (!h_path.dentry)
10450+ continue;
1facf9fc 10451+
1facf9fc 10452+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10453+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10454+ }
10455+
10456+ return err;
10457+}
10458+
10459+static int au_do_fsync_dir(struct file *file, int datasync)
10460+{
10461+ int err;
5afbbe0d 10462+ aufs_bindex_t bbot, bindex;
1facf9fc 10463+ struct file *h_file;
10464+ struct super_block *sb;
10465+ struct inode *inode;
1facf9fc 10466+
521ced18 10467+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10468+ if (unlikely(err))
10469+ goto out;
10470+
c06a8ce3 10471+ inode = file_inode(file);
b912730e 10472+ sb = inode->i_sb;
5afbbe0d
AM
10473+ bbot = au_fbbot_dir(file);
10474+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10475+ h_file = au_hf_dir(file, bindex);
1facf9fc 10476+ if (!h_file || au_test_ro(sb, bindex, inode))
10477+ continue;
10478+
53392da6 10479+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10480+ }
10481+
4f0767ce 10482+out:
1facf9fc 10483+ return err;
10484+}
10485+
10486+/*
10487+ * @file may be NULL
10488+ */
1e00d052
AM
10489+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10490+ int datasync)
1facf9fc 10491+{
10492+ int err;
b752ccd1 10493+ struct dentry *dentry;
5527c038 10494+ struct inode *inode;
1facf9fc 10495+ struct super_block *sb;
1facf9fc 10496+
10497+ err = 0;
2000de60 10498+ dentry = file->f_path.dentry;
5527c038 10499+ inode = d_inode(dentry);
febd17d6 10500+ inode_lock(inode);
1facf9fc 10501+ sb = dentry->d_sb;
10502+ si_noflush_read_lock(sb);
10503+ if (file)
10504+ err = au_do_fsync_dir(file, datasync);
10505+ else {
10506+ di_write_lock_child(dentry);
10507+ err = au_do_fsync_dir_no_file(dentry, datasync);
10508+ }
5527c038 10509+ au_cpup_attr_timesizes(inode);
1facf9fc 10510+ di_write_unlock(dentry);
10511+ if (file)
10512+ fi_write_unlock(file);
10513+
10514+ si_read_unlock(sb);
febd17d6 10515+ inode_unlock(inode);
1facf9fc 10516+ return err;
10517+}
10518+
10519+/* ---------------------------------------------------------------------- */
10520+
5afbbe0d 10521+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10522+{
10523+ int err;
10524+ struct dentry *dentry;
9dbd164d 10525+ struct inode *inode, *h_inode;
1facf9fc 10526+ struct super_block *sb;
10527+
062440b3 10528+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10529+
2000de60 10530+ dentry = file->f_path.dentry;
5527c038 10531+ inode = d_inode(dentry);
1facf9fc 10532+ IMustLock(inode);
10533+
10534+ sb = dentry->d_sb;
10535+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10536+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10537+ if (unlikely(err))
10538+ goto out;
027c5e7a
AM
10539+ err = au_alive_dir(dentry);
10540+ if (!err)
10541+ err = au_vdir_init(file);
1facf9fc 10542+ di_downgrade_lock(dentry, AuLock_IR);
10543+ if (unlikely(err))
10544+ goto out_unlock;
10545+
5afbbe0d 10546+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10547+ if (!au_test_nfsd()) {
392086de 10548+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10549+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10550+ } else {
10551+ /*
10552+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10553+ * encode_fh() and others.
10554+ */
9dbd164d 10555+ atomic_inc(&h_inode->i_count);
1facf9fc 10556+ di_read_unlock(dentry, AuLock_IR);
10557+ si_read_unlock(sb);
392086de 10558+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10559+ fsstack_copy_attr_atime(inode, h_inode);
10560+ fi_write_unlock(file);
9dbd164d 10561+ iput(h_inode);
1facf9fc 10562+
10563+ AuTraceErr(err);
10564+ return err;
10565+ }
10566+
4f0767ce 10567+out_unlock:
1facf9fc 10568+ di_read_unlock(dentry, AuLock_IR);
10569+ fi_write_unlock(file);
4f0767ce 10570+out:
1facf9fc 10571+ si_read_unlock(sb);
10572+ return err;
10573+}
10574+
10575+/* ---------------------------------------------------------------------- */
10576+
10577+#define AuTestEmpty_WHONLY 1
dece6358
AM
10578+#define AuTestEmpty_CALLED (1 << 1)
10579+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10580+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10581+#define au_fset_testempty(flags, name) \
10582+ do { (flags) |= AuTestEmpty_##name; } while (0)
10583+#define au_fclr_testempty(flags, name) \
10584+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10585+
dece6358
AM
10586+#ifndef CONFIG_AUFS_SHWH
10587+#undef AuTestEmpty_SHWH
10588+#define AuTestEmpty_SHWH 0
10589+#endif
10590+
1facf9fc 10591+struct test_empty_arg {
392086de 10592+ struct dir_context ctx;
1308ab2a 10593+ struct au_nhash *whlist;
1facf9fc 10594+ unsigned int flags;
10595+ int err;
10596+ aufs_bindex_t bindex;
10597+};
10598+
392086de
AM
10599+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10600+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10601+ unsigned int d_type)
1facf9fc 10602+{
392086de
AM
10603+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10604+ ctx);
1facf9fc 10605+ char *name = (void *)__name;
10606+
10607+ arg->err = 0;
10608+ au_fset_testempty(arg->flags, CALLED);
10609+ /* smp_mb(); */
10610+ if (name[0] == '.'
10611+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10612+ goto out; /* success */
10613+
10614+ if (namelen <= AUFS_WH_PFX_LEN
10615+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10616+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10617+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10618+ arg->err = -ENOTEMPTY;
10619+ goto out;
10620+ }
10621+
10622+ name += AUFS_WH_PFX_LEN;
10623+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10624+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10625+ arg->err = au_nhash_append_wh
1308ab2a 10626+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10627+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10628+
4f0767ce 10629+out:
1facf9fc 10630+ /* smp_mb(); */
10631+ AuTraceErr(arg->err);
10632+ return arg->err;
10633+}
10634+
10635+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10636+{
10637+ int err;
10638+ struct file *h_file;
acd2b654 10639+ struct au_branch *br;
1facf9fc 10640+
10641+ h_file = au_h_open(dentry, arg->bindex,
10642+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10643+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10644+ err = PTR_ERR(h_file);
10645+ if (IS_ERR(h_file))
10646+ goto out;
10647+
10648+ err = 0;
10649+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10650+ && !file_inode(h_file)->i_nlink)
1facf9fc 10651+ goto out_put;
10652+
10653+ do {
10654+ arg->err = 0;
10655+ au_fclr_testempty(arg->flags, CALLED);
10656+ /* smp_mb(); */
392086de 10657+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10658+ if (err >= 0)
10659+ err = arg->err;
10660+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10661+
4f0767ce 10662+out_put:
1facf9fc 10663+ fput(h_file);
acd2b654
AM
10664+ br = au_sbr(dentry->d_sb, arg->bindex);
10665+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 10666+out:
1facf9fc 10667+ return err;
10668+}
10669+
10670+struct do_test_empty_args {
10671+ int *errp;
10672+ struct dentry *dentry;
10673+ struct test_empty_arg *arg;
10674+};
10675+
10676+static void call_do_test_empty(void *args)
10677+{
10678+ struct do_test_empty_args *a = args;
10679+ *a->errp = do_test_empty(a->dentry, a->arg);
10680+}
10681+
10682+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10683+{
10684+ int err, wkq_err;
10685+ struct dentry *h_dentry;
10686+ struct inode *h_inode;
10687+
10688+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10689+ h_inode = d_inode(h_dentry);
53392da6 10690+ /* todo: i_mode changes anytime? */
be118d29 10691+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10692+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10693+ inode_unlock_shared(h_inode);
1facf9fc 10694+ if (!err)
10695+ err = do_test_empty(dentry, arg);
10696+ else {
10697+ struct do_test_empty_args args = {
10698+ .errp = &err,
10699+ .dentry = dentry,
10700+ .arg = arg
10701+ };
10702+ unsigned int flags = arg->flags;
10703+
10704+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10705+ if (unlikely(wkq_err))
10706+ err = wkq_err;
10707+ arg->flags = flags;
10708+ }
10709+
10710+ return err;
10711+}
10712+
10713+int au_test_empty_lower(struct dentry *dentry)
10714+{
10715+ int err;
1308ab2a 10716+ unsigned int rdhash;
5afbbe0d 10717+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10718+ struct au_nhash whlist;
392086de
AM
10719+ struct test_empty_arg arg = {
10720+ .ctx = {
2000de60 10721+ .actor = test_empty_cb
392086de
AM
10722+ }
10723+ };
076b876e 10724+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10725+
dece6358
AM
10726+ SiMustAnyLock(dentry->d_sb);
10727+
1308ab2a 10728+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10729+ if (!rdhash)
10730+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10731+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10732+ if (unlikely(err))
1facf9fc 10733+ goto out;
10734+
1facf9fc 10735+ arg.flags = 0;
1308ab2a 10736+ arg.whlist = &whlist;
5afbbe0d 10737+ btop = au_dbtop(dentry);
dece6358
AM
10738+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10739+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10740+ test_empty = do_test_empty;
10741+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10742+ test_empty = sio_test_empty;
5afbbe0d 10743+ arg.bindex = btop;
076b876e 10744+ err = test_empty(dentry, &arg);
1facf9fc 10745+ if (unlikely(err))
10746+ goto out_whlist;
10747+
10748+ au_fset_testempty(arg.flags, WHONLY);
10749+ btail = au_dbtaildir(dentry);
5afbbe0d 10750+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10751+ struct dentry *h_dentry;
10752+
10753+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10754+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10755+ arg.bindex = bindex;
076b876e 10756+ err = test_empty(dentry, &arg);
1facf9fc 10757+ }
10758+ }
10759+
4f0767ce 10760+out_whlist:
1308ab2a 10761+ au_nhash_wh_free(&whlist);
4f0767ce 10762+out:
1facf9fc 10763+ return err;
10764+}
10765+
10766+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10767+{
10768+ int err;
392086de
AM
10769+ struct test_empty_arg arg = {
10770+ .ctx = {
2000de60 10771+ .actor = test_empty_cb
392086de
AM
10772+ }
10773+ };
1facf9fc 10774+ aufs_bindex_t bindex, btail;
10775+
10776+ err = 0;
1308ab2a 10777+ arg.whlist = whlist;
1facf9fc 10778+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10779+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10780+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10781+ btail = au_dbtaildir(dentry);
5afbbe0d 10782+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10783+ struct dentry *h_dentry;
10784+
10785+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10786+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10787+ arg.bindex = bindex;
10788+ err = sio_test_empty(dentry, &arg);
10789+ }
10790+ }
10791+
10792+ return err;
10793+}
10794+
10795+/* ---------------------------------------------------------------------- */
10796+
10797+const struct file_operations aufs_dir_fop = {
4a4d8108 10798+ .owner = THIS_MODULE,
027c5e7a 10799+ .llseek = default_llseek,
1facf9fc 10800+ .read = generic_read_dir,
5afbbe0d 10801+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10802+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10803+#ifdef CONFIG_COMPAT
10804+ .compat_ioctl = aufs_compat_ioctl_dir,
10805+#endif
1facf9fc 10806+ .open = aufs_open_dir,
10807+ .release = aufs_release_dir,
4a4d8108 10808+ .flush = aufs_flush_dir,
1facf9fc 10809+ .fsync = aufs_fsync_dir
10810+};
7f207e10 10811diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
eca34b5c
AM
10812--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
10813+++ linux/fs/aufs/dir.h 2019-07-11 15:42:14.462237786 +0200
9f237c51 10814@@ -0,0 +1,134 @@
062440b3 10815+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10816+/*
ba1aed25 10817+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 10818+ *
10819+ * This program, aufs is free software; you can redistribute it and/or modify
10820+ * it under the terms of the GNU General Public License as published by
10821+ * the Free Software Foundation; either version 2 of the License, or
10822+ * (at your option) any later version.
dece6358
AM
10823+ *
10824+ * This program is distributed in the hope that it will be useful,
10825+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10826+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10827+ * GNU General Public License for more details.
10828+ *
10829+ * You should have received a copy of the GNU General Public License
523b37e3 10830+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10831+ */
10832+
10833+/*
10834+ * directory operations
10835+ */
10836+
10837+#ifndef __AUFS_DIR_H__
10838+#define __AUFS_DIR_H__
10839+
10840+#ifdef __KERNEL__
10841+
10842+#include <linux/fs.h>
1facf9fc 10843+
10844+/* ---------------------------------------------------------------------- */
10845+
10846+/* need to be faster and smaller */
10847+
10848+struct au_nhash {
dece6358
AM
10849+ unsigned int nh_num;
10850+ struct hlist_head *nh_head;
1facf9fc 10851+};
10852+
10853+struct au_vdir_destr {
10854+ unsigned char len;
10855+ unsigned char name[0];
10856+} __packed;
10857+
10858+struct au_vdir_dehstr {
10859+ struct hlist_node hash;
1c60b727 10860+ struct au_vdir_destr *str;
9f237c51 10861+ struct rcu_head rcu;
4a4d8108 10862+} ____cacheline_aligned_in_smp;
1facf9fc 10863+
10864+struct au_vdir_de {
10865+ ino_t de_ino;
10866+ unsigned char de_type;
10867+ /* caution: packed */
10868+ struct au_vdir_destr de_str;
10869+} __packed;
10870+
10871+struct au_vdir_wh {
10872+ struct hlist_node wh_hash;
dece6358
AM
10873+#ifdef CONFIG_AUFS_SHWH
10874+ ino_t wh_ino;
1facf9fc 10875+ aufs_bindex_t wh_bindex;
dece6358
AM
10876+ unsigned char wh_type;
10877+#else
10878+ aufs_bindex_t wh_bindex;
10879+#endif
10880+ /* caution: packed */
1facf9fc 10881+ struct au_vdir_destr wh_str;
10882+} __packed;
10883+
10884+union au_vdir_deblk_p {
10885+ unsigned char *deblk;
10886+ struct au_vdir_de *de;
10887+};
10888+
10889+struct au_vdir {
10890+ unsigned char **vd_deblk;
10891+ unsigned long vd_nblk;
1facf9fc 10892+ struct {
10893+ unsigned long ul;
10894+ union au_vdir_deblk_p p;
10895+ } vd_last;
10896+
be118d29 10897+ u64 vd_version;
dece6358 10898+ unsigned int vd_deblk_sz;
9f237c51
AM
10899+ unsigned long vd_jiffy;
10900+ struct rcu_head rcu;
4a4d8108 10901+} ____cacheline_aligned_in_smp;
1facf9fc 10902+
10903+/* ---------------------------------------------------------------------- */
10904+
10905+/* dir.c */
10906+extern const struct file_operations aufs_dir_fop;
10907+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10908+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10909+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10910+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10911+int au_test_empty_lower(struct dentry *dentry);
10912+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10913+
10914+/* vdir.c */
1308ab2a 10915+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10916+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10917+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10918+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10919+ int limit);
dece6358
AM
10920+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10921+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10922+ unsigned int d_type, aufs_bindex_t bindex,
10923+ unsigned char shwh);
1c60b727 10924+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 10925+int au_vdir_init(struct file *file);
392086de 10926+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10927+
10928+/* ioctl.c */
10929+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10930+
1308ab2a 10931+#ifdef CONFIG_AUFS_RDU
10932+/* rdu.c */
10933+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10934+#ifdef CONFIG_COMPAT
10935+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10936+ unsigned long arg);
10937+#endif
1308ab2a 10938+#else
c1595e42
JR
10939+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10940+ unsigned int cmd, unsigned long arg)
b752ccd1 10941+#ifdef CONFIG_COMPAT
c1595e42
JR
10942+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10943+ unsigned int cmd, unsigned long arg)
b752ccd1 10944+#endif
1308ab2a 10945+#endif
10946+
1facf9fc 10947+#endif /* __KERNEL__ */
10948+#endif /* __AUFS_DIR_H__ */
8b6a4947 10949diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
eca34b5c
AM
10950--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
10951+++ linux/fs/aufs/dirren.c 2019-07-11 15:42:14.462237786 +0200
062440b3 10952@@ -0,0 +1,1316 @@
cd7a4cd9 10953+// SPDX-License-Identifier: GPL-2.0
8b6a4947 10954+/*
ba1aed25 10955+ * Copyright (C) 2017-2019 Junjiro R. Okajima
8b6a4947
AM
10956+ *
10957+ * This program, aufs is free software; you can redistribute it and/or modify
10958+ * it under the terms of the GNU General Public License as published by
10959+ * the Free Software Foundation; either version 2 of the License, or
10960+ * (at your option) any later version.
10961+ *
10962+ * This program is distributed in the hope that it will be useful,
10963+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10964+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10965+ * GNU General Public License for more details.
10966+ *
10967+ * You should have received a copy of the GNU General Public License
10968+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10969+ */
10970+
10971+/*
acd2b654 10972+ * special handling in renaming a directory
8b6a4947
AM
10973+ * in order to support looking-up the before-renamed name on the lower readonly
10974+ * branches
10975+ */
10976+
10977+#include <linux/byteorder/generic.h>
10978+#include "aufs.h"
10979+
10980+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10981+{
10982+ int idx;
10983+
10984+ idx = au_dr_ihash(ent->dr_h_ino);
10985+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10986+}
10987+
10988+static int au_dr_hino_test_empty(struct au_dr_br *dr)
10989+{
10990+ int ret, i;
10991+ struct hlist_bl_head *hbl;
10992+
10993+ ret = 1;
10994+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
10995+ hbl = dr->dr_h_ino + i;
10996+ hlist_bl_lock(hbl);
10997+ ret &= hlist_bl_empty(hbl);
10998+ hlist_bl_unlock(hbl);
10999+ }
11000+
11001+ return ret;
11002+}
11003+
11004+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11005+{
11006+ struct au_dr_hino *found, *ent;
11007+ struct hlist_bl_head *hbl;
11008+ struct hlist_bl_node *pos;
11009+ int idx;
11010+
11011+ found = NULL;
11012+ idx = au_dr_ihash(ino);
11013+ hbl = dr->dr_h_ino + idx;
11014+ hlist_bl_lock(hbl);
11015+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11016+ if (ent->dr_h_ino == ino) {
11017+ found = ent;
11018+ break;
11019+ }
11020+ hlist_bl_unlock(hbl);
11021+
11022+ return found;
11023+}
11024+
11025+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11026+ struct au_dr_hino *add_ent)
11027+{
11028+ int found, idx;
11029+ struct hlist_bl_head *hbl;
11030+ struct hlist_bl_node *pos;
11031+ struct au_dr_hino *ent;
11032+
11033+ found = 0;
11034+ idx = au_dr_ihash(ino);
11035+ hbl = dr->dr_h_ino + idx;
11036+#if 0
11037+ {
11038+ struct hlist_bl_node *tmp;
11039+
11040+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11041+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11042+ }
11043+#endif
11044+ hlist_bl_lock(hbl);
11045+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11046+ if (ent->dr_h_ino == ino) {
11047+ found = 1;
11048+ break;
11049+ }
11050+ if (!found && add_ent)
11051+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11052+ hlist_bl_unlock(hbl);
11053+
11054+ if (!found && add_ent)
11055+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11056+
11057+ return found;
11058+}
11059+
11060+void au_dr_hino_free(struct au_dr_br *dr)
11061+{
11062+ int i;
11063+ struct hlist_bl_head *hbl;
11064+ struct hlist_bl_node *pos, *tmp;
11065+ struct au_dr_hino *ent;
11066+
11067+ /* SiMustWriteLock(sb); */
11068+
11069+ for (i = 0; i < AuDirren_NHASH; i++) {
11070+ hbl = dr->dr_h_ino + i;
11071+ /* no spinlock since sbinfo must be write-locked */
11072+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
9f237c51 11073+ au_kfree_rcu(ent);
8b6a4947
AM
11074+ INIT_HLIST_BL_HEAD(hbl);
11075+ }
11076+}
11077+
11078+/* returns the number of inodes or an error */
11079+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11080+ struct file *hinofile)
11081+{
11082+ int err, i;
11083+ ssize_t ssz;
11084+ loff_t pos, oldsize;
11085+ __be64 u64;
11086+ struct inode *hinoinode;
11087+ struct hlist_bl_head *hbl;
11088+ struct hlist_bl_node *n1, *n2;
11089+ struct au_dr_hino *ent;
11090+
11091+ SiMustWriteLock(sb);
11092+ AuDebugOn(!au_br_writable(br->br_perm));
11093+
11094+ hinoinode = file_inode(hinofile);
11095+ oldsize = i_size_read(hinoinode);
11096+
11097+ err = 0;
11098+ pos = 0;
11099+ hbl = br->br_dirren.dr_h_ino;
11100+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11101+ /* no bit-lock since sbinfo must be write-locked */
11102+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11103+ AuDbg("hi%llu, %pD2\n",
11104+ (unsigned long long)ent->dr_h_ino, hinofile);
11105+ u64 = cpu_to_be64(ent->dr_h_ino);
11106+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11107+ if (ssz == sizeof(u64))
11108+ continue;
11109+
11110+ /* write error */
11111+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11112+ err = -ENOSPC;
11113+ if (ssz < 0)
11114+ err = ssz;
11115+ break;
11116+ }
11117+ }
11118+ /* regardless the error */
11119+ if (pos < oldsize) {
11120+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11121+ AuTraceErr(err);
11122+ }
11123+
11124+ AuTraceErr(err);
11125+ return err;
11126+}
11127+
11128+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11129+{
11130+ int err, hidx;
11131+ ssize_t ssz;
11132+ size_t sz, n;
11133+ loff_t pos;
11134+ uint64_t u64;
11135+ struct au_dr_hino *ent;
11136+ struct inode *hinoinode;
11137+ struct hlist_bl_head *hbl;
11138+
11139+ err = 0;
11140+ pos = 0;
11141+ hbl = dr->dr_h_ino;
11142+ hinoinode = file_inode(hinofile);
11143+ sz = i_size_read(hinoinode);
11144+ AuDebugOn(sz % sizeof(u64));
11145+ n = sz / sizeof(u64);
11146+ while (n--) {
11147+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11148+ if (unlikely(ssz != sizeof(u64))) {
11149+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11150+ err = -EINVAL;
11151+ if (ssz < 0)
11152+ err = ssz;
11153+ goto out_free;
11154+ }
11155+
11156+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11157+ if (!ent) {
11158+ err = -ENOMEM;
11159+ AuTraceErr(err);
11160+ goto out_free;
11161+ }
11162+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11163+ AuDbg("hi%llu, %pD2\n",
11164+ (unsigned long long)ent->dr_h_ino, hinofile);
11165+ hidx = au_dr_ihash(ent->dr_h_ino);
11166+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11167+ }
11168+ goto out; /* success */
11169+
11170+out_free:
11171+ au_dr_hino_free(dr);
11172+out:
11173+ AuTraceErr(err);
11174+ return err;
11175+}
11176+
11177+/*
11178+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11179+ * @path is a switch to distinguish load and store.
11180+ */
11181+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11182+ struct au_branch *br, const struct path *path)
11183+{
11184+ int err, flags;
11185+ unsigned char load, suspend;
11186+ struct file *hinofile;
11187+ struct au_hinode *hdir;
11188+ struct inode *dir, *delegated;
11189+ struct path hinopath;
11190+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11191+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11192+
11193+ AuDebugOn(bindex < 0 && !br);
11194+ AuDebugOn(bindex >= 0 && br);
11195+
11196+ err = -EINVAL;
11197+ suspend = !br;
11198+ if (suspend)
11199+ br = au_sbr(sb, bindex);
11200+ load = !!path;
11201+ if (!load) {
11202+ path = &br->br_path;
11203+ AuDebugOn(!au_br_writable(br->br_perm));
11204+ if (unlikely(!au_br_writable(br->br_perm)))
11205+ goto out;
11206+ }
11207+
11208+ hdir = NULL;
11209+ if (suspend) {
11210+ dir = d_inode(sb->s_root);
11211+ hdir = au_hinode(au_ii(dir), bindex);
11212+ dir = hdir->hi_inode;
11213+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11214+ } else {
11215+ dir = d_inode(path->dentry);
11216+ inode_lock_nested(dir, AuLsc_I_CHILD);
11217+ }
11218+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11219+ err = PTR_ERR(hinopath.dentry);
11220+ if (IS_ERR(hinopath.dentry))
11221+ goto out_unlock;
11222+
11223+ err = 0;
11224+ flags = O_RDONLY;
11225+ if (load) {
11226+ if (d_is_negative(hinopath.dentry))
11227+ goto out_dput; /* success */
11228+ } else {
11229+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11230+ if (d_is_positive(hinopath.dentry)) {
11231+ delegated = NULL;
11232+ err = vfsub_unlink(dir, &hinopath, &delegated,
11233+ /*force*/0);
11234+ AuTraceErr(err);
11235+ if (unlikely(err))
11236+ pr_err("ignored err %d, %pd2\n",
11237+ err, hinopath.dentry);
11238+ if (unlikely(err == -EWOULDBLOCK))
11239+ iput(delegated);
11240+ err = 0;
11241+ }
11242+ goto out_dput;
11243+ } else if (!d_is_positive(hinopath.dentry)) {
11244+ err = vfsub_create(dir, &hinopath, 0600,
11245+ /*want_excl*/false);
11246+ AuTraceErr(err);
11247+ if (unlikely(err))
11248+ goto out_dput;
11249+ }
11250+ flags = O_WRONLY;
11251+ }
11252+ hinopath.mnt = path->mnt;
11253+ hinofile = vfsub_dentry_open(&hinopath, flags);
11254+ if (suspend)
11255+ au_hn_inode_unlock(hdir);
11256+ else
11257+ inode_unlock(dir);
11258+ dput(hinopath.dentry);
11259+ AuTraceErrPtr(hinofile);
11260+ if (IS_ERR(hinofile)) {
11261+ err = PTR_ERR(hinofile);
11262+ goto out;
11263+ }
11264+
11265+ if (load)
11266+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11267+ else
11268+ err = au_dr_hino_store(sb, br, hinofile);
11269+ fput(hinofile);
11270+ goto out;
11271+
11272+out_dput:
11273+ dput(hinopath.dentry);
11274+out_unlock:
11275+ if (suspend)
11276+ au_hn_inode_unlock(hdir);
11277+ else
11278+ inode_unlock(dir);
11279+out:
11280+ AuTraceErr(err);
11281+ return err;
11282+}
11283+
11284+/* ---------------------------------------------------------------------- */
11285+
11286+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11287+{
11288+ int err;
11289+ struct kstatfs kstfs;
11290+ dev_t dev;
11291+ struct dentry *dentry;
11292+ struct super_block *sb;
11293+
11294+ err = vfs_statfs((void *)path, &kstfs);
11295+ AuTraceErr(err);
11296+ if (unlikely(err))
11297+ goto out;
11298+
11299+ /* todo: support for UUID */
11300+
11301+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11302+ brid->type = AuBrid_FSID;
11303+ brid->fsid = kstfs.f_fsid;
11304+ } else {
11305+ dentry = path->dentry;
11306+ sb = dentry->d_sb;
11307+ dev = sb->s_dev;
11308+ if (dev) {
11309+ brid->type = AuBrid_DEV;
11310+ brid->dev = dev;
11311+ }
11312+ }
11313+
11314+out:
11315+ return err;
11316+}
11317+
11318+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11319+ const struct path *path)
11320+{
11321+ int err, i;
11322+ struct au_dr_br *dr;
11323+ struct hlist_bl_head *hbl;
11324+
11325+ dr = &br->br_dirren;
11326+ hbl = dr->dr_h_ino;
11327+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11328+ INIT_HLIST_BL_HEAD(hbl);
11329+
11330+ err = au_dr_brid_init(&dr->dr_brid, path);
11331+ if (unlikely(err))
11332+ goto out;
11333+
11334+ if (au_opt_test(au_mntflags(sb), DIRREN))
11335+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11336+
11337+out:
11338+ AuTraceErr(err);
11339+ return err;
11340+}
11341+
11342+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11343+{
11344+ int err;
11345+
11346+ err = 0;
11347+ if (au_br_writable(br->br_perm))
11348+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11349+ if (!err)
11350+ au_dr_hino_free(&br->br_dirren);
11351+
11352+ return err;
11353+}
11354+
11355+/* ---------------------------------------------------------------------- */
11356+
11357+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11358+ char *buf, size_t sz)
11359+{
11360+ int err;
11361+ unsigned int major, minor;
11362+ char *p;
11363+
11364+ p = buf;
11365+ err = snprintf(p, sz, "%d_", brid->type);
11366+ AuDebugOn(err > sz);
11367+ p += err;
11368+ sz -= err;
11369+ switch (brid->type) {
11370+ case AuBrid_Unset:
11371+ return -EINVAL;
11372+ case AuBrid_UUID:
11373+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11374+ break;
11375+ case AuBrid_FSID:
11376+ err = snprintf(p, sz, "%08x-%08x",
11377+ brid->fsid.val[0], brid->fsid.val[1]);
11378+ break;
11379+ case AuBrid_DEV:
11380+ major = MAJOR(brid->dev);
11381+ minor = MINOR(brid->dev);
11382+ if (major <= 0xff && minor <= 0xff)
11383+ err = snprintf(p, sz, "%02x%02x", major, minor);
11384+ else
11385+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11386+ break;
11387+ }
11388+ AuDebugOn(err > sz);
11389+ p += err;
11390+ sz -= err;
11391+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11392+ AuDebugOn(err > sz);
11393+ p += err;
11394+ sz -= err;
11395+
11396+ return p - buf;
11397+}
11398+
11399+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11400+{
11401+ int rlen;
11402+ struct dentry *br_dentry;
11403+ struct inode *br_inode;
11404+
11405+ br_dentry = au_br_dentry(br);
11406+ br_inode = d_inode(br_dentry);
11407+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11408+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11409+ AuDebugOn(rlen > len);
11410+
11411+ return rlen;
11412+}
11413+
11414+/* ---------------------------------------------------------------------- */
11415+
11416+/*
11417+ * from the given @h_dentry, construct drinfo at @*fdata.
11418+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11419+ * @allocated.
11420+ */
11421+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11422+ struct dentry *h_dentry,
11423+ unsigned char *allocated)
11424+{
11425+ int err, v;
11426+ struct au_drinfo_fdata *f, *p;
11427+ struct au_drinfo *drinfo;
11428+ struct inode *h_inode;
11429+ struct qstr *qname;
11430+
11431+ err = 0;
11432+ f = *fdata;
11433+ h_inode = d_inode(h_dentry);
11434+ qname = &h_dentry->d_name;
11435+ drinfo = &f->drinfo;
11436+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11437+ drinfo->oldnamelen = qname->len;
11438+ if (*allocated < sizeof(*f) + qname->len) {
11439+ v = roundup_pow_of_two(*allocated + qname->len);
11440+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11441+ if (unlikely(!p)) {
11442+ err = -ENOMEM;
11443+ AuTraceErr(err);
11444+ goto out;
11445+ }
11446+ f = p;
11447+ *fdata = f;
11448+ *allocated = v;
11449+ drinfo = &f->drinfo;
11450+ }
11451+ memcpy(drinfo->oldname, qname->name, qname->len);
11452+ AuDbg("i%llu, %.*s\n",
11453+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11454+ drinfo->oldname);
11455+
11456+out:
11457+ AuTraceErr(err);
11458+ return err;
11459+}
11460+
11461+/* callers have to free the return value */
11462+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11463+{
11464+ struct au_drinfo *ret, *drinfo;
11465+ struct au_drinfo_fdata fdata;
11466+ int len;
11467+ loff_t pos;
11468+ ssize_t ssz;
11469+
11470+ ret = ERR_PTR(-EIO);
11471+ pos = 0;
11472+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11473+ if (unlikely(ssz != sizeof(fdata))) {
11474+ AuIOErr("ssz %zd, %u, %pD2\n",
11475+ ssz, (unsigned int)sizeof(fdata), file);
11476+ goto out;
11477+ }
11478+
11479+ fdata.magic = ntohl((__force __be32)fdata.magic);
11480+ switch (fdata.magic) {
11481+ case AUFS_DRINFO_MAGIC_V1:
11482+ break;
11483+ default:
11484+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11485+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11486+ goto out;
11487+ }
11488+
11489+ drinfo = &fdata.drinfo;
11490+ len = drinfo->oldnamelen;
11491+ if (!len) {
11492+ AuIOErr("broken drinfo %pD2\n", file);
11493+ goto out;
11494+ }
11495+
11496+ ret = NULL;
11497+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11498+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11499+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11500+ (unsigned long long)drinfo->ino,
11501+ (unsigned long long)h_ino, file);
11502+ goto out; /* success */
11503+ }
11504+
11505+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11506+ if (unlikely(!ret)) {
11507+ ret = ERR_PTR(-ENOMEM);
11508+ AuTraceErrPtr(ret);
11509+ goto out;
11510+ }
11511+
11512+ *ret = *drinfo;
11513+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11514+ if (unlikely(ssz != len)) {
9f237c51 11515+ au_kfree_rcu(ret);
8b6a4947
AM
11516+ ret = ERR_PTR(-EIO);
11517+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11518+ goto out;
11519+ }
11520+
11521+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11522+
11523+out:
11524+ return ret;
11525+}
11526+
11527+/* ---------------------------------------------------------------------- */
11528+
11529+/* in order to be revertible */
11530+struct au_drinfo_rev_elm {
11531+ int created;
11532+ struct dentry *info_dentry;
11533+ struct au_drinfo *info_last;
11534+};
11535+
11536+struct au_drinfo_rev {
11537+ unsigned char already;
11538+ aufs_bindex_t nelm;
11539+ struct au_drinfo_rev_elm elm[0];
11540+};
11541+
11542+/* todo: isn't it too large? */
11543+struct au_drinfo_store {
11544+ struct path h_ppath;
11545+ struct dentry *h_dentry;
11546+ struct au_drinfo_fdata *fdata;
11547+ char *infoname; /* inside of whname, just after PFX */
11548+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11549+ aufs_bindex_t btgt, btail;
11550+ unsigned char no_sio,
11551+ allocated, /* current size of *fdata */
11552+ infonamelen, /* room size for p */
acd2b654 11553+ whnamelen, /* length of the generated name */
8b6a4947
AM
11554+ renameback; /* renamed back */
11555+};
11556+
11557+/* on rename(2) error, the caller should revert it using @elm */
11558+static int au_drinfo_do_store(struct au_drinfo_store *w,
11559+ struct au_drinfo_rev_elm *elm)
11560+{
11561+ int err, len;
11562+ ssize_t ssz;
11563+ loff_t pos;
11564+ struct path infopath = {
11565+ .mnt = w->h_ppath.mnt
11566+ };
11567+ struct inode *h_dir, *h_inode, *delegated;
11568+ struct file *infofile;
11569+ struct qstr *qname;
11570+
11571+ AuDebugOn(elm
11572+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11573+
11574+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11575+ w->whnamelen);
11576+ AuTraceErrPtr(infopath.dentry);
11577+ if (IS_ERR(infopath.dentry)) {
11578+ err = PTR_ERR(infopath.dentry);
11579+ goto out;
11580+ }
11581+
11582+ err = 0;
11583+ h_dir = d_inode(w->h_ppath.dentry);
11584+ if (elm && d_is_negative(infopath.dentry)) {
11585+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11586+ AuTraceErr(err);
11587+ if (unlikely(err))
11588+ goto out_dput;
11589+ elm->created = 1;
11590+ elm->info_dentry = dget(infopath.dentry);
11591+ }
11592+
11593+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11594+ AuTraceErrPtr(infofile);
11595+ if (IS_ERR(infofile)) {
11596+ err = PTR_ERR(infofile);
11597+ goto out_dput;
11598+ }
11599+
11600+ h_inode = d_inode(infopath.dentry);
11601+ if (elm && i_size_read(h_inode)) {
11602+ h_inode = d_inode(w->h_dentry);
11603+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11604+ AuTraceErrPtr(elm->info_last);
11605+ if (IS_ERR(elm->info_last)) {
11606+ err = PTR_ERR(elm->info_last);
11607+ elm->info_last = NULL;
11608+ AuDebugOn(elm->info_dentry);
11609+ goto out_fput;
11610+ }
11611+ }
11612+
11613+ if (elm && w->renameback) {
11614+ delegated = NULL;
11615+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11616+ AuTraceErr(err);
11617+ if (unlikely(err == -EWOULDBLOCK))
11618+ iput(delegated);
11619+ goto out_fput;
11620+ }
11621+
11622+ pos = 0;
11623+ qname = &w->h_dentry->d_name;
11624+ len = sizeof(*w->fdata) + qname->len;
11625+ if (!elm)
11626+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11627+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11628+ if (ssz == len) {
11629+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11630+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11631+ goto out_fput; /* success */
11632+ } else {
11633+ err = -EIO;
11634+ if (ssz < 0)
11635+ err = ssz;
11636+ /* the caller should revert it using @elm */
11637+ }
11638+
11639+out_fput:
11640+ fput(infofile);
11641+out_dput:
11642+ dput(infopath.dentry);
11643+out:
11644+ AuTraceErr(err);
11645+ return err;
11646+}
11647+
11648+struct au_call_drinfo_do_store_args {
11649+ int *errp;
11650+ struct au_drinfo_store *w;
11651+ struct au_drinfo_rev_elm *elm;
11652+};
11653+
11654+static void au_call_drinfo_do_store(void *args)
11655+{
11656+ struct au_call_drinfo_do_store_args *a = args;
11657+
11658+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11659+}
11660+
11661+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11662+ struct au_drinfo_rev_elm *elm)
11663+{
11664+ int err, wkq_err;
11665+
11666+ if (w->no_sio)
11667+ err = au_drinfo_do_store(w, elm);
11668+ else {
11669+ struct au_call_drinfo_do_store_args a = {
11670+ .errp = &err,
11671+ .w = w,
11672+ .elm = elm
11673+ };
11674+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11675+ if (unlikely(wkq_err))
11676+ err = wkq_err;
11677+ }
11678+ AuTraceErr(err);
11679+
11680+ return err;
11681+}
11682+
11683+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11684+ aufs_bindex_t btgt)
11685+{
11686+ int err;
11687+
11688+ memset(w, 0, sizeof(*w));
11689+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11690+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11691+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11692+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11693+ w->btgt = btgt;
11694+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11695+
11696+ err = -ENOMEM;
11697+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11698+ if (unlikely(!w->fdata)) {
11699+ AuTraceErr(err);
11700+ goto out;
11701+ }
11702+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11703+ err = 0;
11704+
11705+out:
11706+ return err;
11707+}
11708+
11709+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11710+{
9f237c51 11711+ au_kfree_rcu(w->fdata);
8b6a4947
AM
11712+}
11713+
11714+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11715+ struct au_drinfo_store *w)
11716+{
11717+ struct au_drinfo_rev_elm *elm;
11718+ struct inode *h_dir, *delegated;
11719+ int err, nelm;
11720+ struct path infopath = {
11721+ .mnt = w->h_ppath.mnt
11722+ };
11723+
11724+ h_dir = d_inode(w->h_ppath.dentry);
11725+ IMustLock(h_dir);
11726+
11727+ err = 0;
11728+ elm = rev->elm;
11729+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11730+ AuDebugOn(elm->created && elm->info_last);
11731+ if (elm->created) {
11732+ AuDbg("here\n");
11733+ delegated = NULL;
11734+ infopath.dentry = elm->info_dentry;
11735+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11736+ !w->no_sio);
11737+ AuTraceErr(err);
11738+ if (unlikely(err == -EWOULDBLOCK))
11739+ iput(delegated);
11740+ dput(elm->info_dentry);
11741+ } else if (elm->info_last) {
11742+ AuDbg("here\n");
11743+ w->fdata->drinfo = *elm->info_last;
11744+ memcpy(w->fdata->drinfo.oldname,
11745+ elm->info_last->oldname,
11746+ elm->info_last->oldnamelen);
11747+ err = au_drinfo_store_sio(w, /*elm*/NULL);
9f237c51 11748+ au_kfree_rcu(elm->info_last);
8b6a4947
AM
11749+ }
11750+ if (unlikely(err))
11751+ AuIOErr("%d, %s\n", err, w->whname);
11752+ /* go on even if err */
11753+ }
11754+}
11755+
11756+/* caller has to call au_dr_rename_fin() later */
11757+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11758+ struct qstr *dst_name, void *_rev)
11759+{
11760+ int err, sz, nelm;
11761+ aufs_bindex_t bindex, btail;
11762+ struct au_drinfo_store work;
11763+ struct au_drinfo_rev *rev, **p;
11764+ struct au_drinfo_rev_elm *elm;
11765+ struct super_block *sb;
11766+ struct au_branch *br;
11767+ struct au_hinode *hdir;
11768+
11769+ err = au_drinfo_store_work_init(&work, btgt);
11770+ AuTraceErr(err);
11771+ if (unlikely(err))
11772+ goto out;
11773+
11774+ err = -ENOMEM;
11775+ btail = au_dbtaildir(dentry);
11776+ nelm = btail - btgt;
11777+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11778+ rev = kcalloc(1, sz, GFP_NOFS);
11779+ if (unlikely(!rev)) {
11780+ AuTraceErr(err);
11781+ goto out_args;
11782+ }
11783+ rev->nelm = nelm;
11784+ elm = rev->elm;
11785+ p = _rev;
11786+ *p = rev;
11787+
11788+ err = 0;
11789+ sb = dentry->d_sb;
11790+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11791+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11792+ hdir = au_hi(d_inode(dentry), btgt);
11793+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11794+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11795+ work.h_dentry = au_h_dptr(dentry, bindex);
11796+ if (!work.h_dentry)
11797+ continue;
11798+
11799+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11800+ &work.allocated);
11801+ AuTraceErr(err);
11802+ if (unlikely(err))
11803+ break;
11804+
11805+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11806+ br = au_sbr(sb, bindex);
11807+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11808+ work.whnamelen += au_drinfo_name(br, work.infoname,
11809+ work.infonamelen);
11810+ AuDbg("whname %.*s, i%llu, %.*s\n",
11811+ work.whnamelen, work.whname,
11812+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11813+ work.fdata->drinfo.oldnamelen,
11814+ work.fdata->drinfo.oldname);
11815+
11816+ err = au_drinfo_store_sio(&work, elm);
11817+ AuTraceErr(err);
11818+ if (unlikely(err))
11819+ break;
11820+ }
11821+ if (unlikely(err)) {
11822+ /* revert all drinfo */
11823+ au_drinfo_store_rev(rev, &work);
9f237c51 11824+ au_kfree_try_rcu(rev);
8b6a4947
AM
11825+ *p = NULL;
11826+ }
11827+ au_hn_inode_unlock(hdir);
11828+
11829+out_args:
11830+ au_drinfo_store_work_fin(&work);
11831+out:
11832+ return err;
11833+}
11834+
11835+/* ---------------------------------------------------------------------- */
11836+
11837+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11838+ struct qstr *dst_name, void *_rev)
11839+{
11840+ int err, already;
11841+ ino_t ino;
11842+ struct super_block *sb;
11843+ struct au_branch *br;
11844+ struct au_dr_br *dr;
11845+ struct dentry *h_dentry;
11846+ struct inode *h_inode;
11847+ struct au_dr_hino *ent;
11848+ struct au_drinfo_rev *rev, **p;
11849+
11850+ AuDbg("bindex %d\n", bindex);
11851+
11852+ err = -ENOMEM;
11853+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11854+ if (unlikely(!ent))
11855+ goto out;
11856+
11857+ sb = src->d_sb;
11858+ br = au_sbr(sb, bindex);
11859+ dr = &br->br_dirren;
11860+ h_dentry = au_h_dptr(src, bindex);
11861+ h_inode = d_inode(h_dentry);
11862+ ino = h_inode->i_ino;
11863+ ent->dr_h_ino = ino;
11864+ already = au_dr_hino_test_add(dr, ino, ent);
11865+ AuDbg("b%d, hi%llu, already %d\n",
11866+ bindex, (unsigned long long)ino, already);
11867+
11868+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11869+ AuTraceErr(err);
11870+ if (!err) {
11871+ p = _rev;
11872+ rev = *p;
11873+ rev->already = already;
11874+ goto out; /* success */
11875+ }
11876+
11877+ /* revert */
11878+ if (!already)
11879+ au_dr_hino_del(dr, ent);
9f237c51 11880+ au_kfree_rcu(ent);
8b6a4947
AM
11881+
11882+out:
11883+ AuTraceErr(err);
11884+ return err;
11885+}
11886+
11887+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11888+{
11889+ struct au_drinfo_rev *rev;
11890+ struct au_drinfo_rev_elm *elm;
11891+ int nelm;
11892+
11893+ rev = _rev;
11894+ elm = rev->elm;
11895+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11896+ dput(elm->info_dentry);
9f237c51 11897+ au_kfree_rcu(elm->info_last);
8b6a4947 11898+ }
9f237c51 11899+ au_kfree_try_rcu(rev);
8b6a4947
AM
11900+}
11901+
11902+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11903+{
11904+ int err;
11905+ struct au_drinfo_store work;
11906+ struct au_drinfo_rev *rev = _rev;
11907+ struct super_block *sb;
11908+ struct au_branch *br;
11909+ struct inode *h_inode;
11910+ struct au_dr_br *dr;
11911+ struct au_dr_hino *ent;
11912+
11913+ err = au_drinfo_store_work_init(&work, btgt);
11914+ if (unlikely(err))
11915+ goto out;
11916+
11917+ sb = src->d_sb;
11918+ br = au_sbr(sb, btgt);
11919+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11920+ work.h_ppath.mnt = au_br_mnt(br);
11921+ au_drinfo_store_rev(rev, &work);
11922+ au_drinfo_store_work_fin(&work);
11923+ if (rev->already)
11924+ goto out;
11925+
11926+ dr = &br->br_dirren;
11927+ h_inode = d_inode(work.h_ppath.dentry);
11928+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11929+ BUG_ON(!ent);
11930+ au_dr_hino_del(dr, ent);
9f237c51 11931+ au_kfree_rcu(ent);
8b6a4947
AM
11932+
11933+out:
9f237c51 11934+ au_kfree_try_rcu(rev);
8b6a4947
AM
11935+ if (unlikely(err))
11936+ pr_err("failed to remove dirren info\n");
11937+}
11938+
11939+/* ---------------------------------------------------------------------- */
11940+
11941+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11942+ char *whname, int whnamelen,
11943+ struct dentry **info_dentry)
11944+{
11945+ struct au_drinfo *drinfo;
11946+ struct file *f;
11947+ struct inode *h_dir;
11948+ struct path infopath;
11949+ int unlocked;
11950+
11951+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11952+
11953+ *info_dentry = NULL;
11954+ drinfo = NULL;
11955+ unlocked = 0;
11956+ h_dir = d_inode(h_ppath->dentry);
be118d29 11957+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947
AM
11958+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11959+ whnamelen);
11960+ if (IS_ERR(infopath.dentry)) {
11961+ drinfo = (void *)infopath.dentry;
11962+ goto out;
11963+ }
11964+
11965+ if (d_is_negative(infopath.dentry))
11966+ goto out_dput; /* success */
11967+
11968+ infopath.mnt = h_ppath->mnt;
11969+ f = vfsub_dentry_open(&infopath, O_RDONLY);
11970+ inode_unlock_shared(h_dir);
11971+ unlocked = 1;
11972+ if (IS_ERR(f)) {
11973+ drinfo = (void *)f;
11974+ goto out_dput;
11975+ }
11976+
11977+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11978+ if (IS_ERR_OR_NULL(drinfo))
11979+ goto out_fput;
11980+
11981+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11982+ *info_dentry = dget(infopath.dentry); /* keep it alive */
11983+
11984+out_fput:
11985+ fput(f);
11986+out_dput:
11987+ dput(infopath.dentry);
11988+out:
11989+ if (!unlocked)
11990+ inode_unlock_shared(h_dir);
11991+ AuTraceErrPtr(drinfo);
11992+ return drinfo;
11993+}
11994+
11995+struct au_drinfo_do_load_args {
11996+ struct au_drinfo **drinfop;
11997+ struct path *h_ppath;
11998+ char *whname;
11999+ int whnamelen;
12000+ struct dentry **info_dentry;
12001+};
12002+
12003+static void au_call_drinfo_do_load(void *args)
12004+{
12005+ struct au_drinfo_do_load_args *a = args;
12006+
12007+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12008+ a->info_dentry);
12009+}
12010+
12011+struct au_drinfo_load {
12012+ struct path h_ppath;
12013+ struct qstr *qname;
12014+ unsigned char no_sio;
12015+
12016+ aufs_bindex_t ninfo;
12017+ struct au_drinfo **drinfo;
12018+};
12019+
12020+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12021+ struct au_branch *br)
12022+{
12023+ int err, wkq_err, whnamelen, e;
12024+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12025+ = AUFS_WH_DR_INFO_PFX;
12026+ struct au_drinfo *drinfo;
12027+ struct qstr oldname;
12028+ struct inode *h_dir, *delegated;
12029+ struct dentry *info_dentry;
12030+ struct path infopath;
12031+
12032+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12033+ whnamelen += au_drinfo_name(br, whname + whnamelen,
12034+ sizeof(whname) - whnamelen);
12035+ if (w->no_sio)
12036+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12037+ &info_dentry);
12038+ else {
12039+ struct au_drinfo_do_load_args args = {
12040+ .drinfop = &drinfo,
12041+ .h_ppath = &w->h_ppath,
12042+ .whname = whname,
12043+ .whnamelen = whnamelen,
12044+ .info_dentry = &info_dentry
12045+ };
12046+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12047+ if (unlikely(wkq_err))
12048+ drinfo = ERR_PTR(wkq_err);
12049+ }
12050+ err = PTR_ERR(drinfo);
12051+ if (IS_ERR_OR_NULL(drinfo))
12052+ goto out;
12053+
12054+ err = 0;
12055+ oldname.len = drinfo->oldnamelen;
12056+ oldname.name = drinfo->oldname;
12057+ if (au_qstreq(w->qname, &oldname)) {
12058+ /* the name is renamed back */
9f237c51 12059+ au_kfree_rcu(drinfo);
8b6a4947
AM
12060+ drinfo = NULL;
12061+
12062+ infopath.dentry = info_dentry;
12063+ infopath.mnt = w->h_ppath.mnt;
12064+ h_dir = d_inode(w->h_ppath.dentry);
12065+ delegated = NULL;
12066+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12067+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12068+ inode_unlock(h_dir);
12069+ if (unlikely(e))
12070+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12071+ if (unlikely(e == -EWOULDBLOCK))
12072+ iput(delegated);
12073+ }
9f237c51 12074+ au_kfree_rcu(w->drinfo[bindex]);
8b6a4947
AM
12075+ w->drinfo[bindex] = drinfo;
12076+ dput(info_dentry);
12077+
12078+out:
12079+ AuTraceErr(err);
12080+ return err;
12081+}
12082+
12083+/* ---------------------------------------------------------------------- */
12084+
12085+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12086+{
12087+ struct au_drinfo **p = drinfo;
12088+
12089+ while (n-- > 0)
9f237c51
AM
12090+ au_kfree_rcu(*drinfo++);
12091+ au_kfree_try_rcu(p);
8b6a4947
AM
12092+}
12093+
12094+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12095+ aufs_bindex_t btgt)
12096+{
12097+ int err, ninfo;
12098+ struct au_drinfo_load w;
12099+ aufs_bindex_t bindex, bbot;
12100+ struct au_branch *br;
12101+ struct inode *h_dir;
12102+ struct au_dr_hino *ent;
12103+ struct super_block *sb;
12104+
12105+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12106+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12107+ AuLNPair(&lkup->whname), btgt);
12108+
12109+ sb = dentry->d_sb;
12110+ bbot = au_sbbot(sb);
12111+ w.ninfo = bbot + 1;
12112+ if (!lkup->dirren.drinfo) {
12113+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12114+ sizeof(*lkup->dirren.drinfo),
12115+ GFP_NOFS);
12116+ if (unlikely(!lkup->dirren.drinfo)) {
12117+ err = -ENOMEM;
12118+ goto out;
12119+ }
12120+ lkup->dirren.ninfo = w.ninfo;
12121+ }
12122+ w.drinfo = lkup->dirren.drinfo;
12123+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12124+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12125+ AuDebugOn(!w.h_ppath.dentry);
12126+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12127+ w.qname = &dentry->d_name;
12128+
12129+ ninfo = 0;
12130+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12131+ br = au_sbr(sb, bindex);
12132+ err = au_drinfo_load(&w, bindex, br);
12133+ if (unlikely(err))
12134+ goto out_free;
12135+ if (w.drinfo[bindex])
12136+ ninfo++;
12137+ }
12138+ if (!ninfo) {
12139+ br = au_sbr(sb, btgt);
12140+ h_dir = d_inode(w.h_ppath.dentry);
12141+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12142+ AuDebugOn(!ent);
12143+ au_dr_hino_del(&br->br_dirren, ent);
9f237c51 12144+ au_kfree_rcu(ent);
8b6a4947
AM
12145+ }
12146+ goto out; /* success */
12147+
12148+out_free:
12149+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12150+ lkup->dirren.ninfo = 0;
12151+ lkup->dirren.drinfo = NULL;
12152+out:
12153+ AuTraceErr(err);
12154+ return err;
12155+}
12156+
12157+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12158+{
12159+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12160+}
12161+
12162+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12163+{
12164+ int err;
12165+ struct au_drinfo *drinfo;
12166+
12167+ err = 0;
12168+ if (!lkup->dirren.drinfo)
12169+ goto out;
12170+ AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12171+ drinfo = lkup->dirren.drinfo[btgt + 1];
12172+ if (!drinfo)
12173+ goto out;
12174+
9f237c51 12175+ au_kfree_try_rcu(lkup->whname.name);
8b6a4947
AM
12176+ lkup->whname.name = NULL;
12177+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12178+ lkup->dirren.dr_name.name = drinfo->oldname;
12179+ lkup->name = &lkup->dirren.dr_name;
12180+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12181+ if (!err)
12182+ AuDbg("name %.*s, whname %.*s, b%d\n",
12183+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12184+ btgt);
12185+
12186+out:
12187+ AuTraceErr(err);
12188+ return err;
12189+}
12190+
12191+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12192+ ino_t h_ino)
12193+{
12194+ int match;
12195+ struct au_drinfo *drinfo;
12196+
12197+ match = 1;
12198+ if (!lkup->dirren.drinfo)
12199+ goto out;
12200+ AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12201+ drinfo = lkup->dirren.drinfo[bindex + 1];
12202+ if (!drinfo)
12203+ goto out;
12204+
12205+ match = (drinfo->ino == h_ino);
12206+ AuDbg("match %d\n", match);
12207+
12208+out:
12209+ return match;
12210+}
12211+
12212+/* ---------------------------------------------------------------------- */
12213+
12214+int au_dr_opt_set(struct super_block *sb)
12215+{
12216+ int err;
12217+ aufs_bindex_t bindex, bbot;
12218+ struct au_branch *br;
12219+
12220+ err = 0;
12221+ bbot = au_sbbot(sb);
12222+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12223+ br = au_sbr(sb, bindex);
12224+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12225+ }
12226+
12227+ return err;
12228+}
12229+
12230+int au_dr_opt_flush(struct super_block *sb)
12231+{
12232+ int err;
12233+ aufs_bindex_t bindex, bbot;
12234+ struct au_branch *br;
12235+
12236+ err = 0;
12237+ bbot = au_sbbot(sb);
12238+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12239+ br = au_sbr(sb, bindex);
12240+ if (au_br_writable(br->br_perm))
12241+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12242+ }
12243+
12244+ return err;
12245+}
12246+
12247+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12248+{
12249+ int err;
12250+ aufs_bindex_t bindex, bbot;
12251+ struct au_branch *br;
12252+
12253+ err = 0;
12254+ if (!no_flush) {
12255+ err = au_dr_opt_flush(sb);
12256+ if (unlikely(err))
12257+ goto out;
12258+ }
12259+
12260+ bbot = au_sbbot(sb);
12261+ for (bindex = 0; bindex <= bbot; bindex++) {
12262+ br = au_sbr(sb, bindex);
12263+ au_dr_hino_free(&br->br_dirren);
12264+ }
12265+
12266+out:
12267+ return err;
12268+}
12269diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
eca34b5c
AM
12270--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
12271+++ linux/fs/aufs/dirren.h 2019-07-11 15:42:14.462237786 +0200
062440b3
AM
12272@@ -0,0 +1,140 @@
12273+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 12274+/*
ba1aed25 12275+ * Copyright (C) 2017-2019 Junjiro R. Okajima
8b6a4947
AM
12276+ *
12277+ * This program, aufs is free software; you can redistribute it and/or modify
12278+ * it under the terms of the GNU General Public License as published by
12279+ * the Free Software Foundation; either version 2 of the License, or
12280+ * (at your option) any later version.
12281+ *
12282+ * This program is distributed in the hope that it will be useful,
12283+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12284+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12285+ * GNU General Public License for more details.
12286+ *
12287+ * You should have received a copy of the GNU General Public License
12288+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12289+ */
12290+
12291+/*
12292+ * renamed dir info
12293+ */
12294+
12295+#ifndef __AUFS_DIRREN_H__
12296+#define __AUFS_DIRREN_H__
12297+
12298+#ifdef __KERNEL__
12299+
12300+#include <linux/dcache.h>
12301+#include <linux/statfs.h>
12302+#include <linux/uuid.h>
12303+#include "hbl.h"
12304+
12305+#define AuDirren_NHASH 100
12306+
12307+#ifdef CONFIG_AUFS_DIRREN
12308+enum au_brid_type {
12309+ AuBrid_Unset,
12310+ AuBrid_UUID,
12311+ AuBrid_FSID,
12312+ AuBrid_DEV
12313+};
12314+
12315+struct au_dr_brid {
12316+ enum au_brid_type type;
12317+ union {
12318+ uuid_t uuid; /* unimplemented yet */
12319+ fsid_t fsid;
12320+ dev_t dev;
12321+ };
12322+};
12323+
12324+/* 20 is the max digits length of ulong 64 */
12325+/* brid-type "_" uuid "_" inum */
12326+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12327+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12328+
12329+struct au_dr_hino {
12330+ struct hlist_bl_node dr_hnode;
12331+ ino_t dr_h_ino;
12332+};
12333+
12334+struct au_dr_br {
12335+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12336+ struct au_dr_brid dr_brid;
12337+};
12338+
12339+struct au_dr_lookup {
12340+ /* dr_name is pointed by struct au_do_lookup_args.name */
12341+ struct qstr dr_name; /* subset of dr_info */
12342+ aufs_bindex_t ninfo;
12343+ struct au_drinfo **drinfo;
12344+};
12345+#else
12346+struct au_dr_hino;
12347+/* empty */
12348+struct au_dr_br { };
12349+struct au_dr_lookup { };
12350+#endif
12351+
12352+/* ---------------------------------------------------------------------- */
12353+
12354+struct au_branch;
12355+struct au_do_lookup_args;
12356+struct au_hinode;
12357+#ifdef CONFIG_AUFS_DIRREN
12358+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12359+ struct au_dr_hino *add_ent);
12360+void au_dr_hino_free(struct au_dr_br *dr);
12361+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12362+ const struct path *path);
12363+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12364+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12365+ struct qstr *dst_name, void *_rev);
12366+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12367+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12368+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12369+ aufs_bindex_t bindex);
12370+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12371+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12372+ ino_t h_ino);
12373+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12374+int au_dr_opt_set(struct super_block *sb);
12375+int au_dr_opt_flush(struct super_block *sb);
12376+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12377+#else
12378+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12379+ struct au_dr_hino *add_ent);
12380+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12381+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12382+ const struct path *path);
12383+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12384+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12385+ struct qstr *dst_name, void *_rev);
12386+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12387+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12388+ void *rev);
12389+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12390+ aufs_bindex_t bindex);
12391+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12392+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12393+ aufs_bindex_t bindex, ino_t h_ino);
12394+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12395+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12396+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12397+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12398+#endif
12399+
12400+/* ---------------------------------------------------------------------- */
12401+
12402+#ifdef CONFIG_AUFS_DIRREN
12403+static inline int au_dr_ihash(ino_t h_ino)
12404+{
12405+ return h_ino % AuDirren_NHASH;
12406+}
12407+#else
12408+AuStubInt0(au_dr_ihash, ino_t h_ino);
12409+#endif
12410+
12411+#endif /* __KERNEL__ */
12412+#endif /* __AUFS_DIRREN_H__ */
7f207e10 12413diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
eca34b5c
AM
12414--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
12415+++ linux/fs/aufs/dynop.c 2019-07-11 15:42:14.465571210 +0200
fbc438ed 12416@@ -0,0 +1,367 @@
cd7a4cd9 12417+// SPDX-License-Identifier: GPL-2.0
1facf9fc 12418+/*
ba1aed25 12419+ * Copyright (C) 2010-2019 Junjiro R. Okajima
1facf9fc 12420+ *
12421+ * This program, aufs is free software; you can redistribute it and/or modify
12422+ * it under the terms of the GNU General Public License as published by
12423+ * the Free Software Foundation; either version 2 of the License, or
12424+ * (at your option) any later version.
dece6358
AM
12425+ *
12426+ * This program is distributed in the hope that it will be useful,
12427+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12428+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12429+ * GNU General Public License for more details.
12430+ *
12431+ * You should have received a copy of the GNU General Public License
523b37e3 12432+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12433+ */
12434+
12435+/*
4a4d8108 12436+ * dynamically customizable operations for regular files
1facf9fc 12437+ */
12438+
1facf9fc 12439+#include "aufs.h"
12440+
4a4d8108 12441+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12442+
4a4d8108
AM
12443+/*
12444+ * How large will these lists be?
12445+ * Usually just a few elements, 20-30 at most for each, I guess.
12446+ */
8b6a4947 12447+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12448+
8b6a4947
AM
12449+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12450+ const void *h_op)
1facf9fc 12451+{
4a4d8108 12452+ struct au_dykey *key, *tmp;
8b6a4947 12453+ struct hlist_bl_node *pos;
1facf9fc 12454+
4a4d8108 12455+ key = NULL;
8b6a4947
AM
12456+ hlist_bl_lock(hbl);
12457+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12458+ if (tmp->dk_op.dy_hop == h_op) {
83b672a5
AM
12459+ if (kref_get_unless_zero(&tmp->dk_kref))
12460+ key = tmp;
4a4d8108
AM
12461+ break;
12462+ }
8b6a4947 12463+ hlist_bl_unlock(hbl);
4a4d8108
AM
12464+
12465+ return key;
1facf9fc 12466+}
12467+
4a4d8108 12468+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12469+{
4a4d8108
AM
12470+ struct au_dykey **k, *found;
12471+ const void *h_op = key->dk_op.dy_hop;
12472+ int i;
1facf9fc 12473+
4a4d8108
AM
12474+ found = NULL;
12475+ k = br->br_dykey;
12476+ for (i = 0; i < AuBrDynOp; i++)
12477+ if (k[i]) {
12478+ if (k[i]->dk_op.dy_hop == h_op) {
12479+ found = k[i];
12480+ break;
12481+ }
12482+ } else
12483+ break;
12484+ if (!found) {
12485+ spin_lock(&br->br_dykey_lock);
12486+ for (; i < AuBrDynOp; i++)
12487+ if (k[i]) {
12488+ if (k[i]->dk_op.dy_hop == h_op) {
12489+ found = k[i];
12490+ break;
12491+ }
12492+ } else {
12493+ k[i] = key;
12494+ break;
12495+ }
12496+ spin_unlock(&br->br_dykey_lock);
12497+ BUG_ON(i == AuBrDynOp); /* expand the array */
12498+ }
12499+
12500+ return found;
1facf9fc 12501+}
12502+
4a4d8108 12503+/* kref_get() if @key is already added */
8b6a4947 12504+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12505+{
12506+ struct au_dykey *tmp, *found;
8b6a4947 12507+ struct hlist_bl_node *pos;
4a4d8108 12508+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12509+
4a4d8108 12510+ found = NULL;
8b6a4947
AM
12511+ hlist_bl_lock(hbl);
12512+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12513+ if (tmp->dk_op.dy_hop == h_op) {
83b672a5
AM
12514+ if (kref_get_unless_zero(&tmp->dk_kref))
12515+ found = tmp;
4a4d8108
AM
12516+ break;
12517+ }
12518+ if (!found)
8b6a4947
AM
12519+ hlist_bl_add_head(&key->dk_hnode, hbl);
12520+ hlist_bl_unlock(hbl);
1facf9fc 12521+
4a4d8108
AM
12522+ if (!found)
12523+ DyPrSym(key);
12524+ return found;
12525+}
12526+
12527+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12528+{
4a4d8108
AM
12529+ struct au_dykey *key;
12530+
12531+ key = container_of(rcu, struct au_dykey, dk_rcu);
12532+ DyPrSym(key);
83b672a5 12533+ kfree(key);
1facf9fc 12534+}
12535+
4a4d8108
AM
12536+static void dy_free(struct kref *kref)
12537+{
12538+ struct au_dykey *key;
8b6a4947 12539+ struct hlist_bl_head *hbl;
1facf9fc 12540+
4a4d8108 12541+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12542+ hbl = dynop + key->dk_op.dy_type;
12543+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12544+ call_rcu(&key->dk_rcu, dy_free_rcu);
12545+}
12546+
12547+void au_dy_put(struct au_dykey *key)
1facf9fc 12548+{
4a4d8108
AM
12549+ kref_put(&key->dk_kref, dy_free);
12550+}
1facf9fc 12551+
4a4d8108
AM
12552+/* ---------------------------------------------------------------------- */
12553+
12554+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12555+
12556+#ifdef CONFIG_AUFS_DEBUG
12557+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12558+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12559+#else
12560+#define DyDbgDeclare(cnt) do {} while (0)
12561+#define DyDbgInc(cnt) do {} while (0)
12562+#endif
12563+
12564+#define DySet(func, dst, src, h_op, h_sb) do { \
12565+ DyDbgInc(cnt); \
12566+ if (h_op->func) { \
12567+ if (src.func) \
12568+ dst.func = src.func; \
12569+ else \
12570+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12571+ } \
12572+} while (0)
12573+
12574+#define DySetForce(func, dst, src) do { \
12575+ AuDebugOn(!src.func); \
12576+ DyDbgInc(cnt); \
12577+ dst.func = src.func; \
12578+} while (0)
12579+
12580+#define DySetAop(func) \
12581+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12582+#define DySetAopForce(func) \
12583+ DySetForce(func, dyaop->da_op, aufs_aop)
12584+
12585+static void dy_aop(struct au_dykey *key, const void *h_op,
12586+ struct super_block *h_sb __maybe_unused)
12587+{
12588+ struct au_dyaop *dyaop = (void *)key;
12589+ const struct address_space_operations *h_aop = h_op;
12590+ DyDbgDeclare(cnt);
12591+
12592+ AuDbg("%s\n", au_sbtype(h_sb));
12593+
12594+ DySetAop(writepage);
12595+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12596+ DySetAop(writepages);
12597+ DySetAop(set_page_dirty);
12598+ DySetAop(readpages);
12599+ DySetAop(write_begin);
12600+ DySetAop(write_end);
12601+ DySetAop(bmap);
12602+ DySetAop(invalidatepage);
12603+ DySetAop(releasepage);
027c5e7a 12604+ DySetAop(freepage);
7e9cd9fe 12605+ /* this one will be changed according to an aufs mount option */
4a4d8108 12606+ DySetAop(direct_IO);
4a4d8108 12607+ DySetAop(migratepage);
e2f27e51
AM
12608+ DySetAop(isolate_page);
12609+ DySetAop(putback_page);
4a4d8108
AM
12610+ DySetAop(launder_page);
12611+ DySetAop(is_partially_uptodate);
392086de 12612+ DySetAop(is_dirty_writeback);
4a4d8108 12613+ DySetAop(error_remove_page);
b4510431
AM
12614+ DySetAop(swap_activate);
12615+ DySetAop(swap_deactivate);
4a4d8108
AM
12616+
12617+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12618+}
12619+
4a4d8108
AM
12620+/* ---------------------------------------------------------------------- */
12621+
12622+static void dy_bug(struct kref *kref)
12623+{
12624+ BUG();
12625+}
12626+
12627+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12628+{
12629+ struct au_dykey *key, *old;
8b6a4947 12630+ struct hlist_bl_head *hbl;
b752ccd1 12631+ struct op {
4a4d8108 12632+ unsigned int sz;
b752ccd1
AM
12633+ void (*set)(struct au_dykey *key, const void *h_op,
12634+ struct super_block *h_sb __maybe_unused);
12635+ };
12636+ static const struct op a[] = {
4a4d8108
AM
12637+ [AuDy_AOP] = {
12638+ .sz = sizeof(struct au_dyaop),
b752ccd1 12639+ .set = dy_aop
4a4d8108 12640+ }
b752ccd1
AM
12641+ };
12642+ const struct op *p;
4a4d8108 12643+
8b6a4947
AM
12644+ hbl = dynop + op->dy_type;
12645+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12646+ if (key)
12647+ goto out_add; /* success */
12648+
12649+ p = a + op->dy_type;
12650+ key = kzalloc(p->sz, GFP_NOFS);
12651+ if (unlikely(!key)) {
12652+ key = ERR_PTR(-ENOMEM);
12653+ goto out;
12654+ }
12655+
12656+ key->dk_op.dy_hop = op->dy_hop;
12657+ kref_init(&key->dk_kref);
86dc4139 12658+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12659+ old = dy_gadd(hbl, key);
4a4d8108 12660+ if (old) {
9f237c51 12661+ au_kfree_rcu(key);
4a4d8108
AM
12662+ key = old;
12663+ }
12664+
12665+out_add:
12666+ old = dy_bradd(br, key);
12667+ if (old)
12668+ /* its ref-count should never be zero here */
12669+ kref_put(&key->dk_kref, dy_bug);
12670+out:
12671+ return key;
12672+}
12673+
12674+/* ---------------------------------------------------------------------- */
12675+/*
acd2b654 12676+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
c1595e42 12677+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12678+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12679+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12680+ * See the aufs manual in detail.
4a4d8108
AM
12681+ */
12682+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12683+{
7e9cd9fe 12684+ if (!do_dx)
4a4d8108 12685+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12686+ else
4a4d8108 12687+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12688+}
12689+
12690+static struct au_dyaop *dy_aget(struct au_branch *br,
12691+ const struct address_space_operations *h_aop,
12692+ int do_dx)
12693+{
12694+ struct au_dyaop *dyaop;
12695+ struct au_dynop op;
12696+
12697+ op.dy_type = AuDy_AOP;
12698+ op.dy_haop = h_aop;
12699+ dyaop = (void *)dy_get(&op, br);
12700+ if (IS_ERR(dyaop))
12701+ goto out;
12702+ dy_adx(dyaop, do_dx);
12703+
12704+out:
12705+ return dyaop;
12706+}
12707+
12708+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12709+ struct inode *h_inode)
12710+{
12711+ int err, do_dx;
12712+ struct super_block *sb;
12713+ struct au_branch *br;
12714+ struct au_dyaop *dyaop;
12715+
12716+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12717+ IiMustWriteLock(inode);
12718+
12719+ sb = inode->i_sb;
12720+ br = au_sbr(sb, bindex);
12721+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12722+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12723+ err = PTR_ERR(dyaop);
12724+ if (IS_ERR(dyaop))
12725+ /* unnecessary to call dy_fput() */
12726+ goto out;
12727+
12728+ err = 0;
12729+ inode->i_mapping->a_ops = &dyaop->da_op;
12730+
12731+out:
12732+ return err;
12733+}
12734+
b752ccd1
AM
12735+/*
12736+ * Is it safe to replace a_ops during the inode/file is in operation?
12737+ * Yes, I hope so.
12738+ */
12739+int au_dy_irefresh(struct inode *inode)
12740+{
12741+ int err;
5afbbe0d 12742+ aufs_bindex_t btop;
b752ccd1
AM
12743+ struct inode *h_inode;
12744+
12745+ err = 0;
12746+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12747+ btop = au_ibtop(inode);
12748+ h_inode = au_h_iptr(inode, btop);
12749+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12750+ }
12751+ return err;
12752+}
12753+
4a4d8108
AM
12754+void au_dy_arefresh(int do_dx)
12755+{
8b6a4947
AM
12756+ struct hlist_bl_head *hbl;
12757+ struct hlist_bl_node *pos;
4a4d8108
AM
12758+ struct au_dykey *key;
12759+
8b6a4947
AM
12760+ hbl = dynop + AuDy_AOP;
12761+ hlist_bl_lock(hbl);
12762+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12763+ dy_adx((void *)key, do_dx);
8b6a4947 12764+ hlist_bl_unlock(hbl);
4a4d8108
AM
12765+}
12766+
4a4d8108
AM
12767+/* ---------------------------------------------------------------------- */
12768+
12769+void __init au_dy_init(void)
12770+{
12771+ int i;
12772+
4a4d8108 12773+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12774+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12775+}
12776+
12777+void au_dy_fin(void)
12778+{
12779+ int i;
12780+
12781+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12782+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12783+}
7f207e10 12784diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
eca34b5c
AM
12785--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
12786+++ linux/fs/aufs/dynop.h 2019-07-11 15:42:14.465571210 +0200
fbc438ed 12787@@ -0,0 +1,77 @@
062440b3 12788+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12789+/*
ba1aed25 12790+ * Copyright (C) 2010-2019 Junjiro R. Okajima
4a4d8108
AM
12791+ *
12792+ * This program, aufs is free software; you can redistribute it and/or modify
12793+ * it under the terms of the GNU General Public License as published by
12794+ * the Free Software Foundation; either version 2 of the License, or
12795+ * (at your option) any later version.
12796+ *
12797+ * This program is distributed in the hope that it will be useful,
12798+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12799+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12800+ * GNU General Public License for more details.
12801+ *
12802+ * You should have received a copy of the GNU General Public License
523b37e3 12803+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12804+ */
12805+
12806+/*
12807+ * dynamically customizable operations (for regular files only)
12808+ */
12809+
12810+#ifndef __AUFS_DYNOP_H__
12811+#define __AUFS_DYNOP_H__
12812+
12813+#ifdef __KERNEL__
12814+
7e9cd9fe
AM
12815+#include <linux/fs.h>
12816+#include <linux/kref.h>
4a4d8108 12817+
2cbb1c4b 12818+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12819+
12820+struct au_dynop {
12821+ int dy_type;
12822+ union {
12823+ const void *dy_hop;
12824+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12825+ };
12826+};
12827+
12828+struct au_dykey {
12829+ union {
8b6a4947 12830+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12831+ struct rcu_head dk_rcu;
12832+ };
12833+ struct au_dynop dk_op;
12834+
12835+ /*
12836+ * during I am in the branch local array, kref is gotten. when the
12837+ * branch is removed, kref is put.
12838+ */
12839+ struct kref dk_kref;
12840+};
12841+
12842+/* stop unioning since their sizes are very different from each other */
12843+struct au_dyaop {
12844+ struct au_dykey da_key;
12845+ struct address_space_operations da_op; /* not const */
4a4d8108 12846+};
fbc438ed
JR
12847+/* make sure that 'struct au_dykey *' can be any type */
12848+static_assert(!offsetof(struct au_dyaop, da_key));
4a4d8108 12849+
4a4d8108
AM
12850+/* ---------------------------------------------------------------------- */
12851+
12852+/* dynop.c */
12853+struct au_branch;
12854+void au_dy_put(struct au_dykey *key);
12855+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12856+ struct inode *h_inode);
b752ccd1 12857+int au_dy_irefresh(struct inode *inode);
4a4d8108 12858+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12859+
12860+void __init au_dy_init(void);
12861+void au_dy_fin(void);
12862+
4a4d8108
AM
12863+#endif /* __KERNEL__ */
12864+#endif /* __AUFS_DYNOP_H__ */
7f207e10 12865diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
eca34b5c
AM
12866--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
12867+++ linux/fs/aufs/export.c 2019-07-11 15:42:14.465571210 +0200
062440b3 12868@@ -0,0 +1,838 @@
cd7a4cd9 12869+// SPDX-License-Identifier: GPL-2.0
4a4d8108 12870+/*
ba1aed25 12871+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
12872+ *
12873+ * This program, aufs is free software; you can redistribute it and/or modify
12874+ * it under the terms of the GNU General Public License as published by
12875+ * the Free Software Foundation; either version 2 of the License, or
12876+ * (at your option) any later version.
12877+ *
12878+ * This program is distributed in the hope that it will be useful,
12879+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12880+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12881+ * GNU General Public License for more details.
12882+ *
12883+ * You should have received a copy of the GNU General Public License
523b37e3 12884+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12885+ */
12886+
12887+/*
12888+ * export via nfs
12889+ */
12890+
12891+#include <linux/exportfs.h>
7eafdf33 12892+#include <linux/fs_struct.h>
4a4d8108
AM
12893+#include <linux/namei.h>
12894+#include <linux/nsproxy.h>
12895+#include <linux/random.h>
12896+#include <linux/writeback.h>
12897+#include "aufs.h"
12898+
12899+union conv {
12900+#ifdef CONFIG_AUFS_INO_T_64
12901+ __u32 a[2];
12902+#else
12903+ __u32 a[1];
12904+#endif
12905+ ino_t ino;
12906+};
12907+
12908+static ino_t decode_ino(__u32 *a)
12909+{
12910+ union conv u;
12911+
12912+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12913+ u.a[0] = a[0];
12914+#ifdef CONFIG_AUFS_INO_T_64
12915+ u.a[1] = a[1];
12916+#endif
12917+ return u.ino;
12918+}
12919+
12920+static void encode_ino(__u32 *a, ino_t ino)
12921+{
12922+ union conv u;
12923+
12924+ u.ino = ino;
12925+ a[0] = u.a[0];
12926+#ifdef CONFIG_AUFS_INO_T_64
12927+ a[1] = u.a[1];
12928+#endif
12929+}
12930+
12931+/* NFS file handle */
12932+enum {
12933+ Fh_br_id,
12934+ Fh_sigen,
12935+#ifdef CONFIG_AUFS_INO_T_64
12936+ /* support 64bit inode number */
12937+ Fh_ino1,
12938+ Fh_ino2,
12939+ Fh_dir_ino1,
12940+ Fh_dir_ino2,
12941+#else
12942+ Fh_ino1,
12943+ Fh_dir_ino1,
12944+#endif
12945+ Fh_igen,
12946+ Fh_h_type,
12947+ Fh_tail,
12948+
12949+ Fh_ino = Fh_ino1,
12950+ Fh_dir_ino = Fh_dir_ino1
12951+};
12952+
12953+static int au_test_anon(struct dentry *dentry)
12954+{
027c5e7a 12955+ /* note: read d_flags without d_lock */
4a4d8108
AM
12956+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12957+}
12958+
a2a7ad62
AM
12959+int au_test_nfsd(void)
12960+{
12961+ int ret;
12962+ struct task_struct *tsk = current;
12963+ char comm[sizeof(tsk->comm)];
12964+
12965+ ret = 0;
12966+ if (tsk->flags & PF_KTHREAD) {
12967+ get_task_comm(comm, tsk);
12968+ ret = !strcmp(comm, "nfsd");
12969+ }
12970+
12971+ return ret;
12972+}
12973+
4a4d8108
AM
12974+/* ---------------------------------------------------------------------- */
12975+/* inode generation external table */
12976+
b752ccd1 12977+void au_xigen_inc(struct inode *inode)
4a4d8108 12978+{
4a4d8108
AM
12979+ loff_t pos;
12980+ ssize_t sz;
12981+ __u32 igen;
12982+ struct super_block *sb;
12983+ struct au_sbinfo *sbinfo;
12984+
4a4d8108 12985+ sb = inode->i_sb;
b752ccd1 12986+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 12987+
b752ccd1 12988+ sbinfo = au_sbi(sb);
1facf9fc 12989+ pos = inode->i_ino;
12990+ pos *= sizeof(igen);
12991+ igen = inode->i_generation + 1;
1facf9fc 12992+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
12993+ sizeof(igen), &pos);
12994+ if (sz == sizeof(igen))
b752ccd1 12995+ return; /* success */
1facf9fc 12996+
b752ccd1 12997+ if (unlikely(sz >= 0))
1facf9fc 12998+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 12999+}
13000+
13001+int au_xigen_new(struct inode *inode)
13002+{
13003+ int err;
13004+ loff_t pos;
13005+ ssize_t sz;
13006+ struct super_block *sb;
13007+ struct au_sbinfo *sbinfo;
13008+ struct file *file;
13009+
13010+ err = 0;
13011+ /* todo: dirty, at mount time */
13012+ if (inode->i_ino == AUFS_ROOT_INO)
13013+ goto out;
13014+ sb = inode->i_sb;
dece6358 13015+ SiMustAnyLock(sb);
1facf9fc 13016+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13017+ goto out;
13018+
13019+ err = -EFBIG;
13020+ pos = inode->i_ino;
13021+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13022+ AuIOErr1("too large i%lld\n", pos);
13023+ goto out;
13024+ }
13025+ pos *= sizeof(inode->i_generation);
13026+
13027+ err = 0;
13028+ sbinfo = au_sbi(sb);
13029+ file = sbinfo->si_xigen;
13030+ BUG_ON(!file);
13031+
c06a8ce3 13032+ if (vfsub_f_size_read(file)
1facf9fc 13033+ < pos + sizeof(inode->i_generation)) {
13034+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13035+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13036+ sizeof(inode->i_generation), &pos);
13037+ } else
13038+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13039+ sizeof(inode->i_generation), &pos);
13040+ if (sz == sizeof(inode->i_generation))
13041+ goto out; /* success */
13042+
13043+ err = sz;
13044+ if (unlikely(sz >= 0)) {
13045+ err = -EIO;
13046+ AuIOErr("xigen error (%zd)\n", sz);
13047+ }
13048+
4f0767ce 13049+out:
1facf9fc 13050+ return err;
13051+}
13052+
062440b3 13053+int au_xigen_set(struct super_block *sb, struct path *path)
1facf9fc 13054+{
13055+ int err;
13056+ struct au_sbinfo *sbinfo;
13057+ struct file *file;
13058+
dece6358
AM
13059+ SiMustWriteLock(sb);
13060+
1facf9fc 13061+ sbinfo = au_sbi(sb);
062440b3 13062+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
1facf9fc 13063+ err = PTR_ERR(file);
13064+ if (IS_ERR(file))
13065+ goto out;
13066+ err = 0;
13067+ if (sbinfo->si_xigen)
13068+ fput(sbinfo->si_xigen);
13069+ sbinfo->si_xigen = file;
13070+
4f0767ce 13071+out:
062440b3 13072+ AuTraceErr(err);
1facf9fc 13073+ return err;
13074+}
13075+
13076+void au_xigen_clr(struct super_block *sb)
13077+{
13078+ struct au_sbinfo *sbinfo;
13079+
dece6358
AM
13080+ SiMustWriteLock(sb);
13081+
1facf9fc 13082+ sbinfo = au_sbi(sb);
13083+ if (sbinfo->si_xigen) {
13084+ fput(sbinfo->si_xigen);
13085+ sbinfo->si_xigen = NULL;
13086+ }
13087+}
13088+
13089+/* ---------------------------------------------------------------------- */
13090+
13091+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13092+ ino_t dir_ino)
13093+{
13094+ struct dentry *dentry, *d;
13095+ struct inode *inode;
13096+ unsigned int sigen;
13097+
13098+ dentry = NULL;
13099+ inode = ilookup(sb, ino);
13100+ if (!inode)
13101+ goto out;
13102+
13103+ dentry = ERR_PTR(-ESTALE);
13104+ sigen = au_sigen(sb);
5afbbe0d 13105+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13106+ || IS_DEADDIR(inode)
537831f9 13107+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13108+ goto out_iput;
13109+
13110+ dentry = NULL;
13111+ if (!dir_ino || S_ISDIR(inode->i_mode))
13112+ dentry = d_find_alias(inode);
13113+ else {
027c5e7a 13114+ spin_lock(&inode->i_lock);
c1595e42 13115+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13116+ spin_lock(&d->d_lock);
1facf9fc 13117+ if (!au_test_anon(d)
5527c038 13118+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13119+ dentry = dget_dlock(d);
13120+ spin_unlock(&d->d_lock);
1facf9fc 13121+ break;
13122+ }
027c5e7a
AM
13123+ spin_unlock(&d->d_lock);
13124+ }
13125+ spin_unlock(&inode->i_lock);
1facf9fc 13126+ }
027c5e7a 13127+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13128+ /* need to refresh */
1facf9fc 13129+ dput(dentry);
2cbb1c4b 13130+ dentry = NULL;
1facf9fc 13131+ }
13132+
4f0767ce 13133+out_iput:
1facf9fc 13134+ iput(inode);
4f0767ce 13135+out:
2cbb1c4b 13136+ AuTraceErrPtr(dentry);
1facf9fc 13137+ return dentry;
13138+}
13139+
13140+/* ---------------------------------------------------------------------- */
13141+
13142+/* todo: dirty? */
13143+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13144+
13145+struct au_compare_mnt_args {
13146+ /* input */
13147+ struct super_block *sb;
13148+
13149+ /* output */
13150+ struct vfsmount *mnt;
13151+};
13152+
13153+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13154+{
13155+ struct au_compare_mnt_args *a = arg;
13156+
13157+ if (mnt->mnt_sb != a->sb)
13158+ return 0;
13159+ a->mnt = mntget(mnt);
13160+ return 1;
13161+}
13162+
1facf9fc 13163+static struct vfsmount *au_mnt_get(struct super_block *sb)
13164+{
4a4d8108 13165+ int err;
7eafdf33 13166+ struct path root;
4a4d8108
AM
13167+ struct au_compare_mnt_args args = {
13168+ .sb = sb
13169+ };
1facf9fc 13170+
7eafdf33 13171+ get_fs_root(current->fs, &root);
523b37e3 13172+ rcu_read_lock();
7eafdf33 13173+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13174+ rcu_read_unlock();
7eafdf33 13175+ path_put(&root);
4a4d8108
AM
13176+ AuDebugOn(!err);
13177+ AuDebugOn(!args.mnt);
13178+ return args.mnt;
1facf9fc 13179+}
13180+
13181+struct au_nfsd_si_lock {
4a4d8108 13182+ unsigned int sigen;
027c5e7a 13183+ aufs_bindex_t bindex, br_id;
1facf9fc 13184+ unsigned char force_lock;
13185+};
13186+
027c5e7a
AM
13187+static int si_nfsd_read_lock(struct super_block *sb,
13188+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13189+{
027c5e7a 13190+ int err;
1facf9fc 13191+ aufs_bindex_t bindex;
13192+
13193+ si_read_lock(sb, AuLock_FLUSH);
13194+
13195+ /* branch id may be wrapped around */
027c5e7a 13196+ err = 0;
1facf9fc 13197+ bindex = au_br_index(sb, nsi_lock->br_id);
13198+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13199+ goto out; /* success */
13200+
027c5e7a
AM
13201+ err = -ESTALE;
13202+ bindex = -1;
1facf9fc 13203+ if (!nsi_lock->force_lock)
13204+ si_read_unlock(sb);
1facf9fc 13205+
4f0767ce 13206+out:
027c5e7a
AM
13207+ nsi_lock->bindex = bindex;
13208+ return err;
1facf9fc 13209+}
13210+
13211+struct find_name_by_ino {
392086de 13212+ struct dir_context ctx;
1facf9fc 13213+ int called, found;
13214+ ino_t ino;
13215+ char *name;
13216+ int namelen;
13217+};
13218+
13219+static int
392086de
AM
13220+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13221+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13222+{
392086de
AM
13223+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13224+ ctx);
1facf9fc 13225+
13226+ a->called++;
13227+ if (a->ino != ino)
13228+ return 0;
13229+
13230+ memcpy(a->name, name, namelen);
13231+ a->namelen = namelen;
13232+ a->found = 1;
13233+ return 1;
13234+}
13235+
13236+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13237+ struct au_nfsd_si_lock *nsi_lock)
13238+{
13239+ struct dentry *dentry, *parent;
13240+ struct file *file;
13241+ struct inode *dir;
392086de
AM
13242+ struct find_name_by_ino arg = {
13243+ .ctx = {
2000de60 13244+ .actor = find_name_by_ino
392086de
AM
13245+ }
13246+ };
1facf9fc 13247+ int err;
13248+
13249+ parent = path->dentry;
13250+ if (nsi_lock)
13251+ si_read_unlock(parent->d_sb);
4a4d8108 13252+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13253+ dentry = (void *)file;
13254+ if (IS_ERR(file))
13255+ goto out;
13256+
13257+ dentry = ERR_PTR(-ENOMEM);
537831f9 13258+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13259+ if (unlikely(!arg.name))
13260+ goto out_file;
13261+ arg.ino = ino;
13262+ arg.found = 0;
13263+ do {
13264+ arg.called = 0;
13265+ /* smp_mb(); */
392086de 13266+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13267+ } while (!err && !arg.found && arg.called);
13268+ dentry = ERR_PTR(err);
13269+ if (unlikely(err))
13270+ goto out_name;
1716fcea
AM
13271+ /* instead of ENOENT */
13272+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13273+ if (!arg.found)
13274+ goto out_name;
13275+
b4510431 13276+ /* do not call vfsub_lkup_one() */
5527c038 13277+ dir = d_inode(parent);
febd17d6 13278+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13279+ AuTraceErrPtr(dentry);
13280+ if (IS_ERR(dentry))
13281+ goto out_name;
13282+ AuDebugOn(au_test_anon(dentry));
5527c038 13283+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13284+ dput(dentry);
13285+ dentry = ERR_PTR(-ENOENT);
13286+ }
13287+
4f0767ce 13288+out_name:
1c60b727 13289+ free_page((unsigned long)arg.name);
4f0767ce 13290+out_file:
1facf9fc 13291+ fput(file);
4f0767ce 13292+out:
1facf9fc 13293+ if (unlikely(nsi_lock
13294+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13295+ if (!IS_ERR(dentry)) {
13296+ dput(dentry);
13297+ dentry = ERR_PTR(-ESTALE);
13298+ }
13299+ AuTraceErrPtr(dentry);
13300+ return dentry;
13301+}
13302+
13303+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13304+ ino_t dir_ino,
13305+ struct au_nfsd_si_lock *nsi_lock)
13306+{
13307+ struct dentry *dentry;
13308+ struct path path;
13309+
13310+ if (dir_ino != AUFS_ROOT_INO) {
13311+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13312+ dentry = path.dentry;
13313+ if (!path.dentry || IS_ERR(path.dentry))
13314+ goto out;
13315+ AuDebugOn(au_test_anon(path.dentry));
13316+ } else
13317+ path.dentry = dget(sb->s_root);
13318+
13319+ path.mnt = au_mnt_get(sb);
13320+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13321+ path_put(&path);
13322+
4f0767ce 13323+out:
1facf9fc 13324+ AuTraceErrPtr(dentry);
13325+ return dentry;
13326+}
13327+
13328+/* ---------------------------------------------------------------------- */
13329+
13330+static int h_acceptable(void *expv, struct dentry *dentry)
13331+{
13332+ return 1;
13333+}
13334+
13335+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13336+ char *buf, int len, struct super_block *sb)
13337+{
13338+ char *p;
13339+ int n;
13340+ struct path path;
13341+
13342+ p = d_path(h_rootpath, buf, len);
13343+ if (IS_ERR(p))
13344+ goto out;
13345+ n = strlen(p);
13346+
13347+ path.mnt = h_rootpath->mnt;
13348+ path.dentry = h_parent;
13349+ p = d_path(&path, buf, len);
13350+ if (IS_ERR(p))
13351+ goto out;
13352+ if (n != 1)
13353+ p += n;
13354+
13355+ path.mnt = au_mnt_get(sb);
13356+ path.dentry = sb->s_root;
13357+ p = d_path(&path, buf, len - strlen(p));
13358+ mntput(path.mnt);
13359+ if (IS_ERR(p))
13360+ goto out;
13361+ if (n != 1)
13362+ p[strlen(p)] = '/';
13363+
4f0767ce 13364+out:
1facf9fc 13365+ AuTraceErrPtr(p);
13366+ return p;
13367+}
13368+
13369+static
027c5e7a
AM
13370+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13371+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13372+{
13373+ struct dentry *dentry, *h_parent, *root;
13374+ struct super_block *h_sb;
13375+ char *pathname, *p;
13376+ struct vfsmount *h_mnt;
13377+ struct au_branch *br;
13378+ int err;
13379+ struct path path;
13380+
027c5e7a 13381+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13382+ h_mnt = au_br_mnt(br);
1facf9fc 13383+ h_sb = h_mnt->mnt_sb;
13384+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13385+ lockdep_off();
1facf9fc 13386+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13387+ fh_len - Fh_tail, fh[Fh_h_type],
13388+ h_acceptable, /*context*/NULL);
5afbbe0d 13389+ lockdep_on();
1facf9fc 13390+ dentry = h_parent;
13391+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13392+ AuWarn1("%s decode_fh failed, %ld\n",
13393+ au_sbtype(h_sb), PTR_ERR(h_parent));
13394+ goto out;
13395+ }
13396+ dentry = NULL;
13397+ if (unlikely(au_test_anon(h_parent))) {
13398+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13399+ au_sbtype(h_sb));
13400+ goto out_h_parent;
13401+ }
13402+
13403+ dentry = ERR_PTR(-ENOMEM);
13404+ pathname = (void *)__get_free_page(GFP_NOFS);
13405+ if (unlikely(!pathname))
13406+ goto out_h_parent;
13407+
13408+ root = sb->s_root;
13409+ path.mnt = h_mnt;
13410+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13411+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13412+ di_read_unlock(root, !AuLock_IR);
13413+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13414+ dentry = (void *)p;
13415+ if (IS_ERR(p))
13416+ goto out_pathname;
13417+
13418+ si_read_unlock(sb);
13419+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13420+ dentry = ERR_PTR(err);
13421+ if (unlikely(err))
13422+ goto out_relock;
13423+
13424+ dentry = ERR_PTR(-ENOENT);
13425+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13426+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13427+ goto out_path;
13428+
5527c038 13429+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13430+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13431+ else
13432+ dentry = dget(path.dentry);
13433+
4f0767ce 13434+out_path:
1facf9fc 13435+ path_put(&path);
4f0767ce 13436+out_relock:
1facf9fc 13437+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13438+ if (!IS_ERR(dentry)) {
13439+ dput(dentry);
13440+ dentry = ERR_PTR(-ESTALE);
13441+ }
4f0767ce 13442+out_pathname:
1c60b727 13443+ free_page((unsigned long)pathname);
4f0767ce 13444+out_h_parent:
1facf9fc 13445+ dput(h_parent);
4f0767ce 13446+out:
1facf9fc 13447+ AuTraceErrPtr(dentry);
13448+ return dentry;
13449+}
13450+
13451+/* ---------------------------------------------------------------------- */
13452+
13453+static struct dentry *
13454+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13455+ int fh_type)
13456+{
13457+ struct dentry *dentry;
13458+ __u32 *fh = fid->raw;
027c5e7a 13459+ struct au_branch *br;
1facf9fc 13460+ ino_t ino, dir_ino;
1facf9fc 13461+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13462+ .force_lock = 0
13463+ };
13464+
1facf9fc 13465+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13466+ /* it should never happen, but the file handle is unreliable */
13467+ if (unlikely(fh_len < Fh_tail))
13468+ goto out;
13469+ nsi_lock.sigen = fh[Fh_sigen];
13470+ nsi_lock.br_id = fh[Fh_br_id];
13471+
1facf9fc 13472+ /* branch id may be wrapped around */
027c5e7a
AM
13473+ br = NULL;
13474+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13475+ goto out;
13476+ nsi_lock.force_lock = 1;
13477+
13478+ /* is this inode still cached? */
13479+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13480+ /* it should never happen */
13481+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13482+ goto out_unlock;
4a4d8108 13483+
1facf9fc 13484+ dir_ino = decode_ino(fh + Fh_dir_ino);
13485+ dentry = decode_by_ino(sb, ino, dir_ino);
13486+ if (IS_ERR(dentry))
13487+ goto out_unlock;
13488+ if (dentry)
13489+ goto accept;
13490+
13491+ /* is the parent dir cached? */
027c5e7a 13492+ br = au_sbr(sb, nsi_lock.bindex);
acd2b654 13493+ au_lcnt_inc(&br->br_nfiles);
1facf9fc 13494+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13495+ if (IS_ERR(dentry))
13496+ goto out_unlock;
13497+ if (dentry)
13498+ goto accept;
13499+
13500+ /* lookup path */
027c5e7a 13501+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13502+ if (IS_ERR(dentry))
13503+ goto out_unlock;
13504+ if (unlikely(!dentry))
13505+ /* todo?: make it ESTALE */
13506+ goto out_unlock;
13507+
4f0767ce 13508+accept:
027c5e7a 13509+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13510+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13511+ goto out_unlock; /* success */
13512+
13513+ dput(dentry);
13514+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13515+out_unlock:
027c5e7a 13516+ if (br)
acd2b654 13517+ au_lcnt_dec(&br->br_nfiles);
1facf9fc 13518+ si_read_unlock(sb);
4f0767ce 13519+out:
1facf9fc 13520+ AuTraceErrPtr(dentry);
13521+ return dentry;
13522+}
13523+
13524+#if 0 /* reserved for future use */
13525+/* support subtreecheck option */
13526+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13527+ int fh_len, int fh_type)
13528+{
13529+ struct dentry *parent;
13530+ __u32 *fh = fid->raw;
13531+ ino_t dir_ino;
13532+
13533+ dir_ino = decode_ino(fh + Fh_dir_ino);
13534+ parent = decode_by_ino(sb, dir_ino, 0);
13535+ if (IS_ERR(parent))
13536+ goto out;
13537+ if (!parent)
13538+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13539+ dir_ino, fh, fh_len);
13540+
4f0767ce 13541+out:
1facf9fc 13542+ AuTraceErrPtr(parent);
13543+ return parent;
13544+}
13545+#endif
13546+
13547+/* ---------------------------------------------------------------------- */
13548+
0c3ec466
AM
13549+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13550+ struct inode *dir)
1facf9fc 13551+{
13552+ int err;
0c3ec466 13553+ aufs_bindex_t bindex;
1facf9fc 13554+ struct super_block *sb, *h_sb;
0c3ec466
AM
13555+ struct dentry *dentry, *parent, *h_parent;
13556+ struct inode *h_dir;
1facf9fc 13557+ struct au_branch *br;
13558+
1facf9fc 13559+ err = -ENOSPC;
13560+ if (unlikely(*max_len <= Fh_tail)) {
13561+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13562+ goto out;
13563+ }
13564+
13565+ err = FILEID_ROOT;
0c3ec466
AM
13566+ if (inode->i_ino == AUFS_ROOT_INO) {
13567+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13568+ goto out;
13569+ }
13570+
1facf9fc 13571+ h_parent = NULL;
0c3ec466
AM
13572+ sb = inode->i_sb;
13573+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13574+ if (unlikely(err))
13575+ goto out;
13576+
1facf9fc 13577+#ifdef CONFIG_AUFS_DEBUG
13578+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13579+ AuWarn1("NFS-exporting requires xino\n");
13580+#endif
027c5e7a 13581+ err = -EIO;
0c3ec466
AM
13582+ parent = NULL;
13583+ ii_read_lock_child(inode);
5afbbe0d 13584+ bindex = au_ibtop(inode);
0c3ec466 13585+ if (!dir) {
c1595e42 13586+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13587+ if (unlikely(!dentry))
13588+ goto out_unlock;
13589+ AuDebugOn(au_test_anon(dentry));
13590+ parent = dget_parent(dentry);
13591+ dput(dentry);
13592+ if (unlikely(!parent))
13593+ goto out_unlock;
5527c038
JR
13594+ if (d_really_is_positive(parent))
13595+ dir = d_inode(parent);
1facf9fc 13596+ }
0c3ec466
AM
13597+
13598+ ii_read_lock_parent(dir);
13599+ h_dir = au_h_iptr(dir, bindex);
13600+ ii_read_unlock(dir);
13601+ if (unlikely(!h_dir))
13602+ goto out_parent;
c1595e42 13603+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13604+ if (unlikely(!h_parent))
0c3ec466 13605+ goto out_hparent;
1facf9fc 13606+
13607+ err = -EPERM;
13608+ br = au_sbr(sb, bindex);
86dc4139 13609+ h_sb = au_br_sb(br);
1facf9fc 13610+ if (unlikely(!h_sb->s_export_op)) {
13611+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13612+ goto out_hparent;
1facf9fc 13613+ }
13614+
13615+ fh[Fh_br_id] = br->br_id;
13616+ fh[Fh_sigen] = au_sigen(sb);
13617+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13618+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13619+ fh[Fh_igen] = inode->i_generation;
13620+
13621+ *max_len -= Fh_tail;
13622+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13623+ max_len,
13624+ /*connectable or subtreecheck*/0);
13625+ err = fh[Fh_h_type];
13626+ *max_len += Fh_tail;
13627+ /* todo: macros? */
1716fcea 13628+ if (err != FILEID_INVALID)
1facf9fc 13629+ err = 99;
13630+ else
13631+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13632+
0c3ec466 13633+out_hparent:
1facf9fc 13634+ dput(h_parent);
0c3ec466 13635+out_parent:
1facf9fc 13636+ dput(parent);
0c3ec466
AM
13637+out_unlock:
13638+ ii_read_unlock(inode);
13639+ si_read_unlock(sb);
4f0767ce 13640+out:
1facf9fc 13641+ if (unlikely(err < 0))
1716fcea 13642+ err = FILEID_INVALID;
1facf9fc 13643+ return err;
13644+}
13645+
13646+/* ---------------------------------------------------------------------- */
13647+
4a4d8108
AM
13648+static int aufs_commit_metadata(struct inode *inode)
13649+{
13650+ int err;
13651+ aufs_bindex_t bindex;
13652+ struct super_block *sb;
13653+ struct inode *h_inode;
13654+ int (*f)(struct inode *inode);
13655+
13656+ sb = inode->i_sb;
e49829fe 13657+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13658+ ii_write_lock_child(inode);
5afbbe0d 13659+ bindex = au_ibtop(inode);
4a4d8108
AM
13660+ AuDebugOn(bindex < 0);
13661+ h_inode = au_h_iptr(inode, bindex);
13662+
13663+ f = h_inode->i_sb->s_export_op->commit_metadata;
13664+ if (f)
13665+ err = f(h_inode);
13666+ else {
13667+ struct writeback_control wbc = {
13668+ .sync_mode = WB_SYNC_ALL,
13669+ .nr_to_write = 0 /* metadata only */
13670+ };
13671+
13672+ err = sync_inode(h_inode, &wbc);
13673+ }
13674+
13675+ au_cpup_attr_timesizes(inode);
13676+ ii_write_unlock(inode);
13677+ si_read_unlock(sb);
13678+ return err;
13679+}
13680+
13681+/* ---------------------------------------------------------------------- */
13682+
1facf9fc 13683+static struct export_operations aufs_export_op = {
4a4d8108 13684+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13685+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13686+ .encode_fh = aufs_encode_fh,
13687+ .commit_metadata = aufs_commit_metadata
1facf9fc 13688+};
13689+
13690+void au_export_init(struct super_block *sb)
13691+{
13692+ struct au_sbinfo *sbinfo;
13693+ __u32 u;
13694+
5afbbe0d
AM
13695+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13696+ && IS_MODULE(CONFIG_EXPORTFS),
13697+ AUFS_NAME ": unsupported configuration "
13698+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13699+
1facf9fc 13700+ sb->s_export_op = &aufs_export_op;
13701+ sbinfo = au_sbi(sb);
13702+ sbinfo->si_xigen = NULL;
13703+ get_random_bytes(&u, sizeof(u));
13704+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13705+ atomic_set(&sbinfo->si_xigen_next, u);
13706+}
076b876e 13707diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
eca34b5c
AM
13708--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
13709+++ linux/fs/aufs/fhsm.c 2019-07-11 15:42:14.465571210 +0200
062440b3 13710@@ -0,0 +1,427 @@
cd7a4cd9 13711+// SPDX-License-Identifier: GPL-2.0
076b876e 13712+/*
ba1aed25 13713+ * Copyright (C) 2011-2019 Junjiro R. Okajima
076b876e
AM
13714+ *
13715+ * This program, aufs is free software; you can redistribute it and/or modify
13716+ * it under the terms of the GNU General Public License as published by
13717+ * the Free Software Foundation; either version 2 of the License, or
13718+ * (at your option) any later version.
13719+ *
13720+ * This program is distributed in the hope that it will be useful,
13721+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13722+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13723+ * GNU General Public License for more details.
13724+ *
13725+ * You should have received a copy of the GNU General Public License
13726+ * along with this program; if not, write to the Free Software
13727+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13728+ */
13729+
13730+/*
13731+ * File-based Hierarchy Storage Management
13732+ */
13733+
13734+#include <linux/anon_inodes.h>
13735+#include <linux/poll.h>
13736+#include <linux/seq_file.h>
13737+#include <linux/statfs.h>
13738+#include "aufs.h"
13739+
c1595e42
JR
13740+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13741+{
13742+ struct au_sbinfo *sbinfo;
13743+ struct au_fhsm *fhsm;
13744+
13745+ SiMustAnyLock(sb);
13746+
13747+ sbinfo = au_sbi(sb);
13748+ fhsm = &sbinfo->si_fhsm;
13749+ AuDebugOn(!fhsm);
13750+ return fhsm->fhsm_bottom;
13751+}
13752+
13753+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13754+{
13755+ struct au_sbinfo *sbinfo;
13756+ struct au_fhsm *fhsm;
13757+
13758+ SiMustWriteLock(sb);
13759+
13760+ sbinfo = au_sbi(sb);
13761+ fhsm = &sbinfo->si_fhsm;
13762+ AuDebugOn(!fhsm);
13763+ fhsm->fhsm_bottom = bindex;
13764+}
13765+
13766+/* ---------------------------------------------------------------------- */
13767+
076b876e
AM
13768+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13769+{
13770+ struct au_br_fhsm *bf;
13771+
13772+ bf = br->br_fhsm;
13773+ MtxMustLock(&bf->bf_lock);
13774+
13775+ return !bf->bf_readable
13776+ || time_after(jiffies,
13777+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13778+}
13779+
13780+/* ---------------------------------------------------------------------- */
13781+
13782+static void au_fhsm_notify(struct super_block *sb, int val)
13783+{
13784+ struct au_sbinfo *sbinfo;
13785+ struct au_fhsm *fhsm;
13786+
13787+ SiMustAnyLock(sb);
13788+
13789+ sbinfo = au_sbi(sb);
13790+ fhsm = &sbinfo->si_fhsm;
13791+ if (au_fhsm_pid(fhsm)
13792+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13793+ atomic_set(&fhsm->fhsm_readable, val);
13794+ if (val)
13795+ wake_up(&fhsm->fhsm_wqh);
13796+ }
13797+}
13798+
13799+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13800+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13801+{
13802+ int err;
13803+ struct au_branch *br;
13804+ struct au_br_fhsm *bf;
13805+
13806+ br = au_sbr(sb, bindex);
13807+ AuDebugOn(au_br_rdonly(br));
13808+ bf = br->br_fhsm;
13809+ AuDebugOn(!bf);
13810+
13811+ if (do_lock)
13812+ mutex_lock(&bf->bf_lock);
13813+ else
13814+ MtxMustLock(&bf->bf_lock);
13815+
13816+ /* sb->s_root for NFS is unreliable */
13817+ err = au_br_stfs(br, &bf->bf_stfs);
13818+ if (unlikely(err)) {
13819+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13820+ goto out;
13821+ }
13822+
13823+ bf->bf_jiffy = jiffies;
13824+ bf->bf_readable = 1;
13825+ if (do_notify)
13826+ au_fhsm_notify(sb, /*val*/1);
13827+ if (rstfs)
13828+ *rstfs = bf->bf_stfs;
13829+
13830+out:
13831+ if (do_lock)
13832+ mutex_unlock(&bf->bf_lock);
13833+ au_fhsm_notify(sb, /*val*/1);
13834+
13835+ return err;
13836+}
13837+
13838+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13839+{
13840+ int err;
076b876e
AM
13841+ struct au_sbinfo *sbinfo;
13842+ struct au_fhsm *fhsm;
13843+ struct au_branch *br;
13844+ struct au_br_fhsm *bf;
13845+
13846+ AuDbg("b%d, force %d\n", bindex, force);
13847+ SiMustAnyLock(sb);
13848+
13849+ sbinfo = au_sbi(sb);
13850+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13851+ if (!au_ftest_si(sbinfo, FHSM)
13852+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13853+ return;
13854+
13855+ br = au_sbr(sb, bindex);
13856+ bf = br->br_fhsm;
13857+ AuDebugOn(!bf);
13858+ mutex_lock(&bf->bf_lock);
13859+ if (force
13860+ || au_fhsm_pid(fhsm)
13861+ || au_fhsm_test_jiffy(sbinfo, br))
13862+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13863+ /*do_notify*/1);
13864+ mutex_unlock(&bf->bf_lock);
13865+}
13866+
13867+void au_fhsm_wrote_all(struct super_block *sb, int force)
13868+{
5afbbe0d 13869+ aufs_bindex_t bindex, bbot;
076b876e
AM
13870+ struct au_branch *br;
13871+
13872+ /* exclude the bottom */
5afbbe0d
AM
13873+ bbot = au_fhsm_bottom(sb);
13874+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13875+ br = au_sbr(sb, bindex);
13876+ if (au_br_fhsm(br->br_perm))
13877+ au_fhsm_wrote(sb, bindex, force);
13878+ }
13879+}
13880+
13881+/* ---------------------------------------------------------------------- */
13882+
be118d29 13883+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13884+{
be118d29 13885+ __poll_t mask;
076b876e
AM
13886+ struct au_sbinfo *sbinfo;
13887+ struct au_fhsm *fhsm;
13888+
13889+ mask = 0;
13890+ sbinfo = file->private_data;
13891+ fhsm = &sbinfo->si_fhsm;
13892+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13893+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13894+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13895+
b00004a5
AM
13896+ if (!mask)
13897+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13898+ return mask;
13899+}
13900+
13901+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13902+ struct aufs_stfs *stfs, __s16 brid)
13903+{
13904+ int err;
13905+
13906+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13907+ if (!err)
13908+ err = __put_user(brid, &stbr->brid);
13909+ if (unlikely(err))
13910+ err = -EFAULT;
13911+
13912+ return err;
13913+}
13914+
13915+static ssize_t au_fhsm_do_read(struct super_block *sb,
13916+ struct aufs_stbr __user *stbr, size_t count)
13917+{
13918+ ssize_t err;
13919+ int nstbr;
5afbbe0d 13920+ aufs_bindex_t bindex, bbot;
076b876e
AM
13921+ struct au_branch *br;
13922+ struct au_br_fhsm *bf;
13923+
13924+ /* except the bottom branch */
13925+ err = 0;
13926+ nstbr = 0;
5afbbe0d
AM
13927+ bbot = au_fhsm_bottom(sb);
13928+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
13929+ br = au_sbr(sb, bindex);
13930+ if (!au_br_fhsm(br->br_perm))
13931+ continue;
13932+
13933+ bf = br->br_fhsm;
13934+ mutex_lock(&bf->bf_lock);
13935+ if (bf->bf_readable) {
13936+ err = -EFAULT;
13937+ if (count >= sizeof(*stbr))
13938+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13939+ br->br_id);
13940+ if (!err) {
13941+ bf->bf_readable = 0;
13942+ count -= sizeof(*stbr);
13943+ nstbr++;
13944+ }
13945+ }
13946+ mutex_unlock(&bf->bf_lock);
13947+ }
13948+ if (!err)
13949+ err = sizeof(*stbr) * nstbr;
13950+
13951+ return err;
13952+}
13953+
13954+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13955+ loff_t *pos)
13956+{
13957+ ssize_t err;
13958+ int readable;
5afbbe0d 13959+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
13960+ struct au_sbinfo *sbinfo;
13961+ struct au_fhsm *fhsm;
13962+ struct au_branch *br;
13963+ struct super_block *sb;
13964+
13965+ err = 0;
13966+ sbinfo = file->private_data;
13967+ fhsm = &sbinfo->si_fhsm;
13968+need_data:
13969+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
13970+ if (!atomic_read(&fhsm->fhsm_readable)) {
13971+ if (vfsub_file_flags(file) & O_NONBLOCK)
13972+ err = -EAGAIN;
13973+ else
13974+ err = wait_event_interruptible_locked_irq
13975+ (fhsm->fhsm_wqh,
13976+ atomic_read(&fhsm->fhsm_readable));
13977+ }
13978+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13979+ if (unlikely(err))
13980+ goto out;
13981+
13982+ /* sb may already be dead */
13983+ au_rw_read_lock(&sbinfo->si_rwsem);
13984+ readable = atomic_read(&fhsm->fhsm_readable);
13985+ if (readable > 0) {
13986+ sb = sbinfo->si_sb;
13987+ AuDebugOn(!sb);
13988+ /* exclude the bottom branch */
13989+ nfhsm = 0;
5afbbe0d
AM
13990+ bbot = au_fhsm_bottom(sb);
13991+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13992+ br = au_sbr(sb, bindex);
13993+ if (au_br_fhsm(br->br_perm))
13994+ nfhsm++;
13995+ }
13996+ err = -EMSGSIZE;
13997+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13998+ atomic_set(&fhsm->fhsm_readable, 0);
13999+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14000+ count);
14001+ }
14002+ }
14003+ au_rw_read_unlock(&sbinfo->si_rwsem);
14004+ if (!readable)
14005+ goto need_data;
14006+
14007+out:
14008+ return err;
14009+}
14010+
14011+static int au_fhsm_release(struct inode *inode, struct file *file)
14012+{
14013+ struct au_sbinfo *sbinfo;
14014+ struct au_fhsm *fhsm;
14015+
14016+ /* sb may already be dead */
14017+ sbinfo = file->private_data;
14018+ fhsm = &sbinfo->si_fhsm;
14019+ spin_lock(&fhsm->fhsm_spin);
14020+ fhsm->fhsm_pid = 0;
14021+ spin_unlock(&fhsm->fhsm_spin);
14022+ kobject_put(&sbinfo->si_kobj);
14023+
14024+ return 0;
14025+}
14026+
14027+static const struct file_operations au_fhsm_fops = {
14028+ .owner = THIS_MODULE,
14029+ .llseek = noop_llseek,
14030+ .read = au_fhsm_read,
14031+ .poll = au_fhsm_poll,
14032+ .release = au_fhsm_release
14033+};
14034+
14035+int au_fhsm_fd(struct super_block *sb, int oflags)
14036+{
14037+ int err, fd;
14038+ struct au_sbinfo *sbinfo;
14039+ struct au_fhsm *fhsm;
14040+
14041+ err = -EPERM;
14042+ if (unlikely(!capable(CAP_SYS_ADMIN)))
14043+ goto out;
14044+
14045+ err = -EINVAL;
14046+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14047+ goto out;
14048+
14049+ err = 0;
14050+ sbinfo = au_sbi(sb);
14051+ fhsm = &sbinfo->si_fhsm;
14052+ spin_lock(&fhsm->fhsm_spin);
14053+ if (!fhsm->fhsm_pid)
14054+ fhsm->fhsm_pid = current->pid;
14055+ else
14056+ err = -EBUSY;
14057+ spin_unlock(&fhsm->fhsm_spin);
14058+ if (unlikely(err))
14059+ goto out;
14060+
14061+ oflags |= O_RDONLY;
14062+ /* oflags |= FMODE_NONOTIFY; */
14063+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14064+ err = fd;
14065+ if (unlikely(fd < 0))
14066+ goto out_pid;
14067+
acd2b654 14068+ /* succeed regardless 'fhsm' status */
076b876e
AM
14069+ kobject_get(&sbinfo->si_kobj);
14070+ si_noflush_read_lock(sb);
14071+ if (au_ftest_si(sbinfo, FHSM))
14072+ au_fhsm_wrote_all(sb, /*force*/0);
14073+ si_read_unlock(sb);
14074+ goto out; /* success */
14075+
14076+out_pid:
14077+ spin_lock(&fhsm->fhsm_spin);
14078+ fhsm->fhsm_pid = 0;
14079+ spin_unlock(&fhsm->fhsm_spin);
14080+out:
14081+ AuTraceErr(err);
14082+ return err;
14083+}
14084+
14085+/* ---------------------------------------------------------------------- */
14086+
14087+int au_fhsm_br_alloc(struct au_branch *br)
14088+{
14089+ int err;
14090+
14091+ err = 0;
14092+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14093+ if (br->br_fhsm)
14094+ au_br_fhsm_init(br->br_fhsm);
14095+ else
14096+ err = -ENOMEM;
14097+
14098+ return err;
14099+}
14100+
14101+/* ---------------------------------------------------------------------- */
14102+
14103+void au_fhsm_fin(struct super_block *sb)
14104+{
14105+ au_fhsm_notify(sb, /*val*/-1);
14106+}
14107+
14108+void au_fhsm_init(struct au_sbinfo *sbinfo)
14109+{
14110+ struct au_fhsm *fhsm;
14111+
14112+ fhsm = &sbinfo->si_fhsm;
14113+ spin_lock_init(&fhsm->fhsm_spin);
14114+ init_waitqueue_head(&fhsm->fhsm_wqh);
14115+ atomic_set(&fhsm->fhsm_readable, 0);
14116+ fhsm->fhsm_expire
14117+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14118+ fhsm->fhsm_bottom = -1;
076b876e
AM
14119+}
14120+
14121+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14122+{
14123+ sbinfo->si_fhsm.fhsm_expire
14124+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14125+}
14126+
14127+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14128+{
14129+ unsigned int u;
14130+
14131+ if (!au_ftest_si(sbinfo, FHSM))
14132+ return;
14133+
14134+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14135+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14136+ seq_printf(seq, ",fhsm_sec=%u", u);
14137+}
7f207e10 14138diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
eca34b5c
AM
14139--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
14140+++ linux/fs/aufs/file.c 2019-07-11 15:42:14.465571210 +0200
acd2b654 14141@@ -0,0 +1,863 @@
cd7a4cd9 14142+// SPDX-License-Identifier: GPL-2.0
1facf9fc 14143+/*
ba1aed25 14144+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 14145+ *
14146+ * This program, aufs is free software; you can redistribute it and/or modify
14147+ * it under the terms of the GNU General Public License as published by
14148+ * the Free Software Foundation; either version 2 of the License, or
14149+ * (at your option) any later version.
dece6358
AM
14150+ *
14151+ * This program is distributed in the hope that it will be useful,
14152+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14153+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14154+ * GNU General Public License for more details.
14155+ *
14156+ * You should have received a copy of the GNU General Public License
523b37e3 14157+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14158+ */
14159+
14160+/*
4a4d8108 14161+ * handling file/dir, and address_space operation
1facf9fc 14162+ */
14163+
7eafdf33
AM
14164+#ifdef CONFIG_AUFS_DEBUG
14165+#include <linux/migrate.h>
14166+#endif
4a4d8108 14167+#include <linux/pagemap.h>
1facf9fc 14168+#include "aufs.h"
14169+
4a4d8108
AM
14170+/* drop flags for writing */
14171+unsigned int au_file_roflags(unsigned int flags)
14172+{
14173+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14174+ flags |= O_RDONLY | O_NOATIME;
14175+ return flags;
14176+}
14177+
14178+/* common functions to regular file and dir */
14179+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14180+ struct file *file, int force_wr)
1facf9fc 14181+{
1308ab2a 14182+ struct file *h_file;
4a4d8108
AM
14183+ struct dentry *h_dentry;
14184+ struct inode *h_inode;
14185+ struct super_block *sb;
14186+ struct au_branch *br;
14187+ struct path h_path;
b912730e 14188+ int err;
1facf9fc 14189+
4a4d8108
AM
14190+ /* a race condition can happen between open and unlink/rmdir */
14191+ h_file = ERR_PTR(-ENOENT);
14192+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14193+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14194+ goto out;
5527c038 14195+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14196+ spin_lock(&h_dentry->d_lock);
14197+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14198+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14199+ ;
14200+ spin_unlock(&h_dentry->d_lock);
14201+ if (unlikely(err))
4a4d8108 14202+ goto out;
1facf9fc 14203+
4a4d8108
AM
14204+ sb = dentry->d_sb;
14205+ br = au_sbr(sb, bindex);
b912730e
AM
14206+ err = au_br_test_oflag(flags, br);
14207+ h_file = ERR_PTR(err);
14208+ if (unlikely(err))
027c5e7a 14209+ goto out;
1facf9fc 14210+
4a4d8108 14211+ /* drop flags for writing */
5527c038 14212+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14213+ if (force_wr && !(flags & O_WRONLY))
14214+ force_wr = 0;
4a4d8108 14215+ flags = au_file_roflags(flags);
392086de
AM
14216+ if (force_wr) {
14217+ h_file = ERR_PTR(-EROFS);
14218+ flags = au_file_roflags(flags);
14219+ if (unlikely(vfsub_native_ro(h_inode)
14220+ || IS_APPEND(h_inode)))
14221+ goto out;
14222+ flags &= ~O_ACCMODE;
14223+ flags |= O_WRONLY;
14224+ }
14225+ }
4a4d8108 14226+ flags &= ~O_CREAT;
acd2b654 14227+ au_lcnt_inc(&br->br_nfiles);
4a4d8108 14228+ h_path.dentry = h_dentry;
86dc4139 14229+ h_path.mnt = au_br_mnt(br);
38d290e6 14230+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14231+ if (IS_ERR(h_file))
14232+ goto out_br;
dece6358 14233+
b912730e 14234+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14235+ err = deny_write_access(h_file);
14236+ if (unlikely(err)) {
14237+ fput(h_file);
14238+ h_file = ERR_PTR(err);
14239+ goto out_br;
14240+ }
14241+ }
953406b4 14242+ fsnotify_open(h_file);
4a4d8108 14243+ goto out; /* success */
1facf9fc 14244+
4f0767ce 14245+out_br:
acd2b654 14246+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 14247+out:
4a4d8108
AM
14248+ return h_file;
14249+}
1308ab2a 14250+
076b876e
AM
14251+static int au_cmoo(struct dentry *dentry)
14252+{
8b6a4947 14253+ int err, cmoo, matched;
076b876e
AM
14254+ unsigned int udba;
14255+ struct path h_path;
14256+ struct au_pin pin;
14257+ struct au_cp_generic cpg = {
14258+ .dentry = dentry,
14259+ .bdst = -1,
14260+ .bsrc = -1,
14261+ .len = -1,
14262+ .pin = &pin,
14263+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14264+ };
7e9cd9fe 14265+ struct inode *delegated;
076b876e
AM
14266+ struct super_block *sb;
14267+ struct au_sbinfo *sbinfo;
14268+ struct au_fhsm *fhsm;
14269+ pid_t pid;
14270+ struct au_branch *br;
14271+ struct dentry *parent;
14272+ struct au_hinode *hdir;
14273+
14274+ DiMustWriteLock(dentry);
5527c038 14275+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14276+
14277+ err = 0;
14278+ if (IS_ROOT(dentry))
14279+ goto out;
5afbbe0d 14280+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14281+ if (!cpg.bsrc)
14282+ goto out;
14283+
14284+ sb = dentry->d_sb;
14285+ sbinfo = au_sbi(sb);
14286+ fhsm = &sbinfo->si_fhsm;
14287+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14288+ rcu_read_lock();
14289+ matched = (pid
14290+ && (current->pid == pid
14291+ || rcu_dereference(current->real_parent)->pid == pid));
14292+ rcu_read_unlock();
14293+ if (matched)
076b876e
AM
14294+ goto out;
14295+
14296+ br = au_sbr(sb, cpg.bsrc);
14297+ cmoo = au_br_cmoo(br->br_perm);
14298+ if (!cmoo)
14299+ goto out;
7e9cd9fe 14300+ if (!d_is_reg(dentry))
076b876e
AM
14301+ cmoo &= AuBrAttr_COO_ALL;
14302+ if (!cmoo)
14303+ goto out;
14304+
14305+ parent = dget_parent(dentry);
14306+ di_write_lock_parent(parent);
14307+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14308+ cpg.bdst = err;
14309+ if (unlikely(err < 0)) {
14310+ err = 0; /* there is no upper writable branch */
14311+ goto out_dgrade;
14312+ }
14313+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14314+
14315+ /* do not respect the coo attrib for the target branch */
14316+ err = au_cpup_dirs(dentry, cpg.bdst);
14317+ if (unlikely(err))
14318+ goto out_dgrade;
14319+
14320+ di_downgrade_lock(parent, AuLock_IR);
14321+ udba = au_opt_udba(sb);
14322+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14323+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14324+ if (unlikely(err))
14325+ goto out_parent;
14326+
14327+ err = au_sio_cpup_simple(&cpg);
14328+ au_unpin(&pin);
14329+ if (unlikely(err))
14330+ goto out_parent;
14331+ if (!(cmoo & AuBrWAttr_MOO))
14332+ goto out_parent; /* success */
14333+
14334+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14335+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14336+ if (unlikely(err))
14337+ goto out_parent;
14338+
14339+ h_path.mnt = au_br_mnt(br);
14340+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14341+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14342+ delegated = NULL;
14343+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14344+ au_unpin(&pin);
14345+ /* todo: keep h_dentry or not? */
14346+ if (unlikely(err == -EWOULDBLOCK)) {
14347+ pr_warn("cannot retry for NFSv4 delegation"
14348+ " for an internal unlink\n");
14349+ iput(delegated);
14350+ }
14351+ if (unlikely(err)) {
14352+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14353+ dentry, err);
14354+ err = 0;
14355+ }
14356+ goto out_parent; /* success */
14357+
14358+out_dgrade:
14359+ di_downgrade_lock(parent, AuLock_IR);
14360+out_parent:
14361+ di_read_unlock(parent, AuLock_IR);
14362+ dput(parent);
14363+out:
14364+ AuTraceErr(err);
14365+ return err;
14366+}
14367+
b912730e 14368+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14369+{
8b6a4947 14370+ int err, aopen = args->aopen;
1facf9fc 14371+ struct dentry *dentry;
076b876e 14372+ struct au_finfo *finfo;
1308ab2a 14373+
8b6a4947 14374+ if (!aopen)
b912730e
AM
14375+ err = au_finfo_init(file, args->fidir);
14376+ else {
14377+ lockdep_off();
14378+ err = au_finfo_init(file, args->fidir);
14379+ lockdep_on();
14380+ }
4a4d8108
AM
14381+ if (unlikely(err))
14382+ goto out;
1facf9fc 14383+
2000de60 14384+ dentry = file->f_path.dentry;
b912730e 14385+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14386+ di_write_lock_child(dentry);
14387+ err = au_cmoo(dentry);
14388+ di_downgrade_lock(dentry, AuLock_IR);
14389+ if (!err) {
14390+ if (!aopen)
b912730e 14391+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14392+ else {
14393+ lockdep_off();
acd2b654
AM
14394+ err = args->open(file, vfsub_file_flags(file),
14395+ args->h_file);
8b6a4947
AM
14396+ lockdep_on();
14397+ }
b912730e 14398+ }
8b6a4947 14399+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14400+
076b876e
AM
14401+ finfo = au_fi(file);
14402+ if (!err) {
14403+ finfo->fi_file = file;
8b6a4947
AM
14404+ au_hbl_add(&finfo->fi_hlist,
14405+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14406+ }
8b6a4947 14407+ if (!aopen)
b912730e
AM
14408+ fi_write_unlock(file);
14409+ else {
14410+ lockdep_off();
14411+ fi_write_unlock(file);
14412+ lockdep_on();
14413+ }
4a4d8108 14414+ if (unlikely(err)) {
076b876e 14415+ finfo->fi_hdir = NULL;
1c60b727 14416+ au_finfo_fin(file);
1308ab2a 14417+ }
4a4d8108 14418+
4f0767ce 14419+out:
8b6a4947 14420+ AuTraceErr(err);
1308ab2a 14421+ return err;
14422+}
dece6358 14423+
4a4d8108 14424+int au_reopen_nondir(struct file *file)
1308ab2a 14425+{
4a4d8108 14426+ int err;
5afbbe0d 14427+ aufs_bindex_t btop;
4a4d8108 14428+ struct dentry *dentry;
acd2b654 14429+ struct au_branch *br;
4a4d8108 14430+ struct file *h_file, *h_file_tmp;
1308ab2a 14431+
2000de60 14432+ dentry = file->f_path.dentry;
5afbbe0d 14433+ btop = au_dbtop(dentry);
acd2b654 14434+ br = au_sbr(dentry->d_sb, btop);
4a4d8108 14435+ h_file_tmp = NULL;
5afbbe0d 14436+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14437+ h_file = au_hf_top(file);
14438+ if (file->f_mode == h_file->f_mode)
14439+ return 0; /* success */
14440+ h_file_tmp = h_file;
14441+ get_file(h_file_tmp);
acd2b654 14442+ au_lcnt_inc(&br->br_nfiles);
5afbbe0d 14443+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14444+ }
14445+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14446+ /*
14447+ * it can happen
14448+ * file exists on both of rw and ro
5afbbe0d 14449+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14450+ * prepend a branch as rw, "rw" become ro
14451+ * remove rw/file
14452+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14453+ * --> dbtop is 1, fbtop is still 0
14454+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14455+ */
5afbbe0d 14456+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14457+
5afbbe0d 14458+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14459+ file, /*force_wr*/0);
4a4d8108 14460+ err = PTR_ERR(h_file);
86dc4139
AM
14461+ if (IS_ERR(h_file)) {
14462+ if (h_file_tmp) {
acd2b654 14463+ /* revert */
5afbbe0d 14464+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14465+ h_file_tmp = NULL;
14466+ }
4a4d8108 14467+ goto out; /* todo: close all? */
86dc4139 14468+ }
4a4d8108
AM
14469+
14470+ err = 0;
5afbbe0d
AM
14471+ au_set_fbtop(file, btop);
14472+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14473+ au_update_figen(file);
14474+ /* todo: necessary? */
14475+ /* file->f_ra = h_file->f_ra; */
14476+
4f0767ce 14477+out:
acd2b654 14478+ if (h_file_tmp) {
4a4d8108 14479+ fput(h_file_tmp);
acd2b654
AM
14480+ au_lcnt_dec(&br->br_nfiles);
14481+ }
4a4d8108 14482+ return err;
1facf9fc 14483+}
14484+
1308ab2a 14485+/* ---------------------------------------------------------------------- */
14486+
4a4d8108
AM
14487+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14488+ struct dentry *hi_wh)
1facf9fc 14489+{
4a4d8108 14490+ int err;
5afbbe0d 14491+ aufs_bindex_t btop;
4a4d8108
AM
14492+ struct au_dinfo *dinfo;
14493+ struct dentry *h_dentry;
14494+ struct au_hdentry *hdp;
1facf9fc 14495+
2000de60 14496+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14497+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14498+
5afbbe0d
AM
14499+ btop = dinfo->di_btop;
14500+ dinfo->di_btop = btgt;
14501+ hdp = au_hdentry(dinfo, btgt);
14502+ h_dentry = hdp->hd_dentry;
14503+ hdp->hd_dentry = hi_wh;
4a4d8108 14504+ err = au_reopen_nondir(file);
5afbbe0d
AM
14505+ hdp->hd_dentry = h_dentry;
14506+ dinfo->di_btop = btop;
1facf9fc 14507+
1facf9fc 14508+ return err;
14509+}
14510+
4a4d8108 14511+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14512+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14513+{
4a4d8108 14514+ int err;
027c5e7a 14515+ struct inode *inode, *h_inode;
c2b27bf2
AM
14516+ struct dentry *h_dentry, *hi_wh;
14517+ struct au_cp_generic cpg = {
2000de60 14518+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14519+ .bdst = bcpup,
14520+ .bsrc = -1,
14521+ .len = len,
14522+ .pin = pin
14523+ };
1facf9fc 14524+
5afbbe0d 14525+ au_update_dbtop(cpg.dentry);
5527c038 14526+ inode = d_inode(cpg.dentry);
027c5e7a 14527+ h_inode = NULL;
5afbbe0d
AM
14528+ if (au_dbtop(cpg.dentry) <= bcpup
14529+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14530+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14531+ if (h_dentry && d_is_positive(h_dentry))
14532+ h_inode = d_inode(h_dentry);
027c5e7a 14533+ }
4a4d8108 14534+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14535+ if (!hi_wh && !h_inode)
c2b27bf2 14536+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14537+ else
14538+ /* already copied-up after unlink */
14539+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14540+
4a4d8108 14541+ if (!err
38d290e6
JR
14542+ && (inode->i_nlink > 1
14543+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14544+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14545+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14546+
dece6358 14547+ return err;
1facf9fc 14548+}
14549+
4a4d8108
AM
14550+/*
14551+ * prepare the @file for writing.
14552+ */
14553+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14554+{
4a4d8108 14555+ int err;
5afbbe0d 14556+ aufs_bindex_t dbtop;
c1595e42 14557+ struct dentry *parent;
86dc4139 14558+ struct inode *inode;
1facf9fc 14559+ struct super_block *sb;
4a4d8108 14560+ struct file *h_file;
c2b27bf2 14561+ struct au_cp_generic cpg = {
2000de60 14562+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14563+ .bdst = -1,
14564+ .bsrc = -1,
14565+ .len = len,
14566+ .pin = pin,
14567+ .flags = AuCpup_DTIME
14568+ };
1facf9fc 14569+
c2b27bf2 14570+ sb = cpg.dentry->d_sb;
5527c038 14571+ inode = d_inode(cpg.dentry);
5afbbe0d 14572+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14573+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14574+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14575+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14576+ /*flags*/0);
1facf9fc 14577+ goto out;
4a4d8108 14578+ }
1facf9fc 14579+
027c5e7a 14580+ /* need to cpup or reopen */
c2b27bf2 14581+ parent = dget_parent(cpg.dentry);
4a4d8108 14582+ di_write_lock_parent(parent);
c2b27bf2
AM
14583+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14584+ cpg.bdst = err;
4a4d8108
AM
14585+ if (unlikely(err < 0))
14586+ goto out_dgrade;
14587+ err = 0;
14588+
c2b27bf2
AM
14589+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14590+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14591+ if (unlikely(err))
4a4d8108
AM
14592+ goto out_dgrade;
14593+ }
14594+
c2b27bf2 14595+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14596+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14597+ if (unlikely(err))
14598+ goto out_dgrade;
14599+
5afbbe0d
AM
14600+ dbtop = au_dbtop(cpg.dentry);
14601+ if (dbtop <= cpg.bdst)
c2b27bf2 14602+ cpg.bsrc = cpg.bdst;
027c5e7a 14603+
5afbbe0d 14604+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14605+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14606+ ) {
392086de 14607+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14608+ if (IS_ERR(h_file))
027c5e7a 14609+ err = PTR_ERR(h_file);
86dc4139 14610+ else {
027c5e7a 14611+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14612+ if (dbtop > cpg.bdst)
c2b27bf2 14613+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14614+ if (!err)
14615+ err = au_reopen_nondir(file);
c2b27bf2 14616+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14617+ }
027c5e7a
AM
14618+ } else { /* copyup as wh and reopen */
14619+ /*
14620+ * since writable hfsplus branch is not supported,
14621+ * h_open_pre/post() are unnecessary.
14622+ */
c2b27bf2 14623+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14624+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14625+ }
4a4d8108
AM
14626+
14627+ if (!err) {
14628+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14629+ goto out_dput; /* success */
14630+ }
14631+ au_unpin(pin);
14632+ goto out_unlock;
1facf9fc 14633+
4f0767ce 14634+out_dgrade:
4a4d8108 14635+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14636+out_unlock:
4a4d8108 14637+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14638+out_dput:
4a4d8108 14639+ dput(parent);
4f0767ce 14640+out:
1facf9fc 14641+ return err;
14642+}
14643+
4a4d8108
AM
14644+/* ---------------------------------------------------------------------- */
14645+
14646+int au_do_flush(struct file *file, fl_owner_t id,
14647+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14648+{
4a4d8108 14649+ int err;
1facf9fc 14650+ struct super_block *sb;
4a4d8108 14651+ struct inode *inode;
1facf9fc 14652+
c06a8ce3
AM
14653+ inode = file_inode(file);
14654+ sb = inode->i_sb;
4a4d8108
AM
14655+ si_noflush_read_lock(sb);
14656+ fi_read_lock(file);
b752ccd1 14657+ ii_read_lock_child(inode);
1facf9fc 14658+
4a4d8108
AM
14659+ err = flush(file, id);
14660+ au_cpup_attr_timesizes(inode);
1facf9fc 14661+
b752ccd1 14662+ ii_read_unlock(inode);
4a4d8108 14663+ fi_read_unlock(file);
1308ab2a 14664+ si_read_unlock(sb);
dece6358 14665+ return err;
1facf9fc 14666+}
14667+
4a4d8108
AM
14668+/* ---------------------------------------------------------------------- */
14669+
14670+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14671+{
4a4d8108 14672+ int err;
4a4d8108
AM
14673+ struct au_pin pin;
14674+ struct au_finfo *finfo;
c2b27bf2 14675+ struct dentry *parent, *hi_wh;
4a4d8108 14676+ struct inode *inode;
1facf9fc 14677+ struct super_block *sb;
c2b27bf2 14678+ struct au_cp_generic cpg = {
2000de60 14679+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14680+ .bdst = -1,
14681+ .bsrc = -1,
14682+ .len = -1,
14683+ .pin = &pin,
14684+ .flags = AuCpup_DTIME
14685+ };
1facf9fc 14686+
4a4d8108
AM
14687+ FiMustWriteLock(file);
14688+
14689+ err = 0;
14690+ finfo = au_fi(file);
c2b27bf2 14691+ sb = cpg.dentry->d_sb;
5527c038 14692+ inode = d_inode(cpg.dentry);
5afbbe0d 14693+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14694+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14695+ goto out;
dece6358 14696+
c2b27bf2
AM
14697+ parent = dget_parent(cpg.dentry);
14698+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14699+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14700+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14701+ cpg.bdst = err;
4a4d8108
AM
14702+ di_read_unlock(parent, !AuLock_IR);
14703+ if (unlikely(err < 0))
14704+ goto out_parent;
14705+ err = 0;
1facf9fc 14706+ }
1facf9fc 14707+
4a4d8108 14708+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14709+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14710+ if (!S_ISDIR(inode->i_mode)
14711+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14712+ && au_plink_test(inode)
c2b27bf2 14713+ && !d_unhashed(cpg.dentry)
5afbbe0d 14714+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14715+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14716+ if (unlikely(err))
14717+ goto out_unlock;
14718+
14719+ /* always superio. */
c2b27bf2 14720+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14721+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14722+ if (!err) {
c2b27bf2 14723+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14724+ au_unpin(&pin);
14725+ }
4a4d8108
AM
14726+ } else if (hi_wh) {
14727+ /* already copied-up after unlink */
c2b27bf2 14728+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14729+ *need_reopen = 0;
14730+ }
1facf9fc 14731+
4f0767ce 14732+out_unlock:
4a4d8108 14733+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14734+out_parent:
4a4d8108 14735+ dput(parent);
4f0767ce 14736+out:
1308ab2a 14737+ return err;
dece6358 14738+}
1facf9fc 14739+
4a4d8108 14740+static void au_do_refresh_dir(struct file *file)
dece6358 14741+{
5afbbe0d 14742+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14743+ struct au_hfile *p, tmp, *q;
14744+ struct au_finfo *finfo;
1308ab2a 14745+ struct super_block *sb;
4a4d8108 14746+ struct au_fidir *fidir;
1facf9fc 14747+
4a4d8108 14748+ FiMustWriteLock(file);
1facf9fc 14749+
2000de60 14750+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14751+ finfo = au_fi(file);
14752+ fidir = finfo->fi_hdir;
14753+ AuDebugOn(!fidir);
14754+ p = fidir->fd_hfile + finfo->fi_btop;
14755+ brid = p->hf_br->br_id;
5afbbe0d
AM
14756+ bbot = fidir->fd_bbot;
14757+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14758+ if (!p->hf_file)
14759+ continue;
1308ab2a 14760+
4a4d8108
AM
14761+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14762+ if (new_bindex == bindex)
14763+ continue;
14764+ if (new_bindex < 0) {
14765+ au_set_h_fptr(file, bindex, NULL);
14766+ continue;
14767+ }
1308ab2a 14768+
4a4d8108
AM
14769+ /* swap two lower inode, and loop again */
14770+ q = fidir->fd_hfile + new_bindex;
14771+ tmp = *q;
14772+ *q = *p;
14773+ *p = tmp;
14774+ if (tmp.hf_file) {
14775+ bindex--;
14776+ p--;
14777+ }
14778+ }
1308ab2a 14779+
4a4d8108 14780+ p = fidir->fd_hfile;
2000de60 14781+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14782+ bbot = au_sbbot(sb);
14783+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14784+ finfo->fi_btop++, p++)
14785+ if (p->hf_file) {
c06a8ce3 14786+ if (file_inode(p->hf_file))
4a4d8108 14787+ break;
1c60b727 14788+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14789+ }
14790+ } else {
5afbbe0d
AM
14791+ bbot = au_br_index(sb, brid);
14792+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14793+ finfo->fi_btop++, p++)
14794+ if (p->hf_file)
1c60b727 14795+ au_hfput(p, /*execed*/0);
5afbbe0d 14796+ bbot = au_sbbot(sb);
4a4d8108 14797+ }
1308ab2a 14798+
5afbbe0d
AM
14799+ p = fidir->fd_hfile + bbot;
14800+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14801+ fidir->fd_bbot--, p--)
14802+ if (p->hf_file) {
c06a8ce3 14803+ if (file_inode(p->hf_file))
4a4d8108 14804+ break;
1c60b727 14805+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14806+ }
14807+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14808+}
14809+
4a4d8108
AM
14810+/*
14811+ * after branch manipulating, refresh the file.
14812+ */
14813+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14814+{
e2f27e51 14815+ int err, need_reopen, nbr;
5afbbe0d 14816+ aufs_bindex_t bbot, bindex;
4a4d8108 14817+ struct dentry *dentry;
e2f27e51 14818+ struct super_block *sb;
1308ab2a 14819+ struct au_finfo *finfo;
4a4d8108 14820+ struct au_hfile *hfile;
1facf9fc 14821+
2000de60 14822+ dentry = file->f_path.dentry;
e2f27e51
AM
14823+ sb = dentry->d_sb;
14824+ nbr = au_sbbot(sb) + 1;
1308ab2a 14825+ finfo = au_fi(file);
4a4d8108
AM
14826+ if (!finfo->fi_hdir) {
14827+ hfile = &finfo->fi_htop;
14828+ AuDebugOn(!hfile->hf_file);
e2f27e51 14829+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14830+ AuDebugOn(bindex < 0);
14831+ if (bindex != finfo->fi_btop)
5afbbe0d 14832+ au_set_fbtop(file, bindex);
4a4d8108 14833+ } else {
e2f27e51 14834+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14835+ if (unlikely(err))
14836+ goto out;
14837+ au_do_refresh_dir(file);
14838+ }
1facf9fc 14839+
4a4d8108
AM
14840+ err = 0;
14841+ need_reopen = 1;
14842+ if (!au_test_mmapped(file))
14843+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14844+ if (finfo->fi_hdir)
14845+ /* harmless if err */
14846+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14847+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14848+ err = reopen(file);
14849+ if (!err) {
14850+ au_update_figen(file);
14851+ goto out; /* success */
14852+ }
14853+
14854+ /* error, close all lower files */
14855+ if (finfo->fi_hdir) {
5afbbe0d
AM
14856+ bbot = au_fbbot_dir(file);
14857+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14858+ au_set_h_fptr(file, bindex, NULL);
14859+ }
1facf9fc 14860+
4f0767ce 14861+out:
1facf9fc 14862+ return err;
14863+}
14864+
4a4d8108
AM
14865+/* common function to regular file and dir */
14866+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14867+ int wlock, unsigned int fi_lsc)
dece6358 14868+{
1308ab2a 14869+ int err;
4a4d8108 14870+ unsigned int sigen, figen;
5afbbe0d 14871+ aufs_bindex_t btop;
4a4d8108
AM
14872+ unsigned char pseudo_link;
14873+ struct dentry *dentry;
14874+ struct inode *inode;
1facf9fc 14875+
4a4d8108 14876+ err = 0;
2000de60 14877+ dentry = file->f_path.dentry;
5527c038 14878+ inode = d_inode(dentry);
4a4d8108 14879+ sigen = au_sigen(dentry->d_sb);
521ced18 14880+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14881+ figen = au_figen(file);
521ced18
JR
14882+ if (!fi_lsc)
14883+ di_write_lock_child(dentry);
14884+ else
14885+ di_write_lock_child2(dentry);
5afbbe0d
AM
14886+ btop = au_dbtop(dentry);
14887+ pseudo_link = (btop != au_ibtop(inode));
14888+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14889+ if (!wlock) {
14890+ di_downgrade_lock(dentry, AuLock_IR);
14891+ fi_downgrade_lock(file);
14892+ }
14893+ goto out; /* success */
14894+ }
dece6358 14895+
4a4d8108 14896+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14897+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14898+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14899+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14900+ }
dece6358 14901+
027c5e7a
AM
14902+ if (!err)
14903+ err = refresh_file(file, reopen);
4a4d8108
AM
14904+ if (!err) {
14905+ if (!wlock) {
14906+ di_downgrade_lock(dentry, AuLock_IR);
14907+ fi_downgrade_lock(file);
14908+ }
14909+ } else {
14910+ di_write_unlock(dentry);
14911+ fi_write_unlock(file);
14912+ }
1facf9fc 14913+
4f0767ce 14914+out:
1308ab2a 14915+ return err;
14916+}
1facf9fc 14917+
4a4d8108
AM
14918+/* ---------------------------------------------------------------------- */
14919+
14920+/* cf. aufs_nopage() */
14921+/* for madvise(2) */
14922+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 14923+{
4a4d8108
AM
14924+ unlock_page(page);
14925+ return 0;
14926+}
1facf9fc 14927+
4a4d8108 14928+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 14929+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 14930+{ BUG(); return 0; }
1facf9fc 14931+
4a4d8108
AM
14932+/* they will never be called. */
14933+#ifdef CONFIG_AUFS_DEBUG
14934+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14935+ loff_t pos, unsigned len, unsigned flags,
14936+ struct page **pagep, void **fsdata)
14937+{ AuUnsupport(); return 0; }
14938+static int aufs_write_end(struct file *file, struct address_space *mapping,
14939+ loff_t pos, unsigned len, unsigned copied,
14940+ struct page *page, void *fsdata)
14941+{ AuUnsupport(); return 0; }
14942+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14943+{ AuUnsupport(); return 0; }
1308ab2a 14944+
4a4d8108
AM
14945+static int aufs_set_page_dirty(struct page *page)
14946+{ AuUnsupport(); return 0; }
392086de
AM
14947+static void aufs_invalidatepage(struct page *page, unsigned int offset,
14948+ unsigned int length)
4a4d8108
AM
14949+{ AuUnsupport(); }
14950+static int aufs_releasepage(struct page *page, gfp_t gfp)
14951+{ AuUnsupport(); return 0; }
79b8bda9 14952+#if 0 /* called by memory compaction regardless file */
4a4d8108 14953+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 14954+ struct page *page, enum migrate_mode mode)
4a4d8108 14955+{ AuUnsupport(); return 0; }
79b8bda9 14956+#endif
e2f27e51
AM
14957+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14958+{ AuUnsupport(); return true; }
14959+static void aufs_putback_page(struct page *page)
14960+{ AuUnsupport(); }
4a4d8108
AM
14961+static int aufs_launder_page(struct page *page)
14962+{ AuUnsupport(); return 0; }
14963+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
14964+ unsigned long from,
14965+ unsigned long count)
4a4d8108 14966+{ AuUnsupport(); return 0; }
392086de
AM
14967+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14968+ bool *writeback)
14969+{ AuUnsupport(); }
4a4d8108
AM
14970+static int aufs_error_remove_page(struct address_space *mapping,
14971+ struct page *page)
14972+{ AuUnsupport(); return 0; }
b4510431
AM
14973+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14974+ sector_t *span)
14975+{ AuUnsupport(); return 0; }
14976+static void aufs_swap_deactivate(struct file *file)
14977+{ AuUnsupport(); }
4a4d8108
AM
14978+#endif /* CONFIG_AUFS_DEBUG */
14979+
14980+const struct address_space_operations aufs_aop = {
14981+ .readpage = aufs_readpage,
14982+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
14983+#ifdef CONFIG_AUFS_DEBUG
14984+ .writepage = aufs_writepage,
4a4d8108
AM
14985+ /* no writepages, because of writepage */
14986+ .set_page_dirty = aufs_set_page_dirty,
14987+ /* no readpages, because of readpage */
14988+ .write_begin = aufs_write_begin,
14989+ .write_end = aufs_write_end,
14990+ /* no bmap, no block device */
14991+ .invalidatepage = aufs_invalidatepage,
14992+ .releasepage = aufs_releasepage,
79b8bda9
AM
14993+ /* is fallback_migrate_page ok? */
14994+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
14995+ .isolate_page = aufs_isolate_page,
14996+ .putback_page = aufs_putback_page,
4a4d8108
AM
14997+ .launder_page = aufs_launder_page,
14998+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 14999+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
15000+ .error_remove_page = aufs_error_remove_page,
15001+ .swap_activate = aufs_swap_activate,
15002+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 15003+#endif /* CONFIG_AUFS_DEBUG */
dece6358 15004+};
7f207e10 15005diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
eca34b5c
AM
15006--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
15007+++ linux/fs/aufs/file.h 2019-07-11 15:42:14.465571210 +0200
9f237c51 15008@@ -0,0 +1,342 @@
062440b3 15009+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 15010+/*
ba1aed25 15011+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
15012+ *
15013+ * This program, aufs is free software; you can redistribute it and/or modify
15014+ * it under the terms of the GNU General Public License as published by
15015+ * the Free Software Foundation; either version 2 of the License, or
15016+ * (at your option) any later version.
15017+ *
15018+ * This program is distributed in the hope that it will be useful,
15019+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15020+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15021+ * GNU General Public License for more details.
15022+ *
15023+ * You should have received a copy of the GNU General Public License
523b37e3 15024+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15025+ */
1facf9fc 15026+
4a4d8108
AM
15027+/*
15028+ * file operations
15029+ */
1facf9fc 15030+
4a4d8108
AM
15031+#ifndef __AUFS_FILE_H__
15032+#define __AUFS_FILE_H__
1facf9fc 15033+
4a4d8108 15034+#ifdef __KERNEL__
1facf9fc 15035+
2cbb1c4b 15036+#include <linux/file.h>
4a4d8108 15037+#include <linux/fs.h>
3c1bdaff 15038+#include <linux/mm_types.h>
4a4d8108 15039+#include <linux/poll.h>
4a4d8108 15040+#include "rwsem.h"
1facf9fc 15041+
4a4d8108
AM
15042+struct au_branch;
15043+struct au_hfile {
15044+ struct file *hf_file;
15045+ struct au_branch *hf_br;
15046+};
1facf9fc 15047+
4a4d8108
AM
15048+struct au_vdir;
15049+struct au_fidir {
15050+ aufs_bindex_t fd_bbot;
15051+ aufs_bindex_t fd_nent;
15052+ struct au_vdir *fd_vdir_cache;
15053+ struct au_hfile fd_hfile[];
15054+};
1facf9fc 15055+
4a4d8108 15056+static inline int au_fidir_sz(int nent)
dece6358 15057+{
4f0767ce
JR
15058+ AuDebugOn(nent < 0);
15059+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 15060+}
1facf9fc 15061+
4a4d8108
AM
15062+struct au_finfo {
15063+ atomic_t fi_generation;
dece6358 15064+
4a4d8108
AM
15065+ struct au_rwsem fi_rwsem;
15066+ aufs_bindex_t fi_btop;
15067+
15068+ /* do not union them */
15069+ struct { /* for non-dir */
15070+ struct au_hfile fi_htop;
2cbb1c4b 15071+ atomic_t fi_mmapped;
4a4d8108
AM
15072+ };
15073+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15074+
8b6a4947 15075+ struct hlist_bl_node fi_hlist;
1c60b727 15076+ struct file *fi_file; /* very ugly */
9f237c51 15077+ struct rcu_head rcu;
4a4d8108 15078+} ____cacheline_aligned_in_smp;
1facf9fc 15079+
4a4d8108 15080+/* ---------------------------------------------------------------------- */
1facf9fc 15081+
4a4d8108
AM
15082+/* file.c */
15083+extern const struct address_space_operations aufs_aop;
15084+unsigned int au_file_roflags(unsigned int flags);
15085+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15086+ struct file *file, int force_wr);
b912730e 15087+struct au_do_open_args {
8b6a4947 15088+ int aopen;
b912730e
AM
15089+ int (*open)(struct file *file, int flags,
15090+ struct file *h_file);
15091+ struct au_fidir *fidir;
15092+ struct file *h_file;
15093+};
15094+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15095+int au_reopen_nondir(struct file *file);
15096+struct au_pin;
15097+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15098+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15099+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15100+int au_do_flush(struct file *file, fl_owner_t id,
15101+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15102+
4a4d8108
AM
15103+/* poll.c */
15104+#ifdef CONFIG_AUFS_POLL
cd7a4cd9 15105+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
4a4d8108 15106+#endif
1facf9fc 15107+
4a4d8108
AM
15108+#ifdef CONFIG_AUFS_BR_HFSPLUS
15109+/* hfsplus.c */
392086de
AM
15110+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15111+ int force_wr);
4a4d8108
AM
15112+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15113+ struct file *h_file);
15114+#else
c1595e42
JR
15115+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15116+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15117+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15118+ struct file *h_file);
15119+#endif
1facf9fc 15120+
4a4d8108
AM
15121+/* f_op.c */
15122+extern const struct file_operations aufs_file_fop;
b912730e 15123+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15124+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15125+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15126+
4a4d8108 15127+/* finfo.c */
f0c0a007 15128+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15129+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15130+ struct file *h_file);
1facf9fc 15131+
4a4d8108 15132+void au_update_figen(struct file *file);
4a4d8108 15133+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15134+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15135+
4a4d8108 15136+void au_fi_init_once(void *_fi);
1c60b727 15137+void au_finfo_fin(struct file *file);
4a4d8108 15138+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15139+
4a4d8108
AM
15140+/* ioctl.c */
15141+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15142+#ifdef CONFIG_COMPAT
15143+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15144+ unsigned long arg);
c2b27bf2
AM
15145+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15146+ unsigned long arg);
b752ccd1 15147+#endif
1facf9fc 15148+
4a4d8108 15149+/* ---------------------------------------------------------------------- */
1facf9fc 15150+
4a4d8108
AM
15151+static inline struct au_finfo *au_fi(struct file *file)
15152+{
38d290e6 15153+ return file->private_data;
4a4d8108 15154+}
1facf9fc 15155+
4a4d8108 15156+/* ---------------------------------------------------------------------- */
1facf9fc 15157+
8b6a4947
AM
15158+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15159+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15160+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15161+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15162+/*
8b6a4947
AM
15163+#define fi_read_trylock_nested(f) \
15164+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15165+#define fi_write_trylock_nested(f) \
15166+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15167+*/
15168+
15169+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15170+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15171+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15172+
521ced18
JR
15173+/* lock subclass for finfo */
15174+enum {
15175+ AuLsc_FI_1,
15176+ AuLsc_FI_2
15177+};
15178+
15179+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15180+{
15181+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15182+}
15183+
15184+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15185+{
15186+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15187+}
15188+
15189+/*
15190+ * fi_read_lock_1, fi_write_lock_1,
15191+ * fi_read_lock_2, fi_write_lock_2
15192+ */
15193+#define AuReadLockFunc(name) \
15194+static inline void fi_read_lock_##name(struct file *f) \
15195+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15196+
15197+#define AuWriteLockFunc(name) \
15198+static inline void fi_write_lock_##name(struct file *f) \
15199+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15200+
15201+#define AuRWLockFuncs(name) \
15202+ AuReadLockFunc(name) \
15203+ AuWriteLockFunc(name)
15204+
15205+AuRWLockFuncs(1);
15206+AuRWLockFuncs(2);
15207+
15208+#undef AuReadLockFunc
15209+#undef AuWriteLockFunc
15210+#undef AuRWLockFuncs
15211+
4a4d8108
AM
15212+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15213+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15214+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15215+
1308ab2a 15216+/* ---------------------------------------------------------------------- */
15217+
4a4d8108 15218+/* todo: hard/soft set? */
5afbbe0d 15219+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15220+{
4a4d8108
AM
15221+ FiMustAnyLock(file);
15222+ return au_fi(file)->fi_btop;
15223+}
dece6358 15224+
5afbbe0d 15225+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15226+{
15227+ FiMustAnyLock(file);
15228+ AuDebugOn(!au_fi(file)->fi_hdir);
15229+ return au_fi(file)->fi_hdir->fd_bbot;
15230+}
1facf9fc 15231+
4a4d8108
AM
15232+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15233+{
15234+ FiMustAnyLock(file);
15235+ AuDebugOn(!au_fi(file)->fi_hdir);
15236+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15237+}
1facf9fc 15238+
5afbbe0d 15239+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15240+{
15241+ FiMustWriteLock(file);
15242+ au_fi(file)->fi_btop = bindex;
15243+}
1facf9fc 15244+
5afbbe0d 15245+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15246+{
15247+ FiMustWriteLock(file);
15248+ AuDebugOn(!au_fi(file)->fi_hdir);
15249+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15250+}
1308ab2a 15251+
4a4d8108
AM
15252+static inline void au_set_fvdir_cache(struct file *file,
15253+ struct au_vdir *vdir_cache)
15254+{
15255+ FiMustWriteLock(file);
15256+ AuDebugOn(!au_fi(file)->fi_hdir);
15257+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15258+}
dece6358 15259+
4a4d8108
AM
15260+static inline struct file *au_hf_top(struct file *file)
15261+{
15262+ FiMustAnyLock(file);
15263+ AuDebugOn(au_fi(file)->fi_hdir);
15264+ return au_fi(file)->fi_htop.hf_file;
15265+}
1facf9fc 15266+
4a4d8108
AM
15267+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15268+{
15269+ FiMustAnyLock(file);
15270+ AuDebugOn(!au_fi(file)->fi_hdir);
15271+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15272+}
15273+
4a4d8108
AM
15274+/* todo: memory barrier? */
15275+static inline unsigned int au_figen(struct file *f)
dece6358 15276+{
4a4d8108
AM
15277+ return atomic_read(&au_fi(f)->fi_generation);
15278+}
dece6358 15279+
2cbb1c4b
JR
15280+static inline void au_set_mmapped(struct file *f)
15281+{
15282+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15283+ return;
0c3ec466 15284+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15285+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15286+ ;
15287+}
15288+
15289+static inline void au_unset_mmapped(struct file *f)
15290+{
15291+ atomic_dec(&au_fi(f)->fi_mmapped);
15292+}
15293+
4a4d8108
AM
15294+static inline int au_test_mmapped(struct file *f)
15295+{
2cbb1c4b
JR
15296+ return atomic_read(&au_fi(f)->fi_mmapped);
15297+}
15298+
15299+/* customize vma->vm_file */
15300+
15301+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15302+ struct file *file)
15303+{
53392da6
AM
15304+ struct file *f;
15305+
15306+ f = vma->vm_file;
2cbb1c4b
JR
15307+ get_file(file);
15308+ vma->vm_file = file;
53392da6 15309+ fput(f);
2cbb1c4b
JR
15310+}
15311+
15312+#ifdef CONFIG_MMU
15313+#define AuDbgVmRegion(file, vma) do {} while (0)
15314+
15315+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15316+ struct file *file)
15317+{
15318+ au_do_vm_file_reset(vma, file);
15319+}
15320+#else
15321+#define AuDbgVmRegion(file, vma) \
15322+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15323+
15324+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15325+ struct file *file)
15326+{
53392da6
AM
15327+ struct file *f;
15328+
2cbb1c4b 15329+ au_do_vm_file_reset(vma, file);
53392da6 15330+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15331+ get_file(file);
15332+ vma->vm_region->vm_file = file;
53392da6 15333+ fput(f);
2cbb1c4b
JR
15334+}
15335+#endif /* CONFIG_MMU */
15336+
15337+/* handle vma->vm_prfile */
fb47a38f 15338+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15339+ struct file *file)
15340+{
2cbb1c4b
JR
15341+ get_file(file);
15342+ vma->vm_prfile = file;
15343+#ifndef CONFIG_MMU
15344+ get_file(file);
15345+ vma->vm_region->vm_prfile = file;
15346+#endif
fb47a38f 15347+}
1308ab2a 15348+
4a4d8108
AM
15349+#endif /* __KERNEL__ */
15350+#endif /* __AUFS_FILE_H__ */
7f207e10 15351diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
eca34b5c
AM
15352--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
15353+++ linux/fs/aufs/finfo.c 2019-07-11 15:42:14.465571210 +0200
062440b3 15354@@ -0,0 +1,149 @@
cd7a4cd9 15355+// SPDX-License-Identifier: GPL-2.0
4a4d8108 15356+/*
ba1aed25 15357+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
15358+ *
15359+ * This program, aufs is free software; you can redistribute it and/or modify
15360+ * it under the terms of the GNU General Public License as published by
15361+ * the Free Software Foundation; either version 2 of the License, or
15362+ * (at your option) any later version.
15363+ *
15364+ * This program is distributed in the hope that it will be useful,
15365+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15366+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15367+ * GNU General Public License for more details.
15368+ *
15369+ * You should have received a copy of the GNU General Public License
523b37e3 15370+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15371+ */
1308ab2a 15372+
4a4d8108
AM
15373+/*
15374+ * file private data
15375+ */
1facf9fc 15376+
4a4d8108 15377+#include "aufs.h"
1facf9fc 15378+
f0c0a007 15379+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15380+{
f0c0a007 15381+ if (execed)
4a4d8108
AM
15382+ allow_write_access(hf->hf_file);
15383+ fput(hf->hf_file);
15384+ hf->hf_file = NULL;
acd2b654 15385+ au_lcnt_dec(&hf->hf_br->br_nfiles);
4a4d8108
AM
15386+ hf->hf_br = NULL;
15387+}
1facf9fc 15388+
4a4d8108
AM
15389+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15390+{
15391+ struct au_finfo *finfo = au_fi(file);
15392+ struct au_hfile *hf;
15393+ struct au_fidir *fidir;
15394+
15395+ fidir = finfo->fi_hdir;
15396+ if (!fidir) {
15397+ AuDebugOn(finfo->fi_btop != bindex);
15398+ hf = &finfo->fi_htop;
15399+ } else
15400+ hf = fidir->fd_hfile + bindex;
15401+
15402+ if (hf && hf->hf_file)
f0c0a007 15403+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15404+ if (val) {
15405+ FiMustWriteLock(file);
b912730e 15406+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15407+ hf->hf_file = val;
2000de60 15408+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15409+ }
4a4d8108 15410+}
1facf9fc 15411+
4a4d8108
AM
15412+void au_update_figen(struct file *file)
15413+{
2000de60 15414+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15415+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15416+}
15417+
4a4d8108
AM
15418+/* ---------------------------------------------------------------------- */
15419+
4a4d8108
AM
15420+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15421+{
15422+ struct au_fidir *fidir;
15423+ int nbr;
15424+
5afbbe0d 15425+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15426+ if (nbr < 2)
15427+ nbr = 2; /* initial allocate for 2 branches */
15428+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15429+ if (fidir) {
15430+ fidir->fd_bbot = -1;
15431+ fidir->fd_nent = nbr;
4a4d8108
AM
15432+ }
15433+
15434+ return fidir;
15435+}
15436+
e2f27e51 15437+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15438+{
15439+ int err;
15440+ struct au_fidir *fidir, *p;
15441+
15442+ AuRwMustWriteLock(&finfo->fi_rwsem);
15443+ fidir = finfo->fi_hdir;
15444+ AuDebugOn(!fidir);
15445+
15446+ err = -ENOMEM;
15447+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15448+ GFP_NOFS, may_shrink);
4a4d8108
AM
15449+ if (p) {
15450+ p->fd_nent = nbr;
15451+ finfo->fi_hdir = p;
15452+ err = 0;
15453+ }
1facf9fc 15454+
dece6358 15455+ return err;
1facf9fc 15456+}
1308ab2a 15457+
15458+/* ---------------------------------------------------------------------- */
15459+
1c60b727 15460+void au_finfo_fin(struct file *file)
1308ab2a 15461+{
4a4d8108
AM
15462+ struct au_finfo *finfo;
15463+
acd2b654 15464+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
7f207e10 15465+
4a4d8108
AM
15466+ finfo = au_fi(file);
15467+ AuDebugOn(finfo->fi_hdir);
15468+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15469+ au_cache_free_finfo(finfo);
1308ab2a 15470+}
1308ab2a 15471+
e49829fe 15472+void au_fi_init_once(void *_finfo)
4a4d8108 15473+{
e49829fe 15474+ struct au_finfo *finfo = _finfo;
1308ab2a 15475+
e49829fe 15476+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15477+}
1308ab2a 15478+
4a4d8108
AM
15479+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15480+{
1716fcea 15481+ int err;
4a4d8108
AM
15482+ struct au_finfo *finfo;
15483+ struct dentry *dentry;
15484+
15485+ err = -ENOMEM;
2000de60 15486+ dentry = file->f_path.dentry;
4a4d8108
AM
15487+ finfo = au_cache_alloc_finfo();
15488+ if (unlikely(!finfo))
15489+ goto out;
15490+
15491+ err = 0;
acd2b654 15492+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
4a4d8108
AM
15493+ au_rw_write_lock(&finfo->fi_rwsem);
15494+ finfo->fi_btop = -1;
15495+ finfo->fi_hdir = fidir;
15496+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15497+ /* smp_mb(); */ /* atomic_set */
15498+
15499+ file->private_data = finfo;
15500+
15501+out:
15502+ return err;
15503+}
7f207e10 15504diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
eca34b5c
AM
15505--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
15506+++ linux/fs/aufs/f_op.c 2019-07-11 15:42:14.465571210 +0200
acd2b654 15507@@ -0,0 +1,819 @@
cd7a4cd9 15508+// SPDX-License-Identifier: GPL-2.0
dece6358 15509+/*
ba1aed25 15510+ * Copyright (C) 2005-2019 Junjiro R. Okajima
dece6358
AM
15511+ *
15512+ * This program, aufs is free software; you can redistribute it and/or modify
15513+ * it under the terms of the GNU General Public License as published by
15514+ * the Free Software Foundation; either version 2 of the License, or
15515+ * (at your option) any later version.
15516+ *
15517+ * This program is distributed in the hope that it will be useful,
15518+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15519+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15520+ * GNU General Public License for more details.
15521+ *
15522+ * You should have received a copy of the GNU General Public License
523b37e3 15523+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15524+ */
1facf9fc 15525+
15526+/*
4a4d8108 15527+ * file and vm operations
1facf9fc 15528+ */
dece6358 15529+
86dc4139 15530+#include <linux/aio.h>
4a4d8108
AM
15531+#include <linux/fs_stack.h>
15532+#include <linux/mman.h>
4a4d8108 15533+#include <linux/security.h>
dece6358
AM
15534+#include "aufs.h"
15535+
b912730e 15536+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15537+{
4a4d8108
AM
15538+ int err;
15539+ aufs_bindex_t bindex;
8cdd5066 15540+ struct dentry *dentry, *h_dentry;
4a4d8108 15541+ struct au_finfo *finfo;
38d290e6 15542+ struct inode *h_inode;
4a4d8108
AM
15543+
15544+ FiMustWriteLock(file);
15545+
523b37e3 15546+ err = 0;
2000de60 15547+ dentry = file->f_path.dentry;
b912730e 15548+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15549+ finfo = au_fi(file);
15550+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15551+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15552+ bindex = au_dbtop(dentry);
8cdd5066
JR
15553+ if (!h_file) {
15554+ h_dentry = au_h_dptr(dentry, bindex);
15555+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15556+ if (unlikely(err))
15557+ goto out;
b912730e 15558+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
acd2b654
AM
15559+ if (IS_ERR(h_file)) {
15560+ err = PTR_ERR(h_file);
15561+ goto out;
15562+ }
8cdd5066
JR
15563+ } else {
15564+ h_dentry = h_file->f_path.dentry;
15565+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15566+ if (unlikely(err))
15567+ goto out;
acd2b654 15568+ /* br ref is already inc-ed */
8cdd5066 15569+ }
acd2b654
AM
15570+
15571+ if ((flags & __O_TMPFILE)
15572+ && !(flags & O_EXCL)) {
15573+ h_inode = file_inode(h_file);
15574+ spin_lock(&h_inode->i_lock);
15575+ h_inode->i_state |= I_LINKABLE;
15576+ spin_unlock(&h_inode->i_lock);
4a4d8108 15577+ }
acd2b654
AM
15578+ au_set_fbtop(file, bindex);
15579+ au_set_h_fptr(file, bindex, h_file);
15580+ au_update_figen(file);
15581+ /* todo: necessary? */
15582+ /* file->f_ra = h_file->f_ra; */
027c5e7a 15583+
8cdd5066 15584+out:
4a4d8108 15585+ return err;
1facf9fc 15586+}
15587+
4a4d8108
AM
15588+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15589+ struct file *file)
1facf9fc 15590+{
4a4d8108 15591+ int err;
1308ab2a 15592+ struct super_block *sb;
b912730e
AM
15593+ struct au_do_open_args args = {
15594+ .open = au_do_open_nondir
15595+ };
1facf9fc 15596+
523b37e3
AM
15597+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15598+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15599+
2000de60 15600+ sb = file->f_path.dentry->d_sb;
4a4d8108 15601+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15602+ err = au_do_open(file, &args);
4a4d8108
AM
15603+ si_read_unlock(sb);
15604+ return err;
15605+}
1facf9fc 15606+
4a4d8108
AM
15607+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15608+{
15609+ struct au_finfo *finfo;
15610+ aufs_bindex_t bindex;
1facf9fc 15611+
4a4d8108 15612+ finfo = au_fi(file);
8b6a4947
AM
15613+ au_hbl_del(&finfo->fi_hlist,
15614+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15615+ bindex = finfo->fi_btop;
b4510431 15616+ if (bindex >= 0)
4a4d8108 15617+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15618+
1c60b727 15619+ au_finfo_fin(file);
4a4d8108 15620+ return 0;
1facf9fc 15621+}
15622+
4a4d8108
AM
15623+/* ---------------------------------------------------------------------- */
15624+
15625+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15626+{
1308ab2a 15627+ int err;
4a4d8108
AM
15628+ struct file *h_file;
15629+
15630+ err = 0;
15631+ h_file = au_hf_top(file);
15632+ if (h_file)
15633+ err = vfsub_flush(h_file, id);
15634+ return err;
15635+}
15636+
15637+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15638+{
15639+ return au_do_flush(file, id, au_do_flush_nondir);
15640+}
15641+
15642+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15643+/*
15644+ * read and write functions acquire [fdi]_rwsem once, but release before
15645+ * mmap_sem. This is because to stop a race condition between mmap(2).
acd2b654 15646+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
9dbd164d
AM
15647+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15648+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15649+ */
4a4d8108 15650+
b912730e 15651+/* Callers should call au_read_post() or fput() in the end */
521ced18 15652+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15653+{
4a4d8108 15654+ struct file *h_file;
b912730e 15655+ int err;
1facf9fc 15656+
521ced18 15657+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15658+ if (!err) {
15659+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15660+ h_file = au_hf_top(file);
15661+ get_file(h_file);
15662+ if (!keep_fi)
15663+ fi_read_unlock(file);
15664+ } else
15665+ h_file = ERR_PTR(err);
15666+
15667+ return h_file;
15668+}
15669+
15670+static void au_read_post(struct inode *inode, struct file *h_file)
15671+{
15672+ /* update without lock, I don't think it a problem */
15673+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15674+ fput(h_file);
15675+}
15676+
15677+struct au_write_pre {
521ced18
JR
15678+ /* input */
15679+ unsigned int lsc;
15680+
15681+ /* output */
b912730e 15682+ blkcnt_t blks;
5afbbe0d 15683+ aufs_bindex_t btop;
b912730e
AM
15684+};
15685+
15686+/*
15687+ * return with iinfo is write-locked
15688+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15689+ * end
15690+ */
15691+static struct file *au_write_pre(struct file *file, int do_ready,
15692+ struct au_write_pre *wpre)
15693+{
15694+ struct file *h_file;
15695+ struct dentry *dentry;
15696+ int err;
521ced18 15697+ unsigned int lsc;
b912730e
AM
15698+ struct au_pin pin;
15699+
521ced18
JR
15700+ lsc = 0;
15701+ if (wpre)
15702+ lsc = wpre->lsc;
15703+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15704+ h_file = ERR_PTR(err);
dece6358
AM
15705+ if (unlikely(err))
15706+ goto out;
1facf9fc 15707+
b912730e
AM
15708+ dentry = file->f_path.dentry;
15709+ if (do_ready) {
15710+ err = au_ready_to_write(file, -1, &pin);
15711+ if (unlikely(err)) {
15712+ h_file = ERR_PTR(err);
15713+ di_write_unlock(dentry);
15714+ goto out_fi;
15715+ }
15716+ }
15717+
15718+ di_downgrade_lock(dentry, /*flags*/0);
15719+ if (wpre)
5afbbe0d 15720+ wpre->btop = au_fbtop(file);
4a4d8108 15721+ h_file = au_hf_top(file);
9dbd164d 15722+ get_file(h_file);
b912730e
AM
15723+ if (wpre)
15724+ wpre->blks = file_inode(h_file)->i_blocks;
15725+ if (do_ready)
15726+ au_unpin(&pin);
15727+ di_read_unlock(dentry, /*flags*/0);
15728+
15729+out_fi:
15730+ fi_write_unlock(file);
15731+out:
15732+ return h_file;
15733+}
15734+
15735+static void au_write_post(struct inode *inode, struct file *h_file,
15736+ struct au_write_pre *wpre, ssize_t written)
15737+{
15738+ struct inode *h_inode;
15739+
15740+ au_cpup_attr_timesizes(inode);
5afbbe0d 15741+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15742+ h_inode = file_inode(h_file);
15743+ inode->i_mode = h_inode->i_mode;
15744+ ii_write_unlock(inode);
b912730e
AM
15745+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15746+ if (written > 0)
5afbbe0d 15747+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15748+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15749+ fput(h_file);
b912730e
AM
15750+}
15751+
15752+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15753+ loff_t *ppos)
15754+{
15755+ ssize_t err;
15756+ struct inode *inode;
15757+ struct file *h_file;
15758+ struct super_block *sb;
15759+
15760+ inode = file_inode(file);
15761+ sb = inode->i_sb;
15762+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15763+
521ced18 15764+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15765+ err = PTR_ERR(h_file);
15766+ if (IS_ERR(h_file))
15767+ goto out;
9dbd164d
AM
15768+
15769+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
15770+ err = vfsub_read_u(h_file, buf, count, ppos);
15771+ /* todo: necessary? */
15772+ /* file->f_ra = h_file->f_ra; */
b912730e 15773+ au_read_post(inode, h_file);
1308ab2a 15774+
4f0767ce 15775+out:
dece6358
AM
15776+ si_read_unlock(sb);
15777+ return err;
15778+}
1facf9fc 15779+
e49829fe
JR
15780+/*
15781+ * todo: very ugly
15782+ * it locks both of i_mutex and si_rwsem for read in safe.
15783+ * if the plink maintenance mode continues forever (that is the problem),
15784+ * may loop forever.
15785+ */
15786+static void au_mtx_and_read_lock(struct inode *inode)
15787+{
15788+ int err;
15789+ struct super_block *sb = inode->i_sb;
15790+
15791+ while (1) {
febd17d6 15792+ inode_lock(inode);
e49829fe
JR
15793+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15794+ if (!err)
15795+ break;
febd17d6 15796+ inode_unlock(inode);
e49829fe
JR
15797+ si_read_lock(sb, AuLock_NOPLMW);
15798+ si_read_unlock(sb);
15799+ }
15800+}
15801+
4a4d8108
AM
15802+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15803+ size_t count, loff_t *ppos)
dece6358 15804+{
4a4d8108 15805+ ssize_t err;
b912730e
AM
15806+ struct au_write_pre wpre;
15807+ struct inode *inode;
4a4d8108
AM
15808+ struct file *h_file;
15809+ char __user *buf = (char __user *)ubuf;
1facf9fc 15810+
b912730e 15811+ inode = file_inode(file);
e49829fe 15812+ au_mtx_and_read_lock(inode);
1facf9fc 15813+
521ced18 15814+ wpre.lsc = 0;
b912730e
AM
15815+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15816+ err = PTR_ERR(h_file);
15817+ if (IS_ERR(h_file))
9dbd164d 15818+ goto out;
9dbd164d 15819+
4a4d8108 15820+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 15821+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15822+
4f0767ce 15823+out:
b912730e 15824+ si_read_unlock(inode->i_sb);
febd17d6 15825+ inode_unlock(inode);
dece6358
AM
15826+ return err;
15827+}
1facf9fc 15828+
076b876e
AM
15829+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15830+ struct iov_iter *iov_iter)
dece6358 15831+{
4a4d8108
AM
15832+ ssize_t err;
15833+ struct file *file;
076b876e 15834+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15835+
4a4d8108
AM
15836+ err = security_file_permission(h_file, rw);
15837+ if (unlikely(err))
15838+ goto out;
1facf9fc 15839+
4a4d8108 15840+ err = -ENOSYS;
076b876e 15841+ iter = NULL;
5527c038 15842+ if (rw == MAY_READ)
076b876e 15843+ iter = h_file->f_op->read_iter;
5527c038 15844+ else if (rw == MAY_WRITE)
076b876e 15845+ iter = h_file->f_op->write_iter;
076b876e
AM
15846+
15847+ file = kio->ki_filp;
15848+ kio->ki_filp = h_file;
15849+ if (iter) {
2cbb1c4b 15850+ lockdep_off();
076b876e
AM
15851+ err = iter(kio, iov_iter);
15852+ lockdep_on();
4a4d8108
AM
15853+ } else
15854+ /* currently there is no such fs */
15855+ WARN_ON_ONCE(1);
076b876e 15856+ kio->ki_filp = file;
1facf9fc 15857+
4f0767ce 15858+out:
dece6358
AM
15859+ return err;
15860+}
1facf9fc 15861+
076b876e 15862+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15863+{
4a4d8108
AM
15864+ ssize_t err;
15865+ struct file *file, *h_file;
b912730e 15866+ struct inode *inode;
dece6358 15867+ struct super_block *sb;
1facf9fc 15868+
4a4d8108 15869+ file = kio->ki_filp;
b912730e
AM
15870+ inode = file_inode(file);
15871+ sb = inode->i_sb;
e49829fe 15872+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15873+
521ced18 15874+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15875+ err = PTR_ERR(h_file);
15876+ if (IS_ERR(h_file))
15877+ goto out;
9dbd164d 15878+
5afbbe0d
AM
15879+ if (au_test_loopback_kthread()) {
15880+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15881+ if (file->f_mapping != h_file->f_mapping) {
15882+ file->f_mapping = h_file->f_mapping;
15883+ smp_mb(); /* unnecessary? */
15884+ }
15885+ }
15886+ fi_read_unlock(file);
15887+
076b876e 15888+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15889+ /* todo: necessary? */
15890+ /* file->f_ra = h_file->f_ra; */
b912730e 15891+ au_read_post(inode, h_file);
1facf9fc 15892+
4f0767ce 15893+out:
4a4d8108 15894+ si_read_unlock(sb);
1308ab2a 15895+ return err;
15896+}
1facf9fc 15897+
076b876e 15898+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15899+{
4a4d8108 15900+ ssize_t err;
b912730e
AM
15901+ struct au_write_pre wpre;
15902+ struct inode *inode;
4a4d8108 15903+ struct file *file, *h_file;
1308ab2a 15904+
4a4d8108 15905+ file = kio->ki_filp;
b912730e 15906+ inode = file_inode(file);
e49829fe
JR
15907+ au_mtx_and_read_lock(inode);
15908+
521ced18 15909+ wpre.lsc = 0;
b912730e
AM
15910+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15911+ err = PTR_ERR(h_file);
15912+ if (IS_ERR(h_file))
9dbd164d 15913+ goto out;
9dbd164d 15914+
076b876e 15915+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 15916+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15917+
4f0767ce 15918+out:
b912730e 15919+ si_read_unlock(inode->i_sb);
febd17d6 15920+ inode_unlock(inode);
dece6358 15921+ return err;
1facf9fc 15922+}
15923+
4a4d8108
AM
15924+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15925+ struct pipe_inode_info *pipe, size_t len,
15926+ unsigned int flags)
1facf9fc 15927+{
4a4d8108
AM
15928+ ssize_t err;
15929+ struct file *h_file;
b912730e 15930+ struct inode *inode;
dece6358 15931+ struct super_block *sb;
1facf9fc 15932+
b912730e
AM
15933+ inode = file_inode(file);
15934+ sb = inode->i_sb;
e49829fe 15935+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 15936+
521ced18 15937+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15938+ err = PTR_ERR(h_file);
15939+ if (IS_ERR(h_file))
dece6358 15940+ goto out;
1facf9fc 15941+
4a4d8108 15942+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
acd2b654 15943+ /* todo: necessary? */
4a4d8108 15944+ /* file->f_ra = h_file->f_ra; */
b912730e 15945+ au_read_post(inode, h_file);
1facf9fc 15946+
4f0767ce 15947+out:
4a4d8108 15948+ si_read_unlock(sb);
dece6358 15949+ return err;
1facf9fc 15950+}
15951+
4a4d8108
AM
15952+static ssize_t
15953+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15954+ size_t len, unsigned int flags)
1facf9fc 15955+{
4a4d8108 15956+ ssize_t err;
b912730e
AM
15957+ struct au_write_pre wpre;
15958+ struct inode *inode;
076b876e 15959+ struct file *h_file;
1facf9fc 15960+
b912730e 15961+ inode = file_inode(file);
e49829fe 15962+ au_mtx_and_read_lock(inode);
9dbd164d 15963+
521ced18 15964+ wpre.lsc = 0;
b912730e
AM
15965+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15966+ err = PTR_ERR(h_file);
15967+ if (IS_ERR(h_file))
9dbd164d 15968+ goto out;
9dbd164d 15969+
4a4d8108 15970+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 15971+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15972+
4f0767ce 15973+out:
b912730e 15974+ si_read_unlock(inode->i_sb);
febd17d6 15975+ inode_unlock(inode);
4a4d8108
AM
15976+ return err;
15977+}
1facf9fc 15978+
38d290e6
JR
15979+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15980+ loff_t len)
15981+{
15982+ long err;
b912730e 15983+ struct au_write_pre wpre;
38d290e6
JR
15984+ struct inode *inode;
15985+ struct file *h_file;
15986+
b912730e 15987+ inode = file_inode(file);
38d290e6
JR
15988+ au_mtx_and_read_lock(inode);
15989+
521ced18 15990+ wpre.lsc = 0;
b912730e
AM
15991+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15992+ err = PTR_ERR(h_file);
15993+ if (IS_ERR(h_file))
38d290e6 15994+ goto out;
38d290e6
JR
15995+
15996+ lockdep_off();
03673fb0 15997+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 15998+ lockdep_on();
b912730e 15999+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
16000+
16001+out:
b912730e 16002+ si_read_unlock(inode->i_sb);
febd17d6 16003+ inode_unlock(inode);
38d290e6
JR
16004+ return err;
16005+}
16006+
521ced18
JR
16007+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16008+ struct file *dst, loff_t dst_pos,
16009+ size_t len, unsigned int flags)
16010+{
16011+ ssize_t err;
16012+ struct au_write_pre wpre;
16013+ enum { SRC, DST };
16014+ struct {
16015+ struct inode *inode;
16016+ struct file *h_file;
16017+ struct super_block *h_sb;
16018+ } a[2];
16019+#define a_src a[SRC]
16020+#define a_dst a[DST]
16021+
16022+ err = -EINVAL;
16023+ a_src.inode = file_inode(src);
16024+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16025+ goto out;
16026+ a_dst.inode = file_inode(dst);
16027+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16028+ goto out;
16029+
16030+ au_mtx_and_read_lock(a_dst.inode);
16031+ /*
16032+ * in order to match the order in di_write_lock2_{child,parent}(),
acd2b654 16033+ * use f_path.dentry for this comparison.
521ced18
JR
16034+ */
16035+ if (src->f_path.dentry < dst->f_path.dentry) {
16036+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16037+ err = PTR_ERR(a_src.h_file);
16038+ if (IS_ERR(a_src.h_file))
16039+ goto out_si;
16040+
16041+ wpre.lsc = AuLsc_FI_2;
16042+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16043+ err = PTR_ERR(a_dst.h_file);
16044+ if (IS_ERR(a_dst.h_file)) {
16045+ au_read_post(a_src.inode, a_src.h_file);
16046+ goto out_si;
16047+ }
16048+ } else {
16049+ wpre.lsc = AuLsc_FI_1;
16050+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16051+ err = PTR_ERR(a_dst.h_file);
16052+ if (IS_ERR(a_dst.h_file))
16053+ goto out_si;
16054+
16055+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16056+ err = PTR_ERR(a_src.h_file);
16057+ if (IS_ERR(a_src.h_file)) {
16058+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16059+ /*written*/0);
16060+ goto out_si;
16061+ }
16062+ }
16063+
16064+ err = -EXDEV;
16065+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16066+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16067+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16068+ AuDbgFile(src);
16069+ AuDbgFile(dst);
16070+ goto out_file;
16071+ }
16072+
16073+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16074+ dst_pos, len, flags);
16075+
16076+out_file:
16077+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16078+ fi_read_unlock(src);
16079+ au_read_post(a_src.inode, a_src.h_file);
16080+out_si:
16081+ si_read_unlock(a_dst.inode->i_sb);
16082+ inode_unlock(a_dst.inode);
16083+out:
16084+ return err;
16085+#undef a_src
16086+#undef a_dst
16087+}
16088+
4a4d8108
AM
16089+/* ---------------------------------------------------------------------- */
16090+
9dbd164d
AM
16091+/*
16092+ * The locking order around current->mmap_sem.
16093+ * - in most and regular cases
16094+ * file I/O syscall -- aufs_read() or something
16095+ * -- si_rwsem for read -- mmap_sem
16096+ * (Note that [fdi]i_rwsem are released before mmap_sem).
16097+ * - in mmap case
16098+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
acd2b654
AM
16099+ * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
16100+ * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16101+ * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
9dbd164d
AM
16102+ * It means that when aufs acquires si_rwsem for write, the process should never
16103+ * acquire mmap_sem.
16104+ *
392086de 16105+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
16106+ * problem either since any directory is not able to be mmap-ed.
16107+ * The similar scenario is applied to aufs_readlink() too.
16108+ */
16109+
38d290e6 16110+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16111+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16112+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16113+
16114+static unsigned long au_arch_prot_conv(unsigned long flags)
16115+{
16116+ /* currently ppc64 only */
16117+#ifdef CONFIG_PPC64
16118+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16119+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16120+ return AuConv_VM_PROT(flags, SAO);
16121+#else
16122+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16123+ return 0;
16124+#endif
16125+}
16126+
16127+static unsigned long au_prot_conv(unsigned long flags)
16128+{
16129+ return AuConv_VM_PROT(flags, READ)
16130+ | AuConv_VM_PROT(flags, WRITE)
16131+ | AuConv_VM_PROT(flags, EXEC)
16132+ | au_arch_prot_conv(flags);
16133+}
16134+
16135+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16136+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16137+
16138+static unsigned long au_flag_conv(unsigned long flags)
16139+{
16140+ return AuConv_VM_MAP(flags, GROWSDOWN)
16141+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16142+ | AuConv_VM_MAP(flags, LOCKED);
16143+}
38d290e6 16144+#endif
2dfbb274 16145+
9dbd164d 16146+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16147+{
4a4d8108 16148+ int err;
4a4d8108 16149+ const unsigned char wlock
9dbd164d 16150+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16151+ struct super_block *sb;
9dbd164d 16152+ struct file *h_file;
b912730e 16153+ struct inode *inode;
9dbd164d
AM
16154+
16155+ AuDbgVmRegion(file, vma);
1308ab2a 16156+
b912730e
AM
16157+ inode = file_inode(file);
16158+ sb = inode->i_sb;
9dbd164d 16159+ lockdep_off();
e49829fe 16160+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16161+
b912730e 16162+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16163+ lockdep_on();
b912730e
AM
16164+ err = PTR_ERR(h_file);
16165+ if (IS_ERR(h_file))
16166+ goto out;
1308ab2a 16167+
b912730e
AM
16168+ err = 0;
16169+ au_set_mmapped(file);
9dbd164d 16170+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16171+ /*
16172+ * we cannot call security_mmap_file() here since it may acquire
16173+ * mmap_sem or i_mutex.
16174+ *
16175+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16176+ * au_flag_conv(vma->vm_flags));
16177+ */
9dbd164d 16178+ if (!err)
521ced18 16179+ err = call_mmap(h_file, vma);
b912730e
AM
16180+ if (!err) {
16181+ au_vm_prfile_set(vma, file);
16182+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16183+ goto out_fput; /* success */
16184+ }
2cbb1c4b
JR
16185+ au_unset_mmapped(file);
16186+ au_vm_file_reset(vma, file);
b912730e 16187+
2cbb1c4b 16188+out_fput:
9dbd164d 16189+ lockdep_off();
b912730e
AM
16190+ ii_write_unlock(inode);
16191+ lockdep_on();
16192+ fput(h_file);
4f0767ce 16193+out:
b912730e 16194+ lockdep_off();
9dbd164d
AM
16195+ si_read_unlock(sb);
16196+ lockdep_on();
16197+ AuTraceErr(err);
4a4d8108
AM
16198+ return err;
16199+}
16200+
16201+/* ---------------------------------------------------------------------- */
16202+
1e00d052
AM
16203+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16204+ int datasync)
4a4d8108
AM
16205+{
16206+ int err;
b912730e 16207+ struct au_write_pre wpre;
4a4d8108
AM
16208+ struct inode *inode;
16209+ struct file *h_file;
4a4d8108
AM
16210+
16211+ err = 0; /* -EBADF; */ /* posix? */
16212+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16213+ goto out;
4a4d8108 16214+
b912730e
AM
16215+ inode = file_inode(file);
16216+ au_mtx_and_read_lock(inode);
16217+
521ced18 16218+ wpre.lsc = 0;
b912730e
AM
16219+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16220+ err = PTR_ERR(h_file);
16221+ if (IS_ERR(h_file))
4a4d8108 16222+ goto out_unlock;
4a4d8108 16223+
53392da6 16224+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16225+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16226+
4f0767ce 16227+out_unlock:
b912730e 16228+ si_read_unlock(inode->i_sb);
febd17d6 16229+ inode_unlock(inode);
b912730e 16230+out:
4a4d8108 16231+ return err;
dece6358
AM
16232+}
16233+
4a4d8108 16234+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16235+{
4a4d8108
AM
16236+ int err;
16237+ struct file *h_file;
4a4d8108 16238+ struct super_block *sb;
1308ab2a 16239+
b912730e 16240+ sb = file->f_path.dentry->d_sb;
e49829fe 16241+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16242+
521ced18 16243+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16244+ err = PTR_ERR(h_file);
16245+ if (IS_ERR(h_file))
4a4d8108
AM
16246+ goto out;
16247+
523b37e3 16248+ if (h_file->f_op->fasync)
4a4d8108 16249+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16250+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16251+
4f0767ce 16252+out:
4a4d8108 16253+ si_read_unlock(sb);
1308ab2a 16254+ return err;
dece6358 16255+}
4a4d8108 16256+
febd17d6
JR
16257+static int aufs_setfl(struct file *file, unsigned long arg)
16258+{
16259+ int err;
16260+ struct file *h_file;
16261+ struct super_block *sb;
16262+
16263+ sb = file->f_path.dentry->d_sb;
16264+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16265+
521ced18 16266+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16267+ err = PTR_ERR(h_file);
16268+ if (IS_ERR(h_file))
16269+ goto out;
16270+
1c60b727
AM
16271+ /* stop calling h_file->fasync */
16272+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16273+ err = setfl(/*unused fd*/-1, h_file, arg);
16274+ fput(h_file); /* instead of au_read_post() */
16275+
16276+out:
16277+ si_read_unlock(sb);
16278+ return err;
16279+}
16280+
4a4d8108
AM
16281+/* ---------------------------------------------------------------------- */
16282+
16283+/* no one supports this operation, currently */
16284+#if 0
16285+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16286+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16287+{
16288+}
16289+#endif
16290+
16291+/* ---------------------------------------------------------------------- */
16292+
16293+const struct file_operations aufs_file_fop = {
16294+ .owner = THIS_MODULE,
2cbb1c4b 16295+
027c5e7a 16296+ .llseek = default_llseek,
4a4d8108
AM
16297+
16298+ .read = aufs_read,
16299+ .write = aufs_write,
076b876e
AM
16300+ .read_iter = aufs_read_iter,
16301+ .write_iter = aufs_write_iter,
16302+
4a4d8108
AM
16303+#ifdef CONFIG_AUFS_POLL
16304+ .poll = aufs_poll,
16305+#endif
16306+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16307+#ifdef CONFIG_COMPAT
c2b27bf2 16308+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16309+#endif
4a4d8108
AM
16310+ .mmap = aufs_mmap,
16311+ .open = aufs_open_nondir,
16312+ .flush = aufs_flush_nondir,
16313+ .release = aufs_release_nondir,
16314+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16315+ .fasync = aufs_fasync,
16316+ /* .sendpage = aufs_sendpage, */
febd17d6 16317+ .setfl = aufs_setfl,
4a4d8108
AM
16318+ .splice_write = aufs_splice_write,
16319+ .splice_read = aufs_splice_read,
16320+#if 0
16321+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16322+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16323+#endif
521ced18
JR
16324+ .fallocate = aufs_fallocate,
16325+ .copy_file_range = aufs_copy_file_range
4a4d8108 16326+};
7f207e10 16327diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
eca34b5c
AM
16328--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
16329+++ linux/fs/aufs/fstype.h 2019-07-11 15:42:14.465571210 +0200
062440b3
AM
16330@@ -0,0 +1,401 @@
16331+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 16332+/*
ba1aed25 16333+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
16334+ *
16335+ * This program, aufs is free software; you can redistribute it and/or modify
16336+ * it under the terms of the GNU General Public License as published by
16337+ * the Free Software Foundation; either version 2 of the License, or
16338+ * (at your option) any later version.
16339+ *
16340+ * This program is distributed in the hope that it will be useful,
16341+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16342+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16343+ * GNU General Public License for more details.
16344+ *
16345+ * You should have received a copy of the GNU General Public License
523b37e3 16346+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16347+ */
16348+
16349+/*
16350+ * judging filesystem type
16351+ */
16352+
16353+#ifndef __AUFS_FSTYPE_H__
16354+#define __AUFS_FSTYPE_H__
16355+
16356+#ifdef __KERNEL__
16357+
16358+#include <linux/fs.h>
16359+#include <linux/magic.h>
b912730e 16360+#include <linux/nfs_fs.h>
b95c5147 16361+#include <linux/romfs_fs.h>
4a4d8108
AM
16362+
16363+static inline int au_test_aufs(struct super_block *sb)
16364+{
16365+ return sb->s_magic == AUFS_SUPER_MAGIC;
16366+}
16367+
16368+static inline const char *au_sbtype(struct super_block *sb)
16369+{
16370+ return sb->s_type->name;
16371+}
1308ab2a 16372+
16373+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16374+{
f0c0a007 16375+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16376+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16377+#else
16378+ return 0;
16379+#endif
16380+}
16381+
1308ab2a 16382+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16383+{
f0c0a007 16384+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16385+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16386+#else
16387+ return 0;
16388+#endif
16389+}
16390+
1308ab2a 16391+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16392+{
f0c0a007 16393+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16394+ return sb->s_magic == CRAMFS_MAGIC;
16395+#endif
16396+ return 0;
16397+}
16398+
16399+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16400+{
f0c0a007 16401+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16402+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16403+#else
16404+ return 0;
16405+#endif
16406+}
16407+
1308ab2a 16408+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16409+{
f0c0a007 16410+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16411+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16412+#else
16413+ return 0;
16414+#endif
16415+}
16416+
1308ab2a 16417+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16418+{
f0c0a007 16419+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16420+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16421+#else
16422+ return 0;
16423+#endif
16424+}
16425+
1308ab2a 16426+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16427+{
1308ab2a 16428+#ifdef CONFIG_TMPFS
16429+ return sb->s_magic == TMPFS_MAGIC;
16430+#else
16431+ return 0;
dece6358 16432+#endif
dece6358
AM
16433+}
16434+
1308ab2a 16435+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16436+{
f0c0a007 16437+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16438+ return !strcmp(au_sbtype(sb), "ecryptfs");
16439+#else
16440+ return 0;
16441+#endif
1facf9fc 16442+}
16443+
1308ab2a 16444+static inline int au_test_ramfs(struct super_block *sb)
16445+{
16446+ return sb->s_magic == RAMFS_MAGIC;
16447+}
16448+
16449+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16450+{
f0c0a007 16451+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16452+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16453+#else
16454+ return 0;
16455+#endif
16456+}
16457+
16458+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16459+{
16460+#ifdef CONFIG_PROC_FS
16461+ return sb->s_magic == PROC_SUPER_MAGIC;
16462+#else
16463+ return 0;
16464+#endif
16465+}
16466+
16467+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16468+{
16469+#ifdef CONFIG_SYSFS
16470+ return sb->s_magic == SYSFS_MAGIC;
16471+#else
16472+ return 0;
16473+#endif
16474+}
16475+
16476+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16477+{
f0c0a007 16478+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16479+ return sb->s_magic == CONFIGFS_MAGIC;
16480+#else
16481+ return 0;
16482+#endif
16483+}
16484+
16485+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16486+{
f0c0a007 16487+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16488+ return sb->s_magic == MINIX3_SUPER_MAGIC
16489+ || sb->s_magic == MINIX2_SUPER_MAGIC
16490+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16491+ || sb->s_magic == MINIX_SUPER_MAGIC
16492+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16493+#else
16494+ return 0;
16495+#endif
16496+}
16497+
1308ab2a 16498+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16499+{
f0c0a007 16500+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16501+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16502+#else
16503+ return 0;
16504+#endif
16505+}
16506+
16507+static inline int au_test_msdos(struct super_block *sb)
16508+{
16509+ return au_test_fat(sb);
16510+}
16511+
16512+static inline int au_test_vfat(struct super_block *sb)
16513+{
16514+ return au_test_fat(sb);
16515+}
16516+
16517+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16518+{
16519+#ifdef CONFIG_SECURITYFS
16520+ return sb->s_magic == SECURITYFS_MAGIC;
16521+#else
16522+ return 0;
16523+#endif
16524+}
16525+
16526+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16527+{
f0c0a007 16528+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16529+ return sb->s_magic == SQUASHFS_MAGIC;
16530+#else
16531+ return 0;
16532+#endif
16533+}
16534+
16535+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16536+{
f0c0a007 16537+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16538+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16539+#else
16540+ return 0;
16541+#endif
16542+}
16543+
16544+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16545+{
f0c0a007 16546+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16547+ return sb->s_magic == XENFS_SUPER_MAGIC;
16548+#else
16549+ return 0;
16550+#endif
16551+}
16552+
16553+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16554+{
16555+#ifdef CONFIG_DEBUG_FS
16556+ return sb->s_magic == DEBUGFS_MAGIC;
16557+#else
16558+ return 0;
16559+#endif
16560+}
16561+
16562+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16563+{
f0c0a007 16564+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16565+ return sb->s_magic == NILFS_SUPER_MAGIC;
16566+#else
16567+ return 0;
16568+#endif
16569+}
16570+
4a4d8108
AM
16571+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16572+{
f0c0a007 16573+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16574+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16575+#else
16576+ return 0;
16577+#endif
16578+}
16579+
1308ab2a 16580+/* ---------------------------------------------------------------------- */
16581+/*
16582+ * they can't be an aufs branch.
16583+ */
16584+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16585+{
16586+ return
16587+#ifndef CONFIG_AUFS_BR_RAMFS
16588+ au_test_ramfs(sb) ||
16589+#endif
16590+ au_test_procfs(sb)
16591+ || au_test_sysfs(sb)
16592+ || au_test_configfs(sb)
16593+ || au_test_debugfs(sb)
16594+ || au_test_securityfs(sb)
16595+ || au_test_xenfs(sb)
16596+ || au_test_ecryptfs(sb)
16597+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16598+ || au_test_aufs(sb); /* will be supported in next version */
16599+}
16600+
1308ab2a 16601+static inline int au_test_fs_remote(struct super_block *sb)
16602+{
16603+ return !au_test_tmpfs(sb)
16604+#ifdef CONFIG_AUFS_BR_RAMFS
16605+ && !au_test_ramfs(sb)
16606+#endif
16607+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16608+}
16609+
16610+/* ---------------------------------------------------------------------- */
16611+
16612+/*
16613+ * Note: these functions (below) are created after reading ->getattr() in all
16614+ * filesystems under linux/fs. it means we have to do so in every update...
16615+ */
16616+
16617+/*
16618+ * some filesystems require getattr to refresh the inode attributes before
16619+ * referencing.
16620+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16621+ * and leave the work for d_revalidate()
16622+ */
16623+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16624+{
16625+ return au_test_nfs(sb)
16626+ || au_test_fuse(sb)
1308ab2a 16627+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16628+ ;
16629+}
16630+
16631+/*
16632+ * filesystems which don't maintain i_size or i_blocks.
16633+ */
16634+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16635+{
16636+ return au_test_xfs(sb)
4a4d8108
AM
16637+ || au_test_btrfs(sb)
16638+ || au_test_ubifs(sb)
16639+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16640+ /* || au_test_minix(sb) */ /* untested */
16641+ ;
16642+}
16643+
16644+/*
16645+ * filesystems which don't store the correct value in some of their inode
16646+ * attributes.
16647+ */
16648+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16649+{
16650+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16651+ || au_test_fat(sb)
16652+ || au_test_msdos(sb)
16653+ || au_test_vfat(sb);
1facf9fc 16654+}
16655+
16656+/* they don't check i_nlink in link(2) */
16657+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16658+{
16659+ return au_test_tmpfs(sb)
16660+#ifdef CONFIG_AUFS_BR_RAMFS
16661+ || au_test_ramfs(sb)
16662+#endif
4a4d8108 16663+ || au_test_ubifs(sb)
4a4d8108 16664+ || au_test_hfsplus(sb);
1facf9fc 16665+}
16666+
16667+/*
16668+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16669+ */
16670+static inline int au_test_fs_notime(struct super_block *sb)
16671+{
16672+ return au_test_nfs(sb)
16673+ || au_test_fuse(sb)
dece6358 16674+ || au_test_ubifs(sb)
1facf9fc 16675+ ;
16676+}
16677+
1facf9fc 16678+/* temporary support for i#1 in cramfs */
16679+static inline int au_test_fs_unique_ino(struct inode *inode)
16680+{
16681+ if (au_test_cramfs(inode->i_sb))
16682+ return inode->i_ino != 1;
16683+ return 1;
16684+}
16685+
16686+/* ---------------------------------------------------------------------- */
16687+
16688+/*
16689+ * the filesystem where the xino files placed must support i/o after unlink and
16690+ * maintain i_size and i_blocks.
16691+ */
16692+static inline int au_test_fs_bad_xino(struct super_block *sb)
16693+{
16694+ return au_test_fs_remote(sb)
16695+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16696+ /* don't want unnecessary work for xino */
16697+ || au_test_aufs(sb)
1308ab2a 16698+ || au_test_ecryptfs(sb)
16699+ || au_test_nilfs(sb);
1facf9fc 16700+}
16701+
16702+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16703+{
16704+ return au_test_tmpfs(sb)
16705+ || au_test_ramfs(sb);
16706+}
16707+
16708+/*
16709+ * test if the @sb is real-readonly.
16710+ */
16711+static inline int au_test_fs_rr(struct super_block *sb)
16712+{
16713+ return au_test_squashfs(sb)
16714+ || au_test_iso9660(sb)
16715+ || au_test_cramfs(sb)
16716+ || au_test_romfs(sb);
16717+}
16718+
b912730e
AM
16719+/*
16720+ * test if the @inode is nfs with 'noacl' option
2121bcd9 16721+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
16722+ */
16723+static inline int au_test_nfs_noacl(struct inode *inode)
16724+{
16725+ return au_test_nfs(inode->i_sb)
16726+ /* && IS_POSIXACL(inode) */
16727+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16728+}
16729+
1facf9fc 16730+#endif /* __KERNEL__ */
16731+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947 16732diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
eca34b5c
AM
16733--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
16734+++ linux/fs/aufs/hbl.h 2019-07-11 15:42:14.465571210 +0200
062440b3
AM
16735@@ -0,0 +1,65 @@
16736+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 16737+/*
ba1aed25 16738+ * Copyright (C) 2017-2019 Junjiro R. Okajima
8b6a4947
AM
16739+ *
16740+ * This program, aufs is free software; you can redistribute it and/or modify
16741+ * it under the terms of the GNU General Public License as published by
16742+ * the Free Software Foundation; either version 2 of the License, or
16743+ * (at your option) any later version.
16744+ *
16745+ * This program is distributed in the hope that it will be useful,
16746+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16747+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16748+ * GNU General Public License for more details.
16749+ *
16750+ * You should have received a copy of the GNU General Public License
16751+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16752+ */
16753+
16754+/*
16755+ * helpers for hlist_bl.h
16756+ */
16757+
16758+#ifndef __AUFS_HBL_H__
16759+#define __AUFS_HBL_H__
16760+
16761+#ifdef __KERNEL__
16762+
16763+#include <linux/list_bl.h>
16764+
16765+static inline void au_hbl_add(struct hlist_bl_node *node,
16766+ struct hlist_bl_head *hbl)
16767+{
16768+ hlist_bl_lock(hbl);
16769+ hlist_bl_add_head(node, hbl);
16770+ hlist_bl_unlock(hbl);
16771+}
16772+
16773+static inline void au_hbl_del(struct hlist_bl_node *node,
16774+ struct hlist_bl_head *hbl)
16775+{
16776+ hlist_bl_lock(hbl);
16777+ hlist_bl_del(node);
16778+ hlist_bl_unlock(hbl);
16779+}
16780+
16781+#define au_hbl_for_each(pos, head) \
16782+ for (pos = hlist_bl_first(head); \
16783+ pos; \
16784+ pos = pos->next)
16785+
16786+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16787+{
16788+ unsigned long cnt;
16789+ struct hlist_bl_node *pos;
16790+
16791+ cnt = 0;
16792+ hlist_bl_lock(hbl);
16793+ au_hbl_for_each(pos, hbl)
16794+ cnt++;
16795+ hlist_bl_unlock(hbl);
16796+ return cnt;
16797+}
16798+
16799+#endif /* __KERNEL__ */
16800+#endif /* __AUFS_HBL_H__ */
7f207e10 16801diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
eca34b5c
AM
16802--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
16803+++ linux/fs/aufs/hfsnotify.c 2019-07-11 15:42:14.465571210 +0200
fbc438ed 16804@@ -0,0 +1,288 @@
cd7a4cd9 16805+// SPDX-License-Identifier: GPL-2.0
1facf9fc 16806+/*
ba1aed25 16807+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 16808+ *
16809+ * This program, aufs is free software; you can redistribute it and/or modify
16810+ * it under the terms of the GNU General Public License as published by
16811+ * the Free Software Foundation; either version 2 of the License, or
16812+ * (at your option) any later version.
dece6358
AM
16813+ *
16814+ * This program is distributed in the hope that it will be useful,
16815+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16816+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16817+ * GNU General Public License for more details.
16818+ *
16819+ * You should have received a copy of the GNU General Public License
523b37e3 16820+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16821+ */
16822+
16823+/*
4a4d8108 16824+ * fsnotify for the lower directories
1facf9fc 16825+ */
16826+
16827+#include "aufs.h"
16828+
4a4d8108
AM
16829+/* FS_IN_IGNORED is unnecessary */
16830+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16831+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16832+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16833+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16834+
0c5527e5 16835+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16836+{
0c5527e5
AM
16837+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16838+ hn_mark);
5afbbe0d 16839+ /* AuDbg("here\n"); */
1c60b727 16840+ au_cache_free_hnotify(hn);
8b6a4947 16841+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16842+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16843+ wake_up(&au_hfsn_wq);
4a4d8108 16844+}
1facf9fc 16845+
027c5e7a 16846+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16847+{
1716fcea 16848+ int err;
027c5e7a
AM
16849+ struct au_hnotify *hn;
16850+ struct super_block *sb;
16851+ struct au_branch *br;
0c5527e5 16852+ struct fsnotify_mark *mark;
027c5e7a 16853+ aufs_bindex_t bindex;
1facf9fc 16854+
027c5e7a
AM
16855+ hn = hinode->hi_notify;
16856+ sb = hn->hn_aufs_inode->i_sb;
16857+ bindex = au_br_index(sb, hinode->hi_id);
16858+ br = au_sbr(sb, bindex);
1716fcea
AM
16859+ AuDebugOn(!br->br_hfsn);
16860+
0c5527e5 16861+ mark = &hn->hn_mark;
ffa93bbd 16862+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16863+ mark->mask = AuHfsnMask;
7f207e10
AM
16864+ /*
16865+ * by udba rename or rmdir, aufs assign a new inode to the known
16866+ * h_inode, so specify 1 to allow dups.
16867+ */
c1595e42 16868+ lockdep_off();
acd2b654 16869+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
c1595e42 16870+ lockdep_on();
1716fcea
AM
16871+
16872+ return err;
1facf9fc 16873+}
16874+
7eafdf33 16875+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16876+{
0c5527e5 16877+ struct fsnotify_mark *mark;
7eafdf33 16878+ unsigned long long ull;
1716fcea 16879+ struct fsnotify_group *group;
7eafdf33
AM
16880+
16881+ ull = atomic64_inc_return(&au_hfsn_ifree);
16882+ BUG_ON(!ull);
953406b4 16883+
0c5527e5 16884+ mark = &hn->hn_mark;
1716fcea
AM
16885+ spin_lock(&mark->lock);
16886+ group = mark->group;
16887+ fsnotify_get_group(group);
16888+ spin_unlock(&mark->lock);
c1595e42 16889+ lockdep_off();
1716fcea 16890+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16891+ fsnotify_put_mark(mark);
1716fcea 16892+ fsnotify_put_group(group);
c1595e42 16893+ lockdep_on();
7f207e10 16894+
7eafdf33
AM
16895+ /* free hn by myself */
16896+ return 0;
1facf9fc 16897+}
16898+
16899+/* ---------------------------------------------------------------------- */
16900+
4a4d8108 16901+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16902+{
0c5527e5 16903+ struct fsnotify_mark *mark;
1facf9fc 16904+
0c5527e5
AM
16905+ mark = &hinode->hi_notify->hn_mark;
16906+ spin_lock(&mark->lock);
1facf9fc 16907+ if (do_set) {
0c5527e5
AM
16908+ AuDebugOn(mark->mask & AuHfsnMask);
16909+ mark->mask |= AuHfsnMask;
1facf9fc 16910+ } else {
0c5527e5
AM
16911+ AuDebugOn(!(mark->mask & AuHfsnMask));
16912+ mark->mask &= ~AuHfsnMask;
1facf9fc 16913+ }
0c5527e5 16914+ spin_unlock(&mark->lock);
4a4d8108 16915+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 16916+}
16917+
4a4d8108 16918+/* ---------------------------------------------------------------------- */
1facf9fc 16919+
4a4d8108
AM
16920+/* #define AuDbgHnotify */
16921+#ifdef AuDbgHnotify
16922+static char *au_hfsn_name(u32 mask)
16923+{
16924+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
16925+#define test_ret(flag) \
16926+ do { \
16927+ if (mask & flag) \
16928+ return #flag; \
16929+ } while (0)
4a4d8108
AM
16930+ test_ret(FS_ACCESS);
16931+ test_ret(FS_MODIFY);
16932+ test_ret(FS_ATTRIB);
16933+ test_ret(FS_CLOSE_WRITE);
16934+ test_ret(FS_CLOSE_NOWRITE);
16935+ test_ret(FS_OPEN);
16936+ test_ret(FS_MOVED_FROM);
16937+ test_ret(FS_MOVED_TO);
16938+ test_ret(FS_CREATE);
16939+ test_ret(FS_DELETE);
16940+ test_ret(FS_DELETE_SELF);
16941+ test_ret(FS_MOVE_SELF);
16942+ test_ret(FS_UNMOUNT);
16943+ test_ret(FS_Q_OVERFLOW);
16944+ test_ret(FS_IN_IGNORED);
b912730e 16945+ test_ret(FS_ISDIR);
4a4d8108
AM
16946+ test_ret(FS_IN_ONESHOT);
16947+ test_ret(FS_EVENT_ON_CHILD);
16948+ return "";
16949+#undef test_ret
16950+#else
16951+ return "??";
16952+#endif
1facf9fc 16953+}
4a4d8108 16954+#endif
1facf9fc 16955+
16956+/* ---------------------------------------------------------------------- */
16957+
1716fcea
AM
16958+static void au_hfsn_free_group(struct fsnotify_group *group)
16959+{
16960+ struct au_br_hfsnotify *hfsn = group->private;
16961+
5afbbe0d 16962+ /* AuDbg("here\n"); */
9f237c51 16963+ au_kfree_try_rcu(hfsn);
1716fcea
AM
16964+}
16965+
4a4d8108 16966+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 16967+ struct inode *inode,
a2654f78 16968+ u32 mask, const void *data, int data_type,
83b672a5 16969+ const struct qstr *file_name, u32 cookie,
ffa93bbd 16970+ struct fsnotify_iter_info *iter_info)
1facf9fc 16971+{
16972+ int err;
4a4d8108
AM
16973+ struct au_hnotify *hnotify;
16974+ struct inode *h_dir, *h_inode;
cd7a4cd9 16975+ struct fsnotify_mark *inode_mark;
4a4d8108 16976+
fb47a38f 16977+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 16978+
16979+ err = 0;
0c5527e5 16980+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 16981+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 16982+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 16983+ goto out;
1facf9fc 16984+
fb47a38f
JR
16985+ h_dir = inode;
16986+ h_inode = NULL;
4a4d8108 16987+#ifdef AuDbgHnotify
392086de 16988+ au_debug_on();
fbc438ed
JR
16989+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16990+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
4a4d8108
AM
16991+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16992+ h_dir->i_ino, mask, au_hfsn_name(mask),
fbc438ed 16993+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
4a4d8108 16994+ /* WARN_ON(1); */
1facf9fc 16995+ }
392086de 16996+ au_debug_off();
1facf9fc 16997+#endif
4a4d8108 16998+
cd7a4cd9 16999+ inode_mark = fsnotify_iter_inode_mark(iter_info);
0c5527e5
AM
17000+ AuDebugOn(!inode_mark);
17001+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
b62f1e65 17002+ err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
1facf9fc 17003+
4a4d8108
AM
17004+out:
17005+ return err;
17006+}
1facf9fc 17007+
4a4d8108 17008+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 17009+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
17010+ .free_group_priv = au_hfsn_free_group,
17011+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
17012+};
17013+
17014+/* ---------------------------------------------------------------------- */
17015+
027c5e7a
AM
17016+static void au_hfsn_fin_br(struct au_branch *br)
17017+{
1716fcea 17018+ struct au_br_hfsnotify *hfsn;
027c5e7a 17019+
1716fcea 17020+ hfsn = br->br_hfsn;
c1595e42
JR
17021+ if (hfsn) {
17022+ lockdep_off();
1716fcea 17023+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
17024+ lockdep_on();
17025+ }
027c5e7a
AM
17026+}
17027+
1716fcea 17028+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
17029+{
17030+ int err;
1716fcea
AM
17031+ struct fsnotify_group *group;
17032+ struct au_br_hfsnotify *hfsn;
1facf9fc 17033+
4a4d8108 17034+ err = 0;
1716fcea
AM
17035+ br->br_hfsn = NULL;
17036+ if (!au_br_hnotifyable(perm))
027c5e7a 17037+ goto out;
027c5e7a 17038+
1716fcea
AM
17039+ err = -ENOMEM;
17040+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17041+ if (unlikely(!hfsn))
027c5e7a
AM
17042+ goto out;
17043+
1716fcea
AM
17044+ err = 0;
17045+ group = fsnotify_alloc_group(&au_hfsn_ops);
17046+ if (IS_ERR(group)) {
17047+ err = PTR_ERR(group);
0c5527e5 17048+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 17049+ goto out_hfsn;
4a4d8108 17050+ }
1facf9fc 17051+
1716fcea
AM
17052+ group->private = hfsn;
17053+ hfsn->hfsn_group = group;
17054+ br->br_hfsn = hfsn;
17055+ goto out; /* success */
17056+
17057+out_hfsn:
9f237c51 17058+ au_kfree_try_rcu(hfsn);
027c5e7a 17059+out:
1716fcea
AM
17060+ return err;
17061+}
17062+
17063+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17064+{
17065+ int err;
17066+
17067+ err = 0;
17068+ if (!br->br_hfsn)
17069+ err = au_hfsn_init_br(br, perm);
17070+
1facf9fc 17071+ return err;
17072+}
17073+
7eafdf33
AM
17074+/* ---------------------------------------------------------------------- */
17075+
17076+static void au_hfsn_fin(void)
17077+{
17078+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17079+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17080+}
17081+
4a4d8108
AM
17082+const struct au_hnotify_op au_hnotify_op = {
17083+ .ctl = au_hfsn_ctl,
17084+ .alloc = au_hfsn_alloc,
17085+ .free = au_hfsn_free,
1facf9fc 17086+
7eafdf33
AM
17087+ .fin = au_hfsn_fin,
17088+
027c5e7a
AM
17089+ .reset_br = au_hfsn_reset_br,
17090+ .fin_br = au_hfsn_fin_br,
17091+ .init_br = au_hfsn_init_br
4a4d8108 17092+};
7f207e10 17093diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
eca34b5c
AM
17094--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
17095+++ linux/fs/aufs/hfsplus.c 2019-07-11 15:42:14.465571210 +0200
acd2b654 17096@@ -0,0 +1,60 @@
cd7a4cd9 17097+// SPDX-License-Identifier: GPL-2.0
4a4d8108 17098+/*
ba1aed25 17099+ * Copyright (C) 2010-2019 Junjiro R. Okajima
4a4d8108
AM
17100+ *
17101+ * This program, aufs is free software; you can redistribute it and/or modify
17102+ * it under the terms of the GNU General Public License as published by
17103+ * the Free Software Foundation; either version 2 of the License, or
17104+ * (at your option) any later version.
17105+ *
17106+ * This program is distributed in the hope that it will be useful,
17107+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17108+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17109+ * GNU General Public License for more details.
17110+ *
17111+ * You should have received a copy of the GNU General Public License
523b37e3 17112+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17113+ */
1facf9fc 17114+
4a4d8108 17115+/*
acd2b654 17116+ * special support for filesystems which acquires an inode mutex
4a4d8108
AM
17117+ * at final closing a file, eg, hfsplus.
17118+ *
17119+ * This trick is very simple and stupid, just to open the file before really
acd2b654 17120+ * necessary open to tell hfsplus that this is not the final closing.
4a4d8108
AM
17121+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17122+ * and au_h_open_post() after releasing it.
17123+ */
1facf9fc 17124+
4a4d8108 17125+#include "aufs.h"
1facf9fc 17126+
392086de
AM
17127+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17128+ int force_wr)
4a4d8108
AM
17129+{
17130+ struct file *h_file;
17131+ struct dentry *h_dentry;
1facf9fc 17132+
4a4d8108
AM
17133+ h_dentry = au_h_dptr(dentry, bindex);
17134+ AuDebugOn(!h_dentry);
5527c038 17135+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17136+
17137+ h_file = NULL;
17138+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17139+ && d_is_reg(h_dentry))
4a4d8108
AM
17140+ h_file = au_h_open(dentry, bindex,
17141+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17142+ /*file*/NULL, force_wr);
4a4d8108 17143+ return h_file;
1facf9fc 17144+}
17145+
4a4d8108
AM
17146+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17147+ struct file *h_file)
17148+{
acd2b654
AM
17149+ struct au_branch *br;
17150+
4a4d8108
AM
17151+ if (h_file) {
17152+ fput(h_file);
acd2b654
AM
17153+ br = au_sbr(dentry->d_sb, bindex);
17154+ au_lcnt_dec(&br->br_nfiles);
4a4d8108
AM
17155+ }
17156+}
7f207e10 17157diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
eca34b5c
AM
17158--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
17159+++ linux/fs/aufs/hnotify.c 2019-07-11 15:42:14.465571210 +0200
eca801bf 17160@@ -0,0 +1,721 @@
cd7a4cd9 17161+// SPDX-License-Identifier: GPL-2.0
e49829fe 17162+/*
ba1aed25 17163+ * Copyright (C) 2005-2019 Junjiro R. Okajima
e49829fe
JR
17164+ *
17165+ * This program, aufs is free software; you can redistribute it and/or modify
17166+ * it under the terms of the GNU General Public License as published by
17167+ * the Free Software Foundation; either version 2 of the License, or
17168+ * (at your option) any later version.
17169+ *
17170+ * This program is distributed in the hope that it will be useful,
17171+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17172+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17173+ * GNU General Public License for more details.
17174+ *
17175+ * You should have received a copy of the GNU General Public License
523b37e3 17176+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17177+ */
17178+
17179+/*
7f207e10 17180+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17181+ */
17182+
eca801bf 17183+/* #include <linux/iversion.h> */
e49829fe
JR
17184+#include "aufs.h"
17185+
027c5e7a 17186+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17187+{
17188+ int err;
7f207e10 17189+ struct au_hnotify *hn;
1facf9fc 17190+
4a4d8108
AM
17191+ err = -ENOMEM;
17192+ hn = au_cache_alloc_hnotify();
17193+ if (hn) {
17194+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17195+ hinode->hi_notify = hn;
17196+ err = au_hnotify_op.alloc(hinode);
17197+ AuTraceErr(err);
17198+ if (unlikely(err)) {
17199+ hinode->hi_notify = NULL;
1c60b727 17200+ au_cache_free_hnotify(hn);
4a4d8108
AM
17201+ /*
17202+ * The upper dir was removed by udba, but the same named
acd2b654 17203+ * dir left. In this case, aufs assigns a new inode
4a4d8108 17204+ * number and set the monitor again.
acd2b654 17205+ * For the lower dir, the old monitor is still left.
4a4d8108
AM
17206+ */
17207+ if (err == -EEXIST)
17208+ err = 0;
17209+ }
1308ab2a 17210+ }
1308ab2a 17211+
027c5e7a 17212+ AuTraceErr(err);
1308ab2a 17213+ return err;
dece6358 17214+}
1facf9fc 17215+
4a4d8108 17216+void au_hn_free(struct au_hinode *hinode)
dece6358 17217+{
4a4d8108 17218+ struct au_hnotify *hn;
1facf9fc 17219+
4a4d8108
AM
17220+ hn = hinode->hi_notify;
17221+ if (hn) {
4a4d8108 17222+ hinode->hi_notify = NULL;
7eafdf33 17223+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17224+ au_cache_free_hnotify(hn);
4a4d8108
AM
17225+ }
17226+}
dece6358 17227+
4a4d8108 17228+/* ---------------------------------------------------------------------- */
dece6358 17229+
4a4d8108
AM
17230+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17231+{
17232+ if (hinode->hi_notify)
17233+ au_hnotify_op.ctl(hinode, do_set);
17234+}
17235+
17236+void au_hn_reset(struct inode *inode, unsigned int flags)
17237+{
5afbbe0d 17238+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17239+ struct inode *hi;
17240+ struct dentry *iwhdentry;
1facf9fc 17241+
5afbbe0d
AM
17242+ bbot = au_ibbot(inode);
17243+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17244+ hi = au_h_iptr(inode, bindex);
17245+ if (!hi)
17246+ continue;
1308ab2a 17247+
febd17d6 17248+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17249+ iwhdentry = au_hi_wh(inode, bindex);
17250+ if (iwhdentry)
17251+ dget(iwhdentry);
17252+ au_igrab(hi);
17253+ au_set_h_iptr(inode, bindex, NULL, 0);
17254+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17255+ flags & ~AuHi_XINO);
17256+ iput(hi);
17257+ dput(iwhdentry);
febd17d6 17258+ /* inode_unlock(hi); */
1facf9fc 17259+ }
1facf9fc 17260+}
17261+
1308ab2a 17262+/* ---------------------------------------------------------------------- */
1facf9fc 17263+
4a4d8108 17264+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17265+{
4a4d8108 17266+ int err;
5afbbe0d 17267+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17268+ struct inode *h_i;
1facf9fc 17269+
4a4d8108
AM
17270+ err = 0;
17271+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17272+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17273+ goto out;
17274+ }
1facf9fc 17275+
4a4d8108 17276+ bfound = -1;
5afbbe0d
AM
17277+ bbot = au_ibbot(inode);
17278+ btop = au_ibtop(inode);
4a4d8108 17279+#if 0 /* reserved for future use */
5afbbe0d 17280+ if (bindex == bbot) {
4a4d8108
AM
17281+ /* keep this ino in rename case */
17282+ goto out;
17283+ }
17284+#endif
5afbbe0d 17285+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17286+ if (au_h_iptr(inode, bindex) == h_inode) {
17287+ bfound = bindex;
17288+ break;
17289+ }
17290+ if (bfound < 0)
1308ab2a 17291+ goto out;
1facf9fc 17292+
5afbbe0d 17293+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17294+ h_i = au_h_iptr(inode, bindex);
17295+ if (!h_i)
17296+ continue;
1facf9fc 17297+
4a4d8108
AM
17298+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17299+ /* ignore this error */
17300+ /* bad action? */
1facf9fc 17301+ }
1facf9fc 17302+
4a4d8108 17303+ /* children inode number will be broken */
1facf9fc 17304+
4f0767ce 17305+out:
4a4d8108
AM
17306+ AuTraceErr(err);
17307+ return err;
1facf9fc 17308+}
17309+
4a4d8108 17310+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17311+{
4a4d8108
AM
17312+ int err, i, j, ndentry;
17313+ struct au_dcsub_pages dpages;
17314+ struct au_dpage *dpage;
17315+ struct dentry **dentries;
1facf9fc 17316+
4a4d8108
AM
17317+ err = au_dpages_init(&dpages, GFP_NOFS);
17318+ if (unlikely(err))
17319+ goto out;
17320+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17321+ if (unlikely(err))
17322+ goto out_dpages;
1facf9fc 17323+
4a4d8108
AM
17324+ for (i = 0; i < dpages.ndpage; i++) {
17325+ dpage = dpages.dpages + i;
17326+ dentries = dpage->dentries;
17327+ ndentry = dpage->ndentry;
17328+ for (j = 0; j < ndentry; j++) {
17329+ struct dentry *d;
17330+
17331+ d = dentries[j];
17332+ if (IS_ROOT(d))
17333+ continue;
17334+
4a4d8108 17335+ au_digen_dec(d);
5527c038 17336+ if (d_really_is_positive(d))
4a4d8108
AM
17337+ /* todo: reset children xino?
17338+ cached children only? */
5527c038 17339+ au_iigen_dec(d_inode(d));
1308ab2a 17340+ }
dece6358 17341+ }
1facf9fc 17342+
4f0767ce 17343+out_dpages:
4a4d8108 17344+ au_dpages_free(&dpages);
dece6358 17345+
027c5e7a 17346+#if 0
4a4d8108
AM
17347+ /* discard children */
17348+ dentry_unhash(dentry);
17349+ dput(dentry);
027c5e7a 17350+#endif
4f0767ce 17351+out:
dece6358
AM
17352+ return err;
17353+}
17354+
1308ab2a 17355+/*
4a4d8108 17356+ * return 0 if processed.
1308ab2a 17357+ */
4a4d8108
AM
17358+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17359+ const unsigned int isdir)
dece6358 17360+{
1308ab2a 17361+ int err;
4a4d8108
AM
17362+ struct dentry *d;
17363+ struct qstr *dname;
1facf9fc 17364+
4a4d8108
AM
17365+ err = 1;
17366+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17367+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17368+ err = 0;
17369+ goto out;
17370+ }
dece6358 17371+
4a4d8108
AM
17372+ if (!isdir) {
17373+ AuDebugOn(!name);
17374+ au_iigen_dec(inode);
027c5e7a 17375+ spin_lock(&inode->i_lock);
c1595e42 17376+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17377+ spin_lock(&d->d_lock);
4a4d8108
AM
17378+ dname = &d->d_name;
17379+ if (dname->len != nlen
027c5e7a
AM
17380+ && memcmp(dname->name, name, nlen)) {
17381+ spin_unlock(&d->d_lock);
4a4d8108 17382+ continue;
027c5e7a 17383+ }
4a4d8108 17384+ err = 0;
4a4d8108
AM
17385+ au_digen_dec(d);
17386+ spin_unlock(&d->d_lock);
17387+ break;
1facf9fc 17388+ }
027c5e7a 17389+ spin_unlock(&inode->i_lock);
1308ab2a 17390+ } else {
027c5e7a 17391+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17392+ d = d_find_any_alias(inode);
4a4d8108
AM
17393+ if (!d) {
17394+ au_iigen_dec(inode);
17395+ goto out;
17396+ }
1facf9fc 17397+
027c5e7a 17398+ spin_lock(&d->d_lock);
4a4d8108 17399+ dname = &d->d_name;
027c5e7a
AM
17400+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17401+ spin_unlock(&d->d_lock);
4a4d8108 17402+ err = hn_gen_tree(d);
027c5e7a
AM
17403+ spin_lock(&d->d_lock);
17404+ }
17405+ spin_unlock(&d->d_lock);
4a4d8108
AM
17406+ dput(d);
17407+ }
1facf9fc 17408+
4f0767ce 17409+out:
4a4d8108 17410+ AuTraceErr(err);
1308ab2a 17411+ return err;
17412+}
dece6358 17413+
4a4d8108 17414+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17415+{
4a4d8108 17416+ int err;
1facf9fc 17417+
5527c038 17418+ if (IS_ROOT(dentry)) {
0c3ec466 17419+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17420+ return 0;
17421+ }
1308ab2a 17422+
4a4d8108
AM
17423+ err = 0;
17424+ if (!isdir) {
4a4d8108 17425+ au_digen_dec(dentry);
5527c038
JR
17426+ if (d_really_is_positive(dentry))
17427+ au_iigen_dec(d_inode(dentry));
4a4d8108 17428+ } else {
027c5e7a 17429+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17430+ if (d_really_is_positive(dentry))
4a4d8108
AM
17431+ err = hn_gen_tree(dentry);
17432+ }
17433+
17434+ AuTraceErr(err);
17435+ return err;
1facf9fc 17436+}
17437+
4a4d8108 17438+/* ---------------------------------------------------------------------- */
1facf9fc 17439+
4a4d8108
AM
17440+/* hnotify job flags */
17441+#define AuHnJob_XINO0 1
17442+#define AuHnJob_GEN (1 << 1)
17443+#define AuHnJob_DIRENT (1 << 2)
17444+#define AuHnJob_ISDIR (1 << 3)
17445+#define AuHnJob_TRYXINO0 (1 << 4)
17446+#define AuHnJob_MNTPNT (1 << 5)
17447+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17448+#define au_fset_hnjob(flags, name) \
17449+ do { (flags) |= AuHnJob_##name; } while (0)
17450+#define au_fclr_hnjob(flags, name) \
17451+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17452+
4a4d8108
AM
17453+enum {
17454+ AuHn_CHILD,
17455+ AuHn_PARENT,
17456+ AuHnLast
17457+};
1facf9fc 17458+
4a4d8108
AM
17459+struct au_hnotify_args {
17460+ struct inode *h_dir, *dir, *h_child_inode;
17461+ u32 mask;
17462+ unsigned int flags[AuHnLast];
17463+ unsigned int h_child_nlen;
17464+ char h_child_name[];
17465+};
1facf9fc 17466+
4a4d8108
AM
17467+struct hn_job_args {
17468+ unsigned int flags;
17469+ struct inode *inode, *h_inode, *dir, *h_dir;
17470+ struct dentry *dentry;
17471+ char *h_name;
17472+ int h_nlen;
17473+};
1308ab2a 17474+
4a4d8108
AM
17475+static int hn_job(struct hn_job_args *a)
17476+{
17477+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17478+ int e;
1308ab2a 17479+
4a4d8108
AM
17480+ /* reset xino */
17481+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17482+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17483+
4a4d8108
AM
17484+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17485+ && a->inode
17486+ && a->h_inode) {
be118d29 17487+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17488+ if (!a->h_inode->i_nlink
17489+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17490+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17491+ inode_unlock_shared(a->h_inode);
1308ab2a 17492+ }
1facf9fc 17493+
4a4d8108
AM
17494+ /* make the generation obsolete */
17495+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17496+ e = -1;
4a4d8108 17497+ if (a->inode)
076b876e 17498+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17499+ isdir);
076b876e 17500+ if (e && a->dentry)
4a4d8108
AM
17501+ hn_gen_by_name(a->dentry, isdir);
17502+ /* ignore this error */
1facf9fc 17503+ }
1facf9fc 17504+
4a4d8108
AM
17505+ /* make dir entries obsolete */
17506+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17507+ struct au_vdir *vdir;
1facf9fc 17508+
4a4d8108
AM
17509+ vdir = au_ivdir(a->inode);
17510+ if (vdir)
17511+ vdir->vd_jiffy = 0;
17512+ /* IMustLock(a->inode); */
be118d29 17513+ /* inode_inc_iversion(a->inode); */
4a4d8108 17514+ }
1facf9fc 17515+
4a4d8108
AM
17516+ /* can do nothing but warn */
17517+ if (au_ftest_hnjob(a->flags, MNTPNT)
17518+ && a->dentry
17519+ && d_mountpoint(a->dentry))
523b37e3 17520+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17521+
4a4d8108 17522+ return 0;
1308ab2a 17523+}
1facf9fc 17524+
1308ab2a 17525+/* ---------------------------------------------------------------------- */
1facf9fc 17526+
4a4d8108
AM
17527+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17528+ struct inode *dir)
1308ab2a 17529+{
4a4d8108
AM
17530+ struct dentry *dentry, *d, *parent;
17531+ struct qstr *dname;
1308ab2a 17532+
c1595e42 17533+ parent = d_find_any_alias(dir);
4a4d8108
AM
17534+ if (!parent)
17535+ return NULL;
1308ab2a 17536+
4a4d8108 17537+ dentry = NULL;
027c5e7a 17538+ spin_lock(&parent->d_lock);
c1595e42 17539+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17540+ /* AuDbg("%pd\n", d); */
027c5e7a 17541+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17542+ dname = &d->d_name;
17543+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17544+ goto cont_unlock;
17545+ if (au_di(d))
17546+ au_digen_dec(d);
17547+ else
17548+ goto cont_unlock;
c1595e42 17549+ if (au_dcount(d) > 0) {
027c5e7a 17550+ dentry = dget_dlock(d);
4a4d8108 17551+ spin_unlock(&d->d_lock);
027c5e7a 17552+ break;
dece6358 17553+ }
1facf9fc 17554+
f6b6e03d 17555+cont_unlock:
027c5e7a 17556+ spin_unlock(&d->d_lock);
1308ab2a 17557+ }
027c5e7a 17558+ spin_unlock(&parent->d_lock);
4a4d8108 17559+ dput(parent);
1facf9fc 17560+
4a4d8108
AM
17561+ if (dentry)
17562+ di_write_lock_child(dentry);
1308ab2a 17563+
4a4d8108
AM
17564+ return dentry;
17565+}
dece6358 17566+
4a4d8108
AM
17567+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17568+ aufs_bindex_t bindex, ino_t h_ino)
17569+{
17570+ struct inode *inode;
17571+ ino_t ino;
17572+ int err;
17573+
17574+ inode = NULL;
17575+ err = au_xino_read(sb, bindex, h_ino, &ino);
17576+ if (!err && ino)
17577+ inode = ilookup(sb, ino);
17578+ if (!inode)
17579+ goto out;
17580+
17581+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17582+ pr_warn("wrong root branch\n");
4a4d8108
AM
17583+ iput(inode);
17584+ inode = NULL;
17585+ goto out;
1308ab2a 17586+ }
17587+
4a4d8108 17588+ ii_write_lock_child(inode);
1308ab2a 17589+
4f0767ce 17590+out:
4a4d8108 17591+ return inode;
dece6358
AM
17592+}
17593+
4a4d8108 17594+static void au_hn_bh(void *_args)
1facf9fc 17595+{
4a4d8108
AM
17596+ struct au_hnotify_args *a = _args;
17597+ struct super_block *sb;
5afbbe0d 17598+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17599+ unsigned char xino, try_iput;
1facf9fc 17600+ int err;
1308ab2a 17601+ struct inode *inode;
4a4d8108
AM
17602+ ino_t h_ino;
17603+ struct hn_job_args args;
17604+ struct dentry *dentry;
17605+ struct au_sbinfo *sbinfo;
1facf9fc 17606+
4a4d8108
AM
17607+ AuDebugOn(!_args);
17608+ AuDebugOn(!a->h_dir);
17609+ AuDebugOn(!a->dir);
17610+ AuDebugOn(!a->mask);
17611+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17612+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17613+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17614+
4a4d8108
AM
17615+ inode = NULL;
17616+ dentry = NULL;
17617+ /*
17618+ * do not lock a->dir->i_mutex here
17619+ * because of d_revalidate() may cause a deadlock.
17620+ */
17621+ sb = a->dir->i_sb;
17622+ AuDebugOn(!sb);
17623+ sbinfo = au_sbi(sb);
17624+ AuDebugOn(!sbinfo);
7f207e10 17625+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17626+
8b6a4947
AM
17627+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17628+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17629+ case FS_MOVED_FROM:
17630+ case FS_MOVED_TO:
17631+ AuWarn1("DIRREN with UDBA may not work correctly "
17632+ "for the direct rename(2)\n");
17633+ }
17634+
4a4d8108
AM
17635+ ii_read_lock_parent(a->dir);
17636+ bfound = -1;
5afbbe0d
AM
17637+ bbot = au_ibbot(a->dir);
17638+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17639+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17640+ bfound = bindex;
17641+ break;
17642+ }
17643+ ii_read_unlock(a->dir);
17644+ if (unlikely(bfound < 0))
17645+ goto out;
1facf9fc 17646+
4a4d8108
AM
17647+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17648+ h_ino = 0;
17649+ if (a->h_child_inode)
17650+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17651+
4a4d8108
AM
17652+ if (a->h_child_nlen
17653+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17654+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17655+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17656+ a->dir);
17657+ try_iput = 0;
5527c038
JR
17658+ if (dentry && d_really_is_positive(dentry))
17659+ inode = d_inode(dentry);
4a4d8108
AM
17660+ if (xino && !inode && h_ino
17661+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17662+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17663+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17664+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17665+ try_iput = 1;
f0c0a007 17666+ }
1facf9fc 17667+
4a4d8108
AM
17668+ args.flags = a->flags[AuHn_CHILD];
17669+ args.dentry = dentry;
17670+ args.inode = inode;
17671+ args.h_inode = a->h_child_inode;
17672+ args.dir = a->dir;
17673+ args.h_dir = a->h_dir;
17674+ args.h_name = a->h_child_name;
17675+ args.h_nlen = a->h_child_nlen;
17676+ err = hn_job(&args);
17677+ if (dentry) {
027c5e7a 17678+ if (au_di(dentry))
4a4d8108
AM
17679+ di_write_unlock(dentry);
17680+ dput(dentry);
17681+ }
17682+ if (inode && try_iput) {
17683+ ii_write_unlock(inode);
17684+ iput(inode);
17685+ }
1facf9fc 17686+
4a4d8108
AM
17687+ ii_write_lock_parent(a->dir);
17688+ args.flags = a->flags[AuHn_PARENT];
17689+ args.dentry = NULL;
17690+ args.inode = a->dir;
17691+ args.h_inode = a->h_dir;
17692+ args.dir = NULL;
17693+ args.h_dir = NULL;
17694+ args.h_name = NULL;
17695+ args.h_nlen = 0;
17696+ err = hn_job(&args);
17697+ ii_write_unlock(a->dir);
1facf9fc 17698+
4f0767ce 17699+out:
4a4d8108
AM
17700+ iput(a->h_child_inode);
17701+ iput(a->h_dir);
17702+ iput(a->dir);
027c5e7a
AM
17703+ si_write_unlock(sb);
17704+ au_nwt_done(&sbinfo->si_nowait);
9f237c51 17705+ au_kfree_rcu(a);
dece6358 17706+}
1facf9fc 17707+
4a4d8108
AM
17708+/* ---------------------------------------------------------------------- */
17709+
17710+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
b62f1e65 17711+ const struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17712+{
4a4d8108 17713+ int err, len;
53392da6 17714+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17715+ unsigned char isdir, isroot, wh;
17716+ struct inode *dir;
17717+ struct au_hnotify_args *args;
17718+ char *p, *h_child_name;
dece6358 17719+
1308ab2a 17720+ err = 0;
4a4d8108
AM
17721+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17722+ dir = igrab(hnotify->hn_aufs_inode);
17723+ if (!dir)
17724+ goto out;
1facf9fc 17725+
4a4d8108
AM
17726+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17727+ wh = 0;
17728+ h_child_name = (void *)h_child_qstr->name;
17729+ len = h_child_qstr->len;
17730+ if (h_child_name) {
17731+ if (len > AUFS_WH_PFX_LEN
17732+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17733+ h_child_name += AUFS_WH_PFX_LEN;
17734+ len -= AUFS_WH_PFX_LEN;
17735+ wh = 1;
17736+ }
1facf9fc 17737+ }
dece6358 17738+
4a4d8108
AM
17739+ isdir = 0;
17740+ if (h_child_inode)
17741+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17742+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17743+ flags[AuHn_CHILD] = 0;
17744+ if (isdir)
17745+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17746+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17747+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
fbc438ed 17748+ switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
4a4d8108
AM
17749+ case FS_MOVED_FROM:
17750+ case FS_MOVED_TO:
17751+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17752+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17753+ /*FALLTHROUGH*/
17754+ case FS_CREATE:
fb47a38f 17755+ AuDebugOn(!h_child_name);
4a4d8108 17756+ break;
1facf9fc 17757+
4a4d8108
AM
17758+ case FS_DELETE:
17759+ /*
17760+ * aufs never be able to get this child inode.
17761+ * revalidation should be in d_revalidate()
17762+ * by checking i_nlink, i_generation or d_unhashed().
17763+ */
17764+ AuDebugOn(!h_child_name);
17765+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17766+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17767+ break;
dece6358 17768+
4a4d8108
AM
17769+ default:
17770+ AuDebugOn(1);
17771+ }
1308ab2a 17772+
4a4d8108
AM
17773+ if (wh)
17774+ h_child_inode = NULL;
1308ab2a 17775+
4a4d8108
AM
17776+ err = -ENOMEM;
17777+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17778+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17779+ if (unlikely(!args)) {
17780+ AuErr1("no memory\n");
17781+ iput(dir);
17782+ goto out;
17783+ }
17784+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17785+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17786+ args->mask = mask;
17787+ args->dir = dir;
17788+ args->h_dir = igrab(h_dir);
17789+ if (h_child_inode)
17790+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17791+ args->h_child_inode = h_child_inode;
17792+ args->h_child_nlen = len;
17793+ if (len) {
17794+ p = (void *)args;
17795+ p += sizeof(*args);
17796+ memcpy(p, h_child_name, len);
17797+ p[len] = 0;
1308ab2a 17798+ }
1308ab2a 17799+
38d290e6 17800+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17801+ f = 0;
38d290e6
JR
17802+ if (!dir->i_nlink
17803+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17804+ f = AuWkq_NEST;
17805+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17806+ if (unlikely(err)) {
17807+ pr_err("wkq %d\n", err);
17808+ iput(args->h_child_inode);
17809+ iput(args->h_dir);
17810+ iput(args->dir);
9f237c51 17811+ au_kfree_rcu(args);
1facf9fc 17812+ }
1facf9fc 17813+
4a4d8108 17814+out:
1facf9fc 17815+ return err;
17816+}
17817+
027c5e7a
AM
17818+/* ---------------------------------------------------------------------- */
17819+
17820+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17821+{
17822+ int err;
17823+
17824+ AuDebugOn(!(udba & AuOptMask_UDBA));
17825+
17826+ err = 0;
17827+ if (au_hnotify_op.reset_br)
17828+ err = au_hnotify_op.reset_br(udba, br, perm);
17829+
17830+ return err;
17831+}
17832+
17833+int au_hnotify_init_br(struct au_branch *br, int perm)
17834+{
17835+ int err;
17836+
17837+ err = 0;
17838+ if (au_hnotify_op.init_br)
17839+ err = au_hnotify_op.init_br(br, perm);
17840+
17841+ return err;
17842+}
17843+
17844+void au_hnotify_fin_br(struct au_branch *br)
17845+{
17846+ if (au_hnotify_op.fin_br)
17847+ au_hnotify_op.fin_br(br);
17848+}
17849+
4a4d8108
AM
17850+static void au_hn_destroy_cache(void)
17851+{
1c60b727
AM
17852+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17853+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17854+}
1308ab2a 17855+
4a4d8108 17856+int __init au_hnotify_init(void)
1facf9fc 17857+{
1308ab2a 17858+ int err;
1308ab2a 17859+
4a4d8108 17860+ err = -ENOMEM;
1c60b727
AM
17861+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17862+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17863+ err = 0;
17864+ if (au_hnotify_op.init)
17865+ err = au_hnotify_op.init();
4a4d8108
AM
17866+ if (unlikely(err))
17867+ au_hn_destroy_cache();
1308ab2a 17868+ }
1308ab2a 17869+ AuTraceErr(err);
4a4d8108 17870+ return err;
1308ab2a 17871+}
17872+
4a4d8108 17873+void au_hnotify_fin(void)
1308ab2a 17874+{
027c5e7a
AM
17875+ if (au_hnotify_op.fin)
17876+ au_hnotify_op.fin();
f0c0a007 17877+
4a4d8108 17878+ /* cf. au_cache_fin() */
1c60b727 17879+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17880+ au_hn_destroy_cache();
dece6358 17881+}
7f207e10 17882diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
eca34b5c
AM
17883--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
17884+++ linux/fs/aufs/iinfo.c 2019-07-11 15:42:14.468904634 +0200
062440b3 17885@@ -0,0 +1,286 @@
cd7a4cd9 17886+// SPDX-License-Identifier: GPL-2.0
dece6358 17887+/*
ba1aed25 17888+ * Copyright (C) 2005-2019 Junjiro R. Okajima
dece6358
AM
17889+ *
17890+ * This program, aufs is free software; you can redistribute it and/or modify
17891+ * it under the terms of the GNU General Public License as published by
17892+ * the Free Software Foundation; either version 2 of the License, or
17893+ * (at your option) any later version.
17894+ *
17895+ * This program is distributed in the hope that it will be useful,
17896+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17897+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17898+ * GNU General Public License for more details.
17899+ *
17900+ * You should have received a copy of the GNU General Public License
523b37e3 17901+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17902+ */
1facf9fc 17903+
dece6358 17904+/*
4a4d8108 17905+ * inode private data
dece6358 17906+ */
1facf9fc 17907+
1308ab2a 17908+#include "aufs.h"
1facf9fc 17909+
4a4d8108 17910+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17911+{
4a4d8108 17912+ struct inode *h_inode;
5afbbe0d 17913+ struct au_hinode *hinode;
1facf9fc 17914+
4a4d8108 17915+ IiMustAnyLock(inode);
1facf9fc 17916+
5afbbe0d
AM
17917+ hinode = au_hinode(au_ii(inode), bindex);
17918+ h_inode = hinode->hi_inode;
4a4d8108
AM
17919+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17920+ return h_inode;
17921+}
1facf9fc 17922+
4a4d8108
AM
17923+/* todo: hard/soft set? */
17924+void au_hiput(struct au_hinode *hinode)
17925+{
17926+ au_hn_free(hinode);
17927+ dput(hinode->hi_whdentry);
17928+ iput(hinode->hi_inode);
17929+}
1facf9fc 17930+
4a4d8108
AM
17931+unsigned int au_hi_flags(struct inode *inode, int isdir)
17932+{
17933+ unsigned int flags;
17934+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 17935+
4a4d8108
AM
17936+ flags = 0;
17937+ if (au_opt_test(mnt_flags, XINO))
17938+ au_fset_hi(flags, XINO);
17939+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17940+ au_fset_hi(flags, HNOTIFY);
17941+ return flags;
1facf9fc 17942+}
17943+
4a4d8108
AM
17944+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17945+ struct inode *h_inode, unsigned int flags)
1308ab2a 17946+{
4a4d8108
AM
17947+ struct au_hinode *hinode;
17948+ struct inode *hi;
17949+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 17950+
4a4d8108 17951+ IiMustWriteLock(inode);
dece6358 17952+
5afbbe0d 17953+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
17954+ hi = hinode->hi_inode;
17955+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17956+
17957+ if (hi)
17958+ au_hiput(hinode);
17959+ hinode->hi_inode = h_inode;
17960+ if (h_inode) {
17961+ int err;
17962+ struct super_block *sb = inode->i_sb;
17963+ struct au_branch *br;
17964+
027c5e7a
AM
17965+ AuDebugOn(inode->i_mode
17966+ && (h_inode->i_mode & S_IFMT)
17967+ != (inode->i_mode & S_IFMT));
5afbbe0d 17968+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
17969+ au_cpup_igen(inode, h_inode);
17970+ br = au_sbr(sb, bindex);
17971+ hinode->hi_id = br->br_id;
17972+ if (au_ftest_hi(flags, XINO)) {
17973+ err = au_xino_write(sb, bindex, h_inode->i_ino,
17974+ inode->i_ino);
17975+ if (unlikely(err))
17976+ AuIOErr1("failed au_xino_write() %d\n", err);
17977+ }
17978+
17979+ if (au_ftest_hi(flags, HNOTIFY)
17980+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 17981+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
17982+ if (unlikely(err))
17983+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 17984+ }
17985+ }
4a4d8108 17986+}
dece6358 17987+
4a4d8108
AM
17988+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17989+ struct dentry *h_wh)
17990+{
17991+ struct au_hinode *hinode;
dece6358 17992+
4a4d8108
AM
17993+ IiMustWriteLock(inode);
17994+
5afbbe0d 17995+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
17996+ AuDebugOn(hinode->hi_whdentry);
17997+ hinode->hi_whdentry = h_wh;
1facf9fc 17998+}
17999+
537831f9 18000+void au_update_iigen(struct inode *inode, int half)
1308ab2a 18001+{
537831f9
AM
18002+ struct au_iinfo *iinfo;
18003+ struct au_iigen *iigen;
18004+ unsigned int sigen;
18005+
18006+ sigen = au_sigen(inode->i_sb);
18007+ iinfo = au_ii(inode);
18008+ iigen = &iinfo->ii_generation;
be52b249 18009+ spin_lock(&iigen->ig_spin);
537831f9
AM
18010+ iigen->ig_generation = sigen;
18011+ if (half)
18012+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18013+ else
18014+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 18015+ spin_unlock(&iigen->ig_spin);
4a4d8108 18016+}
1facf9fc 18017+
4a4d8108
AM
18018+/* it may be called at remount time, too */
18019+void au_update_ibrange(struct inode *inode, int do_put_zero)
18020+{
18021+ struct au_iinfo *iinfo;
5afbbe0d 18022+ aufs_bindex_t bindex, bbot;
1facf9fc 18023+
5afbbe0d 18024+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18025+ IiMustWriteLock(inode);
1facf9fc 18026+
5afbbe0d
AM
18027+ iinfo = au_ii(inode);
18028+ if (do_put_zero && iinfo->ii_btop >= 0) {
18029+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18030+ bindex++) {
18031+ struct inode *h_i;
1facf9fc 18032+
5afbbe0d 18033+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
18034+ if (h_i
18035+ && !h_i->i_nlink
18036+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
18037+ au_set_h_iptr(inode, bindex, NULL, 0);
18038+ }
4a4d8108
AM
18039+ }
18040+
5afbbe0d
AM
18041+ iinfo->ii_btop = -1;
18042+ iinfo->ii_bbot = -1;
18043+ bbot = au_sbbot(inode->i_sb);
18044+ for (bindex = 0; bindex <= bbot; bindex++)
18045+ if (au_hinode(iinfo, bindex)->hi_inode) {
18046+ iinfo->ii_btop = bindex;
4a4d8108 18047+ break;
027c5e7a 18048+ }
5afbbe0d
AM
18049+ if (iinfo->ii_btop >= 0)
18050+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18051+ if (au_hinode(iinfo, bindex)->hi_inode) {
18052+ iinfo->ii_bbot = bindex;
027c5e7a
AM
18053+ break;
18054+ }
5afbbe0d 18055+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 18056+}
1facf9fc 18057+
dece6358 18058+/* ---------------------------------------------------------------------- */
1facf9fc 18059+
4a4d8108 18060+void au_icntnr_init_once(void *_c)
dece6358 18061+{
4a4d8108
AM
18062+ struct au_icntnr *c = _c;
18063+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 18064+
be52b249 18065+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
18066+ au_rw_init(&iinfo->ii_rwsem);
18067+ inode_init_once(&c->vfs_inode);
18068+}
1facf9fc 18069+
5afbbe0d
AM
18070+void au_hinode_init(struct au_hinode *hinode)
18071+{
18072+ hinode->hi_inode = NULL;
18073+ hinode->hi_id = -1;
18074+ au_hn_init(hinode);
18075+ hinode->hi_whdentry = NULL;
18076+}
18077+
4a4d8108
AM
18078+int au_iinfo_init(struct inode *inode)
18079+{
18080+ struct au_iinfo *iinfo;
18081+ struct super_block *sb;
5afbbe0d 18082+ struct au_hinode *hi;
4a4d8108 18083+ int nbr, i;
1facf9fc 18084+
4a4d8108
AM
18085+ sb = inode->i_sb;
18086+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 18087+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
18088+ if (unlikely(nbr <= 0))
18089+ nbr = 1;
5afbbe0d
AM
18090+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18091+ if (hi) {
acd2b654 18092+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
5afbbe0d
AM
18093+
18094+ iinfo->ii_hinode = hi;
18095+ for (i = 0; i < nbr; i++, hi++)
18096+ au_hinode_init(hi);
1facf9fc 18097+
537831f9 18098+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
18099+ iinfo->ii_btop = -1;
18100+ iinfo->ii_bbot = -1;
4a4d8108
AM
18101+ iinfo->ii_vdir = NULL;
18102+ return 0;
1308ab2a 18103+ }
4a4d8108
AM
18104+ return -ENOMEM;
18105+}
1facf9fc 18106+
e2f27e51 18107+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 18108+{
5afbbe0d 18109+ int err, i;
4a4d8108 18110+ struct au_hinode *hip;
1facf9fc 18111+
4a4d8108
AM
18112+ AuRwMustWriteLock(&iinfo->ii_rwsem);
18113+
18114+ err = -ENOMEM;
e2f27e51
AM
18115+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18116+ may_shrink);
4a4d8108
AM
18117+ if (hip) {
18118+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18119+ i = iinfo->ii_bbot + 1;
18120+ hip += i;
18121+ for (; i < nbr; i++, hip++)
18122+ au_hinode_init(hip);
4a4d8108 18123+ err = 0;
1308ab2a 18124+ }
4a4d8108 18125+
1308ab2a 18126+ return err;
1facf9fc 18127+}
18128+
4a4d8108 18129+void au_iinfo_fin(struct inode *inode)
1facf9fc 18130+{
4a4d8108
AM
18131+ struct au_iinfo *iinfo;
18132+ struct au_hinode *hi;
18133+ struct super_block *sb;
5afbbe0d 18134+ aufs_bindex_t bindex, bbot;
b752ccd1 18135+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18136+
5afbbe0d 18137+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18138+
b752ccd1 18139+ sb = inode->i_sb;
acd2b654 18140+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
b752ccd1
AM
18141+ if (si_pid_test(sb))
18142+ au_xino_delete_inode(inode, unlinked);
18143+ else {
18144+ /*
18145+ * it is safe to hide the dependency between sbinfo and
18146+ * sb->s_umount.
18147+ */
18148+ lockdep_off();
18149+ si_noflush_read_lock(sb);
18150+ au_xino_delete_inode(inode, unlinked);
18151+ si_read_unlock(sb);
18152+ lockdep_on();
18153+ }
18154+
5afbbe0d 18155+ iinfo = au_ii(inode);
4a4d8108 18156+ if (iinfo->ii_vdir)
1c60b727 18157+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18158+
5afbbe0d 18159+ bindex = iinfo->ii_btop;
b752ccd1 18160+ if (bindex >= 0) {
5afbbe0d
AM
18161+ hi = au_hinode(iinfo, bindex);
18162+ bbot = iinfo->ii_bbot;
18163+ while (bindex++ <= bbot) {
b752ccd1 18164+ if (hi->hi_inode)
4a4d8108 18165+ au_hiput(hi);
4a4d8108
AM
18166+ hi++;
18167+ }
18168+ }
9f237c51 18169+ au_kfree_rcu(iinfo->ii_hinode);
4a4d8108 18170+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18171+}
7f207e10 18172diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
eca34b5c
AM
18173--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
18174+++ linux/fs/aufs/inode.c 2019-07-11 15:42:14.468904634 +0200
eca801bf 18175@@ -0,0 +1,529 @@
cd7a4cd9 18176+// SPDX-License-Identifier: GPL-2.0
4a4d8108 18177+/*
ba1aed25 18178+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
18179+ *
18180+ * This program, aufs is free software; you can redistribute it and/or modify
18181+ * it under the terms of the GNU General Public License as published by
18182+ * the Free Software Foundation; either version 2 of the License, or
18183+ * (at your option) any later version.
18184+ *
18185+ * This program is distributed in the hope that it will be useful,
18186+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18187+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18188+ * GNU General Public License for more details.
18189+ *
18190+ * You should have received a copy of the GNU General Public License
523b37e3 18191+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18192+ */
1facf9fc 18193+
4a4d8108
AM
18194+/*
18195+ * inode functions
18196+ */
1facf9fc 18197+
eca801bf 18198+#include <linux/iversion.h>
4a4d8108 18199+#include "aufs.h"
1308ab2a 18200+
4a4d8108
AM
18201+struct inode *au_igrab(struct inode *inode)
18202+{
18203+ if (inode) {
18204+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18205+ ihold(inode);
1facf9fc 18206+ }
4a4d8108
AM
18207+ return inode;
18208+}
1facf9fc 18209+
4a4d8108
AM
18210+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18211+{
18212+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18213+ au_update_iigen(inode, /*half*/1);
4a4d8108 18214+ if (do_version)
be118d29 18215+ inode_inc_iversion(inode);
dece6358 18216+}
1facf9fc 18217+
027c5e7a 18218+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18219+{
e2f27e51 18220+ int err, e, nbr;
027c5e7a 18221+ umode_t type;
4a4d8108 18222+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18223+ struct super_block *sb;
4a4d8108 18224+ struct au_iinfo *iinfo;
027c5e7a 18225+ struct au_hinode *p, *q, tmp;
1facf9fc 18226+
5afbbe0d 18227+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18228+ IiMustWriteLock(inode);
1facf9fc 18229+
027c5e7a 18230+ *update = 0;
4a4d8108 18231+ sb = inode->i_sb;
e2f27e51 18232+ nbr = au_sbbot(sb) + 1;
027c5e7a 18233+ type = inode->i_mode & S_IFMT;
4a4d8108 18234+ iinfo = au_ii(inode);
e2f27e51 18235+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18236+ if (unlikely(err))
1308ab2a 18237+ goto out;
1facf9fc 18238+
5afbbe0d
AM
18239+ AuDebugOn(iinfo->ii_btop < 0);
18240+ p = au_hinode(iinfo, iinfo->ii_btop);
18241+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18242+ bindex++, p++) {
18243+ if (!p->hi_inode)
18244+ continue;
1facf9fc 18245+
027c5e7a 18246+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18247+ new_bindex = au_br_index(sb, p->hi_id);
18248+ if (new_bindex == bindex)
18249+ continue;
1facf9fc 18250+
4a4d8108 18251+ if (new_bindex < 0) {
027c5e7a 18252+ *update = 1;
4a4d8108
AM
18253+ au_hiput(p);
18254+ p->hi_inode = NULL;
18255+ continue;
1308ab2a 18256+ }
4a4d8108 18257+
5afbbe0d
AM
18258+ if (new_bindex < iinfo->ii_btop)
18259+ iinfo->ii_btop = new_bindex;
18260+ if (iinfo->ii_bbot < new_bindex)
18261+ iinfo->ii_bbot = new_bindex;
4a4d8108 18262+ /* swap two lower inode, and loop again */
5afbbe0d 18263+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18264+ tmp = *q;
18265+ *q = *p;
18266+ *p = tmp;
18267+ if (tmp.hi_inode) {
18268+ bindex--;
18269+ p--;
1308ab2a 18270+ }
18271+ }
4a4d8108 18272+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18273+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18274+ e = au_dy_irefresh(inode);
18275+ if (unlikely(e && !err))
18276+ err = e;
1facf9fc 18277+
4f0767ce 18278+out:
027c5e7a
AM
18279+ AuTraceErr(err);
18280+ return err;
18281+}
18282+
b95c5147
AM
18283+void au_refresh_iop(struct inode *inode, int force_getattr)
18284+{
18285+ int type;
18286+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18287+ const struct inode_operations *iop
18288+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18289+
18290+ if (inode->i_op == iop)
18291+ return;
18292+
18293+ switch (inode->i_mode & S_IFMT) {
18294+ case S_IFDIR:
18295+ type = AuIop_DIR;
18296+ break;
18297+ case S_IFLNK:
18298+ type = AuIop_SYMLINK;
18299+ break;
18300+ default:
18301+ type = AuIop_OTHER;
18302+ break;
18303+ }
18304+
18305+ inode->i_op = iop + type;
18306+ /* unnecessary smp_wmb() */
18307+}
18308+
027c5e7a
AM
18309+int au_refresh_hinode_self(struct inode *inode)
18310+{
18311+ int err, update;
18312+
18313+ err = au_ii_refresh(inode, &update);
18314+ if (!err)
18315+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18316+
18317+ AuTraceErr(err);
4a4d8108
AM
18318+ return err;
18319+}
1facf9fc 18320+
4a4d8108
AM
18321+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18322+{
027c5e7a 18323+ int err, e, update;
4a4d8108 18324+ unsigned int flags;
027c5e7a 18325+ umode_t mode;
5afbbe0d 18326+ aufs_bindex_t bindex, bbot;
027c5e7a 18327+ unsigned char isdir;
4a4d8108
AM
18328+ struct au_hinode *p;
18329+ struct au_iinfo *iinfo;
1facf9fc 18330+
027c5e7a 18331+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18332+ if (unlikely(err))
18333+ goto out;
18334+
18335+ update = 0;
18336+ iinfo = au_ii(inode);
5afbbe0d 18337+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18338+ mode = (inode->i_mode & S_IFMT);
18339+ isdir = S_ISDIR(mode);
4a4d8108 18340+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18341+ bbot = au_dbbot(dentry);
18342+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18343+ struct inode *h_i, *h_inode;
4a4d8108
AM
18344+ struct dentry *h_d;
18345+
18346+ h_d = au_h_dptr(dentry, bindex);
5527c038 18347+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18348+ continue;
18349+
5527c038
JR
18350+ h_inode = d_inode(h_d);
18351+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18352+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18353+ h_i = au_h_iptr(inode, bindex);
18354+ if (h_i) {
5527c038 18355+ if (h_i == h_inode)
4a4d8108
AM
18356+ continue;
18357+ err = -EIO;
18358+ break;
18359+ }
18360+ }
5afbbe0d
AM
18361+ if (bindex < iinfo->ii_btop)
18362+ iinfo->ii_btop = bindex;
18363+ if (iinfo->ii_bbot < bindex)
18364+ iinfo->ii_bbot = bindex;
5527c038 18365+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18366+ update = 1;
1308ab2a 18367+ }
4a4d8108
AM
18368+ au_update_ibrange(inode, /*do_put_zero*/0);
18369+ e = au_dy_irefresh(inode);
18370+ if (unlikely(e && !err))
18371+ err = e;
027c5e7a
AM
18372+ if (!err)
18373+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18374+
4f0767ce 18375+out:
4a4d8108 18376+ AuTraceErr(err);
1308ab2a 18377+ return err;
dece6358
AM
18378+}
18379+
4a4d8108 18380+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18381+{
4a4d8108
AM
18382+ int err;
18383+ unsigned int flags;
18384+ umode_t mode;
5afbbe0d 18385+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18386+ unsigned char isdir;
18387+ struct dentry *h_dentry;
18388+ struct inode *h_inode;
18389+ struct au_iinfo *iinfo;
b95c5147 18390+ struct inode_operations *iop;
dece6358 18391+
4a4d8108 18392+ IiMustWriteLock(inode);
dece6358 18393+
4a4d8108
AM
18394+ err = 0;
18395+ isdir = 0;
b95c5147 18396+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18397+ btop = au_dbtop(dentry);
18398+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18399+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18400+ mode = h_inode->i_mode;
18401+ switch (mode & S_IFMT) {
18402+ case S_IFREG:
18403+ btail = au_dbtail(dentry);
b95c5147 18404+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18405+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18406+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18407+ if (unlikely(err))
18408+ goto out;
18409+ break;
18410+ case S_IFDIR:
18411+ isdir = 1;
18412+ btail = au_dbtaildir(dentry);
b95c5147 18413+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18414+ inode->i_fop = &aufs_dir_fop;
18415+ break;
18416+ case S_IFLNK:
18417+ btail = au_dbtail(dentry);
b95c5147 18418+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18419+ break;
18420+ case S_IFBLK:
18421+ case S_IFCHR:
18422+ case S_IFIFO:
18423+ case S_IFSOCK:
18424+ btail = au_dbtail(dentry);
b95c5147 18425+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18426+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18427+ break;
18428+ default:
18429+ AuIOErr("Unknown file type 0%o\n", mode);
18430+ err = -EIO;
1308ab2a 18431+ goto out;
4a4d8108 18432+ }
dece6358 18433+
4a4d8108
AM
18434+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18435+ flags = au_hi_flags(inode, isdir);
18436+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18437+ && au_ftest_hi(flags, HNOTIFY)
18438+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18439+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18440+ au_fclr_hi(flags, HNOTIFY);
18441+ iinfo = au_ii(inode);
5afbbe0d
AM
18442+ iinfo->ii_btop = btop;
18443+ iinfo->ii_bbot = btail;
18444+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18445+ h_dentry = au_h_dptr(dentry, bindex);
18446+ if (h_dentry)
18447+ au_set_h_iptr(inode, bindex,
5527c038 18448+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18449+ }
18450+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18451+ /*
18452+ * to force calling aufs_get_acl() every time,
18453+ * do not call cache_no_acl() for aufs inode.
18454+ */
dece6358 18455+
4f0767ce 18456+out:
4a4d8108
AM
18457+ return err;
18458+}
dece6358 18459+
027c5e7a
AM
18460+/*
18461+ * successful returns with iinfo write_locked
18462+ * minus: errno
18463+ * zero: success, matched
18464+ * plus: no error, but unmatched
18465+ */
18466+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18467+{
18468+ int err;
cfc41e69 18469+ unsigned int gen, igflags;
5afbbe0d 18470+ aufs_bindex_t bindex, bbot;
4a4d8108 18471+ struct inode *h_inode, *h_dinode;
5527c038 18472+ struct dentry *h_dentry;
dece6358 18473+
4a4d8108
AM
18474+ /*
18475+ * before this function, if aufs got any iinfo lock, it must be only
18476+ * one, the parent dir.
18477+ * it can happen by UDBA and the obsoleted inode number.
18478+ */
18479+ err = -EIO;
18480+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18481+ goto out;
18482+
027c5e7a 18483+ err = 1;
4a4d8108 18484+ ii_write_lock_new_child(inode);
5afbbe0d 18485+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18486+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18487+ bbot = au_ibbot(inode);
18488+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18489+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18490+ if (!h_inode || h_inode != h_dinode)
18491+ continue;
18492+
18493+ err = 0;
cfc41e69 18494+ gen = au_iigen(inode, &igflags);
537831f9 18495+ if (gen == au_digen(dentry)
cfc41e69 18496+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18497+ break;
537831f9
AM
18498+
18499+ /* fully refresh inode using dentry */
18500+ err = au_refresh_hinode(inode, dentry);
18501+ if (!err)
18502+ au_update_iigen(inode, /*half*/0);
18503+ break;
1facf9fc 18504+ }
dece6358 18505+
4a4d8108
AM
18506+ if (unlikely(err))
18507+ ii_write_unlock(inode);
4f0767ce 18508+out:
1facf9fc 18509+ return err;
18510+}
1facf9fc 18511+
4a4d8108
AM
18512+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18513+ unsigned int d_type, ino_t *ino)
1facf9fc 18514+{
521ced18
JR
18515+ int err, idx;
18516+ const int isnondir = d_type != DT_DIR;
1facf9fc 18517+
b752ccd1 18518+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18519+ if (isnondir) {
18520+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18521+ if (unlikely(err))
18522+ goto out;
4a4d8108 18523+ }
521ced18 18524+
4a4d8108
AM
18525+ err = au_xino_read(sb, bindex, h_ino, ino);
18526+ if (unlikely(err))
521ced18 18527+ goto out_xinondir;
1308ab2a 18528+
4a4d8108
AM
18529+ if (!*ino) {
18530+ err = -EIO;
18531+ *ino = au_xino_new_ino(sb);
18532+ if (unlikely(!*ino))
521ced18 18533+ goto out_xinondir;
4a4d8108
AM
18534+ err = au_xino_write(sb, bindex, h_ino, *ino);
18535+ if (unlikely(err))
521ced18 18536+ goto out_xinondir;
1308ab2a 18537+ }
1facf9fc 18538+
521ced18
JR
18539+out_xinondir:
18540+ if (isnondir && idx >= 0)
18541+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18542+out:
1facf9fc 18543+ return err;
18544+}
18545+
4a4d8108
AM
18546+/* successful returns with iinfo write_locked */
18547+/* todo: return with unlocked? */
18548+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18549+{
5527c038 18550+ struct inode *inode, *h_inode;
4a4d8108
AM
18551+ struct dentry *h_dentry;
18552+ struct super_block *sb;
18553+ ino_t h_ino, ino;
521ced18 18554+ int err, idx, hlinked;
5afbbe0d 18555+ aufs_bindex_t btop;
1facf9fc 18556+
4a4d8108 18557+ sb = dentry->d_sb;
5afbbe0d
AM
18558+ btop = au_dbtop(dentry);
18559+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18560+ h_inode = d_inode(h_dentry);
18561+ h_ino = h_inode->i_ino;
521ced18 18562+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18563+
521ced18 18564+new_ino:
b752ccd1
AM
18565+ /*
18566+ * stop 'race'-ing between hardlinks under different
18567+ * parents.
18568+ */
521ced18
JR
18569+ if (hlinked) {
18570+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18571+ inode = ERR_PTR(err);
18572+ if (unlikely(err))
18573+ goto out;
18574+ }
b752ccd1 18575+
5afbbe0d 18576+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18577+ inode = ERR_PTR(err);
18578+ if (unlikely(err))
521ced18 18579+ goto out_xinondir;
b752ccd1 18580+
4a4d8108
AM
18581+ if (!ino) {
18582+ ino = au_xino_new_ino(sb);
18583+ if (unlikely(!ino)) {
18584+ inode = ERR_PTR(-EIO);
521ced18 18585+ goto out_xinondir;
dece6358
AM
18586+ }
18587+ }
1facf9fc 18588+
4a4d8108
AM
18589+ AuDbg("i%lu\n", (unsigned long)ino);
18590+ inode = au_iget_locked(sb, ino);
18591+ err = PTR_ERR(inode);
18592+ if (IS_ERR(inode))
521ced18 18593+ goto out_xinondir;
1facf9fc 18594+
4a4d8108
AM
18595+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18596+ if (inode->i_state & I_NEW) {
18597+ ii_write_lock_new_child(inode);
18598+ err = set_inode(inode, dentry);
18599+ if (!err) {
18600+ unlock_new_inode(inode);
521ced18 18601+ goto out_xinondir; /* success */
4a4d8108 18602+ }
1308ab2a 18603+
027c5e7a
AM
18604+ /*
18605+ * iget_failed() calls iput(), but we need to call
18606+ * ii_write_unlock() after iget_failed(). so dirty hack for
18607+ * i_count.
18608+ */
18609+ atomic_inc(&inode->i_count);
4a4d8108 18610+ iget_failed(inode);
027c5e7a 18611+ ii_write_unlock(inode);
5afbbe0d 18612+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18613+ /* ignore this error */
18614+ goto out_iput;
18615+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18616+ /*
18617+ * horrible race condition between lookup, readdir and copyup
18618+ * (or something).
18619+ */
521ced18
JR
18620+ if (hlinked && idx >= 0)
18621+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18622+ err = reval_inode(inode, dentry);
18623+ if (unlikely(err < 0)) {
521ced18 18624+ hlinked = 0;
027c5e7a
AM
18625+ goto out_iput;
18626+ }
521ced18 18627+ if (!err)
4a4d8108 18628+ goto out; /* success */
521ced18
JR
18629+ else if (hlinked && idx >= 0) {
18630+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18631+ if (unlikely(err)) {
18632+ iput(inode);
18633+ inode = ERR_PTR(err);
18634+ goto out;
18635+ }
18636+ }
4a4d8108
AM
18637+ }
18638+
5527c038 18639+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18640+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18641+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18642+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18643+ (unsigned long)h_ino, (unsigned long)ino);
18644+ ino = 0;
5afbbe0d 18645+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18646+ if (!err) {
18647+ iput(inode);
521ced18
JR
18648+ if (hlinked && idx >= 0)
18649+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18650+ goto new_ino;
18651+ }
1308ab2a 18652+
4f0767ce 18653+out_iput:
4a4d8108 18654+ iput(inode);
4a4d8108 18655+ inode = ERR_PTR(err);
521ced18
JR
18656+out_xinondir:
18657+ if (hlinked && idx >= 0)
18658+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18659+out:
4a4d8108 18660+ return inode;
1facf9fc 18661+}
18662+
4a4d8108 18663+/* ---------------------------------------------------------------------- */
1facf9fc 18664+
4a4d8108
AM
18665+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18666+ struct inode *inode)
18667+{
18668+ int err;
076b876e 18669+ struct inode *hi;
1facf9fc 18670+
4a4d8108 18671+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18672+
4a4d8108
AM
18673+ /* pseudo-link after flushed may happen out of bounds */
18674+ if (!err
18675+ && inode
5afbbe0d
AM
18676+ && au_ibtop(inode) <= bindex
18677+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18678+ /*
18679+ * permission check is unnecessary since vfsub routine
18680+ * will be called later
18681+ */
076b876e 18682+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18683+ if (hi)
18684+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18685+ }
18686+
4a4d8108
AM
18687+ return err;
18688+}
dece6358 18689+
4a4d8108
AM
18690+int au_test_h_perm(struct inode *h_inode, int mask)
18691+{
2dfbb274 18692+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
18693+ return 0;
18694+ return inode_permission(h_inode, mask);
18695+}
1facf9fc 18696+
4a4d8108
AM
18697+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18698+{
18699+ if (au_test_nfs(h_inode->i_sb)
18700+ && (mask & MAY_WRITE)
18701+ && S_ISDIR(h_inode->i_mode))
18702+ mask |= MAY_READ; /* force permission check */
18703+ return au_test_h_perm(h_inode, mask);
1facf9fc 18704+}
7f207e10 18705diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
eca34b5c
AM
18706--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
18707+++ linux/fs/aufs/inode.h 2019-07-11 15:42:14.468904634 +0200
9f237c51 18708@@ -0,0 +1,698 @@
062440b3 18709+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 18710+/*
ba1aed25 18711+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
18712+ *
18713+ * This program, aufs is free software; you can redistribute it and/or modify
18714+ * it under the terms of the GNU General Public License as published by
18715+ * the Free Software Foundation; either version 2 of the License, or
18716+ * (at your option) any later version.
18717+ *
18718+ * This program is distributed in the hope that it will be useful,
18719+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18720+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18721+ * GNU General Public License for more details.
18722+ *
18723+ * You should have received a copy of the GNU General Public License
523b37e3 18724+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18725+ */
1facf9fc 18726+
1308ab2a 18727+/*
4a4d8108 18728+ * inode operations
1308ab2a 18729+ */
dece6358 18730+
4a4d8108
AM
18731+#ifndef __AUFS_INODE_H__
18732+#define __AUFS_INODE_H__
dece6358 18733+
4a4d8108 18734+#ifdef __KERNEL__
1308ab2a 18735+
4a4d8108 18736+#include <linux/fsnotify.h>
4a4d8108 18737+#include "rwsem.h"
1308ab2a 18738+
4a4d8108 18739+struct vfsmount;
1facf9fc 18740+
4a4d8108
AM
18741+struct au_hnotify {
18742+#ifdef CONFIG_AUFS_HNOTIFY
18743+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18744+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18745+ struct fsnotify_mark hn_mark;
4a4d8108 18746+#endif
1c60b727 18747+ struct inode *hn_aufs_inode; /* no get/put */
9f237c51 18748+ struct rcu_head rcu;
4a4d8108
AM
18749+#endif
18750+} ____cacheline_aligned_in_smp;
1facf9fc 18751+
4a4d8108
AM
18752+struct au_hinode {
18753+ struct inode *hi_inode;
18754+ aufs_bindex_t hi_id;
18755+#ifdef CONFIG_AUFS_HNOTIFY
18756+ struct au_hnotify *hi_notify;
18757+#endif
dece6358 18758+
4a4d8108
AM
18759+ /* reference to the copied-up whiteout with get/put */
18760+ struct dentry *hi_whdentry;
18761+};
dece6358 18762+
537831f9
AM
18763+/* ig_flags */
18764+#define AuIG_HALF_REFRESHED 1
18765+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18766+#define au_ig_fset(flags, name) \
18767+ do { (flags) |= AuIG_##name; } while (0)
18768+#define au_ig_fclr(flags, name) \
18769+ do { (flags) &= ~AuIG_##name; } while (0)
18770+
18771+struct au_iigen {
be52b249 18772+ spinlock_t ig_spin;
537831f9
AM
18773+ __u32 ig_generation, ig_flags;
18774+};
18775+
4a4d8108
AM
18776+struct au_vdir;
18777+struct au_iinfo {
7a9e40b8 18778+ struct au_iigen ii_generation;
4a4d8108 18779+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18780+
4a4d8108 18781+ struct au_rwsem ii_rwsem;
5afbbe0d 18782+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18783+ __u32 ii_higen;
18784+ struct au_hinode *ii_hinode;
18785+ struct au_vdir *ii_vdir;
18786+};
1facf9fc 18787+
4a4d8108 18788+struct au_icntnr {
9f237c51
AM
18789+ struct au_iinfo iinfo;
18790+ struct inode vfs_inode;
18791+ struct hlist_bl_node plink;
18792+ struct rcu_head rcu;
4a4d8108 18793+} ____cacheline_aligned_in_smp;
1308ab2a 18794+
4a4d8108
AM
18795+/* au_pin flags */
18796+#define AuPin_DI_LOCKED 1
18797+#define AuPin_MNT_WRITE (1 << 1)
18798+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18799+#define au_fset_pin(flags, name) \
18800+ do { (flags) |= AuPin_##name; } while (0)
18801+#define au_fclr_pin(flags, name) \
18802+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18803+
18804+struct au_pin {
18805+ /* input */
18806+ struct dentry *dentry;
18807+ unsigned int udba;
18808+ unsigned char lsc_di, lsc_hi, flags;
18809+ aufs_bindex_t bindex;
18810+
18811+ /* output */
18812+ struct dentry *parent;
18813+ struct au_hinode *hdir;
18814+ struct vfsmount *h_mnt;
86dc4139
AM
18815+
18816+ /* temporary unlock/relock for copyup */
18817+ struct dentry *h_dentry, *h_parent;
18818+ struct au_branch *br;
18819+ struct task_struct *task;
4a4d8108 18820+};
1facf9fc 18821+
86dc4139 18822+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18823+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18824+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18825+void au_pin_hdir_acquire_nest(struct au_pin *p);
18826+void au_pin_hdir_release(struct au_pin *p);
18827+
1308ab2a 18828+/* ---------------------------------------------------------------------- */
18829+
4a4d8108 18830+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18831+{
5afbbe0d
AM
18832+ BUG_ON(is_bad_inode(inode));
18833+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18834+}
1facf9fc 18835+
4a4d8108 18836+/* ---------------------------------------------------------------------- */
1facf9fc 18837+
4a4d8108
AM
18838+/* inode.c */
18839+struct inode *au_igrab(struct inode *inode);
b95c5147 18840+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18841+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18842+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18843+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18844+ unsigned int d_type, ino_t *ino);
18845+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18846+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18847+ struct inode *inode);
18848+int au_test_h_perm(struct inode *h_inode, int mask);
18849+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 18850+
4a4d8108
AM
18851+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18852+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18853+{
18854+#ifdef CONFIG_AUFS_SHWH
18855+ return au_ino(sb, bindex, h_ino, d_type, ino);
18856+#else
18857+ return 0;
18858+#endif
18859+}
1facf9fc 18860+
4a4d8108 18861+/* i_op.c */
b95c5147
AM
18862+enum {
18863+ AuIop_SYMLINK,
18864+ AuIop_DIR,
18865+ AuIop_OTHER,
18866+ AuIop_Last
18867+};
18868+extern struct inode_operations aufs_iop[AuIop_Last],
18869+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18870+
4a4d8108
AM
18871+/* au_wr_dir flags */
18872+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18873+#define AuWrDir_ISDIR (1 << 1)
18874+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18875+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18876+#define au_fset_wrdir(flags, name) \
18877+ do { (flags) |= AuWrDir_##name; } while (0)
18878+#define au_fclr_wrdir(flags, name) \
18879+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18880+
4a4d8108
AM
18881+struct au_wr_dir_args {
18882+ aufs_bindex_t force_btgt;
18883+ unsigned char flags;
18884+};
18885+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18886+ struct au_wr_dir_args *args);
dece6358 18887+
4a4d8108
AM
18888+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18889+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18890+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18891+ unsigned int udba, unsigned char flags);
18892+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18893+ unsigned int udba, unsigned char flags) __must_check;
18894+int au_do_pin(struct au_pin *pin) __must_check;
18895+void au_unpin(struct au_pin *pin);
c1595e42
JR
18896+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18897+
18898+#define AuIcpup_DID_CPUP 1
18899+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18900+#define au_fset_icpup(flags, name) \
18901+ do { (flags) |= AuIcpup_##name; } while (0)
18902+#define au_fclr_icpup(flags, name) \
18903+ do { (flags) &= ~AuIcpup_##name; } while (0)
18904+
18905+struct au_icpup_args {
18906+ unsigned char flags;
18907+ unsigned char pin_flags;
18908+ aufs_bindex_t btgt;
18909+ unsigned int udba;
18910+ struct au_pin pin;
18911+ struct path h_path;
18912+ struct inode *h_inode;
18913+};
18914+
18915+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18916+ struct au_icpup_args *a);
18917+
a2654f78
AM
18918+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
18919+ int locked);
1facf9fc 18920+
4a4d8108
AM
18921+/* i_op_add.c */
18922+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18923+ struct dentry *h_parent, int isdir);
7eafdf33
AM
18924+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18925+ dev_t dev);
4a4d8108 18926+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 18927+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 18928+ bool want_excl);
b912730e
AM
18929+struct vfsub_aopen_args;
18930+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18931+ struct vfsub_aopen_args *args);
38d290e6 18932+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
18933+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18934+ struct dentry *dentry);
7eafdf33 18935+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 18936+
4a4d8108
AM
18937+/* i_op_del.c */
18938+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18939+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18940+ struct dentry *h_parent, int isdir);
18941+int aufs_unlink(struct inode *dir, struct dentry *dentry);
18942+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 18943+
4a4d8108
AM
18944+/* i_op_ren.c */
18945+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18946+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
f2c43d5f
AM
18947+ struct inode *dir, struct dentry *dentry,
18948+ unsigned int flags);
1facf9fc 18949+
4a4d8108
AM
18950+/* iinfo.c */
18951+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18952+void au_hiput(struct au_hinode *hinode);
18953+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18954+ struct dentry *h_wh);
18955+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 18956+
4a4d8108
AM
18957+/* hinode flags */
18958+#define AuHi_XINO 1
18959+#define AuHi_HNOTIFY (1 << 1)
18960+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
18961+#define au_fset_hi(flags, name) \
18962+ do { (flags) |= AuHi_##name; } while (0)
18963+#define au_fclr_hi(flags, name) \
18964+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 18965+
4a4d8108
AM
18966+#ifndef CONFIG_AUFS_HNOTIFY
18967+#undef AuHi_HNOTIFY
18968+#define AuHi_HNOTIFY 0
18969+#endif
1facf9fc 18970+
4a4d8108
AM
18971+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18972+ struct inode *h_inode, unsigned int flags);
1facf9fc 18973+
537831f9 18974+void au_update_iigen(struct inode *inode, int half);
4a4d8108 18975+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 18976+
4a4d8108 18977+void au_icntnr_init_once(void *_c);
5afbbe0d 18978+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
18979+int au_iinfo_init(struct inode *inode);
18980+void au_iinfo_fin(struct inode *inode);
e2f27e51 18981+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 18982+
e49829fe 18983+#ifdef CONFIG_PROC_FS
4a4d8108 18984+/* plink.c */
e49829fe 18985+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 18986+struct au_sbinfo;
e49829fe
JR
18987+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18988+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
18989+#ifdef CONFIG_AUFS_DEBUG
18990+void au_plink_list(struct super_block *sb);
18991+#else
18992+AuStubVoid(au_plink_list, struct super_block *sb)
18993+#endif
18994+int au_plink_test(struct inode *inode);
18995+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18996+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18997+ struct dentry *h_dentry);
e49829fe
JR
18998+void au_plink_put(struct super_block *sb, int verbose);
18999+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 19000+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
19001+#else
19002+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
19003+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
19004+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
19005+AuStubVoid(au_plink_list, struct super_block *sb);
19006+AuStubInt0(au_plink_test, struct inode *inode);
19007+AuStub(struct dentry *, au_plink_lkup, return NULL,
19008+ struct inode *inode, aufs_bindex_t bindex);
19009+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19010+ struct dentry *h_dentry);
19011+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19012+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19013+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19014+#endif /* CONFIG_PROC_FS */
1facf9fc 19015+
c1595e42
JR
19016+#ifdef CONFIG_AUFS_XATTR
19017+/* xattr.c */
7e9cd9fe
AM
19018+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19019+ unsigned int verbose);
c1595e42 19020+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 19021+void au_xattr_init(struct super_block *sb);
c1595e42
JR
19022+#else
19023+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 19024+ int ignore_flags, unsigned int verbose);
f2c43d5f 19025+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
19026+#endif
19027+
19028+#ifdef CONFIG_FS_POSIX_ACL
19029+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19030+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19031+#endif
19032+
19033+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19034+enum {
19035+ AU_XATTR_SET,
c1595e42
JR
19036+ AU_ACL_SET
19037+};
19038+
f2c43d5f 19039+struct au_sxattr {
c1595e42
JR
19040+ int type;
19041+ union {
19042+ struct {
19043+ const char *name;
19044+ const void *value;
19045+ size_t size;
19046+ int flags;
19047+ } set;
19048+ struct {
c1595e42
JR
19049+ struct posix_acl *acl;
19050+ int type;
19051+ } acl_set;
19052+ } u;
19053+};
f2c43d5f
AM
19054+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19055+ struct au_sxattr *arg);
c1595e42
JR
19056+#endif
19057+
4a4d8108 19058+/* ---------------------------------------------------------------------- */
1308ab2a 19059+
4a4d8108
AM
19060+/* lock subclass for iinfo */
19061+enum {
19062+ AuLsc_II_CHILD, /* child first */
19063+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
19064+ AuLsc_II_CHILD3, /* copyup dirs */
19065+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
19066+ AuLsc_II_PARENT2,
19067+ AuLsc_II_PARENT3, /* copyup dirs */
19068+ AuLsc_II_NEW_CHILD
19069+};
1308ab2a 19070+
1facf9fc 19071+/*
4a4d8108
AM
19072+ * ii_read_lock_child, ii_write_lock_child,
19073+ * ii_read_lock_child2, ii_write_lock_child2,
19074+ * ii_read_lock_child3, ii_write_lock_child3,
19075+ * ii_read_lock_parent, ii_write_lock_parent,
19076+ * ii_read_lock_parent2, ii_write_lock_parent2,
19077+ * ii_read_lock_parent3, ii_write_lock_parent3,
19078+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 19079+ */
4a4d8108
AM
19080+#define AuReadLockFunc(name, lsc) \
19081+static inline void ii_read_lock_##name(struct inode *i) \
19082+{ \
19083+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19084+}
19085+
19086+#define AuWriteLockFunc(name, lsc) \
19087+static inline void ii_write_lock_##name(struct inode *i) \
19088+{ \
19089+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19090+}
19091+
19092+#define AuRWLockFuncs(name, lsc) \
19093+ AuReadLockFunc(name, lsc) \
19094+ AuWriteLockFunc(name, lsc)
19095+
19096+AuRWLockFuncs(child, CHILD);
19097+AuRWLockFuncs(child2, CHILD2);
19098+AuRWLockFuncs(child3, CHILD3);
19099+AuRWLockFuncs(parent, PARENT);
19100+AuRWLockFuncs(parent2, PARENT2);
19101+AuRWLockFuncs(parent3, PARENT3);
19102+AuRWLockFuncs(new_child, NEW_CHILD);
19103+
19104+#undef AuReadLockFunc
19105+#undef AuWriteLockFunc
19106+#undef AuRWLockFuncs
1facf9fc 19107+
8b6a4947
AM
19108+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19109+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19110+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19111+
4a4d8108
AM
19112+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19113+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19114+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19115+
4a4d8108 19116+/* ---------------------------------------------------------------------- */
1308ab2a 19117+
027c5e7a
AM
19118+static inline void au_icntnr_init(struct au_icntnr *c)
19119+{
19120+#ifdef CONFIG_AUFS_DEBUG
19121+ c->vfs_inode.i_mode = 0;
19122+#endif
19123+}
19124+
cfc41e69 19125+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19126+{
537831f9
AM
19127+ unsigned int gen;
19128+ struct au_iinfo *iinfo;
be52b249 19129+ struct au_iigen *iigen;
537831f9
AM
19130+
19131+ iinfo = au_ii(inode);
be52b249
AM
19132+ iigen = &iinfo->ii_generation;
19133+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19134+ if (igflags)
19135+ *igflags = iigen->ig_flags;
be52b249
AM
19136+ gen = iigen->ig_generation;
19137+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19138+
19139+ return gen;
4a4d8108 19140+}
1308ab2a 19141+
4a4d8108
AM
19142+/* tiny test for inode number */
19143+/* tmpfs generation is too rough */
19144+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19145+{
19146+ struct au_iinfo *iinfo;
1308ab2a 19147+
4a4d8108
AM
19148+ iinfo = au_ii(inode);
19149+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19150+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19151+ && iinfo->ii_higen == h_inode->i_generation);
19152+}
1308ab2a 19153+
4a4d8108
AM
19154+static inline void au_iigen_dec(struct inode *inode)
19155+{
537831f9 19156+ struct au_iinfo *iinfo;
be52b249 19157+ struct au_iigen *iigen;
537831f9
AM
19158+
19159+ iinfo = au_ii(inode);
be52b249
AM
19160+ iigen = &iinfo->ii_generation;
19161+ spin_lock(&iigen->ig_spin);
19162+ iigen->ig_generation--;
19163+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19164+}
19165+
19166+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19167+{
19168+ int err;
19169+
19170+ err = 0;
537831f9 19171+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19172+ err = -EIO;
19173+
19174+ return err;
4a4d8108 19175+}
1308ab2a 19176+
4a4d8108 19177+/* ---------------------------------------------------------------------- */
1308ab2a 19178+
5afbbe0d
AM
19179+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19180+ aufs_bindex_t bindex)
19181+{
19182+ return iinfo->ii_hinode + bindex;
19183+}
19184+
19185+static inline int au_is_bad_inode(struct inode *inode)
19186+{
19187+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19188+}
19189+
4a4d8108
AM
19190+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19191+ aufs_bindex_t bindex)
19192+{
19193+ IiMustAnyLock(inode);
5afbbe0d 19194+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19195+}
1308ab2a 19196+
5afbbe0d 19197+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19198+{
19199+ IiMustAnyLock(inode);
5afbbe0d 19200+ return au_ii(inode)->ii_btop;
4a4d8108 19201+}
1308ab2a 19202+
5afbbe0d 19203+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19204+{
19205+ IiMustAnyLock(inode);
5afbbe0d 19206+ return au_ii(inode)->ii_bbot;
4a4d8108 19207+}
1308ab2a 19208+
4a4d8108
AM
19209+static inline struct au_vdir *au_ivdir(struct inode *inode)
19210+{
19211+ IiMustAnyLock(inode);
19212+ return au_ii(inode)->ii_vdir;
19213+}
1308ab2a 19214+
4a4d8108
AM
19215+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19216+{
19217+ IiMustAnyLock(inode);
5afbbe0d 19218+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19219+}
1308ab2a 19220+
5afbbe0d 19221+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19222+{
4a4d8108 19223+ IiMustWriteLock(inode);
5afbbe0d 19224+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19225+}
1308ab2a 19226+
5afbbe0d 19227+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19228+{
19229+ IiMustWriteLock(inode);
5afbbe0d 19230+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19231+}
19232+
4a4d8108
AM
19233+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19234+{
19235+ IiMustWriteLock(inode);
19236+ au_ii(inode)->ii_vdir = vdir;
19237+}
1facf9fc 19238+
4a4d8108 19239+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19240+{
4a4d8108 19241+ IiMustAnyLock(inode);
5afbbe0d 19242+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19243+}
dece6358 19244+
4a4d8108 19245+/* ---------------------------------------------------------------------- */
1facf9fc 19246+
4a4d8108
AM
19247+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19248+{
19249+ if (pin)
19250+ return pin->parent;
19251+ return NULL;
1facf9fc 19252+}
19253+
4a4d8108 19254+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19255+{
4a4d8108
AM
19256+ if (pin && pin->hdir)
19257+ return pin->hdir->hi_inode;
19258+ return NULL;
1308ab2a 19259+}
1facf9fc 19260+
4a4d8108
AM
19261+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19262+{
19263+ if (pin)
19264+ return pin->hdir;
19265+ return NULL;
19266+}
1facf9fc 19267+
4a4d8108 19268+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19269+{
4a4d8108
AM
19270+ if (pin)
19271+ pin->dentry = dentry;
19272+}
1308ab2a 19273+
4a4d8108
AM
19274+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19275+ unsigned char lflag)
19276+{
19277+ if (pin) {
7f207e10 19278+ if (lflag)
4a4d8108 19279+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19280+ else
4a4d8108 19281+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19282+ }
4a4d8108
AM
19283+}
19284+
7e9cd9fe 19285+#if 0 /* reserved */
4a4d8108
AM
19286+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19287+{
19288+ if (pin) {
19289+ dput(pin->parent);
19290+ pin->parent = dget(parent);
1facf9fc 19291+ }
4a4d8108 19292+}
7e9cd9fe 19293+#endif
1facf9fc 19294+
4a4d8108
AM
19295+/* ---------------------------------------------------------------------- */
19296+
027c5e7a 19297+struct au_branch;
4a4d8108
AM
19298+#ifdef CONFIG_AUFS_HNOTIFY
19299+struct au_hnotify_op {
19300+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19301+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19302+
19303+ /*
19304+ * if it returns true, the the caller should free hinode->hi_notify,
19305+ * otherwise ->free() frees it.
19306+ */
19307+ int (*free)(struct au_hinode *hinode,
19308+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19309+
19310+ void (*fin)(void);
19311+ int (*init)(void);
027c5e7a
AM
19312+
19313+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19314+ void (*fin_br)(struct au_branch *br);
19315+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19316+};
19317+
19318+/* hnotify.c */
027c5e7a 19319+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19320+void au_hn_free(struct au_hinode *hinode);
19321+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19322+void au_hn_reset(struct inode *inode, unsigned int flags);
19323+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
fbc438ed 19324+ const struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19325+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19326+int au_hnotify_init_br(struct au_branch *br, int perm);
19327+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19328+int __init au_hnotify_init(void);
19329+void au_hnotify_fin(void);
19330+
7f207e10 19331+/* hfsnotify.c */
4a4d8108
AM
19332+extern const struct au_hnotify_op au_hnotify_op;
19333+
19334+static inline
19335+void au_hn_init(struct au_hinode *hinode)
19336+{
19337+ hinode->hi_notify = NULL;
1308ab2a 19338+}
19339+
53392da6
AM
19340+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19341+{
19342+ return hinode->hi_notify;
19343+}
19344+
4a4d8108 19345+#else
c1595e42
JR
19346+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19347+ struct au_hinode *hinode __maybe_unused,
19348+ struct inode *inode __maybe_unused)
19349+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19350+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19351+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19352+ int do_set __maybe_unused)
19353+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19354+ unsigned int flags __maybe_unused)
027c5e7a
AM
19355+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19356+ struct au_branch *br __maybe_unused,
19357+ int perm __maybe_unused)
19358+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19359+ int perm __maybe_unused)
19360+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19361+AuStubInt0(__init au_hnotify_init, void)
19362+AuStubVoid(au_hnotify_fin, void)
19363+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19364+#endif /* CONFIG_AUFS_HNOTIFY */
19365+
19366+static inline void au_hn_suspend(struct au_hinode *hdir)
19367+{
19368+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19369+}
19370+
4a4d8108 19371+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19372+{
4a4d8108
AM
19373+ au_hn_ctl(hdir, /*do_set*/1);
19374+}
1308ab2a 19375+
5afbbe0d 19376+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19377+{
febd17d6 19378+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19379+ au_hn_suspend(hdir);
19380+}
dece6358 19381+
5afbbe0d 19382+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19383+ unsigned int sc __maybe_unused)
19384+{
febd17d6 19385+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19386+ au_hn_suspend(hdir);
1facf9fc 19387+}
1facf9fc 19388+
8b6a4947
AM
19389+#if 0 /* unused */
19390+#include "vfsub.h"
3c1bdaff
AM
19391+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19392+ unsigned int sc)
19393+{
be118d29 19394+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
19395+ au_hn_suspend(hdir);
19396+}
8b6a4947 19397+#endif
3c1bdaff 19398+
5afbbe0d 19399+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19400+{
19401+ au_hn_resume(hdir);
febd17d6 19402+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19403+}
19404+
19405+#endif /* __KERNEL__ */
19406+#endif /* __AUFS_INODE_H__ */
7f207e10 19407diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
eca34b5c
AM
19408--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
19409+++ linux/fs/aufs/ioctl.c 2019-07-11 15:42:14.468904634 +0200
062440b3 19410@@ -0,0 +1,220 @@
cd7a4cd9 19411+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19412+/*
ba1aed25 19413+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
19414+ *
19415+ * This program, aufs is free software; you can redistribute it and/or modify
19416+ * it under the terms of the GNU General Public License as published by
19417+ * the Free Software Foundation; either version 2 of the License, or
19418+ * (at your option) any later version.
19419+ *
19420+ * This program is distributed in the hope that it will be useful,
19421+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19422+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19423+ * GNU General Public License for more details.
19424+ *
19425+ * You should have received a copy of the GNU General Public License
523b37e3 19426+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19427+ */
19428+
19429+/*
19430+ * ioctl
19431+ * plink-management and readdir in userspace.
19432+ * assist the pathconf(3) wrapper library.
c2b27bf2 19433+ * move-down
076b876e 19434+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19435+ */
19436+
c2b27bf2
AM
19437+#include <linux/compat.h>
19438+#include <linux/file.h>
4a4d8108
AM
19439+#include "aufs.h"
19440+
1e00d052 19441+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19442+{
19443+ int err, fd;
5afbbe0d 19444+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19445+ struct file *h_file;
19446+ struct super_block *sb;
19447+ struct dentry *root;
1e00d052
AM
19448+ struct au_branch *br;
19449+ struct aufs_wbr_fd wbrfd = {
19450+ .oflags = au_dir_roflags,
19451+ .brid = -1
19452+ };
19453+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19454+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19455+
1e00d052
AM
19456+ AuDebugOn(wbrfd.oflags & ~valid);
19457+
19458+ if (arg) {
19459+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19460+ if (unlikely(err)) {
19461+ err = -EFAULT;
19462+ goto out;
19463+ }
19464+
19465+ err = -EINVAL;
19466+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19467+ wbrfd.oflags |= au_dir_roflags;
19468+ AuDbg("0%o\n", wbrfd.oflags);
19469+ if (unlikely(wbrfd.oflags & ~valid))
19470+ goto out;
19471+ }
19472+
2000de60 19473+ fd = get_unused_fd_flags(0);
1e00d052
AM
19474+ err = fd;
19475+ if (unlikely(fd < 0))
4a4d8108 19476+ goto out;
4a4d8108 19477+
1e00d052 19478+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19479+ wbi = 0;
1e00d052 19480+ br = NULL;
4a4d8108
AM
19481+ sb = path->dentry->d_sb;
19482+ root = sb->s_root;
19483+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19484+ bbot = au_sbbot(sb);
1e00d052
AM
19485+ if (wbrfd.brid >= 0) {
19486+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19487+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19488+ goto out_unlock;
19489+ }
19490+
19491+ h_file = ERR_PTR(-ENOENT);
19492+ br = au_sbr(sb, wbi);
19493+ if (!au_br_writable(br->br_perm)) {
19494+ if (arg)
19495+ goto out_unlock;
19496+
19497+ bindex = wbi + 1;
19498+ wbi = -1;
5afbbe0d 19499+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19500+ br = au_sbr(sb, bindex);
19501+ if (au_br_writable(br->br_perm)) {
4a4d8108 19502+ wbi = bindex;
1e00d052 19503+ br = au_sbr(sb, wbi);
4a4d8108
AM
19504+ break;
19505+ }
19506+ }
4a4d8108
AM
19507+ }
19508+ AuDbg("wbi %d\n", wbi);
1e00d052 19509+ if (wbi >= 0)
392086de
AM
19510+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19511+ /*force_wr*/0);
1e00d052
AM
19512+
19513+out_unlock:
4a4d8108
AM
19514+ aufs_read_unlock(root, AuLock_IR);
19515+ err = PTR_ERR(h_file);
19516+ if (IS_ERR(h_file))
19517+ goto out_fd;
19518+
acd2b654 19519+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
4a4d8108
AM
19520+ fd_install(fd, h_file);
19521+ err = fd;
19522+ goto out; /* success */
19523+
4f0767ce 19524+out_fd:
4a4d8108 19525+ put_unused_fd(fd);
4f0767ce 19526+out:
1e00d052 19527+ AuTraceErr(err);
4a4d8108
AM
19528+ return err;
19529+}
19530+
19531+/* ---------------------------------------------------------------------- */
19532+
19533+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19534+{
19535+ long err;
c1595e42 19536+ struct dentry *dentry;
4a4d8108
AM
19537+
19538+ switch (cmd) {
4a4d8108
AM
19539+ case AUFS_CTL_RDU:
19540+ case AUFS_CTL_RDU_INO:
19541+ err = au_rdu_ioctl(file, cmd, arg);
19542+ break;
19543+
19544+ case AUFS_CTL_WBR_FD:
1e00d052 19545+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19546+ break;
19547+
027c5e7a
AM
19548+ case AUFS_CTL_IBUSY:
19549+ err = au_ibusy_ioctl(file, arg);
19550+ break;
19551+
076b876e
AM
19552+ case AUFS_CTL_BRINFO:
19553+ err = au_brinfo_ioctl(file, arg);
19554+ break;
19555+
19556+ case AUFS_CTL_FHSM_FD:
2000de60 19557+ dentry = file->f_path.dentry;
c1595e42
JR
19558+ if (IS_ROOT(dentry))
19559+ err = au_fhsm_fd(dentry->d_sb, arg);
19560+ else
19561+ err = -ENOTTY;
076b876e
AM
19562+ break;
19563+
4a4d8108
AM
19564+ default:
19565+ /* do not call the lower */
19566+ AuDbg("0x%x\n", cmd);
19567+ err = -ENOTTY;
19568+ }
19569+
19570+ AuTraceErr(err);
19571+ return err;
19572+}
19573+
19574+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19575+{
19576+ long err;
19577+
19578+ switch (cmd) {
c2b27bf2 19579+ case AUFS_CTL_MVDOWN:
2000de60 19580+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19581+ break;
19582+
4a4d8108 19583+ case AUFS_CTL_WBR_FD:
1e00d052 19584+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19585+ break;
19586+
19587+ default:
19588+ /* do not call the lower */
19589+ AuDbg("0x%x\n", cmd);
19590+ err = -ENOTTY;
19591+ }
19592+
19593+ AuTraceErr(err);
19594+ return err;
19595+}
b752ccd1
AM
19596+
19597+#ifdef CONFIG_COMPAT
19598+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19599+ unsigned long arg)
19600+{
19601+ long err;
19602+
19603+ switch (cmd) {
19604+ case AUFS_CTL_RDU:
19605+ case AUFS_CTL_RDU_INO:
19606+ err = au_rdu_compat_ioctl(file, cmd, arg);
19607+ break;
19608+
027c5e7a
AM
19609+ case AUFS_CTL_IBUSY:
19610+ err = au_ibusy_compat_ioctl(file, arg);
19611+ break;
19612+
076b876e
AM
19613+ case AUFS_CTL_BRINFO:
19614+ err = au_brinfo_compat_ioctl(file, arg);
19615+ break;
19616+
b752ccd1
AM
19617+ default:
19618+ err = aufs_ioctl_dir(file, cmd, arg);
19619+ }
19620+
19621+ AuTraceErr(err);
19622+ return err;
19623+}
19624+
b752ccd1
AM
19625+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19626+ unsigned long arg)
19627+{
19628+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19629+}
19630+#endif
7f207e10 19631diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
eca34b5c
AM
19632--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19633+++ linux/fs/aufs/i_op_add.c 2019-07-11 15:42:14.468904634 +0200
eca801bf 19634@@ -0,0 +1,936 @@
cd7a4cd9 19635+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19636+/*
ba1aed25 19637+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
19638+ *
19639+ * This program, aufs is free software; you can redistribute it and/or modify
19640+ * it under the terms of the GNU General Public License as published by
19641+ * the Free Software Foundation; either version 2 of the License, or
19642+ * (at your option) any later version.
19643+ *
19644+ * This program is distributed in the hope that it will be useful,
19645+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19646+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19647+ * GNU General Public License for more details.
19648+ *
19649+ * You should have received a copy of the GNU General Public License
523b37e3 19650+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19651+ */
19652+
19653+/*
19654+ * inode operations (add entry)
19655+ */
19656+
eca801bf 19657+#include <linux/iversion.h>
4a4d8108
AM
19658+#include "aufs.h"
19659+
19660+/*
19661+ * final procedure of adding a new entry, except link(2).
19662+ * remove whiteout, instantiate, copyup the parent dir's times and size
19663+ * and update version.
19664+ * if it failed, re-create the removed whiteout.
19665+ */
19666+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19667+ struct dentry *wh_dentry, struct dentry *dentry)
19668+{
19669+ int err, rerr;
19670+ aufs_bindex_t bwh;
19671+ struct path h_path;
076b876e 19672+ struct super_block *sb;
4a4d8108
AM
19673+ struct inode *inode, *h_dir;
19674+ struct dentry *wh;
19675+
19676+ bwh = -1;
076b876e 19677+ sb = dir->i_sb;
4a4d8108 19678+ if (wh_dentry) {
5527c038 19679+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19680+ IMustLock(h_dir);
19681+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19682+ bwh = au_dbwh(dentry);
19683+ h_path.dentry = wh_dentry;
076b876e 19684+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19685+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19686+ dentry);
19687+ if (unlikely(err))
19688+ goto out;
19689+ }
19690+
19691+ inode = au_new_inode(dentry, /*must_new*/1);
19692+ if (!IS_ERR(inode)) {
19693+ d_instantiate(dentry, inode);
5527c038 19694+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19695+ IMustLock(dir);
b912730e 19696+ au_dir_ts(dir, bindex);
be118d29 19697+ inode_inc_iversion(dir);
076b876e 19698+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19699+ return 0; /* success */
19700+ }
19701+
19702+ err = PTR_ERR(inode);
19703+ if (!wh_dentry)
19704+ goto out;
19705+
19706+ /* revert */
19707+ /* dir inode is locked */
19708+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19709+ rerr = PTR_ERR(wh);
19710+ if (IS_ERR(wh)) {
523b37e3
AM
19711+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19712+ dentry, err, rerr);
4a4d8108
AM
19713+ err = -EIO;
19714+ } else
19715+ dput(wh);
19716+
4f0767ce 19717+out:
4a4d8108
AM
19718+ return err;
19719+}
19720+
027c5e7a
AM
19721+static int au_d_may_add(struct dentry *dentry)
19722+{
19723+ int err;
19724+
19725+ err = 0;
19726+ if (unlikely(d_unhashed(dentry)))
19727+ err = -ENOENT;
5527c038 19728+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19729+ err = -EEXIST;
19730+ return err;
19731+}
19732+
4a4d8108
AM
19733+/*
19734+ * simple tests for the adding inode operations.
19735+ * following the checks in vfs, plus the parent-child relationship.
19736+ */
19737+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19738+ struct dentry *h_parent, int isdir)
19739+{
19740+ int err;
19741+ umode_t h_mode;
19742+ struct dentry *h_dentry;
19743+ struct inode *h_inode;
19744+
19745+ err = -ENAMETOOLONG;
19746+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19747+ goto out;
19748+
19749+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19750+ if (d_really_is_negative(dentry)) {
4a4d8108 19751+ err = -EEXIST;
5527c038 19752+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19753+ goto out;
19754+ } else {
19755+ /* rename(2) case */
19756+ err = -EIO;
5527c038
JR
19757+ if (unlikely(d_is_negative(h_dentry)))
19758+ goto out;
19759+ h_inode = d_inode(h_dentry);
19760+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19761+ goto out;
19762+
19763+ h_mode = h_inode->i_mode;
19764+ if (!isdir) {
19765+ err = -EISDIR;
19766+ if (unlikely(S_ISDIR(h_mode)))
19767+ goto out;
19768+ } else if (unlikely(!S_ISDIR(h_mode))) {
19769+ err = -ENOTDIR;
19770+ goto out;
19771+ }
19772+ }
19773+
19774+ err = 0;
19775+ /* expected parent dir is locked */
19776+ if (unlikely(h_parent != h_dentry->d_parent))
19777+ err = -EIO;
19778+
4f0767ce 19779+out:
4a4d8108
AM
19780+ AuTraceErr(err);
19781+ return err;
19782+}
19783+
19784+/*
19785+ * initial procedure of adding a new entry.
19786+ * prepare writable branch and the parent dir, lock it,
19787+ * and lookup whiteout for the new entry.
19788+ */
19789+static struct dentry*
19790+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19791+ struct dentry *src_dentry, struct au_pin *pin,
19792+ struct au_wr_dir_args *wr_dir_args)
19793+{
19794+ struct dentry *wh_dentry, *h_parent;
19795+ struct super_block *sb;
19796+ struct au_branch *br;
19797+ int err;
19798+ unsigned int udba;
19799+ aufs_bindex_t bcpup;
19800+
523b37e3 19801+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19802+
19803+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19804+ bcpup = err;
19805+ wh_dentry = ERR_PTR(err);
19806+ if (unlikely(err < 0))
19807+ goto out;
19808+
19809+ sb = dentry->d_sb;
19810+ udba = au_opt_udba(sb);
19811+ err = au_pin(pin, dentry, bcpup, udba,
19812+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19813+ wh_dentry = ERR_PTR(err);
19814+ if (unlikely(err))
19815+ goto out;
19816+
19817+ h_parent = au_pinned_h_parent(pin);
19818+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19819+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19820+ err = au_may_add(dentry, bcpup, h_parent,
19821+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19822+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19823+ err = -ENAMETOOLONG;
19824+ wh_dentry = ERR_PTR(err);
19825+ if (unlikely(err))
19826+ goto out_unpin;
19827+
19828+ br = au_sbr(sb, bcpup);
19829+ if (dt) {
19830+ struct path tmp = {
19831+ .dentry = h_parent,
86dc4139 19832+ .mnt = au_br_mnt(br)
4a4d8108
AM
19833+ };
19834+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19835+ }
19836+
19837+ wh_dentry = NULL;
19838+ if (bcpup != au_dbwh(dentry))
19839+ goto out; /* success */
19840+
2000de60
JR
19841+ /*
19842+ * ENAMETOOLONG here means that if we allowed create such name, then it
19843+ * would not be able to removed in the future. So we don't allow such
19844+ * name here and we don't handle ENAMETOOLONG differently here.
19845+ */
4a4d8108
AM
19846+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19847+
4f0767ce 19848+out_unpin:
4a4d8108
AM
19849+ if (IS_ERR(wh_dentry))
19850+ au_unpin(pin);
4f0767ce 19851+out:
4a4d8108
AM
19852+ return wh_dentry;
19853+}
19854+
19855+/* ---------------------------------------------------------------------- */
19856+
19857+enum { Mknod, Symlink, Creat };
19858+struct simple_arg {
19859+ int type;
19860+ union {
19861+ struct {
b912730e
AM
19862+ umode_t mode;
19863+ bool want_excl;
19864+ bool try_aopen;
19865+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19866+ } c;
19867+ struct {
19868+ const char *symname;
19869+ } s;
19870+ struct {
7eafdf33 19871+ umode_t mode;
4a4d8108
AM
19872+ dev_t dev;
19873+ } m;
19874+ } u;
19875+};
19876+
19877+static int add_simple(struct inode *dir, struct dentry *dentry,
19878+ struct simple_arg *arg)
19879+{
076b876e 19880+ int err, rerr;
5afbbe0d 19881+ aufs_bindex_t btop;
4a4d8108 19882+ unsigned char created;
b912730e
AM
19883+ const unsigned char try_aopen
19884+ = (arg->type == Creat && arg->u.c.try_aopen);
acd2b654 19885+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
4a4d8108
AM
19886+ struct dentry *wh_dentry, *parent;
19887+ struct inode *h_dir;
b912730e
AM
19888+ struct super_block *sb;
19889+ struct au_branch *br;
acd2b654 19890+ /* to reduce stack size */
c2b27bf2
AM
19891+ struct {
19892+ struct au_dtime dt;
19893+ struct au_pin pin;
19894+ struct path h_path;
19895+ struct au_wr_dir_args wr_dir_args;
19896+ } *a;
4a4d8108 19897+
523b37e3 19898+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19899+ IMustLock(dir);
19900+
c2b27bf2
AM
19901+ err = -ENOMEM;
19902+ a = kmalloc(sizeof(*a), GFP_NOFS);
19903+ if (unlikely(!a))
19904+ goto out;
19905+ a->wr_dir_args.force_btgt = -1;
19906+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19907+
4a4d8108 19908+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
19909+ if (!try_aopen) {
19910+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19911+ if (unlikely(err))
19912+ goto out_free;
19913+ }
027c5e7a
AM
19914+ err = au_d_may_add(dentry);
19915+ if (unlikely(err))
19916+ goto out_unlock;
b912730e
AM
19917+ if (!try_aopen)
19918+ di_write_lock_parent(parent);
c2b27bf2
AM
19919+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19920+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
19921+ err = PTR_ERR(wh_dentry);
19922+ if (IS_ERR(wh_dentry))
027c5e7a 19923+ goto out_parent;
4a4d8108 19924+
5afbbe0d 19925+ btop = au_dbtop(dentry);
b912730e 19926+ sb = dentry->d_sb;
5afbbe0d
AM
19927+ br = au_sbr(sb, btop);
19928+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 19929+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 19930+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
19931+ switch (arg->type) {
19932+ case Creat:
acd2b654 19933+ if (!try_aopen || !h_dir->i_op->atomic_open) {
b912730e
AM
19934+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19935+ arg->u.c.want_excl);
acd2b654
AM
19936+ created = !err;
19937+ if (!err && try_aopen)
19938+ aopen->file->f_mode |= FMODE_CREATED;
19939+ } else {
19940+ aopen->br = br;
19941+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19942+ AuDbg("err %d\n", err);
19943+ AuDbgFile(aopen->file);
19944+ created = err >= 0
19945+ && !!(aopen->file->f_mode & FMODE_CREATED);
19946+ }
4a4d8108
AM
19947+ break;
19948+ case Symlink:
c2b27bf2 19949+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
acd2b654 19950+ created = !err;
4a4d8108
AM
19951+ break;
19952+ case Mknod:
c2b27bf2
AM
19953+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19954+ arg->u.m.dev);
acd2b654 19955+ created = !err;
4a4d8108
AM
19956+ break;
19957+ default:
19958+ BUG();
19959+ }
acd2b654
AM
19960+ if (unlikely(err < 0))
19961+ goto out_unpin;
19962+
19963+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108 19964+ if (!err)
acd2b654 19965+ goto out_unpin; /* success */
4a4d8108
AM
19966+
19967+ /* revert */
acd2b654 19968+ if (created /* && d_is_positive(a->h_path.dentry) */) {
523b37e3
AM
19969+ /* no delegation since it is just created */
19970+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19971+ /*force*/0);
4a4d8108 19972+ if (rerr) {
523b37e3
AM
19973+ AuIOErr("%pd revert failure(%d, %d)\n",
19974+ dentry, err, rerr);
4a4d8108
AM
19975+ err = -EIO;
19976+ }
c2b27bf2 19977+ au_dtime_revert(&a->dt);
4a4d8108 19978+ }
acd2b654
AM
19979+ if (try_aopen && h_dir->i_op->atomic_open
19980+ && (aopen->file->f_mode & FMODE_OPENED))
19981+ /* aopen->file is still opened */
19982+ au_lcnt_dec(&aopen->br->br_nfiles);
4a4d8108 19983+
acd2b654 19984+out_unpin:
c2b27bf2 19985+ au_unpin(&a->pin);
4a4d8108 19986+ dput(wh_dentry);
027c5e7a 19987+out_parent:
b912730e
AM
19988+ if (!try_aopen)
19989+ di_write_unlock(parent);
027c5e7a 19990+out_unlock:
4a4d8108 19991+ if (unlikely(err)) {
5afbbe0d 19992+ au_update_dbtop(dentry);
4a4d8108
AM
19993+ d_drop(dentry);
19994+ }
b912730e
AM
19995+ if (!try_aopen)
19996+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 19997+out_free:
9f237c51 19998+ au_kfree_rcu(a);
027c5e7a 19999+out:
4a4d8108
AM
20000+ return err;
20001+}
20002+
7eafdf33
AM
20003+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
20004+ dev_t dev)
4a4d8108
AM
20005+{
20006+ struct simple_arg arg = {
20007+ .type = Mknod,
20008+ .u.m = {
20009+ .mode = mode,
20010+ .dev = dev
20011+ }
20012+ };
20013+ return add_simple(dir, dentry, &arg);
20014+}
20015+
20016+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20017+{
20018+ struct simple_arg arg = {
20019+ .type = Symlink,
20020+ .u.s.symname = symname
20021+ };
20022+ return add_simple(dir, dentry, &arg);
20023+}
20024+
7eafdf33 20025+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 20026+ bool want_excl)
4a4d8108
AM
20027+{
20028+ struct simple_arg arg = {
20029+ .type = Creat,
20030+ .u.c = {
b4510431
AM
20031+ .mode = mode,
20032+ .want_excl = want_excl
4a4d8108
AM
20033+ }
20034+ };
20035+ return add_simple(dir, dentry, &arg);
20036+}
20037+
b912730e
AM
20038+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20039+ struct vfsub_aopen_args *aopen_args)
20040+{
20041+ struct simple_arg arg = {
20042+ .type = Creat,
20043+ .u.c = {
20044+ .mode = aopen_args->create_mode,
20045+ .want_excl = aopen_args->open_flag & O_EXCL,
20046+ .try_aopen = true,
20047+ .aopen = aopen_args
20048+ }
20049+ };
20050+ return add_simple(dir, dentry, &arg);
20051+}
20052+
38d290e6
JR
20053+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20054+{
20055+ int err;
20056+ aufs_bindex_t bindex;
20057+ struct super_block *sb;
20058+ struct dentry *parent, *h_parent, *h_dentry;
20059+ struct inode *h_dir, *inode;
20060+ struct vfsmount *h_mnt;
20061+ struct au_wr_dir_args wr_dir_args = {
20062+ .force_btgt = -1,
20063+ .flags = AuWrDir_TMPFILE
20064+ };
20065+
20066+ /* copy-up may happen */
febd17d6 20067+ inode_lock(dir);
38d290e6
JR
20068+
20069+ sb = dir->i_sb;
20070+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20071+ if (unlikely(err))
20072+ goto out;
20073+
20074+ err = au_di_init(dentry);
20075+ if (unlikely(err))
20076+ goto out_si;
20077+
20078+ err = -EBUSY;
20079+ parent = d_find_any_alias(dir);
20080+ AuDebugOn(!parent);
20081+ di_write_lock_parent(parent);
5527c038 20082+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
20083+ goto out_parent;
20084+
20085+ err = au_digen_test(parent, au_sigen(sb));
20086+ if (unlikely(err))
20087+ goto out_parent;
20088+
5afbbe0d
AM
20089+ bindex = au_dbtop(parent);
20090+ au_set_dbtop(dentry, bindex);
20091+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20092+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20093+ bindex = err;
20094+ if (unlikely(err < 0))
20095+ goto out_parent;
20096+
20097+ err = -EOPNOTSUPP;
20098+ h_dir = au_h_iptr(dir, bindex);
20099+ if (unlikely(!h_dir->i_op->tmpfile))
20100+ goto out_parent;
20101+
20102+ h_mnt = au_sbr_mnt(sb, bindex);
20103+ err = vfsub_mnt_want_write(h_mnt);
20104+ if (unlikely(err))
20105+ goto out_parent;
20106+
20107+ h_parent = au_h_dptr(parent, bindex);
521ced18
JR
20108+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20109+ if (IS_ERR(h_dentry)) {
20110+ err = PTR_ERR(h_dentry);
38d290e6 20111+ goto out_mnt;
521ced18 20112+ }
38d290e6 20113+
5afbbe0d
AM
20114+ au_set_dbtop(dentry, bindex);
20115+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20116+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20117+ inode = au_new_inode(dentry, /*must_new*/1);
20118+ if (IS_ERR(inode)) {
20119+ err = PTR_ERR(inode);
20120+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20121+ au_set_dbtop(dentry, -1);
20122+ au_set_dbbot(dentry, -1);
38d290e6
JR
20123+ } else {
20124+ if (!inode->i_nlink)
20125+ set_nlink(inode, 1);
20126+ d_tmpfile(dentry, inode);
20127+ au_di(dentry)->di_tmpfile = 1;
20128+
20129+ /* update without i_mutex */
5afbbe0d 20130+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20131+ au_cpup_attr_timesizes(dir);
20132+ }
38d290e6 20133+ dput(h_dentry);
521ced18 20134+
38d290e6
JR
20135+out_mnt:
20136+ vfsub_mnt_drop_write(h_mnt);
20137+out_parent:
20138+ di_write_unlock(parent);
20139+ dput(parent);
20140+ di_write_unlock(dentry);
5afbbe0d 20141+ if (unlikely(err)) {
38d290e6
JR
20142+ au_di_fin(dentry);
20143+ dentry->d_fsdata = NULL;
20144+ }
20145+out_si:
20146+ si_read_unlock(sb);
20147+out:
febd17d6 20148+ inode_unlock(dir);
38d290e6
JR
20149+ return err;
20150+}
20151+
4a4d8108
AM
20152+/* ---------------------------------------------------------------------- */
20153+
20154+struct au_link_args {
20155+ aufs_bindex_t bdst, bsrc;
20156+ struct au_pin pin;
20157+ struct path h_path;
20158+ struct dentry *src_parent, *parent;
20159+};
20160+
20161+static int au_cpup_before_link(struct dentry *src_dentry,
20162+ struct au_link_args *a)
20163+{
20164+ int err;
20165+ struct dentry *h_src_dentry;
c2b27bf2
AM
20166+ struct au_cp_generic cpg = {
20167+ .dentry = src_dentry,
20168+ .bdst = a->bdst,
20169+ .bsrc = a->bsrc,
20170+ .len = -1,
20171+ .pin = &a->pin,
20172+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20173+ };
4a4d8108
AM
20174+
20175+ di_read_lock_parent(a->src_parent, AuLock_IR);
20176+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20177+ if (unlikely(err))
20178+ goto out;
20179+
20180+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20181+ err = au_pin(&a->pin, src_dentry, a->bdst,
20182+ au_opt_udba(src_dentry->d_sb),
20183+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20184+ if (unlikely(err))
20185+ goto out;
367653fa 20186+
c2b27bf2 20187+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20188+ au_unpin(&a->pin);
20189+
4f0767ce 20190+out:
4a4d8108
AM
20191+ di_read_unlock(a->src_parent, AuLock_IR);
20192+ return err;
20193+}
20194+
86dc4139
AM
20195+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20196+ struct au_link_args *a)
4a4d8108
AM
20197+{
20198+ int err;
20199+ unsigned char plink;
5afbbe0d 20200+ aufs_bindex_t bbot;
4a4d8108 20201+ struct dentry *h_src_dentry;
523b37e3 20202+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20203+ struct super_block *sb;
20204+ struct file *h_file;
20205+
20206+ plink = 0;
20207+ h_inode = NULL;
20208+ sb = src_dentry->d_sb;
5527c038 20209+ inode = d_inode(src_dentry);
5afbbe0d 20210+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20211+ h_inode = au_h_iptr(inode, a->bdst);
20212+ if (!h_inode || !h_inode->i_nlink) {
20213+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20214+ bbot = au_dbbot(dentry);
20215+ if (bbot < a->bsrc)
20216+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20217+ au_set_h_dptr(dentry, a->bsrc,
20218+ dget(au_h_dptr(src_dentry, a->bsrc)));
20219+ dget(a->h_path.dentry);
20220+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20221+ AuDbg("temporary d_inode...\n");
20222+ spin_lock(&dentry->d_lock);
5527c038 20223+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20224+ spin_unlock(&dentry->d_lock);
392086de 20225+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20226+ if (IS_ERR(h_file))
4a4d8108 20227+ err = PTR_ERR(h_file);
86dc4139 20228+ else {
c2b27bf2
AM
20229+ struct au_cp_generic cpg = {
20230+ .dentry = dentry,
20231+ .bdst = a->bdst,
20232+ .bsrc = -1,
20233+ .len = -1,
20234+ .pin = &a->pin,
20235+ .flags = AuCpup_KEEPLINO
20236+ };
20237+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20238+ au_h_open_post(dentry, a->bsrc, h_file);
20239+ if (!err) {
20240+ dput(a->h_path.dentry);
20241+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20242+ } else
20243+ au_set_h_dptr(dentry, a->bdst,
20244+ a->h_path.dentry);
20245+ }
c1595e42 20246+ spin_lock(&dentry->d_lock);
86dc4139 20247+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20248+ spin_unlock(&dentry->d_lock);
20249+ AuDbg("temporary d_inode...done\n");
86dc4139 20250+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20251+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20252+ } else {
20253+ /* the inode of src_dentry already exists on a.bdst branch */
20254+ h_src_dentry = d_find_alias(h_inode);
20255+ if (!h_src_dentry && au_plink_test(inode)) {
20256+ plink = 1;
20257+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20258+ err = PTR_ERR(h_src_dentry);
20259+ if (IS_ERR(h_src_dentry))
20260+ goto out;
20261+
5527c038 20262+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20263+ dput(h_src_dentry);
20264+ h_src_dentry = NULL;
20265+ }
20266+
20267+ }
20268+ if (h_src_dentry) {
523b37e3 20269+ delegated = NULL;
4a4d8108 20270+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20271+ &a->h_path, &delegated);
20272+ if (unlikely(err == -EWOULDBLOCK)) {
20273+ pr_warn("cannot retry for NFSv4 delegation"
20274+ " for an internal link\n");
20275+ iput(delegated);
20276+ }
4a4d8108
AM
20277+ dput(h_src_dentry);
20278+ } else {
20279+ AuIOErr("no dentry found for hi%lu on b%d\n",
20280+ h_inode->i_ino, a->bdst);
20281+ err = -EIO;
20282+ }
20283+ }
20284+
20285+ if (!err && !plink)
20286+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20287+
20288+out:
2cbb1c4b 20289+ AuTraceErr(err);
4a4d8108
AM
20290+ return err;
20291+}
20292+
20293+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20294+ struct dentry *dentry)
20295+{
20296+ int err, rerr;
20297+ struct au_dtime dt;
20298+ struct au_link_args *a;
20299+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20300+ struct inode *inode, *delegated;
4a4d8108
AM
20301+ struct super_block *sb;
20302+ struct au_wr_dir_args wr_dir_args = {
20303+ /* .force_btgt = -1, */
20304+ .flags = AuWrDir_ADD_ENTRY
20305+ };
20306+
20307+ IMustLock(dir);
5527c038 20308+ inode = d_inode(src_dentry);
4a4d8108
AM
20309+ IMustLock(inode);
20310+
4a4d8108
AM
20311+ err = -ENOMEM;
20312+ a = kzalloc(sizeof(*a), GFP_NOFS);
20313+ if (unlikely(!a))
20314+ goto out;
20315+
20316+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20317+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20318+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20319+ if (unlikely(err))
20320+ goto out_kfree;
38d290e6 20321+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20322+ if (unlikely(err))
20323+ goto out_unlock;
20324+ err = au_d_may_add(dentry);
20325+ if (unlikely(err))
20326+ goto out_unlock;
e49829fe 20327+
4a4d8108 20328+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20329+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20330+
20331+ di_write_lock_parent(a->parent);
20332+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20333+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20334+ &wr_dir_args);
20335+ err = PTR_ERR(wh_dentry);
20336+ if (IS_ERR(wh_dentry))
027c5e7a 20337+ goto out_parent;
4a4d8108
AM
20338+
20339+ err = 0;
20340+ sb = dentry->d_sb;
5afbbe0d 20341+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20342+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20343+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20344+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20345+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20346+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20347+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20348+ if (!h_src_dentry) {
5afbbe0d 20349+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20350+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20351+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20352+ } else if (IS_ERR(h_src_dentry)) {
20353+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20354+ goto out_parent;
38d290e6 20355+ }
2cbb1c4b 20356+
f2c43d5f
AM
20357+ /*
20358+ * aufs doesn't touch the credential so
acd2b654 20359+ * security_dentry_create_files_as() is unnecessary.
f2c43d5f 20360+ */
4a4d8108
AM
20361+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20362+ if (a->bdst < a->bsrc
20363+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20364+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20365+ else {
20366+ delegated = NULL;
4a4d8108 20367+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20368+ &a->h_path, &delegated);
20369+ if (unlikely(err == -EWOULDBLOCK)) {
20370+ pr_warn("cannot retry for NFSv4 delegation"
20371+ " for an internal link\n");
20372+ iput(delegated);
20373+ }
20374+ }
2cbb1c4b 20375+ dput(h_src_dentry);
4a4d8108
AM
20376+ } else {
20377+ /*
20378+ * copyup src_dentry to the branch we process,
20379+ * and then link(2) to it.
20380+ */
2cbb1c4b 20381+ dput(h_src_dentry);
4a4d8108
AM
20382+ if (a->bdst < a->bsrc
20383+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20384+ au_unpin(&a->pin);
20385+ di_write_unlock(a->parent);
20386+ err = au_cpup_before_link(src_dentry, a);
20387+ di_write_lock_parent(a->parent);
20388+ if (!err)
20389+ err = au_pin(&a->pin, dentry, a->bdst,
20390+ au_opt_udba(sb),
20391+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20392+ if (unlikely(err))
20393+ goto out_wh;
20394+ }
20395+ if (!err) {
20396+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20397+ err = -ENOENT;
5527c038 20398+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20399+ delegated = NULL;
4a4d8108
AM
20400+ err = vfsub_link(h_src_dentry,
20401+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20402+ &a->h_path, &delegated);
20403+ if (unlikely(err == -EWOULDBLOCK)) {
20404+ pr_warn("cannot retry"
20405+ " for NFSv4 delegation"
20406+ " for an internal link\n");
20407+ iput(delegated);
20408+ }
20409+ }
4a4d8108
AM
20410+ }
20411+ }
20412+ if (unlikely(err))
20413+ goto out_unpin;
20414+
20415+ if (wh_dentry) {
20416+ a->h_path.dentry = wh_dentry;
20417+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20418+ dentry);
20419+ if (unlikely(err))
20420+ goto out_revert;
20421+ }
20422+
b912730e 20423+ au_dir_ts(dir, a->bdst);
be118d29 20424+ inode_inc_iversion(dir);
4a4d8108
AM
20425+ inc_nlink(inode);
20426+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20427+ d_instantiate(dentry, au_igrab(inode));
20428+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20429+ /* some filesystem calls d_drop() */
20430+ d_drop(dentry);
076b876e
AM
20431+ /* some filesystems consume an inode even hardlink */
20432+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20433+ goto out_unpin; /* success */
20434+
4f0767ce 20435+out_revert:
523b37e3
AM
20436+ /* no delegation since it is just created */
20437+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20438+ /*delegated*/NULL, /*force*/0);
027c5e7a 20439+ if (unlikely(rerr)) {
523b37e3 20440+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20441+ err = -EIO;
20442+ }
4a4d8108 20443+ au_dtime_revert(&dt);
4f0767ce 20444+out_unpin:
4a4d8108 20445+ au_unpin(&a->pin);
4f0767ce 20446+out_wh:
4a4d8108 20447+ dput(wh_dentry);
027c5e7a
AM
20448+out_parent:
20449+ di_write_unlock(a->parent);
20450+ dput(a->src_parent);
4f0767ce 20451+out_unlock:
4a4d8108 20452+ if (unlikely(err)) {
5afbbe0d 20453+ au_update_dbtop(dentry);
4a4d8108
AM
20454+ d_drop(dentry);
20455+ }
4a4d8108 20456+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20457+out_kfree:
9f237c51 20458+ au_kfree_rcu(a);
4f0767ce 20459+out:
86dc4139 20460+ AuTraceErr(err);
4a4d8108
AM
20461+ return err;
20462+}
20463+
7eafdf33 20464+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
20465+{
20466+ int err, rerr;
20467+ aufs_bindex_t bindex;
20468+ unsigned char diropq;
20469+ struct path h_path;
20470+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20471+ struct inode *h_inode;
4a4d8108
AM
20472+ struct super_block *sb;
20473+ struct {
20474+ struct au_pin pin;
20475+ struct au_dtime dt;
20476+ } *a; /* reduce the stack usage */
20477+ struct au_wr_dir_args wr_dir_args = {
20478+ .force_btgt = -1,
20479+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20480+ };
20481+
20482+ IMustLock(dir);
20483+
20484+ err = -ENOMEM;
20485+ a = kmalloc(sizeof(*a), GFP_NOFS);
20486+ if (unlikely(!a))
20487+ goto out;
20488+
027c5e7a
AM
20489+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20490+ if (unlikely(err))
20491+ goto out_free;
20492+ err = au_d_may_add(dentry);
20493+ if (unlikely(err))
20494+ goto out_unlock;
20495+
4a4d8108
AM
20496+ parent = dentry->d_parent; /* dir inode is locked */
20497+ di_write_lock_parent(parent);
20498+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20499+ &a->pin, &wr_dir_args);
20500+ err = PTR_ERR(wh_dentry);
20501+ if (IS_ERR(wh_dentry))
027c5e7a 20502+ goto out_parent;
4a4d8108
AM
20503+
20504+ sb = dentry->d_sb;
5afbbe0d 20505+ bindex = au_dbtop(dentry);
4a4d8108
AM
20506+ h_path.dentry = au_h_dptr(dentry, bindex);
20507+ h_path.mnt = au_sbr_mnt(sb, bindex);
20508+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20509+ if (unlikely(err))
027c5e7a 20510+ goto out_unpin;
4a4d8108
AM
20511+
20512+ /* make the dir opaque */
20513+ diropq = 0;
febd17d6 20514+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20515+ if (wh_dentry
20516+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20517+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20518+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20519+ inode_unlock(h_inode);
4a4d8108
AM
20520+ err = PTR_ERR(opq_dentry);
20521+ if (IS_ERR(opq_dentry))
20522+ goto out_dir;
20523+ dput(opq_dentry);
20524+ diropq = 1;
20525+ }
20526+
20527+ err = epilog(dir, bindex, wh_dentry, dentry);
20528+ if (!err) {
20529+ inc_nlink(dir);
027c5e7a 20530+ goto out_unpin; /* success */
4a4d8108
AM
20531+ }
20532+
20533+ /* revert */
20534+ if (diropq) {
20535+ AuLabel(revert opq);
febd17d6 20536+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20537+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20538+ inode_unlock(h_inode);
4a4d8108 20539+ if (rerr) {
523b37e3
AM
20540+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20541+ dentry, err, rerr);
4a4d8108
AM
20542+ err = -EIO;
20543+ }
20544+ }
20545+
4f0767ce 20546+out_dir:
4a4d8108
AM
20547+ AuLabel(revert dir);
20548+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20549+ if (rerr) {
523b37e3
AM
20550+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20551+ dentry, err, rerr);
4a4d8108
AM
20552+ err = -EIO;
20553+ }
4a4d8108 20554+ au_dtime_revert(&a->dt);
027c5e7a 20555+out_unpin:
4a4d8108
AM
20556+ au_unpin(&a->pin);
20557+ dput(wh_dentry);
027c5e7a
AM
20558+out_parent:
20559+ di_write_unlock(parent);
20560+out_unlock:
4a4d8108 20561+ if (unlikely(err)) {
5afbbe0d 20562+ au_update_dbtop(dentry);
4a4d8108
AM
20563+ d_drop(dentry);
20564+ }
4a4d8108 20565+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20566+out_free:
9f237c51 20567+ au_kfree_rcu(a);
4f0767ce 20568+out:
4a4d8108
AM
20569+ return err;
20570+}
7f207e10 20571diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
eca34b5c
AM
20572--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
20573+++ linux/fs/aufs/i_op.c 2019-09-16 09:38:43.216175640 +0200
20574@@ -0,0 +1,1505 @@
cd7a4cd9 20575+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20576+/*
ba1aed25 20577+ * Copyright (C) 2005-2019 Junjiro R. Okajima
4a4d8108
AM
20578+ *
20579+ * This program, aufs is free software; you can redistribute it and/or modify
20580+ * it under the terms of the GNU General Public License as published by
20581+ * the Free Software Foundation; either version 2 of the License, or
20582+ * (at your option) any later version.
20583+ *
20584+ * This program is distributed in the hope that it will be useful,
20585+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20586+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20587+ * GNU General Public License for more details.
20588+ *
20589+ * You should have received a copy of the GNU General Public License
523b37e3 20590+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20591+ */
1facf9fc 20592+
1308ab2a 20593+/*
4a4d8108 20594+ * inode operations (except add/del/rename)
1308ab2a 20595+ */
4a4d8108
AM
20596+
20597+#include <linux/device_cgroup.h>
20598+#include <linux/fs_stack.h>
eca801bf 20599+#include <linux/iversion.h>
4a4d8108
AM
20600+#include <linux/namei.h>
20601+#include <linux/security.h>
4a4d8108
AM
20602+#include "aufs.h"
20603+
1e00d052 20604+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20605+ struct path *h_path, int brperm)
1facf9fc 20606+{
1308ab2a 20607+ int err;
4a4d8108 20608+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 20609+
e2f27e51
AM
20610+ err = -EPERM;
20611+ if (write_mask && IS_IMMUTABLE(h_inode))
20612+ goto out;
20613+
4a4d8108 20614+ err = -EACCES;
e2f27e51
AM
20615+ if (((mask & MAY_EXEC)
20616+ && S_ISREG(h_inode->i_mode)
20617+ && (path_noexec(h_path)
cd7a4cd9 20618+ || !(h_inode->i_mode & 0111))))
4a4d8108
AM
20619+ goto out;
20620+
20621+ /*
20622+ * - skip the lower fs test in the case of write to ro branch.
20623+ * - nfs dir permission write check is optimized, but a policy for
20624+ * link/rename requires a real check.
2121bcd9 20625+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 20626+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
20627+ */
20628+ if ((write_mask && !au_br_writable(brperm))
20629+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20630+ && write_mask && !(mask & MAY_READ))
20631+ || !h_inode->i_op->permission) {
20632+ /* AuLabel(generic_permission); */
062440b3 20633+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
1e00d052 20634+ err = generic_permission(h_inode, mask);
b912730e
AM
20635+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20636+ err = h_inode->i_op->permission(h_inode, mask);
20637+ AuTraceErr(err);
1308ab2a 20638+ } else {
4a4d8108 20639+ /* AuLabel(h_inode->permission); */
1e00d052 20640+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
20641+ AuTraceErr(err);
20642+ }
1facf9fc 20643+
4a4d8108
AM
20644+ if (!err)
20645+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20646+ if (!err)
4a4d8108 20647+ err = security_inode_permission(h_inode, mask);
4a4d8108
AM
20648+
20649+#if 0
20650+ if (!err) {
20651+ /* todo: do we need to call ima_path_check()? */
20652+ struct path h_path = {
20653+ .dentry =
20654+ .mnt = h_mnt
20655+ };
20656+ err = ima_path_check(&h_path,
20657+ mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20658+ IMA_COUNT_LEAVE);
1308ab2a 20659+ }
4a4d8108 20660+#endif
dece6358 20661+
4f0767ce 20662+out:
1308ab2a 20663+ return err;
20664+}
dece6358 20665+
1e00d052 20666+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 20667+{
20668+ int err;
5afbbe0d 20669+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20670+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20671+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20672+ struct inode *h_inode;
20673+ struct super_block *sb;
20674+ struct au_branch *br;
1facf9fc 20675+
027c5e7a 20676+ /* todo: support rcu-walk? */
1e00d052 20677+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20678+ return -ECHILD;
20679+
4a4d8108
AM
20680+ sb = inode->i_sb;
20681+ si_read_lock(sb, AuLock_FLUSH);
20682+ ii_read_lock_child(inode);
027c5e7a
AM
20683+#if 0
20684+ err = au_iigen_test(inode, au_sigen(sb));
20685+ if (unlikely(err))
20686+ goto out;
20687+#endif
dece6358 20688+
076b876e
AM
20689+ if (!isdir
20690+ || write_mask
20691+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20692+ err = au_busy_or_stale();
5afbbe0d 20693+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20694+ if (unlikely(!h_inode
20695+ || (h_inode->i_mode & S_IFMT)
20696+ != (inode->i_mode & S_IFMT)))
20697+ goto out;
1facf9fc 20698+
4a4d8108 20699+ err = 0;
5afbbe0d 20700+ bindex = au_ibtop(inode);
4a4d8108 20701+ br = au_sbr(sb, bindex);
79b8bda9 20702+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20703+ if (write_mask
20704+ && !err
20705+ && !special_file(h_inode->i_mode)) {
20706+ /* test whether the upper writable branch exists */
20707+ err = -EROFS;
20708+ for (; bindex >= 0; bindex--)
20709+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20710+ err = 0;
20711+ break;
20712+ }
20713+ }
20714+ goto out;
20715+ }
dece6358 20716+
4a4d8108 20717+ /* non-write to dir */
1308ab2a 20718+ err = 0;
5afbbe0d
AM
20719+ bbot = au_ibbot(inode);
20720+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20721+ h_inode = au_h_iptr(inode, bindex);
20722+ if (h_inode) {
20723+ err = au_busy_or_stale();
20724+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20725+ break;
20726+
20727+ br = au_sbr(sb, bindex);
79b8bda9 20728+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20729+ br->br_perm);
20730+ }
20731+ }
1308ab2a 20732+
4f0767ce 20733+out:
4a4d8108
AM
20734+ ii_read_unlock(inode);
20735+ si_read_unlock(sb);
1308ab2a 20736+ return err;
20737+}
20738+
4a4d8108 20739+/* ---------------------------------------------------------------------- */
1facf9fc 20740+
4a4d8108 20741+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20742+ unsigned int flags)
4a4d8108
AM
20743+{
20744+ struct dentry *ret, *parent;
b752ccd1 20745+ struct inode *inode;
4a4d8108 20746+ struct super_block *sb;
1716fcea 20747+ int err, npositive;
dece6358 20748+
4a4d8108 20749+ IMustLock(dir);
1308ab2a 20750+
537831f9
AM
20751+ /* todo: support rcu-walk? */
20752+ ret = ERR_PTR(-ECHILD);
20753+ if (flags & LOOKUP_RCU)
20754+ goto out;
20755+
20756+ ret = ERR_PTR(-ENAMETOOLONG);
20757+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20758+ goto out;
20759+
4a4d8108 20760+ sb = dir->i_sb;
7f207e10
AM
20761+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20762+ ret = ERR_PTR(err);
20763+ if (unlikely(err))
20764+ goto out;
20765+
4a4d8108
AM
20766+ err = au_di_init(dentry);
20767+ ret = ERR_PTR(err);
20768+ if (unlikely(err))
7f207e10 20769+ goto out_si;
1308ab2a 20770+
9dbd164d 20771+ inode = NULL;
027c5e7a 20772+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20773+ parent = dentry->d_parent; /* dir inode is locked */
20774+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20775+ err = au_alive_dir(parent);
20776+ if (!err)
20777+ err = au_digen_test(parent, au_sigen(sb));
20778+ if (!err) {
5afbbe0d
AM
20779+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20780+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20781+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20782+ err = npositive;
20783+ }
4a4d8108 20784+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20785+ ret = ERR_PTR(err);
20786+ if (unlikely(err < 0))
20787+ goto out_unlock;
1308ab2a 20788+
4a4d8108 20789+ if (npositive) {
b752ccd1 20790+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20791+ if (IS_ERR(inode)) {
20792+ ret = (void *)inode;
20793+ inode = NULL;
20794+ goto out_unlock;
20795+ }
9dbd164d 20796+ }
4a4d8108 20797+
c1595e42
JR
20798+ if (inode)
20799+ atomic_inc(&inode->i_count);
4a4d8108 20800+ ret = d_splice_alias(inode, dentry);
537831f9
AM
20801+#if 0
20802+ if (unlikely(d_need_lookup(dentry))) {
20803+ spin_lock(&dentry->d_lock);
20804+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20805+ spin_unlock(&dentry->d_lock);
20806+ } else
20807+#endif
c1595e42 20808+ if (inode) {
2000de60 20809+ if (!IS_ERR(ret)) {
c1595e42 20810+ iput(inode);
2000de60
JR
20811+ if (ret && ret != dentry)
20812+ ii_write_unlock(inode);
20813+ } else {
c1595e42
JR
20814+ ii_write_unlock(inode);
20815+ iput(inode);
20816+ inode = NULL;
20817+ }
7f207e10 20818+ }
1facf9fc 20819+
4f0767ce 20820+out_unlock:
4a4d8108 20821+ di_write_unlock(dentry);
7f207e10 20822+out_si:
4a4d8108 20823+ si_read_unlock(sb);
7f207e10 20824+out:
4a4d8108
AM
20825+ return ret;
20826+}
1facf9fc 20827+
4a4d8108 20828+/* ---------------------------------------------------------------------- */
1facf9fc 20829+
acd2b654
AM
20830+/*
20831+ * very dirty and complicated aufs ->atomic_open().
20832+ * aufs_atomic_open()
20833+ * + au_aopen_or_create()
20834+ * + add_simple()
20835+ * + vfsub_atomic_open()
20836+ * + branch fs ->atomic_open()
20837+ * may call the actual 'open' for h_file
20838+ * + inc br_nfiles only if opened
20839+ * + au_aopen_no_open() or au_aopen_do_open()
20840+ *
20841+ * au_aopen_do_open()
20842+ * + finish_open()
20843+ * + au_do_aopen()
20844+ * + au_do_open() the body of all 'open'
20845+ * + au_do_open_nondir()
20846+ * set the passed h_file
20847+ *
20848+ * au_aopen_no_open()
20849+ * + finish_no_open()
20850+ */
20851+
b912730e 20852+struct aopen_node {
8b6a4947 20853+ struct hlist_bl_node hblist;
b912730e
AM
20854+ struct file *file, *h_file;
20855+};
20856+
20857+static int au_do_aopen(struct inode *inode, struct file *file)
20858+{
8b6a4947
AM
20859+ struct hlist_bl_head *aopen;
20860+ struct hlist_bl_node *pos;
b912730e
AM
20861+ struct aopen_node *node;
20862+ struct au_do_open_args args = {
8b6a4947
AM
20863+ .aopen = 1,
20864+ .open = au_do_open_nondir
b912730e
AM
20865+ };
20866+
20867+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20868+ hlist_bl_lock(aopen);
20869+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20870+ if (node->file == file) {
20871+ args.h_file = node->h_file;
20872+ break;
20873+ }
8b6a4947 20874+ hlist_bl_unlock(aopen);
b912730e
AM
20875+ /* AuDebugOn(!args.h_file); */
20876+
20877+ return au_do_open(file, &args);
20878+}
20879+
acd2b654
AM
20880+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20881+ struct aopen_node *aopen_node)
20882+{
20883+ int err;
20884+ struct hlist_bl_head *aopen;
20885+
20886+ AuLabel(here);
20887+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
20888+ au_hbl_add(&aopen_node->hblist, aopen);
20889+ err = finish_open(file, dentry, au_do_aopen);
20890+ au_hbl_del(&aopen_node->hblist, aopen);
20891+ /* AuDbgFile(file); */
20892+ AuDbg("%pd%s%s\n", dentry,
20893+ (file->f_mode & FMODE_CREATED) ? " created" : "",
20894+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
20895+
20896+ AuTraceErr(err);
20897+ return err;
20898+}
20899+
20900+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20901+{
20902+ int err;
20903+
20904+ AuLabel(here);
20905+ dget(dentry);
20906+ err = finish_no_open(file, dentry);
20907+
20908+ AuTraceErr(err);
20909+ return err;
20910+}
20911+
b912730e
AM
20912+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20913+ struct file *file, unsigned int open_flag,
acd2b654 20914+ umode_t create_mode)
b912730e 20915+{
acd2b654 20916+ int err, did_open;
5afbbe0d 20917+ unsigned int lkup_flags;
acd2b654
AM
20918+ aufs_bindex_t bindex;
20919+ struct super_block *sb;
f0c0a007 20920+ struct dentry *parent, *d;
b912730e
AM
20921+ struct vfsub_aopen_args args = {
20922+ .open_flag = open_flag,
acd2b654 20923+ .create_mode = create_mode
b912730e
AM
20924+ };
20925+ struct aopen_node aopen_node = {
20926+ .file = file
20927+ };
20928+
20929+ IMustLock(dir);
5afbbe0d 20930+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
20931+ AuDbgDentry(dentry);
20932+
20933+ err = 0;
20934+ if (!au_di(dentry)) {
5afbbe0d
AM
20935+ lkup_flags = LOOKUP_OPEN;
20936+ if (open_flag & O_CREAT)
20937+ lkup_flags |= LOOKUP_CREATE;
20938+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
20939+ if (IS_ERR(d)) {
20940+ err = PTR_ERR(d);
5afbbe0d 20941+ AuTraceErr(err);
b912730e
AM
20942+ goto out;
20943+ } else if (d) {
20944+ /*
20945+ * obsoleted dentry found.
20946+ * another error will be returned later.
20947+ */
20948+ d_drop(d);
b912730e 20949+ AuDbgDentry(d);
5afbbe0d 20950+ dput(d);
b912730e
AM
20951+ }
20952+ AuDbgDentry(dentry);
20953+ }
20954+
20955+ if (d_is_positive(dentry)
20956+ || d_unhashed(dentry)
20957+ || d_unlinked(dentry)
acd2b654
AM
20958+ || !(open_flag & O_CREAT)) {
20959+ err = au_aopen_no_open(file, dentry);
20960+ goto out; /* success */
20961+ }
b912730e
AM
20962+
20963+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20964+ if (unlikely(err))
20965+ goto out;
20966+
acd2b654 20967+ sb = dentry->d_sb;
b912730e
AM
20968+ parent = dentry->d_parent; /* dir is locked */
20969+ di_write_lock_parent(parent);
5afbbe0d 20970+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
acd2b654
AM
20971+ if (unlikely(err < 0))
20972+ goto out_parent;
b912730e
AM
20973+
20974+ AuDbgDentry(dentry);
acd2b654
AM
20975+ if (d_is_positive(dentry)) {
20976+ err = au_aopen_no_open(file, dentry);
20977+ goto out_parent; /* success */
20978+ }
b912730e 20979+
acd2b654 20980+ args.file = alloc_empty_file(file->f_flags, current_cred());
b912730e
AM
20981+ err = PTR_ERR(args.file);
20982+ if (IS_ERR(args.file))
acd2b654 20983+ goto out_parent;
b912730e 20984+
acd2b654 20985+ bindex = au_dbtop(dentry);
b912730e
AM
20986+ err = au_aopen_or_create(dir, dentry, &args);
20987+ AuTraceErr(err);
20988+ AuDbgFile(args.file);
acd2b654
AM
20989+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20990+ did_open = !!(args.file->f_mode & FMODE_OPENED);
20991+ if (!did_open) {
20992+ fput(args.file);
20993+ args.file = NULL;
b912730e 20994+ }
8b6a4947
AM
20995+ di_write_unlock(parent);
20996+ di_write_unlock(dentry);
acd2b654
AM
20997+ if (unlikely(err < 0)) {
20998+ if (args.file)
20999+ fput(args.file);
21000+ goto out_sb;
b912730e 21001+ }
b912730e 21002+
acd2b654
AM
21003+ if (!did_open)
21004+ err = au_aopen_no_open(file, dentry);
8b6a4947 21005+ else {
acd2b654
AM
21006+ aopen_node.h_file = args.file;
21007+ err = au_aopen_do_open(file, dentry, &aopen_node);
8b6a4947 21008+ }
acd2b654
AM
21009+ if (unlikely(err < 0)) {
21010+ if (args.file)
21011+ fput(args.file);
21012+ if (did_open)
21013+ au_lcnt_dec(&args.br->br_nfiles);
b912730e 21014+ }
acd2b654
AM
21015+ goto out_sb; /* success */
21016+
21017+out_parent:
21018+ di_write_unlock(parent);
21019+ di_write_unlock(dentry);
21020+out_sb:
21021+ si_read_unlock(sb);
b912730e 21022+out:
b912730e 21023+ AuTraceErr(err);
acd2b654 21024+ AuDbgFile(file);
b912730e
AM
21025+ return err;
21026+}
21027+
21028+
21029+/* ---------------------------------------------------------------------- */
21030+
4a4d8108
AM
21031+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21032+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 21033+ aufs_bindex_t btop)
4a4d8108
AM
21034+{
21035+ int err;
21036+ struct dentry *h_parent;
21037+ struct inode *h_dir;
1facf9fc 21038+
027c5e7a 21039+ if (add_entry)
5527c038 21040+ IMustLock(d_inode(parent));
027c5e7a 21041+ else
4a4d8108
AM
21042+ di_write_lock_parent(parent);
21043+
21044+ err = 0;
21045+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 21046+ if (btop > bcpup)
c2b27bf2 21047+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 21048+ else if (btop < bcpup)
4a4d8108
AM
21049+ err = au_cpdown_dirs(dentry, bcpup);
21050+ else
c2b27bf2 21051+ BUG();
4a4d8108 21052+ }
38d290e6 21053+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 21054+ h_parent = au_h_dptr(parent, bcpup);
5527c038 21055+ h_dir = d_inode(h_parent);
be118d29 21056+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 21057+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 21058+ /* todo: no unlock here */
3c1bdaff 21059+ inode_unlock_shared(h_dir);
027c5e7a
AM
21060+
21061+ AuDbg("bcpup %d\n", bcpup);
21062+ if (!err) {
5527c038 21063+ if (d_really_is_negative(dentry))
5afbbe0d 21064+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
21065+ au_update_dbrange(dentry, /*do_put_zero*/0);
21066+ }
1308ab2a 21067+ }
1facf9fc 21068+
4a4d8108
AM
21069+ if (!add_entry)
21070+ di_write_unlock(parent);
21071+ if (!err)
21072+ err = bcpup; /* success */
1308ab2a 21073+
027c5e7a 21074+ AuTraceErr(err);
4a4d8108
AM
21075+ return err;
21076+}
1facf9fc 21077+
4a4d8108
AM
21078+/*
21079+ * decide the branch and the parent dir where we will create a new entry.
21080+ * returns new bindex or an error.
21081+ * copyup the parent dir if needed.
21082+ */
21083+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21084+ struct au_wr_dir_args *args)
21085+{
21086+ int err;
392086de 21087+ unsigned int flags;
5afbbe0d 21088+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
21089+ const unsigned char add_entry
21090+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 21091+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
21092+ struct super_block *sb;
21093+ struct dentry *parent;
21094+ struct au_sbinfo *sbinfo;
1facf9fc 21095+
4a4d8108
AM
21096+ sb = dentry->d_sb;
21097+ sbinfo = au_sbi(sb);
21098+ parent = dget_parent(dentry);
5afbbe0d
AM
21099+ btop = au_dbtop(dentry);
21100+ bcpup = btop;
4a4d8108
AM
21101+ if (args->force_btgt < 0) {
21102+ if (src_dentry) {
5afbbe0d
AM
21103+ src_btop = au_dbtop(src_dentry);
21104+ if (src_btop < btop)
21105+ bcpup = src_btop;
4a4d8108 21106+ } else if (add_entry) {
392086de
AM
21107+ flags = 0;
21108+ if (au_ftest_wrdir(args->flags, ISDIR))
21109+ au_fset_wbr(flags, DIR);
21110+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
21111+ bcpup = err;
21112+ }
1facf9fc 21113+
5527c038 21114+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
21115+ if (add_entry)
21116+ err = AuWbrCopyup(sbinfo, dentry);
21117+ else {
21118+ if (!IS_ROOT(dentry)) {
21119+ di_read_lock_parent(parent, !AuLock_IR);
21120+ err = AuWbrCopyup(sbinfo, dentry);
21121+ di_read_unlock(parent, !AuLock_IR);
21122+ } else
21123+ err = AuWbrCopyup(sbinfo, dentry);
21124+ }
21125+ bcpup = err;
21126+ if (unlikely(err < 0))
21127+ goto out;
21128+ }
21129+ } else {
21130+ bcpup = args->force_btgt;
5527c038 21131+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21132+ }
027c5e7a 21133+
5afbbe0d 21134+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21135+ err = bcpup;
5afbbe0d 21136+ if (bcpup == btop)
4a4d8108 21137+ goto out; /* success */
4a4d8108
AM
21138+
21139+ /* copyup the new parent into the branch we process */
5afbbe0d 21140+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21141+ if (err >= 0) {
5527c038 21142+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21143+ au_set_h_dptr(dentry, btop, NULL);
21144+ au_set_dbtop(dentry, bcpup);
21145+ au_set_dbbot(dentry, bcpup);
027c5e7a 21146+ }
38d290e6
JR
21147+ AuDebugOn(add_entry
21148+ && !au_ftest_wrdir(args->flags, TMPFILE)
21149+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21150+ }
86dc4139
AM
21151+
21152+out:
21153+ dput(parent);
21154+ return err;
21155+}
21156+
21157+/* ---------------------------------------------------------------------- */
21158+
21159+void au_pin_hdir_unlock(struct au_pin *p)
21160+{
21161+ if (p->hdir)
5afbbe0d 21162+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21163+}
21164+
c1595e42 21165+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21166+{
21167+ int err;
21168+
21169+ err = 0;
21170+ if (!p->hdir)
21171+ goto out;
21172+
21173+ /* even if an error happens later, keep this lock */
5afbbe0d 21174+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21175+
21176+ err = -EBUSY;
5527c038 21177+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21178+ goto out;
21179+
21180+ err = 0;
21181+ if (p->h_dentry)
21182+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21183+ p->h_parent, p->br);
21184+
21185+out:
21186+ return err;
21187+}
21188+
21189+int au_pin_hdir_relock(struct au_pin *p)
21190+{
21191+ int err, i;
21192+ struct inode *h_i;
21193+ struct dentry *h_d[] = {
21194+ p->h_dentry,
21195+ p->h_parent
21196+ };
21197+
21198+ err = au_pin_hdir_lock(p);
21199+ if (unlikely(err))
21200+ goto out;
21201+
21202+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21203+ if (!h_d[i])
21204+ continue;
5527c038
JR
21205+ if (d_is_positive(h_d[i])) {
21206+ h_i = d_inode(h_d[i]);
86dc4139 21207+ err = !h_i->i_nlink;
5527c038 21208+ }
86dc4139
AM
21209+ }
21210+
21211+out:
21212+ return err;
21213+}
21214+
5afbbe0d 21215+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21216+{
eca34b5c 21217+ atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
86dc4139
AM
21218+}
21219+
21220+void au_pin_hdir_acquire_nest(struct au_pin *p)
21221+{
21222+ if (p->hdir) {
5afbbe0d 21223+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21224+ p->lsc_hi, 0, NULL, _RET_IP_);
21225+ au_pin_hdir_set_owner(p, current);
21226+ }
dece6358 21227+}
1facf9fc 21228+
86dc4139
AM
21229+void au_pin_hdir_release(struct au_pin *p)
21230+{
21231+ if (p->hdir) {
21232+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 21233+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
21234+ }
21235+}
1308ab2a 21236+
4a4d8108 21237+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21238+{
4a4d8108
AM
21239+ if (pin && pin->parent)
21240+ return au_h_dptr(pin->parent, pin->bindex);
21241+ return NULL;
dece6358 21242+}
1facf9fc 21243+
4a4d8108 21244+void au_unpin(struct au_pin *p)
dece6358 21245+{
86dc4139
AM
21246+ if (p->hdir)
21247+ au_pin_hdir_unlock(p);
e49829fe 21248+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21249+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21250+ if (!p->hdir)
21251+ return;
1facf9fc 21252+
4a4d8108
AM
21253+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21254+ di_read_unlock(p->parent, AuLock_IR);
21255+ iput(p->hdir->hi_inode);
21256+ dput(p->parent);
21257+ p->parent = NULL;
21258+ p->hdir = NULL;
21259+ p->h_mnt = NULL;
86dc4139 21260+ /* do not clear p->task */
4a4d8108 21261+}
1308ab2a 21262+
4a4d8108
AM
21263+int au_do_pin(struct au_pin *p)
21264+{
21265+ int err;
21266+ struct super_block *sb;
4a4d8108
AM
21267+ struct inode *h_dir;
21268+
21269+ err = 0;
21270+ sb = p->dentry->d_sb;
86dc4139 21271+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21272+ if (IS_ROOT(p->dentry)) {
21273+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21274+ p->h_mnt = au_br_mnt(p->br);
b4510431 21275+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21276+ if (unlikely(err)) {
21277+ au_fclr_pin(p->flags, MNT_WRITE);
21278+ goto out_err;
21279+ }
21280+ }
dece6358 21281+ goto out;
1facf9fc 21282+ }
21283+
86dc4139 21284+ p->h_dentry = NULL;
5afbbe0d 21285+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21286+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21287+
4a4d8108
AM
21288+ p->parent = dget_parent(p->dentry);
21289+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21290+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21291+
4a4d8108 21292+ h_dir = NULL;
86dc4139 21293+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21294+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21295+ if (p->hdir)
21296+ h_dir = p->hdir->hi_inode;
dece6358 21297+
b752ccd1
AM
21298+ /*
21299+ * udba case, or
21300+ * if DI_LOCKED is not set, then p->parent may be different
21301+ * and h_parent can be NULL.
21302+ */
86dc4139 21303+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21304+ err = -EBUSY;
4a4d8108
AM
21305+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21306+ di_read_unlock(p->parent, AuLock_IR);
21307+ dput(p->parent);
21308+ p->parent = NULL;
21309+ goto out_err;
21310+ }
1308ab2a 21311+
4a4d8108 21312+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21313+ p->h_mnt = au_br_mnt(p->br);
b4510431 21314+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21315+ if (unlikely(err)) {
4a4d8108 21316+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
21317+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21318+ di_read_unlock(p->parent, AuLock_IR);
21319+ dput(p->parent);
21320+ p->parent = NULL;
21321+ goto out_err;
dece6358
AM
21322+ }
21323+ }
4a4d8108 21324+
86dc4139
AM
21325+ au_igrab(h_dir);
21326+ err = au_pin_hdir_lock(p);
21327+ if (!err)
21328+ goto out; /* success */
21329+
076b876e
AM
21330+ au_unpin(p);
21331+
4f0767ce 21332+out_err:
4a4d8108
AM
21333+ pr_err("err %d\n", err);
21334+ err = au_busy_or_stale();
4f0767ce 21335+out:
1facf9fc 21336+ return err;
21337+}
21338+
4a4d8108
AM
21339+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21340+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21341+ unsigned int udba, unsigned char flags)
21342+{
21343+ p->dentry = dentry;
21344+ p->udba = udba;
21345+ p->lsc_di = lsc_di;
21346+ p->lsc_hi = lsc_hi;
21347+ p->flags = flags;
21348+ p->bindex = bindex;
21349+
21350+ p->parent = NULL;
21351+ p->hdir = NULL;
21352+ p->h_mnt = NULL;
86dc4139
AM
21353+
21354+ p->h_dentry = NULL;
21355+ p->h_parent = NULL;
21356+ p->br = NULL;
21357+ p->task = current;
4a4d8108
AM
21358+}
21359+
21360+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21361+ unsigned int udba, unsigned char flags)
21362+{
21363+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21364+ udba, flags);
21365+ return au_do_pin(pin);
21366+}
21367+
dece6358
AM
21368+/* ---------------------------------------------------------------------- */
21369+
1308ab2a 21370+/*
4a4d8108
AM
21371+ * ->setattr() and ->getattr() are called in various cases.
21372+ * chmod, stat: dentry is revalidated.
21373+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21374+ * unhashed.
21375+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21376+ */
027c5e7a 21377+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21378+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21379+{
4a4d8108 21380+ int err;
4a4d8108 21381+ struct dentry *parent;
1facf9fc 21382+
1308ab2a 21383+ err = 0;
027c5e7a 21384+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21385+ parent = dget_parent(dentry);
21386+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21387+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21388+ di_read_unlock(parent, AuLock_IR);
21389+ dput(parent);
dece6358 21390+ }
1facf9fc 21391+
4a4d8108 21392+ AuTraceErr(err);
1308ab2a 21393+ return err;
21394+}
dece6358 21395+
c1595e42
JR
21396+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21397+ struct au_icpup_args *a)
1308ab2a 21398+{
21399+ int err;
4a4d8108 21400+ loff_t sz;
5afbbe0d 21401+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21402+ struct dentry *hi_wh, *parent;
21403+ struct inode *inode;
4a4d8108
AM
21404+ struct au_wr_dir_args wr_dir_args = {
21405+ .force_btgt = -1,
21406+ .flags = 0
21407+ };
21408+
2000de60 21409+ if (d_is_dir(dentry))
4a4d8108
AM
21410+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21411+ /* plink or hi_wh() case */
5afbbe0d 21412+ btop = au_dbtop(dentry);
5527c038 21413+ inode = d_inode(dentry);
5afbbe0d
AM
21414+ ibtop = au_ibtop(inode);
21415+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21416+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21417+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21418+ if (unlikely(err < 0))
21419+ goto out;
21420+ a->btgt = err;
5afbbe0d 21421+ if (err != btop)
4a4d8108
AM
21422+ au_fset_icpup(a->flags, DID_CPUP);
21423+
21424+ err = 0;
21425+ a->pin_flags = AuPin_MNT_WRITE;
21426+ parent = NULL;
21427+ if (!IS_ROOT(dentry)) {
21428+ au_fset_pin(a->pin_flags, DI_LOCKED);
21429+ parent = dget_parent(dentry);
21430+ di_write_lock_parent(parent);
21431+ }
21432+
21433+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21434+ if (unlikely(err))
21435+ goto out_parent;
21436+
4a4d8108 21437+ sz = -1;
5afbbe0d 21438+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21439+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21440+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 21441+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21442+ if (ia->ia_size < i_size_read(a->h_inode))
21443+ sz = ia->ia_size;
3c1bdaff 21444+ inode_unlock_shared(a->h_inode);
c1595e42 21445+ }
4a4d8108 21446+
4a4d8108 21447+ hi_wh = NULL;
027c5e7a 21448+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21449+ hi_wh = au_hi_wh(inode, a->btgt);
21450+ if (!hi_wh) {
c2b27bf2
AM
21451+ struct au_cp_generic cpg = {
21452+ .dentry = dentry,
21453+ .bdst = a->btgt,
21454+ .bsrc = -1,
21455+ .len = sz,
21456+ .pin = &a->pin
21457+ };
21458+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21459+ if (unlikely(err))
21460+ goto out_unlock;
21461+ hi_wh = au_hi_wh(inode, a->btgt);
21462+ /* todo: revalidate hi_wh? */
21463+ }
21464+ }
21465+
21466+ if (parent) {
21467+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21468+ di_downgrade_lock(parent, AuLock_IR);
21469+ dput(parent);
21470+ parent = NULL;
21471+ }
21472+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21473+ goto out; /* success */
21474+
21475+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21476+ struct au_cp_generic cpg = {
21477+ .dentry = dentry,
21478+ .bdst = a->btgt,
5afbbe0d 21479+ .bsrc = btop,
c2b27bf2
AM
21480+ .len = sz,
21481+ .pin = &a->pin,
21482+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21483+ };
21484+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21485+ if (!err)
21486+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21487+ } else if (!hi_wh)
21488+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21489+ else
21490+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21491+
4f0767ce 21492+out_unlock:
5527c038 21493+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21494+ if (!err)
dece6358 21495+ goto out; /* success */
4a4d8108 21496+ au_unpin(&a->pin);
4f0767ce 21497+out_parent:
4a4d8108
AM
21498+ if (parent) {
21499+ di_write_unlock(parent);
21500+ dput(parent);
21501+ }
4f0767ce 21502+out:
86dc4139 21503+ if (!err)
febd17d6 21504+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21505+ return err;
21506+}
21507+
4a4d8108 21508+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 21509+{
4a4d8108 21510+ int err;
523b37e3 21511+ struct inode *inode, *delegated;
4a4d8108
AM
21512+ struct super_block *sb;
21513+ struct file *file;
21514+ struct au_icpup_args *a;
1facf9fc 21515+
5527c038 21516+ inode = d_inode(dentry);
4a4d8108 21517+ IMustLock(inode);
dece6358 21518+
f2c43d5f
AM
21519+ err = setattr_prepare(dentry, ia);
21520+ if (unlikely(err))
21521+ goto out;
21522+
4a4d8108
AM
21523+ err = -ENOMEM;
21524+ a = kzalloc(sizeof(*a), GFP_NOFS);
21525+ if (unlikely(!a))
21526+ goto out;
1facf9fc 21527+
4a4d8108
AM
21528+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21529+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21530+
4a4d8108
AM
21531+ file = NULL;
21532+ sb = dentry->d_sb;
e49829fe
JR
21533+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21534+ if (unlikely(err))
21535+ goto out_kfree;
21536+
4a4d8108
AM
21537+ if (ia->ia_valid & ATTR_FILE) {
21538+ /* currently ftruncate(2) only */
7e9cd9fe 21539+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21540+ file = ia->ia_file;
521ced18
JR
21541+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21542+ /*fi_lsc*/0);
4a4d8108
AM
21543+ if (unlikely(err))
21544+ goto out_si;
21545+ ia->ia_file = au_hf_top(file);
21546+ a->udba = AuOpt_UDBA_NONE;
21547+ } else {
21548+ /* fchmod() doesn't pass ia_file */
21549+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21550+ di_write_lock_child(dentry);
21551+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21552+ if (d_unhashed(dentry))
21553+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21554+ if (a->udba != AuOpt_UDBA_NONE) {
21555+ AuDebugOn(IS_ROOT(dentry));
21556+ err = au_reval_for_attr(dentry, au_sigen(sb));
21557+ if (unlikely(err))
21558+ goto out_dentry;
21559+ }
dece6358 21560+ }
dece6358 21561+
4a4d8108
AM
21562+ err = au_pin_and_icpup(dentry, ia, a);
21563+ if (unlikely(err < 0))
21564+ goto out_dentry;
21565+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21566+ ia->ia_file = NULL;
21567+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21568+ }
dece6358 21569+
4a4d8108
AM
21570+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21571+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21572+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21573+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21574+ if (unlikely(err))
21575+ goto out_unlock;
21576+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21577+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21578+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21579+ if (unlikely(err))
21580+ goto out_unlock;
21581+ }
dece6358 21582+
4a4d8108
AM
21583+ if (ia->ia_valid & ATTR_SIZE) {
21584+ struct file *f;
1308ab2a 21585+
953406b4 21586+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21587+ /* unmap only */
953406b4 21588+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21589+
4a4d8108
AM
21590+ f = NULL;
21591+ if (ia->ia_valid & ATTR_FILE)
21592+ f = ia->ia_file;
febd17d6 21593+ inode_unlock(a->h_inode);
4a4d8108 21594+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21595+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21596+ } else {
21597+ delegated = NULL;
21598+ while (1) {
21599+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21600+ if (delegated) {
21601+ err = break_deleg_wait(&delegated);
21602+ if (!err)
21603+ continue;
21604+ }
21605+ break;
21606+ }
21607+ }
8cdd5066
JR
21608+ /*
21609+ * regardless aufs 'acl' option setting.
21610+ * why don't all acl-aware fs call this func from their ->setattr()?
21611+ */
21612+ if (!err && (ia->ia_valid & ATTR_MODE))
21613+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
21614+ if (!err)
21615+ au_cpup_attr_changeable(inode);
1308ab2a 21616+
4f0767ce 21617+out_unlock:
febd17d6 21618+ inode_unlock(a->h_inode);
4a4d8108 21619+ au_unpin(&a->pin);
027c5e7a 21620+ if (unlikely(err))
5afbbe0d 21621+ au_update_dbtop(dentry);
4f0767ce 21622+out_dentry:
4a4d8108
AM
21623+ di_write_unlock(dentry);
21624+ if (file) {
21625+ fi_write_unlock(file);
21626+ ia->ia_file = file;
21627+ ia->ia_valid |= ATTR_FILE;
21628+ }
4f0767ce 21629+out_si:
4a4d8108 21630+ si_read_unlock(sb);
e49829fe 21631+out_kfree:
9f237c51 21632+ au_kfree_rcu(a);
4f0767ce 21633+out:
4a4d8108
AM
21634+ AuTraceErr(err);
21635+ return err;
1facf9fc 21636+}
21637+
c1595e42
JR
21638+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21639+static int au_h_path_to_set_attr(struct dentry *dentry,
21640+ struct au_icpup_args *a, struct path *h_path)
21641+{
21642+ int err;
21643+ struct super_block *sb;
21644+
21645+ sb = dentry->d_sb;
21646+ a->udba = au_opt_udba(sb);
21647+ /* no d_unlinked(), to set UDBA_NONE for root */
21648+ if (d_unhashed(dentry))
21649+ a->udba = AuOpt_UDBA_NONE;
21650+ if (a->udba != AuOpt_UDBA_NONE) {
21651+ AuDebugOn(IS_ROOT(dentry));
21652+ err = au_reval_for_attr(dentry, au_sigen(sb));
21653+ if (unlikely(err))
21654+ goto out;
21655+ }
21656+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21657+ if (unlikely(err < 0))
21658+ goto out;
21659+
21660+ h_path->dentry = a->h_path.dentry;
21661+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21662+
21663+out:
21664+ return err;
21665+}
21666+
f2c43d5f
AM
21667+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21668+ struct au_sxattr *arg)
c1595e42
JR
21669+{
21670+ int err;
21671+ struct path h_path;
21672+ struct super_block *sb;
21673+ struct au_icpup_args *a;
5afbbe0d 21674+ struct inode *h_inode;
c1595e42 21675+
c1595e42
JR
21676+ IMustLock(inode);
21677+
21678+ err = -ENOMEM;
21679+ a = kzalloc(sizeof(*a), GFP_NOFS);
21680+ if (unlikely(!a))
21681+ goto out;
21682+
21683+ sb = dentry->d_sb;
21684+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21685+ if (unlikely(err))
21686+ goto out_kfree;
21687+
21688+ h_path.dentry = NULL; /* silence gcc */
21689+ di_write_lock_child(dentry);
21690+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21691+ if (unlikely(err))
21692+ goto out_di;
21693+
febd17d6 21694+ inode_unlock(a->h_inode);
c1595e42
JR
21695+ switch (arg->type) {
21696+ case AU_XATTR_SET:
5afbbe0d 21697+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
21698+ err = vfsub_setxattr(h_path.dentry,
21699+ arg->u.set.name, arg->u.set.value,
21700+ arg->u.set.size, arg->u.set.flags);
21701+ break;
c1595e42
JR
21702+ case AU_ACL_SET:
21703+ err = -EOPNOTSUPP;
5527c038 21704+ h_inode = d_inode(h_path.dentry);
c1595e42 21705+ if (h_inode->i_op->set_acl)
f2c43d5f 21706+ /* this will call posix_acl_update_mode */
c1595e42
JR
21707+ err = h_inode->i_op->set_acl(h_inode,
21708+ arg->u.acl_set.acl,
21709+ arg->u.acl_set.type);
21710+ break;
21711+ }
21712+ if (!err)
21713+ au_cpup_attr_timesizes(inode);
21714+
21715+ au_unpin(&a->pin);
21716+ if (unlikely(err))
5afbbe0d 21717+ au_update_dbtop(dentry);
c1595e42
JR
21718+
21719+out_di:
21720+ di_write_unlock(dentry);
21721+ si_read_unlock(sb);
21722+out_kfree:
9f237c51 21723+ au_kfree_rcu(a);
c1595e42
JR
21724+out:
21725+ AuTraceErr(err);
21726+ return err;
21727+}
21728+#endif
21729+
4a4d8108
AM
21730+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21731+ unsigned int nlink)
1facf9fc 21732+{
9dbd164d
AM
21733+ unsigned int n;
21734+
4a4d8108 21735+ inode->i_mode = st->mode;
86dc4139
AM
21736+ /* don't i_[ug]id_write() here */
21737+ inode->i_uid = st->uid;
21738+ inode->i_gid = st->gid;
4a4d8108
AM
21739+ inode->i_atime = st->atime;
21740+ inode->i_mtime = st->mtime;
21741+ inode->i_ctime = st->ctime;
1facf9fc 21742+
4a4d8108
AM
21743+ au_cpup_attr_nlink(inode, /*force*/0);
21744+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21745+ n = inode->i_nlink;
21746+ n -= nlink;
21747+ n += st->nlink;
f6b6e03d 21748+ smp_mb(); /* for i_nlink */
7eafdf33 21749+ /* 0 can happen */
92d182d2 21750+ set_nlink(inode, n);
4a4d8108 21751+ }
1facf9fc 21752+
4a4d8108
AM
21753+ spin_lock(&inode->i_lock);
21754+ inode->i_blocks = st->blocks;
21755+ i_size_write(inode, st->size);
21756+ spin_unlock(&inode->i_lock);
1facf9fc 21757+}
21758+
c1595e42 21759+/*
f2c43d5f 21760+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21761+ * returns zero or negative (an error).
21762+ * @dentry will be read-locked in success.
21763+ */
a2654f78
AM
21764+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21765+ int locked)
1facf9fc 21766+{
4a4d8108 21767+ int err;
076b876e 21768+ unsigned int mnt_flags, sigen;
c1595e42 21769+ unsigned char udba_none;
4a4d8108 21770+ aufs_bindex_t bindex;
4a4d8108
AM
21771+ struct super_block *sb, *h_sb;
21772+ struct inode *inode;
1facf9fc 21773+
c1595e42
JR
21774+ h_path->mnt = NULL;
21775+ h_path->dentry = NULL;
21776+
21777+ err = 0;
4a4d8108 21778+ sb = dentry->d_sb;
4a4d8108
AM
21779+ mnt_flags = au_mntflags(sb);
21780+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21781+
a2654f78
AM
21782+ if (unlikely(locked))
21783+ goto body; /* skip locking dinfo */
21784+
4a4d8108 21785+ /* support fstat(2) */
027c5e7a 21786+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21787+ sigen = au_sigen(sb);
027c5e7a
AM
21788+ err = au_digen_test(dentry, sigen);
21789+ if (!err) {
4a4d8108 21790+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21791+ err = au_dbrange_test(dentry);
c1595e42
JR
21792+ if (unlikely(err)) {
21793+ di_read_unlock(dentry, AuLock_IR);
21794+ goto out;
21795+ }
027c5e7a 21796+ } else {
4a4d8108
AM
21797+ AuDebugOn(IS_ROOT(dentry));
21798+ di_write_lock_child(dentry);
027c5e7a
AM
21799+ err = au_dbrange_test(dentry);
21800+ if (!err)
21801+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21802+ if (!err)
21803+ di_downgrade_lock(dentry, AuLock_IR);
21804+ else {
21805+ di_write_unlock(dentry);
21806+ goto out;
21807+ }
4a4d8108
AM
21808+ }
21809+ } else
21810+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21811+
a2654f78 21812+body:
5527c038 21813+ inode = d_inode(dentry);
5afbbe0d 21814+ bindex = au_ibtop(inode);
c1595e42
JR
21815+ h_path->mnt = au_sbr_mnt(sb, bindex);
21816+ h_sb = h_path->mnt->mnt_sb;
21817+ if (!force
21818+ && !au_test_fs_bad_iattr(h_sb)
21819+ && udba_none)
21820+ goto out; /* success */
1facf9fc 21821+
5afbbe0d 21822+ if (au_dbtop(dentry) == bindex)
c1595e42 21823+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21824+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21825+ h_path->dentry = au_plink_lkup(inode, bindex);
21826+ if (IS_ERR(h_path->dentry))
21827+ /* pretending success */
21828+ h_path->dentry = NULL;
21829+ else
21830+ dput(h_path->dentry);
4a4d8108 21831+ }
c1595e42
JR
21832+
21833+out:
21834+ return err;
21835+}
21836+
521ced18
JR
21837+static int aufs_getattr(const struct path *path, struct kstat *st,
21838+ u32 request, unsigned int query)
c1595e42
JR
21839+{
21840+ int err;
21841+ unsigned char positive;
21842+ struct path h_path;
521ced18 21843+ struct dentry *dentry;
c1595e42
JR
21844+ struct inode *inode;
21845+ struct super_block *sb;
21846+
521ced18 21847+ dentry = path->dentry;
5527c038 21848+ inode = d_inode(dentry);
c1595e42
JR
21849+ sb = dentry->d_sb;
21850+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21851+ if (unlikely(err))
21852+ goto out;
a2654f78 21853+ err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
c1595e42
JR
21854+ if (unlikely(err))
21855+ goto out_si;
c06a8ce3 21856+ if (unlikely(!h_path.dentry))
c1595e42 21857+ /* illegally overlapped or something */
4a4d8108
AM
21858+ goto out_fill; /* pretending success */
21859+
5527c038 21860+ positive = d_is_positive(h_path.dentry);
4a4d8108 21861+ if (positive)
521ced18
JR
21862+ /* no vfsub version */
21863+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21864+ if (!err) {
21865+ if (positive)
c06a8ce3 21866+ au_refresh_iattr(inode, st,
5527c038 21867+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21868+ goto out_fill; /* success */
1facf9fc 21869+ }
7f207e10 21870+ AuTraceErr(err);
c1595e42 21871+ goto out_di;
4a4d8108 21872+
4f0767ce 21873+out_fill:
4a4d8108 21874+ generic_fillattr(inode, st);
c1595e42 21875+out_di:
4a4d8108 21876+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21877+out_si:
4a4d8108 21878+ si_read_unlock(sb);
7f207e10
AM
21879+out:
21880+ AuTraceErr(err);
4a4d8108 21881+ return err;
1facf9fc 21882+}
21883+
21884+/* ---------------------------------------------------------------------- */
21885+
febd17d6
JR
21886+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21887+ struct delayed_call *done)
4a4d8108 21888+{
c2c0f25c 21889+ const char *ret;
c2c0f25c 21890+ struct dentry *h_dentry;
febd17d6 21891+ struct inode *h_inode;
4a4d8108 21892+ int err;
c2c0f25c 21893+ aufs_bindex_t bindex;
1facf9fc 21894+
79b8bda9 21895+ ret = NULL; /* suppress a warning */
febd17d6
JR
21896+ err = -ECHILD;
21897+ if (!dentry)
21898+ goto out;
21899+
027c5e7a
AM
21900+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21901+ if (unlikely(err))
c2c0f25c 21902+ goto out;
027c5e7a
AM
21903+
21904+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
21905+ if (unlikely(err))
21906+ goto out_unlock;
21907+
21908+ err = -EINVAL;
21909+ inode = d_inode(dentry);
5afbbe0d 21910+ bindex = au_ibtop(inode);
c2c0f25c 21911+ h_inode = au_h_iptr(inode, bindex);
febd17d6 21912+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
21913+ goto out_unlock;
21914+
21915+ err = -EBUSY;
21916+ h_dentry = NULL;
5afbbe0d 21917+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
21918+ h_dentry = au_h_dptr(dentry, bindex);
21919+ if (h_dentry)
21920+ dget(h_dentry);
027c5e7a 21921+ }
c2c0f25c
AM
21922+ if (!h_dentry) {
21923+ h_dentry = d_find_any_alias(h_inode);
21924+ if (IS_ERR(h_dentry)) {
21925+ err = PTR_ERR(h_dentry);
febd17d6 21926+ goto out_unlock;
c2c0f25c
AM
21927+ }
21928+ }
21929+ if (unlikely(!h_dentry))
febd17d6 21930+ goto out_unlock;
1facf9fc 21931+
c2c0f25c 21932+ err = 0;
062440b3 21933+ AuDbg("%ps\n", h_inode->i_op->get_link);
c2c0f25c 21934+ AuDbgDentry(h_dentry);
f2c43d5f 21935+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 21936+ dput(h_dentry);
febd17d6
JR
21937+ if (IS_ERR(ret))
21938+ err = PTR_ERR(ret);
c2c0f25c 21939+
c2c0f25c
AM
21940+out_unlock:
21941+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 21942+out:
c2c0f25c
AM
21943+ if (unlikely(err))
21944+ ret = ERR_PTR(err);
21945+ AuTraceErrPtr(ret);
21946+ return ret;
4a4d8108 21947+}
1facf9fc 21948+
4a4d8108 21949+/* ---------------------------------------------------------------------- */
1facf9fc 21950+
e2f27e51
AM
21951+static int au_is_special(struct inode *inode)
21952+{
21953+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21954+}
21955+
acd2b654
AM
21956+static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21957+ int flags)
4a4d8108 21958+{
0c3ec466 21959+ int err;
e2f27e51 21960+ aufs_bindex_t bindex;
0c3ec466
AM
21961+ struct super_block *sb;
21962+ struct inode *h_inode;
e2f27e51 21963+ struct vfsmount *h_mnt;
0c3ec466
AM
21964+
21965+ sb = inode->i_sb;
e2f27e51
AM
21966+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21967+ "unexpected s_flags 0x%lx", sb->s_flags);
21968+
0c3ec466
AM
21969+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
21970+ lockdep_off();
21971+ si_read_lock(sb, AuLock_FLUSH);
21972+ ii_write_lock_child(inode);
e2f27e51
AM
21973+
21974+ err = 0;
21975+ bindex = au_ibtop(inode);
21976+ h_inode = au_h_iptr(inode, bindex);
21977+ if (!au_test_ro(sb, bindex, inode)) {
21978+ h_mnt = au_sbr_mnt(sb, bindex);
21979+ err = vfsub_mnt_want_write(h_mnt);
21980+ if (!err) {
21981+ err = vfsub_update_time(h_inode, ts, flags);
21982+ vfsub_mnt_drop_write(h_mnt);
21983+ }
21984+ } else if (au_is_special(h_inode)) {
21985+ /*
21986+ * Never copy-up here.
21987+ * These special files may already be opened and used for
21988+ * communicating. If we copied it up, then the communication
21989+ * would be corrupted.
21990+ */
21991+ AuWarn1("timestamps for i%lu are ignored "
21992+ "since it is on readonly branch (hi%lu).\n",
21993+ inode->i_ino, h_inode->i_ino);
21994+ } else if (flags & ~S_ATIME) {
21995+ err = -EIO;
21996+ AuIOErr1("unexpected flags 0x%x\n", flags);
21997+ AuDebugOn(1);
21998+ }
21999+
38d290e6
JR
22000+ if (!err)
22001+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
22002+ ii_write_unlock(inode);
22003+ si_read_unlock(sb);
22004+ lockdep_on();
38d290e6
JR
22005+
22006+ if (!err && (flags & S_VERSION))
22007+ inode_inc_iversion(inode);
22008+
0c3ec466 22009+ return err;
4a4d8108 22010+}
1facf9fc 22011+
4a4d8108 22012+/* ---------------------------------------------------------------------- */
1308ab2a 22013+
b95c5147
AM
22014+/* no getattr version will be set by module.c:aufs_init() */
22015+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22016+ aufs_iop[] = {
22017+ [AuIop_SYMLINK] = {
22018+ .permission = aufs_permission,
c1595e42 22019+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22020+ .get_acl = aufs_get_acl,
22021+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
22022+#endif
22023+
b95c5147
AM
22024+ .setattr = aufs_setattr,
22025+ .getattr = aufs_getattr,
0c3ec466 22026+
c1595e42 22027+#ifdef CONFIG_AUFS_XATTR
b95c5147 22028+ .listxattr = aufs_listxattr,
c1595e42
JR
22029+#endif
22030+
febd17d6 22031+ .get_link = aufs_get_link,
0c3ec466 22032+
b95c5147
AM
22033+ /* .update_time = aufs_update_time */
22034+ },
22035+ [AuIop_DIR] = {
22036+ .create = aufs_create,
22037+ .lookup = aufs_lookup,
22038+ .link = aufs_link,
22039+ .unlink = aufs_unlink,
22040+ .symlink = aufs_symlink,
22041+ .mkdir = aufs_mkdir,
22042+ .rmdir = aufs_rmdir,
22043+ .mknod = aufs_mknod,
22044+ .rename = aufs_rename,
22045+
22046+ .permission = aufs_permission,
c1595e42 22047+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22048+ .get_acl = aufs_get_acl,
22049+ .set_acl = aufs_set_acl,
c1595e42
JR
22050+#endif
22051+
b95c5147
AM
22052+ .setattr = aufs_setattr,
22053+ .getattr = aufs_getattr,
0c3ec466 22054+
c1595e42 22055+#ifdef CONFIG_AUFS_XATTR
b95c5147 22056+ .listxattr = aufs_listxattr,
c1595e42
JR
22057+#endif
22058+
b95c5147
AM
22059+ .update_time = aufs_update_time,
22060+ .atomic_open = aufs_atomic_open,
22061+ .tmpfile = aufs_tmpfile
22062+ },
22063+ [AuIop_OTHER] = {
22064+ .permission = aufs_permission,
c1595e42 22065+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22066+ .get_acl = aufs_get_acl,
22067+ .set_acl = aufs_set_acl,
c1595e42
JR
22068+#endif
22069+
b95c5147
AM
22070+ .setattr = aufs_setattr,
22071+ .getattr = aufs_getattr,
0c3ec466 22072+
c1595e42 22073+#ifdef CONFIG_AUFS_XATTR
b95c5147 22074+ .listxattr = aufs_listxattr,
c1595e42
JR
22075+#endif
22076+
b95c5147
AM
22077+ .update_time = aufs_update_time
22078+ }
4a4d8108 22079+};
7f207e10 22080diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
eca34b5c
AM
22081--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
22082+++ linux/fs/aufs/i_op_del.c 2019-07-11 15:42:14.468904634 +0200
eca801bf 22083@@ -0,0 +1,513 @@
cd7a4cd9 22084+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22085+/*
ba1aed25 22086+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 22087+ *
22088+ * This program, aufs is free software; you can redistribute it and/or modify
22089+ * it under the terms of the GNU General Public License as published by
22090+ * the Free Software Foundation; either version 2 of the License, or
22091+ * (at your option) any later version.
dece6358
AM
22092+ *
22093+ * This program is distributed in the hope that it will be useful,
22094+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22095+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22096+ * GNU General Public License for more details.
22097+ *
22098+ * You should have received a copy of the GNU General Public License
523b37e3 22099+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22100+ */
22101+
22102+/*
4a4d8108 22103+ * inode operations (del entry)
1308ab2a 22104+ */
dece6358 22105+
eca801bf 22106+#include <linux/iversion.h>
1308ab2a 22107+#include "aufs.h"
dece6358 22108+
4a4d8108
AM
22109+/*
22110+ * decide if a new whiteout for @dentry is necessary or not.
22111+ * when it is necessary, prepare the parent dir for the upper branch whose
22112+ * branch index is @bcpup for creation. the actual creation of the whiteout will
22113+ * be done by caller.
22114+ * return value:
22115+ * 0: wh is unnecessary
22116+ * plus: wh is necessary
22117+ * minus: error
22118+ */
22119+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 22120+{
4a4d8108 22121+ int need_wh, err;
5afbbe0d 22122+ aufs_bindex_t btop;
4a4d8108 22123+ struct super_block *sb;
dece6358 22124+
4a4d8108 22125+ sb = dentry->d_sb;
5afbbe0d 22126+ btop = au_dbtop(dentry);
4a4d8108 22127+ if (*bcpup < 0) {
5afbbe0d
AM
22128+ *bcpup = btop;
22129+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
22130+ err = AuWbrCopyup(au_sbi(sb), dentry);
22131+ *bcpup = err;
22132+ if (unlikely(err < 0))
22133+ goto out;
22134+ }
22135+ } else
5afbbe0d 22136+ AuDebugOn(btop < *bcpup
5527c038 22137+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22138+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22139+
5afbbe0d 22140+ if (*bcpup != btop) {
4a4d8108
AM
22141+ err = au_cpup_dirs(dentry, *bcpup);
22142+ if (unlikely(err))
22143+ goto out;
22144+ need_wh = 1;
22145+ } else {
027c5e7a 22146+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22147+
027c5e7a
AM
22148+ need_wh = -ENOMEM;
22149+ dinfo = au_di(dentry);
22150+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22151+ if (tmp) {
22152+ au_di_cp(tmp, dinfo);
22153+ au_di_swap(tmp, dinfo);
22154+ /* returns the number of positive dentries */
5afbbe0d
AM
22155+ need_wh = au_lkup_dentry(dentry, btop + 1,
22156+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22157+ au_di_swap(tmp, dinfo);
22158+ au_rw_write_unlock(&tmp->di_rwsem);
22159+ au_di_free(tmp);
4a4d8108
AM
22160+ }
22161+ }
22162+ AuDbg("need_wh %d\n", need_wh);
22163+ err = need_wh;
22164+
4f0767ce 22165+out:
4a4d8108 22166+ return err;
1facf9fc 22167+}
22168+
4a4d8108
AM
22169+/*
22170+ * simple tests for the del-entry operations.
22171+ * following the checks in vfs, plus the parent-child relationship.
22172+ */
22173+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22174+ struct dentry *h_parent, int isdir)
1facf9fc 22175+{
4a4d8108
AM
22176+ int err;
22177+ umode_t h_mode;
22178+ struct dentry *h_dentry, *h_latest;
1308ab2a 22179+ struct inode *h_inode;
1facf9fc 22180+
4a4d8108 22181+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22182+ if (d_really_is_positive(dentry)) {
4a4d8108 22183+ err = -ENOENT;
5527c038
JR
22184+ if (unlikely(d_is_negative(h_dentry)))
22185+ goto out;
22186+ h_inode = d_inode(h_dentry);
22187+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22188+ goto out;
1facf9fc 22189+
4a4d8108
AM
22190+ h_mode = h_inode->i_mode;
22191+ if (!isdir) {
22192+ err = -EISDIR;
22193+ if (unlikely(S_ISDIR(h_mode)))
22194+ goto out;
22195+ } else if (unlikely(!S_ISDIR(h_mode))) {
22196+ err = -ENOTDIR;
22197+ goto out;
22198+ }
22199+ } else {
22200+ /* rename(2) case */
22201+ err = -EIO;
5527c038 22202+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22203+ goto out;
22204+ }
1facf9fc 22205+
4a4d8108
AM
22206+ err = -ENOENT;
22207+ /* expected parent dir is locked */
22208+ if (unlikely(h_parent != h_dentry->d_parent))
22209+ goto out;
22210+ err = 0;
22211+
22212+ /*
22213+ * rmdir a dir may break the consistency on some filesystem.
22214+ * let's try heavy test.
22215+ */
22216+ err = -EACCES;
076b876e 22217+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 22218+ && au_test_h_perm(d_inode(h_parent),
076b876e 22219+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22220+ goto out;
22221+
076b876e 22222+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
22223+ err = -EIO;
22224+ if (IS_ERR(h_latest))
22225+ goto out;
22226+ if (h_latest == h_dentry)
22227+ err = 0;
22228+ dput(h_latest);
22229+
4f0767ce 22230+out:
4a4d8108 22231+ return err;
1308ab2a 22232+}
1facf9fc 22233+
4a4d8108
AM
22234+/*
22235+ * decide the branch where we operate for @dentry. the branch index will be set
acd2b654 22236+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
4a4d8108
AM
22237+ * dir for reverting.
22238+ * when a new whiteout is necessary, create it.
22239+ */
22240+static struct dentry*
22241+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22242+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22243+{
4a4d8108
AM
22244+ struct dentry *wh_dentry;
22245+ struct super_block *sb;
22246+ struct path h_path;
22247+ int err, need_wh;
22248+ unsigned int udba;
22249+ aufs_bindex_t bcpup;
dece6358 22250+
4a4d8108
AM
22251+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22252+ wh_dentry = ERR_PTR(need_wh);
22253+ if (unlikely(need_wh < 0))
22254+ goto out;
22255+
22256+ sb = dentry->d_sb;
22257+ udba = au_opt_udba(sb);
22258+ bcpup = *rbcpup;
22259+ err = au_pin(pin, dentry, bcpup, udba,
22260+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22261+ wh_dentry = ERR_PTR(err);
22262+ if (unlikely(err))
22263+ goto out;
22264+
22265+ h_path.dentry = au_pinned_h_parent(pin);
22266+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22267+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22268+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22269+ wh_dentry = ERR_PTR(err);
22270+ if (unlikely(err))
22271+ goto out_unpin;
22272+ }
22273+
22274+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22275+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22276+ wh_dentry = NULL;
22277+ if (!need_wh)
22278+ goto out; /* success, no need to create whiteout */
22279+
22280+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22281+ if (IS_ERR(wh_dentry))
22282+ goto out_unpin;
22283+
22284+ /* returns with the parent is locked and wh_dentry is dget-ed */
22285+ goto out; /* success */
22286+
4f0767ce 22287+out_unpin:
4a4d8108 22288+ au_unpin(pin);
4f0767ce 22289+out:
4a4d8108 22290+ return wh_dentry;
1facf9fc 22291+}
22292+
4a4d8108
AM
22293+/*
22294+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22295+ * in order to be revertible and save time for removing many child whiteouts
22296+ * under the dir.
22297+ * returns 1 when there are too many child whiteout and caller should remove
22298+ * them asynchronously. returns 0 when the number of children is enough small to
22299+ * remove now or the branch fs is a remote fs.
22300+ * otherwise return an error.
22301+ */
22302+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22303+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22304+{
4a4d8108
AM
22305+ int rmdir_later, err, dirwh;
22306+ struct dentry *h_dentry;
22307+ struct super_block *sb;
5527c038 22308+ struct inode *inode;
4a4d8108
AM
22309+
22310+ sb = dentry->d_sb;
22311+ SiMustAnyLock(sb);
22312+ h_dentry = au_h_dptr(dentry, bindex);
22313+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22314+ if (unlikely(err))
22315+ goto out;
22316+
22317+ /* stop monitoring */
5527c038
JR
22318+ inode = d_inode(dentry);
22319+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22320+
22321+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22322+ dirwh = au_sbi(sb)->si_dirwh;
22323+ rmdir_later = (dirwh <= 1);
22324+ if (!rmdir_later)
22325+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22326+ dirwh);
22327+ if (rmdir_later)
22328+ return rmdir_later;
22329+ }
1facf9fc 22330+
4a4d8108
AM
22331+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22332+ if (unlikely(err)) {
523b37e3
AM
22333+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22334+ h_dentry, bindex, err);
4a4d8108
AM
22335+ err = 0;
22336+ }
dece6358 22337+
4f0767ce 22338+out:
4a4d8108
AM
22339+ AuTraceErr(err);
22340+ return err;
22341+}
1308ab2a 22342+
4a4d8108
AM
22343+/*
22344+ * final procedure for deleting a entry.
22345+ * maintain dentry and iattr.
22346+ */
22347+static void epilog(struct inode *dir, struct dentry *dentry,
22348+ aufs_bindex_t bindex)
22349+{
22350+ struct inode *inode;
1308ab2a 22351+
5527c038 22352+ inode = d_inode(dentry);
4a4d8108
AM
22353+ d_drop(dentry);
22354+ inode->i_ctime = dir->i_ctime;
1308ab2a 22355+
b912730e 22356+ au_dir_ts(dir, bindex);
be118d29 22357+ inode_inc_iversion(dir);
1facf9fc 22358+}
22359+
4a4d8108
AM
22360+/*
22361+ * when an error happened, remove the created whiteout and revert everything.
22362+ */
7f207e10
AM
22363+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22364+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22365+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22366+{
4a4d8108
AM
22367+ int rerr;
22368+ struct path h_path = {
22369+ .dentry = wh_dentry,
7f207e10 22370+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22371+ };
dece6358 22372+
7f207e10 22373+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22374+ if (!rerr) {
22375+ au_set_dbwh(dentry, bwh);
22376+ au_dtime_revert(dt);
22377+ return 0;
22378+ }
dece6358 22379+
523b37e3 22380+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22381+ return -EIO;
1facf9fc 22382+}
22383+
4a4d8108 22384+/* ---------------------------------------------------------------------- */
1facf9fc 22385+
4a4d8108 22386+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22387+{
4a4d8108 22388+ int err;
5afbbe0d 22389+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22390+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22391+ struct dentry *parent, *wh_dentry;
acd2b654 22392+ /* to reduce stack size */
c2b27bf2
AM
22393+ struct {
22394+ struct au_dtime dt;
22395+ struct au_pin pin;
22396+ struct path h_path;
22397+ } *a;
1facf9fc 22398+
4a4d8108 22399+ IMustLock(dir);
027c5e7a 22400+
c2b27bf2
AM
22401+ err = -ENOMEM;
22402+ a = kmalloc(sizeof(*a), GFP_NOFS);
22403+ if (unlikely(!a))
22404+ goto out;
22405+
027c5e7a
AM
22406+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22407+ if (unlikely(err))
c2b27bf2 22408+ goto out_free;
027c5e7a
AM
22409+ err = au_d_hashed_positive(dentry);
22410+ if (unlikely(err))
22411+ goto out_unlock;
5527c038 22412+ inode = d_inode(dentry);
4a4d8108 22413+ IMustLock(inode);
027c5e7a 22414+ err = -EISDIR;
2000de60 22415+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22416+ goto out_unlock; /* possible? */
1facf9fc 22417+
5afbbe0d 22418+ btop = au_dbtop(dentry);
4a4d8108
AM
22419+ bwh = au_dbwh(dentry);
22420+ bindex = -1;
027c5e7a
AM
22421+ parent = dentry->d_parent; /* dir inode is locked */
22422+ di_write_lock_parent(parent);
c2b27bf2
AM
22423+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22424+ &a->pin);
4a4d8108
AM
22425+ err = PTR_ERR(wh_dentry);
22426+ if (IS_ERR(wh_dentry))
027c5e7a 22427+ goto out_parent;
1facf9fc 22428+
5afbbe0d
AM
22429+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22430+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22431+ dget(a->h_path.dentry);
5afbbe0d 22432+ if (bindex == btop) {
c2b27bf2 22433+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22434+ delegated = NULL;
22435+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22436+ if (unlikely(err == -EWOULDBLOCK)) {
22437+ pr_warn("cannot retry for NFSv4 delegation"
22438+ " for an internal unlink\n");
22439+ iput(delegated);
22440+ }
4a4d8108
AM
22441+ } else {
22442+ /* dir inode is locked */
5527c038 22443+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22444+ IMustLock(h_dir);
22445+ err = 0;
22446+ }
dece6358 22447+
4a4d8108 22448+ if (!err) {
7f207e10 22449+ vfsub_drop_nlink(inode);
4a4d8108
AM
22450+ epilog(dir, dentry, bindex);
22451+
22452+ /* update target timestamps */
5afbbe0d 22453+ if (bindex == btop) {
c2b27bf2
AM
22454+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22455+ /*ignore*/
5527c038 22456+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22457+ } else
22458+ /* todo: this timestamp may be reverted later */
22459+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22460+ goto out_unpin; /* success */
1facf9fc 22461+ }
22462+
4a4d8108
AM
22463+ /* revert */
22464+ if (wh_dentry) {
22465+ int rerr;
22466+
c2b27bf2
AM
22467+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22468+ &a->dt);
4a4d8108
AM
22469+ if (rerr)
22470+ err = rerr;
dece6358 22471+ }
1facf9fc 22472+
027c5e7a 22473+out_unpin:
c2b27bf2 22474+ au_unpin(&a->pin);
4a4d8108 22475+ dput(wh_dentry);
c2b27bf2 22476+ dput(a->h_path.dentry);
027c5e7a 22477+out_parent:
4a4d8108 22478+ di_write_unlock(parent);
027c5e7a 22479+out_unlock:
4a4d8108 22480+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22481+out_free:
9f237c51 22482+ au_kfree_rcu(a);
027c5e7a 22483+out:
4a4d8108 22484+ return err;
dece6358
AM
22485+}
22486+
4a4d8108 22487+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22488+{
4a4d8108 22489+ int err, rmdir_later;
5afbbe0d 22490+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22491+ struct inode *inode;
22492+ struct dentry *parent, *wh_dentry, *h_dentry;
22493+ struct au_whtmp_rmdir *args;
acd2b654 22494+ /* to reduce stack size */
c2b27bf2
AM
22495+ struct {
22496+ struct au_dtime dt;
22497+ struct au_pin pin;
22498+ } *a;
1facf9fc 22499+
4a4d8108 22500+ IMustLock(dir);
027c5e7a 22501+
c2b27bf2
AM
22502+ err = -ENOMEM;
22503+ a = kmalloc(sizeof(*a), GFP_NOFS);
22504+ if (unlikely(!a))
22505+ goto out;
22506+
027c5e7a
AM
22507+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22508+ if (unlikely(err))
c2b27bf2 22509+ goto out_free;
53392da6
AM
22510+ err = au_alive_dir(dentry);
22511+ if (unlikely(err))
027c5e7a 22512+ goto out_unlock;
5527c038 22513+ inode = d_inode(dentry);
4a4d8108 22514+ IMustLock(inode);
027c5e7a 22515+ err = -ENOTDIR;
2000de60 22516+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22517+ goto out_unlock; /* possible? */
dece6358 22518+
4a4d8108
AM
22519+ err = -ENOMEM;
22520+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22521+ if (unlikely(!args))
22522+ goto out_unlock;
dece6358 22523+
4a4d8108
AM
22524+ parent = dentry->d_parent; /* dir inode is locked */
22525+ di_write_lock_parent(parent);
22526+ err = au_test_empty(dentry, &args->whlist);
22527+ if (unlikely(err))
027c5e7a 22528+ goto out_parent;
1facf9fc 22529+
5afbbe0d 22530+ btop = au_dbtop(dentry);
4a4d8108
AM
22531+ bwh = au_dbwh(dentry);
22532+ bindex = -1;
c2b27bf2
AM
22533+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22534+ &a->pin);
4a4d8108
AM
22535+ err = PTR_ERR(wh_dentry);
22536+ if (IS_ERR(wh_dentry))
027c5e7a 22537+ goto out_parent;
1facf9fc 22538+
5afbbe0d 22539+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22540+ dget(h_dentry);
22541+ rmdir_later = 0;
5afbbe0d
AM
22542+ if (bindex == btop) {
22543+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22544+ if (err > 0) {
22545+ rmdir_later = err;
22546+ err = 0;
22547+ }
22548+ } else {
22549+ /* stop monitoring */
5afbbe0d 22550+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22551+
22552+ /* dir inode is locked */
5527c038 22553+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22554+ err = 0;
22555+ }
22556+
4a4d8108 22557+ if (!err) {
027c5e7a 22558+ vfsub_dead_dir(inode);
4a4d8108
AM
22559+ au_set_dbdiropq(dentry, -1);
22560+ epilog(dir, dentry, bindex);
1308ab2a 22561+
4a4d8108 22562+ if (rmdir_later) {
5afbbe0d 22563+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22564+ args = NULL;
22565+ }
1308ab2a 22566+
4a4d8108 22567+ goto out_unpin; /* success */
1facf9fc 22568+ }
22569+
4a4d8108
AM
22570+ /* revert */
22571+ AuLabel(revert);
22572+ if (wh_dentry) {
22573+ int rerr;
1308ab2a 22574+
c2b27bf2
AM
22575+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22576+ &a->dt);
4a4d8108
AM
22577+ if (rerr)
22578+ err = rerr;
1facf9fc 22579+ }
22580+
4f0767ce 22581+out_unpin:
c2b27bf2 22582+ au_unpin(&a->pin);
4a4d8108
AM
22583+ dput(wh_dentry);
22584+ dput(h_dentry);
027c5e7a 22585+out_parent:
4a4d8108
AM
22586+ di_write_unlock(parent);
22587+ if (args)
22588+ au_whtmp_rmdir_free(args);
4f0767ce 22589+out_unlock:
4a4d8108 22590+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22591+out_free:
9f237c51 22592+ au_kfree_rcu(a);
4f0767ce 22593+out:
4a4d8108
AM
22594+ AuTraceErr(err);
22595+ return err;
dece6358 22596+}
7f207e10 22597diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
eca34b5c
AM
22598--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22599+++ linux/fs/aufs/i_op_ren.c 2019-07-11 15:42:14.468904634 +0200
eca801bf 22600@@ -0,0 +1,1250 @@
cd7a4cd9 22601+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22602+/*
ba1aed25 22603+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 22604+ *
22605+ * This program, aufs is free software; you can redistribute it and/or modify
22606+ * it under the terms of the GNU General Public License as published by
22607+ * the Free Software Foundation; either version 2 of the License, or
22608+ * (at your option) any later version.
dece6358
AM
22609+ *
22610+ * This program is distributed in the hope that it will be useful,
22611+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22612+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22613+ * GNU General Public License for more details.
22614+ *
22615+ * You should have received a copy of the GNU General Public License
523b37e3 22616+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22617+ */
22618+
22619+/*
4a4d8108
AM
22620+ * inode operation (rename entry)
22621+ * todo: this is crazy monster
1facf9fc 22622+ */
22623+
eca801bf 22624+#include <linux/iversion.h>
1facf9fc 22625+#include "aufs.h"
22626+
4a4d8108
AM
22627+enum { AuSRC, AuDST, AuSrcDst };
22628+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22629+
f2c43d5f
AM
22630+#define AuRen_ISDIR_SRC 1
22631+#define AuRen_ISDIR_DST (1 << 1)
22632+#define AuRen_ISSAMEDIR (1 << 2)
22633+#define AuRen_WHSRC (1 << 3)
22634+#define AuRen_WHDST (1 << 4)
22635+#define AuRen_MNT_WRITE (1 << 5)
22636+#define AuRen_DT_DSTDIR (1 << 6)
22637+#define AuRen_DIROPQ_SRC (1 << 7)
22638+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22639+#define AuRen_DIRREN (1 << 9)
22640+#define AuRen_DROPPED_SRC (1 << 10)
22641+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22642+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22643+#define au_fset_ren(flags, name) \
22644+ do { (flags) |= AuRen_##name; } while (0)
22645+#define au_fclr_ren(flags, name) \
22646+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22647+
8b6a4947
AM
22648+#ifndef CONFIG_AUFS_DIRREN
22649+#undef AuRen_DIRREN
22650+#define AuRen_DIRREN 0
22651+#endif
22652+
4a4d8108
AM
22653+struct au_ren_args {
22654+ struct {
22655+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22656+ *wh_dentry;
22657+ struct inode *dir, *inode;
f2c43d5f 22658+ struct au_hinode *hdir, *hinode;
4a4d8108 22659+ struct au_dtime dt[AuParentChild];
f2c43d5f 22660+ aufs_bindex_t btop, bdiropq;
4a4d8108 22661+ } sd[AuSrcDst];
1facf9fc 22662+
4a4d8108
AM
22663+#define src_dentry sd[AuSRC].dentry
22664+#define src_dir sd[AuSRC].dir
22665+#define src_inode sd[AuSRC].inode
22666+#define src_h_dentry sd[AuSRC].h_dentry
22667+#define src_parent sd[AuSRC].parent
22668+#define src_h_parent sd[AuSRC].h_parent
22669+#define src_wh_dentry sd[AuSRC].wh_dentry
22670+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22671+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22672+#define src_h_dir sd[AuSRC].hdir->hi_inode
22673+#define src_dt sd[AuSRC].dt
5afbbe0d 22674+#define src_btop sd[AuSRC].btop
f2c43d5f 22675+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22676+
4a4d8108
AM
22677+#define dst_dentry sd[AuDST].dentry
22678+#define dst_dir sd[AuDST].dir
22679+#define dst_inode sd[AuDST].inode
22680+#define dst_h_dentry sd[AuDST].h_dentry
22681+#define dst_parent sd[AuDST].parent
22682+#define dst_h_parent sd[AuDST].h_parent
22683+#define dst_wh_dentry sd[AuDST].wh_dentry
22684+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22685+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22686+#define dst_h_dir sd[AuDST].hdir->hi_inode
22687+#define dst_dt sd[AuDST].dt
5afbbe0d 22688+#define dst_btop sd[AuDST].btop
f2c43d5f 22689+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22690+
22691+ struct dentry *h_trap;
22692+ struct au_branch *br;
4a4d8108
AM
22693+ struct path h_path;
22694+ struct au_nhash whlist;
f2c43d5f 22695+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22696+
f2c43d5f
AM
22697+ struct {
22698+ unsigned short auren_flags;
22699+ unsigned char flags; /* syscall parameter */
22700+ unsigned char exchange;
22701+ } __packed;
1facf9fc 22702+
4a4d8108
AM
22703+ struct au_whtmp_rmdir *thargs;
22704+ struct dentry *h_dst;
8b6a4947 22705+ struct au_hinode *h_root;
4a4d8108 22706+};
1308ab2a 22707+
4a4d8108 22708+/* ---------------------------------------------------------------------- */
1308ab2a 22709+
4a4d8108
AM
22710+/*
22711+ * functions for reverting.
22712+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22713+ * everything as if nothing happened.
4a4d8108
AM
22714+ * we don't need to revert the copied-up/down the parent dir since they are
22715+ * harmless.
22716+ */
1facf9fc 22717+
4a4d8108
AM
22718+#define RevertFailure(fmt, ...) do { \
22719+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22720+ ##__VA_ARGS__, err, rerr); \
22721+ err = -EIO; \
22722+} while (0)
1facf9fc 22723+
f2c43d5f 22724+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22725+{
4a4d8108 22726+ int rerr;
f2c43d5f
AM
22727+ struct dentry *d;
22728+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22729+
f2c43d5f
AM
22730+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22731+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22732+ rerr = au_diropq_remove(d, a->btgt);
22733+ au_hn_inode_unlock(src_or_dst(hinode));
22734+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22735+ if (rerr)
f2c43d5f
AM
22736+ RevertFailure("remove diropq %pd", d);
22737+
22738+#undef src_or_dst_
22739+}
22740+
22741+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22742+{
22743+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22744+ au_ren_do_rev_diropq(err, a, AuSRC);
22745+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22746+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22747+}
1facf9fc 22748+
4a4d8108
AM
22749+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22750+{
22751+ int rerr;
523b37e3 22752+ struct inode *delegated;
1facf9fc 22753+
b4510431
AM
22754+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22755+ a->src_h_parent);
4a4d8108
AM
22756+ rerr = PTR_ERR(a->h_path.dentry);
22757+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22758+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22759+ return;
1facf9fc 22760+ }
22761+
523b37e3 22762+ delegated = NULL;
4a4d8108
AM
22763+ rerr = vfsub_rename(a->dst_h_dir,
22764+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22765+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22766+ if (unlikely(rerr == -EWOULDBLOCK)) {
22767+ pr_warn("cannot retry for NFSv4 delegation"
22768+ " for an internal rename\n");
22769+ iput(delegated);
22770+ }
4a4d8108
AM
22771+ d_drop(a->h_path.dentry);
22772+ dput(a->h_path.dentry);
22773+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22774+ if (rerr)
523b37e3 22775+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22776+}
22777+
4a4d8108 22778+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22779+{
4a4d8108 22780+ int rerr;
523b37e3 22781+ struct inode *delegated;
dece6358 22782+
b4510431
AM
22783+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22784+ a->dst_h_parent);
4a4d8108
AM
22785+ rerr = PTR_ERR(a->h_path.dentry);
22786+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22787+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22788+ return;
22789+ }
5527c038 22790+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22791+ d_drop(a->h_path.dentry);
22792+ dput(a->h_path.dentry);
22793+ return;
dece6358
AM
22794+ }
22795+
523b37e3
AM
22796+ delegated = NULL;
22797+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22798+ &delegated, a->flags);
523b37e3
AM
22799+ if (unlikely(rerr == -EWOULDBLOCK)) {
22800+ pr_warn("cannot retry for NFSv4 delegation"
22801+ " for an internal rename\n");
22802+ iput(delegated);
22803+ }
4a4d8108
AM
22804+ d_drop(a->h_path.dentry);
22805+ dput(a->h_path.dentry);
22806+ if (!rerr)
22807+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22808+ else
523b37e3 22809+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22810+}
1308ab2a 22811+
4a4d8108
AM
22812+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22813+{
22814+ int rerr;
1308ab2a 22815+
4a4d8108
AM
22816+ a->h_path.dentry = a->src_wh_dentry;
22817+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22818+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22819+ if (rerr)
523b37e3 22820+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22821+}
4a4d8108 22822+#undef RevertFailure
1facf9fc 22823+
1308ab2a 22824+/* ---------------------------------------------------------------------- */
22825+
4a4d8108
AM
22826+/*
22827+ * when we have to copyup the renaming entry, do it with the rename-target name
22828+ * in order to minimize the cost (the later actual rename is unnecessary).
22829+ * otherwise rename it on the target branch.
22830+ */
22831+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22832+{
dece6358 22833+ int err;
4a4d8108 22834+ struct dentry *d;
523b37e3 22835+ struct inode *delegated;
1facf9fc 22836+
4a4d8108 22837+ d = a->src_dentry;
5afbbe0d 22838+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22839+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22840+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22841+ delegated = NULL;
4a4d8108 22842+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22843+ a->dst_h_dir, &a->h_path, &delegated,
22844+ a->flags);
523b37e3
AM
22845+ if (unlikely(err == -EWOULDBLOCK)) {
22846+ pr_warn("cannot retry for NFSv4 delegation"
22847+ " for an internal rename\n");
22848+ iput(delegated);
22849+ }
c2b27bf2 22850+ } else
86dc4139 22851+ BUG();
1308ab2a 22852+
027c5e7a
AM
22853+ if (!err && a->h_dst)
22854+ /* it will be set to dinfo later */
22855+ dget(a->h_dst);
1facf9fc 22856+
dece6358
AM
22857+ return err;
22858+}
1facf9fc 22859+
4a4d8108
AM
22860+/* cf. aufs_rmdir() */
22861+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22862+{
4a4d8108
AM
22863+ int err;
22864+ struct inode *dir;
1facf9fc 22865+
4a4d8108
AM
22866+ dir = a->dst_dir;
22867+ SiMustAnyLock(dir->i_sb);
22868+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22869+ au_sbi(dir->i_sb)->si_dirwh)
22870+ || au_test_fs_remote(a->h_dst->d_sb)) {
22871+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22872+ if (unlikely(err))
523b37e3
AM
22873+ pr_warn("failed removing whtmp dir %pd (%d), "
22874+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22875+ } else {
22876+ au_nhash_wh_free(&a->thargs->whlist);
22877+ a->thargs->whlist = a->whlist;
22878+ a->whlist.nh_num = 0;
22879+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22880+ dput(a->h_dst);
22881+ a->thargs = NULL;
22882+ }
22883+
22884+ return 0;
1308ab2a 22885+}
1facf9fc 22886+
4a4d8108 22887+/* make it 'opaque' dir. */
f2c43d5f 22888+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22889+{
22890+ int err;
f2c43d5f
AM
22891+ struct dentry *d, *diropq;
22892+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22893+
4a4d8108 22894+ err = 0;
f2c43d5f
AM
22895+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22896+ src_or_dst(bdiropq) = au_dbdiropq(d);
22897+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22898+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22899+ diropq = au_diropq_create(d, a->btgt);
22900+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22901+ if (IS_ERR(diropq))
22902+ err = PTR_ERR(diropq);
076b876e
AM
22903+ else
22904+ dput(diropq);
1facf9fc 22905+
f2c43d5f 22906+#undef src_or_dst_
4a4d8108
AM
22907+ return err;
22908+}
1facf9fc 22909+
f2c43d5f 22910+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
22911+{
22912+ int err;
f2c43d5f
AM
22913+ unsigned char always;
22914+ struct dentry *d;
1facf9fc 22915+
f2c43d5f
AM
22916+ err = 0;
22917+ d = a->dst_dentry; /* already renamed on the branch */
22918+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22919+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 22920+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
22921+ && a->btgt != au_dbdiropq(a->src_dentry)
22922+ && (a->dst_wh_dentry
22923+ || a->btgt <= au_dbdiropq(d)
22924+ /* hide the lower to keep xino */
22925+ /* the lowers may not be a dir, but we hide them anyway */
22926+ || a->btgt < au_dbbot(d)
22927+ || always)) {
22928+ AuDbg("here\n");
22929+ err = au_ren_do_diropq(a, AuSRC);
22930+ if (unlikely(err))
4a4d8108 22931+ goto out;
f2c43d5f 22932+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 22933+ }
f2c43d5f
AM
22934+ if (!a->exchange)
22935+ goto out; /* success */
1facf9fc 22936+
f2c43d5f
AM
22937+ d = a->src_dentry; /* already renamed on the branch */
22938+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22939+ && a->btgt != au_dbdiropq(a->dst_dentry)
22940+ && (a->btgt < au_dbdiropq(d)
22941+ || a->btgt < au_dbbot(d)
22942+ || always)) {
22943+ AuDbgDentry(a->src_dentry);
22944+ AuDbgDentry(a->dst_dentry);
22945+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 22946+ if (unlikely(err))
f2c43d5f
AM
22947+ goto out_rev_src;
22948+ au_fset_ren(a->auren_flags, DIROPQ_DST);
22949+ }
22950+ goto out; /* success */
dece6358 22951+
f2c43d5f
AM
22952+out_rev_src:
22953+ AuDbg("err %d, reverting src\n", err);
22954+ au_ren_rev_diropq(err, a);
22955+out:
22956+ return err;
22957+}
22958+
22959+static int do_rename(struct au_ren_args *a)
22960+{
22961+ int err;
22962+ struct dentry *d, *h_d;
22963+
22964+ if (!a->exchange) {
22965+ /* prepare workqueue args for asynchronous rmdir */
22966+ h_d = a->dst_h_dentry;
22967+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 22968+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
22969+ && d_is_positive(h_d)) {
22970+ err = -ENOMEM;
22971+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22972+ GFP_NOFS);
22973+ if (unlikely(!a->thargs))
22974+ goto out;
22975+ a->h_dst = dget(h_d);
22976+ }
22977+
22978+ /* create whiteout for src_dentry */
22979+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
22980+ a->src_bwh = au_dbwh(a->src_dentry);
22981+ AuDebugOn(a->src_bwh >= 0);
22982+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22983+ a->src_h_parent);
22984+ err = PTR_ERR(a->src_wh_dentry);
22985+ if (IS_ERR(a->src_wh_dentry))
22986+ goto out_thargs;
22987+ }
22988+
22989+ /* lookup whiteout for dentry */
22990+ if (au_ftest_ren(a->auren_flags, WHDST)) {
22991+ h_d = au_wh_lkup(a->dst_h_parent,
22992+ &a->dst_dentry->d_name, a->br);
22993+ err = PTR_ERR(h_d);
22994+ if (IS_ERR(h_d))
22995+ goto out_whsrc;
22996+ if (d_is_negative(h_d))
22997+ dput(h_d);
22998+ else
22999+ a->dst_wh_dentry = h_d;
23000+ }
23001+
23002+ /* rename dentry to tmpwh */
23003+ if (a->thargs) {
23004+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
23005+ if (unlikely(err))
23006+ goto out_whdst;
23007+
23008+ d = a->dst_dentry;
23009+ au_set_h_dptr(d, a->btgt, NULL);
23010+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
23011+ if (unlikely(err))
23012+ goto out_whtmp;
23013+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
23014+ }
4a4d8108 23015+ }
1facf9fc 23016+
5afbbe0d 23017+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
8b6a4947
AM
23018+#if 0
23019+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23020+ && d_is_positive(a->dst_h_dentry)
23021+ && a->src_btop != a->btgt);
23022+#endif
1facf9fc 23023+
4a4d8108 23024+ /* rename by vfs_rename or cpup */
4a4d8108
AM
23025+ err = au_ren_or_cpup(a);
23026+ if (unlikely(err))
23027+ /* leave the copied-up one */
23028+ goto out_whtmp;
1308ab2a 23029+
4a4d8108 23030+ /* make dir opaque */
f2c43d5f
AM
23031+ err = au_ren_diropq(a);
23032+ if (unlikely(err))
23033+ goto out_rename;
1308ab2a 23034+
4a4d8108 23035+ /* update target timestamps */
f2c43d5f
AM
23036+ if (a->exchange) {
23037+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23038+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23039+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23040+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23041+ }
5afbbe0d 23042+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
23043+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23044+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 23045+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 23046+
f2c43d5f
AM
23047+ if (!a->exchange) {
23048+ /* remove whiteout for dentry */
23049+ if (a->dst_wh_dentry) {
23050+ a->h_path.dentry = a->dst_wh_dentry;
23051+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23052+ a->dst_dentry);
23053+ if (unlikely(err))
23054+ goto out_diropq;
23055+ }
1facf9fc 23056+
f2c43d5f
AM
23057+ /* remove whtmp */
23058+ if (a->thargs)
23059+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 23060+
f2c43d5f
AM
23061+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23062+ }
4a4d8108
AM
23063+ err = 0;
23064+ goto out_success;
23065+
4f0767ce 23066+out_diropq:
f2c43d5f 23067+ au_ren_rev_diropq(err, a);
4f0767ce 23068+out_rename:
7e9cd9fe 23069+ au_ren_rev_rename(err, a);
027c5e7a 23070+ dput(a->h_dst);
4f0767ce 23071+out_whtmp:
4a4d8108
AM
23072+ if (a->thargs)
23073+ au_ren_rev_whtmp(err, a);
4f0767ce 23074+out_whdst:
4a4d8108
AM
23075+ dput(a->dst_wh_dentry);
23076+ a->dst_wh_dentry = NULL;
4f0767ce 23077+out_whsrc:
4a4d8108
AM
23078+ if (a->src_wh_dentry)
23079+ au_ren_rev_whsrc(err, a);
4f0767ce 23080+out_success:
4a4d8108
AM
23081+ dput(a->src_wh_dentry);
23082+ dput(a->dst_wh_dentry);
4f0767ce 23083+out_thargs:
4a4d8108
AM
23084+ if (a->thargs) {
23085+ dput(a->h_dst);
23086+ au_whtmp_rmdir_free(a->thargs);
23087+ a->thargs = NULL;
23088+ }
4f0767ce 23089+out:
4a4d8108 23090+ return err;
dece6358 23091+}
1facf9fc 23092+
1308ab2a 23093+/* ---------------------------------------------------------------------- */
1facf9fc 23094+
4a4d8108
AM
23095+/*
23096+ * test if @dentry dir can be rename destination or not.
23097+ * success means, it is a logically empty dir.
23098+ */
23099+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 23100+{
4a4d8108 23101+ return au_test_empty(dentry, whlist);
1308ab2a 23102+}
1facf9fc 23103+
4a4d8108 23104+/*
8b6a4947
AM
23105+ * test if @a->src_dentry dir can be rename source or not.
23106+ * if it can, return 0.
4a4d8108
AM
23107+ * success means,
23108+ * - it is a logically empty dir.
23109+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 23110+ * on the lower branch unless DIRREN is on.
4a4d8108 23111+ */
8b6a4947 23112+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
23113+{
23114+ int err;
23115+ unsigned int rdhash;
8b6a4947
AM
23116+ aufs_bindex_t btop, btgt;
23117+ struct dentry *dentry;
23118+ struct super_block *sb;
23119+ struct au_sbinfo *sbinfo;
1facf9fc 23120+
8b6a4947
AM
23121+ dentry = a->src_dentry;
23122+ sb = dentry->d_sb;
23123+ sbinfo = au_sbi(sb);
23124+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23125+ au_fset_ren(a->auren_flags, DIRREN);
23126+
23127+ btgt = a->btgt;
5afbbe0d
AM
23128+ btop = au_dbtop(dentry);
23129+ if (btop != btgt) {
4a4d8108 23130+ struct au_nhash whlist;
dece6358 23131+
8b6a4947
AM
23132+ SiMustAnyLock(sb);
23133+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23134+ if (!rdhash)
23135+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23136+ dentry));
23137+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23138+ if (unlikely(err))
23139+ goto out;
23140+ err = au_test_empty(dentry, &whlist);
23141+ au_nhash_wh_free(&whlist);
23142+ goto out;
23143+ }
dece6358 23144+
5afbbe0d 23145+ if (btop == au_dbtaildir(dentry))
4a4d8108 23146+ return 0; /* success */
dece6358 23147+
4a4d8108 23148+ err = au_test_empty_lower(dentry);
1facf9fc 23149+
4f0767ce 23150+out:
4a4d8108 23151+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23152+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23153+ err = 0;
23154+ } else {
23155+ AuWarn1("renaming dir who has child(ren) on multiple "
23156+ "branches, is not supported\n");
23157+ err = -EXDEV;
23158+ }
4a4d8108
AM
23159+ }
23160+ return err;
23161+}
1308ab2a 23162+
4a4d8108
AM
23163+/* side effect: sets whlist and h_dentry */
23164+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23165+{
4a4d8108
AM
23166+ int err;
23167+ unsigned int rdhash;
23168+ struct dentry *d;
1facf9fc 23169+
4a4d8108
AM
23170+ d = a->dst_dentry;
23171+ SiMustAnyLock(d->d_sb);
1facf9fc 23172+
4a4d8108 23173+ err = 0;
f2c43d5f 23174+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23175+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23176+ if (!rdhash)
23177+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23178+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23179+ if (unlikely(err))
23180+ goto out;
1308ab2a 23181+
f2c43d5f
AM
23182+ if (!a->exchange) {
23183+ au_set_dbtop(d, a->dst_btop);
23184+ err = may_rename_dstdir(d, &a->whlist);
23185+ au_set_dbtop(d, a->btgt);
23186+ } else
8b6a4947 23187+ err = may_rename_srcdir(a);
4a4d8108 23188+ }
5afbbe0d 23189+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23190+ if (unlikely(err))
23191+ goto out;
23192+
23193+ d = a->src_dentry;
5afbbe0d 23194+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23195+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23196+ err = may_rename_srcdir(a);
4a4d8108
AM
23197+ if (unlikely(err)) {
23198+ au_nhash_wh_free(&a->whlist);
23199+ a->whlist.nh_num = 0;
23200+ }
23201+ }
4f0767ce 23202+out:
4a4d8108 23203+ return err;
1facf9fc 23204+}
23205+
4a4d8108 23206+/* ---------------------------------------------------------------------- */
1facf9fc 23207+
4a4d8108
AM
23208+/*
23209+ * simple tests for rename.
23210+ * following the checks in vfs, plus the parent-child relationship.
23211+ */
23212+static int au_may_ren(struct au_ren_args *a)
23213+{
23214+ int err, isdir;
23215+ struct inode *h_inode;
1facf9fc 23216+
5afbbe0d 23217+ if (a->src_btop == a->btgt) {
4a4d8108 23218+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23219+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23220+ if (unlikely(err))
23221+ goto out;
23222+ err = -EINVAL;
23223+ if (unlikely(a->src_h_dentry == a->h_trap))
23224+ goto out;
23225+ }
1facf9fc 23226+
4a4d8108 23227+ err = 0;
5afbbe0d 23228+ if (a->dst_btop != a->btgt)
4a4d8108 23229+ goto out;
1facf9fc 23230+
027c5e7a
AM
23231+ err = -ENOTEMPTY;
23232+ if (unlikely(a->dst_h_dentry == a->h_trap))
23233+ goto out;
23234+
4a4d8108 23235+ err = -EIO;
f2c43d5f 23236+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23237+ if (d_really_is_negative(a->dst_dentry)) {
23238+ if (d_is_negative(a->dst_h_dentry))
23239+ err = au_may_add(a->dst_dentry, a->btgt,
23240+ a->dst_h_parent, isdir);
4a4d8108 23241+ } else {
5527c038 23242+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23243+ goto out;
5527c038
JR
23244+ h_inode = d_inode(a->dst_h_dentry);
23245+ if (h_inode->i_nlink)
23246+ err = au_may_del(a->dst_dentry, a->btgt,
23247+ a->dst_h_parent, isdir);
4a4d8108 23248+ }
1facf9fc 23249+
4f0767ce 23250+out:
4a4d8108
AM
23251+ if (unlikely(err == -ENOENT || err == -EEXIST))
23252+ err = -EIO;
23253+ AuTraceErr(err);
23254+ return err;
23255+}
1facf9fc 23256+
1308ab2a 23257+/* ---------------------------------------------------------------------- */
1facf9fc 23258+
4a4d8108
AM
23259+/*
23260+ * locking order
23261+ * (VFS)
23262+ * - src_dir and dir by lock_rename()
acd2b654 23263+ * - inode if exists
4a4d8108
AM
23264+ * (aufs)
23265+ * - lock all
23266+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23267+ * + si_read_lock
23268+ * + di_write_lock2_child()
23269+ * + di_write_lock_child()
23270+ * + ii_write_lock_child()
23271+ * + di_write_lock_child2()
23272+ * + ii_write_lock_child2()
23273+ * + src_parent and parent
23274+ * + di_write_lock_parent()
23275+ * + ii_write_lock_parent()
23276+ * + di_write_lock_parent2()
23277+ * + ii_write_lock_parent2()
23278+ * + lower src_dir and dir by vfsub_lock_rename()
23279+ * + verify the every relationships between child and parent. if any
23280+ * of them failed, unlock all and return -EBUSY.
23281+ */
23282+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23283+{
4a4d8108
AM
23284+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23285+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23286+ if (au_ftest_ren(a->auren_flags, DIRREN)
23287+ && a->h_root)
23288+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23289+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23290+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23291+}
23292+
4a4d8108 23293+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23294+{
4a4d8108
AM
23295+ int err;
23296+ unsigned int udba;
1308ab2a 23297+
4a4d8108
AM
23298+ err = 0;
23299+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23300+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23301+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23302+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23303+
23304+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23305+ if (unlikely(err))
23306+ goto out;
f2c43d5f 23307+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23308+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23309+ struct dentry *root;
23310+ struct inode *dir;
23311+
23312+ /*
23313+ * sbinfo is already locked, so this ii_read_lock is
23314+ * unnecessary. but our debugging feature checks it.
23315+ */
23316+ root = a->src_inode->i_sb->s_root;
23317+ if (root != a->src_parent && root != a->dst_parent) {
23318+ dir = d_inode(root);
23319+ ii_read_lock_parent3(dir);
23320+ a->h_root = au_hi(dir, a->btgt);
23321+ ii_read_unlock(dir);
23322+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23323+ }
23324+ }
4a4d8108
AM
23325+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23326+ a->dst_h_parent, a->dst_hdir);
23327+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23328+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23329+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23330+ err = au_busy_or_stale();
5afbbe0d 23331+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23332+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23333+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23334+ a->br);
5afbbe0d 23335+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23336+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23337+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23338+ a->br);
86dc4139 23339+ if (!err)
4a4d8108 23340+ goto out; /* success */
4a4d8108
AM
23341+
23342+ err = au_busy_or_stale();
4a4d8108 23343+ au_ren_unlock(a);
86dc4139 23344+
4f0767ce 23345+out:
4a4d8108 23346+ return err;
1facf9fc 23347+}
23348+
23349+/* ---------------------------------------------------------------------- */
23350+
4a4d8108 23351+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23352+{
4a4d8108 23353+ struct inode *dir;
dece6358 23354+
4a4d8108 23355+ dir = a->dst_dir;
be118d29 23356+ inode_inc_iversion(dir);
f2c43d5f 23357+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23358+ /* is this updating defined in POSIX? */
23359+ au_cpup_attr_timesizes(a->src_inode);
23360+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23361+ }
b912730e 23362+ au_dir_ts(dir, a->btgt);
dece6358 23363+
f2c43d5f
AM
23364+ if (a->exchange) {
23365+ dir = a->src_dir;
be118d29 23366+ inode_inc_iversion(dir);
f2c43d5f
AM
23367+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23368+ /* is this updating defined in POSIX? */
23369+ au_cpup_attr_timesizes(a->dst_inode);
23370+ au_cpup_attr_nlink(dir, /*force*/1);
23371+ }
23372+ au_dir_ts(dir, a->btgt);
23373+ }
23374+
23375+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23376+ return;
dece6358 23377+
4a4d8108 23378+ dir = a->src_dir;
be118d29 23379+ inode_inc_iversion(dir);
f2c43d5f 23380+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23381+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23382+ au_dir_ts(dir, a->btgt);
1facf9fc 23383+}
23384+
4a4d8108 23385+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23386+{
5afbbe0d 23387+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23388+ struct dentry *d, *h_d;
23389+ struct inode *i, *h_i;
23390+ struct super_block *sb;
dece6358 23391+
027c5e7a
AM
23392+ d = a->dst_dentry;
23393+ d_drop(d);
23394+ if (a->h_dst)
23395+ /* already dget-ed by au_ren_or_cpup() */
23396+ au_set_h_dptr(d, a->btgt, a->h_dst);
23397+
23398+ i = a->dst_inode;
23399+ if (i) {
f2c43d5f
AM
23400+ if (!a->exchange) {
23401+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23402+ vfsub_drop_nlink(i);
23403+ else {
23404+ vfsub_dead_dir(i);
23405+ au_cpup_attr_timesizes(i);
23406+ }
23407+ au_update_dbrange(d, /*do_put_zero*/1);
23408+ } else
23409+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23410+ } else {
5afbbe0d
AM
23411+ bbot = a->btgt;
23412+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23413+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23414+ bbot = au_dbbot(d);
23415+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23416+ au_set_h_dptr(d, bindex, NULL);
23417+ au_update_dbrange(d, /*do_put_zero*/0);
23418+ }
23419+
8b6a4947
AM
23420+ if (a->exchange
23421+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23422+ d_drop(a->src_dentry);
23423+ if (au_ftest_ren(a->auren_flags, DIRREN))
23424+ au_set_dbwh(a->src_dentry, -1);
23425+ return;
23426+ }
23427+
4a4d8108 23428+ d = a->src_dentry;
8b6a4947
AM
23429+ au_set_dbwh(d, -1);
23430+ bbot = au_dbbot(d);
23431+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23432+ h_d = au_h_dptr(d, bindex);
23433+ if (h_d)
23434+ au_set_h_dptr(d, bindex, NULL);
23435+ }
23436+ au_set_dbbot(d, a->btgt);
4a4d8108 23437+
8b6a4947
AM
23438+ sb = d->d_sb;
23439+ i = a->src_inode;
23440+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23441+ return; /* success */
4a4d8108 23442+
8b6a4947
AM
23443+ bbot = au_ibbot(i);
23444+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23445+ h_i = au_h_iptr(i, bindex);
23446+ if (h_i) {
23447+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23448+ /* ignore this error */
23449+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23450+ }
23451+ }
8b6a4947 23452+ au_set_ibbot(i, a->btgt);
1308ab2a 23453+}
dece6358 23454+
4a4d8108
AM
23455+/* ---------------------------------------------------------------------- */
23456+
23457+/* mainly for link(2) and rename(2) */
23458+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23459+{
4a4d8108
AM
23460+ aufs_bindex_t bdiropq, bwh;
23461+ struct dentry *parent;
23462+ struct au_branch *br;
23463+
23464+ parent = dentry->d_parent;
5527c038 23465+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23466+
23467+ bdiropq = au_dbdiropq(parent);
23468+ bwh = au_dbwh(dentry);
23469+ br = au_sbr(dentry->d_sb, btgt);
23470+ if (au_br_rdonly(br)
23471+ || (0 <= bdiropq && bdiropq < btgt)
23472+ || (0 <= bwh && bwh < btgt))
23473+ btgt = -1;
23474+
23475+ AuDbg("btgt %d\n", btgt);
23476+ return btgt;
1facf9fc 23477+}
23478+
5afbbe0d 23479+/* sets src_btop, dst_btop and btgt */
4a4d8108 23480+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23481+{
4a4d8108
AM
23482+ int err;
23483+ struct au_wr_dir_args wr_dir_args = {
23484+ /* .force_btgt = -1, */
23485+ .flags = AuWrDir_ADD_ENTRY
23486+ };
dece6358 23487+
5afbbe0d
AM
23488+ a->src_btop = au_dbtop(a->src_dentry);
23489+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23490+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23491+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23492+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23493+ wr_dir_args.force_btgt = a->src_btop;
23494+ if (a->dst_inode && a->dst_btop < a->src_btop)
23495+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23496+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23497+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23498+ a->btgt = err;
f2c43d5f
AM
23499+ if (a->exchange)
23500+ au_update_dbtop(a->dst_dentry);
dece6358 23501+
4a4d8108 23502+ return err;
1facf9fc 23503+}
23504+
4a4d8108 23505+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23506+{
4a4d8108
AM
23507+ a->h_path.dentry = a->src_h_parent;
23508+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23509+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23510+ a->h_path.dentry = a->dst_h_parent;
23511+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23512+ }
1facf9fc 23513+
f2c43d5f
AM
23514+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23515+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23516+ && !a->exchange)
4a4d8108 23517+ return;
dece6358 23518+
4a4d8108
AM
23519+ a->h_path.dentry = a->src_h_dentry;
23520+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23521+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23522+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23523+ a->h_path.dentry = a->dst_h_dentry;
23524+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23525+ }
1308ab2a 23526+}
dece6358 23527+
4a4d8108 23528+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23529+{
4a4d8108 23530+ struct dentry *h_d;
febd17d6 23531+ struct inode *h_inode;
4a4d8108
AM
23532+
23533+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23534+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23535+ au_dtime_revert(a->dst_dt + AuPARENT);
23536+
f2c43d5f 23537+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23538+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23539+ h_inode = d_inode(h_d);
23540+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23541+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23542+ inode_unlock(h_inode);
4a4d8108 23543+
f2c43d5f 23544+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23545+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23546+ h_inode = d_inode(h_d);
23547+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23548+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 23549+ inode_unlock(h_inode);
1facf9fc 23550+ }
23551+ }
23552+}
23553+
4a4d8108
AM
23554+/* ---------------------------------------------------------------------- */
23555+
23556+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23557+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23558+ unsigned int _flags)
1facf9fc 23559+{
f2c43d5f 23560+ int err, lock_flags;
8b6a4947 23561+ void *rev;
4a4d8108
AM
23562+ /* reduce stack space */
23563+ struct au_ren_args *a;
f2c43d5f 23564+ struct au_pin pin;
4a4d8108 23565+
f2c43d5f 23566+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23567+ IMustLock(_src_dir);
23568+ IMustLock(_dst_dir);
23569+
f2c43d5f
AM
23570+ err = -EINVAL;
23571+ if (unlikely(_flags & RENAME_WHITEOUT))
23572+ goto out;
23573+
4a4d8108
AM
23574+ err = -ENOMEM;
23575+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23576+ a = kzalloc(sizeof(*a), GFP_NOFS);
23577+ if (unlikely(!a))
23578+ goto out;
23579+
f2c43d5f 23580+ a->flags = _flags;
acd2b654
AM
23581+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23582+ && RENAME_EXCHANGE > U8_MAX);
f2c43d5f 23583+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23584+ a->src_dir = _src_dir;
23585+ a->src_dentry = _src_dentry;
5527c038
JR
23586+ a->src_inode = NULL;
23587+ if (d_really_is_positive(a->src_dentry))
23588+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23589+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23590+ a->dst_dir = _dst_dir;
23591+ a->dst_dentry = _dst_dentry;
5527c038
JR
23592+ a->dst_inode = NULL;
23593+ if (d_really_is_positive(a->dst_dentry))
23594+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23595+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23596+ if (a->dst_inode) {
f2c43d5f
AM
23597+ /*
23598+ * if EXCHANGE && src is non-dir && dst is dir,
23599+ * dst is not locked.
23600+ */
23601+ /* IMustLock(a->dst_inode); */
4a4d8108 23602+ au_igrab(a->dst_inode);
1facf9fc 23603+ }
1facf9fc 23604+
4a4d8108 23605+ err = -ENOTDIR;
f2c43d5f 23606+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23607+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23608+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23609+ if (unlikely(!a->exchange
23610+ && d_really_is_positive(a->dst_dentry)
2000de60 23611+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23612+ goto out_free;
f2c43d5f
AM
23613+ lock_flags |= AuLock_DIRS;
23614+ }
23615+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23616+ au_fset_ren(a->auren_flags, ISDIR_DST);
23617+ if (unlikely(!a->exchange
23618+ && d_really_is_positive(a->src_dentry)
23619+ && !d_is_dir(a->src_dentry)))
23620+ goto out_free;
23621+ lock_flags |= AuLock_DIRS;
b95c5147 23622+ }
8b6a4947
AM
23623+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23624+ lock_flags);
e49829fe
JR
23625+ if (unlikely(err))
23626+ goto out_free;
1facf9fc 23627+
027c5e7a
AM
23628+ err = au_d_hashed_positive(a->src_dentry);
23629+ if (unlikely(err))
23630+ goto out_unlock;
23631+ err = -ENOENT;
23632+ if (a->dst_inode) {
23633+ /*
f2c43d5f 23634+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23635+ * function. It means we cannot rely upon d_unhashed().
23636+ */
23637+ if (unlikely(!a->dst_inode->i_nlink))
23638+ goto out_unlock;
f2c43d5f 23639+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23640+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23641+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23642+ goto out_unlock;
23643+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23644+ goto out_unlock;
23645+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23646+ goto out_unlock;
23647+
7eafdf33
AM
23648+ /*
23649+ * is it possible?
79b8bda9 23650+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23651+ * there may exist a problem somewhere else.
23652+ */
23653+ err = -EINVAL;
5527c038 23654+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23655+ goto out_unlock;
23656+
f2c43d5f 23657+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23658+ di_write_lock_parent(a->dst_parent);
1facf9fc 23659+
4a4d8108
AM
23660+ /* which branch we process */
23661+ err = au_ren_wbr(a);
23662+ if (unlikely(err < 0))
027c5e7a 23663+ goto out_parent;
4a4d8108 23664+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23665+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23666+
4a4d8108
AM
23667+ /* are they available to be renamed */
23668+ err = au_ren_may_dir(a);
23669+ if (unlikely(err))
23670+ goto out_children;
1facf9fc 23671+
4a4d8108 23672+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23673+ if (a->dst_btop == a->btgt) {
f2c43d5f 23674+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23675+ } else {
23676+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23677+ if (unlikely(err))
23678+ goto out_children;
23679+ }
1facf9fc 23680+
f2c43d5f
AM
23681+ err = 0;
23682+ if (!a->exchange) {
23683+ if (a->src_dir != a->dst_dir) {
23684+ /*
23685+ * this temporary unlock is safe,
23686+ * because both dir->i_mutex are locked.
23687+ */
23688+ di_write_unlock(a->dst_parent);
23689+ di_write_lock_parent(a->src_parent);
23690+ err = au_wr_dir_need_wh(a->src_dentry,
23691+ au_ftest_ren(a->auren_flags,
23692+ ISDIR_SRC),
23693+ &a->btgt);
23694+ di_write_unlock(a->src_parent);
23695+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23696+ /*isdir*/1);
23697+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23698+ } else
23699+ err = au_wr_dir_need_wh(a->src_dentry,
23700+ au_ftest_ren(a->auren_flags,
23701+ ISDIR_SRC),
23702+ &a->btgt);
23703+ }
4a4d8108
AM
23704+ if (unlikely(err < 0))
23705+ goto out_children;
23706+ if (err)
f2c43d5f 23707+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23708+
86dc4139 23709+ /* cpup src */
5afbbe0d 23710+ if (a->src_btop != a->btgt) {
86dc4139
AM
23711+ err = au_pin(&pin, a->src_dentry, a->btgt,
23712+ au_opt_udba(a->src_dentry->d_sb),
23713+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23714+ if (!err) {
c2b27bf2
AM
23715+ struct au_cp_generic cpg = {
23716+ .dentry = a->src_dentry,
23717+ .bdst = a->btgt,
5afbbe0d 23718+ .bsrc = a->src_btop,
c2b27bf2
AM
23719+ .len = -1,
23720+ .pin = &pin,
23721+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23722+ };
5afbbe0d 23723+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23724+ err = au_sio_cpup_simple(&cpg);
367653fa 23725+ au_unpin(&pin);
86dc4139 23726+ }
86dc4139
AM
23727+ if (unlikely(err))
23728+ goto out_children;
5afbbe0d 23729+ a->src_btop = a->btgt;
86dc4139 23730+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23731+ if (!a->exchange)
23732+ au_fset_ren(a->auren_flags, WHSRC);
23733+ }
23734+
23735+ /* cpup dst */
23736+ if (a->exchange && a->dst_inode
23737+ && a->dst_btop != a->btgt) {
23738+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23739+ au_opt_udba(a->dst_dentry->d_sb),
23740+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23741+ if (!err) {
23742+ struct au_cp_generic cpg = {
23743+ .dentry = a->dst_dentry,
23744+ .bdst = a->btgt,
23745+ .bsrc = a->dst_btop,
23746+ .len = -1,
23747+ .pin = &pin,
23748+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23749+ };
23750+ err = au_sio_cpup_simple(&cpg);
23751+ au_unpin(&pin);
23752+ }
23753+ if (unlikely(err))
23754+ goto out_children;
23755+ a->dst_btop = a->btgt;
23756+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23757+ }
23758+
4a4d8108
AM
23759+ /* lock them all */
23760+ err = au_ren_lock(a);
23761+ if (unlikely(err))
86dc4139 23762+ /* leave the copied-up one */
4a4d8108 23763+ goto out_children;
1facf9fc 23764+
f2c43d5f
AM
23765+ if (!a->exchange) {
23766+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23767+ err = au_may_ren(a);
23768+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23769+ err = -ENAMETOOLONG;
23770+ if (unlikely(err))
23771+ goto out_hdir;
23772+ }
1facf9fc 23773+
4a4d8108
AM
23774+ /* store timestamps to be revertible */
23775+ au_ren_dt(a);
1facf9fc 23776+
8b6a4947
AM
23777+ /* store dirren info */
23778+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23779+ err = au_dr_rename(a->src_dentry, a->btgt,
23780+ &a->dst_dentry->d_name, &rev);
23781+ AuTraceErr(err);
23782+ if (unlikely(err))
23783+ goto out_dt;
23784+ }
23785+
4a4d8108
AM
23786+ /* here we go */
23787+ err = do_rename(a);
23788+ if (unlikely(err))
8b6a4947
AM
23789+ goto out_dirren;
23790+
23791+ if (au_ftest_ren(a->auren_flags, DIRREN))
23792+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23793+
23794+ /* update dir attributes */
23795+ au_ren_refresh_dir(a);
23796+
23797+ /* dput/iput all lower dentries */
23798+ au_ren_refresh(a);
23799+
23800+ goto out_hdir; /* success */
23801+
8b6a4947
AM
23802+out_dirren:
23803+ if (au_ftest_ren(a->auren_flags, DIRREN))
23804+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23805+out_dt:
4a4d8108 23806+ au_ren_rev_dt(err, a);
4f0767ce 23807+out_hdir:
4a4d8108 23808+ au_ren_unlock(a);
4f0767ce 23809+out_children:
4a4d8108 23810+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23811+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23812+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23813+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23814+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23815+ }
027c5e7a 23816+out_parent:
f2c43d5f 23817+ if (!err) {
8b6a4947
AM
23818+ if (d_unhashed(a->src_dentry))
23819+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23820+ if (d_unhashed(a->dst_dentry))
23821+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23822+ if (!a->exchange)
23823+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23824+ else {
f2c43d5f 23825+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23826+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23827+ d_drop(a->dst_dentry);
23828+ }
23829+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23830+ d_drop(a->src_dentry);
f2c43d5f 23831+ } else {
5afbbe0d 23832+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23833+ if (!a->dst_inode)
23834+ d_drop(a->dst_dentry);
23835+ }
f2c43d5f 23836+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23837+ di_write_unlock(a->dst_parent);
23838+ else
23839+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23840+out_unlock:
4a4d8108 23841+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23842+out_free:
4a4d8108
AM
23843+ iput(a->dst_inode);
23844+ if (a->thargs)
23845+ au_whtmp_rmdir_free(a->thargs);
9f237c51 23846+ au_kfree_rcu(a);
4f0767ce 23847+out:
4a4d8108
AM
23848+ AuTraceErr(err);
23849+ return err;
1308ab2a 23850+}
7f207e10 23851diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
eca34b5c
AM
23852--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
23853+++ linux/fs/aufs/Kconfig 2019-07-11 15:42:14.458904362 +0200
2121bcd9
AM
23854@@ -0,0 +1,199 @@
23855+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
23856+config AUFS_FS
23857+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23858+ help
23859+ Aufs is a stackable unification filesystem such as Unionfs,
23860+ which unifies several directories and provides a merged single
23861+ directory.
23862+ In the early days, aufs was entirely re-designed and
23863+ re-implemented Unionfs Version 1.x series. Introducing many
23864+ original ideas, approaches and improvements, it becomes totally
23865+ different from Unionfs while keeping the basic features.
1facf9fc 23866+
4a4d8108
AM
23867+if AUFS_FS
23868+choice
23869+ prompt "Maximum number of branches"
23870+ default AUFS_BRANCH_MAX_127
23871+ help
23872+ Specifies the maximum number of branches (or member directories)
23873+ in a single aufs. The larger value consumes more system
23874+ resources and has a minor impact to performance.
23875+config AUFS_BRANCH_MAX_127
23876+ bool "127"
23877+ help
23878+ Specifies the maximum number of branches (or member directories)
23879+ in a single aufs. The larger value consumes more system
23880+ resources and has a minor impact to performance.
23881+config AUFS_BRANCH_MAX_511
23882+ bool "511"
23883+ help
23884+ Specifies the maximum number of branches (or member directories)
23885+ in a single aufs. The larger value consumes more system
23886+ resources and has a minor impact to performance.
23887+config AUFS_BRANCH_MAX_1023
23888+ bool "1023"
23889+ help
23890+ Specifies the maximum number of branches (or member directories)
23891+ in a single aufs. The larger value consumes more system
23892+ resources and has a minor impact to performance.
23893+config AUFS_BRANCH_MAX_32767
23894+ bool "32767"
23895+ help
23896+ Specifies the maximum number of branches (or member directories)
23897+ in a single aufs. The larger value consumes more system
23898+ resources and has a minor impact to performance.
23899+endchoice
1facf9fc 23900+
e49829fe
JR
23901+config AUFS_SBILIST
23902+ bool
23903+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23904+ default y
23905+ help
23906+ Automatic configuration for internal use.
23907+ When aufs supports Magic SysRq or /proc, enabled automatically.
23908+
4a4d8108
AM
23909+config AUFS_HNOTIFY
23910+ bool "Detect direct branch access (bypassing aufs)"
23911+ help
23912+ If you want to modify files on branches directly, eg. bypassing aufs,
23913+ and want aufs to detect the changes of them fully, then enable this
23914+ option and use 'udba=notify' mount option.
7f207e10 23915+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
23916+ It will have a negative impact to the performance.
23917+ See detail in aufs.5.
dece6358 23918+
4a4d8108
AM
23919+choice
23920+ prompt "method" if AUFS_HNOTIFY
23921+ default AUFS_HFSNOTIFY
23922+config AUFS_HFSNOTIFY
23923+ bool "fsnotify"
23924+ select FSNOTIFY
4a4d8108 23925+endchoice
1facf9fc 23926+
4a4d8108
AM
23927+config AUFS_EXPORT
23928+ bool "NFS-exportable aufs"
2cbb1c4b 23929+ depends on EXPORTFS
4a4d8108
AM
23930+ help
23931+ If you want to export your mounted aufs via NFS, then enable this
23932+ option. There are several requirements for this configuration.
23933+ See detail in aufs.5.
1facf9fc 23934+
4a4d8108
AM
23935+config AUFS_INO_T_64
23936+ bool
23937+ depends on AUFS_EXPORT
23938+ depends on 64BIT && !(ALPHA || S390)
23939+ default y
23940+ help
23941+ Automatic configuration for internal use.
23942+ /* typedef unsigned long/int __kernel_ino_t */
23943+ /* alpha and s390x are int */
1facf9fc 23944+
c1595e42
JR
23945+config AUFS_XATTR
23946+ bool "support for XATTR/EA (including Security Labels)"
23947+ help
23948+ If your branch fs supports XATTR/EA and you want to make them
23949+ available in aufs too, then enable this opsion and specify the
23950+ branch attributes for EA.
23951+ See detail in aufs.5.
23952+
076b876e
AM
23953+config AUFS_FHSM
23954+ bool "File-based Hierarchical Storage Management"
23955+ help
23956+ Hierarchical Storage Management (or HSM) is a well-known feature
23957+ in the storage world. Aufs provides this feature as file-based.
23958+ with multiple branches.
23959+ These multiple branches are prioritized, ie. the topmost one
23960+ should be the fastest drive and be used heavily.
23961+
4a4d8108
AM
23962+config AUFS_RDU
23963+ bool "Readdir in userspace"
23964+ help
23965+ Aufs has two methods to provide a merged view for a directory,
23966+ by a user-space library and by kernel-space natively. The latter
23967+ is always enabled but sometimes large and slow.
23968+ If you enable this option, install the library in aufs2-util
23969+ package, and set some environment variables for your readdir(3),
23970+ then the work will be handled in user-space which generally
23971+ shows better performance in most cases.
23972+ See detail in aufs.5.
1facf9fc 23973+
8b6a4947
AM
23974+config AUFS_DIRREN
23975+ bool "Workaround for rename(2)-ing a directory"
23976+ help
23977+ By default, aufs returns EXDEV error in renameing a dir who has
23978+ his child on the lower branch, since it is a bad idea to issue
23979+ rename(2) internally for every lower branch. But user may not
23980+ accept this behaviour. So here is a workaround to allow such
23981+ rename(2) and store some extra infromation on the writable
23982+ branch. Obviously this costs high (and I don't like it).
23983+ To use this feature, you need to enable this configuration AND
23984+ to specify the mount option `dirren.'
23985+ See details in aufs.5 and the design documents.
23986+
4a4d8108
AM
23987+config AUFS_SHWH
23988+ bool "Show whiteouts"
23989+ help
23990+ If you want to make the whiteouts in aufs visible, then enable
23991+ this option and specify 'shwh' mount option. Although it may
23992+ sounds like philosophy or something, but in technically it
23993+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 23994+
4a4d8108
AM
23995+config AUFS_BR_RAMFS
23996+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
23997+ help
23998+ If you want to use ramfs as an aufs branch fs, then enable this
23999+ option. Generally tmpfs is recommended.
24000+ Aufs prohibited them to be a branch fs by default, because
24001+ initramfs becomes unusable after switch_root or something
24002+ generally. If you sets initramfs as an aufs branch and boot your
24003+ system by switch_root, you will meet a problem easily since the
24004+ files in initramfs may be inaccessible.
24005+ Unless you are going to use ramfs as an aufs branch fs without
24006+ switch_root or something, leave it N.
1facf9fc 24007+
4a4d8108
AM
24008+config AUFS_BR_FUSE
24009+ bool "Fuse fs as an aufs branch"
24010+ depends on FUSE_FS
24011+ select AUFS_POLL
24012+ help
24013+ If you want to use fuse-based userspace filesystem as an aufs
24014+ branch fs, then enable this option.
24015+ It implements the internal poll(2) operation which is
24016+ implemented by fuse only (curretnly).
1facf9fc 24017+
4a4d8108
AM
24018+config AUFS_POLL
24019+ bool
24020+ help
24021+ Automatic configuration for internal use.
1facf9fc 24022+
4a4d8108
AM
24023+config AUFS_BR_HFSPLUS
24024+ bool "Hfsplus as an aufs branch"
24025+ depends on HFSPLUS_FS
24026+ default y
24027+ help
24028+ If you want to use hfsplus fs as an aufs branch fs, then enable
24029+ this option. This option introduces a small overhead at
24030+ copying-up a file on hfsplus.
1facf9fc 24031+
4a4d8108
AM
24032+config AUFS_BDEV_LOOP
24033+ bool
24034+ depends on BLK_DEV_LOOP
24035+ default y
24036+ help
24037+ Automatic configuration for internal use.
24038+ Convert =[ym] into =y.
1308ab2a 24039+
4a4d8108
AM
24040+config AUFS_DEBUG
24041+ bool "Debug aufs"
24042+ help
24043+ Enable this to compile aufs internal debug code.
24044+ It will have a negative impact to the performance.
24045+
24046+config AUFS_MAGIC_SYSRQ
24047+ bool
24048+ depends on AUFS_DEBUG && MAGIC_SYSRQ
24049+ default y
24050+ help
24051+ Automatic configuration for internal use.
24052+ When aufs supports Magic SysRq, enabled automatically.
24053+endif
acd2b654 24054diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
eca34b5c
AM
24055--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
24056+++ linux/fs/aufs/lcnt.h 2019-07-11 15:42:14.468904634 +0200
acd2b654
AM
24057@@ -0,0 +1,186 @@
24058+/* SPDX-License-Identifier: GPL-2.0 */
24059+/*
ba1aed25 24060+ * Copyright (C) 2018-2019 Junjiro R. Okajima
acd2b654
AM
24061+ *
24062+ * This program, aufs is free software; you can redistribute it and/or modify
24063+ * it under the terms of the GNU General Public License as published by
24064+ * the Free Software Foundation; either version 2 of the License, or
24065+ * (at your option) any later version.
24066+ *
24067+ * This program is distributed in the hope that it will be useful,
24068+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24069+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24070+ * GNU General Public License for more details.
24071+ *
24072+ * You should have received a copy of the GNU General Public License
24073+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24074+ */
24075+
24076+/*
24077+ * simple long counter wrapper
24078+ */
24079+
24080+#ifndef __AUFS_LCNT_H__
24081+#define __AUFS_LCNT_H__
24082+
24083+#ifdef __KERNEL__
24084+
24085+#include "debug.h"
24086+
24087+#define AuLCntATOMIC 1
24088+#define AuLCntPCPUCNT 2
24089+/*
24090+ * why does percpu_refcount require extra synchronize_rcu()s in
24091+ * au_br_do_free()
24092+ */
24093+#define AuLCntPCPUREF 3
24094+
24095+/* #define AuLCntChosen AuLCntATOMIC */
24096+#define AuLCntChosen AuLCntPCPUCNT
24097+/* #define AuLCntChosen AuLCntPCPUREF */
24098+
24099+#if AuLCntChosen == AuLCntATOMIC
24100+#include <linux/atomic.h>
24101+
24102+typedef atomic_long_t au_lcnt_t;
24103+
24104+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24105+{
24106+ atomic_long_set(cnt, 0);
24107+ return 0;
24108+}
24109+
24110+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24111+{
24112+ /* empty */
24113+}
24114+
24115+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24116+ int do_sync __maybe_unused)
24117+{
24118+ /* empty */
24119+}
24120+
24121+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24122+{
24123+ atomic_long_inc(cnt);
24124+}
24125+
24126+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24127+{
24128+ atomic_long_dec(cnt);
24129+}
24130+
24131+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24132+{
24133+ return atomic_long_read(cnt);
24134+}
24135+#endif
24136+
24137+#if AuLCntChosen == AuLCntPCPUCNT
24138+#include <linux/percpu_counter.h>
24139+
24140+typedef struct percpu_counter au_lcnt_t;
24141+
24142+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24143+{
24144+ return percpu_counter_init(cnt, 0, GFP_NOFS);
24145+}
24146+
24147+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24148+{
24149+ /* empty */
24150+}
24151+
24152+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24153+{
24154+ percpu_counter_destroy(cnt);
24155+}
24156+
24157+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24158+{
24159+ percpu_counter_inc(cnt);
24160+}
24161+
24162+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24163+{
24164+ percpu_counter_dec(cnt);
24165+}
24166+
24167+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24168+{
24169+ s64 n;
24170+
24171+ n = percpu_counter_sum(cnt);
24172+ BUG_ON(n < 0);
24173+ if (LONG_MAX != LLONG_MAX
24174+ && n > LONG_MAX)
24175+ AuWarn1("%s\n", "wrap-around");
24176+
24177+ return n;
24178+}
24179+#endif
24180+
24181+#if AuLCntChosen == AuLCntPCPUREF
24182+#include <linux/percpu-refcount.h>
24183+
24184+typedef struct percpu_ref au_lcnt_t;
24185+
24186+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24187+{
24188+ if (!release)
24189+ release = percpu_ref_exit;
24190+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24191+}
24192+
24193+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24194+{
24195+ synchronize_rcu();
24196+}
24197+
24198+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24199+{
24200+ percpu_ref_kill(cnt);
24201+ if (do_sync)
24202+ au_lcnt_wait_for_fin(cnt);
24203+}
24204+
24205+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24206+{
24207+ percpu_ref_get(cnt);
24208+}
24209+
24210+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24211+{
24212+ percpu_ref_put(cnt);
24213+}
24214+
24215+/*
24216+ * avoid calling this func as possible.
24217+ */
24218+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24219+{
24220+ long l;
24221+
24222+ percpu_ref_switch_to_atomic_sync(cnt);
24223+ l = atomic_long_read(&cnt->count);
24224+ if (do_rev)
24225+ percpu_ref_switch_to_percpu(cnt);
24226+
24227+ /* percpu_ref is initialized by 1 instead of 0 */
24228+ return l - 1;
24229+}
24230+#endif
24231+
24232+#ifdef CONFIG_AUFS_DEBUG
24233+#define AuLCntZero(val) do { \
24234+ long l = val; \
24235+ if (l) \
24236+ AuDbg("%s = %ld\n", #val, l); \
24237+} while (0)
24238+#else
24239+#define AuLCntZero(val) do {} while (0)
24240+#endif
24241+
24242+#endif /* __KERNEL__ */
24243+#endif /* __AUFS_LCNT_H__ */
7f207e10 24244diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
eca34b5c
AM
24245--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
24246+++ linux/fs/aufs/loop.c 2019-07-11 15:42:14.468904634 +0200
062440b3 24247@@ -0,0 +1,148 @@
cd7a4cd9 24248+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24249+/*
ba1aed25 24250+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 24251+ *
24252+ * This program, aufs is free software; you can redistribute it and/or modify
24253+ * it under the terms of the GNU General Public License as published by
24254+ * the Free Software Foundation; either version 2 of the License, or
24255+ * (at your option) any later version.
dece6358
AM
24256+ *
24257+ * This program is distributed in the hope that it will be useful,
24258+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24259+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24260+ * GNU General Public License for more details.
24261+ *
24262+ * You should have received a copy of the GNU General Public License
523b37e3 24263+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24264+ */
24265+
24266+/*
24267+ * support for loopback block device as a branch
24268+ */
24269+
1facf9fc 24270+#include "aufs.h"
24271+
392086de
AM
24272+/* added into drivers/block/loop.c */
24273+static struct file *(*backing_file_func)(struct super_block *sb);
24274+
1facf9fc 24275+/*
24276+ * test if two lower dentries have overlapping branches.
24277+ */
b752ccd1 24278+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 24279+{
b752ccd1 24280+ struct super_block *h_sb;
392086de
AM
24281+ struct file *backing_file;
24282+
24283+ if (unlikely(!backing_file_func)) {
24284+ /* don't load "loop" module here */
24285+ backing_file_func = symbol_get(loop_backing_file);
24286+ if (unlikely(!backing_file_func))
24287+ /* "loop" module is not loaded */
24288+ return 0;
24289+ }
1facf9fc 24290+
b752ccd1 24291+ h_sb = h_adding->d_sb;
392086de
AM
24292+ backing_file = backing_file_func(h_sb);
24293+ if (!backing_file)
1facf9fc 24294+ return 0;
24295+
2000de60 24296+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
24297+ /*
24298+ * h_adding can be local NFS.
24299+ * in this case aufs cannot detect the loop.
24300+ */
24301+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 24302+ return 1;
b752ccd1 24303+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 24304+}
24305+
24306+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24307+int au_test_loopback_kthread(void)
24308+{
b752ccd1
AM
24309+ int ret;
24310+ struct task_struct *tsk = current;
a2a7ad62 24311+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
24312+
24313+ ret = 0;
24314+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
24315+ get_task_comm(comm, tsk);
24316+ c = comm[4];
b752ccd1 24317+ ret = ('0' <= c && c <= '9'
a2a7ad62 24318+ && !strncmp(comm, "loop", 4));
b752ccd1 24319+ }
1facf9fc 24320+
b752ccd1 24321+ return ret;
1facf9fc 24322+}
87a755f4
AM
24323+
24324+/* ---------------------------------------------------------------------- */
24325+
24326+#define au_warn_loopback_step 16
24327+static int au_warn_loopback_nelem = au_warn_loopback_step;
24328+static unsigned long *au_warn_loopback_array;
24329+
24330+void au_warn_loopback(struct super_block *h_sb)
24331+{
24332+ int i, new_nelem;
24333+ unsigned long *a, magic;
24334+ static DEFINE_SPINLOCK(spin);
24335+
24336+ magic = h_sb->s_magic;
24337+ spin_lock(&spin);
24338+ a = au_warn_loopback_array;
24339+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24340+ if (a[i] == magic) {
24341+ spin_unlock(&spin);
24342+ return;
24343+ }
24344+
24345+ /* h_sb is new to us, print it */
24346+ if (i < au_warn_loopback_nelem) {
24347+ a[i] = magic;
24348+ goto pr;
24349+ }
24350+
24351+ /* expand the array */
24352+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24353+ a = au_kzrealloc(au_warn_loopback_array,
24354+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24355+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24356+ /*may_shrink*/0);
87a755f4
AM
24357+ if (a) {
24358+ au_warn_loopback_nelem = new_nelem;
24359+ au_warn_loopback_array = a;
24360+ a[i] = magic;
24361+ goto pr;
24362+ }
24363+
24364+ spin_unlock(&spin);
24365+ AuWarn1("realloc failed, ignored\n");
24366+ return;
24367+
24368+pr:
24369+ spin_unlock(&spin);
0c3ec466
AM
24370+ pr_warn("you may want to try another patch for loopback file "
24371+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24372+}
24373+
24374+int au_loopback_init(void)
24375+{
24376+ int err;
24377+ struct super_block *sb __maybe_unused;
24378+
fbc438ed 24379+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
87a755f4
AM
24380+
24381+ err = 0;
24382+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24383+ sizeof(unsigned long), GFP_NOFS);
24384+ if (unlikely(!au_warn_loopback_array))
24385+ err = -ENOMEM;
24386+
24387+ return err;
24388+}
24389+
24390+void au_loopback_fin(void)
24391+{
79b8bda9
AM
24392+ if (backing_file_func)
24393+ symbol_put(loop_backing_file);
9f237c51 24394+ au_kfree_try_rcu(au_warn_loopback_array);
87a755f4 24395+}
7f207e10 24396diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
eca34b5c
AM
24397--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
24398+++ linux/fs/aufs/loop.h 2019-07-11 15:42:14.468904634 +0200
eca801bf 24399@@ -0,0 +1,55 @@
062440b3 24400+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24401+/*
ba1aed25 24402+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 24403+ *
24404+ * This program, aufs is free software; you can redistribute it and/or modify
24405+ * it under the terms of the GNU General Public License as published by
24406+ * the Free Software Foundation; either version 2 of the License, or
24407+ * (at your option) any later version.
dece6358
AM
24408+ *
24409+ * This program is distributed in the hope that it will be useful,
24410+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24411+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24412+ * GNU General Public License for more details.
24413+ *
24414+ * You should have received a copy of the GNU General Public License
523b37e3 24415+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24416+ */
24417+
24418+/*
24419+ * support for loopback mount as a branch
24420+ */
24421+
24422+#ifndef __AUFS_LOOP_H__
24423+#define __AUFS_LOOP_H__
24424+
24425+#ifdef __KERNEL__
24426+
dece6358
AM
24427+struct dentry;
24428+struct super_block;
1facf9fc 24429+
24430+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24431+/* drivers/block/loop.c */
24432+struct file *loop_backing_file(struct super_block *sb);
24433+
1facf9fc 24434+/* loop.c */
b752ccd1 24435+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24436+int au_test_loopback_kthread(void);
87a755f4
AM
24437+void au_warn_loopback(struct super_block *h_sb);
24438+
24439+int au_loopback_init(void);
24440+void au_loopback_fin(void);
1facf9fc 24441+#else
83b672a5 24442+AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
eca801bf 24443+
4a4d8108 24444+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24445+ struct dentry *h_adding)
4a4d8108 24446+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24447+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24448+
24449+AuStubInt0(au_loopback_init, void)
24450+AuStubVoid(au_loopback_fin, void)
1facf9fc 24451+#endif /* BLK_DEV_LOOP */
24452+
24453+#endif /* __KERNEL__ */
24454+#endif /* __AUFS_LOOP_H__ */
7f207e10 24455diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
eca34b5c
AM
24456--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
24457+++ linux/fs/aufs/magic.mk 2019-07-11 15:42:14.468904634 +0200
2121bcd9
AM
24458@@ -0,0 +1,31 @@
24459+# SPDX-License-Identifier: GPL-2.0
1facf9fc 24460+
24461+# defined in ${srctree}/fs/fuse/inode.c
24462+# tristate
24463+ifdef CONFIG_FUSE_FS
24464+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24465+endif
24466+
1facf9fc 24467+# defined in ${srctree}/fs/xfs/xfs_sb.h
24468+# tristate
24469+ifdef CONFIG_XFS_FS
24470+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24471+endif
24472+
24473+# defined in ${srctree}/fs/configfs/mount.c
24474+# tristate
24475+ifdef CONFIG_CONFIGFS_FS
24476+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24477+endif
24478+
1facf9fc 24479+# defined in ${srctree}/fs/ubifs/ubifs.h
24480+# tristate
24481+ifdef CONFIG_UBIFS_FS
24482+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24483+endif
4a4d8108
AM
24484+
24485+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24486+# tristate
24487+ifdef CONFIG_HFSPLUS_FS
24488+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24489+endif
7f207e10 24490diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
eca34b5c
AM
24491--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
24492+++ linux/fs/aufs/Makefile 2019-07-11 15:42:14.462237786 +0200
2121bcd9
AM
24493@@ -0,0 +1,46 @@
24494+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
24495+
24496+include ${src}/magic.mk
24497+ifeq (${CONFIG_AUFS_FS},m)
24498+include ${src}/conf.mk
24499+endif
24500+-include ${src}/priv_def.mk
24501+
24502+# cf. include/linux/kernel.h
24503+# enable pr_debug
24504+ccflags-y += -DDEBUG
f6c5ef8b
AM
24505+# sparse requires the full pathname
24506+ifdef M
523b37e3 24507+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24508+else
523b37e3 24509+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24510+endif
4a4d8108
AM
24511+
24512+obj-$(CONFIG_AUFS_FS) += aufs.o
24513+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24514+ wkq.o vfsub.o dcsub.o \
e49829fe 24515+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24516+ dinfo.o dentry.o \
24517+ dynop.o \
24518+ finfo.o file.o f_op.o \
24519+ dir.o vdir.o \
24520+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24521+ mvdown.o ioctl.o
4a4d8108
AM
24522+
24523+# all are boolean
e49829fe 24524+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24525+aufs-$(CONFIG_SYSFS) += sysfs.o
24526+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24527+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24528+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24529+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24530+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24531+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24532+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24533+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24534+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24535+aufs-$(CONFIG_AUFS_POLL) += poll.o
24536+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24537+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24538+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24539+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10 24540diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
eca34b5c
AM
24541--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
24542+++ linux/fs/aufs/module.c 2019-07-11 15:42:14.468904634 +0200
062440b3 24543@@ -0,0 +1,273 @@
cd7a4cd9 24544+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24545+/*
ba1aed25 24546+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 24547+ *
24548+ * This program, aufs is free software; you can redistribute it and/or modify
24549+ * it under the terms of the GNU General Public License as published by
24550+ * the Free Software Foundation; either version 2 of the License, or
24551+ * (at your option) any later version.
dece6358
AM
24552+ *
24553+ * This program is distributed in the hope that it will be useful,
24554+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24555+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24556+ * GNU General Public License for more details.
24557+ *
24558+ * You should have received a copy of the GNU General Public License
523b37e3 24559+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24560+ */
24561+
24562+/*
24563+ * module global variables and operations
24564+ */
24565+
24566+#include <linux/module.h>
24567+#include <linux/seq_file.h>
24568+#include "aufs.h"
24569+
e2f27e51
AM
24570+/* shrinkable realloc */
24571+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24572+{
e2f27e51
AM
24573+ size_t sz;
24574+ int diff;
1facf9fc 24575+
e2f27e51
AM
24576+ sz = 0;
24577+ diff = -1;
24578+ if (p) {
24579+#if 0 /* unused */
24580+ if (!new_sz) {
9f237c51 24581+ au_kfree_rcu(p);
e2f27e51
AM
24582+ p = NULL;
24583+ goto out;
24584+ }
24585+#else
24586+ AuDebugOn(!new_sz);
24587+#endif
24588+ sz = ksize(p);
24589+ diff = au_kmidx_sub(sz, new_sz);
24590+ }
24591+ if (sz && !diff)
24592+ goto out;
24593+
24594+ if (sz < new_sz)
24595+ /* expand or SLOB */
24596+ p = krealloc(p, new_sz, gfp);
24597+ else if (new_sz < sz && may_shrink) {
24598+ /* shrink */
24599+ void *q;
24600+
24601+ q = kmalloc(new_sz, gfp);
24602+ if (q) {
24603+ if (p) {
24604+ memcpy(q, p, new_sz);
9f237c51 24605+ au_kfree_try_rcu(p);
e2f27e51
AM
24606+ }
24607+ p = q;
24608+ } else
24609+ p = NULL;
24610+ }
24611+
24612+out:
24613+ return p;
24614+}
24615+
24616+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24617+ int may_shrink)
24618+{
24619+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24620+ if (p && new_sz > nused)
1facf9fc 24621+ memset(p + nused, 0, new_sz - nused);
24622+ return p;
24623+}
24624+
24625+/* ---------------------------------------------------------------------- */
1facf9fc 24626+/*
24627+ * aufs caches
24628+ */
1c60b727 24629+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24630+
24631+static void au_cache_fin(void)
24632+{
24633+ int i;
24634+
24635+ /*
24636+ * Make sure all delayed rcu free inodes are flushed before we
24637+ * destroy cache.
24638+ */
24639+ rcu_barrier();
24640+
24641+ /* excluding AuCache_HNOTIFY */
24642+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24643+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24644+ kmem_cache_destroy(au_cache[i]);
24645+ au_cache[i] = NULL;
5afbbe0d
AM
24646+ }
24647+}
24648+
1facf9fc 24649+static int __init au_cache_init(void)
24650+{
1c60b727
AM
24651+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24652+ if (au_cache[AuCache_DINFO])
027c5e7a 24653+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24654+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24655+ au_icntnr_init_once);
1c60b727
AM
24656+ if (au_cache[AuCache_ICNTNR])
24657+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24658+ au_fi_init_once);
1c60b727
AM
24659+ if (au_cache[AuCache_FINFO])
24660+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24661+ if (au_cache[AuCache_VDIR])
24662+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24663+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24664+ return 0;
24665+
5afbbe0d 24666+ au_cache_fin();
1facf9fc 24667+ return -ENOMEM;
24668+}
24669+
1facf9fc 24670+/* ---------------------------------------------------------------------- */
24671+
24672+int au_dir_roflags;
24673+
e49829fe 24674+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24675+/*
24676+ * iterate_supers_type() doesn't protect us from
24677+ * remounting (branch management)
24678+ */
8b6a4947 24679+struct hlist_bl_head au_sbilist;
e49829fe
JR
24680+#endif
24681+
1facf9fc 24682+/*
24683+ * functions for module interface.
24684+ */
24685+MODULE_LICENSE("GPL");
24686+/* MODULE_LICENSE("GPL v2"); */
dece6358 24687+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24688+MODULE_DESCRIPTION(AUFS_NAME
24689+ " -- Advanced multi layered unification filesystem");
24690+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24691+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24692+
1facf9fc 24693+/* this module parameter has no meaning when SYSFS is disabled */
24694+int sysaufs_brs = 1;
24695+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
cd7a4cd9 24696+module_param_named(brs, sysaufs_brs, int, 0444);
1facf9fc 24697+
076b876e 24698+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24699+bool au_userns;
076b876e 24700+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
cd7a4cd9 24701+module_param_named(allow_userns, au_userns, bool, 0444);
076b876e 24702+
1facf9fc 24703+/* ---------------------------------------------------------------------- */
24704+
24705+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24706+
24707+int au_seq_path(struct seq_file *seq, struct path *path)
24708+{
79b8bda9
AM
24709+ int err;
24710+
24711+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24712+ if (err >= 0)
79b8bda9 24713+ err = 0;
1c60b727 24714+ else
79b8bda9
AM
24715+ err = -ENOMEM;
24716+
24717+ return err;
1facf9fc 24718+}
24719+
24720+/* ---------------------------------------------------------------------- */
24721+
24722+static int __init aufs_init(void)
24723+{
24724+ int err, i;
24725+ char *p;
24726+
24727+ p = au_esc_chars;
24728+ for (i = 1; i <= ' '; i++)
24729+ *p++ = i;
24730+ *p++ = '\\';
24731+ *p++ = '\x7f';
24732+ *p = 0;
24733+
24734+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24735+
b95c5147
AM
24736+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24737+ for (i = 0; i < AuIop_Last; i++)
24738+ aufs_iop_nogetattr[i].getattr = NULL;
24739+
1c60b727 24740+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24741+
e49829fe 24742+ au_sbilist_init();
1facf9fc 24743+ sysaufs_brs_init();
24744+ au_debug_init();
4a4d8108 24745+ au_dy_init();
1facf9fc 24746+ err = sysaufs_init();
24747+ if (unlikely(err))
24748+ goto out;
062440b3 24749+ err = dbgaufs_init();
4f0767ce 24750+ if (unlikely(err))
953406b4 24751+ goto out_sysaufs;
062440b3
AM
24752+ err = au_procfs_init();
24753+ if (unlikely(err))
24754+ goto out_dbgaufs;
e49829fe
JR
24755+ err = au_wkq_init();
24756+ if (unlikely(err))
24757+ goto out_procfs;
87a755f4 24758+ err = au_loopback_init();
1facf9fc 24759+ if (unlikely(err))
24760+ goto out_wkq;
87a755f4
AM
24761+ err = au_hnotify_init();
24762+ if (unlikely(err))
24763+ goto out_loopback;
1facf9fc 24764+ err = au_sysrq_init();
24765+ if (unlikely(err))
24766+ goto out_hin;
24767+ err = au_cache_init();
24768+ if (unlikely(err))
24769+ goto out_sysrq;
076b876e
AM
24770+
24771+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24772+ err = register_filesystem(&aufs_fs_type);
24773+ if (unlikely(err))
24774+ goto out_cache;
076b876e 24775+
4a4d8108
AM
24776+ /* since we define pr_fmt, call printk directly */
24777+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24778+ goto out; /* success */
24779+
4f0767ce 24780+out_cache:
1facf9fc 24781+ au_cache_fin();
4f0767ce 24782+out_sysrq:
1facf9fc 24783+ au_sysrq_fin();
4f0767ce 24784+out_hin:
4a4d8108 24785+ au_hnotify_fin();
87a755f4
AM
24786+out_loopback:
24787+ au_loopback_fin();
4f0767ce 24788+out_wkq:
1facf9fc 24789+ au_wkq_fin();
e49829fe
JR
24790+out_procfs:
24791+ au_procfs_fin();
062440b3
AM
24792+out_dbgaufs:
24793+ dbgaufs_fin();
4f0767ce 24794+out_sysaufs:
1facf9fc 24795+ sysaufs_fin();
4a4d8108 24796+ au_dy_fin();
4f0767ce 24797+out:
1facf9fc 24798+ return err;
24799+}
24800+
24801+static void __exit aufs_exit(void)
24802+{
24803+ unregister_filesystem(&aufs_fs_type);
24804+ au_cache_fin();
24805+ au_sysrq_fin();
4a4d8108 24806+ au_hnotify_fin();
87a755f4 24807+ au_loopback_fin();
1facf9fc 24808+ au_wkq_fin();
e49829fe 24809+ au_procfs_fin();
062440b3 24810+ dbgaufs_fin();
1facf9fc 24811+ sysaufs_fin();
4a4d8108 24812+ au_dy_fin();
1facf9fc 24813+}
24814+
24815+module_init(aufs_init);
24816+module_exit(aufs_exit);
7f207e10 24817diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
eca34b5c
AM
24818--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
24819+++ linux/fs/aufs/module.h 2019-07-11 15:42:14.468904634 +0200
9f237c51 24820@@ -0,0 +1,166 @@
062440b3 24821+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24822+/*
ba1aed25 24823+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 24824+ *
24825+ * This program, aufs is free software; you can redistribute it and/or modify
24826+ * it under the terms of the GNU General Public License as published by
24827+ * the Free Software Foundation; either version 2 of the License, or
24828+ * (at your option) any later version.
dece6358
AM
24829+ *
24830+ * This program is distributed in the hope that it will be useful,
24831+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24832+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24833+ * GNU General Public License for more details.
24834+ *
24835+ * You should have received a copy of the GNU General Public License
523b37e3 24836+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24837+ */
24838+
24839+/*
24840+ * module initialization and module-global
24841+ */
24842+
24843+#ifndef __AUFS_MODULE_H__
24844+#define __AUFS_MODULE_H__
24845+
24846+#ifdef __KERNEL__
24847+
24848+#include <linux/slab.h>
9f237c51
AM
24849+#include "debug.h"
24850+#include "dentry.h"
24851+#include "dir.h"
24852+#include "file.h"
24853+#include "inode.h"
1facf9fc 24854+
dece6358
AM
24855+struct path;
24856+struct seq_file;
24857+
1facf9fc 24858+/* module parameters */
1facf9fc 24859+extern int sysaufs_brs;
8cdd5066 24860+extern bool au_userns;
1facf9fc 24861+
24862+/* ---------------------------------------------------------------------- */
24863+
24864+extern int au_dir_roflags;
24865+
e2f27e51
AM
24866+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24867+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24868+ int may_shrink);
24869+
9f237c51
AM
24870+/*
24871+ * Comparing the size of the object with sizeof(struct rcu_head)
24872+ * case 1: object is always larger
24873+ * --> au_kfree_rcu() or au_kfree_do_rcu()
24874+ * case 2: object is always smaller
24875+ * --> au_kfree_small()
24876+ * case 3: object can be any size
24877+ * --> au_kfree_try_rcu()
24878+ */
24879+
24880+static inline void au_kfree_do_rcu(const void *p)
24881+{
24882+ struct {
24883+ struct rcu_head rcu;
24884+ } *a = (void *)p;
24885+
24886+ kfree_rcu(a, rcu);
24887+}
24888+
24889+#define au_kfree_rcu(_p) do { \
24890+ typeof(_p) p = (_p); \
24891+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
24892+ if (p) \
24893+ au_kfree_do_rcu(p); \
24894+ } while (0)
24895+
24896+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
24897+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
24898+
24899+static inline void au_kfree_try_rcu(const void *p)
24900+{
24901+ if (!p)
24902+ return;
24903+ if (au_kfree_sz_test(p))
24904+ au_kfree_do_rcu(p);
24905+ else
24906+ kfree(p);
24907+}
24908+
24909+static inline void au_kfree_small(const void *p)
24910+{
24911+ if (!p)
24912+ return;
24913+ AuDebugOn(au_kfree_sz_test(p));
24914+ kfree(p);
24915+}
24916+
e2f27e51
AM
24917+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24918+{
24919+#ifndef CONFIG_SLOB
24920+ return kmalloc_index(sz) - kmalloc_index(new_sz);
24921+#else
24922+ return -1; /* SLOB is untested */
24923+#endif
24924+}
24925+
1facf9fc 24926+int au_seq_path(struct seq_file *seq, struct path *path);
24927+
e49829fe
JR
24928+#ifdef CONFIG_PROC_FS
24929+/* procfs.c */
24930+int __init au_procfs_init(void);
24931+void au_procfs_fin(void);
24932+#else
24933+AuStubInt0(au_procfs_init, void);
24934+AuStubVoid(au_procfs_fin, void);
24935+#endif
24936+
4f0767ce
JR
24937+/* ---------------------------------------------------------------------- */
24938+
1c60b727 24939+/* kmem cache */
1facf9fc 24940+enum {
24941+ AuCache_DINFO,
24942+ AuCache_ICNTNR,
24943+ AuCache_FINFO,
24944+ AuCache_VDIR,
24945+ AuCache_DEHSTR,
7eafdf33 24946+ AuCache_HNOTIFY, /* must be last */
1facf9fc 24947+ AuCache_Last
24948+};
24949+
1c60b727 24950+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 24951+
4a4d8108
AM
24952+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24953+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
24954+#define AuCacheCtor(type, ctor) \
24955+ kmem_cache_create(#type, sizeof(struct type), \
24956+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 24957+
9f237c51
AM
24958+#define AuCacheFuncs(name, index) \
24959+ static inline struct au_##name *au_cache_alloc_##name(void) \
24960+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24961+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24962+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
24963+ \
24964+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24965+ { void *p = rcu; \
24966+ p -= offsetof(struct au_##name, rcu); \
24967+ kmem_cache_free(au_cache[AuCache_##index], p); } \
24968+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24969+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24970+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
24971+ \
24972+ static inline void au_cache_free_##name(struct au_##name *p) \
24973+ { /* au_cache_free_##name##_norcu(p); */ \
24974+ au_cache_free_##name##_rcu(p); }
1facf9fc 24975+
24976+AuCacheFuncs(dinfo, DINFO);
24977+AuCacheFuncs(icntnr, ICNTNR);
24978+AuCacheFuncs(finfo, FINFO);
24979+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
24980+AuCacheFuncs(vdir_dehstr, DEHSTR);
24981+#ifdef CONFIG_AUFS_HNOTIFY
24982+AuCacheFuncs(hnotify, HNOTIFY);
24983+#endif
1facf9fc 24984+
4a4d8108
AM
24985+#endif /* __KERNEL__ */
24986+#endif /* __AUFS_MODULE_H__ */
c2b27bf2 24987diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
eca34b5c
AM
24988--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
24989+++ linux/fs/aufs/mvdown.c 2019-07-11 15:42:14.472238057 +0200
ba1aed25 24990@@ -0,0 +1,706 @@
cd7a4cd9 24991+// SPDX-License-Identifier: GPL-2.0
c2b27bf2 24992+/*
ba1aed25 24993+ * Copyright (C) 2011-2019 Junjiro R. Okajima
c2b27bf2
AM
24994+ *
24995+ * This program, aufs is free software; you can redistribute it and/or modify
24996+ * it under the terms of the GNU General Public License as published by
24997+ * the Free Software Foundation; either version 2 of the License, or
24998+ * (at your option) any later version.
24999+ *
25000+ * This program is distributed in the hope that it will be useful,
25001+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25002+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25003+ * GNU General Public License for more details.
25004+ *
25005+ * You should have received a copy of the GNU General Public License
523b37e3
AM
25006+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25007+ */
25008+
25009+/*
25010+ * move-down, opposite of copy-up
c2b27bf2
AM
25011+ */
25012+
25013+#include "aufs.h"
25014+
c2b27bf2
AM
25015+struct au_mvd_args {
25016+ struct {
c2b27bf2
AM
25017+ struct super_block *h_sb;
25018+ struct dentry *h_parent;
25019+ struct au_hinode *hdir;
392086de 25020+ struct inode *h_dir, *h_inode;
c1595e42 25021+ struct au_pin pin;
c2b27bf2
AM
25022+ } info[AUFS_MVDOWN_NARRAY];
25023+
25024+ struct aufs_mvdown mvdown;
25025+ struct dentry *dentry, *parent;
25026+ struct inode *inode, *dir;
25027+ struct super_block *sb;
25028+ aufs_bindex_t bopq, bwh, bfound;
25029+ unsigned char rename_lock;
c2b27bf2
AM
25030+};
25031+
392086de 25032+#define mvd_errno mvdown.au_errno
076b876e
AM
25033+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25034+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25035+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25036+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 25037+
392086de
AM
25038+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
25039+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
25040+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
25041+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
25042+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 25043+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
25044+
25045+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
25046+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
25047+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
25048+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
25049+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 25050+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
25051+
25052+#define AU_MVD_PR(flag, ...) do { \
25053+ if (flag) \
25054+ pr_err(__VA_ARGS__); \
25055+ } while (0)
25056+
076b876e
AM
25057+static int find_lower_writable(struct au_mvd_args *a)
25058+{
25059+ struct super_block *sb;
5afbbe0d 25060+ aufs_bindex_t bindex, bbot;
076b876e
AM
25061+ struct au_branch *br;
25062+
25063+ sb = a->sb;
25064+ bindex = a->mvd_bsrc;
5afbbe0d 25065+ bbot = au_sbbot(sb);
076b876e 25066+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 25067+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25068+ br = au_sbr(sb, bindex);
25069+ if (au_br_fhsm(br->br_perm)
8b6a4947 25070+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
25071+ return bindex;
25072+ }
25073+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 25074+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25075+ br = au_sbr(sb, bindex);
25076+ if (!au_br_rdonly(br))
25077+ return bindex;
25078+ }
25079+ else
5afbbe0d 25080+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 25081+ br = au_sbr(sb, bindex);
8b6a4947 25082+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
25083+ if (au_br_rdonly(br))
25084+ a->mvdown.flags
25085+ |= AUFS_MVDOWN_ROLOWER_R;
25086+ return bindex;
25087+ }
25088+ }
25089+
25090+ return -1;
25091+}
25092+
c2b27bf2 25093+/* make the parent dir on bdst */
392086de 25094+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25095+{
25096+ int err;
25097+
25098+ err = 0;
25099+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25100+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25101+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25102+ a->mvd_h_dst_parent = NULL;
5afbbe0d 25103+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
25104+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25105+ if (!a->mvd_h_dst_parent) {
25106+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25107+ if (unlikely(err)) {
392086de 25108+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
25109+ goto out;
25110+ }
25111+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25112+ }
25113+
25114+out:
25115+ AuTraceErr(err);
25116+ return err;
25117+}
25118+
25119+/* lock them all */
392086de 25120+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25121+{
25122+ int err;
25123+ struct dentry *h_trap;
25124+
25125+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25126+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
25127+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25128+ au_opt_udba(a->sb),
25129+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25130+ AuTraceErr(err);
25131+ if (unlikely(err)) {
25132+ AU_MVD_PR(dmsg, "pin_dst failed\n");
25133+ goto out;
25134+ }
25135+
c2b27bf2
AM
25136+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25137+ a->rename_lock = 0;
c1595e42
JR
25138+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25139+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25140+ au_opt_udba(a->sb),
25141+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25142+ err = au_do_pin(&a->mvd_pin_src);
25143+ AuTraceErr(err);
5527c038 25144+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25145+ if (unlikely(err)) {
25146+ AU_MVD_PR(dmsg, "pin_src failed\n");
25147+ goto out_dst;
25148+ }
25149+ goto out; /* success */
c2b27bf2
AM
25150+ }
25151+
c2b27bf2 25152+ a->rename_lock = 1;
c1595e42
JR
25153+ au_pin_hdir_unlock(&a->mvd_pin_dst);
25154+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25155+ au_opt_udba(a->sb),
25156+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25157+ AuTraceErr(err);
5527c038 25158+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25159+ if (unlikely(err)) {
25160+ AU_MVD_PR(dmsg, "pin_src failed\n");
25161+ au_pin_hdir_lock(&a->mvd_pin_dst);
25162+ goto out_dst;
25163+ }
25164+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
25165+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25166+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25167+ if (h_trap) {
25168+ err = (h_trap != a->mvd_h_src_parent);
25169+ if (err)
25170+ err = (h_trap != a->mvd_h_dst_parent);
25171+ }
25172+ BUG_ON(err); /* it should never happen */
c1595e42
JR
25173+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25174+ err = -EBUSY;
25175+ AuTraceErr(err);
25176+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25177+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25178+ au_pin_hdir_lock(&a->mvd_pin_src);
25179+ au_unpin(&a->mvd_pin_src);
25180+ au_pin_hdir_lock(&a->mvd_pin_dst);
25181+ goto out_dst;
25182+ }
25183+ goto out; /* success */
c2b27bf2 25184+
c1595e42
JR
25185+out_dst:
25186+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25187+out:
25188+ AuTraceErr(err);
25189+ return err;
25190+}
25191+
392086de 25192+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 25193+{
c1595e42
JR
25194+ if (!a->rename_lock)
25195+ au_unpin(&a->mvd_pin_src);
25196+ else {
c2b27bf2
AM
25197+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25198+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
25199+ au_pin_hdir_lock(&a->mvd_pin_src);
25200+ au_unpin(&a->mvd_pin_src);
25201+ au_pin_hdir_lock(&a->mvd_pin_dst);
25202+ }
25203+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25204+}
25205+
25206+/* copy-down the file */
392086de 25207+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25208+{
25209+ int err;
25210+ struct au_cp_generic cpg = {
25211+ .dentry = a->dentry,
25212+ .bdst = a->mvd_bdst,
25213+ .bsrc = a->mvd_bsrc,
25214+ .len = -1,
c1595e42 25215+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
25216+ .flags = AuCpup_DTIME | AuCpup_HOPEN
25217+ };
25218+
25219+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
25220+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25221+ au_fset_cpup(cpg.flags, OVERWRITE);
25222+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25223+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
25224+ err = au_sio_cpdown_simple(&cpg);
25225+ if (unlikely(err))
392086de 25226+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
25227+
25228+ AuTraceErr(err);
25229+ return err;
25230+}
25231+
25232+/*
25233+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
25234+ * were sleeping
25235+ */
392086de 25236+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25237+{
25238+ int err;
25239+ struct path h_path;
25240+ struct au_branch *br;
523b37e3 25241+ struct inode *delegated;
c2b27bf2
AM
25242+
25243+ br = au_sbr(a->sb, a->mvd_bdst);
25244+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25245+ err = PTR_ERR(h_path.dentry);
25246+ if (IS_ERR(h_path.dentry)) {
392086de 25247+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
25248+ goto out;
25249+ }
25250+
25251+ err = 0;
5527c038 25252+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 25253+ h_path.mnt = au_br_mnt(br);
523b37e3 25254+ delegated = NULL;
5527c038 25255+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
25256+ &delegated, /*force*/0);
25257+ if (unlikely(err == -EWOULDBLOCK)) {
25258+ pr_warn("cannot retry for NFSv4 delegation"
25259+ " for an internal unlink\n");
25260+ iput(delegated);
25261+ }
c2b27bf2 25262+ if (unlikely(err))
392086de 25263+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
25264+ }
25265+ dput(h_path.dentry);
25266+
25267+out:
25268+ AuTraceErr(err);
25269+ return err;
25270+}
25271+
25272+/*
25273+ * unlink the topmost h_dentry
c2b27bf2 25274+ */
392086de 25275+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25276+{
25277+ int err;
25278+ struct path h_path;
523b37e3 25279+ struct inode *delegated;
c2b27bf2
AM
25280+
25281+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25282+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
25283+ delegated = NULL;
25284+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25285+ if (unlikely(err == -EWOULDBLOCK)) {
25286+ pr_warn("cannot retry for NFSv4 delegation"
25287+ " for an internal unlink\n");
25288+ iput(delegated);
25289+ }
c2b27bf2 25290+ if (unlikely(err))
392086de 25291+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
25292+
25293+ AuTraceErr(err);
25294+ return err;
25295+}
25296+
076b876e
AM
25297+/* Since mvdown succeeded, we ignore an error of this function */
25298+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25299+{
25300+ int err;
25301+ struct au_branch *br;
25302+
25303+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25304+ br = au_sbr(a->sb, a->mvd_bsrc);
25305+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25306+ if (!err) {
25307+ br = au_sbr(a->sb, a->mvd_bdst);
25308+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25309+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25310+ }
25311+ if (!err)
25312+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25313+ else
25314+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25315+}
25316+
c2b27bf2
AM
25317+/*
25318+ * copy-down the file and unlink the bsrc file.
25319+ * - unlink the bdst whout if exist
25320+ * - copy-down the file (with whtmp name and rename)
25321+ * - unlink the bsrc file
25322+ */
392086de 25323+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25324+{
25325+ int err;
25326+
392086de 25327+ err = au_do_mkdir(dmsg, a);
c2b27bf2 25328+ if (!err)
392086de 25329+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
25330+ if (unlikely(err))
25331+ goto out;
25332+
25333+ /*
25334+ * do not revert the activities we made on bdst since they should be
25335+ * harmless in aufs.
25336+ */
25337+
392086de 25338+ err = au_do_cpdown(dmsg, a);
c2b27bf2 25339+ if (!err)
392086de
AM
25340+ err = au_do_unlink_wh(dmsg, a);
25341+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25342+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
25343+ if (unlikely(err))
25344+ goto out_unlock;
25345+
c1595e42
JR
25346+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25347+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
25348+ if (find_lower_writable(a) < 0)
25349+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25350+
25351+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25352+ au_do_stfs(dmsg, a);
25353+
c2b27bf2 25354+ /* maintain internal array */
392086de
AM
25355+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25356+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 25357+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 25358+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 25359+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
25360+ } else {
25361+ /* hide the lower */
25362+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 25363+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 25364+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 25365+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 25366+ }
5afbbe0d
AM
25367+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25368+ au_set_dbbot(a->dentry, a->mvd_bdst);
25369+ if (au_ibbot(a->inode) < a->mvd_bdst)
25370+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
25371+
25372+out_unlock:
392086de 25373+ au_do_unlock(dmsg, a);
c2b27bf2
AM
25374+out:
25375+ AuTraceErr(err);
25376+ return err;
25377+}
25378+
25379+/* ---------------------------------------------------------------------- */
25380+
c2b27bf2 25381+/* make sure the file is idle */
392086de 25382+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25383+{
25384+ int err, plinked;
c2b27bf2
AM
25385+
25386+ err = 0;
c2b27bf2 25387+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 25388+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 25389+ && au_dcount(a->dentry) == 1
c2b27bf2 25390+ && atomic_read(&a->inode->i_count) == 1
392086de 25391+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
25392+ && (!plinked || !au_plink_test(a->inode))
25393+ && a->inode->i_nlink == 1)
25394+ goto out;
25395+
25396+ err = -EBUSY;
392086de 25397+ AU_MVD_PR(dmsg,
c1595e42 25398+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 25399+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 25400+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 25401+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
25402+ plinked, plinked ? au_plink_test(a->inode) : 0);
25403+
25404+out:
25405+ AuTraceErr(err);
25406+ return err;
25407+}
25408+
25409+/* make sure the parent dir is fine */
392086de 25410+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25411+ struct au_mvd_args *a)
25412+{
25413+ int err;
25414+ aufs_bindex_t bindex;
25415+
25416+ err = 0;
25417+ if (unlikely(au_alive_dir(a->parent))) {
25418+ err = -ENOENT;
392086de 25419+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25420+ goto out;
25421+ }
25422+
25423+ a->bopq = au_dbdiropq(a->parent);
25424+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25425+ AuDbg("b%d\n", bindex);
25426+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25427+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25428+ err = -EINVAL;
392086de
AM
25429+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25430+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25431+ a->bopq, a->mvd_bdst);
25432+ }
25433+
25434+out:
25435+ AuTraceErr(err);
25436+ return err;
25437+}
25438+
392086de 25439+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25440+ struct au_mvd_args *a)
25441+{
25442+ int err;
25443+ struct au_dinfo *dinfo, *tmp;
25444+
25445+ /* lookup the next lower positive entry */
25446+ err = -ENOMEM;
25447+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25448+ if (unlikely(!tmp))
25449+ goto out;
25450+
25451+ a->bfound = -1;
25452+ a->bwh = -1;
25453+ dinfo = au_di(a->dentry);
25454+ au_di_cp(tmp, dinfo);
25455+ au_di_swap(tmp, dinfo);
25456+
25457+ /* returns the number of positive dentries */
5afbbe0d
AM
25458+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25459+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25460+ if (!err)
25461+ a->bwh = au_dbwh(a->dentry);
25462+ else if (err > 0)
5afbbe0d 25463+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25464+
25465+ au_di_swap(tmp, dinfo);
25466+ au_rw_write_unlock(&tmp->di_rwsem);
25467+ au_di_free(tmp);
25468+ if (unlikely(err < 0))
392086de 25469+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25470+
25471+ /*
25472+ * here, we have these cases.
25473+ * bfound == -1
25474+ * no positive dentry under bsrc. there are more sub-cases.
25475+ * bwh < 0
25476+ * there no whiteout, we can safely move-down.
25477+ * bwh <= bsrc
25478+ * impossible
25479+ * bsrc < bwh && bwh < bdst
25480+ * there is a whiteout on RO branch. cannot proceed.
25481+ * bwh == bdst
25482+ * there is a whiteout on the RW target branch. it should
25483+ * be removed.
25484+ * bdst < bwh
25485+ * there is a whiteout somewhere unrelated branch.
25486+ * -1 < bfound && bfound <= bsrc
25487+ * impossible.
25488+ * bfound < bdst
25489+ * found, but it is on RO branch between bsrc and bdst. cannot
25490+ * proceed.
25491+ * bfound == bdst
25492+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25493+ * error.
25494+ * bdst < bfound
25495+ * found, after we create the file on bdst, it will be hidden.
25496+ */
25497+
25498+ AuDebugOn(a->bfound == -1
25499+ && a->bwh != -1
25500+ && a->bwh <= a->mvd_bsrc);
25501+ AuDebugOn(-1 < a->bfound
25502+ && a->bfound <= a->mvd_bsrc);
25503+
25504+ err = -EINVAL;
25505+ if (a->bfound == -1
25506+ && a->mvd_bsrc < a->bwh
25507+ && a->bwh != -1
25508+ && a->bwh < a->mvd_bdst) {
392086de
AM
25509+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25510+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25511+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25512+ goto out;
25513+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25514+ a->mvd_errno = EAU_MVDOWN_UPPER;
25515+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25516+ a->mvd_bdst, a->bfound);
25517+ goto out;
25518+ }
25519+
25520+ err = 0; /* success */
25521+
25522+out:
25523+ AuTraceErr(err);
25524+ return err;
25525+}
25526+
392086de 25527+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25528+{
25529+ int err;
25530+
392086de
AM
25531+ err = 0;
25532+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25533+ && a->bfound == a->mvd_bdst)
25534+ err = -EEXIST;
c2b27bf2
AM
25535+ AuTraceErr(err);
25536+ return err;
25537+}
25538+
392086de 25539+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25540+{
25541+ int err;
25542+ struct au_branch *br;
25543+
25544+ err = -EISDIR;
25545+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25546+ goto out;
25547+
25548+ err = -EINVAL;
392086de 25549+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25550+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25551+ else {
25552+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25553+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25554+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25555+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25556+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25557+ || (a->mvd_bsrc < au_ibtop(a->inode)
25558+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25559+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25560+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25561+ AU_MVD_PR(dmsg, "no upper\n");
25562+ goto out;
25563+ }
25564+ }
5afbbe0d 25565+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25566+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25567+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25568+ goto out;
25569+ }
392086de 25570+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25571+ br = au_sbr(a->sb, a->mvd_bsrc);
25572+ err = au_br_rdonly(br);
392086de
AM
25573+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25574+ if (unlikely(err))
25575+ goto out;
25576+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25577+ || IS_APPEND(a->mvd_h_src_inode))) {
25578+ if (err)
25579+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25580+ /* go on */
25581+ } else
c2b27bf2
AM
25582+ goto out;
25583+
25584+ err = -EINVAL;
392086de
AM
25585+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25586+ a->mvd_bdst = find_lower_writable(a);
25587+ if (unlikely(a->mvd_bdst < 0)) {
25588+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25589+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25590+ goto out;
25591+ }
25592+ } else {
25593+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25594+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25595+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25596+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25597+ AU_MVD_PR(dmsg, "no lower brid\n");
25598+ goto out;
25599+ }
c2b27bf2
AM
25600+ }
25601+
392086de 25602+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25603+ if (!err)
392086de 25604+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25605+ if (!err)
392086de 25606+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25607+ if (!err)
392086de 25608+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25609+ if (!err)
25610+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25611+
25612+out:
25613+ AuTraceErr(err);
25614+ return err;
25615+}
25616+
25617+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25618+{
392086de
AM
25619+ int err, e;
25620+ unsigned char dmsg;
25621+ struct au_mvd_args *args;
79b8bda9 25622+ struct inode *inode;
c2b27bf2 25623+
79b8bda9 25624+ inode = d_inode(dentry);
c2b27bf2
AM
25625+ err = -EPERM;
25626+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25627+ goto out;
25628+
392086de
AM
25629+ err = -ENOMEM;
25630+ args = kmalloc(sizeof(*args), GFP_NOFS);
25631+ if (unlikely(!args))
25632+ goto out;
25633+
25634+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25635+ if (!err)
ba1aed25
AM
25636+ /* VERIFY_WRITE */
25637+ err = !access_ok(uarg, sizeof(*uarg));
c2b27bf2
AM
25638+ if (unlikely(err)) {
25639+ err = -EFAULT;
392086de
AM
25640+ AuTraceErr(err);
25641+ goto out_free;
c2b27bf2 25642+ }
392086de
AM
25643+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25644+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25645+ args->mvdown.au_errno = 0;
25646+ args->dentry = dentry;
79b8bda9 25647+ args->inode = inode;
392086de 25648+ args->sb = dentry->d_sb;
c2b27bf2 25649+
392086de
AM
25650+ err = -ENOENT;
25651+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25652+ args->parent = dget_parent(dentry);
5527c038 25653+ args->dir = d_inode(args->parent);
febd17d6 25654+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25655+ dput(args->parent);
25656+ if (unlikely(args->parent != dentry->d_parent)) {
25657+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25658+ goto out_dir;
25659+ }
25660+
febd17d6 25661+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25662+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25663+ if (unlikely(err))
25664+ goto out_inode;
25665+
392086de
AM
25666+ di_write_lock_parent(args->parent);
25667+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25668+ if (unlikely(err))
25669+ goto out_parent;
25670+
392086de 25671+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25672+ if (unlikely(err))
25673+ goto out_parent;
c2b27bf2 25674+
392086de 25675+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25676+ au_cpup_attr_timesizes(inode);
25677+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25678+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25679+ /* au_digen_dec(dentry); */
25680+
25681+out_parent:
392086de 25682+ di_write_unlock(args->parent);
c2b27bf2
AM
25683+ aufs_read_unlock(dentry, AuLock_DW);
25684+out_inode:
febd17d6 25685+ inode_unlock(inode);
c2b27bf2 25686+out_dir:
febd17d6 25687+ inode_unlock(args->dir);
392086de
AM
25688+out_free:
25689+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25690+ if (unlikely(e))
25691+ err = -EFAULT;
9f237c51 25692+ au_kfree_rcu(args);
c2b27bf2
AM
25693+out:
25694+ AuTraceErr(err);
25695+ return err;
25696+}
25697diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
eca34b5c
AM
25698--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
25699+++ linux/fs/aufs/opts.c 2019-09-16 09:38:43.216175640 +0200
25700@@ -0,0 +1,1880 @@
cd7a4cd9 25701+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25702+/*
ba1aed25 25703+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 25704+ *
25705+ * This program, aufs is free software; you can redistribute it and/or modify
25706+ * it under the terms of the GNU General Public License as published by
25707+ * the Free Software Foundation; either version 2 of the License, or
25708+ * (at your option) any later version.
dece6358
AM
25709+ *
25710+ * This program is distributed in the hope that it will be useful,
25711+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25712+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25713+ * GNU General Public License for more details.
25714+ *
25715+ * You should have received a copy of the GNU General Public License
523b37e3 25716+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25717+ */
25718+
25719+/*
25720+ * mount options/flags
25721+ */
25722+
dece6358 25723+#include <linux/namei.h>
1facf9fc 25724+#include <linux/types.h> /* a distribution requires */
25725+#include <linux/parser.h>
25726+#include "aufs.h"
25727+
25728+/* ---------------------------------------------------------------------- */
25729+
25730+enum {
25731+ Opt_br,
7e9cd9fe
AM
25732+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25733+ Opt_idel, Opt_imod,
25734+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25735+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25736+ Opt_xino, Opt_noxino,
1facf9fc 25737+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25738+ Opt_trunc_xino_path, Opt_itrunc_xino,
25739+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25740+ Opt_shwh, Opt_noshwh,
1facf9fc 25741+ Opt_plink, Opt_noplink, Opt_list_plink,
25742+ Opt_udba,
4a4d8108 25743+ Opt_dio, Opt_nodio,
1facf9fc 25744+ Opt_diropq_a, Opt_diropq_w,
25745+ Opt_warn_perm, Opt_nowarn_perm,
25746+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25747+ Opt_fhsm_sec,
1facf9fc 25748+ Opt_verbose, Opt_noverbose,
25749+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25750+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25751+ Opt_dirren, Opt_nodirren,
c1595e42 25752+ Opt_acl, Opt_noacl,
1facf9fc 25753+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25754+};
25755+
25756+static match_table_t options = {
25757+ {Opt_br, "br=%s"},
25758+ {Opt_br, "br:%s"},
25759+
25760+ {Opt_add, "add=%d:%s"},
25761+ {Opt_add, "add:%d:%s"},
25762+ {Opt_add, "ins=%d:%s"},
25763+ {Opt_add, "ins:%d:%s"},
25764+ {Opt_append, "append=%s"},
25765+ {Opt_append, "append:%s"},
25766+ {Opt_prepend, "prepend=%s"},
25767+ {Opt_prepend, "prepend:%s"},
25768+
25769+ {Opt_del, "del=%s"},
25770+ {Opt_del, "del:%s"},
25771+ /* {Opt_idel, "idel:%d"}, */
25772+ {Opt_mod, "mod=%s"},
25773+ {Opt_mod, "mod:%s"},
25774+ /* {Opt_imod, "imod:%d:%s"}, */
25775+
25776+ {Opt_dirwh, "dirwh=%d"},
25777+
25778+ {Opt_xino, "xino=%s"},
25779+ {Opt_noxino, "noxino"},
25780+ {Opt_trunc_xino, "trunc_xino"},
25781+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25782+ {Opt_notrunc_xino, "notrunc_xino"},
25783+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25784+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25785+ /* {Opt_zxino, "zxino=%s"}, */
25786+ {Opt_trunc_xib, "trunc_xib"},
25787+ {Opt_notrunc_xib, "notrunc_xib"},
25788+
e49829fe 25789+#ifdef CONFIG_PROC_FS
1facf9fc 25790+ {Opt_plink, "plink"},
e49829fe
JR
25791+#else
25792+ {Opt_ignore_silent, "plink"},
25793+#endif
25794+
1facf9fc 25795+ {Opt_noplink, "noplink"},
e49829fe 25796+
1facf9fc 25797+#ifdef CONFIG_AUFS_DEBUG
25798+ {Opt_list_plink, "list_plink"},
25799+#endif
25800+
25801+ {Opt_udba, "udba=%s"},
25802+
4a4d8108
AM
25803+ {Opt_dio, "dio"},
25804+ {Opt_nodio, "nodio"},
25805+
8b6a4947
AM
25806+#ifdef CONFIG_AUFS_DIRREN
25807+ {Opt_dirren, "dirren"},
25808+ {Opt_nodirren, "nodirren"},
25809+#else
25810+ {Opt_ignore, "dirren"},
25811+ {Opt_ignore_silent, "nodirren"},
25812+#endif
25813+
076b876e
AM
25814+#ifdef CONFIG_AUFS_FHSM
25815+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25816+#else
8b6a4947 25817+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25818+#endif
25819+
1facf9fc 25820+ {Opt_diropq_a, "diropq=always"},
25821+ {Opt_diropq_a, "diropq=a"},
25822+ {Opt_diropq_w, "diropq=whiteouted"},
25823+ {Opt_diropq_w, "diropq=w"},
25824+
25825+ {Opt_warn_perm, "warn_perm"},
25826+ {Opt_nowarn_perm, "nowarn_perm"},
25827+
25828+ /* keep them temporary */
1facf9fc 25829+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25830+ {Opt_ignore, "clean_plink"},
1facf9fc 25831+
dece6358
AM
25832+#ifdef CONFIG_AUFS_SHWH
25833+ {Opt_shwh, "shwh"},
25834+#endif
25835+ {Opt_noshwh, "noshwh"},
25836+
076b876e
AM
25837+ {Opt_dirperm1, "dirperm1"},
25838+ {Opt_nodirperm1, "nodirperm1"},
25839+
1facf9fc 25840+ {Opt_verbose, "verbose"},
25841+ {Opt_verbose, "v"},
25842+ {Opt_noverbose, "noverbose"},
25843+ {Opt_noverbose, "quiet"},
25844+ {Opt_noverbose, "q"},
25845+ {Opt_noverbose, "silent"},
25846+
25847+ {Opt_sum, "sum"},
25848+ {Opt_nosum, "nosum"},
25849+ {Opt_wsum, "wsum"},
25850+
25851+ {Opt_rdcache, "rdcache=%d"},
25852+ {Opt_rdblk, "rdblk=%d"},
dece6358 25853+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25854+ {Opt_rdhash, "rdhash=%d"},
dece6358 25855+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25856+
25857+ {Opt_wbr_create, "create=%s"},
25858+ {Opt_wbr_create, "create_policy=%s"},
25859+ {Opt_wbr_copyup, "cpup=%s"},
25860+ {Opt_wbr_copyup, "copyup=%s"},
25861+ {Opt_wbr_copyup, "copyup_policy=%s"},
25862+
c1595e42
JR
25863+ /* generic VFS flag */
25864+#ifdef CONFIG_FS_POSIX_ACL
25865+ {Opt_acl, "acl"},
25866+ {Opt_noacl, "noacl"},
25867+#else
8b6a4947 25868+ {Opt_ignore, "acl"},
c1595e42
JR
25869+ {Opt_ignore_silent, "noacl"},
25870+#endif
25871+
1facf9fc 25872+ /* internal use for the scripts */
25873+ {Opt_ignore_silent, "si=%s"},
25874+
25875+ {Opt_br, "dirs=%s"},
25876+ {Opt_ignore, "debug=%d"},
25877+ {Opt_ignore, "delete=whiteout"},
25878+ {Opt_ignore, "delete=all"},
25879+ {Opt_ignore, "imap=%s"},
25880+
1308ab2a 25881+ /* temporary workaround, due to old mount(8)? */
25882+ {Opt_ignore_silent, "relatime"},
25883+
1facf9fc 25884+ {Opt_err, NULL}
25885+};
25886+
25887+/* ---------------------------------------------------------------------- */
25888+
076b876e 25889+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25890+{
076b876e
AM
25891+ struct match_token *p;
25892+
25893+ p = tbl;
25894+ while (p->pattern) {
25895+ if (p->token == val)
25896+ return p->pattern;
25897+ p++;
1facf9fc 25898+ }
25899+ BUG();
25900+ return "??";
25901+}
25902+
076b876e
AM
25903+static const char *au_optstr(int *val, match_table_t tbl)
25904+{
25905+ struct match_token *p;
25906+ int v;
25907+
25908+ v = *val;
2000de60
JR
25909+ if (!v)
25910+ goto out;
076b876e 25911+ p = tbl;
2000de60
JR
25912+ while (p->pattern) {
25913+ if (p->token
25914+ && (v & p->token) == p->token) {
076b876e
AM
25915+ *val &= ~p->token;
25916+ return p->pattern;
25917+ }
25918+ p++;
25919+ }
2000de60
JR
25920+
25921+out:
076b876e
AM
25922+ return NULL;
25923+}
25924+
1facf9fc 25925+/* ---------------------------------------------------------------------- */
25926+
1e00d052 25927+static match_table_t brperm = {
1facf9fc 25928+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25929+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25930+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
25931+ {0, NULL}
25932+};
1facf9fc 25933+
86dc4139 25934+static match_table_t brattr = {
076b876e
AM
25935+ /* general */
25936+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25937+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 25938+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 25939+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 25940+#ifdef CONFIG_AUFS_FHSM
076b876e 25941+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
25942+#endif
25943+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
25944+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25945+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25946+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25947+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25948+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25949+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 25950+#endif
076b876e
AM
25951+
25952+ /* ro/rr branch */
1e00d052 25953+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
25954+
25955+ /* rw branch */
25956+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 25957+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 25958+
1e00d052 25959+ {0, NULL}
1facf9fc 25960+};
25961+
1e00d052
AM
25962+static int br_attr_val(char *str, match_table_t table, substring_t args[])
25963+{
25964+ int attr, v;
25965+ char *p;
25966+
25967+ attr = 0;
25968+ do {
25969+ p = strchr(str, '+');
25970+ if (p)
25971+ *p = 0;
25972+ v = match_token(str, table, args);
076b876e
AM
25973+ if (v) {
25974+ if (v & AuBrAttr_CMOO_Mask)
25975+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 25976+ attr |= v;
076b876e 25977+ } else {
1e00d052
AM
25978+ if (p)
25979+ *p = '+';
0c3ec466 25980+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
25981+ break;
25982+ }
25983+ if (p)
25984+ str = p + 1;
25985+ } while (p);
25986+
25987+ return attr;
25988+}
25989+
076b876e
AM
25990+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25991+{
25992+ int sz;
25993+ const char *p;
25994+ char *q;
25995+
076b876e
AM
25996+ q = str->a;
25997+ *q = 0;
25998+ p = au_optstr(&perm, brattr);
25999+ if (p) {
26000+ sz = strlen(p);
26001+ memcpy(q, p, sz + 1);
26002+ q += sz;
26003+ } else
26004+ goto out;
26005+
26006+ do {
26007+ p = au_optstr(&perm, brattr);
26008+ if (p) {
26009+ *q++ = '+';
26010+ sz = strlen(p);
26011+ memcpy(q, p, sz + 1);
26012+ q += sz;
26013+ }
26014+ } while (p);
26015+
26016+out:
c1595e42 26017+ return q - str->a;
076b876e
AM
26018+}
26019+
4a4d8108 26020+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 26021+{
076b876e
AM
26022+ int val, bad, sz;
26023+ char *p;
1facf9fc 26024+ substring_t args[MAX_OPT_ARGS];
076b876e 26025+ au_br_perm_str_t attr;
1facf9fc 26026+
1e00d052
AM
26027+ p = strchr(perm, '+');
26028+ if (p)
26029+ *p = 0;
26030+ val = match_token(perm, brperm, args);
26031+ if (!val) {
26032+ if (p)
26033+ *p = '+';
0c3ec466 26034+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
26035+ val = AuBrPerm_RO;
26036+ goto out;
26037+ }
26038+ if (!p)
26039+ goto out;
26040+
076b876e
AM
26041+ val |= br_attr_val(p + 1, brattr, args);
26042+
26043+ bad = 0;
86dc4139 26044+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
26045+ case AuBrPerm_RO:
26046+ case AuBrPerm_RR:
076b876e
AM
26047+ bad = val & AuBrWAttr_Mask;
26048+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
26049+ break;
26050+ case AuBrPerm_RW:
076b876e
AM
26051+ bad = val & AuBrRAttr_Mask;
26052+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
26053+ break;
26054+ }
c1595e42
JR
26055+
26056+ /*
26057+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26058+ * does not treat it as an error, just warning.
26059+ * this is a tiny guard for the user operation.
26060+ */
26061+ if (val & AuBrAttr_UNPIN) {
26062+ bad |= AuBrAttr_UNPIN;
26063+ val &= ~AuBrAttr_UNPIN;
26064+ }
26065+
076b876e
AM
26066+ if (unlikely(bad)) {
26067+ sz = au_do_optstr_br_attr(&attr, bad);
26068+ AuDebugOn(!sz);
26069+ pr_warn("ignored branch attribute %s\n", attr.a);
26070+ }
1e00d052
AM
26071+
26072+out:
1facf9fc 26073+ return val;
26074+}
26075+
076b876e 26076+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 26077+{
076b876e
AM
26078+ au_br_perm_str_t attr;
26079+ const char *p;
26080+ char *q;
1e00d052
AM
26081+ int sz;
26082+
076b876e
AM
26083+ q = str->a;
26084+ p = au_optstr(&perm, brperm);
26085+ AuDebugOn(!p || !*p);
26086+ sz = strlen(p);
26087+ memcpy(q, p, sz + 1);
26088+ q += sz;
1e00d052 26089+
076b876e
AM
26090+ sz = au_do_optstr_br_attr(&attr, perm);
26091+ if (sz) {
26092+ *q++ = '+';
26093+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
26094+ }
26095+
076b876e 26096+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 26097+}
26098+
26099+/* ---------------------------------------------------------------------- */
26100+
26101+static match_table_t udbalevel = {
26102+ {AuOpt_UDBA_REVAL, "reval"},
26103+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
26104+#ifdef CONFIG_AUFS_HNOTIFY
26105+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26106+#ifdef CONFIG_AUFS_HFSNOTIFY
26107+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 26108+#endif
1facf9fc 26109+#endif
26110+ {-1, NULL}
26111+};
26112+
4a4d8108 26113+static int noinline_for_stack udba_val(char *str)
1facf9fc 26114+{
26115+ substring_t args[MAX_OPT_ARGS];
26116+
7f207e10 26117+ return match_token(str, udbalevel, args);
1facf9fc 26118+}
26119+
26120+const char *au_optstr_udba(int udba)
26121+{
076b876e 26122+ return au_parser_pattern(udba, udbalevel);
1facf9fc 26123+}
26124+
26125+/* ---------------------------------------------------------------------- */
26126+
26127+static match_table_t au_wbr_create_policy = {
26128+ {AuWbrCreate_TDP, "tdp"},
26129+ {AuWbrCreate_TDP, "top-down-parent"},
26130+ {AuWbrCreate_RR, "rr"},
26131+ {AuWbrCreate_RR, "round-robin"},
26132+ {AuWbrCreate_MFS, "mfs"},
26133+ {AuWbrCreate_MFS, "most-free-space"},
26134+ {AuWbrCreate_MFSV, "mfs:%d"},
26135+ {AuWbrCreate_MFSV, "most-free-space:%d"},
26136+
f2c43d5f
AM
26137+ /* top-down regardless the parent, and then mfs */
26138+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
26139+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26140+
1facf9fc 26141+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
26142+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26143+ {AuWbrCreate_PMFS, "pmfs"},
26144+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
26145+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26146+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 26147+
26148+ {-1, NULL}
26149+};
26150+
1facf9fc 26151+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26152+ struct au_opt_wbr_create *create)
26153+{
26154+ int err;
26155+ unsigned long long ull;
26156+
26157+ err = 0;
a2654f78 26158+ if (!match_u64(arg, &ull))
1facf9fc 26159+ create->mfsrr_watermark = ull;
26160+ else {
4a4d8108 26161+ pr_err("bad integer in %s\n", str);
1facf9fc 26162+ err = -EINVAL;
26163+ }
26164+
26165+ return err;
26166+}
26167+
26168+static int au_wbr_mfs_sec(substring_t *arg, char *str,
26169+ struct au_opt_wbr_create *create)
26170+{
26171+ int n, err;
26172+
26173+ err = 0;
027c5e7a 26174+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 26175+ create->mfs_second = n;
26176+ else {
4a4d8108 26177+ pr_err("bad integer in %s\n", str);
1facf9fc 26178+ err = -EINVAL;
26179+ }
26180+
26181+ return err;
26182+}
26183+
4a4d8108
AM
26184+static int noinline_for_stack
26185+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 26186+{
26187+ int err, e;
26188+ substring_t args[MAX_OPT_ARGS];
26189+
26190+ err = match_token(str, au_wbr_create_policy, args);
26191+ create->wbr_create = err;
26192+ switch (err) {
26193+ case AuWbrCreate_MFSRRV:
f2c43d5f 26194+ case AuWbrCreate_TDMFSV:
392086de 26195+ case AuWbrCreate_PMFSRRV:
1facf9fc 26196+ e = au_wbr_mfs_wmark(&args[0], str, create);
26197+ if (!e)
26198+ e = au_wbr_mfs_sec(&args[1], str, create);
26199+ if (unlikely(e))
26200+ err = e;
26201+ break;
26202+ case AuWbrCreate_MFSRR:
f2c43d5f 26203+ case AuWbrCreate_TDMFS:
392086de 26204+ case AuWbrCreate_PMFSRR:
1facf9fc 26205+ e = au_wbr_mfs_wmark(&args[0], str, create);
26206+ if (unlikely(e)) {
26207+ err = e;
26208+ break;
26209+ }
26210+ /*FALLTHROUGH*/
26211+ case AuWbrCreate_MFS:
26212+ case AuWbrCreate_PMFS:
027c5e7a 26213+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 26214+ break;
26215+ case AuWbrCreate_MFSV:
26216+ case AuWbrCreate_PMFSV:
26217+ e = au_wbr_mfs_sec(&args[0], str, create);
26218+ if (unlikely(e))
26219+ err = e;
26220+ break;
26221+ }
26222+
26223+ return err;
26224+}
26225+
26226+const char *au_optstr_wbr_create(int wbr_create)
26227+{
076b876e 26228+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 26229+}
26230+
26231+static match_table_t au_wbr_copyup_policy = {
26232+ {AuWbrCopyup_TDP, "tdp"},
26233+ {AuWbrCopyup_TDP, "top-down-parent"},
26234+ {AuWbrCopyup_BUP, "bup"},
26235+ {AuWbrCopyup_BUP, "bottom-up-parent"},
26236+ {AuWbrCopyup_BU, "bu"},
26237+ {AuWbrCopyup_BU, "bottom-up"},
26238+ {-1, NULL}
26239+};
26240+
4a4d8108 26241+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 26242+{
26243+ substring_t args[MAX_OPT_ARGS];
26244+
26245+ return match_token(str, au_wbr_copyup_policy, args);
26246+}
26247+
26248+const char *au_optstr_wbr_copyup(int wbr_copyup)
26249+{
076b876e 26250+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 26251+}
26252+
26253+/* ---------------------------------------------------------------------- */
26254+
26255+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26256+
26257+static void dump_opts(struct au_opts *opts)
26258+{
26259+#ifdef CONFIG_AUFS_DEBUG
26260+ /* reduce stack space */
26261+ union {
26262+ struct au_opt_add *add;
26263+ struct au_opt_del *del;
26264+ struct au_opt_mod *mod;
26265+ struct au_opt_xino *xino;
26266+ struct au_opt_xino_itrunc *xino_itrunc;
26267+ struct au_opt_wbr_create *create;
26268+ } u;
26269+ struct au_opt *opt;
26270+
26271+ opt = opts->opt;
26272+ while (opt->type != Opt_tail) {
26273+ switch (opt->type) {
26274+ case Opt_add:
26275+ u.add = &opt->add;
26276+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26277+ u.add->bindex, u.add->pathname, u.add->perm,
26278+ u.add->path.dentry);
26279+ break;
26280+ case Opt_del:
26281+ case Opt_idel:
26282+ u.del = &opt->del;
26283+ AuDbg("del {%s, %p}\n",
26284+ u.del->pathname, u.del->h_path.dentry);
26285+ break;
26286+ case Opt_mod:
26287+ case Opt_imod:
26288+ u.mod = &opt->mod;
26289+ AuDbg("mod {%s, 0x%x, %p}\n",
26290+ u.mod->path, u.mod->perm, u.mod->h_root);
26291+ break;
26292+ case Opt_append:
26293+ u.add = &opt->add;
26294+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26295+ u.add->bindex, u.add->pathname, u.add->perm,
26296+ u.add->path.dentry);
26297+ break;
26298+ case Opt_prepend:
26299+ u.add = &opt->add;
26300+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26301+ u.add->bindex, u.add->pathname, u.add->perm,
26302+ u.add->path.dentry);
26303+ break;
26304+ case Opt_dirwh:
26305+ AuDbg("dirwh %d\n", opt->dirwh);
26306+ break;
26307+ case Opt_rdcache:
26308+ AuDbg("rdcache %d\n", opt->rdcache);
26309+ break;
26310+ case Opt_rdblk:
26311+ AuDbg("rdblk %u\n", opt->rdblk);
26312+ break;
dece6358
AM
26313+ case Opt_rdblk_def:
26314+ AuDbg("rdblk_def\n");
26315+ break;
1facf9fc 26316+ case Opt_rdhash:
26317+ AuDbg("rdhash %u\n", opt->rdhash);
26318+ break;
dece6358
AM
26319+ case Opt_rdhash_def:
26320+ AuDbg("rdhash_def\n");
26321+ break;
1facf9fc 26322+ case Opt_xino:
26323+ u.xino = &opt->xino;
523b37e3 26324+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 26325+ break;
26326+ case Opt_trunc_xino:
26327+ AuLabel(trunc_xino);
26328+ break;
26329+ case Opt_notrunc_xino:
26330+ AuLabel(notrunc_xino);
26331+ break;
26332+ case Opt_trunc_xino_path:
26333+ case Opt_itrunc_xino:
26334+ u.xino_itrunc = &opt->xino_itrunc;
26335+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26336+ break;
1facf9fc 26337+ case Opt_noxino:
26338+ AuLabel(noxino);
26339+ break;
26340+ case Opt_trunc_xib:
26341+ AuLabel(trunc_xib);
26342+ break;
26343+ case Opt_notrunc_xib:
26344+ AuLabel(notrunc_xib);
26345+ break;
dece6358
AM
26346+ case Opt_shwh:
26347+ AuLabel(shwh);
26348+ break;
26349+ case Opt_noshwh:
26350+ AuLabel(noshwh);
26351+ break;
076b876e
AM
26352+ case Opt_dirperm1:
26353+ AuLabel(dirperm1);
26354+ break;
26355+ case Opt_nodirperm1:
26356+ AuLabel(nodirperm1);
26357+ break;
1facf9fc 26358+ case Opt_plink:
26359+ AuLabel(plink);
26360+ break;
26361+ case Opt_noplink:
26362+ AuLabel(noplink);
26363+ break;
26364+ case Opt_list_plink:
26365+ AuLabel(list_plink);
26366+ break;
26367+ case Opt_udba:
26368+ AuDbg("udba %d, %s\n",
26369+ opt->udba, au_optstr_udba(opt->udba));
26370+ break;
4a4d8108
AM
26371+ case Opt_dio:
26372+ AuLabel(dio);
26373+ break;
26374+ case Opt_nodio:
26375+ AuLabel(nodio);
26376+ break;
1facf9fc 26377+ case Opt_diropq_a:
26378+ AuLabel(diropq_a);
26379+ break;
26380+ case Opt_diropq_w:
26381+ AuLabel(diropq_w);
26382+ break;
26383+ case Opt_warn_perm:
26384+ AuLabel(warn_perm);
26385+ break;
26386+ case Opt_nowarn_perm:
26387+ AuLabel(nowarn_perm);
26388+ break;
1facf9fc 26389+ case Opt_verbose:
26390+ AuLabel(verbose);
26391+ break;
26392+ case Opt_noverbose:
26393+ AuLabel(noverbose);
26394+ break;
26395+ case Opt_sum:
26396+ AuLabel(sum);
26397+ break;
26398+ case Opt_nosum:
26399+ AuLabel(nosum);
26400+ break;
26401+ case Opt_wsum:
26402+ AuLabel(wsum);
26403+ break;
26404+ case Opt_wbr_create:
26405+ u.create = &opt->wbr_create;
26406+ AuDbg("create %d, %s\n", u.create->wbr_create,
26407+ au_optstr_wbr_create(u.create->wbr_create));
26408+ switch (u.create->wbr_create) {
26409+ case AuWbrCreate_MFSV:
26410+ case AuWbrCreate_PMFSV:
26411+ AuDbg("%d sec\n", u.create->mfs_second);
26412+ break;
26413+ case AuWbrCreate_MFSRR:
f2c43d5f 26414+ case AuWbrCreate_TDMFS:
1facf9fc 26415+ AuDbg("%llu watermark\n",
26416+ u.create->mfsrr_watermark);
26417+ break;
26418+ case AuWbrCreate_MFSRRV:
f2c43d5f 26419+ case AuWbrCreate_TDMFSV:
392086de 26420+ case AuWbrCreate_PMFSRRV:
1facf9fc 26421+ AuDbg("%llu watermark, %d sec\n",
26422+ u.create->mfsrr_watermark,
26423+ u.create->mfs_second);
26424+ break;
26425+ }
26426+ break;
26427+ case Opt_wbr_copyup:
26428+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26429+ au_optstr_wbr_copyup(opt->wbr_copyup));
26430+ break;
076b876e
AM
26431+ case Opt_fhsm_sec:
26432+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26433+ break;
8b6a4947
AM
26434+ case Opt_dirren:
26435+ AuLabel(dirren);
26436+ break;
26437+ case Opt_nodirren:
26438+ AuLabel(nodirren);
26439+ break;
c1595e42
JR
26440+ case Opt_acl:
26441+ AuLabel(acl);
26442+ break;
26443+ case Opt_noacl:
26444+ AuLabel(noacl);
26445+ break;
1facf9fc 26446+ default:
26447+ BUG();
26448+ }
26449+ opt++;
26450+ }
26451+#endif
26452+}
26453+
26454+void au_opts_free(struct au_opts *opts)
26455+{
26456+ struct au_opt *opt;
26457+
26458+ opt = opts->opt;
26459+ while (opt->type != Opt_tail) {
26460+ switch (opt->type) {
26461+ case Opt_add:
26462+ case Opt_append:
26463+ case Opt_prepend:
26464+ path_put(&opt->add.path);
26465+ break;
26466+ case Opt_del:
26467+ case Opt_idel:
26468+ path_put(&opt->del.h_path);
26469+ break;
26470+ case Opt_mod:
26471+ case Opt_imod:
26472+ dput(opt->mod.h_root);
26473+ break;
26474+ case Opt_xino:
26475+ fput(opt->xino.file);
26476+ break;
26477+ }
26478+ opt++;
26479+ }
26480+}
26481+
26482+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26483+ aufs_bindex_t bindex)
26484+{
26485+ int err;
26486+ struct au_opt_add *add = &opt->add;
26487+ char *p;
26488+
26489+ add->bindex = bindex;
1e00d052 26490+ add->perm = AuBrPerm_RO;
1facf9fc 26491+ add->pathname = opt_str;
26492+ p = strchr(opt_str, '=');
26493+ if (p) {
26494+ *p++ = 0;
26495+ if (*p)
26496+ add->perm = br_perm_val(p);
26497+ }
26498+
26499+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26500+ if (!err) {
26501+ if (!p) {
26502+ add->perm = AuBrPerm_RO;
26503+ if (au_test_fs_rr(add->path.dentry->d_sb))
26504+ add->perm = AuBrPerm_RR;
2121bcd9 26505+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 26506+ add->perm = AuBrPerm_RW;
26507+ }
26508+ opt->type = Opt_add;
26509+ goto out;
26510+ }
4a4d8108 26511+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26512+ err = -EINVAL;
26513+
4f0767ce 26514+out:
1facf9fc 26515+ return err;
26516+}
26517+
26518+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26519+{
26520+ int err;
26521+
26522+ del->pathname = args[0].from;
26523+ AuDbg("del path %s\n", del->pathname);
26524+
26525+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26526+ if (unlikely(err))
4a4d8108 26527+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26528+
26529+ return err;
26530+}
26531+
26532+#if 0 /* reserved for future use */
26533+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26534+ struct au_opt_del *del, substring_t args[])
26535+{
26536+ int err;
26537+ struct dentry *root;
26538+
26539+ err = -EINVAL;
26540+ root = sb->s_root;
26541+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26542+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26543+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26544+ goto out;
26545+ }
26546+
26547+ err = 0;
26548+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26549+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26550+
4f0767ce 26551+out:
1facf9fc 26552+ aufs_read_unlock(root, !AuLock_IR);
26553+ return err;
26554+}
26555+#endif
26556+
4a4d8108
AM
26557+static int noinline_for_stack
26558+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26559+{
26560+ int err;
26561+ struct path path;
26562+ char *p;
26563+
26564+ err = -EINVAL;
26565+ mod->path = args[0].from;
26566+ p = strchr(mod->path, '=');
26567+ if (unlikely(!p)) {
acd2b654 26568+ pr_err("no permission %s\n", args[0].from);
1facf9fc 26569+ goto out;
26570+ }
26571+
26572+ *p++ = 0;
26573+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26574+ if (unlikely(err)) {
4a4d8108 26575+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26576+ goto out;
26577+ }
26578+
26579+ mod->perm = br_perm_val(p);
26580+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26581+ mod->h_root = dget(path.dentry);
26582+ path_put(&path);
26583+
4f0767ce 26584+out:
1facf9fc 26585+ return err;
26586+}
26587+
26588+#if 0 /* reserved for future use */
26589+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26590+ struct au_opt_mod *mod, substring_t args[])
26591+{
26592+ int err;
26593+ struct dentry *root;
26594+
26595+ err = -EINVAL;
26596+ root = sb->s_root;
26597+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26598+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26599+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26600+ goto out;
26601+ }
26602+
26603+ err = 0;
26604+ mod->perm = br_perm_val(args[1].from);
26605+ AuDbg("mod path %s, perm 0x%x, %s\n",
26606+ mod->path, mod->perm, args[1].from);
26607+ mod->h_root = dget(au_h_dptr(root, bindex));
26608+
4f0767ce 26609+out:
1facf9fc 26610+ aufs_read_unlock(root, !AuLock_IR);
26611+ return err;
26612+}
26613+#endif
26614+
26615+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26616+ substring_t args[])
26617+{
26618+ int err;
26619+ struct file *file;
26620+
83b672a5 26621+ file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
1facf9fc 26622+ err = PTR_ERR(file);
26623+ if (IS_ERR(file))
26624+ goto out;
26625+
26626+ err = -EINVAL;
2000de60 26627+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26628+ fput(file);
4a4d8108 26629+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26630+ goto out;
26631+ }
26632+
26633+ err = 0;
26634+ xino->file = file;
26635+ xino->path = args[0].from;
26636+
4f0767ce 26637+out:
1facf9fc 26638+ return err;
26639+}
26640+
4a4d8108
AM
26641+static int noinline_for_stack
26642+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26643+ struct au_opt_xino_itrunc *xino_itrunc,
26644+ substring_t args[])
1facf9fc 26645+{
26646+ int err;
5afbbe0d 26647+ aufs_bindex_t bbot, bindex;
1facf9fc 26648+ struct path path;
26649+ struct dentry *root;
26650+
26651+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26652+ if (unlikely(err)) {
4a4d8108 26653+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26654+ goto out;
26655+ }
26656+
26657+ xino_itrunc->bindex = -1;
26658+ root = sb->s_root;
26659+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26660+ bbot = au_sbbot(sb);
26661+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26662+ if (au_h_dptr(root, bindex) == path.dentry) {
26663+ xino_itrunc->bindex = bindex;
26664+ break;
26665+ }
26666+ }
26667+ aufs_read_unlock(root, !AuLock_IR);
26668+ path_put(&path);
26669+
26670+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26671+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26672+ err = -EINVAL;
26673+ }
26674+
4f0767ce 26675+out:
1facf9fc 26676+ return err;
26677+}
26678+
26679+/* called without aufs lock */
26680+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26681+{
26682+ int err, n, token;
26683+ aufs_bindex_t bindex;
26684+ unsigned char skipped;
26685+ struct dentry *root;
26686+ struct au_opt *opt, *opt_tail;
26687+ char *opt_str;
26688+ /* reduce the stack space */
26689+ union {
26690+ struct au_opt_xino_itrunc *xino_itrunc;
26691+ struct au_opt_wbr_create *create;
26692+ } u;
26693+ struct {
26694+ substring_t args[MAX_OPT_ARGS];
26695+ } *a;
26696+
26697+ err = -ENOMEM;
26698+ a = kmalloc(sizeof(*a), GFP_NOFS);
26699+ if (unlikely(!a))
26700+ goto out;
26701+
26702+ root = sb->s_root;
26703+ err = 0;
26704+ bindex = 0;
26705+ opt = opts->opt;
26706+ opt_tail = opt + opts->max_opt - 1;
26707+ opt->type = Opt_tail;
26708+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26709+ err = -EINVAL;
26710+ skipped = 0;
26711+ token = match_token(opt_str, options, a->args);
26712+ switch (token) {
26713+ case Opt_br:
26714+ err = 0;
26715+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26716+ && *opt_str) {
26717+ err = opt_add(opt, opt_str, opts->sb_flags,
26718+ bindex++);
26719+ if (unlikely(!err && ++opt > opt_tail)) {
26720+ err = -E2BIG;
26721+ break;
26722+ }
26723+ opt->type = Opt_tail;
26724+ skipped = 1;
26725+ }
26726+ break;
26727+ case Opt_add:
26728+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26729+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26730+ break;
26731+ }
26732+ bindex = n;
26733+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26734+ bindex);
26735+ if (!err)
26736+ opt->type = token;
26737+ break;
26738+ case Opt_append:
26739+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26740+ /*dummy bindex*/1);
26741+ if (!err)
26742+ opt->type = token;
26743+ break;
26744+ case Opt_prepend:
26745+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26746+ /*bindex*/0);
26747+ if (!err)
26748+ opt->type = token;
26749+ break;
26750+ case Opt_del:
26751+ err = au_opts_parse_del(&opt->del, a->args);
26752+ if (!err)
26753+ opt->type = token;
26754+ break;
26755+#if 0 /* reserved for future use */
26756+ case Opt_idel:
26757+ del->pathname = "(indexed)";
26758+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26759+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26760+ break;
26761+ }
26762+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26763+ if (!err)
26764+ opt->type = token;
26765+ break;
26766+#endif
26767+ case Opt_mod:
26768+ err = au_opts_parse_mod(&opt->mod, a->args);
26769+ if (!err)
26770+ opt->type = token;
26771+ break;
26772+#ifdef IMOD /* reserved for future use */
26773+ case Opt_imod:
26774+ u.mod->path = "(indexed)";
26775+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26776+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26777+ break;
26778+ }
26779+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26780+ if (!err)
26781+ opt->type = token;
26782+ break;
26783+#endif
26784+ case Opt_xino:
26785+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26786+ if (!err)
26787+ opt->type = token;
26788+ break;
26789+
26790+ case Opt_trunc_xino_path:
26791+ err = au_opts_parse_xino_itrunc_path
26792+ (sb, &opt->xino_itrunc, a->args);
26793+ if (!err)
26794+ opt->type = token;
26795+ break;
26796+
26797+ case Opt_itrunc_xino:
26798+ u.xino_itrunc = &opt->xino_itrunc;
26799+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26800+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26801+ break;
26802+ }
26803+ u.xino_itrunc->bindex = n;
26804+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26805+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26806+ pr_err("out of bounds, %d\n", n);
1facf9fc 26807+ aufs_read_unlock(root, !AuLock_IR);
26808+ break;
26809+ }
26810+ aufs_read_unlock(root, !AuLock_IR);
26811+ err = 0;
26812+ opt->type = token;
26813+ break;
26814+
26815+ case Opt_dirwh:
26816+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26817+ break;
26818+ err = 0;
26819+ opt->type = token;
26820+ break;
26821+
26822+ case Opt_rdcache:
027c5e7a
AM
26823+ if (unlikely(match_int(&a->args[0], &n))) {
26824+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26825+ break;
027c5e7a
AM
26826+ }
26827+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26828+ pr_err("rdcache must be smaller than %d\n",
26829+ AUFS_RDCACHE_MAX);
26830+ break;
26831+ }
26832+ opt->rdcache = n;
1facf9fc 26833+ err = 0;
26834+ opt->type = token;
26835+ break;
26836+ case Opt_rdblk:
26837+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26838+ || n < 0
1facf9fc 26839+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26840+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26841+ break;
26842+ }
1308ab2a 26843+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26844+ pr_err("rdblk must be larger than %d\n",
26845+ NAME_MAX);
1facf9fc 26846+ break;
26847+ }
26848+ opt->rdblk = n;
26849+ err = 0;
26850+ opt->type = token;
26851+ break;
26852+ case Opt_rdhash:
26853+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26854+ || n < 0
1facf9fc 26855+ || n * sizeof(struct hlist_head)
26856+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26857+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26858+ break;
26859+ }
26860+ opt->rdhash = n;
26861+ err = 0;
26862+ opt->type = token;
26863+ break;
26864+
26865+ case Opt_trunc_xino:
26866+ case Opt_notrunc_xino:
26867+ case Opt_noxino:
26868+ case Opt_trunc_xib:
26869+ case Opt_notrunc_xib:
dece6358
AM
26870+ case Opt_shwh:
26871+ case Opt_noshwh:
076b876e
AM
26872+ case Opt_dirperm1:
26873+ case Opt_nodirperm1:
1facf9fc 26874+ case Opt_plink:
26875+ case Opt_noplink:
26876+ case Opt_list_plink:
4a4d8108
AM
26877+ case Opt_dio:
26878+ case Opt_nodio:
1facf9fc 26879+ case Opt_diropq_a:
26880+ case Opt_diropq_w:
26881+ case Opt_warn_perm:
26882+ case Opt_nowarn_perm:
1facf9fc 26883+ case Opt_verbose:
26884+ case Opt_noverbose:
26885+ case Opt_sum:
26886+ case Opt_nosum:
26887+ case Opt_wsum:
dece6358
AM
26888+ case Opt_rdblk_def:
26889+ case Opt_rdhash_def:
8b6a4947
AM
26890+ case Opt_dirren:
26891+ case Opt_nodirren:
c1595e42
JR
26892+ case Opt_acl:
26893+ case Opt_noacl:
1facf9fc 26894+ err = 0;
26895+ opt->type = token;
26896+ break;
26897+
26898+ case Opt_udba:
26899+ opt->udba = udba_val(a->args[0].from);
26900+ if (opt->udba >= 0) {
26901+ err = 0;
26902+ opt->type = token;
26903+ } else
4a4d8108 26904+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26905+ break;
26906+
26907+ case Opt_wbr_create:
26908+ u.create = &opt->wbr_create;
26909+ u.create->wbr_create
26910+ = au_wbr_create_val(a->args[0].from, u.create);
26911+ if (u.create->wbr_create >= 0) {
26912+ err = 0;
26913+ opt->type = token;
26914+ } else
4a4d8108 26915+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26916+ break;
26917+ case Opt_wbr_copyup:
26918+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26919+ if (opt->wbr_copyup >= 0) {
26920+ err = 0;
26921+ opt->type = token;
26922+ } else
4a4d8108 26923+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26924+ break;
26925+
076b876e
AM
26926+ case Opt_fhsm_sec:
26927+ if (unlikely(match_int(&a->args[0], &n)
26928+ || n < 0)) {
26929+ pr_err("bad integer in %s\n", opt_str);
26930+ break;
26931+ }
26932+ if (sysaufs_brs) {
26933+ opt->fhsm_second = n;
26934+ opt->type = token;
26935+ } else
26936+ pr_warn("ignored %s\n", opt_str);
26937+ err = 0;
26938+ break;
26939+
1facf9fc 26940+ case Opt_ignore:
0c3ec466 26941+ pr_warn("ignored %s\n", opt_str);
1facf9fc 26942+ /*FALLTHROUGH*/
26943+ case Opt_ignore_silent:
26944+ skipped = 1;
26945+ err = 0;
26946+ break;
26947+ case Opt_err:
4a4d8108 26948+ pr_err("unknown option %s\n", opt_str);
1facf9fc 26949+ break;
26950+ }
26951+
26952+ if (!err && !skipped) {
26953+ if (unlikely(++opt > opt_tail)) {
26954+ err = -E2BIG;
26955+ opt--;
26956+ opt->type = Opt_tail;
26957+ break;
26958+ }
26959+ opt->type = Opt_tail;
26960+ }
26961+ }
26962+
9f237c51 26963+ au_kfree_rcu(a);
1facf9fc 26964+ dump_opts(opts);
26965+ if (unlikely(err))
26966+ au_opts_free(opts);
26967+
4f0767ce 26968+out:
1facf9fc 26969+ return err;
26970+}
26971+
26972+static int au_opt_wbr_create(struct super_block *sb,
26973+ struct au_opt_wbr_create *create)
26974+{
26975+ int err;
26976+ struct au_sbinfo *sbinfo;
26977+
dece6358
AM
26978+ SiMustWriteLock(sb);
26979+
1facf9fc 26980+ err = 1; /* handled */
26981+ sbinfo = au_sbi(sb);
26982+ if (sbinfo->si_wbr_create_ops->fin) {
26983+ err = sbinfo->si_wbr_create_ops->fin(sb);
26984+ if (!err)
26985+ err = 1;
26986+ }
26987+
26988+ sbinfo->si_wbr_create = create->wbr_create;
26989+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26990+ switch (create->wbr_create) {
26991+ case AuWbrCreate_MFSRRV:
26992+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
26993+ case AuWbrCreate_TDMFS:
26994+ case AuWbrCreate_TDMFSV:
392086de
AM
26995+ case AuWbrCreate_PMFSRR:
26996+ case AuWbrCreate_PMFSRRV:
1facf9fc 26997+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26998+ /*FALLTHROUGH*/
26999+ case AuWbrCreate_MFS:
27000+ case AuWbrCreate_MFSV:
27001+ case AuWbrCreate_PMFS:
27002+ case AuWbrCreate_PMFSV:
e49829fe
JR
27003+ sbinfo->si_wbr_mfs.mfs_expire
27004+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 27005+ break;
27006+ }
27007+
27008+ if (sbinfo->si_wbr_create_ops->init)
27009+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27010+
27011+ return err;
27012+}
27013+
27014+/*
27015+ * returns,
27016+ * plus: processed without an error
27017+ * zero: unprocessed
27018+ */
27019+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27020+ struct au_opts *opts)
27021+{
27022+ int err;
27023+ struct au_sbinfo *sbinfo;
27024+
dece6358
AM
27025+ SiMustWriteLock(sb);
27026+
1facf9fc 27027+ err = 1; /* handled */
27028+ sbinfo = au_sbi(sb);
27029+ switch (opt->type) {
27030+ case Opt_udba:
27031+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27032+ sbinfo->si_mntflags |= opt->udba;
27033+ opts->given_udba |= opt->udba;
27034+ break;
27035+
27036+ case Opt_plink:
27037+ au_opt_set(sbinfo->si_mntflags, PLINK);
27038+ break;
27039+ case Opt_noplink:
27040+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 27041+ au_plink_put(sb, /*verbose*/1);
1facf9fc 27042+ au_opt_clr(sbinfo->si_mntflags, PLINK);
27043+ break;
27044+ case Opt_list_plink:
27045+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
27046+ au_plink_list(sb);
27047+ break;
27048+
4a4d8108
AM
27049+ case Opt_dio:
27050+ au_opt_set(sbinfo->si_mntflags, DIO);
27051+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27052+ break;
27053+ case Opt_nodio:
27054+ au_opt_clr(sbinfo->si_mntflags, DIO);
27055+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27056+ break;
27057+
076b876e
AM
27058+ case Opt_fhsm_sec:
27059+ au_fhsm_set(sbinfo, opt->fhsm_second);
27060+ break;
27061+
1facf9fc 27062+ case Opt_diropq_a:
27063+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27064+ break;
27065+ case Opt_diropq_w:
27066+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27067+ break;
27068+
27069+ case Opt_warn_perm:
27070+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27071+ break;
27072+ case Opt_nowarn_perm:
27073+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27074+ break;
27075+
1facf9fc 27076+ case Opt_verbose:
27077+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
27078+ break;
27079+ case Opt_noverbose:
27080+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27081+ break;
27082+
27083+ case Opt_sum:
27084+ au_opt_set(sbinfo->si_mntflags, SUM);
27085+ break;
27086+ case Opt_wsum:
27087+ au_opt_clr(sbinfo->si_mntflags, SUM);
27088+ au_opt_set(sbinfo->si_mntflags, SUM_W);
eca34b5c 27089+ break;
1facf9fc 27090+ case Opt_nosum:
27091+ au_opt_clr(sbinfo->si_mntflags, SUM);
27092+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
27093+ break;
27094+
27095+ case Opt_wbr_create:
27096+ err = au_opt_wbr_create(sb, &opt->wbr_create);
27097+ break;
27098+ case Opt_wbr_copyup:
27099+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
27100+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27101+ break;
27102+
27103+ case Opt_dirwh:
27104+ sbinfo->si_dirwh = opt->dirwh;
27105+ break;
27106+
27107+ case Opt_rdcache:
e49829fe
JR
27108+ sbinfo->si_rdcache
27109+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 27110+ break;
27111+ case Opt_rdblk:
27112+ sbinfo->si_rdblk = opt->rdblk;
27113+ break;
dece6358
AM
27114+ case Opt_rdblk_def:
27115+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27116+ break;
1facf9fc 27117+ case Opt_rdhash:
27118+ sbinfo->si_rdhash = opt->rdhash;
27119+ break;
dece6358
AM
27120+ case Opt_rdhash_def:
27121+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27122+ break;
27123+
27124+ case Opt_shwh:
27125+ au_opt_set(sbinfo->si_mntflags, SHWH);
27126+ break;
27127+ case Opt_noshwh:
27128+ au_opt_clr(sbinfo->si_mntflags, SHWH);
27129+ break;
1facf9fc 27130+
076b876e
AM
27131+ case Opt_dirperm1:
27132+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27133+ break;
27134+ case Opt_nodirperm1:
27135+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27136+ break;
27137+
1facf9fc 27138+ case Opt_trunc_xino:
27139+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27140+ break;
27141+ case Opt_notrunc_xino:
27142+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27143+ break;
27144+
27145+ case Opt_trunc_xino_path:
27146+ case Opt_itrunc_xino:
acd2b654
AM
27147+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27148+ /*idx_begin*/0);
1facf9fc 27149+ if (!err)
27150+ err = 1;
27151+ break;
27152+
27153+ case Opt_trunc_xib:
27154+ au_fset_opts(opts->flags, TRUNC_XIB);
27155+ break;
27156+ case Opt_notrunc_xib:
27157+ au_fclr_opts(opts->flags, TRUNC_XIB);
27158+ break;
27159+
8b6a4947
AM
27160+ case Opt_dirren:
27161+ err = 1;
27162+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27163+ err = au_dr_opt_set(sb);
27164+ if (!err)
27165+ err = 1;
27166+ }
27167+ if (err == 1)
27168+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27169+ break;
27170+ case Opt_nodirren:
27171+ err = 1;
27172+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27173+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27174+ DR_FLUSHED));
27175+ if (!err)
27176+ err = 1;
27177+ }
27178+ if (err == 1)
27179+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
27180+ break;
27181+
c1595e42 27182+ case Opt_acl:
2121bcd9 27183+ sb->s_flags |= SB_POSIXACL;
c1595e42
JR
27184+ break;
27185+ case Opt_noacl:
2121bcd9 27186+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
27187+ break;
27188+
1facf9fc 27189+ default:
27190+ err = 0;
27191+ break;
27192+ }
27193+
27194+ return err;
27195+}
27196+
27197+/*
27198+ * returns tri-state.
27199+ * plus: processed without an error
27200+ * zero: unprocessed
27201+ * minus: error
27202+ */
27203+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27204+ struct au_opts *opts)
27205+{
27206+ int err, do_refresh;
27207+
27208+ err = 0;
27209+ switch (opt->type) {
27210+ case Opt_append:
5afbbe0d 27211+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 27212+ if (opt->add.bindex < 0)
27213+ opt->add.bindex = 0;
27214+ goto add;
eca34b5c 27215+ /* Always goto add, not fallthrough */
1facf9fc 27216+ case Opt_prepend:
27217+ opt->add.bindex = 0;
eca34b5c 27218+ /* fallthrough */
f6b6e03d 27219+ add: /* indented label */
1facf9fc 27220+ case Opt_add:
27221+ err = au_br_add(sb, &opt->add,
27222+ au_ftest_opts(opts->flags, REMOUNT));
27223+ if (!err) {
27224+ err = 1;
027c5e7a 27225+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27226+ }
27227+ break;
27228+
27229+ case Opt_del:
27230+ case Opt_idel:
27231+ err = au_br_del(sb, &opt->del,
27232+ au_ftest_opts(opts->flags, REMOUNT));
27233+ if (!err) {
27234+ err = 1;
27235+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 27236+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27237+ }
27238+ break;
27239+
27240+ case Opt_mod:
27241+ case Opt_imod:
27242+ err = au_br_mod(sb, &opt->mod,
27243+ au_ftest_opts(opts->flags, REMOUNT),
27244+ &do_refresh);
27245+ if (!err) {
27246+ err = 1;
027c5e7a
AM
27247+ if (do_refresh)
27248+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27249+ }
27250+ break;
27251+ }
1facf9fc 27252+ return err;
27253+}
27254+
27255+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27256+ struct au_opt_xino **opt_xino,
27257+ struct au_opts *opts)
27258+{
27259+ int err;
1facf9fc 27260+
27261+ err = 0;
27262+ switch (opt->type) {
27263+ case Opt_xino:
062440b3
AM
27264+ err = au_xino_set(sb, &opt->xino,
27265+ !!au_ftest_opts(opts->flags, REMOUNT));
27266+ if (unlikely(err))
27267+ break;
27268+
27269+ *opt_xino = &opt->xino;
1facf9fc 27270+ break;
27271+
27272+ case Opt_noxino:
27273+ au_xino_clr(sb);
1facf9fc 27274+ *opt_xino = (void *)-1;
27275+ break;
27276+ }
27277+
27278+ return err;
27279+}
27280+
27281+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27282+ unsigned int pending)
27283+{
076b876e 27284+ int err, fhsm;
5afbbe0d 27285+ aufs_bindex_t bindex, bbot;
79b8bda9 27286+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 27287+ struct au_branch *br;
27288+ struct au_wbr *wbr;
79b8bda9 27289+ struct dentry *root, *dentry;
1facf9fc 27290+ struct inode *dir, *h_dir;
27291+ struct au_sbinfo *sbinfo;
27292+ struct au_hinode *hdir;
27293+
dece6358
AM
27294+ SiMustAnyLock(sb);
27295+
1facf9fc 27296+ sbinfo = au_sbi(sb);
27297+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27298+
2121bcd9 27299+ if (!(sb_flags & SB_RDONLY)) {
dece6358 27300+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 27301+ pr_warn("first branch should be rw\n");
dece6358 27302+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 27303+ pr_warn_once("shwh should be used with ro\n");
dece6358 27304+ }
1facf9fc 27305+
4a4d8108 27306+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 27307+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 27308+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 27309+
076b876e 27310+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
27311+ pr_warn_once("dirperm1 breaks the protection"
27312+ " by the permission bits on the lower branch\n");
076b876e 27313+
1facf9fc 27314+ err = 0;
076b876e 27315+ fhsm = 0;
1facf9fc 27316+ root = sb->s_root;
5527c038 27317+ dir = d_inode(root);
1facf9fc 27318+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
27319+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27320+ UDBA_NONE);
5afbbe0d
AM
27321+ bbot = au_sbbot(sb);
27322+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 27323+ skip = 0;
27324+ h_dir = au_h_iptr(dir, bindex);
27325+ br = au_sbr(sb, bindex);
1facf9fc 27326+
c1595e42
JR
27327+ if ((br->br_perm & AuBrAttr_ICEX)
27328+ && !h_dir->i_op->listxattr)
27329+ br->br_perm &= ~AuBrAttr_ICEX;
27330+#if 0
27331+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 27332+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
27333+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27334+#endif
27335+
27336+ do_free = 0;
1facf9fc 27337+ wbr = br->br_wbr;
27338+ if (wbr)
27339+ wbr_wh_read_lock(wbr);
27340+
1e00d052 27341+ if (!au_br_writable(br->br_perm)) {
1facf9fc 27342+ do_free = !!wbr;
27343+ skip = (!wbr
27344+ || (!wbr->wbr_whbase
27345+ && !wbr->wbr_plink
27346+ && !wbr->wbr_orph));
1e00d052 27347+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 27348+ /* skip = (!br->br_whbase && !br->br_orph); */
27349+ skip = (!wbr || !wbr->wbr_whbase);
27350+ if (skip && wbr) {
27351+ if (do_plink)
27352+ skip = !!wbr->wbr_plink;
27353+ else
27354+ skip = !wbr->wbr_plink;
27355+ }
1e00d052 27356+ } else {
1facf9fc 27357+ /* skip = (br->br_whbase && br->br_ohph); */
27358+ skip = (wbr && wbr->wbr_whbase);
27359+ if (skip) {
27360+ if (do_plink)
27361+ skip = !!wbr->wbr_plink;
27362+ else
27363+ skip = !wbr->wbr_plink;
27364+ }
1facf9fc 27365+ }
27366+ if (wbr)
27367+ wbr_wh_read_unlock(wbr);
27368+
79b8bda9
AM
27369+ if (can_no_dreval) {
27370+ dentry = br->br_path.dentry;
27371+ spin_lock(&dentry->d_lock);
27372+ if (dentry->d_flags &
27373+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27374+ can_no_dreval = 0;
27375+ spin_unlock(&dentry->d_lock);
27376+ }
27377+
076b876e
AM
27378+ if (au_br_fhsm(br->br_perm)) {
27379+ fhsm++;
27380+ AuDebugOn(!br->br_fhsm);
27381+ }
27382+
1facf9fc 27383+ if (skip)
27384+ continue;
27385+
27386+ hdir = au_hi(dir, bindex);
5afbbe0d 27387+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27388+ if (wbr)
27389+ wbr_wh_write_lock(wbr);
86dc4139 27390+ err = au_wh_init(br, sb);
1facf9fc 27391+ if (wbr)
27392+ wbr_wh_write_unlock(wbr);
5afbbe0d 27393+ au_hn_inode_unlock(hdir);
1facf9fc 27394+
27395+ if (!err && do_free) {
9f237c51 27396+ au_kfree_rcu(wbr);
1facf9fc 27397+ br->br_wbr = NULL;
27398+ }
27399+ }
27400+
79b8bda9
AM
27401+ if (can_no_dreval)
27402+ au_fset_si(sbinfo, NO_DREVAL);
27403+ else
27404+ au_fclr_si(sbinfo, NO_DREVAL);
27405+
c1595e42 27406+ if (fhsm >= 2) {
076b876e 27407+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27408+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27409+ br = au_sbr(sb, bindex);
27410+ if (au_br_fhsm(br->br_perm)) {
27411+ au_fhsm_set_bottom(sb, bindex);
27412+ break;
27413+ }
27414+ }
27415+ } else {
076b876e 27416+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27417+ au_fhsm_set_bottom(sb, -1);
27418+ }
076b876e 27419+
1facf9fc 27420+ return err;
27421+}
27422+
27423+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27424+{
27425+ int err;
27426+ unsigned int tmp;
5afbbe0d 27427+ aufs_bindex_t bindex, bbot;
1facf9fc 27428+ struct au_opt *opt;
27429+ struct au_opt_xino *opt_xino, xino;
27430+ struct au_sbinfo *sbinfo;
027c5e7a 27431+ struct au_branch *br;
076b876e 27432+ struct inode *dir;
1facf9fc 27433+
dece6358
AM
27434+ SiMustWriteLock(sb);
27435+
1facf9fc 27436+ err = 0;
27437+ opt_xino = NULL;
27438+ opt = opts->opt;
27439+ while (err >= 0 && opt->type != Opt_tail)
27440+ err = au_opt_simple(sb, opt++, opts);
27441+ if (err > 0)
27442+ err = 0;
27443+ else if (unlikely(err < 0))
27444+ goto out;
27445+
27446+ /* disable xino and udba temporary */
27447+ sbinfo = au_sbi(sb);
27448+ tmp = sbinfo->si_mntflags;
27449+ au_opt_clr(sbinfo->si_mntflags, XINO);
27450+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27451+
27452+ opt = opts->opt;
27453+ while (err >= 0 && opt->type != Opt_tail)
27454+ err = au_opt_br(sb, opt++, opts);
27455+ if (err > 0)
27456+ err = 0;
27457+ else if (unlikely(err < 0))
27458+ goto out;
27459+
5afbbe0d
AM
27460+ bbot = au_sbbot(sb);
27461+ if (unlikely(bbot < 0)) {
1facf9fc 27462+ err = -EINVAL;
4a4d8108 27463+ pr_err("no branches\n");
1facf9fc 27464+ goto out;
27465+ }
27466+
27467+ if (au_opt_test(tmp, XINO))
27468+ au_opt_set(sbinfo->si_mntflags, XINO);
27469+ opt = opts->opt;
27470+ while (!err && opt->type != Opt_tail)
27471+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27472+ if (unlikely(err))
27473+ goto out;
27474+
27475+ err = au_opts_verify(sb, sb->s_flags, tmp);
27476+ if (unlikely(err))
27477+ goto out;
27478+
27479+ /* restore xino */
27480+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27481+ xino.file = au_xino_def(sb);
27482+ err = PTR_ERR(xino.file);
27483+ if (IS_ERR(xino.file))
27484+ goto out;
27485+
27486+ err = au_xino_set(sb, &xino, /*remount*/0);
27487+ fput(xino.file);
27488+ if (unlikely(err))
27489+ goto out;
27490+ }
27491+
27492+ /* restore udba */
027c5e7a 27493+ tmp &= AuOptMask_UDBA;
1facf9fc 27494+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27495+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27496+ bbot = au_sbbot(sb);
27497+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27498+ br = au_sbr(sb, bindex);
27499+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27500+ if (unlikely(err))
27501+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27502+ bindex, err);
27503+ /* go on even if err */
27504+ }
4a4d8108 27505+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27506+ dir = d_inode(sb->s_root);
4a4d8108 27507+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27508+ }
27509+
4f0767ce 27510+out:
1facf9fc 27511+ return err;
27512+}
27513+
27514+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27515+{
27516+ int err, rerr;
79b8bda9 27517+ unsigned char no_dreval;
1facf9fc 27518+ struct inode *dir;
27519+ struct au_opt_xino *opt_xino;
27520+ struct au_opt *opt;
27521+ struct au_sbinfo *sbinfo;
27522+
dece6358
AM
27523+ SiMustWriteLock(sb);
27524+
8b6a4947
AM
27525+ err = au_dr_opt_flush(sb);
27526+ if (unlikely(err))
27527+ goto out;
27528+ au_fset_opts(opts->flags, DR_FLUSHED);
27529+
5527c038 27530+ dir = d_inode(sb->s_root);
1facf9fc 27531+ sbinfo = au_sbi(sb);
1facf9fc 27532+ opt_xino = NULL;
27533+ opt = opts->opt;
27534+ while (err >= 0 && opt->type != Opt_tail) {
27535+ err = au_opt_simple(sb, opt, opts);
27536+ if (!err)
27537+ err = au_opt_br(sb, opt, opts);
27538+ if (!err)
27539+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27540+ opt++;
27541+ }
27542+ if (err > 0)
27543+ err = 0;
27544+ AuTraceErr(err);
27545+ /* go on even err */
27546+
79b8bda9 27547+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27548+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27549+ if (unlikely(rerr && !err))
27550+ err = rerr;
27551+
79b8bda9 27552+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27553+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27554+
1facf9fc 27555+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27556+ rerr = au_xib_trunc(sb);
27557+ if (unlikely(rerr && !err))
27558+ err = rerr;
27559+ }
27560+
27561+ /* will be handled by the caller */
027c5e7a 27562+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27563+ && (opts->given_udba
27564+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27565+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27566+ ))
027c5e7a 27567+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27568+
27569+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27570+
27571+out:
1facf9fc 27572+ return err;
27573+}
27574+
27575+/* ---------------------------------------------------------------------- */
27576+
27577+unsigned int au_opt_udba(struct super_block *sb)
27578+{
27579+ return au_mntflags(sb) & AuOptMask_UDBA;
27580+}
7f207e10 27581diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
eca34b5c
AM
27582--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
27583+++ linux/fs/aufs/opts.h 2019-07-11 15:42:14.472238057 +0200
062440b3
AM
27584@@ -0,0 +1,225 @@
27585+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27586+/*
ba1aed25 27587+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 27588+ *
27589+ * This program, aufs is free software; you can redistribute it and/or modify
27590+ * it under the terms of the GNU General Public License as published by
27591+ * the Free Software Foundation; either version 2 of the License, or
27592+ * (at your option) any later version.
dece6358
AM
27593+ *
27594+ * This program is distributed in the hope that it will be useful,
27595+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27596+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27597+ * GNU General Public License for more details.
27598+ *
27599+ * You should have received a copy of the GNU General Public License
523b37e3 27600+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27601+ */
27602+
27603+/*
27604+ * mount options/flags
27605+ */
27606+
27607+#ifndef __AUFS_OPTS_H__
27608+#define __AUFS_OPTS_H__
27609+
27610+#ifdef __KERNEL__
27611+
dece6358 27612+#include <linux/path.h>
1facf9fc 27613+
dece6358 27614+struct file;
dece6358 27615+
1facf9fc 27616+/* ---------------------------------------------------------------------- */
27617+
27618+/* mount flags */
27619+#define AuOpt_XINO 1 /* external inode number bitmap
27620+ and translation table */
27621+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27622+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27623+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27624+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27625+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27626+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27627+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27628+ bits */
dece6358
AM
27629+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27630+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27631+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27632+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
eca801bf 27633+#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
4a4d8108 27634+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27635+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27636+
4a4d8108
AM
27637+#ifndef CONFIG_AUFS_HNOTIFY
27638+#undef AuOpt_UDBA_HNOTIFY
27639+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27640+#endif
8b6a4947
AM
27641+#ifndef CONFIG_AUFS_DIRREN
27642+#undef AuOpt_DIRREN
27643+#define AuOpt_DIRREN 0
27644+#endif
dece6358
AM
27645+#ifndef CONFIG_AUFS_SHWH
27646+#undef AuOpt_SHWH
27647+#define AuOpt_SHWH 0
27648+#endif
1facf9fc 27649+
27650+#define AuOpt_Def (AuOpt_XINO \
27651+ | AuOpt_UDBA_REVAL \
27652+ | AuOpt_PLINK \
27653+ /* | AuOpt_DIRPERM1 */ \
27654+ | AuOpt_WARN_PERM)
27655+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27656+ | AuOpt_UDBA_REVAL \
4a4d8108 27657+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27658+
27659+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27660+#define au_opt_set(flags, name) do { \
27661+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27662+ ((flags) |= AuOpt_##name); \
27663+} while (0)
27664+#define au_opt_set_udba(flags, name) do { \
27665+ (flags) &= ~AuOptMask_UDBA; \
27666+ ((flags) |= AuOpt_##name); \
27667+} while (0)
7f207e10
AM
27668+#define au_opt_clr(flags, name) do { \
27669+ ((flags) &= ~AuOpt_##name); \
27670+} while (0)
1facf9fc 27671+
e49829fe
JR
27672+static inline unsigned int au_opts_plink(unsigned int mntflags)
27673+{
27674+#ifdef CONFIG_PROC_FS
27675+ return mntflags;
27676+#else
27677+ return mntflags & ~AuOpt_PLINK;
27678+#endif
27679+}
27680+
1facf9fc 27681+/* ---------------------------------------------------------------------- */
27682+
27683+/* policies to select one among multiple writable branches */
27684+enum {
27685+ AuWbrCreate_TDP, /* top down parent */
27686+ AuWbrCreate_RR, /* round robin */
27687+ AuWbrCreate_MFS, /* most free space */
27688+ AuWbrCreate_MFSV, /* mfs with seconds */
27689+ AuWbrCreate_MFSRR, /* mfs then rr */
27690+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27691+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27692+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27693+ AuWbrCreate_PMFS, /* parent and mfs */
27694+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27695+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27696+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27697+
27698+ AuWbrCreate_Def = AuWbrCreate_TDP
27699+};
27700+
27701+enum {
27702+ AuWbrCopyup_TDP, /* top down parent */
27703+ AuWbrCopyup_BUP, /* bottom up parent */
27704+ AuWbrCopyup_BU, /* bottom up */
27705+
27706+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27707+};
27708+
27709+/* ---------------------------------------------------------------------- */
27710+
27711+struct au_opt_add {
27712+ aufs_bindex_t bindex;
27713+ char *pathname;
27714+ int perm;
27715+ struct path path;
27716+};
27717+
27718+struct au_opt_del {
27719+ char *pathname;
27720+ struct path h_path;
27721+};
27722+
27723+struct au_opt_mod {
27724+ char *path;
27725+ int perm;
27726+ struct dentry *h_root;
27727+};
27728+
27729+struct au_opt_xino {
27730+ char *path;
27731+ struct file *file;
27732+};
27733+
27734+struct au_opt_xino_itrunc {
27735+ aufs_bindex_t bindex;
27736+};
27737+
27738+struct au_opt_wbr_create {
27739+ int wbr_create;
27740+ int mfs_second;
27741+ unsigned long long mfsrr_watermark;
27742+};
27743+
27744+struct au_opt {
27745+ int type;
27746+ union {
27747+ struct au_opt_xino xino;
27748+ struct au_opt_xino_itrunc xino_itrunc;
27749+ struct au_opt_add add;
27750+ struct au_opt_del del;
27751+ struct au_opt_mod mod;
27752+ int dirwh;
27753+ int rdcache;
27754+ unsigned int rdblk;
27755+ unsigned int rdhash;
27756+ int udba;
27757+ struct au_opt_wbr_create wbr_create;
27758+ int wbr_copyup;
076b876e 27759+ unsigned int fhsm_second;
1facf9fc 27760+ };
27761+};
27762+
27763+/* opts flags */
27764+#define AuOpts_REMOUNT 1
027c5e7a
AM
27765+#define AuOpts_REFRESH (1 << 1)
27766+#define AuOpts_TRUNC_XIB (1 << 2)
27767+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27768+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27769+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27770+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27771+#define au_fset_opts(flags, name) \
27772+ do { (flags) |= AuOpts_##name; } while (0)
27773+#define au_fclr_opts(flags, name) \
27774+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27775+
8b6a4947
AM
27776+#ifndef CONFIG_AUFS_DIRREN
27777+#undef AuOpts_DR_FLUSHED
27778+#define AuOpts_DR_FLUSHED 0
27779+#endif
27780+
1facf9fc 27781+struct au_opts {
27782+ struct au_opt *opt;
27783+ int max_opt;
27784+
27785+ unsigned int given_udba;
27786+ unsigned int flags;
27787+ unsigned long sb_flags;
27788+};
27789+
27790+/* ---------------------------------------------------------------------- */
27791+
7e9cd9fe 27792+/* opts.c */
076b876e 27793+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27794+const char *au_optstr_udba(int udba);
27795+const char *au_optstr_wbr_copyup(int wbr_copyup);
27796+const char *au_optstr_wbr_create(int wbr_create);
27797+
27798+void au_opts_free(struct au_opts *opts);
3c1bdaff 27799+struct super_block;
1facf9fc 27800+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27801+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27802+ unsigned int pending);
27803+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27804+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27805+
27806+unsigned int au_opt_udba(struct super_block *sb);
27807+
1facf9fc 27808+#endif /* __KERNEL__ */
27809+#endif /* __AUFS_OPTS_H__ */
7f207e10 27810diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
eca34b5c
AM
27811--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
27812+++ linux/fs/aufs/plink.c 2019-07-11 15:42:14.472238057 +0200
062440b3 27813@@ -0,0 +1,516 @@
cd7a4cd9 27814+// SPDX-License-Identifier: GPL-2.0
1facf9fc 27815+/*
ba1aed25 27816+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 27817+ *
27818+ * This program, aufs is free software; you can redistribute it and/or modify
27819+ * it under the terms of the GNU General Public License as published by
27820+ * the Free Software Foundation; either version 2 of the License, or
27821+ * (at your option) any later version.
dece6358
AM
27822+ *
27823+ * This program is distributed in the hope that it will be useful,
27824+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27825+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27826+ * GNU General Public License for more details.
27827+ *
27828+ * You should have received a copy of the GNU General Public License
523b37e3 27829+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27830+ */
27831+
27832+/*
27833+ * pseudo-link
27834+ */
27835+
27836+#include "aufs.h"
27837+
27838+/*
e49829fe 27839+ * the pseudo-link maintenance mode.
1facf9fc 27840+ * during a user process maintains the pseudo-links,
27841+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27842+ *
27843+ * Flags
27844+ * NOPLM:
27845+ * For entry functions which will handle plink, and i_mutex is already held
27846+ * in VFS.
27847+ * They cannot wait and should return an error at once.
27848+ * Callers has to check the error.
27849+ * NOPLMW:
27850+ * For entry functions which will handle plink, but i_mutex is not held
27851+ * in VFS.
27852+ * They can wait the plink maintenance mode to finish.
27853+ *
27854+ * They behave like F_SETLK and F_SETLKW.
27855+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27856+ */
e49829fe
JR
27857+
27858+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27859+{
e49829fe
JR
27860+ int err;
27861+ pid_t pid, ppid;
f0c0a007 27862+ struct task_struct *parent, *prev;
e49829fe 27863+ struct au_sbinfo *sbi;
dece6358
AM
27864+
27865+ SiMustAnyLock(sb);
27866+
e49829fe
JR
27867+ err = 0;
27868+ if (!au_opt_test(au_mntflags(sb), PLINK))
27869+ goto out;
27870+
27871+ sbi = au_sbi(sb);
27872+ pid = sbi->si_plink_maint_pid;
27873+ if (!pid || pid == current->pid)
27874+ goto out;
27875+
27876+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27877+ prev = NULL;
27878+ parent = current;
27879+ ppid = 0;
e49829fe 27880+ rcu_read_lock();
f0c0a007
AM
27881+ while (1) {
27882+ parent = rcu_dereference(parent->real_parent);
27883+ if (parent == prev)
27884+ break;
27885+ ppid = task_pid_vnr(parent);
27886+ if (pid == ppid) {
27887+ rcu_read_unlock();
27888+ goto out;
27889+ }
27890+ prev = parent;
27891+ }
e49829fe 27892+ rcu_read_unlock();
e49829fe
JR
27893+
27894+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27895+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27896+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27897+ while (sbi->si_plink_maint_pid) {
27898+ si_read_unlock(sb);
27899+ /* gave up wake_up_bit() */
27900+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27901+
27902+ if (au_ftest_lock(flags, FLUSH))
27903+ au_nwt_flush(&sbi->si_nowait);
27904+ si_noflush_read_lock(sb);
27905+ }
27906+ } else if (au_ftest_lock(flags, NOPLM)) {
27907+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27908+ err = -EAGAIN;
27909+ }
27910+
27911+out:
27912+ return err;
4a4d8108
AM
27913+}
27914+
e49829fe 27915+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 27916+{
4a4d8108 27917+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 27918+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 27919+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 27920+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
27921+}
27922+
e49829fe 27923+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
27924+{
27925+ int err;
4a4d8108
AM
27926+ struct au_sbinfo *sbinfo;
27927+
27928+ err = 0;
4a4d8108
AM
27929+ sbinfo = au_sbi(sb);
27930+ /* make sure i am the only one in this fs */
e49829fe
JR
27931+ si_write_lock(sb, AuLock_FLUSH);
27932+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27933+ spin_lock(&sbinfo->si_plink_maint_lock);
27934+ if (!sbinfo->si_plink_maint_pid)
27935+ sbinfo->si_plink_maint_pid = current->pid;
27936+ else
27937+ err = -EBUSY;
27938+ spin_unlock(&sbinfo->si_plink_maint_lock);
27939+ }
4a4d8108
AM
27940+ si_write_unlock(sb);
27941+
27942+ return err;
1facf9fc 27943+}
27944+
27945+/* ---------------------------------------------------------------------- */
27946+
1facf9fc 27947+#ifdef CONFIG_AUFS_DEBUG
27948+void au_plink_list(struct super_block *sb)
27949+{
86dc4139 27950+ int i;
1facf9fc 27951+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27952+ struct hlist_bl_head *hbl;
27953+ struct hlist_bl_node *pos;
5afbbe0d 27954+ struct au_icntnr *icntnr;
1facf9fc 27955+
dece6358
AM
27956+ SiMustAnyLock(sb);
27957+
1facf9fc 27958+ sbinfo = au_sbi(sb);
27959+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27960+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27961+
86dc4139 27962+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27963+ hbl = sbinfo->si_plink + i;
27964+ hlist_bl_lock(hbl);
27965+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27966+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 27967+ hlist_bl_unlock(hbl);
86dc4139 27968+ }
1facf9fc 27969+}
27970+#endif
27971+
27972+/* is the inode pseudo-linked? */
27973+int au_plink_test(struct inode *inode)
27974+{
86dc4139 27975+ int found, i;
1facf9fc 27976+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27977+ struct hlist_bl_head *hbl;
27978+ struct hlist_bl_node *pos;
5afbbe0d 27979+ struct au_icntnr *icntnr;
1facf9fc 27980+
27981+ sbinfo = au_sbi(inode->i_sb);
dece6358 27982+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 27983+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 27984+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 27985+
27986+ found = 0;
86dc4139 27987+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
27988+ hbl = sbinfo->si_plink + i;
27989+ hlist_bl_lock(hbl);
27990+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27991+ if (&icntnr->vfs_inode == inode) {
1facf9fc 27992+ found = 1;
27993+ break;
27994+ }
8b6a4947 27995+ hlist_bl_unlock(hbl);
1facf9fc 27996+ return found;
27997+}
27998+
27999+/* ---------------------------------------------------------------------- */
28000+
28001+/*
28002+ * generate a name for plink.
28003+ * the file will be stored under AUFS_WH_PLINKDIR.
28004+ */
28005+/* 20 is max digits length of ulong 64 */
28006+#define PLINK_NAME_LEN ((20 + 1) * 2)
28007+
28008+static int plink_name(char *name, int len, struct inode *inode,
28009+ aufs_bindex_t bindex)
28010+{
28011+ int rlen;
28012+ struct inode *h_inode;
28013+
28014+ h_inode = au_h_iptr(inode, bindex);
28015+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28016+ return rlen;
28017+}
28018+
7f207e10
AM
28019+struct au_do_plink_lkup_args {
28020+ struct dentry **errp;
28021+ struct qstr *tgtname;
28022+ struct dentry *h_parent;
28023+ struct au_branch *br;
28024+};
28025+
28026+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28027+ struct dentry *h_parent,
28028+ struct au_branch *br)
28029+{
28030+ struct dentry *h_dentry;
febd17d6 28031+ struct inode *h_inode;
7f207e10 28032+
febd17d6 28033+ h_inode = d_inode(h_parent);
be118d29 28034+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 28035+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 28036+ inode_unlock_shared(h_inode);
7f207e10
AM
28037+ return h_dentry;
28038+}
28039+
28040+static void au_call_do_plink_lkup(void *args)
28041+{
28042+ struct au_do_plink_lkup_args *a = args;
28043+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
28044+}
28045+
1facf9fc 28046+/* lookup the plink-ed @inode under the branch at @bindex */
28047+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28048+{
28049+ struct dentry *h_dentry, *h_parent;
28050+ struct au_branch *br;
7f207e10 28051+ int wkq_err;
1facf9fc 28052+ char a[PLINK_NAME_LEN];
0c3ec466 28053+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28054+
e49829fe
JR
28055+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28056+
1facf9fc 28057+ br = au_sbr(inode->i_sb, bindex);
28058+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 28059+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28060+
2dfbb274 28061+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
28062+ struct au_do_plink_lkup_args args = {
28063+ .errp = &h_dentry,
28064+ .tgtname = &tgtname,
28065+ .h_parent = h_parent,
28066+ .br = br
28067+ };
28068+
28069+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28070+ if (unlikely(wkq_err))
28071+ h_dentry = ERR_PTR(wkq_err);
28072+ } else
28073+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
28074+
1facf9fc 28075+ return h_dentry;
28076+}
28077+
28078+/* create a pseudo-link */
28079+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
28080+ struct dentry *h_dentry, struct au_branch *br)
28081+{
28082+ int err;
28083+ struct path h_path = {
86dc4139 28084+ .mnt = au_br_mnt(br)
1facf9fc 28085+ };
523b37e3 28086+ struct inode *h_dir, *delegated;
1facf9fc 28087+
5527c038 28088+ h_dir = d_inode(h_parent);
febd17d6 28089+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 28090+again:
b4510431 28091+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 28092+ err = PTR_ERR(h_path.dentry);
28093+ if (IS_ERR(h_path.dentry))
28094+ goto out;
28095+
28096+ err = 0;
28097+ /* wh.plink dir is not monitored */
7f207e10 28098+ /* todo: is it really safe? */
5527c038
JR
28099+ if (d_is_positive(h_path.dentry)
28100+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
28101+ delegated = NULL;
28102+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28103+ if (unlikely(err == -EWOULDBLOCK)) {
28104+ pr_warn("cannot retry for NFSv4 delegation"
28105+ " for an internal unlink\n");
28106+ iput(delegated);
28107+ }
1facf9fc 28108+ dput(h_path.dentry);
28109+ h_path.dentry = NULL;
28110+ if (!err)
28111+ goto again;
28112+ }
5527c038 28113+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
28114+ delegated = NULL;
28115+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28116+ if (unlikely(err == -EWOULDBLOCK)) {
28117+ pr_warn("cannot retry for NFSv4 delegation"
28118+ " for an internal link\n");
28119+ iput(delegated);
28120+ }
28121+ }
1facf9fc 28122+ dput(h_path.dentry);
28123+
4f0767ce 28124+out:
febd17d6 28125+ inode_unlock(h_dir);
1facf9fc 28126+ return err;
28127+}
28128+
28129+struct do_whplink_args {
28130+ int *errp;
28131+ struct qstr *tgt;
28132+ struct dentry *h_parent;
28133+ struct dentry *h_dentry;
28134+ struct au_branch *br;
28135+};
28136+
28137+static void call_do_whplink(void *args)
28138+{
28139+ struct do_whplink_args *a = args;
28140+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28141+}
28142+
28143+static int whplink(struct dentry *h_dentry, struct inode *inode,
28144+ aufs_bindex_t bindex, struct au_branch *br)
28145+{
28146+ int err, wkq_err;
28147+ struct au_wbr *wbr;
28148+ struct dentry *h_parent;
1facf9fc 28149+ char a[PLINK_NAME_LEN];
0c3ec466 28150+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28151+
28152+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28153+ h_parent = wbr->wbr_plink;
1facf9fc 28154+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28155+
28156+ /* always superio. */
2dfbb274 28157+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 28158+ struct do_whplink_args args = {
28159+ .errp = &err,
28160+ .tgt = &tgtname,
28161+ .h_parent = h_parent,
28162+ .h_dentry = h_dentry,
28163+ .br = br
28164+ };
28165+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28166+ if (unlikely(wkq_err))
28167+ err = wkq_err;
28168+ } else
28169+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 28170+
28171+ return err;
28172+}
28173+
1facf9fc 28174+/*
28175+ * create a new pseudo-link for @h_dentry on @bindex.
28176+ * the linked inode is held in aufs @inode.
28177+ */
28178+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28179+ struct dentry *h_dentry)
28180+{
28181+ struct super_block *sb;
28182+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28183+ struct hlist_bl_head *hbl;
28184+ struct hlist_bl_node *pos;
5afbbe0d 28185+ struct au_icntnr *icntnr;
86dc4139 28186+ int found, err, cnt, i;
1facf9fc 28187+
28188+ sb = inode->i_sb;
28189+ sbinfo = au_sbi(sb);
28190+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28191+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28192+
86dc4139 28193+ found = au_plink_test(inode);
4a4d8108 28194+ if (found)
1facf9fc 28195+ return;
4a4d8108 28196+
86dc4139 28197+ i = au_plink_hash(inode->i_ino);
8b6a4947 28198+ hbl = sbinfo->si_plink + i;
5afbbe0d 28199+ au_igrab(inode);
1facf9fc 28200+
8b6a4947
AM
28201+ hlist_bl_lock(hbl);
28202+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 28203+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
28204+ found = 1;
28205+ break;
28206+ }
1facf9fc 28207+ }
5afbbe0d
AM
28208+ if (!found) {
28209+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 28210+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 28211+ }
8b6a4947 28212+ hlist_bl_unlock(hbl);
4a4d8108 28213+ if (!found) {
8b6a4947 28214+ cnt = au_hbl_count(hbl);
acd2b654 28215+#define msg "unexpectedly unbalanced or too many pseudo-links"
86dc4139
AM
28216+ if (cnt > AUFS_PLINK_WARN)
28217+ AuWarn1(msg ", %d\n", cnt);
28218+#undef msg
1facf9fc 28219+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
28220+ if (unlikely(err)) {
28221+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 28222+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 28223+ iput(&icntnr->vfs_inode);
4a4d8108 28224+ }
5afbbe0d
AM
28225+ } else
28226+ iput(&icntnr->vfs_inode);
1facf9fc 28227+}
28228+
28229+/* free all plinks */
e49829fe 28230+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 28231+{
86dc4139 28232+ int i, warned;
1facf9fc 28233+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28234+ struct hlist_bl_head *hbl;
28235+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28236+ struct au_icntnr *icntnr;
1facf9fc 28237+
dece6358
AM
28238+ SiMustWriteLock(sb);
28239+
1facf9fc 28240+ sbinfo = au_sbi(sb);
28241+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28242+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28243+
1facf9fc 28244+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
28245+ warned = 0;
28246+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28247+ hbl = sbinfo->si_plink + i;
28248+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
28249+ pr_warn("pseudo-link is not flushed");
28250+ warned = 1;
28251+ }
8b6a4947 28252+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 28253+ iput(&icntnr->vfs_inode);
8b6a4947 28254+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 28255+ }
1facf9fc 28256+}
28257+
e49829fe
JR
28258+void au_plink_clean(struct super_block *sb, int verbose)
28259+{
28260+ struct dentry *root;
28261+
28262+ root = sb->s_root;
28263+ aufs_write_lock(root);
28264+ if (au_opt_test(au_mntflags(sb), PLINK))
28265+ au_plink_put(sb, verbose);
28266+ aufs_write_unlock(root);
28267+}
28268+
86dc4139
AM
28269+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28270+{
28271+ int do_put;
5afbbe0d 28272+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
28273+
28274+ do_put = 0;
5afbbe0d
AM
28275+ btop = au_ibtop(inode);
28276+ bbot = au_ibbot(inode);
28277+ if (btop >= 0) {
28278+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
28279+ if (!au_h_iptr(inode, bindex)
28280+ || au_ii_br_id(inode, bindex) != br_id)
28281+ continue;
28282+ au_set_h_iptr(inode, bindex, NULL, 0);
28283+ do_put = 1;
28284+ break;
28285+ }
28286+ if (do_put)
5afbbe0d 28287+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
28288+ if (au_h_iptr(inode, bindex)) {
28289+ do_put = 0;
28290+ break;
28291+ }
28292+ } else
28293+ do_put = 1;
28294+
28295+ return do_put;
28296+}
28297+
1facf9fc 28298+/* free the plinks on a branch specified by @br_id */
28299+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28300+{
28301+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28302+ struct hlist_bl_head *hbl;
28303+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28304+ struct au_icntnr *icntnr;
1facf9fc 28305+ struct inode *inode;
86dc4139 28306+ int i, do_put;
1facf9fc 28307+
dece6358
AM
28308+ SiMustWriteLock(sb);
28309+
1facf9fc 28310+ sbinfo = au_sbi(sb);
28311+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28312+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28313+
8b6a4947 28314+ /* no bit_lock since sbinfo is write-locked */
86dc4139 28315+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28316+ hbl = sbinfo->si_plink + i;
28317+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 28318+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
28319+ ii_write_lock_child(inode);
28320+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 28321+ if (do_put) {
8b6a4947 28322+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
28323+ iput(inode);
28324+ }
86dc4139
AM
28325+ ii_write_unlock(inode);
28326+ iput(inode);
dece6358 28327+ }
dece6358
AM
28328+ }
28329+}
7f207e10 28330diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
eca34b5c
AM
28331--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
28332+++ linux/fs/aufs/poll.c 2019-07-11 15:42:14.472238057 +0200
cd7a4cd9
AM
28333@@ -0,0 +1,51 @@
28334+// SPDX-License-Identifier: GPL-2.0
dece6358 28335+/*
ba1aed25 28336+ * Copyright (C) 2005-2019 Junjiro R. Okajima
dece6358
AM
28337+ *
28338+ * This program, aufs is free software; you can redistribute it and/or modify
28339+ * it under the terms of the GNU General Public License as published by
28340+ * the Free Software Foundation; either version 2 of the License, or
28341+ * (at your option) any later version.
28342+ *
28343+ * This program is distributed in the hope that it will be useful,
28344+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28345+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28346+ * GNU General Public License for more details.
28347+ *
28348+ * You should have received a copy of the GNU General Public License
523b37e3 28349+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
28350+ */
28351+
1308ab2a 28352+/*
28353+ * poll operation
28354+ * There is only one filesystem which implements ->poll operation, currently.
28355+ */
28356+
28357+#include "aufs.h"
28358+
cd7a4cd9 28359+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
1308ab2a 28360+{
be118d29 28361+ __poll_t mask;
1308ab2a 28362+ struct file *h_file;
1308ab2a 28363+ struct super_block *sb;
28364+
28365+ /* We should pretend an error happened. */
be118d29 28366+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 28367+ sb = file->f_path.dentry->d_sb;
e49829fe 28368+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 28369+
521ced18 28370+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
062440b3
AM
28371+ if (IS_ERR(h_file)) {
28372+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
1308ab2a 28373+ goto out;
062440b3 28374+ }
1308ab2a 28375+
cd7a4cd9 28376+ mask = vfs_poll(h_file, pt);
b912730e 28377+ fput(h_file); /* instead of au_read_post() */
1308ab2a 28378+
4f0767ce 28379+out:
1308ab2a 28380+ si_read_unlock(sb);
062440b3 28381+ if (mask & EPOLLERR)
b00004a5 28382+ AuDbg("mask 0x%x\n", mask);
1308ab2a 28383+ return mask;
28384+}
c1595e42 28385diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
eca34b5c 28386--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
9f00928d
AM
28387+++ linux/fs/aufs/posix_acl.c 2019-11-25 12:42:00.275843147 +0100
28388@@ -0,0 +1,105 @@
cd7a4cd9 28389+// SPDX-License-Identifier: GPL-2.0
c1595e42 28390+/*
ba1aed25 28391+ * Copyright (C) 2014-2019 Junjiro R. Okajima
c1595e42
JR
28392+ *
28393+ * This program, aufs is free software; you can redistribute it and/or modify
28394+ * it under the terms of the GNU General Public License as published by
28395+ * the Free Software Foundation; either version 2 of the License, or
28396+ * (at your option) any later version.
28397+ *
28398+ * This program is distributed in the hope that it will be useful,
28399+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28400+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28401+ * GNU General Public License for more details.
28402+ *
28403+ * You should have received a copy of the GNU General Public License
28404+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28405+ */
28406+
28407+/*
28408+ * posix acl operations
28409+ */
28410+
28411+#include <linux/fs.h>
c1595e42
JR
28412+#include "aufs.h"
28413+
28414+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28415+{
28416+ struct posix_acl *acl;
28417+ int err;
28418+ aufs_bindex_t bindex;
28419+ struct inode *h_inode;
28420+ struct super_block *sb;
28421+
28422+ acl = NULL;
28423+ sb = inode->i_sb;
28424+ si_read_lock(sb, AuLock_FLUSH);
28425+ ii_read_lock_child(inode);
2121bcd9 28426+ if (!(sb->s_flags & SB_POSIXACL))
c1595e42
JR
28427+ goto out;
28428+
5afbbe0d 28429+ bindex = au_ibtop(inode);
c1595e42
JR
28430+ h_inode = au_h_iptr(inode, bindex);
28431+ if (unlikely(!h_inode
28432+ || ((h_inode->i_mode & S_IFMT)
28433+ != (inode->i_mode & S_IFMT)))) {
28434+ err = au_busy_or_stale();
28435+ acl = ERR_PTR(err);
28436+ goto out;
28437+ }
28438+
28439+ /* always topmost only */
28440+ acl = get_acl(h_inode, type);
9f00928d
AM
28441+ if (IS_ERR(acl))
28442+ forget_cached_acl(inode, type);
28443+ else
a2654f78 28444+ set_cached_acl(inode, type, acl);
c1595e42
JR
28445+
28446+out:
28447+ ii_read_unlock(inode);
28448+ si_read_unlock(sb);
28449+
28450+ AuTraceErrPtr(acl);
28451+ return acl;
28452+}
28453+
28454+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28455+{
28456+ int err;
28457+ ssize_t ssz;
28458+ struct dentry *dentry;
f2c43d5f 28459+ struct au_sxattr arg = {
c1595e42
JR
28460+ .type = AU_ACL_SET,
28461+ .u.acl_set = {
28462+ .acl = acl,
28463+ .type = type
28464+ },
28465+ };
28466+
5afbbe0d
AM
28467+ IMustLock(inode);
28468+
c1595e42
JR
28469+ if (inode->i_ino == AUFS_ROOT_INO)
28470+ dentry = dget(inode->i_sb->s_root);
28471+ else {
28472+ dentry = d_find_alias(inode);
28473+ if (!dentry)
28474+ dentry = d_find_any_alias(inode);
28475+ if (!dentry) {
28476+ pr_warn("cannot handle this inode, "
28477+ "please report to aufs-users ML\n");
28478+ err = -ENOENT;
28479+ goto out;
28480+ }
28481+ }
28482+
f2c43d5f 28483+ ssz = au_sxattr(dentry, inode, &arg);
9f00928d
AM
28484+ /* forget even it if succeeds since the branch might set differently */
28485+ forget_cached_acl(inode, type);
c1595e42
JR
28486+ dput(dentry);
28487+ err = ssz;
9f00928d 28488+ if (ssz >= 0)
c1595e42
JR
28489+ err = 0;
28490+
28491+out:
c1595e42
JR
28492+ return err;
28493+}
7f207e10 28494diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
eca34b5c
AM
28495--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
28496+++ linux/fs/aufs/procfs.c 2019-07-11 15:42:14.472238057 +0200
062440b3 28497@@ -0,0 +1,171 @@
cd7a4cd9 28498+// SPDX-License-Identifier: GPL-2.0
e49829fe 28499+/*
ba1aed25 28500+ * Copyright (C) 2010-2019 Junjiro R. Okajima
e49829fe
JR
28501+ *
28502+ * This program, aufs is free software; you can redistribute it and/or modify
28503+ * it under the terms of the GNU General Public License as published by
28504+ * the Free Software Foundation; either version 2 of the License, or
28505+ * (at your option) any later version.
28506+ *
28507+ * This program is distributed in the hope that it will be useful,
28508+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28509+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28510+ * GNU General Public License for more details.
28511+ *
28512+ * You should have received a copy of the GNU General Public License
523b37e3 28513+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28514+ */
28515+
28516+/*
28517+ * procfs interfaces
28518+ */
28519+
28520+#include <linux/proc_fs.h>
28521+#include "aufs.h"
28522+
28523+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28524+{
28525+ struct au_sbinfo *sbinfo;
28526+
28527+ sbinfo = file->private_data;
28528+ if (sbinfo) {
28529+ au_plink_maint_leave(sbinfo);
28530+ kobject_put(&sbinfo->si_kobj);
28531+ }
28532+
28533+ return 0;
28534+}
28535+
28536+static void au_procfs_plm_write_clean(struct file *file)
28537+{
28538+ struct au_sbinfo *sbinfo;
28539+
28540+ sbinfo = file->private_data;
28541+ if (sbinfo)
28542+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28543+}
28544+
28545+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28546+{
28547+ int err;
28548+ struct super_block *sb;
28549+ struct au_sbinfo *sbinfo;
8b6a4947 28550+ struct hlist_bl_node *pos;
e49829fe
JR
28551+
28552+ err = -EBUSY;
28553+ if (unlikely(file->private_data))
28554+ goto out;
28555+
28556+ sb = NULL;
53392da6 28557+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28558+ hlist_bl_lock(&au_sbilist);
28559+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe 28560+ if (id == sysaufs_si_id(sbinfo)) {
83b672a5
AM
28561+ if (kobject_get_unless_zero(&sbinfo->si_kobj))
28562+ sb = sbinfo->si_sb;
e49829fe
JR
28563+ break;
28564+ }
8b6a4947 28565+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28566+
28567+ err = -EINVAL;
28568+ if (unlikely(!sb))
28569+ goto out;
28570+
28571+ err = au_plink_maint_enter(sb);
28572+ if (!err)
28573+ /* keep kobject_get() */
28574+ file->private_data = sbinfo;
28575+ else
28576+ kobject_put(&sbinfo->si_kobj);
28577+out:
28578+ return err;
28579+}
28580+
28581+/*
28582+ * Accept a valid "si=xxxx" only.
28583+ * Once it is accepted successfully, accept "clean" too.
28584+ */
28585+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28586+ size_t count, loff_t *ppos)
28587+{
28588+ ssize_t err;
28589+ unsigned long id;
28590+ /* last newline is allowed */
28591+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28592+
28593+ err = -EACCES;
28594+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28595+ goto out;
28596+
28597+ err = -EINVAL;
28598+ if (unlikely(count > sizeof(buf)))
28599+ goto out;
28600+
28601+ err = copy_from_user(buf, ubuf, count);
28602+ if (unlikely(err)) {
28603+ err = -EFAULT;
28604+ goto out;
28605+ }
28606+ buf[count] = 0;
28607+
28608+ err = -EINVAL;
28609+ if (!strcmp("clean", buf)) {
28610+ au_procfs_plm_write_clean(file);
28611+ goto out_success;
28612+ } else if (unlikely(strncmp("si=", buf, 3)))
28613+ goto out;
28614+
9dbd164d 28615+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28616+ if (unlikely(err))
28617+ goto out;
28618+
28619+ err = au_procfs_plm_write_si(file, id);
28620+ if (unlikely(err))
28621+ goto out;
28622+
28623+out_success:
28624+ err = count; /* success */
28625+out:
28626+ return err;
28627+}
28628+
28629+static const struct file_operations au_procfs_plm_fop = {
28630+ .write = au_procfs_plm_write,
28631+ .release = au_procfs_plm_release,
28632+ .owner = THIS_MODULE
28633+};
28634+
28635+/* ---------------------------------------------------------------------- */
28636+
28637+static struct proc_dir_entry *au_procfs_dir;
28638+
28639+void au_procfs_fin(void)
28640+{
28641+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28642+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28643+}
28644+
28645+int __init au_procfs_init(void)
28646+{
28647+ int err;
28648+ struct proc_dir_entry *entry;
28649+
28650+ err = -ENOMEM;
28651+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28652+ if (unlikely(!au_procfs_dir))
28653+ goto out;
28654+
cd7a4cd9 28655+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
e49829fe
JR
28656+ au_procfs_dir, &au_procfs_plm_fop);
28657+ if (unlikely(!entry))
28658+ goto out_dir;
28659+
28660+ err = 0;
28661+ goto out; /* success */
28662+
28663+
28664+out_dir:
28665+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28666+out:
28667+ return err;
28668+}
7f207e10 28669diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
eca34b5c
AM
28670--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
28671+++ linux/fs/aufs/rdu.c 2019-07-11 15:42:14.472238057 +0200
ba1aed25 28672@@ -0,0 +1,384 @@
cd7a4cd9 28673+// SPDX-License-Identifier: GPL-2.0
1308ab2a 28674+/*
ba1aed25 28675+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1308ab2a 28676+ *
28677+ * This program, aufs is free software; you can redistribute it and/or modify
28678+ * it under the terms of the GNU General Public License as published by
28679+ * the Free Software Foundation; either version 2 of the License, or
28680+ * (at your option) any later version.
28681+ *
28682+ * This program is distributed in the hope that it will be useful,
28683+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28684+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28685+ * GNU General Public License for more details.
28686+ *
28687+ * You should have received a copy of the GNU General Public License
523b37e3 28688+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28689+ */
28690+
28691+/*
28692+ * readdir in userspace.
28693+ */
28694+
b752ccd1 28695+#include <linux/compat.h>
4a4d8108 28696+#include <linux/fs_stack.h>
1308ab2a 28697+#include <linux/security.h>
1308ab2a 28698+#include "aufs.h"
28699+
28700+/* bits for struct aufs_rdu.flags */
28701+#define AuRdu_CALLED 1
28702+#define AuRdu_CONT (1 << 1)
28703+#define AuRdu_FULL (1 << 2)
28704+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28705+#define au_fset_rdu(flags, name) \
28706+ do { (flags) |= AuRdu_##name; } while (0)
28707+#define au_fclr_rdu(flags, name) \
28708+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28709+
28710+struct au_rdu_arg {
392086de 28711+ struct dir_context ctx;
1308ab2a 28712+ struct aufs_rdu *rdu;
28713+ union au_rdu_ent_ul ent;
28714+ unsigned long end;
28715+
28716+ struct super_block *sb;
28717+ int err;
28718+};
28719+
392086de 28720+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28721+ loff_t offset, u64 h_ino, unsigned int d_type)
28722+{
28723+ int err, len;
392086de 28724+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28725+ struct aufs_rdu *rdu = arg->rdu;
28726+ struct au_rdu_ent ent;
28727+
28728+ err = 0;
28729+ arg->err = 0;
28730+ au_fset_rdu(rdu->cookie.flags, CALLED);
28731+ len = au_rdu_len(nlen);
28732+ if (arg->ent.ul + len < arg->end) {
28733+ ent.ino = h_ino;
28734+ ent.bindex = rdu->cookie.bindex;
28735+ ent.type = d_type;
28736+ ent.nlen = nlen;
4a4d8108
AM
28737+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28738+ ent.type = DT_UNKNOWN;
1308ab2a 28739+
9dbd164d 28740+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28741+ err = -EFAULT;
28742+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28743+ goto out;
28744+ if (copy_to_user(arg->ent.e->name, name, nlen))
28745+ goto out;
28746+ /* the terminating NULL */
28747+ if (__put_user(0, arg->ent.e->name + nlen))
28748+ goto out;
28749+ err = 0;
28750+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28751+ arg->ent.ul += len;
28752+ rdu->rent++;
28753+ } else {
28754+ err = -EFAULT;
28755+ au_fset_rdu(rdu->cookie.flags, FULL);
28756+ rdu->full = 1;
28757+ rdu->tail = arg->ent;
28758+ }
28759+
4f0767ce 28760+out:
1308ab2a 28761+ /* AuTraceErr(err); */
28762+ return err;
28763+}
28764+
28765+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28766+{
28767+ int err;
28768+ loff_t offset;
28769+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28770+
92d182d2 28771+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28772+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28773+ err = offset;
28774+ if (unlikely(offset != cookie->h_pos))
28775+ goto out;
28776+
28777+ err = 0;
28778+ do {
28779+ arg->err = 0;
28780+ au_fclr_rdu(cookie->flags, CALLED);
28781+ /* smp_mb(); */
392086de 28782+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28783+ if (err >= 0)
28784+ err = arg->err;
28785+ } while (!err
28786+ && au_ftest_rdu(cookie->flags, CALLED)
28787+ && !au_ftest_rdu(cookie->flags, FULL));
28788+ cookie->h_pos = h_file->f_pos;
28789+
4f0767ce 28790+out:
1308ab2a 28791+ AuTraceErr(err);
28792+ return err;
28793+}
28794+
28795+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28796+{
28797+ int err;
5afbbe0d 28798+ aufs_bindex_t bbot;
392086de
AM
28799+ struct au_rdu_arg arg = {
28800+ .ctx = {
2000de60 28801+ .actor = au_rdu_fill
392086de
AM
28802+ }
28803+ };
1308ab2a 28804+ struct dentry *dentry;
28805+ struct inode *inode;
28806+ struct file *h_file;
28807+ struct au_rdu_cookie *cookie = &rdu->cookie;
28808+
ba1aed25
AM
28809+ /* VERIFY_WRITE */
28810+ err = !access_ok(rdu->ent.e, rdu->sz);
1308ab2a 28811+ if (unlikely(err)) {
28812+ err = -EFAULT;
28813+ AuTraceErr(err);
28814+ goto out;
28815+ }
28816+ rdu->rent = 0;
28817+ rdu->tail = rdu->ent;
28818+ rdu->full = 0;
28819+ arg.rdu = rdu;
28820+ arg.ent = rdu->ent;
28821+ arg.end = arg.ent.ul;
28822+ arg.end += rdu->sz;
28823+
28824+ err = -ENOTDIR;
5afbbe0d 28825+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28826+ goto out;
28827+
28828+ err = security_file_permission(file, MAY_READ);
28829+ AuTraceErr(err);
28830+ if (unlikely(err))
28831+ goto out;
28832+
2000de60 28833+ dentry = file->f_path.dentry;
5527c038 28834+ inode = d_inode(dentry);
5afbbe0d 28835+ inode_lock_shared(inode);
1308ab2a 28836+
28837+ arg.sb = inode->i_sb;
e49829fe
JR
28838+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28839+ if (unlikely(err))
28840+ goto out_mtx;
027c5e7a
AM
28841+ err = au_alive_dir(dentry);
28842+ if (unlikely(err))
28843+ goto out_si;
e49829fe 28844+ /* todo: reval? */
1308ab2a 28845+ fi_read_lock(file);
28846+
28847+ err = -EAGAIN;
28848+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28849+ && cookie->generation != au_figen(file)))
28850+ goto out_unlock;
28851+
28852+ err = 0;
28853+ if (!rdu->blk) {
28854+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28855+ if (!rdu->blk)
28856+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28857+ }
5afbbe0d
AM
28858+ bbot = au_fbtop(file);
28859+ if (cookie->bindex < bbot)
28860+ cookie->bindex = bbot;
28861+ bbot = au_fbbot_dir(file);
28862+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28863+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28864+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28865+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28866+ if (!h_file)
28867+ continue;
28868+
28869+ au_fclr_rdu(cookie->flags, FULL);
28870+ err = au_rdu_do(h_file, &arg);
28871+ AuTraceErr(err);
28872+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28873+ break;
28874+ }
28875+ AuDbg("rent %llu\n", rdu->rent);
28876+
28877+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28878+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28879+ au_fset_rdu(cookie->flags, CONT);
28880+ cookie->generation = au_figen(file);
28881+ }
28882+
28883+ ii_read_lock_child(inode);
5afbbe0d 28884+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28885+ ii_read_unlock(inode);
28886+
4f0767ce 28887+out_unlock:
1308ab2a 28888+ fi_read_unlock(file);
027c5e7a 28889+out_si:
1308ab2a 28890+ si_read_unlock(arg.sb);
4f0767ce 28891+out_mtx:
5afbbe0d 28892+ inode_unlock_shared(inode);
4f0767ce 28893+out:
1308ab2a 28894+ AuTraceErr(err);
28895+ return err;
28896+}
28897+
28898+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28899+{
28900+ int err;
28901+ ino_t ino;
28902+ unsigned long long nent;
28903+ union au_rdu_ent_ul *u;
28904+ struct au_rdu_ent ent;
28905+ struct super_block *sb;
28906+
28907+ err = 0;
28908+ nent = rdu->nent;
28909+ u = &rdu->ent;
2000de60 28910+ sb = file->f_path.dentry->d_sb;
1308ab2a 28911+ si_read_lock(sb, AuLock_FLUSH);
28912+ while (nent-- > 0) {
9dbd164d 28913+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28914+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108 28915+ if (!err)
ba1aed25
AM
28916+ /* VERIFY_WRITE */
28917+ err = !access_ok(&u->e->ino, sizeof(ino));
1308ab2a 28918+ if (unlikely(err)) {
28919+ err = -EFAULT;
28920+ AuTraceErr(err);
28921+ break;
28922+ }
28923+
28924+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28925+ if (!ent.wh)
28926+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28927+ else
28928+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28929+ &ino);
28930+ if (unlikely(err)) {
28931+ AuTraceErr(err);
28932+ break;
28933+ }
28934+
28935+ err = __put_user(ino, &u->e->ino);
28936+ if (unlikely(err)) {
28937+ err = -EFAULT;
28938+ AuTraceErr(err);
28939+ break;
28940+ }
28941+ u->ul += au_rdu_len(ent.nlen);
28942+ }
28943+ si_read_unlock(sb);
28944+
28945+ return err;
28946+}
28947+
28948+/* ---------------------------------------------------------------------- */
28949+
28950+static int au_rdu_verify(struct aufs_rdu *rdu)
28951+{
b752ccd1 28952+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 28953+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 28954+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 28955+ rdu->blk,
28956+ rdu->rent, rdu->shwh, rdu->full,
28957+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28958+ rdu->cookie.generation);
dece6358 28959+
b752ccd1 28960+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 28961+ return 0;
dece6358 28962+
b752ccd1
AM
28963+ AuDbg("%u:%u\n",
28964+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 28965+ return -EINVAL;
28966+}
28967+
28968+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 28969+{
1308ab2a 28970+ long err, e;
28971+ struct aufs_rdu rdu;
28972+ void __user *p = (void __user *)arg;
dece6358 28973+
1308ab2a 28974+ err = copy_from_user(&rdu, p, sizeof(rdu));
28975+ if (unlikely(err)) {
28976+ err = -EFAULT;
28977+ AuTraceErr(err);
28978+ goto out;
28979+ }
28980+ err = au_rdu_verify(&rdu);
dece6358
AM
28981+ if (unlikely(err))
28982+ goto out;
28983+
1308ab2a 28984+ switch (cmd) {
28985+ case AUFS_CTL_RDU:
28986+ err = au_rdu(file, &rdu);
28987+ if (unlikely(err))
28988+ break;
dece6358 28989+
1308ab2a 28990+ e = copy_to_user(p, &rdu, sizeof(rdu));
28991+ if (unlikely(e)) {
28992+ err = -EFAULT;
28993+ AuTraceErr(err);
28994+ }
28995+ break;
28996+ case AUFS_CTL_RDU_INO:
28997+ err = au_rdu_ino(file, &rdu);
28998+ break;
28999+
29000+ default:
4a4d8108 29001+ /* err = -ENOTTY; */
1308ab2a 29002+ err = -EINVAL;
29003+ }
dece6358 29004+
4f0767ce 29005+out:
1308ab2a 29006+ AuTraceErr(err);
29007+ return err;
1facf9fc 29008+}
b752ccd1
AM
29009+
29010+#ifdef CONFIG_COMPAT
29011+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29012+{
29013+ long err, e;
29014+ struct aufs_rdu rdu;
29015+ void __user *p = compat_ptr(arg);
29016+
29017+ /* todo: get_user()? */
29018+ err = copy_from_user(&rdu, p, sizeof(rdu));
29019+ if (unlikely(err)) {
29020+ err = -EFAULT;
29021+ AuTraceErr(err);
29022+ goto out;
29023+ }
29024+ rdu.ent.e = compat_ptr(rdu.ent.ul);
29025+ err = au_rdu_verify(&rdu);
29026+ if (unlikely(err))
29027+ goto out;
29028+
29029+ switch (cmd) {
29030+ case AUFS_CTL_RDU:
29031+ err = au_rdu(file, &rdu);
29032+ if (unlikely(err))
29033+ break;
29034+
29035+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29036+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29037+ e = copy_to_user(p, &rdu, sizeof(rdu));
29038+ if (unlikely(e)) {
29039+ err = -EFAULT;
29040+ AuTraceErr(err);
29041+ }
29042+ break;
29043+ case AUFS_CTL_RDU_INO:
29044+ err = au_rdu_ino(file, &rdu);
29045+ break;
29046+
29047+ default:
29048+ /* err = -ENOTTY; */
29049+ err = -EINVAL;
29050+ }
29051+
4f0767ce 29052+out:
b752ccd1
AM
29053+ AuTraceErr(err);
29054+ return err;
29055+}
29056+#endif
7f207e10 29057diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
eca34b5c
AM
29058--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
29059+++ linux/fs/aufs/rwsem.h 2019-07-11 15:42:14.472238057 +0200
062440b3
AM
29060@@ -0,0 +1,73 @@
29061+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 29062+/*
ba1aed25 29063+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 29064+ *
29065+ * This program, aufs is free software; you can redistribute it and/or modify
29066+ * it under the terms of the GNU General Public License as published by
29067+ * the Free Software Foundation; either version 2 of the License, or
29068+ * (at your option) any later version.
dece6358
AM
29069+ *
29070+ * This program is distributed in the hope that it will be useful,
29071+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29072+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29073+ * GNU General Public License for more details.
29074+ *
29075+ * You should have received a copy of the GNU General Public License
523b37e3 29076+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29077+ */
29078+
29079+/*
29080+ * simple read-write semaphore wrappers
29081+ */
29082+
29083+#ifndef __AUFS_RWSEM_H__
29084+#define __AUFS_RWSEM_H__
29085+
29086+#ifdef __KERNEL__
29087+
4a4d8108 29088+#include "debug.h"
dece6358 29089+
acd2b654 29090+/* in the future, the name 'au_rwsem' will be totally gone */
8b6a4947 29091+#define au_rwsem rw_semaphore
dece6358
AM
29092+
29093+/* to debug easier, do not make them inlined functions */
8b6a4947 29094+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
dece6358 29095+/* rwsem_is_locked() is unusable */
8b6a4947
AM
29096+#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
29097+ && debug_locks \
29098+ && !lockdep_is_held_type(rw, 1))
29099+#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
29100+ && debug_locks \
29101+ && !lockdep_is_held_type(rw, 0))
29102+#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
29103+ && debug_locks \
29104+ && !lockdep_is_held(rw))
29105+#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
29106+ && debug_locks \
29107+ && lockdep_is_held(rw))
29108+
29109+#define au_rw_init(rw) init_rwsem(rw)
dece6358 29110+
5afbbe0d
AM
29111+#define au_rw_init_wlock(rw) do { \
29112+ au_rw_init(rw); \
8b6a4947 29113+ down_write(rw); \
5afbbe0d 29114+ } while (0)
dece6358 29115+
8b6a4947
AM
29116+#define au_rw_init_wlock_nested(rw, lsc) do { \
29117+ au_rw_init(rw); \
29118+ down_write_nested(rw, lsc); \
5afbbe0d 29119+ } while (0)
dece6358 29120+
8b6a4947
AM
29121+#define au_rw_read_lock(rw) down_read(rw)
29122+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
29123+#define au_rw_read_unlock(rw) up_read(rw)
29124+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
29125+#define au_rw_write_lock(rw) down_write(rw)
29126+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29127+#define au_rw_write_unlock(rw) up_write(rw)
29128+/* why is not _nested version defined? */
29129+#define au_rw_read_trylock(rw) down_read_trylock(rw)
29130+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 29131+
29132+#endif /* __KERNEL__ */
29133+#endif /* __AUFS_RWSEM_H__ */
7f207e10 29134diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
eca34b5c
AM
29135--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
29136+++ linux/fs/aufs/sbinfo.c 2019-07-11 15:42:14.472238057 +0200
eca801bf 29137@@ -0,0 +1,314 @@
cd7a4cd9 29138+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29139+/*
ba1aed25 29140+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 29141+ *
29142+ * This program, aufs is free software; you can redistribute it and/or modify
29143+ * it under the terms of the GNU General Public License as published by
29144+ * the Free Software Foundation; either version 2 of the License, or
29145+ * (at your option) any later version.
dece6358
AM
29146+ *
29147+ * This program is distributed in the hope that it will be useful,
29148+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29149+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29150+ * GNU General Public License for more details.
29151+ *
29152+ * You should have received a copy of the GNU General Public License
523b37e3 29153+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29154+ */
29155+
29156+/*
29157+ * superblock private data
29158+ */
29159+
eca801bf 29160+#include <linux/iversion.h>
1facf9fc 29161+#include "aufs.h"
29162+
29163+/*
29164+ * they are necessary regardless sysfs is disabled.
29165+ */
29166+void au_si_free(struct kobject *kobj)
29167+{
86dc4139 29168+ int i;
1facf9fc 29169+ struct au_sbinfo *sbinfo;
b752ccd1 29170+ char *locked __maybe_unused; /* debug only */
1facf9fc 29171+
29172+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 29173+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29174+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 29175+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d 29176+
acd2b654
AM
29177+ AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29178+ au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29179+ AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29180+ au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
1facf9fc 29181+
062440b3 29182+ dbgaufs_si_fin(sbinfo);
e49829fe 29183+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 29184+ au_br_free(sbinfo);
e49829fe 29185+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 29186+
9f237c51 29187+ au_kfree_try_rcu(sbinfo->si_branch);
1facf9fc 29188+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 29189+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 29190+
acd2b654
AM
29191+ au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29192+ /* si_nfiles is waited too */
9f237c51 29193+ au_kfree_rcu(sbinfo);
1facf9fc 29194+}
29195+
29196+int au_si_alloc(struct super_block *sb)
29197+{
86dc4139 29198+ int err, i;
1facf9fc 29199+ struct au_sbinfo *sbinfo;
29200+
29201+ err = -ENOMEM;
4a4d8108 29202+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 29203+ if (unlikely(!sbinfo))
29204+ goto out;
29205+
29206+ /* will be reallocated separately */
29207+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29208+ if (unlikely(!sbinfo->si_branch))
febd17d6 29209+ goto out_sbinfo;
1facf9fc 29210+
1facf9fc 29211+ err = sysaufs_si_init(sbinfo);
062440b3
AM
29212+ if (!err) {
29213+ dbgaufs_si_null(sbinfo);
29214+ err = dbgaufs_si_init(sbinfo);
29215+ if (unlikely(err))
29216+ kobject_put(&sbinfo->si_kobj);
29217+ }
1facf9fc 29218+ if (unlikely(err))
29219+ goto out_br;
29220+
29221+ au_nwt_init(&sbinfo->si_nowait);
dece6358 29222+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 29223+
acd2b654
AM
29224+ au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29225+ au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
7f207e10 29226+
5afbbe0d 29227+ sbinfo->si_bbot = -1;
392086de 29228+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 29229+
29230+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29231+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
29232+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29233+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 29234+
076b876e
AM
29235+ au_fhsm_init(sbinfo);
29236+
e49829fe 29237+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 29238+
392086de
AM
29239+ sbinfo->si_xino_jiffy = jiffies;
29240+ sbinfo->si_xino_expire
29241+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 29242+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 29243+ /* leave si_xib_last_pindex and si_xib_next_bit */
29244+
8b6a4947 29245+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 29246+
e49829fe 29247+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 29248+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29249+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29250+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29251+
86dc4139 29252+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29253+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 29254+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 29255+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 29256+
8b6a4947 29257+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 29258+
b95c5147
AM
29259+ /* with getattr by default */
29260+ sbinfo->si_iop_array = aufs_iop;
29261+
1facf9fc 29262+ /* leave other members for sysaufs and si_mnt. */
29263+ sbinfo->si_sb = sb;
29264+ sb->s_fs_info = sbinfo;
b752ccd1 29265+ si_pid_set(sb);
1facf9fc 29266+ return 0; /* success */
29267+
4f0767ce 29268+out_br:
9f237c51 29269+ au_kfree_try_rcu(sbinfo->si_branch);
4f0767ce 29270+out_sbinfo:
9f237c51 29271+ au_kfree_rcu(sbinfo);
4f0767ce 29272+out:
1facf9fc 29273+ return err;
29274+}
29275+
e2f27e51 29276+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 29277+{
29278+ int err, sz;
29279+ struct au_branch **brp;
29280+
dece6358
AM
29281+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29282+
1facf9fc 29283+ err = -ENOMEM;
5afbbe0d 29284+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 29285+ if (unlikely(!sz))
29286+ sz = sizeof(*brp);
e2f27e51
AM
29287+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29288+ may_shrink);
1facf9fc 29289+ if (brp) {
29290+ sbinfo->si_branch = brp;
29291+ err = 0;
29292+ }
29293+
29294+ return err;
29295+}
29296+
29297+/* ---------------------------------------------------------------------- */
29298+
29299+unsigned int au_sigen_inc(struct super_block *sb)
29300+{
29301+ unsigned int gen;
5527c038 29302+ struct inode *inode;
1facf9fc 29303+
dece6358
AM
29304+ SiMustWriteLock(sb);
29305+
1facf9fc 29306+ gen = ++au_sbi(sb)->si_generation;
29307+ au_update_digen(sb->s_root);
5527c038
JR
29308+ inode = d_inode(sb->s_root);
29309+ au_update_iigen(inode, /*half*/0);
be118d29 29310+ inode_inc_iversion(inode);
1facf9fc 29311+ return gen;
29312+}
29313+
29314+aufs_bindex_t au_new_br_id(struct super_block *sb)
29315+{
29316+ aufs_bindex_t br_id;
29317+ int i;
29318+ struct au_sbinfo *sbinfo;
29319+
dece6358
AM
29320+ SiMustWriteLock(sb);
29321+
1facf9fc 29322+ sbinfo = au_sbi(sb);
29323+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29324+ br_id = ++sbinfo->si_last_br_id;
7f207e10 29325+ AuDebugOn(br_id < 0);
1facf9fc 29326+ if (br_id && au_br_index(sb, br_id) < 0)
29327+ return br_id;
29328+ }
29329+
29330+ return -1;
29331+}
29332+
29333+/* ---------------------------------------------------------------------- */
29334+
e49829fe
JR
29335+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29336+int si_read_lock(struct super_block *sb, int flags)
29337+{
29338+ int err;
29339+
29340+ err = 0;
29341+ if (au_ftest_lock(flags, FLUSH))
29342+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29343+
29344+ si_noflush_read_lock(sb);
29345+ err = au_plink_maint(sb, flags);
29346+ if (unlikely(err))
29347+ si_read_unlock(sb);
29348+
29349+ return err;
29350+}
29351+
29352+int si_write_lock(struct super_block *sb, int flags)
29353+{
29354+ int err;
29355+
29356+ if (au_ftest_lock(flags, FLUSH))
29357+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29358+
29359+ si_noflush_write_lock(sb);
29360+ err = au_plink_maint(sb, flags);
29361+ if (unlikely(err))
29362+ si_write_unlock(sb);
29363+
29364+ return err;
29365+}
29366+
1facf9fc 29367+/* dentry and super_block lock. call at entry point */
e49829fe 29368+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 29369+{
e49829fe 29370+ int err;
027c5e7a 29371+ struct super_block *sb;
e49829fe 29372+
027c5e7a
AM
29373+ sb = dentry->d_sb;
29374+ err = si_read_lock(sb, flags);
29375+ if (unlikely(err))
29376+ goto out;
29377+
29378+ if (au_ftest_lock(flags, DW))
29379+ di_write_lock_child(dentry);
29380+ else
29381+ di_read_lock_child(dentry, flags);
29382+
29383+ if (au_ftest_lock(flags, GEN)) {
29384+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
29385+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29386+ AuDebugOn(!err && au_dbrange_test(dentry));
29387+ else if (!err)
29388+ err = au_dbrange_test(dentry);
027c5e7a
AM
29389+ if (unlikely(err))
29390+ aufs_read_unlock(dentry, flags);
e49829fe
JR
29391+ }
29392+
027c5e7a 29393+out:
e49829fe 29394+ return err;
1facf9fc 29395+}
29396+
29397+void aufs_read_unlock(struct dentry *dentry, int flags)
29398+{
29399+ if (au_ftest_lock(flags, DW))
29400+ di_write_unlock(dentry);
29401+ else
29402+ di_read_unlock(dentry, flags);
29403+ si_read_unlock(dentry->d_sb);
29404+}
29405+
29406+void aufs_write_lock(struct dentry *dentry)
29407+{
e49829fe 29408+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29409+ di_write_lock_child(dentry);
29410+}
29411+
29412+void aufs_write_unlock(struct dentry *dentry)
29413+{
29414+ di_write_unlock(dentry);
29415+ si_write_unlock(dentry->d_sb);
29416+}
29417+
e49829fe 29418+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29419+{
e49829fe 29420+ int err;
027c5e7a
AM
29421+ unsigned int sigen;
29422+ struct super_block *sb;
e49829fe 29423+
027c5e7a
AM
29424+ sb = d1->d_sb;
29425+ err = si_read_lock(sb, flags);
29426+ if (unlikely(err))
29427+ goto out;
29428+
b95c5147 29429+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29430+
29431+ if (au_ftest_lock(flags, GEN)) {
29432+ sigen = au_sigen(sb);
29433+ err = au_digen_test(d1, sigen);
29434+ AuDebugOn(!err && au_dbrange_test(d1));
29435+ if (!err) {
29436+ err = au_digen_test(d2, sigen);
29437+ AuDebugOn(!err && au_dbrange_test(d2));
29438+ }
29439+ if (unlikely(err))
29440+ aufs_read_and_write_unlock2(d1, d2);
29441+ }
29442+
29443+out:
e49829fe 29444+ return err;
1facf9fc 29445+}
29446+
29447+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29448+{
29449+ di_write_unlock2(d1, d2);
29450+ si_read_unlock(d1->d_sb);
29451+}
7f207e10 29452diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
eca34b5c
AM
29453--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
29454+++ linux/fs/aufs/super.c 2019-07-11 15:42:14.472238057 +0200
fbc438ed 29455@@ -0,0 +1,1047 @@
cd7a4cd9 29456+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29457+/*
ba1aed25 29458+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 29459+ *
29460+ * This program, aufs is free software; you can redistribute it and/or modify
29461+ * it under the terms of the GNU General Public License as published by
29462+ * the Free Software Foundation; either version 2 of the License, or
29463+ * (at your option) any later version.
dece6358
AM
29464+ *
29465+ * This program is distributed in the hope that it will be useful,
29466+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29467+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29468+ * GNU General Public License for more details.
29469+ *
29470+ * You should have received a copy of the GNU General Public License
523b37e3 29471+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29472+ */
29473+
29474+/*
29475+ * mount and super_block operations
29476+ */
29477+
eca801bf 29478+#include <linux/iversion.h>
f6c5ef8b 29479+#include <linux/mm.h>
1facf9fc 29480+#include <linux/seq_file.h>
29481+#include <linux/statfs.h>
7f207e10 29482+#include <linux/vmalloc.h>
1facf9fc 29483+#include "aufs.h"
29484+
29485+/*
29486+ * super_operations
29487+ */
29488+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29489+{
29490+ struct au_icntnr *c;
29491+
29492+ c = au_cache_alloc_icntnr();
29493+ if (c) {
027c5e7a 29494+ au_icntnr_init(c);
be118d29 29495+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29496+ c->iinfo.ii_hinode = NULL;
29497+ return &c->vfs_inode;
29498+ }
29499+ return NULL;
29500+}
29501+
29502+static void aufs_destroy_inode(struct inode *inode)
29503+{
5afbbe0d
AM
29504+ if (!au_is_bad_inode(inode))
29505+ au_iinfo_fin(inode);
fbc438ed
JR
29506+}
29507+
29508+static void aufs_free_inode(struct inode *inode)
29509+{
29510+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
1facf9fc 29511+}
29512+
29513+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29514+{
29515+ struct inode *inode;
29516+ int err;
29517+
29518+ inode = iget_locked(sb, ino);
29519+ if (unlikely(!inode)) {
29520+ inode = ERR_PTR(-ENOMEM);
29521+ goto out;
29522+ }
29523+ if (!(inode->i_state & I_NEW))
29524+ goto out;
29525+
29526+ err = au_xigen_new(inode);
29527+ if (!err)
29528+ err = au_iinfo_init(inode);
29529+ if (!err)
be118d29 29530+ inode_inc_iversion(inode);
1facf9fc 29531+ else {
29532+ iget_failed(inode);
29533+ inode = ERR_PTR(err);
29534+ }
29535+
4f0767ce 29536+out:
1facf9fc 29537+ /* never return NULL */
29538+ AuDebugOn(!inode);
29539+ AuTraceErrPtr(inode);
29540+ return inode;
29541+}
29542+
29543+/* lock free root dinfo */
29544+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29545+{
29546+ int err;
5afbbe0d 29547+ aufs_bindex_t bindex, bbot;
1facf9fc 29548+ struct path path;
4a4d8108 29549+ struct au_hdentry *hdp;
1facf9fc 29550+ struct au_branch *br;
076b876e 29551+ au_br_perm_str_t perm;
1facf9fc 29552+
29553+ err = 0;
5afbbe0d
AM
29554+ bbot = au_sbbot(sb);
29555+ bindex = 0;
29556+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29557+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29558+ br = au_sbr(sb, bindex);
86dc4139 29559+ path.mnt = au_br_mnt(br);
5afbbe0d 29560+ path.dentry = hdp->hd_dentry;
1facf9fc 29561+ err = au_seq_path(seq, &path);
79b8bda9 29562+ if (!err) {
076b876e 29563+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29564+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29565+ if (bindex != bbot)
79b8bda9 29566+ seq_putc(seq, ':');
1e00d052 29567+ }
1facf9fc 29568+ }
79b8bda9
AM
29569+ if (unlikely(err || seq_has_overflowed(seq)))
29570+ err = -E2BIG;
1facf9fc 29571+
29572+ return err;
29573+}
29574+
f2c43d5f
AM
29575+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29576+ const char *append)
29577+{
29578+ char *p;
29579+
29580+ p = fmt;
29581+ while (*pat != ':')
29582+ *p++ = *pat++;
29583+ *p++ = *pat++;
29584+ strcpy(p, append);
29585+ AuDebugOn(strlen(fmt) >= len);
29586+}
29587+
1facf9fc 29588+static void au_show_wbr_create(struct seq_file *m, int v,
29589+ struct au_sbinfo *sbinfo)
29590+{
29591+ const char *pat;
f2c43d5f
AM
29592+ char fmt[32];
29593+ struct au_wbr_mfs *mfs;
1facf9fc 29594+
dece6358
AM
29595+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29596+
c2b27bf2 29597+ seq_puts(m, ",create=");
1facf9fc 29598+ pat = au_optstr_wbr_create(v);
f2c43d5f 29599+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29600+ switch (v) {
29601+ case AuWbrCreate_TDP:
29602+ case AuWbrCreate_RR:
29603+ case AuWbrCreate_MFS:
29604+ case AuWbrCreate_PMFS:
c2b27bf2 29605+ seq_puts(m, pat);
1facf9fc 29606+ break;
f2c43d5f
AM
29607+ case AuWbrCreate_MFSRR:
29608+ case AuWbrCreate_TDMFS:
29609+ case AuWbrCreate_PMFSRR:
29610+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29611+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29612+ break;
f2c43d5f 29613+ case AuWbrCreate_MFSV:
1facf9fc 29614+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29615+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29616+ seq_printf(m, fmt,
29617+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29618+ / MSEC_PER_SEC);
1facf9fc 29619+ break;
1facf9fc 29620+ case AuWbrCreate_MFSRRV:
f2c43d5f 29621+ case AuWbrCreate_TDMFSV:
392086de 29622+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29623+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29624+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29625+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29626+ break;
f2c43d5f
AM
29627+ default:
29628+ BUG();
1facf9fc 29629+ }
29630+}
29631+
7eafdf33 29632+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29633+{
29634+#ifdef CONFIG_SYSFS
29635+ return 0;
29636+#else
29637+ int err;
29638+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29639+ aufs_bindex_t bindex, brid;
1facf9fc 29640+ struct qstr *name;
29641+ struct file *f;
29642+ struct dentry *d, *h_root;
acd2b654 29643+ struct au_branch *br;
1facf9fc 29644+
dece6358
AM
29645+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29646+
1facf9fc 29647+ err = 0;
1facf9fc 29648+ f = au_sbi(sb)->si_xib;
29649+ if (!f)
29650+ goto out;
29651+
29652+ /* stop printing the default xino path on the first writable branch */
29653+ h_root = NULL;
acd2b654
AM
29654+ bindex = au_xi_root(sb, f->f_path.dentry);
29655+ if (bindex >= 0) {
29656+ br = au_sbr_sb(sb, bindex);
29657+ h_root = au_br_dentry(br);
1facf9fc 29658+ }
acd2b654 29659+
2000de60 29660+ d = f->f_path.dentry;
1facf9fc 29661+ name = &d->d_name;
29662+ /* safe ->d_parent because the file is unlinked */
29663+ if (d->d_parent == h_root
29664+ && name->len == len
29665+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29666+ goto out;
29667+
29668+ seq_puts(seq, ",xino=");
29669+ err = au_xino_path(seq, f);
29670+
4f0767ce 29671+out:
1facf9fc 29672+ return err;
29673+#endif
29674+}
29675+
29676+/* seq_file will re-call me in case of too long string */
7eafdf33 29677+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29678+{
027c5e7a 29679+ int err;
1facf9fc 29680+ unsigned int mnt_flags, v;
29681+ struct super_block *sb;
29682+ struct au_sbinfo *sbinfo;
29683+
29684+#define AuBool(name, str) do { \
29685+ v = au_opt_test(mnt_flags, name); \
29686+ if (v != au_opt_test(AuOpt_Def, name)) \
29687+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29688+} while (0)
29689+
29690+#define AuStr(name, str) do { \
29691+ v = mnt_flags & AuOptMask_##name; \
29692+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29693+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29694+} while (0)
29695+
29696+#define AuUInt(name, str, val) do { \
29697+ if (val != AUFS_##name##_DEF) \
29698+ seq_printf(m, "," #str "=%u", val); \
29699+} while (0)
29700+
7eafdf33 29701+ sb = dentry->d_sb;
2121bcd9 29702+ if (sb->s_flags & SB_POSIXACL)
c1595e42 29703+ seq_puts(m, ",acl");
be118d29
JR
29704+#if 0
29705+ if (sb->s_flags & SB_I_VERSION)
29706+ seq_puts(m, ",i_version");
29707+#endif
c1595e42
JR
29708+
29709+ /* lock free root dinfo */
1facf9fc 29710+ si_noflush_read_lock(sb);
29711+ sbinfo = au_sbi(sb);
29712+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29713+
29714+ mnt_flags = au_mntflags(sb);
29715+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29716+ err = au_show_xino(m, sb);
1facf9fc 29717+ if (unlikely(err))
29718+ goto out;
29719+ } else
29720+ seq_puts(m, ",noxino");
29721+
29722+ AuBool(TRUNC_XINO, trunc_xino);
29723+ AuStr(UDBA, udba);
dece6358 29724+ AuBool(SHWH, shwh);
1facf9fc 29725+ AuBool(PLINK, plink);
4a4d8108 29726+ AuBool(DIO, dio);
076b876e 29727+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29728+
29729+ v = sbinfo->si_wbr_create;
29730+ if (v != AuWbrCreate_Def)
29731+ au_show_wbr_create(m, v, sbinfo);
29732+
29733+ v = sbinfo->si_wbr_copyup;
29734+ if (v != AuWbrCopyup_Def)
29735+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29736+
29737+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29738+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29739+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29740+
29741+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29742+
027c5e7a
AM
29743+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29744+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29745+
29746+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29747+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29748+
076b876e
AM
29749+ au_fhsm_show(m, sbinfo);
29750+
8b6a4947 29751+ AuBool(DIRREN, dirren);
1facf9fc 29752+ AuBool(SUM, sum);
29753+ /* AuBool(SUM_W, wsum); */
29754+ AuBool(WARN_PERM, warn_perm);
29755+ AuBool(VERBOSE, verbose);
29756+
4f0767ce 29757+out:
1facf9fc 29758+ /* be sure to print "br:" last */
29759+ if (!sysaufs_brs) {
29760+ seq_puts(m, ",br:");
29761+ au_show_brs(m, sb);
29762+ }
29763+ si_read_unlock(sb);
29764+ return 0;
29765+
1facf9fc 29766+#undef AuBool
29767+#undef AuStr
4a4d8108 29768+#undef AuUInt
1facf9fc 29769+}
29770+
29771+/* ---------------------------------------------------------------------- */
29772+
29773+/* sum mode which returns the summation for statfs(2) */
29774+
29775+static u64 au_add_till_max(u64 a, u64 b)
29776+{
29777+ u64 old;
29778+
29779+ old = a;
29780+ a += b;
92d182d2
AM
29781+ if (old <= a)
29782+ return a;
29783+ return ULLONG_MAX;
29784+}
29785+
29786+static u64 au_mul_till_max(u64 a, long mul)
29787+{
29788+ u64 old;
29789+
29790+ old = a;
29791+ a *= mul;
29792+ if (old <= a)
1facf9fc 29793+ return a;
29794+ return ULLONG_MAX;
29795+}
29796+
29797+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29798+{
29799+ int err;
92d182d2 29800+ long bsize, factor;
1facf9fc 29801+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29802+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29803+ unsigned char shared;
7f207e10 29804+ struct path h_path;
1facf9fc 29805+ struct super_block *h_sb;
29806+
92d182d2
AM
29807+ err = 0;
29808+ bsize = LONG_MAX;
29809+ files = 0;
29810+ ffree = 0;
1facf9fc 29811+ blocks = 0;
29812+ bfree = 0;
29813+ bavail = 0;
5afbbe0d
AM
29814+ bbot = au_sbbot(sb);
29815+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29816+ h_path.mnt = au_sbr_mnt(sb, bindex);
29817+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29818+ shared = 0;
92d182d2 29819+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29820+ shared = (au_sbr_sb(sb, i) == h_sb);
29821+ if (shared)
29822+ continue;
29823+
29824+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29825+ h_path.dentry = h_path.mnt->mnt_root;
29826+ err = vfs_statfs(&h_path, buf);
1facf9fc 29827+ if (unlikely(err))
29828+ goto out;
29829+
92d182d2
AM
29830+ if (bsize > buf->f_bsize) {
29831+ /*
29832+ * we will reduce bsize, so we have to expand blocks
29833+ * etc. to match them again
29834+ */
29835+ factor = (bsize / buf->f_bsize);
29836+ blocks = au_mul_till_max(blocks, factor);
29837+ bfree = au_mul_till_max(bfree, factor);
29838+ bavail = au_mul_till_max(bavail, factor);
29839+ bsize = buf->f_bsize;
29840+ }
29841+
29842+ factor = (buf->f_bsize / bsize);
29843+ blocks = au_add_till_max(blocks,
29844+ au_mul_till_max(buf->f_blocks, factor));
29845+ bfree = au_add_till_max(bfree,
29846+ au_mul_till_max(buf->f_bfree, factor));
29847+ bavail = au_add_till_max(bavail,
29848+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29849+ files = au_add_till_max(files, buf->f_files);
29850+ ffree = au_add_till_max(ffree, buf->f_ffree);
29851+ }
29852+
92d182d2 29853+ buf->f_bsize = bsize;
1facf9fc 29854+ buf->f_blocks = blocks;
29855+ buf->f_bfree = bfree;
29856+ buf->f_bavail = bavail;
29857+ buf->f_files = files;
29858+ buf->f_ffree = ffree;
92d182d2 29859+ buf->f_frsize = 0;
1facf9fc 29860+
4f0767ce 29861+out:
1facf9fc 29862+ return err;
29863+}
29864+
29865+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29866+{
29867+ int err;
7f207e10 29868+ struct path h_path;
1facf9fc 29869+ struct super_block *sb;
29870+
29871+ /* lock free root dinfo */
29872+ sb = dentry->d_sb;
29873+ si_noflush_read_lock(sb);
7f207e10 29874+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29875+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29876+ h_path.mnt = au_sbr_mnt(sb, 0);
29877+ h_path.dentry = h_path.mnt->mnt_root;
29878+ err = vfs_statfs(&h_path, buf);
29879+ } else
1facf9fc 29880+ err = au_statfs_sum(sb, buf);
29881+ si_read_unlock(sb);
29882+
29883+ if (!err) {
29884+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29885+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29886+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29887+ }
29888+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29889+
29890+ return err;
29891+}
29892+
29893+/* ---------------------------------------------------------------------- */
29894+
537831f9
AM
29895+static int aufs_sync_fs(struct super_block *sb, int wait)
29896+{
29897+ int err, e;
5afbbe0d 29898+ aufs_bindex_t bbot, bindex;
537831f9
AM
29899+ struct au_branch *br;
29900+ struct super_block *h_sb;
29901+
29902+ err = 0;
29903+ si_noflush_read_lock(sb);
5afbbe0d
AM
29904+ bbot = au_sbbot(sb);
29905+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29906+ br = au_sbr(sb, bindex);
29907+ if (!au_br_writable(br->br_perm))
29908+ continue;
29909+
29910+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29911+ e = vfsub_sync_filesystem(h_sb, wait);
29912+ if (unlikely(e && !err))
29913+ err = e;
29914+ /* go on even if an error happens */
537831f9
AM
29915+ }
29916+ si_read_unlock(sb);
29917+
29918+ return err;
29919+}
29920+
29921+/* ---------------------------------------------------------------------- */
29922+
1facf9fc 29923+/* final actions when unmounting a file system */
29924+static void aufs_put_super(struct super_block *sb)
29925+{
29926+ struct au_sbinfo *sbinfo;
29927+
29928+ sbinfo = au_sbi(sb);
062440b3
AM
29929+ if (sbinfo)
29930+ kobject_put(&sbinfo->si_kobj);
1facf9fc 29931+}
29932+
29933+/* ---------------------------------------------------------------------- */
29934+
79b8bda9
AM
29935+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29936+ struct super_block *sb, void *arg)
7f207e10
AM
29937+{
29938+ void *array;
076b876e 29939+ unsigned long long n, sz;
7f207e10
AM
29940+
29941+ array = NULL;
29942+ n = 0;
29943+ if (!*hint)
29944+ goto out;
29945+
29946+ if (*hint > ULLONG_MAX / sizeof(array)) {
29947+ array = ERR_PTR(-EMFILE);
29948+ pr_err("hint %llu\n", *hint);
29949+ goto out;
29950+ }
29951+
076b876e
AM
29952+ sz = sizeof(array) * *hint;
29953+ array = kzalloc(sz, GFP_NOFS);
7f207e10 29954+ if (unlikely(!array))
076b876e 29955+ array = vzalloc(sz);
7f207e10
AM
29956+ if (unlikely(!array)) {
29957+ array = ERR_PTR(-ENOMEM);
29958+ goto out;
29959+ }
29960+
79b8bda9 29961+ n = cb(sb, array, *hint, arg);
7f207e10
AM
29962+ AuDebugOn(n > *hint);
29963+
29964+out:
29965+ *hint = n;
29966+ return array;
29967+}
29968+
79b8bda9 29969+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
29970+ unsigned long long max __maybe_unused,
29971+ void *arg)
29972+{
29973+ unsigned long long n;
29974+ struct inode **p, *inode;
29975+ struct list_head *head;
29976+
29977+ n = 0;
29978+ p = a;
29979+ head = arg;
79b8bda9 29980+ spin_lock(&sb->s_inode_list_lock);
7f207e10 29981+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
29982+ if (!au_is_bad_inode(inode)
29983+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
29984+ spin_lock(&inode->i_lock);
29985+ if (atomic_read(&inode->i_count)) {
29986+ au_igrab(inode);
29987+ *p++ = inode;
29988+ n++;
29989+ AuDebugOn(n > max);
29990+ }
29991+ spin_unlock(&inode->i_lock);
7f207e10
AM
29992+ }
29993+ }
79b8bda9 29994+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
29995+
29996+ return n;
29997+}
29998+
29999+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30000+{
acd2b654
AM
30001+ struct au_sbinfo *sbi;
30002+
30003+ sbi = au_sbi(sb);
30004+ *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
79b8bda9 30005+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
30006+}
30007+
30008+void au_iarray_free(struct inode **a, unsigned long long max)
30009+{
30010+ unsigned long long ull;
30011+
30012+ for (ull = 0; ull < max; ull++)
30013+ iput(a[ull]);
be52b249 30014+ kvfree(a);
7f207e10
AM
30015+}
30016+
30017+/* ---------------------------------------------------------------------- */
30018+
1facf9fc 30019+/*
30020+ * refresh dentry and inode at remount time.
30021+ */
027c5e7a
AM
30022+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30023+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30024+ struct dentry *parent)
1facf9fc 30025+{
30026+ int err;
1facf9fc 30027+
30028+ di_write_lock_child(dentry);
1facf9fc 30029+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
30030+ err = au_refresh_dentry(dentry, parent);
30031+ if (!err && dir_flags)
5527c038 30032+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 30033+ di_read_unlock(parent, AuLock_IR);
1facf9fc 30034+ di_write_unlock(dentry);
30035+
30036+ return err;
30037+}
30038+
027c5e7a
AM
30039+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30040+ struct au_sbinfo *sbinfo,
b95c5147 30041+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 30042+{
027c5e7a
AM
30043+ int err;
30044+ struct dentry *parent;
027c5e7a
AM
30045+
30046+ err = 0;
30047+ parent = dget_parent(dentry);
30048+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
30049+ if (d_really_is_positive(dentry)) {
30050+ if (!d_is_dir(dentry))
027c5e7a
AM
30051+ err = au_do_refresh(dentry, /*dir_flags*/0,
30052+ parent);
30053+ else {
30054+ err = au_do_refresh(dentry, dir_flags, parent);
30055+ if (unlikely(err))
30056+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30057+ }
30058+ } else
30059+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30060+ AuDbgDentry(dentry);
30061+ }
30062+ dput(parent);
30063+
79b8bda9 30064+ if (!err) {
b95c5147 30065+ if (do_idop)
79b8bda9
AM
30066+ au_refresh_dop(dentry, /*force_reval*/0);
30067+ } else
30068+ au_refresh_dop(dentry, /*force_reval*/1);
30069+
027c5e7a
AM
30070+ AuTraceErr(err);
30071+ return err;
1facf9fc 30072+}
30073+
b95c5147 30074+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 30075+{
30076+ int err, i, j, ndentry, e;
027c5e7a 30077+ unsigned int sigen;
1facf9fc 30078+ struct au_dcsub_pages dpages;
30079+ struct au_dpage *dpage;
027c5e7a
AM
30080+ struct dentry **dentries, *d;
30081+ struct au_sbinfo *sbinfo;
30082+ struct dentry *root = sb->s_root;
5527c038 30083+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 30084+
b95c5147 30085+ if (do_idop)
79b8bda9
AM
30086+ au_refresh_dop(root, /*force_reval*/0);
30087+
027c5e7a
AM
30088+ err = au_dpages_init(&dpages, GFP_NOFS);
30089+ if (unlikely(err))
1facf9fc 30090+ goto out;
027c5e7a
AM
30091+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
30092+ if (unlikely(err))
1facf9fc 30093+ goto out_dpages;
1facf9fc 30094+
027c5e7a
AM
30095+ sigen = au_sigen(sb);
30096+ sbinfo = au_sbi(sb);
30097+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 30098+ dpage = dpages.dpages + i;
30099+ dentries = dpage->dentries;
30100+ ndentry = dpage->ndentry;
027c5e7a 30101+ for (j = 0; j < ndentry; j++) {
1facf9fc 30102+ d = dentries[j];
79b8bda9 30103+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 30104+ do_idop);
027c5e7a
AM
30105+ if (unlikely(e && !err))
30106+ err = e;
30107+ /* go on even err */
1facf9fc 30108+ }
30109+ }
30110+
4f0767ce 30111+out_dpages:
1facf9fc 30112+ au_dpages_free(&dpages);
4f0767ce 30113+out:
1facf9fc 30114+ return err;
30115+}
30116+
b95c5147 30117+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 30118+{
027c5e7a
AM
30119+ int err, e;
30120+ unsigned int sigen;
30121+ unsigned long long max, ull;
30122+ struct inode *inode, **array;
1facf9fc 30123+
027c5e7a
AM
30124+ array = au_iarray_alloc(sb, &max);
30125+ err = PTR_ERR(array);
30126+ if (IS_ERR(array))
30127+ goto out;
1facf9fc 30128+
30129+ err = 0;
027c5e7a
AM
30130+ sigen = au_sigen(sb);
30131+ for (ull = 0; ull < max; ull++) {
30132+ inode = array[ull];
076b876e
AM
30133+ if (unlikely(!inode))
30134+ break;
b95c5147
AM
30135+
30136+ e = 0;
30137+ ii_write_lock_child(inode);
537831f9 30138+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 30139+ e = au_refresh_hinode_self(inode);
1facf9fc 30140+ if (unlikely(e)) {
b95c5147 30141+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 30142+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 30143+ if (!err)
30144+ err = e;
30145+ /* go on even if err */
30146+ }
30147+ }
b95c5147
AM
30148+ if (!e && do_idop)
30149+ au_refresh_iop(inode, /*force_getattr*/0);
30150+ ii_write_unlock(inode);
1facf9fc 30151+ }
30152+
027c5e7a 30153+ au_iarray_free(array, max);
1facf9fc 30154+
4f0767ce 30155+out:
1facf9fc 30156+ return err;
30157+}
30158+
b95c5147 30159+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 30160+{
027c5e7a
AM
30161+ int err, e;
30162+ unsigned int udba;
5afbbe0d 30163+ aufs_bindex_t bindex, bbot;
1facf9fc 30164+ struct dentry *root;
30165+ struct inode *inode;
027c5e7a 30166+ struct au_branch *br;
79b8bda9 30167+ struct au_sbinfo *sbi;
1facf9fc 30168+
30169+ au_sigen_inc(sb);
79b8bda9
AM
30170+ sbi = au_sbi(sb);
30171+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 30172+
30173+ root = sb->s_root;
30174+ DiMustNoWaiters(root);
5527c038 30175+ inode = d_inode(root);
1facf9fc 30176+ IiMustNoWaiters(inode);
1facf9fc 30177+
027c5e7a 30178+ udba = au_opt_udba(sb);
5afbbe0d
AM
30179+ bbot = au_sbbot(sb);
30180+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
30181+ br = au_sbr(sb, bindex);
30182+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 30183+ if (unlikely(err))
027c5e7a
AM
30184+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
30185+ bindex, err);
30186+ /* go on even if err */
1facf9fc 30187+ }
027c5e7a 30188+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 30189+
b95c5147 30190+ if (do_idop) {
79b8bda9
AM
30191+ if (au_ftest_si(sbi, NO_DREVAL)) {
30192+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30193+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
30194+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30195+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
30196+ } else {
30197+ AuDebugOn(sb->s_d_op == &aufs_dop);
30198+ sb->s_d_op = &aufs_dop;
b95c5147
AM
30199+ AuDebugOn(sbi->si_iop_array == aufs_iop);
30200+ sbi->si_iop_array = aufs_iop;
79b8bda9 30201+ }
062440b3 30202+ pr_info("reset to %ps and %ps\n",
b95c5147 30203+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
30204+ }
30205+
027c5e7a 30206+ di_write_unlock(root);
b95c5147
AM
30207+ err = au_refresh_d(sb, do_idop);
30208+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
30209+ if (unlikely(e && !err))
30210+ err = e;
1facf9fc 30211+ /* aufs_write_lock() calls ..._child() */
30212+ di_write_lock_child(root);
027c5e7a
AM
30213+
30214+ au_cpup_attr_all(inode, /*force*/1);
30215+
30216+ if (unlikely(err))
30217+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 30218+}
30219+
30220+/* stop extra interpretation of errno in mount(8), and strange error messages */
30221+static int cvt_err(int err)
30222+{
30223+ AuTraceErr(err);
30224+
30225+ switch (err) {
30226+ case -ENOENT:
30227+ case -ENOTDIR:
30228+ case -EEXIST:
30229+ case -EIO:
30230+ err = -EINVAL;
30231+ }
30232+ return err;
30233+}
30234+
30235+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30236+{
4a4d8108
AM
30237+ int err, do_dx;
30238+ unsigned int mntflags;
be52b249
AM
30239+ struct au_opts opts = {
30240+ .opt = NULL
30241+ };
1facf9fc 30242+ struct dentry *root;
30243+ struct inode *inode;
30244+ struct au_sbinfo *sbinfo;
30245+
30246+ err = 0;
30247+ root = sb->s_root;
30248+ if (!data || !*data) {
e49829fe
JR
30249+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30250+ if (!err) {
30251+ di_write_lock_child(root);
30252+ err = au_opts_verify(sb, *flags, /*pending*/0);
30253+ aufs_write_unlock(root);
30254+ }
1facf9fc 30255+ goto out;
30256+ }
30257+
30258+ err = -ENOMEM;
1facf9fc 30259+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30260+ if (unlikely(!opts.opt))
30261+ goto out;
30262+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30263+ opts.flags = AuOpts_REMOUNT;
30264+ opts.sb_flags = *flags;
30265+
30266+ /* parse it before aufs lock */
30267+ err = au_opts_parse(sb, data, &opts);
30268+ if (unlikely(err))
30269+ goto out_opts;
30270+
30271+ sbinfo = au_sbi(sb);
5527c038 30272+ inode = d_inode(root);
febd17d6 30273+ inode_lock(inode);
e49829fe
JR
30274+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30275+ if (unlikely(err))
30276+ goto out_mtx;
30277+ di_write_lock_child(root);
1facf9fc 30278+
30279+ /* au_opts_remount() may return an error */
30280+ err = au_opts_remount(sb, &opts);
30281+ au_opts_free(&opts);
30282+
027c5e7a 30283+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 30284+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 30285+
4a4d8108
AM
30286+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30287+ mntflags = au_mntflags(sb);
30288+ do_dx = !!au_opt_test(mntflags, DIO);
30289+ au_dy_arefresh(do_dx);
30290+ }
30291+
076b876e 30292+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 30293+ aufs_write_unlock(root);
953406b4 30294+
e49829fe 30295+out_mtx:
febd17d6 30296+ inode_unlock(inode);
4f0767ce 30297+out_opts:
1c60b727 30298+ free_page((unsigned long)opts.opt);
4f0767ce 30299+out:
1facf9fc 30300+ err = cvt_err(err);
30301+ AuTraceErr(err);
30302+ return err;
30303+}
30304+
4a4d8108 30305+static const struct super_operations aufs_sop = {
1facf9fc 30306+ .alloc_inode = aufs_alloc_inode,
30307+ .destroy_inode = aufs_destroy_inode,
fbc438ed 30308+ .free_inode = aufs_free_inode,
b752ccd1 30309+ /* always deleting, no clearing */
1facf9fc 30310+ .drop_inode = generic_delete_inode,
30311+ .show_options = aufs_show_options,
30312+ .statfs = aufs_statfs,
30313+ .put_super = aufs_put_super,
537831f9 30314+ .sync_fs = aufs_sync_fs,
1facf9fc 30315+ .remount_fs = aufs_remount_fs
30316+};
30317+
30318+/* ---------------------------------------------------------------------- */
30319+
30320+static int alloc_root(struct super_block *sb)
30321+{
30322+ int err;
30323+ struct inode *inode;
30324+ struct dentry *root;
30325+
30326+ err = -ENOMEM;
30327+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30328+ err = PTR_ERR(inode);
30329+ if (IS_ERR(inode))
30330+ goto out;
30331+
b95c5147 30332+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 30333+ inode->i_fop = &aufs_dir_fop;
30334+ inode->i_mode = S_IFDIR;
9dbd164d 30335+ set_nlink(inode, 2);
1facf9fc 30336+ unlock_new_inode(inode);
30337+
92d182d2 30338+ root = d_make_root(inode);
1facf9fc 30339+ if (unlikely(!root))
92d182d2 30340+ goto out;
1facf9fc 30341+ err = PTR_ERR(root);
30342+ if (IS_ERR(root))
92d182d2 30343+ goto out;
1facf9fc 30344+
4a4d8108 30345+ err = au_di_init(root);
1facf9fc 30346+ if (!err) {
30347+ sb->s_root = root;
30348+ return 0; /* success */
30349+ }
30350+ dput(root);
1facf9fc 30351+
4f0767ce 30352+out:
1facf9fc 30353+ return err;
1facf9fc 30354+}
30355+
30356+static int aufs_fill_super(struct super_block *sb, void *raw_data,
30357+ int silent __maybe_unused)
30358+{
30359+ int err;
be52b249
AM
30360+ struct au_opts opts = {
30361+ .opt = NULL
30362+ };
79b8bda9 30363+ struct au_sbinfo *sbinfo;
1facf9fc 30364+ struct dentry *root;
30365+ struct inode *inode;
30366+ char *arg = raw_data;
30367+
30368+ if (unlikely(!arg || !*arg)) {
30369+ err = -EINVAL;
4a4d8108 30370+ pr_err("no arg\n");
1facf9fc 30371+ goto out;
30372+ }
30373+
30374+ err = -ENOMEM;
1facf9fc 30375+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30376+ if (unlikely(!opts.opt))
30377+ goto out;
30378+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30379+ opts.sb_flags = sb->s_flags;
30380+
30381+ err = au_si_alloc(sb);
30382+ if (unlikely(err))
30383+ goto out_opts;
79b8bda9 30384+ sbinfo = au_sbi(sb);
1facf9fc 30385+
30386+ /* all timestamps always follow the ones on the branch */
2121bcd9 30387+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
be118d29 30388+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
1facf9fc 30389+ sb->s_op = &aufs_sop;
027c5e7a 30390+ sb->s_d_op = &aufs_dop;
1facf9fc 30391+ sb->s_magic = AUFS_SUPER_MAGIC;
30392+ sb->s_maxbytes = 0;
c1595e42 30393+ sb->s_stack_depth = 1;
1facf9fc 30394+ au_export_init(sb);
f2c43d5f 30395+ au_xattr_init(sb);
1facf9fc 30396+
30397+ err = alloc_root(sb);
30398+ if (unlikely(err)) {
30399+ si_write_unlock(sb);
30400+ goto out_info;
30401+ }
30402+ root = sb->s_root;
5527c038 30403+ inode = d_inode(root);
1facf9fc 30404+
30405+ /*
30406+ * actually we can parse options regardless aufs lock here.
30407+ * but at remount time, parsing must be done before aufs lock.
30408+ * so we follow the same rule.
30409+ */
30410+ ii_write_lock_parent(inode);
30411+ aufs_write_unlock(root);
30412+ err = au_opts_parse(sb, arg, &opts);
30413+ if (unlikely(err))
30414+ goto out_root;
30415+
30416+ /* lock vfs_inode first, then aufs. */
febd17d6 30417+ inode_lock(inode);
1facf9fc 30418+ aufs_write_lock(root);
30419+ err = au_opts_mount(sb, &opts);
30420+ au_opts_free(&opts);
79b8bda9
AM
30421+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30422+ sb->s_d_op = &aufs_dop_noreval;
062440b3 30423+ pr_info("%ps\n", sb->s_d_op);
79b8bda9 30424+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30425+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30426+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30427+ }
1facf9fc 30428+ aufs_write_unlock(root);
febd17d6 30429+ inode_unlock(inode);
4a4d8108
AM
30430+ if (!err)
30431+ goto out_opts; /* success */
1facf9fc 30432+
4f0767ce 30433+out_root:
1facf9fc 30434+ dput(root);
30435+ sb->s_root = NULL;
4f0767ce 30436+out_info:
79b8bda9 30437+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30438+ sb->s_fs_info = NULL;
4f0767ce 30439+out_opts:
1c60b727 30440+ free_page((unsigned long)opts.opt);
4f0767ce 30441+out:
1facf9fc 30442+ AuTraceErr(err);
30443+ err = cvt_err(err);
30444+ AuTraceErr(err);
30445+ return err;
30446+}
30447+
30448+/* ---------------------------------------------------------------------- */
30449+
027c5e7a
AM
30450+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30451+ const char *dev_name __maybe_unused,
30452+ void *raw_data)
1facf9fc 30453+{
027c5e7a 30454+ struct dentry *root;
1facf9fc 30455+
30456+ /* all timestamps always follow the ones on the branch */
30457+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30458+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30459+ if (IS_ERR(root))
30460+ goto out;
30461+
062440b3 30462+ au_sbilist_add(root->d_sb);
027c5e7a
AM
30463+
30464+out:
30465+ return root;
1facf9fc 30466+}
30467+
e49829fe
JR
30468+static void aufs_kill_sb(struct super_block *sb)
30469+{
30470+ struct au_sbinfo *sbinfo;
30471+
30472+ sbinfo = au_sbi(sb);
30473+ if (sbinfo) {
30474+ au_sbilist_del(sb);
30475+ aufs_write_lock(sb->s_root);
076b876e 30476+ au_fhsm_fin(sb);
e49829fe
JR
30477+ if (sbinfo->si_wbr_create_ops->fin)
30478+ sbinfo->si_wbr_create_ops->fin(sb);
30479+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30480+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30481+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30482+ }
30483+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30484+ au_plink_put(sb, /*verbose*/1);
30485+ au_xino_clr(sb);
8b6a4947 30486+ au_dr_opt_flush(sb);
1e00d052 30487+ sbinfo->si_sb = NULL;
e49829fe 30488+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30489+ au_nwt_flush(&sbinfo->si_nowait);
30490+ }
98d9a5b1 30491+ kill_anon_super(sb);
e49829fe
JR
30492+}
30493+
1facf9fc 30494+struct file_system_type aufs_fs_type = {
30495+ .name = AUFS_FSTYPE,
c06a8ce3
AM
30496+ /* a race between rename and others */
30497+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 30498+ .mount = aufs_mount,
e49829fe 30499+ .kill_sb = aufs_kill_sb,
1facf9fc 30500+ /* no need to __module_get() and module_put(). */
30501+ .owner = THIS_MODULE,
30502+};
7f207e10 30503diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
eca34b5c
AM
30504--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
30505+++ linux/fs/aufs/super.h 2019-07-11 15:42:14.472238057 +0200
acd2b654 30506@@ -0,0 +1,589 @@
062440b3 30507+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30508+/*
ba1aed25 30509+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 30510+ *
30511+ * This program, aufs is free software; you can redistribute it and/or modify
30512+ * it under the terms of the GNU General Public License as published by
30513+ * the Free Software Foundation; either version 2 of the License, or
30514+ * (at your option) any later version.
dece6358
AM
30515+ *
30516+ * This program is distributed in the hope that it will be useful,
30517+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30518+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30519+ * GNU General Public License for more details.
30520+ *
30521+ * You should have received a copy of the GNU General Public License
523b37e3 30522+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30523+ */
30524+
30525+/*
30526+ * super_block operations
30527+ */
30528+
30529+#ifndef __AUFS_SUPER_H__
30530+#define __AUFS_SUPER_H__
30531+
30532+#ifdef __KERNEL__
30533+
30534+#include <linux/fs.h>
5527c038 30535+#include <linux/kobject.h>
8b6a4947 30536+#include "hbl.h"
acd2b654 30537+#include "lcnt.h"
1facf9fc 30538+#include "rwsem.h"
1facf9fc 30539+#include "wkq.h"
30540+
1facf9fc 30541+/* policies to select one among multiple writable branches */
30542+struct au_wbr_copyup_operations {
30543+ int (*copyup)(struct dentry *dentry);
30544+};
30545+
392086de
AM
30546+#define AuWbr_DIR 1 /* target is a dir */
30547+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30548+
30549+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30550+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30551+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30552+
1facf9fc 30553+struct au_wbr_create_operations {
392086de 30554+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30555+ int (*init)(struct super_block *sb);
30556+ int (*fin)(struct super_block *sb);
30557+};
30558+
30559+struct au_wbr_mfs {
30560+ struct mutex mfs_lock; /* protect this structure */
30561+ unsigned long mfs_jiffy;
30562+ unsigned long mfs_expire;
30563+ aufs_bindex_t mfs_bindex;
30564+
30565+ unsigned long long mfsrr_bytes;
30566+ unsigned long long mfsrr_watermark;
30567+};
30568+
86dc4139
AM
30569+#define AuPlink_NHASH 100
30570+static inline int au_plink_hash(ino_t ino)
30571+{
30572+ return ino % AuPlink_NHASH;
30573+}
30574+
076b876e
AM
30575+/* File-based Hierarchical Storage Management */
30576+struct au_fhsm {
30577+#ifdef CONFIG_AUFS_FHSM
30578+ /* allow only one process who can receive the notification */
30579+ spinlock_t fhsm_spin;
30580+ pid_t fhsm_pid;
30581+ wait_queue_head_t fhsm_wqh;
30582+ atomic_t fhsm_readable;
30583+
c1595e42 30584+ /* these are protected by si_rwsem */
076b876e 30585+ unsigned long fhsm_expire;
c1595e42 30586+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30587+#endif
30588+};
30589+
1facf9fc 30590+struct au_branch;
30591+struct au_sbinfo {
30592+ /* nowait tasks in the system-wide workqueue */
30593+ struct au_nowait_tasks si_nowait;
30594+
b752ccd1 30595+ /*
acd2b654 30596+ * tried sb->s_umount, but failed due to the dependency between i_mutex.
b752ccd1
AM
30597+ * rwsem for au_sbinfo is necessary.
30598+ */
dece6358 30599+ struct au_rwsem si_rwsem;
1facf9fc 30600+
7f207e10 30601+ /*
523b37e3
AM
30602+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30603+ * remount.
7f207e10 30604+ */
acd2b654 30605+ au_lcnt_t si_ninodes, si_nfiles;
7f207e10 30606+
1facf9fc 30607+ /* branch management */
30608+ unsigned int si_generation;
30609+
2000de60 30610+ /* see AuSi_ flags */
1facf9fc 30611+ unsigned char au_si_status;
30612+
5afbbe0d 30613+ aufs_bindex_t si_bbot;
7f207e10
AM
30614+
30615+ /* dirty trick to keep br_id plus */
30616+ unsigned int si_last_br_id :
30617+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30618+ struct au_branch **si_branch;
30619+
30620+ /* policy to select a writable branch */
30621+ unsigned char si_wbr_copyup;
30622+ unsigned char si_wbr_create;
30623+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30624+ struct au_wbr_create_operations *si_wbr_create_ops;
30625+
30626+ /* round robin */
30627+ atomic_t si_wbr_rr_next;
30628+
30629+ /* most free space */
30630+ struct au_wbr_mfs si_wbr_mfs;
30631+
076b876e
AM
30632+ /* File-based Hierarchical Storage Management */
30633+ struct au_fhsm si_fhsm;
30634+
1facf9fc 30635+ /* mount flags */
30636+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30637+ unsigned int si_mntflags;
30638+
30639+ /* external inode number (bitmap and translation table) */
5527c038
JR
30640+ vfs_readf_t si_xread;
30641+ vfs_writef_t si_xwrite;
acd2b654
AM
30642+ loff_t si_ximaxent; /* max entries in a xino */
30643+
1facf9fc 30644+ struct file *si_xib;
30645+ struct mutex si_xib_mtx; /* protect xib members */
30646+ unsigned long *si_xib_buf;
30647+ unsigned long si_xib_last_pindex;
30648+ int si_xib_next_bit;
acd2b654 30649+
392086de
AM
30650+ unsigned long si_xino_jiffy;
30651+ unsigned long si_xino_expire;
1facf9fc 30652+ /* reserved for future use */
30653+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30654+
30655+#ifdef CONFIG_AUFS_EXPORT
30656+ /* i_generation */
acd2b654 30657+ /* todo: make xigen file an array to support many inode numbers */
1facf9fc 30658+ struct file *si_xigen;
30659+ atomic_t si_xigen_next;
30660+#endif
30661+
acd2b654 30662+ /* dirty trick to support atomic_open */
8b6a4947 30663+ struct hlist_bl_head si_aopen;
b912730e 30664+
1facf9fc 30665+ /* vdir parameters */
e49829fe 30666+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30667+ unsigned int si_rdblk; /* deblk size */
30668+ unsigned int si_rdhash; /* hash size */
30669+
30670+ /*
30671+ * If the number of whiteouts are larger than si_dirwh, leave all of
30672+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30673+ * future fsck.aufs or kernel thread will remove them later.
30674+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30675+ */
30676+ unsigned int si_dirwh;
30677+
1facf9fc 30678+ /* pseudo_link list */
8b6a4947 30679+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30680+ wait_queue_head_t si_plink_wq;
4a4d8108 30681+ spinlock_t si_plink_maint_lock;
e49829fe 30682+ pid_t si_plink_maint_pid;
1facf9fc 30683+
523b37e3 30684+ /* file list */
8b6a4947 30685+ struct hlist_bl_head si_files;
523b37e3 30686+
b95c5147
AM
30687+ /* with/without getattr, brother of sb->s_d_op */
30688+ struct inode_operations *si_iop_array;
30689+
1facf9fc 30690+ /*
30691+ * sysfs and lifetime management.
30692+ * this is not a small structure and it may be a waste of memory in case
acd2b654 30693+ * of sysfs is disabled, particularly when many aufs-es are mounted.
1facf9fc 30694+ * but using sysfs is majority.
30695+ */
30696+ struct kobject si_kobj;
30697+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30698+ struct dentry *si_dbgaufs;
30699+ struct dentry *si_dbgaufs_plink;
30700+ struct dentry *si_dbgaufs_xib;
1facf9fc 30701+#ifdef CONFIG_AUFS_EXPORT
30702+ struct dentry *si_dbgaufs_xigen;
30703+#endif
30704+#endif
30705+
e49829fe 30706+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30707+ struct hlist_bl_node si_list;
e49829fe
JR
30708+#endif
30709+
1facf9fc 30710+ /* dirty, necessary for unmounting, sysfs and sysrq */
30711+ struct super_block *si_sb;
30712+};
30713+
dece6358
AM
30714+/* sbinfo status flags */
30715+/*
30716+ * set true when refresh_dirs() failed at remount time.
30717+ * then try refreshing dirs at access time again.
062440b3 30718+ * if it is false, refreshing dirs at access time is unnecessary
dece6358 30719+ */
027c5e7a 30720+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30721+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30722+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30723+
30724+#ifndef CONFIG_AUFS_FHSM
30725+#undef AuSi_FHSM
30726+#define AuSi_FHSM 0
30727+#endif
30728+
dece6358
AM
30729+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30730+ unsigned int flag)
30731+{
30732+ AuRwMustAnyLock(&sbi->si_rwsem);
30733+ return sbi->au_si_status & flag;
30734+}
30735+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30736+#define au_fset_si(sbinfo, name) do { \
30737+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30738+ (sbinfo)->au_si_status |= AuSi_##name; \
30739+} while (0)
30740+#define au_fclr_si(sbinfo, name) do { \
30741+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30742+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30743+} while (0)
30744+
1facf9fc 30745+/* ---------------------------------------------------------------------- */
30746+
30747+/* policy to select one among writable branches */
4a4d8108
AM
30748+#define AuWbrCopyup(sbinfo, ...) \
30749+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30750+#define AuWbrCreate(sbinfo, ...) \
30751+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30752+
30753+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30754+#define AuLock_DW 1 /* write-lock dentry */
30755+#define AuLock_IR (1 << 1) /* read-lock inode */
30756+#define AuLock_IW (1 << 2) /* write-lock inode */
30757+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30758+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30759+ /* except RENAME_EXCHANGE */
e49829fe
JR
30760+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30761+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30762+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30763+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30764+#define au_fset_lock(flags, name) \
30765+ do { (flags) |= AuLock_##name; } while (0)
30766+#define au_fclr_lock(flags, name) \
30767+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30768+
30769+/* ---------------------------------------------------------------------- */
30770+
30771+/* super.c */
30772+extern struct file_system_type aufs_fs_type;
30773+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30774+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30775+ unsigned long long max, void *arg);
79b8bda9
AM
30776+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30777+ struct super_block *sb, void *arg);
7f207e10
AM
30778+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30779+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30780+
30781+/* sbinfo.c */
30782+void au_si_free(struct kobject *kobj);
30783+int au_si_alloc(struct super_block *sb);
e2f27e51 30784+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30785+
30786+unsigned int au_sigen_inc(struct super_block *sb);
30787+aufs_bindex_t au_new_br_id(struct super_block *sb);
30788+
e49829fe
JR
30789+int si_read_lock(struct super_block *sb, int flags);
30790+int si_write_lock(struct super_block *sb, int flags);
30791+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30792+void aufs_read_unlock(struct dentry *dentry, int flags);
30793+void aufs_write_lock(struct dentry *dentry);
30794+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30795+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30796+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30797+
30798+/* wbr_policy.c */
30799+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30800+extern struct au_wbr_create_operations au_wbr_create_ops[];
30801+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30802+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30803+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30804+
30805+/* mvdown.c */
30806+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30807+
076b876e
AM
30808+#ifdef CONFIG_AUFS_FHSM
30809+/* fhsm.c */
30810+
30811+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30812+{
30813+ pid_t pid;
30814+
30815+ spin_lock(&fhsm->fhsm_spin);
30816+ pid = fhsm->fhsm_pid;
30817+ spin_unlock(&fhsm->fhsm_spin);
30818+
30819+ return pid;
30820+}
30821+
30822+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30823+void au_fhsm_wrote_all(struct super_block *sb, int force);
30824+int au_fhsm_fd(struct super_block *sb, int oflags);
30825+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30826+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30827+void au_fhsm_fin(struct super_block *sb);
30828+void au_fhsm_init(struct au_sbinfo *sbinfo);
30829+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30830+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30831+#else
30832+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30833+ int force)
30834+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30835+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30836+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30837+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30838+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30839+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30840+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30841+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30842+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30843+#endif
30844+
1facf9fc 30845+/* ---------------------------------------------------------------------- */
30846+
30847+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30848+{
30849+ return sb->s_fs_info;
30850+}
30851+
30852+/* ---------------------------------------------------------------------- */
30853+
30854+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30855+int au_test_nfsd(void);
1facf9fc 30856+void au_export_init(struct super_block *sb);
b752ccd1 30857+void au_xigen_inc(struct inode *inode);
1facf9fc 30858+int au_xigen_new(struct inode *inode);
062440b3 30859+int au_xigen_set(struct super_block *sb, struct path *path);
1facf9fc 30860+void au_xigen_clr(struct super_block *sb);
30861+
30862+static inline int au_busy_or_stale(void)
30863+{
b752ccd1 30864+ if (!au_test_nfsd())
1facf9fc 30865+ return -EBUSY;
30866+ return -ESTALE;
30867+}
30868+#else
b752ccd1 30869+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30870+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30871+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108 30872+AuStubInt0(au_xigen_new, struct inode *inode)
062440b3 30873+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
4a4d8108 30874+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30875+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30876+#endif /* CONFIG_AUFS_EXPORT */
30877+
30878+/* ---------------------------------------------------------------------- */
30879+
e49829fe
JR
30880+#ifdef CONFIG_AUFS_SBILIST
30881+/* module.c */
8b6a4947 30882+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30883+
30884+static inline void au_sbilist_init(void)
30885+{
8b6a4947 30886+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30887+}
30888+
30889+static inline void au_sbilist_add(struct super_block *sb)
30890+{
8b6a4947 30891+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30892+}
30893+
30894+static inline void au_sbilist_del(struct super_block *sb)
30895+{
8b6a4947 30896+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30897+}
53392da6
AM
30898+
30899+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30900+static inline void au_sbilist_lock(void)
30901+{
8b6a4947 30902+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30903+}
30904+
30905+static inline void au_sbilist_unlock(void)
30906+{
8b6a4947 30907+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30908+}
30909+#define AuGFP_SBILIST GFP_ATOMIC
30910+#else
30911+AuStubVoid(au_sbilist_lock, void)
30912+AuStubVoid(au_sbilist_unlock, void)
30913+#define AuGFP_SBILIST GFP_NOFS
30914+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
30915+#else
30916+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
30917+AuStubVoid(au_sbilist_add, struct super_block *sb)
30918+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
30919+AuStubVoid(au_sbilist_lock, void)
30920+AuStubVoid(au_sbilist_unlock, void)
30921+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
30922+#endif
30923+
30924+/* ---------------------------------------------------------------------- */
30925+
1facf9fc 30926+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30927+{
dece6358 30928+ /*
c1595e42 30929+ * This function is a dynamic '__init' function actually,
dece6358
AM
30930+ * so the tiny check for si_rwsem is unnecessary.
30931+ */
30932+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 30933+#ifdef CONFIG_DEBUG_FS
30934+ sbinfo->si_dbgaufs = NULL;
86dc4139 30935+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 30936+ sbinfo->si_dbgaufs_xib = NULL;
30937+#ifdef CONFIG_AUFS_EXPORT
30938+ sbinfo->si_dbgaufs_xigen = NULL;
30939+#endif
30940+#endif
30941+}
30942+
30943+/* ---------------------------------------------------------------------- */
30944+
a2654f78
AM
30945+/* current->atomic_flags */
30946+/* this value should never corrupt the ones defined in linux/sched.h */
fbc438ed 30947+#define PFA_AUFS 0x10
a2654f78
AM
30948+
30949+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30950+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
30951+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
30952+
30953+static inline int si_pid_test(struct super_block *sb)
30954+{
a2654f78 30955+ return !!task_test_aufs(current);
b752ccd1
AM
30956+}
30957+
30958+static inline void si_pid_clr(struct super_block *sb)
30959+{
a2654f78
AM
30960+ AuDebugOn(!task_test_aufs(current));
30961+ task_clear_aufs(current);
b752ccd1
AM
30962+}
30963+
a2654f78
AM
30964+static inline void si_pid_set(struct super_block *sb)
30965+{
30966+ AuDebugOn(task_test_aufs(current));
30967+ task_set_aufs(current);
30968+}
febd17d6 30969+
b752ccd1
AM
30970+/* ---------------------------------------------------------------------- */
30971+
1facf9fc 30972+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
30973+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30974+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30975+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30976+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30977+/*
30978+#define __si_read_trylock_nested(sb) \
30979+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30980+#define __si_write_trylock_nested(sb) \
30981+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30982+*/
30983+
30984+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30985+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30986+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 30987+
dece6358
AM
30988+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30989+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30990+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30991+
b752ccd1
AM
30992+static inline void si_noflush_read_lock(struct super_block *sb)
30993+{
30994+ __si_read_lock(sb);
30995+ si_pid_set(sb);
30996+}
30997+
30998+static inline int si_noflush_read_trylock(struct super_block *sb)
30999+{
076b876e
AM
31000+ int locked;
31001+
31002+ locked = __si_read_trylock(sb);
b752ccd1
AM
31003+ if (locked)
31004+ si_pid_set(sb);
31005+ return locked;
31006+}
31007+
31008+static inline void si_noflush_write_lock(struct super_block *sb)
31009+{
31010+ __si_write_lock(sb);
31011+ si_pid_set(sb);
31012+}
31013+
31014+static inline int si_noflush_write_trylock(struct super_block *sb)
31015+{
076b876e
AM
31016+ int locked;
31017+
31018+ locked = __si_write_trylock(sb);
b752ccd1
AM
31019+ if (locked)
31020+ si_pid_set(sb);
31021+ return locked;
31022+}
31023+
7e9cd9fe 31024+#if 0 /* reserved */
1facf9fc 31025+static inline int si_read_trylock(struct super_block *sb, int flags)
31026+{
31027+ if (au_ftest_lock(flags, FLUSH))
31028+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31029+ return si_noflush_read_trylock(sb);
31030+}
e49829fe 31031+#endif
1facf9fc 31032+
b752ccd1
AM
31033+static inline void si_read_unlock(struct super_block *sb)
31034+{
31035+ si_pid_clr(sb);
31036+ __si_read_unlock(sb);
31037+}
31038+
7e9cd9fe 31039+#if 0 /* reserved */
1facf9fc 31040+static inline int si_write_trylock(struct super_block *sb, int flags)
31041+{
31042+ if (au_ftest_lock(flags, FLUSH))
31043+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31044+ return si_noflush_write_trylock(sb);
31045+}
b752ccd1
AM
31046+#endif
31047+
31048+static inline void si_write_unlock(struct super_block *sb)
31049+{
31050+ si_pid_clr(sb);
31051+ __si_write_unlock(sb);
31052+}
31053+
7e9cd9fe 31054+#if 0 /* reserved */
b752ccd1
AM
31055+static inline void si_downgrade_lock(struct super_block *sb)
31056+{
31057+ __si_downgrade_lock(sb);
31058+}
31059+#endif
1facf9fc 31060+
31061+/* ---------------------------------------------------------------------- */
31062+
5afbbe0d 31063+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 31064+{
dece6358 31065+ SiMustAnyLock(sb);
5afbbe0d 31066+ return au_sbi(sb)->si_bbot;
1facf9fc 31067+}
31068+
31069+static inline unsigned int au_mntflags(struct super_block *sb)
31070+{
dece6358 31071+ SiMustAnyLock(sb);
1facf9fc 31072+ return au_sbi(sb)->si_mntflags;
31073+}
31074+
31075+static inline unsigned int au_sigen(struct super_block *sb)
31076+{
dece6358 31077+ SiMustAnyLock(sb);
1facf9fc 31078+ return au_sbi(sb)->si_generation;
31079+}
31080+
31081+static inline struct au_branch *au_sbr(struct super_block *sb,
31082+ aufs_bindex_t bindex)
31083+{
dece6358 31084+ SiMustAnyLock(sb);
1facf9fc 31085+ return au_sbi(sb)->si_branch[0 + bindex];
31086+}
31087+
acd2b654 31088+static inline loff_t au_xi_maxent(struct super_block *sb)
1facf9fc 31089+{
dece6358 31090+ SiMustAnyLock(sb);
acd2b654 31091+ return au_sbi(sb)->si_ximaxent;
1facf9fc 31092+}
31093+
31094+#endif /* __KERNEL__ */
31095+#endif /* __AUFS_SUPER_H__ */
7f207e10 31096diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
eca34b5c
AM
31097--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
31098+++ linux/fs/aufs/sysaufs.c 2019-07-11 15:42:14.472238057 +0200
062440b3 31099@@ -0,0 +1,93 @@
cd7a4cd9 31100+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31101+/*
ba1aed25 31102+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 31103+ *
31104+ * This program, aufs is free software; you can redistribute it and/or modify
31105+ * it under the terms of the GNU General Public License as published by
31106+ * the Free Software Foundation; either version 2 of the License, or
31107+ * (at your option) any later version.
dece6358
AM
31108+ *
31109+ * This program is distributed in the hope that it will be useful,
31110+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31111+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31112+ * GNU General Public License for more details.
31113+ *
31114+ * You should have received a copy of the GNU General Public License
523b37e3 31115+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31116+ */
31117+
31118+/*
31119+ * sysfs interface and lifetime management
31120+ * they are necessary regardless sysfs is disabled.
31121+ */
31122+
1facf9fc 31123+#include <linux/random.h>
1facf9fc 31124+#include "aufs.h"
31125+
31126+unsigned long sysaufs_si_mask;
e49829fe 31127+struct kset *sysaufs_kset;
1facf9fc 31128+
31129+#define AuSiAttr(_name) { \
31130+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
31131+ .show = sysaufs_si_##_name, \
31132+}
31133+
31134+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31135+struct attribute *sysaufs_si_attrs[] = {
31136+ &sysaufs_si_attr_xi_path.attr,
31137+ NULL,
31138+};
31139+
4a4d8108 31140+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 31141+ .show = sysaufs_si_show
31142+};
31143+
31144+static struct kobj_type au_sbi_ktype = {
31145+ .release = au_si_free,
31146+ .sysfs_ops = &au_sbi_ops,
31147+ .default_attrs = sysaufs_si_attrs
31148+};
31149+
31150+/* ---------------------------------------------------------------------- */
31151+
31152+int sysaufs_si_init(struct au_sbinfo *sbinfo)
31153+{
31154+ int err;
31155+
e49829fe 31156+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 31157+ /* cf. sysaufs_name() */
31158+ err = kobject_init_and_add
e49829fe 31159+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 31160+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31161+
1facf9fc 31162+ return err;
31163+}
31164+
31165+void sysaufs_fin(void)
31166+{
e49829fe
JR
31167+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31168+ kset_unregister(sysaufs_kset);
1facf9fc 31169+}
31170+
31171+int __init sysaufs_init(void)
31172+{
31173+ int err;
31174+
31175+ do {
31176+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31177+ } while (!sysaufs_si_mask);
31178+
4a4d8108 31179+ err = -EINVAL;
e49829fe
JR
31180+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31181+ if (unlikely(!sysaufs_kset))
4a4d8108 31182+ goto out;
e49829fe
JR
31183+ err = PTR_ERR(sysaufs_kset);
31184+ if (IS_ERR(sysaufs_kset))
1facf9fc 31185+ goto out;
e49829fe 31186+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
062440b3 31187+ if (unlikely(err))
e49829fe 31188+ kset_unregister(sysaufs_kset);
1facf9fc 31189+
4f0767ce 31190+out:
1facf9fc 31191+ return err;
31192+}
7f207e10 31193diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
eca34b5c
AM
31194--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
31195+++ linux/fs/aufs/sysaufs.h 2019-07-11 15:42:14.475571481 +0200
062440b3
AM
31196@@ -0,0 +1,102 @@
31197+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31198+/*
ba1aed25 31199+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 31200+ *
31201+ * This program, aufs is free software; you can redistribute it and/or modify
31202+ * it under the terms of the GNU General Public License as published by
31203+ * the Free Software Foundation; either version 2 of the License, or
31204+ * (at your option) any later version.
dece6358
AM
31205+ *
31206+ * This program is distributed in the hope that it will be useful,
31207+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31208+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31209+ * GNU General Public License for more details.
31210+ *
31211+ * You should have received a copy of the GNU General Public License
523b37e3 31212+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31213+ */
31214+
31215+/*
31216+ * sysfs interface and mount lifetime management
31217+ */
31218+
31219+#ifndef __SYSAUFS_H__
31220+#define __SYSAUFS_H__
31221+
31222+#ifdef __KERNEL__
31223+
1facf9fc 31224+#include <linux/sysfs.h>
1facf9fc 31225+#include "module.h"
31226+
dece6358
AM
31227+struct super_block;
31228+struct au_sbinfo;
31229+
1facf9fc 31230+struct sysaufs_si_attr {
31231+ struct attribute attr;
31232+ int (*show)(struct seq_file *seq, struct super_block *sb);
31233+};
31234+
31235+/* ---------------------------------------------------------------------- */
31236+
31237+/* sysaufs.c */
31238+extern unsigned long sysaufs_si_mask;
e49829fe 31239+extern struct kset *sysaufs_kset;
1facf9fc 31240+extern struct attribute *sysaufs_si_attrs[];
31241+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31242+int __init sysaufs_init(void);
31243+void sysaufs_fin(void);
31244+
31245+/* ---------------------------------------------------------------------- */
31246+
31247+/* some people doesn't like to show a pointer in kernel */
31248+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31249+{
31250+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31251+}
31252+
31253+#define SysaufsSiNamePrefix "si_"
31254+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31255+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31256+{
31257+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31258+ sysaufs_si_id(sbinfo));
31259+}
31260+
31261+struct au_branch;
31262+#ifdef CONFIG_SYSFS
31263+/* sysfs.c */
31264+extern struct attribute_group *sysaufs_attr_group;
31265+
31266+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31267+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31268+ char *buf);
076b876e
AM
31269+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31270+#ifdef CONFIG_COMPAT
31271+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31272+#endif
1facf9fc 31273+
31274+void sysaufs_br_init(struct au_branch *br);
31275+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31276+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31277+
31278+#define sysaufs_brs_init() do {} while (0)
31279+
31280+#else
31281+#define sysaufs_attr_group NULL
31282+
4a4d8108 31283+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
31284+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31285+ struct attribute *attr, char *buf)
4a4d8108
AM
31286+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31287+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31288+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 31289+
31290+static inline void sysaufs_brs_init(void)
31291+{
31292+ sysaufs_brs = 0;
31293+}
31294+
31295+#endif /* CONFIG_SYSFS */
31296+
31297+#endif /* __KERNEL__ */
31298+#endif /* __SYSAUFS_H__ */
7f207e10 31299diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
eca34b5c
AM
31300--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
31301+++ linux/fs/aufs/sysfs.c 2019-07-11 15:42:14.475571481 +0200
ba1aed25 31302@@ -0,0 +1,374 @@
cd7a4cd9 31303+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31304+/*
ba1aed25 31305+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 31306+ *
31307+ * This program, aufs is free software; you can redistribute it and/or modify
31308+ * it under the terms of the GNU General Public License as published by
31309+ * the Free Software Foundation; either version 2 of the License, or
31310+ * (at your option) any later version.
dece6358
AM
31311+ *
31312+ * This program is distributed in the hope that it will be useful,
31313+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31314+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31315+ * GNU General Public License for more details.
31316+ *
31317+ * You should have received a copy of the GNU General Public License
523b37e3 31318+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31319+ */
31320+
31321+/*
31322+ * sysfs interface
31323+ */
31324+
076b876e 31325+#include <linux/compat.h>
1facf9fc 31326+#include <linux/seq_file.h>
1facf9fc 31327+#include "aufs.h"
31328+
4a4d8108
AM
31329+#ifdef CONFIG_AUFS_FS_MODULE
31330+/* this entry violates the "one line per file" policy of sysfs */
31331+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31332+ char *buf)
31333+{
31334+ ssize_t err;
31335+ static char *conf =
31336+/* this file is generated at compiling */
31337+#include "conf.str"
31338+ ;
31339+
31340+ err = snprintf(buf, PAGE_SIZE, conf);
31341+ if (unlikely(err >= PAGE_SIZE))
31342+ err = -EFBIG;
31343+ return err;
31344+}
31345+
31346+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31347+#endif
31348+
1facf9fc 31349+static struct attribute *au_attr[] = {
4a4d8108
AM
31350+#ifdef CONFIG_AUFS_FS_MODULE
31351+ &au_config_attr.attr,
31352+#endif
1facf9fc 31353+ NULL, /* need to NULL terminate the list of attributes */
31354+};
31355+
31356+static struct attribute_group sysaufs_attr_group_body = {
31357+ .attrs = au_attr
31358+};
31359+
31360+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31361+
31362+/* ---------------------------------------------------------------------- */
31363+
31364+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31365+{
31366+ int err;
31367+
dece6358
AM
31368+ SiMustAnyLock(sb);
31369+
1facf9fc 31370+ err = 0;
31371+ if (au_opt_test(au_mntflags(sb), XINO)) {
31372+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31373+ seq_putc(seq, '\n');
31374+ }
31375+ return err;
31376+}
31377+
31378+/*
31379+ * the lifetime of branch is independent from the entry under sysfs.
31380+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31381+ * unlinked.
31382+ */
31383+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31384+ aufs_bindex_t bindex, int idx)
1facf9fc 31385+{
1e00d052 31386+ int err;
1facf9fc 31387+ struct path path;
31388+ struct dentry *root;
31389+ struct au_branch *br;
076b876e 31390+ au_br_perm_str_t perm;
1facf9fc 31391+
31392+ AuDbg("b%d\n", bindex);
31393+
1e00d052 31394+ err = 0;
1facf9fc 31395+ root = sb->s_root;
31396+ di_read_lock_parent(root, !AuLock_IR);
31397+ br = au_sbr(sb, bindex);
392086de
AM
31398+
31399+ switch (idx) {
31400+ case AuBrSysfs_BR:
31401+ path.mnt = au_br_mnt(br);
31402+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31403+ err = au_seq_path(seq, &path);
31404+ if (!err) {
31405+ au_optstr_br_perm(&perm, br->br_perm);
31406+ seq_printf(seq, "=%s\n", perm.a);
31407+ }
392086de
AM
31408+ break;
31409+ case AuBrSysfs_BRID:
79b8bda9 31410+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31411+ break;
31412+ }
076b876e 31413+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31414+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31415+ err = -E2BIG;
392086de 31416+
1e00d052 31417+ return err;
1facf9fc 31418+}
31419+
31420+/* ---------------------------------------------------------------------- */
31421+
31422+static struct seq_file *au_seq(char *p, ssize_t len)
31423+{
31424+ struct seq_file *seq;
31425+
31426+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31427+ if (seq) {
31428+ /* mutex_init(&seq.lock); */
31429+ seq->buf = p;
31430+ seq->size = len;
31431+ return seq; /* success */
31432+ }
31433+
31434+ seq = ERR_PTR(-ENOMEM);
31435+ return seq;
31436+}
31437+
392086de
AM
31438+#define SysaufsBr_PREFIX "br"
31439+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31440+
31441+/* todo: file size may exceed PAGE_SIZE */
31442+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31443+ char *buf)
1facf9fc 31444+{
31445+ ssize_t err;
392086de 31446+ int idx;
1facf9fc 31447+ long l;
5afbbe0d 31448+ aufs_bindex_t bbot;
1facf9fc 31449+ struct au_sbinfo *sbinfo;
31450+ struct super_block *sb;
31451+ struct seq_file *seq;
31452+ char *name;
31453+ struct attribute **cattr;
31454+
31455+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31456+ sb = sbinfo->si_sb;
1308ab2a 31457+
31458+ /*
31459+ * prevent a race condition between sysfs and aufs.
31460+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31461+ * prohibits maintaining the sysfs entries.
31462+ * hew we acquire read lock after sysfs_get_active_two().
31463+ * on the other hand, the remount process may maintain the sysfs/aufs
31464+ * entries after acquiring write lock.
31465+ * it can cause a deadlock.
31466+ * simply we gave up processing read here.
31467+ */
31468+ err = -EBUSY;
31469+ if (unlikely(!si_noflush_read_trylock(sb)))
31470+ goto out;
1facf9fc 31471+
31472+ seq = au_seq(buf, PAGE_SIZE);
31473+ err = PTR_ERR(seq);
31474+ if (IS_ERR(seq))
1308ab2a 31475+ goto out_unlock;
1facf9fc 31476+
31477+ name = (void *)attr->name;
31478+ cattr = sysaufs_si_attrs;
31479+ while (*cattr) {
31480+ if (!strcmp(name, (*cattr)->name)) {
31481+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31482+ ->show(seq, sb);
31483+ goto out_seq;
31484+ }
31485+ cattr++;
31486+ }
31487+
392086de
AM
31488+ if (!strncmp(name, SysaufsBrid_PREFIX,
31489+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31490+ idx = AuBrSysfs_BRID;
31491+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31492+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31493+ sizeof(SysaufsBr_PREFIX) - 1)) {
31494+ idx = AuBrSysfs_BR;
1facf9fc 31495+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31496+ } else
31497+ BUG();
31498+
31499+ err = kstrtol(name, 10, &l);
31500+ if (!err) {
5afbbe0d
AM
31501+ bbot = au_sbbot(sb);
31502+ if (l <= bbot)
392086de
AM
31503+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31504+ else
31505+ err = -ENOENT;
1facf9fc 31506+ }
1facf9fc 31507+
4f0767ce 31508+out_seq:
1facf9fc 31509+ if (!err) {
31510+ err = seq->count;
31511+ /* sysfs limit */
31512+ if (unlikely(err == PAGE_SIZE))
31513+ err = -EFBIG;
31514+ }
9f237c51 31515+ au_kfree_rcu(seq);
4f0767ce 31516+out_unlock:
1facf9fc 31517+ si_read_unlock(sb);
4f0767ce 31518+out:
1facf9fc 31519+ return err;
31520+}
31521+
31522+/* ---------------------------------------------------------------------- */
31523+
076b876e
AM
31524+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31525+{
31526+ int err;
31527+ int16_t brid;
5afbbe0d 31528+ aufs_bindex_t bindex, bbot;
076b876e
AM
31529+ size_t sz;
31530+ char *buf;
31531+ struct seq_file *seq;
31532+ struct au_branch *br;
31533+
31534+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31535+ bbot = au_sbbot(sb);
31536+ err = bbot + 1;
076b876e
AM
31537+ if (!arg)
31538+ goto out;
31539+
31540+ err = -ENOMEM;
31541+ buf = (void *)__get_free_page(GFP_NOFS);
31542+ if (unlikely(!buf))
31543+ goto out;
31544+
31545+ seq = au_seq(buf, PAGE_SIZE);
31546+ err = PTR_ERR(seq);
31547+ if (IS_ERR(seq))
31548+ goto out_buf;
31549+
31550+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31551+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
ba1aed25
AM
31552+ /* VERIFY_WRITE */
31553+ err = !access_ok(arg, sizeof(*arg));
076b876e
AM
31554+ if (unlikely(err))
31555+ break;
31556+
31557+ br = au_sbr(sb, bindex);
31558+ brid = br->br_id;
31559+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31560+ err = __put_user(brid, &arg->id);
31561+ if (unlikely(err))
31562+ break;
31563+
31564+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31565+ err = __put_user(br->br_perm, &arg->perm);
31566+ if (unlikely(err))
31567+ break;
31568+
79b8bda9
AM
31569+ err = au_seq_path(seq, &br->br_path);
31570+ if (unlikely(err))
31571+ break;
31572+ seq_putc(seq, '\0');
31573+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31574+ err = copy_to_user(arg->path, seq->buf, seq->count);
31575+ seq->count = 0;
31576+ if (unlikely(err))
31577+ break;
31578+ } else {
31579+ err = -E2BIG;
31580+ goto out_seq;
31581+ }
31582+ }
31583+ if (unlikely(err))
31584+ err = -EFAULT;
31585+
31586+out_seq:
9f237c51 31587+ au_kfree_rcu(seq);
076b876e 31588+out_buf:
1c60b727 31589+ free_page((unsigned long)buf);
076b876e
AM
31590+out:
31591+ si_read_unlock(sb);
31592+ return err;
31593+}
31594+
31595+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31596+{
2000de60 31597+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31598+}
31599+
31600+#ifdef CONFIG_COMPAT
31601+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31602+{
2000de60 31603+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31604+}
31605+#endif
31606+
31607+/* ---------------------------------------------------------------------- */
31608+
1facf9fc 31609+void sysaufs_br_init(struct au_branch *br)
31610+{
392086de
AM
31611+ int i;
31612+ struct au_brsysfs *br_sysfs;
31613+ struct attribute *attr;
4a4d8108 31614+
392086de
AM
31615+ br_sysfs = br->br_sysfs;
31616+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31617+ attr = &br_sysfs->attr;
31618+ sysfs_attr_init(attr);
31619+ attr->name = br_sysfs->name;
cd7a4cd9 31620+ attr->mode = 0444;
392086de
AM
31621+ br_sysfs++;
31622+ }
1facf9fc 31623+}
31624+
31625+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31626+{
31627+ struct au_branch *br;
31628+ struct kobject *kobj;
392086de
AM
31629+ struct au_brsysfs *br_sysfs;
31630+ int i;
5afbbe0d 31631+ aufs_bindex_t bbot;
1facf9fc 31632+
1facf9fc 31633+ if (!sysaufs_brs)
31634+ return;
31635+
31636+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31637+ bbot = au_sbbot(sb);
31638+ for (; bindex <= bbot; bindex++) {
1facf9fc 31639+ br = au_sbr(sb, bindex);
392086de
AM
31640+ br_sysfs = br->br_sysfs;
31641+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31642+ sysfs_remove_file(kobj, &br_sysfs->attr);
31643+ br_sysfs++;
31644+ }
1facf9fc 31645+ }
31646+}
31647+
31648+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31649+{
392086de 31650+ int err, i;
5afbbe0d 31651+ aufs_bindex_t bbot;
1facf9fc 31652+ struct kobject *kobj;
31653+ struct au_branch *br;
392086de 31654+ struct au_brsysfs *br_sysfs;
1facf9fc 31655+
1facf9fc 31656+ if (!sysaufs_brs)
31657+ return;
31658+
31659+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31660+ bbot = au_sbbot(sb);
31661+ for (; bindex <= bbot; bindex++) {
1facf9fc 31662+ br = au_sbr(sb, bindex);
392086de
AM
31663+ br_sysfs = br->br_sysfs;
31664+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31665+ SysaufsBr_PREFIX "%d", bindex);
31666+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31667+ SysaufsBrid_PREFIX "%d", bindex);
31668+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31669+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31670+ if (unlikely(err))
31671+ pr_warn("failed %s under sysfs(%d)\n",
31672+ br_sysfs->name, err);
31673+ br_sysfs++;
31674+ }
1facf9fc 31675+ }
31676+}
7f207e10 31677diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
eca34b5c
AM
31678--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
31679+++ linux/fs/aufs/sysrq.c 2019-07-11 15:42:14.475571481 +0200
062440b3 31680@@ -0,0 +1,160 @@
cd7a4cd9 31681+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31682+/*
ba1aed25 31683+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 31684+ *
31685+ * This program, aufs is free software; you can redistribute it and/or modify
31686+ * it under the terms of the GNU General Public License as published by
31687+ * the Free Software Foundation; either version 2 of the License, or
31688+ * (at your option) any later version.
dece6358
AM
31689+ *
31690+ * This program is distributed in the hope that it will be useful,
31691+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31692+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31693+ * GNU General Public License for more details.
31694+ *
31695+ * You should have received a copy of the GNU General Public License
523b37e3 31696+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31697+ */
31698+
31699+/*
acd2b654 31700+ * magic sysrq handler
1facf9fc 31701+ */
31702+
1facf9fc 31703+/* #include <linux/sysrq.h> */
027c5e7a 31704+#include <linux/writeback.h>
1facf9fc 31705+#include "aufs.h"
31706+
31707+/* ---------------------------------------------------------------------- */
31708+
31709+static void sysrq_sb(struct super_block *sb)
31710+{
31711+ char *plevel;
31712+ struct au_sbinfo *sbinfo;
31713+ struct file *file;
8b6a4947
AM
31714+ struct hlist_bl_head *files;
31715+ struct hlist_bl_node *pos;
523b37e3 31716+ struct au_finfo *finfo;
1facf9fc 31717+
31718+ plevel = au_plevel;
31719+ au_plevel = KERN_WARNING;
1facf9fc 31720+
4a4d8108 31721+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31722+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31723+
31724+ sbinfo = au_sbi(sb);
4a4d8108 31725+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31726+ pr("superblock\n");
1facf9fc 31727+ au_dpri_sb(sb);
027c5e7a
AM
31728+
31729+#if 0
c06a8ce3 31730+ pr("root dentry\n");
1facf9fc 31731+ au_dpri_dentry(sb->s_root);
c06a8ce3 31732+ pr("root inode\n");
5527c038 31733+ au_dpri_inode(d_inode(sb->s_root));
027c5e7a
AM
31734+#endif
31735+
1facf9fc 31736+#if 0
027c5e7a
AM
31737+ do {
31738+ int err, i, j, ndentry;
31739+ struct au_dcsub_pages dpages;
31740+ struct au_dpage *dpage;
31741+
31742+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31743+ if (unlikely(err))
31744+ break;
31745+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31746+ if (!err)
31747+ for (i = 0; i < dpages.ndpage; i++) {
31748+ dpage = dpages.dpages + i;
31749+ ndentry = dpage->ndentry;
31750+ for (j = 0; j < ndentry; j++)
31751+ au_dpri_dentry(dpage->dentries[j]);
31752+ }
31753+ au_dpages_free(&dpages);
31754+ } while (0);
31755+#endif
31756+
31757+#if 1
31758+ {
31759+ struct inode *i;
076b876e 31760+
c06a8ce3 31761+ pr("isolated inode\n");
79b8bda9 31762+ spin_lock(&sb->s_inode_list_lock);
2cbb1c4b
JR
31763+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31764+ spin_lock(&i->i_lock);
b4510431 31765+ if (1 || hlist_empty(&i->i_dentry))
027c5e7a 31766+ au_dpri_inode(i);
2cbb1c4b
JR
31767+ spin_unlock(&i->i_lock);
31768+ }
79b8bda9 31769+ spin_unlock(&sb->s_inode_list_lock);
027c5e7a 31770+ }
1facf9fc 31771+#endif
c06a8ce3 31772+ pr("files\n");
523b37e3 31773+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31774+ hlist_bl_lock(files);
31775+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31776+ umode_t mode;
076b876e 31777+
523b37e3 31778+ file = finfo->fi_file;
c06a8ce3 31779+ mode = file_inode(file)->i_mode;
38d290e6 31780+ if (!special_file(mode))
1facf9fc 31781+ au_dpri_file(file);
523b37e3 31782+ }
8b6a4947 31783+ hlist_bl_unlock(files);
c06a8ce3 31784+ pr("done\n");
1facf9fc 31785+
c06a8ce3 31786+#undef pr
1facf9fc 31787+ au_plevel = plevel;
1facf9fc 31788+}
31789+
31790+/* ---------------------------------------------------------------------- */
31791+
31792+/* module parameter */
31793+static char *aufs_sysrq_key = "a";
cd7a4cd9 31794+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
1facf9fc 31795+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31796+
0c5527e5 31797+static void au_sysrq(int key __maybe_unused)
1facf9fc 31798+{
1facf9fc 31799+ struct au_sbinfo *sbinfo;
8b6a4947 31800+ struct hlist_bl_node *pos;
1facf9fc 31801+
027c5e7a 31802+ lockdep_off();
53392da6 31803+ au_sbilist_lock();
8b6a4947 31804+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31805+ sysrq_sb(sbinfo->si_sb);
53392da6 31806+ au_sbilist_unlock();
027c5e7a 31807+ lockdep_on();
1facf9fc 31808+}
31809+
31810+static struct sysrq_key_op au_sysrq_op = {
31811+ .handler = au_sysrq,
31812+ .help_msg = "Aufs",
31813+ .action_msg = "Aufs",
31814+ .enable_mask = SYSRQ_ENABLE_DUMP
31815+};
31816+
31817+/* ---------------------------------------------------------------------- */
31818+
31819+int __init au_sysrq_init(void)
31820+{
31821+ int err;
31822+ char key;
31823+
31824+ err = -1;
31825+ key = *aufs_sysrq_key;
31826+ if ('a' <= key && key <= 'z')
31827+ err = register_sysrq_key(key, &au_sysrq_op);
31828+ if (unlikely(err))
4a4d8108 31829+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31830+ return err;
31831+}
31832+
31833+void au_sysrq_fin(void)
31834+{
31835+ int err;
076b876e 31836+
1facf9fc 31837+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31838+ if (unlikely(err))
4a4d8108 31839+ pr_err("err %d (ignored)\n", err);
1facf9fc 31840+}
7f207e10 31841diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
eca34b5c
AM
31842--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
31843+++ linux/fs/aufs/vdir.c 2019-07-11 15:42:14.475571481 +0200
eca801bf 31844@@ -0,0 +1,896 @@
cd7a4cd9 31845+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31846+/*
ba1aed25 31847+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 31848+ *
31849+ * This program, aufs is free software; you can redistribute it and/or modify
31850+ * it under the terms of the GNU General Public License as published by
31851+ * the Free Software Foundation; either version 2 of the License, or
31852+ * (at your option) any later version.
dece6358
AM
31853+ *
31854+ * This program is distributed in the hope that it will be useful,
31855+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31856+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31857+ * GNU General Public License for more details.
31858+ *
31859+ * You should have received a copy of the GNU General Public License
523b37e3 31860+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31861+ */
31862+
31863+/*
31864+ * virtual or vertical directory
31865+ */
31866+
eca801bf 31867+#include <linux/iversion.h>
1facf9fc 31868+#include "aufs.h"
31869+
dece6358 31870+static unsigned int calc_size(int nlen)
1facf9fc 31871+{
dece6358 31872+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31873+}
31874+
31875+static int set_deblk_end(union au_vdir_deblk_p *p,
31876+ union au_vdir_deblk_p *deblk_end)
31877+{
31878+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31879+ p->de->de_str.len = 0;
31880+ /* smp_mb(); */
31881+ return 0;
31882+ }
31883+ return -1; /* error */
31884+}
31885+
31886+/* returns true or false */
31887+static int is_deblk_end(union au_vdir_deblk_p *p,
31888+ union au_vdir_deblk_p *deblk_end)
31889+{
31890+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31891+ return !p->de->de_str.len;
31892+ return 1;
31893+}
31894+
31895+static unsigned char *last_deblk(struct au_vdir *vdir)
31896+{
31897+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31898+}
31899+
31900+/* ---------------------------------------------------------------------- */
31901+
79b8bda9 31902+/* estimate the appropriate size for name hash table */
1308ab2a 31903+unsigned int au_rdhash_est(loff_t sz)
31904+{
31905+ unsigned int n;
31906+
31907+ n = UINT_MAX;
31908+ sz >>= 10;
31909+ if (sz < n)
31910+ n = sz;
31911+ if (sz < AUFS_RDHASH_DEF)
31912+ n = AUFS_RDHASH_DEF;
4a4d8108 31913+ /* pr_info("n %u\n", n); */
1308ab2a 31914+ return n;
31915+}
31916+
1facf9fc 31917+/*
31918+ * the allocated memory has to be freed by
dece6358 31919+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 31920+ */
dece6358 31921+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 31922+{
1facf9fc 31923+ struct hlist_head *head;
dece6358 31924+ unsigned int u;
076b876e 31925+ size_t sz;
1facf9fc 31926+
076b876e
AM
31927+ sz = sizeof(*nhash->nh_head) * num_hash;
31928+ head = kmalloc(sz, gfp);
dece6358
AM
31929+ if (head) {
31930+ nhash->nh_num = num_hash;
31931+ nhash->nh_head = head;
31932+ for (u = 0; u < num_hash; u++)
1facf9fc 31933+ INIT_HLIST_HEAD(head++);
dece6358 31934+ return 0; /* success */
1facf9fc 31935+ }
1facf9fc 31936+
dece6358 31937+ return -ENOMEM;
1facf9fc 31938+}
31939+
dece6358
AM
31940+static void nhash_count(struct hlist_head *head)
31941+{
31942+#if 0
31943+ unsigned long n;
31944+ struct hlist_node *pos;
31945+
31946+ n = 0;
31947+ hlist_for_each(pos, head)
31948+ n++;
4a4d8108 31949+ pr_info("%lu\n", n);
dece6358
AM
31950+#endif
31951+}
31952+
31953+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 31954+{
c06a8ce3
AM
31955+ struct au_vdir_wh *pos;
31956+ struct hlist_node *node;
1facf9fc 31957+
c06a8ce3 31958+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
9f237c51 31959+ au_kfree_rcu(pos);
1facf9fc 31960+}
31961+
dece6358 31962+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 31963+{
c06a8ce3
AM
31964+ struct au_vdir_dehstr *pos;
31965+ struct hlist_node *node;
1facf9fc 31966+
c06a8ce3 31967+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 31968+ au_cache_free_vdir_dehstr(pos);
1facf9fc 31969+}
31970+
dece6358
AM
31971+static void au_nhash_do_free(struct au_nhash *nhash,
31972+ void (*free)(struct hlist_head *head))
1facf9fc 31973+{
1308ab2a 31974+ unsigned int n;
1facf9fc 31975+ struct hlist_head *head;
1facf9fc 31976+
dece6358 31977+ n = nhash->nh_num;
1308ab2a 31978+ if (!n)
31979+ return;
31980+
dece6358 31981+ head = nhash->nh_head;
1308ab2a 31982+ while (n-- > 0) {
dece6358
AM
31983+ nhash_count(head);
31984+ free(head++);
1facf9fc 31985+ }
9f237c51 31986+ au_kfree_try_rcu(nhash->nh_head);
1facf9fc 31987+}
31988+
dece6358 31989+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 31990+{
dece6358
AM
31991+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
31992+}
1facf9fc 31993+
dece6358
AM
31994+static void au_nhash_de_free(struct au_nhash *delist)
31995+{
31996+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 31997+}
31998+
31999+/* ---------------------------------------------------------------------- */
32000+
32001+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32002+ int limit)
32003+{
32004+ int num;
32005+ unsigned int u, n;
32006+ struct hlist_head *head;
c06a8ce3 32007+ struct au_vdir_wh *pos;
1facf9fc 32008+
32009+ num = 0;
32010+ n = whlist->nh_num;
32011+ head = whlist->nh_head;
1308ab2a 32012+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
32013+ hlist_for_each_entry(pos, head, wh_hash)
32014+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 32015+ return 1;
1facf9fc 32016+ return 0;
32017+}
32018+
32019+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 32020+ unsigned char *name,
1facf9fc 32021+ unsigned int len)
32022+{
dece6358
AM
32023+ unsigned int v;
32024+ /* const unsigned int magic_bit = 12; */
32025+
1308ab2a 32026+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32027+
dece6358 32028+ v = 0;
f0c0a007
AM
32029+ if (len > 8)
32030+ len = 8;
dece6358
AM
32031+ while (len--)
32032+ v += *name++;
32033+ /* v = hash_long(v, magic_bit); */
32034+ v %= nhash->nh_num;
32035+ return nhash->nh_head + v;
32036+}
32037+
32038+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32039+ int nlen)
32040+{
32041+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 32042+}
32043+
32044+/* returns found or not */
dece6358 32045+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 32046+{
32047+ struct hlist_head *head;
c06a8ce3 32048+ struct au_vdir_wh *pos;
1facf9fc 32049+ struct au_vdir_destr *str;
32050+
dece6358 32051+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
32052+ hlist_for_each_entry(pos, head, wh_hash) {
32053+ str = &pos->wh_str;
1facf9fc 32054+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
32055+ if (au_nhash_test_name(str, name, nlen))
32056+ return 1;
32057+ }
32058+ return 0;
32059+}
32060+
32061+/* returns found(true) or not */
32062+static int test_known(struct au_nhash *delist, char *name, int nlen)
32063+{
32064+ struct hlist_head *head;
c06a8ce3 32065+ struct au_vdir_dehstr *pos;
dece6358
AM
32066+ struct au_vdir_destr *str;
32067+
32068+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
32069+ hlist_for_each_entry(pos, head, hash) {
32070+ str = pos->str;
dece6358
AM
32071+ AuDbg("%.*s\n", str->len, str->name);
32072+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 32073+ return 1;
32074+ }
32075+ return 0;
32076+}
32077+
dece6358
AM
32078+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32079+ unsigned char d_type)
32080+{
32081+#ifdef CONFIG_AUFS_SHWH
32082+ wh->wh_ino = ino;
32083+ wh->wh_type = d_type;
32084+#endif
32085+}
32086+
32087+/* ---------------------------------------------------------------------- */
32088+
32089+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32090+ unsigned int d_type, aufs_bindex_t bindex,
32091+ unsigned char shwh)
1facf9fc 32092+{
32093+ int err;
32094+ struct au_vdir_destr *str;
32095+ struct au_vdir_wh *wh;
32096+
dece6358 32097+ AuDbg("%.*s\n", nlen, name);
1308ab2a 32098+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32099+
1facf9fc 32100+ err = -ENOMEM;
dece6358 32101+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 32102+ if (unlikely(!wh))
32103+ goto out;
32104+
32105+ err = 0;
32106+ wh->wh_bindex = bindex;
dece6358
AM
32107+ if (shwh)
32108+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 32109+ str = &wh->wh_str;
dece6358
AM
32110+ str->len = nlen;
32111+ memcpy(str->name, name, nlen);
32112+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 32113+ /* smp_mb(); */
32114+
4f0767ce 32115+out:
1facf9fc 32116+ return err;
32117+}
32118+
1facf9fc 32119+static int append_deblk(struct au_vdir *vdir)
32120+{
32121+ int err;
dece6358 32122+ unsigned long ul;
1facf9fc 32123+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32124+ union au_vdir_deblk_p p, deblk_end;
32125+ unsigned char **o;
32126+
32127+ err = -ENOMEM;
e2f27e51
AM
32128+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32129+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32130+ if (unlikely(!o))
32131+ goto out;
32132+
32133+ vdir->vd_deblk = o;
32134+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32135+ if (p.deblk) {
32136+ ul = vdir->vd_nblk++;
32137+ vdir->vd_deblk[ul] = p.deblk;
32138+ vdir->vd_last.ul = ul;
32139+ vdir->vd_last.p.deblk = p.deblk;
32140+ deblk_end.deblk = p.deblk + deblk_sz;
32141+ err = set_deblk_end(&p, &deblk_end);
32142+ }
32143+
4f0767ce 32144+out:
1facf9fc 32145+ return err;
32146+}
32147+
dece6358
AM
32148+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32149+ unsigned int d_type, struct au_nhash *delist)
32150+{
32151+ int err;
32152+ unsigned int sz;
32153+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32154+ union au_vdir_deblk_p p, *room, deblk_end;
32155+ struct au_vdir_dehstr *dehstr;
32156+
32157+ p.deblk = last_deblk(vdir);
32158+ deblk_end.deblk = p.deblk + deblk_sz;
32159+ room = &vdir->vd_last.p;
32160+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32161+ || !is_deblk_end(room, &deblk_end));
32162+
32163+ sz = calc_size(nlen);
32164+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32165+ err = append_deblk(vdir);
32166+ if (unlikely(err))
32167+ goto out;
32168+
32169+ p.deblk = last_deblk(vdir);
32170+ deblk_end.deblk = p.deblk + deblk_sz;
32171+ /* smp_mb(); */
32172+ AuDebugOn(room->deblk != p.deblk);
32173+ }
32174+
32175+ err = -ENOMEM;
4a4d8108 32176+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
32177+ if (unlikely(!dehstr))
32178+ goto out;
32179+
32180+ dehstr->str = &room->de->de_str;
32181+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32182+ room->de->de_ino = ino;
32183+ room->de->de_type = d_type;
32184+ room->de->de_str.len = nlen;
32185+ memcpy(room->de->de_str.name, name, nlen);
32186+
32187+ err = 0;
32188+ room->deblk += sz;
32189+ if (unlikely(set_deblk_end(room, &deblk_end)))
32190+ err = append_deblk(vdir);
32191+ /* smp_mb(); */
32192+
4f0767ce 32193+out:
dece6358
AM
32194+ return err;
32195+}
32196+
32197+/* ---------------------------------------------------------------------- */
32198+
1c60b727 32199+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
32200+{
32201+ unsigned char **deblk;
32202+
32203+ deblk = vdir->vd_deblk;
1c60b727 32204+ while (vdir->vd_nblk--)
9f237c51
AM
32205+ au_kfree_try_rcu(*deblk++);
32206+ au_kfree_try_rcu(vdir->vd_deblk);
1c60b727 32207+ au_cache_free_vdir(vdir);
dece6358
AM
32208+}
32209+
1308ab2a 32210+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 32211+{
32212+ struct au_vdir *vdir;
1308ab2a 32213+ struct super_block *sb;
1facf9fc 32214+ int err;
32215+
2000de60 32216+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32217+ SiMustAnyLock(sb);
32218+
1facf9fc 32219+ err = -ENOMEM;
32220+ vdir = au_cache_alloc_vdir();
32221+ if (unlikely(!vdir))
32222+ goto out;
32223+
32224+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32225+ if (unlikely(!vdir->vd_deblk))
32226+ goto out_free;
32227+
32228+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 32229+ if (!vdir->vd_deblk_sz) {
79b8bda9 32230+ /* estimate the appropriate size for deblk */
1308ab2a 32231+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 32232+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 32233+ }
1facf9fc 32234+ vdir->vd_nblk = 0;
32235+ vdir->vd_version = 0;
32236+ vdir->vd_jiffy = 0;
32237+ err = append_deblk(vdir);
32238+ if (!err)
32239+ return vdir; /* success */
32240+
9f237c51 32241+ au_kfree_try_rcu(vdir->vd_deblk);
1facf9fc 32242+
4f0767ce 32243+out_free:
1c60b727 32244+ au_cache_free_vdir(vdir);
4f0767ce 32245+out:
1facf9fc 32246+ vdir = ERR_PTR(err);
32247+ return vdir;
32248+}
32249+
32250+static int reinit_vdir(struct au_vdir *vdir)
32251+{
32252+ int err;
32253+ union au_vdir_deblk_p p, deblk_end;
32254+
32255+ while (vdir->vd_nblk > 1) {
9f237c51 32256+ au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 32257+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32258+ vdir->vd_nblk--;
32259+ }
32260+ p.deblk = vdir->vd_deblk[0];
32261+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32262+ err = set_deblk_end(&p, &deblk_end);
32263+ /* keep vd_dblk_sz */
32264+ vdir->vd_last.ul = 0;
32265+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32266+ vdir->vd_version = 0;
32267+ vdir->vd_jiffy = 0;
32268+ /* smp_mb(); */
32269+ return err;
32270+}
32271+
32272+/* ---------------------------------------------------------------------- */
32273+
1facf9fc 32274+#define AuFillVdir_CALLED 1
32275+#define AuFillVdir_WHABLE (1 << 1)
dece6358 32276+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 32277+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
32278+#define au_fset_fillvdir(flags, name) \
32279+ do { (flags) |= AuFillVdir_##name; } while (0)
32280+#define au_fclr_fillvdir(flags, name) \
32281+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 32282+
dece6358
AM
32283+#ifndef CONFIG_AUFS_SHWH
32284+#undef AuFillVdir_SHWH
32285+#define AuFillVdir_SHWH 0
32286+#endif
32287+
1facf9fc 32288+struct fillvdir_arg {
392086de 32289+ struct dir_context ctx;
1facf9fc 32290+ struct file *file;
32291+ struct au_vdir *vdir;
dece6358
AM
32292+ struct au_nhash delist;
32293+ struct au_nhash whlist;
1facf9fc 32294+ aufs_bindex_t bindex;
32295+ unsigned int flags;
32296+ int err;
32297+};
32298+
392086de 32299+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 32300+ loff_t offset __maybe_unused, u64 h_ino,
32301+ unsigned int d_type)
32302+{
392086de 32303+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 32304+ char *name = (void *)__name;
32305+ struct super_block *sb;
1facf9fc 32306+ ino_t ino;
dece6358 32307+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 32308+
1facf9fc 32309+ arg->err = 0;
2000de60 32310+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 32311+ au_fset_fillvdir(arg->flags, CALLED);
32312+ /* smp_mb(); */
dece6358 32313+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 32314+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
32315+ if (test_known(&arg->delist, name, nlen)
32316+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32317+ goto out; /* already exists or whiteouted */
1facf9fc 32318+
dece6358 32319+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
32320+ if (!arg->err) {
32321+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32322+ d_type = DT_UNKNOWN;
dece6358
AM
32323+ arg->err = append_de(arg->vdir, name, nlen, ino,
32324+ d_type, &arg->delist);
4a4d8108 32325+ }
1facf9fc 32326+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32327+ name += AUFS_WH_PFX_LEN;
dece6358
AM
32328+ nlen -= AUFS_WH_PFX_LEN;
32329+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32330+ goto out; /* already whiteouted */
1facf9fc 32331+
acd2b654 32332+ ino = 0; /* just to suppress a warning */
dece6358
AM
32333+ if (shwh)
32334+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32335+ &ino);
4a4d8108
AM
32336+ if (!arg->err) {
32337+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32338+ d_type = DT_UNKNOWN;
1facf9fc 32339+ arg->err = au_nhash_append_wh
dece6358
AM
32340+ (&arg->whlist, name, nlen, ino, d_type,
32341+ arg->bindex, shwh);
4a4d8108 32342+ }
1facf9fc 32343+ }
32344+
4f0767ce 32345+out:
1facf9fc 32346+ if (!arg->err)
32347+ arg->vdir->vd_jiffy = jiffies;
32348+ /* smp_mb(); */
32349+ AuTraceErr(arg->err);
32350+ return arg->err;
32351+}
32352+
dece6358
AM
32353+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32354+ struct au_nhash *whlist, struct au_nhash *delist)
32355+{
32356+#ifdef CONFIG_AUFS_SHWH
32357+ int err;
32358+ unsigned int nh, u;
32359+ struct hlist_head *head;
c06a8ce3
AM
32360+ struct au_vdir_wh *pos;
32361+ struct hlist_node *n;
dece6358
AM
32362+ char *p, *o;
32363+ struct au_vdir_destr *destr;
32364+
32365+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32366+
32367+ err = -ENOMEM;
537831f9 32368+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32369+ if (unlikely(!p))
32370+ goto out;
32371+
32372+ err = 0;
32373+ nh = whlist->nh_num;
32374+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32375+ p += AUFS_WH_PFX_LEN;
32376+ for (u = 0; u < nh; u++) {
32377+ head = whlist->nh_head + u;
c06a8ce3
AM
32378+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32379+ destr = &pos->wh_str;
dece6358
AM
32380+ memcpy(p, destr->name, destr->len);
32381+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32382+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32383+ if (unlikely(err))
32384+ break;
32385+ }
32386+ }
32387+
1c60b727 32388+ free_page((unsigned long)o);
dece6358 32389+
4f0767ce 32390+out:
dece6358
AM
32391+ AuTraceErr(err);
32392+ return err;
32393+#else
32394+ return 0;
32395+#endif
32396+}
32397+
1facf9fc 32398+static int au_do_read_vdir(struct fillvdir_arg *arg)
32399+{
32400+ int err;
dece6358 32401+ unsigned int rdhash;
1facf9fc 32402+ loff_t offset;
5afbbe0d 32403+ aufs_bindex_t bbot, bindex, btop;
dece6358 32404+ unsigned char shwh;
1facf9fc 32405+ struct file *hf, *file;
32406+ struct super_block *sb;
32407+
1facf9fc 32408+ file = arg->file;
2000de60 32409+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32410+ SiMustAnyLock(sb);
32411+
32412+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32413+ if (!rdhash)
32414+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32415+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32416+ if (unlikely(err))
1facf9fc 32417+ goto out;
dece6358
AM
32418+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32419+ if (unlikely(err))
1facf9fc 32420+ goto out_delist;
32421+
32422+ err = 0;
32423+ arg->flags = 0;
dece6358
AM
32424+ shwh = 0;
32425+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32426+ shwh = 1;
32427+ au_fset_fillvdir(arg->flags, SHWH);
32428+ }
5afbbe0d
AM
32429+ btop = au_fbtop(file);
32430+ bbot = au_fbbot_dir(file);
32431+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32432+ hf = au_hf_dir(file, bindex);
1facf9fc 32433+ if (!hf)
32434+ continue;
32435+
32436+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32437+ err = offset;
32438+ if (unlikely(offset))
32439+ break;
32440+
32441+ arg->bindex = bindex;
32442+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32443+ if (shwh
5afbbe0d 32444+ || (bindex != bbot
dece6358 32445+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32446+ au_fset_fillvdir(arg->flags, WHABLE);
32447+ do {
32448+ arg->err = 0;
32449+ au_fclr_fillvdir(arg->flags, CALLED);
32450+ /* smp_mb(); */
392086de 32451+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32452+ if (err >= 0)
32453+ err = arg->err;
32454+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32455+
32456+ /*
32457+ * dir_relax() may be good for concurrency, but aufs should not
32458+ * use it since it will cause a lockdep problem.
32459+ */
1facf9fc 32460+ }
dece6358
AM
32461+
32462+ if (!err && shwh)
32463+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32464+
32465+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32466+
4f0767ce 32467+out_delist:
dece6358 32468+ au_nhash_de_free(&arg->delist);
4f0767ce 32469+out:
1facf9fc 32470+ return err;
32471+}
32472+
32473+static int read_vdir(struct file *file, int may_read)
32474+{
32475+ int err;
32476+ unsigned long expire;
32477+ unsigned char do_read;
392086de
AM
32478+ struct fillvdir_arg arg = {
32479+ .ctx = {
2000de60 32480+ .actor = fillvdir
392086de
AM
32481+ }
32482+ };
1facf9fc 32483+ struct inode *inode;
32484+ struct au_vdir *vdir, *allocated;
32485+
32486+ err = 0;
c06a8ce3 32487+ inode = file_inode(file);
1facf9fc 32488+ IMustLock(inode);
5afbbe0d 32489+ IiMustWriteLock(inode);
dece6358
AM
32490+ SiMustAnyLock(inode->i_sb);
32491+
1facf9fc 32492+ allocated = NULL;
32493+ do_read = 0;
32494+ expire = au_sbi(inode->i_sb)->si_rdcache;
32495+ vdir = au_ivdir(inode);
32496+ if (!vdir) {
32497+ do_read = 1;
1308ab2a 32498+ vdir = alloc_vdir(file);
1facf9fc 32499+ err = PTR_ERR(vdir);
32500+ if (IS_ERR(vdir))
32501+ goto out;
32502+ err = 0;
32503+ allocated = vdir;
32504+ } else if (may_read
be118d29 32505+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32506+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32507+ do_read = 1;
32508+ err = reinit_vdir(vdir);
32509+ if (unlikely(err))
32510+ goto out;
32511+ }
32512+
32513+ if (!do_read)
32514+ return 0; /* success */
32515+
32516+ arg.file = file;
32517+ arg.vdir = vdir;
32518+ err = au_do_read_vdir(&arg);
32519+ if (!err) {
392086de 32520+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32521+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32522+ vdir->vd_last.ul = 0;
32523+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32524+ if (allocated)
32525+ au_set_ivdir(inode, allocated);
32526+ } else if (allocated)
1c60b727 32527+ au_vdir_free(allocated);
1facf9fc 32528+
4f0767ce 32529+out:
1facf9fc 32530+ return err;
32531+}
32532+
32533+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32534+{
32535+ int err, rerr;
32536+ unsigned long ul, n;
32537+ const unsigned int deblk_sz = src->vd_deblk_sz;
32538+
32539+ AuDebugOn(tgt->vd_nblk != 1);
32540+
32541+ err = -ENOMEM;
32542+ if (tgt->vd_nblk < src->vd_nblk) {
32543+ unsigned char **p;
32544+
e2f27e51
AM
32545+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32546+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32547+ if (unlikely(!p))
32548+ goto out;
32549+ tgt->vd_deblk = p;
32550+ }
32551+
1308ab2a 32552+ if (tgt->vd_deblk_sz != deblk_sz) {
32553+ unsigned char *p;
32554+
32555+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32556+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32557+ /*may_shrink*/1);
1308ab2a 32558+ if (unlikely(!p))
32559+ goto out;
32560+ tgt->vd_deblk[0] = p;
32561+ }
1facf9fc 32562+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32563+ tgt->vd_version = src->vd_version;
32564+ tgt->vd_jiffy = src->vd_jiffy;
32565+
32566+ n = src->vd_nblk;
32567+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32568+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32569+ GFP_NOFS);
32570+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32571+ goto out;
1308ab2a 32572+ tgt->vd_nblk++;
1facf9fc 32573+ }
1308ab2a 32574+ tgt->vd_nblk = n;
32575+ tgt->vd_last.ul = tgt->vd_last.ul;
32576+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32577+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32578+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32579+ /* smp_mb(); */
32580+ return 0; /* success */
32581+
4f0767ce 32582+out:
1facf9fc 32583+ rerr = reinit_vdir(tgt);
32584+ BUG_ON(rerr);
32585+ return err;
32586+}
32587+
32588+int au_vdir_init(struct file *file)
32589+{
32590+ int err;
32591+ struct inode *inode;
32592+ struct au_vdir *vdir_cache, *allocated;
32593+
392086de 32594+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32595+ err = read_vdir(file, !file->f_pos);
32596+ if (unlikely(err))
32597+ goto out;
32598+
32599+ allocated = NULL;
32600+ vdir_cache = au_fvdir_cache(file);
32601+ if (!vdir_cache) {
1308ab2a 32602+ vdir_cache = alloc_vdir(file);
1facf9fc 32603+ err = PTR_ERR(vdir_cache);
32604+ if (IS_ERR(vdir_cache))
32605+ goto out;
32606+ allocated = vdir_cache;
32607+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32608+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32609+ err = reinit_vdir(vdir_cache);
32610+ if (unlikely(err))
32611+ goto out;
32612+ } else
32613+ return 0; /* success */
32614+
c06a8ce3 32615+ inode = file_inode(file);
1facf9fc 32616+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32617+ if (!err) {
be118d29 32618+ file->f_version = inode_query_iversion(inode);
1facf9fc 32619+ if (allocated)
32620+ au_set_fvdir_cache(file, allocated);
32621+ } else if (allocated)
1c60b727 32622+ au_vdir_free(allocated);
1facf9fc 32623+
4f0767ce 32624+out:
1facf9fc 32625+ return err;
32626+}
32627+
32628+static loff_t calc_offset(struct au_vdir *vdir)
32629+{
32630+ loff_t offset;
32631+ union au_vdir_deblk_p p;
32632+
32633+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32634+ offset = vdir->vd_last.p.deblk - p.deblk;
32635+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32636+ return offset;
32637+}
32638+
32639+/* returns true or false */
392086de 32640+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32641+{
32642+ int valid;
32643+ unsigned int deblk_sz;
32644+ unsigned long ul, n;
32645+ loff_t offset;
32646+ union au_vdir_deblk_p p, deblk_end;
32647+ struct au_vdir *vdir_cache;
32648+
32649+ valid = 1;
32650+ vdir_cache = au_fvdir_cache(file);
32651+ offset = calc_offset(vdir_cache);
32652+ AuDbg("offset %lld\n", offset);
392086de 32653+ if (ctx->pos == offset)
1facf9fc 32654+ goto out;
32655+
32656+ vdir_cache->vd_last.ul = 0;
32657+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32658+ if (!ctx->pos)
1facf9fc 32659+ goto out;
32660+
32661+ valid = 0;
32662+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32663+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32664+ AuDbg("ul %lu\n", ul);
32665+ if (ul >= vdir_cache->vd_nblk)
32666+ goto out;
32667+
32668+ n = vdir_cache->vd_nblk;
32669+ for (; ul < n; ul++) {
32670+ p.deblk = vdir_cache->vd_deblk[ul];
32671+ deblk_end.deblk = p.deblk + deblk_sz;
32672+ offset = ul;
32673+ offset *= deblk_sz;
392086de 32674+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32675+ unsigned int l;
32676+
32677+ l = calc_size(p.de->de_str.len);
32678+ offset += l;
32679+ p.deblk += l;
32680+ }
32681+ if (!is_deblk_end(&p, &deblk_end)) {
32682+ valid = 1;
32683+ vdir_cache->vd_last.ul = ul;
32684+ vdir_cache->vd_last.p = p;
32685+ break;
32686+ }
32687+ }
32688+
4f0767ce 32689+out:
1facf9fc 32690+ /* smp_mb(); */
b00004a5
AM
32691+ if (!valid)
32692+ AuDbg("valid %d\n", !valid);
1facf9fc 32693+ return valid;
32694+}
32695+
392086de 32696+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32697+{
1facf9fc 32698+ unsigned int l, deblk_sz;
32699+ union au_vdir_deblk_p deblk_end;
32700+ struct au_vdir *vdir_cache;
32701+ struct au_vdir_de *de;
32702+
392086de 32703+ if (!seek_vdir(file, ctx))
1facf9fc 32704+ return 0;
32705+
acd2b654 32706+ vdir_cache = au_fvdir_cache(file);
1facf9fc 32707+ deblk_sz = vdir_cache->vd_deblk_sz;
32708+ while (1) {
32709+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32710+ deblk_end.deblk += deblk_sz;
32711+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32712+ de = vdir_cache->vd_last.p.de;
32713+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32714+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32715+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32716+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32717+ de->de_str.len, de->de_ino,
32718+ de->de_type))) {
1facf9fc 32719+ /* todo: ignore the error caused by udba? */
32720+ /* return err; */
32721+ return 0;
32722+ }
32723+
32724+ l = calc_size(de->de_str.len);
32725+ vdir_cache->vd_last.p.deblk += l;
392086de 32726+ ctx->pos += l;
1facf9fc 32727+ }
32728+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32729+ vdir_cache->vd_last.ul++;
32730+ vdir_cache->vd_last.p.deblk
32731+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32732+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32733+ continue;
32734+ }
32735+ break;
32736+ }
32737+
32738+ /* smp_mb(); */
32739+ return 0;
32740+}
7f207e10 32741diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
eca34b5c
AM
32742--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
32743+++ linux/fs/aufs/vfsub.c 2019-07-11 15:42:14.475571481 +0200
acd2b654 32744@@ -0,0 +1,902 @@
cd7a4cd9 32745+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32746+/*
ba1aed25 32747+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 32748+ *
32749+ * This program, aufs is free software; you can redistribute it and/or modify
32750+ * it under the terms of the GNU General Public License as published by
32751+ * the Free Software Foundation; either version 2 of the License, or
32752+ * (at your option) any later version.
dece6358
AM
32753+ *
32754+ * This program is distributed in the hope that it will be useful,
32755+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32756+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32757+ * GNU General Public License for more details.
32758+ *
32759+ * You should have received a copy of the GNU General Public License
523b37e3 32760+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32761+ */
32762+
32763+/*
32764+ * sub-routines for VFS
32765+ */
32766+
8b6a4947 32767+#include <linux/mnt_namespace.h>
dece6358 32768+#include <linux/namei.h>
8cdd5066 32769+#include <linux/nsproxy.h>
dece6358
AM
32770+#include <linux/security.h>
32771+#include <linux/splice.h>
1facf9fc 32772+#include "aufs.h"
32773+
8cdd5066
JR
32774+#ifdef CONFIG_AUFS_BR_FUSE
32775+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32776+{
8cdd5066
JR
32777+ if (!au_test_fuse(h_sb) || !au_userns)
32778+ return 0;
32779+
8b6a4947 32780+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32781+}
32782+#endif
32783+
a2654f78
AM
32784+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32785+{
32786+ int err;
32787+
32788+ lockdep_off();
32789+ down_read(&h_sb->s_umount);
32790+ err = __sync_filesystem(h_sb, wait);
32791+ up_read(&h_sb->s_umount);
32792+ lockdep_on();
32793+
32794+ return err;
32795+}
32796+
8cdd5066
JR
32797+/* ---------------------------------------------------------------------- */
32798+
1facf9fc 32799+int vfsub_update_h_iattr(struct path *h_path, int *did)
32800+{
32801+ int err;
32802+ struct kstat st;
32803+ struct super_block *h_sb;
32804+
32805+ /* for remote fs, leave work for its getattr or d_revalidate */
32806+ /* for bad i_attr fs, handle them in aufs_getattr() */
32807+ /* still some fs may acquire i_mutex. we need to skip them */
32808+ err = 0;
32809+ if (!did)
32810+ did = &err;
32811+ h_sb = h_path->dentry->d_sb;
32812+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32813+ if (*did)
521ced18 32814+ err = vfsub_getattr(h_path, &st);
1facf9fc 32815+
32816+ return err;
32817+}
32818+
32819+/* ---------------------------------------------------------------------- */
32820+
4a4d8108 32821+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32822+{
32823+ struct file *file;
32824+
b4510431 32825+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32826+ current_cred());
2cbb1c4b
JR
32827+ if (!IS_ERR_OR_NULL(file)
32828+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 32829+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 32830+
1308ab2a 32831+ return file;
32832+}
32833+
1facf9fc 32834+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32835+{
32836+ struct file *file;
32837+
2cbb1c4b 32838+ lockdep_off();
7f207e10 32839+ file = filp_open(path,
2cbb1c4b 32840+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32841+ mode);
2cbb1c4b 32842+ lockdep_on();
1facf9fc 32843+ if (IS_ERR(file))
32844+ goto out;
32845+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32846+
4f0767ce 32847+out:
1facf9fc 32848+ return file;
32849+}
32850+
b912730e
AM
32851+/*
32852+ * Ideally this function should call VFS:do_last() in order to keep all its
32853+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32854+ * structure such as nameidata. This is a second (or third) best approach.
32855+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32856+ */
32857+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 32858+ struct vfsub_aopen_args *args)
b912730e
AM
32859+{
32860+ int err;
acd2b654 32861+ struct au_branch *br = args->br;
b912730e
AM
32862+ struct file *file = args->file;
32863+ /* copied from linux/fs/namei.c:atomic_open() */
32864+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32865+
32866+ IMustLock(dir);
32867+ AuDebugOn(!dir->i_op->atomic_open);
32868+
32869+ err = au_br_test_oflag(args->open_flag, br);
32870+ if (unlikely(err))
32871+ goto out;
32872+
acd2b654
AM
32873+ au_lcnt_inc(&br->br_nfiles);
32874+ file->f_path.dentry = DENTRY_NOT_SET;
32875+ file->f_path.mnt = au_br_mnt(br);
32876+ AuDbg("%ps\n", dir->i_op->atomic_open);
b912730e 32877+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
acd2b654
AM
32878+ args->create_mode);
32879+ if (unlikely(err < 0)) {
32880+ au_lcnt_dec(&br->br_nfiles);
b912730e 32881+ goto out;
acd2b654 32882+ }
b912730e 32883+
acd2b654
AM
32884+ /* temporary workaround for nfsv4 branch */
32885+ if (au_test_nfs(dir->i_sb))
32886+ nfs_mark_for_revalidate(dir);
b912730e 32887+
acd2b654
AM
32888+ if (file->f_mode & FMODE_CREATED)
32889+ fsnotify_create(dir, dentry);
32890+ if (!(file->f_mode & FMODE_OPENED)) {
32891+ au_lcnt_dec(&br->br_nfiles);
32892+ goto out;
b912730e
AM
32893+ }
32894+
acd2b654
AM
32895+ /* todo: call VFS:may_open() here */
32896+ /* todo: ima_file_check() too? */
32897+ if (!err && (args->open_flag & __FMODE_EXEC))
32898+ err = deny_write_access(file);
32899+ if (!err)
32900+ fsnotify_open(file);
32901+ else
32902+ au_lcnt_dec(&br->br_nfiles);
32903+ /* note that the file is created and still opened */
b912730e
AM
32904+
32905+out:
32906+ return err;
32907+}
32908+
1facf9fc 32909+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32910+{
32911+ int err;
32912+
1facf9fc 32913+ err = kern_path(name, flags, path);
5527c038 32914+ if (!err && d_is_positive(path->dentry))
1facf9fc 32915+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32916+ return err;
32917+}
32918+
febd17d6
JR
32919+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32920+ struct dentry *parent, int len)
32921+{
32922+ struct path path = {
32923+ .mnt = NULL
32924+ };
32925+
32926+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32927+ if (IS_ERR(path.dentry))
32928+ goto out;
32929+ if (d_is_positive(path.dentry))
32930+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32931+
32932+out:
32933+ AuTraceErrPtr(path.dentry);
32934+ return path.dentry;
32935+}
32936+
1facf9fc 32937+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32938+ int len)
32939+{
32940+ struct path path = {
32941+ .mnt = NULL
32942+ };
32943+
1308ab2a 32944+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 32945+ IMustLock(d_inode(parent));
1facf9fc 32946+
32947+ path.dentry = lookup_one_len(name, parent, len);
32948+ if (IS_ERR(path.dentry))
32949+ goto out;
5527c038 32950+ if (d_is_positive(path.dentry))
1facf9fc 32951+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32952+
4f0767ce 32953+out:
4a4d8108 32954+ AuTraceErrPtr(path.dentry);
1facf9fc 32955+ return path.dentry;
32956+}
32957+
b4510431 32958+void vfsub_call_lkup_one(void *args)
2cbb1c4b 32959+{
b4510431
AM
32960+ struct vfsub_lkup_one_args *a = args;
32961+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
32962+}
32963+
1facf9fc 32964+/* ---------------------------------------------------------------------- */
32965+
32966+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32967+ struct dentry *d2, struct au_hinode *hdir2)
32968+{
32969+ struct dentry *d;
32970+
2cbb1c4b 32971+ lockdep_off();
1facf9fc 32972+ d = lock_rename(d1, d2);
2cbb1c4b 32973+ lockdep_on();
4a4d8108 32974+ au_hn_suspend(hdir1);
1facf9fc 32975+ if (hdir1 != hdir2)
4a4d8108 32976+ au_hn_suspend(hdir2);
1facf9fc 32977+
32978+ return d;
32979+}
32980+
32981+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32982+ struct dentry *d2, struct au_hinode *hdir2)
32983+{
4a4d8108 32984+ au_hn_resume(hdir1);
1facf9fc 32985+ if (hdir1 != hdir2)
4a4d8108 32986+ au_hn_resume(hdir2);
2cbb1c4b 32987+ lockdep_off();
1facf9fc 32988+ unlock_rename(d1, d2);
2cbb1c4b 32989+ lockdep_on();
1facf9fc 32990+}
32991+
32992+/* ---------------------------------------------------------------------- */
32993+
b4510431 32994+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 32995+{
32996+ int err;
32997+ struct dentry *d;
32998+
32999+ IMustLock(dir);
33000+
33001+ d = path->dentry;
33002+ path->dentry = d->d_parent;
b752ccd1 33003+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 33004+ path->dentry = d;
33005+ if (unlikely(err))
33006+ goto out;
33007+
c1595e42 33008+ lockdep_off();
b4510431 33009+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 33010+ lockdep_on();
1facf9fc 33011+ if (!err) {
33012+ struct path tmp = *path;
33013+ int did;
33014+
33015+ vfsub_update_h_iattr(&tmp, &did);
33016+ if (did) {
33017+ tmp.dentry = path->dentry->d_parent;
33018+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33019+ }
33020+ /*ignore*/
33021+ }
33022+
4f0767ce 33023+out:
1facf9fc 33024+ return err;
33025+}
33026+
33027+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33028+{
33029+ int err;
33030+ struct dentry *d;
33031+
33032+ IMustLock(dir);
33033+
33034+ d = path->dentry;
33035+ path->dentry = d->d_parent;
b752ccd1 33036+ err = security_path_symlink(path, d, symname);
1facf9fc 33037+ path->dentry = d;
33038+ if (unlikely(err))
33039+ goto out;
33040+
c1595e42 33041+ lockdep_off();
1facf9fc 33042+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 33043+ lockdep_on();
1facf9fc 33044+ if (!err) {
33045+ struct path tmp = *path;
33046+ int did;
33047+
33048+ vfsub_update_h_iattr(&tmp, &did);
33049+ if (did) {
33050+ tmp.dentry = path->dentry->d_parent;
33051+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33052+ }
33053+ /*ignore*/
33054+ }
33055+
4f0767ce 33056+out:
1facf9fc 33057+ return err;
33058+}
33059+
33060+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33061+{
33062+ int err;
33063+ struct dentry *d;
33064+
33065+ IMustLock(dir);
33066+
33067+ d = path->dentry;
33068+ path->dentry = d->d_parent;
027c5e7a 33069+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 33070+ path->dentry = d;
33071+ if (unlikely(err))
33072+ goto out;
33073+
c1595e42 33074+ lockdep_off();
1facf9fc 33075+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 33076+ lockdep_on();
1facf9fc 33077+ if (!err) {
33078+ struct path tmp = *path;
33079+ int did;
33080+
33081+ vfsub_update_h_iattr(&tmp, &did);
33082+ if (did) {
33083+ tmp.dentry = path->dentry->d_parent;
33084+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33085+ }
33086+ /*ignore*/
33087+ }
33088+
4f0767ce 33089+out:
1facf9fc 33090+ return err;
33091+}
33092+
33093+static int au_test_nlink(struct inode *inode)
33094+{
33095+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33096+
33097+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
33098+ || inode->i_nlink < link_max)
33099+ return 0;
33100+ return -EMLINK;
33101+}
33102+
523b37e3
AM
33103+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33104+ struct inode **delegated_inode)
1facf9fc 33105+{
33106+ int err;
33107+ struct dentry *d;
33108+
33109+ IMustLock(dir);
33110+
5527c038 33111+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 33112+ if (unlikely(err))
33113+ return err;
33114+
b4510431 33115+ /* we don't call may_linkat() */
1facf9fc 33116+ d = path->dentry;
33117+ path->dentry = d->d_parent;
b752ccd1 33118+ err = security_path_link(src_dentry, path, d);
1facf9fc 33119+ path->dentry = d;
33120+ if (unlikely(err))
33121+ goto out;
33122+
2cbb1c4b 33123+ lockdep_off();
523b37e3 33124+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 33125+ lockdep_on();
1facf9fc 33126+ if (!err) {
33127+ struct path tmp = *path;
33128+ int did;
33129+
33130+ /* fuse has different memory inode for the same inumber */
33131+ vfsub_update_h_iattr(&tmp, &did);
33132+ if (did) {
33133+ tmp.dentry = path->dentry->d_parent;
33134+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33135+ tmp.dentry = src_dentry;
33136+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33137+ }
33138+ /*ignore*/
33139+ }
33140+
4f0767ce 33141+out:
1facf9fc 33142+ return err;
33143+}
33144+
33145+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 33146+ struct inode *dir, struct path *path,
f2c43d5f 33147+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 33148+{
33149+ int err;
33150+ struct path tmp = {
33151+ .mnt = path->mnt
33152+ };
33153+ struct dentry *d;
33154+
33155+ IMustLock(dir);
33156+ IMustLock(src_dir);
33157+
33158+ d = path->dentry;
33159+ path->dentry = d->d_parent;
33160+ tmp.dentry = src_dentry->d_parent;
38d290e6 33161+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 33162+ path->dentry = d;
33163+ if (unlikely(err))
33164+ goto out;
33165+
2cbb1c4b 33166+ lockdep_off();
523b37e3 33167+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
f2c43d5f 33168+ delegated_inode, flags);
2cbb1c4b 33169+ lockdep_on();
1facf9fc 33170+ if (!err) {
33171+ int did;
33172+
33173+ tmp.dentry = d->d_parent;
33174+ vfsub_update_h_iattr(&tmp, &did);
33175+ if (did) {
33176+ tmp.dentry = src_dentry;
33177+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33178+ tmp.dentry = src_dentry->d_parent;
33179+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33180+ }
33181+ /*ignore*/
33182+ }
33183+
4f0767ce 33184+out:
1facf9fc 33185+ return err;
33186+}
33187+
33188+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33189+{
33190+ int err;
33191+ struct dentry *d;
33192+
33193+ IMustLock(dir);
33194+
33195+ d = path->dentry;
33196+ path->dentry = d->d_parent;
b752ccd1 33197+ err = security_path_mkdir(path, d, mode);
1facf9fc 33198+ path->dentry = d;
33199+ if (unlikely(err))
33200+ goto out;
33201+
c1595e42 33202+ lockdep_off();
1facf9fc 33203+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 33204+ lockdep_on();
1facf9fc 33205+ if (!err) {
33206+ struct path tmp = *path;
33207+ int did;
33208+
33209+ vfsub_update_h_iattr(&tmp, &did);
33210+ if (did) {
33211+ tmp.dentry = path->dentry->d_parent;
33212+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33213+ }
33214+ /*ignore*/
33215+ }
33216+
4f0767ce 33217+out:
1facf9fc 33218+ return err;
33219+}
33220+
33221+int vfsub_rmdir(struct inode *dir, struct path *path)
33222+{
33223+ int err;
33224+ struct dentry *d;
33225+
33226+ IMustLock(dir);
33227+
33228+ d = path->dentry;
33229+ path->dentry = d->d_parent;
b752ccd1 33230+ err = security_path_rmdir(path, d);
1facf9fc 33231+ path->dentry = d;
33232+ if (unlikely(err))
33233+ goto out;
33234+
2cbb1c4b 33235+ lockdep_off();
1facf9fc 33236+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 33237+ lockdep_on();
1facf9fc 33238+ if (!err) {
33239+ struct path tmp = {
33240+ .dentry = path->dentry->d_parent,
33241+ .mnt = path->mnt
33242+ };
33243+
33244+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33245+ }
33246+
4f0767ce 33247+out:
1facf9fc 33248+ return err;
33249+}
33250+
33251+/* ---------------------------------------------------------------------- */
33252+
9dbd164d 33253+/* todo: support mmap_sem? */
1facf9fc 33254+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33255+ loff_t *ppos)
33256+{
33257+ ssize_t err;
33258+
2cbb1c4b 33259+ lockdep_off();
1facf9fc 33260+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 33261+ lockdep_on();
1facf9fc 33262+ if (err >= 0)
33263+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33264+ return err;
33265+}
33266+
33267+/* todo: kernel_read()? */
33268+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33269+ loff_t *ppos)
33270+{
33271+ ssize_t err;
33272+ mm_segment_t oldfs;
b752ccd1
AM
33273+ union {
33274+ void *k;
33275+ char __user *u;
33276+ } buf;
1facf9fc 33277+
b752ccd1 33278+ buf.k = kbuf;
1facf9fc 33279+ oldfs = get_fs();
33280+ set_fs(KERNEL_DS);
b752ccd1 33281+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 33282+ set_fs(oldfs);
33283+ return err;
33284+}
33285+
33286+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33287+ loff_t *ppos)
33288+{
33289+ ssize_t err;
33290+
2cbb1c4b 33291+ lockdep_off();
1facf9fc 33292+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 33293+ lockdep_on();
1facf9fc 33294+ if (err >= 0)
33295+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33296+ return err;
33297+}
33298+
33299+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33300+{
33301+ ssize_t err;
33302+ mm_segment_t oldfs;
b752ccd1
AM
33303+ union {
33304+ void *k;
33305+ const char __user *u;
33306+ } buf;
1facf9fc 33307+
b752ccd1 33308+ buf.k = kbuf;
1facf9fc 33309+ oldfs = get_fs();
33310+ set_fs(KERNEL_DS);
b752ccd1 33311+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 33312+ set_fs(oldfs);
33313+ return err;
33314+}
33315+
4a4d8108
AM
33316+int vfsub_flush(struct file *file, fl_owner_t id)
33317+{
33318+ int err;
33319+
33320+ err = 0;
523b37e3 33321+ if (file->f_op->flush) {
2000de60 33322+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
33323+ err = file->f_op->flush(file, id);
33324+ else {
33325+ lockdep_off();
33326+ err = file->f_op->flush(file, id);
33327+ lockdep_on();
33328+ }
4a4d8108
AM
33329+ if (!err)
33330+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33331+ /*ignore*/
33332+ }
33333+ return err;
33334+}
33335+
392086de 33336+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 33337+{
33338+ int err;
33339+
062440b3 33340+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 33341+
2cbb1c4b 33342+ lockdep_off();
392086de 33343+ err = iterate_dir(file, ctx);
2cbb1c4b 33344+ lockdep_on();
1facf9fc 33345+ if (err >= 0)
33346+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 33347+
1facf9fc 33348+ return err;
33349+}
33350+
33351+long vfsub_splice_to(struct file *in, loff_t *ppos,
33352+ struct pipe_inode_info *pipe, size_t len,
33353+ unsigned int flags)
33354+{
33355+ long err;
33356+
2cbb1c4b 33357+ lockdep_off();
0fc653ad 33358+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 33359+ lockdep_on();
4a4d8108 33360+ file_accessed(in);
1facf9fc 33361+ if (err >= 0)
33362+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33363+ return err;
33364+}
33365+
33366+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33367+ loff_t *ppos, size_t len, unsigned int flags)
33368+{
33369+ long err;
33370+
2cbb1c4b 33371+ lockdep_off();
0fc653ad 33372+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33373+ lockdep_on();
1facf9fc 33374+ if (err >= 0)
33375+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33376+ return err;
33377+}
33378+
53392da6
AM
33379+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33380+{
33381+ int err;
33382+
33383+ /* file can be NULL */
33384+ lockdep_off();
33385+ err = vfs_fsync(file, datasync);
33386+ lockdep_on();
33387+ if (!err) {
33388+ if (!path) {
33389+ AuDebugOn(!file);
33390+ path = &file->f_path;
33391+ }
33392+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33393+ }
33394+ return err;
33395+}
33396+
1facf9fc 33397+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33398+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33399+ struct file *h_file)
33400+{
33401+ int err;
33402+ struct inode *h_inode;
c06a8ce3 33403+ struct super_block *h_sb;
1facf9fc 33404+
1facf9fc 33405+ if (!h_file) {
c06a8ce3
AM
33406+ err = vfsub_truncate(h_path, length);
33407+ goto out;
1facf9fc 33408+ }
33409+
5527c038 33410+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33411+ h_sb = h_inode->i_sb;
33412+ lockdep_off();
33413+ sb_start_write(h_sb);
33414+ lockdep_on();
1facf9fc 33415+ err = locks_verify_truncate(h_inode, h_file, length);
33416+ if (!err)
953406b4 33417+ err = security_path_truncate(h_path);
2cbb1c4b
JR
33418+ if (!err) {
33419+ lockdep_off();
1facf9fc 33420+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
33421+ lockdep_on();
33422+ }
c06a8ce3
AM
33423+ lockdep_off();
33424+ sb_end_write(h_sb);
33425+ lockdep_on();
1facf9fc 33426+
4f0767ce 33427+out:
1facf9fc 33428+ return err;
33429+}
33430+
33431+/* ---------------------------------------------------------------------- */
33432+
33433+struct au_vfsub_mkdir_args {
33434+ int *errp;
33435+ struct inode *dir;
33436+ struct path *path;
33437+ int mode;
33438+};
33439+
33440+static void au_call_vfsub_mkdir(void *args)
33441+{
33442+ struct au_vfsub_mkdir_args *a = args;
33443+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33444+}
33445+
33446+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33447+{
33448+ int err, do_sio, wkq_err;
33449+
33450+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33451+ if (!do_sio) {
33452+ lockdep_off();
1facf9fc 33453+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33454+ lockdep_on();
33455+ } else {
1facf9fc 33456+ struct au_vfsub_mkdir_args args = {
33457+ .errp = &err,
33458+ .dir = dir,
33459+ .path = path,
33460+ .mode = mode
33461+ };
33462+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33463+ if (unlikely(wkq_err))
33464+ err = wkq_err;
33465+ }
33466+
33467+ return err;
33468+}
33469+
33470+struct au_vfsub_rmdir_args {
33471+ int *errp;
33472+ struct inode *dir;
33473+ struct path *path;
33474+};
33475+
33476+static void au_call_vfsub_rmdir(void *args)
33477+{
33478+ struct au_vfsub_rmdir_args *a = args;
33479+ *a->errp = vfsub_rmdir(a->dir, a->path);
33480+}
33481+
33482+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33483+{
33484+ int err, do_sio, wkq_err;
33485+
33486+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33487+ if (!do_sio) {
33488+ lockdep_off();
1facf9fc 33489+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33490+ lockdep_on();
33491+ } else {
1facf9fc 33492+ struct au_vfsub_rmdir_args args = {
33493+ .errp = &err,
33494+ .dir = dir,
33495+ .path = path
33496+ };
33497+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33498+ if (unlikely(wkq_err))
33499+ err = wkq_err;
33500+ }
33501+
33502+ return err;
33503+}
33504+
33505+/* ---------------------------------------------------------------------- */
33506+
33507+struct notify_change_args {
33508+ int *errp;
33509+ struct path *path;
33510+ struct iattr *ia;
523b37e3 33511+ struct inode **delegated_inode;
1facf9fc 33512+};
33513+
33514+static void call_notify_change(void *args)
33515+{
33516+ struct notify_change_args *a = args;
33517+ struct inode *h_inode;
33518+
5527c038 33519+ h_inode = d_inode(a->path->dentry);
1facf9fc 33520+ IMustLock(h_inode);
33521+
33522+ *a->errp = -EPERM;
33523+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 33524+ lockdep_off();
523b37e3
AM
33525+ *a->errp = notify_change(a->path->dentry, a->ia,
33526+ a->delegated_inode);
c1595e42 33527+ lockdep_on();
1facf9fc 33528+ if (!*a->errp)
33529+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33530+ }
33531+ AuTraceErr(*a->errp);
33532+}
33533+
523b37e3
AM
33534+int vfsub_notify_change(struct path *path, struct iattr *ia,
33535+ struct inode **delegated_inode)
1facf9fc 33536+{
33537+ int err;
33538+ struct notify_change_args args = {
523b37e3
AM
33539+ .errp = &err,
33540+ .path = path,
33541+ .ia = ia,
33542+ .delegated_inode = delegated_inode
1facf9fc 33543+ };
33544+
33545+ call_notify_change(&args);
33546+
33547+ return err;
33548+}
33549+
523b37e3
AM
33550+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33551+ struct inode **delegated_inode)
1facf9fc 33552+{
33553+ int err, wkq_err;
33554+ struct notify_change_args args = {
523b37e3
AM
33555+ .errp = &err,
33556+ .path = path,
33557+ .ia = ia,
33558+ .delegated_inode = delegated_inode
1facf9fc 33559+ };
33560+
33561+ wkq_err = au_wkq_wait(call_notify_change, &args);
33562+ if (unlikely(wkq_err))
33563+ err = wkq_err;
33564+
33565+ return err;
33566+}
33567+
33568+/* ---------------------------------------------------------------------- */
33569+
33570+struct unlink_args {
33571+ int *errp;
33572+ struct inode *dir;
33573+ struct path *path;
523b37e3 33574+ struct inode **delegated_inode;
1facf9fc 33575+};
33576+
33577+static void call_unlink(void *args)
33578+{
33579+ struct unlink_args *a = args;
33580+ struct dentry *d = a->path->dentry;
33581+ struct inode *h_inode;
33582+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33583+ && au_dcount(d) == 1);
1facf9fc 33584+
33585+ IMustLock(a->dir);
33586+
33587+ a->path->dentry = d->d_parent;
33588+ *a->errp = security_path_unlink(a->path, d);
33589+ a->path->dentry = d;
33590+ if (unlikely(*a->errp))
33591+ return;
33592+
33593+ if (!stop_sillyrename)
33594+ dget(d);
5527c038
JR
33595+ h_inode = NULL;
33596+ if (d_is_positive(d)) {
33597+ h_inode = d_inode(d);
027c5e7a 33598+ ihold(h_inode);
5527c038 33599+ }
1facf9fc 33600+
2cbb1c4b 33601+ lockdep_off();
523b37e3 33602+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 33603+ lockdep_on();
1facf9fc 33604+ if (!*a->errp) {
33605+ struct path tmp = {
33606+ .dentry = d->d_parent,
33607+ .mnt = a->path->mnt
33608+ };
33609+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33610+ }
33611+
33612+ if (!stop_sillyrename)
33613+ dput(d);
33614+ if (h_inode)
33615+ iput(h_inode);
33616+
33617+ AuTraceErr(*a->errp);
33618+}
33619+
33620+/*
33621+ * @dir: must be locked.
33622+ * @dentry: target dentry.
33623+ */
523b37e3
AM
33624+int vfsub_unlink(struct inode *dir, struct path *path,
33625+ struct inode **delegated_inode, int force)
1facf9fc 33626+{
33627+ int err;
33628+ struct unlink_args args = {
523b37e3
AM
33629+ .errp = &err,
33630+ .dir = dir,
33631+ .path = path,
33632+ .delegated_inode = delegated_inode
1facf9fc 33633+ };
33634+
33635+ if (!force)
33636+ call_unlink(&args);
33637+ else {
33638+ int wkq_err;
33639+
33640+ wkq_err = au_wkq_wait(call_unlink, &args);
33641+ if (unlikely(wkq_err))
33642+ err = wkq_err;
33643+ }
33644+
33645+ return err;
33646+}
7f207e10 33647diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
eca34b5c
AM
33648--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
33649+++ linux/fs/aufs/vfsub.h 2019-07-11 15:42:14.475571481 +0200
eca801bf 33650@@ -0,0 +1,354 @@
062440b3 33651+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33652+/*
ba1aed25 33653+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 33654+ *
33655+ * This program, aufs is free software; you can redistribute it and/or modify
33656+ * it under the terms of the GNU General Public License as published by
33657+ * the Free Software Foundation; either version 2 of the License, or
33658+ * (at your option) any later version.
dece6358
AM
33659+ *
33660+ * This program is distributed in the hope that it will be useful,
33661+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33662+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33663+ * GNU General Public License for more details.
33664+ *
33665+ * You should have received a copy of the GNU General Public License
523b37e3 33666+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33667+ */
33668+
33669+/*
33670+ * sub-routines for VFS
33671+ */
33672+
33673+#ifndef __AUFS_VFSUB_H__
33674+#define __AUFS_VFSUB_H__
33675+
33676+#ifdef __KERNEL__
33677+
33678+#include <linux/fs.h>
b4510431 33679+#include <linux/mount.h>
8cdd5066 33680+#include <linux/posix_acl.h>
c1595e42 33681+#include <linux/xattr.h>
7f207e10 33682+#include "debug.h"
1facf9fc 33683+
7f207e10 33684+/* copied from linux/fs/internal.h */
2cbb1c4b 33685+/* todo: BAD approach!! */
c06a8ce3 33686+extern void __mnt_drop_write(struct vfsmount *);
acd2b654 33687+extern struct file *alloc_empty_file(int, const struct cred *);
7f207e10
AM
33688+
33689+/* ---------------------------------------------------------------------- */
1facf9fc 33690+
33691+/* lock subclass for lower inode */
33692+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33693+/* reduce? gave up. */
33694+enum {
c1595e42 33695+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33696+ AuLsc_I_PARENT, /* lower inode, parent first */
33697+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33698+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33699+ AuLsc_I_CHILD,
33700+ AuLsc_I_CHILD2,
33701+ AuLsc_I_End
33702+};
33703+
33704+/* to debug easier, do not make them inlined functions */
33705+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33706+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33707+
33708+/* ---------------------------------------------------------------------- */
33709+
7f207e10
AM
33710+static inline void vfsub_drop_nlink(struct inode *inode)
33711+{
33712+ AuDebugOn(!inode->i_nlink);
33713+ drop_nlink(inode);
33714+}
33715+
027c5e7a
AM
33716+static inline void vfsub_dead_dir(struct inode *inode)
33717+{
33718+ AuDebugOn(!S_ISDIR(inode->i_mode));
33719+ inode->i_flags |= S_DEAD;
33720+ clear_nlink(inode);
33721+}
33722+
392086de
AM
33723+static inline int vfsub_native_ro(struct inode *inode)
33724+{
8b6a4947 33725+ return sb_rdonly(inode->i_sb)
392086de
AM
33726+ || IS_RDONLY(inode)
33727+ /* || IS_APPEND(inode) */
33728+ || IS_IMMUTABLE(inode);
33729+}
33730+
8cdd5066
JR
33731+#ifdef CONFIG_AUFS_BR_FUSE
33732+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33733+#else
33734+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33735+#endif
33736+
a2654f78
AM
33737+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33738+
7f207e10
AM
33739+/* ---------------------------------------------------------------------- */
33740+
33741+int vfsub_update_h_iattr(struct path *h_path, int *did);
33742+struct file *vfsub_dentry_open(struct path *path, int flags);
33743+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
acd2b654 33744+struct au_branch;
b912730e 33745+struct vfsub_aopen_args {
acd2b654
AM
33746+ struct file *file;
33747+ unsigned int open_flag;
33748+ umode_t create_mode;
33749+ struct au_branch *br;
b912730e 33750+};
b912730e 33751+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 33752+ struct vfsub_aopen_args *args);
1facf9fc 33753+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33754+
febd17d6
JR
33755+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33756+ struct dentry *parent, int len);
1facf9fc 33757+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33758+ int len);
b4510431
AM
33759+
33760+struct vfsub_lkup_one_args {
33761+ struct dentry **errp;
33762+ struct qstr *name;
33763+ struct dentry *parent;
33764+};
33765+
33766+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33767+ struct dentry *parent)
33768+{
33769+ return vfsub_lookup_one_len(name->name, parent, name->len);
33770+}
33771+
33772+void vfsub_call_lkup_one(void *args);
33773+
33774+/* ---------------------------------------------------------------------- */
33775+
33776+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33777+{
33778+ int err;
076b876e 33779+
b4510431
AM
33780+ lockdep_off();
33781+ err = mnt_want_write(mnt);
33782+ lockdep_on();
33783+ return err;
33784+}
33785+
33786+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33787+{
33788+ lockdep_off();
33789+ mnt_drop_write(mnt);
33790+ lockdep_on();
33791+}
1facf9fc 33792+
7e9cd9fe 33793+#if 0 /* reserved */
c06a8ce3
AM
33794+static inline void vfsub_mnt_drop_write_file(struct file *file)
33795+{
33796+ lockdep_off();
33797+ mnt_drop_write_file(file);
33798+ lockdep_on();
33799+}
7e9cd9fe 33800+#endif
c06a8ce3 33801+
1facf9fc 33802+/* ---------------------------------------------------------------------- */
33803+
33804+struct au_hinode;
33805+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33806+ struct dentry *d2, struct au_hinode *hdir2);
33807+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33808+ struct dentry *d2, struct au_hinode *hdir2);
33809+
537831f9
AM
33810+int vfsub_create(struct inode *dir, struct path *path, int mode,
33811+ bool want_excl);
1facf9fc 33812+int vfsub_symlink(struct inode *dir, struct path *path,
33813+ const char *symname);
33814+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33815+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33816+ struct path *path, struct inode **delegated_inode);
1facf9fc 33817+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33818+ struct inode *hdir, struct path *path,
f2c43d5f 33819+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33820+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33821+int vfsub_rmdir(struct inode *dir, struct path *path);
33822+
33823+/* ---------------------------------------------------------------------- */
33824+
33825+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33826+ loff_t *ppos);
33827+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33828+ loff_t *ppos);
33829+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33830+ loff_t *ppos);
33831+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33832+ loff_t *ppos);
4a4d8108 33833+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33834+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33835+
c06a8ce3
AM
33836+static inline loff_t vfsub_f_size_read(struct file *file)
33837+{
33838+ return i_size_read(file_inode(file));
33839+}
33840+
4a4d8108
AM
33841+static inline unsigned int vfsub_file_flags(struct file *file)
33842+{
33843+ unsigned int flags;
33844+
33845+ spin_lock(&file->f_lock);
33846+ flags = file->f_flags;
33847+ spin_unlock(&file->f_lock);
33848+
33849+ return flags;
33850+}
1308ab2a 33851+
f0c0a007
AM
33852+static inline int vfsub_file_execed(struct file *file)
33853+{
33854+ /* todo: direct access f_flags */
33855+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33856+}
33857+
7e9cd9fe 33858+#if 0 /* reserved */
1facf9fc 33859+static inline void vfsub_file_accessed(struct file *h_file)
33860+{
33861+ file_accessed(h_file);
33862+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33863+}
7e9cd9fe 33864+#endif
1facf9fc 33865+
79b8bda9 33866+#if 0 /* reserved */
1facf9fc 33867+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33868+ struct dentry *h_dentry)
33869+{
33870+ struct path h_path = {
33871+ .dentry = h_dentry,
33872+ .mnt = h_mnt
33873+ };
92d182d2 33874+ touch_atime(&h_path);
1facf9fc 33875+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33876+}
79b8bda9 33877+#endif
1facf9fc 33878+
cd7a4cd9
AM
33879+static inline int vfsub_update_time(struct inode *h_inode,
33880+ struct timespec64 *ts, int flags)
0c3ec466 33881+{
5afbbe0d 33882+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33883+ /* no vfsub_update_h_iattr() since we don't have struct path */
33884+}
33885+
8cdd5066
JR
33886+#ifdef CONFIG_FS_POSIX_ACL
33887+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33888+{
33889+ int err;
33890+
33891+ err = posix_acl_chmod(h_inode, h_mode);
33892+ if (err == -EOPNOTSUPP)
33893+ err = 0;
33894+ return err;
33895+}
33896+#else
33897+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33898+#endif
33899+
4a4d8108
AM
33900+long vfsub_splice_to(struct file *in, loff_t *ppos,
33901+ struct pipe_inode_info *pipe, size_t len,
33902+ unsigned int flags);
33903+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33904+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33905+
33906+static inline long vfsub_truncate(struct path *path, loff_t length)
33907+{
33908+ long err;
076b876e 33909+
c06a8ce3
AM
33910+ lockdep_off();
33911+ err = vfs_truncate(path, length);
33912+ lockdep_on();
33913+ return err;
33914+}
33915+
4a4d8108
AM
33916+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33917+ struct file *h_file);
53392da6 33918+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 33919+
521ced18
JR
33920+/*
33921+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33922+ * ioctl.
33923+ */
9f237c51
AM
33924+static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33925+ loff_t len)
521ced18 33926+{
9f237c51 33927+ loff_t err;
521ced18
JR
33928+
33929+ lockdep_off();
9f237c51 33930+ err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
521ced18
JR
33931+ lockdep_on();
33932+
33933+ return err;
33934+}
33935+
33936+/* copy_file_range(2) is a systemcall */
33937+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33938+ struct file *dst, loff_t dst_pos,
33939+ size_t len, unsigned int flags)
33940+{
33941+ ssize_t ssz;
33942+
33943+ lockdep_off();
33944+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33945+ lockdep_on();
33946+
33947+ return ssz;
33948+}
33949+
1facf9fc 33950+/* ---------------------------------------------------------------------- */
33951+
33952+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33953+{
33954+ loff_t err;
33955+
2cbb1c4b 33956+ lockdep_off();
1facf9fc 33957+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 33958+ lockdep_on();
1facf9fc 33959+ return err;
33960+}
33961+
33962+/* ---------------------------------------------------------------------- */
33963+
4a4d8108
AM
33964+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33965+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
33966+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33967+ struct inode **delegated_inode);
33968+int vfsub_notify_change(struct path *path, struct iattr *ia,
33969+ struct inode **delegated_inode);
33970+int vfsub_unlink(struct inode *dir, struct path *path,
33971+ struct inode **delegated_inode, int force);
4a4d8108 33972+
521ced18
JR
33973+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33974+{
33975+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33976+}
33977+
c1595e42
JR
33978+/* ---------------------------------------------------------------------- */
33979+
33980+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33981+ const void *value, size_t size, int flags)
33982+{
33983+ int err;
33984+
33985+ lockdep_off();
33986+ err = vfs_setxattr(dentry, name, value, size, flags);
33987+ lockdep_on();
33988+
33989+ return err;
33990+}
33991+
33992+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33993+{
33994+ int err;
33995+
33996+ lockdep_off();
33997+ err = vfs_removexattr(dentry, name);
33998+ lockdep_on();
33999+
34000+ return err;
34001+}
34002+
1facf9fc 34003+#endif /* __KERNEL__ */
34004+#endif /* __AUFS_VFSUB_H__ */
7f207e10 34005diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
eca34b5c
AM
34006--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
34007+++ linux/fs/aufs/wbr_policy.c 2019-07-11 15:42:14.475571481 +0200
cd7a4cd9
AM
34008@@ -0,0 +1,830 @@
34009+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34010+/*
ba1aed25 34011+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 34012+ *
34013+ * This program, aufs is free software; you can redistribute it and/or modify
34014+ * it under the terms of the GNU General Public License as published by
34015+ * the Free Software Foundation; either version 2 of the License, or
34016+ * (at your option) any later version.
dece6358
AM
34017+ *
34018+ * This program is distributed in the hope that it will be useful,
34019+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34020+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34021+ * GNU General Public License for more details.
34022+ *
34023+ * You should have received a copy of the GNU General Public License
523b37e3 34024+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34025+ */
34026+
34027+/*
34028+ * policies for selecting one among multiple writable branches
34029+ */
34030+
34031+#include <linux/statfs.h>
34032+#include "aufs.h"
34033+
34034+/* subset of cpup_attr() */
34035+static noinline_for_stack
34036+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34037+{
34038+ int err, sbits;
34039+ struct iattr ia;
34040+ struct inode *h_isrc;
34041+
5527c038 34042+ h_isrc = d_inode(h_src);
1facf9fc 34043+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34044+ ia.ia_mode = h_isrc->i_mode;
34045+ ia.ia_uid = h_isrc->i_uid;
34046+ ia.ia_gid = h_isrc->i_gid;
34047+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 34048+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
34049+ /* no delegation since it is just created */
34050+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34051+
34052+ /* is this nfs only? */
34053+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34054+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34055+ ia.ia_mode = h_isrc->i_mode;
523b37e3 34056+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34057+ }
34058+
34059+ return err;
34060+}
34061+
34062+#define AuCpdown_PARENT_OPQ 1
34063+#define AuCpdown_WHED (1 << 1)
34064+#define AuCpdown_MADE_DIR (1 << 2)
34065+#define AuCpdown_DIROPQ (1 << 3)
34066+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
34067+#define au_fset_cpdown(flags, name) \
34068+ do { (flags) |= AuCpdown_##name; } while (0)
34069+#define au_fclr_cpdown(flags, name) \
34070+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 34071+
1facf9fc 34072+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 34073+ unsigned int *flags)
1facf9fc 34074+{
34075+ int err;
34076+ struct dentry *opq_dentry;
34077+
34078+ opq_dentry = au_diropq_create(dentry, bdst);
34079+ err = PTR_ERR(opq_dentry);
34080+ if (IS_ERR(opq_dentry))
34081+ goto out;
34082+ dput(opq_dentry);
c2b27bf2 34083+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 34084+
4f0767ce 34085+out:
1facf9fc 34086+ return err;
34087+}
34088+
34089+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34090+ struct inode *dir, aufs_bindex_t bdst)
34091+{
34092+ int err;
34093+ struct path h_path;
34094+ struct au_branch *br;
34095+
34096+ br = au_sbr(dentry->d_sb, bdst);
34097+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34098+ err = PTR_ERR(h_path.dentry);
34099+ if (IS_ERR(h_path.dentry))
34100+ goto out;
34101+
34102+ err = 0;
5527c038 34103+ if (d_is_positive(h_path.dentry)) {
86dc4139 34104+ h_path.mnt = au_br_mnt(br);
1facf9fc 34105+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34106+ dentry);
34107+ }
34108+ dput(h_path.dentry);
34109+
4f0767ce 34110+out:
1facf9fc 34111+ return err;
34112+}
34113+
34114+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 34115+ struct au_pin *pin,
1facf9fc 34116+ struct dentry *h_parent, void *arg)
34117+{
34118+ int err, rerr;
5afbbe0d 34119+ aufs_bindex_t bopq, btop;
1facf9fc 34120+ struct path h_path;
34121+ struct dentry *parent;
34122+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 34123+ unsigned int *flags = arg;
1facf9fc 34124+
5afbbe0d 34125+ btop = au_dbtop(dentry);
1facf9fc 34126+ /* dentry is di-locked */
34127+ parent = dget_parent(dentry);
5527c038
JR
34128+ dir = d_inode(parent);
34129+ h_dir = d_inode(h_parent);
1facf9fc 34130+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34131+ IMustLock(h_dir);
34132+
86dc4139 34133+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 34134+ if (unlikely(err < 0))
34135+ goto out;
34136+ h_path.dentry = au_h_dptr(dentry, bdst);
34137+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
cd7a4cd9 34138+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
1facf9fc 34139+ if (unlikely(err))
34140+ goto out_put;
c2b27bf2 34141+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 34142+
1facf9fc 34143+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
34144+ au_fclr_cpdown(*flags, WHED);
34145+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 34146+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
34147+ au_fset_cpdown(*flags, WHED);
34148+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34149+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 34150+ h_inode = d_inode(h_path.dentry);
febd17d6 34151+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
34152+ if (au_ftest_cpdown(*flags, WHED)) {
34153+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 34154+ if (unlikely(err)) {
febd17d6 34155+ inode_unlock(h_inode);
1facf9fc 34156+ goto out_dir;
34157+ }
34158+ }
34159+
5afbbe0d 34160+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 34161+ inode_unlock(h_inode);
1facf9fc 34162+ if (unlikely(err))
34163+ goto out_opq;
34164+
c2b27bf2 34165+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 34166+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34167+ if (unlikely(err))
34168+ goto out_opq;
34169+ }
34170+
5527c038 34171+ inode = d_inode(dentry);
5afbbe0d
AM
34172+ if (au_ibbot(inode) < bdst)
34173+ au_set_ibbot(inode, bdst);
1facf9fc 34174+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34175+ au_hi_flags(inode, /*isdir*/1));
076b876e 34176+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 34177+ goto out; /* success */
34178+
34179+ /* revert */
4f0767ce 34180+out_opq:
c2b27bf2 34181+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 34182+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 34183+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 34184+ inode_unlock(h_inode);
1facf9fc 34185+ if (unlikely(rerr)) {
523b37e3
AM
34186+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34187+ dentry, bdst, rerr);
1facf9fc 34188+ err = -EIO;
34189+ goto out;
34190+ }
34191+ }
4f0767ce 34192+out_dir:
c2b27bf2 34193+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 34194+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34195+ if (unlikely(rerr)) {
523b37e3
AM
34196+ AuIOErr("failed removing %pd b%d (%d)\n",
34197+ dentry, bdst, rerr);
1facf9fc 34198+ err = -EIO;
34199+ }
34200+ }
4f0767ce 34201+out_put:
1facf9fc 34202+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
34203+ if (au_dbbot(dentry) == bdst)
34204+ au_update_dbbot(dentry);
4f0767ce 34205+out:
1facf9fc 34206+ dput(parent);
34207+ return err;
34208+}
34209+
34210+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34211+{
34212+ int err;
c2b27bf2 34213+ unsigned int flags;
1facf9fc 34214+
c2b27bf2
AM
34215+ flags = 0;
34216+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 34217+
34218+ return err;
34219+}
34220+
34221+/* ---------------------------------------------------------------------- */
34222+
34223+/* policies for create */
34224+
c2b27bf2 34225+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
34226+{
34227+ int err, i, j, ndentry;
34228+ aufs_bindex_t bopq;
34229+ struct au_dcsub_pages dpages;
34230+ struct au_dpage *dpage;
34231+ struct dentry **dentries, *parent, *d;
34232+
34233+ err = au_dpages_init(&dpages, GFP_NOFS);
34234+ if (unlikely(err))
34235+ goto out;
34236+ parent = dget_parent(dentry);
027c5e7a 34237+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
34238+ if (unlikely(err))
34239+ goto out_free;
34240+
34241+ err = bindex;
34242+ for (i = 0; i < dpages.ndpage; i++) {
34243+ dpage = dpages.dpages + i;
34244+ dentries = dpage->dentries;
34245+ ndentry = dpage->ndentry;
34246+ for (j = 0; j < ndentry; j++) {
34247+ d = dentries[j];
34248+ di_read_lock_parent2(d, !AuLock_IR);
34249+ bopq = au_dbdiropq(d);
34250+ di_read_unlock(d, !AuLock_IR);
34251+ if (bopq >= 0 && bopq < err)
34252+ err = bopq;
34253+ }
34254+ }
34255+
34256+out_free:
34257+ dput(parent);
34258+ au_dpages_free(&dpages);
34259+out:
34260+ return err;
34261+}
34262+
1facf9fc 34263+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34264+{
34265+ for (; bindex >= 0; bindex--)
34266+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34267+ return bindex;
34268+ return -EROFS;
34269+}
34270+
34271+/* top down parent */
392086de
AM
34272+static int au_wbr_create_tdp(struct dentry *dentry,
34273+ unsigned int flags __maybe_unused)
1facf9fc 34274+{
34275+ int err;
5afbbe0d 34276+ aufs_bindex_t btop, bindex;
1facf9fc 34277+ struct super_block *sb;
34278+ struct dentry *parent, *h_parent;
34279+
34280+ sb = dentry->d_sb;
5afbbe0d
AM
34281+ btop = au_dbtop(dentry);
34282+ err = btop;
34283+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 34284+ goto out;
34285+
34286+ err = -EROFS;
34287+ parent = dget_parent(dentry);
5afbbe0d 34288+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 34289+ h_parent = au_h_dptr(parent, bindex);
5527c038 34290+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34291+ continue;
34292+
34293+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34294+ err = bindex;
34295+ break;
34296+ }
34297+ }
34298+ dput(parent);
34299+
34300+ /* bottom up here */
4a4d8108 34301+ if (unlikely(err < 0)) {
5afbbe0d 34302+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
34303+ if (err >= 0)
34304+ err = au_wbr_nonopq(dentry, err);
34305+ }
1facf9fc 34306+
4f0767ce 34307+out:
1facf9fc 34308+ AuDbg("b%d\n", err);
34309+ return err;
34310+}
34311+
34312+/* ---------------------------------------------------------------------- */
34313+
34314+/* an exception for the policy other than tdp */
34315+static int au_wbr_create_exp(struct dentry *dentry)
34316+{
34317+ int err;
34318+ aufs_bindex_t bwh, bdiropq;
34319+ struct dentry *parent;
34320+
34321+ err = -1;
34322+ bwh = au_dbwh(dentry);
34323+ parent = dget_parent(dentry);
34324+ bdiropq = au_dbdiropq(parent);
34325+ if (bwh >= 0) {
34326+ if (bdiropq >= 0)
34327+ err = min(bdiropq, bwh);
34328+ else
34329+ err = bwh;
34330+ AuDbg("%d\n", err);
34331+ } else if (bdiropq >= 0) {
34332+ err = bdiropq;
34333+ AuDbg("%d\n", err);
34334+ }
34335+ dput(parent);
34336+
4a4d8108
AM
34337+ if (err >= 0)
34338+ err = au_wbr_nonopq(dentry, err);
34339+
1facf9fc 34340+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34341+ err = -1;
34342+
34343+ AuDbg("%d\n", err);
34344+ return err;
34345+}
34346+
34347+/* ---------------------------------------------------------------------- */
34348+
34349+/* round robin */
34350+static int au_wbr_create_init_rr(struct super_block *sb)
34351+{
34352+ int err;
34353+
5afbbe0d 34354+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 34355+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 34356+ /* smp_mb(); */
1facf9fc 34357+
34358+ AuDbg("b%d\n", err);
34359+ return err;
34360+}
34361+
392086de 34362+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 34363+{
34364+ int err, nbr;
34365+ unsigned int u;
5afbbe0d 34366+ aufs_bindex_t bindex, bbot;
1facf9fc 34367+ struct super_block *sb;
34368+ atomic_t *next;
34369+
34370+ err = au_wbr_create_exp(dentry);
34371+ if (err >= 0)
34372+ goto out;
34373+
34374+ sb = dentry->d_sb;
34375+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34376+ bbot = au_sbbot(sb);
34377+ nbr = bbot + 1;
34378+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34379+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34380+ err = atomic_dec_return(next) + 1;
34381+ /* modulo for 0 is meaningless */
34382+ if (unlikely(!err))
34383+ err = atomic_dec_return(next) + 1;
34384+ } else
34385+ err = atomic_read(next);
34386+ AuDbg("%d\n", err);
34387+ u = err;
34388+ err = u % nbr;
34389+ AuDbg("%d\n", err);
34390+ if (!au_br_rdonly(au_sbr(sb, err)))
34391+ break;
34392+ err = -EROFS;
34393+ }
34394+
4a4d8108
AM
34395+ if (err >= 0)
34396+ err = au_wbr_nonopq(dentry, err);
34397+
4f0767ce 34398+out:
1facf9fc 34399+ AuDbg("%d\n", err);
34400+ return err;
34401+}
34402+
34403+/* ---------------------------------------------------------------------- */
34404+
34405+/* most free space */
392086de 34406+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34407+{
34408+ struct super_block *sb;
34409+ struct au_branch *br;
34410+ struct au_wbr_mfs *mfs;
392086de 34411+ struct dentry *h_parent;
5afbbe0d 34412+ aufs_bindex_t bindex, bbot;
1facf9fc 34413+ int err;
34414+ unsigned long long b, bavail;
7f207e10 34415+ struct path h_path;
1facf9fc 34416+ /* reduce the stack usage */
34417+ struct kstatfs *st;
34418+
34419+ st = kmalloc(sizeof(*st), GFP_NOFS);
34420+ if (unlikely(!st)) {
34421+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34422+ return;
34423+ }
34424+
34425+ bavail = 0;
34426+ sb = dentry->d_sb;
34427+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34428+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34429+ mfs->mfs_bindex = -EROFS;
34430+ mfs->mfsrr_bytes = 0;
392086de
AM
34431+ if (!parent) {
34432+ bindex = 0;
5afbbe0d 34433+ bbot = au_sbbot(sb);
392086de 34434+ } else {
5afbbe0d
AM
34435+ bindex = au_dbtop(parent);
34436+ bbot = au_dbtaildir(parent);
392086de
AM
34437+ }
34438+
5afbbe0d 34439+ for (; bindex <= bbot; bindex++) {
392086de
AM
34440+ if (parent) {
34441+ h_parent = au_h_dptr(parent, bindex);
5527c038 34442+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34443+ continue;
34444+ }
1facf9fc 34445+ br = au_sbr(sb, bindex);
34446+ if (au_br_rdonly(br))
34447+ continue;
34448+
34449+ /* sb->s_root for NFS is unreliable */
86dc4139 34450+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34451+ h_path.dentry = h_path.mnt->mnt_root;
34452+ err = vfs_statfs(&h_path, st);
1facf9fc 34453+ if (unlikely(err)) {
34454+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34455+ continue;
34456+ }
34457+
34458+ /* when the available size is equal, select the lower one */
34459+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34460+ || sizeof(b) < sizeof(st->f_bsize));
34461+ b = st->f_bavail * st->f_bsize;
34462+ br->br_wbr->wbr_bytes = b;
34463+ if (b >= bavail) {
34464+ bavail = b;
34465+ mfs->mfs_bindex = bindex;
34466+ mfs->mfs_jiffy = jiffies;
34467+ }
34468+ }
34469+
34470+ mfs->mfsrr_bytes = bavail;
34471+ AuDbg("b%d\n", mfs->mfs_bindex);
9f237c51 34472+ au_kfree_rcu(st);
1facf9fc 34473+}
34474+
392086de 34475+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34476+{
34477+ int err;
392086de 34478+ struct dentry *parent;
1facf9fc 34479+ struct super_block *sb;
34480+ struct au_wbr_mfs *mfs;
34481+
34482+ err = au_wbr_create_exp(dentry);
34483+ if (err >= 0)
34484+ goto out;
34485+
34486+ sb = dentry->d_sb;
392086de
AM
34487+ parent = NULL;
34488+ if (au_ftest_wbr(flags, PARENT))
34489+ parent = dget_parent(dentry);
1facf9fc 34490+ mfs = &au_sbi(sb)->si_wbr_mfs;
34491+ mutex_lock(&mfs->mfs_lock);
34492+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34493+ || mfs->mfs_bindex < 0
34494+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34495+ au_mfs(dentry, parent);
1facf9fc 34496+ mutex_unlock(&mfs->mfs_lock);
34497+ err = mfs->mfs_bindex;
392086de 34498+ dput(parent);
1facf9fc 34499+
4a4d8108
AM
34500+ if (err >= 0)
34501+ err = au_wbr_nonopq(dentry, err);
34502+
4f0767ce 34503+out:
1facf9fc 34504+ AuDbg("b%d\n", err);
34505+ return err;
34506+}
34507+
34508+static int au_wbr_create_init_mfs(struct super_block *sb)
34509+{
34510+ struct au_wbr_mfs *mfs;
34511+
34512+ mfs = &au_sbi(sb)->si_wbr_mfs;
34513+ mutex_init(&mfs->mfs_lock);
34514+ mfs->mfs_jiffy = 0;
34515+ mfs->mfs_bindex = -EROFS;
34516+
34517+ return 0;
34518+}
34519+
34520+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34521+{
34522+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34523+ return 0;
34524+}
34525+
34526+/* ---------------------------------------------------------------------- */
34527+
f2c43d5f
AM
34528+/* top down regardless parent, and then mfs */
34529+static int au_wbr_create_tdmfs(struct dentry *dentry,
34530+ unsigned int flags __maybe_unused)
34531+{
34532+ int err;
34533+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34534+ unsigned long long watermark;
34535+ struct super_block *sb;
34536+ struct au_wbr_mfs *mfs;
34537+ struct au_branch *br;
34538+ struct dentry *parent;
34539+
34540+ sb = dentry->d_sb;
34541+ mfs = &au_sbi(sb)->si_wbr_mfs;
34542+ mutex_lock(&mfs->mfs_lock);
34543+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34544+ || mfs->mfs_bindex < 0)
34545+ au_mfs(dentry, /*parent*/NULL);
34546+ watermark = mfs->mfsrr_watermark;
34547+ bmfs = mfs->mfs_bindex;
34548+ mutex_unlock(&mfs->mfs_lock);
34549+
34550+ /* another style of au_wbr_create_exp() */
34551+ bwh = au_dbwh(dentry);
34552+ parent = dget_parent(dentry);
34553+ btail = au_dbtaildir(parent);
34554+ if (bwh >= 0 && bwh < btail)
34555+ btail = bwh;
34556+
34557+ err = au_wbr_nonopq(dentry, btail);
34558+ if (unlikely(err < 0))
34559+ goto out;
34560+ btail = err;
34561+ bfound = -1;
34562+ for (bindex = 0; bindex <= btail; bindex++) {
34563+ br = au_sbr(sb, bindex);
34564+ if (au_br_rdonly(br))
34565+ continue;
34566+ if (br->br_wbr->wbr_bytes > watermark) {
34567+ bfound = bindex;
34568+ break;
34569+ }
34570+ }
34571+ err = bfound;
34572+ if (err < 0)
34573+ err = bmfs;
34574+
34575+out:
34576+ dput(parent);
34577+ AuDbg("b%d\n", err);
34578+ return err;
34579+}
34580+
34581+/* ---------------------------------------------------------------------- */
34582+
1facf9fc 34583+/* most free space and then round robin */
392086de 34584+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34585+{
34586+ int err;
34587+ struct au_wbr_mfs *mfs;
34588+
392086de 34589+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34590+ if (err >= 0) {
34591+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34592+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34593+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34594+ err = au_wbr_create_rr(dentry, flags);
dece6358 34595+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34596+ }
34597+
34598+ AuDbg("b%d\n", err);
34599+ return err;
34600+}
34601+
34602+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34603+{
34604+ int err;
34605+
34606+ au_wbr_create_init_mfs(sb); /* ignore */
34607+ err = au_wbr_create_init_rr(sb);
34608+
34609+ return err;
34610+}
34611+
34612+/* ---------------------------------------------------------------------- */
34613+
34614+/* top down parent and most free space */
392086de 34615+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34616+{
34617+ int err, e2;
34618+ unsigned long long b;
5afbbe0d 34619+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34620+ struct super_block *sb;
34621+ struct dentry *parent, *h_parent;
34622+ struct au_branch *br;
34623+
392086de 34624+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34625+ if (unlikely(err < 0))
34626+ goto out;
34627+ parent = dget_parent(dentry);
5afbbe0d
AM
34628+ btop = au_dbtop(parent);
34629+ bbot = au_dbtaildir(parent);
34630+ if (btop == bbot)
1facf9fc 34631+ goto out_parent; /* success */
34632+
392086de 34633+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34634+ if (e2 < 0)
34635+ goto out_parent; /* success */
34636+
34637+ /* when the available size is equal, select upper one */
34638+ sb = dentry->d_sb;
34639+ br = au_sbr(sb, err);
34640+ b = br->br_wbr->wbr_bytes;
34641+ AuDbg("b%d, %llu\n", err, b);
34642+
5afbbe0d 34643+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34644+ h_parent = au_h_dptr(parent, bindex);
5527c038 34645+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34646+ continue;
34647+
34648+ br = au_sbr(sb, bindex);
34649+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34650+ b = br->br_wbr->wbr_bytes;
34651+ err = bindex;
34652+ AuDbg("b%d, %llu\n", err, b);
34653+ }
34654+ }
34655+
4a4d8108
AM
34656+ if (err >= 0)
34657+ err = au_wbr_nonopq(dentry, err);
34658+
4f0767ce 34659+out_parent:
1facf9fc 34660+ dput(parent);
4f0767ce 34661+out:
1facf9fc 34662+ AuDbg("b%d\n", err);
34663+ return err;
34664+}
34665+
34666+/* ---------------------------------------------------------------------- */
34667+
392086de
AM
34668+/*
34669+ * - top down parent
34670+ * - most free space with parent
34671+ * - most free space round-robin regardless parent
34672+ */
34673+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34674+{
34675+ int err;
34676+ unsigned long long watermark;
34677+ struct super_block *sb;
34678+ struct au_branch *br;
34679+ struct au_wbr_mfs *mfs;
34680+
34681+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34682+ if (unlikely(err < 0))
34683+ goto out;
34684+
34685+ sb = dentry->d_sb;
34686+ br = au_sbr(sb, err);
34687+ mfs = &au_sbi(sb)->si_wbr_mfs;
34688+ mutex_lock(&mfs->mfs_lock);
34689+ watermark = mfs->mfsrr_watermark;
34690+ mutex_unlock(&mfs->mfs_lock);
34691+ if (br->br_wbr->wbr_bytes < watermark)
34692+ /* regardless the parent dir */
34693+ err = au_wbr_create_mfsrr(dentry, flags);
34694+
34695+out:
34696+ AuDbg("b%d\n", err);
34697+ return err;
34698+}
34699+
34700+/* ---------------------------------------------------------------------- */
34701+
1facf9fc 34702+/* policies for copyup */
34703+
34704+/* top down parent */
34705+static int au_wbr_copyup_tdp(struct dentry *dentry)
34706+{
392086de 34707+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34708+}
34709+
34710+/* bottom up parent */
34711+static int au_wbr_copyup_bup(struct dentry *dentry)
34712+{
34713+ int err;
5afbbe0d 34714+ aufs_bindex_t bindex, btop;
1facf9fc 34715+ struct dentry *parent, *h_parent;
34716+ struct super_block *sb;
34717+
34718+ err = -EROFS;
34719+ sb = dentry->d_sb;
34720+ parent = dget_parent(dentry);
5afbbe0d
AM
34721+ btop = au_dbtop(parent);
34722+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34723+ h_parent = au_h_dptr(parent, bindex);
5527c038 34724+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34725+ continue;
34726+
34727+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34728+ err = bindex;
34729+ break;
34730+ }
34731+ }
34732+ dput(parent);
34733+
34734+ /* bottom up here */
34735+ if (unlikely(err < 0))
5afbbe0d 34736+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34737+
34738+ AuDbg("b%d\n", err);
34739+ return err;
34740+}
34741+
34742+/* bottom up */
5afbbe0d 34743+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34744+{
34745+ int err;
34746+
5afbbe0d 34747+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34748+ AuDbg("b%d\n", err);
5afbbe0d 34749+ if (err > btop)
4a4d8108 34750+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34751+
34752+ AuDbg("b%d\n", err);
34753+ return err;
34754+}
34755+
076b876e
AM
34756+static int au_wbr_copyup_bu(struct dentry *dentry)
34757+{
34758+ int err;
5afbbe0d 34759+ aufs_bindex_t btop;
076b876e 34760+
5afbbe0d
AM
34761+ btop = au_dbtop(dentry);
34762+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34763+ return err;
34764+}
34765+
1facf9fc 34766+/* ---------------------------------------------------------------------- */
34767+
34768+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34769+ [AuWbrCopyup_TDP] = {
34770+ .copyup = au_wbr_copyup_tdp
34771+ },
34772+ [AuWbrCopyup_BUP] = {
34773+ .copyup = au_wbr_copyup_bup
34774+ },
34775+ [AuWbrCopyup_BU] = {
34776+ .copyup = au_wbr_copyup_bu
34777+ }
34778+};
34779+
34780+struct au_wbr_create_operations au_wbr_create_ops[] = {
34781+ [AuWbrCreate_TDP] = {
34782+ .create = au_wbr_create_tdp
34783+ },
34784+ [AuWbrCreate_RR] = {
34785+ .create = au_wbr_create_rr,
34786+ .init = au_wbr_create_init_rr
34787+ },
34788+ [AuWbrCreate_MFS] = {
34789+ .create = au_wbr_create_mfs,
34790+ .init = au_wbr_create_init_mfs,
34791+ .fin = au_wbr_create_fin_mfs
34792+ },
34793+ [AuWbrCreate_MFSV] = {
34794+ .create = au_wbr_create_mfs,
34795+ .init = au_wbr_create_init_mfs,
34796+ .fin = au_wbr_create_fin_mfs
34797+ },
34798+ [AuWbrCreate_MFSRR] = {
34799+ .create = au_wbr_create_mfsrr,
34800+ .init = au_wbr_create_init_mfsrr,
34801+ .fin = au_wbr_create_fin_mfs
34802+ },
34803+ [AuWbrCreate_MFSRRV] = {
34804+ .create = au_wbr_create_mfsrr,
34805+ .init = au_wbr_create_init_mfsrr,
34806+ .fin = au_wbr_create_fin_mfs
34807+ },
f2c43d5f
AM
34808+ [AuWbrCreate_TDMFS] = {
34809+ .create = au_wbr_create_tdmfs,
34810+ .init = au_wbr_create_init_mfs,
34811+ .fin = au_wbr_create_fin_mfs
34812+ },
34813+ [AuWbrCreate_TDMFSV] = {
34814+ .create = au_wbr_create_tdmfs,
34815+ .init = au_wbr_create_init_mfs,
34816+ .fin = au_wbr_create_fin_mfs
34817+ },
1facf9fc 34818+ [AuWbrCreate_PMFS] = {
34819+ .create = au_wbr_create_pmfs,
34820+ .init = au_wbr_create_init_mfs,
34821+ .fin = au_wbr_create_fin_mfs
34822+ },
34823+ [AuWbrCreate_PMFSV] = {
34824+ .create = au_wbr_create_pmfs,
34825+ .init = au_wbr_create_init_mfs,
34826+ .fin = au_wbr_create_fin_mfs
392086de
AM
34827+ },
34828+ [AuWbrCreate_PMFSRR] = {
34829+ .create = au_wbr_create_pmfsrr,
34830+ .init = au_wbr_create_init_mfsrr,
34831+ .fin = au_wbr_create_fin_mfs
34832+ },
34833+ [AuWbrCreate_PMFSRRV] = {
34834+ .create = au_wbr_create_pmfsrr,
34835+ .init = au_wbr_create_init_mfsrr,
34836+ .fin = au_wbr_create_fin_mfs
1facf9fc 34837+ }
34838+};
7f207e10 34839diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
eca34b5c
AM
34840--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
34841+++ linux/fs/aufs/whout.c 2019-07-11 15:42:14.475571481 +0200
062440b3 34842@@ -0,0 +1,1062 @@
cd7a4cd9 34843+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34844+/*
ba1aed25 34845+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 34846+ *
34847+ * This program, aufs is free software; you can redistribute it and/or modify
34848+ * it under the terms of the GNU General Public License as published by
34849+ * the Free Software Foundation; either version 2 of the License, or
34850+ * (at your option) any later version.
dece6358
AM
34851+ *
34852+ * This program is distributed in the hope that it will be useful,
34853+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34854+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34855+ * GNU General Public License for more details.
34856+ *
34857+ * You should have received a copy of the GNU General Public License
523b37e3 34858+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34859+ */
34860+
34861+/*
34862+ * whiteout for logical deletion and opaque directory
34863+ */
34864+
1facf9fc 34865+#include "aufs.h"
34866+
cd7a4cd9 34867+#define WH_MASK 0444
1facf9fc 34868+
34869+/*
34870+ * If a directory contains this file, then it is opaque. We start with the
34871+ * .wh. flag so that it is blocked by lookup.
34872+ */
0c3ec466
AM
34873+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34874+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34875+
34876+/*
34877+ * generate whiteout name, which is NOT terminated by NULL.
34878+ * @name: original d_name.name
34879+ * @len: original d_name.len
34880+ * @wh: whiteout qstr
34881+ * returns zero when succeeds, otherwise error.
34882+ * succeeded value as wh->name should be freed by kfree().
34883+ */
34884+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34885+{
34886+ char *p;
34887+
34888+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34889+ return -ENAMETOOLONG;
34890+
34891+ wh->len = name->len + AUFS_WH_PFX_LEN;
34892+ p = kmalloc(wh->len, GFP_NOFS);
34893+ wh->name = p;
34894+ if (p) {
34895+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34896+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34897+ /* smp_mb(); */
34898+ return 0;
34899+ }
34900+ return -ENOMEM;
34901+}
34902+
34903+/* ---------------------------------------------------------------------- */
34904+
34905+/*
34906+ * test if the @wh_name exists under @h_parent.
34907+ * @try_sio specifies the necessary of super-io.
34908+ */
076b876e 34909+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 34910+{
34911+ int err;
34912+ struct dentry *wh_dentry;
1facf9fc 34913+
1facf9fc 34914+ if (!try_sio)
b4510431 34915+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 34916+ else
076b876e 34917+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 34918+ err = PTR_ERR(wh_dentry);
2000de60
JR
34919+ if (IS_ERR(wh_dentry)) {
34920+ if (err == -ENAMETOOLONG)
34921+ err = 0;
1facf9fc 34922+ goto out;
2000de60 34923+ }
1facf9fc 34924+
34925+ err = 0;
5527c038 34926+ if (d_is_negative(wh_dentry))
1facf9fc 34927+ goto out_wh; /* success */
34928+
34929+ err = 1;
7e9cd9fe 34930+ if (d_is_reg(wh_dentry))
1facf9fc 34931+ goto out_wh; /* success */
34932+
34933+ err = -EIO;
523b37e3 34934+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 34935+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 34936+
4f0767ce 34937+out_wh:
1facf9fc 34938+ dput(wh_dentry);
4f0767ce 34939+out:
1facf9fc 34940+ return err;
34941+}
34942+
34943+/*
34944+ * test if the @h_dentry sets opaque or not.
34945+ */
076b876e 34946+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 34947+{
34948+ int err;
34949+ struct inode *h_dir;
34950+
5527c038 34951+ h_dir = d_inode(h_dentry);
076b876e 34952+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 34953+ au_test_h_perm_sio(h_dir, MAY_EXEC));
34954+ return err;
34955+}
34956+
34957+/*
34958+ * returns a negative dentry whose name is unique and temporary.
34959+ */
34960+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34961+ struct qstr *prefix)
34962+{
1facf9fc 34963+ struct dentry *dentry;
34964+ int i;
027c5e7a 34965+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 34966+ *name, *p;
027c5e7a 34967+ /* strict atomic_t is unnecessary here */
1facf9fc 34968+ static unsigned short cnt;
34969+ struct qstr qs;
34970+
4a4d8108
AM
34971+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34972+
1facf9fc 34973+ name = defname;
027c5e7a
AM
34974+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34975+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 34976+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 34977+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 34978+ goto out;
34979+ dentry = ERR_PTR(-ENOMEM);
34980+ name = kmalloc(qs.len + 1, GFP_NOFS);
34981+ if (unlikely(!name))
34982+ goto out;
34983+ }
34984+
34985+ /* doubly whiteout-ed */
34986+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34987+ p = name + AUFS_WH_PFX_LEN * 2;
34988+ memcpy(p, prefix->name, prefix->len);
34989+ p += prefix->len;
34990+ *p++ = '.';
4a4d8108 34991+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 34992+
34993+ qs.name = name;
34994+ for (i = 0; i < 3; i++) {
b752ccd1 34995+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 34996+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 34997+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 34998+ goto out_name;
34999+ dput(dentry);
35000+ }
0c3ec466 35001+ /* pr_warn("could not get random name\n"); */
1facf9fc 35002+ dentry = ERR_PTR(-EEXIST);
35003+ AuDbg("%.*s\n", AuLNPair(&qs));
35004+ BUG();
35005+
4f0767ce 35006+out_name:
1facf9fc 35007+ if (name != defname)
9f237c51 35008+ au_kfree_try_rcu(name);
4f0767ce 35009+out:
4a4d8108 35010+ AuTraceErrPtr(dentry);
1facf9fc 35011+ return dentry;
1facf9fc 35012+}
35013+
35014+/*
35015+ * rename the @h_dentry on @br to the whiteouted temporary name.
35016+ */
35017+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35018+{
35019+ int err;
35020+ struct path h_path = {
86dc4139 35021+ .mnt = au_br_mnt(br)
1facf9fc 35022+ };
523b37e3 35023+ struct inode *h_dir, *delegated;
1facf9fc 35024+ struct dentry *h_parent;
35025+
35026+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 35027+ h_dir = d_inode(h_parent);
1facf9fc 35028+ IMustLock(h_dir);
35029+
35030+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35031+ err = PTR_ERR(h_path.dentry);
35032+ if (IS_ERR(h_path.dentry))
35033+ goto out;
35034+
35035+ /* under the same dir, no need to lock_rename() */
523b37e3 35036+ delegated = NULL;
f2c43d5f
AM
35037+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35038+ /*flags*/0);
1facf9fc 35039+ AuTraceErr(err);
523b37e3
AM
35040+ if (unlikely(err == -EWOULDBLOCK)) {
35041+ pr_warn("cannot retry for NFSv4 delegation"
35042+ " for an internal rename\n");
35043+ iput(delegated);
35044+ }
1facf9fc 35045+ dput(h_path.dentry);
35046+
4f0767ce 35047+out:
4a4d8108 35048+ AuTraceErr(err);
1facf9fc 35049+ return err;
35050+}
35051+
35052+/* ---------------------------------------------------------------------- */
35053+/*
35054+ * functions for removing a whiteout
35055+ */
35056+
35057+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35058+{
523b37e3
AM
35059+ int err, force;
35060+ struct inode *delegated;
1facf9fc 35061+
35062+ /*
35063+ * forces superio when the dir has a sticky bit.
35064+ * this may be a violation of unix fs semantics.
35065+ */
35066+ force = (h_dir->i_mode & S_ISVTX)
5527c038 35067+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
35068+ delegated = NULL;
35069+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
35070+ if (unlikely(err == -EWOULDBLOCK)) {
35071+ pr_warn("cannot retry for NFSv4 delegation"
35072+ " for an internal unlink\n");
35073+ iput(delegated);
35074+ }
35075+ return err;
1facf9fc 35076+}
35077+
35078+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35079+ struct dentry *dentry)
35080+{
35081+ int err;
35082+
35083+ err = do_unlink_wh(h_dir, h_path);
35084+ if (!err && dentry)
35085+ au_set_dbwh(dentry, -1);
35086+
35087+ return err;
35088+}
35089+
35090+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35091+ struct au_branch *br)
35092+{
35093+ int err;
35094+ struct path h_path = {
86dc4139 35095+ .mnt = au_br_mnt(br)
1facf9fc 35096+ };
35097+
35098+ err = 0;
b4510431 35099+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 35100+ if (IS_ERR(h_path.dentry))
35101+ err = PTR_ERR(h_path.dentry);
35102+ else {
5527c038
JR
35103+ if (d_is_reg(h_path.dentry))
35104+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 35105+ dput(h_path.dentry);
35106+ }
35107+
35108+ return err;
35109+}
35110+
35111+/* ---------------------------------------------------------------------- */
35112+/*
35113+ * initialize/clean whiteout for a branch
35114+ */
35115+
35116+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35117+ const int isdir)
35118+{
35119+ int err;
523b37e3 35120+ struct inode *delegated;
1facf9fc 35121+
5527c038 35122+ if (d_is_negative(whpath->dentry))
1facf9fc 35123+ return;
35124+
86dc4139
AM
35125+ if (isdir)
35126+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
35127+ else {
35128+ delegated = NULL;
35129+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35130+ if (unlikely(err == -EWOULDBLOCK)) {
35131+ pr_warn("cannot retry for NFSv4 delegation"
35132+ " for an internal unlink\n");
35133+ iput(delegated);
35134+ }
35135+ }
1facf9fc 35136+ if (unlikely(err))
523b37e3
AM
35137+ pr_warn("failed removing %pd (%d), ignored.\n",
35138+ whpath->dentry, err);
1facf9fc 35139+}
35140+
35141+static int test_linkable(struct dentry *h_root)
35142+{
5527c038 35143+ struct inode *h_dir = d_inode(h_root);
1facf9fc 35144+
35145+ if (h_dir->i_op->link)
35146+ return 0;
35147+
523b37e3
AM
35148+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35149+ h_root, au_sbtype(h_root->d_sb));
1facf9fc 35150+ return -ENOSYS;
35151+}
35152+
35153+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35154+static int au_whdir(struct inode *h_dir, struct path *path)
35155+{
35156+ int err;
35157+
35158+ err = -EEXIST;
5527c038 35159+ if (d_is_negative(path->dentry)) {
cd7a4cd9 35160+ int mode = 0700;
1facf9fc 35161+
35162+ if (au_test_nfs(path->dentry->d_sb))
cd7a4cd9 35163+ mode |= 0111;
86dc4139 35164+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 35165+ } else if (d_is_dir(path->dentry))
1facf9fc 35166+ err = 0;
35167+ else
523b37e3 35168+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 35169+
35170+ return err;
35171+}
35172+
35173+struct au_wh_base {
35174+ const struct qstr *name;
35175+ struct dentry *dentry;
35176+};
35177+
35178+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35179+ struct path *h_path)
35180+{
35181+ h_path->dentry = base[AuBrWh_BASE].dentry;
35182+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35183+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35184+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35185+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35186+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35187+}
35188+
35189+/*
35190+ * returns tri-state,
c1595e42 35191+ * minus: error, caller should print the message
acd2b654 35192+ * zero: success
c1595e42 35193+ * plus: error, caller should NOT print the message
1facf9fc 35194+ */
35195+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35196+ int do_plink, struct au_wh_base base[],
35197+ struct path *h_path)
35198+{
35199+ int err;
35200+ struct inode *h_dir;
35201+
5527c038 35202+ h_dir = d_inode(h_root);
1facf9fc 35203+ h_path->dentry = base[AuBrWh_BASE].dentry;
35204+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35205+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35206+ if (do_plink) {
35207+ err = test_linkable(h_root);
35208+ if (unlikely(err)) {
35209+ err = 1;
35210+ goto out;
35211+ }
35212+
35213+ err = au_whdir(h_dir, h_path);
35214+ if (unlikely(err))
35215+ goto out;
35216+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35217+ } else
35218+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35219+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35220+ err = au_whdir(h_dir, h_path);
35221+ if (unlikely(err))
35222+ goto out;
35223+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35224+
4f0767ce 35225+out:
1facf9fc 35226+ return err;
35227+}
35228+
35229+/*
35230+ * for the moment, aufs supports the branch filesystem which does not support
35231+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35232+ * copyup failed. finally, such filesystem will not be used as the writable
35233+ * branch.
35234+ *
35235+ * returns tri-state, see above.
35236+ */
35237+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35238+ int do_plink, struct au_wh_base base[],
35239+ struct path *h_path)
35240+{
35241+ int err;
35242+ struct inode *h_dir;
35243+
1308ab2a 35244+ WbrWhMustWriteLock(wbr);
35245+
1facf9fc 35246+ err = test_linkable(h_root);
35247+ if (unlikely(err)) {
35248+ err = 1;
35249+ goto out;
35250+ }
35251+
35252+ /*
35253+ * todo: should this create be done in /sbin/mount.aufs helper?
35254+ */
35255+ err = -EEXIST;
5527c038
JR
35256+ h_dir = d_inode(h_root);
35257+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
35258+ h_path->dentry = base[AuBrWh_BASE].dentry;
35259+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 35260+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 35261+ err = 0;
35262+ else
523b37e3 35263+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 35264+ if (unlikely(err))
35265+ goto out;
35266+
35267+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35268+ if (do_plink) {
35269+ err = au_whdir(h_dir, h_path);
35270+ if (unlikely(err))
35271+ goto out;
35272+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35273+ } else
35274+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35275+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35276+
35277+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35278+ err = au_whdir(h_dir, h_path);
35279+ if (unlikely(err))
35280+ goto out;
35281+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35282+
4f0767ce 35283+out:
1facf9fc 35284+ return err;
35285+}
35286+
35287+/*
35288+ * initialize the whiteout base file/dir for @br.
35289+ */
86dc4139 35290+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 35291+{
35292+ int err, i;
35293+ const unsigned char do_plink
35294+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 35295+ struct inode *h_dir;
86dc4139
AM
35296+ struct path path = br->br_path;
35297+ struct dentry *h_root = path.dentry;
1facf9fc 35298+ struct au_wbr *wbr = br->br_wbr;
35299+ static const struct qstr base_name[] = {
0c3ec466
AM
35300+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35301+ sizeof(AUFS_BASE_NAME) - 1),
35302+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35303+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35304+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35305+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 35306+ };
35307+ struct au_wh_base base[] = {
35308+ [AuBrWh_BASE] = {
35309+ .name = base_name + AuBrWh_BASE,
35310+ .dentry = NULL
35311+ },
35312+ [AuBrWh_PLINK] = {
35313+ .name = base_name + AuBrWh_PLINK,
35314+ .dentry = NULL
35315+ },
35316+ [AuBrWh_ORPH] = {
35317+ .name = base_name + AuBrWh_ORPH,
35318+ .dentry = NULL
35319+ }
35320+ };
35321+
1308ab2a 35322+ if (wbr)
35323+ WbrWhMustWriteLock(wbr);
1facf9fc 35324+
1facf9fc 35325+ for (i = 0; i < AuBrWh_Last; i++) {
35326+ /* doubly whiteouted */
35327+ struct dentry *d;
35328+
35329+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35330+ err = PTR_ERR(d);
35331+ if (IS_ERR(d))
35332+ goto out;
35333+
35334+ base[i].dentry = d;
35335+ AuDebugOn(wbr
35336+ && wbr->wbr_wh[i]
35337+ && wbr->wbr_wh[i] != base[i].dentry);
35338+ }
35339+
35340+ if (wbr)
35341+ for (i = 0; i < AuBrWh_Last; i++) {
35342+ dput(wbr->wbr_wh[i]);
35343+ wbr->wbr_wh[i] = NULL;
35344+ }
35345+
35346+ err = 0;
1e00d052 35347+ if (!au_br_writable(br->br_perm)) {
5527c038 35348+ h_dir = d_inode(h_root);
1facf9fc 35349+ au_wh_init_ro(h_dir, base, &path);
1e00d052 35350+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 35351+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35352+ if (err > 0)
35353+ goto out;
35354+ else if (err)
35355+ goto out_err;
1e00d052 35356+ } else {
1facf9fc 35357+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35358+ if (err > 0)
35359+ goto out;
35360+ else if (err)
35361+ goto out_err;
1facf9fc 35362+ }
35363+ goto out; /* success */
35364+
4f0767ce 35365+out_err:
523b37e3
AM
35366+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35367+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35368+out:
1facf9fc 35369+ for (i = 0; i < AuBrWh_Last; i++)
35370+ dput(base[i].dentry);
35371+ return err;
35372+}
35373+
35374+/* ---------------------------------------------------------------------- */
35375+/*
35376+ * whiteouts are all hard-linked usually.
35377+ * when its link count reaches a ceiling, we create a new whiteout base
35378+ * asynchronously.
35379+ */
35380+
35381+struct reinit_br_wh {
35382+ struct super_block *sb;
35383+ struct au_branch *br;
35384+};
35385+
35386+static void reinit_br_wh(void *arg)
35387+{
35388+ int err;
35389+ aufs_bindex_t bindex;
35390+ struct path h_path;
35391+ struct reinit_br_wh *a = arg;
35392+ struct au_wbr *wbr;
523b37e3 35393+ struct inode *dir, *delegated;
1facf9fc 35394+ struct dentry *h_root;
35395+ struct au_hinode *hdir;
35396+
35397+ err = 0;
35398+ wbr = a->br->br_wbr;
35399+ /* big aufs lock */
35400+ si_noflush_write_lock(a->sb);
35401+ if (!au_br_writable(a->br->br_perm))
35402+ goto out;
35403+ bindex = au_br_index(a->sb, a->br->br_id);
35404+ if (unlikely(bindex < 0))
35405+ goto out;
35406+
1308ab2a 35407+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35408+ dir = d_inode(a->sb->s_root);
1facf9fc 35409+ hdir = au_hi(dir, bindex);
35410+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35411+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35412+
5afbbe0d 35413+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35414+ wbr_wh_write_lock(wbr);
35415+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35416+ h_root, a->br);
35417+ if (!err) {
86dc4139
AM
35418+ h_path.dentry = wbr->wbr_whbase;
35419+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35420+ delegated = NULL;
35421+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35422+ /*force*/0);
35423+ if (unlikely(err == -EWOULDBLOCK)) {
35424+ pr_warn("cannot retry for NFSv4 delegation"
35425+ " for an internal unlink\n");
35426+ iput(delegated);
35427+ }
1facf9fc 35428+ } else {
523b37e3 35429+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35430+ err = 0;
35431+ }
35432+ dput(wbr->wbr_whbase);
35433+ wbr->wbr_whbase = NULL;
35434+ if (!err)
86dc4139 35435+ err = au_wh_init(a->br, a->sb);
1facf9fc 35436+ wbr_wh_write_unlock(wbr);
5afbbe0d 35437+ au_hn_inode_unlock(hdir);
1308ab2a 35438+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35439+ if (!err)
35440+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35441+
4f0767ce 35442+out:
1facf9fc 35443+ if (wbr)
35444+ atomic_dec(&wbr->wbr_wh_running);
acd2b654 35445+ au_lcnt_dec(&a->br->br_count);
1facf9fc 35446+ si_write_unlock(a->sb);
027c5e7a 35447+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
9f237c51 35448+ au_kfree_rcu(a);
1facf9fc 35449+ if (unlikely(err))
35450+ AuIOErr("err %d\n", err);
35451+}
35452+
35453+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35454+{
35455+ int do_dec, wkq_err;
35456+ struct reinit_br_wh *arg;
35457+
35458+ do_dec = 1;
35459+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35460+ goto out;
35461+
35462+ /* ignore ENOMEM */
35463+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35464+ if (arg) {
35465+ /*
35466+ * dec(wh_running), kfree(arg) and dec(br_count)
35467+ * in reinit function
35468+ */
35469+ arg->sb = sb;
35470+ arg->br = br;
acd2b654 35471+ au_lcnt_inc(&br->br_count);
53392da6 35472+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35473+ if (unlikely(wkq_err)) {
35474+ atomic_dec(&br->br_wbr->wbr_wh_running);
acd2b654 35475+ au_lcnt_dec(&br->br_count);
9f237c51 35476+ au_kfree_rcu(arg);
1facf9fc 35477+ }
35478+ do_dec = 0;
35479+ }
35480+
4f0767ce 35481+out:
1facf9fc 35482+ if (do_dec)
35483+ atomic_dec(&br->br_wbr->wbr_wh_running);
35484+}
35485+
35486+/* ---------------------------------------------------------------------- */
35487+
35488+/*
35489+ * create the whiteout @wh.
35490+ */
35491+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35492+ struct dentry *wh)
35493+{
35494+ int err;
35495+ struct path h_path = {
35496+ .dentry = wh
35497+ };
35498+ struct au_branch *br;
35499+ struct au_wbr *wbr;
35500+ struct dentry *h_parent;
523b37e3 35501+ struct inode *h_dir, *delegated;
1facf9fc 35502+
35503+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35504+ h_dir = d_inode(h_parent);
1facf9fc 35505+ IMustLock(h_dir);
35506+
35507+ br = au_sbr(sb, bindex);
86dc4139 35508+ h_path.mnt = au_br_mnt(br);
1facf9fc 35509+ wbr = br->br_wbr;
35510+ wbr_wh_read_lock(wbr);
35511+ if (wbr->wbr_whbase) {
523b37e3
AM
35512+ delegated = NULL;
35513+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35514+ if (unlikely(err == -EWOULDBLOCK)) {
35515+ pr_warn("cannot retry for NFSv4 delegation"
35516+ " for an internal link\n");
35517+ iput(delegated);
35518+ }
1facf9fc 35519+ if (!err || err != -EMLINK)
35520+ goto out;
35521+
35522+ /* link count full. re-initialize br_whbase. */
35523+ kick_reinit_br_wh(sb, br);
35524+ }
35525+
35526+ /* return this error in this context */
b4510431 35527+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35528+ if (!err)
35529+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35530+
4f0767ce 35531+out:
1facf9fc 35532+ wbr_wh_read_unlock(wbr);
35533+ return err;
35534+}
35535+
35536+/* ---------------------------------------------------------------------- */
35537+
35538+/*
35539+ * create or remove the diropq.
35540+ */
35541+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35542+ unsigned int flags)
35543+{
35544+ struct dentry *opq_dentry, *h_dentry;
35545+ struct super_block *sb;
35546+ struct au_branch *br;
35547+ int err;
35548+
35549+ sb = dentry->d_sb;
35550+ br = au_sbr(sb, bindex);
35551+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35552+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35553+ if (IS_ERR(opq_dentry))
35554+ goto out;
35555+
35556+ if (au_ftest_diropq(flags, CREATE)) {
35557+ err = link_or_create_wh(sb, bindex, opq_dentry);
35558+ if (!err) {
35559+ au_set_dbdiropq(dentry, bindex);
35560+ goto out; /* success */
35561+ }
35562+ } else {
35563+ struct path tmp = {
35564+ .dentry = opq_dentry,
86dc4139 35565+ .mnt = au_br_mnt(br)
1facf9fc 35566+ };
5527c038 35567+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35568+ if (!err)
35569+ au_set_dbdiropq(dentry, -1);
35570+ }
35571+ dput(opq_dentry);
35572+ opq_dentry = ERR_PTR(err);
35573+
4f0767ce 35574+out:
1facf9fc 35575+ return opq_dentry;
35576+}
35577+
35578+struct do_diropq_args {
35579+ struct dentry **errp;
35580+ struct dentry *dentry;
35581+ aufs_bindex_t bindex;
35582+ unsigned int flags;
35583+};
35584+
35585+static void call_do_diropq(void *args)
35586+{
35587+ struct do_diropq_args *a = args;
35588+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35589+}
35590+
35591+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35592+ unsigned int flags)
35593+{
35594+ struct dentry *diropq, *h_dentry;
35595+
35596+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 35597+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 35598+ diropq = do_diropq(dentry, bindex, flags);
35599+ else {
35600+ int wkq_err;
35601+ struct do_diropq_args args = {
35602+ .errp = &diropq,
35603+ .dentry = dentry,
35604+ .bindex = bindex,
35605+ .flags = flags
35606+ };
35607+
35608+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35609+ if (unlikely(wkq_err))
35610+ diropq = ERR_PTR(wkq_err);
35611+ }
35612+
35613+ return diropq;
35614+}
35615+
35616+/* ---------------------------------------------------------------------- */
35617+
35618+/*
35619+ * lookup whiteout dentry.
35620+ * @h_parent: lower parent dentry which must exist and be locked
35621+ * @base_name: name of dentry which will be whiteouted
35622+ * returns dentry for whiteout.
35623+ */
35624+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35625+ struct au_branch *br)
35626+{
35627+ int err;
35628+ struct qstr wh_name;
35629+ struct dentry *wh_dentry;
35630+
35631+ err = au_wh_name_alloc(&wh_name, base_name);
35632+ wh_dentry = ERR_PTR(err);
35633+ if (!err) {
b4510431 35634+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
9f237c51 35635+ au_kfree_try_rcu(wh_name.name);
1facf9fc 35636+ }
35637+ return wh_dentry;
35638+}
35639+
35640+/*
35641+ * link/create a whiteout for @dentry on @bindex.
35642+ */
35643+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35644+ struct dentry *h_parent)
35645+{
35646+ struct dentry *wh_dentry;
35647+ struct super_block *sb;
35648+ int err;
35649+
35650+ sb = dentry->d_sb;
35651+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35652+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35653+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35654+ if (!err) {
1facf9fc 35655+ au_set_dbwh(dentry, bindex);
076b876e
AM
35656+ au_fhsm_wrote(sb, bindex, /*force*/0);
35657+ } else {
1facf9fc 35658+ dput(wh_dentry);
35659+ wh_dentry = ERR_PTR(err);
35660+ }
35661+ }
35662+
35663+ return wh_dentry;
35664+}
35665+
35666+/* ---------------------------------------------------------------------- */
35667+
35668+/* Delete all whiteouts in this directory on branch bindex. */
35669+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35670+ aufs_bindex_t bindex, struct au_branch *br)
35671+{
35672+ int err;
35673+ unsigned long ul, n;
35674+ struct qstr wh_name;
35675+ char *p;
35676+ struct hlist_head *head;
c06a8ce3 35677+ struct au_vdir_wh *pos;
1facf9fc 35678+ struct au_vdir_destr *str;
35679+
35680+ err = -ENOMEM;
537831f9 35681+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35682+ wh_name.name = p;
35683+ if (unlikely(!wh_name.name))
35684+ goto out;
35685+
35686+ err = 0;
35687+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35688+ p += AUFS_WH_PFX_LEN;
35689+ n = whlist->nh_num;
35690+ head = whlist->nh_head;
35691+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35692+ hlist_for_each_entry(pos, head, wh_hash) {
35693+ if (pos->wh_bindex != bindex)
1facf9fc 35694+ continue;
35695+
c06a8ce3 35696+ str = &pos->wh_str;
1facf9fc 35697+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35698+ memcpy(p, str->name, str->len);
35699+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35700+ err = unlink_wh_name(h_dentry, &wh_name, br);
35701+ if (!err)
35702+ continue;
35703+ break;
35704+ }
35705+ AuIOErr("whiteout name too long %.*s\n",
35706+ str->len, str->name);
35707+ err = -EIO;
35708+ break;
35709+ }
35710+ }
1c60b727 35711+ free_page((unsigned long)wh_name.name);
1facf9fc 35712+
4f0767ce 35713+out:
1facf9fc 35714+ return err;
35715+}
35716+
35717+struct del_wh_children_args {
35718+ int *errp;
35719+ struct dentry *h_dentry;
1308ab2a 35720+ struct au_nhash *whlist;
1facf9fc 35721+ aufs_bindex_t bindex;
35722+ struct au_branch *br;
35723+};
35724+
35725+static void call_del_wh_children(void *args)
35726+{
35727+ struct del_wh_children_args *a = args;
1308ab2a 35728+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35729+}
35730+
35731+/* ---------------------------------------------------------------------- */
35732+
35733+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35734+{
35735+ struct au_whtmp_rmdir *whtmp;
dece6358 35736+ int err;
1308ab2a 35737+ unsigned int rdhash;
dece6358
AM
35738+
35739+ SiMustAnyLock(sb);
1facf9fc 35740+
be52b249 35741+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35742+ if (unlikely(!whtmp)) {
35743+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35744+ goto out;
dece6358 35745+ }
1facf9fc 35746+
1308ab2a 35747+ /* no estimation for dir size */
35748+ rdhash = au_sbi(sb)->si_rdhash;
35749+ if (!rdhash)
35750+ rdhash = AUFS_RDHASH_DEF;
35751+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35752+ if (unlikely(err)) {
9f237c51 35753+ au_kfree_rcu(whtmp);
1308ab2a 35754+ whtmp = ERR_PTR(err);
35755+ }
dece6358 35756+
4f0767ce 35757+out:
dece6358 35758+ return whtmp;
1facf9fc 35759+}
35760+
35761+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35762+{
027c5e7a 35763+ if (whtmp->br)
acd2b654 35764+ au_lcnt_dec(&whtmp->br->br_count);
1facf9fc 35765+ dput(whtmp->wh_dentry);
35766+ iput(whtmp->dir);
dece6358 35767+ au_nhash_wh_free(&whtmp->whlist);
9f237c51 35768+ au_kfree_rcu(whtmp);
1facf9fc 35769+}
35770+
35771+/*
35772+ * rmdir the whiteouted temporary named dir @h_dentry.
35773+ * @whlist: whiteouted children.
35774+ */
35775+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35776+ struct dentry *wh_dentry, struct au_nhash *whlist)
35777+{
35778+ int err;
2000de60 35779+ unsigned int h_nlink;
1facf9fc 35780+ struct path h_tmp;
35781+ struct inode *wh_inode, *h_dir;
35782+ struct au_branch *br;
35783+
5527c038 35784+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35785+ IMustLock(h_dir);
35786+
35787+ br = au_sbr(dir->i_sb, bindex);
5527c038 35788+ wh_inode = d_inode(wh_dentry);
febd17d6 35789+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35790+
35791+ /*
35792+ * someone else might change some whiteouts while we were sleeping.
35793+ * it means this whlist may have an obsoleted entry.
35794+ */
35795+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35796+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35797+ else {
35798+ int wkq_err;
35799+ struct del_wh_children_args args = {
35800+ .errp = &err,
35801+ .h_dentry = wh_dentry,
1308ab2a 35802+ .whlist = whlist,
1facf9fc 35803+ .bindex = bindex,
35804+ .br = br
35805+ };
35806+
35807+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35808+ if (unlikely(wkq_err))
35809+ err = wkq_err;
35810+ }
febd17d6 35811+ inode_unlock(wh_inode);
1facf9fc 35812+
35813+ if (!err) {
35814+ h_tmp.dentry = wh_dentry;
86dc4139 35815+ h_tmp.mnt = au_br_mnt(br);
2000de60 35816+ h_nlink = h_dir->i_nlink;
1facf9fc 35817+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35818+ /* some fs doesn't change the parent nlink in some cases */
35819+ h_nlink -= h_dir->i_nlink;
1facf9fc 35820+ }
35821+
35822+ if (!err) {
5afbbe0d 35823+ if (au_ibtop(dir) == bindex) {
7f207e10 35824+ /* todo: dir->i_mutex is necessary */
1facf9fc 35825+ au_cpup_attr_timesizes(dir);
2000de60
JR
35826+ if (h_nlink)
35827+ vfsub_drop_nlink(dir);
1facf9fc 35828+ }
35829+ return 0; /* success */
35830+ }
35831+
523b37e3 35832+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35833+ return err;
35834+}
35835+
35836+static void call_rmdir_whtmp(void *args)
35837+{
35838+ int err;
e49829fe 35839+ aufs_bindex_t bindex;
1facf9fc 35840+ struct au_whtmp_rmdir *a = args;
35841+ struct super_block *sb;
35842+ struct dentry *h_parent;
35843+ struct inode *h_dir;
1facf9fc 35844+ struct au_hinode *hdir;
35845+
35846+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35847+ /* inode_lock(a->dir); */
e49829fe 35848+ err = -EROFS;
1facf9fc 35849+ sb = a->dir->i_sb;
e49829fe
JR
35850+ si_read_lock(sb, !AuLock_FLUSH);
35851+ if (!au_br_writable(a->br->br_perm))
35852+ goto out;
35853+ bindex = au_br_index(sb, a->br->br_id);
35854+ if (unlikely(bindex < 0))
1facf9fc 35855+ goto out;
35856+
35857+ err = -EIO;
1facf9fc 35858+ ii_write_lock_parent(a->dir);
35859+ h_parent = dget_parent(a->wh_dentry);
5527c038 35860+ h_dir = d_inode(h_parent);
e49829fe 35861+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35862+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35863+ if (unlikely(err))
35864+ goto out_mnt;
5afbbe0d 35865+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35866+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35867+ a->br);
86dc4139
AM
35868+ if (!err)
35869+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35870+ au_hn_inode_unlock(hdir);
86dc4139
AM
35871+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35872+
35873+out_mnt:
1facf9fc 35874+ dput(h_parent);
35875+ ii_write_unlock(a->dir);
4f0767ce 35876+out:
febd17d6 35877+ /* inode_unlock(a->dir); */
1facf9fc 35878+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35879+ si_read_unlock(sb);
35880+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35881+ if (unlikely(err))
35882+ AuIOErr("err %d\n", err);
35883+}
35884+
35885+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35886+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35887+{
35888+ int wkq_err;
e49829fe 35889+ struct super_block *sb;
1facf9fc 35890+
35891+ IMustLock(dir);
35892+
35893+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35894+ sb = dir->i_sb;
1facf9fc 35895+ args->dir = au_igrab(dir);
e49829fe 35896+ args->br = au_sbr(sb, bindex);
acd2b654 35897+ au_lcnt_inc(&args->br->br_count);
1facf9fc 35898+ args->wh_dentry = dget(wh_dentry);
53392da6 35899+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35900+ if (unlikely(wkq_err)) {
523b37e3 35901+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35902+ au_whtmp_rmdir_free(args);
35903+ }
35904+}
7f207e10 35905diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
eca34b5c
AM
35906--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
35907+++ linux/fs/aufs/whout.h 2019-07-11 15:42:14.475571481 +0200
062440b3
AM
35908@@ -0,0 +1,86 @@
35909+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 35910+/*
ba1aed25 35911+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 35912+ *
35913+ * This program, aufs is free software; you can redistribute it and/or modify
35914+ * it under the terms of the GNU General Public License as published by
35915+ * the Free Software Foundation; either version 2 of the License, or
35916+ * (at your option) any later version.
dece6358
AM
35917+ *
35918+ * This program is distributed in the hope that it will be useful,
35919+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35920+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35921+ * GNU General Public License for more details.
35922+ *
35923+ * You should have received a copy of the GNU General Public License
523b37e3 35924+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35925+ */
35926+
35927+/*
35928+ * whiteout for logical deletion and opaque directory
35929+ */
35930+
35931+#ifndef __AUFS_WHOUT_H__
35932+#define __AUFS_WHOUT_H__
35933+
35934+#ifdef __KERNEL__
35935+
1facf9fc 35936+#include "dir.h"
35937+
35938+/* whout.c */
35939+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
35940+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35941+int au_diropq_test(struct dentry *h_dentry);
3c1bdaff 35942+struct au_branch;
1facf9fc 35943+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35944+ struct qstr *prefix);
35945+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35946+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35947+ struct dentry *dentry);
86dc4139 35948+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 35949+
35950+/* diropq flags */
35951+#define AuDiropq_CREATE 1
35952+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
35953+#define au_fset_diropq(flags, name) \
35954+ do { (flags) |= AuDiropq_##name; } while (0)
35955+#define au_fclr_diropq(flags, name) \
35956+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 35957+
35958+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35959+ unsigned int flags);
35960+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35961+ struct au_branch *br);
35962+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35963+ struct dentry *h_parent);
35964+
35965+/* real rmdir for the whiteout-ed dir */
35966+struct au_whtmp_rmdir {
35967+ struct inode *dir;
e49829fe 35968+ struct au_branch *br;
1facf9fc 35969+ struct dentry *wh_dentry;
dece6358 35970+ struct au_nhash whlist;
1facf9fc 35971+};
35972+
35973+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35974+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35975+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35976+ struct dentry *wh_dentry, struct au_nhash *whlist);
35977+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35978+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35979+
35980+/* ---------------------------------------------------------------------- */
35981+
35982+static inline struct dentry *au_diropq_create(struct dentry *dentry,
35983+ aufs_bindex_t bindex)
35984+{
35985+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35986+}
35987+
35988+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35989+{
35990+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35991+}
35992+
35993+#endif /* __KERNEL__ */
35994+#endif /* __AUFS_WHOUT_H__ */
7f207e10 35995diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
eca34b5c
AM
35996--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
35997+++ linux/fs/aufs/wkq.c 2019-07-11 15:42:14.475571481 +0200
acd2b654 35998@@ -0,0 +1,392 @@
cd7a4cd9 35999+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36000+/*
ba1aed25 36001+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 36002+ *
36003+ * This program, aufs is free software; you can redistribute it and/or modify
36004+ * it under the terms of the GNU General Public License as published by
36005+ * the Free Software Foundation; either version 2 of the License, or
36006+ * (at your option) any later version.
dece6358
AM
36007+ *
36008+ * This program is distributed in the hope that it will be useful,
36009+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36010+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36011+ * GNU General Public License for more details.
36012+ *
36013+ * You should have received a copy of the GNU General Public License
523b37e3 36014+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36015+ */
36016+
36017+/*
36018+ * workqueue for asynchronous/super-io operations
acd2b654 36019+ * todo: try new credential scheme
1facf9fc 36020+ */
36021+
dece6358 36022+#include <linux/module.h>
1facf9fc 36023+#include "aufs.h"
36024+
9dbd164d 36025+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 36026+
9dbd164d 36027+static struct workqueue_struct *au_wkq;
1facf9fc 36028+
36029+struct au_wkinfo {
36030+ struct work_struct wk;
7f207e10 36031+ struct kobject *kobj;
1facf9fc 36032+
36033+ unsigned int flags; /* see wkq.h */
36034+
36035+ au_wkq_func_t func;
36036+ void *args;
36037+
8b6a4947
AM
36038+#ifdef CONFIG_LOCKDEP
36039+ int dont_check;
36040+ struct held_lock **hlock;
36041+#endif
36042+
1facf9fc 36043+ struct completion *comp;
36044+};
36045+
36046+/* ---------------------------------------------------------------------- */
8b6a4947
AM
36047+/*
36048+ * Aufs passes some operations to the workqueue such as the internal copyup.
36049+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36050+ * job run by workqueue depends upon the locks acquired in the other task.
36051+ * Delegating a small operation to the workqueue, aufs passes its lockdep
36052+ * information too. And the job in the workqueue restores the info in order to
36053+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
36054+ * correctly and expectedly.
36055+ */
36056+
36057+#ifndef CONFIG_LOCKDEP
36058+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36059+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36060+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36061+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36062+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36063+#else
36064+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36065+{
36066+ wkinfo->hlock = NULL;
36067+ wkinfo->dont_check = 0;
36068+}
36069+
36070+/*
36071+ * 1: matched
36072+ * 0: unmatched
36073+ */
36074+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36075+{
36076+ static DEFINE_SPINLOCK(spin);
36077+ static struct {
36078+ char *name;
36079+ struct lock_class_key *key;
36080+ } a[] = {
36081+ { .name = "&sbinfo->si_rwsem" },
36082+ { .name = "&finfo->fi_rwsem" },
36083+ { .name = "&dinfo->di_rwsem" },
36084+ { .name = "&iinfo->ii_rwsem" }
36085+ };
36086+ static int set;
36087+ int i;
36088+
36089+ /* lockless read from 'set.' see below */
36090+ if (set == ARRAY_SIZE(a)) {
36091+ for (i = 0; i < ARRAY_SIZE(a); i++)
36092+ if (a[i].key == key)
36093+ goto match;
36094+ goto unmatch;
36095+ }
36096+
36097+ spin_lock(&spin);
36098+ if (set)
36099+ for (i = 0; i < ARRAY_SIZE(a); i++)
36100+ if (a[i].key == key) {
36101+ spin_unlock(&spin);
36102+ goto match;
36103+ }
36104+ for (i = 0; i < ARRAY_SIZE(a); i++) {
36105+ if (a[i].key) {
36106+ if (unlikely(a[i].key == key)) { /* rare but possible */
36107+ spin_unlock(&spin);
36108+ goto match;
36109+ } else
36110+ continue;
36111+ }
36112+ if (strstr(a[i].name, name)) {
36113+ /*
36114+ * the order of these three lines is important for the
36115+ * lockless read above.
36116+ */
36117+ a[i].key = key;
36118+ spin_unlock(&spin);
36119+ set++;
36120+ /* AuDbg("%d, %s\n", set, name); */
36121+ goto match;
36122+ }
36123+ }
36124+ spin_unlock(&spin);
36125+ goto unmatch;
36126+
36127+match:
36128+ return 1;
36129+unmatch:
36130+ return 0;
36131+}
36132+
36133+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36134+{
36135+ int err, n;
36136+ struct task_struct *curr;
36137+ struct held_lock **hl, *held_locks, *p;
36138+
36139+ err = 0;
36140+ curr = current;
36141+ wkinfo->dont_check = lockdep_recursing(curr);
36142+ if (wkinfo->dont_check)
36143+ goto out;
36144+ n = curr->lockdep_depth;
36145+ if (!n)
36146+ goto out;
36147+
36148+ err = -ENOMEM;
36149+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36150+ if (unlikely(!wkinfo->hlock))
36151+ goto out;
36152+
36153+ err = 0;
36154+#if 0
36155+ if (0 && au_debug_test()) /* left for debugging */
36156+ lockdep_print_held_locks(curr);
36157+#endif
36158+ held_locks = curr->held_locks;
36159+ hl = wkinfo->hlock;
36160+ while (n--) {
36161+ p = held_locks++;
36162+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36163+ *hl++ = p;
36164+ }
36165+ *hl = NULL;
36166+
36167+out:
36168+ return err;
36169+}
36170+
36171+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36172+{
9f237c51 36173+ au_kfree_try_rcu(wkinfo->hlock);
8b6a4947
AM
36174+}
36175+
36176+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36177+{
36178+ struct held_lock *p, **hl = wkinfo->hlock;
36179+ int subclass;
36180+
36181+ if (wkinfo->dont_check)
36182+ lockdep_off();
36183+ if (!hl)
36184+ return;
36185+ while ((p = *hl++)) { /* assignment */
36186+ subclass = lockdep_hlock_class(p)->subclass;
36187+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36188+ if (p->read)
36189+ rwsem_acquire_read(p->instance, subclass, 0,
36190+ /*p->acquire_ip*/_RET_IP_);
36191+ else
36192+ rwsem_acquire(p->instance, subclass, 0,
36193+ /*p->acquire_ip*/_RET_IP_);
36194+ }
36195+}
36196+
36197+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36198+{
36199+ struct held_lock *p, **hl = wkinfo->hlock;
36200+
36201+ if (wkinfo->dont_check)
36202+ lockdep_on();
36203+ if (!hl)
36204+ return;
36205+ while ((p = *hl++)) /* assignment */
36206+ rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
36207+}
36208+#endif
1facf9fc 36209+
1facf9fc 36210+static void wkq_func(struct work_struct *wk)
36211+{
36212+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36213+
2dfbb274 36214+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
36215+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36216+
8b6a4947 36217+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 36218+ wkinfo->func(wkinfo->args);
8b6a4947 36219+ au_wkq_lockdep_post(wkinfo);
1facf9fc 36220+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36221+ complete(wkinfo->comp);
36222+ else {
7f207e10 36223+ kobject_put(wkinfo->kobj);
9dbd164d 36224+ module_put(THIS_MODULE); /* todo: ?? */
9f237c51 36225+ au_kfree_rcu(wkinfo);
1facf9fc 36226+ }
36227+}
36228+
36229+/*
36230+ * Since struct completion is large, try allocating it dynamically.
36231+ */
c2b27bf2 36232+#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
1facf9fc 36233+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36234+
36235+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36236+{
36237+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36238+ if (*comp) {
36239+ init_completion(*comp);
36240+ wkinfo->comp = *comp;
36241+ return 0;
36242+ }
36243+ return -ENOMEM;
36244+}
36245+
36246+static void au_wkq_comp_free(struct completion *comp)
36247+{
9f237c51 36248+ au_kfree_rcu(comp);
1facf9fc 36249+}
36250+
36251+#else
36252+
36253+/* no braces */
36254+#define AuWkqCompDeclare(name) \
36255+ DECLARE_COMPLETION_ONSTACK(_ ## name); \
36256+ struct completion *comp = &_ ## name
36257+
36258+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36259+{
36260+ wkinfo->comp = *comp;
36261+ return 0;
36262+}
36263+
36264+static void au_wkq_comp_free(struct completion *comp __maybe_unused)
36265+{
36266+ /* empty */
36267+}
36268+#endif /* 4KSTACKS */
36269+
53392da6 36270+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 36271+{
53392da6
AM
36272+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36273+ if (au_wkq_test()) {
38d290e6 36274+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
acd2b654
AM
36275+ " due to a dead dir by UDBA,"
36276+ " or async xino write?\n");
53392da6
AM
36277+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36278+ }
36279+ } else
36280+ au_dbg_verify_kthread();
36281+
36282+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 36283+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 36284+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
36285+ } else {
36286+ INIT_WORK(&wkinfo->wk, wkq_func);
36287+ schedule_work(&wkinfo->wk);
36288+ }
1facf9fc 36289+}
36290+
7f207e10
AM
36291+/*
36292+ * Be careful. It is easy to make deadlock happen.
36293+ * processA: lock, wkq and wait
36294+ * processB: wkq and wait, lock in wkq
36295+ * --> deadlock
36296+ */
b752ccd1 36297+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 36298+{
36299+ int err;
36300+ AuWkqCompDeclare(comp);
36301+ struct au_wkinfo wkinfo = {
b752ccd1 36302+ .flags = flags,
1facf9fc 36303+ .func = func,
36304+ .args = args
36305+ };
36306+
36307+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
36308+ if (unlikely(err))
36309+ goto out;
36310+ err = au_wkq_lockdep_alloc(&wkinfo);
36311+ if (unlikely(err))
36312+ goto out_comp;
1facf9fc 36313+ if (!err) {
53392da6 36314+ au_wkq_run(&wkinfo);
1facf9fc 36315+ /* no timeout, no interrupt */
36316+ wait_for_completion(wkinfo.comp);
1facf9fc 36317+ }
8b6a4947 36318+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 36319+
8b6a4947
AM
36320+out_comp:
36321+ au_wkq_comp_free(comp);
36322+out:
36323+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 36324+ return err;
1facf9fc 36325+}
36326+
027c5e7a
AM
36327+/*
36328+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36329+ * problem in a concurrent umounting.
36330+ */
53392da6
AM
36331+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36332+ unsigned int flags)
1facf9fc 36333+{
36334+ int err;
36335+ struct au_wkinfo *wkinfo;
36336+
f0c0a007 36337+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 36338+
36339+ /*
36340+ * wkq_func() must free this wkinfo.
36341+ * it highly depends upon the implementation of workqueue.
36342+ */
36343+ err = 0;
36344+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36345+ if (wkinfo) {
7f207e10 36346+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 36347+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 36348+ wkinfo->func = func;
36349+ wkinfo->args = args;
36350+ wkinfo->comp = NULL;
8b6a4947 36351+ au_wkq_lockdep_init(wkinfo);
7f207e10 36352+ kobject_get(wkinfo->kobj);
9dbd164d 36353+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 36354+
53392da6 36355+ au_wkq_run(wkinfo);
1facf9fc 36356+ } else {
36357+ err = -ENOMEM;
e49829fe 36358+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36359+ }
36360+
36361+ return err;
36362+}
36363+
36364+/* ---------------------------------------------------------------------- */
36365+
36366+void au_nwt_init(struct au_nowait_tasks *nwt)
36367+{
f0c0a007
AM
36368+ atomic_set(&nwt->nw_len, 0);
36369+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36370+ init_waitqueue_head(&nwt->nw_wq);
36371+}
36372+
36373+void au_wkq_fin(void)
36374+{
9dbd164d 36375+ destroy_workqueue(au_wkq);
1facf9fc 36376+}
36377+
36378+int __init au_wkq_init(void)
36379+{
9dbd164d 36380+ int err;
b752ccd1
AM
36381+
36382+ err = 0;
86dc4139 36383+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36384+ if (IS_ERR(au_wkq))
36385+ err = PTR_ERR(au_wkq);
36386+ else if (!au_wkq)
36387+ err = -ENOMEM;
b752ccd1
AM
36388+
36389+ return err;
1facf9fc 36390+}
7f207e10 36391diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
eca34b5c
AM
36392--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
36393+++ linux/fs/aufs/wkq.h 2019-07-11 15:42:14.475571481 +0200
acd2b654 36394@@ -0,0 +1,89 @@
062440b3 36395+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36396+/*
ba1aed25 36397+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 36398+ *
36399+ * This program, aufs is free software; you can redistribute it and/or modify
36400+ * it under the terms of the GNU General Public License as published by
36401+ * the Free Software Foundation; either version 2 of the License, or
36402+ * (at your option) any later version.
dece6358
AM
36403+ *
36404+ * This program is distributed in the hope that it will be useful,
36405+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36406+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36407+ * GNU General Public License for more details.
36408+ *
36409+ * You should have received a copy of the GNU General Public License
523b37e3 36410+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36411+ */
36412+
36413+/*
36414+ * workqueue for asynchronous/super-io operations
36415+ * todo: try new credentials management scheme
36416+ */
36417+
36418+#ifndef __AUFS_WKQ_H__
36419+#define __AUFS_WKQ_H__
36420+
36421+#ifdef __KERNEL__
36422+
8b6a4947 36423+#include <linux/wait.h>
5afbbe0d 36424+
dece6358
AM
36425+struct super_block;
36426+
1facf9fc 36427+/* ---------------------------------------------------------------------- */
36428+
36429+/*
36430+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36431+ */
36432+struct au_nowait_tasks {
f0c0a007 36433+ atomic_t nw_len;
1facf9fc 36434+ wait_queue_head_t nw_wq;
36435+};
36436+
36437+/* ---------------------------------------------------------------------- */
36438+
36439+typedef void (*au_wkq_func_t)(void *args);
36440+
36441+/* wkq flags */
36442+#define AuWkq_WAIT 1
9dbd164d 36443+#define AuWkq_NEST (1 << 1)
1facf9fc 36444+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36445+#define au_fset_wkq(flags, name) \
36446+ do { (flags) |= AuWkq_##name; } while (0)
36447+#define au_fclr_wkq(flags, name) \
36448+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36449+
36450+/* wkq.c */
b752ccd1 36451+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36452+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36453+ unsigned int flags);
1facf9fc 36454+void au_nwt_init(struct au_nowait_tasks *nwt);
36455+int __init au_wkq_init(void);
36456+void au_wkq_fin(void);
36457+
36458+/* ---------------------------------------------------------------------- */
36459+
53392da6
AM
36460+static inline int au_wkq_test(void)
36461+{
36462+ return current->flags & PF_WQ_WORKER;
36463+}
36464+
b752ccd1 36465+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36466+{
b752ccd1 36467+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36468+}
36469+
36470+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36471+{
f0c0a007 36472+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36473+ wake_up_all(&nwt->nw_wq);
36474+}
36475+
36476+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36477+{
f0c0a007 36478+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36479+ return 0;
36480+}
36481+
36482+#endif /* __KERNEL__ */
36483+#endif /* __AUFS_WKQ_H__ */
c1595e42 36484diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
eca34b5c
AM
36485--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
36486+++ linux/fs/aufs/xattr.c 2019-07-11 15:42:14.475571481 +0200
062440b3 36487@@ -0,0 +1,356 @@
cd7a4cd9 36488+// SPDX-License-Identifier: GPL-2.0
c1595e42 36489+/*
ba1aed25 36490+ * Copyright (C) 2014-2019 Junjiro R. Okajima
c1595e42
JR
36491+ *
36492+ * This program, aufs is free software; you can redistribute it and/or modify
36493+ * it under the terms of the GNU General Public License as published by
36494+ * the Free Software Foundation; either version 2 of the License, or
36495+ * (at your option) any later version.
36496+ *
36497+ * This program is distributed in the hope that it will be useful,
36498+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36499+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36500+ * GNU General Public License for more details.
36501+ *
36502+ * You should have received a copy of the GNU General Public License
36503+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36504+ */
36505+
36506+/*
36507+ * handling xattr functions
36508+ */
36509+
a2654f78
AM
36510+#include <linux/fs.h>
36511+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36512+#include <linux/xattr.h>
36513+#include "aufs.h"
36514+
36515+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36516+{
36517+ if (!ignore_flags)
36518+ goto out;
36519+ switch (err) {
36520+ case -ENOMEM:
36521+ case -EDQUOT:
36522+ goto out;
36523+ }
36524+
36525+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36526+ err = 0;
36527+ goto out;
36528+ }
36529+
36530+#define cmp(brattr, prefix) do { \
36531+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36532+ XATTR_##prefix##_PREFIX_LEN)) { \
36533+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36534+ err = 0; \
36535+ goto out; \
36536+ } \
36537+ } while (0)
36538+
36539+ cmp(SEC, SECURITY);
36540+ cmp(SYS, SYSTEM);
36541+ cmp(TR, TRUSTED);
36542+ cmp(USR, USER);
36543+#undef cmp
36544+
36545+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36546+ err = 0;
36547+
36548+out:
36549+ return err;
36550+}
36551+
36552+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36553+
36554+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
36555+ char *name, char **buf, unsigned int ignore_flags,
36556+ unsigned int verbose)
c1595e42
JR
36557+{
36558+ int err;
36559+ ssize_t ssz;
36560+ struct inode *h_idst;
36561+
36562+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36563+ err = ssz;
36564+ if (unlikely(err <= 0)) {
c1595e42
JR
36565+ if (err == -ENODATA
36566+ || (err == -EOPNOTSUPP
b912730e 36567+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 36568+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
36569+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36570+ || !strcmp(name,
36571+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36572+ ))
c1595e42 36573+ err = 0;
b912730e
AM
36574+ if (err && (verbose || au_debug_test()))
36575+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36576+ goto out;
36577+ }
36578+
36579+ /* unlock it temporary */
5527c038 36580+ h_idst = d_inode(h_dst);
febd17d6 36581+ inode_unlock(h_idst);
c1595e42 36582+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 36583+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36584+ if (unlikely(err)) {
7e9cd9fe
AM
36585+ if (verbose || au_debug_test())
36586+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36587+ err = au_xattr_ignore(err, name, ignore_flags);
36588+ }
36589+
36590+out:
36591+ return err;
36592+}
36593+
7e9cd9fe
AM
36594+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36595+ unsigned int verbose)
c1595e42
JR
36596+{
36597+ int err, unlocked, acl_access, acl_default;
36598+ ssize_t ssz;
36599+ struct inode *h_isrc, *h_idst;
36600+ char *value, *p, *o, *e;
36601+
36602+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36603+ /* there should not be the parent-child relationship between them */
5527c038
JR
36604+ h_isrc = d_inode(h_src);
36605+ h_idst = d_inode(h_dst);
febd17d6 36606+ inode_unlock(h_idst);
be118d29 36607+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36608+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36609+ unlocked = 0;
36610+
36611+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36612+ ssz = vfs_listxattr(h_src, NULL, 0);
36613+ err = ssz;
36614+ if (unlikely(err < 0)) {
36615+ AuTraceErr(err);
36616+ if (err == -ENODATA
36617+ || err == -EOPNOTSUPP)
36618+ err = 0; /* ignore */
36619+ goto out;
36620+ }
36621+
36622+ err = 0;
36623+ p = NULL;
36624+ o = NULL;
36625+ if (ssz) {
36626+ err = -ENOMEM;
36627+ p = kmalloc(ssz, GFP_NOFS);
36628+ o = p;
36629+ if (unlikely(!p))
36630+ goto out;
36631+ err = vfs_listxattr(h_src, p, ssz);
36632+ }
3c1bdaff 36633+ inode_unlock_shared(h_isrc);
c1595e42
JR
36634+ unlocked = 1;
36635+ AuDbg("err %d, ssz %zd\n", err, ssz);
36636+ if (unlikely(err < 0))
36637+ goto out_free;
36638+
36639+ err = 0;
36640+ e = p + ssz;
36641+ value = NULL;
36642+ acl_access = 0;
36643+ acl_default = 0;
36644+ while (!err && p < e) {
36645+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36646+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36647+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36648+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36649+ - 1);
7e9cd9fe
AM
36650+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36651+ verbose);
c1595e42
JR
36652+ p += strlen(p) + 1;
36653+ }
36654+ AuTraceErr(err);
36655+ ignore_flags |= au_xattr_out_of_list;
36656+ if (!err && !acl_access) {
36657+ err = au_do_cpup_xattr(h_dst, h_src,
36658+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36659+ ignore_flags, verbose);
c1595e42
JR
36660+ AuTraceErr(err);
36661+ }
36662+ if (!err && !acl_default) {
36663+ err = au_do_cpup_xattr(h_dst, h_src,
36664+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36665+ ignore_flags, verbose);
c1595e42
JR
36666+ AuTraceErr(err);
36667+ }
36668+
9f237c51 36669+ au_kfree_try_rcu(value);
c1595e42
JR
36670+
36671+out_free:
9f237c51 36672+ au_kfree_try_rcu(o);
c1595e42
JR
36673+out:
36674+ if (!unlocked)
3c1bdaff 36675+ inode_unlock_shared(h_isrc);
c1595e42
JR
36676+ AuTraceErr(err);
36677+ return err;
36678+}
36679+
36680+/* ---------------------------------------------------------------------- */
36681+
a2654f78
AM
36682+static int au_smack_reentering(struct super_block *sb)
36683+{
36684+#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36685+ /*
36686+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36687+ * i_op->getxattr(). ouch.
36688+ */
36689+ return si_pid_test(sb);
36690+#else
36691+ return 0;
36692+#endif
36693+}
36694+
c1595e42
JR
36695+enum {
36696+ AU_XATTR_LIST,
36697+ AU_XATTR_GET
36698+};
36699+
36700+struct au_lgxattr {
36701+ int type;
36702+ union {
36703+ struct {
36704+ char *list;
36705+ size_t size;
36706+ } list;
36707+ struct {
36708+ const char *name;
36709+ void *value;
36710+ size_t size;
36711+ } get;
36712+ } u;
36713+};
36714+
36715+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36716+{
36717+ ssize_t err;
a2654f78 36718+ int reenter;
c1595e42
JR
36719+ struct path h_path;
36720+ struct super_block *sb;
36721+
36722+ sb = dentry->d_sb;
a2654f78
AM
36723+ reenter = au_smack_reentering(sb);
36724+ if (!reenter) {
36725+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36726+ if (unlikely(err))
36727+ goto out;
36728+ }
36729+ err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
c1595e42
JR
36730+ if (unlikely(err))
36731+ goto out_si;
36732+ if (unlikely(!h_path.dentry))
36733+ /* illegally overlapped or something */
36734+ goto out_di; /* pretending success */
36735+
36736+ /* always topmost entry only */
36737+ switch (arg->type) {
36738+ case AU_XATTR_LIST:
36739+ err = vfs_listxattr(h_path.dentry,
36740+ arg->u.list.list, arg->u.list.size);
36741+ break;
36742+ case AU_XATTR_GET:
5afbbe0d 36743+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
36744+ err = vfs_getxattr(h_path.dentry,
36745+ arg->u.get.name, arg->u.get.value,
36746+ arg->u.get.size);
36747+ break;
36748+ }
36749+
36750+out_di:
a2654f78
AM
36751+ if (!reenter)
36752+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36753+out_si:
a2654f78
AM
36754+ if (!reenter)
36755+ si_read_unlock(sb);
c1595e42
JR
36756+out:
36757+ AuTraceErr(err);
36758+ return err;
36759+}
36760+
36761+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36762+{
36763+ struct au_lgxattr arg = {
36764+ .type = AU_XATTR_LIST,
36765+ .u.list = {
36766+ .list = list,
36767+ .size = size
36768+ },
36769+ };
36770+
36771+ return au_lgxattr(dentry, &arg);
36772+}
36773+
f2c43d5f
AM
36774+static ssize_t au_getxattr(struct dentry *dentry,
36775+ struct inode *inode __maybe_unused,
36776+ const char *name, void *value, size_t size)
c1595e42
JR
36777+{
36778+ struct au_lgxattr arg = {
36779+ .type = AU_XATTR_GET,
36780+ .u.get = {
36781+ .name = name,
36782+ .value = value,
36783+ .size = size
36784+ },
36785+ };
36786+
36787+ return au_lgxattr(dentry, &arg);
36788+}
36789+
f2c43d5f
AM
36790+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36791+ const char *name, const void *value, size_t size,
36792+ int flags)
c1595e42 36793+{
f2c43d5f 36794+ struct au_sxattr arg = {
c1595e42
JR
36795+ .type = AU_XATTR_SET,
36796+ .u.set = {
36797+ .name = name,
36798+ .value = value,
36799+ .size = size,
36800+ .flags = flags
36801+ },
36802+ };
36803+
f2c43d5f 36804+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36805+}
36806+
36807+/* ---------------------------------------------------------------------- */
36808+
f2c43d5f
AM
36809+static int au_xattr_get(const struct xattr_handler *handler,
36810+ struct dentry *dentry, struct inode *inode,
36811+ const char *name, void *buffer, size_t size)
c1595e42 36812+{
f2c43d5f 36813+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36814+}
36815+
f2c43d5f
AM
36816+static int au_xattr_set(const struct xattr_handler *handler,
36817+ struct dentry *dentry, struct inode *inode,
36818+ const char *name, const void *value, size_t size,
36819+ int flags)
c1595e42 36820+{
f2c43d5f 36821+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36822+}
36823+
36824+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36825+ .name = "",
36826+ .prefix = "",
c1595e42
JR
36827+ .get = au_xattr_get,
36828+ .set = au_xattr_set
c1595e42
JR
36829+};
36830+
36831+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36832+#ifdef CONFIG_FS_POSIX_ACL
36833+ &posix_acl_access_xattr_handler,
36834+ &posix_acl_default_xattr_handler,
36835+#endif
36836+ &au_xattr_handler, /* must be last */
f2c43d5f 36837+ NULL
c1595e42
JR
36838+};
36839+
36840+void au_xattr_init(struct super_block *sb)
36841+{
f2c43d5f 36842+ sb->s_xattr = au_xattr_handlers;
c1595e42 36843+}
7f207e10 36844diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
eca34b5c
AM
36845--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
36846+++ linux/fs/aufs/xino.c 2019-07-11 15:42:14.475571481 +0200
83b672a5 36847@@ -0,0 +1,1965 @@
cd7a4cd9 36848+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36849+/*
ba1aed25 36850+ * Copyright (C) 2005-2019 Junjiro R. Okajima
1facf9fc 36851+ *
36852+ * This program, aufs is free software; you can redistribute it and/or modify
36853+ * it under the terms of the GNU General Public License as published by
36854+ * the Free Software Foundation; either version 2 of the License, or
36855+ * (at your option) any later version.
dece6358
AM
36856+ *
36857+ * This program is distributed in the hope that it will be useful,
36858+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36859+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36860+ * GNU General Public License for more details.
36861+ *
36862+ * You should have received a copy of the GNU General Public License
523b37e3 36863+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36864+ */
36865+
36866+/*
36867+ * external inode number translation table and bitmap
062440b3
AM
36868+ *
36869+ * things to consider
36870+ * - the lifetime
36871+ * + au_xino object
36872+ * + XINO files (xino, xib, xigen)
36873+ * + dynamic debugfs entries (xiN)
36874+ * + static debugfs entries (xib, xigen)
36875+ * + static sysfs entry (xi_path)
36876+ * - several entry points to handle them.
36877+ * + mount(2) without xino option (default)
36878+ * + mount(2) with xino option
36879+ * + mount(2) with noxino option
36880+ * + umount(2)
36881+ * + remount with add/del branches
36882+ * + remount with xino/noxino options
1facf9fc 36883+ */
36884+
36885+#include <linux/seq_file.h>
392086de 36886+#include <linux/statfs.h>
1facf9fc 36887+#include "aufs.h"
36888+
062440b3
AM
36889+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36890+ aufs_bindex_t bbot,
36891+ struct super_block *h_sb)
1facf9fc 36892+{
062440b3
AM
36893+ /* todo: try binary-search if the branches are many */
36894+ for (; btop <= bbot; btop++)
36895+ if (h_sb == au_sbr_sb(sb, btop))
36896+ return btop;
36897+ return -1;
be52b249
AM
36898+}
36899+
062440b3
AM
36900+/*
36901+ * find another branch who is on the same filesystem of the specified
36902+ * branch{@btgt}. search until @bbot.
36903+ */
36904+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36905+ aufs_bindex_t bbot)
1facf9fc 36906+{
062440b3
AM
36907+ aufs_bindex_t bindex;
36908+ struct super_block *tgt_sb;
1facf9fc 36909+
062440b3
AM
36910+ tgt_sb = au_sbr_sb(sb, btgt);
36911+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36912+ if (bindex < 0)
36913+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
1facf9fc 36914+
062440b3 36915+ return bindex;
1facf9fc 36916+}
36917+
36918+/* ---------------------------------------------------------------------- */
36919+
36920+/*
062440b3 36921+ * stop unnecessary notify events at creating xino files
1facf9fc 36922+ */
acd2b654
AM
36923+
36924+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36925+{
36926+ aufs_bindex_t bfound, bindex, bbot;
36927+ struct dentry *parent;
36928+ struct au_branch *br;
36929+
36930+ bfound = -1;
36931+ parent = dentry->d_parent; /* safe d_parent access */
36932+ bbot = au_sbbot(sb);
36933+ for (bindex = 0; bindex <= bbot; bindex++) {
36934+ br = au_sbr(sb, bindex);
36935+ if (au_br_dentry(br) == parent) {
36936+ bfound = bindex;
36937+ break;
36938+ }
36939+ }
36940+
36941+ AuDbg("bfound b%d\n", bfound);
36942+ return bfound;
36943+}
36944+
062440b3
AM
36945+struct au_xino_lock_dir {
36946+ struct au_hinode *hdir;
36947+ struct dentry *parent;
36948+ struct inode *dir;
36949+};
36950+
36951+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36952+ unsigned int lsc)
1facf9fc 36953+{
062440b3
AM
36954+ struct dentry *parent;
36955+ struct inode *dir;
1facf9fc 36956+
062440b3 36957+ parent = dget_parent(dentry);
5527c038 36958+ dir = d_inode(parent);
062440b3
AM
36959+ inode_lock_nested(dir, lsc);
36960+#if 0 /* it should not happen */
36961+ spin_lock(&dentry->d_lock);
36962+ if (unlikely(dentry->d_parent != parent)) {
36963+ spin_unlock(&dentry->d_lock);
36964+ inode_unlock(dir);
36965+ dput(parent);
36966+ parent = NULL;
1facf9fc 36967+ goto out;
36968+ }
062440b3 36969+ spin_unlock(&dentry->d_lock);
1facf9fc 36970+
4f0767ce 36971+out:
062440b3
AM
36972+#endif
36973+ return parent;
1facf9fc 36974+}
36975+
062440b3 36976+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
1facf9fc 36977+ struct au_xino_lock_dir *ldir)
36978+{
acd2b654 36979+ aufs_bindex_t bindex;
1facf9fc 36980+
36981+ ldir->hdir = NULL;
acd2b654 36982+ bindex = au_xi_root(sb, xipath->dentry);
1facf9fc 36983+ if (bindex >= 0) {
062440b3 36984+ /* rw branch root */
5527c038 36985+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 36986+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 36987+ } else {
062440b3
AM
36988+ /* other */
36989+ ldir->parent = au_dget_parent_lock(xipath->dentry,
36990+ AuLsc_I_PARENT);
febd17d6 36991+ ldir->dir = d_inode(ldir->parent);
1facf9fc 36992+ }
36993+}
36994+
36995+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36996+{
36997+ if (ldir->hdir)
5afbbe0d 36998+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 36999+ else {
febd17d6 37000+ inode_unlock(ldir->dir);
1facf9fc 37001+ dput(ldir->parent);
37002+ }
37003+}
37004+
37005+/* ---------------------------------------------------------------------- */
37006+
062440b3
AM
37007+/*
37008+ * create and set a new xino file
37009+ */
83b672a5
AM
37010+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37011+ int wbrtop)
062440b3
AM
37012+{
37013+ struct file *file;
37014+ struct dentry *h_parent, *d;
37015+ struct inode *h_dir, *inode;
37016+ int err;
83b672a5 37017+ static DEFINE_MUTEX(mtx);
062440b3
AM
37018+
37019+ /*
37020+ * at mount-time, and the xino file is the default path,
37021+ * hnotify is disabled so we have no notify events to ignore.
37022+ * when a user specified the xino, we cannot get au_hdir to be ignored.
37023+ */
83b672a5
AM
37024+ if (!wbrtop)
37025+ mutex_lock(&mtx);
062440b3
AM
37026+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37027+ /* | __FMODE_NONOTIFY */,
cd7a4cd9 37028+ 0666);
062440b3 37029+ if (IS_ERR(file)) {
83b672a5
AM
37030+ if (!wbrtop)
37031+ mutex_unlock(&mtx);
062440b3
AM
37032+ if (!silent)
37033+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37034+ return file;
37035+ }
37036+
37037+ /* keep file count */
37038+ err = 0;
37039+ d = file->f_path.dentry;
37040+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
83b672a5
AM
37041+ if (!wbrtop)
37042+ mutex_unlock(&mtx);
062440b3
AM
37043+ /* mnt_want_write() is unnecessary here */
37044+ h_dir = d_inode(h_parent);
37045+ inode = file_inode(file);
37046+ /* no delegation since it is just created */
37047+ if (inode->i_nlink)
37048+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37049+ /*force*/0);
37050+ inode_unlock(h_dir);
37051+ dput(h_parent);
37052+ if (unlikely(err)) {
37053+ if (!silent)
37054+ pr_err("unlink %s(%d)\n", fpath, err);
37055+ goto out;
37056+ }
37057+
37058+ err = -EINVAL;
37059+ if (unlikely(sb == d->d_sb)) {
37060+ if (!silent)
37061+ pr_err("%s must be outside\n", fpath);
37062+ goto out;
37063+ }
37064+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37065+ if (!silent)
37066+ pr_err("xino doesn't support %s(%s)\n",
37067+ fpath, au_sbtype(d->d_sb));
37068+ goto out;
37069+ }
37070+ return file; /* success */
37071+
37072+out:
37073+ fput(file);
37074+ file = ERR_PTR(err);
37075+ return file;
37076+}
37077+
37078+/*
37079+ * create a new xinofile at the same place/path as @base.
37080+ */
37081+struct file *au_xino_create2(struct super_block *sb, struct path *base,
37082+ struct file *copy_src)
37083+{
37084+ struct file *file;
37085+ struct dentry *dentry, *parent;
37086+ struct inode *dir, *delegated;
37087+ struct qstr *name;
37088+ struct path path;
37089+ int err, do_unlock;
37090+ struct au_xino_lock_dir ldir;
37091+
37092+ do_unlock = 1;
37093+ au_xino_lock_dir(sb, base, &ldir);
37094+ dentry = base->dentry;
37095+ parent = dentry->d_parent; /* dir inode is locked */
37096+ dir = d_inode(parent);
37097+ IMustLock(dir);
37098+
37099+ name = &dentry->d_name;
37100+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37101+ if (IS_ERR(path.dentry)) {
37102+ file = (void *)path.dentry;
37103+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37104+ goto out;
37105+ }
37106+
37107+ /* no need to mnt_want_write() since we call dentry_open() later */
cd7a4cd9 37108+ err = vfs_create(dir, path.dentry, 0666, NULL);
062440b3
AM
37109+ if (unlikely(err)) {
37110+ file = ERR_PTR(err);
37111+ pr_err("%pd create err %d\n", dentry, err);
37112+ goto out_dput;
37113+ }
37114+
37115+ path.mnt = base->mnt;
37116+ file = vfsub_dentry_open(&path,
37117+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37118+ /* | __FMODE_NONOTIFY */);
37119+ if (IS_ERR(file)) {
37120+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37121+ goto out_dput;
37122+ }
37123+
37124+ delegated = NULL;
37125+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37126+ au_xino_unlock_dir(&ldir);
37127+ do_unlock = 0;
37128+ if (unlikely(err == -EWOULDBLOCK)) {
37129+ pr_warn("cannot retry for NFSv4 delegation"
37130+ " for an internal unlink\n");
37131+ iput(delegated);
37132+ }
37133+ if (unlikely(err)) {
37134+ pr_err("%pd unlink err %d\n", dentry, err);
37135+ goto out_fput;
37136+ }
37137+
37138+ if (copy_src) {
37139+ /* no one can touch copy_src xino */
37140+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37141+ if (unlikely(err)) {
37142+ pr_err("%pd copy err %d\n", dentry, err);
37143+ goto out_fput;
37144+ }
37145+ }
37146+ goto out_dput; /* success */
1facf9fc 37147+
062440b3
AM
37148+out_fput:
37149+ fput(file);
37150+ file = ERR_PTR(err);
37151+out_dput:
37152+ dput(path.dentry);
37153+out:
37154+ if (do_unlock)
37155+ au_xino_unlock_dir(&ldir);
37156+ return file;
37157+}
37158+
acd2b654
AM
37159+struct file *au_xino_file1(struct au_xino *xi)
37160+{
37161+ struct file *file;
37162+ unsigned int u, nfile;
37163+
37164+ file = NULL;
37165+ nfile = xi->xi_nfile;
37166+ for (u = 0; u < nfile; u++) {
37167+ file = xi->xi_file[u];
37168+ if (file)
37169+ break;
37170+ }
37171+
37172+ return file;
37173+}
37174+
37175+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37176+{
37177+ int err;
37178+ struct file *f;
37179+ void *p;
37180+
37181+ if (file)
37182+ get_file(file);
37183+
37184+ err = 0;
37185+ f = NULL;
37186+ if (idx < xi->xi_nfile) {
37187+ f = xi->xi_file[idx];
37188+ if (f)
37189+ fput(f);
37190+ } else {
37191+ p = au_kzrealloc(xi->xi_file,
37192+ sizeof(*xi->xi_file) * xi->xi_nfile,
37193+ sizeof(*xi->xi_file) * (idx + 1),
37194+ GFP_NOFS, /*may_shrink*/0);
37195+ if (p) {
37196+ MtxMustLock(&xi->xi_mtx);
37197+ xi->xi_file = p;
37198+ xi->xi_nfile = idx + 1;
37199+ } else {
37200+ err = -ENOMEM;
37201+ if (file)
37202+ fput(file);
37203+ goto out;
37204+ }
37205+ }
37206+ xi->xi_file[idx] = file;
37207+
37208+out:
37209+ return err;
37210+}
37211+
37212+/*
37213+ * if @xinew->xi is not set, then create new xigen file.
37214+ */
37215+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37216+{
37217+ struct file *file;
37218+ int err;
37219+
37220+ SiMustAnyLock(sb);
37221+
37222+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37223+ if (IS_ERR(file)) {
37224+ err = PTR_ERR(file);
37225+ pr_err("%s[%d], err %d\n",
37226+ xinew->xi ? "xino" : "xigen",
37227+ xinew->idx, err);
37228+ goto out;
37229+ }
37230+
37231+ if (xinew->xi)
37232+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37233+ else {
37234+ BUG();
37235+ /* todo: make xigen file an array */
37236+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37237+ }
37238+ fput(file);
37239+ if (unlikely(err))
37240+ file = ERR_PTR(err);
37241+
37242+out:
37243+ return file;
37244+}
37245+
062440b3
AM
37246+/* ---------------------------------------------------------------------- */
37247+
37248+/*
37249+ * truncate xino files
37250+ */
acd2b654
AM
37251+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37252+ int idx, struct kstatfs *st)
1facf9fc 37253+{
37254+ int err;
392086de 37255+ blkcnt_t blocks;
acd2b654
AM
37256+ struct file *file, *new_xino;
37257+ struct au_xi_new xinew = {
37258+ .idx = idx
37259+ };
37260+
37261+ err = 0;
37262+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37263+ file = au_xino_file(xinew.xi, idx);
37264+ if (!file)
37265+ goto out;
37266+
37267+ xinew.base = &file->f_path;
37268+ err = vfs_statfs(xinew.base, st);
37269+ if (unlikely(err)) {
37270+ AuErr1("statfs err %d, ignored\n", err);
37271+ err = 0;
37272+ goto out;
37273+ }
37274+
37275+ blocks = file_inode(file)->i_blocks;
37276+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37277+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37278+
37279+ xinew.copy_src = file;
37280+ new_xino = au_xi_new(sb, &xinew);
37281+ if (IS_ERR(new_xino)) {
37282+ err = PTR_ERR(new_xino);
37283+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37284+ goto out;
37285+ }
37286+
37287+ err = vfs_statfs(&new_xino->f_path, st);
37288+ if (!err)
37289+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37290+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37291+ st->f_bfree, st->f_blocks);
37292+ else {
37293+ AuErr1("statfs err %d, ignored\n", err);
37294+ err = 0;
37295+ }
37296+
37297+out:
37298+ return err;
37299+}
37300+
37301+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37302+{
37303+ int err, i;
37304+ unsigned long jiffy;
062440b3 37305+ aufs_bindex_t bbot;
392086de 37306+ struct kstatfs *st;
1facf9fc 37307+ struct au_branch *br;
acd2b654 37308+ struct au_xino *xi;
1facf9fc 37309+
392086de 37310+ err = -ENOMEM;
be52b249 37311+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
37312+ if (unlikely(!st))
37313+ goto out;
37314+
1facf9fc 37315+ err = -EINVAL;
5afbbe0d
AM
37316+ bbot = au_sbbot(sb);
37317+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 37318+ goto out_st;
392086de 37319+
1facf9fc 37320+ err = 0;
acd2b654
AM
37321+ jiffy = jiffies;
37322+ br = au_sbr(sb, bindex);
37323+ xi = br->br_xino;
37324+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37325+ err = au_xino_do_trunc(sb, bindex, i, st);
37326+ if (!err)
37327+ au_sbi(sb)->si_xino_jiffy = jiffy;
392086de
AM
37328+
37329+out_st:
9f237c51 37330+ au_kfree_rcu(st);
4f0767ce 37331+out:
1facf9fc 37332+ return err;
37333+}
37334+
37335+struct xino_do_trunc_args {
37336+ struct super_block *sb;
37337+ struct au_branch *br;
acd2b654 37338+ int idx;
1facf9fc 37339+};
37340+
37341+static void xino_do_trunc(void *_args)
37342+{
37343+ struct xino_do_trunc_args *args = _args;
37344+ struct super_block *sb;
37345+ struct au_branch *br;
37346+ struct inode *dir;
acd2b654 37347+ int err, idx;
1facf9fc 37348+ aufs_bindex_t bindex;
37349+
37350+ err = 0;
37351+ sb = args->sb;
5527c038 37352+ dir = d_inode(sb->s_root);
1facf9fc 37353+ br = args->br;
acd2b654 37354+ idx = args->idx;
1facf9fc 37355+
37356+ si_noflush_write_lock(sb);
37357+ ii_read_lock_parent(dir);
37358+ bindex = au_br_index(sb, br->br_id);
acd2b654 37359+ err = au_xino_trunc(sb, bindex, idx);
1facf9fc 37360+ ii_read_unlock(dir);
37361+ if (unlikely(err))
392086de 37362+ pr_warn("err b%d, (%d)\n", bindex, err);
062440b3 37363+ atomic_dec(&br->br_xino->xi_truncating);
acd2b654 37364+ au_lcnt_dec(&br->br_count);
1facf9fc 37365+ si_write_unlock(sb);
027c5e7a 37366+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 37367+ au_kfree_rcu(args);
1facf9fc 37368+}
37369+
acd2b654
AM
37370+/*
37371+ * returns the index in the xi_file array whose corresponding file is necessary
37372+ * to truncate, or -1 which means no need to truncate.
37373+ */
392086de
AM
37374+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37375+{
37376+ int err;
acd2b654 37377+ unsigned int u;
392086de
AM
37378+ struct kstatfs st;
37379+ struct au_sbinfo *sbinfo;
acd2b654 37380+ struct au_xino *xi;
062440b3 37381+ struct file *file;
392086de
AM
37382+
37383+ /* todo: si_xino_expire and the ratio should be customizable */
37384+ sbinfo = au_sbi(sb);
37385+ if (time_before(jiffies,
37386+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
acd2b654 37387+ return -1;
392086de
AM
37388+
37389+ /* truncation border */
acd2b654
AM
37390+ xi = br->br_xino;
37391+ for (u = 0; u < xi->xi_nfile; u++) {
37392+ file = au_xino_file(xi, u);
37393+ if (!file)
37394+ continue;
37395+
37396+ err = vfs_statfs(&file->f_path, &st);
37397+ if (unlikely(err)) {
37398+ AuErr1("statfs err %d, ignored\n", err);
37399+ return -1;
37400+ }
37401+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37402+ >= AUFS_XINO_DEF_TRUNC)
37403+ return u;
392086de 37404+ }
392086de 37405+
acd2b654 37406+ return -1;
392086de
AM
37407+}
37408+
1facf9fc 37409+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37410+{
acd2b654 37411+ int idx;
1facf9fc 37412+ struct xino_do_trunc_args *args;
37413+ int wkq_err;
37414+
acd2b654
AM
37415+ idx = xino_trunc_test(sb, br);
37416+ if (idx < 0)
1facf9fc 37417+ return;
37418+
062440b3 37419+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
1facf9fc 37420+ goto out;
37421+
37422+ /* lock and kfree() will be called in trunc_xino() */
37423+ args = kmalloc(sizeof(*args), GFP_NOFS);
37424+ if (unlikely(!args)) {
37425+ AuErr1("no memory\n");
f0c0a007 37426+ goto out;
1facf9fc 37427+ }
37428+
acd2b654 37429+ au_lcnt_inc(&br->br_count);
1facf9fc 37430+ args->sb = sb;
37431+ args->br = br;
acd2b654 37432+ args->idx = idx;
53392da6 37433+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 37434+ if (!wkq_err)
37435+ return; /* success */
37436+
4a4d8108 37437+ pr_err("wkq %d\n", wkq_err);
acd2b654 37438+ au_lcnt_dec(&br->br_count);
9f237c51 37439+ au_kfree_rcu(args);
1facf9fc 37440+
4f0767ce 37441+out:
062440b3 37442+ atomic_dec(&br->br_xino->xi_truncating);
1facf9fc 37443+}
37444+
37445+/* ---------------------------------------------------------------------- */
37446+
acd2b654
AM
37447+struct au_xi_calc {
37448+ int idx;
37449+ loff_t pos;
37450+};
37451+
37452+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37453+ struct au_xi_calc *calc)
37454+{
37455+ loff_t maxent;
37456+
37457+ maxent = au_xi_maxent(sb);
37458+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37459+ calc->pos *= sizeof(ino_t);
37460+}
37461+
37462+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37463+ struct au_xi_calc *calc)
37464+{
37465+ int err;
37466+ struct file *file;
37467+ struct au_xino *xi = br->br_xino;
37468+ struct au_xi_new xinew = {
37469+ .xi = xi
37470+ };
37471+
37472+ SiMustAnyLock(sb);
37473+
37474+ err = 0;
37475+ if (!xi)
37476+ goto out;
37477+
37478+ mutex_lock(&xi->xi_mtx);
37479+ file = au_xino_file(xi, calc->idx);
37480+ if (file)
37481+ goto out_mtx;
37482+
37483+ file = au_xino_file(xi, /*idx*/-1);
37484+ AuDebugOn(!file);
37485+ xinew.idx = calc->idx;
37486+ xinew.base = &file->f_path;
37487+ /* xinew.copy_src = NULL; */
37488+ file = au_xi_new(sb, &xinew);
37489+ if (IS_ERR(file))
37490+ err = PTR_ERR(file);
37491+
37492+out_mtx:
37493+ mutex_unlock(&xi->xi_mtx);
37494+out:
37495+ return err;
37496+}
37497+
37498+struct au_xino_do_new_async_args {
37499+ struct super_block *sb;
37500+ struct au_branch *br;
37501+ struct au_xi_calc calc;
37502+ ino_t ino;
37503+};
37504+
9f237c51
AM
37505+struct au_xi_writing {
37506+ struct hlist_bl_node node;
37507+ ino_t h_ino, ino;
37508+};
37509+
acd2b654
AM
37510+static int au_xino_do_write(vfs_writef_t write, struct file *file,
37511+ struct au_xi_calc *calc, ino_t ino);
37512+
37513+static void au_xino_call_do_new_async(void *args)
37514+{
37515+ struct au_xino_do_new_async_args *a = args;
37516+ struct au_branch *br;
37517+ struct super_block *sb;
37518+ struct au_sbinfo *sbi;
37519+ struct inode *root;
37520+ struct file *file;
9f237c51
AM
37521+ struct au_xi_writing *del, *p;
37522+ struct hlist_bl_head *hbl;
37523+ struct hlist_bl_node *pos;
acd2b654
AM
37524+ int err;
37525+
37526+ br = a->br;
37527+ sb = a->sb;
37528+ sbi = au_sbi(sb);
37529+ si_noflush_read_lock(sb);
37530+ root = d_inode(sb->s_root);
37531+ ii_read_lock_child(root);
37532+ err = au_xino_do_new_async(sb, br, &a->calc);
9f237c51
AM
37533+ if (unlikely(err)) {
37534+ AuIOErr("err %d\n", err);
37535+ goto out;
37536+ }
37537+
37538+ file = au_xino_file(br->br_xino, a->calc.idx);
37539+ AuDebugOn(!file);
37540+ err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
37541+ if (unlikely(err)) {
acd2b654 37542+ AuIOErr("err %d\n", err);
9f237c51
AM
37543+ goto out;
37544+ }
37545+
37546+ del = NULL;
37547+ hbl = &br->br_xino->xi_writing;
37548+ hlist_bl_lock(hbl);
37549+ au_hbl_for_each(pos, hbl) {
37550+ p = container_of(pos, struct au_xi_writing, node);
37551+ if (p->ino == a->ino) {
37552+ del = p;
37553+ hlist_bl_del(&p->node);
37554+ break;
37555+ }
37556+ }
37557+ hlist_bl_unlock(hbl);
37558+ au_kfree_rcu(del);
37559+
37560+out:
acd2b654
AM
37561+ au_lcnt_dec(&br->br_count);
37562+ ii_read_unlock(root);
37563+ si_read_unlock(sb);
37564+ au_nwt_done(&sbi->si_nowait);
9f237c51 37565+ au_kfree_rcu(a);
acd2b654
AM
37566+}
37567+
37568+/*
37569+ * create a new xino file asynchronously
37570+ */
37571+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37572+ struct au_xi_calc *calc, ino_t ino)
37573+{
37574+ int err;
37575+ struct au_xino_do_new_async_args *arg;
37576+
37577+ err = -ENOMEM;
37578+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37579+ if (unlikely(!arg))
37580+ goto out;
37581+
37582+ arg->sb = sb;
37583+ arg->br = br;
37584+ arg->calc = *calc;
37585+ arg->ino = ino;
37586+ au_lcnt_inc(&br->br_count);
37587+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37588+ if (unlikely(err)) {
37589+ pr_err("wkq %d\n", err);
37590+ au_lcnt_dec(&br->br_count);
9f237c51 37591+ au_kfree_rcu(arg);
acd2b654
AM
37592+ }
37593+
37594+out:
37595+ return err;
37596+}
37597+
062440b3
AM
37598+/*
37599+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37600+ * at the position of @h_ino.
37601+ */
37602+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37603+ ino_t *ino)
37604+{
37605+ int err;
37606+ ssize_t sz;
acd2b654 37607+ struct au_xi_calc calc;
062440b3 37608+ struct au_sbinfo *sbinfo;
acd2b654 37609+ struct file *file;
9f237c51
AM
37610+ struct au_xino *xi;
37611+ struct hlist_bl_head *hbl;
37612+ struct hlist_bl_node *pos;
37613+ struct au_xi_writing *p;
062440b3
AM
37614+
37615+ *ino = 0;
37616+ if (!au_opt_test(au_mntflags(sb), XINO))
37617+ return 0; /* no xino */
37618+
37619+ err = 0;
acd2b654 37620+ au_xi_calc(sb, h_ino, &calc);
9f237c51
AM
37621+ xi = au_sbr(sb, bindex)->br_xino;
37622+ file = au_xino_file(xi, calc.idx);
37623+ if (!file) {
37624+ hbl = &xi->xi_writing;
37625+ hlist_bl_lock(hbl);
37626+ au_hbl_for_each(pos, hbl) {
37627+ p = container_of(pos, struct au_xi_writing, node);
37628+ if (p->h_ino == h_ino) {
37629+ AuDbg("hi%llu, i%llu, found\n",
37630+ (u64)p->h_ino, (u64)p->ino);
37631+ *ino = p->ino;
37632+ break;
37633+ }
37634+ }
37635+ hlist_bl_unlock(hbl);
37636+ return 0;
37637+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37638+ return 0; /* no xino */
062440b3 37639+
acd2b654
AM
37640+ sbinfo = au_sbi(sb);
37641+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
062440b3
AM
37642+ if (sz == sizeof(*ino))
37643+ return 0; /* success */
37644+
37645+ err = sz;
37646+ if (unlikely(sz >= 0)) {
37647+ err = -EIO;
37648+ AuIOErr("xino read error (%zd)\n", sz);
37649+ }
062440b3
AM
37650+ return err;
37651+}
37652+
5527c038 37653+static int au_xino_do_write(vfs_writef_t write, struct file *file,
acd2b654 37654+ struct au_xi_calc *calc, ino_t ino)
1facf9fc 37655+{
1facf9fc 37656+ ssize_t sz;
37657+
acd2b654 37658+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
1facf9fc 37659+ if (sz == sizeof(ino))
37660+ return 0; /* success */
37661+
37662+ AuIOErr("write failed (%zd)\n", sz);
37663+ return -EIO;
37664+}
37665+
37666+/*
37667+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37668+ * at the position of @h_ino.
37669+ * even if @ino is zero, it is written to the xinofile and means no entry.
37670+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37671+ * try truncating it.
37672+ */
37673+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37674+ ino_t ino)
37675+{
37676+ int err;
37677+ unsigned int mnt_flags;
acd2b654
AM
37678+ struct au_xi_calc calc;
37679+ struct file *file;
1facf9fc 37680+ struct au_branch *br;
acd2b654 37681+ struct au_xino *xi;
9f237c51 37682+ struct au_xi_writing *p;
1facf9fc 37683+
dece6358 37684+ SiMustAnyLock(sb);
1facf9fc 37685+
37686+ mnt_flags = au_mntflags(sb);
37687+ if (!au_opt_test(mnt_flags, XINO))
37688+ return 0;
37689+
acd2b654 37690+ au_xi_calc(sb, h_ino, &calc);
1facf9fc 37691+ br = au_sbr(sb, bindex);
acd2b654
AM
37692+ xi = br->br_xino;
37693+ file = au_xino_file(xi, calc.idx);
37694+ if (!file) {
9f237c51
AM
37695+ /* store the inum pair into the list */
37696+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37697+ p->h_ino = h_ino;
37698+ p->ino = ino;
37699+ au_hbl_add(&p->node, &xi->xi_writing);
37700+
acd2b654
AM
37701+ /* create and write a new xino file asynchronously */
37702+ err = au_xino_new_async(sb, br, &calc, ino);
37703+ if (!err)
37704+ return 0; /* success */
37705+ goto out;
37706+ }
37707+
37708+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
1facf9fc 37709+ if (!err) {
acd2b654 37710+ br = au_sbr(sb, bindex);
1facf9fc 37711+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37712+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37713+ xino_try_trunc(sb, br);
37714+ return 0; /* success */
37715+ }
37716+
acd2b654 37717+out:
1facf9fc 37718+ AuIOErr("write failed (%d)\n", err);
37719+ return -EIO;
37720+}
37721+
062440b3
AM
37722+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37723+ size_t size, loff_t *pos);
1facf9fc 37724+
062440b3
AM
37725+/* todo: unnecessary to support mmap_sem since kernel-space? */
37726+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37727+ loff_t *pos)
37728+{
37729+ ssize_t err;
37730+ mm_segment_t oldfs;
37731+ union {
37732+ void *k;
37733+ char __user *u;
37734+ } buf;
37735+ int i;
37736+ const int prevent_endless = 10;
1facf9fc 37737+
062440b3
AM
37738+ i = 0;
37739+ buf.k = kbuf;
37740+ oldfs = get_fs();
37741+ set_fs(KERNEL_DS);
37742+ do {
37743+ err = func(file, buf.u, size, pos);
37744+ if (err == -EINTR
37745+ && !au_wkq_test()
37746+ && fatal_signal_pending(current)) {
37747+ set_fs(oldfs);
37748+ err = xino_fread_wkq(func, file, kbuf, size, pos);
37749+ BUG_ON(err == -EINTR);
37750+ oldfs = get_fs();
37751+ set_fs(KERNEL_DS);
37752+ }
37753+ } while (i++ < prevent_endless
37754+ && (err == -EAGAIN || err == -EINTR));
37755+ set_fs(oldfs);
37756+
37757+#if 0 /* reserved for future use */
37758+ if (err > 0)
37759+ fsnotify_access(file->f_path.dentry);
37760+#endif
37761+
37762+ return err;
37763+}
37764+
37765+struct xino_fread_args {
37766+ ssize_t *errp;
37767+ vfs_readf_t func;
37768+ struct file *file;
37769+ void *buf;
37770+ size_t size;
37771+ loff_t *pos;
37772+};
37773+
37774+static void call_xino_fread(void *args)
37775+{
37776+ struct xino_fread_args *a = args;
37777+ *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37778+}
37779+
37780+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37781+ size_t size, loff_t *pos)
37782+{
37783+ ssize_t err;
37784+ int wkq_err;
37785+ struct xino_fread_args args = {
37786+ .errp = &err,
37787+ .func = func,
37788+ .file = file,
37789+ .buf = buf,
37790+ .size = size,
37791+ .pos = pos
37792+ };
37793+
37794+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37795+ if (unlikely(wkq_err))
37796+ err = wkq_err;
37797+
37798+ return err;
37799+}
37800+
37801+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37802+ size_t size, loff_t *pos);
37803+
37804+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37805+ size_t size, loff_t *pos)
37806+{
37807+ ssize_t err;
37808+ mm_segment_t oldfs;
37809+ union {
37810+ void *k;
37811+ const char __user *u;
37812+ } buf;
37813+ int i;
37814+ const int prevent_endless = 10;
37815+
37816+ i = 0;
37817+ buf.k = kbuf;
37818+ oldfs = get_fs();
37819+ set_fs(KERNEL_DS);
37820+ do {
37821+ err = func(file, buf.u, size, pos);
37822+ if (err == -EINTR
37823+ && !au_wkq_test()
37824+ && fatal_signal_pending(current)) {
37825+ set_fs(oldfs);
37826+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37827+ BUG_ON(err == -EINTR);
37828+ oldfs = get_fs();
37829+ set_fs(KERNEL_DS);
37830+ }
37831+ } while (i++ < prevent_endless
37832+ && (err == -EAGAIN || err == -EINTR));
37833+ set_fs(oldfs);
37834+
37835+#if 0 /* reserved for future use */
37836+ if (err > 0)
37837+ fsnotify_modify(file->f_path.dentry);
37838+#endif
37839+
37840+ return err;
37841+}
37842+
37843+struct do_xino_fwrite_args {
37844+ ssize_t *errp;
37845+ vfs_writef_t func;
37846+ struct file *file;
37847+ void *buf;
37848+ size_t size;
37849+ loff_t *pos;
37850+};
37851+
37852+static void call_do_xino_fwrite(void *args)
37853+{
37854+ struct do_xino_fwrite_args *a = args;
37855+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37856+}
37857+
37858+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37859+ size_t size, loff_t *pos)
37860+{
37861+ ssize_t err;
37862+ int wkq_err;
37863+ struct do_xino_fwrite_args args = {
37864+ .errp = &err,
37865+ .func = func,
37866+ .file = file,
37867+ .buf = buf,
37868+ .size = size,
37869+ .pos = pos
37870+ };
37871+
37872+ /*
37873+ * it breaks RLIMIT_FSIZE and normal user's limit,
37874+ * users should care about quota and real 'filesystem full.'
37875+ */
37876+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37877+ if (unlikely(wkq_err))
37878+ err = wkq_err;
37879+
37880+ return err;
37881+}
37882+
37883+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37884+ size_t size, loff_t *pos)
37885+{
37886+ ssize_t err;
37887+
37888+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37889+ lockdep_off();
37890+ err = do_xino_fwrite(func, file, buf, size, pos);
37891+ lockdep_on();
37892+ } else {
37893+ lockdep_off();
37894+ err = xino_fwrite_wkq(func, file, buf, size, pos);
37895+ lockdep_on();
37896+ }
37897+
37898+ return err;
37899+}
37900+
37901+/* ---------------------------------------------------------------------- */
37902+
37903+/*
37904+ * inode number bitmap
37905+ */
1facf9fc 37906+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37907+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37908+{
37909+ ino_t ino;
37910+
37911+ AuDebugOn(bit < 0 || page_bits <= bit);
37912+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37913+ return ino;
37914+}
37915+
37916+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37917+{
37918+ AuDebugOn(ino < AUFS_FIRST_INO);
37919+ ino -= AUFS_FIRST_INO;
37920+ *pindex = ino / page_bits;
37921+ *bit = ino % page_bits;
37922+}
37923+
37924+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37925+{
37926+ int err;
37927+ loff_t pos;
37928+ ssize_t sz;
37929+ struct au_sbinfo *sbinfo;
37930+ struct file *xib;
37931+ unsigned long *p;
37932+
37933+ sbinfo = au_sbi(sb);
37934+ MtxMustLock(&sbinfo->si_xib_mtx);
37935+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37936+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37937+
37938+ if (pindex == sbinfo->si_xib_last_pindex)
37939+ return 0;
37940+
37941+ xib = sbinfo->si_xib;
37942+ p = sbinfo->si_xib_buf;
37943+ pos = sbinfo->si_xib_last_pindex;
37944+ pos *= PAGE_SIZE;
37945+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37946+ if (unlikely(sz != PAGE_SIZE))
37947+ goto out;
37948+
37949+ pos = pindex;
37950+ pos *= PAGE_SIZE;
c06a8ce3 37951+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 37952+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37953+ else {
37954+ memset(p, 0, PAGE_SIZE);
37955+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37956+ }
37957+ if (sz == PAGE_SIZE) {
37958+ sbinfo->si_xib_last_pindex = pindex;
37959+ return 0; /* success */
37960+ }
37961+
4f0767ce 37962+out:
b752ccd1
AM
37963+ AuIOErr1("write failed (%zd)\n", sz);
37964+ err = sz;
37965+ if (sz >= 0)
37966+ err = -EIO;
37967+ return err;
37968+}
37969+
b752ccd1
AM
37970+static void au_xib_clear_bit(struct inode *inode)
37971+{
37972+ int err, bit;
37973+ unsigned long pindex;
37974+ struct super_block *sb;
37975+ struct au_sbinfo *sbinfo;
37976+
37977+ AuDebugOn(inode->i_nlink);
37978+
37979+ sb = inode->i_sb;
37980+ xib_calc_bit(inode->i_ino, &pindex, &bit);
37981+ AuDebugOn(page_bits <= bit);
37982+ sbinfo = au_sbi(sb);
37983+ mutex_lock(&sbinfo->si_xib_mtx);
37984+ err = xib_pindex(sb, pindex);
37985+ if (!err) {
37986+ clear_bit(bit, sbinfo->si_xib_buf);
37987+ sbinfo->si_xib_next_bit = bit;
37988+ }
37989+ mutex_unlock(&sbinfo->si_xib_mtx);
37990+}
37991+
1facf9fc 37992+/* ---------------------------------------------------------------------- */
37993+
1facf9fc 37994+/*
062440b3 37995+ * truncate a xino bitmap file
1facf9fc 37996+ */
1facf9fc 37997+
37998+/* todo: slow */
37999+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38000+{
38001+ int err, bit;
38002+ ssize_t sz;
38003+ unsigned long pindex;
38004+ loff_t pos, pend;
38005+ struct au_sbinfo *sbinfo;
5527c038 38006+ vfs_readf_t func;
1facf9fc 38007+ ino_t *ino;
38008+ unsigned long *p;
38009+
38010+ err = 0;
38011+ sbinfo = au_sbi(sb);
dece6358 38012+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 38013+ p = sbinfo->si_xib_buf;
38014+ func = sbinfo->si_xread;
c06a8ce3 38015+ pend = vfsub_f_size_read(file);
1facf9fc 38016+ pos = 0;
38017+ while (pos < pend) {
38018+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
38019+ err = sz;
38020+ if (unlikely(sz <= 0))
38021+ goto out;
38022+
38023+ err = 0;
38024+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38025+ if (unlikely(*ino < AUFS_FIRST_INO))
38026+ continue;
38027+
38028+ xib_calc_bit(*ino, &pindex, &bit);
38029+ AuDebugOn(page_bits <= bit);
38030+ err = xib_pindex(sb, pindex);
38031+ if (!err)
38032+ set_bit(bit, p);
38033+ else
38034+ goto out;
38035+ }
38036+ }
38037+
4f0767ce 38038+out:
1facf9fc 38039+ return err;
38040+}
38041+
38042+static int xib_restore(struct super_block *sb)
38043+{
acd2b654
AM
38044+ int err, i;
38045+ unsigned int nfile;
5afbbe0d 38046+ aufs_bindex_t bindex, bbot;
1facf9fc 38047+ void *page;
062440b3 38048+ struct au_branch *br;
acd2b654
AM
38049+ struct au_xino *xi;
38050+ struct file *file;
1facf9fc 38051+
38052+ err = -ENOMEM;
38053+ page = (void *)__get_free_page(GFP_NOFS);
38054+ if (unlikely(!page))
38055+ goto out;
38056+
38057+ err = 0;
5afbbe0d
AM
38058+ bbot = au_sbbot(sb);
38059+ for (bindex = 0; !err && bindex <= bbot; bindex++)
062440b3
AM
38060+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38061+ br = au_sbr(sb, bindex);
acd2b654
AM
38062+ xi = br->br_xino;
38063+ nfile = xi->xi_nfile;
38064+ for (i = 0; i < nfile; i++) {
38065+ file = au_xino_file(xi, i);
38066+ if (file)
38067+ err = do_xib_restore(sb, file, page);
38068+ }
062440b3
AM
38069+ } else
38070+ AuDbg("skip shared b%d\n", bindex);
1c60b727 38071+ free_page((unsigned long)page);
1facf9fc 38072+
4f0767ce 38073+out:
1facf9fc 38074+ return err;
38075+}
38076+
38077+int au_xib_trunc(struct super_block *sb)
38078+{
38079+ int err;
38080+ ssize_t sz;
38081+ loff_t pos;
1facf9fc 38082+ struct au_sbinfo *sbinfo;
38083+ unsigned long *p;
38084+ struct file *file;
38085+
dece6358
AM
38086+ SiMustWriteLock(sb);
38087+
1facf9fc 38088+ err = 0;
38089+ sbinfo = au_sbi(sb);
38090+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
38091+ goto out;
38092+
38093+ file = sbinfo->si_xib;
c06a8ce3 38094+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 38095+ goto out;
38096+
062440b3 38097+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
1facf9fc 38098+ err = PTR_ERR(file);
38099+ if (IS_ERR(file))
38100+ goto out;
38101+ fput(sbinfo->si_xib);
38102+ sbinfo->si_xib = file;
38103+
38104+ p = sbinfo->si_xib_buf;
38105+ memset(p, 0, PAGE_SIZE);
38106+ pos = 0;
38107+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
38108+ if (unlikely(sz != PAGE_SIZE)) {
38109+ err = sz;
38110+ AuIOErr("err %d\n", err);
38111+ if (sz >= 0)
38112+ err = -EIO;
38113+ goto out;
38114+ }
38115+
38116+ mutex_lock(&sbinfo->si_xib_mtx);
38117+ /* mnt_want_write() is unnecessary here */
38118+ err = xib_restore(sb);
38119+ mutex_unlock(&sbinfo->si_xib_mtx);
38120+
38121+out:
38122+ return err;
38123+}
38124+
38125+/* ---------------------------------------------------------------------- */
38126+
acd2b654 38127+struct au_xino *au_xino_alloc(unsigned int nfile)
062440b3
AM
38128+{
38129+ struct au_xino *xi;
38130+
38131+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
38132+ if (unlikely(!xi))
38133+ goto out;
acd2b654
AM
38134+ xi->xi_nfile = nfile;
38135+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38136+ if (unlikely(!xi->xi_file))
38137+ goto out_free;
062440b3
AM
38138+
38139+ xi->xi_nondir.total = 8; /* initial size */
38140+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38141+ GFP_NOFS);
38142+ if (unlikely(!xi->xi_nondir.array))
acd2b654 38143+ goto out_file;
062440b3
AM
38144+
38145+ spin_lock_init(&xi->xi_nondir.spin);
38146+ init_waitqueue_head(&xi->xi_nondir.wqh);
acd2b654 38147+ mutex_init(&xi->xi_mtx);
9f237c51 38148+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
062440b3
AM
38149+ atomic_set(&xi->xi_truncating, 0);
38150+ kref_init(&xi->xi_kref);
38151+ goto out; /* success */
38152+
acd2b654 38153+out_file:
9f237c51 38154+ au_kfree_try_rcu(xi->xi_file);
062440b3 38155+out_free:
9f237c51 38156+ au_kfree_rcu(xi);
062440b3
AM
38157+ xi = NULL;
38158+out:
38159+ return xi;
38160+}
38161+
acd2b654 38162+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
062440b3
AM
38163+{
38164+ int err;
38165+ struct au_xino *xi;
38166+
38167+ err = 0;
acd2b654 38168+ xi = au_xino_alloc(idx + 1);
062440b3
AM
38169+ if (unlikely(!xi)) {
38170+ err = -ENOMEM;
38171+ goto out;
38172+ }
38173+
acd2b654
AM
38174+ if (file)
38175+ get_file(file);
38176+ xi->xi_file[idx] = file;
062440b3
AM
38177+ AuDebugOn(br->br_xino);
38178+ br->br_xino = xi;
38179+
38180+out:
38181+ return err;
38182+}
38183+
38184+static void au_xino_release(struct kref *kref)
38185+{
38186+ struct au_xino *xi;
38187+ int i;
9f237c51
AM
38188+ unsigned long ul;
38189+ struct hlist_bl_head *hbl;
38190+ struct hlist_bl_node *pos, *n;
38191+ struct au_xi_writing *p;
062440b3
AM
38192+
38193+ xi = container_of(kref, struct au_xino, xi_kref);
acd2b654
AM
38194+ for (i = 0; i < xi->xi_nfile; i++)
38195+ if (xi->xi_file[i])
38196+ fput(xi->xi_file[i]);
062440b3
AM
38197+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38198+ AuDebugOn(xi->xi_nondir.array[i]);
acd2b654 38199+ mutex_destroy(&xi->xi_mtx);
9f237c51
AM
38200+ hbl = &xi->xi_writing;
38201+ ul = au_hbl_count(hbl);
38202+ if (unlikely(ul)) {
38203+ pr_warn("xi_writing %lu\n", ul);
38204+ hlist_bl_lock(hbl);
38205+ hlist_bl_for_each_entry_safe (p, pos, n, hbl, node) {
38206+ hlist_bl_del(&p->node);
38207+ au_kfree_rcu(p);
38208+ }
38209+ hlist_bl_unlock(hbl);
38210+ }
38211+ au_kfree_try_rcu(xi->xi_file);
38212+ au_kfree_try_rcu(xi->xi_nondir.array);
38213+ au_kfree_rcu(xi);
062440b3
AM
38214+}
38215+
38216+int au_xino_put(struct au_branch *br)
38217+{
38218+ int ret;
38219+ struct au_xino *xi;
38220+
38221+ ret = 0;
38222+ xi = br->br_xino;
38223+ if (xi) {
38224+ br->br_xino = NULL;
38225+ ret = kref_put(&xi->xi_kref, au_xino_release);
38226+ }
38227+
38228+ return ret;
38229+}
38230+
062440b3
AM
38231+/* ---------------------------------------------------------------------- */
38232+
1facf9fc 38233+/*
38234+ * xino mount option handlers
38235+ */
1facf9fc 38236+
38237+/* xino bitmap */
38238+static void xino_clear_xib(struct super_block *sb)
38239+{
38240+ struct au_sbinfo *sbinfo;
38241+
dece6358
AM
38242+ SiMustWriteLock(sb);
38243+
1facf9fc 38244+ sbinfo = au_sbi(sb);
062440b3 38245+ /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
1facf9fc 38246+ if (sbinfo->si_xib)
38247+ fput(sbinfo->si_xib);
38248+ sbinfo->si_xib = NULL;
f0c0a007 38249+ if (sbinfo->si_xib_buf)
1c60b727 38250+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 38251+ sbinfo->si_xib_buf = NULL;
38252+}
38253+
062440b3 38254+static int au_xino_set_xib(struct super_block *sb, struct path *path)
1facf9fc 38255+{
38256+ int err;
38257+ loff_t pos;
38258+ struct au_sbinfo *sbinfo;
38259+ struct file *file;
acd2b654 38260+ struct super_block *xi_sb;
1facf9fc 38261+
dece6358
AM
38262+ SiMustWriteLock(sb);
38263+
1facf9fc 38264+ sbinfo = au_sbi(sb);
062440b3 38265+ file = au_xino_create2(sb, path, sbinfo->si_xib);
1facf9fc 38266+ err = PTR_ERR(file);
38267+ if (IS_ERR(file))
38268+ goto out;
38269+ if (sbinfo->si_xib)
38270+ fput(sbinfo->si_xib);
38271+ sbinfo->si_xib = file;
5527c038
JR
38272+ sbinfo->si_xread = vfs_readf(file);
38273+ sbinfo->si_xwrite = vfs_writef(file);
acd2b654
AM
38274+ xi_sb = file_inode(file)->i_sb;
38275+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38276+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38277+ err = -EIO;
38278+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38279+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38280+ goto out_unset;
38281+ }
38282+ sbinfo->si_ximaxent /= sizeof(ino_t);
1facf9fc 38283+
38284+ err = -ENOMEM;
38285+ if (!sbinfo->si_xib_buf)
38286+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38287+ if (unlikely(!sbinfo->si_xib_buf))
38288+ goto out_unset;
38289+
38290+ sbinfo->si_xib_last_pindex = 0;
38291+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 38292+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 38293+ pos = 0;
38294+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
38295+ PAGE_SIZE, &pos);
38296+ if (unlikely(err != PAGE_SIZE))
38297+ goto out_free;
38298+ }
38299+ err = 0;
38300+ goto out; /* success */
38301+
4f0767ce 38302+out_free:
f0c0a007 38303+ if (sbinfo->si_xib_buf)
1c60b727 38304+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
38305+ sbinfo->si_xib_buf = NULL;
38306+ if (err >= 0)
38307+ err = -EIO;
4f0767ce 38308+out_unset:
b752ccd1
AM
38309+ fput(sbinfo->si_xib);
38310+ sbinfo->si_xib = NULL;
4f0767ce 38311+out:
062440b3 38312+ AuTraceErr(err);
b752ccd1 38313+ return err;
1facf9fc 38314+}
38315+
b752ccd1
AM
38316+/* xino for each branch */
38317+static void xino_clear_br(struct super_block *sb)
38318+{
5afbbe0d 38319+ aufs_bindex_t bindex, bbot;
b752ccd1 38320+ struct au_branch *br;
1facf9fc 38321+
5afbbe0d
AM
38322+ bbot = au_sbbot(sb);
38323+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1 38324+ br = au_sbr(sb, bindex);
062440b3
AM
38325+ AuDebugOn(!br);
38326+ au_xino_put(br);
38327+ }
38328+}
38329+
38330+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38331+ aufs_bindex_t bshared)
38332+{
38333+ struct au_branch *brshared;
b752ccd1 38334+
062440b3
AM
38335+ brshared = au_sbr(sb, bshared);
38336+ AuDebugOn(!brshared->br_xino);
38337+ AuDebugOn(!brshared->br_xino->xi_file);
38338+ if (br->br_xino != brshared->br_xino) {
38339+ au_xino_get(brshared);
38340+ au_xino_put(br);
38341+ br->br_xino = brshared->br_xino;
b752ccd1
AM
38342+ }
38343+}
38344+
062440b3
AM
38345+struct au_xino_do_set_br {
38346+ vfs_writef_t writef;
38347+ struct au_branch *br;
38348+ ino_t h_ino;
38349+ aufs_bindex_t bshared;
38350+};
38351+
38352+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38353+ struct au_xino_do_set_br *args)
1facf9fc 38354+{
38355+ int err;
acd2b654 38356+ struct au_xi_calc calc;
062440b3 38357+ struct file *file;
acd2b654
AM
38358+ struct au_branch *br;
38359+ struct au_xi_new xinew = {
38360+ .base = path
38361+ };
062440b3 38362+
acd2b654
AM
38363+ br = args->br;
38364+ xinew.xi = br->br_xino;
38365+ au_xi_calc(sb, args->h_ino, &calc);
38366+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38367+ if (args->bshared >= 0)
062440b3 38368+ /* shared xino */
acd2b654
AM
38369+ au_xino_set_br_shared(sb, br, args->bshared);
38370+ else if (!xinew.xi) {
38371+ /* new xino */
38372+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38373+ if (unlikely(err))
38374+ goto out;
062440b3
AM
38375+ }
38376+
acd2b654
AM
38377+ /* force re-creating */
38378+ xinew.xi = br->br_xino;
38379+ xinew.idx = calc.idx;
38380+ mutex_lock(&xinew.xi->xi_mtx);
38381+ file = au_xi_new(sb, &xinew);
38382+ mutex_unlock(&xinew.xi->xi_mtx);
062440b3
AM
38383+ err = PTR_ERR(file);
38384+ if (IS_ERR(file))
38385+ goto out;
acd2b654
AM
38386+ AuDebugOn(!file);
38387+
38388+ err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
38389+ if (unlikely(err))
38390+ au_xino_put(br);
062440b3 38391+
062440b3
AM
38392+out:
38393+ AuTraceErr(err);
38394+ return err;
38395+}
38396+
38397+static int au_xino_set_br(struct super_block *sb, struct path *path)
38398+{
38399+ int err;
38400+ aufs_bindex_t bindex, bbot;
38401+ struct au_xino_do_set_br args;
b752ccd1 38402+ struct inode *inode;
1facf9fc 38403+
b752ccd1
AM
38404+ SiMustWriteLock(sb);
38405+
5afbbe0d 38406+ bbot = au_sbbot(sb);
5527c038 38407+ inode = d_inode(sb->s_root);
062440b3
AM
38408+ args.writef = au_sbi(sb)->si_xwrite;
38409+ for (bindex = 0; bindex <= bbot; bindex++) {
38410+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38411+ args.br = au_sbr(sb, bindex);
38412+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38413+ err = au_xino_do_set_br(sb, path, &args);
b752ccd1 38414+ if (unlikely(err))
062440b3 38415+ break;
b752ccd1 38416+ }
1facf9fc 38417+
062440b3 38418+ AuTraceErr(err);
1facf9fc 38419+ return err;
38420+}
b752ccd1
AM
38421+
38422+void au_xino_clr(struct super_block *sb)
38423+{
38424+ struct au_sbinfo *sbinfo;
38425+
38426+ au_xigen_clr(sb);
38427+ xino_clear_xib(sb);
38428+ xino_clear_br(sb);
062440b3 38429+ dbgaufs_brs_del(sb, 0);
b752ccd1
AM
38430+ sbinfo = au_sbi(sb);
38431+ /* lvalue, do not call au_mntflags() */
38432+ au_opt_clr(sbinfo->si_mntflags, XINO);
38433+}
38434+
062440b3 38435+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
b752ccd1
AM
38436+{
38437+ int err, skip;
062440b3 38438+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
b752ccd1
AM
38439+ struct qstr *dname, *cur_name;
38440+ struct file *cur_xino;
b752ccd1 38441+ struct au_sbinfo *sbinfo;
062440b3 38442+ struct path *path, *cur_path;
b752ccd1
AM
38443+
38444+ SiMustWriteLock(sb);
38445+
38446+ err = 0;
38447+ sbinfo = au_sbi(sb);
062440b3
AM
38448+ path = &xiopt->file->f_path;
38449+ dentry = path->dentry;
38450+ parent = dget_parent(dentry);
b752ccd1
AM
38451+ if (remount) {
38452+ skip = 0;
b752ccd1
AM
38453+ cur_xino = sbinfo->si_xib;
38454+ if (cur_xino) {
062440b3
AM
38455+ cur_path = &cur_xino->f_path;
38456+ cur_dentry = cur_path->dentry;
38457+ cur_parent = dget_parent(cur_dentry);
38458+ cur_name = &cur_dentry->d_name;
38459+ dname = &dentry->d_name;
b752ccd1 38460+ skip = (cur_parent == parent
38d290e6 38461+ && au_qstreq(dname, cur_name));
b752ccd1
AM
38462+ dput(cur_parent);
38463+ }
38464+ if (skip)
38465+ goto out;
38466+ }
38467+
38468+ au_opt_set(sbinfo->si_mntflags, XINO);
062440b3
AM
38469+ err = au_xino_set_xib(sb, path);
38470+ /* si_x{read,write} are set */
b752ccd1 38471+ if (!err)
062440b3 38472+ err = au_xigen_set(sb, path);
b752ccd1 38473+ if (!err)
062440b3
AM
38474+ err = au_xino_set_br(sb, path);
38475+ if (!err) {
38476+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
b752ccd1 38477+ goto out; /* success */
062440b3 38478+ }
b752ccd1
AM
38479+
38480+ /* reset all */
062440b3
AM
38481+ AuIOErr("failed setting xino(%d).\n", err);
38482+ au_xino_clr(sb);
b752ccd1 38483+
4f0767ce 38484+out:
b752ccd1
AM
38485+ dput(parent);
38486+ return err;
38487+}
38488+
b752ccd1
AM
38489+/*
38490+ * create a xinofile at the default place/path.
38491+ */
38492+struct file *au_xino_def(struct super_block *sb)
38493+{
38494+ struct file *file;
38495+ char *page, *p;
38496+ struct au_branch *br;
38497+ struct super_block *h_sb;
38498+ struct path path;
5afbbe0d 38499+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
38500+
38501+ br = NULL;
5afbbe0d 38502+ bbot = au_sbbot(sb);
b752ccd1 38503+ bwr = -1;
5afbbe0d 38504+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
38505+ br = au_sbr(sb, bindex);
38506+ if (au_br_writable(br->br_perm)
86dc4139 38507+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
38508+ bwr = bindex;
38509+ break;
38510+ }
38511+ }
38512+
7f207e10
AM
38513+ if (bwr >= 0) {
38514+ file = ERR_PTR(-ENOMEM);
537831f9 38515+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
38516+ if (unlikely(!page))
38517+ goto out;
86dc4139 38518+ path.mnt = au_br_mnt(br);
7f207e10
AM
38519+ path.dentry = au_h_dptr(sb->s_root, bwr);
38520+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38521+ file = (void *)p;
38522+ if (!IS_ERR(p)) {
38523+ strcat(p, "/" AUFS_XINO_FNAME);
38524+ AuDbg("%s\n", p);
83b672a5 38525+ file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
7f207e10 38526+ }
1c60b727 38527+ free_page((unsigned long)page);
7f207e10 38528+ } else {
83b672a5
AM
38529+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38530+ /*wbrtop*/0);
7f207e10
AM
38531+ if (IS_ERR(file))
38532+ goto out;
2000de60 38533+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
38534+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38535+ pr_err("xino doesn't support %s(%s)\n",
38536+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38537+ fput(file);
38538+ file = ERR_PTR(-EINVAL);
38539+ }
7f207e10 38540+ }
0c5527e5 38541+
7f207e10
AM
38542+out:
38543+ return file;
38544+}
38545+
38546+/* ---------------------------------------------------------------------- */
38547+
062440b3
AM
38548+/*
38549+ * initialize the xinofile for the specified branch @br
38550+ * at the place/path where @base_file indicates.
38551+ * test whether another branch is on the same filesystem or not,
38552+ * if found then share the xinofile with another branch.
38553+ */
38554+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38555+ struct path *base)
7f207e10
AM
38556+{
38557+ int err;
062440b3
AM
38558+ struct au_xino_do_set_br args = {
38559+ .h_ino = h_ino,
38560+ .br = br
38561+ };
7f207e10 38562+
062440b3
AM
38563+ args.writef = au_sbi(sb)->si_xwrite;
38564+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38565+ au_br_sb(br));
38566+ err = au_xino_do_set_br(sb, base, &args);
79b8bda9 38567+ if (unlikely(err))
062440b3 38568+ au_xino_put(br);
7f207e10 38569+
7f207e10
AM
38570+ return err;
38571+}
521ced18
JR
38572+
38573+/* ---------------------------------------------------------------------- */
38574+
062440b3
AM
38575+/*
38576+ * get an unused inode number from bitmap
38577+ */
38578+ino_t au_xino_new_ino(struct super_block *sb)
38579+{
38580+ ino_t ino;
38581+ unsigned long *p, pindex, ul, pend;
38582+ struct au_sbinfo *sbinfo;
38583+ struct file *file;
38584+ int free_bit, err;
38585+
38586+ if (!au_opt_test(au_mntflags(sb), XINO))
38587+ return iunique(sb, AUFS_FIRST_INO);
38588+
38589+ sbinfo = au_sbi(sb);
38590+ mutex_lock(&sbinfo->si_xib_mtx);
38591+ p = sbinfo->si_xib_buf;
38592+ free_bit = sbinfo->si_xib_next_bit;
38593+ if (free_bit < page_bits && !test_bit(free_bit, p))
38594+ goto out; /* success */
38595+ free_bit = find_first_zero_bit(p, page_bits);
38596+ if (free_bit < page_bits)
38597+ goto out; /* success */
38598+
38599+ pindex = sbinfo->si_xib_last_pindex;
38600+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38601+ err = xib_pindex(sb, ul);
38602+ if (unlikely(err))
38603+ goto out_err;
38604+ free_bit = find_first_zero_bit(p, page_bits);
38605+ if (free_bit < page_bits)
38606+ goto out; /* success */
38607+ }
38608+
38609+ file = sbinfo->si_xib;
38610+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38611+ for (ul = pindex + 1; ul <= pend; ul++) {
38612+ err = xib_pindex(sb, ul);
38613+ if (unlikely(err))
38614+ goto out_err;
38615+ free_bit = find_first_zero_bit(p, page_bits);
38616+ if (free_bit < page_bits)
38617+ goto out; /* success */
38618+ }
38619+ BUG();
38620+
38621+out:
38622+ set_bit(free_bit, p);
38623+ sbinfo->si_xib_next_bit = free_bit + 1;
38624+ pindex = sbinfo->si_xib_last_pindex;
38625+ mutex_unlock(&sbinfo->si_xib_mtx);
38626+ ino = xib_calc_ino(pindex, free_bit);
38627+ AuDbg("i%lu\n", (unsigned long)ino);
38628+ return ino;
38629+out_err:
38630+ mutex_unlock(&sbinfo->si_xib_mtx);
38631+ AuDbg("i0\n");
38632+ return 0;
38633+}
38634+
38635+/* for s_op->delete_inode() */
38636+void au_xino_delete_inode(struct inode *inode, const int unlinked)
521ced18 38637+{
062440b3
AM
38638+ int err;
38639+ unsigned int mnt_flags;
38640+ aufs_bindex_t bindex, bbot, bi;
38641+ unsigned char try_trunc;
38642+ struct au_iinfo *iinfo;
38643+ struct super_block *sb;
38644+ struct au_hinode *hi;
38645+ struct inode *h_inode;
38646+ struct au_branch *br;
38647+ vfs_writef_t xwrite;
acd2b654
AM
38648+ struct au_xi_calc calc;
38649+ struct file *file;
521ced18 38650+
062440b3 38651+ AuDebugOn(au_is_bad_inode(inode));
521ced18 38652+
062440b3
AM
38653+ sb = inode->i_sb;
38654+ mnt_flags = au_mntflags(sb);
38655+ if (!au_opt_test(mnt_flags, XINO)
38656+ || inode->i_ino == AUFS_ROOT_INO)
38657+ return;
38658+
38659+ if (unlinked) {
38660+ au_xigen_inc(inode);
38661+ au_xib_clear_bit(inode);
38662+ }
38663+
38664+ iinfo = au_ii(inode);
38665+ bindex = iinfo->ii_btop;
38666+ if (bindex < 0)
38667+ return;
38668+
38669+ xwrite = au_sbi(sb)->si_xwrite;
38670+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38671+ hi = au_hinode(iinfo, bindex);
38672+ bbot = iinfo->ii_bbot;
38673+ for (; bindex <= bbot; bindex++, hi++) {
38674+ h_inode = hi->hi_inode;
38675+ if (!h_inode
38676+ || (!unlinked && h_inode->i_nlink))
38677+ continue;
38678+
38679+ /* inode may not be revalidated */
38680+ bi = au_br_index(sb, hi->hi_id);
38681+ if (bi < 0)
38682+ continue;
38683+
38684+ br = au_sbr(sb, bi);
acd2b654
AM
38685+ au_xi_calc(sb, h_inode->i_ino, &calc);
38686+ file = au_xino_file(br->br_xino, calc.idx);
38687+ if (IS_ERR_OR_NULL(file))
38688+ continue;
38689+
38690+ err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
062440b3
AM
38691+ if (!err && try_trunc
38692+ && au_test_fs_trunc_xino(au_br_sb(br)))
38693+ xino_try_trunc(sb, br);
38694+ }
521ced18
JR
38695+}
38696+
062440b3
AM
38697+/* ---------------------------------------------------------------------- */
38698+
38699+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
521ced18
JR
38700+{
38701+ int found, total, i;
38702+
38703+ found = -1;
062440b3 38704+ total = xi->xi_nondir.total;
521ced18 38705+ for (i = 0; i < total; i++) {
062440b3 38706+ if (xi->xi_nondir.array[i] != h_ino)
521ced18
JR
38707+ continue;
38708+ found = i;
38709+ break;
38710+ }
38711+
38712+ return found;
38713+}
38714+
062440b3 38715+static int au_xinondir_expand(struct au_xino *xi)
521ced18
JR
38716+{
38717+ int err, sz;
38718+ ino_t *p;
38719+
38720+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38721+
38722+ err = -ENOMEM;
062440b3 38723+ sz = xi->xi_nondir.total * sizeof(ino_t);
521ced18
JR
38724+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38725+ goto out;
062440b3 38726+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
521ced18
JR
38727+ /*may_shrink*/0);
38728+ if (p) {
062440b3
AM
38729+ xi->xi_nondir.array = p;
38730+ xi->xi_nondir.total <<= 1;
38731+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
521ced18
JR
38732+ err = 0;
38733+ }
38734+
38735+out:
38736+ return err;
38737+}
38738+
062440b3
AM
38739+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38740+ ino_t h_ino, int idx)
38741+{
38742+ struct au_xino *xi;
38743+
38744+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38745+ xi = au_sbr(sb, bindex)->br_xino;
38746+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38747+
38748+ spin_lock(&xi->xi_nondir.spin);
38749+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38750+ xi->xi_nondir.array[idx] = 0;
38751+ spin_unlock(&xi->xi_nondir.spin);
38752+ wake_up_all(&xi->xi_nondir.wqh);
38753+}
38754+
521ced18
JR
38755+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38756+ int *idx)
38757+{
38758+ int err, found, empty;
062440b3 38759+ struct au_xino *xi;
521ced18
JR
38760+
38761+ err = 0;
38762+ *idx = -1;
38763+ if (!au_opt_test(au_mntflags(sb), XINO))
38764+ goto out; /* no xino */
38765+
062440b3 38766+ xi = au_sbr(sb, bindex)->br_xino;
521ced18
JR
38767+
38768+again:
062440b3
AM
38769+ spin_lock(&xi->xi_nondir.spin);
38770+ found = au_xinondir_find(xi, h_ino);
521ced18 38771+ if (found == -1) {
062440b3 38772+ empty = au_xinondir_find(xi, /*h_ino*/0);
521ced18 38773+ if (empty == -1) {
062440b3
AM
38774+ empty = xi->xi_nondir.total;
38775+ err = au_xinondir_expand(xi);
521ced18
JR
38776+ if (unlikely(err))
38777+ goto out_unlock;
38778+ }
062440b3 38779+ xi->xi_nondir.array[empty] = h_ino;
521ced18
JR
38780+ *idx = empty;
38781+ } else {
062440b3
AM
38782+ spin_unlock(&xi->xi_nondir.spin);
38783+ wait_event(xi->xi_nondir.wqh,
38784+ xi->xi_nondir.array[found] != h_ino);
521ced18
JR
38785+ goto again;
38786+ }
38787+
38788+out_unlock:
062440b3
AM
38789+ spin_unlock(&xi->xi_nondir.spin);
38790+out:
38791+ return err;
38792+}
38793+
38794+/* ---------------------------------------------------------------------- */
38795+
38796+int au_xino_path(struct seq_file *seq, struct file *file)
38797+{
38798+ int err;
38799+
38800+ err = au_seq_path(seq, &file->f_path);
38801+ if (unlikely(err))
38802+ goto out;
38803+
38804+#define Deleted "\\040(deleted)"
38805+ seq->count -= sizeof(Deleted) - 1;
38806+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38807+ sizeof(Deleted) - 1));
38808+#undef Deleted
38809+
521ced18
JR
38810+out:
38811+ return err;
38812+}
537831f9 38813diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
eca34b5c 38814--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
9f00928d 38815+++ linux/include/uapi/linux/aufs_type.h 2019-11-25 12:42:00.275843147 +0100
eca34b5c
AM
38816@@ -0,0 +1,452 @@
38817+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
7f207e10 38818+/*
ba1aed25 38819+ * Copyright (C) 2005-2019 Junjiro R. Okajima
7f207e10
AM
38820+ *
38821+ * This program, aufs is free software; you can redistribute it and/or modify
38822+ * it under the terms of the GNU General Public License as published by
38823+ * the Free Software Foundation; either version 2 of the License, or
38824+ * (at your option) any later version.
38825+ *
38826+ * This program is distributed in the hope that it will be useful,
38827+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38828+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38829+ * GNU General Public License for more details.
38830+ *
38831+ * You should have received a copy of the GNU General Public License
523b37e3 38832+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
38833+ */
38834+
38835+#ifndef __AUFS_TYPE_H__
38836+#define __AUFS_TYPE_H__
38837+
f6c5ef8b
AM
38838+#define AUFS_NAME "aufs"
38839+
9dbd164d 38840+#ifdef __KERNEL__
f6c5ef8b
AM
38841+/*
38842+ * define it before including all other headers.
38843+ * sched.h may use pr_* macros before defining "current", so define the
38844+ * no-current version first, and re-define later.
38845+ */
38846+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38847+#include <linux/sched.h>
38848+#undef pr_fmt
a2a7ad62
AM
38849+#define pr_fmt(fmt) \
38850+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38851+ (int)sizeof(current->comm), current->comm, current->pid
9dbd164d
AM
38852+#else
38853+#include <stdint.h>
38854+#include <sys/types.h>
f6c5ef8b 38855+#endif /* __KERNEL__ */
7f207e10 38856+
f6c5ef8b
AM
38857+#include <linux/limits.h>
38858+
9f00928d 38859+#define AUFS_VERSION "5.x-rcN-20191021"
7f207e10
AM
38860+
38861+/* todo? move this to linux-2.6.19/include/magic.h */
38862+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38863+
38864+/* ---------------------------------------------------------------------- */
38865+
38866+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 38867+typedef int8_t aufs_bindex_t;
7f207e10
AM
38868+#define AUFS_BRANCH_MAX 127
38869+#else
9dbd164d 38870+typedef int16_t aufs_bindex_t;
7f207e10
AM
38871+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38872+#define AUFS_BRANCH_MAX 511
38873+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38874+#define AUFS_BRANCH_MAX 1023
38875+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38876+#define AUFS_BRANCH_MAX 32767
38877+#endif
38878+#endif
38879+
38880+#ifdef __KERNEL__
38881+#ifndef AUFS_BRANCH_MAX
38882+#error unknown CONFIG_AUFS_BRANCH_MAX value
38883+#endif
38884+#endif /* __KERNEL__ */
38885+
38886+/* ---------------------------------------------------------------------- */
38887+
7f207e10
AM
38888+#define AUFS_FSTYPE AUFS_NAME
38889+
38890+#define AUFS_ROOT_INO 2
38891+#define AUFS_FIRST_INO 11
38892+
38893+#define AUFS_WH_PFX ".wh."
38894+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38895+#define AUFS_WH_TMP_LEN 4
86dc4139 38896+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
38897+#define AUFS_MAX_NAMELEN (NAME_MAX \
38898+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38899+ - 1 /* dot */\
38900+ - AUFS_WH_TMP_LEN) /* hex */
38901+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38902+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
38903+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38904+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
38905+#define AUFS_DIRWH_DEF 3
38906+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 38907+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
38908+#define AUFS_RDBLK_DEF 512 /* bytes */
38909+#define AUFS_RDHASH_DEF 32
38910+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
38911+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38912+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 38913+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 38914+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
38915+
38916+/* pseudo-link maintenace under /proc */
38917+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38918+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38919+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38920+
8b6a4947
AM
38921+/* dirren, renamed dir */
38922+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38923+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38924+/* whiteouted doubly */
38925+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38926+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38927+
7f207e10
AM
38928+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38929+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38930+
38931+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38932+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38933+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38934+
38935+/* doubly whiteouted */
38936+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38937+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38938+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38939+
1e00d052 38940+/* branch permissions and attributes */
7f207e10
AM
38941+#define AUFS_BRPERM_RW "rw"
38942+#define AUFS_BRPERM_RO "ro"
38943+#define AUFS_BRPERM_RR "rr"
076b876e
AM
38944+#define AUFS_BRATTR_COO_REG "coo_reg"
38945+#define AUFS_BRATTR_COO_ALL "coo_all"
38946+#define AUFS_BRATTR_FHSM "fhsm"
38947+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
38948+#define AUFS_BRATTR_ICEX "icex"
38949+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38950+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38951+#define AUFS_BRATTR_ICEX_TR "icextr"
38952+#define AUFS_BRATTR_ICEX_USR "icexusr"
38953+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
38954+#define AUFS_BRRATTR_WH "wh"
38955+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
38956+#define AUFS_BRWATTR_MOO "moo"
38957+
38958+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38959+#define AuBrPerm_RO (1 << 1) /* readonly */
38960+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38961+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38962+
38963+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38964+#define AuBrAttr_COO_ALL (1 << 4)
38965+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38966+
38967+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38968+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
38969+ branch. meaningless since
38970+ linux-3.18-rc1 */
38971+
38972+/* ignore error in copying XATTR */
38973+#define AuBrAttr_ICEX_SEC (1 << 7)
38974+#define AuBrAttr_ICEX_SYS (1 << 8)
38975+#define AuBrAttr_ICEX_TR (1 << 9)
38976+#define AuBrAttr_ICEX_USR (1 << 10)
38977+#define AuBrAttr_ICEX_OTH (1 << 11)
38978+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38979+ | AuBrAttr_ICEX_SYS \
38980+ | AuBrAttr_ICEX_TR \
38981+ | AuBrAttr_ICEX_USR \
38982+ | AuBrAttr_ICEX_OTH)
38983+
38984+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
38985+#define AuBrRAttr_Mask AuBrRAttr_WH
38986+
c1595e42
JR
38987+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
38988+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
38989+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38990+
38991+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38992+
c1595e42 38993+/* #warning test userspace */
076b876e
AM
38994+#ifdef __KERNEL__
38995+#ifndef CONFIG_AUFS_FHSM
38996+#undef AuBrAttr_FHSM
38997+#define AuBrAttr_FHSM 0
38998+#endif
c1595e42
JR
38999+#ifndef CONFIG_AUFS_XATTR
39000+#undef AuBrAttr_ICEX
39001+#define AuBrAttr_ICEX 0
39002+#undef AuBrAttr_ICEX_SEC
39003+#define AuBrAttr_ICEX_SEC 0
39004+#undef AuBrAttr_ICEX_SYS
39005+#define AuBrAttr_ICEX_SYS 0
39006+#undef AuBrAttr_ICEX_TR
39007+#define AuBrAttr_ICEX_TR 0
39008+#undef AuBrAttr_ICEX_USR
39009+#define AuBrAttr_ICEX_USR 0
39010+#undef AuBrAttr_ICEX_OTH
39011+#define AuBrAttr_ICEX_OTH 0
39012+#endif
076b876e
AM
39013+#endif
39014+
39015+/* the longest combination */
c1595e42
JR
39016+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39017+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
39018+ "+" AUFS_BRATTR_COO_REG \
39019+ "+" AUFS_BRATTR_FHSM \
39020+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
39021+ "+" AUFS_BRATTR_ICEX_SEC \
39022+ "+" AUFS_BRATTR_ICEX_SYS \
39023+ "+" AUFS_BRATTR_ICEX_USR \
39024+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
39025+ "+" AUFS_BRWATTR_NLWH)
39026+
39027+typedef struct {
39028+ char a[AuBrPermStrSz];
39029+} au_br_perm_str_t;
39030+
39031+static inline int au_br_writable(int brperm)
39032+{
39033+ return brperm & AuBrPerm_RW;
39034+}
39035+
39036+static inline int au_br_whable(int brperm)
39037+{
39038+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39039+}
39040+
39041+static inline int au_br_wh_linkable(int brperm)
39042+{
39043+ return !(brperm & AuBrWAttr_NoLinkWH);
39044+}
39045+
39046+static inline int au_br_cmoo(int brperm)
39047+{
39048+ return brperm & AuBrAttr_CMOO_Mask;
39049+}
39050+
39051+static inline int au_br_fhsm(int brperm)
39052+{
39053+ return brperm & AuBrAttr_FHSM;
39054+}
7f207e10
AM
39055+
39056+/* ---------------------------------------------------------------------- */
39057+
39058+/* ioctl */
39059+enum {
39060+ /* readdir in userspace */
39061+ AuCtl_RDU,
39062+ AuCtl_RDU_INO,
39063+
076b876e
AM
39064+ AuCtl_WBR_FD, /* pathconf wrapper */
39065+ AuCtl_IBUSY, /* busy inode */
39066+ AuCtl_MVDOWN, /* move-down */
39067+ AuCtl_BR, /* info about branches */
39068+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
39069+};
39070+
39071+/* borrowed from linux/include/linux/kernel.h */
39072+#ifndef ALIGN
eca34b5c 39073+#ifdef _GNU_SOURCE
7f207e10 39074+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
eca34b5c
AM
39075+#else
39076+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
39077+#endif
7f207e10
AM
39078+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
39079+#endif
39080+
39081+/* borrowed from linux/include/linux/compiler-gcc3.h */
39082+#ifndef __aligned
39083+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
39084+#endif
39085+
39086+#ifdef __KERNEL__
39087+#ifndef __packed
7f207e10
AM
39088+#define __packed __attribute__((packed))
39089+#endif
53392da6 39090+#endif
7f207e10
AM
39091+
39092+struct au_rdu_cookie {
9dbd164d
AM
39093+ uint64_t h_pos;
39094+ int16_t bindex;
39095+ uint8_t flags;
39096+ uint8_t pad;
39097+ uint32_t generation;
7f207e10
AM
39098+} __aligned(8);
39099+
39100+struct au_rdu_ent {
9dbd164d
AM
39101+ uint64_t ino;
39102+ int16_t bindex;
39103+ uint8_t type;
39104+ uint8_t nlen;
39105+ uint8_t wh;
7f207e10
AM
39106+ char name[0];
39107+} __aligned(8);
39108+
39109+static inline int au_rdu_len(int nlen)
39110+{
39111+ /* include the terminating NULL */
39112+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 39113+ sizeof(uint64_t));
7f207e10
AM
39114+}
39115+
39116+union au_rdu_ent_ul {
39117+ struct au_rdu_ent __user *e;
9dbd164d 39118+ uint64_t ul;
7f207e10
AM
39119+};
39120+
39121+enum {
39122+ AufsCtlRduV_SZ,
39123+ AufsCtlRduV_End
39124+};
39125+
39126+struct aufs_rdu {
39127+ /* input */
39128+ union {
9dbd164d
AM
39129+ uint64_t sz; /* AuCtl_RDU */
39130+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
39131+ };
39132+ union au_rdu_ent_ul ent;
9dbd164d 39133+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
39134+
39135+ /* input/output */
9dbd164d 39136+ uint32_t blk;
7f207e10
AM
39137+
39138+ /* output */
39139+ union au_rdu_ent_ul tail;
39140+ /* number of entries which were added in a single call */
9dbd164d
AM
39141+ uint64_t rent;
39142+ uint8_t full;
39143+ uint8_t shwh;
7f207e10
AM
39144+
39145+ struct au_rdu_cookie cookie;
39146+} __aligned(8);
39147+
1e00d052
AM
39148+/* ---------------------------------------------------------------------- */
39149+
8b6a4947
AM
39150+/* dirren. the branch is identified by the filename who contains this */
39151+struct au_drinfo {
39152+ uint64_t ino;
39153+ union {
39154+ uint8_t oldnamelen;
39155+ uint64_t _padding;
39156+ };
39157+ uint8_t oldname[0];
39158+} __aligned(8);
39159+
39160+struct au_drinfo_fdata {
39161+ uint32_t magic;
39162+ struct au_drinfo drinfo;
39163+} __aligned(8);
39164+
39165+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39166+/* future */
39167+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39168+
39169+/* ---------------------------------------------------------------------- */
39170+
1e00d052 39171+struct aufs_wbr_fd {
9dbd164d
AM
39172+ uint32_t oflags;
39173+ int16_t brid;
1e00d052
AM
39174+} __aligned(8);
39175+
39176+/* ---------------------------------------------------------------------- */
39177+
027c5e7a 39178+struct aufs_ibusy {
9dbd164d
AM
39179+ uint64_t ino, h_ino;
39180+ int16_t bindex;
027c5e7a
AM
39181+} __aligned(8);
39182+
1e00d052
AM
39183+/* ---------------------------------------------------------------------- */
39184+
392086de
AM
39185+/* error code for move-down */
39186+/* the actual message strings are implemented in aufs-util.git */
39187+enum {
39188+ EAU_MVDOWN_OPAQUE = 1,
39189+ EAU_MVDOWN_WHITEOUT,
39190+ EAU_MVDOWN_UPPER,
39191+ EAU_MVDOWN_BOTTOM,
39192+ EAU_MVDOWN_NOUPPER,
39193+ EAU_MVDOWN_NOLOWERBR,
39194+ EAU_Last
39195+};
39196+
c2b27bf2 39197+/* flags for move-down */
392086de
AM
39198+#define AUFS_MVDOWN_DMSG 1
39199+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39200+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39201+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39202+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39203+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39204+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39205+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39206+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
39207+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39208+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39209+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39210+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 39211+
076b876e 39212+/* index for move-down */
392086de
AM
39213+enum {
39214+ AUFS_MVDOWN_UPPER,
39215+ AUFS_MVDOWN_LOWER,
39216+ AUFS_MVDOWN_NARRAY
39217+};
39218+
076b876e
AM
39219+/*
39220+ * additional info of move-down
39221+ * number of free blocks and inodes.
39222+ * subset of struct kstatfs, but smaller and always 64bit.
39223+ */
39224+struct aufs_stfs {
39225+ uint64_t f_blocks;
39226+ uint64_t f_bavail;
39227+ uint64_t f_files;
39228+ uint64_t f_ffree;
39229+};
39230+
39231+struct aufs_stbr {
39232+ int16_t brid; /* optional input */
39233+ int16_t bindex; /* output */
39234+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39235+} __aligned(8);
39236+
c2b27bf2 39237+struct aufs_mvdown {
076b876e
AM
39238+ uint32_t flags; /* input/output */
39239+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39240+ int8_t au_errno; /* output */
39241+} __aligned(8);
39242+
39243+/* ---------------------------------------------------------------------- */
39244+
39245+union aufs_brinfo {
39246+ /* PATH_MAX may differ between kernel-space and user-space */
39247+ char _spacer[4096];
392086de 39248+ struct {
076b876e
AM
39249+ int16_t id;
39250+ int perm;
39251+ char path[0];
39252+ };
c2b27bf2
AM
39253+} __aligned(8);
39254+
39255+/* ---------------------------------------------------------------------- */
39256+
7f207e10
AM
39257+#define AuCtlType 'A'
39258+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39259+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
39260+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39261+ struct aufs_wbr_fd)
027c5e7a 39262+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
39263+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39264+ struct aufs_mvdown)
076b876e
AM
39265+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39266+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
39267+
39268+#endif /* __AUFS_TYPE_H__ */
2121bcd9 39269SPDX-License-Identifier: GPL-2.0
fbc438ed 39270aufs5.x-rcN loopback patch
5527c038
JR
39271
39272diff --git a/drivers/block/loop.c b/drivers/block/loop.c
eca34b5c 39273index 80d06084b043..a83cdfec7359 100644
5527c038
JR
39274--- a/drivers/block/loop.c
39275+++ b/drivers/block/loop.c
eca34b5c 39276@@ -625,6 +625,15 @@ static inline void loop_update_dio(struct loop_device *lo)
8b6a4947 39277 lo->use_dio);
5527c038
JR
39278 }
39279
5527c038
JR
39280+static struct file *loop_real_file(struct file *file)
39281+{
39282+ struct file *f = NULL;
39283+
39284+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39285+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39286+ return f;
8b6a4947
AM
39287+}
39288+
c2c0f25c 39289 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
39290 struct block_device *bdev)
39291 {
eca34b5c 39292@@ -678,6 +687,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
39293 unsigned int arg)
39294 {
e1106418 39295 struct file *file = NULL, *old_file;
5527c038 39296+ struct file *f, *virt_file = NULL, *old_virt_file;
5527c038 39297 int error;
ba1aed25 39298 bool partscan;
5527c038 39299
eca34b5c 39300@@ -697,12 +707,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
cd7a4cd9
AM
39301 file = fget(arg);
39302 if (!file)
e1106418 39303 goto out_err;
5527c038
JR
39304+ f = loop_real_file(file);
39305+ if (f) {
39306+ virt_file = file;
39307+ file = f;
39308+ get_file(file);
39309+ }
39310
cd7a4cd9
AM
39311 error = loop_validate_file(file, bdev);
39312 if (error)
e1106418 39313 goto out_err;
cd7a4cd9 39314
5527c038
JR
39315 old_file = lo->lo_backing_file;
39316+ old_virt_file = lo->lo_backing_virt_file;
39317
39318 error = -EINVAL;
39319
eca34b5c 39320@@ -714,6 +731,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
39321 blk_mq_freeze_queue(lo->lo_queue);
39322 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39323 lo->lo_backing_file = file;
39324+ lo->lo_backing_virt_file = virt_file;
39325 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39326 mapping_set_gfp_mask(file->f_mapping,
39327 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
eca34b5c 39328@@ -727,6 +745,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
e1106418
JR
39329 * dependency.
39330 */
5527c038
JR
39331 fput(old_file);
39332+ if (old_virt_file)
39333+ fput(old_virt_file);
e1106418 39334 if (partscan)
c2c0f25c 39335 loop_reread_partitions(lo, bdev);
5527c038 39336 return 0;
eca34b5c 39337@@ -735,6 +755,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
e1106418
JR
39338 mutex_unlock(&loop_ctl_mutex);
39339 if (file)
39340 fput(file);
5527c038
JR
39341+ if (virt_file)
39342+ fput(virt_file);
5527c038
JR
39343 return error;
39344 }
e1106418 39345
eca34b5c 39346@@ -939,7 +961,7 @@ static void loop_update_rotational(struct loop_device *lo)
5527c038
JR
39347 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39348 struct block_device *bdev, unsigned int arg)
39349 {
062440b3 39350- struct file *file;
5527c038
JR
39351+ struct file *file, *f, *virt_file = NULL;
39352 struct inode *inode;
39353 struct address_space *mapping;
eca34b5c
AM
39354 struct block_device *claimed_bdev = NULL;
39355@@ -955,6 +977,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39356 file = fget(arg);
39357 if (!file)
39358 goto out;
39359+ f = loop_real_file(file);
39360+ if (f) {
39361+ virt_file = file;
39362+ file = f;
39363+ get_file(file);
39364+ }
39365
fbc438ed
JR
39366 /*
39367 * If we don't hold exclusive handle for the device, upgrade to it
eca34b5c 39368@@ -1003,6 +1031,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39369 lo->lo_device = bdev;
39370 lo->lo_flags = lo_flags;
39371 lo->lo_backing_file = file;
39372+ lo->lo_backing_virt_file = virt_file;
39373 lo->transfer = NULL;
39374 lo->ioctl = NULL;
39375 lo->lo_sizelimit = 0;
eca34b5c
AM
39376@@ -1046,6 +1075,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39377 bd_abort_claiming(bdev, claimed_bdev, loop_set_fd);
e1106418 39378 out_putf:
5527c038
JR
39379 fput(file);
39380+ if (virt_file)
39381+ fput(virt_file);
e1106418 39382 out:
5527c038
JR
39383 /* This is safe: open() is still holding a reference. */
39384 module_put(THIS_MODULE);
eca34b5c 39385@@ -1092,6 +1123,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
ba1aed25 39386 static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39387 {
e1106418 39388 struct file *filp = NULL;
5527c038
JR
39389+ struct file *virt_filp = lo->lo_backing_virt_file;
39390 gfp_t gfp = lo->old_gfp_mask;
39391 struct block_device *bdev = lo->lo_device;
ba1aed25 39392 int err = 0;
eca34b5c 39393@@ -1115,6 +1147,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39394
5527c038 39395 spin_lock_irq(&lo->lo_lock);
5527c038
JR
39396 lo->lo_backing_file = NULL;
39397+ lo->lo_backing_virt_file = NULL;
39398 spin_unlock_irq(&lo->lo_lock);
39399
39400 loop_release_xfer(lo);
eca34b5c 39401@@ -1197,6 +1230,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39402 */
e1106418
JR
39403 if (filp)
39404 fput(filp);
5527c038
JR
39405+ if (virt_filp)
39406+ fput(virt_filp);
e1106418 39407 return err;
5527c038
JR
39408 }
39409
39410diff --git a/drivers/block/loop.h b/drivers/block/loop.h
ba1aed25 39411index af75a5ee4094..1e6ee5a4f623 100644
5527c038
JR
39412--- a/drivers/block/loop.h
39413+++ b/drivers/block/loop.h
39414@@ -46,7 +46,7 @@ struct loop_device {
39415 int (*ioctl)(struct loop_device *, int cmd,
39416 unsigned long arg);
39417
39418- struct file * lo_backing_file;
39419+ struct file * lo_backing_file, *lo_backing_virt_file;
39420 struct block_device *lo_device;
5527c038 39421 void *key_data;
8b6a4947 39422
5527c038 39423diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
ba1aed25 39424index 0309f0d502ff..19feb4f3cb5f 100644
5527c038
JR
39425--- a/fs/aufs/f_op.c
39426+++ b/fs/aufs/f_op.c
acd2b654 39427@@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
39428 if (IS_ERR(h_file))
39429 goto out;
39430
39431- if (au_test_loopback_kthread()) {
39432+ if (0 && au_test_loopback_kthread()) {
39433 au_warn_loopback(h_file->f_path.dentry->d_sb);
39434 if (file->f_mapping != h_file->f_mapping) {
39435 file->f_mapping = h_file->f_mapping;
39436diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
fbc438ed 39437index 9ba35a878ecd..4ed0ff03d5ab 100644
5527c038
JR
39438--- a/fs/aufs/loop.c
39439+++ b/fs/aufs/loop.c
2121bcd9 39440@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 39441 symbol_put(loop_backing_file);
9f237c51 39442 au_kfree_try_rcu(au_warn_loopback_array);
5527c038
JR
39443 }
39444+
39445+/* ---------------------------------------------------------------------- */
39446+
39447+/* support the loopback block device insude aufs */
39448+
39449+struct file *aufs_real_loop(struct file *file)
39450+{
39451+ struct file *f;
39452+
39453+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39454+ fi_read_lock(file);
39455+ f = au_hf_top(file);
39456+ fi_read_unlock(file);
39457+ AuDebugOn(!f);
39458+ return f;
39459+}
39460diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
83b672a5 39461index f31e40aff267..e13fb1a0717a 100644
5527c038
JR
39462--- a/fs/aufs/loop.h
39463+++ b/fs/aufs/loop.h
eca801bf 39464@@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
39465
39466 int au_loopback_init(void);
39467 void au_loopback_fin(void);
39468+
39469+struct file *aufs_real_loop(struct file *file);
39470 #else
83b672a5 39471 AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
eca801bf
AM
39472
39473@@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
39474
39475 AuStubInt0(au_loopback_init, void)
39476 AuStubVoid(au_loopback_fin, void)
39477+
39478+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39479 #endif /* BLK_DEV_LOOP */
39480
39481 #endif /* __KERNEL__ */
39482diff --git a/fs/aufs/super.c b/fs/aufs/super.c
fbc438ed 39483index a97e2921cb09..f74eb6962684 100644
5527c038
JR
39484--- a/fs/aufs/super.c
39485+++ b/fs/aufs/super.c
fbc438ed 39486@@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
39487 .statfs = aufs_statfs,
39488 .put_super = aufs_put_super,
39489 .sync_fs = aufs_sync_fs,
39490- .remount_fs = aufs_remount_fs
39491+ .remount_fs = aufs_remount_fs,
39492+#ifdef CONFIG_AUFS_BDEV_LOOP
39493+ .real_loop = aufs_real_loop
39494+#endif
39495 };
39496
39497 /* ---------------------------------------------------------------------- */
39498diff --git a/include/linux/fs.h b/include/linux/fs.h
eca34b5c 39499index 3dbec51c2037..38ab7b6c7c6d 100644
5527c038
JR
39500--- a/include/linux/fs.h
39501+++ b/include/linux/fs.h
eca34b5c 39502@@ -1948,6 +1948,10 @@ struct super_operations {
5527c038
JR
39503 struct shrink_control *);
39504 long (*free_cached_objects)(struct super_block *,
39505 struct shrink_control *);
39506+#if defined(CONFIG_BLK_DEV_LOOP) || defined(CONFIG_BLK_DEV_LOOP_MODULE)
39507+ /* and aufs */
39508+ struct file *(*real_loop)(struct file *);
39509+#endif
39510 };
39511
39512 /*
This page took 7.866796 seconds and 4 git commands to generate.